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 PyArena
*c_arena
; /* arena for allocating memeory */
22 const char *c_filename
; /* filename */
25 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
26 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
27 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
28 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
29 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*,
31 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
32 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
34 /* Note different signature for ast_for_call */
35 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
37 static PyObject
*parsenumber(const char *);
38 static PyObject
*parsestr(const char *s
, const char *encoding
);
39 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
42 #define LINENO(n) ((n)->n_lineno)
46 new_identifier(const char* n
, PyArena
*arena
) {
47 PyObject
* id
= PyString_InternFromString(n
);
48 PyArena_AddPyObject(arena
, id
);
52 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
54 /* This routine provides an invalid object for the syntax error.
55 The outermost routine must unpack this error and create the
56 proper object. We do this so that we don't have to pass
57 the filename to everything function.
59 XXX Maybe we should just pass the filename...
63 ast_error(const node
*n
, const char *errstr
)
65 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
68 PyErr_SetObject(PyExc_SyntaxError
, u
);
74 ast_error_finish(const char *filename
)
76 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
79 assert(PyErr_Occurred());
80 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
83 PyErr_Fetch(&type
, &value
, &tback
);
84 errstr
= PyTuple_GetItem(value
, 0);
88 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
95 loc
= PyErr_ProgramText(filename
, lineno
);
100 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
106 value
= PyTuple_Pack(2, errstr
, tmp
);
111 PyErr_Restore(type
, value
, tback
);
114 /* num_stmts() returns number of contained statements.
116 Use this routine to determine how big a sequence is needed for
117 the statements in a parse tree. Its raison d'etre is this bit of
120 stmt: simple_stmt | compound_stmt
121 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
123 A simple_stmt can contain multiple small_stmt elements joined
124 by semicolons. If the arg is a simple_stmt, the number of
125 small_stmt elements is returned.
129 num_stmts(const node
*n
)
136 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
139 return num_stmts(CHILD(n
, 0));
142 for (i
= 0; i
< NCH(n
); i
++) {
144 if (TYPE(ch
) == stmt
)
149 return num_stmts(CHILD(n
, 0));
153 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
156 return num_stmts(CHILD(n
, 0));
159 for (i
= 2; i
< (NCH(n
) - 1); i
++)
160 l
+= num_stmts(CHILD(n
, i
));
166 sprintf(buf
, "Non-statement found: %d %d\n",
175 /* Transform the CST rooted at node * to the appropriate AST
179 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
183 asdl_seq
*stmts
= NULL
;
188 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
189 c
.c_encoding
= "utf-8";
190 if (TYPE(n
) == encoding_decl
) {
191 ast_error(n
, "encoding declaration in Unicode string");
194 } else if (TYPE(n
) == encoding_decl
) {
195 c
.c_encoding
= STR(n
);
201 c
.c_filename
= filename
;
206 stmts
= asdl_seq_new(num_stmts(n
), arena
);
209 for (i
= 0; i
< NCH(n
) - 1; i
++) {
211 if (TYPE(ch
) == NEWLINE
)
216 s
= ast_for_stmt(&c
, ch
);
219 asdl_seq_SET(stmts
, k
++, s
);
223 REQ(ch
, simple_stmt
);
224 for (j
= 0; j
< num
; j
++) {
225 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
228 asdl_seq_SET(stmts
, k
++, s
);
232 return Module(stmts
, arena
);
234 expr_ty testlist_ast
;
236 /* XXX Why not gen_for here? */
237 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
240 return Expression(testlist_ast
, arena
);
243 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
244 stmts
= asdl_seq_new(1, arena
);
247 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
249 if (!asdl_seq_GET(stmts
, 0))
251 return Interactive(stmts
, arena
);
256 stmts
= asdl_seq_new(num
, arena
);
260 s
= ast_for_stmt(&c
, n
);
263 asdl_seq_SET(stmts
, 0, s
);
266 /* Only a simple_stmt can contain multiple statements. */
268 for (i
= 0; i
< NCH(n
); i
+= 2) {
269 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
271 s
= ast_for_stmt(&c
, CHILD(n
, i
));
274 asdl_seq_SET(stmts
, i
/ 2, s
);
278 return Interactive(stmts
, arena
);
281 PyErr_Format(PyExc_SystemError
,
282 "invalid node %d for PyAST_FromNode", TYPE(n
));
286 ast_error_finish(filename
);
290 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
294 get_operator(const node
*n
)
320 return (operator_ty
)0;
324 /* Set the context ctx for expr_ty e, recursively traversing e.
326 Only sets context for expr kinds that "can appear in assignment context"
327 (according to ../Parser/Python.asdl). For other expr kinds, it sets
328 an appropriate syntax error and returns false.
332 set_context(expr_ty e
, expr_context_ty ctx
, const node
*n
)
335 /* If a particular expression type can't be used for assign / delete,
336 set expr_name to its name and an error message will be generated.
338 const char* expr_name
= NULL
;
340 /* The ast defines augmented store and load contexts, but the
341 implementation here doesn't actually use them. The code may be
342 a little more complex than necessary as a result. It also means
343 that expressions in an augmented assignment have a Store context.
344 Consider restructuring so that augmented assignment uses
347 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
352 !strcmp(PyString_AS_STRING(e
->v
.Attribute
.attr
), "None")) {
353 return ast_error(n
, "assignment to None");
355 e
->v
.Attribute
.ctx
= ctx
;
358 e
->v
.Subscript
.ctx
= ctx
;
362 !strcmp(PyString_AS_STRING(e
->v
.Name
.id
), "None")) {
363 return ast_error(n
, "assignment to None");
372 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
373 return ast_error(n
, "can't assign to ()");
374 e
->v
.Tuple
.ctx
= ctx
;
378 expr_name
= "lambda";
381 expr_name
= "function call";
386 expr_name
= "operator";
388 case GeneratorExp_kind
:
389 expr_name
= "generator expression";
392 expr_name
= "yield expression";
395 expr_name
= "list comprehension";
400 expr_name
= "literal";
403 expr_name
= "comparison";
409 expr_name
= "conditional expression";
412 PyErr_Format(PyExc_SystemError
,
413 "unexpected expression in assignment %d (line %d)",
417 /* Check for error string set by switch */
420 PyOS_snprintf(buf
, sizeof(buf
),
422 ctx
== Store
? "assign to" : "delete",
424 return ast_error(n
, buf
);
427 /* If the LHS is a list or tuple, we need to set the assignment
428 context for all the contained elements.
433 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
434 if (!set_context((expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
442 ast_for_augassign(const node
*n
)
452 if (STR(n
)[1] == '/')
469 if (STR(n
)[1] == '*')
474 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
475 return (operator_ty
)0;
480 ast_for_comp_op(const node
*n
)
482 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
493 case EQEQUAL
: /* == */
502 if (strcmp(STR(n
), "in") == 0)
504 if (strcmp(STR(n
), "is") == 0)
507 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
512 else if (NCH(n
) == 2) {
513 /* handle "not in" and "is not" */
514 switch (TYPE(CHILD(n
, 0))) {
516 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
518 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
521 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
522 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
526 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
532 seq_for_testlist(struct compiling
*c
, const node
*n
)
534 /* testlist: test (',' test)* [','] */
538 assert(TYPE(n
) == testlist
||
539 TYPE(n
) == listmaker
||
540 TYPE(n
) == testlist_gexp
||
541 TYPE(n
) == testlist_safe
||
542 TYPE(n
) == testlist1
);
544 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
548 for (i
= 0; i
< NCH(n
); i
+= 2) {
549 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
551 expression
= ast_for_expr(c
, CHILD(n
, i
));
555 assert(i
/ 2 < seq
->size
);
556 asdl_seq_SET(seq
, i
/ 2, expression
);
562 compiler_complex_args(struct compiling
*c
, const node
*n
)
564 int i
, len
= (NCH(n
) + 1) / 2;
566 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
570 /* fpdef: NAME | '(' fplist ')'
571 fplist: fpdef (',' fpdef)* [',']
574 for (i
= 0; i
< len
; i
++) {
575 const node
*fpdef_node
= CHILD(n
, 2*i
);
579 /* fpdef_node is either a NAME or an fplist */
580 child
= CHILD(fpdef_node
, 0);
581 if (TYPE(child
) == NAME
) {
582 if (!strcmp(STR(child
), "None")) {
583 ast_error(child
, "assignment to None");
586 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
),
587 child
->n_col_offset
, c
->c_arena
);
590 assert(TYPE(fpdef_node
) == fpdef
);
591 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
592 child
= CHILD(fpdef_node
, 1);
593 assert(TYPE(child
) == fplist
);
594 /* NCH == 1 means we have (x), we need to elide the extra parens */
595 if (NCH(child
) == 1) {
596 fpdef_node
= CHILD(child
, 0);
597 assert(TYPE(fpdef_node
) == fpdef
);
600 arg
= compiler_complex_args(c
, child
);
602 asdl_seq_SET(args
, i
, arg
);
605 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
606 if (!set_context(result
, Store
, n
))
612 /* Create AST for argument list. */
615 ast_for_arguments(struct compiling
*c
, const node
*n
)
617 /* parameters: '(' [varargslist] ')'
618 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
619 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
621 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
622 asdl_seq
*args
, *defaults
;
623 identifier vararg
= NULL
, kwarg
= NULL
;
626 if (TYPE(n
) == parameters
) {
627 if (NCH(n
) == 2) /* () as argument list */
628 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
633 /* first count the number of normal args & defaults */
634 for (i
= 0; i
< NCH(n
); i
++) {
636 if (TYPE(ch
) == fpdef
)
638 if (TYPE(ch
) == EQUAL
)
641 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
643 return NULL
; /* Don't need to goto error; no objects allocated */
644 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
645 if (!defaults
&& n_defaults
)
646 return NULL
; /* Don't need to goto error; no objects allocated */
648 /* fpdef: NAME | '(' fplist ')'
649 fplist: fpdef (',' fpdef)* [',']
652 j
= 0; /* index for defaults */
653 k
= 0; /* index for args */
659 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
660 anything other than EQUAL or a comma? */
661 /* XXX Should NCH(n) check be made a separate check? */
662 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
663 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
666 assert(defaults
!= NULL
);
667 asdl_seq_SET(defaults
, j
++, expression
);
671 else if (found_default
) {
673 "non-default argument follows default argument");
678 /* def foo((x)): is not complex, special case. */
680 /* We have complex arguments, setup for unpacking. */
681 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
682 if (!asdl_seq_GET(args
, k
-1))
685 /* def foo((x)): setup for checking NAME below. */
686 /* Loop because there can be many parens and tuple
687 unpacking mixed in. */
689 assert(TYPE(ch
) == fpdef
);
693 if (TYPE(CHILD(ch
, 0)) == NAME
) {
695 if (!strcmp(STR(CHILD(ch
, 0)), "None")) {
696 ast_error(CHILD(ch
, 0), "assignment to None");
699 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
700 Param
, LINENO(ch
), ch
->n_col_offset
,
704 asdl_seq_SET(args
, k
++, name
);
707 i
+= 2; /* the name and the comma */
710 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
711 ast_error(CHILD(n
, i
+1), "assignment to None");
714 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
718 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
719 ast_error(CHILD(n
, i
+1), "assignment to None");
722 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
726 PyErr_Format(PyExc_SystemError
,
727 "unexpected node in varargslist: %d @ %d",
733 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
742 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
746 int lineno
, col_offset
;
752 col_offset
= n
->n_col_offset
;
754 id
= NEW_IDENTIFIER(CHILD(n
, 0));
757 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
761 for (i
= 2; i
< NCH(n
); i
+=2) {
762 id
= NEW_IDENTIFIER(CHILD(n
, i
));
765 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
774 ast_for_decorator(struct compiling
*c
, const node
*n
)
776 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
781 REQ(CHILD(n
, 0), AT
);
782 REQ(RCHILD(n
, -1), NEWLINE
);
784 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
788 if (NCH(n
) == 3) { /* No arguments */
792 else if (NCH(n
) == 5) { /* Call with no arguments */
793 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
794 n
->n_col_offset
, c
->c_arena
);
800 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
810 ast_for_decorators(struct compiling
*c
, const node
*n
)
812 asdl_seq
* decorator_seq
;
817 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
821 for (i
= 0; i
< NCH(n
); i
++) {
822 d
= ast_for_decorator(c
, CHILD(n
, i
));
825 asdl_seq_SET(decorator_seq
, i
, d
);
827 return decorator_seq
;
831 ast_for_funcdef(struct compiling
*c
, const node
*n
)
833 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
837 asdl_seq
*decorator_seq
= NULL
;
842 if (NCH(n
) == 6) { /* decorators are present */
843 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
852 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
855 else if (!strcmp(STR(CHILD(n
, name_i
)), "None")) {
856 ast_error(CHILD(n
, name_i
), "assignment to None");
859 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
862 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
866 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
867 n
->n_col_offset
, c
->c_arena
);
871 ast_for_lambdef(struct compiling
*c
, const node
*n
)
873 /* lambdef: 'lambda' [varargslist] ':' test */
878 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
881 expression
= ast_for_expr(c
, CHILD(n
, 2));
886 args
= ast_for_arguments(c
, CHILD(n
, 1));
889 expression
= ast_for_expr(c
, CHILD(n
, 3));
894 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
898 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
900 /* test: or_test 'if' or_test 'else' test */
901 expr_ty expression
, body
, orelse
;
904 body
= ast_for_expr(c
, CHILD(n
, 0));
907 expression
= ast_for_expr(c
, CHILD(n
, 2));
910 orelse
= ast_for_expr(c
, CHILD(n
, 4));
913 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
917 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
918 so there is only a single version. Possibly for loops can also re-use
922 /* Count the number of 'for' loop in a list comprehension.
924 Helper for ast_for_listcomp().
928 count_list_fors(const node
*n
)
931 node
*ch
= CHILD(n
, 1);
943 if (TYPE(ch
) == list_for
)
945 else if (TYPE(ch
) == list_if
) {
948 goto count_list_iter
;
954 /* Should never be reached */
955 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
959 /* Count the number of 'if' statements in a list comprehension.
961 Helper for ast_for_listcomp().
965 count_list_ifs(const node
*n
)
971 if (TYPE(CHILD(n
, 0)) == list_for
)
979 goto count_list_iter
;
983 ast_for_listcomp(struct compiling
*c
, const node
*n
)
985 /* listmaker: test ( list_for | (',' test)* [','] )
986 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
987 list_iter: list_for | list_if
988 list_if: 'if' test [list_iter]
989 testlist_safe: test [(',' test)+ [',']]
999 elt
= ast_for_expr(c
, CHILD(n
, 0));
1003 n_fors
= count_list_fors(n
);
1007 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1012 for (i
= 0; i
< n_fors
; i
++) {
1013 comprehension_ty lc
;
1020 for_ch
= CHILD(ch
, 1);
1021 t
= ast_for_exprlist(c
, for_ch
, Store
);
1024 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1028 /* Check the # of children rather than the length of t, since
1029 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1031 if (NCH(for_ch
) == 1)
1032 lc
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
, NULL
,
1035 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1037 expression
, NULL
, c
->c_arena
);
1044 expr_ty list_for_expr
;
1047 n_ifs
= count_list_ifs(ch
);
1051 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1055 for (j
= 0; j
< n_ifs
; j
++) {
1060 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1064 asdl_seq_SET(ifs
, j
, list_for_expr
);
1068 /* on exit, must guarantee that ch is a list_for */
1069 if (TYPE(ch
) == list_iter
)
1073 asdl_seq_SET(listcomps
, i
, lc
);
1076 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1079 /* Count the number of 'for' loops in a generator expression.
1081 Helper for ast_for_genexp().
1085 count_gen_fors(const node
*n
)
1088 node
*ch
= CHILD(n
, 1);
1100 if (TYPE(ch
) == gen_for
)
1102 else if (TYPE(ch
) == gen_if
) {
1105 goto count_gen_iter
;
1111 /* Should never be reached */
1112 PyErr_SetString(PyExc_SystemError
,
1113 "logic error in count_gen_fors");
1117 /* Count the number of 'if' statements in a generator expression.
1119 Helper for ast_for_genexp().
1123 count_gen_ifs(const node
*n
)
1129 if (TYPE(CHILD(n
, 0)) == gen_for
)
1140 /* TODO(jhylton): Combine with list comprehension code? */
1142 ast_for_genexp(struct compiling
*c
, const node
*n
)
1144 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1145 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1151 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1154 elt
= ast_for_expr(c
, CHILD(n
, 0));
1158 n_fors
= count_gen_fors(n
);
1162 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1167 for (i
= 0; i
< n_fors
; i
++) {
1168 comprehension_ty ge
;
1175 for_ch
= CHILD(ch
, 1);
1176 t
= ast_for_exprlist(c
, for_ch
, Store
);
1179 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1183 /* Check the # of children rather than the length of t, since
1184 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1185 if (NCH(for_ch
) == 1)
1186 ge
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
,
1189 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1191 expression
, NULL
, c
->c_arena
);
1201 n_ifs
= count_gen_ifs(ch
);
1205 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1209 for (j
= 0; j
< n_ifs
; j
++) {
1214 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1217 asdl_seq_SET(ifs
, j
, expression
);
1221 /* on exit, must guarantee that ch is a gen_for */
1222 if (TYPE(ch
) == gen_iter
)
1226 asdl_seq_SET(genexps
, i
, ge
);
1229 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1233 ast_for_atom(struct compiling
*c
, const node
*n
)
1235 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1236 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1238 node
*ch
= CHILD(n
, 0);
1242 /* All names start in Load context, but may later be
1244 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), n
->n_col_offset
,
1247 PyObject
*str
= parsestrplus(c
, n
);
1249 #ifdef Py_USING_UNICODE
1250 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1251 PyObject
*type
, *value
, *tback
, *errstr
;
1252 PyErr_Fetch(&type
, &value
, &tback
);
1253 errstr
= ((PyUnicodeErrorObject
*)value
)->reason
;
1257 s
= PyString_AsString(errstr
);
1258 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1261 ast_error(n
, "(unicode error) unknown error");
1270 PyArena_AddPyObject(c
->c_arena
, str
);
1271 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1274 PyObject
*pynum
= parsenumber(STR(ch
));
1278 PyArena_AddPyObject(c
->c_arena
, pynum
);
1279 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1281 case LPAR
: /* some parenthesized expressions */
1284 if (TYPE(ch
) == RPAR
)
1285 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1287 if (TYPE(ch
) == yield_expr
)
1288 return ast_for_expr(c
, ch
);
1290 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1291 return ast_for_genexp(c
, ch
);
1293 return ast_for_testlist_gexp(c
, ch
);
1294 case LSQB
: /* list (or list comprehension) */
1297 if (TYPE(ch
) == RSQB
)
1298 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1301 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1302 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1306 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1309 return ast_for_listcomp(c
, ch
);
1311 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1313 asdl_seq
*keys
, *values
;
1316 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1317 keys
= asdl_seq_new(size
, c
->c_arena
);
1321 values
= asdl_seq_new(size
, c
->c_arena
);
1325 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1328 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1332 asdl_seq_SET(keys
, i
/ 4, expression
);
1334 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1338 asdl_seq_SET(values
, i
/ 4, expression
);
1340 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1342 case BACKQUOTE
: { /* repr */
1344 if (Py_Py3kWarningFlag
) {
1345 if (PyErr_WarnExplicit(PyExc_DeprecationWarning
,
1346 "backquote not supported in 3.x",
1347 c
->c_filename
, LINENO(n
),
1352 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1356 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1359 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1365 ast_for_slice(struct compiling
*c
, const node
*n
)
1368 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1373 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1377 if (TYPE(ch
) == DOT
)
1378 return Ellipsis(c
->c_arena
);
1380 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1381 /* 'step' variable hold no significance in terms of being used over
1383 step
= ast_for_expr(c
, ch
);
1387 return Index(step
, c
->c_arena
);
1390 if (TYPE(ch
) == test
) {
1391 lower
= ast_for_expr(c
, ch
);
1396 /* If there's an upper bound it's in the second or third position. */
1397 if (TYPE(ch
) == COLON
) {
1399 node
*n2
= CHILD(n
, 1);
1401 if (TYPE(n2
) == test
) {
1402 upper
= ast_for_expr(c
, n2
);
1407 } else if (NCH(n
) > 2) {
1408 node
*n2
= CHILD(n
, 2);
1410 if (TYPE(n2
) == test
) {
1411 upper
= ast_for_expr(c
, n2
);
1417 ch
= CHILD(n
, NCH(n
) - 1);
1418 if (TYPE(ch
) == sliceop
) {
1420 /* No expression, so step is None */
1422 step
= Name(new_identifier("None", c
->c_arena
), Load
,
1423 LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1428 if (TYPE(ch
) == test
) {
1429 step
= ast_for_expr(c
, ch
);
1436 return Slice(lower
, upper
, step
, c
->c_arena
);
1440 ast_for_binop(struct compiling
*c
, const node
*n
)
1442 /* Must account for a sequence of expressions.
1443 How should A op B op C by represented?
1444 BinOp(BinOp(A, op, B), op, C).
1448 expr_ty expr1
, expr2
, result
;
1449 operator_ty newoperator
;
1451 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1455 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1459 newoperator
= get_operator(CHILD(n
, 1));
1463 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1468 nops
= (NCH(n
) - 1) / 2;
1469 for (i
= 1; i
< nops
; i
++) {
1470 expr_ty tmp_result
, tmp
;
1471 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1473 newoperator
= get_operator(next_oper
);
1477 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1481 tmp_result
= BinOp(result
, newoperator
, tmp
,
1482 LINENO(next_oper
), next_oper
->n_col_offset
,
1486 result
= tmp_result
;
1492 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1494 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1495 subscriptlist: subscript (',' subscript)* [',']
1496 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1499 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1501 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1502 n
->n_col_offset
, c
->c_arena
);
1504 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1506 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1507 return Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
,
1508 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1511 REQ(CHILD(n
, 0), LSQB
);
1512 REQ(CHILD(n
, 2), RSQB
);
1515 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1518 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1522 /* The grammar is ambiguous here. The ambiguity is resolved
1523 by treating the sequence as a tuple literal if there are
1530 asdl_seq
*slices
, *elts
;
1531 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1534 for (j
= 0; j
< NCH(n
); j
+= 2) {
1535 slc
= ast_for_slice(c
, CHILD(n
, j
));
1538 if (slc
->kind
!= Index_kind
)
1540 asdl_seq_SET(slices
, j
/ 2, slc
);
1543 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1544 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1546 /* extract Index values and put them in a Tuple */
1547 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1550 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1551 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1552 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1553 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1555 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1558 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1559 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1565 ast_for_factor(struct compiling
*c
, const node
*n
)
1567 node
*pfactor
, *ppower
, *patom
, *pnum
;
1570 /* If the unary - operator is applied to a constant, don't generate
1571 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1572 constant. The peephole optimizer already does something like
1573 this but it doesn't handle the case where the constant is
1574 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1577 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1579 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1580 NCH(pfactor
) == 1 &&
1581 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1583 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1584 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1585 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1589 strcpy(s
+ 1, STR(pnum
));
1590 PyObject_FREE(STR(pnum
));
1592 return ast_for_atom(c
, patom
);
1595 expression
= ast_for_expr(c
, CHILD(n
, 1));
1599 switch (TYPE(CHILD(n
, 0))) {
1601 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1604 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1607 return UnaryOp(Invert
, expression
, LINENO(n
),
1608 n
->n_col_offset
, c
->c_arena
);
1610 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1616 ast_for_power(struct compiling
*c
, const node
*n
)
1618 /* power: atom trailer* ('**' factor)*
1623 e
= ast_for_atom(c
, CHILD(n
, 0));
1628 for (i
= 1; i
< NCH(n
); i
++) {
1629 node
*ch
= CHILD(n
, i
);
1630 if (TYPE(ch
) != trailer
)
1632 tmp
= ast_for_trailer(c
, ch
, e
);
1635 tmp
->lineno
= e
->lineno
;
1636 tmp
->col_offset
= e
->col_offset
;
1639 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1640 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1643 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1651 /* Do not name a variable 'expr'! Will cause a compile error.
1655 ast_for_expr(struct compiling
*c
, const node
*n
)
1657 /* handle the full range of simple expressions
1658 test: or_test ['if' or_test 'else' test] | lambdef
1659 or_test: and_test ('or' and_test)*
1660 and_test: not_test ('and' not_test)*
1661 not_test: 'not' not_test | comparison
1662 comparison: expr (comp_op expr)*
1663 expr: xor_expr ('|' xor_expr)*
1664 xor_expr: and_expr ('^' and_expr)*
1665 and_expr: shift_expr ('&' shift_expr)*
1666 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1667 arith_expr: term (('+'|'-') term)*
1668 term: factor (('*'|'/'|'%'|'//') factor)*
1669 factor: ('+'|'-'|'~') factor | power
1670 power: atom trailer* ('**' factor)*
1672 As well as modified versions that exist for backward compatibility,
1673 to explicitly allow:
1674 [ x for x in lambda: 0, lambda: 1 ]
1675 (which would be ambiguous without these extra rules)
1677 old_test: or_test | old_lambdef
1678 old_lambdef: 'lambda' [vararglist] ':' old_test
1689 if (TYPE(CHILD(n
, 0)) == lambdef
||
1690 TYPE(CHILD(n
, 0)) == old_lambdef
)
1691 return ast_for_lambdef(c
, CHILD(n
, 0));
1692 else if (NCH(n
) > 1)
1693 return ast_for_ifexpr(c
, n
);
1701 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1704 for (i
= 0; i
< NCH(n
); i
+= 2) {
1705 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1708 asdl_seq_SET(seq
, i
/ 2, e
);
1710 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1711 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1713 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1714 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1721 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1725 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1737 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1740 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1744 for (i
= 1; i
< NCH(n
); i
+= 2) {
1745 cmpop_ty newoperator
;
1747 newoperator
= ast_for_comp_op(CHILD(n
, i
));
1752 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1757 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1758 asdl_seq_SET(cmps
, i
/ 2, expression
);
1760 expression
= ast_for_expr(c
, CHILD(n
, 0));
1765 return Compare(expression
, ops
, cmps
, LINENO(n
),
1766 n
->n_col_offset
, c
->c_arena
);
1770 /* The next five cases all handle BinOps. The main body of code
1771 is the same in each case, but the switch turned inside out to
1772 reuse the code for each type of operator.
1784 return ast_for_binop(c
, n
);
1788 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1792 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1799 return ast_for_factor(c
, n
);
1801 return ast_for_power(c
, n
);
1803 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1806 /* should never get here unless if error is set */
1811 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1814 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1816 argument: [test '='] test [gen_for] # Really [keyword '='] test
1819 int i
, nargs
, nkeywords
, ngens
;
1822 expr_ty vararg
= NULL
, kwarg
= NULL
;
1829 for (i
= 0; i
< NCH(n
); i
++) {
1830 node
*ch
= CHILD(n
, i
);
1831 if (TYPE(ch
) == argument
) {
1834 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1840 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1841 ast_error(n
, "Generator expression must be parenthesized "
1842 "if not sole argument");
1846 if (nargs
+ nkeywords
+ ngens
> 255) {
1847 ast_error(n
, "more than 255 arguments");
1851 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1854 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1859 for (i
= 0; i
< NCH(n
); i
++) {
1860 node
*ch
= CHILD(n
, i
);
1861 if (TYPE(ch
) == argument
) {
1865 ast_error(CHILD(ch
, 0),
1866 "non-keyword arg after keyword arg");
1869 e
= ast_for_expr(c
, CHILD(ch
, 0));
1872 asdl_seq_SET(args
, nargs
++, e
);
1874 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1875 e
= ast_for_genexp(c
, ch
);
1878 asdl_seq_SET(args
, nargs
++, e
);
1884 /* CHILD(ch, 0) is test, but must be an identifier? */
1885 e
= ast_for_expr(c
, CHILD(ch
, 0));
1888 /* f(lambda x: x[0] = 3) ends up getting parsed with
1889 * LHS test = lambda x: x[0], and RHS test = 3.
1890 * SF bug 132313 points out that complaining about a keyword
1891 * then is very confusing.
1893 if (e
->kind
== Lambda_kind
) {
1894 ast_error(CHILD(ch
, 0),
1895 "lambda cannot contain assignment");
1897 } else if (e
->kind
!= Name_kind
) {
1898 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1902 if (!strcmp(PyString_AS_STRING(key
), "None")) {
1903 ast_error(CHILD(ch
, 0), "assignment to None");
1906 e
= ast_for_expr(c
, CHILD(ch
, 2));
1909 kw
= keyword(key
, e
, c
->c_arena
);
1912 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1915 else if (TYPE(ch
) == STAR
) {
1916 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1919 else if (TYPE(ch
) == DOUBLESTAR
) {
1920 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1925 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
1926 func
->col_offset
, c
->c_arena
);
1930 ast_for_testlist(struct compiling
*c
, const node
* n
)
1932 /* testlist_gexp: test (',' test)* [','] */
1933 /* testlist: test (',' test)* [','] */
1934 /* testlist_safe: test (',' test)+ [','] */
1935 /* testlist1: test (',' test)* */
1937 if (TYPE(n
) == testlist_gexp
) {
1939 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1942 assert(TYPE(n
) == testlist
||
1943 TYPE(n
) == testlist_safe
||
1944 TYPE(n
) == testlist1
);
1947 return ast_for_expr(c
, CHILD(n
, 0));
1949 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1952 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1957 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
1959 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1960 /* argument: test [ gen_for ] */
1961 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
1962 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
1963 return ast_for_genexp(c
, n
);
1964 return ast_for_testlist(c
, n
);
1967 /* like ast_for_testlist() but returns a sequence */
1969 ast_for_class_bases(struct compiling
*c
, const node
* n
)
1971 /* testlist: test (',' test)* [','] */
1976 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
1979 base
= ast_for_expr(c
, CHILD(n
, 0));
1982 asdl_seq_SET(bases
, 0, base
);
1986 return seq_for_testlist(c
, n
);
1990 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
1993 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1994 | ('=' (yield_expr|testlist))*)
1995 testlist: test (',' test)* [',']
1996 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1997 | '<<=' | '>>=' | '**=' | '//='
1998 test: ... here starts the operator precendence dance
2002 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2006 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2008 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2009 expr_ty expr1
, expr2
;
2010 operator_ty newoperator
;
2011 node
*ch
= CHILD(n
, 0);
2013 expr1
= ast_for_testlist(c
, ch
);
2016 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2017 switch (expr1
->kind
) {
2018 case GeneratorExp_kind
:
2019 ast_error(ch
, "augmented assignment to generator "
2020 "expression not possible");
2023 ast_error(ch
, "augmented assignment to yield "
2024 "expression not possible");
2027 const char *var_name
= PyString_AS_STRING(expr1
->v
.Name
.id
);
2028 if (var_name
[0] == 'N' && !strcmp(var_name
, "None")) {
2029 ast_error(ch
, "assignment to None");
2034 case Attribute_kind
:
2035 case Subscript_kind
:
2038 ast_error(ch
, "illegal expression for augmented "
2042 if(!set_context(expr1
, Store
, ch
))
2046 if (TYPE(ch
) == testlist
)
2047 expr2
= ast_for_testlist(c
, ch
);
2049 expr2
= ast_for_expr(c
, ch
);
2053 newoperator
= ast_for_augassign(CHILD(n
, 1));
2057 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2066 /* a normal assignment */
2067 REQ(CHILD(n
, 1), EQUAL
);
2068 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2071 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2073 node
*ch
= CHILD(n
, i
);
2074 if (TYPE(ch
) == yield_expr
) {
2075 ast_error(ch
, "assignment to yield expression not possible");
2078 e
= ast_for_testlist(c
, ch
);
2080 /* set context to assign */
2084 if (!set_context(e
, Store
, CHILD(n
, i
)))
2087 asdl_seq_SET(targets
, i
/ 2, e
);
2089 value
= CHILD(n
, NCH(n
) - 1);
2090 if (TYPE(value
) == testlist
)
2091 expression
= ast_for_testlist(c
, value
);
2093 expression
= ast_for_expr(c
, value
);
2096 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2102 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2104 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2105 | '>>' test [ (',' test)+ [','] ] )
2107 expr_ty dest
= NULL
, expression
;
2110 int i
, j
, start
= 1;
2113 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2114 dest
= ast_for_expr(c
, CHILD(n
, 2));
2119 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
2122 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2123 expression
= ast_for_expr(c
, CHILD(n
, i
));
2126 asdl_seq_SET(seq
, j
, expression
);
2128 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2129 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2133 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2141 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2144 for (i
= 0; i
< NCH(n
); i
+= 2) {
2145 e
= ast_for_expr(c
, CHILD(n
, i
));
2148 asdl_seq_SET(seq
, i
/ 2, e
);
2149 if (context
&& !set_context(e
, context
, CHILD(n
, i
)))
2156 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2158 asdl_seq
*expr_list
;
2160 /* del_stmt: 'del' exprlist */
2163 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2166 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2170 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2173 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2176 continue_stmt: 'continue'
2177 return_stmt: 'return' [testlist]
2178 yield_stmt: yield_expr
2179 yield_expr: 'yield' testlist
2180 raise_stmt: 'raise' [test [',' test [',' test]]]
2188 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2190 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2191 case yield_stmt
: { /* will reduce to yield_expr */
2192 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2195 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2199 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2201 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2204 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2209 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2211 else if (NCH(ch
) == 2) {
2212 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2215 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2216 n
->n_col_offset
, c
->c_arena
);
2218 else if (NCH(ch
) == 4) {
2219 expr_ty expr1
, expr2
;
2221 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2224 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2228 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2231 else if (NCH(ch
) == 6) {
2232 expr_ty expr1
, expr2
, expr3
;
2234 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2237 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2240 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2244 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2248 PyErr_Format(PyExc_SystemError
,
2249 "unexpected flow_stmt: %d", TYPE(ch
));
2253 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2258 alias_for_import_name(struct compiling
*c
, const node
*n
)
2261 import_as_name: NAME ['as' NAME]
2262 dotted_as_name: dotted_name ['as' NAME]
2263 dotted_name: NAME ('.' NAME)*
2269 case import_as_name
:
2272 str
= NEW_IDENTIFIER(CHILD(n
, 2));
2274 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2275 case dotted_as_name
:
2281 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2285 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2291 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2293 /* Create a string of the form "a.b.c" */
2299 for (i
= 0; i
< NCH(n
); i
+= 2)
2300 /* length of string plus one for the dot */
2301 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2302 len
--; /* the last name doesn't have a dot */
2303 str
= PyString_FromStringAndSize(NULL
, len
);
2306 s
= PyString_AS_STRING(str
);
2309 for (i
= 0; i
< NCH(n
); i
+= 2) {
2310 char *sch
= STR(CHILD(n
, i
));
2311 strcpy(s
, STR(CHILD(n
, i
)));
2317 PyString_InternInPlace(&str
);
2318 PyArena_AddPyObject(c
->c_arena
, str
);
2319 return alias(str
, NULL
, c
->c_arena
);
2323 str
= PyString_InternFromString("*");
2324 PyArena_AddPyObject(c
->c_arena
, str
);
2325 return alias(str
, NULL
, c
->c_arena
);
2327 PyErr_Format(PyExc_SystemError
,
2328 "unexpected import name: %d", TYPE(n
));
2332 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2337 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2340 import_stmt: import_name | import_from
2341 import_name: 'import' dotted_as_names
2342 import_from: 'from' ('.'* dotted_name | '.') 'import'
2343 ('*' | '(' import_as_names ')' | import_as_names)
2350 REQ(n
, import_stmt
);
2352 col_offset
= n
->n_col_offset
;
2354 if (TYPE(n
) == import_name
) {
2356 REQ(n
, dotted_as_names
);
2357 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2360 for (i
= 0; i
< NCH(n
); i
+= 2) {
2361 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2364 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2366 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2368 else if (TYPE(n
) == import_from
) {
2371 alias_ty mod
= NULL
;
2374 /* Count the number of dots (for relative imports) and check for the
2375 optional module name */
2376 for (idx
= 1; idx
< NCH(n
); idx
++) {
2377 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2378 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2381 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2386 idx
++; /* skip over the 'import' keyword */
2387 switch (TYPE(CHILD(n
, idx
))) {
2389 /* from ... import * */
2393 ast_error(n
, "'import *' not allowed with 'from .'");
2398 /* from ... import (x, y, z) */
2399 n
= CHILD(n
, idx
+ 1);
2400 n_children
= NCH(n
);
2402 case import_as_names
:
2403 /* from ... import x, y, z */
2405 n_children
= NCH(n
);
2406 if (n_children
% 2 == 0) {
2407 ast_error(n
, "trailing comma not allowed without"
2408 " surrounding parentheses");
2413 ast_error(n
, "Unexpected node-type in from-import");
2417 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2421 /* handle "from ... import *" special b/c there's no children */
2422 if (TYPE(n
) == STAR
) {
2423 alias_ty import_alias
= alias_for_import_name(c
, n
);
2426 asdl_seq_SET(aliases
, 0, import_alias
);
2429 for (i
= 0; i
< NCH(n
); i
+= 2) {
2430 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2433 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2437 modname
= mod
->name
;
2439 modname
= new_identifier("", c
->c_arena
);
2440 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2443 PyErr_Format(PyExc_SystemError
,
2444 "unknown import statement: starts with command '%s'",
2450 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2452 /* global_stmt: 'global' NAME (',' NAME)* */
2457 REQ(n
, global_stmt
);
2458 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2461 for (i
= 1; i
< NCH(n
); i
+= 2) {
2462 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2465 asdl_seq_SET(s
, i
/ 2, name
);
2467 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2471 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2473 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2474 int n_children
= NCH(n
);
2475 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2476 PyErr_Format(PyExc_SystemError
,
2477 "poorly formed 'exec' statement: %d parts to statement",
2482 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2484 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2487 if (n_children
>= 4) {
2488 globals
= ast_for_expr(c
, CHILD(n
, 3));
2492 if (n_children
== 6) {
2493 locals
= ast_for_expr(c
, CHILD(n
, 5));
2498 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2503 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2505 /* assert_stmt: 'assert' test [',' test] */
2506 REQ(n
, assert_stmt
);
2508 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2511 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2514 else if (NCH(n
) == 4) {
2515 expr_ty expr1
, expr2
;
2517 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2520 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2524 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2526 PyErr_Format(PyExc_SystemError
,
2527 "improper number of parts to 'assert' statement: %d",
2533 ast_for_suite(struct compiling
*c
, const node
*n
)
2535 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2538 int i
, total
, num
, end
, pos
= 0;
2543 total
= num_stmts(n
);
2544 seq
= asdl_seq_new(total
, c
->c_arena
);
2547 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2549 /* simple_stmt always ends with a NEWLINE,
2550 and may have a trailing SEMI
2553 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2555 /* loop by 2 to skip semi-colons */
2556 for (i
= 0; i
< end
; i
+= 2) {
2558 s
= ast_for_stmt(c
, ch
);
2561 asdl_seq_SET(seq
, pos
++, s
);
2565 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2568 num
= num_stmts(ch
);
2570 /* small_stmt or compound_stmt with only one child */
2571 s
= ast_for_stmt(c
, ch
);
2574 asdl_seq_SET(seq
, pos
++, s
);
2579 REQ(ch
, simple_stmt
);
2580 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2581 /* statement terminates with a semi-colon ';' */
2582 if (NCH(CHILD(ch
, j
)) == 0) {
2583 assert((j
+ 1) == NCH(ch
));
2586 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2589 asdl_seq_SET(seq
, pos
++, s
);
2594 assert(pos
== seq
->size
);
2599 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2601 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2610 asdl_seq
*suite_seq
;
2612 expression
= ast_for_expr(c
, CHILD(n
, 1));
2615 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2619 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2623 s
= STR(CHILD(n
, 4));
2624 /* s[2], the third character in the string, will be
2630 asdl_seq
*seq1
, *seq2
;
2632 expression
= ast_for_expr(c
, CHILD(n
, 1));
2635 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2638 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2642 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2645 else if (s
[2] == 'i') {
2646 int i
, n_elif
, has_else
= 0;
2648 asdl_seq
*suite_seq
;
2649 asdl_seq
*orelse
= NULL
;
2650 n_elif
= NCH(n
) - 4;
2651 /* must reference the child n_elif+1 since 'else' token is third,
2652 not fourth, child from the end. */
2653 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2654 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2661 asdl_seq
*suite_seq2
;
2663 orelse
= asdl_seq_new(1, c
->c_arena
);
2666 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2669 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2672 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2676 asdl_seq_SET(orelse
, 0,
2677 If(expression
, suite_seq
, suite_seq2
,
2678 LINENO(CHILD(n
, NCH(n
) - 6)),
2679 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2681 /* the just-created orelse handled the last elif */
2685 for (i
= 0; i
< n_elif
; i
++) {
2686 int off
= 5 + (n_elif
- i
- 1) * 4;
2687 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2690 expression
= ast_for_expr(c
, CHILD(n
, off
));
2693 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2697 asdl_seq_SET(newobj
, 0,
2698 If(expression
, suite_seq
, orelse
,
2699 LINENO(CHILD(n
, off
)),
2700 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2703 expression
= ast_for_expr(c
, CHILD(n
, 1));
2706 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2709 return If(expression
, suite_seq
, orelse
,
2710 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2713 PyErr_Format(PyExc_SystemError
,
2714 "unexpected token in 'if' statement: %s", s
);
2719 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2721 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2726 asdl_seq
*suite_seq
;
2728 expression
= ast_for_expr(c
, CHILD(n
, 1));
2731 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2734 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2737 else if (NCH(n
) == 7) {
2739 asdl_seq
*seq1
, *seq2
;
2741 expression
= ast_for_expr(c
, CHILD(n
, 1));
2744 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2747 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2751 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2755 PyErr_Format(PyExc_SystemError
,
2756 "wrong number of tokens for 'while' statement: %d",
2762 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2764 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2767 const node
*node_target
;
2768 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2772 seq
= ast_for_suite(c
, CHILD(n
, 8));
2777 node_target
= CHILD(n
, 1);
2778 _target
= ast_for_exprlist(c
, node_target
, Store
);
2781 /* Check the # of children rather than the length of _target, since
2782 for x, in ... has 1 element in _target, but still requires a Tuple. */
2783 if (NCH(node_target
) == 1)
2784 target
= (expr_ty
)asdl_seq_GET(_target
, 0);
2786 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2788 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2791 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2795 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2799 static excepthandler_ty
2800 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2802 /* except_clause: 'except' [test [(',' | 'as') test]] */
2803 REQ(exc
, except_clause
);
2806 if (NCH(exc
) == 1) {
2807 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2811 return excepthandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2812 exc
->n_col_offset
, c
->c_arena
);
2814 else if (NCH(exc
) == 2) {
2816 asdl_seq
*suite_seq
;
2818 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2821 suite_seq
= ast_for_suite(c
, body
);
2825 return excepthandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2826 exc
->n_col_offset
, c
->c_arena
);
2828 else if (NCH(exc
) == 4) {
2829 asdl_seq
*suite_seq
;
2831 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2834 if (!set_context(e
, Store
, CHILD(exc
, 3)))
2836 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2839 suite_seq
= ast_for_suite(c
, body
);
2843 return excepthandler(expression
, e
, suite_seq
, LINENO(exc
),
2844 exc
->n_col_offset
, c
->c_arena
);
2847 PyErr_Format(PyExc_SystemError
,
2848 "wrong number of children for 'except' clause: %d",
2854 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2856 const int nch
= NCH(n
);
2857 int n_except
= (nch
- 3)/3;
2858 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2862 body
= ast_for_suite(c
, CHILD(n
, 2));
2866 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2867 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2868 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2869 /* we can assume it's an "else",
2870 because nch >= 9 for try-else-finally and
2871 it would otherwise have a type of except_clause */
2872 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2878 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2879 if (finally
== NULL
)
2884 /* we can assume it's an "else",
2885 otherwise it would have a type of except_clause */
2886 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2892 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2893 ast_error(n
, "malformed 'try' statement");
2900 /* process except statements to create a try ... except */
2901 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2902 if (handlers
== NULL
)
2905 for (i
= 0; i
< n_except
; i
++) {
2906 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2907 CHILD(n
, 5 + i
* 3));
2910 asdl_seq_SET(handlers
, i
, e
);
2913 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
2914 n
->n_col_offset
, c
->c_arena
);
2918 /* if a 'finally' is present too, we nest the TryExcept within a
2919 TryFinally to emulate try ... except ... finally */
2920 body
= asdl_seq_new(1, c
->c_arena
);
2923 asdl_seq_SET(body
, 0, except_st
);
2926 /* must be a try ... finally (except clauses are in body, if any exist) */
2927 assert(finally
!= NULL
);
2928 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2932 ast_for_with_var(struct compiling
*c
, const node
*n
)
2935 return ast_for_expr(c
, CHILD(n
, 1));
2938 /* with_stmt: 'with' test [ with_var ] ':' suite */
2940 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2942 expr_ty context_expr
, optional_vars
= NULL
;
2943 int suite_index
= 3; /* skip 'with', test, and ':' */
2944 asdl_seq
*suite_seq
;
2946 assert(TYPE(n
) == with_stmt
);
2947 context_expr
= ast_for_expr(c
, CHILD(n
, 1));
2950 if (TYPE(CHILD(n
, 2)) == with_var
) {
2951 optional_vars
= ast_for_with_var(c
, CHILD(n
, 2));
2953 if (!optional_vars
) {
2956 if (!set_context(optional_vars
, Store
, n
)) {
2962 suite_seq
= ast_for_suite(c
, CHILD(n
, suite_index
));
2966 return With(context_expr
, optional_vars
, suite_seq
, LINENO(n
),
2967 n
->n_col_offset
, c
->c_arena
);
2971 ast_for_classdef(struct compiling
*c
, const node
*n
)
2973 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2974 asdl_seq
*bases
, *s
;
2978 if (!strcmp(STR(CHILD(n
, 1)), "None")) {
2979 ast_error(n
, "assignment to None");
2984 s
= ast_for_suite(c
, CHILD(n
, 3));
2987 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2988 n
->n_col_offset
, c
->c_arena
);
2990 /* check for empty base list */
2991 if (TYPE(CHILD(n
,3)) == RPAR
) {
2992 s
= ast_for_suite(c
, CHILD(n
,5));
2995 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2996 n
->n_col_offset
, c
->c_arena
);
2999 /* else handle the base class list */
3000 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3004 s
= ast_for_suite(c
, CHILD(n
, 6));
3007 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, LINENO(n
),
3008 n
->n_col_offset
, c
->c_arena
);
3012 ast_for_stmt(struct compiling
*c
, const node
*n
)
3014 if (TYPE(n
) == stmt
) {
3015 assert(NCH(n
) == 1);
3018 if (TYPE(n
) == simple_stmt
) {
3019 assert(num_stmts(n
) == 1);
3022 if (TYPE(n
) == small_stmt
) {
3025 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3026 | flow_stmt | import_stmt | global_stmt | exec_stmt
3031 return ast_for_expr_stmt(c
, n
);
3033 return ast_for_print_stmt(c
, n
);
3035 return ast_for_del_stmt(c
, n
);
3037 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3039 return ast_for_flow_stmt(c
, n
);
3041 return ast_for_import_stmt(c
, n
);
3043 return ast_for_global_stmt(c
, n
);
3045 return ast_for_exec_stmt(c
, n
);
3047 return ast_for_assert_stmt(c
, n
);
3049 PyErr_Format(PyExc_SystemError
,
3050 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3056 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3057 | funcdef | classdef
3059 node
*ch
= CHILD(n
, 0);
3060 REQ(n
, compound_stmt
);
3063 return ast_for_if_stmt(c
, ch
);
3065 return ast_for_while_stmt(c
, ch
);
3067 return ast_for_for_stmt(c
, ch
);
3069 return ast_for_try_stmt(c
, ch
);
3071 return ast_for_with_stmt(c
, ch
);
3073 return ast_for_funcdef(c
, ch
);
3075 return ast_for_classdef(c
, ch
);
3077 PyErr_Format(PyExc_SystemError
,
3078 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3086 parsenumber(const char *s
)
3091 #ifndef WITHOUT_COMPLEX
3097 end
= s
+ strlen(s
) - 1;
3098 #ifndef WITHOUT_COMPLEX
3099 imflag
= *end
== 'j' || *end
== 'J';
3101 if (*end
== 'l' || *end
== 'L')
3102 return PyLong_FromString((char *)s
, (char **)0, 0);
3104 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
3105 if (x
< 0 && errno
== 0) {
3106 return PyLong_FromString((char *)s
,
3112 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3115 return PyLong_FromString((char *)s
, (char **)0, 0);
3116 return PyInt_FromLong(x
);
3118 /* XXX Huge floats may silently fail */
3119 #ifndef WITHOUT_COMPLEX
3122 PyFPE_START_PROTECT("atof", return 0)
3123 c
.imag
= PyOS_ascii_atof(s
);
3124 PyFPE_END_PROTECT(c
)
3125 return PyComplex_FromCComplex(c
);
3130 PyFPE_START_PROTECT("atof", return 0)
3131 dx
= PyOS_ascii_atof(s
);
3132 PyFPE_END_PROTECT(dx
)
3133 return PyFloat_FromDouble(dx
);
3138 decode_utf8(const char **sPtr
, const char *end
, char* encoding
)
3140 #ifndef Py_USING_UNICODE
3141 Py_FatalError("decode_utf8 should not be called in this build.");
3146 t
= s
= (char *)*sPtr
;
3147 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3148 while (s
< end
&& (*s
& 0x80)) s
++;
3150 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3153 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3159 #ifdef Py_USING_UNICODE
3161 decode_unicode(const char *s
, size_t len
, int rawmode
, const char *encoding
)
3167 if (encoding
== NULL
) {
3170 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3174 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3175 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
3178 p
= buf
= PyString_AsString(u
);
3188 if (*s
& 0x80) { /* XXX inefficient */
3192 w
= decode_utf8(&s
, end
, "utf-16-be");
3197 r
= PyString_AsString(w
);
3198 rn
= PyString_Size(w
);
3199 assert(rn
% 2 == 0);
3200 for (i
= 0; i
< rn
; i
+= 2) {
3201 sprintf(p
, "\\u%02x%02x",
3215 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3217 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3223 /* s is a Python string literal, including the bracketing quote characters,
3224 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3225 * parsestr parses it, and returns the decoded Python string object.
3228 parsestr(const char *s
, const char *encoding
)
3231 int quote
= Py_CHARMASK(*s
);
3236 if (isalpha(quote
) || quote
== '_') {
3237 if (quote
== 'u' || quote
== 'U') {
3241 if (quote
== 'r' || quote
== 'R') {
3246 if (quote
!= '\'' && quote
!= '\"') {
3247 PyErr_BadInternalCall();
3252 if (len
> INT_MAX
) {
3253 PyErr_SetString(PyExc_OverflowError
,
3254 "string to parse is too long");
3257 if (s
[--len
] != quote
) {
3258 PyErr_BadInternalCall();
3261 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3264 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3265 PyErr_BadInternalCall();
3269 #ifdef Py_USING_UNICODE
3270 if (unicode
|| Py_UnicodeFlag
) {
3271 return decode_unicode(s
, len
, rawmode
, encoding
);
3274 need_encoding
= (encoding
!= NULL
&&
3275 strcmp(encoding
, "utf-8") != 0 &&
3276 strcmp(encoding
, "iso-8859-1") != 0);
3277 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3278 if (need_encoding
) {
3279 #ifndef Py_USING_UNICODE
3280 /* This should not happen - we never see any other
3283 "cannot deal with encodings in this build.");
3285 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3288 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3293 return PyString_FromStringAndSize(s
, len
);
3297 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3298 need_encoding
? encoding
: NULL
);
3301 /* Build a Python string object out of a STRING atom. This takes care of
3302 * compile-time literal catenation, calling parsestr() on each piece, and
3303 * pasting the intermediate results together.
3306 parsestrplus(struct compiling
*c
, const node
*n
)
3310 REQ(CHILD(n
, 0), STRING
);
3311 if ((v
= parsestr(STR(CHILD(n
, 0)), c
->c_encoding
)) != NULL
) {
3312 /* String literal concatenation */
3313 for (i
= 1; i
< NCH(n
); i
++) {
3315 s
= parsestr(STR(CHILD(n
, i
)), c
->c_encoding
);
3318 if (PyString_Check(v
) && PyString_Check(s
)) {
3319 PyString_ConcatAndDel(&v
, s
);
3323 #ifdef Py_USING_UNICODE
3325 PyObject
*temp
= PyUnicode_Concat(v
, s
);