2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
7 #include "Python-ast.h"
19 - re-indent this file (should be done)
20 - internal error checking (freeing memory, etc.)
24 /* Data structure used internally */
26 char *c_encoding
; /* source encoding */
27 PyArena
*c_arena
; /* arena for allocating memeory */
30 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
31 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
32 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
33 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
34 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*, int);
35 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
36 static expr_ty
ast_for_testlist_gexp(struct compiling
*, const node
*);
38 /* Note different signature for ast_for_call */
39 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
41 static PyObject
*parsenumber(const char *);
42 static PyObject
*parsestr(const char *s
, const char *encoding
);
43 static PyObject
*parsestrplus(struct compiling
*, const node
*n
);
46 #define LINENO(n) ((n)->n_lineno)
50 new_identifier(const char* n
, PyArena
*arena
) {
51 PyObject
* id
= PyString_InternFromString(n
);
52 PyArena_AddPyObject(arena
, id
);
56 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
58 /* This routine provides an invalid object for the syntax error.
59 The outermost routine must unpack this error and create the
60 proper object. We do this so that we don't have to pass
61 the filename to everything function.
63 XXX Maybe we should just pass the filename...
67 ast_error(const node
*n
, const char *errstr
)
69 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
72 PyErr_SetObject(PyExc_SyntaxError
, u
);
78 ast_error_finish(const char *filename
)
80 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
83 assert(PyErr_Occurred());
84 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
87 PyErr_Fetch(&type
, &value
, &tback
);
88 errstr
= PyTuple_GetItem(value
, 0);
92 lineno
= PyInt_AsLong(PyTuple_GetItem(value
, 1));
99 loc
= PyErr_ProgramText(filename
, lineno
);
104 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
110 value
= Py_BuildValue("(OO)", errstr
, tmp
);
115 PyErr_Restore(type
, value
, tback
);
118 /* num_stmts() returns number of contained statements.
120 Use this routine to determine how big a sequence is needed for
121 the statements in a parse tree. Its raison d'etre is this bit of
124 stmt: simple_stmt | compound_stmt
125 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
127 A simple_stmt can contain multiple small_stmt elements joined
128 by semicolons. If the arg is a simple_stmt, the number of
129 small_stmt elements is returned.
133 num_stmts(const node
*n
)
140 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
143 return num_stmts(CHILD(n
, 0));
146 for (i
= 0; i
< NCH(n
); i
++) {
148 if (TYPE(ch
) == stmt
)
153 return num_stmts(CHILD(n
, 0));
157 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
160 return num_stmts(CHILD(n
, 0));
163 for (i
= 2; i
< (NCH(n
) - 1); i
++)
164 l
+= num_stmts(CHILD(n
, i
));
170 sprintf(buf
, "Non-statement found: %d %d\n",
179 /* Transform the CST rooted at node * to the appropriate AST
183 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
187 asdl_seq
*stmts
= NULL
;
192 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
193 c
.c_encoding
= "utf-8";
194 } else if (TYPE(n
) == encoding_decl
) {
195 c
.c_encoding
= STR(n
);
205 stmts
= asdl_seq_new(num_stmts(n
), arena
);
208 for (i
= 0; i
< NCH(n
) - 1; i
++) {
210 if (TYPE(ch
) == NEWLINE
)
215 s
= ast_for_stmt(&c
, ch
);
218 asdl_seq_SET(stmts
, k
++, s
);
222 REQ(ch
, simple_stmt
);
223 for (j
= 0; j
< num
; j
++) {
224 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
227 asdl_seq_SET(stmts
, k
++, s
);
231 return Module(stmts
, arena
);
233 expr_ty testlist_ast
;
235 /* XXX Why not gen_for here? */
236 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
239 return Expression(testlist_ast
, arena
);
242 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
243 stmts
= asdl_seq_new(1, arena
);
246 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
, arena
));
247 return Interactive(stmts
, arena
);
252 stmts
= asdl_seq_new(num
, arena
);
256 s
= ast_for_stmt(&c
, n
);
259 asdl_seq_SET(stmts
, 0, s
);
262 /* Only a simple_stmt can contain multiple statements. */
264 for (i
= 0; i
< NCH(n
); i
+= 2) {
265 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
267 s
= ast_for_stmt(&c
, CHILD(n
, i
));
270 asdl_seq_SET(stmts
, i
/ 2, s
);
274 return Interactive(stmts
, arena
);
280 ast_error_finish(filename
);
284 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
288 get_operator(const node
*n
)
318 /* Set the context ctx for expr_ty e, recursively traversing e.
320 Only sets context for expr kinds that "can appear in assignment context"
321 (according to ../Parser/Python.asdl). For other expr kinds, it sets
322 an appropriate syntax error and returns false.
326 set_context(expr_ty e
, expr_context_ty ctx
, const node
*n
)
329 /* If a particular expression type can't be used for assign / delete,
330 set expr_name to its name and an error message will be generated.
332 const char* expr_name
= NULL
;
334 /* The ast defines augmented store and load contexts, but the
335 implementation here doesn't actually use them. The code may be
336 a little more complex than necessary as a result. It also means
337 that expressions in an augmented assignment have no context.
338 Consider restructuring so that augmented assignment uses
341 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
346 !strcmp(PyString_AS_STRING(e
->v
.Attribute
.attr
), "None")) {
347 return ast_error(n
, "assignment to None");
349 e
->v
.Attribute
.ctx
= ctx
;
352 e
->v
.Subscript
.ctx
= ctx
;
356 !strcmp(PyString_AS_STRING(e
->v
.Name
.id
), "None")) {
357 return ast_error(n
, "assignment to None");
366 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
367 return ast_error(n
, "can't assign to ()");
368 e
->v
.Tuple
.ctx
= ctx
;
372 expr_name
= "lambda";
375 expr_name
= "function call";
380 expr_name
= "operator";
382 case GeneratorExp_kind
:
383 expr_name
= "generator expression";
386 expr_name
= "list comprehension";
391 expr_name
= "literal";
394 expr_name
= "comparison";
400 PyErr_Format(PyExc_SystemError
,
401 "unexpected expression in assignment %d (line %d)",
405 /* Check for error string set by switch */
408 PyOS_snprintf(buf
, sizeof(buf
),
410 ctx
== Store
? "assign to" : "delete",
412 return ast_error(n
, buf
);
415 /* If the LHS is a list or tuple, we need to set the assignment
416 context for all the contained elements.
421 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
422 if (!set_context(asdl_seq_GET(s
, i
), ctx
, n
))
430 ast_for_augassign(const node
*n
)
440 if (STR(n
)[1] == '/')
457 if (STR(n
)[1] == '*')
462 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
468 ast_for_comp_op(const node
*n
)
470 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
481 case EQEQUAL
: /* == */
490 if (strcmp(STR(n
), "in") == 0)
492 if (strcmp(STR(n
), "is") == 0)
495 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
500 else if (NCH(n
) == 2) {
501 /* handle "not in" and "is not" */
502 switch (TYPE(CHILD(n
, 0))) {
504 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
506 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
509 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
510 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
514 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
520 seq_for_testlist(struct compiling
*c
, const node
*n
)
522 /* testlist: test (',' test)* [','] */
526 assert(TYPE(n
) == testlist
527 || TYPE(n
) == listmaker
528 || TYPE(n
) == testlist_gexp
529 || TYPE(n
) == testlist_safe
532 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
536 for (i
= 0; i
< NCH(n
); i
+= 2) {
537 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == old_test
);
539 expression
= ast_for_expr(c
, CHILD(n
, i
));
543 assert(i
/ 2 < seq
->size
);
544 asdl_seq_SET(seq
, i
/ 2, expression
);
550 compiler_complex_args(struct compiling
*c
, const node
*n
)
552 int i
, len
= (NCH(n
) + 1) / 2;
554 asdl_seq
*args
= asdl_seq_new(len
, c
->c_arena
);
559 for (i
= 0; i
< len
; i
++) {
560 const node
*child
= CHILD(CHILD(n
, 2*i
), 0);
562 if (TYPE(child
) == NAME
) {
563 if (!strcmp(STR(child
), "None")) {
564 ast_error(child
, "assignment to None");
567 arg
= Name(NEW_IDENTIFIER(child
), Store
, LINENO(child
), child
->n_col_offset
,
571 arg
= compiler_complex_args(c
, CHILD(CHILD(n
, 2*i
), 1));
573 asdl_seq_SET(args
, i
, arg
);
576 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
577 if (!set_context(result
, Store
, n
))
583 /* Create AST for argument list. */
586 ast_for_arguments(struct compiling
*c
, const node
*n
)
588 /* parameters: '(' [varargslist] ')'
589 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
590 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
592 int i
, j
, k
, n_args
= 0, n_defaults
= 0, found_default
= 0;
593 asdl_seq
*args
, *defaults
;
594 identifier vararg
= NULL
, kwarg
= NULL
;
597 if (TYPE(n
) == parameters
) {
598 if (NCH(n
) == 2) /* () as argument list */
599 return arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
604 /* first count the number of normal args & defaults */
605 for (i
= 0; i
< NCH(n
); i
++) {
607 if (TYPE(ch
) == fpdef
)
609 if (TYPE(ch
) == EQUAL
)
612 args
= (n_args
? asdl_seq_new(n_args
, c
->c_arena
) : NULL
);
614 return NULL
; /* Don't need to go to NULL; nothing allocated */
615 defaults
= (n_defaults
? asdl_seq_new(n_defaults
, c
->c_arena
) : NULL
);
616 if (!defaults
&& n_defaults
)
619 /* fpdef: NAME | '(' fplist ')'
620 fplist: fpdef (',' fpdef)* [',']
623 j
= 0; /* index for defaults */
624 k
= 0; /* index for args */
629 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
630 anything other than EQUAL or a comma? */
631 /* XXX Should NCH(n) check be made a separate check? */
632 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
633 asdl_seq_SET(defaults
, j
++,
634 ast_for_expr(c
, CHILD(n
, i
+ 2)));
638 else if (found_default
) {
640 "non-default argument follows default argument");
644 asdl_seq_SET(args
, k
++,
645 compiler_complex_args(c
, CHILD(ch
, 1)));
647 else if (TYPE(CHILD(ch
, 0)) == NAME
) {
649 if (!strcmp(STR(CHILD(ch
, 0)), "None")) {
650 ast_error(CHILD(ch
, 0), "assignment to None");
653 name
= Name(NEW_IDENTIFIER(CHILD(ch
, 0)),
654 Param
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
657 asdl_seq_SET(args
, k
++, name
);
660 i
+= 2; /* the name and the comma */
663 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
664 ast_error(CHILD(n
, i
+1), "assignment to None");
667 vararg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
671 if (!strcmp(STR(CHILD(n
, i
+1)), "None")) {
672 ast_error(CHILD(n
, i
+1), "assignment to None");
675 kwarg
= NEW_IDENTIFIER(CHILD(n
, i
+1));
679 PyErr_Format(PyExc_SystemError
,
680 "unexpected node in varargslist: %d @ %d",
686 return arguments(args
, vararg
, kwarg
, defaults
, c
->c_arena
);
695 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
699 int lineno
, col_offset
;
705 col_offset
= n
->n_col_offset
;
707 id
= NEW_IDENTIFIER(CHILD(n
, 0));
710 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
714 for (i
= 2; i
< NCH(n
); i
+=2) {
715 id
= NEW_IDENTIFIER(CHILD(n
, i
));
718 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
727 ast_for_decorator(struct compiling
*c
, const node
*n
)
729 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
734 REQ(CHILD(n
, 0), AT
);
735 REQ(RCHILD(n
, -1), NEWLINE
);
737 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
741 if (NCH(n
) == 3) { /* No arguments */
745 else if (NCH(n
) == 5) { /* Call with no arguments */
746 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
752 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
762 ast_for_decorators(struct compiling
*c
, const node
*n
)
764 asdl_seq
* decorator_seq
;
769 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
773 for (i
= 0; i
< NCH(n
); i
++) {
774 d
= ast_for_decorator(c
, CHILD(n
, i
));
777 asdl_seq_SET(decorator_seq
, i
, d
);
779 return decorator_seq
;
783 ast_for_funcdef(struct compiling
*c
, const node
*n
)
785 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
789 asdl_seq
*decorator_seq
= NULL
;
794 if (NCH(n
) == 6) { /* decorators are present */
795 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
804 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
807 else if (!strcmp(STR(CHILD(n
, name_i
)), "None")) {
808 ast_error(CHILD(n
, name_i
), "assignment to None");
811 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
814 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
818 return FunctionDef(name
, args
, body
, decorator_seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
822 ast_for_lambdef(struct compiling
*c
, const node
*n
)
824 /* lambdef: 'lambda' [varargslist] ':' test */
829 args
= arguments(NULL
, NULL
, NULL
, NULL
, c
->c_arena
);
832 expression
= ast_for_expr(c
, CHILD(n
, 2));
837 args
= ast_for_arguments(c
, CHILD(n
, 1));
840 expression
= ast_for_expr(c
, CHILD(n
, 3));
845 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
849 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
851 /* test: or_test 'if' or_test 'else' test */
852 expr_ty expression
, body
, orelse
;
855 body
= ast_for_expr(c
, CHILD(n
, 0));
858 expression
= ast_for_expr(c
, CHILD(n
, 2));
861 orelse
= ast_for_expr(c
, CHILD(n
, 4));
864 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
867 /* Count the number of 'for' loop in a list comprehension.
869 Helper for ast_for_listcomp().
873 count_list_fors(const node
*n
)
876 node
*ch
= CHILD(n
, 1);
888 if (TYPE(ch
) == list_for
)
890 else if (TYPE(ch
) == list_if
) {
893 goto count_list_iter
;
899 /* Should never be reached */
900 PyErr_SetString(PyExc_SystemError
, "logic error in count_list_fors");
904 /* Count the number of 'if' statements in a list comprehension.
906 Helper for ast_for_listcomp().
910 count_list_ifs(const node
*n
)
916 if (TYPE(CHILD(n
, 0)) == list_for
)
924 goto count_list_iter
;
928 ast_for_listcomp(struct compiling
*c
, const node
*n
)
930 /* listmaker: test ( list_for | (',' test)* [','] )
931 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
932 list_iter: list_for | list_if
933 list_if: 'if' test [list_iter]
934 testlist_safe: test [(',' test)+ [',']]
944 elt
= ast_for_expr(c
, CHILD(n
, 0));
948 n_fors
= count_list_fors(n
);
952 listcomps
= asdl_seq_new(n_fors
, c
->c_arena
);
957 for (i
= 0; i
< n_fors
; i
++) {
964 t
= ast_for_exprlist(c
, CHILD(ch
, 1), Store
);
967 expression
= ast_for_testlist(c
, CHILD(ch
, 3));
971 if (asdl_seq_LEN(t
) == 1)
972 lc
= comprehension(asdl_seq_GET(t
, 0), expression
, NULL
,
975 lc
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
),
976 expression
, NULL
, c
->c_arena
);
985 n_ifs
= count_list_ifs(ch
);
989 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
993 for (j
= 0; j
< n_ifs
; j
++) {
998 asdl_seq_SET(ifs
, j
, ast_for_expr(c
, CHILD(ch
, 1)));
1002 /* on exit, must guarantee that ch is a list_for */
1003 if (TYPE(ch
) == list_iter
)
1007 asdl_seq_SET(listcomps
, i
, lc
);
1010 return ListComp(elt
, listcomps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1014 Count the number of 'for' loops in a generator expression.
1016 Helper for ast_for_genexp().
1020 count_gen_fors(const node
*n
)
1023 node
*ch
= CHILD(n
, 1);
1035 if (TYPE(ch
) == gen_for
)
1037 else if (TYPE(ch
) == gen_if
) {
1040 goto count_gen_iter
;
1046 /* Should never be reached */
1047 PyErr_SetString(PyExc_SystemError
,
1048 "logic error in count_gen_fors");
1052 /* Count the number of 'if' statements in a generator expression.
1054 Helper for ast_for_genexp().
1058 count_gen_ifs(const node
*n
)
1064 if (TYPE(CHILD(n
, 0)) == gen_for
)
1075 /* TODO(jhylton): Combine with list comprehension code? */
1077 ast_for_genexp(struct compiling
*c
, const node
*n
)
1079 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1080 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1086 assert(TYPE(n
) == (testlist_gexp
) || TYPE(n
) == (argument
));
1089 elt
= ast_for_expr(c
, CHILD(n
, 0));
1093 n_fors
= count_gen_fors(n
);
1097 genexps
= asdl_seq_new(n_fors
, c
->c_arena
);
1102 for (i
= 0; i
< n_fors
; i
++) {
1103 comprehension_ty ge
;
1109 t
= ast_for_exprlist(c
, CHILD(ch
, 1), Store
);
1112 expression
= ast_for_expr(c
, CHILD(ch
, 3));
1116 if (asdl_seq_LEN(t
) == 1)
1117 ge
= comprehension(asdl_seq_GET(t
, 0), expression
,
1120 ge
= comprehension(Tuple(t
, Store
, LINENO(ch
), ch
->n_col_offset
, c
->c_arena
),
1121 expression
, NULL
, c
->c_arena
);
1131 n_ifs
= count_gen_ifs(ch
);
1135 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1139 for (j
= 0; j
< n_ifs
; j
++) {
1144 expression
= ast_for_expr(c
, CHILD(ch
, 1));
1147 asdl_seq_SET(ifs
, j
, expression
);
1151 /* on exit, must guarantee that ch is a gen_for */
1152 if (TYPE(ch
) == gen_iter
)
1156 asdl_seq_SET(genexps
, i
, ge
);
1159 return GeneratorExp(elt
, genexps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1163 ast_for_atom(struct compiling
*c
, const node
*n
)
1165 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1166 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1168 node
*ch
= CHILD(n
, 0);
1172 /* All names start in Load context, but may later be
1174 return Name(NEW_IDENTIFIER(ch
), Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1176 PyObject
*str
= parsestrplus(c
, n
);
1180 PyArena_AddPyObject(c
->c_arena
, str
);
1181 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1184 PyObject
*pynum
= parsenumber(STR(ch
));
1188 PyArena_AddPyObject(c
->c_arena
, pynum
);
1189 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1191 case LPAR
: /* some parenthesized expressions */
1194 if (TYPE(ch
) == RPAR
)
1195 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1197 if (TYPE(ch
) == yield_expr
)
1198 return ast_for_expr(c
, ch
);
1200 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == gen_for
))
1201 return ast_for_genexp(c
, ch
);
1203 return ast_for_testlist_gexp(c
, ch
);
1204 case LSQB
: /* list (or list comprehension) */
1207 if (TYPE(ch
) == RSQB
)
1208 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1211 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1212 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1216 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1219 return ast_for_listcomp(c
, ch
);
1221 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1223 asdl_seq
*keys
, *values
;
1226 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1227 keys
= asdl_seq_new(size
, c
->c_arena
);
1231 values
= asdl_seq_new(size
, c
->c_arena
);
1235 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1238 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1242 asdl_seq_SET(keys
, i
/ 4, expression
);
1244 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1248 asdl_seq_SET(values
, i
/ 4, expression
);
1250 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1252 case BACKQUOTE
: { /* repr */
1253 expr_ty expression
= ast_for_testlist(c
, CHILD(n
, 1));
1257 return Repr(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1260 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1266 ast_for_slice(struct compiling
*c
, const node
*n
)
1269 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1274 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1278 if (TYPE(ch
) == DOT
)
1279 return Ellipsis(c
->c_arena
);
1281 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1282 /* 'step' variable hold no significance in terms of being used over
1284 step
= ast_for_expr(c
, ch
);
1288 return Index(step
, c
->c_arena
);
1291 if (TYPE(ch
) == test
) {
1292 lower
= ast_for_expr(c
, ch
);
1297 /* If there's an upper bound it's in the second or third position. */
1298 if (TYPE(ch
) == COLON
) {
1300 node
*n2
= CHILD(n
, 1);
1302 if (TYPE(n2
) == test
) {
1303 upper
= ast_for_expr(c
, n2
);
1308 } else if (NCH(n
) > 2) {
1309 node
*n2
= CHILD(n
, 2);
1311 if (TYPE(n2
) == test
) {
1312 upper
= ast_for_expr(c
, n2
);
1318 ch
= CHILD(n
, NCH(n
) - 1);
1319 if (TYPE(ch
) == sliceop
) {
1321 /* XXX: If only 1 child, then should just be a colon. Should we
1322 just skip assigning and just get to the return? */
1326 if (TYPE(ch
) == test
) {
1327 step
= ast_for_expr(c
, ch
);
1333 return Slice(lower
, upper
, step
, c
->c_arena
);
1337 ast_for_binop(struct compiling
*c
, const node
*n
)
1339 /* Must account for a sequence of expressions.
1340 How should A op B op C by represented?
1341 BinOp(BinOp(A, op, B), op, C).
1345 expr_ty expr1
, expr2
, result
;
1346 operator_ty
operator;
1348 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1352 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1356 operator = get_operator(CHILD(n
, 1));
1360 result
= BinOp(expr1
, operator, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1364 nops
= (NCH(n
) - 1) / 2;
1365 for (i
= 1; i
< nops
; i
++) {
1366 expr_ty tmp_result
, tmp
;
1367 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1369 operator = get_operator(next_oper
);
1373 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1377 tmp_result
= BinOp(result
, operator, tmp
,
1378 LINENO(next_oper
), next_oper
->n_col_offset
, c
->c_arena
);
1381 result
= tmp_result
;
1387 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1389 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1390 subscriptlist: subscript (',' subscript)* [',']
1391 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1394 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1396 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1398 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1400 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1401 return Attribute(left_expr
, NEW_IDENTIFIER(CHILD(n
, 1)), Load
,
1402 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1405 REQ(CHILD(n
, 0), LSQB
);
1406 REQ(CHILD(n
, 2), RSQB
);
1409 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1412 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1415 /* The grammar is ambiguous here. The ambiguity is resolved
1416 by treating the sequence as a tuple literal if there are
1423 asdl_seq
*slices
, *elts
;
1424 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1427 for (j
= 0; j
< NCH(n
); j
+= 2) {
1428 slc
= ast_for_slice(c
, CHILD(n
, j
));
1431 if (slc
->kind
!= Index_kind
)
1433 asdl_seq_SET(slices
, j
/ 2, slc
);
1436 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1437 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1439 /* extract Index values and put them in a Tuple */
1440 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1443 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1444 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1445 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1446 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1448 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1451 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1452 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1458 ast_for_power(struct compiling
*c
, const node
*n
)
1460 /* power: atom trailer* ('**' factor)*
1465 e
= ast_for_atom(c
, CHILD(n
, 0));
1470 for (i
= 1; i
< NCH(n
); i
++) {
1471 node
*ch
= CHILD(n
, i
);
1472 if (TYPE(ch
) != trailer
)
1474 tmp
= ast_for_trailer(c
, ch
, e
);
1477 tmp
->lineno
= e
->lineno
;
1478 tmp
->col_offset
= e
->col_offset
;
1481 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1482 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1485 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1493 /* Do not name a variable 'expr'! Will cause a compile error.
1497 ast_for_expr(struct compiling
*c
, const node
*n
)
1499 /* handle the full range of simple expressions
1500 test: or_test ['if' or_test 'else' test] | lambdef
1501 or_test: and_test ('or' and_test)*
1502 and_test: not_test ('and' not_test)*
1503 not_test: 'not' not_test | comparison
1504 comparison: expr (comp_op expr)*
1505 expr: xor_expr ('|' xor_expr)*
1506 xor_expr: and_expr ('^' and_expr)*
1507 and_expr: shift_expr ('&' shift_expr)*
1508 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1509 arith_expr: term (('+'|'-') term)*
1510 term: factor (('*'|'/'|'%'|'//') factor)*
1511 factor: ('+'|'-'|'~') factor | power
1512 power: atom trailer* ('**' factor)*
1514 As well as modified versions that exist for backward compatibility,
1515 to explicitly allow:
1516 [ x for x in lambda: 0, lambda: 1 ]
1517 (which would be ambiguous without these extra rules)
1519 old_test: or_test | old_lambdef
1520 old_lambdef: 'lambda' [vararglist] ':' old_test
1531 if (TYPE(CHILD(n
, 0)) == lambdef
||
1532 TYPE(CHILD(n
, 0)) == old_lambdef
)
1533 return ast_for_lambdef(c
, CHILD(n
, 0));
1534 else if (NCH(n
) > 1)
1535 return ast_for_ifexpr(c
, n
);
1543 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1546 for (i
= 0; i
< NCH(n
); i
+= 2) {
1547 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1550 asdl_seq_SET(seq
, i
/ 2, e
);
1552 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1553 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1554 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1555 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1562 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1566 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1575 asdl_seq
*ops
, *cmps
;
1576 ops
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1579 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1583 for (i
= 1; i
< NCH(n
); i
+= 2) {
1584 /* XXX cmpop_ty is just an enum */
1587 operator = ast_for_comp_op(CHILD(n
, i
));
1592 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1597 asdl_seq_SET(ops
, i
/ 2, (void *)(Py_uintptr_t
)operator);
1598 asdl_seq_SET(cmps
, i
/ 2, expression
);
1600 expression
= ast_for_expr(c
, CHILD(n
, 0));
1605 return Compare(expression
, ops
, cmps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1609 /* The next five cases all handle BinOps. The main body of code
1610 is the same in each case, but the switch turned inside out to
1611 reuse the code for each type of operator.
1623 return ast_for_binop(c
, n
);
1627 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1631 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1641 expression
= ast_for_expr(c
, CHILD(n
, 1));
1645 switch (TYPE(CHILD(n
, 0))) {
1647 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1649 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1651 return UnaryOp(Invert
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1653 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1658 return ast_for_power(c
, n
);
1660 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1663 /* should never get here unless if error is set */
1668 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1671 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1673 argument: [test '='] test [gen_for] # Really [keyword '='] test
1676 int i
, nargs
, nkeywords
, ngens
;
1679 expr_ty vararg
= NULL
, kwarg
= NULL
;
1686 for (i
= 0; i
< NCH(n
); i
++) {
1687 node
*ch
= CHILD(n
, i
);
1688 if (TYPE(ch
) == argument
) {
1691 else if (TYPE(CHILD(ch
, 1)) == gen_for
)
1697 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1698 ast_error(n
, "Generator expression must be parenthesized "
1699 "if not sole argument");
1703 if (nargs
+ nkeywords
+ ngens
> 255) {
1704 ast_error(n
, "more than 255 arguments");
1708 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1711 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1716 for (i
= 0; i
< NCH(n
); i
++) {
1717 node
*ch
= CHILD(n
, i
);
1718 if (TYPE(ch
) == argument
) {
1721 e
= ast_for_expr(c
, CHILD(ch
, 0));
1724 asdl_seq_SET(args
, nargs
++, e
);
1726 else if (TYPE(CHILD(ch
, 1)) == gen_for
) {
1727 e
= ast_for_genexp(c
, ch
);
1730 asdl_seq_SET(args
, nargs
++, e
);
1736 /* CHILD(ch, 0) is test, but must be an identifier? */
1737 e
= ast_for_expr(c
, CHILD(ch
, 0));
1740 /* f(lambda x: x[0] = 3) ends up getting parsed with
1741 * LHS test = lambda x: x[0], and RHS test = 3.
1742 * SF bug 132313 points out that complaining about a keyword
1743 * then is very confusing.
1745 if (e
->kind
== Lambda_kind
) {
1746 ast_error(CHILD(ch
, 0), "lambda cannot contain assignment");
1748 } else if (e
->kind
!= Name_kind
) {
1749 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
1753 e
= ast_for_expr(c
, CHILD(ch
, 2));
1756 kw
= keyword(key
, e
, c
->c_arena
);
1759 asdl_seq_SET(keywords
, nkeywords
++, kw
);
1762 else if (TYPE(ch
) == STAR
) {
1763 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
1766 else if (TYPE(ch
) == DOUBLESTAR
) {
1767 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
1772 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
, func
->col_offset
, c
->c_arena
);
1776 ast_for_testlist(struct compiling
*c
, const node
* n
)
1778 /* testlist_gexp: test (',' test)* [','] */
1779 /* testlist: test (',' test)* [','] */
1780 /* testlist_safe: test (',' test)+ [','] */
1781 /* testlist1: test (',' test)* */
1783 if (TYPE(n
) == testlist_gexp
) {
1785 assert(TYPE(CHILD(n
, 1)) != gen_for
);
1788 assert(TYPE(n
) == testlist
||
1789 TYPE(n
) == testlist_safe
||
1790 TYPE(n
) == testlist1
);
1793 return ast_for_expr(c
, CHILD(n
, 0));
1795 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
1798 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1803 ast_for_testlist_gexp(struct compiling
*c
, const node
* n
)
1805 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1806 /* argument: test [ gen_for ] */
1807 assert(TYPE(n
) == testlist_gexp
|| TYPE(n
) == argument
);
1808 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == gen_for
)
1809 return ast_for_genexp(c
, n
);
1810 return ast_for_testlist(c
, n
);
1813 /* like ast_for_testlist() but returns a sequence */
1815 ast_for_class_bases(struct compiling
*c
, const node
* n
)
1817 /* testlist: test (',' test)* [','] */
1822 asdl_seq
*bases
= asdl_seq_new(1, c
->c_arena
);
1825 base
= ast_for_expr(c
, CHILD(n
, 0));
1828 asdl_seq_SET(bases
, 0, base
);
1832 return seq_for_testlist(c
, n
);
1836 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
1839 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1840 | ('=' (yield_expr|testlist))*)
1841 testlist: test (',' test)* [',']
1842 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
1843 | '<<=' | '>>=' | '**=' | '//='
1844 test: ... here starts the operator precendence dance
1848 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
1852 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1854 else if (TYPE(CHILD(n
, 1)) == augassign
) {
1855 expr_ty expr1
, expr2
;
1856 operator_ty
operator;
1857 node
*ch
= CHILD(n
, 0);
1859 if (TYPE(ch
) == testlist
)
1860 expr1
= ast_for_testlist(c
, ch
);
1862 expr1
= Yield(ast_for_expr(c
, CHILD(ch
, 0)), LINENO(ch
), n
->n_col_offset
,
1867 /* TODO(jhylton): Figure out why set_context() can't be used here. */
1868 switch (expr1
->kind
) {
1869 case GeneratorExp_kind
:
1870 ast_error(ch
, "augmented assignment to generator "
1871 "expression not possible");
1874 const char *var_name
= PyString_AS_STRING(expr1
->v
.Name
.id
);
1875 if (var_name
[0] == 'N' && !strcmp(var_name
, "None")) {
1876 ast_error(ch
, "assignment to None");
1881 case Attribute_kind
:
1882 case Subscript_kind
:
1885 ast_error(ch
, "illegal expression for augmented "
1891 if (TYPE(ch
) == testlist
)
1892 expr2
= ast_for_testlist(c
, ch
);
1894 expr2
= Yield(ast_for_expr(c
, ch
), LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1898 operator = ast_for_augassign(CHILD(n
, 1));
1902 return AugAssign(expr1
, operator, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1910 /* a normal assignment */
1911 REQ(CHILD(n
, 1), EQUAL
);
1912 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1915 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
1917 node
*ch
= CHILD(n
, i
);
1918 if (TYPE(ch
) == yield_expr
) {
1919 ast_error(ch
, "assignment to yield expression not possible");
1922 e
= ast_for_testlist(c
, ch
);
1924 /* set context to assign */
1928 if (!set_context(e
, Store
, CHILD(n
, i
)))
1931 asdl_seq_SET(targets
, i
/ 2, e
);
1933 value
= CHILD(n
, NCH(n
) - 1);
1934 if (TYPE(value
) == testlist
)
1935 expression
= ast_for_testlist(c
, value
);
1937 expression
= ast_for_expr(c
, value
);
1940 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1945 ast_for_print_stmt(struct compiling
*c
, const node
*n
)
1947 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1948 | '>>' test [ (',' test)+ [','] ] )
1950 expr_ty dest
= NULL
, expression
;
1953 int i
, j
, start
= 1;
1956 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
1957 dest
= ast_for_expr(c
, CHILD(n
, 2));
1962 seq
= asdl_seq_new((NCH(n
) + 1 - start
) / 2, c
->c_arena
);
1965 for (i
= start
, j
= 0; i
< NCH(n
); i
+= 2, ++j
) {
1966 expression
= ast_for_expr(c
, CHILD(n
, i
));
1969 asdl_seq_SET(seq
, j
, expression
);
1971 nl
= (TYPE(CHILD(n
, NCH(n
) - 1)) == COMMA
) ? false : true;
1972 return Print(dest
, seq
, nl
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1976 ast_for_exprlist(struct compiling
*c
, const node
*n
, int context
)
1984 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1987 for (i
= 0; i
< NCH(n
); i
+= 2) {
1988 e
= ast_for_expr(c
, CHILD(n
, i
));
1991 asdl_seq_SET(seq
, i
/ 2, e
);
1992 if (context
&& !set_context(e
, context
, CHILD(n
, i
)))
1999 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2001 asdl_seq
*expr_list
;
2003 /* del_stmt: 'del' exprlist */
2006 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2009 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2013 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2016 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2019 continue_stmt: 'continue'
2020 return_stmt: 'return' [testlist]
2021 yield_stmt: yield_expr
2022 yield_expr: 'yield' testlist
2023 raise_stmt: 'raise' [test [',' test [',' test]]]
2031 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2033 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2034 case yield_stmt
: { /* will reduce to yield_expr */
2035 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2038 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2042 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2044 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2047 return Return(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2051 return Raise(NULL
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2052 else if (NCH(ch
) == 2) {
2053 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2056 return Raise(expression
, NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2058 else if (NCH(ch
) == 4) {
2059 expr_ty expr1
, expr2
;
2061 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2064 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2068 return Raise(expr1
, expr2
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2070 else if (NCH(ch
) == 6) {
2071 expr_ty expr1
, expr2
, expr3
;
2073 expr1
= ast_for_expr(c
, CHILD(ch
, 1));
2076 expr2
= ast_for_expr(c
, CHILD(ch
, 3));
2079 expr3
= ast_for_expr(c
, CHILD(ch
, 5));
2083 return Raise(expr1
, expr2
, expr3
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2086 PyErr_Format(PyExc_SystemError
,
2087 "unexpected flow_stmt: %d", TYPE(ch
));
2091 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2096 alias_for_import_name(struct compiling
*c
, const node
*n
)
2099 import_as_name: NAME ['as' NAME]
2100 dotted_as_name: dotted_name ['as' NAME]
2101 dotted_name: NAME ('.' NAME)*
2107 case import_as_name
:
2108 str
= (NCH(n
) == 3) ? NEW_IDENTIFIER(CHILD(n
, 2)) : NULL
;
2109 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), str
, c
->c_arena
);
2110 case dotted_as_name
:
2116 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2118 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2124 return alias(NEW_IDENTIFIER(CHILD(n
, 0)), NULL
, c
->c_arena
);
2126 /* Create a string of the form "a.b.c" */
2132 for (i
= 0; i
< NCH(n
); i
+= 2)
2133 /* length of string plus one for the dot */
2134 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2135 len
--; /* the last name doesn't have a dot */
2136 str
= PyString_FromStringAndSize(NULL
, len
);
2139 s
= PyString_AS_STRING(str
);
2142 for (i
= 0; i
< NCH(n
); i
+= 2) {
2143 char *sch
= STR(CHILD(n
, i
));
2144 strcpy(s
, STR(CHILD(n
, i
)));
2150 PyString_InternInPlace(&str
);
2151 PyArena_AddPyObject(c
->c_arena
, str
);
2152 return alias(str
, NULL
, c
->c_arena
);
2156 str
= PyString_InternFromString("*");
2157 PyArena_AddPyObject(c
->c_arena
, str
);
2158 return alias(str
, NULL
, c
->c_arena
);
2160 PyErr_Format(PyExc_SystemError
,
2161 "unexpected import name: %d", TYPE(n
));
2165 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2170 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2173 import_stmt: import_name | import_from
2174 import_name: 'import' dotted_as_names
2175 import_from: 'from' ('.'* dotted_name | '.') 'import'
2176 ('*' | '(' import_as_names ')' | import_as_names)
2183 REQ(n
, import_stmt
);
2185 col_offset
= n
->n_col_offset
;
2187 if (TYPE(n
) == import_name
) {
2189 REQ(n
, dotted_as_names
);
2190 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2193 for (i
= 0; i
< NCH(n
); i
+= 2) {
2194 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2197 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2199 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2201 else if (TYPE(n
) == import_from
) {
2204 alias_ty mod
= NULL
;
2207 /* Count the number of dots (for relative imports) and check for the
2208 optional module name */
2209 for (idx
= 1; idx
< NCH(n
); idx
++) {
2210 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2211 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2214 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2219 idx
++; /* skip over the 'import' keyword */
2220 switch (TYPE(CHILD(n
, idx
))) {
2222 /* from ... import * */
2226 ast_error(n
, "'import *' not allowed with 'from .'");
2231 /* from ... import (x, y, z) */
2232 n
= CHILD(n
, idx
+ 1);
2233 n_children
= NCH(n
);
2235 case import_as_names
:
2236 /* from ... import x, y, z */
2238 n_children
= NCH(n
);
2239 if (n_children
% 2 == 0) {
2240 ast_error(n
, "trailing comma not allowed without"
2241 " surrounding parentheses");
2246 ast_error(n
, "Unexpected node-type in from-import");
2250 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2254 /* handle "from ... import *" special b/c there's no children */
2255 if (TYPE(n
) == STAR
) {
2256 alias_ty import_alias
= alias_for_import_name(c
, n
);
2259 asdl_seq_SET(aliases
, 0, import_alias
);
2262 for (i
= 0; i
< NCH(n
); i
+= 2) {
2263 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2266 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2270 modname
= mod
->name
;
2272 modname
= new_identifier("", c
->c_arena
);
2273 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2276 PyErr_Format(PyExc_SystemError
,
2277 "unknown import statement: starts with command '%s'",
2283 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2285 /* global_stmt: 'global' NAME (',' NAME)* */
2290 REQ(n
, global_stmt
);
2291 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2294 for (i
= 1; i
< NCH(n
); i
+= 2) {
2295 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2298 asdl_seq_SET(s
, i
/ 2, name
);
2300 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2304 ast_for_exec_stmt(struct compiling
*c
, const node
*n
)
2306 expr_ty expr1
, globals
= NULL
, locals
= NULL
;
2307 int n_children
= NCH(n
);
2308 if (n_children
!= 2 && n_children
!= 4 && n_children
!= 6) {
2309 PyErr_Format(PyExc_SystemError
,
2310 "poorly formed 'exec' statement: %d parts to statement",
2315 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2317 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2320 if (n_children
>= 4) {
2321 globals
= ast_for_expr(c
, CHILD(n
, 3));
2325 if (n_children
== 6) {
2326 locals
= ast_for_expr(c
, CHILD(n
, 5));
2331 return Exec(expr1
, globals
, locals
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2335 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2337 /* assert_stmt: 'assert' test [',' test] */
2338 REQ(n
, assert_stmt
);
2340 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2343 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2345 else if (NCH(n
) == 4) {
2346 expr_ty expr1
, expr2
;
2348 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2351 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2355 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2357 PyErr_Format(PyExc_SystemError
,
2358 "improper number of parts to 'assert' statement: %d",
2364 ast_for_suite(struct compiling
*c
, const node
*n
)
2366 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2369 int i
, total
, num
, end
, pos
= 0;
2374 total
= num_stmts(n
);
2375 seq
= asdl_seq_new(total
, c
->c_arena
);
2378 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2380 /* simple_stmt always ends with a NEWLINE,
2381 and may have a trailing SEMI
2384 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2386 /* loop by 2 to skip semi-colons */
2387 for (i
= 0; i
< end
; i
+= 2) {
2389 s
= ast_for_stmt(c
, ch
);
2392 asdl_seq_SET(seq
, pos
++, s
);
2396 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2399 num
= num_stmts(ch
);
2401 /* small_stmt or compound_stmt with only one child */
2402 s
= ast_for_stmt(c
, ch
);
2405 asdl_seq_SET(seq
, pos
++, s
);
2410 REQ(ch
, simple_stmt
);
2411 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2412 /* statement terminates with a semi-colon ';' */
2413 if (NCH(CHILD(ch
, j
)) == 0) {
2414 assert((j
+ 1) == NCH(ch
));
2417 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2420 asdl_seq_SET(seq
, pos
++, s
);
2425 assert(pos
== seq
->size
);
2430 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2432 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2441 asdl_seq
*suite_seq
;
2443 expression
= ast_for_expr(c
, CHILD(n
, 1));
2446 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2450 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2453 s
= STR(CHILD(n
, 4));
2454 /* s[2], the third character in the string, will be
2460 asdl_seq
*seq1
, *seq2
;
2462 expression
= ast_for_expr(c
, CHILD(n
, 1));
2465 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2468 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2472 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2474 else if (s
[2] == 'i') {
2475 int i
, n_elif
, has_else
= 0;
2476 asdl_seq
*orelse
= NULL
;
2477 n_elif
= NCH(n
) - 4;
2478 /* must reference the child n_elif+1 since 'else' token is third,
2479 not fourth, child from the end. */
2480 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2481 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2489 asdl_seq
*seq1
, *seq2
;
2491 orelse
= asdl_seq_new(1, c
->c_arena
);
2494 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2497 seq1
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2500 seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2504 asdl_seq_SET(orelse
, 0, If(expression
, seq1
, seq2
,
2505 LINENO(CHILD(n
, NCH(n
) - 6)), CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2507 /* the just-created orelse handled the last elif */
2511 for (i
= 0; i
< n_elif
; i
++) {
2512 int off
= 5 + (n_elif
- i
- 1) * 4;
2514 asdl_seq
*suite_seq
;
2515 asdl_seq
*new = asdl_seq_new(1, c
->c_arena
);
2518 expression
= ast_for_expr(c
, CHILD(n
, off
));
2521 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2525 asdl_seq_SET(new, 0,
2526 If(expression
, suite_seq
, orelse
,
2527 LINENO(CHILD(n
, off
)), CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2530 return If(ast_for_expr(c
, CHILD(n
, 1)),
2531 ast_for_suite(c
, CHILD(n
, 3)),
2532 orelse
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2535 PyErr_Format(PyExc_SystemError
,
2536 "unexpected token in 'if' statement: %s", s
);
2541 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2543 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2548 asdl_seq
*suite_seq
;
2550 expression
= ast_for_expr(c
, CHILD(n
, 1));
2553 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2556 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2558 else if (NCH(n
) == 7) {
2560 asdl_seq
*seq1
, *seq2
;
2562 expression
= ast_for_expr(c
, CHILD(n
, 1));
2565 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2568 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2572 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2575 PyErr_Format(PyExc_SystemError
,
2576 "wrong number of tokens for 'while' statement: %d",
2582 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2584 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2587 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2591 seq
= ast_for_suite(c
, CHILD(n
, 8));
2596 _target
= ast_for_exprlist(c
, CHILD(n
, 1), Store
);
2599 if (asdl_seq_LEN(_target
) == 1)
2600 target
= asdl_seq_GET(_target
, 0);
2602 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2604 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2607 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2611 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2614 static excepthandler_ty
2615 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2617 /* except_clause: 'except' [test [',' test]] */
2618 REQ(exc
, except_clause
);
2621 if (NCH(exc
) == 1) {
2622 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2626 return excepthandler(NULL
, NULL
, suite_seq
, c
->c_arena
);
2628 else if (NCH(exc
) == 2) {
2630 asdl_seq
*suite_seq
;
2632 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2635 suite_seq
= ast_for_suite(c
, body
);
2639 return excepthandler(expression
, NULL
, suite_seq
, c
->c_arena
);
2641 else if (NCH(exc
) == 4) {
2642 asdl_seq
*suite_seq
;
2644 expr_ty e
= ast_for_expr(c
, CHILD(exc
, 3));
2647 if (!set_context(e
, Store
, CHILD(exc
, 3)))
2649 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2652 suite_seq
= ast_for_suite(c
, body
);
2656 return excepthandler(expression
, e
, suite_seq
, c
->c_arena
);
2659 PyErr_Format(PyExc_SystemError
,
2660 "wrong number of children for 'except' clause: %d",
2666 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2668 const int nch
= NCH(n
);
2669 int n_except
= (nch
- 3)/3;
2670 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2674 body
= ast_for_suite(c
, CHILD(n
, 2));
2678 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2679 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2680 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2681 /* we can assume it's an "else",
2682 because nch >= 9 for try-else-finally and
2683 it would otherwise have a type of except_clause */
2684 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2690 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2691 if (finally
== NULL
)
2696 /* we can assume it's an "else",
2697 otherwise it would have a type of except_clause */
2698 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2704 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2705 ast_error(n
, "malformed 'try' statement");
2712 /* process except statements to create a try ... except */
2713 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2714 if (handlers
== NULL
)
2717 for (i
= 0; i
< n_except
; i
++) {
2718 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2719 CHILD(n
, 5 + i
* 3));
2722 asdl_seq_SET(handlers
, i
, e
);
2725 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2729 /* if a 'finally' is present too, we nest the TryExcept within a
2730 TryFinally to emulate try ... except ... finally */
2731 body
= asdl_seq_new(1, c
->c_arena
);
2734 asdl_seq_SET(body
, 0, except_st
);
2737 /* must be a try ... finally (except clauses are in body, if any exist) */
2738 assert(finally
!= NULL
);
2739 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2743 ast_for_with_var(struct compiling
*c
, const node
*n
)
2746 if (strcmp(STR(CHILD(n
, 0)), "as") != 0) {
2747 ast_error(n
, "expected \"with [expr] as [var]\"");
2750 return ast_for_expr(c
, CHILD(n
, 1));
2753 /* with_stmt: 'with' test [ with_var ] ':' suite */
2755 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2757 expr_ty context_expr
, optional_vars
= NULL
;
2758 int suite_index
= 3; /* skip 'with', test, and ':' */
2759 asdl_seq
*suite_seq
;
2761 assert(TYPE(n
) == with_stmt
);
2762 context_expr
= ast_for_expr(c
, CHILD(n
, 1));
2763 if (TYPE(CHILD(n
, 2)) == with_var
) {
2764 optional_vars
= ast_for_with_var(c
, CHILD(n
, 2));
2766 if (!optional_vars
) {
2769 if (!set_context(optional_vars
, Store
, n
)) {
2775 suite_seq
= ast_for_suite(c
, CHILD(n
, suite_index
));
2779 return With(context_expr
, optional_vars
, suite_seq
, LINENO(n
),
2780 n
->n_col_offset
, c
->c_arena
);
2784 ast_for_classdef(struct compiling
*c
, const node
*n
)
2786 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2787 asdl_seq
*bases
, *s
;
2791 if (!strcmp(STR(CHILD(n
, 1)), "None")) {
2792 ast_error(n
, "assignment to None");
2797 s
= ast_for_suite(c
, CHILD(n
, 3));
2800 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
), n
->n_col_offset
,
2803 /* check for empty base list */
2804 if (TYPE(CHILD(n
,3)) == RPAR
) {
2805 s
= ast_for_suite(c
, CHILD(n
,5));
2808 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), NULL
, s
, LINENO(n
), n
->n_col_offset
,
2812 /* else handle the base class list */
2813 bases
= ast_for_class_bases(c
, CHILD(n
, 3));
2817 s
= ast_for_suite(c
, CHILD(n
, 6));
2820 return ClassDef(NEW_IDENTIFIER(CHILD(n
, 1)), bases
, s
, LINENO(n
), n
->n_col_offset
,
2825 ast_for_stmt(struct compiling
*c
, const node
*n
)
2827 if (TYPE(n
) == stmt
) {
2828 assert(NCH(n
) == 1);
2831 if (TYPE(n
) == simple_stmt
) {
2832 assert(num_stmts(n
) == 1);
2835 if (TYPE(n
) == small_stmt
) {
2838 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
2839 | flow_stmt | import_stmt | global_stmt | exec_stmt
2844 return ast_for_expr_stmt(c
, n
);
2846 return ast_for_print_stmt(c
, n
);
2848 return ast_for_del_stmt(c
, n
);
2850 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2852 return ast_for_flow_stmt(c
, n
);
2854 return ast_for_import_stmt(c
, n
);
2856 return ast_for_global_stmt(c
, n
);
2858 return ast_for_exec_stmt(c
, n
);
2860 return ast_for_assert_stmt(c
, n
);
2862 PyErr_Format(PyExc_SystemError
,
2863 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2869 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
2870 | funcdef | classdef
2872 node
*ch
= CHILD(n
, 0);
2873 REQ(n
, compound_stmt
);
2876 return ast_for_if_stmt(c
, ch
);
2878 return ast_for_while_stmt(c
, ch
);
2880 return ast_for_for_stmt(c
, ch
);
2882 return ast_for_try_stmt(c
, ch
);
2884 return ast_for_with_stmt(c
, ch
);
2886 return ast_for_funcdef(c
, ch
);
2888 return ast_for_classdef(c
, ch
);
2890 PyErr_Format(PyExc_SystemError
,
2891 "unhandled small_stmt: TYPE=%d NCH=%d\n",
2899 parsenumber(const char *s
)
2904 #ifndef WITHOUT_COMPLEX
2910 end
= s
+ strlen(s
) - 1;
2911 #ifndef WITHOUT_COMPLEX
2912 imflag
= *end
== 'j' || *end
== 'J';
2914 if (*end
== 'l' || *end
== 'L')
2915 return PyLong_FromString((char *)s
, (char **)0, 0);
2917 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
2918 if (x
< 0 && errno
== 0) {
2919 return PyLong_FromString((char *)s
,
2925 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
2928 return PyLong_FromString((char *)s
, (char **)0, 0);
2929 return PyInt_FromLong(x
);
2931 /* XXX Huge floats may silently fail */
2932 #ifndef WITHOUT_COMPLEX
2935 PyFPE_START_PROTECT("atof", return 0)
2936 c
.imag
= PyOS_ascii_atof(s
);
2937 PyFPE_END_PROTECT(c
)
2938 return PyComplex_FromCComplex(c
);
2943 PyFPE_START_PROTECT("atof", return 0)
2944 dx
= PyOS_ascii_atof(s
);
2945 PyFPE_END_PROTECT(dx
)
2946 return PyFloat_FromDouble(dx
);
2951 decode_utf8(const char **sPtr
, const char *end
, char* encoding
)
2953 #ifndef Py_USING_UNICODE
2954 Py_FatalError("decode_utf8 should not be called in this build.");
2959 t
= s
= (char *)*sPtr
;
2960 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
2961 while (s
< end
&& (*s
& 0x80)) s
++;
2963 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
2966 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
2973 decode_unicode(const char *s
, size_t len
, int rawmode
, const char *encoding
)
2979 if (encoding
== NULL
) {
2982 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
2986 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
2987 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
2990 p
= buf
= PyString_AsString(u
);
3000 if (*s
& 0x80) { /* XXX inefficient */
3004 w
= decode_utf8(&s
, end
, "utf-16-be");
3009 r
= PyString_AsString(w
);
3010 rn
= PyString_Size(w
);
3011 assert(rn
% 2 == 0);
3012 for (i
= 0; i
< rn
; i
+= 2) {
3013 sprintf(p
, "\\u%02x%02x",
3027 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3029 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3034 /* s is a Python string literal, including the bracketing quote characters,
3035 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3036 * parsestr parses it, and returns the decoded Python string object.
3039 parsestr(const char *s
, const char *encoding
)
3042 int quote
= Py_CHARMASK(*s
);
3047 if (isalpha(quote
) || quote
== '_') {
3048 if (quote
== 'u' || quote
== 'U') {
3052 if (quote
== 'r' || quote
== 'R') {
3057 if (quote
!= '\'' && quote
!= '\"') {
3058 PyErr_BadInternalCall();
3063 if (len
> INT_MAX
) {
3064 PyErr_SetString(PyExc_OverflowError
,
3065 "string to parse is too long");
3068 if (s
[--len
] != quote
) {
3069 PyErr_BadInternalCall();
3072 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3075 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3076 PyErr_BadInternalCall();
3080 #ifdef Py_USING_UNICODE
3081 if (unicode
|| Py_UnicodeFlag
) {
3082 return decode_unicode(s
, len
, rawmode
, encoding
);
3085 need_encoding
= (encoding
!= NULL
&&
3086 strcmp(encoding
, "utf-8") != 0 &&
3087 strcmp(encoding
, "iso-8859-1") != 0);
3088 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3089 if (need_encoding
) {
3090 #ifndef Py_USING_UNICODE
3091 /* This should not happen - we never see any other
3093 Py_FatalError("cannot deal with encodings in this build.");
3095 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3098 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3103 return PyString_FromStringAndSize(s
, len
);
3107 return PyString_DecodeEscape(s
, len
, NULL
, unicode
,
3108 need_encoding
? encoding
: NULL
);
3111 /* Build a Python string object out of a STRING atom. This takes care of
3112 * compile-time literal catenation, calling parsestr() on each piece, and
3113 * pasting the intermediate results together.
3116 parsestrplus(struct compiling
*c
, const node
*n
)
3120 REQ(CHILD(n
, 0), STRING
);
3121 if ((v
= parsestr(STR(CHILD(n
, 0)), c
->c_encoding
)) != NULL
) {
3122 /* String literal concatenation */
3123 for (i
= 1; i
< NCH(n
); i
++) {
3125 s
= parsestr(STR(CHILD(n
, i
)), c
->c_encoding
);
3128 if (PyString_Check(v
) && PyString_Check(s
)) {
3129 PyString_ConcatAndDel(&v
, s
);
3133 #ifdef Py_USING_UNICODE
3135 PyObject
*temp
= PyUnicode_Concat(v
, s
);