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"
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
24 /* Data structure used internally */
26 char *c_encoding
; /* source encoding */
27 PyArena
*c_arena
; /* arena for allocating memeory */
30 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
31 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
32 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
33 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
34 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*, int);
35 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
36 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
38 /* Note different signature for ast_for_call */
39 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
41 static PyObject
*parsenumber(const char *);
42 static PyObject
*parsestr(const char *s
, const char *encoding
);
43 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
46 #define LINENO(n) ((n)->n_lineno)
50 new_identifier(const char* n
, PyArena
*arena
) {
51 PyObject
* id
= PyString_InternFromString(n
);
52 PyArena_AddPyObject(arena
, id
);
56 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
58 /* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
63 XXX Maybe we should just pass the filename...
67 ast_error(const node
*n
, const char *errstr
)
69 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
72 PyErr_SetObject(PyExc_SyntaxError
, u
);
78 ast_error_finish(const char *filename
)
80 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
87 PyErr_Fetch(&type
, &value
, &tback
);
88 errstr
= PyTuple_GetItem(value
, 0);
92 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
99 loc
= PyErr_ProgramText(filename
, lineno
);
104 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
110 value
= Py_BuildValue("(OO)", errstr
, tmp
);
115 PyErr_Restore(type
, value
, tback
);
118 /* num_stmts() returns number of contained statements.
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
133 num_stmts(const node
*n
)
140 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
143 return num_stmts(CHILD(n
, 0));
146 for (i
= 0; i
< NCH(n
); i
++) {
148 if (TYPE(ch
) == stmt
)
153 return num_stmts(CHILD(n
, 0));
157 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
160 return num_stmts(CHILD(n
, 0));
163 for (i
= 2; i
< (NCH(n
) - 1); i
++)
164 l
+= num_stmts(CHILD(n
, i
));
170 sprintf(buf
, "Non-statement found: %d %d\n",
179 /* Transform the CST rooted at node * to the appropriate AST
183 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
187 asdl_seq
*stmts
= NULL
;
192 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
193 c
.c_encoding
= "utf-8";
194 } else if (TYPE(n
) == encoding_decl
) {
195 c
.c_encoding
= STR(n
);
204 stmts
= asdl_seq_new(num_stmts(n
), arena
);
207 for (i
= 0; i
< NCH(n
) - 1; i
++) {
209 if (TYPE(ch
) == NEWLINE
)
214 s
= ast_for_stmt(&c
, ch
);
217 asdl_seq_APPEND(stmts
, s
);
221 REQ(ch
, simple_stmt
);
222 for (j
= 0; j
< num
; j
++) {
223 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
226 asdl_seq_APPEND(stmts
, s
);
230 return Module(stmts
, arena
);
232 expr_ty testlist_ast
;
234 /* XXX Why not gen_for here? */
235 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
238 return Expression(testlist_ast
, arena
);
241 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
242 stmts
= asdl_seq_new(1, arena
);
245 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, arena
));
246 return Interactive(stmts
, arena
);
251 stmts
= asdl_seq_new(num
, arena
);
255 s
= ast_for_stmt(&c
, n
);
258 asdl_seq_SET(stmts
, 0, s
);
261 /* Only a simple_stmt can contain multiple statements. */
263 for (i
= 0; i
< NCH(n
); i
+= 2) {
264 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
266 s
= ast_for_stmt(&c
, CHILD(n
, i
));
269 asdl_seq_SET(stmts
, i
/ 2, s
);
273 return Interactive(stmts
, arena
);
279 ast_error_finish(filename
);
283 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
287 get_operator(const node
*n
)
317 /* Set the context ctx for expr_ty e returning 0 on success, -1 on error.
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
323 If e is a sequential type, items in sequence will also have their context
329 set_context(expr_ty e
, expr_context_ty ctx
, const node
*n
)
336 !strcmp(PyString_AS_STRING(e
->v
.Attribute
.attr
), "None")) {
337 return ast_error(n
, "assignment to None");
339 e
->v
.Attribute
.ctx
= ctx
;
342 e
->v
.Subscript
.ctx
= ctx
;
346 !strcmp(PyString_AS_STRING(e
->v
.Name
.id
), "None")) {
347 return ast_error(n
, "assignment to None");
356 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
357 return ast_error(n
, "can't assign to ()");
358 e
->v
.Tuple
.ctx
= ctx
;
363 return ast_error(n
, "can't assign to function call");
365 return ast_error(n
, "can't delete function call");
367 return ast_error(n
, "unexpected operation on function call");
370 return ast_error(n
, "can't assign to operator");
371 case GeneratorExp_kind
:
372 return ast_error(n
, "assignment to generator expression "
376 return ast_error(n
, "can't assign to literal");
379 PyOS_snprintf(buf
, sizeof(buf
),
380 "unexpected expression in assignment %d (line %d)",
382 return ast_error(n
, buf
);
385 /* If the LHS is a list or tuple, we need to set the assignment
386 context for all the tuple elements.
391 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
392 if (!set_context(asdl_seq_GET(s
, i
), ctx
, n
))
400 ast_for_augassign(const node
*n
)
410 if (STR(n
)[1] == '/')
427 if (STR(n
)[1] == '*')
432 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
438 ast_for_comp_op(const node
*n
)
440 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
451 case EQEQUAL
: /* == */
460 if (strcmp(STR(n
), "in") == 0)
462 if (strcmp(STR(n
), "is") == 0)
465 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
470 else if (NCH(n
) == 2) {
471 /* handle "not in" and "is not" */
472 switch (TYPE(CHILD(n
, 0))) {
474 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
476 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
479 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
480 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
484 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
490 seq_for_testlist(struct compiling
*c
, const node
*n
)
492 /* testlist: test (',' test)* [','] */
496 assert(TYPE(n
) == testlist
497 || TYPE(n
) == listmaker
498 || TYPE(n
) == testlist_gexp
499 || TYPE(n
) == testlist_safe
502 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
506 for (i
= 0; i
< NCH(n
); i
+= 2) {
507 REQ(CHILD(n
, i
), test
);
509 expression
= ast_for_expr(c
, CHILD(n
, i
));
513 assert(i
/ 2 < seq
->size
);
514 asdl_seq_SET(seq
, i
/ 2, expression
);
520 compiler_complex_args(struct compiling
*c
, const node
*n
)
522 int i
, len
= (NCH(n
) + 1) / 2;
524 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
530 for (i
= 0; i
< len
; i
++) {
531 const node
*child
= CHILD(CHILD(n
, 2*i
), 0);
533 if (TYPE(child
) == NAME
) {
534 if (!strcmp(STR(child
), "None")) {
535 ast_error(child
, "assignment to None");
538 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
),
542 arg
= compiler_complex_args(c
, CHILD(CHILD(n
, 2*i
), 1));
543 set_context(arg
, Store
, n
);
544 asdl_seq_SET(args
, i
, arg
);
547 result
= Tuple(args
, Store
, LINENO(n
), c
->c_arena
);
548 set_context(result
, Store
, n
);
552 /* Create AST for argument list.
555 - check for invalid argument lists like normal after default
559 ast_for_arguments(struct compiling
*c
, const node
*n
)
561 /* parameters: '(' [varargslist] ')'
562 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
563 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
565 int i
, n_args
= 0, n_defaults
= 0, found_default
= 0;
566 asdl_seq
*args
, *defaults
;
567 identifier vararg
= NULL
, kwarg
= NULL
;
570 if (TYPE(n
) == parameters
) {
571 if (NCH(n
) == 2) /* () as argument list */
572 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
577 /* first count the number of normal args & defaults */
578 for (i
= 0; i
< NCH(n
); i
++) {
580 if (TYPE(ch
) == fpdef
)
582 if (TYPE(ch
) == EQUAL
)
585 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
587 return NULL
; /* Don't need to go to NULL; nothing allocated */
588 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
589 if (!defaults
&& n_defaults
)
592 /* fpdef: NAME | '(' fplist ')'
593 fplist: fpdef (',' fpdef)* [',']
600 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
601 anything other than EQUAL or a comma? */
602 /* XXX Should NCH(n) check be made a separate check? */
603 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
604 asdl_seq_APPEND(defaults
,
605 ast_for_expr(c
, CHILD(n
, i
+ 2)));
609 else if (found_default
) {
611 "non-default argument follows default argument");
616 asdl_seq_APPEND(args
,
617 compiler_complex_args(c
, CHILD(ch
, 1)));
619 else if (TYPE(CHILD(ch
, 0)) == NAME
) {
621 if (!strcmp(STR(CHILD(ch
, 0)), "None")) {
622 ast_error(CHILD(ch
, 0), "assignment to None");
625 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
626 Param
, LINENO(ch
), c
->c_arena
);
629 asdl_seq_APPEND(args
, name
);
632 i
+= 2; /* the name and the comma */
635 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
636 ast_error(CHILD(n
, i
+1), "assignment to None");
639 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
643 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
644 ast_error(CHILD(n
, i
+1), "assignment to None");
647 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
651 PyErr_Format(PyExc_SystemError
,
652 "unexpected node in varargslist: %d @ %d",
658 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
667 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
675 id
= NEW_IDENTIFIER(CHILD(n
, 0));
678 e
= Name(id
, Load
, LINENO(n
), c
->c_arena
);
682 for (i
= 2; i
< NCH(n
); i
+=2) {
683 id
= NEW_IDENTIFIER(CHILD(n
, i
));
686 e
= Attribute(e
, id
, Load
, LINENO(CHILD(n
, i
)), c
->c_arena
);
695 ast_for_decorator(struct compiling
*c
, const node
*n
)
697 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
703 if ((NCH(n
) < 3 && NCH(n
) != 5 && NCH(n
) != 6)
704 || TYPE(CHILD(n
, 0)) != AT
|| TYPE(RCHILD(n
, -1)) != NEWLINE
) {
705 ast_error(n
, "Invalid decorator node");
709 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
713 if (NCH(n
) == 3) { /* No arguments */
717 else if (NCH(n
) == 5) { /* Call with no arguments */
718 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
), c
->c_arena
);
724 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
734 ast_for_decorators(struct compiling
*c
, const node
*n
)
736 asdl_seq
* decorator_seq
;
742 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
746 for (i
= 0; i
< NCH(n
); i
++) {
747 d
= ast_for_decorator(c
, CHILD(n
, i
));
750 asdl_seq_APPEND(decorator_seq
, d
);
752 return decorator_seq
;
756 ast_for_funcdef(struct compiling
*c
, const node
*n
)
758 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
762 asdl_seq
*decorator_seq
= NULL
;
767 if (NCH(n
) == 6) { /* decorators are present */
768 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
777 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
780 else if (!strcmp(STR(CHILD(n
, name_i
)), "None")) {
781 ast_error(CHILD(n
, name_i
), "assignment to None");
784 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
787 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
791 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
), c
->c_arena
);
795 ast_for_lambdef(struct compiling
*c
, const node
*n
)
797 /* lambdef: 'lambda' [varargslist] ':' test */
802 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
805 expression
= ast_for_expr(c
, CHILD(n
, 2));
810 args
= ast_for_arguments(c
, CHILD(n
, 1));
813 expression
= ast_for_expr(c
, CHILD(n
, 3));
818 return Lambda(args
, expression
, LINENO(n
), c
->c_arena
);
821 /* Count the number of 'for' loop in a list comprehension.
823 Helper for ast_for_listcomp().
827 count_list_fors(const node
*n
)
830 node
*ch
= CHILD(n
, 1);
842 if (TYPE(ch
) == list_for
)
844 else if (TYPE(ch
) == list_if
) {
847 goto count_list_iter
;
853 /* Should never be reached */
854 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
858 /* Count the number of 'if' statements in a list comprehension.
860 Helper for ast_for_listcomp().
864 count_list_ifs(const node
*n
)
870 if (TYPE(CHILD(n
, 0)) == list_for
)
878 goto count_list_iter
;
882 ast_for_listcomp(struct compiling
*c
, const node
*n
)
884 /* listmaker: test ( list_for | (',' test)* [','] )
885 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
886 list_iter: list_for | list_if
887 list_if: 'if' test [list_iter]
888 testlist_safe: test [(',' test)+ [',']]
898 elt
= ast_for_expr(c
, CHILD(n
, 0));
902 n_fors
= count_list_fors(n
);
906 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
911 for (i
= 0; i
< n_fors
; i
++) {
918 t
= ast_for_exprlist(c
, CHILD(ch
, 1), Store
);
921 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
925 if (asdl_seq_LEN(t
) == 1)
926 lc
= comprehension(asdl_seq_GET(t
, 0), expression
, NULL
,
929 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), c
->c_arena
),
930 expression
, NULL
, c
->c_arena
);
939 n_ifs
= count_list_ifs(ch
);
943 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
947 for (j
= 0; j
< n_ifs
; j
++) {
953 asdl_seq_APPEND(ifs
, ast_for_expr(c
, CHILD(ch
, 1)));
957 /* on exit, must guarantee that ch is a list_for */
958 if (TYPE(ch
) == list_iter
)
962 asdl_seq_APPEND(listcomps
, lc
);
965 return ListComp(elt
, listcomps
, LINENO(n
), c
->c_arena
);
969 Count the number of 'for' loops in a generator expression.
971 Helper for ast_for_genexp().
975 count_gen_fors(const node
*n
)
978 node
*ch
= CHILD(n
, 1);
990 if (TYPE(ch
) == gen_for
)
992 else if (TYPE(ch
) == gen_if
) {
1001 /* Should never be reached */
1002 PyErr_SetString(PyExc_SystemError
,
1003 "logic error in count_gen_fors");
1007 /* Count the number of 'if' statements in a generator expression.
1009 Helper for ast_for_genexp().
1013 count_gen_ifs(const node
*n
)
1019 if (TYPE(CHILD(n
, 0)) == gen_for
)
1031 ast_for_genexp(struct compiling
*c
, const node
*n
)
1033 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1034 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1040 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1043 elt
= ast_for_expr(c
, CHILD(n
, 0));
1047 n_fors
= count_gen_fors(n
);
1051 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1056 for (i
= 0; i
< n_fors
; i
++) {
1057 comprehension_ty ge
;
1063 t
= ast_for_exprlist(c
, CHILD(ch
, 1), Store
);
1066 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1070 if (asdl_seq_LEN(t
) == 1)
1071 ge
= comprehension(asdl_seq_GET(t
, 0), expression
,
1074 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), c
->c_arena
),
1075 expression
, NULL
, c
->c_arena
);
1085 n_ifs
= count_gen_ifs(ch
);
1089 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1093 for (j
= 0; j
< n_ifs
; j
++) {
1098 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1101 asdl_seq_APPEND(ifs
, expression
);
1105 /* on exit, must guarantee that ch is a gen_for */
1106 if (TYPE(ch
) == gen_iter
)
1110 asdl_seq_APPEND(genexps
, ge
);
1113 return GeneratorExp(elt
, genexps
, LINENO(n
), c
->c_arena
);
1117 ast_for_atom(struct compiling
*c
, const node
*n
)
1119 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1120 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1122 node
*ch
= CHILD(n
, 0);
1126 /* All names start in Load context, but may later be
1128 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), c
->c_arena
);
1130 PyObject
*str
= parsestrplus(c
, n
);
1134 PyArena_AddPyObject(c
->c_arena
, str
);
1135 return Str(str
, LINENO(n
), c
->c_arena
);
1138 PyObject
*pynum
= parsenumber(STR(ch
));
1142 PyArena_AddPyObject(c
->c_arena
, pynum
);
1143 return Num(pynum
, LINENO(n
), c
->c_arena
);
1145 case LPAR
: /* some parenthesized expressions */
1148 if (TYPE(ch
) == RPAR
)
1149 return Tuple(NULL
, Load
, LINENO(n
), c
->c_arena
);
1151 if (TYPE(ch
) == yield_expr
)
1152 return ast_for_expr(c
, ch
);
1154 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1155 return ast_for_genexp(c
, ch
);
1157 return ast_for_testlist_gexp(c
, ch
);
1158 case LSQB
: /* list (or list comprehension) */
1161 if (TYPE(ch
) == RSQB
)
1162 return List(NULL
, Load
, LINENO(n
), c
->c_arena
);
1165 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1166 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1170 return List(elts
, Load
, LINENO(n
), c
->c_arena
);
1173 return ast_for_listcomp(c
, ch
);
1175 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1177 asdl_seq
*keys
, *values
;
1180 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1181 keys
= asdl_seq_new(size
, c
->c_arena
);
1185 values
= asdl_seq_new(size
, c
->c_arena
);
1189 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1192 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1196 asdl_seq_SET(keys
, i
/ 4, expression
);
1198 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1202 asdl_seq_SET(values
, i
/ 4, expression
);
1204 return Dict(keys
, values
, LINENO(n
), c
->c_arena
);
1206 case BACKQUOTE
: { /* repr */
1207 expr_ty expression
= ast_for_testlist(c
, CHILD(n
, 1));
1211 return Repr(expression
, LINENO(n
), c
->c_arena
);
1214 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1220 ast_for_slice(struct compiling
*c
, const node
*n
)
1223 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1228 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1232 if (TYPE(ch
) == DOT
)
1233 return Ellipsis(c
->c_arena
);
1235 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1236 /* 'step' variable hold no significance in terms of being used over
1238 step
= ast_for_expr(c
, ch
);
1242 return Index(step
, c
->c_arena
);
1245 if (TYPE(ch
) == test
) {
1246 lower
= ast_for_expr(c
, ch
);
1251 /* If there's an upper bound it's in the second or third position. */
1252 if (TYPE(ch
) == COLON
) {
1254 node
*n2
= CHILD(n
, 1);
1256 if (TYPE(n2
) == test
) {
1257 upper
= ast_for_expr(c
, n2
);
1262 } else if (NCH(n
) > 2) {
1263 node
*n2
= CHILD(n
, 2);
1265 if (TYPE(n2
) == test
) {
1266 upper
= ast_for_expr(c
, n2
);
1272 ch
= CHILD(n
, NCH(n
) - 1);
1273 if (TYPE(ch
) == sliceop
) {
1275 /* XXX: If only 1 child, then should just be a colon. Should we
1276 just skip assigning and just get to the return? */
1280 if (TYPE(ch
) == test
) {
1281 step
= ast_for_expr(c
, ch
);
1287 return Slice(lower
, upper
, step
, c
->c_arena
);
1291 ast_for_binop(struct compiling
*c
, const node
*n
)
1293 /* Must account for a sequence of expressions.
1294 How should A op B op C by represented?
1295 BinOp(BinOp(A, op, B), op, C).
1299 expr_ty expr1
, expr2
, result
;
1300 operator_ty
operator;
1302 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1306 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1310 operator = get_operator(CHILD(n
, 1));
1314 result
= BinOp(expr1
, operator, expr2
, LINENO(n
), c
->c_arena
);
1318 nops
= (NCH(n
) - 1) / 2;
1319 for (i
= 1; i
< nops
; i
++) {
1320 expr_ty tmp_result
, tmp
;
1321 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1323 operator = get_operator(next_oper
);
1327 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1331 tmp_result
= BinOp(result
, operator, tmp
,
1332 LINENO(next_oper
), c
->c_arena
);
1335 result
= tmp_result
;
1341 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1343 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1346 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1348 e
= Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
), c
->c_arena
);
1350 e
= ast_for_call(c
, CHILD(n
, 1), left_expr
);
1352 else if (TYPE(CHILD(n
, 0)) == LSQB
) {
1353 REQ(CHILD(n
, 2), RSQB
);
1356 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1359 e
= Subscript(left_expr
, slc
, Load
, LINENO(n
), c
->c_arena
);
1366 asdl_seq
*slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1369 for (j
= 0; j
< NCH(n
); j
+= 2) {
1370 slc
= ast_for_slice(c
, CHILD(n
, j
));
1373 asdl_seq_SET(slices
, j
/ 2, slc
);
1375 e
= Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1376 Load
, LINENO(n
), c
->c_arena
);
1382 assert(TYPE(CHILD(n
, 0)) == DOT
);
1383 e
= Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
, LINENO(n
),
1390 ast_for_power(struct compiling
*c
, const node
*n
)
1392 /* power: atom trailer* ('**' factor)*
1397 e
= ast_for_atom(c
, CHILD(n
, 0));
1402 for (i
= 1; i
< NCH(n
); i
++) {
1403 node
*ch
= CHILD(n
, i
);
1404 if (TYPE(ch
) != trailer
)
1406 tmp
= ast_for_trailer(c
, ch
, e
);
1411 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1412 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1415 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), c
->c_arena
);
1423 /* Do not name a variable 'expr'! Will cause a compile error.
1427 ast_for_expr(struct compiling
*c
, const node
*n
)
1429 /* handle the full range of simple expressions
1430 test: and_test ('or' and_test)* | lambdef
1431 and_test: not_test ('and' not_test)*
1432 not_test: 'not' not_test | comparison
1433 comparison: expr (comp_op expr)*
1434 expr: xor_expr ('|' xor_expr)*
1435 xor_expr: and_expr ('^' and_expr)*
1436 and_expr: shift_expr ('&' shift_expr)*
1437 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1438 arith_expr: term (('+'|'-') term)*
1439 term: factor (('*'|'/'|'%'|'//') factor)*
1440 factor: ('+'|'-'|'~') factor | power
1441 power: atom trailer* ('**' factor)*
1450 if (TYPE(CHILD(n
, 0)) == lambdef
)
1451 return ast_for_lambdef(c
, CHILD(n
, 0));
1452 /* Fall through to and_test */
1458 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1461 for (i
= 0; i
< NCH(n
); i
+= 2) {
1462 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1465 asdl_seq_SET(seq
, i
/ 2, e
);
1467 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1468 return BoolOp(And
, seq
, LINENO(n
), c
->c_arena
);
1469 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1470 return BoolOp(Or
, seq
, LINENO(n
), c
->c_arena
);
1477 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1481 return UnaryOp(Not
, expression
, LINENO(n
), c
->c_arena
);
1490 asdl_seq
*ops
, *cmps
;
1491 ops
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1494 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1498 for (i
= 1; i
< NCH(n
); i
+= 2) {
1499 /* XXX cmpop_ty is just an enum */
1502 operator = ast_for_comp_op(CHILD(n
, i
));
1507 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1512 asdl_seq_SET(ops
, i
/ 2, (void *)(Py_uintptr_t
)operator);
1513 asdl_seq_SET(cmps
, i
/ 2, expression
);
1515 expression
= ast_for_expr(c
, CHILD(n
, 0));
1520 return Compare(expression
, ops
, cmps
, LINENO(n
), c
->c_arena
);
1524 /* The next five cases all handle BinOps. The main body of code
1525 is the same in each case, but the switch turned inside out to
1526 reuse the code for each type of operator.
1538 return ast_for_binop(c
, n
);
1542 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1546 return Yield(exp
, LINENO(n
), c
->c_arena
);
1556 expression
= ast_for_expr(c
, CHILD(n
, 1));
1560 switch (TYPE(CHILD(n
, 0))) {
1562 return UnaryOp(UAdd
, expression
, LINENO(n
), c
->c_arena
);
1564 return UnaryOp(USub
, expression
, LINENO(n
), c
->c_arena
);
1566 return UnaryOp(Invert
, expression
, LINENO(n
), c
->c_arena
);
1568 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1573 return ast_for_power(c
, n
);
1575 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1578 /* should never get here unless if error is set */
1583 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1586 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1588 argument: [test '='] test [gen_for] # Really [keyword '='] test
1591 int i
, nargs
, nkeywords
, ngens
;
1594 expr_ty vararg
= NULL
, kwarg
= NULL
;
1601 for (i
= 0; i
< NCH(n
); i
++) {
1602 node
*ch
= CHILD(n
, i
);
1603 if (TYPE(ch
) == argument
) {
1606 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1612 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1613 ast_error(n
, "Generator expression must be parenthesised "
1614 "if not sole argument");
1618 if (nargs
+ nkeywords
+ ngens
> 255) {
1619 ast_error(n
, "more than 255 arguments");
1623 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1626 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1631 for (i
= 0; i
< NCH(n
); i
++) {
1632 node
*ch
= CHILD(n
, i
);
1633 if (TYPE(ch
) == argument
) {
1636 e
= ast_for_expr(c
, CHILD(ch
, 0));
1639 asdl_seq_SET(args
, nargs
++, e
);
1641 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1642 e
= ast_for_genexp(c
, ch
);
1645 asdl_seq_SET(args
, nargs
++, e
);
1651 /* CHILD(ch, 0) is test, but must be an identifier? */
1652 e
= ast_for_expr(c
, CHILD(ch
, 0));
1655 /* f(lambda x: x[0] = 3) ends up getting parsed with
1656 * LHS test = lambda x: x[0], and RHS test = 3.
1657 * SF bug 132313 points out that complaining about a keyword
1658 * then is very confusing.
1660 if (e
->kind
== Lambda_kind
) {
1661 ast_error(CHILD(ch
, 0), "lambda cannot contain assignment");
1663 } else if (e
->kind
!= Name_kind
) {
1664 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1668 e
= ast_for_expr(c
, CHILD(ch
, 2));
1671 kw
= keyword(key
, e
, c
->c_arena
);
1674 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1677 else if (TYPE(ch
) == STAR
) {
1678 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1681 else if (TYPE(ch
) == DOUBLESTAR
) {
1682 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1687 return Call(func
, args
, keywords
, vararg
, kwarg
, LINENO(n
), c
->c_arena
);
1691 ast_for_testlist(struct compiling
*c
, const node
* n
)
1693 /* testlist_gexp: test (',' test)* [','] */
1694 /* testlist: test (',' test)* [','] */
1695 /* testlist_safe: test (',' test)+ [','] */
1696 /* testlist1: test (',' test)* */
1698 if (TYPE(n
) == testlist_gexp
) {
1700 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1703 assert(TYPE(n
) == testlist
||
1704 TYPE(n
) == testlist_safe
||
1705 TYPE(n
) == testlist1
);
1708 return ast_for_expr(c
, CHILD(n
, 0));
1710 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1713 return Tuple(tmp
, Load
, LINENO(n
), c
->c_arena
);
1718 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
1720 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1721 /* argument: test [ gen_for ] */
1722 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
1723 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
1724 return ast_for_genexp(c
, n
);
1725 return ast_for_testlist(c
, n
);
1728 /* like ast_for_testlist() but returns a sequence */
1730 ast_for_class_bases(struct compiling
*c
, const node
* n
)
1732 /* testlist: test (',' test)* [','] */
1737 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
1740 base
= ast_for_expr(c
, CHILD(n
, 0));
1743 asdl_seq_SET(bases
, 0, base
);
1747 return seq_for_testlist(c
, n
);
1751 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
1754 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1755 | ('=' (yield_expr|testlist))*)
1756 testlist: test (',' test)* [',']
1757 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1758 | '<<=' | '>>=' | '**=' | '//='
1759 test: ... here starts the operator precendence dance
1763 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
1767 return Expr(e
, LINENO(n
), c
->c_arena
);
1769 else if (TYPE(CHILD(n
, 1)) == augassign
) {
1770 expr_ty expr1
, expr2
;
1771 operator_ty
operator;
1772 node
*ch
= CHILD(n
, 0);
1774 if (TYPE(ch
) == testlist
)
1775 expr1
= ast_for_testlist(c
, ch
);
1777 expr1
= Yield(ast_for_expr(c
, CHILD(ch
, 0)), LINENO(ch
),
1782 if (expr1
->kind
== GeneratorExp_kind
) {
1783 ast_error(ch
, "augmented assignment to generator "
1784 "expression not possible");
1787 if (expr1
->kind
== Name_kind
) {
1788 char *var_name
= PyString_AS_STRING(expr1
->v
.Name
.id
);
1789 if (var_name
[0] == 'N' && !strcmp(var_name
, "None")) {
1790 ast_error(ch
, "assignment to None");
1796 if (TYPE(ch
) == testlist
)
1797 expr2
= ast_for_testlist(c
, ch
);
1799 expr2
= Yield(ast_for_expr(c
, ch
), LINENO(ch
), c
->c_arena
);
1803 operator = ast_for_augassign(CHILD(n
, 1));
1807 return AugAssign(expr1
, operator, expr2
, LINENO(n
), c
->c_arena
);
1815 /* a normal assignment */
1816 REQ(CHILD(n
, 1), EQUAL
);
1817 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1820 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
1822 node
*ch
= CHILD(n
, i
);
1823 if (TYPE(ch
) == yield_expr
) {
1824 ast_error(ch
, "assignment to yield expression not possible");
1827 e
= ast_for_testlist(c
, ch
);
1829 /* set context to assign */
1833 if (!set_context(e
, Store
, CHILD(n
, i
)))
1836 asdl_seq_SET(targets
, i
/ 2, e
);
1838 value
= CHILD(n
, NCH(n
) - 1);
1839 if (TYPE(value
) == testlist
)
1840 expression
= ast_for_testlist(c
, value
);
1842 expression
= ast_for_expr(c
, value
);
1845 return Assign(targets
, expression
, LINENO(n
), c
->c_arena
);
1850 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
1852 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1853 | '>>' test [ (',' test)+ [','] ] )
1855 expr_ty dest
= NULL
, expression
;
1861 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
1862 dest
= ast_for_expr(c
, CHILD(n
, 2));
1867 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
1870 for (i
= start
; i
< NCH(n
); i
+= 2) {
1871 expression
= ast_for_expr(c
, CHILD(n
, i
));
1875 asdl_seq_APPEND(seq
, expression
);
1877 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
1878 return Print(dest
, seq
, nl
, LINENO(n
), c
->c_arena
);
1882 ast_for_exprlist(struct compiling
*c
, const node
*n
, int context
)
1890 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1893 for (i
= 0; i
< NCH(n
); i
+= 2) {
1894 e
= ast_for_expr(c
, CHILD(n
, i
));
1897 asdl_seq_SET(seq
, i
/ 2, e
);
1898 if (context
&& !set_context(e
, context
, CHILD(n
, i
)))
1905 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
1907 asdl_seq
*expr_list
;
1909 /* del_stmt: 'del' exprlist */
1912 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
1915 return Delete(expr_list
, LINENO(n
), c
->c_arena
);
1919 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
1922 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1925 continue_stmt: 'continue'
1926 return_stmt: 'return' [testlist]
1927 yield_stmt: yield_expr
1928 yield_expr: 'yield' testlist
1929 raise_stmt: 'raise' [test [',' test [',' test]]]
1937 return Break(LINENO(n
), c
->c_arena
);
1939 return Continue(LINENO(n
), c
->c_arena
);
1940 case yield_stmt
: { /* will reduce to yield_expr */
1941 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
1944 return Expr(exp
, LINENO(n
), c
->c_arena
);
1948 return Return(NULL
, LINENO(n
), c
->c_arena
);
1950 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
1953 return Return(expression
, LINENO(n
), c
->c_arena
);
1957 return Raise(NULL
, NULL
, NULL
, LINENO(n
), c
->c_arena
);
1958 else if (NCH(ch
) == 2) {
1959 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
1962 return Raise(expression
, NULL
, NULL
, LINENO(n
), c
->c_arena
);
1964 else if (NCH(ch
) == 4) {
1965 expr_ty expr1
, expr2
;
1967 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
1970 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
1974 return Raise(expr1
, expr2
, NULL
, LINENO(n
), c
->c_arena
);
1976 else if (NCH(ch
) == 6) {
1977 expr_ty expr1
, expr2
, expr3
;
1979 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
1982 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
1985 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
1989 return Raise(expr1
, expr2
, expr3
, LINENO(n
), c
->c_arena
);
1992 PyErr_Format(PyExc_SystemError
,
1993 "unexpected flow_stmt: %d", TYPE(ch
));
1997 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2002 alias_for_import_name(struct compiling
*c
, const node
*n
)
2005 import_as_name: NAME [NAME NAME]
2006 dotted_as_name: dotted_name [NAME NAME]
2007 dotted_name: NAME ('.' NAME)*
2013 case import_as_name
:
2014 str
= (NCH(n
) == 3) ? NEW_IDENTIFIER(CHILD(n
, 2)) : NULL
;
2015 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2016 case dotted_as_name
:
2022 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2024 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2030 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2032 /* Create a string of the form "a.b.c" */
2038 for (i
= 0; i
< NCH(n
); i
+= 2)
2039 /* length of string plus one for the dot */
2040 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2041 len
--; /* the last name doesn't have a dot */
2042 str
= PyString_FromStringAndSize(NULL
, len
);
2045 s
= PyString_AS_STRING(str
);
2048 for (i
= 0; i
< NCH(n
); i
+= 2) {
2049 char *sch
= STR(CHILD(n
, i
));
2050 strcpy(s
, STR(CHILD(n
, i
)));
2056 PyString_InternInPlace(&str
);
2057 PyArena_AddPyObject(c
->c_arena
, str
);
2058 return alias(str
, NULL
, c
->c_arena
);
2062 str
= PyString_InternFromString("*");
2063 PyArena_AddPyObject(c
->c_arena
, str
);
2064 return alias(str
, NULL
, c
->c_arena
);
2066 PyErr_Format(PyExc_SystemError
,
2067 "unexpected import name: %d", TYPE(n
));
2071 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2076 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2079 import_stmt: import_name | import_from
2080 import_name: 'import' dotted_as_names
2081 import_from: 'from' dotted_name 'import' ('*' |
2082 '(' import_as_names ')' |
2088 REQ(n
, import_stmt
);
2090 if (STR(CHILD(n
, 0))[0] == 'i') { /* import */
2092 REQ(n
, dotted_as_names
);
2093 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2096 for (i
= 0; i
< NCH(n
); i
+= 2) {
2097 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2100 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2102 return Import(aliases
, LINENO(n
), c
->c_arena
);
2104 else if (STR(CHILD(n
, 0))[0] == 'f') { /* from */
2106 const char *from_modules
;
2107 int lineno
= LINENO(n
);
2108 alias_ty mod
= alias_for_import_name(c
, CHILD(n
, 1));
2112 /* XXX this needs to be cleaned up */
2114 from_modules
= STR(CHILD(n
, 3));
2115 if (!from_modules
) {
2116 n
= CHILD(n
, 3); /* from ... import x, y, z */
2117 if (NCH(n
) % 2 == 0) {
2118 /* it ends with a comma, not valid but the parser allows it */
2119 ast_error(n
, "trailing comma not allowed without"
2120 " surrounding parentheses");
2124 else if (from_modules
[0] == '*') {
2125 n
= CHILD(n
, 3); /* from ... import * */
2127 else if (from_modules
[0] == '(')
2128 n
= CHILD(n
, 4); /* from ... import (x, y, z) */
2130 /* XXX: don't we need to call ast_error(n, "..."); */
2134 n_children
= NCH(n
);
2135 if (from_modules
&& from_modules
[0] == '*')
2138 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2142 /* handle "from ... import *" special b/c there's no children */
2143 if (from_modules
&& from_modules
[0] == '*') {
2144 alias_ty import_alias
= alias_for_import_name(c
, n
);
2147 asdl_seq_APPEND(aliases
, import_alias
);
2150 for (i
= 0; i
< NCH(n
); i
+= 2) {
2151 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2154 asdl_seq_APPEND(aliases
, import_alias
);
2156 return ImportFrom(mod
->name
, aliases
, lineno
, c
->c_arena
);
2158 PyErr_Format(PyExc_SystemError
,
2159 "unknown import statement: starts with command '%s'",
2165 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2167 /* global_stmt: 'global' NAME (',' NAME)* */
2172 REQ(n
, global_stmt
);
2173 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2176 for (i
= 1; i
< NCH(n
); i
+= 2) {
2177 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2180 asdl_seq_SET(s
, i
/ 2, name
);
2182 return Global(s
, LINENO(n
), c
->c_arena
);
2186 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2188 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2189 int n_children
= NCH(n
);
2190 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2191 PyErr_Format(PyExc_SystemError
,
2192 "poorly formed 'exec' statement: %d parts to statement",
2197 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2199 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2202 if (n_children
>= 4) {
2203 globals
= ast_for_expr(c
, CHILD(n
, 3));
2207 if (n_children
== 6) {
2208 locals
= ast_for_expr(c
, CHILD(n
, 5));
2213 return Exec(expr1
, globals
, locals
, LINENO(n
), c
->c_arena
);
2217 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2219 /* assert_stmt: 'assert' test [',' test] */
2220 REQ(n
, assert_stmt
);
2222 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2225 return Assert(expression
, NULL
, LINENO(n
), c
->c_arena
);
2227 else if (NCH(n
) == 4) {
2228 expr_ty expr1
, expr2
;
2230 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2233 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2237 return Assert(expr1
, expr2
, LINENO(n
), c
->c_arena
);
2239 PyErr_Format(PyExc_SystemError
,
2240 "improper number of parts to 'assert' statement: %d",
2246 ast_for_suite(struct compiling
*c
, const node
*n
)
2248 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2251 int i
, total
, num
, end
, pos
= 0;
2256 total
= num_stmts(n
);
2257 seq
= asdl_seq_new(total
, c
->c_arena
);
2260 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2262 /* simple_stmt always ends with a NEWLINE,
2263 and may have a trailing SEMI
2266 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2268 /* loop by 2 to skip semi-colons */
2269 for (i
= 0; i
< end
; i
+= 2) {
2271 s
= ast_for_stmt(c
, ch
);
2274 asdl_seq_SET(seq
, pos
++, s
);
2278 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2281 num
= num_stmts(ch
);
2283 /* small_stmt or compound_stmt with only one child */
2284 s
= ast_for_stmt(c
, ch
);
2287 asdl_seq_SET(seq
, pos
++, s
);
2292 REQ(ch
, simple_stmt
);
2293 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2294 /* statement terminates with a semi-colon ';' */
2295 if (NCH(CHILD(ch
, j
)) == 0) {
2296 assert((j
+ 1) == NCH(ch
));
2299 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2302 asdl_seq_SET(seq
, pos
++, s
);
2307 assert(pos
== seq
->size
);
2312 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2314 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2323 asdl_seq
*suite_seq
;
2325 expression
= ast_for_expr(c
, CHILD(n
, 1));
2328 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2332 return If(expression
, suite_seq
, NULL
, LINENO(n
), c
->c_arena
);
2335 s
= STR(CHILD(n
, 4));
2336 /* s[2], the third character in the string, will be
2342 asdl_seq
*seq1
, *seq2
;
2344 expression
= ast_for_expr(c
, CHILD(n
, 1));
2347 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2350 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2354 return If(expression
, seq1
, seq2
, LINENO(n
), c
->c_arena
);
2356 else if (s
[2] == 'i') {
2357 int i
, n_elif
, has_else
= 0;
2358 asdl_seq
*orelse
= NULL
;
2359 n_elif
= NCH(n
) - 4;
2360 /* must reference the child n_elif+1 since 'else' token is third,
2361 not fourth, child from the end. */
2362 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2363 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2371 asdl_seq
*seq1
, *seq2
;
2373 orelse
= asdl_seq_new(1, c
->c_arena
);
2376 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2379 seq1
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2382 seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2386 asdl_seq_SET(orelse
, 0, If(expression
, seq1
, seq2
,
2387 LINENO(CHILD(n
, NCH(n
) - 6)),
2389 /* the just-created orelse handled the last elif */
2393 for (i
= 0; i
< n_elif
; i
++) {
2394 int off
= 5 + (n_elif
- i
- 1) * 4;
2396 asdl_seq
*suite_seq
;
2397 asdl_seq
*new = asdl_seq_new(1, c
->c_arena
);
2400 expression
= ast_for_expr(c
, CHILD(n
, off
));
2403 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2407 asdl_seq_SET(new, 0,
2408 If(expression
, suite_seq
, orelse
,
2409 LINENO(CHILD(n
, off
)), c
->c_arena
));
2412 return If(ast_for_expr(c
, CHILD(n
, 1)),
2413 ast_for_suite(c
, CHILD(n
, 3)),
2414 orelse
, LINENO(n
), c
->c_arena
);
2417 PyErr_Format(PyExc_SystemError
,
2418 "unexpected token in 'if' statement: %s", s
);
2423 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2425 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2430 asdl_seq
*suite_seq
;
2432 expression
= ast_for_expr(c
, CHILD(n
, 1));
2435 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2438 return While(expression
, suite_seq
, NULL
, LINENO(n
), c
->c_arena
);
2440 else if (NCH(n
) == 7) {
2442 asdl_seq
*seq1
, *seq2
;
2444 expression
= ast_for_expr(c
, CHILD(n
, 1));
2447 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2450 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2454 return While(expression
, seq1
, seq2
, LINENO(n
), c
->c_arena
);
2457 PyErr_Format(PyExc_SystemError
,
2458 "wrong number of tokens for 'while' statement: %d",
2464 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2466 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2469 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2473 seq
= ast_for_suite(c
, CHILD(n
, 8));
2478 _target
= ast_for_exprlist(c
, CHILD(n
, 1), Store
);
2481 if (asdl_seq_LEN(_target
) == 1)
2482 target
= asdl_seq_GET(_target
, 0);
2484 target
= Tuple(_target
, Store
, LINENO(n
), c
->c_arena
);
2486 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2489 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2493 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), c
->c_arena
);
2496 static excepthandler_ty
2497 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2499 /* except_clause: 'except' [test [',' test]] */
2500 REQ(exc
, except_clause
);
2503 if (NCH(exc
) == 1) {
2504 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2508 return excepthandler(NULL
, NULL
, suite_seq
, c
->c_arena
);
2510 else if (NCH(exc
) == 2) {
2512 asdl_seq
*suite_seq
;
2514 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2517 suite_seq
= ast_for_suite(c
, body
);
2521 return excepthandler(expression
, NULL
, suite_seq
, c
->c_arena
);
2523 else if (NCH(exc
) == 4) {
2524 asdl_seq
*suite_seq
;
2526 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2529 if (!set_context(e
, Store
, CHILD(exc
, 3)))
2531 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2534 suite_seq
= ast_for_suite(c
, body
);
2538 return excepthandler(expression
, e
, suite_seq
, c
->c_arena
);
2541 PyErr_Format(PyExc_SystemError
,
2542 "wrong number of children for 'except' clause: %d",
2548 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2550 const int nch
= NCH(n
);
2551 int n_except
= (nch
- 3)/3;
2552 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2556 body
= ast_for_suite(c
, CHILD(n
, 2));
2560 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2561 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2562 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2563 /* we can assume it's an "else",
2564 because nch >= 9 for try-else-finally and
2565 it would otherwise have a type of except_clause */
2566 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2572 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2573 if (finally
== NULL
)
2578 /* we can assume it's an "else",
2579 otherwise it would have a type of except_clause */
2580 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2586 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2587 ast_error(n
, "malformed 'try' statement");
2594 /* process except statements to create a try ... except */
2595 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2596 if (handlers
== NULL
)
2599 for (i
= 0; i
< n_except
; i
++) {
2600 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2601 CHILD(n
, 5 + i
* 3));
2604 asdl_seq_SET(handlers
, i
, e
);
2607 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
), c
->c_arena
);
2611 /* if a 'finally' is present too, we nest the TryExcept within a
2612 TryFinally to emulate try ... except ... finally */
2613 body
= asdl_seq_new(1, c
->c_arena
);
2616 asdl_seq_SET(body
, 0, except_st
);
2619 /* must be a try ... finally (except clauses are in body, if any exist) */
2620 assert(finally
!= NULL
);
2621 return TryFinally(body
, finally
, LINENO(n
), c
->c_arena
);
2625 ast_for_classdef(struct compiling
*c
, const node
*n
)
2627 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2628 asdl_seq
*bases
, *s
;
2632 if (!strcmp(STR(CHILD(n
, 1)), "None")) {
2633 ast_error(n
, "assignment to None");
2638 s
= ast_for_suite(c
, CHILD(n
, 3));
2641 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2644 /* check for empty base list */
2645 if (TYPE(CHILD(n
,3)) == RPAR
) {
2646 s
= ast_for_suite(c
, CHILD(n
,5));
2649 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2653 /* else handle the base class list */
2654 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
2658 s
= ast_for_suite(c
, CHILD(n
, 6));
2661 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, LINENO(n
),
2666 ast_for_stmt(struct compiling
*c
, const node
*n
)
2668 if (TYPE(n
) == stmt
) {
2669 assert(NCH(n
) == 1);
2672 if (TYPE(n
) == simple_stmt
) {
2673 assert(num_stmts(n
) == 1);
2676 if (TYPE(n
) == small_stmt
) {
2679 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2680 | flow_stmt | import_stmt | global_stmt | exec_stmt
2685 return ast_for_expr_stmt(c
, n
);
2687 return ast_for_print_stmt(c
, n
);
2689 return ast_for_del_stmt(c
, n
);
2691 return Pass(LINENO(n
), c
->c_arena
);
2693 return ast_for_flow_stmt(c
, n
);
2695 return ast_for_import_stmt(c
, n
);
2697 return ast_for_global_stmt(c
, n
);
2699 return ast_for_exec_stmt(c
, n
);
2701 return ast_for_assert_stmt(c
, n
);
2703 PyErr_Format(PyExc_SystemError
,
2704 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2710 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2711 | funcdef | classdef
2713 node
*ch
= CHILD(n
, 0);
2714 REQ(n
, compound_stmt
);
2717 return ast_for_if_stmt(c
, ch
);
2719 return ast_for_while_stmt(c
, ch
);
2721 return ast_for_for_stmt(c
, ch
);
2723 return ast_for_try_stmt(c
, ch
);
2725 return ast_for_funcdef(c
, ch
);
2727 return ast_for_classdef(c
, ch
);
2729 PyErr_Format(PyExc_SystemError
,
2730 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2738 parsenumber(const char *s
)
2743 #ifndef WITHOUT_COMPLEX
2749 end
= s
+ strlen(s
) - 1;
2750 #ifndef WITHOUT_COMPLEX
2751 imflag
= *end
== 'j' || *end
== 'J';
2753 if (*end
== 'l' || *end
== 'L')
2754 return PyLong_FromString((char *)s
, (char **)0, 0);
2756 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
2757 if (x
< 0 && errno
== 0) {
2758 return PyLong_FromString((char *)s
,
2764 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
2767 return PyLong_FromString((char *)s
, (char **)0, 0);
2768 return PyInt_FromLong(x
);
2770 /* XXX Huge floats may silently fail */
2771 #ifndef WITHOUT_COMPLEX
2774 PyFPE_START_PROTECT("atof", return 0)
2775 c
.imag
= PyOS_ascii_atof(s
);
2776 PyFPE_END_PROTECT(c
)
2777 return PyComplex_FromCComplex(c
);
2782 PyFPE_START_PROTECT("atof", return 0)
2783 dx
= PyOS_ascii_atof(s
);
2784 PyFPE_END_PROTECT(dx
)
2785 return PyFloat_FromDouble(dx
);
2790 decode_utf8(const char **sPtr
, const char *end
, char* encoding
)
2792 #ifndef Py_USING_UNICODE
2793 Py_FatalError("decode_utf8 should not be called in this build.");
2798 t
= s
= (char *)*sPtr
;
2799 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2800 while (s
< end
&& (*s
& 0x80)) s
++;
2802 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
2805 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
2812 decode_unicode(const char *s
, size_t len
, int rawmode
, const char *encoding
)
2818 if (encoding
== NULL
) {
2821 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
2825 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2826 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
2829 p
= buf
= PyString_AsString(u
);
2839 if (*s
& 0x80) { /* XXX inefficient */
2843 w
= decode_utf8(&s
, end
, "utf-16-be");
2848 r
= PyString_AsString(w
);
2849 rn
= PyString_Size(w
);
2850 assert(rn
% 2 == 0);
2851 for (i
= 0; i
< rn
; i
+= 2) {
2852 sprintf(p
, "\\u%02x%02x",
2866 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
2868 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
2873 /* s is a Python string literal, including the bracketing quote characters,
2874 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
2875 * parsestr parses it, and returns the decoded Python string object.
2878 parsestr(const char *s
, const char *encoding
)
2881 int quote
= Py_CHARMASK(*s
);
2886 if (isalpha(quote
) || quote
== '_') {
2887 if (quote
== 'u' || quote
== 'U') {
2891 if (quote
== 'r' || quote
== 'R') {
2896 if (quote
!= '\'' && quote
!= '\"') {
2897 PyErr_BadInternalCall();
2902 if (len
> INT_MAX
) {
2903 PyErr_SetString(PyExc_OverflowError
,
2904 "string to parse is too long");
2907 if (s
[--len
] != quote
) {
2908 PyErr_BadInternalCall();
2911 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
2914 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
2915 PyErr_BadInternalCall();
2919 #ifdef Py_USING_UNICODE
2920 if (unicode
|| Py_UnicodeFlag
) {
2921 return decode_unicode(s
, len
, rawmode
, encoding
);
2924 need_encoding
= (encoding
!= NULL
&&
2925 strcmp(encoding
, "utf-8") != 0 &&
2926 strcmp(encoding
, "iso-8859-1") != 0);
2927 if (rawmode
|| strchr(s
, '\\') == NULL
) {
2928 if (need_encoding
) {
2929 #ifndef Py_USING_UNICODE
2930 /* This should not happen - we never see any other
2932 Py_FatalError("cannot deal with encodings in this build.");
2934 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
2937 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
2942 return PyString_FromStringAndSize(s
, len
);
2946 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
2947 need_encoding
? encoding
: NULL
);
2950 /* Build a Python string object out of a STRING atom. This takes care of
2951 * compile-time literal catenation, calling parsestr() on each piece, and
2952 * pasting the intermediate results together.
2955 parsestrplus(struct compiling
*c
, const node
*n
)
2959 REQ(CHILD(n
, 0), STRING
);
2960 if ((v
= parsestr(STR(CHILD(n
, 0)), c
->c_encoding
)) != NULL
) {
2961 /* String literal concatenation */
2962 for (i
= 1; i
< NCH(n
); i
++) {
2964 s
= parsestr(STR(CHILD(n
, i
)), c
->c_encoding
);
2967 if (PyString_Check(v
) && PyString_Check(s
)) {
2968 PyString_ConcatAndDel(&v
, s
);
2972 #ifdef Py_USING_UNICODE
2974 PyObject
*temp
= PyUnicode_Concat(v
, s
);