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 */
696 int complex_args
= 0, parenthesized
= 0;
698 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
699 anything other than EQUAL or a comma? */
700 /* XXX Should NCH(n) check be made a separate check? */
701 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
702 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
705 assert(defaults
!= NULL
);
706 asdl_seq_SET(defaults
, j
++, expression
);
710 else if (found_default
) {
711 /* def f((x)=4): pass should raise an error.
712 def f((x, (y))): pass will just incur the tuple unpacking warning. */
713 if (parenthesized
&& !complex_args
) {
714 ast_error(n
, "parenthesized arg with default");
718 "non-default argument follows default argument");
723 /* def foo((x)): is not complex, special case. */
725 /* We have complex arguments, setup for unpacking. */
726 if (Py_Py3kWarningFlag
&& !ast_warn(c
, ch
,
727 "tuple parameter unpacking has been removed in 3.x"))
730 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
731 if (!asdl_seq_GET(args
, k
-1))
734 /* def foo((x)): setup for checking NAME below. */
735 /* Loop because there can be many parens and tuple
736 unpacking mixed in. */
739 assert(TYPE(ch
) == fpdef
);
743 if (TYPE(CHILD(ch
, 0)) == NAME
) {
746 if (!forbidden_check(c
, n
, STR(CHILD(ch
, 0))))
748 id
= NEW_IDENTIFIER(CHILD(ch
, 0));
751 name
= Name(id
, Param
, LINENO(ch
), ch
->n_col_offset
,
755 asdl_seq_SET(args
, k
++, name
);
758 i
+= 2; /* the name and the comma */
759 if (parenthesized
&& Py_Py3kWarningFlag
&&
760 !ast_warn(c
, ch
, "parenthesized argument names "
761 "are invalid in 3.x"))
767 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
769 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
775 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
777 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
783 PyErr_Format(PyExc_SystemError
,
784 "unexpected node in varargslist: %d @ %d",
790 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
799 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
803 int lineno
, col_offset
;
809 col_offset
= n
->n_col_offset
;
811 id
= NEW_IDENTIFIER(CHILD(n
, 0));
814 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
818 for (i
= 2; i
< NCH(n
); i
+=2) {
819 id
= NEW_IDENTIFIER(CHILD(n
, i
));
822 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
831 ast_for_decorator(struct compiling
*c
, const node
*n
)
833 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
838 REQ(CHILD(n
, 0), AT
);
839 REQ(RCHILD(n
, -1), NEWLINE
);
841 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
845 if (NCH(n
) == 3) { /* No arguments */
849 else if (NCH(n
) == 5) { /* Call with no arguments */
850 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
851 n
->n_col_offset
, c
->c_arena
);
857 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
867 ast_for_decorators(struct compiling
*c
, const node
*n
)
869 asdl_seq
* decorator_seq
;
874 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
878 for (i
= 0; i
< NCH(n
); i
++) {
879 d
= ast_for_decorator(c
, CHILD(n
, i
));
882 asdl_seq_SET(decorator_seq
, i
, d
);
884 return decorator_seq
;
888 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
890 /* funcdef: 'def' NAME parameters ':' suite */
898 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
901 else if (!forbidden_check(c
, CHILD(n
, name_i
), STR(CHILD(n
, name_i
))))
903 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
906 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
910 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
911 n
->n_col_offset
, c
->c_arena
);
915 ast_for_decorated(struct compiling
*c
, const node
*n
)
917 /* decorated: decorators (classdef | funcdef) */
918 stmt_ty thing
= NULL
;
919 asdl_seq
*decorator_seq
= NULL
;
923 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
927 assert(TYPE(CHILD(n
, 1)) == funcdef
||
928 TYPE(CHILD(n
, 1)) == classdef
);
930 if (TYPE(CHILD(n
, 1)) == funcdef
) {
931 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
932 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
933 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
935 /* we count the decorators in when talking about the class' or
936 function's line number */
938 thing
->lineno
= LINENO(n
);
939 thing
->col_offset
= n
->n_col_offset
;
945 ast_for_lambdef(struct compiling
*c
, const node
*n
)
947 /* lambdef: 'lambda' [varargslist] ':' test */
952 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
955 expression
= ast_for_expr(c
, CHILD(n
, 2));
960 args
= ast_for_arguments(c
, CHILD(n
, 1));
963 expression
= ast_for_expr(c
, CHILD(n
, 3));
968 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
972 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
974 /* test: or_test 'if' or_test 'else' test */
975 expr_ty expression
, body
, orelse
;
978 body
= ast_for_expr(c
, CHILD(n
, 0));
981 expression
= ast_for_expr(c
, CHILD(n
, 2));
984 orelse
= ast_for_expr(c
, CHILD(n
, 4));
987 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
991 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
992 so there is only a single version. Possibly for loops can also re-use
996 /* Count the number of 'for' loop in a list comprehension.
998 Helper for ast_for_listcomp().
1002 count_list_fors(struct compiling
*c
, const node
*n
)
1005 node
*ch
= CHILD(n
, 1);
1017 if (TYPE(ch
) == list_for
)
1018 goto count_list_for
;
1019 else if (TYPE(ch
) == list_if
) {
1022 goto count_list_iter
;
1028 /* Should never be reached */
1029 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
1033 /* Count the number of 'if' statements in a list comprehension.
1035 Helper for ast_for_listcomp().
1039 count_list_ifs(struct compiling
*c
, const node
*n
)
1045 if (TYPE(CHILD(n
, 0)) == list_for
)
1053 goto count_list_iter
;
1057 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1059 /* listmaker: test ( list_for | (',' test)* [','] )
1060 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1061 list_iter: list_for | list_if
1062 list_if: 'if' test [list_iter]
1063 testlist_safe: test [(',' test)+ [',']]
1066 asdl_seq
*listcomps
;
1073 elt
= ast_for_expr(c
, CHILD(n
, 0));
1077 n_fors
= count_list_fors(c
, n
);
1081 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1086 for (i
= 0; i
< n_fors
; i
++) {
1087 comprehension_ty lc
;
1094 for_ch
= CHILD(ch
, 1);
1095 t
= ast_for_exprlist(c
, for_ch
, Store
);
1098 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1102 /* Check the # of children rather than the length of t, since
1103 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1105 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1106 if (NCH(for_ch
) == 1)
1107 lc
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1109 lc
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1111 expression
, NULL
, c
->c_arena
);
1118 expr_ty list_for_expr
;
1121 n_ifs
= count_list_ifs(c
, ch
);
1125 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1129 for (j
= 0; j
< n_ifs
; j
++) {
1134 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1138 asdl_seq_SET(ifs
, j
, list_for_expr
);
1142 /* on exit, must guarantee that ch is a list_for */
1143 if (TYPE(ch
) == list_iter
)
1147 asdl_seq_SET(listcomps
, i
, lc
);
1150 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1153 /* Count the number of 'for' loops in a generator expression.
1155 Helper for ast_for_genexp().
1159 count_gen_fors(struct compiling
*c
, const node
*n
)
1162 node
*ch
= CHILD(n
, 1);
1174 if (TYPE(ch
) == gen_for
)
1176 else if (TYPE(ch
) == gen_if
) {
1179 goto count_gen_iter
;
1185 /* Should never be reached */
1186 PyErr_SetString(PyExc_SystemError
,
1187 "logic error in count_gen_fors");
1191 /* Count the number of 'if' statements in a generator expression.
1193 Helper for ast_for_genexp().
1197 count_gen_ifs(struct compiling
*c
, const node
*n
)
1203 if (TYPE(CHILD(n
, 0)) == gen_for
)
1214 /* TODO(jhylton): Combine with list comprehension code? */
1216 ast_for_genexp(struct compiling
*c
, const node
*n
)
1218 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1219 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1225 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1228 elt
= ast_for_expr(c
, CHILD(n
, 0));
1232 n_fors
= count_gen_fors(c
, n
);
1236 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1241 for (i
= 0; i
< n_fors
; i
++) {
1242 comprehension_ty ge
;
1244 expr_ty expression
, first
;
1249 for_ch
= CHILD(ch
, 1);
1250 t
= ast_for_exprlist(c
, for_ch
, Store
);
1253 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1257 /* Check the # of children rather than the length of t, since
1258 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1259 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1260 if (NCH(for_ch
) == 1)
1261 ge
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1263 ge
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1265 expression
, NULL
, c
->c_arena
);
1275 n_ifs
= count_gen_ifs(c
, ch
);
1279 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1283 for (j
= 0; j
< n_ifs
; j
++) {
1288 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1291 asdl_seq_SET(ifs
, j
, expression
);
1295 /* on exit, must guarantee that ch is a gen_for */
1296 if (TYPE(ch
) == gen_iter
)
1300 asdl_seq_SET(genexps
, i
, ge
);
1303 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1307 ast_for_atom(struct compiling
*c
, const node
*n
)
1309 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1310 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1312 node
*ch
= CHILD(n
, 0);
1316 /* All names start in Load context, but may later be
1318 PyObject
*name
= NEW_IDENTIFIER(ch
);
1321 return Name(name
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1324 PyObject
*str
= parsestrplus(c
, n
);
1326 #ifdef Py_USING_UNICODE
1327 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1328 PyObject
*type
, *value
, *tback
, *errstr
;
1329 PyErr_Fetch(&type
, &value
, &tback
);
1330 errstr
= PyObject_Str(value
);
1334 s
= PyString_AsString(errstr
);
1335 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1339 ast_error(n
, "(unicode error) unknown error");
1348 PyArena_AddPyObject(c
->c_arena
, str
);
1349 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1352 PyObject
*pynum
= parsenumber(c
, STR(ch
));
1356 PyArena_AddPyObject(c
->c_arena
, pynum
);
1357 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1359 case LPAR
: /* some parenthesized expressions */
1362 if (TYPE(ch
) == RPAR
)
1363 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1365 if (TYPE(ch
) == yield_expr
)
1366 return ast_for_expr(c
, ch
);
1368 return ast_for_testlist_gexp(c
, ch
);
1369 case LSQB
: /* list (or list comprehension) */
1372 if (TYPE(ch
) == RSQB
)
1373 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1376 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1377 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1381 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1384 return ast_for_listcomp(c
, ch
);
1386 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1388 asdl_seq
*keys
, *values
;
1391 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1392 keys
= asdl_seq_new(size
, c
->c_arena
);
1396 values
= asdl_seq_new(size
, c
->c_arena
);
1400 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1403 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1407 asdl_seq_SET(keys
, i
/ 4, expression
);
1409 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1413 asdl_seq_SET(values
, i
/ 4, expression
);
1415 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1417 case BACKQUOTE
: { /* repr */
1419 if (Py_Py3kWarningFlag
&&
1420 !ast_warn(c
, n
, "backquote not supported in 3.x; use repr()"))
1422 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1426 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1429 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1435 ast_for_slice(struct compiling
*c
, const node
*n
)
1438 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1443 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1447 if (TYPE(ch
) == DOT
)
1448 return Ellipsis(c
->c_arena
);
1450 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1451 /* 'step' variable hold no significance in terms of being used over
1453 step
= ast_for_expr(c
, ch
);
1457 return Index(step
, c
->c_arena
);
1460 if (TYPE(ch
) == test
) {
1461 lower
= ast_for_expr(c
, ch
);
1466 /* If there's an upper bound it's in the second or third position. */
1467 if (TYPE(ch
) == COLON
) {
1469 node
*n2
= CHILD(n
, 1);
1471 if (TYPE(n2
) == test
) {
1472 upper
= ast_for_expr(c
, n2
);
1477 } else if (NCH(n
) > 2) {
1478 node
*n2
= CHILD(n
, 2);
1480 if (TYPE(n2
) == test
) {
1481 upper
= ast_for_expr(c
, n2
);
1487 ch
= CHILD(n
, NCH(n
) - 1);
1488 if (TYPE(ch
) == sliceop
) {
1491 This is an extended slice (ie "x[::]") with no expression in the
1492 step field. We set this literally to "None" in order to
1493 disambiguate it from x[:]. (The interpreter might have to call
1494 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1496 identifier none
= new_identifier("None", c
->c_arena
);
1500 step
= Name(none
, Load
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1505 if (TYPE(ch
) == test
) {
1506 step
= ast_for_expr(c
, ch
);
1513 return Slice(lower
, upper
, step
, c
->c_arena
);
1517 ast_for_binop(struct compiling
*c
, const node
*n
)
1519 /* Must account for a sequence of expressions.
1520 How should A op B op C by represented?
1521 BinOp(BinOp(A, op, B), op, C).
1525 expr_ty expr1
, expr2
, result
;
1526 operator_ty newoperator
;
1528 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1532 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1536 newoperator
= get_operator(CHILD(n
, 1));
1540 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1545 nops
= (NCH(n
) - 1) / 2;
1546 for (i
= 1; i
< nops
; i
++) {
1547 expr_ty tmp_result
, tmp
;
1548 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1550 newoperator
= get_operator(next_oper
);
1554 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1558 tmp_result
= BinOp(result
, newoperator
, tmp
,
1559 LINENO(next_oper
), next_oper
->n_col_offset
,
1563 result
= tmp_result
;
1569 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1571 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1572 subscriptlist: subscript (',' subscript)* [',']
1573 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1576 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1578 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1579 n
->n_col_offset
, c
->c_arena
);
1581 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1583 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1584 PyObject
*attr_id
= NEW_IDENTIFIER(CHILD(n
, 1));
1587 return Attribute(left_expr
, attr_id
, Load
,
1588 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1591 REQ(CHILD(n
, 0), LSQB
);
1592 REQ(CHILD(n
, 2), RSQB
);
1595 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1598 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1602 /* The grammar is ambiguous here. The ambiguity is resolved
1603 by treating the sequence as a tuple literal if there are
1610 asdl_seq
*slices
, *elts
;
1611 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1614 for (j
= 0; j
< NCH(n
); j
+= 2) {
1615 slc
= ast_for_slice(c
, CHILD(n
, j
));
1618 if (slc
->kind
!= Index_kind
)
1620 asdl_seq_SET(slices
, j
/ 2, slc
);
1623 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1624 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1626 /* extract Index values and put them in a Tuple */
1627 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1630 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1631 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1632 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1633 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1635 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1638 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1639 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1645 ast_for_factor(struct compiling
*c
, const node
*n
)
1647 node
*pfactor
, *ppower
, *patom
, *pnum
;
1650 /* If the unary - operator is applied to a constant, don't generate
1651 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1652 constant. The peephole optimizer already does something like
1653 this but it doesn't handle the case where the constant is
1654 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1657 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1659 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1660 NCH(pfactor
) == 1 &&
1661 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1663 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1664 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1665 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1669 strcpy(s
+ 1, STR(pnum
));
1670 PyObject_FREE(STR(pnum
));
1672 return ast_for_atom(c
, patom
);
1675 expression
= ast_for_expr(c
, CHILD(n
, 1));
1679 switch (TYPE(CHILD(n
, 0))) {
1681 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1684 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1687 return UnaryOp(Invert
, expression
, LINENO(n
),
1688 n
->n_col_offset
, c
->c_arena
);
1690 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1696 ast_for_power(struct compiling
*c
, const node
*n
)
1698 /* power: atom trailer* ('**' factor)*
1703 e
= ast_for_atom(c
, CHILD(n
, 0));
1708 for (i
= 1; i
< NCH(n
); i
++) {
1709 node
*ch
= CHILD(n
, i
);
1710 if (TYPE(ch
) != trailer
)
1712 tmp
= ast_for_trailer(c
, ch
, e
);
1715 tmp
->lineno
= e
->lineno
;
1716 tmp
->col_offset
= e
->col_offset
;
1719 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1720 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1723 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1731 /* Do not name a variable 'expr'! Will cause a compile error.
1735 ast_for_expr(struct compiling
*c
, const node
*n
)
1737 /* handle the full range of simple expressions
1738 test: or_test ['if' or_test 'else' test] | lambdef
1739 or_test: and_test ('or' and_test)*
1740 and_test: not_test ('and' not_test)*
1741 not_test: 'not' not_test | comparison
1742 comparison: expr (comp_op expr)*
1743 expr: xor_expr ('|' xor_expr)*
1744 xor_expr: and_expr ('^' and_expr)*
1745 and_expr: shift_expr ('&' shift_expr)*
1746 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1747 arith_expr: term (('+'|'-') term)*
1748 term: factor (('*'|'/'|'%'|'//') factor)*
1749 factor: ('+'|'-'|'~') factor | power
1750 power: atom trailer* ('**' factor)*
1752 As well as modified versions that exist for backward compatibility,
1753 to explicitly allow:
1754 [ x for x in lambda: 0, lambda: 1 ]
1755 (which would be ambiguous without these extra rules)
1757 old_test: or_test | old_lambdef
1758 old_lambdef: 'lambda' [vararglist] ':' old_test
1769 if (TYPE(CHILD(n
, 0)) == lambdef
||
1770 TYPE(CHILD(n
, 0)) == old_lambdef
)
1771 return ast_for_lambdef(c
, CHILD(n
, 0));
1772 else if (NCH(n
) > 1)
1773 return ast_for_ifexpr(c
, n
);
1781 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1784 for (i
= 0; i
< NCH(n
); i
+= 2) {
1785 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1788 asdl_seq_SET(seq
, i
/ 2, e
);
1790 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1791 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1793 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1794 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1801 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1805 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1817 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1820 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1824 for (i
= 1; i
< NCH(n
); i
+= 2) {
1825 cmpop_ty newoperator
;
1827 newoperator
= ast_for_comp_op(c
, CHILD(n
, i
));
1832 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1837 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1838 asdl_seq_SET(cmps
, i
/ 2, expression
);
1840 expression
= ast_for_expr(c
, CHILD(n
, 0));
1845 return Compare(expression
, ops
, cmps
, LINENO(n
),
1846 n
->n_col_offset
, c
->c_arena
);
1850 /* The next five cases all handle BinOps. The main body of code
1851 is the same in each case, but the switch turned inside out to
1852 reuse the code for each type of operator.
1864 return ast_for_binop(c
, n
);
1868 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1872 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1879 return ast_for_factor(c
, n
);
1881 return ast_for_power(c
, n
);
1883 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1886 /* should never get here unless if error is set */
1891 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1894 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1896 argument: [test '='] test [gen_for] # Really [keyword '='] test
1899 int i
, nargs
, nkeywords
, ngens
;
1902 expr_ty vararg
= NULL
, kwarg
= NULL
;
1909 for (i
= 0; i
< NCH(n
); i
++) {
1910 node
*ch
= CHILD(n
, i
);
1911 if (TYPE(ch
) == argument
) {
1914 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1920 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1921 ast_error(n
, "Generator expression must be parenthesized "
1922 "if not sole argument");
1926 if (nargs
+ nkeywords
+ ngens
> 255) {
1927 ast_error(n
, "more than 255 arguments");
1931 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1934 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1939 for (i
= 0; i
< NCH(n
); i
++) {
1940 node
*ch
= CHILD(n
, i
);
1941 if (TYPE(ch
) == argument
) {
1945 ast_error(CHILD(ch
, 0),
1946 "non-keyword arg after keyword arg");
1950 ast_error(CHILD(ch
, 0),
1951 "only named arguments may follow *expression");
1954 e
= ast_for_expr(c
, CHILD(ch
, 0));
1957 asdl_seq_SET(args
, nargs
++, e
);
1959 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1960 e
= ast_for_genexp(c
, ch
);
1963 asdl_seq_SET(args
, nargs
++, e
);
1971 /* CHILD(ch, 0) is test, but must be an identifier? */
1972 e
= ast_for_expr(c
, CHILD(ch
, 0));
1975 /* f(lambda x: x[0] = 3) ends up getting parsed with
1976 * LHS test = lambda x: x[0], and RHS test = 3.
1977 * SF bug 132313 points out that complaining about a keyword
1978 * then is very confusing.
1980 if (e
->kind
== Lambda_kind
) {
1981 ast_error(CHILD(ch
, 0),
1982 "lambda cannot contain assignment");
1984 } else if (e
->kind
!= Name_kind
) {
1985 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1989 if (!forbidden_check(c
, CHILD(ch
, 0), PyBytes_AS_STRING(key
)))
1991 for (k
= 0; k
< nkeywords
; k
++) {
1992 tmp
= PyString_AS_STRING(
1993 ((keyword_ty
)asdl_seq_GET(keywords
, k
))->arg
);
1994 if (!strcmp(tmp
, PyString_AS_STRING(key
))) {
1995 ast_error(CHILD(ch
, 0), "keyword argument repeated");
1999 e
= ast_for_expr(c
, CHILD(ch
, 2));
2002 kw
= keyword(key
, e
, c
->c_arena
);
2005 asdl_seq_SET(keywords
, nkeywords
++, kw
);
2008 else if (TYPE(ch
) == STAR
) {
2009 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
2014 else if (TYPE(ch
) == DOUBLESTAR
) {
2015 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
2022 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
2023 func
->col_offset
, c
->c_arena
);
2027 ast_for_testlist(struct compiling
*c
, const node
* n
)
2029 /* testlist_gexp: test (',' test)* [','] */
2030 /* testlist: test (',' test)* [','] */
2031 /* testlist_safe: test (',' test)+ [','] */
2032 /* testlist1: test (',' test)* */
2034 if (TYPE(n
) == testlist_gexp
) {
2036 assert(TYPE(CHILD(n
, 1)) != gen_for
);
2039 assert(TYPE(n
) == testlist
||
2040 TYPE(n
) == testlist_safe
||
2041 TYPE(n
) == testlist1
);
2044 return ast_for_expr(c
, CHILD(n
, 0));
2046 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
2049 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2054 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
2056 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2057 /* argument: test [ gen_for ] */
2058 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
2059 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
2060 return ast_for_genexp(c
, n
);
2061 return ast_for_testlist(c
, n
);
2064 /* like ast_for_testlist() but returns a sequence */
2066 ast_for_class_bases(struct compiling
*c
, const node
* n
)
2068 /* testlist: test (',' test)* [','] */
2073 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
2076 base
= ast_for_expr(c
, CHILD(n
, 0));
2079 asdl_seq_SET(bases
, 0, base
);
2083 return seq_for_testlist(c
, n
);
2087 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2090 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2091 | ('=' (yield_expr|testlist))*)
2092 testlist: test (',' test)* [',']
2093 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2094 | '<<=' | '>>=' | '**=' | '//='
2095 test: ... here starts the operator precendence dance
2099 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2103 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2105 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2106 expr_ty expr1
, expr2
;
2107 operator_ty newoperator
;
2108 node
*ch
= CHILD(n
, 0);
2110 expr1
= ast_for_testlist(c
, ch
);
2113 if(!set_context(c
, expr1
, Store
, ch
))
2115 /* set_context checks that most expressions are not the left side.
2116 Augmented assignments can only have a name, a subscript, or an
2117 attribute on the left, though, so we have to explicitly check for
2119 switch (expr1
->kind
) {
2121 case Attribute_kind
:
2122 case Subscript_kind
:
2125 ast_error(ch
, "illegal expression for augmented assignment");
2130 if (TYPE(ch
) == testlist
)
2131 expr2
= ast_for_testlist(c
, ch
);
2133 expr2
= ast_for_expr(c
, ch
);
2137 newoperator
= ast_for_augassign(c
, CHILD(n
, 1));
2141 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2150 /* a normal assignment */
2151 REQ(CHILD(n
, 1), EQUAL
);
2152 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2155 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2157 node
*ch
= CHILD(n
, i
);
2158 if (TYPE(ch
) == yield_expr
) {
2159 ast_error(ch
, "assignment to yield expression not possible");
2162 e
= ast_for_testlist(c
, ch
);
2164 /* set context to assign */
2168 if (!set_context(c
, e
, Store
, CHILD(n
, i
)))
2171 asdl_seq_SET(targets
, i
/ 2, e
);
2173 value
= CHILD(n
, NCH(n
) - 1);
2174 if (TYPE(value
) == testlist
)
2175 expression
= ast_for_testlist(c
, value
);
2177 expression
= ast_for_expr(c
, value
);
2180 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2186 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2188 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2189 | '>>' test [ (',' test)+ [','] ] )
2191 expr_ty dest
= NULL
, expression
;
2192 asdl_seq
*seq
= NULL
;
2194 int i
, j
, values_count
, start
= 1;
2197 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2198 dest
= ast_for_expr(c
, CHILD(n
, 2));
2203 values_count
= (NCH(n
) + 1 - start
) / 2;
2205 seq
= asdl_seq_new(values_count
, c
->c_arena
);
2208 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2209 expression
= ast_for_expr(c
, CHILD(n
, i
));
2212 asdl_seq_SET(seq
, j
, expression
);
2215 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2216 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2220 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2228 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2231 for (i
= 0; i
< NCH(n
); i
+= 2) {
2232 e
= ast_for_expr(c
, CHILD(n
, i
));
2235 asdl_seq_SET(seq
, i
/ 2, e
);
2236 if (context
&& !set_context(c
, e
, context
, CHILD(n
, i
)))
2243 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2245 asdl_seq
*expr_list
;
2247 /* del_stmt: 'del' exprlist */
2250 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2253 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2257 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2260 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2263 continue_stmt: 'continue'
2264 return_stmt: 'return' [testlist]
2265 yield_stmt: yield_expr
2266 yield_expr: 'yield' testlist
2267 raise_stmt: 'raise' [test [',' test [',' test]]]
2275 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2277 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2278 case yield_stmt
: { /* will reduce to yield_expr */
2279 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2282 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2286 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2288 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2291 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2296 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2298 else if (NCH(ch
) == 2) {
2299 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2302 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2303 n
->n_col_offset
, c
->c_arena
);
2305 else if (NCH(ch
) == 4) {
2306 expr_ty expr1
, expr2
;
2308 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2311 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2315 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2318 else if (NCH(ch
) == 6) {
2319 expr_ty expr1
, expr2
, expr3
;
2321 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2324 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2327 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2331 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2335 PyErr_Format(PyExc_SystemError
,
2336 "unexpected flow_stmt: %d", TYPE(ch
));
2340 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2345 alias_for_import_name(struct compiling
*c
, const node
*n
, int store
)
2348 import_as_name: NAME ['as' NAME]
2349 dotted_as_name: dotted_name ['as' NAME]
2350 dotted_name: NAME ('.' NAME)*
2352 PyObject
*str
, *name
;
2356 case import_as_name
: {
2357 node
*name_node
= CHILD(n
, 0);
2360 node
*str_node
= CHILD(n
, 2);
2361 if (store
&& !forbidden_check(c
, str_node
, STR(str_node
)))
2363 str
= NEW_IDENTIFIER(str_node
);
2368 if (!forbidden_check(c
, name_node
, STR(name_node
)))
2371 name
= NEW_IDENTIFIER(name_node
);
2374 return alias(name
, str
, c
->c_arena
);
2376 case dotted_as_name
:
2382 node
*asname_node
= CHILD(n
, 2);
2383 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0), 0);
2387 if (!forbidden_check(c
, asname_node
, STR(asname_node
)))
2389 a
->asname
= NEW_IDENTIFIER(asname_node
);
2397 node
*name_node
= CHILD(n
, 0);
2398 if (store
&& !forbidden_check(c
, name_node
, STR(name_node
)))
2400 name
= NEW_IDENTIFIER(name_node
);
2403 return alias(name
, NULL
, c
->c_arena
);
2406 /* Create a string of the form "a.b.c" */
2412 for (i
= 0; i
< NCH(n
); i
+= 2)
2413 /* length of string plus one for the dot */
2414 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2415 len
--; /* the last name doesn't have a dot */
2416 str
= PyString_FromStringAndSize(NULL
, len
);
2419 s
= PyString_AS_STRING(str
);
2422 for (i
= 0; i
< NCH(n
); i
+= 2) {
2423 char *sch
= STR(CHILD(n
, i
));
2424 strcpy(s
, STR(CHILD(n
, i
)));
2430 PyString_InternInPlace(&str
);
2431 PyArena_AddPyObject(c
->c_arena
, str
);
2432 return alias(str
, NULL
, c
->c_arena
);
2436 str
= PyString_InternFromString("*");
2437 PyArena_AddPyObject(c
->c_arena
, str
);
2438 return alias(str
, NULL
, c
->c_arena
);
2440 PyErr_Format(PyExc_SystemError
,
2441 "unexpected import name: %d", TYPE(n
));
2445 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2450 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2453 import_stmt: import_name | import_from
2454 import_name: 'import' dotted_as_names
2455 import_from: 'from' ('.'* dotted_name | '.') 'import'
2456 ('*' | '(' import_as_names ')' | import_as_names)
2463 REQ(n
, import_stmt
);
2465 col_offset
= n
->n_col_offset
;
2467 if (TYPE(n
) == import_name
) {
2469 REQ(n
, dotted_as_names
);
2470 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2473 for (i
= 0; i
< NCH(n
); i
+= 2) {
2474 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2477 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2479 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2481 else if (TYPE(n
) == import_from
) {
2484 alias_ty mod
= NULL
;
2485 identifier modname
= NULL
;
2487 /* Count the number of dots (for relative imports) and check for the
2488 optional module name */
2489 for (idx
= 1; idx
< NCH(n
); idx
++) {
2490 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2491 mod
= alias_for_import_name(c
, CHILD(n
, idx
), 0);
2496 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2501 idx
++; /* skip over the 'import' keyword */
2502 switch (TYPE(CHILD(n
, idx
))) {
2504 /* from ... import * */
2509 /* from ... import (x, y, z) */
2510 n
= CHILD(n
, idx
+ 1);
2511 n_children
= NCH(n
);
2513 case import_as_names
:
2514 /* from ... import x, y, z */
2516 n_children
= NCH(n
);
2517 if (n_children
% 2 == 0) {
2518 ast_error(n
, "trailing comma not allowed without"
2519 " surrounding parentheses");
2524 ast_error(n
, "Unexpected node-type in from-import");
2528 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2532 /* handle "from ... import *" special b/c there's no children */
2533 if (TYPE(n
) == STAR
) {
2534 alias_ty import_alias
= alias_for_import_name(c
, n
, 1);
2537 asdl_seq_SET(aliases
, 0, import_alias
);
2540 for (i
= 0; i
< NCH(n
); i
+= 2) {
2541 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2544 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2548 modname
= mod
->name
;
2549 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2552 PyErr_Format(PyExc_SystemError
,
2553 "unknown import statement: starts with command '%s'",
2559 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2561 /* global_stmt: 'global' NAME (',' NAME)* */
2566 REQ(n
, global_stmt
);
2567 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2570 for (i
= 1; i
< NCH(n
); i
+= 2) {
2571 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2574 asdl_seq_SET(s
, i
/ 2, name
);
2576 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2580 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2582 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2583 int n_children
= NCH(n
);
2584 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2585 PyErr_Format(PyExc_SystemError
,
2586 "poorly formed 'exec' statement: %d parts to statement",
2591 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2593 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2596 if (n_children
>= 4) {
2597 globals
= ast_for_expr(c
, CHILD(n
, 3));
2601 if (n_children
== 6) {
2602 locals
= ast_for_expr(c
, CHILD(n
, 5));
2607 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2612 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2614 /* assert_stmt: 'assert' test [',' test] */
2615 REQ(n
, assert_stmt
);
2617 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2620 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2623 else if (NCH(n
) == 4) {
2624 expr_ty expr1
, expr2
;
2626 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2629 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2633 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2635 PyErr_Format(PyExc_SystemError
,
2636 "improper number of parts to 'assert' statement: %d",
2642 ast_for_suite(struct compiling
*c
, const node
*n
)
2644 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2647 int i
, total
, num
, end
, pos
= 0;
2652 total
= num_stmts(n
);
2653 seq
= asdl_seq_new(total
, c
->c_arena
);
2656 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2658 /* simple_stmt always ends with a NEWLINE,
2659 and may have a trailing SEMI
2662 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2664 /* loop by 2 to skip semi-colons */
2665 for (i
= 0; i
< end
; i
+= 2) {
2667 s
= ast_for_stmt(c
, ch
);
2670 asdl_seq_SET(seq
, pos
++, s
);
2674 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2677 num
= num_stmts(ch
);
2679 /* small_stmt or compound_stmt with only one child */
2680 s
= ast_for_stmt(c
, ch
);
2683 asdl_seq_SET(seq
, pos
++, s
);
2688 REQ(ch
, simple_stmt
);
2689 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2690 /* statement terminates with a semi-colon ';' */
2691 if (NCH(CHILD(ch
, j
)) == 0) {
2692 assert((j
+ 1) == NCH(ch
));
2695 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2698 asdl_seq_SET(seq
, pos
++, s
);
2703 assert(pos
== seq
->size
);
2708 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2710 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2719 asdl_seq
*suite_seq
;
2721 expression
= ast_for_expr(c
, CHILD(n
, 1));
2724 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2728 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2732 s
= STR(CHILD(n
, 4));
2733 /* s[2], the third character in the string, will be
2739 asdl_seq
*seq1
, *seq2
;
2741 expression
= ast_for_expr(c
, CHILD(n
, 1));
2744 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2747 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2751 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2754 else if (s
[2] == 'i') {
2755 int i
, n_elif
, has_else
= 0;
2757 asdl_seq
*suite_seq
;
2758 asdl_seq
*orelse
= NULL
;
2759 n_elif
= NCH(n
) - 4;
2760 /* must reference the child n_elif+1 since 'else' token is third,
2761 not fourth, child from the end. */
2762 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2763 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2770 asdl_seq
*suite_seq2
;
2772 orelse
= asdl_seq_new(1, c
->c_arena
);
2775 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2778 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2781 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2785 asdl_seq_SET(orelse
, 0,
2786 If(expression
, suite_seq
, suite_seq2
,
2787 LINENO(CHILD(n
, NCH(n
) - 6)),
2788 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2790 /* the just-created orelse handled the last elif */
2794 for (i
= 0; i
< n_elif
; i
++) {
2795 int off
= 5 + (n_elif
- i
- 1) * 4;
2796 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2799 expression
= ast_for_expr(c
, CHILD(n
, off
));
2802 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2806 asdl_seq_SET(newobj
, 0,
2807 If(expression
, suite_seq
, orelse
,
2808 LINENO(CHILD(n
, off
)),
2809 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2812 expression
= ast_for_expr(c
, CHILD(n
, 1));
2815 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2818 return If(expression
, suite_seq
, orelse
,
2819 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2822 PyErr_Format(PyExc_SystemError
,
2823 "unexpected token in 'if' statement: %s", s
);
2828 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2830 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2835 asdl_seq
*suite_seq
;
2837 expression
= ast_for_expr(c
, CHILD(n
, 1));
2840 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2843 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2846 else if (NCH(n
) == 7) {
2848 asdl_seq
*seq1
, *seq2
;
2850 expression
= ast_for_expr(c
, CHILD(n
, 1));
2853 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2856 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2860 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2864 PyErr_Format(PyExc_SystemError
,
2865 "wrong number of tokens for 'while' statement: %d",
2871 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2873 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2875 expr_ty target
, first
;
2876 const node
*node_target
;
2877 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2881 seq
= ast_for_suite(c
, CHILD(n
, 8));
2886 node_target
= CHILD(n
, 1);
2887 _target
= ast_for_exprlist(c
, node_target
, Store
);
2890 /* Check the # of children rather than the length of _target, since
2891 for x, in ... has 1 element in _target, but still requires a Tuple. */
2892 first
= (expr_ty
)asdl_seq_GET(_target
, 0);
2893 if (NCH(node_target
) == 1)
2896 target
= Tuple(_target
, Store
, first
->lineno
, first
->col_offset
, c
->c_arena
);
2898 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2901 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2905 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2909 static excepthandler_ty
2910 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2912 /* except_clause: 'except' [test [(',' | 'as') test]] */
2913 REQ(exc
, except_clause
);
2916 if (NCH(exc
) == 1) {
2917 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2921 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2922 exc
->n_col_offset
, c
->c_arena
);
2924 else if (NCH(exc
) == 2) {
2926 asdl_seq
*suite_seq
;
2928 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2931 suite_seq
= ast_for_suite(c
, body
);
2935 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2936 exc
->n_col_offset
, c
->c_arena
);
2938 else if (NCH(exc
) == 4) {
2939 asdl_seq
*suite_seq
;
2941 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2944 if (!set_context(c
, e
, Store
, CHILD(exc
, 3)))
2946 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2949 suite_seq
= ast_for_suite(c
, body
);
2953 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
2954 exc
->n_col_offset
, c
->c_arena
);
2957 PyErr_Format(PyExc_SystemError
,
2958 "wrong number of children for 'except' clause: %d",
2964 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2966 const int nch
= NCH(n
);
2967 int n_except
= (nch
- 3)/3;
2968 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2972 body
= ast_for_suite(c
, CHILD(n
, 2));
2976 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2977 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2978 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2979 /* we can assume it's an "else",
2980 because nch >= 9 for try-else-finally and
2981 it would otherwise have a type of except_clause */
2982 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2988 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2989 if (finally
== NULL
)
2994 /* we can assume it's an "else",
2995 otherwise it would have a type of except_clause */
2996 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
3002 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
3003 ast_error(n
, "malformed 'try' statement");
3010 /* process except statements to create a try ... except */
3011 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
3012 if (handlers
== NULL
)
3015 for (i
= 0; i
< n_except
; i
++) {
3016 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
3017 CHILD(n
, 5 + i
* 3));
3020 asdl_seq_SET(handlers
, i
, e
);
3023 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
3024 n
->n_col_offset
, c
->c_arena
);
3028 /* if a 'finally' is present too, we nest the TryExcept within a
3029 TryFinally to emulate try ... except ... finally */
3030 body
= asdl_seq_new(1, c
->c_arena
);
3033 asdl_seq_SET(body
, 0, except_st
);
3036 /* must be a try ... finally (except clauses are in body, if any exist) */
3037 assert(finally
!= NULL
);
3038 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3041 /* with_item: test ['as' expr] */
3043 ast_for_with_item(struct compiling
*c
, const node
*n
, asdl_seq
*content
)
3045 expr_ty context_expr
, optional_vars
= NULL
;
3048 context_expr
= ast_for_expr(c
, CHILD(n
, 0));
3052 optional_vars
= ast_for_expr(c
, CHILD(n
, 2));
3054 if (!optional_vars
) {
3057 if (!set_context(c
, optional_vars
, Store
, n
)) {
3062 return With(context_expr
, optional_vars
, content
, LINENO(n
),
3063 n
->n_col_offset
, c
->c_arena
);
3066 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3068 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
3076 /* process the with items inside-out */
3078 /* the suite of the innermost with item is the suite of the with stmt */
3079 inner
= ast_for_suite(c
, CHILD(n
, i
));
3085 ret
= ast_for_with_item(c
, CHILD(n
, i
), inner
);
3088 /* was this the last item? */
3091 /* if not, wrap the result so far in a new sequence */
3092 inner
= asdl_seq_new(1, c
->c_arena
);
3095 asdl_seq_SET(inner
, 0, ret
);
3102 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
3104 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3105 PyObject
*classname
;
3106 asdl_seq
*bases
, *s
;
3110 if (!forbidden_check(c
, n
, STR(CHILD(n
, 1))))
3114 s
= ast_for_suite(c
, CHILD(n
, 3));
3117 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3120 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3121 n
->n_col_offset
, c
->c_arena
);
3123 /* check for empty base list */
3124 if (TYPE(CHILD(n
,3)) == RPAR
) {
3125 s
= ast_for_suite(c
, CHILD(n
,5));
3128 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3131 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3132 n
->n_col_offset
, c
->c_arena
);
3135 /* else handle the base class list */
3136 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3140 s
= ast_for_suite(c
, CHILD(n
, 6));
3143 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3146 return ClassDef(classname
, bases
, s
, decorator_seq
,
3147 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3151 ast_for_stmt(struct compiling
*c
, const node
*n
)
3153 if (TYPE(n
) == stmt
) {
3154 assert(NCH(n
) == 1);
3157 if (TYPE(n
) == simple_stmt
) {
3158 assert(num_stmts(n
) == 1);
3161 if (TYPE(n
) == small_stmt
) {
3163 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3164 | flow_stmt | import_stmt | global_stmt | exec_stmt
3169 return ast_for_expr_stmt(c
, n
);
3171 return ast_for_print_stmt(c
, n
);
3173 return ast_for_del_stmt(c
, n
);
3175 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3177 return ast_for_flow_stmt(c
, n
);
3179 return ast_for_import_stmt(c
, n
);
3181 return ast_for_global_stmt(c
, n
);
3183 return ast_for_exec_stmt(c
, n
);
3185 return ast_for_assert_stmt(c
, n
);
3187 PyErr_Format(PyExc_SystemError
,
3188 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3194 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3195 | funcdef | classdef | decorated
3197 node
*ch
= CHILD(n
, 0);
3198 REQ(n
, compound_stmt
);
3201 return ast_for_if_stmt(c
, ch
);
3203 return ast_for_while_stmt(c
, ch
);
3205 return ast_for_for_stmt(c
, ch
);
3207 return ast_for_try_stmt(c
, ch
);
3209 return ast_for_with_stmt(c
, ch
);
3211 return ast_for_funcdef(c
, ch
, NULL
);
3213 return ast_for_classdef(c
, ch
, NULL
);
3215 return ast_for_decorated(c
, ch
);
3217 PyErr_Format(PyExc_SystemError
,
3218 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3226 parsenumber(struct compiling
*c
, const char *s
)
3231 #ifndef WITHOUT_COMPLEX
3238 end
= s
+ strlen(s
) - 1;
3239 #ifndef WITHOUT_COMPLEX
3240 imflag
= *end
== 'j' || *end
== 'J';
3242 if (*end
== 'l' || *end
== 'L')
3243 return PyLong_FromString((char *)s
, (char **)0, 0);
3244 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3247 return PyLong_FromString((char *)s
, (char **)0, 0);
3248 return PyInt_FromLong(x
);
3250 /* XXX Huge floats may silently fail */
3251 #ifndef WITHOUT_COMPLEX
3254 complex.imag
= PyOS_string_to_double(s
, (char **)&end
, NULL
);
3255 if (complex.imag
== -1.0 && PyErr_Occurred())
3257 return PyComplex_FromCComplex(complex);
3262 dx
= PyOS_string_to_double(s
, NULL
, NULL
);
3263 if (dx
== -1.0 && PyErr_Occurred())
3265 return PyFloat_FromDouble(dx
);
3270 decode_utf8(struct compiling
*c
, const char **sPtr
, const char *end
, char* encoding
)
3272 #ifndef Py_USING_UNICODE
3273 Py_FatalError("decode_utf8 should not be called in this build.");
3278 t
= s
= (char *)*sPtr
;
3279 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3280 while (s
< end
&& (*s
& 0x80)) s
++;
3282 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3285 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3291 #ifdef Py_USING_UNICODE
3293 decode_unicode(struct compiling
*c
, const char *s
, size_t len
, int rawmode
, const char *encoding
)
3299 if (encoding
== NULL
) {
3302 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3306 /* check for integer overflow */
3307 if (len
> PY_SIZE_MAX
/ 6)
3309 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3310 "\รค" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3311 u
= PyString_FromStringAndSize((char *)NULL
, len
* 6);
3314 p
= buf
= PyString_AsString(u
);
3324 if (*s
& 0x80) { /* XXX inefficient */
3328 w
= decode_utf8(c
, &s
, end
, "utf-32-be");
3333 r
= PyString_AsString(w
);
3334 rn
= PyString_Size(w
);
3335 assert(rn
% 4 == 0);
3336 for (i
= 0; i
< rn
; i
+= 4) {
3337 sprintf(p
, "\\U%02x%02x%02x%02x",
3353 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3355 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3361 /* s is a Python string literal, including the bracketing quote characters,
3362 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3363 * parsestr parses it, and returns the decoded Python string object.
3366 parsestr(struct compiling
*c
, const char *s
)
3369 int quote
= Py_CHARMASK(*s
);
3372 int unicode
= c
->c_future_unicode
;
3374 if (isalpha(quote
) || quote
== '_') {
3375 if (quote
== 'u' || quote
== 'U') {
3379 if (quote
== 'b' || quote
== 'B') {
3383 if (quote
== 'r' || quote
== 'R') {
3388 if (quote
!= '\'' && quote
!= '\"') {
3389 PyErr_BadInternalCall();
3394 if (len
> INT_MAX
) {
3395 PyErr_SetString(PyExc_OverflowError
,
3396 "string to parse is too long");
3399 if (s
[--len
] != quote
) {
3400 PyErr_BadInternalCall();
3403 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3406 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3407 PyErr_BadInternalCall();
3411 #ifdef Py_USING_UNICODE
3412 if (unicode
|| Py_UnicodeFlag
) {
3413 return decode_unicode(c
, s
, len
, rawmode
, c
->c_encoding
);
3416 need_encoding
= (c
->c_encoding
!= NULL
&&
3417 strcmp(c
->c_encoding
, "utf-8") != 0 &&
3418 strcmp(c
->c_encoding
, "iso-8859-1") != 0);
3419 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3420 if (need_encoding
) {
3421 #ifndef Py_USING_UNICODE
3422 /* This should not happen - we never see any other
3425 "cannot deal with encodings in this build.");
3427 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3430 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3435 return PyString_FromStringAndSize(s
, len
);
3439 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3440 need_encoding
? c
->c_encoding
: NULL
);
3443 /* Build a Python string object out of a STRING atom. This takes care of
3444 * compile-time literal catenation, calling parsestr() on each piece, and
3445 * pasting the intermediate results together.
3448 parsestrplus(struct compiling
*c
, const node
*n
)
3452 REQ(CHILD(n
, 0), STRING
);
3453 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
3454 /* String literal concatenation */
3455 for (i
= 1; i
< NCH(n
); i
++) {
3457 s
= parsestr(c
, STR(CHILD(n
, i
)));
3460 if (PyString_Check(v
) && PyString_Check(s
)) {
3461 PyString_ConcatAndDel(&v
, s
);
3465 #ifdef Py_USING_UNICODE
3467 PyObject
*temp
= PyUnicode_Concat(v
, s
);