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 */
24 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
25 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
26 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
27 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
28 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*, expr_context_ty
);
29 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
30 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
32 /* Note different signature for ast_for_call */
33 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
35 static PyObject
*parsenumber(const char *);
36 static PyObject
*parsestr(const char *s
, const char *encoding
);
37 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
40 #define LINENO(n) ((n)->n_lineno)
44 new_identifier(const char* n
, PyArena
*arena
) {
45 PyObject
* id
= PyString_InternFromString(n
);
46 PyArena_AddPyObject(arena
, id
);
50 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
52 /* This routine provides an invalid object for the syntax error.
53 The outermost routine must unpack this error and create the
54 proper object. We do this so that we don't have to pass
55 the filename to everything function.
57 XXX Maybe we should just pass the filename...
61 ast_error(const node
*n
, const char *errstr
)
63 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
66 PyErr_SetObject(PyExc_SyntaxError
, u
);
72 ast_error_finish(const char *filename
)
74 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
77 assert(PyErr_Occurred());
78 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
81 PyErr_Fetch(&type
, &value
, &tback
);
82 errstr
= PyTuple_GetItem(value
, 0);
86 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
93 loc
= PyErr_ProgramText(filename
, lineno
);
98 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
104 value
= PyTuple_Pack(2, errstr
, tmp
);
109 PyErr_Restore(type
, value
, tback
);
112 /* num_stmts() returns number of contained statements.
114 Use this routine to determine how big a sequence is needed for
115 the statements in a parse tree. Its raison d'etre is this bit of
118 stmt: simple_stmt | compound_stmt
119 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
121 A simple_stmt can contain multiple small_stmt elements joined
122 by semicolons. If the arg is a simple_stmt, the number of
123 small_stmt elements is returned.
127 num_stmts(const node
*n
)
134 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
137 return num_stmts(CHILD(n
, 0));
140 for (i
= 0; i
< NCH(n
); i
++) {
142 if (TYPE(ch
) == stmt
)
147 return num_stmts(CHILD(n
, 0));
151 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
154 return num_stmts(CHILD(n
, 0));
157 for (i
= 2; i
< (NCH(n
) - 1); i
++)
158 l
+= num_stmts(CHILD(n
, i
));
164 sprintf(buf
, "Non-statement found: %d %d\n",
173 /* Transform the CST rooted at node * to the appropriate AST
177 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
181 asdl_seq
*stmts
= NULL
;
186 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
187 c
.c_encoding
= "utf-8";
188 if (TYPE(n
) == encoding_decl
) {
189 ast_error(n
, "encoding declaration in Unicode string");
192 } else if (TYPE(n
) == encoding_decl
) {
193 c
.c_encoding
= STR(n
);
203 stmts
= asdl_seq_new(num_stmts(n
), arena
);
206 for (i
= 0; i
< NCH(n
) - 1; i
++) {
208 if (TYPE(ch
) == NEWLINE
)
213 s
= ast_for_stmt(&c
, ch
);
216 asdl_seq_SET(stmts
, k
++, s
);
220 REQ(ch
, simple_stmt
);
221 for (j
= 0; j
< num
; j
++) {
222 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
225 asdl_seq_SET(stmts
, k
++, s
);
229 return Module(stmts
, arena
);
231 expr_ty testlist_ast
;
233 /* XXX Why not gen_for here? */
234 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
237 return Expression(testlist_ast
, arena
);
240 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
241 stmts
= asdl_seq_new(1, arena
);
244 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
246 return Interactive(stmts
, arena
);
251 stmts
= asdl_seq_new(num
, arena
);
255 s
= ast_for_stmt(&c
, n
);
258 asdl_seq_SET(stmts
, 0, s
);
261 /* Only a simple_stmt can contain multiple statements. */
263 for (i
= 0; i
< NCH(n
); i
+= 2) {
264 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
266 s
= ast_for_stmt(&c
, CHILD(n
, i
));
269 asdl_seq_SET(stmts
, i
/ 2, s
);
273 return Interactive(stmts
, arena
);
279 ast_error_finish(filename
);
283 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
287 get_operator(const node
*n
)
313 return (operator_ty
)0;
317 /* Set the context ctx for expr_ty e, recursively traversing e.
319 Only sets context for expr kinds that "can appear in assignment context"
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
321 an appropriate syntax error and returns false.
325 set_context(expr_ty e
, expr_context_ty ctx
, const node
*n
)
328 /* If a particular expression type can't be used for assign / delete,
329 set expr_name to its name and an error message will be generated.
331 const char* expr_name
= NULL
;
333 /* The ast defines augmented store and load contexts, but the
334 implementation here doesn't actually use them. The code may be
335 a little more complex than necessary as a result. It also means
336 that expressions in an augmented assignment have a Store context.
337 Consider restructuring so that augmented assignment uses
340 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
345 !strcmp(PyString_AS_STRING(e
->v
.Attribute
.attr
), "None")) {
346 return ast_error(n
, "assignment to None");
348 e
->v
.Attribute
.ctx
= ctx
;
351 e
->v
.Subscript
.ctx
= ctx
;
355 !strcmp(PyString_AS_STRING(e
->v
.Name
.id
), "None")) {
356 return ast_error(n
, "assignment to None");
365 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
366 return ast_error(n
, "can't assign to ()");
367 e
->v
.Tuple
.ctx
= ctx
;
371 expr_name
= "lambda";
374 expr_name
= "function call";
379 expr_name
= "operator";
381 case GeneratorExp_kind
:
382 expr_name
= "generator expression";
385 expr_name
= "yield expression";
388 expr_name
= "list comprehension";
393 expr_name
= "literal";
396 expr_name
= "comparison";
402 expr_name
= "conditional expression";
405 PyErr_Format(PyExc_SystemError
,
406 "unexpected expression in assignment %d (line %d)",
410 /* Check for error string set by switch */
413 PyOS_snprintf(buf
, sizeof(buf
),
415 ctx
== Store
? "assign to" : "delete",
417 return ast_error(n
, buf
);
420 /* If the LHS is a list or tuple, we need to set the assignment
421 context for all the contained elements.
426 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
427 if (!set_context((expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
435 ast_for_augassign(const node
*n
)
445 if (STR(n
)[1] == '/')
462 if (STR(n
)[1] == '*')
467 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
468 return (operator_ty
)0;
473 ast_for_comp_op(const node
*n
)
475 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
486 case EQEQUAL
: /* == */
495 if (strcmp(STR(n
), "in") == 0)
497 if (strcmp(STR(n
), "is") == 0)
500 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
505 else if (NCH(n
) == 2) {
506 /* handle "not in" and "is not" */
507 switch (TYPE(CHILD(n
, 0))) {
509 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
511 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
514 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
515 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
519 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
525 seq_for_testlist(struct compiling
*c
, const node
*n
)
527 /* testlist: test (',' test)* [','] */
531 assert(TYPE(n
) == testlist
532 || TYPE(n
) == listmaker
533 || TYPE(n
) == testlist_gexp
534 || TYPE(n
) == testlist_safe
535 || TYPE(n
) == testlist1
538 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
542 for (i
= 0; i
< NCH(n
); i
+= 2) {
543 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
545 expression
= ast_for_expr(c
, CHILD(n
, i
));
549 assert(i
/ 2 < seq
->size
);
550 asdl_seq_SET(seq
, i
/ 2, expression
);
556 compiler_complex_args(struct compiling
*c
, const node
*n
)
558 int i
, len
= (NCH(n
) + 1) / 2;
560 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
564 /* fpdef: NAME | '(' fplist ')'
565 fplist: fpdef (',' fpdef)* [',']
568 for (i
= 0; i
< len
; i
++) {
569 const node
*fpdef_node
= CHILD(n
, 2*i
);
573 /* fpdef_node is either a NAME or an fplist */
574 child
= CHILD(fpdef_node
, 0);
575 if (TYPE(child
) == NAME
) {
576 if (!strcmp(STR(child
), "None")) {
577 ast_error(child
, "assignment to None");
580 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
),
581 child
->n_col_offset
, c
->c_arena
);
584 assert(TYPE(fpdef_node
) == fpdef
);
585 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
586 child
= CHILD(fpdef_node
, 1);
587 assert(TYPE(child
) == fplist
);
588 /* NCH == 1 means we have (x), we need to elide the extra parens */
589 if (NCH(child
) == 1) {
590 fpdef_node
= CHILD(child
, 0);
591 assert(TYPE(fpdef_node
) == fpdef
);
594 arg
= compiler_complex_args(c
, child
);
596 asdl_seq_SET(args
, i
, arg
);
599 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
600 if (!set_context(result
, Store
, n
))
606 /* Create AST for argument list. */
609 ast_for_arguments(struct compiling
*c
, const node
*n
)
611 /* parameters: '(' [varargslist] ')'
612 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
613 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
615 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
616 asdl_seq
*args
, *defaults
;
617 identifier vararg
= NULL
, kwarg
= NULL
;
620 if (TYPE(n
) == parameters
) {
621 if (NCH(n
) == 2) /* () as argument list */
622 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
627 /* first count the number of normal args & defaults */
628 for (i
= 0; i
< NCH(n
); i
++) {
630 if (TYPE(ch
) == fpdef
)
632 if (TYPE(ch
) == EQUAL
)
635 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
637 return NULL
; /* Don't need to goto error; no objects allocated */
638 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
639 if (!defaults
&& n_defaults
)
640 return NULL
; /* Don't need to goto error; no objects allocated */
642 /* fpdef: NAME | '(' fplist ')'
643 fplist: fpdef (',' fpdef)* [',']
646 j
= 0; /* index for defaults */
647 k
= 0; /* index for args */
653 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
654 anything other than EQUAL or a comma? */
655 /* XXX Should NCH(n) check be made a separate check? */
656 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
657 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
660 assert(defaults
!= NULL
);
661 asdl_seq_SET(defaults
, j
++, expression
);
665 else if (found_default
) {
667 "non-default argument follows default argument");
672 /* def foo((x)): is not complex, special case. */
674 /* We have complex arguments, setup for unpacking. */
675 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
677 /* def foo((x)): setup for checking NAME below. */
678 /* Loop because there can be many parens and tuple
679 unpacking mixed in. */
681 assert(TYPE(ch
) == fpdef
);
685 if (TYPE(CHILD(ch
, 0)) == NAME
) {
687 if (!strcmp(STR(CHILD(ch
, 0)), "None")) {
688 ast_error(CHILD(ch
, 0), "assignment to None");
691 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
692 Param
, LINENO(ch
), ch
->n_col_offset
,
696 asdl_seq_SET(args
, k
++, name
);
699 i
+= 2; /* the name and the comma */
702 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
703 ast_error(CHILD(n
, i
+1), "assignment to None");
706 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
710 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
711 ast_error(CHILD(n
, i
+1), "assignment to None");
714 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
718 PyErr_Format(PyExc_SystemError
,
719 "unexpected node in varargslist: %d @ %d",
725 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
734 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
738 int lineno
, col_offset
;
744 col_offset
= n
->n_col_offset
;
746 id
= NEW_IDENTIFIER(CHILD(n
, 0));
749 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
753 for (i
= 2; i
< NCH(n
); i
+=2) {
754 id
= NEW_IDENTIFIER(CHILD(n
, i
));
757 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
766 ast_for_decorator(struct compiling
*c
, const node
*n
)
768 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
773 REQ(CHILD(n
, 0), AT
);
774 REQ(RCHILD(n
, -1), NEWLINE
);
776 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
780 if (NCH(n
) == 3) { /* No arguments */
784 else if (NCH(n
) == 5) { /* Call with no arguments */
785 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
786 n
->n_col_offset
, c
->c_arena
);
792 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
802 ast_for_decorators(struct compiling
*c
, const node
*n
)
804 asdl_seq
* decorator_seq
;
809 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
813 for (i
= 0; i
< NCH(n
); i
++) {
814 d
= ast_for_decorator(c
, CHILD(n
, i
));
817 asdl_seq_SET(decorator_seq
, i
, d
);
819 return decorator_seq
;
823 ast_for_funcdef(struct compiling
*c
, const node
*n
)
825 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
829 asdl_seq
*decorator_seq
= NULL
;
834 if (NCH(n
) == 6) { /* decorators are present */
835 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
844 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
847 else if (!strcmp(STR(CHILD(n
, name_i
)), "None")) {
848 ast_error(CHILD(n
, name_i
), "assignment to None");
851 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
854 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
858 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
859 n
->n_col_offset
, c
->c_arena
);
863 ast_for_lambdef(struct compiling
*c
, const node
*n
)
865 /* lambdef: 'lambda' [varargslist] ':' test */
870 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
873 expression
= ast_for_expr(c
, CHILD(n
, 2));
878 args
= ast_for_arguments(c
, CHILD(n
, 1));
881 expression
= ast_for_expr(c
, CHILD(n
, 3));
886 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
890 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
892 /* test: or_test 'if' or_test 'else' test */
893 expr_ty expression
, body
, orelse
;
896 body
= ast_for_expr(c
, CHILD(n
, 0));
899 expression
= ast_for_expr(c
, CHILD(n
, 2));
902 orelse
= ast_for_expr(c
, CHILD(n
, 4));
905 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
909 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
910 so there is only a single version. Possibly for loops can also re-use
914 /* Count the number of 'for' loop in a list comprehension.
916 Helper for ast_for_listcomp().
920 count_list_fors(const node
*n
)
923 node
*ch
= CHILD(n
, 1);
935 if (TYPE(ch
) == list_for
)
937 else if (TYPE(ch
) == list_if
) {
940 goto count_list_iter
;
946 /* Should never be reached */
947 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
951 /* Count the number of 'if' statements in a list comprehension.
953 Helper for ast_for_listcomp().
957 count_list_ifs(const node
*n
)
963 if (TYPE(CHILD(n
, 0)) == list_for
)
971 goto count_list_iter
;
975 ast_for_listcomp(struct compiling
*c
, const node
*n
)
977 /* listmaker: test ( list_for | (',' test)* [','] )
978 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
979 list_iter: list_for | list_if
980 list_if: 'if' test [list_iter]
981 testlist_safe: test [(',' test)+ [',']]
991 elt
= ast_for_expr(c
, CHILD(n
, 0));
995 n_fors
= count_list_fors(n
);
999 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
1004 for (i
= 0; i
< n_fors
; i
++) {
1005 comprehension_ty lc
;
1012 for_ch
= CHILD(ch
, 1);
1013 t
= ast_for_exprlist(c
, for_ch
, Store
);
1016 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
1020 /* Check the # of children rather than the length of t, since
1021 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
1022 if (NCH(for_ch
) == 1)
1023 lc
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
, NULL
,
1026 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1028 expression
, NULL
, c
->c_arena
);
1037 n_ifs
= count_list_ifs(ch
);
1041 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1045 for (j
= 0; j
< n_ifs
; j
++) {
1050 asdl_seq_SET(ifs
, j
, ast_for_expr(c
, CHILD(ch
, 1)));
1054 /* on exit, must guarantee that ch is a list_for */
1055 if (TYPE(ch
) == list_iter
)
1059 asdl_seq_SET(listcomps
, i
, lc
);
1062 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1066 Count the number of 'for' loops in a generator expression.
1068 Helper for ast_for_genexp().
1072 count_gen_fors(const node
*n
)
1075 node
*ch
= CHILD(n
, 1);
1087 if (TYPE(ch
) == gen_for
)
1089 else if (TYPE(ch
) == gen_if
) {
1092 goto count_gen_iter
;
1098 /* Should never be reached */
1099 PyErr_SetString(PyExc_SystemError
,
1100 "logic error in count_gen_fors");
1104 /* Count the number of 'if' statements in a generator expression.
1106 Helper for ast_for_genexp().
1110 count_gen_ifs(const node
*n
)
1116 if (TYPE(CHILD(n
, 0)) == gen_for
)
1127 /* TODO(jhylton): Combine with list comprehension code? */
1129 ast_for_genexp(struct compiling
*c
, const node
*n
)
1131 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1132 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1138 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1141 elt
= ast_for_expr(c
, CHILD(n
, 0));
1145 n_fors
= count_gen_fors(n
);
1149 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1154 for (i
= 0; i
< n_fors
; i
++) {
1155 comprehension_ty ge
;
1162 for_ch
= CHILD(ch
, 1);
1163 t
= ast_for_exprlist(c
, for_ch
, Store
);
1166 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1170 /* Check the # of children rather than the length of t, since
1171 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1172 if (NCH(for_ch
) == 1)
1173 ge
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
,
1176 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1178 expression
, NULL
, c
->c_arena
);
1188 n_ifs
= count_gen_ifs(ch
);
1192 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1196 for (j
= 0; j
< n_ifs
; j
++) {
1201 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1204 asdl_seq_SET(ifs
, j
, expression
);
1208 /* on exit, must guarantee that ch is a gen_for */
1209 if (TYPE(ch
) == gen_iter
)
1213 asdl_seq_SET(genexps
, i
, ge
);
1216 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1220 ast_for_atom(struct compiling
*c
, const node
*n
)
1222 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1223 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1225 node
*ch
= CHILD(n
, 0);
1229 /* All names start in Load context, but may later be
1231 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1233 PyObject
*str
= parsestrplus(c
, n
);
1237 PyArena_AddPyObject(c
->c_arena
, str
);
1238 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1241 PyObject
*pynum
= parsenumber(STR(ch
));
1245 PyArena_AddPyObject(c
->c_arena
, pynum
);
1246 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1248 case LPAR
: /* some parenthesized expressions */
1251 if (TYPE(ch
) == RPAR
)
1252 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1254 if (TYPE(ch
) == yield_expr
)
1255 return ast_for_expr(c
, ch
);
1257 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1258 return ast_for_genexp(c
, ch
);
1260 return ast_for_testlist_gexp(c
, ch
);
1261 case LSQB
: /* list (or list comprehension) */
1264 if (TYPE(ch
) == RSQB
)
1265 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1268 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1269 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1273 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1276 return ast_for_listcomp(c
, ch
);
1278 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1280 asdl_seq
*keys
, *values
;
1283 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1284 keys
= asdl_seq_new(size
, c
->c_arena
);
1288 values
= asdl_seq_new(size
, c
->c_arena
);
1292 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1295 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1299 asdl_seq_SET(keys
, i
/ 4, expression
);
1301 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1305 asdl_seq_SET(values
, i
/ 4, expression
);
1307 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1309 case BACKQUOTE
: { /* repr */
1310 expr_ty expression
= ast_for_testlist(c
, CHILD(n
, 1));
1314 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1317 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1323 ast_for_slice(struct compiling
*c
, const node
*n
)
1326 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1331 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1335 if (TYPE(ch
) == DOT
)
1336 return Ellipsis(c
->c_arena
);
1338 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1339 /* 'step' variable hold no significance in terms of being used over
1341 step
= ast_for_expr(c
, ch
);
1345 return Index(step
, c
->c_arena
);
1348 if (TYPE(ch
) == test
) {
1349 lower
= ast_for_expr(c
, ch
);
1354 /* If there's an upper bound it's in the second or third position. */
1355 if (TYPE(ch
) == COLON
) {
1357 node
*n2
= CHILD(n
, 1);
1359 if (TYPE(n2
) == test
) {
1360 upper
= ast_for_expr(c
, n2
);
1365 } else if (NCH(n
) > 2) {
1366 node
*n2
= CHILD(n
, 2);
1368 if (TYPE(n2
) == test
) {
1369 upper
= ast_for_expr(c
, n2
);
1375 ch
= CHILD(n
, NCH(n
) - 1);
1376 if (TYPE(ch
) == sliceop
) {
1378 /* No expression, so step is None */
1380 step
= Name(new_identifier("None", c
->c_arena
), Load
,
1381 LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1386 if (TYPE(ch
) == test
) {
1387 step
= ast_for_expr(c
, ch
);
1394 return Slice(lower
, upper
, step
, c
->c_arena
);
1398 ast_for_binop(struct compiling
*c
, const node
*n
)
1400 /* Must account for a sequence of expressions.
1401 How should A op B op C by represented?
1402 BinOp(BinOp(A, op, B), op, C).
1406 expr_ty expr1
, expr2
, result
;
1407 operator_ty newoperator
;
1409 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1413 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1417 newoperator
= get_operator(CHILD(n
, 1));
1421 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1426 nops
= (NCH(n
) - 1) / 2;
1427 for (i
= 1; i
< nops
; i
++) {
1428 expr_ty tmp_result
, tmp
;
1429 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1431 newoperator
= get_operator(next_oper
);
1435 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1439 tmp_result
= BinOp(result
, newoperator
, tmp
,
1440 LINENO(next_oper
), next_oper
->n_col_offset
,
1444 result
= tmp_result
;
1450 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1452 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1453 subscriptlist: subscript (',' subscript)* [',']
1454 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1457 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1459 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1460 n
->n_col_offset
, c
->c_arena
);
1462 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1464 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1465 return Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
,
1466 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1469 REQ(CHILD(n
, 0), LSQB
);
1470 REQ(CHILD(n
, 2), RSQB
);
1473 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1476 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1480 /* The grammar is ambiguous here. The ambiguity is resolved
1481 by treating the sequence as a tuple literal if there are
1488 asdl_seq
*slices
, *elts
;
1489 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1492 for (j
= 0; j
< NCH(n
); j
+= 2) {
1493 slc
= ast_for_slice(c
, CHILD(n
, j
));
1496 if (slc
->kind
!= Index_kind
)
1498 asdl_seq_SET(slices
, j
/ 2, slc
);
1501 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1502 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1504 /* extract Index values and put them in a Tuple */
1505 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1508 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1509 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1510 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1511 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1513 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1516 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1517 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1523 ast_for_factor(struct compiling
*c
, const node
*n
)
1525 node
*pfactor
, *ppower
, *patom
, *pnum
;
1528 /* If the unary - operator is applied to a constant, don't generate
1529 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1530 constant. The peephole optimizer already does something like
1531 this but it doesn't handle the case where the constant is
1532 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1535 if (TYPE(CHILD(n
, 0)) == MINUS
1537 && TYPE((pfactor
= CHILD(n
, 1))) == factor
1538 && NCH(pfactor
) == 1
1539 && TYPE((ppower
= CHILD(pfactor
, 0))) == power
1541 && TYPE((patom
= CHILD(ppower
, 0))) == atom
1542 && TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1543 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1547 strcpy(s
+ 1, STR(pnum
));
1548 PyObject_FREE(STR(pnum
));
1550 return ast_for_atom(c
, patom
);
1553 expression
= ast_for_expr(c
, CHILD(n
, 1));
1557 switch (TYPE(CHILD(n
, 0))) {
1559 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1562 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1565 return UnaryOp(Invert
, expression
, LINENO(n
),
1566 n
->n_col_offset
, c
->c_arena
);
1568 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1574 ast_for_power(struct compiling
*c
, const node
*n
)
1576 /* power: atom trailer* ('**' factor)*
1581 e
= ast_for_atom(c
, CHILD(n
, 0));
1586 for (i
= 1; i
< NCH(n
); i
++) {
1587 node
*ch
= CHILD(n
, i
);
1588 if (TYPE(ch
) != trailer
)
1590 tmp
= ast_for_trailer(c
, ch
, e
);
1593 tmp
->lineno
= e
->lineno
;
1594 tmp
->col_offset
= e
->col_offset
;
1597 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1598 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1601 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1609 /* Do not name a variable 'expr'! Will cause a compile error.
1613 ast_for_expr(struct compiling
*c
, const node
*n
)
1615 /* handle the full range of simple expressions
1616 test: or_test ['if' or_test 'else' test] | lambdef
1617 or_test: and_test ('or' and_test)*
1618 and_test: not_test ('and' not_test)*
1619 not_test: 'not' not_test | comparison
1620 comparison: expr (comp_op expr)*
1621 expr: xor_expr ('|' xor_expr)*
1622 xor_expr: and_expr ('^' and_expr)*
1623 and_expr: shift_expr ('&' shift_expr)*
1624 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1625 arith_expr: term (('+'|'-') term)*
1626 term: factor (('*'|'/'|'%'|'//') factor)*
1627 factor: ('+'|'-'|'~') factor | power
1628 power: atom trailer* ('**' factor)*
1630 As well as modified versions that exist for backward compatibility,
1631 to explicitly allow:
1632 [ x for x in lambda: 0, lambda: 1 ]
1633 (which would be ambiguous without these extra rules)
1635 old_test: or_test | old_lambdef
1636 old_lambdef: 'lambda' [vararglist] ':' old_test
1647 if (TYPE(CHILD(n
, 0)) == lambdef
||
1648 TYPE(CHILD(n
, 0)) == old_lambdef
)
1649 return ast_for_lambdef(c
, CHILD(n
, 0));
1650 else if (NCH(n
) > 1)
1651 return ast_for_ifexpr(c
, n
);
1659 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1662 for (i
= 0; i
< NCH(n
); i
+= 2) {
1663 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1666 asdl_seq_SET(seq
, i
/ 2, e
);
1668 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1669 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1671 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1672 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1679 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1683 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1695 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1698 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1702 for (i
= 1; i
< NCH(n
); i
+= 2) {
1703 cmpop_ty newoperator
;
1705 newoperator
= ast_for_comp_op(CHILD(n
, i
));
1710 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1715 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1716 asdl_seq_SET(cmps
, i
/ 2, expression
);
1718 expression
= ast_for_expr(c
, CHILD(n
, 0));
1723 return Compare(expression
, ops
, cmps
, LINENO(n
),
1724 n
->n_col_offset
, c
->c_arena
);
1728 /* The next five cases all handle BinOps. The main body of code
1729 is the same in each case, but the switch turned inside out to
1730 reuse the code for each type of operator.
1742 return ast_for_binop(c
, n
);
1746 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1750 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1757 return ast_for_factor(c
, n
);
1759 return ast_for_power(c
, n
);
1761 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1764 /* should never get here unless if error is set */
1769 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1772 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1774 argument: [test '='] test [gen_for] # Really [keyword '='] test
1777 int i
, nargs
, nkeywords
, ngens
;
1780 expr_ty vararg
= NULL
, kwarg
= NULL
;
1787 for (i
= 0; i
< NCH(n
); i
++) {
1788 node
*ch
= CHILD(n
, i
);
1789 if (TYPE(ch
) == argument
) {
1792 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1798 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1799 ast_error(n
, "Generator expression must be parenthesized "
1800 "if not sole argument");
1804 if (nargs
+ nkeywords
+ ngens
> 255) {
1805 ast_error(n
, "more than 255 arguments");
1809 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1812 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1817 for (i
= 0; i
< NCH(n
); i
++) {
1818 node
*ch
= CHILD(n
, i
);
1819 if (TYPE(ch
) == argument
) {
1823 ast_error(CHILD(ch
, 0),
1824 "non-keyword arg after keyword arg");
1827 e
= ast_for_expr(c
, CHILD(ch
, 0));
1830 asdl_seq_SET(args
, nargs
++, e
);
1832 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1833 e
= ast_for_genexp(c
, ch
);
1836 asdl_seq_SET(args
, nargs
++, e
);
1842 /* CHILD(ch, 0) is test, but must be an identifier? */
1843 e
= ast_for_expr(c
, CHILD(ch
, 0));
1846 /* f(lambda x: x[0] = 3) ends up getting parsed with
1847 * LHS test = lambda x: x[0], and RHS test = 3.
1848 * SF bug 132313 points out that complaining about a keyword
1849 * then is very confusing.
1851 if (e
->kind
== Lambda_kind
) {
1852 ast_error(CHILD(ch
, 0), "lambda cannot contain assignment");
1854 } else if (e
->kind
!= Name_kind
) {
1855 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1859 e
= ast_for_expr(c
, CHILD(ch
, 2));
1862 kw
= keyword(key
, e
, c
->c_arena
);
1865 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1868 else if (TYPE(ch
) == STAR
) {
1869 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1872 else if (TYPE(ch
) == DOUBLESTAR
) {
1873 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1878 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
, func
->col_offset
, c
->c_arena
);
1882 ast_for_testlist(struct compiling
*c
, const node
* n
)
1884 /* testlist_gexp: test (',' test)* [','] */
1885 /* testlist: test (',' test)* [','] */
1886 /* testlist_safe: test (',' test)+ [','] */
1887 /* testlist1: test (',' test)* */
1889 if (TYPE(n
) == testlist_gexp
) {
1891 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1894 assert(TYPE(n
) == testlist
||
1895 TYPE(n
) == testlist_safe
||
1896 TYPE(n
) == testlist1
);
1899 return ast_for_expr(c
, CHILD(n
, 0));
1901 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1904 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1909 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
1911 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1912 /* argument: test [ gen_for ] */
1913 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
1914 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
1915 return ast_for_genexp(c
, n
);
1916 return ast_for_testlist(c
, n
);
1919 /* like ast_for_testlist() but returns a sequence */
1921 ast_for_class_bases(struct compiling
*c
, const node
* n
)
1923 /* testlist: test (',' test)* [','] */
1928 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
1931 base
= ast_for_expr(c
, CHILD(n
, 0));
1934 asdl_seq_SET(bases
, 0, base
);
1938 return seq_for_testlist(c
, n
);
1942 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
1945 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1946 | ('=' (yield_expr|testlist))*)
1947 testlist: test (',' test)* [',']
1948 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1949 | '<<=' | '>>=' | '**=' | '//='
1950 test: ... here starts the operator precendence dance
1954 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
1958 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1960 else if (TYPE(CHILD(n
, 1)) == augassign
) {
1961 expr_ty expr1
, expr2
;
1962 operator_ty newoperator
;
1963 node
*ch
= CHILD(n
, 0);
1965 expr1
= ast_for_testlist(c
, ch
);
1968 /* TODO(nas): Remove duplicated error checks (set_context does it) */
1969 switch (expr1
->kind
) {
1970 case GeneratorExp_kind
:
1971 ast_error(ch
, "augmented assignment to generator "
1972 "expression not possible");
1975 ast_error(ch
, "augmented assignment to yield "
1976 "expression not possible");
1979 const char *var_name
= PyString_AS_STRING(expr1
->v
.Name
.id
);
1980 if (var_name
[0] == 'N' && !strcmp(var_name
, "None")) {
1981 ast_error(ch
, "assignment to None");
1986 case Attribute_kind
:
1987 case Subscript_kind
:
1990 ast_error(ch
, "illegal expression for augmented "
1994 set_context(expr1
, Store
, ch
);
1997 if (TYPE(ch
) == testlist
)
1998 expr2
= ast_for_testlist(c
, ch
);
2000 expr2
= ast_for_expr(c
, ch
);
2004 newoperator
= ast_for_augassign(CHILD(n
, 1));
2008 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2016 /* a normal assignment */
2017 REQ(CHILD(n
, 1), EQUAL
);
2018 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2021 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2023 node
*ch
= CHILD(n
, i
);
2024 if (TYPE(ch
) == yield_expr
) {
2025 ast_error(ch
, "assignment to yield expression not possible");
2028 e
= ast_for_testlist(c
, ch
);
2030 /* set context to assign */
2034 if (!set_context(e
, Store
, CHILD(n
, i
)))
2037 asdl_seq_SET(targets
, i
/ 2, e
);
2039 value
= CHILD(n
, NCH(n
) - 1);
2040 if (TYPE(value
) == testlist
)
2041 expression
= ast_for_testlist(c
, value
);
2043 expression
= ast_for_expr(c
, value
);
2046 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2051 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
2053 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2054 | '>>' test [ (',' test)+ [','] ] )
2056 expr_ty dest
= NULL
, expression
;
2059 int i
, j
, start
= 1;
2062 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2063 dest
= ast_for_expr(c
, CHILD(n
, 2));
2068 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
2071 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2072 expression
= ast_for_expr(c
, CHILD(n
, i
));
2075 asdl_seq_SET(seq
, j
, expression
);
2077 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2078 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2082 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2090 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2093 for (i
= 0; i
< NCH(n
); i
+= 2) {
2094 e
= ast_for_expr(c
, CHILD(n
, i
));
2097 asdl_seq_SET(seq
, i
/ 2, e
);
2098 if (context
&& !set_context(e
, context
, CHILD(n
, i
)))
2105 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2107 asdl_seq
*expr_list
;
2109 /* del_stmt: 'del' exprlist */
2112 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2115 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2119 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2122 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2125 continue_stmt: 'continue'
2126 return_stmt: 'return' [testlist]
2127 yield_stmt: yield_expr
2128 yield_expr: 'yield' testlist
2129 raise_stmt: 'raise' [test [',' test [',' test]]]
2137 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2139 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2140 case yield_stmt
: { /* will reduce to yield_expr */
2141 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2144 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2148 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2150 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2153 return Return(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2157 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2158 else if (NCH(ch
) == 2) {
2159 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2162 return Raise(expression
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2164 else if (NCH(ch
) == 4) {
2165 expr_ty expr1
, expr2
;
2167 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2170 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2174 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2176 else if (NCH(ch
) == 6) {
2177 expr_ty expr1
, expr2
, expr3
;
2179 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2182 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2185 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2189 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2192 PyErr_Format(PyExc_SystemError
,
2193 "unexpected flow_stmt: %d", TYPE(ch
));
2197 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2202 alias_for_import_name(struct compiling
*c
, const node
*n
)
2205 import_as_name: NAME ['as' NAME]
2206 dotted_as_name: dotted_name ['as' NAME]
2207 dotted_name: NAME ('.' NAME)*
2213 case import_as_name
:
2216 str
= NEW_IDENTIFIER(CHILD(n
, 2));
2218 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2219 case dotted_as_name
:
2225 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2229 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2235 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2237 /* Create a string of the form "a.b.c" */
2243 for (i
= 0; i
< NCH(n
); i
+= 2)
2244 /* length of string plus one for the dot */
2245 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2246 len
--; /* the last name doesn't have a dot */
2247 str
= PyString_FromStringAndSize(NULL
, len
);
2250 s
= PyString_AS_STRING(str
);
2253 for (i
= 0; i
< NCH(n
); i
+= 2) {
2254 char *sch
= STR(CHILD(n
, i
));
2255 strcpy(s
, STR(CHILD(n
, i
)));
2261 PyString_InternInPlace(&str
);
2262 PyArena_AddPyObject(c
->c_arena
, str
);
2263 return alias(str
, NULL
, c
->c_arena
);
2267 str
= PyString_InternFromString("*");
2268 PyArena_AddPyObject(c
->c_arena
, str
);
2269 return alias(str
, NULL
, c
->c_arena
);
2271 PyErr_Format(PyExc_SystemError
,
2272 "unexpected import name: %d", TYPE(n
));
2276 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2281 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2284 import_stmt: import_name | import_from
2285 import_name: 'import' dotted_as_names
2286 import_from: 'from' ('.'* dotted_name | '.') 'import'
2287 ('*' | '(' import_as_names ')' | import_as_names)
2294 REQ(n
, import_stmt
);
2296 col_offset
= n
->n_col_offset
;
2298 if (TYPE(n
) == import_name
) {
2300 REQ(n
, dotted_as_names
);
2301 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2304 for (i
= 0; i
< NCH(n
); i
+= 2) {
2305 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2308 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2310 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2312 else if (TYPE(n
) == import_from
) {
2315 alias_ty mod
= NULL
;
2318 /* Count the number of dots (for relative imports) and check for the
2319 optional module name */
2320 for (idx
= 1; idx
< NCH(n
); idx
++) {
2321 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2322 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2325 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2330 idx
++; /* skip over the 'import' keyword */
2331 switch (TYPE(CHILD(n
, idx
))) {
2333 /* from ... import * */
2337 ast_error(n
, "'import *' not allowed with 'from .'");
2342 /* from ... import (x, y, z) */
2343 n
= CHILD(n
, idx
+ 1);
2344 n_children
= NCH(n
);
2346 case import_as_names
:
2347 /* from ... import x, y, z */
2349 n_children
= NCH(n
);
2350 if (n_children
% 2 == 0) {
2351 ast_error(n
, "trailing comma not allowed without"
2352 " surrounding parentheses");
2357 ast_error(n
, "Unexpected node-type in from-import");
2361 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2365 /* handle "from ... import *" special b/c there's no children */
2366 if (TYPE(n
) == STAR
) {
2367 alias_ty import_alias
= alias_for_import_name(c
, n
);
2370 asdl_seq_SET(aliases
, 0, import_alias
);
2373 for (i
= 0; i
< NCH(n
); i
+= 2) {
2374 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2377 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2381 modname
= mod
->name
;
2383 modname
= new_identifier("", c
->c_arena
);
2384 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2387 PyErr_Format(PyExc_SystemError
,
2388 "unknown import statement: starts with command '%s'",
2394 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2396 /* global_stmt: 'global' NAME (',' NAME)* */
2401 REQ(n
, global_stmt
);
2402 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2405 for (i
= 1; i
< NCH(n
); i
+= 2) {
2406 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2409 asdl_seq_SET(s
, i
/ 2, name
);
2411 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2415 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2417 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2418 int n_children
= NCH(n
);
2419 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2420 PyErr_Format(PyExc_SystemError
,
2421 "poorly formed 'exec' statement: %d parts to statement",
2426 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2428 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2431 if (n_children
>= 4) {
2432 globals
= ast_for_expr(c
, CHILD(n
, 3));
2436 if (n_children
== 6) {
2437 locals
= ast_for_expr(c
, CHILD(n
, 5));
2442 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2446 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2448 /* assert_stmt: 'assert' test [',' test] */
2449 REQ(n
, assert_stmt
);
2451 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2454 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2456 else if (NCH(n
) == 4) {
2457 expr_ty expr1
, expr2
;
2459 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2462 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2466 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2468 PyErr_Format(PyExc_SystemError
,
2469 "improper number of parts to 'assert' statement: %d",
2475 ast_for_suite(struct compiling
*c
, const node
*n
)
2477 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2480 int i
, total
, num
, end
, pos
= 0;
2485 total
= num_stmts(n
);
2486 seq
= asdl_seq_new(total
, c
->c_arena
);
2489 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2491 /* simple_stmt always ends with a NEWLINE,
2492 and may have a trailing SEMI
2495 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2497 /* loop by 2 to skip semi-colons */
2498 for (i
= 0; i
< end
; i
+= 2) {
2500 s
= ast_for_stmt(c
, ch
);
2503 asdl_seq_SET(seq
, pos
++, s
);
2507 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2510 num
= num_stmts(ch
);
2512 /* small_stmt or compound_stmt with only one child */
2513 s
= ast_for_stmt(c
, ch
);
2516 asdl_seq_SET(seq
, pos
++, s
);
2521 REQ(ch
, simple_stmt
);
2522 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2523 /* statement terminates with a semi-colon ';' */
2524 if (NCH(CHILD(ch
, j
)) == 0) {
2525 assert((j
+ 1) == NCH(ch
));
2528 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2531 asdl_seq_SET(seq
, pos
++, s
);
2536 assert(pos
== seq
->size
);
2541 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2543 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2552 asdl_seq
*suite_seq
;
2554 expression
= ast_for_expr(c
, CHILD(n
, 1));
2557 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2561 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2564 s
= STR(CHILD(n
, 4));
2565 /* s[2], the third character in the string, will be
2571 asdl_seq
*seq1
, *seq2
;
2573 expression
= ast_for_expr(c
, CHILD(n
, 1));
2576 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2579 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2583 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2585 else if (s
[2] == 'i') {
2586 int i
, n_elif
, has_else
= 0;
2587 asdl_seq
*orelse
= NULL
;
2588 n_elif
= NCH(n
) - 4;
2589 /* must reference the child n_elif+1 since 'else' token is third,
2590 not fourth, child from the end. */
2591 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2592 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2600 asdl_seq
*seq1
, *seq2
;
2602 orelse
= asdl_seq_new(1, c
->c_arena
);
2605 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2608 seq1
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2611 seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2615 asdl_seq_SET(orelse
, 0, If(expression
, seq1
, seq2
,
2616 LINENO(CHILD(n
, NCH(n
) - 6)), CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2618 /* the just-created orelse handled the last elif */
2622 for (i
= 0; i
< n_elif
; i
++) {
2623 int off
= 5 + (n_elif
- i
- 1) * 4;
2625 asdl_seq
*suite_seq
;
2626 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2629 expression
= ast_for_expr(c
, CHILD(n
, off
));
2632 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2636 asdl_seq_SET(newobj
, 0,
2637 If(expression
, suite_seq
, orelse
,
2638 LINENO(CHILD(n
, off
)), CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2641 return If(ast_for_expr(c
, CHILD(n
, 1)),
2642 ast_for_suite(c
, CHILD(n
, 3)),
2643 orelse
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2646 PyErr_Format(PyExc_SystemError
,
2647 "unexpected token in 'if' statement: %s", s
);
2652 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2654 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2659 asdl_seq
*suite_seq
;
2661 expression
= ast_for_expr(c
, CHILD(n
, 1));
2664 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2667 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2669 else if (NCH(n
) == 7) {
2671 asdl_seq
*seq1
, *seq2
;
2673 expression
= ast_for_expr(c
, CHILD(n
, 1));
2676 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2679 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2683 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2686 PyErr_Format(PyExc_SystemError
,
2687 "wrong number of tokens for 'while' statement: %d",
2693 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2695 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2698 const node
*node_target
;
2699 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2703 seq
= ast_for_suite(c
, CHILD(n
, 8));
2708 node_target
= CHILD(n
, 1);
2709 _target
= ast_for_exprlist(c
, node_target
, Store
);
2712 /* Check the # of children rather than the length of _target, since
2713 for x, in ... has 1 element in _target, but still requires a Tuple. */
2714 if (NCH(node_target
) == 1)
2715 target
= (expr_ty
)asdl_seq_GET(_target
, 0);
2717 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2719 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2722 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2726 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2730 static excepthandler_ty
2731 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2733 /* except_clause: 'except' [test [',' test]] */
2734 REQ(exc
, except_clause
);
2737 if (NCH(exc
) == 1) {
2738 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2742 return excepthandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2743 exc
->n_col_offset
, c
->c_arena
);
2745 else if (NCH(exc
) == 2) {
2747 asdl_seq
*suite_seq
;
2749 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2752 suite_seq
= ast_for_suite(c
, body
);
2756 return excepthandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2757 exc
->n_col_offset
, c
->c_arena
);
2759 else if (NCH(exc
) == 4) {
2760 asdl_seq
*suite_seq
;
2762 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2765 if (!set_context(e
, Store
, CHILD(exc
, 3)))
2767 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2770 suite_seq
= ast_for_suite(c
, body
);
2774 return excepthandler(expression
, e
, suite_seq
, LINENO(exc
),
2775 exc
->n_col_offset
, c
->c_arena
);
2778 PyErr_Format(PyExc_SystemError
,
2779 "wrong number of children for 'except' clause: %d",
2785 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2787 const int nch
= NCH(n
);
2788 int n_except
= (nch
- 3)/3;
2789 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2793 body
= ast_for_suite(c
, CHILD(n
, 2));
2797 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2798 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2799 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2800 /* we can assume it's an "else",
2801 because nch >= 9 for try-else-finally and
2802 it would otherwise have a type of except_clause */
2803 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2809 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2810 if (finally
== NULL
)
2815 /* we can assume it's an "else",
2816 otherwise it would have a type of except_clause */
2817 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2823 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2824 ast_error(n
, "malformed 'try' statement");
2831 /* process except statements to create a try ... except */
2832 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2833 if (handlers
== NULL
)
2836 for (i
= 0; i
< n_except
; i
++) {
2837 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2838 CHILD(n
, 5 + i
* 3));
2841 asdl_seq_SET(handlers
, i
, e
);
2844 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
2845 n
->n_col_offset
, c
->c_arena
);
2849 /* if a 'finally' is present too, we nest the TryExcept within a
2850 TryFinally to emulate try ... except ... finally */
2851 body
= asdl_seq_new(1, c
->c_arena
);
2854 asdl_seq_SET(body
, 0, except_st
);
2857 /* must be a try ... finally (except clauses are in body, if any exist) */
2858 assert(finally
!= NULL
);
2859 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2863 ast_for_with_var(struct compiling
*c
, const node
*n
)
2866 return ast_for_expr(c
, CHILD(n
, 1));
2869 /* with_stmt: 'with' test [ with_var ] ':' suite */
2871 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2873 expr_ty context_expr
, optional_vars
= NULL
;
2874 int suite_index
= 3; /* skip 'with', test, and ':' */
2875 asdl_seq
*suite_seq
;
2877 assert(TYPE(n
) == with_stmt
);
2878 context_expr
= ast_for_expr(c
, CHILD(n
, 1));
2879 if (TYPE(CHILD(n
, 2)) == with_var
) {
2880 optional_vars
= ast_for_with_var(c
, CHILD(n
, 2));
2882 if (!optional_vars
) {
2885 if (!set_context(optional_vars
, Store
, n
)) {
2891 suite_seq
= ast_for_suite(c
, CHILD(n
, suite_index
));
2895 return With(context_expr
, optional_vars
, suite_seq
, LINENO(n
),
2896 n
->n_col_offset
, c
->c_arena
);
2900 ast_for_classdef(struct compiling
*c
, const node
*n
)
2902 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2903 asdl_seq
*bases
, *s
;
2907 if (!strcmp(STR(CHILD(n
, 1)), "None")) {
2908 ast_error(n
, "assignment to None");
2913 s
= ast_for_suite(c
, CHILD(n
, 3));
2916 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2917 n
->n_col_offset
, c
->c_arena
);
2919 /* check for empty base list */
2920 if (TYPE(CHILD(n
,3)) == RPAR
) {
2921 s
= ast_for_suite(c
, CHILD(n
,5));
2924 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2925 n
->n_col_offset
, c
->c_arena
);
2928 /* else handle the base class list */
2929 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
2933 s
= ast_for_suite(c
, CHILD(n
, 6));
2936 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, LINENO(n
),
2937 n
->n_col_offset
, c
->c_arena
);
2941 ast_for_stmt(struct compiling
*c
, const node
*n
)
2943 if (TYPE(n
) == stmt
) {
2944 assert(NCH(n
) == 1);
2947 if (TYPE(n
) == simple_stmt
) {
2948 assert(num_stmts(n
) == 1);
2951 if (TYPE(n
) == small_stmt
) {
2954 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2955 | flow_stmt | import_stmt | global_stmt | exec_stmt
2960 return ast_for_expr_stmt(c
, n
);
2962 return ast_for_print_stmt(c
, n
);
2964 return ast_for_del_stmt(c
, n
);
2966 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2968 return ast_for_flow_stmt(c
, n
);
2970 return ast_for_import_stmt(c
, n
);
2972 return ast_for_global_stmt(c
, n
);
2974 return ast_for_exec_stmt(c
, n
);
2976 return ast_for_assert_stmt(c
, n
);
2978 PyErr_Format(PyExc_SystemError
,
2979 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2985 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2986 | funcdef | classdef
2988 node
*ch
= CHILD(n
, 0);
2989 REQ(n
, compound_stmt
);
2992 return ast_for_if_stmt(c
, ch
);
2994 return ast_for_while_stmt(c
, ch
);
2996 return ast_for_for_stmt(c
, ch
);
2998 return ast_for_try_stmt(c
, ch
);
3000 return ast_for_with_stmt(c
, ch
);
3002 return ast_for_funcdef(c
, ch
);
3004 return ast_for_classdef(c
, ch
);
3006 PyErr_Format(PyExc_SystemError
,
3007 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3015 parsenumber(const char *s
)
3020 #ifndef WITHOUT_COMPLEX
3026 end
= s
+ strlen(s
) - 1;
3027 #ifndef WITHOUT_COMPLEX
3028 imflag
= *end
== 'j' || *end
== 'J';
3030 if (*end
== 'l' || *end
== 'L')
3031 return PyLong_FromString((char *)s
, (char **)0, 0);
3033 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
3034 if (x
< 0 && errno
== 0) {
3035 return PyLong_FromString((char *)s
,
3041 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3044 return PyLong_FromString((char *)s
, (char **)0, 0);
3045 return PyInt_FromLong(x
);
3047 /* XXX Huge floats may silently fail */
3048 #ifndef WITHOUT_COMPLEX
3051 PyFPE_START_PROTECT("atof", return 0)
3052 c
.imag
= PyOS_ascii_atof(s
);
3053 PyFPE_END_PROTECT(c
)
3054 return PyComplex_FromCComplex(c
);
3059 PyFPE_START_PROTECT("atof", return 0)
3060 dx
= PyOS_ascii_atof(s
);
3061 PyFPE_END_PROTECT(dx
)
3062 return PyFloat_FromDouble(dx
);
3067 decode_utf8(const char **sPtr
, const char *end
, char* encoding
)
3069 #ifndef Py_USING_UNICODE
3070 Py_FatalError("decode_utf8 should not be called in this build.");
3075 t
= s
= (char *)*sPtr
;
3076 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3077 while (s
< end
&& (*s
& 0x80)) s
++;
3079 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3082 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3089 decode_unicode(const char *s
, size_t len
, int rawmode
, const char *encoding
)
3095 if (encoding
== NULL
) {
3098 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3102 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3103 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
3106 p
= buf
= PyString_AsString(u
);
3116 if (*s
& 0x80) { /* XXX inefficient */
3120 w
= decode_utf8(&s
, end
, "utf-16-be");
3125 r
= PyString_AsString(w
);
3126 rn
= PyString_Size(w
);
3127 assert(rn
% 2 == 0);
3128 for (i
= 0; i
< rn
; i
+= 2) {
3129 sprintf(p
, "\\u%02x%02x",
3143 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3145 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3150 /* s is a Python string literal, including the bracketing quote characters,
3151 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3152 * parsestr parses it, and returns the decoded Python string object.
3155 parsestr(const char *s
, const char *encoding
)
3158 int quote
= Py_CHARMASK(*s
);
3163 if (isalpha(quote
) || quote
== '_') {
3164 if (quote
== 'u' || quote
== 'U') {
3168 if (quote
== 'r' || quote
== 'R') {
3173 if (quote
!= '\'' && quote
!= '\"') {
3174 PyErr_BadInternalCall();
3179 if (len
> INT_MAX
) {
3180 PyErr_SetString(PyExc_OverflowError
,
3181 "string to parse is too long");
3184 if (s
[--len
] != quote
) {
3185 PyErr_BadInternalCall();
3188 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3191 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3192 PyErr_BadInternalCall();
3196 #ifdef Py_USING_UNICODE
3197 if (unicode
|| Py_UnicodeFlag
) {
3198 return decode_unicode(s
, len
, rawmode
, encoding
);
3201 need_encoding
= (encoding
!= NULL
&&
3202 strcmp(encoding
, "utf-8") != 0 &&
3203 strcmp(encoding
, "iso-8859-1") != 0);
3204 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3205 if (need_encoding
) {
3206 #ifndef Py_USING_UNICODE
3207 /* This should not happen - we never see any other
3210 "cannot deal with encodings in this build.");
3212 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3215 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3220 return PyString_FromStringAndSize(s
, len
);
3224 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3225 need_encoding
? encoding
: NULL
);
3228 /* Build a Python string object out of a STRING atom. This takes care of
3229 * compile-time literal catenation, calling parsestr() on each piece, and
3230 * pasting the intermediate results together.
3233 parsestrplus(struct compiling
*c
, const node
*n
)
3237 REQ(CHILD(n
, 0), STRING
);
3238 if ((v
= parsestr(STR(CHILD(n
, 0)), c
->c_encoding
)) != NULL
) {
3239 /* String literal concatenation */
3240 for (i
= 1; i
< NCH(n
); i
++) {
3242 s
= parsestr(STR(CHILD(n
, i
)), c
->c_encoding
);
3245 if (PyString_Check(v
) && PyString_Check(s
)) {
3246 PyString_ConcatAndDel(&v
, s
);
3250 #ifdef Py_USING_UNICODE
3252 PyObject
*temp
= PyUnicode_Concat(v
, s
);