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";
446 expr_name
= "literal";
449 expr_name
= "comparison";
455 expr_name
= "conditional expression";
458 PyErr_Format(PyExc_SystemError
,
459 "unexpected expression in assignment %d (line %d)",
463 /* Check for error string set by switch */
466 PyOS_snprintf(buf
, sizeof(buf
),
468 ctx
== Store
? "assign to" : "delete",
470 return ast_error(n
, buf
);
473 /* If the LHS is a list or tuple, we need to set the assignment
474 context for all the contained elements.
479 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
480 if (!set_context(c
, (expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
488 ast_for_augassign(struct compiling
*c
, const node
*n
)
498 if (STR(n
)[1] == '/')
515 if (STR(n
)[1] == '*')
520 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
521 return (operator_ty
)0;
526 ast_for_comp_op(struct compiling
*c
, const node
*n
)
528 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
539 case EQEQUAL
: /* == */
548 if (strcmp(STR(n
), "in") == 0)
550 if (strcmp(STR(n
), "is") == 0)
553 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
558 else if (NCH(n
) == 2) {
559 /* handle "not in" and "is not" */
560 switch (TYPE(CHILD(n
, 0))) {
562 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
564 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
567 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
568 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
572 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
578 seq_for_testlist(struct compiling
*c
, const node
*n
)
580 /* testlist: test (',' test)* [','] */
584 assert(TYPE(n
) == testlist
||
585 TYPE(n
) == listmaker
||
586 TYPE(n
) == testlist_comp
||
587 TYPE(n
) == testlist_safe
||
588 TYPE(n
) == testlist1
);
590 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
594 for (i
= 0; i
< NCH(n
); i
+= 2) {
595 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
597 expression
= ast_for_expr(c
, CHILD(n
, i
));
601 assert(i
/ 2 < seq
->size
);
602 asdl_seq_SET(seq
, i
/ 2, expression
);
608 compiler_complex_args(struct compiling
*c
, const node
*n
)
610 int i
, len
= (NCH(n
) + 1) / 2;
612 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
616 /* fpdef: NAME | '(' fplist ')'
617 fplist: fpdef (',' fpdef)* [',']
620 for (i
= 0; i
< len
; i
++) {
622 const node
*fpdef_node
= CHILD(n
, 2*i
);
626 /* fpdef_node is either a NAME or an fplist */
627 child
= CHILD(fpdef_node
, 0);
628 if (TYPE(child
) == NAME
) {
629 if (!forbidden_check(c
, n
, STR(child
)))
631 arg_id
= NEW_IDENTIFIER(child
);
634 arg
= Name(arg_id
, Store
, LINENO(child
), child
->n_col_offset
,
638 assert(TYPE(fpdef_node
) == fpdef
);
639 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
640 child
= CHILD(fpdef_node
, 1);
641 assert(TYPE(child
) == fplist
);
642 /* NCH == 1 means we have (x), we need to elide the extra parens */
643 if (NCH(child
) == 1) {
644 fpdef_node
= CHILD(child
, 0);
645 assert(TYPE(fpdef_node
) == fpdef
);
648 arg
= compiler_complex_args(c
, child
);
650 asdl_seq_SET(args
, i
, arg
);
653 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
654 if (!set_context(c
, result
, Store
, n
))
660 /* Create AST for argument list. */
663 ast_for_arguments(struct compiling
*c
, const node
*n
)
665 /* parameters: '(' [varargslist] ')'
666 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
667 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
669 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
670 asdl_seq
*args
, *defaults
;
671 identifier vararg
= NULL
, kwarg
= NULL
;
674 if (TYPE(n
) == parameters
) {
675 if (NCH(n
) == 2) /* () as argument list */
676 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
681 /* first count the number of normal args & defaults */
682 for (i
= 0; i
< NCH(n
); i
++) {
684 if (TYPE(ch
) == fpdef
)
686 if (TYPE(ch
) == EQUAL
)
689 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
692 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
693 if (!defaults
&& n_defaults
)
696 /* fpdef: NAME | '(' fplist ')'
697 fplist: fpdef (',' fpdef)* [',']
700 j
= 0; /* index for defaults */
701 k
= 0; /* index for args */
706 int complex_args
= 0, parenthesized
= 0;
708 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
709 anything other than EQUAL or a comma? */
710 /* XXX Should NCH(n) check be made a separate check? */
711 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
712 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
715 assert(defaults
!= NULL
);
716 asdl_seq_SET(defaults
, j
++, expression
);
720 else if (found_default
) {
721 /* def f((x)=4): pass should raise an error.
722 def f((x, (y))): pass will just incur the tuple unpacking warning. */
723 if (parenthesized
&& !complex_args
) {
724 ast_error(n
, "parenthesized arg with default");
728 "non-default argument follows default argument");
733 /* def foo((x)): is not complex, special case. */
735 /* We have complex arguments, setup for unpacking. */
736 if (Py_Py3kWarningFlag
&& !ast_warn(c
, ch
,
737 "tuple parameter unpacking has been removed in 3.x"))
740 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
741 if (!asdl_seq_GET(args
, k
-1))
744 /* def foo((x)): setup for checking NAME below. */
745 /* Loop because there can be many parens and tuple
746 unpacking mixed in. */
749 assert(TYPE(ch
) == fpdef
);
753 if (TYPE(CHILD(ch
, 0)) == NAME
) {
756 if (!forbidden_check(c
, n
, STR(CHILD(ch
, 0))))
758 id
= NEW_IDENTIFIER(CHILD(ch
, 0));
761 name
= Name(id
, Param
, LINENO(ch
), ch
->n_col_offset
,
765 asdl_seq_SET(args
, k
++, name
);
768 i
+= 2; /* the name and the comma */
769 if (parenthesized
&& Py_Py3kWarningFlag
&&
770 !ast_warn(c
, ch
, "parenthesized argument names "
771 "are invalid in 3.x"))
777 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
779 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
785 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
787 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
793 PyErr_Format(PyExc_SystemError
,
794 "unexpected node in varargslist: %d @ %d",
800 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
804 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
808 int lineno
, col_offset
;
814 col_offset
= n
->n_col_offset
;
816 id
= NEW_IDENTIFIER(CHILD(n
, 0));
819 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
823 for (i
= 2; i
< NCH(n
); i
+=2) {
824 id
= NEW_IDENTIFIER(CHILD(n
, i
));
827 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
836 ast_for_decorator(struct compiling
*c
, const node
*n
)
838 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
843 REQ(CHILD(n
, 0), AT
);
844 REQ(RCHILD(n
, -1), NEWLINE
);
846 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
850 if (NCH(n
) == 3) { /* No arguments */
854 else if (NCH(n
) == 5) { /* Call with no arguments */
855 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
856 n
->n_col_offset
, c
->c_arena
);
862 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
872 ast_for_decorators(struct compiling
*c
, const node
*n
)
874 asdl_seq
* decorator_seq
;
879 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
883 for (i
= 0; i
< NCH(n
); i
++) {
884 d
= ast_for_decorator(c
, CHILD(n
, i
));
887 asdl_seq_SET(decorator_seq
, i
, d
);
889 return decorator_seq
;
893 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
895 /* funcdef: 'def' NAME parameters ':' suite */
903 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
906 else if (!forbidden_check(c
, CHILD(n
, name_i
), STR(CHILD(n
, name_i
))))
908 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
911 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
915 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
916 n
->n_col_offset
, c
->c_arena
);
920 ast_for_decorated(struct compiling
*c
, const node
*n
)
922 /* decorated: decorators (classdef | funcdef) */
923 stmt_ty thing
= NULL
;
924 asdl_seq
*decorator_seq
= NULL
;
928 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
932 assert(TYPE(CHILD(n
, 1)) == funcdef
||
933 TYPE(CHILD(n
, 1)) == classdef
);
935 if (TYPE(CHILD(n
, 1)) == funcdef
) {
936 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
937 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
938 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
940 /* we count the decorators in when talking about the class' or
941 function's line number */
943 thing
->lineno
= LINENO(n
);
944 thing
->col_offset
= n
->n_col_offset
;
950 ast_for_lambdef(struct compiling
*c
, const node
*n
)
952 /* lambdef: 'lambda' [varargslist] ':' test */
957 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
960 expression
= ast_for_expr(c
, CHILD(n
, 2));
965 args
= ast_for_arguments(c
, CHILD(n
, 1));
968 expression
= ast_for_expr(c
, CHILD(n
, 3));
973 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
977 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
979 /* test: or_test 'if' or_test 'else' test */
980 expr_ty expression
, body
, orelse
;
983 body
= ast_for_expr(c
, CHILD(n
, 0));
986 expression
= ast_for_expr(c
, CHILD(n
, 2));
989 orelse
= ast_for_expr(c
, CHILD(n
, 4));
992 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
996 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
997 so there is only a single version. Possibly for loops can also re-use
1001 /* Count the number of 'for' loop in a list comprehension.
1003 Helper for ast_for_listcomp().
1007 count_list_fors(struct compiling
*c
, const node
*n
)
1010 node
*ch
= CHILD(n
, 1);
1022 if (TYPE(ch
) == list_for
)
1023 goto count_list_for
;
1024 else if (TYPE(ch
) == list_if
) {
1027 goto count_list_iter
;
1033 /* Should never be reached */
1034 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
1038 /* Count the number of 'if' statements in a list comprehension.
1040 Helper for ast_for_listcomp().
1044 count_list_ifs(struct compiling
*c
, const node
*n
)
1050 if (TYPE(CHILD(n
, 0)) == list_for
)
1058 goto count_list_iter
;
1062 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1064 /* listmaker: test ( list_for | (',' test)* [','] )
1065 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1066 list_iter: list_for | list_if
1067 list_if: 'if' test [list_iter]
1068 testlist_safe: test [(',' test)+ [',']]
1071 asdl_seq
*listcomps
;
1078 elt
= ast_for_expr(c
, CHILD(n
, 0));
1082 n_fors
= count_list_fors(c
, n
);
1086 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1091 for (i
= 0; i
< n_fors
; i
++) {
1092 comprehension_ty lc
;
1099 for_ch
= CHILD(ch
, 1);
1100 t
= ast_for_exprlist(c
, for_ch
, Store
);
1103 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1107 /* Check the # of children rather than the length of t, since
1108 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1110 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1111 if (NCH(for_ch
) == 1)
1112 lc
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1114 lc
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1116 expression
, NULL
, c
->c_arena
);
1123 expr_ty list_for_expr
;
1126 n_ifs
= count_list_ifs(c
, ch
);
1130 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1134 for (j
= 0; j
< n_ifs
; j
++) {
1139 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1143 asdl_seq_SET(ifs
, j
, list_for_expr
);
1147 /* on exit, must guarantee that ch is a list_for */
1148 if (TYPE(ch
) == list_iter
)
1152 asdl_seq_SET(listcomps
, i
, lc
);
1155 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1159 Count the number of 'for' loops in a comprehension.
1161 Helper for ast_for_comprehension().
1165 count_comp_fors(struct compiling
*c
, const node
*n
)
1179 if (TYPE(n
) == comp_for
)
1180 goto count_comp_for
;
1181 else if (TYPE(n
) == comp_if
) {
1184 goto count_comp_iter
;
1190 /* Should never be reached */
1191 PyErr_SetString(PyExc_SystemError
,
1192 "logic error in count_comp_fors");
1196 /* Count the number of 'if' statements in a comprehension.
1198 Helper for ast_for_comprehension().
1202 count_comp_ifs(struct compiling
*c
, const node
*n
)
1208 if (TYPE(CHILD(n
, 0)) == comp_for
)
1220 ast_for_comprehension(struct compiling
*c
, const node
*n
)
1225 n_fors
= count_comp_fors(c
, n
);
1229 comps
= asdl_seq_new(n_fors
, c
->c_arena
);
1233 for (i
= 0; i
< n_fors
; i
++) {
1234 comprehension_ty comp
;
1236 expr_ty expression
, first
;
1241 for_ch
= CHILD(n
, 1);
1242 t
= ast_for_exprlist(c
, for_ch
, Store
);
1245 expression
= ast_for_expr(c
, CHILD(n
, 3));
1249 /* Check the # of children rather than the length of t, since
1250 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1251 first
= (expr_ty
)asdl_seq_GET(t
, 0);
1252 if (NCH(for_ch
) == 1)
1253 comp
= comprehension(first
, expression
, NULL
, c
->c_arena
);
1255 comp
= comprehension(Tuple(t
, Store
, first
->lineno
, first
->col_offset
,
1257 expression
, NULL
, c
->c_arena
);
1266 n_ifs
= count_comp_ifs(c
, n
);
1270 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1274 for (j
= 0; j
< n_ifs
; j
++) {
1279 expression
= ast_for_expr(c
, CHILD(n
, 1));
1282 asdl_seq_SET(ifs
, j
, expression
);
1286 /* on exit, must guarantee that n is a comp_for */
1287 if (TYPE(n
) == comp_iter
)
1291 asdl_seq_SET(comps
, i
, comp
);
1297 ast_for_itercomp(struct compiling
*c
, const node
*n
, int type
)
1304 elt
= ast_for_expr(c
, CHILD(n
, 0));
1308 comps
= ast_for_comprehension(c
, CHILD(n
, 1));
1312 if (type
== COMP_GENEXP
)
1313 return GeneratorExp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1314 else if (type
== COMP_SETCOMP
)
1315 return SetComp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1317 /* Should never happen */
1322 ast_for_dictcomp(struct compiling
*c
, const node
*n
)
1328 REQ(CHILD(n
, 1), COLON
);
1330 key
= ast_for_expr(c
, CHILD(n
, 0));
1334 value
= ast_for_expr(c
, CHILD(n
, 2));
1338 comps
= ast_for_comprehension(c
, CHILD(n
, 3));
1342 return DictComp(key
, value
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1346 ast_for_genexp(struct compiling
*c
, const node
*n
)
1348 assert(TYPE(n
) == (testlist_comp
) || TYPE(n
) == (argument
));
1349 return ast_for_itercomp(c
, n
, COMP_GENEXP
);
1353 ast_for_setcomp(struct compiling
*c
, const node
*n
)
1355 assert(TYPE(n
) == (dictorsetmaker
));
1356 return ast_for_itercomp(c
, n
, COMP_SETCOMP
);
1360 ast_for_atom(struct compiling
*c
, const node
*n
)
1362 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'
1363 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1365 node
*ch
= CHILD(n
, 0);
1369 /* All names start in Load context, but may later be
1371 PyObject
*name
= NEW_IDENTIFIER(ch
);
1374 return Name(name
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1377 PyObject
*str
= parsestrplus(c
, n
);
1379 #ifdef Py_USING_UNICODE
1380 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1381 PyObject
*type
, *value
, *tback
, *errstr
;
1382 PyErr_Fetch(&type
, &value
, &tback
);
1383 errstr
= PyObject_Str(value
);
1387 s
= PyString_AsString(errstr
);
1388 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1392 ast_error(n
, "(unicode error) unknown error");
1401 PyArena_AddPyObject(c
->c_arena
, str
);
1402 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1405 PyObject
*pynum
= parsenumber(c
, STR(ch
));
1409 PyArena_AddPyObject(c
->c_arena
, pynum
);
1410 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1412 case LPAR
: /* some parenthesized expressions */
1415 if (TYPE(ch
) == RPAR
)
1416 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1418 if (TYPE(ch
) == yield_expr
)
1419 return ast_for_expr(c
, ch
);
1421 return ast_for_testlist_comp(c
, ch
);
1422 case LSQB
: /* list (or list comprehension) */
1425 if (TYPE(ch
) == RSQB
)
1426 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1429 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1430 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1434 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1437 return ast_for_listcomp(c
, ch
);
1440 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
1441 * (test (comp_for | (',' test)* [',']))
1444 asdl_seq
*keys
, *values
;
1447 if (TYPE(ch
) == RBRACE
) {
1448 /* it's an empty dict */
1449 return Dict(NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1450 } else if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1451 /* it's a simple set */
1453 size
= (NCH(ch
) + 1) / 2; /* +1 in case no trailing comma */
1454 elts
= asdl_seq_new(size
, c
->c_arena
);
1457 for (i
= 0; i
< NCH(ch
); i
+= 2) {
1459 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1462 asdl_seq_SET(elts
, i
/ 2, expression
);
1464 return Set(elts
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1465 } else if (TYPE(CHILD(ch
, 1)) == comp_for
) {
1466 /* it's a set comprehension */
1467 return ast_for_setcomp(c
, ch
);
1468 } else if (NCH(ch
) > 3 && TYPE(CHILD(ch
, 3)) == comp_for
) {
1469 return ast_for_dictcomp(c
, ch
);
1472 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1473 keys
= asdl_seq_new(size
, c
->c_arena
);
1477 values
= asdl_seq_new(size
, c
->c_arena
);
1481 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1484 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1488 asdl_seq_SET(keys
, i
/ 4, expression
);
1490 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1494 asdl_seq_SET(values
, i
/ 4, expression
);
1496 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1499 case BACKQUOTE
: { /* repr */
1501 if (Py_Py3kWarningFlag
&&
1502 !ast_warn(c
, n
, "backquote not supported in 3.x; use repr()"))
1504 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1508 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1511 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1517 ast_for_slice(struct compiling
*c
, const node
*n
)
1520 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1525 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1529 if (TYPE(ch
) == DOT
)
1530 return Ellipsis(c
->c_arena
);
1532 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1533 /* 'step' variable hold no significance in terms of being used over
1535 step
= ast_for_expr(c
, ch
);
1539 return Index(step
, c
->c_arena
);
1542 if (TYPE(ch
) == test
) {
1543 lower
= ast_for_expr(c
, ch
);
1548 /* If there's an upper bound it's in the second or third position. */
1549 if (TYPE(ch
) == COLON
) {
1551 node
*n2
= CHILD(n
, 1);
1553 if (TYPE(n2
) == test
) {
1554 upper
= ast_for_expr(c
, n2
);
1559 } else if (NCH(n
) > 2) {
1560 node
*n2
= CHILD(n
, 2);
1562 if (TYPE(n2
) == test
) {
1563 upper
= ast_for_expr(c
, n2
);
1569 ch
= CHILD(n
, NCH(n
) - 1);
1570 if (TYPE(ch
) == sliceop
) {
1573 This is an extended slice (ie "x[::]") with no expression in the
1574 step field. We set this literally to "None" in order to
1575 disambiguate it from x[:]. (The interpreter might have to call
1576 __getslice__ for x[:], but it must call __getitem__ for x[::].)
1578 identifier none
= new_identifier("None", c
->c_arena
);
1582 step
= Name(none
, Load
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1587 if (TYPE(ch
) == test
) {
1588 step
= ast_for_expr(c
, ch
);
1595 return Slice(lower
, upper
, step
, c
->c_arena
);
1599 ast_for_binop(struct compiling
*c
, const node
*n
)
1601 /* Must account for a sequence of expressions.
1602 How should A op B op C by represented?
1603 BinOp(BinOp(A, op, B), op, C).
1607 expr_ty expr1
, expr2
, result
;
1608 operator_ty newoperator
;
1610 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1614 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1618 newoperator
= get_operator(CHILD(n
, 1));
1622 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1627 nops
= (NCH(n
) - 1) / 2;
1628 for (i
= 1; i
< nops
; i
++) {
1629 expr_ty tmp_result
, tmp
;
1630 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1632 newoperator
= get_operator(next_oper
);
1636 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1640 tmp_result
= BinOp(result
, newoperator
, tmp
,
1641 LINENO(next_oper
), next_oper
->n_col_offset
,
1645 result
= tmp_result
;
1651 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1653 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1654 subscriptlist: subscript (',' subscript)* [',']
1655 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1658 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1660 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1661 n
->n_col_offset
, c
->c_arena
);
1663 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1665 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1666 PyObject
*attr_id
= NEW_IDENTIFIER(CHILD(n
, 1));
1669 return Attribute(left_expr
, attr_id
, Load
,
1670 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1673 REQ(CHILD(n
, 0), LSQB
);
1674 REQ(CHILD(n
, 2), RSQB
);
1677 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1680 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1684 /* The grammar is ambiguous here. The ambiguity is resolved
1685 by treating the sequence as a tuple literal if there are
1692 asdl_seq
*slices
, *elts
;
1693 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1696 for (j
= 0; j
< NCH(n
); j
+= 2) {
1697 slc
= ast_for_slice(c
, CHILD(n
, j
));
1700 if (slc
->kind
!= Index_kind
)
1702 asdl_seq_SET(slices
, j
/ 2, slc
);
1705 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1706 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1708 /* extract Index values and put them in a Tuple */
1709 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1712 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1713 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1714 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1715 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1717 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1720 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1721 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1727 ast_for_factor(struct compiling
*c
, const node
*n
)
1729 node
*pfactor
, *ppower
, *patom
, *pnum
;
1732 /* If the unary - operator is applied to a constant, don't generate
1733 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1734 constant. The peephole optimizer already does something like
1735 this but it doesn't handle the case where the constant is
1736 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1739 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1741 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1742 NCH(pfactor
) == 1 &&
1743 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1745 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1746 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1747 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1751 strcpy(s
+ 1, STR(pnum
));
1752 PyObject_FREE(STR(pnum
));
1754 return ast_for_atom(c
, patom
);
1757 expression
= ast_for_expr(c
, CHILD(n
, 1));
1761 switch (TYPE(CHILD(n
, 0))) {
1763 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1766 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1769 return UnaryOp(Invert
, expression
, LINENO(n
),
1770 n
->n_col_offset
, c
->c_arena
);
1772 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1778 ast_for_power(struct compiling
*c
, const node
*n
)
1780 /* power: atom trailer* ('**' factor)*
1785 e
= ast_for_atom(c
, CHILD(n
, 0));
1790 for (i
= 1; i
< NCH(n
); i
++) {
1791 node
*ch
= CHILD(n
, i
);
1792 if (TYPE(ch
) != trailer
)
1794 tmp
= ast_for_trailer(c
, ch
, e
);
1797 tmp
->lineno
= e
->lineno
;
1798 tmp
->col_offset
= e
->col_offset
;
1801 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1802 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1805 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1813 /* Do not name a variable 'expr'! Will cause a compile error.
1817 ast_for_expr(struct compiling
*c
, const node
*n
)
1819 /* handle the full range of simple expressions
1820 test: or_test ['if' or_test 'else' test] | lambdef
1821 or_test: and_test ('or' and_test)*
1822 and_test: not_test ('and' not_test)*
1823 not_test: 'not' not_test | comparison
1824 comparison: expr (comp_op expr)*
1825 expr: xor_expr ('|' xor_expr)*
1826 xor_expr: and_expr ('^' and_expr)*
1827 and_expr: shift_expr ('&' shift_expr)*
1828 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1829 arith_expr: term (('+'|'-') term)*
1830 term: factor (('*'|'/'|'%'|'//') factor)*
1831 factor: ('+'|'-'|'~') factor | power
1832 power: atom trailer* ('**' factor)*
1834 As well as modified versions that exist for backward compatibility,
1835 to explicitly allow:
1836 [ x for x in lambda: 0, lambda: 1 ]
1837 (which would be ambiguous without these extra rules)
1839 old_test: or_test | old_lambdef
1840 old_lambdef: 'lambda' [vararglist] ':' old_test
1851 if (TYPE(CHILD(n
, 0)) == lambdef
||
1852 TYPE(CHILD(n
, 0)) == old_lambdef
)
1853 return ast_for_lambdef(c
, CHILD(n
, 0));
1854 else if (NCH(n
) > 1)
1855 return ast_for_ifexpr(c
, n
);
1863 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1866 for (i
= 0; i
< NCH(n
); i
+= 2) {
1867 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1870 asdl_seq_SET(seq
, i
/ 2, e
);
1872 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1873 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1875 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1876 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1883 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1887 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1899 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1902 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1906 for (i
= 1; i
< NCH(n
); i
+= 2) {
1907 cmpop_ty newoperator
;
1909 newoperator
= ast_for_comp_op(c
, CHILD(n
, i
));
1914 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1919 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1920 asdl_seq_SET(cmps
, i
/ 2, expression
);
1922 expression
= ast_for_expr(c
, CHILD(n
, 0));
1927 return Compare(expression
, ops
, cmps
, LINENO(n
),
1928 n
->n_col_offset
, c
->c_arena
);
1932 /* The next five cases all handle BinOps. The main body of code
1933 is the same in each case, but the switch turned inside out to
1934 reuse the code for each type of operator.
1946 return ast_for_binop(c
, n
);
1950 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1954 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1961 return ast_for_factor(c
, n
);
1963 return ast_for_power(c
, n
);
1965 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1968 /* should never get here unless if error is set */
1973 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1976 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1978 argument: [test '='] test [comp_for] # Really [keyword '='] test
1981 int i
, nargs
, nkeywords
, ngens
;
1984 expr_ty vararg
= NULL
, kwarg
= NULL
;
1991 for (i
= 0; i
< NCH(n
); i
++) {
1992 node
*ch
= CHILD(n
, i
);
1993 if (TYPE(ch
) == argument
) {
1996 else if (TYPE(CHILD(ch
, 1)) == comp_for
)
2002 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
2003 ast_error(n
, "Generator expression must be parenthesized "
2004 "if not sole argument");
2008 if (nargs
+ nkeywords
+ ngens
> 255) {
2009 ast_error(n
, "more than 255 arguments");
2013 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
2016 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
2021 for (i
= 0; i
< NCH(n
); i
++) {
2022 node
*ch
= CHILD(n
, i
);
2023 if (TYPE(ch
) == argument
) {
2027 ast_error(CHILD(ch
, 0),
2028 "non-keyword arg after keyword arg");
2032 ast_error(CHILD(ch
, 0),
2033 "only named arguments may follow *expression");
2036 e
= ast_for_expr(c
, CHILD(ch
, 0));
2039 asdl_seq_SET(args
, nargs
++, e
);
2041 else if (TYPE(CHILD(ch
, 1)) == comp_for
) {
2042 e
= ast_for_genexp(c
, ch
);
2045 asdl_seq_SET(args
, nargs
++, e
);
2053 /* CHILD(ch, 0) is test, but must be an identifier? */
2054 e
= ast_for_expr(c
, CHILD(ch
, 0));
2057 /* f(lambda x: x[0] = 3) ends up getting parsed with
2058 * LHS test = lambda x: x[0], and RHS test = 3.
2059 * SF bug 132313 points out that complaining about a keyword
2060 * then is very confusing.
2062 if (e
->kind
== Lambda_kind
) {
2063 ast_error(CHILD(ch
, 0),
2064 "lambda cannot contain assignment");
2066 } else if (e
->kind
!= Name_kind
) {
2067 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
2071 if (!forbidden_check(c
, CHILD(ch
, 0), PyBytes_AS_STRING(key
)))
2073 for (k
= 0; k
< nkeywords
; k
++) {
2074 tmp
= PyString_AS_STRING(
2075 ((keyword_ty
)asdl_seq_GET(keywords
, k
))->arg
);
2076 if (!strcmp(tmp
, PyString_AS_STRING(key
))) {
2077 ast_error(CHILD(ch
, 0), "keyword argument repeated");
2081 e
= ast_for_expr(c
, CHILD(ch
, 2));
2084 kw
= keyword(key
, e
, c
->c_arena
);
2087 asdl_seq_SET(keywords
, nkeywords
++, kw
);
2090 else if (TYPE(ch
) == STAR
) {
2091 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
2096 else if (TYPE(ch
) == DOUBLESTAR
) {
2097 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
2104 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
2105 func
->col_offset
, c
->c_arena
);
2109 ast_for_testlist(struct compiling
*c
, const node
* n
)
2111 /* testlist_comp: test (',' test)* [','] */
2112 /* testlist: test (',' test)* [','] */
2113 /* testlist_safe: test (',' test)+ [','] */
2114 /* testlist1: test (',' test)* */
2116 if (TYPE(n
) == testlist_comp
) {
2118 assert(TYPE(CHILD(n
, 1)) != comp_for
);
2121 assert(TYPE(n
) == testlist
||
2122 TYPE(n
) == testlist_safe
||
2123 TYPE(n
) == testlist1
);
2126 return ast_for_expr(c
, CHILD(n
, 0));
2128 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
2131 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2136 ast_for_testlist_comp(struct compiling
*c
, const node
* n
)
2138 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
2139 /* argument: test [ comp_for ] */
2140 assert(TYPE(n
) == testlist_comp
|| TYPE(n
) == argument
);
2141 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == comp_for
)
2142 return ast_for_genexp(c
, n
);
2143 return ast_for_testlist(c
, n
);
2146 /* like ast_for_testlist() but returns a sequence */
2148 ast_for_class_bases(struct compiling
*c
, const node
* n
)
2150 /* testlist: test (',' test)* [','] */
2155 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
2158 base
= ast_for_expr(c
, CHILD(n
, 0));
2161 asdl_seq_SET(bases
, 0, base
);
2165 return seq_for_testlist(c
, n
);
2169 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2172 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2173 | ('=' (yield_expr|testlist))*)
2174 testlist: test (',' test)* [',']
2175 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2176 | '<<=' | '>>=' | '**=' | '//='
2177 test: ... here starts the operator precendence dance
2181 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2185 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2187 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2188 expr_ty expr1
, expr2
;
2189 operator_ty newoperator
;
2190 node
*ch
= CHILD(n
, 0);
2192 expr1
= ast_for_testlist(c
, ch
);
2195 if(!set_context(c
, expr1
, Store
, ch
))
2197 /* set_context checks that most expressions are not the left side.
2198 Augmented assignments can only have a name, a subscript, or an
2199 attribute on the left, though, so we have to explicitly check for
2201 switch (expr1
->kind
) {
2203 case Attribute_kind
:
2204 case Subscript_kind
:
2207 ast_error(ch
, "illegal expression for augmented assignment");
2212 if (TYPE(ch
) == testlist
)
2213 expr2
= ast_for_testlist(c
, ch
);
2215 expr2
= ast_for_expr(c
, ch
);
2219 newoperator
= ast_for_augassign(c
, CHILD(n
, 1));
2223 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2232 /* a normal assignment */
2233 REQ(CHILD(n
, 1), EQUAL
);
2234 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2237 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2239 node
*ch
= CHILD(n
, i
);
2240 if (TYPE(ch
) == yield_expr
) {
2241 ast_error(ch
, "assignment to yield expression not possible");
2244 e
= ast_for_testlist(c
, ch
);
2248 /* set context to assign */
2249 if (!set_context(c
, e
, Store
, CHILD(n
, i
)))
2252 asdl_seq_SET(targets
, i
/ 2, e
);
2254 value
= CHILD(n
, NCH(n
) - 1);
2255 if (TYPE(value
) == testlist
)
2256 expression
= ast_for_testlist(c
, value
);
2258 expression
= ast_for_expr(c
, value
);
2261 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2267 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2269 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2270 | '>>' test [ (',' test)+ [','] ] )
2272 expr_ty dest
= NULL
, expression
;
2273 asdl_seq
*seq
= NULL
;
2275 int i
, j
, values_count
, start
= 1;
2278 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2279 dest
= ast_for_expr(c
, CHILD(n
, 2));
2284 values_count
= (NCH(n
) + 1 - start
) / 2;
2286 seq
= asdl_seq_new(values_count
, c
->c_arena
);
2289 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2290 expression
= ast_for_expr(c
, CHILD(n
, i
));
2293 asdl_seq_SET(seq
, j
, expression
);
2296 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2297 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2301 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2309 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2312 for (i
= 0; i
< NCH(n
); i
+= 2) {
2313 e
= ast_for_expr(c
, CHILD(n
, i
));
2316 asdl_seq_SET(seq
, i
/ 2, e
);
2317 if (context
&& !set_context(c
, e
, context
, CHILD(n
, i
)))
2324 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2326 asdl_seq
*expr_list
;
2328 /* del_stmt: 'del' exprlist */
2331 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2334 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2338 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2341 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2344 continue_stmt: 'continue'
2345 return_stmt: 'return' [testlist]
2346 yield_stmt: yield_expr
2347 yield_expr: 'yield' testlist
2348 raise_stmt: 'raise' [test [',' test [',' test]]]
2356 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2358 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2359 case yield_stmt
: { /* will reduce to yield_expr */
2360 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2363 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2367 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2369 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2372 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2377 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2379 else if (NCH(ch
) == 2) {
2380 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2383 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2384 n
->n_col_offset
, c
->c_arena
);
2386 else if (NCH(ch
) == 4) {
2387 expr_ty expr1
, expr2
;
2389 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2392 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2396 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2399 else if (NCH(ch
) == 6) {
2400 expr_ty expr1
, expr2
, expr3
;
2402 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2405 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2408 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2412 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2416 PyErr_Format(PyExc_SystemError
,
2417 "unexpected flow_stmt: %d", TYPE(ch
));
2421 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2426 alias_for_import_name(struct compiling
*c
, const node
*n
, int store
)
2429 import_as_name: NAME ['as' NAME]
2430 dotted_as_name: dotted_name ['as' NAME]
2431 dotted_name: NAME ('.' NAME)*
2433 PyObject
*str
, *name
;
2437 case import_as_name
: {
2438 node
*name_node
= CHILD(n
, 0);
2441 node
*str_node
= CHILD(n
, 2);
2442 if (store
&& !forbidden_check(c
, str_node
, STR(str_node
)))
2444 str
= NEW_IDENTIFIER(str_node
);
2449 if (!forbidden_check(c
, name_node
, STR(name_node
)))
2452 name
= NEW_IDENTIFIER(name_node
);
2455 return alias(name
, str
, c
->c_arena
);
2457 case dotted_as_name
:
2463 node
*asname_node
= CHILD(n
, 2);
2464 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0), 0);
2468 if (!forbidden_check(c
, asname_node
, STR(asname_node
)))
2470 a
->asname
= NEW_IDENTIFIER(asname_node
);
2478 node
*name_node
= CHILD(n
, 0);
2479 if (store
&& !forbidden_check(c
, name_node
, STR(name_node
)))
2481 name
= NEW_IDENTIFIER(name_node
);
2484 return alias(name
, NULL
, c
->c_arena
);
2487 /* Create a string of the form "a.b.c" */
2493 for (i
= 0; i
< NCH(n
); i
+= 2)
2494 /* length of string plus one for the dot */
2495 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2496 len
--; /* the last name doesn't have a dot */
2497 str
= PyString_FromStringAndSize(NULL
, len
);
2500 s
= PyString_AS_STRING(str
);
2503 for (i
= 0; i
< NCH(n
); i
+= 2) {
2504 char *sch
= STR(CHILD(n
, i
));
2505 strcpy(s
, STR(CHILD(n
, i
)));
2511 PyString_InternInPlace(&str
);
2512 PyArena_AddPyObject(c
->c_arena
, str
);
2513 return alias(str
, NULL
, c
->c_arena
);
2517 str
= PyString_InternFromString("*");
2518 PyArena_AddPyObject(c
->c_arena
, str
);
2519 return alias(str
, NULL
, c
->c_arena
);
2521 PyErr_Format(PyExc_SystemError
,
2522 "unexpected import name: %d", TYPE(n
));
2526 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2531 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2534 import_stmt: import_name | import_from
2535 import_name: 'import' dotted_as_names
2536 import_from: 'from' ('.'* dotted_name | '.') 'import'
2537 ('*' | '(' import_as_names ')' | import_as_names)
2544 REQ(n
, import_stmt
);
2546 col_offset
= n
->n_col_offset
;
2548 if (TYPE(n
) == import_name
) {
2550 REQ(n
, dotted_as_names
);
2551 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2554 for (i
= 0; i
< NCH(n
); i
+= 2) {
2555 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2558 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2560 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2562 else if (TYPE(n
) == import_from
) {
2565 alias_ty mod
= NULL
;
2566 identifier modname
= NULL
;
2568 /* Count the number of dots (for relative imports) and check for the
2569 optional module name */
2570 for (idx
= 1; idx
< NCH(n
); idx
++) {
2571 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2572 mod
= alias_for_import_name(c
, CHILD(n
, idx
), 0);
2577 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2582 idx
++; /* skip over the 'import' keyword */
2583 switch (TYPE(CHILD(n
, idx
))) {
2585 /* from ... import * */
2590 /* from ... import (x, y, z) */
2591 n
= CHILD(n
, idx
+ 1);
2592 n_children
= NCH(n
);
2594 case import_as_names
:
2595 /* from ... import x, y, z */
2597 n_children
= NCH(n
);
2598 if (n_children
% 2 == 0) {
2599 ast_error(n
, "trailing comma not allowed without"
2600 " surrounding parentheses");
2605 ast_error(n
, "Unexpected node-type in from-import");
2609 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2613 /* handle "from ... import *" special b/c there's no children */
2614 if (TYPE(n
) == STAR
) {
2615 alias_ty import_alias
= alias_for_import_name(c
, n
, 1);
2618 asdl_seq_SET(aliases
, 0, import_alias
);
2621 for (i
= 0; i
< NCH(n
); i
+= 2) {
2622 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
), 1);
2625 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2629 modname
= mod
->name
;
2630 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2633 PyErr_Format(PyExc_SystemError
,
2634 "unknown import statement: starts with command '%s'",
2640 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2642 /* global_stmt: 'global' NAME (',' NAME)* */
2647 REQ(n
, global_stmt
);
2648 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2651 for (i
= 1; i
< NCH(n
); i
+= 2) {
2652 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2655 asdl_seq_SET(s
, i
/ 2, name
);
2657 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2661 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2663 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2664 int n_children
= NCH(n
);
2665 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2666 PyErr_Format(PyExc_SystemError
,
2667 "poorly formed 'exec' statement: %d parts to statement",
2672 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2674 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2677 if (n_children
>= 4) {
2678 globals
= ast_for_expr(c
, CHILD(n
, 3));
2682 if (n_children
== 6) {
2683 locals
= ast_for_expr(c
, CHILD(n
, 5));
2688 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2693 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2695 /* assert_stmt: 'assert' test [',' test] */
2696 REQ(n
, assert_stmt
);
2698 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2701 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2704 else if (NCH(n
) == 4) {
2705 expr_ty expr1
, expr2
;
2707 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2710 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2714 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2716 PyErr_Format(PyExc_SystemError
,
2717 "improper number of parts to 'assert' statement: %d",
2723 ast_for_suite(struct compiling
*c
, const node
*n
)
2725 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2728 int i
, total
, num
, end
, pos
= 0;
2733 total
= num_stmts(n
);
2734 seq
= asdl_seq_new(total
, c
->c_arena
);
2737 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2739 /* simple_stmt always ends with a NEWLINE,
2740 and may have a trailing SEMI
2743 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2745 /* loop by 2 to skip semi-colons */
2746 for (i
= 0; i
< end
; i
+= 2) {
2748 s
= ast_for_stmt(c
, ch
);
2751 asdl_seq_SET(seq
, pos
++, s
);
2755 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2758 num
= num_stmts(ch
);
2760 /* small_stmt or compound_stmt with only one child */
2761 s
= ast_for_stmt(c
, ch
);
2764 asdl_seq_SET(seq
, pos
++, s
);
2769 REQ(ch
, simple_stmt
);
2770 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2771 /* statement terminates with a semi-colon ';' */
2772 if (NCH(CHILD(ch
, j
)) == 0) {
2773 assert((j
+ 1) == NCH(ch
));
2776 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2779 asdl_seq_SET(seq
, pos
++, s
);
2784 assert(pos
== seq
->size
);
2789 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2791 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2800 asdl_seq
*suite_seq
;
2802 expression
= ast_for_expr(c
, CHILD(n
, 1));
2805 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2809 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2813 s
= STR(CHILD(n
, 4));
2814 /* s[2], the third character in the string, will be
2820 asdl_seq
*seq1
, *seq2
;
2822 expression
= ast_for_expr(c
, CHILD(n
, 1));
2825 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2828 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2832 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2835 else if (s
[2] == 'i') {
2836 int i
, n_elif
, has_else
= 0;
2838 asdl_seq
*suite_seq
;
2839 asdl_seq
*orelse
= NULL
;
2840 n_elif
= NCH(n
) - 4;
2841 /* must reference the child n_elif+1 since 'else' token is third,
2842 not fourth, child from the end. */
2843 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2844 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2851 asdl_seq
*suite_seq2
;
2853 orelse
= asdl_seq_new(1, c
->c_arena
);
2856 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2859 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2862 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2866 asdl_seq_SET(orelse
, 0,
2867 If(expression
, suite_seq
, suite_seq2
,
2868 LINENO(CHILD(n
, NCH(n
) - 6)),
2869 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2871 /* the just-created orelse handled the last elif */
2875 for (i
= 0; i
< n_elif
; i
++) {
2876 int off
= 5 + (n_elif
- i
- 1) * 4;
2877 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2880 expression
= ast_for_expr(c
, CHILD(n
, off
));
2883 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2887 asdl_seq_SET(newobj
, 0,
2888 If(expression
, suite_seq
, orelse
,
2889 LINENO(CHILD(n
, off
)),
2890 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2893 expression
= ast_for_expr(c
, CHILD(n
, 1));
2896 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2899 return If(expression
, suite_seq
, orelse
,
2900 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2903 PyErr_Format(PyExc_SystemError
,
2904 "unexpected token in 'if' statement: %s", s
);
2909 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2911 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2916 asdl_seq
*suite_seq
;
2918 expression
= ast_for_expr(c
, CHILD(n
, 1));
2921 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2924 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2927 else if (NCH(n
) == 7) {
2929 asdl_seq
*seq1
, *seq2
;
2931 expression
= ast_for_expr(c
, CHILD(n
, 1));
2934 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2937 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2941 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2945 PyErr_Format(PyExc_SystemError
,
2946 "wrong number of tokens for 'while' statement: %d",
2952 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2954 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2956 expr_ty target
, first
;
2957 const node
*node_target
;
2958 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2962 seq
= ast_for_suite(c
, CHILD(n
, 8));
2967 node_target
= CHILD(n
, 1);
2968 _target
= ast_for_exprlist(c
, node_target
, Store
);
2971 /* Check the # of children rather than the length of _target, since
2972 for x, in ... has 1 element in _target, but still requires a Tuple. */
2973 first
= (expr_ty
)asdl_seq_GET(_target
, 0);
2974 if (NCH(node_target
) == 1)
2977 target
= Tuple(_target
, Store
, first
->lineno
, first
->col_offset
, c
->c_arena
);
2979 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2982 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2986 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2990 static excepthandler_ty
2991 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2993 /* except_clause: 'except' [test [(',' | 'as') test]] */
2994 REQ(exc
, except_clause
);
2997 if (NCH(exc
) == 1) {
2998 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
3002 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
3003 exc
->n_col_offset
, c
->c_arena
);
3005 else if (NCH(exc
) == 2) {
3007 asdl_seq
*suite_seq
;
3009 expression
= ast_for_expr(c
, CHILD(exc
, 1));
3012 suite_seq
= ast_for_suite(c
, body
);
3016 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
3017 exc
->n_col_offset
, c
->c_arena
);
3019 else if (NCH(exc
) == 4) {
3020 asdl_seq
*suite_seq
;
3022 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
3025 if (!set_context(c
, e
, Store
, CHILD(exc
, 3)))
3027 expression
= ast_for_expr(c
, CHILD(exc
, 1));
3030 suite_seq
= ast_for_suite(c
, body
);
3034 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
3035 exc
->n_col_offset
, c
->c_arena
);
3038 PyErr_Format(PyExc_SystemError
,
3039 "wrong number of children for 'except' clause: %d",
3045 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
3047 const int nch
= NCH(n
);
3048 int n_except
= (nch
- 3)/3;
3049 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
3053 body
= ast_for_suite(c
, CHILD(n
, 2));
3057 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
3058 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
3059 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
3060 /* we can assume it's an "else",
3061 because nch >= 9 for try-else-finally and
3062 it would otherwise have a type of except_clause */
3063 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
3069 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
3070 if (finally
== NULL
)
3075 /* we can assume it's an "else",
3076 otherwise it would have a type of except_clause */
3077 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
3083 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
3084 ast_error(n
, "malformed 'try' statement");
3091 /* process except statements to create a try ... except */
3092 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
3093 if (handlers
== NULL
)
3096 for (i
= 0; i
< n_except
; i
++) {
3097 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
3098 CHILD(n
, 5 + i
* 3));
3101 asdl_seq_SET(handlers
, i
, e
);
3104 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
3105 n
->n_col_offset
, c
->c_arena
);
3109 /* if a 'finally' is present too, we nest the TryExcept within a
3110 TryFinally to emulate try ... except ... finally */
3111 body
= asdl_seq_new(1, c
->c_arena
);
3114 asdl_seq_SET(body
, 0, except_st
);
3117 /* must be a try ... finally (except clauses are in body, if any exist) */
3118 assert(finally
!= NULL
);
3119 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3122 /* with_item: test ['as' expr] */
3124 ast_for_with_item(struct compiling
*c
, const node
*n
, asdl_seq
*content
)
3126 expr_ty context_expr
, optional_vars
= NULL
;
3129 context_expr
= ast_for_expr(c
, CHILD(n
, 0));
3133 optional_vars
= ast_for_expr(c
, CHILD(n
, 2));
3135 if (!optional_vars
) {
3138 if (!set_context(c
, optional_vars
, Store
, n
)) {
3143 return With(context_expr
, optional_vars
, content
, LINENO(n
),
3144 n
->n_col_offset
, c
->c_arena
);
3147 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3149 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
3157 /* process the with items inside-out */
3159 /* the suite of the innermost with item is the suite of the with stmt */
3160 inner
= ast_for_suite(c
, CHILD(n
, i
));
3166 ret
= ast_for_with_item(c
, CHILD(n
, i
), inner
);
3169 /* was this the last item? */
3172 /* if not, wrap the result so far in a new sequence */
3173 inner
= asdl_seq_new(1, c
->c_arena
);
3176 asdl_seq_SET(inner
, 0, ret
);
3183 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
3185 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3186 PyObject
*classname
;
3187 asdl_seq
*bases
, *s
;
3191 if (!forbidden_check(c
, n
, STR(CHILD(n
, 1))))
3195 s
= ast_for_suite(c
, CHILD(n
, 3));
3198 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3201 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3202 n
->n_col_offset
, c
->c_arena
);
3204 /* check for empty base list */
3205 if (TYPE(CHILD(n
,3)) == RPAR
) {
3206 s
= ast_for_suite(c
, CHILD(n
,5));
3209 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3212 return ClassDef(classname
, NULL
, s
, decorator_seq
, LINENO(n
),
3213 n
->n_col_offset
, c
->c_arena
);
3216 /* else handle the base class list */
3217 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3221 s
= ast_for_suite(c
, CHILD(n
, 6));
3224 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3227 return ClassDef(classname
, bases
, s
, decorator_seq
,
3228 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3232 ast_for_stmt(struct compiling
*c
, const node
*n
)
3234 if (TYPE(n
) == stmt
) {
3235 assert(NCH(n
) == 1);
3238 if (TYPE(n
) == simple_stmt
) {
3239 assert(num_stmts(n
) == 1);
3242 if (TYPE(n
) == small_stmt
) {
3244 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3245 | flow_stmt | import_stmt | global_stmt | exec_stmt
3250 return ast_for_expr_stmt(c
, n
);
3252 return ast_for_print_stmt(c
, n
);
3254 return ast_for_del_stmt(c
, n
);
3256 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3258 return ast_for_flow_stmt(c
, n
);
3260 return ast_for_import_stmt(c
, n
);
3262 return ast_for_global_stmt(c
, n
);
3264 return ast_for_exec_stmt(c
, n
);
3266 return ast_for_assert_stmt(c
, n
);
3268 PyErr_Format(PyExc_SystemError
,
3269 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3275 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3276 | funcdef | classdef | decorated
3278 node
*ch
= CHILD(n
, 0);
3279 REQ(n
, compound_stmt
);
3282 return ast_for_if_stmt(c
, ch
);
3284 return ast_for_while_stmt(c
, ch
);
3286 return ast_for_for_stmt(c
, ch
);
3288 return ast_for_try_stmt(c
, ch
);
3290 return ast_for_with_stmt(c
, ch
);
3292 return ast_for_funcdef(c
, ch
, NULL
);
3294 return ast_for_classdef(c
, ch
, NULL
);
3296 return ast_for_decorated(c
, ch
);
3298 PyErr_Format(PyExc_SystemError
,
3299 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3307 parsenumber(struct compiling
*c
, const char *s
)
3312 #ifndef WITHOUT_COMPLEX
3319 end
= s
+ strlen(s
) - 1;
3320 #ifndef WITHOUT_COMPLEX
3321 imflag
= *end
== 'j' || *end
== 'J';
3323 if (*end
== 'l' || *end
== 'L')
3324 return PyLong_FromString((char *)s
, (char **)0, 0);
3325 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3328 return PyLong_FromString((char *)s
, (char **)0, 0);
3329 return PyInt_FromLong(x
);
3331 /* XXX Huge floats may silently fail */
3332 #ifndef WITHOUT_COMPLEX
3335 complex.imag
= PyOS_string_to_double(s
, (char **)&end
, NULL
);
3336 if (complex.imag
== -1.0 && PyErr_Occurred())
3338 return PyComplex_FromCComplex(complex);
3343 dx
= PyOS_string_to_double(s
, NULL
, NULL
);
3344 if (dx
== -1.0 && PyErr_Occurred())
3346 return PyFloat_FromDouble(dx
);
3351 decode_utf8(struct compiling
*c
, const char **sPtr
, const char *end
, char* encoding
)
3353 #ifndef Py_USING_UNICODE
3354 Py_FatalError("decode_utf8 should not be called in this build.");
3359 t
= s
= (char *)*sPtr
;
3360 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3361 while (s
< end
&& (*s
& 0x80)) s
++;
3363 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3366 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3372 #ifdef Py_USING_UNICODE
3374 decode_unicode(struct compiling
*c
, const char *s
, size_t len
, int rawmode
, const char *encoding
)
3381 if (encoding
!= NULL
&& strcmp(encoding
, "iso-8859-1")) {
3382 /* check for integer overflow */
3383 if (len
> PY_SIZE_MAX
/ 6)
3385 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3386 "\รค" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3387 u
= PyString_FromStringAndSize((char *)NULL
, len
* 6);
3390 p
= buf
= PyString_AsString(u
);
3400 if (*s
& 0x80) { /* XXX inefficient */
3404 w
= decode_utf8(c
, &s
, end
, "utf-32-be");
3409 r
= PyString_AsString(w
);
3410 rn
= PyString_Size(w
);
3411 assert(rn
% 4 == 0);
3412 for (i
= 0; i
< rn
; i
+= 4) {
3413 sprintf(p
, "\\U%02x%02x%02x%02x",
3429 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3431 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3437 /* s is a Python string literal, including the bracketing quote characters,
3438 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3439 * parsestr parses it, and returns the decoded Python string object.
3442 parsestr(struct compiling
*c
, const char *s
)
3445 int quote
= Py_CHARMASK(*s
);
3448 int unicode
= c
->c_future_unicode
;
3450 if (isalpha(quote
) || quote
== '_') {
3451 if (quote
== 'u' || quote
== 'U') {
3455 if (quote
== 'b' || quote
== 'B') {
3459 if (quote
== 'r' || quote
== 'R') {
3464 if (quote
!= '\'' && quote
!= '\"') {
3465 PyErr_BadInternalCall();
3470 if (len
> INT_MAX
) {
3471 PyErr_SetString(PyExc_OverflowError
,
3472 "string to parse is too long");
3475 if (s
[--len
] != quote
) {
3476 PyErr_BadInternalCall();
3479 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3482 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3483 PyErr_BadInternalCall();
3487 #ifdef Py_USING_UNICODE
3488 if (unicode
|| Py_UnicodeFlag
) {
3489 return decode_unicode(c
, s
, len
, rawmode
, c
->c_encoding
);
3492 need_encoding
= (c
->c_encoding
!= NULL
&&
3493 strcmp(c
->c_encoding
, "utf-8") != 0 &&
3494 strcmp(c
->c_encoding
, "iso-8859-1") != 0);
3495 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3496 if (need_encoding
) {
3497 #ifndef Py_USING_UNICODE
3498 /* This should not happen - we never see any other
3501 "cannot deal with encodings in this build.");
3503 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3506 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3511 return PyString_FromStringAndSize(s
, len
);
3515 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3516 need_encoding
? c
->c_encoding
: NULL
);
3519 /* Build a Python string object out of a STRING atom. This takes care of
3520 * compile-time literal catenation, calling parsestr() on each piece, and
3521 * pasting the intermediate results together.
3524 parsestrplus(struct compiling
*c
, const node
*n
)
3528 REQ(CHILD(n
, 0), STRING
);
3529 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
3530 /* String literal concatenation */
3531 for (i
= 1; i
< NCH(n
); i
++) {
3533 s
= parsestr(c
, STR(CHILD(n
, i
)));
3536 if (PyString_Check(v
) && PyString_Check(s
)) {
3537 PyString_ConcatAndDel(&v
, s
);
3541 #ifdef Py_USING_UNICODE
3543 PyObject
*temp
= PyUnicode_Concat(v
, s
);