2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
7 #include "Python-ast.h"
18 /* Data structure used internally */
20 char *c_encoding
; /* source encoding */
21 int c_future_unicode
; /* __future__ unicode literals flag */
22 PyArena
*c_arena
; /* arena for allocating memeory */
23 const char *c_filename
; /* filename */
26 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
27 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
28 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
29 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
30 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*,
32 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
33 static stmt_ty
ast_for_classdef(struct compiling
*, const node
*, asdl_seq
*);
34 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
36 /* Note different signature for ast_for_call */
37 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
39 static PyObject
*parsenumber(struct compiling
*, const char *);
40 static PyObject
*parsestr(struct compiling
*, const char *);
41 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
44 #define LINENO(n) ((n)->n_lineno)
48 new_identifier(const char* n
, PyArena
*arena
) {
49 PyObject
* id
= PyString_InternFromString(n
);
50 PyArena_AddPyObject(arena
, id
);
54 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
56 /* This routine provides an invalid object for the syntax error.
57 The outermost routine must unpack this error and create the
58 proper object. We do this so that we don't have to pass
59 the filename to everything function.
61 XXX Maybe we should just pass the filename...
65 ast_error(const node
*n
, const char *errstr
)
67 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
70 PyErr_SetObject(PyExc_SyntaxError
, u
);
76 ast_error_finish(const char *filename
)
78 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
81 assert(PyErr_Occurred());
82 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
85 PyErr_Fetch(&type
, &value
, &tback
);
86 errstr
= PyTuple_GetItem(value
, 0);
90 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
97 loc
= PyErr_ProgramText(filename
, lineno
);
102 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
108 value
= PyTuple_Pack(2, errstr
, tmp
);
113 PyErr_Restore(type
, value
, tback
);
117 ast_warn(struct compiling
*c
, const node
*n
, char *msg
)
119 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
, LINENO(n
),
121 /* if -Werr, change it to a SyntaxError */
122 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning
))
130 forbidden_check(struct compiling
*c
, const node
*n
, const char *x
)
132 if (!strcmp(x
, "None"))
133 return ast_error(n
, "assignment to None");
134 if (Py_Py3kWarningFlag
&& !(strcmp(x
, "True") && strcmp(x
, "False")) &&
135 !ast_warn(c
, n
, "assignment to True or False is forbidden in 3.x"))
140 /* num_stmts() returns number of contained statements.
142 Use this routine to determine how big a sequence is needed for
143 the statements in a parse tree. Its raison d'etre is this bit of
146 stmt: simple_stmt | compound_stmt
147 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
149 A simple_stmt can contain multiple small_stmt elements joined
150 by semicolons. If the arg is a simple_stmt, the number of
151 small_stmt elements is returned.
155 num_stmts(const node
*n
)
162 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
165 return num_stmts(CHILD(n
, 0));
168 for (i
= 0; i
< NCH(n
); i
++) {
170 if (TYPE(ch
) == stmt
)
175 return num_stmts(CHILD(n
, 0));
179 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
182 return num_stmts(CHILD(n
, 0));
185 for (i
= 2; i
< (NCH(n
) - 1); i
++)
186 l
+= num_stmts(CHILD(n
, i
));
192 sprintf(buf
, "Non-statement found: %d %d\n",
201 /* Transform the CST rooted at node * to the appropriate AST
205 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
209 asdl_seq
*stmts
= NULL
;
214 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
215 c
.c_encoding
= "utf-8";
216 if (TYPE(n
) == encoding_decl
) {
217 ast_error(n
, "encoding declaration in Unicode string");
220 } else if (TYPE(n
) == encoding_decl
) {
221 c
.c_encoding
= STR(n
);
226 c
.c_future_unicode
= flags
&& flags
->cf_flags
& CO_FUTURE_UNICODE_LITERALS
;
228 c
.c_filename
= filename
;
233 stmts
= asdl_seq_new(num_stmts(n
), arena
);
236 for (i
= 0; i
< NCH(n
) - 1; i
++) {
238 if (TYPE(ch
) == NEWLINE
)
243 s
= ast_for_stmt(&c
, ch
);
246 asdl_seq_SET(stmts
, k
++, s
);
250 REQ(ch
, simple_stmt
);
251 for (j
= 0; j
< num
; j
++) {
252 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
255 asdl_seq_SET(stmts
, k
++, s
);
259 return Module(stmts
, arena
);
261 expr_ty testlist_ast
;
263 /* XXX Why not gen_for here? */
264 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
267 return Expression(testlist_ast
, arena
);
270 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
271 stmts
= asdl_seq_new(1, arena
);
274 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
276 if (!asdl_seq_GET(stmts
, 0))
278 return Interactive(stmts
, arena
);
283 stmts
= asdl_seq_new(num
, arena
);
287 s
= ast_for_stmt(&c
, n
);
290 asdl_seq_SET(stmts
, 0, s
);
293 /* Only a simple_stmt can contain multiple statements. */
295 for (i
= 0; i
< NCH(n
); i
+= 2) {
296 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
298 s
= ast_for_stmt(&c
, CHILD(n
, i
));
301 asdl_seq_SET(stmts
, i
/ 2, s
);
305 return Interactive(stmts
, arena
);
308 PyErr_Format(PyExc_SystemError
,
309 "invalid node %d for PyAST_FromNode", TYPE(n
));
313 ast_error_finish(filename
);
317 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
321 get_operator(const node
*n
)
347 return (operator_ty
)0;
351 /* Set the context ctx for expr_ty e, recursively traversing e.
353 Only sets context for expr kinds that "can appear in assignment context"
354 (according to ../Parser/Python.asdl). For other expr kinds, it sets
355 an appropriate syntax error and returns false.
359 set_context(struct compiling
*c
, expr_ty e
, expr_context_ty ctx
, const node
*n
)
362 /* If a particular expression type can't be used for assign / delete,
363 set expr_name to its name and an error message will be generated.
365 const char* expr_name
= NULL
;
367 /* The ast defines augmented store and load contexts, but the
368 implementation here doesn't actually use them. The code may be
369 a little more complex than necessary as a result. It also means
370 that expressions in an augmented assignment have a Store context.
371 Consider restructuring so that augmented assignment uses
374 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
378 if (ctx
== Store
&& !forbidden_check(c
, n
,
379 PyBytes_AS_STRING(e
->v
.Attribute
.attr
)))
381 e
->v
.Attribute
.ctx
= ctx
;
384 e
->v
.Subscript
.ctx
= ctx
;
387 if (ctx
== Store
&& !forbidden_check(c
, n
,
388 PyBytes_AS_STRING(e
->v
.Name
.id
)))
397 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
398 return ast_error(n
, "can't assign to ()");
399 e
->v
.Tuple
.ctx
= ctx
;
403 expr_name
= "lambda";
406 expr_name
= "function call";
411 expr_name
= "operator";
413 case GeneratorExp_kind
:
414 expr_name
= "generator expression";
417 expr_name
= "yield expression";
420 expr_name
= "list comprehension";
425 expr_name
= "literal";
428 expr_name
= "comparison";
434 expr_name
= "conditional expression";
437 PyErr_Format(PyExc_SystemError
,
438 "unexpected expression in assignment %d (line %d)",
442 /* Check for error string set by switch */
445 PyOS_snprintf(buf
, sizeof(buf
),
447 ctx
== Store
? "assign to" : "delete",
449 return ast_error(n
, buf
);
452 /* If the LHS is a list or tuple, we need to set the assignment
453 context for all the contained elements.
458 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
459 if (!set_context(c
, (expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
467 ast_for_augassign(struct compiling
*c
, const node
*n
)
477 if (STR(n
)[1] == '/')
494 if (STR(n
)[1] == '*')
499 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
500 return (operator_ty
)0;
505 ast_for_comp_op(struct compiling
*c
, const node
*n
)
507 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
518 case EQEQUAL
: /* == */
527 if (strcmp(STR(n
), "in") == 0)
529 if (strcmp(STR(n
), "is") == 0)
532 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
537 else if (NCH(n
) == 2) {
538 /* handle "not in" and "is not" */
539 switch (TYPE(CHILD(n
, 0))) {
541 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
543 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
546 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
547 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
551 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
557 seq_for_testlist(struct compiling
*c
, const node
*n
)
559 /* testlist: test (',' test)* [','] */
563 assert(TYPE(n
) == testlist
||
564 TYPE(n
) == listmaker
||
565 TYPE(n
) == testlist_gexp
||
566 TYPE(n
) == testlist_safe
||
567 TYPE(n
) == testlist1
);
569 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
573 for (i
= 0; i
< NCH(n
); i
+= 2) {
574 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
576 expression
= ast_for_expr(c
, CHILD(n
, i
));
580 assert(i
/ 2 < seq
->size
);
581 asdl_seq_SET(seq
, i
/ 2, expression
);
587 compiler_complex_args(struct compiling
*c
, const node
*n
)
589 int i
, len
= (NCH(n
) + 1) / 2;
591 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
595 /* fpdef: NAME | '(' fplist ')'
596 fplist: fpdef (',' fpdef)* [',']
599 for (i
= 0; i
< len
; i
++) {
600 const node
*fpdef_node
= CHILD(n
, 2*i
);
604 /* fpdef_node is either a NAME or an fplist */
605 child
= CHILD(fpdef_node
, 0);
606 if (TYPE(child
) == NAME
) {
607 if (!forbidden_check(c
, n
, STR(child
)))
609 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
),
610 child
->n_col_offset
, c
->c_arena
);
613 assert(TYPE(fpdef_node
) == fpdef
);
614 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
615 child
= CHILD(fpdef_node
, 1);
616 assert(TYPE(child
) == fplist
);
617 /* NCH == 1 means we have (x), we need to elide the extra parens */
618 if (NCH(child
) == 1) {
619 fpdef_node
= CHILD(child
, 0);
620 assert(TYPE(fpdef_node
) == fpdef
);
623 arg
= compiler_complex_args(c
, child
);
625 asdl_seq_SET(args
, i
, arg
);
628 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
629 if (!set_context(c
, result
, Store
, n
))
635 /* Create AST for argument list. */
638 ast_for_arguments(struct compiling
*c
, const node
*n
)
640 /* parameters: '(' [varargslist] ')'
641 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
642 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
644 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
645 asdl_seq
*args
, *defaults
;
646 identifier vararg
= NULL
, kwarg
= NULL
;
649 if (TYPE(n
) == parameters
) {
650 if (NCH(n
) == 2) /* () as argument list */
651 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
656 /* first count the number of normal args & defaults */
657 for (i
= 0; i
< NCH(n
); i
++) {
659 if (TYPE(ch
) == fpdef
)
661 if (TYPE(ch
) == EQUAL
)
664 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
666 return NULL
; /* Don't need to goto error; no objects allocated */
667 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
668 if (!defaults
&& n_defaults
)
669 return NULL
; /* Don't need to goto error; no objects allocated */
671 /* fpdef: NAME | '(' fplist ')'
672 fplist: fpdef (',' fpdef)* [',']
675 j
= 0; /* index for defaults */
676 k
= 0; /* index for args */
682 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
683 anything other than EQUAL or a comma? */
684 /* XXX Should NCH(n) check be made a separate check? */
685 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
686 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
689 assert(defaults
!= NULL
);
690 asdl_seq_SET(defaults
, j
++, expression
);
694 else if (found_default
) {
696 "non-default argument follows default argument");
701 /* def foo((x)): is not complex, special case. */
703 /* We have complex arguments, setup for unpacking. */
704 if (Py_Py3kWarningFlag
&& !ast_warn(c
, ch
,
705 "tuple parameter unpacking has been removed in 3.x"))
707 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
708 if (!asdl_seq_GET(args
, k
-1))
711 /* def foo((x)): setup for checking NAME below. */
712 /* Loop because there can be many parens and tuple
713 unpacking mixed in. */
715 assert(TYPE(ch
) == fpdef
);
719 if (TYPE(CHILD(ch
, 0)) == NAME
) {
721 if (!forbidden_check(c
, n
, STR(CHILD(ch
, 0))))
723 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
724 Param
, LINENO(ch
), ch
->n_col_offset
,
728 asdl_seq_SET(args
, k
++, name
);
731 i
+= 2; /* the name and the comma */
734 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
736 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
740 if (!forbidden_check(c
, CHILD(n
, i
+1), STR(CHILD(n
, i
+1))))
742 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
746 PyErr_Format(PyExc_SystemError
,
747 "unexpected node in varargslist: %d @ %d",
753 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
762 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
766 int lineno
, col_offset
;
772 col_offset
= n
->n_col_offset
;
774 id
= NEW_IDENTIFIER(CHILD(n
, 0));
777 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
781 for (i
= 2; i
< NCH(n
); i
+=2) {
782 id
= NEW_IDENTIFIER(CHILD(n
, i
));
785 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
794 ast_for_decorator(struct compiling
*c
, const node
*n
)
796 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
801 REQ(CHILD(n
, 0), AT
);
802 REQ(RCHILD(n
, -1), NEWLINE
);
804 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
808 if (NCH(n
) == 3) { /* No arguments */
812 else if (NCH(n
) == 5) { /* Call with no arguments */
813 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
814 n
->n_col_offset
, c
->c_arena
);
820 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
830 ast_for_decorators(struct compiling
*c
, const node
*n
)
832 asdl_seq
* decorator_seq
;
837 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
841 for (i
= 0; i
< NCH(n
); i
++) {
842 d
= ast_for_decorator(c
, CHILD(n
, i
));
845 asdl_seq_SET(decorator_seq
, i
, d
);
847 return decorator_seq
;
851 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
853 /* funcdef: 'def' NAME parameters ':' suite */
861 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
864 else if (!forbidden_check(c
, CHILD(n
, name_i
), STR(CHILD(n
, name_i
))))
866 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
869 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
873 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
874 n
->n_col_offset
, c
->c_arena
);
878 ast_for_decorated(struct compiling
*c
, const node
*n
)
880 /* decorated: decorators (classdef | funcdef) */
881 stmt_ty thing
= NULL
;
882 asdl_seq
*decorator_seq
= NULL
;
886 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
890 assert(TYPE(CHILD(n
, 1)) == funcdef
||
891 TYPE(CHILD(n
, 1)) == classdef
);
893 if (TYPE(CHILD(n
, 1)) == funcdef
) {
894 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
895 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
896 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
898 /* we count the decorators in when talking about the class' or
899 function's line number */
901 thing
->lineno
= LINENO(n
);
902 thing
->col_offset
= n
->n_col_offset
;
908 ast_for_lambdef(struct compiling
*c
, const node
*n
)
910 /* lambdef: 'lambda' [varargslist] ':' test */
915 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
918 expression
= ast_for_expr(c
, CHILD(n
, 2));
923 args
= ast_for_arguments(c
, CHILD(n
, 1));
926 expression
= ast_for_expr(c
, CHILD(n
, 3));
931 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
935 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
937 /* test: or_test 'if' or_test 'else' test */
938 expr_ty expression
, body
, orelse
;
941 body
= ast_for_expr(c
, CHILD(n
, 0));
944 expression
= ast_for_expr(c
, CHILD(n
, 2));
947 orelse
= ast_for_expr(c
, CHILD(n
, 4));
950 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
954 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
955 so there is only a single version. Possibly for loops can also re-use
959 /* Count the number of 'for' loop in a list comprehension.
961 Helper for ast_for_listcomp().
965 count_list_fors(struct compiling
*c
, const node
*n
)
968 node
*ch
= CHILD(n
, 1);
980 if (TYPE(ch
) == list_for
)
982 else if (TYPE(ch
) == list_if
) {
985 goto count_list_iter
;
991 /* Should never be reached */
992 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
996 /* Count the number of 'if' statements in a list comprehension.
998 Helper for ast_for_listcomp().
1002 count_list_ifs(struct compiling
*c
, const node
*n
)
1008 if (TYPE(CHILD(n
, 0)) == list_for
)
1016 goto count_list_iter
;
1020 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1022 /* listmaker: test ( list_for | (',' test)* [','] )
1023 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1024 list_iter: list_for | list_if
1025 list_if: 'if' test [list_iter]
1026 testlist_safe: test [(',' test)+ [',']]
1029 asdl_seq
*listcomps
;
1036 elt
= ast_for_expr(c
, CHILD(n
, 0));
1040 n_fors
= count_list_fors(c
, n
);
1044 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1049 for (i
= 0; i
< n_fors
; i
++) {
1050 comprehension_ty lc
;
1057 for_ch
= CHILD(ch
, 1);
1058 t
= ast_for_exprlist(c
, for_ch
, Store
);
1061 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1065 /* Check the # of children rather than the length of t, since
1066 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1068 if (NCH(for_ch
) == 1)
1069 lc
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
, NULL
,
1072 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1074 expression
, NULL
, c
->c_arena
);
1081 expr_ty list_for_expr
;
1084 n_ifs
= count_list_ifs(c
, ch
);
1088 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1092 for (j
= 0; j
< n_ifs
; j
++) {
1097 list_for_expr
= ast_for_expr(c
, CHILD(ch
, 1));
1101 asdl_seq_SET(ifs
, j
, list_for_expr
);
1105 /* on exit, must guarantee that ch is a list_for */
1106 if (TYPE(ch
) == list_iter
)
1110 asdl_seq_SET(listcomps
, i
, lc
);
1113 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1116 /* Count the number of 'for' loops in a generator expression.
1118 Helper for ast_for_genexp().
1122 count_gen_fors(struct compiling
*c
, const node
*n
)
1125 node
*ch
= CHILD(n
, 1);
1137 if (TYPE(ch
) == gen_for
)
1139 else if (TYPE(ch
) == gen_if
) {
1142 goto count_gen_iter
;
1148 /* Should never be reached */
1149 PyErr_SetString(PyExc_SystemError
,
1150 "logic error in count_gen_fors");
1154 /* Count the number of 'if' statements in a generator expression.
1156 Helper for ast_for_genexp().
1160 count_gen_ifs(struct compiling
*c
, const node
*n
)
1166 if (TYPE(CHILD(n
, 0)) == gen_for
)
1177 /* TODO(jhylton): Combine with list comprehension code? */
1179 ast_for_genexp(struct compiling
*c
, const node
*n
)
1181 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1182 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1188 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1191 elt
= ast_for_expr(c
, CHILD(n
, 0));
1195 n_fors
= count_gen_fors(c
, n
);
1199 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1204 for (i
= 0; i
< n_fors
; i
++) {
1205 comprehension_ty ge
;
1212 for_ch
= CHILD(ch
, 1);
1213 t
= ast_for_exprlist(c
, for_ch
, Store
);
1216 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1220 /* Check the # of children rather than the length of t, since
1221 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1222 if (NCH(for_ch
) == 1)
1223 ge
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
,
1226 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1228 expression
, NULL
, c
->c_arena
);
1238 n_ifs
= count_gen_ifs(c
, ch
);
1242 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1246 for (j
= 0; j
< n_ifs
; j
++) {
1251 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1254 asdl_seq_SET(ifs
, j
, expression
);
1258 /* on exit, must guarantee that ch is a gen_for */
1259 if (TYPE(ch
) == gen_iter
)
1263 asdl_seq_SET(genexps
, i
, ge
);
1266 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1270 ast_for_atom(struct compiling
*c
, const node
*n
)
1272 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1273 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1275 node
*ch
= CHILD(n
, 0);
1279 /* All names start in Load context, but may later be
1281 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), n
->n_col_offset
,
1284 PyObject
*str
= parsestrplus(c
, n
);
1286 #ifdef Py_USING_UNICODE
1287 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)){
1288 PyObject
*type
, *value
, *tback
, *errstr
;
1289 PyErr_Fetch(&type
, &value
, &tback
);
1290 errstr
= ((PyUnicodeErrorObject
*)value
)->reason
;
1294 s
= PyString_AsString(errstr
);
1295 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1298 ast_error(n
, "(unicode error) unknown error");
1307 PyArena_AddPyObject(c
->c_arena
, str
);
1308 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1311 PyObject
*pynum
= parsenumber(c
, STR(ch
));
1315 PyArena_AddPyObject(c
->c_arena
, pynum
);
1316 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1318 case LPAR
: /* some parenthesized expressions */
1321 if (TYPE(ch
) == RPAR
)
1322 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1324 if (TYPE(ch
) == yield_expr
)
1325 return ast_for_expr(c
, ch
);
1327 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1328 return ast_for_genexp(c
, ch
);
1330 return ast_for_testlist_gexp(c
, ch
);
1331 case LSQB
: /* list (or list comprehension) */
1334 if (TYPE(ch
) == RSQB
)
1335 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1338 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1339 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1343 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1346 return ast_for_listcomp(c
, ch
);
1348 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1350 asdl_seq
*keys
, *values
;
1353 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1354 keys
= asdl_seq_new(size
, c
->c_arena
);
1358 values
= asdl_seq_new(size
, c
->c_arena
);
1362 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1365 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1369 asdl_seq_SET(keys
, i
/ 4, expression
);
1371 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1375 asdl_seq_SET(values
, i
/ 4, expression
);
1377 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1379 case BACKQUOTE
: { /* repr */
1381 if (Py_Py3kWarningFlag
&&
1382 !ast_warn(c
, n
, "backquote not supported in 3.x; use repr()"))
1384 expression
= ast_for_testlist(c
, CHILD(n
, 1));
1388 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1391 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1397 ast_for_slice(struct compiling
*c
, const node
*n
)
1400 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1405 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1409 if (TYPE(ch
) == DOT
)
1410 return Ellipsis(c
->c_arena
);
1412 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1413 /* 'step' variable hold no significance in terms of being used over
1415 step
= ast_for_expr(c
, ch
);
1419 return Index(step
, c
->c_arena
);
1422 if (TYPE(ch
) == test
) {
1423 lower
= ast_for_expr(c
, ch
);
1428 /* If there's an upper bound it's in the second or third position. */
1429 if (TYPE(ch
) == COLON
) {
1431 node
*n2
= CHILD(n
, 1);
1433 if (TYPE(n2
) == test
) {
1434 upper
= ast_for_expr(c
, n2
);
1439 } else if (NCH(n
) > 2) {
1440 node
*n2
= CHILD(n
, 2);
1442 if (TYPE(n2
) == test
) {
1443 upper
= ast_for_expr(c
, n2
);
1449 ch
= CHILD(n
, NCH(n
) - 1);
1450 if (TYPE(ch
) == sliceop
) {
1452 /* No expression, so step is None */
1454 step
= Name(new_identifier("None", c
->c_arena
), Load
,
1455 LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1460 if (TYPE(ch
) == test
) {
1461 step
= ast_for_expr(c
, ch
);
1468 return Slice(lower
, upper
, step
, c
->c_arena
);
1472 ast_for_binop(struct compiling
*c
, const node
*n
)
1474 /* Must account for a sequence of expressions.
1475 How should A op B op C by represented?
1476 BinOp(BinOp(A, op, B), op, C).
1480 expr_ty expr1
, expr2
, result
;
1481 operator_ty newoperator
;
1483 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1487 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1491 newoperator
= get_operator(CHILD(n
, 1));
1495 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1500 nops
= (NCH(n
) - 1) / 2;
1501 for (i
= 1; i
< nops
; i
++) {
1502 expr_ty tmp_result
, tmp
;
1503 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1505 newoperator
= get_operator(next_oper
);
1509 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1513 tmp_result
= BinOp(result
, newoperator
, tmp
,
1514 LINENO(next_oper
), next_oper
->n_col_offset
,
1518 result
= tmp_result
;
1524 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1526 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1527 subscriptlist: subscript (',' subscript)* [',']
1528 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1531 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1533 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1534 n
->n_col_offset
, c
->c_arena
);
1536 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1538 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1539 return Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
,
1540 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1543 REQ(CHILD(n
, 0), LSQB
);
1544 REQ(CHILD(n
, 2), RSQB
);
1547 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1550 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1554 /* The grammar is ambiguous here. The ambiguity is resolved
1555 by treating the sequence as a tuple literal if there are
1562 asdl_seq
*slices
, *elts
;
1563 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1566 for (j
= 0; j
< NCH(n
); j
+= 2) {
1567 slc
= ast_for_slice(c
, CHILD(n
, j
));
1570 if (slc
->kind
!= Index_kind
)
1572 asdl_seq_SET(slices
, j
/ 2, slc
);
1575 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1576 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1578 /* extract Index values and put them in a Tuple */
1579 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1582 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1583 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1584 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1585 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1587 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1590 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1591 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1597 ast_for_factor(struct compiling
*c
, const node
*n
)
1599 node
*pfactor
, *ppower
, *patom
, *pnum
;
1602 /* If the unary - operator is applied to a constant, don't generate
1603 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1604 constant. The peephole optimizer already does something like
1605 this but it doesn't handle the case where the constant is
1606 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1609 if (TYPE(CHILD(n
, 0)) == MINUS
&&
1611 TYPE((pfactor
= CHILD(n
, 1))) == factor
&&
1612 NCH(pfactor
) == 1 &&
1613 TYPE((ppower
= CHILD(pfactor
, 0))) == power
&&
1615 TYPE((patom
= CHILD(ppower
, 0))) == atom
&&
1616 TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1617 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1621 strcpy(s
+ 1, STR(pnum
));
1622 PyObject_FREE(STR(pnum
));
1624 return ast_for_atom(c
, patom
);
1627 expression
= ast_for_expr(c
, CHILD(n
, 1));
1631 switch (TYPE(CHILD(n
, 0))) {
1633 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1636 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1639 return UnaryOp(Invert
, expression
, LINENO(n
),
1640 n
->n_col_offset
, c
->c_arena
);
1642 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1648 ast_for_power(struct compiling
*c
, const node
*n
)
1650 /* power: atom trailer* ('**' factor)*
1655 e
= ast_for_atom(c
, CHILD(n
, 0));
1660 for (i
= 1; i
< NCH(n
); i
++) {
1661 node
*ch
= CHILD(n
, i
);
1662 if (TYPE(ch
) != trailer
)
1664 tmp
= ast_for_trailer(c
, ch
, e
);
1667 tmp
->lineno
= e
->lineno
;
1668 tmp
->col_offset
= e
->col_offset
;
1671 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1672 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1675 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1683 /* Do not name a variable 'expr'! Will cause a compile error.
1687 ast_for_expr(struct compiling
*c
, const node
*n
)
1689 /* handle the full range of simple expressions
1690 test: or_test ['if' or_test 'else' test] | lambdef
1691 or_test: and_test ('or' and_test)*
1692 and_test: not_test ('and' not_test)*
1693 not_test: 'not' not_test | comparison
1694 comparison: expr (comp_op expr)*
1695 expr: xor_expr ('|' xor_expr)*
1696 xor_expr: and_expr ('^' and_expr)*
1697 and_expr: shift_expr ('&' shift_expr)*
1698 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1699 arith_expr: term (('+'|'-') term)*
1700 term: factor (('*'|'/'|'%'|'//') factor)*
1701 factor: ('+'|'-'|'~') factor | power
1702 power: atom trailer* ('**' factor)*
1704 As well as modified versions that exist for backward compatibility,
1705 to explicitly allow:
1706 [ x for x in lambda: 0, lambda: 1 ]
1707 (which would be ambiguous without these extra rules)
1709 old_test: or_test | old_lambdef
1710 old_lambdef: 'lambda' [vararglist] ':' old_test
1721 if (TYPE(CHILD(n
, 0)) == lambdef
||
1722 TYPE(CHILD(n
, 0)) == old_lambdef
)
1723 return ast_for_lambdef(c
, CHILD(n
, 0));
1724 else if (NCH(n
) > 1)
1725 return ast_for_ifexpr(c
, n
);
1733 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1736 for (i
= 0; i
< NCH(n
); i
+= 2) {
1737 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1740 asdl_seq_SET(seq
, i
/ 2, e
);
1742 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1743 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1745 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1746 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1753 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1757 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1769 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1772 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1776 for (i
= 1; i
< NCH(n
); i
+= 2) {
1777 cmpop_ty newoperator
;
1779 newoperator
= ast_for_comp_op(c
, CHILD(n
, i
));
1784 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1789 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1790 asdl_seq_SET(cmps
, i
/ 2, expression
);
1792 expression
= ast_for_expr(c
, CHILD(n
, 0));
1797 return Compare(expression
, ops
, cmps
, LINENO(n
),
1798 n
->n_col_offset
, c
->c_arena
);
1802 /* The next five cases all handle BinOps. The main body of code
1803 is the same in each case, but the switch turned inside out to
1804 reuse the code for each type of operator.
1816 return ast_for_binop(c
, n
);
1820 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1824 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1831 return ast_for_factor(c
, n
);
1833 return ast_for_power(c
, n
);
1835 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1838 /* should never get here unless if error is set */
1843 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1846 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1848 argument: [test '='] test [gen_for] # Really [keyword '='] test
1851 int i
, nargs
, nkeywords
, ngens
;
1854 expr_ty vararg
= NULL
, kwarg
= NULL
;
1861 for (i
= 0; i
< NCH(n
); i
++) {
1862 node
*ch
= CHILD(n
, i
);
1863 if (TYPE(ch
) == argument
) {
1866 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1872 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1873 ast_error(n
, "Generator expression must be parenthesized "
1874 "if not sole argument");
1878 if (nargs
+ nkeywords
+ ngens
> 255) {
1879 ast_error(n
, "more than 255 arguments");
1883 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1886 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1891 for (i
= 0; i
< NCH(n
); i
++) {
1892 node
*ch
= CHILD(n
, i
);
1893 if (TYPE(ch
) == argument
) {
1897 ast_error(CHILD(ch
, 0),
1898 "non-keyword arg after keyword arg");
1901 e
= ast_for_expr(c
, CHILD(ch
, 0));
1904 asdl_seq_SET(args
, nargs
++, e
);
1906 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1907 e
= ast_for_genexp(c
, ch
);
1910 asdl_seq_SET(args
, nargs
++, e
);
1918 /* CHILD(ch, 0) is test, but must be an identifier? */
1919 e
= ast_for_expr(c
, CHILD(ch
, 0));
1922 /* f(lambda x: x[0] = 3) ends up getting parsed with
1923 * LHS test = lambda x: x[0], and RHS test = 3.
1924 * SF bug 132313 points out that complaining about a keyword
1925 * then is very confusing.
1927 if (e
->kind
== Lambda_kind
) {
1928 ast_error(CHILD(ch
, 0),
1929 "lambda cannot contain assignment");
1931 } else if (e
->kind
!= Name_kind
) {
1932 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1936 if (!forbidden_check(c
, CHILD(ch
, 0), PyBytes_AS_STRING(key
)))
1938 for (k
= 0; k
< nkeywords
; k
++) {
1939 tmp
= PyString_AS_STRING(
1940 ((keyword_ty
)asdl_seq_GET(keywords
, k
))->arg
);
1941 if (!strcmp(tmp
, PyString_AS_STRING(key
))) {
1942 ast_error(CHILD(ch
, 0), "keyword argument repeated");
1946 e
= ast_for_expr(c
, CHILD(ch
, 2));
1949 kw
= keyword(key
, e
, c
->c_arena
);
1952 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1955 else if (TYPE(ch
) == STAR
) {
1956 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1961 else if (TYPE(ch
) == DOUBLESTAR
) {
1962 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1969 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
,
1970 func
->col_offset
, c
->c_arena
);
1974 ast_for_testlist(struct compiling
*c
, const node
* n
)
1976 /* testlist_gexp: test (',' test)* [','] */
1977 /* testlist: test (',' test)* [','] */
1978 /* testlist_safe: test (',' test)+ [','] */
1979 /* testlist1: test (',' test)* */
1981 if (TYPE(n
) == testlist_gexp
) {
1983 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1986 assert(TYPE(n
) == testlist
||
1987 TYPE(n
) == testlist_safe
||
1988 TYPE(n
) == testlist1
);
1991 return ast_for_expr(c
, CHILD(n
, 0));
1993 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1996 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2001 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
2003 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
2004 /* argument: test [ gen_for ] */
2005 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
2006 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
2007 return ast_for_genexp(c
, n
);
2008 return ast_for_testlist(c
, n
);
2011 /* like ast_for_testlist() but returns a sequence */
2013 ast_for_class_bases(struct compiling
*c
, const node
* n
)
2015 /* testlist: test (',' test)* [','] */
2020 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
2023 base
= ast_for_expr(c
, CHILD(n
, 0));
2026 asdl_seq_SET(bases
, 0, base
);
2030 return seq_for_testlist(c
, n
);
2034 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2037 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2038 | ('=' (yield_expr|testlist))*)
2039 testlist: test (',' test)* [',']
2040 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2041 | '<<=' | '>>=' | '**=' | '//='
2042 test: ... here starts the operator precendence dance
2046 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2050 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2052 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2053 expr_ty expr1
, expr2
;
2054 operator_ty newoperator
;
2055 node
*ch
= CHILD(n
, 0);
2057 expr1
= ast_for_testlist(c
, ch
);
2060 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2061 switch (expr1
->kind
) {
2062 case GeneratorExp_kind
:
2063 ast_error(ch
, "augmented assignment to generator "
2064 "expression not possible");
2067 ast_error(ch
, "augmented assignment to yield "
2068 "expression not possible");
2071 const char *var_name
= PyBytes_AS_STRING(expr1
->v
.Name
.id
);
2072 if ((var_name
[0] == 'N' || var_name
[0] == 'T' || var_name
[0] == 'F') &&
2073 !forbidden_check(c
, ch
, var_name
))
2077 case Attribute_kind
:
2078 case Subscript_kind
:
2081 ast_error(ch
, "illegal expression for augmented "
2085 if(!set_context(c
, expr1
, Store
, ch
))
2089 if (TYPE(ch
) == testlist
)
2090 expr2
= ast_for_testlist(c
, ch
);
2092 expr2
= ast_for_expr(c
, ch
);
2096 newoperator
= ast_for_augassign(c
, CHILD(n
, 1));
2100 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
2109 /* a normal assignment */
2110 REQ(CHILD(n
, 1), EQUAL
);
2111 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2114 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2116 node
*ch
= CHILD(n
, i
);
2117 if (TYPE(ch
) == yield_expr
) {
2118 ast_error(ch
, "assignment to yield expression not possible");
2121 e
= ast_for_testlist(c
, ch
);
2123 /* set context to assign */
2127 if (!set_context(c
, e
, Store
, CHILD(n
, i
)))
2130 asdl_seq_SET(targets
, i
/ 2, e
);
2132 value
= CHILD(n
, NCH(n
) - 1);
2133 if (TYPE(value
) == testlist
)
2134 expression
= ast_for_testlist(c
, value
);
2136 expression
= ast_for_expr(c
, value
);
2139 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
,
2145 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2147 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2148 | '>>' test [ (',' test)+ [','] ] )
2150 expr_ty dest
= NULL
, expression
;
2153 int i
, j
, start
= 1;
2156 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2157 dest
= ast_for_expr(c
, CHILD(n
, 2));
2162 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
2165 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2166 expression
= ast_for_expr(c
, CHILD(n
, i
));
2169 asdl_seq_SET(seq
, j
, expression
);
2171 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2172 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2176 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2184 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2187 for (i
= 0; i
< NCH(n
); i
+= 2) {
2188 e
= ast_for_expr(c
, CHILD(n
, i
));
2191 asdl_seq_SET(seq
, i
/ 2, e
);
2192 if (context
&& !set_context(c
, e
, context
, CHILD(n
, i
)))
2199 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2201 asdl_seq
*expr_list
;
2203 /* del_stmt: 'del' exprlist */
2206 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2209 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2213 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2216 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2219 continue_stmt: 'continue'
2220 return_stmt: 'return' [testlist]
2221 yield_stmt: yield_expr
2222 yield_expr: 'yield' testlist
2223 raise_stmt: 'raise' [test [',' test [',' test]]]
2231 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2233 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2234 case yield_stmt
: { /* will reduce to yield_expr */
2235 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2238 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2242 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2244 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2247 return Return(expression
, LINENO(n
), n
->n_col_offset
,
2252 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
,
2254 else if (NCH(ch
) == 2) {
2255 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2258 return Raise(expression
, NULL
, NULL
, LINENO(n
),
2259 n
->n_col_offset
, c
->c_arena
);
2261 else if (NCH(ch
) == 4) {
2262 expr_ty expr1
, expr2
;
2264 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2267 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2271 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
,
2274 else if (NCH(ch
) == 6) {
2275 expr_ty expr1
, expr2
, expr3
;
2277 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2280 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2283 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2287 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
,
2291 PyErr_Format(PyExc_SystemError
,
2292 "unexpected flow_stmt: %d", TYPE(ch
));
2296 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2301 alias_for_import_name(struct compiling
*c
, const node
*n
)
2304 import_as_name: NAME ['as' NAME]
2305 dotted_as_name: dotted_name ['as' NAME]
2306 dotted_name: NAME ('.' NAME)*
2312 case import_as_name
:
2315 str
= NEW_IDENTIFIER(CHILD(n
, 2));
2317 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2318 case dotted_as_name
:
2324 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2328 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2334 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2336 /* Create a string of the form "a.b.c" */
2342 for (i
= 0; i
< NCH(n
); i
+= 2)
2343 /* length of string plus one for the dot */
2344 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2345 len
--; /* the last name doesn't have a dot */
2346 str
= PyString_FromStringAndSize(NULL
, len
);
2349 s
= PyString_AS_STRING(str
);
2352 for (i
= 0; i
< NCH(n
); i
+= 2) {
2353 char *sch
= STR(CHILD(n
, i
));
2354 strcpy(s
, STR(CHILD(n
, i
)));
2360 PyString_InternInPlace(&str
);
2361 PyArena_AddPyObject(c
->c_arena
, str
);
2362 return alias(str
, NULL
, c
->c_arena
);
2366 str
= PyString_InternFromString("*");
2367 PyArena_AddPyObject(c
->c_arena
, str
);
2368 return alias(str
, NULL
, c
->c_arena
);
2370 PyErr_Format(PyExc_SystemError
,
2371 "unexpected import name: %d", TYPE(n
));
2375 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2380 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2383 import_stmt: import_name | import_from
2384 import_name: 'import' dotted_as_names
2385 import_from: 'from' ('.'* dotted_name | '.') 'import'
2386 ('*' | '(' import_as_names ')' | import_as_names)
2393 REQ(n
, import_stmt
);
2395 col_offset
= n
->n_col_offset
;
2397 if (TYPE(n
) == import_name
) {
2399 REQ(n
, dotted_as_names
);
2400 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2403 for (i
= 0; i
< NCH(n
); i
+= 2) {
2404 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2407 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2409 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2411 else if (TYPE(n
) == import_from
) {
2414 alias_ty mod
= NULL
;
2417 /* Count the number of dots (for relative imports) and check for the
2418 optional module name */
2419 for (idx
= 1; idx
< NCH(n
); idx
++) {
2420 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2421 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2424 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2429 idx
++; /* skip over the 'import' keyword */
2430 switch (TYPE(CHILD(n
, idx
))) {
2432 /* from ... import * */
2437 /* from ... import (x, y, z) */
2438 n
= CHILD(n
, idx
+ 1);
2439 n_children
= NCH(n
);
2441 case import_as_names
:
2442 /* from ... import x, y, z */
2444 n_children
= NCH(n
);
2445 if (n_children
% 2 == 0) {
2446 ast_error(n
, "trailing comma not allowed without"
2447 " surrounding parentheses");
2452 ast_error(n
, "Unexpected node-type in from-import");
2456 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2460 /* handle "from ... import *" special b/c there's no children */
2461 if (TYPE(n
) == STAR
) {
2462 alias_ty import_alias
= alias_for_import_name(c
, n
);
2465 asdl_seq_SET(aliases
, 0, import_alias
);
2468 for (i
= 0; i
< NCH(n
); i
+= 2) {
2469 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2472 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2476 modname
= mod
->name
;
2478 modname
= new_identifier("", c
->c_arena
);
2479 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2482 PyErr_Format(PyExc_SystemError
,
2483 "unknown import statement: starts with command '%s'",
2489 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2491 /* global_stmt: 'global' NAME (',' NAME)* */
2496 REQ(n
, global_stmt
);
2497 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2500 for (i
= 1; i
< NCH(n
); i
+= 2) {
2501 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2504 asdl_seq_SET(s
, i
/ 2, name
);
2506 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2510 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2512 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2513 int n_children
= NCH(n
);
2514 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2515 PyErr_Format(PyExc_SystemError
,
2516 "poorly formed 'exec' statement: %d parts to statement",
2521 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2523 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2526 if (n_children
>= 4) {
2527 globals
= ast_for_expr(c
, CHILD(n
, 3));
2531 if (n_children
== 6) {
2532 locals
= ast_for_expr(c
, CHILD(n
, 5));
2537 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
,
2542 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2544 /* assert_stmt: 'assert' test [',' test] */
2545 REQ(n
, assert_stmt
);
2547 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2550 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
,
2553 else if (NCH(n
) == 4) {
2554 expr_ty expr1
, expr2
;
2556 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2559 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2563 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2565 PyErr_Format(PyExc_SystemError
,
2566 "improper number of parts to 'assert' statement: %d",
2572 ast_for_suite(struct compiling
*c
, const node
*n
)
2574 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2577 int i
, total
, num
, end
, pos
= 0;
2582 total
= num_stmts(n
);
2583 seq
= asdl_seq_new(total
, c
->c_arena
);
2586 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2588 /* simple_stmt always ends with a NEWLINE,
2589 and may have a trailing SEMI
2592 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2594 /* loop by 2 to skip semi-colons */
2595 for (i
= 0; i
< end
; i
+= 2) {
2597 s
= ast_for_stmt(c
, ch
);
2600 asdl_seq_SET(seq
, pos
++, s
);
2604 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2607 num
= num_stmts(ch
);
2609 /* small_stmt or compound_stmt with only one child */
2610 s
= ast_for_stmt(c
, ch
);
2613 asdl_seq_SET(seq
, pos
++, s
);
2618 REQ(ch
, simple_stmt
);
2619 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2620 /* statement terminates with a semi-colon ';' */
2621 if (NCH(CHILD(ch
, j
)) == 0) {
2622 assert((j
+ 1) == NCH(ch
));
2625 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2628 asdl_seq_SET(seq
, pos
++, s
);
2633 assert(pos
== seq
->size
);
2638 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2640 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2649 asdl_seq
*suite_seq
;
2651 expression
= ast_for_expr(c
, CHILD(n
, 1));
2654 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2658 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2662 s
= STR(CHILD(n
, 4));
2663 /* s[2], the third character in the string, will be
2669 asdl_seq
*seq1
, *seq2
;
2671 expression
= ast_for_expr(c
, CHILD(n
, 1));
2674 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2677 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2681 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2684 else if (s
[2] == 'i') {
2685 int i
, n_elif
, has_else
= 0;
2687 asdl_seq
*suite_seq
;
2688 asdl_seq
*orelse
= NULL
;
2689 n_elif
= NCH(n
) - 4;
2690 /* must reference the child n_elif+1 since 'else' token is third,
2691 not fourth, child from the end. */
2692 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2693 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2700 asdl_seq
*suite_seq2
;
2702 orelse
= asdl_seq_new(1, c
->c_arena
);
2705 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2708 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2711 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2715 asdl_seq_SET(orelse
, 0,
2716 If(expression
, suite_seq
, suite_seq2
,
2717 LINENO(CHILD(n
, NCH(n
) - 6)),
2718 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2720 /* the just-created orelse handled the last elif */
2724 for (i
= 0; i
< n_elif
; i
++) {
2725 int off
= 5 + (n_elif
- i
- 1) * 4;
2726 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2729 expression
= ast_for_expr(c
, CHILD(n
, off
));
2732 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2736 asdl_seq_SET(newobj
, 0,
2737 If(expression
, suite_seq
, orelse
,
2738 LINENO(CHILD(n
, off
)),
2739 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2742 expression
= ast_for_expr(c
, CHILD(n
, 1));
2745 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2748 return If(expression
, suite_seq
, orelse
,
2749 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2752 PyErr_Format(PyExc_SystemError
,
2753 "unexpected token in 'if' statement: %s", s
);
2758 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2760 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2765 asdl_seq
*suite_seq
;
2767 expression
= ast_for_expr(c
, CHILD(n
, 1));
2770 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2773 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2776 else if (NCH(n
) == 7) {
2778 asdl_seq
*seq1
, *seq2
;
2780 expression
= ast_for_expr(c
, CHILD(n
, 1));
2783 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2786 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2790 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2794 PyErr_Format(PyExc_SystemError
,
2795 "wrong number of tokens for 'while' statement: %d",
2801 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2803 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2806 const node
*node_target
;
2807 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2811 seq
= ast_for_suite(c
, CHILD(n
, 8));
2816 node_target
= CHILD(n
, 1);
2817 _target
= ast_for_exprlist(c
, node_target
, Store
);
2820 /* Check the # of children rather than the length of _target, since
2821 for x, in ... has 1 element in _target, but still requires a Tuple. */
2822 if (NCH(node_target
) == 1)
2823 target
= (expr_ty
)asdl_seq_GET(_target
, 0);
2825 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2827 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2830 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2834 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2838 static excepthandler_ty
2839 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2841 /* except_clause: 'except' [test [(',' | 'as') test]] */
2842 REQ(exc
, except_clause
);
2845 if (NCH(exc
) == 1) {
2846 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2850 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2851 exc
->n_col_offset
, c
->c_arena
);
2853 else if (NCH(exc
) == 2) {
2855 asdl_seq
*suite_seq
;
2857 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2860 suite_seq
= ast_for_suite(c
, body
);
2864 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2865 exc
->n_col_offset
, c
->c_arena
);
2867 else if (NCH(exc
) == 4) {
2868 asdl_seq
*suite_seq
;
2870 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2873 if (!set_context(c
, e
, Store
, CHILD(exc
, 3)))
2875 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2878 suite_seq
= ast_for_suite(c
, body
);
2882 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
2883 exc
->n_col_offset
, c
->c_arena
);
2886 PyErr_Format(PyExc_SystemError
,
2887 "wrong number of children for 'except' clause: %d",
2893 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2895 const int nch
= NCH(n
);
2896 int n_except
= (nch
- 3)/3;
2897 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2901 body
= ast_for_suite(c
, CHILD(n
, 2));
2905 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2906 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2907 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2908 /* we can assume it's an "else",
2909 because nch >= 9 for try-else-finally and
2910 it would otherwise have a type of except_clause */
2911 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2917 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2918 if (finally
== NULL
)
2923 /* we can assume it's an "else",
2924 otherwise it would have a type of except_clause */
2925 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2931 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2932 ast_error(n
, "malformed 'try' statement");
2939 /* process except statements to create a try ... except */
2940 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2941 if (handlers
== NULL
)
2944 for (i
= 0; i
< n_except
; i
++) {
2945 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2946 CHILD(n
, 5 + i
* 3));
2949 asdl_seq_SET(handlers
, i
, e
);
2952 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
2953 n
->n_col_offset
, c
->c_arena
);
2957 /* if a 'finally' is present too, we nest the TryExcept within a
2958 TryFinally to emulate try ... except ... finally */
2959 body
= asdl_seq_new(1, c
->c_arena
);
2962 asdl_seq_SET(body
, 0, except_st
);
2965 /* must be a try ... finally (except clauses are in body, if any exist) */
2966 assert(finally
!= NULL
);
2967 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2971 ast_for_with_var(struct compiling
*c
, const node
*n
)
2974 return ast_for_expr(c
, CHILD(n
, 1));
2977 /* with_stmt: 'with' test [ with_var ] ':' suite */
2979 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2981 expr_ty context_expr
, optional_vars
= NULL
;
2982 int suite_index
= 3; /* skip 'with', test, and ':' */
2983 asdl_seq
*suite_seq
;
2985 assert(TYPE(n
) == with_stmt
);
2986 context_expr
= ast_for_expr(c
, CHILD(n
, 1));
2989 if (TYPE(CHILD(n
, 2)) == with_var
) {
2990 optional_vars
= ast_for_with_var(c
, CHILD(n
, 2));
2992 if (!optional_vars
) {
2995 if (!set_context(c
, optional_vars
, Store
, n
)) {
3001 suite_seq
= ast_for_suite(c
, CHILD(n
, suite_index
));
3005 return With(context_expr
, optional_vars
, suite_seq
, LINENO(n
),
3006 n
->n_col_offset
, c
->c_arena
);
3010 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
3012 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3013 asdl_seq
*bases
, *s
;
3017 if (!forbidden_check(c
, n
, STR(CHILD(n
, 1))))
3021 s
= ast_for_suite(c
, CHILD(n
, 3));
3024 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, decorator_seq
,
3025 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3027 /* check for empty base list */
3028 if (TYPE(CHILD(n
,3)) == RPAR
) {
3029 s
= ast_for_suite(c
, CHILD(n
,5));
3032 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, decorator_seq
,
3033 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3036 /* else handle the base class list */
3037 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
3041 s
= ast_for_suite(c
, CHILD(n
, 6));
3044 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, decorator_seq
,
3045 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3049 ast_for_stmt(struct compiling
*c
, const node
*n
)
3051 if (TYPE(n
) == stmt
) {
3052 assert(NCH(n
) == 1);
3055 if (TYPE(n
) == simple_stmt
) {
3056 assert(num_stmts(n
) == 1);
3059 if (TYPE(n
) == small_stmt
) {
3062 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3063 | flow_stmt | import_stmt | global_stmt | exec_stmt
3068 return ast_for_expr_stmt(c
, n
);
3070 return ast_for_print_stmt(c
, n
);
3072 return ast_for_del_stmt(c
, n
);
3074 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3076 return ast_for_flow_stmt(c
, n
);
3078 return ast_for_import_stmt(c
, n
);
3080 return ast_for_global_stmt(c
, n
);
3082 return ast_for_exec_stmt(c
, n
);
3084 return ast_for_assert_stmt(c
, n
);
3086 PyErr_Format(PyExc_SystemError
,
3087 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3093 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3094 | funcdef | classdef | decorated
3096 node
*ch
= CHILD(n
, 0);
3097 REQ(n
, compound_stmt
);
3100 return ast_for_if_stmt(c
, ch
);
3102 return ast_for_while_stmt(c
, ch
);
3104 return ast_for_for_stmt(c
, ch
);
3106 return ast_for_try_stmt(c
, ch
);
3108 return ast_for_with_stmt(c
, ch
);
3110 return ast_for_funcdef(c
, ch
, NULL
);
3112 return ast_for_classdef(c
, ch
, NULL
);
3114 return ast_for_decorated(c
, ch
);
3116 PyErr_Format(PyExc_SystemError
,
3117 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3125 parsenumber(struct compiling
*c
, const char *s
)
3130 #ifndef WITHOUT_COMPLEX
3136 end
= s
+ strlen(s
) - 1;
3137 #ifndef WITHOUT_COMPLEX
3138 imflag
= *end
== 'j' || *end
== 'J';
3140 if (*end
== 'l' || *end
== 'L')
3141 return PyLong_FromString((char *)s
, (char **)0, 0);
3143 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
3144 if (x
< 0 && errno
== 0) {
3145 return PyLong_FromString((char *)s
,
3151 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3154 return PyLong_FromString((char *)s
, (char **)0, 0);
3155 return PyInt_FromLong(x
);
3157 /* XXX Huge floats may silently fail */
3158 #ifndef WITHOUT_COMPLEX
3161 PyFPE_START_PROTECT("atof", return 0)
3162 complex.imag
= PyOS_ascii_atof(s
);
3163 PyFPE_END_PROTECT(complex)
3164 return PyComplex_FromCComplex(complex);
3169 PyFPE_START_PROTECT("atof", return 0)
3170 dx
= PyOS_ascii_atof(s
);
3171 PyFPE_END_PROTECT(dx
)
3172 return PyFloat_FromDouble(dx
);
3177 decode_utf8(struct compiling
*c
, const char **sPtr
, const char *end
, char* encoding
)
3179 #ifndef Py_USING_UNICODE
3180 Py_FatalError("decode_utf8 should not be called in this build.");
3185 t
= s
= (char *)*sPtr
;
3186 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3187 while (s
< end
&& (*s
& 0x80)) s
++;
3189 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3192 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3198 #ifdef Py_USING_UNICODE
3200 decode_unicode(struct compiling
*c
, const char *s
, size_t len
, int rawmode
, const char *encoding
)
3206 if (encoding
== NULL
) {
3209 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3213 /* check for integer overflow */
3214 if (len
> PY_SIZE_MAX
/ 4)
3216 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3217 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
3220 p
= buf
= PyString_AsString(u
);
3230 if (*s
& 0x80) { /* XXX inefficient */
3234 w
= decode_utf8(c
, &s
, end
, "utf-16-be");
3239 r
= PyString_AsString(w
);
3240 rn
= PyString_Size(w
);
3241 assert(rn
% 2 == 0);
3242 for (i
= 0; i
< rn
; i
+= 2) {
3243 sprintf(p
, "\\u%02x%02x",
3257 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3259 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3265 /* s is a Python string literal, including the bracketing quote characters,
3266 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3267 * parsestr parses it, and returns the decoded Python string object.
3270 parsestr(struct compiling
*c
, const char *s
)
3273 int quote
= Py_CHARMASK(*s
);
3276 int unicode
= c
->c_future_unicode
;
3278 if (isalpha(quote
) || quote
== '_') {
3279 if (quote
== 'u' || quote
== 'U') {
3283 if (quote
== 'b' || quote
== 'B') {
3287 if (quote
== 'r' || quote
== 'R') {
3292 if (quote
!= '\'' && quote
!= '\"') {
3293 PyErr_BadInternalCall();
3298 if (len
> INT_MAX
) {
3299 PyErr_SetString(PyExc_OverflowError
,
3300 "string to parse is too long");
3303 if (s
[--len
] != quote
) {
3304 PyErr_BadInternalCall();
3307 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3310 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3311 PyErr_BadInternalCall();
3315 #ifdef Py_USING_UNICODE
3316 if (unicode
|| Py_UnicodeFlag
) {
3317 return decode_unicode(c
, s
, len
, rawmode
, c
->c_encoding
);
3320 need_encoding
= (c
->c_encoding
!= NULL
&&
3321 strcmp(c
->c_encoding
, "utf-8") != 0 &&
3322 strcmp(c
->c_encoding
, "iso-8859-1") != 0);
3323 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3324 if (need_encoding
) {
3325 #ifndef Py_USING_UNICODE
3326 /* This should not happen - we never see any other
3329 "cannot deal with encodings in this build.");
3331 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3334 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3339 return PyString_FromStringAndSize(s
, len
);
3343 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3344 need_encoding
? c
->c_encoding
: NULL
);
3347 /* Build a Python string object out of a STRING atom. This takes care of
3348 * compile-time literal catenation, calling parsestr() on each piece, and
3349 * pasting the intermediate results together.
3352 parsestrplus(struct compiling
*c
, const node
*n
)
3356 REQ(CHILD(n
, 0), STRING
);
3357 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
3358 /* String literal concatenation */
3359 for (i
= 1; i
< NCH(n
); i
++) {
3361 s
= parsestr(c
, STR(CHILD(n
, i
)));
3364 if (PyString_Check(v
) && PyString_Check(s
)) {
3365 PyString_ConcatAndDel(&v
, s
);
3369 #ifdef Py_USING_UNICODE
3371 PyObject
*temp
= PyUnicode_Concat(v
, s
);