2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
7 #include "Python-ast.h"
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
24 /* Data structure used internally */
26 char *c_encoding
; /* source encoding */
27 PyArena
*c_arena
; /* arena for allocating memeory */
30 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
31 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
32 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
33 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
34 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*, expr_context_ty
);
35 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
36 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
38 /* Note different signature for ast_for_call */
39 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
41 static PyObject
*parsenumber(const char *);
42 static PyObject
*parsestr(const char *s
, const char *encoding
);
43 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
46 #define LINENO(n) ((n)->n_lineno)
50 new_identifier(const char* n
, PyArena
*arena
) {
51 PyObject
* id
= PyString_InternFromString(n
);
52 PyArena_AddPyObject(arena
, id
);
56 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
58 /* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
63 XXX Maybe we should just pass the filename...
67 ast_error(const node
*n
, const char *errstr
)
69 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
72 PyErr_SetObject(PyExc_SyntaxError
, u
);
78 ast_error_finish(const char *filename
)
80 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
87 PyErr_Fetch(&type
, &value
, &tback
);
88 errstr
= PyTuple_GetItem(value
, 0);
92 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
99 loc
= PyErr_ProgramText(filename
, lineno
);
104 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
110 value
= PyTuple_Pack(2, errstr
, tmp
);
115 PyErr_Restore(type
, value
, tback
);
118 /* num_stmts() returns number of contained statements.
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
133 num_stmts(const node
*n
)
140 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
143 return num_stmts(CHILD(n
, 0));
146 for (i
= 0; i
< NCH(n
); i
++) {
148 if (TYPE(ch
) == stmt
)
153 return num_stmts(CHILD(n
, 0));
157 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
160 return num_stmts(CHILD(n
, 0));
163 for (i
= 2; i
< (NCH(n
) - 1); i
++)
164 l
+= num_stmts(CHILD(n
, i
));
170 sprintf(buf
, "Non-statement found: %d %d\n",
179 /* Transform the CST rooted at node * to the appropriate AST
183 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
187 asdl_seq
*stmts
= NULL
;
192 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
193 c
.c_encoding
= "utf-8";
194 if (TYPE(n
) == encoding_decl
) {
195 ast_error(n
, "encoding declaration in Unicode string");
198 } else if (TYPE(n
) == encoding_decl
) {
199 c
.c_encoding
= STR(n
);
209 stmts
= asdl_seq_new(num_stmts(n
), arena
);
212 for (i
= 0; i
< NCH(n
) - 1; i
++) {
214 if (TYPE(ch
) == NEWLINE
)
219 s
= ast_for_stmt(&c
, ch
);
222 asdl_seq_SET(stmts
, k
++, s
);
226 REQ(ch
, simple_stmt
);
227 for (j
= 0; j
< num
; j
++) {
228 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
231 asdl_seq_SET(stmts
, k
++, s
);
235 return Module(stmts
, arena
);
237 expr_ty testlist_ast
;
239 /* XXX Why not gen_for here? */
240 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
243 return Expression(testlist_ast
, arena
);
246 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
247 stmts
= asdl_seq_new(1, arena
);
250 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
252 return Interactive(stmts
, arena
);
257 stmts
= asdl_seq_new(num
, arena
);
261 s
= ast_for_stmt(&c
, n
);
264 asdl_seq_SET(stmts
, 0, s
);
267 /* Only a simple_stmt can contain multiple statements. */
269 for (i
= 0; i
< NCH(n
); i
+= 2) {
270 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
272 s
= ast_for_stmt(&c
, CHILD(n
, i
));
275 asdl_seq_SET(stmts
, i
/ 2, s
);
279 return Interactive(stmts
, arena
);
285 ast_error_finish(filename
);
289 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
293 get_operator(const node
*n
)
319 return (operator_ty
)0;
323 /* Set the context ctx for expr_ty e, recursively traversing e.
325 Only sets context for expr kinds that "can appear in assignment context"
326 (according to ../Parser/Python.asdl). For other expr kinds, it sets
327 an appropriate syntax error and returns false.
331 set_context(expr_ty e
, expr_context_ty ctx
, const node
*n
)
334 /* If a particular expression type can't be used for assign / delete,
335 set expr_name to its name and an error message will be generated.
337 const char* expr_name
= NULL
;
339 /* The ast defines augmented store and load contexts, but the
340 implementation here doesn't actually use them. The code may be
341 a little more complex than necessary as a result. It also means
342 that expressions in an augmented assignment have no context.
343 Consider restructuring so that augmented assignment uses
346 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
351 !strcmp(PyString_AS_STRING(e
->v
.Attribute
.attr
), "None")) {
352 return ast_error(n
, "assignment to None");
354 e
->v
.Attribute
.ctx
= ctx
;
357 e
->v
.Subscript
.ctx
= ctx
;
361 !strcmp(PyString_AS_STRING(e
->v
.Name
.id
), "None")) {
362 return ast_error(n
, "assignment to None");
371 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
372 return ast_error(n
, "can't assign to ()");
373 e
->v
.Tuple
.ctx
= ctx
;
377 expr_name
= "lambda";
380 expr_name
= "function call";
385 expr_name
= "operator";
387 case GeneratorExp_kind
:
388 expr_name
= "generator expression";
391 expr_name
= "list comprehension";
396 expr_name
= "literal";
399 expr_name
= "comparison";
405 expr_name
= "conditional expression";
408 PyErr_Format(PyExc_SystemError
,
409 "unexpected expression in assignment %d (line %d)",
413 /* Check for error string set by switch */
416 PyOS_snprintf(buf
, sizeof(buf
),
418 ctx
== Store
? "assign to" : "delete",
420 return ast_error(n
, buf
);
423 /* If the LHS is a list or tuple, we need to set the assignment
424 context for all the contained elements.
429 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
430 if (!set_context((expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
438 ast_for_augassign(const node
*n
)
448 if (STR(n
)[1] == '/')
465 if (STR(n
)[1] == '*')
470 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
471 return (operator_ty
)0;
476 ast_for_comp_op(const node
*n
)
478 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
489 case EQEQUAL
: /* == */
498 if (strcmp(STR(n
), "in") == 0)
500 if (strcmp(STR(n
), "is") == 0)
503 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
508 else if (NCH(n
) == 2) {
509 /* handle "not in" and "is not" */
510 switch (TYPE(CHILD(n
, 0))) {
512 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
514 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
517 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
518 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
522 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
528 seq_for_testlist(struct compiling
*c
, const node
*n
)
530 /* testlist: test (',' test)* [','] */
534 assert(TYPE(n
) == testlist
535 || TYPE(n
) == listmaker
536 || TYPE(n
) == testlist_gexp
537 || TYPE(n
) == testlist_safe
540 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
544 for (i
= 0; i
< NCH(n
); i
+= 2) {
545 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
547 expression
= ast_for_expr(c
, CHILD(n
, i
));
551 assert(i
/ 2 < seq
->size
);
552 asdl_seq_SET(seq
, i
/ 2, expression
);
558 compiler_complex_args(struct compiling
*c
, const node
*n
)
560 int i
, len
= (NCH(n
) + 1) / 2;
562 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
567 for (i
= 0; i
< len
; i
++) {
568 const node
*child
= CHILD(CHILD(n
, 2*i
), 0);
570 if (TYPE(child
) == NAME
) {
571 if (!strcmp(STR(child
), "None")) {
572 ast_error(child
, "assignment to None");
575 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
),
576 child
->n_col_offset
, c
->c_arena
);
579 arg
= compiler_complex_args(c
, CHILD(CHILD(n
, 2*i
), 1));
581 asdl_seq_SET(args
, i
, arg
);
584 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
585 if (!set_context(result
, Store
, n
))
591 /* Create AST for argument list. */
594 ast_for_arguments(struct compiling
*c
, const node
*n
)
596 /* parameters: '(' [varargslist] ')'
597 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
598 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
600 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
601 asdl_seq
*args
, *defaults
;
602 identifier vararg
= NULL
, kwarg
= NULL
;
605 if (TYPE(n
) == parameters
) {
606 if (NCH(n
) == 2) /* () as argument list */
607 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
612 /* first count the number of normal args & defaults */
613 for (i
= 0; i
< NCH(n
); i
++) {
615 if (TYPE(ch
) == fpdef
)
617 if (TYPE(ch
) == EQUAL
)
620 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
622 return NULL
; /* Don't need to go to NULL; nothing allocated */
623 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
624 if (!defaults
&& n_defaults
)
627 /* fpdef: NAME | '(' fplist ')'
628 fplist: fpdef (',' fpdef)* [',']
631 j
= 0; /* index for defaults */
632 k
= 0; /* index for args */
637 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
638 anything other than EQUAL or a comma? */
639 /* XXX Should NCH(n) check be made a separate check? */
640 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
641 asdl_seq_SET(defaults
, j
++,
642 ast_for_expr(c
, CHILD(n
, i
+ 2)));
646 else if (found_default
) {
648 "non-default argument follows default argument");
653 /* def foo((x)): is not complex, special case. */
655 /* We have complex arguments, setup for unpacking. */
656 asdl_seq_SET(args
, k
++, compiler_complex_args(c
, ch
));
658 /* def foo((x)): setup for checking NAME below. */
662 if (TYPE(CHILD(ch
, 0)) == NAME
) {
664 if (!strcmp(STR(CHILD(ch
, 0)), "None")) {
665 ast_error(CHILD(ch
, 0), "assignment to None");
668 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
669 Param
, LINENO(ch
), ch
->n_col_offset
,
673 asdl_seq_SET(args
, k
++, name
);
676 i
+= 2; /* the name and the comma */
679 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
680 ast_error(CHILD(n
, i
+1), "assignment to None");
683 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
687 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
688 ast_error(CHILD(n
, i
+1), "assignment to None");
691 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
695 PyErr_Format(PyExc_SystemError
,
696 "unexpected node in varargslist: %d @ %d",
702 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
711 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
715 int lineno
, col_offset
;
721 col_offset
= n
->n_col_offset
;
723 id
= NEW_IDENTIFIER(CHILD(n
, 0));
726 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
730 for (i
= 2; i
< NCH(n
); i
+=2) {
731 id
= NEW_IDENTIFIER(CHILD(n
, i
));
734 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
743 ast_for_decorator(struct compiling
*c
, const node
*n
)
745 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
750 REQ(CHILD(n
, 0), AT
);
751 REQ(RCHILD(n
, -1), NEWLINE
);
753 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
757 if (NCH(n
) == 3) { /* No arguments */
761 else if (NCH(n
) == 5) { /* Call with no arguments */
762 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
763 n
->n_col_offset
, c
->c_arena
);
769 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
779 ast_for_decorators(struct compiling
*c
, const node
*n
)
781 asdl_seq
* decorator_seq
;
786 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
790 for (i
= 0; i
< NCH(n
); i
++) {
791 d
= ast_for_decorator(c
, CHILD(n
, i
));
794 asdl_seq_SET(decorator_seq
, i
, d
);
796 return decorator_seq
;
800 ast_for_funcdef(struct compiling
*c
, const node
*n
)
802 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
806 asdl_seq
*decorator_seq
= NULL
;
811 if (NCH(n
) == 6) { /* decorators are present */
812 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
821 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
824 else if (!strcmp(STR(CHILD(n
, name_i
)), "None")) {
825 ast_error(CHILD(n
, name_i
), "assignment to None");
828 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
831 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
835 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
),
836 n
->n_col_offset
, c
->c_arena
);
840 ast_for_lambdef(struct compiling
*c
, const node
*n
)
842 /* lambdef: 'lambda' [varargslist] ':' test */
847 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
850 expression
= ast_for_expr(c
, CHILD(n
, 2));
855 args
= ast_for_arguments(c
, CHILD(n
, 1));
858 expression
= ast_for_expr(c
, CHILD(n
, 3));
863 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
867 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
869 /* test: or_test 'if' or_test 'else' test */
870 expr_ty expression
, body
, orelse
;
873 body
= ast_for_expr(c
, CHILD(n
, 0));
876 expression
= ast_for_expr(c
, CHILD(n
, 2));
879 orelse
= ast_for_expr(c
, CHILD(n
, 4));
882 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
886 /* Count the number of 'for' loop in a list comprehension.
888 Helper for ast_for_listcomp().
892 count_list_fors(const node
*n
)
895 node
*ch
= CHILD(n
, 1);
907 if (TYPE(ch
) == list_for
)
909 else if (TYPE(ch
) == list_if
) {
912 goto count_list_iter
;
918 /* Should never be reached */
919 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
923 /* Count the number of 'if' statements in a list comprehension.
925 Helper for ast_for_listcomp().
929 count_list_ifs(const node
*n
)
935 if (TYPE(CHILD(n
, 0)) == list_for
)
943 goto count_list_iter
;
947 ast_for_listcomp(struct compiling
*c
, const node
*n
)
949 /* listmaker: test ( list_for | (',' test)* [','] )
950 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
951 list_iter: list_for | list_if
952 list_if: 'if' test [list_iter]
953 testlist_safe: test [(',' test)+ [',']]
963 elt
= ast_for_expr(c
, CHILD(n
, 0));
967 n_fors
= count_list_fors(n
);
971 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
976 for (i
= 0; i
< n_fors
; i
++) {
983 t
= ast_for_exprlist(c
, CHILD(ch
, 1), Store
);
986 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
990 if (asdl_seq_LEN(t
) == 1)
991 lc
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
, NULL
,
994 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
996 expression
, NULL
, c
->c_arena
);
1005 n_ifs
= count_list_ifs(ch
);
1009 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1013 for (j
= 0; j
< n_ifs
; j
++) {
1018 asdl_seq_SET(ifs
, j
, ast_for_expr(c
, CHILD(ch
, 1)));
1022 /* on exit, must guarantee that ch is a list_for */
1023 if (TYPE(ch
) == list_iter
)
1027 asdl_seq_SET(listcomps
, i
, lc
);
1030 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1034 Count the number of 'for' loops in a generator expression.
1036 Helper for ast_for_genexp().
1040 count_gen_fors(const node
*n
)
1043 node
*ch
= CHILD(n
, 1);
1055 if (TYPE(ch
) == gen_for
)
1057 else if (TYPE(ch
) == gen_if
) {
1060 goto count_gen_iter
;
1066 /* Should never be reached */
1067 PyErr_SetString(PyExc_SystemError
,
1068 "logic error in count_gen_fors");
1072 /* Count the number of 'if' statements in a generator expression.
1074 Helper for ast_for_genexp().
1078 count_gen_ifs(const node
*n
)
1084 if (TYPE(CHILD(n
, 0)) == gen_for
)
1095 /* TODO(jhylton): Combine with list comprehension code? */
1097 ast_for_genexp(struct compiling
*c
, const node
*n
)
1099 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1100 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1106 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1109 elt
= ast_for_expr(c
, CHILD(n
, 0));
1113 n_fors
= count_gen_fors(n
);
1117 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1122 for (i
= 0; i
< n_fors
; i
++) {
1123 comprehension_ty ge
;
1129 t
= ast_for_exprlist(c
, CHILD(ch
, 1), Store
);
1132 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1136 if (asdl_seq_LEN(t
) == 1)
1137 ge
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
,
1140 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
,
1142 expression
, NULL
, c
->c_arena
);
1152 n_ifs
= count_gen_ifs(ch
);
1156 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1160 for (j
= 0; j
< n_ifs
; j
++) {
1165 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1168 asdl_seq_SET(ifs
, j
, expression
);
1172 /* on exit, must guarantee that ch is a gen_for */
1173 if (TYPE(ch
) == gen_iter
)
1177 asdl_seq_SET(genexps
, i
, ge
);
1180 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1184 ast_for_atom(struct compiling
*c
, const node
*n
)
1186 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1187 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1189 node
*ch
= CHILD(n
, 0);
1193 /* All names start in Load context, but may later be
1195 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1197 PyObject
*str
= parsestrplus(c
, n
);
1201 PyArena_AddPyObject(c
->c_arena
, str
);
1202 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1205 PyObject
*pynum
= parsenumber(STR(ch
));
1209 PyArena_AddPyObject(c
->c_arena
, pynum
);
1210 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1212 case LPAR
: /* some parenthesized expressions */
1215 if (TYPE(ch
) == RPAR
)
1216 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1218 if (TYPE(ch
) == yield_expr
)
1219 return ast_for_expr(c
, ch
);
1221 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1222 return ast_for_genexp(c
, ch
);
1224 return ast_for_testlist_gexp(c
, ch
);
1225 case LSQB
: /* list (or list comprehension) */
1228 if (TYPE(ch
) == RSQB
)
1229 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1232 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1233 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1237 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1240 return ast_for_listcomp(c
, ch
);
1242 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1244 asdl_seq
*keys
, *values
;
1247 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1248 keys
= asdl_seq_new(size
, c
->c_arena
);
1252 values
= asdl_seq_new(size
, c
->c_arena
);
1256 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1259 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1263 asdl_seq_SET(keys
, i
/ 4, expression
);
1265 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1269 asdl_seq_SET(values
, i
/ 4, expression
);
1271 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1273 case BACKQUOTE
: { /* repr */
1274 expr_ty expression
= ast_for_testlist(c
, CHILD(n
, 1));
1278 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1281 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1287 ast_for_slice(struct compiling
*c
, const node
*n
)
1290 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1295 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1299 if (TYPE(ch
) == DOT
)
1300 return Ellipsis(c
->c_arena
);
1302 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1303 /* 'step' variable hold no significance in terms of being used over
1305 step
= ast_for_expr(c
, ch
);
1309 return Index(step
, c
->c_arena
);
1312 if (TYPE(ch
) == test
) {
1313 lower
= ast_for_expr(c
, ch
);
1318 /* If there's an upper bound it's in the second or third position. */
1319 if (TYPE(ch
) == COLON
) {
1321 node
*n2
= CHILD(n
, 1);
1323 if (TYPE(n2
) == test
) {
1324 upper
= ast_for_expr(c
, n2
);
1329 } else if (NCH(n
) > 2) {
1330 node
*n2
= CHILD(n
, 2);
1332 if (TYPE(n2
) == test
) {
1333 upper
= ast_for_expr(c
, n2
);
1339 ch
= CHILD(n
, NCH(n
) - 1);
1340 if (TYPE(ch
) == sliceop
) {
1342 /* No expression, so step is None */
1344 step
= Name(new_identifier("None", c
->c_arena
), Load
,
1345 LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1350 if (TYPE(ch
) == test
) {
1351 step
= ast_for_expr(c
, ch
);
1358 return Slice(lower
, upper
, step
, c
->c_arena
);
1362 ast_for_binop(struct compiling
*c
, const node
*n
)
1364 /* Must account for a sequence of expressions.
1365 How should A op B op C by represented?
1366 BinOp(BinOp(A, op, B), op, C).
1370 expr_ty expr1
, expr2
, result
;
1371 operator_ty newoperator
;
1373 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1377 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1381 newoperator
= get_operator(CHILD(n
, 1));
1385 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1390 nops
= (NCH(n
) - 1) / 2;
1391 for (i
= 1; i
< nops
; i
++) {
1392 expr_ty tmp_result
, tmp
;
1393 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1395 newoperator
= get_operator(next_oper
);
1399 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1403 tmp_result
= BinOp(result
, newoperator
, tmp
,
1404 LINENO(next_oper
), next_oper
->n_col_offset
,
1408 result
= tmp_result
;
1414 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1416 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1417 subscriptlist: subscript (',' subscript)* [',']
1418 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1421 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1423 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1424 n
->n_col_offset
, c
->c_arena
);
1426 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1428 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1429 return Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
,
1430 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1433 REQ(CHILD(n
, 0), LSQB
);
1434 REQ(CHILD(n
, 2), RSQB
);
1437 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1440 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1444 /* The grammar is ambiguous here. The ambiguity is resolved
1445 by treating the sequence as a tuple literal if there are
1452 asdl_seq
*slices
, *elts
;
1453 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1456 for (j
= 0; j
< NCH(n
); j
+= 2) {
1457 slc
= ast_for_slice(c
, CHILD(n
, j
));
1460 if (slc
->kind
!= Index_kind
)
1462 asdl_seq_SET(slices
, j
/ 2, slc
);
1465 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1466 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1468 /* extract Index values and put them in a Tuple */
1469 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1472 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1473 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1474 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1475 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1477 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1480 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1481 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1487 ast_for_power(struct compiling
*c
, const node
*n
)
1489 /* power: atom trailer* ('**' factor)*
1494 e
= ast_for_atom(c
, CHILD(n
, 0));
1499 for (i
= 1; i
< NCH(n
); i
++) {
1500 node
*ch
= CHILD(n
, i
);
1501 if (TYPE(ch
) != trailer
)
1503 tmp
= ast_for_trailer(c
, ch
, e
);
1506 tmp
->lineno
= e
->lineno
;
1507 tmp
->col_offset
= e
->col_offset
;
1510 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1511 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1514 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1522 /* Do not name a variable 'expr'! Will cause a compile error.
1526 ast_for_expr(struct compiling
*c
, const node
*n
)
1528 /* handle the full range of simple expressions
1529 test: or_test ['if' or_test 'else' test] | lambdef
1530 or_test: and_test ('or' and_test)*
1531 and_test: not_test ('and' not_test)*
1532 not_test: 'not' not_test | comparison
1533 comparison: expr (comp_op expr)*
1534 expr: xor_expr ('|' xor_expr)*
1535 xor_expr: and_expr ('^' and_expr)*
1536 and_expr: shift_expr ('&' shift_expr)*
1537 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1538 arith_expr: term (('+'|'-') term)*
1539 term: factor (('*'|'/'|'%'|'//') factor)*
1540 factor: ('+'|'-'|'~') factor | power
1541 power: atom trailer* ('**' factor)*
1543 As well as modified versions that exist for backward compatibility,
1544 to explicitly allow:
1545 [ x for x in lambda: 0, lambda: 1 ]
1546 (which would be ambiguous without these extra rules)
1548 old_test: or_test | old_lambdef
1549 old_lambdef: 'lambda' [vararglist] ':' old_test
1560 if (TYPE(CHILD(n
, 0)) == lambdef
||
1561 TYPE(CHILD(n
, 0)) == old_lambdef
)
1562 return ast_for_lambdef(c
, CHILD(n
, 0));
1563 else if (NCH(n
) > 1)
1564 return ast_for_ifexpr(c
, n
);
1572 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1575 for (i
= 0; i
< NCH(n
); i
+= 2) {
1576 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1579 asdl_seq_SET(seq
, i
/ 2, e
);
1581 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1582 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1584 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1585 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1592 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1596 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1608 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1611 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1615 for (i
= 1; i
< NCH(n
); i
+= 2) {
1616 cmpop_ty newoperator
;
1618 newoperator
= ast_for_comp_op(CHILD(n
, i
));
1623 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1628 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1629 asdl_seq_SET(cmps
, i
/ 2, expression
);
1631 expression
= ast_for_expr(c
, CHILD(n
, 0));
1636 return Compare(expression
, ops
, cmps
, LINENO(n
),
1637 n
->n_col_offset
, c
->c_arena
);
1641 /* The next five cases all handle BinOps. The main body of code
1642 is the same in each case, but the switch turned inside out to
1643 reuse the code for each type of operator.
1655 return ast_for_binop(c
, n
);
1659 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1663 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1673 expression
= ast_for_expr(c
, CHILD(n
, 1));
1677 switch (TYPE(CHILD(n
, 0))) {
1679 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1681 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1683 return UnaryOp(Invert
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1685 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1690 return ast_for_power(c
, n
);
1692 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1695 /* should never get here unless if error is set */
1700 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1703 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1705 argument: [test '='] test [gen_for] # Really [keyword '='] test
1708 int i
, nargs
, nkeywords
, ngens
;
1711 expr_ty vararg
= NULL
, kwarg
= NULL
;
1718 for (i
= 0; i
< NCH(n
); i
++) {
1719 node
*ch
= CHILD(n
, i
);
1720 if (TYPE(ch
) == argument
) {
1723 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1729 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1730 ast_error(n
, "Generator expression must be parenthesized "
1731 "if not sole argument");
1735 if (nargs
+ nkeywords
+ ngens
> 255) {
1736 ast_error(n
, "more than 255 arguments");
1740 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1743 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1748 for (i
= 0; i
< NCH(n
); i
++) {
1749 node
*ch
= CHILD(n
, i
);
1750 if (TYPE(ch
) == argument
) {
1754 ast_error(CHILD(ch
, 0),
1755 "non-keyword arg after keyword arg");
1758 e
= ast_for_expr(c
, CHILD(ch
, 0));
1761 asdl_seq_SET(args
, nargs
++, e
);
1763 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1764 e
= ast_for_genexp(c
, ch
);
1767 asdl_seq_SET(args
, nargs
++, e
);
1773 /* CHILD(ch, 0) is test, but must be an identifier? */
1774 e
= ast_for_expr(c
, CHILD(ch
, 0));
1777 /* f(lambda x: x[0] = 3) ends up getting parsed with
1778 * LHS test = lambda x: x[0], and RHS test = 3.
1779 * SF bug 132313 points out that complaining about a keyword
1780 * then is very confusing.
1782 if (e
->kind
== Lambda_kind
) {
1783 ast_error(CHILD(ch
, 0), "lambda cannot contain assignment");
1785 } else if (e
->kind
!= Name_kind
) {
1786 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1790 e
= ast_for_expr(c
, CHILD(ch
, 2));
1793 kw
= keyword(key
, e
, c
->c_arena
);
1796 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1799 else if (TYPE(ch
) == STAR
) {
1800 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1803 else if (TYPE(ch
) == DOUBLESTAR
) {
1804 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1809 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
, func
->col_offset
, c
->c_arena
);
1813 ast_for_testlist(struct compiling
*c
, const node
* n
)
1815 /* testlist_gexp: test (',' test)* [','] */
1816 /* testlist: test (',' test)* [','] */
1817 /* testlist_safe: test (',' test)+ [','] */
1818 /* testlist1: test (',' test)* */
1820 if (TYPE(n
) == testlist_gexp
) {
1822 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1825 assert(TYPE(n
) == testlist
||
1826 TYPE(n
) == testlist_safe
||
1827 TYPE(n
) == testlist1
);
1830 return ast_for_expr(c
, CHILD(n
, 0));
1832 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1835 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1840 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
1842 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1843 /* argument: test [ gen_for ] */
1844 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
1845 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
1846 return ast_for_genexp(c
, n
);
1847 return ast_for_testlist(c
, n
);
1850 /* like ast_for_testlist() but returns a sequence */
1852 ast_for_class_bases(struct compiling
*c
, const node
* n
)
1854 /* testlist: test (',' test)* [','] */
1859 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
1862 base
= ast_for_expr(c
, CHILD(n
, 0));
1865 asdl_seq_SET(bases
, 0, base
);
1869 return seq_for_testlist(c
, n
);
1873 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
1876 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1877 | ('=' (yield_expr|testlist))*)
1878 testlist: test (',' test)* [',']
1879 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1880 | '<<=' | '>>=' | '**=' | '//='
1881 test: ... here starts the operator precendence dance
1885 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
1889 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1891 else if (TYPE(CHILD(n
, 1)) == augassign
) {
1892 expr_ty expr1
, expr2
;
1893 operator_ty newoperator
;
1894 node
*ch
= CHILD(n
, 0);
1896 if (TYPE(ch
) == testlist
)
1897 expr1
= ast_for_testlist(c
, ch
);
1899 expr1
= Yield(ast_for_expr(c
, CHILD(ch
, 0)), LINENO(ch
), n
->n_col_offset
,
1904 /* TODO(jhylton): Figure out why set_context() can't be used here. */
1905 switch (expr1
->kind
) {
1906 case GeneratorExp_kind
:
1907 ast_error(ch
, "augmented assignment to generator "
1908 "expression not possible");
1911 const char *var_name
= PyString_AS_STRING(expr1
->v
.Name
.id
);
1912 if (var_name
[0] == 'N' && !strcmp(var_name
, "None")) {
1913 ast_error(ch
, "assignment to None");
1918 case Attribute_kind
:
1919 case Subscript_kind
:
1922 ast_error(ch
, "illegal expression for augmented "
1928 if (TYPE(ch
) == testlist
)
1929 expr2
= ast_for_testlist(c
, ch
);
1931 expr2
= Yield(ast_for_expr(c
, ch
), LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1935 newoperator
= ast_for_augassign(CHILD(n
, 1));
1939 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1947 /* a normal assignment */
1948 REQ(CHILD(n
, 1), EQUAL
);
1949 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1952 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
1954 node
*ch
= CHILD(n
, i
);
1955 if (TYPE(ch
) == yield_expr
) {
1956 ast_error(ch
, "assignment to yield expression not possible");
1959 e
= ast_for_testlist(c
, ch
);
1961 /* set context to assign */
1965 if (!set_context(e
, Store
, CHILD(n
, i
)))
1968 asdl_seq_SET(targets
, i
/ 2, e
);
1970 value
= CHILD(n
, NCH(n
) - 1);
1971 if (TYPE(value
) == testlist
)
1972 expression
= ast_for_testlist(c
, value
);
1974 expression
= ast_for_expr(c
, value
);
1977 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1982 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
1984 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1985 | '>>' test [ (',' test)+ [','] ] )
1987 expr_ty dest
= NULL
, expression
;
1990 int i
, j
, start
= 1;
1993 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
1994 dest
= ast_for_expr(c
, CHILD(n
, 2));
1999 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
2002 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
2003 expression
= ast_for_expr(c
, CHILD(n
, i
));
2006 asdl_seq_SET(seq
, j
, expression
);
2008 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
2009 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2013 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2021 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2024 for (i
= 0; i
< NCH(n
); i
+= 2) {
2025 e
= ast_for_expr(c
, CHILD(n
, i
));
2028 asdl_seq_SET(seq
, i
/ 2, e
);
2029 if (context
&& !set_context(e
, context
, CHILD(n
, i
)))
2036 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2038 asdl_seq
*expr_list
;
2040 /* del_stmt: 'del' exprlist */
2043 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2046 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2050 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2053 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2056 continue_stmt: 'continue'
2057 return_stmt: 'return' [testlist]
2058 yield_stmt: yield_expr
2059 yield_expr: 'yield' testlist
2060 raise_stmt: 'raise' [test [',' test [',' test]]]
2068 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2070 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2071 case yield_stmt
: { /* will reduce to yield_expr */
2072 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2075 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2079 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2081 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2084 return Return(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2088 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2089 else if (NCH(ch
) == 2) {
2090 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2093 return Raise(expression
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2095 else if (NCH(ch
) == 4) {
2096 expr_ty expr1
, expr2
;
2098 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2101 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2105 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2107 else if (NCH(ch
) == 6) {
2108 expr_ty expr1
, expr2
, expr3
;
2110 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2113 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2116 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2120 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2123 PyErr_Format(PyExc_SystemError
,
2124 "unexpected flow_stmt: %d", TYPE(ch
));
2128 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2133 alias_for_import_name(struct compiling
*c
, const node
*n
)
2136 import_as_name: NAME ['as' NAME]
2137 dotted_as_name: dotted_name ['as' NAME]
2138 dotted_name: NAME ('.' NAME)*
2144 case import_as_name
:
2145 str
= (NCH(n
) == 3) ? NEW_IDENTIFIER(CHILD(n
, 2)) : NULL
;
2146 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2147 case dotted_as_name
:
2153 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2155 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2161 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2163 /* Create a string of the form "a.b.c" */
2169 for (i
= 0; i
< NCH(n
); i
+= 2)
2170 /* length of string plus one for the dot */
2171 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2172 len
--; /* the last name doesn't have a dot */
2173 str
= PyString_FromStringAndSize(NULL
, len
);
2176 s
= PyString_AS_STRING(str
);
2179 for (i
= 0; i
< NCH(n
); i
+= 2) {
2180 char *sch
= STR(CHILD(n
, i
));
2181 strcpy(s
, STR(CHILD(n
, i
)));
2187 PyString_InternInPlace(&str
);
2188 PyArena_AddPyObject(c
->c_arena
, str
);
2189 return alias(str
, NULL
, c
->c_arena
);
2193 str
= PyString_InternFromString("*");
2194 PyArena_AddPyObject(c
->c_arena
, str
);
2195 return alias(str
, NULL
, c
->c_arena
);
2197 PyErr_Format(PyExc_SystemError
,
2198 "unexpected import name: %d", TYPE(n
));
2202 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2207 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2210 import_stmt: import_name | import_from
2211 import_name: 'import' dotted_as_names
2212 import_from: 'from' ('.'* dotted_name | '.') 'import'
2213 ('*' | '(' import_as_names ')' | import_as_names)
2220 REQ(n
, import_stmt
);
2222 col_offset
= n
->n_col_offset
;
2224 if (TYPE(n
) == import_name
) {
2226 REQ(n
, dotted_as_names
);
2227 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2230 for (i
= 0; i
< NCH(n
); i
+= 2) {
2231 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2234 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2236 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2238 else if (TYPE(n
) == import_from
) {
2241 alias_ty mod
= NULL
;
2244 /* Count the number of dots (for relative imports) and check for the
2245 optional module name */
2246 for (idx
= 1; idx
< NCH(n
); idx
++) {
2247 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2248 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2251 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2256 idx
++; /* skip over the 'import' keyword */
2257 switch (TYPE(CHILD(n
, idx
))) {
2259 /* from ... import * */
2263 ast_error(n
, "'import *' not allowed with 'from .'");
2268 /* from ... import (x, y, z) */
2269 n
= CHILD(n
, idx
+ 1);
2270 n_children
= NCH(n
);
2272 case import_as_names
:
2273 /* from ... import x, y, z */
2275 n_children
= NCH(n
);
2276 if (n_children
% 2 == 0) {
2277 ast_error(n
, "trailing comma not allowed without"
2278 " surrounding parentheses");
2283 ast_error(n
, "Unexpected node-type in from-import");
2287 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2291 /* handle "from ... import *" special b/c there's no children */
2292 if (TYPE(n
) == STAR
) {
2293 alias_ty import_alias
= alias_for_import_name(c
, n
);
2296 asdl_seq_SET(aliases
, 0, import_alias
);
2299 for (i
= 0; i
< NCH(n
); i
+= 2) {
2300 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2303 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2307 modname
= mod
->name
;
2309 modname
= new_identifier("", c
->c_arena
);
2310 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2313 PyErr_Format(PyExc_SystemError
,
2314 "unknown import statement: starts with command '%s'",
2320 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2322 /* global_stmt: 'global' NAME (',' NAME)* */
2327 REQ(n
, global_stmt
);
2328 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2331 for (i
= 1; i
< NCH(n
); i
+= 2) {
2332 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2335 asdl_seq_SET(s
, i
/ 2, name
);
2337 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2341 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2343 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2344 int n_children
= NCH(n
);
2345 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2346 PyErr_Format(PyExc_SystemError
,
2347 "poorly formed 'exec' statement: %d parts to statement",
2352 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2354 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2357 if (n_children
>= 4) {
2358 globals
= ast_for_expr(c
, CHILD(n
, 3));
2362 if (n_children
== 6) {
2363 locals
= ast_for_expr(c
, CHILD(n
, 5));
2368 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2372 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2374 /* assert_stmt: 'assert' test [',' test] */
2375 REQ(n
, assert_stmt
);
2377 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2380 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2382 else if (NCH(n
) == 4) {
2383 expr_ty expr1
, expr2
;
2385 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2388 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2392 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2394 PyErr_Format(PyExc_SystemError
,
2395 "improper number of parts to 'assert' statement: %d",
2401 ast_for_suite(struct compiling
*c
, const node
*n
)
2403 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2406 int i
, total
, num
, end
, pos
= 0;
2411 total
= num_stmts(n
);
2412 seq
= asdl_seq_new(total
, c
->c_arena
);
2415 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2417 /* simple_stmt always ends with a NEWLINE,
2418 and may have a trailing SEMI
2421 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2423 /* loop by 2 to skip semi-colons */
2424 for (i
= 0; i
< end
; i
+= 2) {
2426 s
= ast_for_stmt(c
, ch
);
2429 asdl_seq_SET(seq
, pos
++, s
);
2433 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2436 num
= num_stmts(ch
);
2438 /* small_stmt or compound_stmt with only one child */
2439 s
= ast_for_stmt(c
, ch
);
2442 asdl_seq_SET(seq
, pos
++, s
);
2447 REQ(ch
, simple_stmt
);
2448 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2449 /* statement terminates with a semi-colon ';' */
2450 if (NCH(CHILD(ch
, j
)) == 0) {
2451 assert((j
+ 1) == NCH(ch
));
2454 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2457 asdl_seq_SET(seq
, pos
++, s
);
2462 assert(pos
== seq
->size
);
2467 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2469 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2478 asdl_seq
*suite_seq
;
2480 expression
= ast_for_expr(c
, CHILD(n
, 1));
2483 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2487 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2490 s
= STR(CHILD(n
, 4));
2491 /* s[2], the third character in the string, will be
2497 asdl_seq
*seq1
, *seq2
;
2499 expression
= ast_for_expr(c
, CHILD(n
, 1));
2502 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2505 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2509 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2511 else if (s
[2] == 'i') {
2512 int i
, n_elif
, has_else
= 0;
2513 asdl_seq
*orelse
= NULL
;
2514 n_elif
= NCH(n
) - 4;
2515 /* must reference the child n_elif+1 since 'else' token is third,
2516 not fourth, child from the end. */
2517 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2518 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2526 asdl_seq
*seq1
, *seq2
;
2528 orelse
= asdl_seq_new(1, c
->c_arena
);
2531 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2534 seq1
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2537 seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2541 asdl_seq_SET(orelse
, 0, If(expression
, seq1
, seq2
,
2542 LINENO(CHILD(n
, NCH(n
) - 6)), CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2544 /* the just-created orelse handled the last elif */
2548 for (i
= 0; i
< n_elif
; i
++) {
2549 int off
= 5 + (n_elif
- i
- 1) * 4;
2551 asdl_seq
*suite_seq
;
2552 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2555 expression
= ast_for_expr(c
, CHILD(n
, off
));
2558 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2562 asdl_seq_SET(newobj
, 0,
2563 If(expression
, suite_seq
, orelse
,
2564 LINENO(CHILD(n
, off
)), CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2567 return If(ast_for_expr(c
, CHILD(n
, 1)),
2568 ast_for_suite(c
, CHILD(n
, 3)),
2569 orelse
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2572 PyErr_Format(PyExc_SystemError
,
2573 "unexpected token in 'if' statement: %s", s
);
2578 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2580 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2585 asdl_seq
*suite_seq
;
2587 expression
= ast_for_expr(c
, CHILD(n
, 1));
2590 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2593 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2595 else if (NCH(n
) == 7) {
2597 asdl_seq
*seq1
, *seq2
;
2599 expression
= ast_for_expr(c
, CHILD(n
, 1));
2602 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2605 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2609 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2612 PyErr_Format(PyExc_SystemError
,
2613 "wrong number of tokens for 'while' statement: %d",
2619 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2621 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2624 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2628 seq
= ast_for_suite(c
, CHILD(n
, 8));
2633 _target
= ast_for_exprlist(c
, CHILD(n
, 1), Store
);
2636 if (asdl_seq_LEN(_target
) == 1)
2637 target
= (expr_ty
)asdl_seq_GET(_target
, 0);
2639 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2641 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2644 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2648 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2652 static excepthandler_ty
2653 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2655 /* except_clause: 'except' [test [',' test]] */
2656 REQ(exc
, except_clause
);
2659 if (NCH(exc
) == 1) {
2660 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2664 return excepthandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2665 exc
->n_col_offset
, c
->c_arena
);
2667 else if (NCH(exc
) == 2) {
2669 asdl_seq
*suite_seq
;
2671 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2674 suite_seq
= ast_for_suite(c
, body
);
2678 return excepthandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2679 exc
->n_col_offset
, c
->c_arena
);
2681 else if (NCH(exc
) == 4) {
2682 asdl_seq
*suite_seq
;
2684 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2687 if (!set_context(e
, Store
, CHILD(exc
, 3)))
2689 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2692 suite_seq
= ast_for_suite(c
, body
);
2696 return excepthandler(expression
, e
, suite_seq
, LINENO(exc
),
2697 exc
->n_col_offset
, c
->c_arena
);
2700 PyErr_Format(PyExc_SystemError
,
2701 "wrong number of children for 'except' clause: %d",
2707 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2709 const int nch
= NCH(n
);
2710 int n_except
= (nch
- 3)/3;
2711 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2715 body
= ast_for_suite(c
, CHILD(n
, 2));
2719 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2720 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2721 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2722 /* we can assume it's an "else",
2723 because nch >= 9 for try-else-finally and
2724 it would otherwise have a type of except_clause */
2725 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2731 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2732 if (finally
== NULL
)
2737 /* we can assume it's an "else",
2738 otherwise it would have a type of except_clause */
2739 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2745 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2746 ast_error(n
, "malformed 'try' statement");
2753 /* process except statements to create a try ... except */
2754 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2755 if (handlers
== NULL
)
2758 for (i
= 0; i
< n_except
; i
++) {
2759 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2760 CHILD(n
, 5 + i
* 3));
2763 asdl_seq_SET(handlers
, i
, e
);
2766 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
2767 n
->n_col_offset
, c
->c_arena
);
2771 /* if a 'finally' is present too, we nest the TryExcept within a
2772 TryFinally to emulate try ... except ... finally */
2773 body
= asdl_seq_new(1, c
->c_arena
);
2776 asdl_seq_SET(body
, 0, except_st
);
2779 /* must be a try ... finally (except clauses are in body, if any exist) */
2780 assert(finally
!= NULL
);
2781 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2785 ast_for_with_var(struct compiling
*c
, const node
*n
)
2788 if (strcmp(STR(CHILD(n
, 0)), "as") != 0) {
2789 ast_error(n
, "expected \"with [expr] as [var]\"");
2792 return ast_for_expr(c
, CHILD(n
, 1));
2795 /* with_stmt: 'with' test [ with_var ] ':' suite */
2797 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2799 expr_ty context_expr
, optional_vars
= NULL
;
2800 int suite_index
= 3; /* skip 'with', test, and ':' */
2801 asdl_seq
*suite_seq
;
2803 assert(TYPE(n
) == with_stmt
);
2804 context_expr
= ast_for_expr(c
, CHILD(n
, 1));
2805 if (TYPE(CHILD(n
, 2)) == with_var
) {
2806 optional_vars
= ast_for_with_var(c
, CHILD(n
, 2));
2808 if (!optional_vars
) {
2811 if (!set_context(optional_vars
, Store
, n
)) {
2817 suite_seq
= ast_for_suite(c
, CHILD(n
, suite_index
));
2821 return With(context_expr
, optional_vars
, suite_seq
, LINENO(n
),
2822 n
->n_col_offset
, c
->c_arena
);
2826 ast_for_classdef(struct compiling
*c
, const node
*n
)
2828 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2829 asdl_seq
*bases
, *s
;
2833 if (!strcmp(STR(CHILD(n
, 1)), "None")) {
2834 ast_error(n
, "assignment to None");
2839 s
= ast_for_suite(c
, CHILD(n
, 3));
2842 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2843 n
->n_col_offset
, c
->c_arena
);
2845 /* check for empty base list */
2846 if (TYPE(CHILD(n
,3)) == RPAR
) {
2847 s
= ast_for_suite(c
, CHILD(n
,5));
2850 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
),
2851 n
->n_col_offset
, c
->c_arena
);
2854 /* else handle the base class list */
2855 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
2859 s
= ast_for_suite(c
, CHILD(n
, 6));
2862 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, LINENO(n
),
2863 n
->n_col_offset
, c
->c_arena
);
2867 ast_for_stmt(struct compiling
*c
, const node
*n
)
2869 if (TYPE(n
) == stmt
) {
2870 assert(NCH(n
) == 1);
2873 if (TYPE(n
) == simple_stmt
) {
2874 assert(num_stmts(n
) == 1);
2877 if (TYPE(n
) == small_stmt
) {
2880 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2881 | flow_stmt | import_stmt | global_stmt | exec_stmt
2886 return ast_for_expr_stmt(c
, n
);
2888 return ast_for_print_stmt(c
, n
);
2890 return ast_for_del_stmt(c
, n
);
2892 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2894 return ast_for_flow_stmt(c
, n
);
2896 return ast_for_import_stmt(c
, n
);
2898 return ast_for_global_stmt(c
, n
);
2900 return ast_for_exec_stmt(c
, n
);
2902 return ast_for_assert_stmt(c
, n
);
2904 PyErr_Format(PyExc_SystemError
,
2905 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2911 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2912 | funcdef | classdef
2914 node
*ch
= CHILD(n
, 0);
2915 REQ(n
, compound_stmt
);
2918 return ast_for_if_stmt(c
, ch
);
2920 return ast_for_while_stmt(c
, ch
);
2922 return ast_for_for_stmt(c
, ch
);
2924 return ast_for_try_stmt(c
, ch
);
2926 return ast_for_with_stmt(c
, ch
);
2928 return ast_for_funcdef(c
, ch
);
2930 return ast_for_classdef(c
, ch
);
2932 PyErr_Format(PyExc_SystemError
,
2933 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2941 parsenumber(const char *s
)
2946 #ifndef WITHOUT_COMPLEX
2952 end
= s
+ strlen(s
) - 1;
2953 #ifndef WITHOUT_COMPLEX
2954 imflag
= *end
== 'j' || *end
== 'J';
2956 if (*end
== 'l' || *end
== 'L')
2957 return PyLong_FromString((char *)s
, (char **)0, 0);
2959 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
2960 if (x
< 0 && errno
== 0) {
2961 return PyLong_FromString((char *)s
,
2967 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
2970 return PyLong_FromString((char *)s
, (char **)0, 0);
2971 return PyInt_FromLong(x
);
2973 /* XXX Huge floats may silently fail */
2974 #ifndef WITHOUT_COMPLEX
2977 PyFPE_START_PROTECT("atof", return 0)
2978 c
.imag
= PyOS_ascii_atof(s
);
2979 PyFPE_END_PROTECT(c
)
2980 return PyComplex_FromCComplex(c
);
2985 PyFPE_START_PROTECT("atof", return 0)
2986 dx
= PyOS_ascii_atof(s
);
2987 PyFPE_END_PROTECT(dx
)
2988 return PyFloat_FromDouble(dx
);
2993 decode_utf8(const char **sPtr
, const char *end
, char* encoding
)
2995 #ifndef Py_USING_UNICODE
2996 Py_FatalError("decode_utf8 should not be called in this build.");
3001 t
= s
= (char *)*sPtr
;
3002 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3003 while (s
< end
&& (*s
& 0x80)) s
++;
3005 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3008 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3015 decode_unicode(const char *s
, size_t len
, int rawmode
, const char *encoding
)
3021 if (encoding
== NULL
) {
3024 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
3028 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3029 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
3032 p
= buf
= PyString_AsString(u
);
3042 if (*s
& 0x80) { /* XXX inefficient */
3046 w
= decode_utf8(&s
, end
, "utf-16-be");
3051 r
= PyString_AsString(w
);
3052 rn
= PyString_Size(w
);
3053 assert(rn
% 2 == 0);
3054 for (i
= 0; i
< rn
; i
+= 2) {
3055 sprintf(p
, "\\u%02x%02x",
3069 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3071 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3076 /* s is a Python string literal, including the bracketing quote characters,
3077 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3078 * parsestr parses it, and returns the decoded Python string object.
3081 parsestr(const char *s
, const char *encoding
)
3084 int quote
= Py_CHARMASK(*s
);
3089 if (isalpha(quote
) || quote
== '_') {
3090 if (quote
== 'u' || quote
== 'U') {
3094 if (quote
== 'r' || quote
== 'R') {
3099 if (quote
!= '\'' && quote
!= '\"') {
3100 PyErr_BadInternalCall();
3105 if (len
> INT_MAX
) {
3106 PyErr_SetString(PyExc_OverflowError
,
3107 "string to parse is too long");
3110 if (s
[--len
] != quote
) {
3111 PyErr_BadInternalCall();
3114 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3117 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3118 PyErr_BadInternalCall();
3122 #ifdef Py_USING_UNICODE
3123 if (unicode
|| Py_UnicodeFlag
) {
3124 return decode_unicode(s
, len
, rawmode
, encoding
);
3127 need_encoding
= (encoding
!= NULL
&&
3128 strcmp(encoding
, "utf-8") != 0 &&
3129 strcmp(encoding
, "iso-8859-1") != 0);
3130 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3131 if (need_encoding
) {
3132 #ifndef Py_USING_UNICODE
3133 /* This should not happen - we never see any other
3136 "cannot deal with encodings in this build.");
3138 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3141 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3146 return PyString_FromStringAndSize(s
, len
);
3150 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3151 need_encoding
? encoding
: NULL
);
3154 /* Build a Python string object out of a STRING atom. This takes care of
3155 * compile-time literal catenation, calling parsestr() on each piece, and
3156 * pasting the intermediate results together.
3159 parsestrplus(struct compiling
*c
, const node
*n
)
3163 REQ(CHILD(n
, 0), STRING
);
3164 if ((v
= parsestr(STR(CHILD(n
, 0)), c
->c_encoding
)) != NULL
) {
3165 /* String literal concatenation */
3166 for (i
= 1; i
< NCH(n
); i
++) {
3168 s
= parsestr(STR(CHILD(n
, i
)), c
->c_encoding
);
3171 if (PyString_Check(v
) && PyString_Check(s
)) {
3172 PyString_ConcatAndDel(&v
, s
);
3176 #ifdef Py_USING_UNICODE
3178 PyObject
*temp
= PyUnicode_Concat(v
, s
);