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_comp(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 #define COMP_SETCOMP 1
51 new_identifier(const char* n
, PyArena
*arena
) {
52 PyObject
* id
= PyString_InternFromString(n
);
54 PyArena_AddPyObject(arena
, id
);
58 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
60 /* This routine provides an invalid object for the syntax error.
61 The outermost routine must unpack this error and create the
62 proper object. We do this so that we don't have to pass
63 the filename to everything function.
65 XXX Maybe we should just pass the filename...
69 ast_error(const node
*n
, const char *errstr
)
71 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
74 PyErr_SetObject(PyExc_SyntaxError
, u
);
80 ast_error_finish(const char *filename
)
82 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
85 assert(PyErr_Occurred());
86 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
89 PyErr_Fetch(&type
, &value
, &tback
);
90 errstr
= PyTuple_GetItem(value
, 0);
94 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
101 loc
= PyErr_ProgramText(filename
, lineno
);
106 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
112 value
= PyTuple_Pack(2, errstr
, tmp
);
117 PyErr_Restore(type
, value
, tback
);
121 ast_warn(struct compiling
*c
, const node
*n
, char *msg
)
123 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
, LINENO(n
),
125 /* if -Werr, change it to a SyntaxError */
126 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning
))
134 forbidden_check(struct compiling
*c
, const node
*n
, const char *x
)
136 if (!strcmp(x
, "None"))
137 return ast_error(n
, "cannot assign to None");
138 if (!strcmp(x
, "__debug__"))
139 return ast_error(n
, "cannot assign to __debug__");
140 if (Py_Py3kWarningFlag
) {
141 if (!(strcmp(x
, "True") && strcmp(x
, "False")) &&
142 !ast_warn(c
, n
, "assignment to True or False is forbidden in 3.x"))
144 if (!strcmp(x
, "nonlocal") &&
145 !ast_warn(c
, n
, "nonlocal is a keyword in 3.x"))
151 /* num_stmts() returns number of contained statements.
153 Use this routine to determine how big a sequence is needed for
154 the statements in a parse tree. Its raison d'etre is this bit of
157 stmt: simple_stmt | compound_stmt
158 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
160 A simple_stmt can contain multiple small_stmt elements joined
161 by semicolons. If the arg is a simple_stmt, the number of
162 small_stmt elements is returned.
166 num_stmts(const node
*n
)
173 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
176 return num_stmts(CHILD(n
, 0));
179 for (i
= 0; i
< NCH(n
); i
++) {
181 if (TYPE(ch
) == stmt
)
186 return num_stmts(CHILD(n
, 0));
190 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
193 return num_stmts(CHILD(n
, 0));
196 for (i
= 2; i
< (NCH(n
) - 1); i
++)
197 l
+= num_stmts(CHILD(n
, i
));
203 sprintf(buf
, "Non-statement found: %d %d",
212 /* Transform the CST rooted at node * to the appropriate AST
216 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
220 asdl_seq
*stmts
= NULL
;
225 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
226 c
.c_encoding
= "utf-8";
227 if (TYPE(n
) == encoding_decl
) {
228 ast_error(n
, "encoding declaration in Unicode string");
231 } else if (TYPE(n
) == encoding_decl
) {
232 c
.c_encoding
= STR(n
);
237 c
.c_future_unicode
= flags
&& flags
->cf_flags
& CO_FUTURE_UNICODE_LITERALS
;
239 c
.c_filename
= filename
;
244 stmts
= asdl_seq_new(num_stmts(n
), arena
);
247 for (i
= 0; i
< NCH(n
) - 1; i
++) {
249 if (TYPE(ch
) == NEWLINE
)
254 s
= ast_for_stmt(&c
, ch
);
257 asdl_seq_SET(stmts
, k
++, s
);
261 REQ(ch
, simple_stmt
);
262 for (j
= 0; j
< num
; j
++) {
263 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
266 asdl_seq_SET(stmts
, k
++, s
);
270 return Module(stmts
, arena
);
272 expr_ty testlist_ast
;
274 /* XXX Why not comp_for here? */
275 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
278 return Expression(testlist_ast
, arena
);
281 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
282 stmts
= asdl_seq_new(1, arena
);
285 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
287 if (!asdl_seq_GET(stmts
, 0))
289 return Interactive(stmts
, arena
);
294 stmts
= asdl_seq_new(num
, arena
);
298 s
= ast_for_stmt(&c
, n
);
301 asdl_seq_SET(stmts
, 0, s
);
304 /* Only a simple_stmt can contain multiple statements. */
306 for (i
= 0; i
< NCH(n
); i
+= 2) {
307 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
309 s
= ast_for_stmt(&c
, CHILD(n
, i
));
312 asdl_seq_SET(stmts
, i
/ 2, s
);
316 return Interactive(stmts
, arena
);
319 PyErr_Format(PyExc_SystemError
,
320 "invalid node %d for PyAST_FromNode", TYPE(n
));
324 ast_error_finish(filename
);
328 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
332 get_operator(const node
*n
)
358 return (operator_ty
)0;
362 /* Set the context ctx for expr_ty e, recursively traversing e.
364 Only sets context for expr kinds that "can appear in assignment context"
365 (according to ../Parser/Python.asdl). For other expr kinds, it sets
366 an appropriate syntax error and returns false.
370 set_context(struct compiling
*c
, expr_ty e
, expr_context_ty ctx
, const node
*n
)
373 /* If a particular expression type can't be used for assign / delete,
374 set expr_name to its name and an error message will be generated.
376 const char* expr_name
= NULL
;
378 /* The ast defines augmented store and load contexts, but the
379 implementation here doesn't actually use them. The code may be
380 a little more complex than necessary as a result. It also means
381 that expressions in an augmented assignment have a Store context.
382 Consider restructuring so that augmented assignment uses
385 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
389 if (ctx
== Store
&& !forbidden_check(c
, n
,
390 PyBytes_AS_STRING(e
->v
.Attribute
.attr
)))
392 e
->v
.Attribute
.ctx
= ctx
;
395 e
->v
.Subscript
.ctx
= ctx
;
398 if (ctx
== Store
&& !forbidden_check(c
, n
,
399 PyBytes_AS_STRING(e
->v
.Name
.id
)))
408 if (asdl_seq_LEN(e
->v
.Tuple
.elts
)) {
409 e
->v
.Tuple
.ctx
= ctx
;
417 expr_name
= "lambda";
420 expr_name
= "function call";
425 expr_name
= "operator";
427 case GeneratorExp_kind
:
428 expr_name
= "generator expression";
431 expr_name
= "yield expression";
434 expr_name
= "list comprehension";
437 expr_name
= "set comprehension";
440 expr_name
= "dict comprehension";
445 expr_name
= "literal";
448 expr_name
= "comparison";
454 expr_name
= "conditional expression";
457 PyErr_Format(PyExc_SystemError
,
458 "unexpected expression in assignment %d (line %d)",
462 /* Check for error string set by switch */
465 PyOS_snprintf(buf
, sizeof(buf
),
467 ctx
== Store
? "assign to" : "delete",
469 return ast_error(n
, buf
);
472 /* If the LHS is a list or tuple, we need to set the assignment
473 context for all the contained elements.
478 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
479 if (!set_context(c
, (expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
487 ast_for_augassign(struct compiling
*c
, const node
*n
)
497 if (STR(n
)[1] == '/')
514 if (STR(n
)[1] == '*')
519 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
520 return (operator_ty
)0;
525 ast_for_comp_op(struct compiling
*c
, const node
*n
)
527 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
538 case EQEQUAL
: /* == */
547 if (strcmp(STR(n
), "in") == 0)
549 if (strcmp(STR(n
), "is") == 0)
552 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
557 else if (NCH(n
) == 2) {
558 /* handle "not in" and "is not" */
559 switch (TYPE(CHILD(n
, 0))) {
561 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
563 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
566 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
567 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
571 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
577 seq_for_testlist(struct compiling
*c
, const node
*n
)
579 /* testlist: test (',' test)* [','] */
583 assert(TYPE(n
) == testlist
||
584 TYPE(n
) == listmaker
||
585 TYPE(n
) == testlist_comp
||
586 TYPE(n
) == testlist_safe
||
587 TYPE(n
) == testlist1
);
589 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
593 for (i
= 0; i
< NCH(n
); i
+= 2) {
594 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
596 expression
= ast_for_expr(c
, CHILD(n
, i
));
600 assert(i
/ 2 < seq
->size
);
601 asdl_seq_SET(seq
, i
/ 2, expression
);
607 compiler_complex_args(struct compiling
*c
, const node
*n
)
609 int i
, len
= (NCH(n
) + 1) / 2;
611 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
615 /* fpdef: NAME | '(' fplist ')'
616 fplist: fpdef (',' fpdef)* [',']
619 for (i
= 0; i
< len
; i
++) {
621 const node
*fpdef_node
= CHILD(n
, 2*i
);
625 /* fpdef_node is either a NAME or an fplist */
626 child
= CHILD(fpdef_node
, 0);
627 if (TYPE(child
) == NAME
) {
628 if (!forbidden_check(c
, n
, STR(child
)))
630 arg_id
= NEW_IDENTIFIER(child
);
633 arg
= Name(arg_id
, Store
, LINENO(child
), child
->n_col_offset
,
637 assert(TYPE(fpdef_node
) == fpdef
);
638 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
639 child
= CHILD(fpdef_node
, 1);
640 assert(TYPE(child
) == fplist
);
641 /* NCH == 1 means we have (x), we need to elide the extra parens */
642 if (NCH(child
) == 1) {
643 fpdef_node
= CHILD(child
, 0);
644 assert(TYPE(fpdef_node
) == fpdef
);
647 arg
= compiler_complex_args(c
, child
);
649 asdl_seq_SET(args
, i
, arg
);
652 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
653 if (!set_context(c
, result
, Store
, n
))
659 /* Create AST for argument list. */
662 ast_for_arguments(struct compiling
*c
, const node
*n
)
664 /* parameters: '(' [varargslist] ')'
665 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
666 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
668 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
669 asdl_seq
*args
, *defaults
;
670 identifier vararg
= NULL
, kwarg
= NULL
;
673 if (TYPE(n
) == parameters
) {
674 if (NCH(n
) == 2) /* () as argument list */
675 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
680 /* first count the number of normal args & defaults */
681 for (i
= 0; i
< NCH(n
); i
++) {
683 if (TYPE(ch
) == fpdef
)
685 if (TYPE(ch
) == EQUAL
)
688 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
690 return NULL
; /* Don't need to goto error; no objects allocated */
691 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
692 if (!defaults
&& n_defaults
)
693 return NULL
; /* Don't need to goto error; no objects allocated */
695 /* fpdef: NAME | '(' fplist ')'
696 fplist: fpdef (',' fpdef)* [',']
699 j
= 0; /* index for defaults */
700 k
= 0; /* index for args */
705 int complex_args
= 0, parenthesized
= 0;
707 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
708 anything other than EQUAL or a comma? */
709 /* XXX Should NCH(n) check be made a separate check? */
710 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
711 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
714 assert(defaults
!= NULL
);
715 asdl_seq_SET(defaults
, j
++, expression
);
719 else if (found_default
) {
720 /* def f((x)=4): pass should raise an error.
721 def f((x, (y))): pass will just incur the tuple unpacking warning. */
722 if (parenthesized
&& !complex_args
) {
723 ast_error(n
, "parenthesized arg with default");
727 "non-default argument follows default argument");
732 /* def foo((x)): is not complex, special case. */
734 /* We have complex arguments, setup for unpacking. */
735 if (Py_Py3kWarningFlag
&& !ast_warn(c
, ch
,
736 "tuple parameter unpacking has been removed in 3.x"))
739 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
740 if (!asdl_seq_GET(args
, k
-1))
743 /* def foo((x)): setup for checking NAME below. */
744 /* Loop because there can be many parens and tuple
745 unpacking mixed in. */
748 assert(TYPE(ch
) == fpdef
);
752 if (TYPE(CHILD(ch
, 0)) == NAME
) {
755 if (!forbidden_check(c
, n
, STR(CHILD(ch
, 0))))
757 id
= NEW_IDENTIFIER(CHILD(ch
, 0));
760 name
= Name(id
, Param
, LINENO(ch
), ch
->n_col_offset
,
764 asdl_seq_SET(args
, k
++, name
);
767 i
+= 2; /* the name and the comma */
768 if (parenthesized
&& Py_Py3kWarningFlag
&&
769 !ast_warn(c
, ch
, "parenthesized argument names "
770 "are invalid in 3.x"))
776 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
778 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
784 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
786 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
792 PyErr_Format(PyExc_SystemError
,
793 "unexpected node in varargslist: %d @ %d",
799 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
808 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
812 int lineno
, col_offset
;
818 col_offset
= n
->n_col_offset
;
820 id
= NEW_IDENTIFIER(CHILD(n
, 0));
823 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
827 for (i
= 2; i
< NCH(n
); i
+=2) {
828 id
= NEW_IDENTIFIER(CHILD(n
, i
));
831 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
840 ast_for_decorator(struct compiling
*c
, const node
*n
)
842 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
847 REQ(CHILD(n
, 0), AT
);
848 REQ(RCHILD(n
, -1), NEWLINE
);
850 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
854 if (NCH(n
) == 3) { /* No arguments */
858 else if (NCH(n
) == 5) { /* Call with no arguments */
859 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
860 n
->n_col_offset
, c
->c_arena
);
866 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
876 ast_for_decorators(struct compiling
*c
, const node
*n
)
878 asdl_seq
* decorator_seq
;
883 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
887 for (i
= 0; i
< NCH(n
); i
++) {
888 d
= ast_for_decorator(c
, CHILD(n
, i
));
891 asdl_seq_SET(decorator_seq
, i
, d
);
893 return decorator_seq
;
897 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
899 /* funcdef: 'def' NAME parameters ':' suite */
907 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
910 else if (!forbidden_check(c
, CHILD(n
, name_i
), STR(CHILD(n
, name_i
))))
912 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
915 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
919 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
920 n
->n_col_offset
, c
->c_arena
);
924 ast_for_decorated(struct compiling
*c
, const node
*n
)
926 /* decorated: decorators (classdef | funcdef) */
927 stmt_ty thing
= NULL
;
928 asdl_seq
*decorator_seq
= NULL
;
932 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
936 assert(TYPE(CHILD(n
, 1)) == funcdef
||
937 TYPE(CHILD(n
, 1)) == classdef
);
939 if (TYPE(CHILD(n
, 1)) == funcdef
) {
940 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
941 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
942 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
944 /* we count the decorators in when talking about the class' or
945 function's line number */
947 thing
->lineno
= LINENO(n
);
948 thing
->col_offset
= n
->n_col_offset
;
954 ast_for_lambdef(struct compiling
*c
, const node
*n
)
956 /* lambdef: 'lambda' [varargslist] ':' test */
961 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
964 expression
= ast_for_expr(c
, CHILD(n
, 2));
969 args
= ast_for_arguments(c
, CHILD(n
, 1));
972 expression
= ast_for_expr(c
, CHILD(n
, 3));
977 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
981 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
983 /* test: or_test 'if' or_test 'else' test */
984 expr_ty expression
, body
, orelse
;
987 body
= ast_for_expr(c
, CHILD(n
, 0));
990 expression
= ast_for_expr(c
, CHILD(n
, 2));
993 orelse
= ast_for_expr(c
, CHILD(n
, 4));
996 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
1000 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
1001 so there is only a single version. Possibly for loops can also re-use
1005 /* Count the number of 'for' loop in a list comprehension.
1007 Helper for ast_for_listcomp().
1011 count_list_fors(struct compiling
*c
, const node
*n
)
1014 node
*ch
= CHILD(n
, 1);
1026 if (TYPE(ch
) == list_for
)
1027 goto count_list_for
;
1028 else if (TYPE(ch
) == list_if
) {
1031 goto count_list_iter
;
1037 /* Should never be reached */
1038 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
1042 /* Count the number of 'if' statements in a list comprehension.
1044 Helper for ast_for_listcomp().
1048 count_list_ifs(struct compiling
*c
, const node
*n
)
1054 if (TYPE(CHILD(n
, 0)) == list_for
)
1062 goto count_list_iter
;
1066 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1068 /* listmaker: test ( list_for | (',' test)* [','] )
1069 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1070 list_iter: list_for | list_if
1071 list_if: 'if' test [list_iter]
1072 testlist_safe: test [(',' test)+ [',']]
1075 asdl_seq
*listcomps
;
1082 elt
= ast_for_expr(c
, CHILD(n
, 0));
1086 n_fors
= count_list_fors(c
, n
);
1090 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1095 for (i
= 0; i
< n_fors
; i
++) {
1096 comprehension_ty lc
;
1103 for_ch
= CHILD(ch
, 1);
1104 t
= ast_for_exprlist(c
, for_ch
, Store
);
1107 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1111 /* Check the # of children rather than the length of t, since
1112 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1114 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1115 if (NCH(for_ch
) == 1)
1116 lc
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1118 lc
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1120 expression
, NULL
, c
->c_arena
);
1127 expr_ty list_for_expr
;
1130 n_ifs
= count_list_ifs(c
, ch
);
1134 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1138 for (j
= 0; j
< n_ifs
; j
++) {
1143 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1147 asdl_seq_SET(ifs
, j
, list_for_expr
);
1151 /* on exit, must guarantee that ch is a list_for */
1152 if (TYPE(ch
) == list_iter
)
1156 asdl_seq_SET(listcomps
, i
, lc
);
1159 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1163 Count the number of 'for' loops in a comprehension.
1165 Helper for ast_for_comprehension().
1169 count_comp_fors(struct compiling
*c
, const node
*n
)
1183 if (TYPE(n
) == comp_for
)
1184 goto count_comp_for
;
1185 else if (TYPE(n
) == comp_if
) {
1188 goto count_comp_iter
;
1194 /* Should never be reached */
1195 PyErr_SetString(PyExc_SystemError
,
1196 "logic error in count_comp_fors");
1200 /* Count the number of 'if' statements in a comprehension.
1202 Helper for ast_for_comprehension().
1206 count_comp_ifs(struct compiling
*c
, const node
*n
)
1212 if (TYPE(CHILD(n
, 0)) == comp_for
)
1224 ast_for_comprehension(struct compiling
*c
, const node
*n
)
1229 n_fors
= count_comp_fors(c
, n
);
1233 comps
= asdl_seq_new(n_fors
, c
->c_arena
);
1237 for (i
= 0; i
< n_fors
; i
++) {
1238 comprehension_ty comp
;
1240 expr_ty expression
, first
;
1245 for_ch
= CHILD(n
, 1);
1246 t
= ast_for_exprlist(c
, for_ch
, Store
);
1249 expression
= ast_for_expr(c
, CHILD(n
, 3));
1253 /* Check the # of children rather than the length of t, since
1254 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1255 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1256 if (NCH(for_ch
) == 1)
1257 comp
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1259 comp
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1261 expression
, NULL
, c
->c_arena
);
1270 n_ifs
= count_comp_ifs(c
, n
);
1274 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1278 for (j
= 0; j
< n_ifs
; j
++) {
1283 expression
= ast_for_expr(c
, CHILD(n
, 1));
1286 asdl_seq_SET(ifs
, j
, expression
);
1290 /* on exit, must guarantee that n is a comp_for */
1291 if (TYPE(n
) == comp_iter
)
1295 asdl_seq_SET(comps
, i
, comp
);
1301 ast_for_itercomp(struct compiling
*c
, const node
*n
, int type
)
1308 elt
= ast_for_expr(c
, CHILD(n
, 0));
1312 comps
= ast_for_comprehension(c
, CHILD(n
, 1));
1316 if (type
== COMP_GENEXP
)
1317 return GeneratorExp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1318 else if (type
== COMP_SETCOMP
)
1319 return SetComp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1321 /* Should never happen */
1326 ast_for_dictcomp(struct compiling
*c
, const node
*n
)
1332 REQ(CHILD(n
, 1), COLON
);
1334 key
= ast_for_expr(c
, CHILD(n
, 0));
1338 value
= ast_for_expr(c
, CHILD(n
, 2));
1342 comps
= ast_for_comprehension(c
, CHILD(n
, 3));
1346 return DictComp(key
, value
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1350 ast_for_genexp(struct compiling
*c
, const node
*n
)
1352 assert(TYPE(n
) == (testlist_comp
) || TYPE(n
) == (argument
));
1353 return ast_for_itercomp(c
, n
, COMP_GENEXP
);
1357 ast_for_setcomp(struct compiling
*c
, const node
*n
)
1359 assert(TYPE(n
) == (dictorsetmaker
));
1360 return ast_for_itercomp(c
, n
, COMP_SETCOMP
);
1364 ast_for_atom(struct compiling
*c
, const node
*n
)
1366 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'
1367 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1369 node
*ch
= CHILD(n
, 0);
1373 /* All names start in Load context, but may later be
1375 PyObject
*name
= NEW_IDENTIFIER(ch
);
1378 return Name(name
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1381 PyObject
*str
= parsestrplus(c
, n
);
1383 #ifdef Py_USING_UNICODE
1384 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1385 PyObject
*type
, *value
, *tback
, *errstr
;
1386 PyErr_Fetch(&type
, &value
, &tback
);
1387 errstr
= PyObject_Str(value
);
1391 s
= PyString_AsString(errstr
);
1392 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1396 ast_error(n
, "(unicode error) unknown error");
1405 PyArena_AddPyObject(c
->c_arena
, str
);
1406 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1409 PyObject
*pynum
= parsenumber(c
, STR(ch
));
1413 PyArena_AddPyObject(c
->c_arena
, pynum
);
1414 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1416 case LPAR
: /* some parenthesized expressions */
1419 if (TYPE(ch
) == RPAR
)
1420 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1422 if (TYPE(ch
) == yield_expr
)
1423 return ast_for_expr(c
, ch
);
1425 return ast_for_testlist_comp(c
, ch
);
1426 case LSQB
: /* list (or list comprehension) */
1429 if (TYPE(ch
) == RSQB
)
1430 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1433 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1434 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1438 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1441 return ast_for_listcomp(c
, ch
);
1444 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
1445 * (test (comp_for | (',' test)* [',']))
1448 asdl_seq
*keys
, *values
;
1451 if (TYPE(ch
) == RBRACE
) {
1452 /* it's an empty dict */
1453 return Dict(NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1454 } else if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1455 /* it's a simple set */
1457 size
= (NCH(ch
) + 1) / 2; /* +1 in case no trailing comma */
1458 elts
= asdl_seq_new(size
, c
->c_arena
);
1461 for (i
= 0; i
< NCH(ch
); i
+= 2) {
1463 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1466 asdl_seq_SET(elts
, i
/ 2, expression
);
1468 return Set(elts
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1469 } else if (TYPE(CHILD(ch
, 1)) == comp_for
) {
1470 /* it's a set comprehension */
1471 return ast_for_setcomp(c
, ch
);
1472 } else if (NCH(ch
) > 3 && TYPE(CHILD(ch
, 3)) == comp_for
) {
1473 return ast_for_dictcomp(c
, ch
);
1476 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1477 keys
= asdl_seq_new(size
, c
->c_arena
);
1481 values
= asdl_seq_new(size
, c
->c_arena
);
1485 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1488 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1492 asdl_seq_SET(keys
, i
/ 4, expression
);
1494 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1498 asdl_seq_SET(values
, i
/ 4, expression
);
1500 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1503 case BACKQUOTE
: { /* repr */
1505 if (Py_Py3kWarningFlag
&&
1506 !ast_warn(c
, n
, "backquote not supported in 3.x; use repr()"))
1508 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1512 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1515 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1521 ast_for_slice(struct compiling
*c
, const node
*n
)
1524 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1529 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1533 if (TYPE(ch
) == DOT
)
1534 return Ellipsis(c
->c_arena
);
1536 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1537 /* 'step' variable hold no significance in terms of being used over
1539 step
= ast_for_expr(c
, ch
);
1543 return Index(step
, c
->c_arena
);
1546 if (TYPE(ch
) == test
) {
1547 lower
= ast_for_expr(c
, ch
);
1552 /* If there's an upper bound it's in the second or third position. */
1553 if (TYPE(ch
) == COLON
) {
1555 node
*n2
= CHILD(n
, 1);
1557 if (TYPE(n2
) == test
) {
1558 upper
= ast_for_expr(c
, n2
);
1563 } else if (NCH(n
) > 2) {
1564 node
*n2
= CHILD(n
, 2);
1566 if (TYPE(n2
) == test
) {
1567 upper
= ast_for_expr(c
, n2
);
1573 ch
= CHILD(n
, NCH(n
) - 1);
1574 if (TYPE(ch
) == sliceop
) {
1577 This is an extended slice (ie "x[::]") with no expression in the
1578 step field. We set this literally to "None" in order to
1579 disambiguate it from x[:]. (The interpreter might have to call
1580 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1582 identifier none
= new_identifier("None", c
->c_arena
);
1586 step
= Name(none
, Load
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1591 if (TYPE(ch
) == test
) {
1592 step
= ast_for_expr(c
, ch
);
1599 return Slice(lower
, upper
, step
, c
->c_arena
);
1603 ast_for_binop(struct compiling
*c
, const node
*n
)
1605 /* Must account for a sequence of expressions.
1606 How should A op B op C by represented?
1607 BinOp(BinOp(A, op, B), op, C).
1611 expr_ty expr1
, expr2
, result
;
1612 operator_ty newoperator
;
1614 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1618 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1622 newoperator
= get_operator(CHILD(n
, 1));
1626 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1631 nops
= (NCH(n
) - 1) / 2;
1632 for (i
= 1; i
< nops
; i
++) {
1633 expr_ty tmp_result
, tmp
;
1634 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1636 newoperator
= get_operator(next_oper
);
1640 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1644 tmp_result
= BinOp(result
, newoperator
, tmp
,
1645 LINENO(next_oper
), next_oper
->n_col_offset
,
1649 result
= tmp_result
;
1655 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1657 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1658 subscriptlist: subscript (',' subscript)* [',']
1659 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1662 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1664 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1665 n
->n_col_offset
, c
->c_arena
);
1667 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1669 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1670 PyObject
*attr_id
= NEW_IDENTIFIER(CHILD(n
, 1));
1673 return Attribute(left_expr
, attr_id
, Load
,
1674 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1677 REQ(CHILD(n
, 0), LSQB
);
1678 REQ(CHILD(n
, 2), RSQB
);
1681 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1684 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1688 /* The grammar is ambiguous here. The ambiguity is resolved
1689 by treating the sequence as a tuple literal if there are
1696 asdl_seq
*slices
, *elts
;
1697 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1700 for (j
= 0; j
< NCH(n
); j
+= 2) {
1701 slc
= ast_for_slice(c
, CHILD(n
, j
));
1704 if (slc
->kind
!= Index_kind
)
1706 asdl_seq_SET(slices
, j
/ 2, slc
);
1709 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1710 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1712 /* extract Index values and put them in a Tuple */
1713 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1716 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1717 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1718 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1719 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1721 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1724 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1725 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1731 ast_for_factor(struct compiling
*c
, const node
*n
)
1733 node
*pfactor
, *ppower
, *patom
, *pnum
;
1736 /* If the unary - operator is applied to a constant, don't generate
1737 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1738 constant. The peephole optimizer already does something like
1739 this but it doesn't handle the case where the constant is
1740 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1743 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1745 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1746 NCH(pfactor
) == 1 &&
1747 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1749 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1750 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1751 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1755 strcpy(s
+ 1, STR(pnum
));
1756 PyObject_FREE(STR(pnum
));
1758 return ast_for_atom(c
, patom
);
1761 expression
= ast_for_expr(c
, CHILD(n
, 1));
1765 switch (TYPE(CHILD(n
, 0))) {
1767 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1770 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1773 return UnaryOp(Invert
, expression
, LINENO(n
),
1774 n
->n_col_offset
, c
->c_arena
);
1776 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1782 ast_for_power(struct compiling
*c
, const node
*n
)
1784 /* power: atom trailer* ('**' factor)*
1789 e
= ast_for_atom(c
, CHILD(n
, 0));
1794 for (i
= 1; i
< NCH(n
); i
++) {
1795 node
*ch
= CHILD(n
, i
);
1796 if (TYPE(ch
) != trailer
)
1798 tmp
= ast_for_trailer(c
, ch
, e
);
1801 tmp
->lineno
= e
->lineno
;
1802 tmp
->col_offset
= e
->col_offset
;
1805 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1806 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1809 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1817 /* Do not name a variable 'expr'! Will cause a compile error.
1821 ast_for_expr(struct compiling
*c
, const node
*n
)
1823 /* handle the full range of simple expressions
1824 test: or_test ['if' or_test 'else' test] | lambdef
1825 or_test: and_test ('or' and_test)*
1826 and_test: not_test ('and' not_test)*
1827 not_test: 'not' not_test | comparison
1828 comparison: expr (comp_op expr)*
1829 expr: xor_expr ('|' xor_expr)*
1830 xor_expr: and_expr ('^' and_expr)*
1831 and_expr: shift_expr ('&' shift_expr)*
1832 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1833 arith_expr: term (('+'|'-') term)*
1834 term: factor (('*'|'/'|'%'|'//') factor)*
1835 factor: ('+'|'-'|'~') factor | power
1836 power: atom trailer* ('**' factor)*
1838 As well as modified versions that exist for backward compatibility,
1839 to explicitly allow:
1840 [ x for x in lambda: 0, lambda: 1 ]
1841 (which would be ambiguous without these extra rules)
1843 old_test: or_test | old_lambdef
1844 old_lambdef: 'lambda' [vararglist] ':' old_test
1855 if (TYPE(CHILD(n
, 0)) == lambdef
||
1856 TYPE(CHILD(n
, 0)) == old_lambdef
)
1857 return ast_for_lambdef(c
, CHILD(n
, 0));
1858 else if (NCH(n
) > 1)
1859 return ast_for_ifexpr(c
, n
);
1867 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1870 for (i
= 0; i
< NCH(n
); i
+= 2) {
1871 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1874 asdl_seq_SET(seq
, i
/ 2, e
);
1876 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1877 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1879 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1880 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1887 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1891 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1903 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1906 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1910 for (i
= 1; i
< NCH(n
); i
+= 2) {
1911 cmpop_ty newoperator
;
1913 newoperator
= ast_for_comp_op(c
, CHILD(n
, i
));
1918 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1923 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1924 asdl_seq_SET(cmps
, i
/ 2, expression
);
1926 expression
= ast_for_expr(c
, CHILD(n
, 0));
1931 return Compare(expression
, ops
, cmps
, LINENO(n
),
1932 n
->n_col_offset
, c
->c_arena
);
1936 /* The next five cases all handle BinOps. The main body of code
1937 is the same in each case, but the switch turned inside out to
1938 reuse the code for each type of operator.
1950 return ast_for_binop(c
, n
);
1954 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1958 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1965 return ast_for_factor(c
, n
);
1967 return ast_for_power(c
, n
);
1969 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1972 /* should never get here unless if error is set */
1977 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1980 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1982 argument: [test '='] test [comp_for] # Really [keyword '='] test
1985 int i
, nargs
, nkeywords
, ngens
;
1988 expr_ty vararg
= NULL
, kwarg
= NULL
;
1995 for (i
= 0; i
< NCH(n
); i
++) {
1996 node
*ch
= CHILD(n
, i
);
1997 if (TYPE(ch
) == argument
) {
2000 else if (TYPE(CHILD(ch
, 1)) == comp_for
)
2006 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
2007 ast_error(n
, "Generator expression must be parenthesized "
2008 "if not sole argument");
2012 if (nargs
+ nkeywords
+ ngens
> 255) {
2013 ast_error(n
, "more than 255 arguments");
2017 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
2020 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
2025 for (i
= 0; i
< NCH(n
); i
++) {
2026 node
*ch
= CHILD(n
, i
);
2027 if (TYPE(ch
) == argument
) {
2031 ast_error(CHILD(ch
, 0),
2032 "non-keyword arg after keyword arg");
2036 ast_error(CHILD(ch
, 0),
2037 "only named arguments may follow *expression");
2040 e
= ast_for_expr(c
, CHILD(ch
, 0));
2043 asdl_seq_SET(args
, nargs
++, e
);
2045 else if (TYPE(CHILD(ch
, 1)) == comp_for
) {
2046 e
= ast_for_genexp(c
, ch
);
2049 asdl_seq_SET(args
, nargs
++, e
);
2057 /* CHILD(ch, 0) is test, but must be an identifier? */
2058 e
= ast_for_expr(c
, CHILD(ch
, 0));
2061 /* f(lambda x: x[0] = 3) ends up getting parsed with
2062 * LHS test = lambda x: x[0], and RHS test = 3.
2063 * SF bug 132313 points out that complaining about a keyword
2064 * then is very confusing.
2066 if (e
->kind
== Lambda_kind
) {
2067 ast_error(CHILD(ch
, 0),
2068 "lambda cannot contain assignment");
2070 } else if (e
->kind
!= Name_kind
) {
2071 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
2075 if (!forbidden_check(c
, CHILD(ch
, 0), PyBytes_AS_STRING(key
)))
2077 for (k
= 0; k
< nkeywords
; k
++) {
2078 tmp
= PyString_AS_STRING(
2079 ((keyword_ty
)asdl_seq_GET(keywords
, k
))->arg
);
2080 if (!strcmp(tmp
, PyString_AS_STRING(key
))) {
2081 ast_error(CHILD(ch
, 0), "keyword argument repeated");
2085 e
= ast_for_expr(c
, CHILD(ch
, 2));
2088 kw
= keyword(key
, e
, c
->c_arena
);
2091 asdl_seq_SET(keywords
, nkeywords
++, kw
);
2094 else if (TYPE(ch
) == STAR
) {
2095 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
2100 else if (TYPE(ch
) == DOUBLESTAR
) {
2101 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
2108 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
2109 func
->col_offset
, c
->c_arena
);
2113 ast_for_testlist(struct compiling
*c
, const node
* n
)
2115 /* testlist_comp: test (',' test)* [','] */
2116 /* testlist: test (',' test)* [','] */
2117 /* testlist_safe: test (',' test)+ [','] */
2118 /* testlist1: test (',' test)* */
2120 if (TYPE(n
) == testlist_comp
) {
2122 assert(TYPE(CHILD(n
, 1)) != comp_for
);
2125 assert(TYPE(n
) == testlist
||
2126 TYPE(n
) == testlist_safe
||
2127 TYPE(n
) == testlist1
);
2130 return ast_for_expr(c
, CHILD(n
, 0));
2132 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
2135 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2140 ast_for_testlist_comp(struct compiling
*c
, const node
* n
)
2142 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
2143 /* argument: test [ comp_for ] */
2144 assert(TYPE(n
) == testlist_comp
|| TYPE(n
) == argument
);
2145 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == comp_for
)
2146 return ast_for_genexp(c
, n
);
2147 return ast_for_testlist(c
, n
);
2150 /* like ast_for_testlist() but returns a sequence */
2152 ast_for_class_bases(struct compiling
*c
, const node
* n
)
2154 /* testlist: test (',' test)* [','] */
2159 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
2162 base
= ast_for_expr(c
, CHILD(n
, 0));
2165 asdl_seq_SET(bases
, 0, base
);
2169 return seq_for_testlist(c
, n
);
2173 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2176 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2177 | ('=' (yield_expr|testlist))*)
2178 testlist: test (',' test)* [',']
2179 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2180 | '<<=' | '>>=' | '**=' | '//='
2181 test: ... here starts the operator precendence dance
2185 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2189 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2191 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2192 expr_ty expr1
, expr2
;
2193 operator_ty newoperator
;
2194 node
*ch
= CHILD(n
, 0);
2196 expr1
= ast_for_testlist(c
, ch
);
2199 if(!set_context(c
, expr1
, Store
, ch
))
2201 /* set_context checks that most expressions are not the left side.
2202 Augmented assignments can only have a name, a subscript, or an
2203 attribute on the left, though, so we have to explicitly check for
2205 switch (expr1
->kind
) {
2207 case Attribute_kind
:
2208 case Subscript_kind
:
2211 ast_error(ch
, "illegal expression for augmented assignment");
2216 if (TYPE(ch
) == testlist
)
2217 expr2
= ast_for_testlist(c
, ch
);
2219 expr2
= ast_for_expr(c
, ch
);
2223 newoperator
= ast_for_augassign(c
, CHILD(n
, 1));
2227 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2236 /* a normal assignment */
2237 REQ(CHILD(n
, 1), EQUAL
);
2238 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2241 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2243 node
*ch
= CHILD(n
, i
);
2244 if (TYPE(ch
) == yield_expr
) {
2245 ast_error(ch
, "assignment to yield expression not possible");
2248 e
= ast_for_testlist(c
, ch
);
2250 /* set context to assign */
2254 if (!set_context(c
, e
, Store
, CHILD(n
, i
)))
2257 asdl_seq_SET(targets
, i
/ 2, e
);
2259 value
= CHILD(n
, NCH(n
) - 1);
2260 if (TYPE(value
) == testlist
)
2261 expression
= ast_for_testlist(c
, value
);
2263 expression
= ast_for_expr(c
, value
);
2266 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2272 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2274 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2275 | '>>' test [ (',' test)+ [','] ] )
2277 expr_ty dest
= NULL
, expression
;
2278 asdl_seq
*seq
= NULL
;
2280 int i
, j
, values_count
, start
= 1;
2283 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2284 dest
= ast_for_expr(c
, CHILD(n
, 2));
2289 values_count
= (NCH(n
) + 1 - start
) / 2;
2291 seq
= asdl_seq_new(values_count
, c
->c_arena
);
2294 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2295 expression
= ast_for_expr(c
, CHILD(n
, i
));
2298 asdl_seq_SET(seq
, j
, expression
);
2301 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2302 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2306 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2314 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2317 for (i
= 0; i
< NCH(n
); i
+= 2) {
2318 e
= ast_for_expr(c
, CHILD(n
, i
));
2321 asdl_seq_SET(seq
, i
/ 2, e
);
2322 if (context
&& !set_context(c
, e
, context
, CHILD(n
, i
)))
2329 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2331 asdl_seq
*expr_list
;
2333 /* del_stmt: 'del' exprlist */
2336 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2339 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2343 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2346 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2349 continue_stmt: 'continue'
2350 return_stmt: 'return' [testlist]
2351 yield_stmt: yield_expr
2352 yield_expr: 'yield' testlist
2353 raise_stmt: 'raise' [test [',' test [',' test]]]
2361 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2363 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2364 case yield_stmt
: { /* will reduce to yield_expr */
2365 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2368 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2372 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2374 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2377 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2382 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2384 else if (NCH(ch
) == 2) {
2385 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2388 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2389 n
->n_col_offset
, c
->c_arena
);
2391 else if (NCH(ch
) == 4) {
2392 expr_ty expr1
, expr2
;
2394 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2397 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2401 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2404 else if (NCH(ch
) == 6) {
2405 expr_ty expr1
, expr2
, expr3
;
2407 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2410 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2413 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2417 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2421 PyErr_Format(PyExc_SystemError
,
2422 "unexpected flow_stmt: %d", TYPE(ch
));
2426 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2431 alias_for_import_name(struct compiling
*c
, const node
*n
, int store
)
2434 import_as_name: NAME ['as' NAME]
2435 dotted_as_name: dotted_name ['as' NAME]
2436 dotted_name: NAME ('.' NAME)*
2438 PyObject
*str
, *name
;
2442 case import_as_name
: {
2443 node
*name_node
= CHILD(n
, 0);
2446 node
*str_node
= CHILD(n
, 2);
2447 if (store
&& !forbidden_check(c
, str_node
, STR(str_node
)))
2449 str
= NEW_IDENTIFIER(str_node
);
2454 if (!forbidden_check(c
, name_node
, STR(name_node
)))
2457 name
= NEW_IDENTIFIER(name_node
);
2460 return alias(name
, str
, c
->c_arena
);
2462 case dotted_as_name
:
2468 node
*asname_node
= CHILD(n
, 2);
2469 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0), 0);
2473 if (!forbidden_check(c
, asname_node
, STR(asname_node
)))
2475 a
->asname
= NEW_IDENTIFIER(asname_node
);
2483 node
*name_node
= CHILD(n
, 0);
2484 if (store
&& !forbidden_check(c
, name_node
, STR(name_node
)))
2486 name
= NEW_IDENTIFIER(name_node
);
2489 return alias(name
, NULL
, c
->c_arena
);
2492 /* Create a string of the form "a.b.c" */
2498 for (i
= 0; i
< NCH(n
); i
+= 2)
2499 /* length of string plus one for the dot */
2500 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2501 len
--; /* the last name doesn't have a dot */
2502 str
= PyString_FromStringAndSize(NULL
, len
);
2505 s
= PyString_AS_STRING(str
);
2508 for (i
= 0; i
< NCH(n
); i
+= 2) {
2509 char *sch
= STR(CHILD(n
, i
));
2510 strcpy(s
, STR(CHILD(n
, i
)));
2516 PyString_InternInPlace(&str
);
2517 PyArena_AddPyObject(c
->c_arena
, str
);
2518 return alias(str
, NULL
, c
->c_arena
);
2522 str
= PyString_InternFromString("*");
2523 PyArena_AddPyObject(c
->c_arena
, str
);
2524 return alias(str
, NULL
, c
->c_arena
);
2526 PyErr_Format(PyExc_SystemError
,
2527 "unexpected import name: %d", TYPE(n
));
2531 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2536 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2539 import_stmt: import_name | import_from
2540 import_name: 'import' dotted_as_names
2541 import_from: 'from' ('.'* dotted_name | '.') 'import'
2542 ('*' | '(' import_as_names ')' | import_as_names)
2549 REQ(n
, import_stmt
);
2551 col_offset
= n
->n_col_offset
;
2553 if (TYPE(n
) == import_name
) {
2555 REQ(n
, dotted_as_names
);
2556 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2559 for (i
= 0; i
< NCH(n
); i
+= 2) {
2560 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2563 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2565 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2567 else if (TYPE(n
) == import_from
) {
2570 alias_ty mod
= NULL
;
2571 identifier modname
= NULL
;
2573 /* Count the number of dots (for relative imports) and check for the
2574 optional module name */
2575 for (idx
= 1; idx
< NCH(n
); idx
++) {
2576 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2577 mod
= alias_for_import_name(c
, CHILD(n
, idx
), 0);
2582 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2587 idx
++; /* skip over the 'import' keyword */
2588 switch (TYPE(CHILD(n
, idx
))) {
2590 /* from ... import * */
2595 /* from ... import (x, y, z) */
2596 n
= CHILD(n
, idx
+ 1);
2597 n_children
= NCH(n
);
2599 case import_as_names
:
2600 /* from ... import x, y, z */
2602 n_children
= NCH(n
);
2603 if (n_children
% 2 == 0) {
2604 ast_error(n
, "trailing comma not allowed without"
2605 " surrounding parentheses");
2610 ast_error(n
, "Unexpected node-type in from-import");
2614 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2618 /* handle "from ... import *" special b/c there's no children */
2619 if (TYPE(n
) == STAR
) {
2620 alias_ty import_alias
= alias_for_import_name(c
, n
, 1);
2623 asdl_seq_SET(aliases
, 0, import_alias
);
2626 for (i
= 0; i
< NCH(n
); i
+= 2) {
2627 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2630 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2634 modname
= mod
->name
;
2635 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2638 PyErr_Format(PyExc_SystemError
,
2639 "unknown import statement: starts with command '%s'",
2645 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2647 /* global_stmt: 'global' NAME (',' NAME)* */
2652 REQ(n
, global_stmt
);
2653 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2656 for (i
= 1; i
< NCH(n
); i
+= 2) {
2657 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2660 asdl_seq_SET(s
, i
/ 2, name
);
2662 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2666 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2668 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2669 int n_children
= NCH(n
);
2670 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2671 PyErr_Format(PyExc_SystemError
,
2672 "poorly formed 'exec' statement: %d parts to statement",
2677 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2679 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2682 if (n_children
>= 4) {
2683 globals
= ast_for_expr(c
, CHILD(n
, 3));
2687 if (n_children
== 6) {
2688 locals
= ast_for_expr(c
, CHILD(n
, 5));
2693 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2698 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2700 /* assert_stmt: 'assert' test [',' test] */
2701 REQ(n
, assert_stmt
);
2703 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2706 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2709 else if (NCH(n
) == 4) {
2710 expr_ty expr1
, expr2
;
2712 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2715 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2719 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2721 PyErr_Format(PyExc_SystemError
,
2722 "improper number of parts to 'assert' statement: %d",
2728 ast_for_suite(struct compiling
*c
, const node
*n
)
2730 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2733 int i
, total
, num
, end
, pos
= 0;
2738 total
= num_stmts(n
);
2739 seq
= asdl_seq_new(total
, c
->c_arena
);
2742 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2744 /* simple_stmt always ends with a NEWLINE,
2745 and may have a trailing SEMI
2748 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2750 /* loop by 2 to skip semi-colons */
2751 for (i
= 0; i
< end
; i
+= 2) {
2753 s
= ast_for_stmt(c
, ch
);
2756 asdl_seq_SET(seq
, pos
++, s
);
2760 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2763 num
= num_stmts(ch
);
2765 /* small_stmt or compound_stmt with only one child */
2766 s
= ast_for_stmt(c
, ch
);
2769 asdl_seq_SET(seq
, pos
++, s
);
2774 REQ(ch
, simple_stmt
);
2775 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2776 /* statement terminates with a semi-colon ';' */
2777 if (NCH(CHILD(ch
, j
)) == 0) {
2778 assert((j
+ 1) == NCH(ch
));
2781 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2784 asdl_seq_SET(seq
, pos
++, s
);
2789 assert(pos
== seq
->size
);
2794 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2796 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2805 asdl_seq
*suite_seq
;
2807 expression
= ast_for_expr(c
, CHILD(n
, 1));
2810 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2814 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2818 s
= STR(CHILD(n
, 4));
2819 /* s[2], the third character in the string, will be
2825 asdl_seq
*seq1
, *seq2
;
2827 expression
= ast_for_expr(c
, CHILD(n
, 1));
2830 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2833 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2837 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2840 else if (s
[2] == 'i') {
2841 int i
, n_elif
, has_else
= 0;
2843 asdl_seq
*suite_seq
;
2844 asdl_seq
*orelse
= NULL
;
2845 n_elif
= NCH(n
) - 4;
2846 /* must reference the child n_elif+1 since 'else' token is third,
2847 not fourth, child from the end. */
2848 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2849 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2856 asdl_seq
*suite_seq2
;
2858 orelse
= asdl_seq_new(1, c
->c_arena
);
2861 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2864 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2867 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2871 asdl_seq_SET(orelse
, 0,
2872 If(expression
, suite_seq
, suite_seq2
,
2873 LINENO(CHILD(n
, NCH(n
) - 6)),
2874 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2876 /* the just-created orelse handled the last elif */
2880 for (i
= 0; i
< n_elif
; i
++) {
2881 int off
= 5 + (n_elif
- i
- 1) * 4;
2882 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2885 expression
= ast_for_expr(c
, CHILD(n
, off
));
2888 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2892 asdl_seq_SET(newobj
, 0,
2893 If(expression
, suite_seq
, orelse
,
2894 LINENO(CHILD(n
, off
)),
2895 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2898 expression
= ast_for_expr(c
, CHILD(n
, 1));
2901 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2904 return If(expression
, suite_seq
, orelse
,
2905 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2908 PyErr_Format(PyExc_SystemError
,
2909 "unexpected token in 'if' statement: %s", s
);
2914 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2916 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2921 asdl_seq
*suite_seq
;
2923 expression
= ast_for_expr(c
, CHILD(n
, 1));
2926 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2929 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2932 else if (NCH(n
) == 7) {
2934 asdl_seq
*seq1
, *seq2
;
2936 expression
= ast_for_expr(c
, CHILD(n
, 1));
2939 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2942 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2946 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2950 PyErr_Format(PyExc_SystemError
,
2951 "wrong number of tokens for 'while' statement: %d",
2957 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2959 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2961 expr_ty target
, first
;
2962 const node
*node_target
;
2963 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2967 seq
= ast_for_suite(c
, CHILD(n
, 8));
2972 node_target
= CHILD(n
, 1);
2973 _target
= ast_for_exprlist(c
, node_target
, Store
);
2976 /* Check the # of children rather than the length of _target, since
2977 for x, in ... has 1 element in _target, but still requires a Tuple. */
2978 first
= (expr_ty
)asdl_seq_GET(_target
, 0);
2979 if (NCH(node_target
) == 1)
2982 target
= Tuple(_target
, Store
, first
->lineno
, first
->col_offset
, c
->c_arena
);
2984 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2987 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2991 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2995 static excepthandler_ty
2996 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2998 /* except_clause: 'except' [test [(',' | 'as') test]] */
2999 REQ(exc
, except_clause
);
3002 if (NCH(exc
) == 1) {
3003 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
3007 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
3008 exc
->n_col_offset
, c
->c_arena
);
3010 else if (NCH(exc
) == 2) {
3012 asdl_seq
*suite_seq
;
3014 expression
= ast_for_expr(c
, CHILD(exc
, 1));
3017 suite_seq
= ast_for_suite(c
, body
);
3021 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
3022 exc
->n_col_offset
, c
->c_arena
);
3024 else if (NCH(exc
) == 4) {
3025 asdl_seq
*suite_seq
;
3027 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
3030 if (!set_context(c
, e
, Store
, CHILD(exc
, 3)))
3032 expression
= ast_for_expr(c
, CHILD(exc
, 1));
3035 suite_seq
= ast_for_suite(c
, body
);
3039 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
3040 exc
->n_col_offset
, c
->c_arena
);
3043 PyErr_Format(PyExc_SystemError
,
3044 "wrong number of children for 'except' clause: %d",
3050 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
3052 const int nch
= NCH(n
);
3053 int n_except
= (nch
- 3)/3;
3054 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
3058 body
= ast_for_suite(c
, CHILD(n
, 2));
3062 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
3063 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
3064 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
3065 /* we can assume it's an "else",
3066 because nch >= 9 for try-else-finally and
3067 it would otherwise have a type of except_clause */
3068 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
3074 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
3075 if (finally
== NULL
)
3080 /* we can assume it's an "else",
3081 otherwise it would have a type of except_clause */
3082 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
3088 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
3089 ast_error(n
, "malformed 'try' statement");
3096 /* process except statements to create a try ... except */
3097 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
3098 if (handlers
== NULL
)
3101 for (i
= 0; i
< n_except
; i
++) {
3102 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
3103 CHILD(n
, 5 + i
* 3));
3106 asdl_seq_SET(handlers
, i
, e
);
3109 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
3110 n
->n_col_offset
, c
->c_arena
);
3114 /* if a 'finally' is present too, we nest the TryExcept within a
3115 TryFinally to emulate try ... except ... finally */
3116 body
= asdl_seq_new(1, c
->c_arena
);
3119 asdl_seq_SET(body
, 0, except_st
);
3122 /* must be a try ... finally (except clauses are in body, if any exist) */
3123 assert(finally
!= NULL
);
3124 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3127 /* with_item: test ['as' expr] */
3129 ast_for_with_item(struct compiling
*c
, const node
*n
, asdl_seq
*content
)
3131 expr_ty context_expr
, optional_vars
= NULL
;
3134 context_expr
= ast_for_expr(c
, CHILD(n
, 0));
3138 optional_vars
= ast_for_expr(c
, CHILD(n
, 2));
3140 if (!optional_vars
) {
3143 if (!set_context(c
, optional_vars
, Store
, n
)) {
3148 return With(context_expr
, optional_vars
, content
, LINENO(n
),
3149 n
->n_col_offset
, c
->c_arena
);
3152 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3154 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
3162 /* process the with items inside-out */
3164 /* the suite of the innermost with item is the suite of the with stmt */
3165 inner
= ast_for_suite(c
, CHILD(n
, i
));
3171 ret
= ast_for_with_item(c
, CHILD(n
, i
), inner
);
3174 /* was this the last item? */
3177 /* if not, wrap the result so far in a new sequence */
3178 inner
= asdl_seq_new(1, c
->c_arena
);
3181 asdl_seq_SET(inner
, 0, ret
);
3188 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
3190 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3191 PyObject
*classname
;
3192 asdl_seq
*bases
, *s
;
3196 if (!forbidden_check(c
, n
, STR(CHILD(n
, 1))))
3200 s
= ast_for_suite(c
, CHILD(n
, 3));
3203 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3206 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3207 n
->n_col_offset
, c
->c_arena
);
3209 /* check for empty base list */
3210 if (TYPE(CHILD(n
,3)) == RPAR
) {
3211 s
= ast_for_suite(c
, CHILD(n
,5));
3214 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3217 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3218 n
->n_col_offset
, c
->c_arena
);
3221 /* else handle the base class list */
3222 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3226 s
= ast_for_suite(c
, CHILD(n
, 6));
3229 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3232 return ClassDef(classname
, bases
, s
, decorator_seq
,
3233 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3237 ast_for_stmt(struct compiling
*c
, const node
*n
)
3239 if (TYPE(n
) == stmt
) {
3240 assert(NCH(n
) == 1);
3243 if (TYPE(n
) == simple_stmt
) {
3244 assert(num_stmts(n
) == 1);
3247 if (TYPE(n
) == small_stmt
) {
3249 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3250 | flow_stmt | import_stmt | global_stmt | exec_stmt
3255 return ast_for_expr_stmt(c
, n
);
3257 return ast_for_print_stmt(c
, n
);
3259 return ast_for_del_stmt(c
, n
);
3261 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3263 return ast_for_flow_stmt(c
, n
);
3265 return ast_for_import_stmt(c
, n
);
3267 return ast_for_global_stmt(c
, n
);
3269 return ast_for_exec_stmt(c
, n
);
3271 return ast_for_assert_stmt(c
, n
);
3273 PyErr_Format(PyExc_SystemError
,
3274 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3280 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3281 | funcdef | classdef | decorated
3283 node
*ch
= CHILD(n
, 0);
3284 REQ(n
, compound_stmt
);
3287 return ast_for_if_stmt(c
, ch
);
3289 return ast_for_while_stmt(c
, ch
);
3291 return ast_for_for_stmt(c
, ch
);
3293 return ast_for_try_stmt(c
, ch
);
3295 return ast_for_with_stmt(c
, ch
);
3297 return ast_for_funcdef(c
, ch
, NULL
);
3299 return ast_for_classdef(c
, ch
, NULL
);
3301 return ast_for_decorated(c
, ch
);
3303 PyErr_Format(PyExc_SystemError
,
3304 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3312 parsenumber(struct compiling
*c
, const char *s
)
3317 #ifndef WITHOUT_COMPLEX
3324 end
= s
+ strlen(s
) - 1;
3325 #ifndef WITHOUT_COMPLEX
3326 imflag
= *end
== 'j' || *end
== 'J';
3328 if (*end
== 'l' || *end
== 'L')
3329 return PyLong_FromString((char *)s
, (char **)0, 0);
3330 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3333 return PyLong_FromString((char *)s
, (char **)0, 0);
3334 return PyInt_FromLong(x
);
3336 /* XXX Huge floats may silently fail */
3337 #ifndef WITHOUT_COMPLEX
3340 complex.imag
= PyOS_string_to_double(s
, (char **)&end
, NULL
);
3341 if (complex.imag
== -1.0 && PyErr_Occurred())
3343 return PyComplex_FromCComplex(complex);
3348 dx
= PyOS_string_to_double(s
, NULL
, NULL
);
3349 if (dx
== -1.0 && PyErr_Occurred())
3351 return PyFloat_FromDouble(dx
);
3356 decode_utf8(struct compiling
*c
, const char **sPtr
, const char *end
, char* encoding
)
3358 #ifndef Py_USING_UNICODE
3359 Py_FatalError("decode_utf8 should not be called in this build.");
3364 t
= s
= (char *)*sPtr
;
3365 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3366 while (s
< end
&& (*s
& 0x80)) s
++;
3368 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3371 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3377 #ifdef Py_USING_UNICODE
3379 decode_unicode(struct compiling
*c
, const char *s
, size_t len
, int rawmode
, const char *encoding
)
3385 if (encoding
== NULL
) {
3388 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3392 /* check for integer overflow */
3393 if (len
> PY_SIZE_MAX
/ 6)
3395 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3396 "\รค" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3397 u
= PyString_FromStringAndSize((char *)NULL
, len
* 6);
3400 p
= buf
= PyString_AsString(u
);
3410 if (*s
& 0x80) { /* XXX inefficient */
3414 w
= decode_utf8(c
, &s
, end
, "utf-32-be");
3419 r
= PyString_AsString(w
);
3420 rn
= PyString_Size(w
);
3421 assert(rn
% 4 == 0);
3422 for (i
= 0; i
< rn
; i
+= 4) {
3423 sprintf(p
, "\\U%02x%02x%02x%02x",
3439 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3441 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3447 /* s is a Python string literal, including the bracketing quote characters,
3448 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3449 * parsestr parses it, and returns the decoded Python string object.
3452 parsestr(struct compiling
*c
, const char *s
)
3455 int quote
= Py_CHARMASK(*s
);
3458 int unicode
= c
->c_future_unicode
;
3460 if (isalpha(quote
) || quote
== '_') {
3461 if (quote
== 'u' || quote
== 'U') {
3465 if (quote
== 'b' || quote
== 'B') {
3469 if (quote
== 'r' || quote
== 'R') {
3474 if (quote
!= '\'' && quote
!= '\"') {
3475 PyErr_BadInternalCall();
3480 if (len
> INT_MAX
) {
3481 PyErr_SetString(PyExc_OverflowError
,
3482 "string to parse is too long");
3485 if (s
[--len
] != quote
) {
3486 PyErr_BadInternalCall();
3489 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3492 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3493 PyErr_BadInternalCall();
3497 #ifdef Py_USING_UNICODE
3498 if (unicode
|| Py_UnicodeFlag
) {
3499 return decode_unicode(c
, s
, len
, rawmode
, c
->c_encoding
);
3502 need_encoding
= (c
->c_encoding
!= NULL
&&
3503 strcmp(c
->c_encoding
, "utf-8") != 0 &&
3504 strcmp(c
->c_encoding
, "iso-8859-1") != 0);
3505 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3506 if (need_encoding
) {
3507 #ifndef Py_USING_UNICODE
3508 /* This should not happen - we never see any other
3511 "cannot deal with encodings in this build.");
3513 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3516 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3521 return PyString_FromStringAndSize(s
, len
);
3525 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3526 need_encoding
? c
->c_encoding
: NULL
);
3529 /* Build a Python string object out of a STRING atom. This takes care of
3530 * compile-time literal catenation, calling parsestr() on each piece, and
3531 * pasting the intermediate results together.
3534 parsestrplus(struct compiling
*c
, const node
*n
)
3538 REQ(CHILD(n
, 0), STRING
);
3539 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
3540 /* String literal concatenation */
3541 for (i
= 1; i
< NCH(n
); i
++) {
3543 s
= parsestr(c
, STR(CHILD(n
, i
)));
3546 if (PyString_Check(v
) && PyString_Check(s
)) {
3547 PyString_ConcatAndDel(&v
, s
);
3551 #ifdef Py_USING_UNICODE
3553 PyObject
*temp
= PyUnicode_Concat(v
, s
);