2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
7 #include "Python-ast.h"
18 /* Data structure used internally */
20 char *c_encoding
; /* source encoding */
21 PyArena
*c_arena
; /* arena for allocating memeory */
22 const char *c_filename
; /* filename */
25 static asdl_seq
*seq_for_testlist(struct compiling
*, const node
*);
26 static expr_ty
ast_for_expr(struct compiling
*, const node
*);
27 static stmt_ty
ast_for_stmt(struct compiling
*, const node
*);
28 static asdl_seq
*ast_for_suite(struct compiling
*, const node
*);
29 static asdl_seq
*ast_for_exprlist(struct compiling
*, const node
*,
31 static expr_ty
ast_for_testlist(struct compiling
*, const node
*);
32 static stmt_ty
ast_for_classdef(struct compiling
*, const node
*, asdl_seq
*);
34 /* Note different signature for ast_for_call */
35 static expr_ty
ast_for_call(struct compiling
*, const node
*, expr_ty
);
37 static PyObject
*parsenumber(struct compiling
*, const char *);
38 static PyObject
*parsestr(struct compiling
*, const node
*n
, int *bytesmode
);
39 static PyObject
*parsestrplus(struct compiling
*, const node
*n
,
43 #define LINENO(n) ((n)->n_lineno)
47 #define COMP_LISTCOMP 1
48 #define COMP_SETCOMP 2
51 new_identifier(const char* n
, PyArena
*arena
)
53 PyObject
* id
= PyUnicode_DecodeUTF8(n
, strlen(n
), NULL
);
57 u
= PyUnicode_AS_UNICODE(id
);
58 /* Check whether there are non-ASCII characters in the
59 identifier; if so, normalize to NFKC. */
62 PyObject
*m
= PyImport_ImportModuleNoBlock("unicodedata");
66 id2
= PyObject_CallMethod(m
, "normalize", "sO", "NFKC", id
);
75 PyUnicode_InternInPlace(&id
);
76 PyArena_AddPyObject(arena
, id
);
80 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
82 /* This routine provides an invalid object for the syntax error.
83 The outermost routine must unpack this error and create the
84 proper object. We do this so that we don't have to pass
85 the filename to everything function.
87 XXX Maybe we should just pass the filename...
91 ast_error(const node
*n
, const char *errstr
)
93 PyObject
*u
= Py_BuildValue("zi", errstr
, LINENO(n
));
96 PyErr_SetObject(PyExc_SyntaxError
, u
);
102 ast_error_finish(const char *filename
)
104 PyObject
*type
, *value
, *tback
, *errstr
, *loc
, *tmp
;
107 assert(PyErr_Occurred());
108 if (!PyErr_ExceptionMatches(PyExc_SyntaxError
))
111 PyErr_Fetch(&type
, &value
, &tback
);
112 errstr
= PyTuple_GetItem(value
, 0);
116 lineno
= PyLong_AsLong(PyTuple_GetItem(value
, 1));
123 loc
= PyErr_ProgramText(filename
, lineno
);
128 tmp
= Py_BuildValue("(zlOO)", filename
, lineno
, Py_None
, loc
);
134 value
= PyTuple_Pack(2, errstr
, tmp
);
139 PyErr_Restore(type
, value
, tback
);
142 /* num_stmts() returns number of contained statements.
144 Use this routine to determine how big a sequence is needed for
145 the statements in a parse tree. Its raison d'etre is this bit of
148 stmt: simple_stmt | compound_stmt
149 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
151 A simple_stmt can contain multiple small_stmt elements joined
152 by semicolons. If the arg is a simple_stmt, the number of
153 small_stmt elements is returned.
157 num_stmts(const node
*n
)
164 if (TYPE(CHILD(n
, 0)) == NEWLINE
)
167 return num_stmts(CHILD(n
, 0));
170 for (i
= 0; i
< NCH(n
); i
++) {
172 if (TYPE(ch
) == stmt
)
177 return num_stmts(CHILD(n
, 0));
181 return NCH(n
) / 2; /* Divide by 2 to remove count of semi-colons */
184 return num_stmts(CHILD(n
, 0));
187 for (i
= 2; i
< (NCH(n
) - 1); i
++)
188 l
+= num_stmts(CHILD(n
, i
));
194 sprintf(buf
, "Non-statement found: %d %d",
203 /* Transform the CST rooted at node * to the appropriate AST
207 PyAST_FromNode(const node
*n
, PyCompilerFlags
*flags
, const char *filename
,
211 asdl_seq
*stmts
= NULL
;
216 if (flags
&& flags
->cf_flags
& PyCF_SOURCE_IS_UTF8
) {
217 c
.c_encoding
= "utf-8";
218 if (TYPE(n
) == encoding_decl
) {
220 ast_error(n
, "encoding declaration in Unicode string");
225 } else if (TYPE(n
) == encoding_decl
) {
226 c
.c_encoding
= STR(n
);
230 c
.c_encoding
= "utf-8";
233 c
.c_filename
= filename
;
238 stmts
= asdl_seq_new(num_stmts(n
), arena
);
241 for (i
= 0; i
< NCH(n
) - 1; i
++) {
243 if (TYPE(ch
) == NEWLINE
)
248 s
= ast_for_stmt(&c
, ch
);
251 asdl_seq_SET(stmts
, k
++, s
);
255 REQ(ch
, simple_stmt
);
256 for (j
= 0; j
< num
; j
++) {
257 s
= ast_for_stmt(&c
, CHILD(ch
, j
* 2));
260 asdl_seq_SET(stmts
, k
++, s
);
264 return Module(stmts
, arena
);
266 expr_ty testlist_ast
;
268 /* XXX Why not comp_for here? */
269 testlist_ast
= ast_for_testlist(&c
, CHILD(n
, 0));
272 return Expression(testlist_ast
, arena
);
275 if (TYPE(CHILD(n
, 0)) == NEWLINE
) {
276 stmts
= asdl_seq_new(1, arena
);
279 asdl_seq_SET(stmts
, 0, Pass(n
->n_lineno
, n
->n_col_offset
,
281 if (!asdl_seq_GET(stmts
, 0))
283 return Interactive(stmts
, arena
);
288 stmts
= asdl_seq_new(num
, arena
);
292 s
= ast_for_stmt(&c
, n
);
295 asdl_seq_SET(stmts
, 0, s
);
298 /* Only a simple_stmt can contain multiple statements. */
300 for (i
= 0; i
< NCH(n
); i
+= 2) {
301 if (TYPE(CHILD(n
, i
)) == NEWLINE
)
303 s
= ast_for_stmt(&c
, CHILD(n
, i
));
306 asdl_seq_SET(stmts
, i
/ 2, s
);
310 return Interactive(stmts
, arena
);
313 PyErr_Format(PyExc_SystemError
,
314 "invalid node %d for PyAST_FromNode", TYPE(n
));
318 ast_error_finish(filename
);
322 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
326 get_operator(const node
*n
)
352 return (operator_ty
)0;
356 static const char* FORBIDDEN
[] = {
365 forbidden_name(expr_ty e
, const node
*n
)
368 assert(PyUnicode_Check(e
->v
.Name
.id
));
369 for (p
= FORBIDDEN
; *p
; p
++) {
370 if (PyUnicode_CompareWithASCIIString(e
->v
.Name
.id
, *p
) == 0) {
371 ast_error(n
, "assignment to keyword");
378 /* Set the context ctx for expr_ty e, recursively traversing e.
380 Only sets context for expr kinds that "can appear in assignment context"
381 (according to ../Parser/Python.asdl). For other expr kinds, it sets
382 an appropriate syntax error and returns false.
386 set_context(struct compiling
*c
, expr_ty e
, expr_context_ty ctx
, const node
*n
)
389 /* If a particular expression type can't be used for assign / delete,
390 set expr_name to its name and an error message will be generated.
392 const char* expr_name
= NULL
;
394 /* The ast defines augmented store and load contexts, but the
395 implementation here doesn't actually use them. The code may be
396 a little more complex than necessary as a result. It also means
397 that expressions in an augmented assignment have a Store context.
398 Consider restructuring so that augmented assignment uses
401 assert(ctx
!= AugStore
&& ctx
!= AugLoad
);
405 e
->v
.Attribute
.ctx
= ctx
;
408 e
->v
.Subscript
.ctx
= ctx
;
411 e
->v
.Starred
.ctx
= ctx
;
412 if (!set_context(c
, e
->v
.Starred
.value
, ctx
, n
))
417 if (forbidden_name(e
, n
))
418 return 0; /* forbidden_name() calls ast_error() */
427 if (asdl_seq_LEN(e
->v
.Tuple
.elts
) == 0)
428 return ast_error(n
, "can't assign to ()");
429 e
->v
.Tuple
.ctx
= ctx
;
433 expr_name
= "lambda";
436 expr_name
= "function call";
441 expr_name
= "operator";
443 case GeneratorExp_kind
:
444 expr_name
= "generator expression";
447 expr_name
= "yield expression";
450 expr_name
= "list comprehension";
453 expr_name
= "set comprehension";
456 expr_name
= "dict comprehension";
462 expr_name
= "literal";
465 expr_name
= "Ellipsis";
468 expr_name
= "comparison";
471 expr_name
= "conditional expression";
474 PyErr_Format(PyExc_SystemError
,
475 "unexpected expression in assignment %d (line %d)",
479 /* Check for error string set by switch */
482 PyOS_snprintf(buf
, sizeof(buf
),
484 ctx
== Store
? "assign to" : "delete",
486 return ast_error(n
, buf
);
489 /* If the LHS is a list or tuple, we need to set the assignment
490 context for all the contained elements.
495 for (i
= 0; i
< asdl_seq_LEN(s
); i
++) {
496 if (!set_context(c
, (expr_ty
)asdl_seq_GET(s
, i
), ctx
, n
))
504 ast_for_augassign(struct compiling
*c
, const node
*n
)
514 if (STR(n
)[1] == '/')
531 if (STR(n
)[1] == '*')
536 PyErr_Format(PyExc_SystemError
, "invalid augassign: %s", STR(n
));
537 return (operator_ty
)0;
542 ast_for_comp_op(struct compiling
*c
, const node
*n
)
544 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
555 case EQEQUAL
: /* == */
564 if (strcmp(STR(n
), "in") == 0)
566 if (strcmp(STR(n
), "is") == 0)
569 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s",
574 else if (NCH(n
) == 2) {
575 /* handle "not in" and "is not" */
576 switch (TYPE(CHILD(n
, 0))) {
578 if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
580 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
583 PyErr_Format(PyExc_SystemError
, "invalid comp_op: %s %s",
584 STR(CHILD(n
, 0)), STR(CHILD(n
, 1)));
588 PyErr_Format(PyExc_SystemError
, "invalid comp_op: has %d children",
594 seq_for_testlist(struct compiling
*c
, const node
*n
)
596 /* testlist: test (',' test)* [','] */
600 assert(TYPE(n
) == testlist
|| TYPE(n
) == testlist_comp
);
602 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
606 for (i
= 0; i
< NCH(n
); i
+= 2) {
607 assert(TYPE(CHILD(n
, i
)) == test
|| TYPE(CHILD(n
, i
)) == test_nocond
);
609 expression
= ast_for_expr(c
, CHILD(n
, i
));
613 assert(i
/ 2 < seq
->size
);
614 asdl_seq_SET(seq
, i
/ 2, expression
);
620 compiler_arg(struct compiling
*c
, const node
*n
)
623 expr_ty annotation
= NULL
;
626 assert(TYPE(n
) == tfpdef
|| TYPE(n
) == vfpdef
);
628 name
= NEW_IDENTIFIER(ch
);
632 if (NCH(n
) == 3 && TYPE(CHILD(n
, 1)) == COLON
) {
633 annotation
= ast_for_expr(c
, CHILD(n
, 2));
638 return arg(name
, annotation
, c
->c_arena
);
640 result
= Tuple(args
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
641 if (!set_context(c
, result
, Store
, n
))
647 /* returns -1 if failed to handle keyword only arguments
648 returns new position to keep processing if successful
649 (',' tfpdef ['=' test])*
654 handle_keywordonly_args(struct compiling
*c
, const node
*n
, int start
,
655 asdl_seq
*kwonlyargs
, asdl_seq
*kwdefaults
)
659 expr_ty expression
, annotation
;
662 int j
= 0; /* index for kwdefaults and kwonlyargs */
664 if (kwonlyargs
== NULL
) {
665 ast_error(CHILD(n
, start
), "named arguments must follow bare *");
668 assert(kwdefaults
!= NULL
);
674 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
675 expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
676 asdl_seq_SET(kwdefaults
, j
, expression
);
677 i
+= 2; /* '=' and test */
679 else { /* setting NULL if no default value exists */
680 asdl_seq_SET(kwdefaults
, j
, NULL
);
683 /* ch is NAME ':' test */
684 annotation
= ast_for_expr(c
, CHILD(ch
, 2));
686 ast_error(ch
, "expected expression");
694 argname
= NEW_IDENTIFIER(ch
);
697 arg
= arg(argname
, annotation
, c
->c_arena
);
700 asdl_seq_SET(kwonlyargs
, j
++, arg
);
701 i
+= 2; /* the name and the comma */
706 ast_error(ch
, "unexpected node");
715 /* Create AST for argument list. */
718 ast_for_arguments(struct compiling
*c
, const node
*n
)
720 /* This function handles both typedargslist (function definition)
721 and varargslist (lambda definition).
723 parameters: '(' [typedargslist] ')'
724 typedargslist: ((tfpdef ['=' test] ',')*
725 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
727 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
728 tfpdef: NAME [':' test]
729 varargslist: ((vfpdef ['=' test] ',')*
730 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
732 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
735 int i
, j
, k
, nposargs
= 0, nkwonlyargs
= 0;
736 int nposdefaults
= 0, found_default
= 0;
737 asdl_seq
*posargs
, *posdefaults
, *kwonlyargs
, *kwdefaults
;
738 identifier vararg
= NULL
, kwarg
= NULL
;
740 expr_ty varargannotation
= NULL
, kwargannotation
= NULL
;
743 if (TYPE(n
) == parameters
) {
744 if (NCH(n
) == 2) /* () as argument list */
745 return arguments(NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
749 assert(TYPE(n
) == typedargslist
|| TYPE(n
) == varargslist
);
751 /* First count the number of positional args & defaults. The
752 variable i is the loop index for this for loop and the next.
753 The next loop picks up where the first leaves off.
755 for (i
= 0; i
< NCH(n
); i
++) {
757 if (TYPE(ch
) == STAR
) {
760 if (i
< NCH(n
) && /* skip argument following star */
761 (TYPE(CHILD(n
, i
)) == tfpdef
||
762 TYPE(CHILD(n
, i
)) == vfpdef
)) {
767 if (TYPE(ch
) == DOUBLESTAR
) break;
768 if (TYPE(ch
) == vfpdef
|| TYPE(ch
) == tfpdef
) nposargs
++;
769 if (TYPE(ch
) == EQUAL
) nposdefaults
++;
771 /* count the number of keyword only args &
772 defaults for keyword only args */
773 for ( ; i
< NCH(n
); ++i
) {
775 if (TYPE(ch
) == DOUBLESTAR
) break;
776 if (TYPE(ch
) == tfpdef
|| TYPE(ch
) == vfpdef
) nkwonlyargs
++;
778 posargs
= (nposargs
? asdl_seq_new(nposargs
, c
->c_arena
) : NULL
);
779 if (!posargs
&& nposargs
)
781 kwonlyargs
= (nkwonlyargs
?
782 asdl_seq_new(nkwonlyargs
, c
->c_arena
) : NULL
);
783 if (!kwonlyargs
&& nkwonlyargs
)
785 posdefaults
= (nposdefaults
?
786 asdl_seq_new(nposdefaults
, c
->c_arena
) : NULL
);
787 if (!posdefaults
&& nposdefaults
)
789 /* The length of kwonlyargs and kwdefaults are same
790 since we set NULL as default for keyword only argument w/o default
791 - we have sequence data structure, but no dictionary */
792 kwdefaults
= (nkwonlyargs
?
793 asdl_seq_new(nkwonlyargs
, c
->c_arena
) : NULL
);
794 if (!kwdefaults
&& nkwonlyargs
)
797 if (nposargs
+ nkwonlyargs
> 255) {
798 ast_error(n
, "more than 255 arguments");
802 /* tfpdef: NAME [':' test]
806 j
= 0; /* index for defaults */
807 k
= 0; /* index for args */
813 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
814 anything other than EQUAL or a comma? */
815 /* XXX Should NCH(n) check be made a separate check? */
816 if (i
+ 1 < NCH(n
) && TYPE(CHILD(n
, i
+ 1)) == EQUAL
) {
817 expr_ty expression
= ast_for_expr(c
, CHILD(n
, i
+ 2));
820 assert(posdefaults
!= NULL
);
821 asdl_seq_SET(posdefaults
, j
++, expression
);
825 else if (found_default
) {
827 "non-default argument follows default argument");
830 arg
= compiler_arg(c
, ch
);
833 asdl_seq_SET(posargs
, k
++, arg
);
834 i
+= 2; /* the name and the comma */
838 ast_error(CHILD(n
, i
),
839 "named arguments must follow bare *");
842 ch
= CHILD(n
, i
+1); /* tfpdef or COMMA */
843 if (TYPE(ch
) == COMMA
) {
845 i
+= 2; /* now follows keyword only arguments */
846 res
= handle_keywordonly_args(c
, n
, i
,
847 kwonlyargs
, kwdefaults
);
848 if (res
== -1) goto error
;
849 i
= res
; /* res has new position to process */
852 vararg
= NEW_IDENTIFIER(CHILD(ch
, 0));
856 /* there is an annotation on the vararg */
857 varargannotation
= ast_for_expr(c
, CHILD(ch
, 2));
860 if (i
< NCH(n
) && (TYPE(CHILD(n
, i
)) == tfpdef
861 || TYPE(CHILD(n
, i
)) == vfpdef
)) {
863 res
= handle_keywordonly_args(c
, n
, i
,
864 kwonlyargs
, kwdefaults
);
865 if (res
== -1) goto error
;
866 i
= res
; /* res has new position to process */
871 ch
= CHILD(n
, i
+1); /* tfpdef */
872 assert(TYPE(ch
) == tfpdef
|| TYPE(ch
) == vfpdef
);
873 kwarg
= NEW_IDENTIFIER(CHILD(ch
, 0));
875 /* there is an annotation on the kwarg */
876 kwargannotation
= ast_for_expr(c
, CHILD(ch
, 2));
883 PyErr_Format(PyExc_SystemError
,
884 "unexpected node in varargslist: %d @ %d",
889 return arguments(posargs
, vararg
, varargannotation
, kwonlyargs
, kwarg
,
890 kwargannotation
, posdefaults
, kwdefaults
, c
->c_arena
);
898 ast_for_dotted_name(struct compiling
*c
, const node
*n
)
902 int lineno
, col_offset
;
908 col_offset
= n
->n_col_offset
;
910 id
= NEW_IDENTIFIER(CHILD(n
, 0));
913 e
= Name(id
, Load
, lineno
, col_offset
, c
->c_arena
);
917 for (i
= 2; i
< NCH(n
); i
+=2) {
918 id
= NEW_IDENTIFIER(CHILD(n
, i
));
921 e
= Attribute(e
, id
, Load
, lineno
, col_offset
, c
->c_arena
);
930 ast_for_decorator(struct compiling
*c
, const node
*n
)
932 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
937 REQ(CHILD(n
, 0), AT
);
938 REQ(RCHILD(n
, -1), NEWLINE
);
940 name_expr
= ast_for_dotted_name(c
, CHILD(n
, 1));
944 if (NCH(n
) == 3) { /* No arguments */
948 else if (NCH(n
) == 5) { /* Call with no arguments */
949 d
= Call(name_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
950 n
->n_col_offset
, c
->c_arena
);
956 d
= ast_for_call(c
, CHILD(n
, 3), name_expr
);
966 ast_for_decorators(struct compiling
*c
, const node
*n
)
968 asdl_seq
* decorator_seq
;
973 decorator_seq
= asdl_seq_new(NCH(n
), c
->c_arena
);
977 for (i
= 0; i
< NCH(n
); i
++) {
978 d
= ast_for_decorator(c
, CHILD(n
, i
));
981 asdl_seq_SET(decorator_seq
, i
, d
);
983 return decorator_seq
;
987 ast_for_funcdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
989 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
993 expr_ty returns
= NULL
;
998 name
= NEW_IDENTIFIER(CHILD(n
, name_i
));
1001 args
= ast_for_arguments(c
, CHILD(n
, name_i
+ 1));
1004 if (TYPE(CHILD(n
, name_i
+2)) == RARROW
) {
1005 returns
= ast_for_expr(c
, CHILD(n
, name_i
+ 3));
1010 body
= ast_for_suite(c
, CHILD(n
, name_i
+ 3));
1014 return FunctionDef(name
, args
, body
, decorator_seq
, returns
, LINENO(n
),
1015 n
->n_col_offset
, c
->c_arena
);
1019 ast_for_decorated(struct compiling
*c
, const node
*n
)
1021 /* decorated: decorators (classdef | funcdef) */
1022 stmt_ty thing
= NULL
;
1023 asdl_seq
*decorator_seq
= NULL
;
1027 decorator_seq
= ast_for_decorators(c
, CHILD(n
, 0));
1031 assert(TYPE(CHILD(n
, 1)) == funcdef
||
1032 TYPE(CHILD(n
, 1)) == classdef
);
1034 if (TYPE(CHILD(n
, 1)) == funcdef
) {
1035 thing
= ast_for_funcdef(c
, CHILD(n
, 1), decorator_seq
);
1036 } else if (TYPE(CHILD(n
, 1)) == classdef
) {
1037 thing
= ast_for_classdef(c
, CHILD(n
, 1), decorator_seq
);
1039 /* we count the decorators in when talking about the class' or
1040 * function's line number */
1042 thing
->lineno
= LINENO(n
);
1043 thing
->col_offset
= n
->n_col_offset
;
1049 ast_for_lambdef(struct compiling
*c
, const node
*n
)
1051 /* lambdef: 'lambda' [varargslist] ':' test
1052 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
1057 args
= arguments(NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
1061 expression
= ast_for_expr(c
, CHILD(n
, 2));
1066 args
= ast_for_arguments(c
, CHILD(n
, 1));
1069 expression
= ast_for_expr(c
, CHILD(n
, 3));
1074 return Lambda(args
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1078 ast_for_ifexpr(struct compiling
*c
, const node
*n
)
1080 /* test: or_test 'if' or_test 'else' test */
1081 expr_ty expression
, body
, orelse
;
1083 assert(NCH(n
) == 5);
1084 body
= ast_for_expr(c
, CHILD(n
, 0));
1087 expression
= ast_for_expr(c
, CHILD(n
, 2));
1090 orelse
= ast_for_expr(c
, CHILD(n
, 4));
1093 return IfExp(expression
, body
, orelse
, LINENO(n
), n
->n_col_offset
,
1098 Count the number of 'for' loops in a comprehension.
1100 Helper for ast_for_comprehension().
1104 count_comp_fors(struct compiling
*c
, const node
*n
)
1118 if (TYPE(n
) == comp_for
)
1119 goto count_comp_for
;
1120 else if (TYPE(n
) == comp_if
) {
1123 goto count_comp_iter
;
1129 /* Should never be reached */
1130 PyErr_SetString(PyExc_SystemError
,
1131 "logic error in count_comp_fors");
1135 /* Count the number of 'if' statements in a comprehension.
1137 Helper for ast_for_comprehension().
1141 count_comp_ifs(struct compiling
*c
, const node
*n
)
1147 if (TYPE(CHILD(n
, 0)) == comp_for
)
1159 ast_for_comprehension(struct compiling
*c
, const node
*n
)
1164 n_fors
= count_comp_fors(c
, n
);
1168 comps
= asdl_seq_new(n_fors
, c
->c_arena
);
1172 for (i
= 0; i
< n_fors
; i
++) {
1173 comprehension_ty comp
;
1180 for_ch
= CHILD(n
, 1);
1181 t
= ast_for_exprlist(c
, for_ch
, Store
);
1184 expression
= ast_for_expr(c
, CHILD(n
, 3));
1188 /* Check the # of children rather than the length of t, since
1189 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1190 if (NCH(for_ch
) == 1)
1191 comp
= comprehension((expr_ty
)asdl_seq_GET(t
, 0), expression
,
1194 comp
= comprehension(Tuple(t
, Store
, LINENO(n
), n
->n_col_offset
,
1196 expression
, NULL
, c
->c_arena
);
1206 n_ifs
= count_comp_ifs(c
, n
);
1210 ifs
= asdl_seq_new(n_ifs
, c
->c_arena
);
1214 for (j
= 0; j
< n_ifs
; j
++) {
1219 expression
= ast_for_expr(c
, CHILD(n
, 1));
1222 asdl_seq_SET(ifs
, j
, expression
);
1226 /* on exit, must guarantee that n is a comp_for */
1227 if (TYPE(n
) == comp_iter
)
1231 asdl_seq_SET(comps
, i
, comp
);
1237 ast_for_itercomp(struct compiling
*c
, const node
*n
, int type
)
1239 /* testlist_comp: test ( comp_for | (',' test)* [','] )
1240 argument: [test '='] test [comp_for] # Really [keyword '='] test */
1246 elt
= ast_for_expr(c
, CHILD(n
, 0));
1250 comps
= ast_for_comprehension(c
, CHILD(n
, 1));
1254 if (type
== COMP_GENEXP
)
1255 return GeneratorExp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1256 else if (type
== COMP_LISTCOMP
)
1257 return ListComp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1258 else if (type
== COMP_SETCOMP
)
1259 return SetComp(elt
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1261 /* Should never happen */
1266 ast_for_dictcomp(struct compiling
*c
, const node
*n
)
1272 REQ(CHILD(n
, 1), COLON
);
1274 key
= ast_for_expr(c
, CHILD(n
, 0));
1278 value
= ast_for_expr(c
, CHILD(n
, 2));
1282 comps
= ast_for_comprehension(c
, CHILD(n
, 3));
1286 return DictComp(key
, value
, comps
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1290 ast_for_genexp(struct compiling
*c
, const node
*n
)
1292 assert(TYPE(n
) == (testlist_comp
) || TYPE(n
) == (argument
));
1293 return ast_for_itercomp(c
, n
, COMP_GENEXP
);
1297 ast_for_listcomp(struct compiling
*c
, const node
*n
)
1299 assert(TYPE(n
) == (testlist_comp
));
1300 return ast_for_itercomp(c
, n
, COMP_LISTCOMP
);
1304 ast_for_setcomp(struct compiling
*c
, const node
*n
)
1306 assert(TYPE(n
) == (dictorsetmaker
));
1307 return ast_for_itercomp(c
, n
, COMP_SETCOMP
);
1312 ast_for_atom(struct compiling
*c
, const node
*n
)
1314 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1315 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
1316 | '...' | 'None' | 'True' | 'False'
1318 node
*ch
= CHILD(n
, 0);
1323 /* All names start in Load context, but may later be
1325 PyObject
*name
= NEW_IDENTIFIER(ch
);
1328 return Name(name
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1331 PyObject
*str
= parsestrplus(c
, n
, &bytesmode
);
1333 if (PyErr_ExceptionMatches(PyExc_UnicodeError
)) {
1334 PyObject
*type
, *value
, *tback
, *errstr
;
1335 PyErr_Fetch(&type
, &value
, &tback
);
1336 errstr
= PyObject_Str(value
);
1340 s
= _PyUnicode_AsString(errstr
);
1341 PyOS_snprintf(buf
, sizeof(buf
), "(unicode error) %s", s
);
1345 ast_error(n
, "(unicode error) unknown error");
1353 PyArena_AddPyObject(c
->c_arena
, str
);
1355 return Bytes(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1357 return Str(str
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1360 PyObject
*pynum
= parsenumber(c
, STR(ch
));
1364 PyArena_AddPyObject(c
->c_arena
, pynum
);
1365 return Num(pynum
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1367 case ELLIPSIS
: /* Ellipsis */
1368 return Ellipsis(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1369 case LPAR
: /* some parenthesized expressions */
1372 if (TYPE(ch
) == RPAR
)
1373 return Tuple(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1375 if (TYPE(ch
) == yield_expr
)
1376 return ast_for_expr(c
, ch
);
1378 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
1379 if ((NCH(ch
) > 1) && (TYPE(CHILD(ch
, 1)) == comp_for
))
1380 return ast_for_genexp(c
, ch
);
1382 return ast_for_testlist(c
, ch
);
1383 case LSQB
: /* list (or list comprehension) */
1386 if (TYPE(ch
) == RSQB
)
1387 return List(NULL
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1389 REQ(ch
, testlist_comp
);
1390 if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1391 asdl_seq
*elts
= seq_for_testlist(c
, ch
);
1395 return List(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1398 return ast_for_listcomp(c
, ch
);
1400 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
1401 * test (gen_for | (',' test)* [',']) */
1403 asdl_seq
*keys
, *values
;
1406 if (TYPE(ch
) == RBRACE
) {
1407 /* it's an empty dict */
1408 return Dict(NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1409 } else if (NCH(ch
) == 1 || TYPE(CHILD(ch
, 1)) == COMMA
) {
1410 /* it's a simple set */
1412 size
= (NCH(ch
) + 1) / 2; /* +1 in case no trailing comma */
1413 elts
= asdl_seq_new(size
, c
->c_arena
);
1416 for (i
= 0; i
< NCH(ch
); i
+= 2) {
1418 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1421 asdl_seq_SET(elts
, i
/ 2, expression
);
1423 return Set(elts
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1424 } else if (TYPE(CHILD(ch
, 1)) == comp_for
) {
1425 /* it's a set comprehension */
1426 return ast_for_setcomp(c
, ch
);
1427 } else if (NCH(ch
) > 3 && TYPE(CHILD(ch
, 3)) == comp_for
) {
1428 return ast_for_dictcomp(c
, ch
);
1431 size
= (NCH(ch
) + 1) / 4; /* +1 in case no trailing comma */
1432 keys
= asdl_seq_new(size
, c
->c_arena
);
1436 values
= asdl_seq_new(size
, c
->c_arena
);
1440 for (i
= 0; i
< NCH(ch
); i
+= 4) {
1443 expression
= ast_for_expr(c
, CHILD(ch
, i
));
1447 asdl_seq_SET(keys
, i
/ 4, expression
);
1449 expression
= ast_for_expr(c
, CHILD(ch
, i
+ 2));
1453 asdl_seq_SET(values
, i
/ 4, expression
);
1455 return Dict(keys
, values
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1459 PyErr_Format(PyExc_SystemError
, "unhandled atom %d", TYPE(ch
));
1465 ast_for_slice(struct compiling
*c
, const node
*n
)
1468 expr_ty lower
= NULL
, upper
= NULL
, step
= NULL
;
1473 subscript: test | [test] ':' [test] [sliceop]
1477 if (NCH(n
) == 1 && TYPE(ch
) == test
) {
1478 /* 'step' variable hold no significance in terms of being used over
1480 step
= ast_for_expr(c
, ch
);
1484 return Index(step
, c
->c_arena
);
1487 if (TYPE(ch
) == test
) {
1488 lower
= ast_for_expr(c
, ch
);
1493 /* If there's an upper bound it's in the second or third position. */
1494 if (TYPE(ch
) == COLON
) {
1496 node
*n2
= CHILD(n
, 1);
1498 if (TYPE(n2
) == test
) {
1499 upper
= ast_for_expr(c
, n2
);
1504 } else if (NCH(n
) > 2) {
1505 node
*n2
= CHILD(n
, 2);
1507 if (TYPE(n2
) == test
) {
1508 upper
= ast_for_expr(c
, n2
);
1514 ch
= CHILD(n
, NCH(n
) - 1);
1515 if (TYPE(ch
) == sliceop
) {
1517 /* No expression, so step is None */
1519 step
= Name(new_identifier("None", c
->c_arena
), Load
,
1520 LINENO(ch
), ch
->n_col_offset
, c
->c_arena
);
1525 if (TYPE(ch
) == test
) {
1526 step
= ast_for_expr(c
, ch
);
1533 return Slice(lower
, upper
, step
, c
->c_arena
);
1537 ast_for_binop(struct compiling
*c
, const node
*n
)
1539 /* Must account for a sequence of expressions.
1540 How should A op B op C by represented?
1541 BinOp(BinOp(A, op, B), op, C).
1545 expr_ty expr1
, expr2
, result
;
1546 operator_ty newoperator
;
1548 expr1
= ast_for_expr(c
, CHILD(n
, 0));
1552 expr2
= ast_for_expr(c
, CHILD(n
, 2));
1556 newoperator
= get_operator(CHILD(n
, 1));
1560 result
= BinOp(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
,
1565 nops
= (NCH(n
) - 1) / 2;
1566 for (i
= 1; i
< nops
; i
++) {
1567 expr_ty tmp_result
, tmp
;
1568 const node
* next_oper
= CHILD(n
, i
* 2 + 1);
1570 newoperator
= get_operator(next_oper
);
1574 tmp
= ast_for_expr(c
, CHILD(n
, i
* 2 + 2));
1578 tmp_result
= BinOp(result
, newoperator
, tmp
,
1579 LINENO(next_oper
), next_oper
->n_col_offset
,
1583 result
= tmp_result
;
1589 ast_for_trailer(struct compiling
*c
, const node
*n
, expr_ty left_expr
)
1591 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1592 subscriptlist: subscript (',' subscript)* [',']
1593 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1596 if (TYPE(CHILD(n
, 0)) == LPAR
) {
1598 return Call(left_expr
, NULL
, NULL
, NULL
, NULL
, LINENO(n
),
1599 n
->n_col_offset
, c
->c_arena
);
1601 return ast_for_call(c
, CHILD(n
, 1), left_expr
);
1603 else if (TYPE(CHILD(n
, 0)) == DOT
) {
1604 PyObject
*attr_id
= NEW_IDENTIFIER(CHILD(n
, 1));
1607 return Attribute(left_expr
, attr_id
, Load
,
1608 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1611 REQ(CHILD(n
, 0), LSQB
);
1612 REQ(CHILD(n
, 2), RSQB
);
1615 slice_ty slc
= ast_for_slice(c
, CHILD(n
, 0));
1618 return Subscript(left_expr
, slc
, Load
, LINENO(n
), n
->n_col_offset
,
1622 /* The grammar is ambiguous here. The ambiguity is resolved
1623 by treating the sequence as a tuple literal if there are
1630 asdl_seq
*slices
, *elts
;
1631 slices
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1634 for (j
= 0; j
< NCH(n
); j
+= 2) {
1635 slc
= ast_for_slice(c
, CHILD(n
, j
));
1638 if (slc
->kind
!= Index_kind
)
1640 asdl_seq_SET(slices
, j
/ 2, slc
);
1643 return Subscript(left_expr
, ExtSlice(slices
, c
->c_arena
),
1644 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1646 /* extract Index values and put them in a Tuple */
1647 elts
= asdl_seq_new(asdl_seq_LEN(slices
), c
->c_arena
);
1650 for (j
= 0; j
< asdl_seq_LEN(slices
); ++j
) {
1651 slc
= (slice_ty
)asdl_seq_GET(slices
, j
);
1652 assert(slc
->kind
== Index_kind
&& slc
->v
.Index
.value
);
1653 asdl_seq_SET(elts
, j
, slc
->v
.Index
.value
);
1655 e
= Tuple(elts
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1658 return Subscript(left_expr
, Index(e
, c
->c_arena
),
1659 Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1665 ast_for_factor(struct compiling
*c
, const node
*n
)
1667 node
*pfactor
, *ppower
, *patom
, *pnum
;
1670 /* If the unary - operator is applied to a constant, don't generate
1671 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1672 constant. The peephole optimizer already does something like
1673 this but it doesn't handle the case where the constant is
1674 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1677 if (TYPE(CHILD(n
, 0)) == MINUS
1679 && TYPE((pfactor
= CHILD(n
, 1))) == factor
1680 && NCH(pfactor
) == 1
1681 && TYPE((ppower
= CHILD(pfactor
, 0))) == power
1683 && TYPE((patom
= CHILD(ppower
, 0))) == atom
1684 && TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
) {
1685 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
1689 strcpy(s
+ 1, STR(pnum
));
1690 PyObject_FREE(STR(pnum
));
1692 return ast_for_atom(c
, patom
);
1695 expression
= ast_for_expr(c
, CHILD(n
, 1));
1699 switch (TYPE(CHILD(n
, 0))) {
1701 return UnaryOp(UAdd
, expression
, LINENO(n
), n
->n_col_offset
,
1704 return UnaryOp(USub
, expression
, LINENO(n
), n
->n_col_offset
,
1707 return UnaryOp(Invert
, expression
, LINENO(n
),
1708 n
->n_col_offset
, c
->c_arena
);
1710 PyErr_Format(PyExc_SystemError
, "unhandled factor: %d",
1716 ast_for_power(struct compiling
*c
, const node
*n
)
1718 /* power: atom trailer* ('**' factor)*
1723 e
= ast_for_atom(c
, CHILD(n
, 0));
1728 for (i
= 1; i
< NCH(n
); i
++) {
1729 node
*ch
= CHILD(n
, i
);
1730 if (TYPE(ch
) != trailer
)
1732 tmp
= ast_for_trailer(c
, ch
, e
);
1735 tmp
->lineno
= e
->lineno
;
1736 tmp
->col_offset
= e
->col_offset
;
1739 if (TYPE(CHILD(n
, NCH(n
) - 1)) == factor
) {
1740 expr_ty f
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 1));
1743 tmp
= BinOp(e
, Pow
, f
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1752 ast_for_starred(struct compiling
*c
, const node
*n
)
1757 tmp
= ast_for_expr(c
, CHILD(n
, 1));
1761 /* The Load context is changed later. */
1762 return Starred(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1766 /* Do not name a variable 'expr'! Will cause a compile error.
1770 ast_for_expr(struct compiling
*c
, const node
*n
)
1772 /* handle the full range of simple expressions
1773 test: or_test ['if' or_test 'else' test] | lambdef
1774 test_nocond: or_test | lambdef_nocond
1775 or_test: and_test ('or' and_test)*
1776 and_test: not_test ('and' not_test)*
1777 not_test: 'not' not_test | comparison
1778 comparison: expr (comp_op expr)*
1779 expr: xor_expr ('|' xor_expr)*
1780 xor_expr: and_expr ('^' and_expr)*
1781 and_expr: shift_expr ('&' shift_expr)*
1782 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1783 arith_expr: term (('+'|'-') term)*
1784 term: factor (('*'|'/'|'%'|'//') factor)*
1785 factor: ('+'|'-'|'~') factor | power
1786 power: atom trailer* ('**' factor)*
1796 if (TYPE(CHILD(n
, 0)) == lambdef
||
1797 TYPE(CHILD(n
, 0)) == lambdef_nocond
)
1798 return ast_for_lambdef(c
, CHILD(n
, 0));
1799 else if (NCH(n
) > 1)
1800 return ast_for_ifexpr(c
, n
);
1808 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
1811 for (i
= 0; i
< NCH(n
); i
+= 2) {
1812 expr_ty e
= ast_for_expr(c
, CHILD(n
, i
));
1815 asdl_seq_SET(seq
, i
/ 2, e
);
1817 if (!strcmp(STR(CHILD(n
, 1)), "and"))
1818 return BoolOp(And
, seq
, LINENO(n
), n
->n_col_offset
,
1820 assert(!strcmp(STR(CHILD(n
, 1)), "or"));
1821 return BoolOp(Or
, seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1828 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
1832 return UnaryOp(Not
, expression
, LINENO(n
), n
->n_col_offset
,
1844 ops
= asdl_int_seq_new(NCH(n
) / 2, c
->c_arena
);
1847 cmps
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
1851 for (i
= 1; i
< NCH(n
); i
+= 2) {
1852 cmpop_ty newoperator
;
1854 newoperator
= ast_for_comp_op(c
, CHILD(n
, i
));
1859 expression
= ast_for_expr(c
, CHILD(n
, i
+ 1));
1864 asdl_seq_SET(ops
, i
/ 2, newoperator
);
1865 asdl_seq_SET(cmps
, i
/ 2, expression
);
1867 expression
= ast_for_expr(c
, CHILD(n
, 0));
1872 return Compare(expression
, ops
, cmps
, LINENO(n
),
1873 n
->n_col_offset
, c
->c_arena
);
1878 if (TYPE(CHILD(n
, 0)) == STAR
) {
1879 return ast_for_starred(c
, n
);
1882 /* The next five cases all handle BinOps. The main body of code
1883 is the same in each case, but the switch turned inside out to
1884 reuse the code for each type of operator.
1896 return ast_for_binop(c
, n
);
1900 exp
= ast_for_testlist(c
, CHILD(n
, 1));
1904 return Yield(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
1911 return ast_for_factor(c
, n
);
1913 return ast_for_power(c
, n
);
1915 PyErr_Format(PyExc_SystemError
, "unhandled expr: %d", TYPE(n
));
1918 /* should never get here unless if error is set */
1923 ast_for_call(struct compiling
*c
, const node
*n
, expr_ty func
)
1926 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1928 argument: [test '='] test [comp_for] # Really [keyword '='] test
1931 int i
, nargs
, nkeywords
, ngens
;
1934 expr_ty vararg
= NULL
, kwarg
= NULL
;
1941 for (i
= 0; i
< NCH(n
); i
++) {
1942 node
*ch
= CHILD(n
, i
);
1943 if (TYPE(ch
) == argument
) {
1946 else if (TYPE(CHILD(ch
, 1)) == comp_for
)
1952 if (ngens
> 1 || (ngens
&& (nargs
|| nkeywords
))) {
1953 ast_error(n
, "Generator expression must be parenthesized "
1954 "if not sole argument");
1958 if (nargs
+ nkeywords
+ ngens
> 255) {
1959 ast_error(n
, "more than 255 arguments");
1963 args
= asdl_seq_new(nargs
+ ngens
, c
->c_arena
);
1966 keywords
= asdl_seq_new(nkeywords
, c
->c_arena
);
1971 for (i
= 0; i
< NCH(n
); i
++) {
1972 node
*ch
= CHILD(n
, i
);
1973 if (TYPE(ch
) == argument
) {
1977 ast_error(CHILD(ch
, 0),
1978 "non-keyword arg after keyword arg");
1982 ast_error(CHILD(ch
, 0),
1983 "only named arguments may follow *expression");
1986 e
= ast_for_expr(c
, CHILD(ch
, 0));
1989 asdl_seq_SET(args
, nargs
++, e
);
1991 else if (TYPE(CHILD(ch
, 1)) == comp_for
) {
1992 e
= ast_for_genexp(c
, ch
);
1995 asdl_seq_SET(args
, nargs
++, e
);
1999 identifier key
, tmp
;
2002 /* CHILD(ch, 0) is test, but must be an identifier? */
2003 e
= ast_for_expr(c
, CHILD(ch
, 0));
2006 /* f(lambda x: x[0] = 3) ends up getting parsed with
2007 * LHS test = lambda x: x[0], and RHS test = 3.
2008 * SF bug 132313 points out that complaining about a keyword
2009 * then is very confusing.
2011 if (e
->kind
== Lambda_kind
) {
2012 ast_error(CHILD(ch
, 0), "lambda cannot contain assignment");
2014 } else if (e
->kind
!= Name_kind
) {
2015 ast_error(CHILD(ch
, 0), "keyword can't be an expression");
2017 } else if (forbidden_name(e
, ch
)) {
2021 for (k
= 0; k
< nkeywords
; k
++) {
2022 tmp
= ((keyword_ty
)asdl_seq_GET(keywords
, k
))->arg
;
2023 if (!PyUnicode_Compare(tmp
, key
)) {
2024 ast_error(CHILD(ch
, 0), "keyword argument repeated");
2028 e
= ast_for_expr(c
, CHILD(ch
, 2));
2031 kw
= keyword(key
, e
, c
->c_arena
);
2034 asdl_seq_SET(keywords
, nkeywords
++, kw
);
2037 else if (TYPE(ch
) == STAR
) {
2038 vararg
= ast_for_expr(c
, CHILD(n
, i
+1));
2043 else if (TYPE(ch
) == DOUBLESTAR
) {
2044 kwarg
= ast_for_expr(c
, CHILD(n
, i
+1));
2051 return Call(func
, args
, keywords
, vararg
, kwarg
, func
->lineno
, func
->col_offset
, c
->c_arena
);
2055 ast_for_testlist(struct compiling
*c
, const node
* n
)
2057 /* testlist_comp: test (comp_for | (',' test)* [',']) */
2058 /* testlist: test (',' test)* [','] */
2059 /* testlist1: test (',' test)* */
2061 if (TYPE(n
) == testlist_comp
) {
2063 assert(TYPE(CHILD(n
, 1)) != comp_for
);
2066 assert(TYPE(n
) == testlist
||
2067 TYPE(n
) == testlist1
);
2070 return ast_for_expr(c
, CHILD(n
, 0));
2072 asdl_seq
*tmp
= seq_for_testlist(c
, n
);
2075 return Tuple(tmp
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2080 ast_for_expr_stmt(struct compiling
*c
, const node
*n
)
2083 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2084 | ('=' (yield_expr|testlist))*)
2085 testlist: test (',' test)* [',']
2086 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2087 | '<<=' | '>>=' | '**=' | '//='
2088 test: ... here starts the operator precendence dance
2092 expr_ty e
= ast_for_testlist(c
, CHILD(n
, 0));
2096 return Expr(e
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2098 else if (TYPE(CHILD(n
, 1)) == augassign
) {
2099 expr_ty expr1
, expr2
;
2100 operator_ty newoperator
;
2101 node
*ch
= CHILD(n
, 0);
2103 expr1
= ast_for_testlist(c
, ch
);
2106 if(!set_context(c
, expr1
, Store
, ch
))
2110 if (TYPE(ch
) == testlist
)
2111 expr2
= ast_for_testlist(c
, ch
);
2113 expr2
= ast_for_expr(c
, ch
);
2117 newoperator
= ast_for_augassign(c
, CHILD(n
, 1));
2121 return AugAssign(expr1
, newoperator
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2129 /* a normal assignment */
2130 REQ(CHILD(n
, 1), EQUAL
);
2131 targets
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2134 for (i
= 0; i
< NCH(n
) - 2; i
+= 2) {
2136 node
*ch
= CHILD(n
, i
);
2137 if (TYPE(ch
) == yield_expr
) {
2138 ast_error(ch
, "assignment to yield expression not possible");
2141 e
= ast_for_testlist(c
, ch
);
2143 /* set context to assign */
2147 if (!set_context(c
, e
, Store
, CHILD(n
, i
)))
2150 asdl_seq_SET(targets
, i
/ 2, e
);
2152 value
= CHILD(n
, NCH(n
) - 1);
2153 if (TYPE(value
) == testlist
)
2154 expression
= ast_for_testlist(c
, value
);
2156 expression
= ast_for_expr(c
, value
);
2159 return Assign(targets
, expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2164 ast_for_exprlist(struct compiling
*c
, const node
*n
, expr_context_ty context
)
2172 seq
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2175 for (i
= 0; i
< NCH(n
); i
+= 2) {
2176 e
= ast_for_expr(c
, CHILD(n
, i
));
2179 asdl_seq_SET(seq
, i
/ 2, e
);
2180 if (context
&& !set_context(c
, e
, context
, CHILD(n
, i
)))
2187 ast_for_del_stmt(struct compiling
*c
, const node
*n
)
2189 asdl_seq
*expr_list
;
2191 /* del_stmt: 'del' exprlist */
2194 expr_list
= ast_for_exprlist(c
, CHILD(n
, 1), Del
);
2197 return Delete(expr_list
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2201 ast_for_flow_stmt(struct compiling
*c
, const node
*n
)
2204 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2207 continue_stmt: 'continue'
2208 return_stmt: 'return' [testlist]
2209 yield_stmt: yield_expr
2210 yield_expr: 'yield' testlist
2211 raise_stmt: 'raise' [test [',' test [',' test]]]
2219 return Break(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2221 return Continue(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2222 case yield_stmt
: { /* will reduce to yield_expr */
2223 expr_ty exp
= ast_for_expr(c
, CHILD(ch
, 0));
2226 return Expr(exp
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2230 return Return(NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2232 expr_ty expression
= ast_for_testlist(c
, CHILD(ch
, 1));
2235 return Return(expression
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2239 return Raise(NULL
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2240 else if (NCH(ch
) >= 2) {
2241 expr_ty cause
= NULL
;
2242 expr_ty expression
= ast_for_expr(c
, CHILD(ch
, 1));
2246 cause
= ast_for_expr(c
, CHILD(ch
, 3));
2250 return Raise(expression
, cause
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2253 PyErr_Format(PyExc_SystemError
,
2254 "unexpected flow_stmt: %d", TYPE(ch
));
2258 PyErr_SetString(PyExc_SystemError
, "unhandled flow statement");
2263 alias_for_import_name(struct compiling
*c
, const node
*n
)
2266 import_as_name: NAME ['as' NAME]
2267 dotted_as_name: dotted_name ['as' NAME]
2268 dotted_name: NAME ('.' NAME)*
2270 PyObject
*str
, *name
;
2274 case import_as_name
:
2277 str
= NEW_IDENTIFIER(CHILD(n
, 2));
2281 name
= NEW_IDENTIFIER(CHILD(n
, 0));
2284 return alias(name
, str
, c
->c_arena
);
2285 case dotted_as_name
:
2291 alias_ty a
= alias_for_import_name(c
, CHILD(n
, 0));
2295 a
->asname
= NEW_IDENTIFIER(CHILD(n
, 2));
2303 name
= NEW_IDENTIFIER(CHILD(n
, 0));
2306 return alias(name
, NULL
, c
->c_arena
);
2309 /* Create a string of the form "a.b.c" */
2316 for (i
= 0; i
< NCH(n
); i
+= 2)
2317 /* length of string plus one for the dot */
2318 len
+= strlen(STR(CHILD(n
, i
))) + 1;
2319 len
--; /* the last name doesn't have a dot */
2320 str
= PyBytes_FromStringAndSize(NULL
, len
);
2323 s
= PyBytes_AS_STRING(str
);
2326 for (i
= 0; i
< NCH(n
); i
+= 2) {
2327 char *sch
= STR(CHILD(n
, i
));
2328 strcpy(s
, STR(CHILD(n
, i
)));
2334 uni
= PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str
),
2335 PyBytes_GET_SIZE(str
),
2341 PyUnicode_InternInPlace(&str
);
2342 PyArena_AddPyObject(c
->c_arena
, str
);
2343 return alias(str
, NULL
, c
->c_arena
);
2347 str
= PyUnicode_InternFromString("*");
2348 PyArena_AddPyObject(c
->c_arena
, str
);
2349 return alias(str
, NULL
, c
->c_arena
);
2351 PyErr_Format(PyExc_SystemError
,
2352 "unexpected import name: %d", TYPE(n
));
2356 PyErr_SetString(PyExc_SystemError
, "unhandled import name condition");
2361 ast_for_import_stmt(struct compiling
*c
, const node
*n
)
2364 import_stmt: import_name | import_from
2365 import_name: 'import' dotted_as_names
2366 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
2367 'import' ('*' | '(' import_as_names ')' | import_as_names)
2374 REQ(n
, import_stmt
);
2376 col_offset
= n
->n_col_offset
;
2378 if (TYPE(n
) == import_name
) {
2380 REQ(n
, dotted_as_names
);
2381 aliases
= asdl_seq_new((NCH(n
) + 1) / 2, c
->c_arena
);
2384 for (i
= 0; i
< NCH(n
); i
+= 2) {
2385 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2388 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2390 return Import(aliases
, lineno
, col_offset
, c
->c_arena
);
2392 else if (TYPE(n
) == import_from
) {
2395 alias_ty mod
= NULL
;
2398 /* Count the number of dots (for relative imports) and check for the
2399 optional module name */
2400 for (idx
= 1; idx
< NCH(n
); idx
++) {
2401 if (TYPE(CHILD(n
, idx
)) == dotted_name
) {
2402 mod
= alias_for_import_name(c
, CHILD(n
, idx
));
2405 } else if (TYPE(CHILD(n
, idx
)) == ELLIPSIS
) {
2406 /* three consecutive dots are tokenized as one ELLIPSIS */
2409 } else if (TYPE(CHILD(n
, idx
)) != DOT
) {
2414 idx
++; /* skip over the 'import' keyword */
2415 switch (TYPE(CHILD(n
, idx
))) {
2417 /* from ... import * */
2422 /* from ... import (x, y, z) */
2423 n
= CHILD(n
, idx
+ 1);
2424 n_children
= NCH(n
);
2426 case import_as_names
:
2427 /* from ... import x, y, z */
2429 n_children
= NCH(n
);
2430 if (n_children
% 2 == 0) {
2431 ast_error(n
, "trailing comma not allowed without"
2432 " surrounding parentheses");
2437 ast_error(n
, "Unexpected node-type in from-import");
2441 aliases
= asdl_seq_new((n_children
+ 1) / 2, c
->c_arena
);
2445 /* handle "from ... import *" special b/c there's no children */
2446 if (TYPE(n
) == STAR
) {
2447 alias_ty import_alias
= alias_for_import_name(c
, n
);
2450 asdl_seq_SET(aliases
, 0, import_alias
);
2453 for (i
= 0; i
< NCH(n
); i
+= 2) {
2454 alias_ty import_alias
= alias_for_import_name(c
, CHILD(n
, i
));
2457 asdl_seq_SET(aliases
, i
/ 2, import_alias
);
2461 modname
= mod
->name
;
2463 modname
= new_identifier("", c
->c_arena
);
2464 return ImportFrom(modname
, aliases
, ndots
, lineno
, col_offset
,
2467 PyErr_Format(PyExc_SystemError
,
2468 "unknown import statement: starts with command '%s'",
2474 ast_for_global_stmt(struct compiling
*c
, const node
*n
)
2476 /* global_stmt: 'global' NAME (',' NAME)* */
2481 REQ(n
, global_stmt
);
2482 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2485 for (i
= 1; i
< NCH(n
); i
+= 2) {
2486 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2489 asdl_seq_SET(s
, i
/ 2, name
);
2491 return Global(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2495 ast_for_nonlocal_stmt(struct compiling
*c
, const node
*n
)
2497 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
2502 REQ(n
, nonlocal_stmt
);
2503 s
= asdl_seq_new(NCH(n
) / 2, c
->c_arena
);
2506 for (i
= 1; i
< NCH(n
); i
+= 2) {
2507 name
= NEW_IDENTIFIER(CHILD(n
, i
));
2510 asdl_seq_SET(s
, i
/ 2, name
);
2512 return Nonlocal(s
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2516 ast_for_assert_stmt(struct compiling
*c
, const node
*n
)
2518 /* assert_stmt: 'assert' test [',' test] */
2519 REQ(n
, assert_stmt
);
2521 expr_ty expression
= ast_for_expr(c
, CHILD(n
, 1));
2524 return Assert(expression
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2526 else if (NCH(n
) == 4) {
2527 expr_ty expr1
, expr2
;
2529 expr1
= ast_for_expr(c
, CHILD(n
, 1));
2532 expr2
= ast_for_expr(c
, CHILD(n
, 3));
2536 return Assert(expr1
, expr2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2538 PyErr_Format(PyExc_SystemError
,
2539 "improper number of parts to 'assert' statement: %d",
2545 ast_for_suite(struct compiling
*c
, const node
*n
)
2547 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2550 int i
, total
, num
, end
, pos
= 0;
2555 total
= num_stmts(n
);
2556 seq
= asdl_seq_new(total
, c
->c_arena
);
2559 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
2561 /* simple_stmt always ends with a NEWLINE,
2562 and may have a trailing SEMI
2565 if (TYPE(CHILD(n
, end
- 1)) == SEMI
)
2567 /* loop by 2 to skip semi-colons */
2568 for (i
= 0; i
< end
; i
+= 2) {
2570 s
= ast_for_stmt(c
, ch
);
2573 asdl_seq_SET(seq
, pos
++, s
);
2577 for (i
= 2; i
< (NCH(n
) - 1); i
++) {
2580 num
= num_stmts(ch
);
2582 /* small_stmt or compound_stmt with only one child */
2583 s
= ast_for_stmt(c
, ch
);
2586 asdl_seq_SET(seq
, pos
++, s
);
2591 REQ(ch
, simple_stmt
);
2592 for (j
= 0; j
< NCH(ch
); j
+= 2) {
2593 /* statement terminates with a semi-colon ';' */
2594 if (NCH(CHILD(ch
, j
)) == 0) {
2595 assert((j
+ 1) == NCH(ch
));
2598 s
= ast_for_stmt(c
, CHILD(ch
, j
));
2601 asdl_seq_SET(seq
, pos
++, s
);
2606 assert(pos
== seq
->size
);
2611 ast_for_if_stmt(struct compiling
*c
, const node
*n
)
2613 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2622 asdl_seq
*suite_seq
;
2624 expression
= ast_for_expr(c
, CHILD(n
, 1));
2627 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2631 return If(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
,
2635 s
= STR(CHILD(n
, 4));
2636 /* s[2], the third character in the string, will be
2642 asdl_seq
*seq1
, *seq2
;
2644 expression
= ast_for_expr(c
, CHILD(n
, 1));
2647 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2650 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2654 return If(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
,
2657 else if (s
[2] == 'i') {
2658 int i
, n_elif
, has_else
= 0;
2660 asdl_seq
*suite_seq
;
2661 asdl_seq
*orelse
= NULL
;
2662 n_elif
= NCH(n
) - 4;
2663 /* must reference the child n_elif+1 since 'else' token is third,
2664 not fourth, child from the end. */
2665 if (TYPE(CHILD(n
, (n_elif
+ 1))) == NAME
2666 && STR(CHILD(n
, (n_elif
+ 1)))[2] == 's') {
2673 asdl_seq
*suite_seq2
;
2675 orelse
= asdl_seq_new(1, c
->c_arena
);
2678 expression
= ast_for_expr(c
, CHILD(n
, NCH(n
) - 6));
2681 suite_seq
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 4));
2684 suite_seq2
= ast_for_suite(c
, CHILD(n
, NCH(n
) - 1));
2688 asdl_seq_SET(orelse
, 0,
2689 If(expression
, suite_seq
, suite_seq2
,
2690 LINENO(CHILD(n
, NCH(n
) - 6)),
2691 CHILD(n
, NCH(n
) - 6)->n_col_offset
,
2693 /* the just-created orelse handled the last elif */
2697 for (i
= 0; i
< n_elif
; i
++) {
2698 int off
= 5 + (n_elif
- i
- 1) * 4;
2699 asdl_seq
*newobj
= asdl_seq_new(1, c
->c_arena
);
2702 expression
= ast_for_expr(c
, CHILD(n
, off
));
2705 suite_seq
= ast_for_suite(c
, CHILD(n
, off
+ 2));
2709 asdl_seq_SET(newobj
, 0,
2710 If(expression
, suite_seq
, orelse
,
2711 LINENO(CHILD(n
, off
)),
2712 CHILD(n
, off
)->n_col_offset
, c
->c_arena
));
2715 expression
= ast_for_expr(c
, CHILD(n
, 1));
2718 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2721 return If(expression
, suite_seq
, orelse
,
2722 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2725 PyErr_Format(PyExc_SystemError
,
2726 "unexpected token in 'if' statement: %s", s
);
2731 ast_for_while_stmt(struct compiling
*c
, const node
*n
)
2733 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2738 asdl_seq
*suite_seq
;
2740 expression
= ast_for_expr(c
, CHILD(n
, 1));
2743 suite_seq
= ast_for_suite(c
, CHILD(n
, 3));
2746 return While(expression
, suite_seq
, NULL
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2748 else if (NCH(n
) == 7) {
2750 asdl_seq
*seq1
, *seq2
;
2752 expression
= ast_for_expr(c
, CHILD(n
, 1));
2755 seq1
= ast_for_suite(c
, CHILD(n
, 3));
2758 seq2
= ast_for_suite(c
, CHILD(n
, 6));
2762 return While(expression
, seq1
, seq2
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2765 PyErr_Format(PyExc_SystemError
,
2766 "wrong number of tokens for 'while' statement: %d",
2772 ast_for_for_stmt(struct compiling
*c
, const node
*n
)
2774 asdl_seq
*_target
, *seq
= NULL
, *suite_seq
;
2777 const node
*node_target
;
2778 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2782 seq
= ast_for_suite(c
, CHILD(n
, 8));
2787 node_target
= CHILD(n
, 1);
2788 _target
= ast_for_exprlist(c
, node_target
, Store
);
2791 /* Check the # of children rather than the length of _target, since
2792 for x, in ... has 1 element in _target, but still requires a Tuple. */
2793 if (NCH(node_target
) == 1)
2794 target
= (expr_ty
)asdl_seq_GET(_target
, 0);
2796 target
= Tuple(_target
, Store
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2798 expression
= ast_for_testlist(c
, CHILD(n
, 3));
2801 suite_seq
= ast_for_suite(c
, CHILD(n
, 5));
2805 return For(target
, expression
, suite_seq
, seq
, LINENO(n
), n
->n_col_offset
,
2809 static excepthandler_ty
2810 ast_for_except_clause(struct compiling
*c
, const node
*exc
, node
*body
)
2812 /* except_clause: 'except' [test ['as' test]] */
2813 REQ(exc
, except_clause
);
2816 if (NCH(exc
) == 1) {
2817 asdl_seq
*suite_seq
= ast_for_suite(c
, body
);
2821 return ExceptHandler(NULL
, NULL
, suite_seq
, LINENO(exc
),
2822 exc
->n_col_offset
, c
->c_arena
);
2824 else if (NCH(exc
) == 2) {
2826 asdl_seq
*suite_seq
;
2828 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2831 suite_seq
= ast_for_suite(c
, body
);
2835 return ExceptHandler(expression
, NULL
, suite_seq
, LINENO(exc
),
2836 exc
->n_col_offset
, c
->c_arena
);
2838 else if (NCH(exc
) == 4) {
2839 asdl_seq
*suite_seq
;
2841 identifier e
= NEW_IDENTIFIER(CHILD(exc
, 3));
2844 expression
= ast_for_expr(c
, CHILD(exc
, 1));
2847 suite_seq
= ast_for_suite(c
, body
);
2851 return ExceptHandler(expression
, e
, suite_seq
, LINENO(exc
),
2852 exc
->n_col_offset
, c
->c_arena
);
2855 PyErr_Format(PyExc_SystemError
,
2856 "wrong number of children for 'except' clause: %d",
2862 ast_for_try_stmt(struct compiling
*c
, const node
*n
)
2864 const int nch
= NCH(n
);
2865 int n_except
= (nch
- 3)/3;
2866 asdl_seq
*body
, *orelse
= NULL
, *finally
= NULL
;
2870 body
= ast_for_suite(c
, CHILD(n
, 2));
2874 if (TYPE(CHILD(n
, nch
- 3)) == NAME
) {
2875 if (strcmp(STR(CHILD(n
, nch
- 3)), "finally") == 0) {
2876 if (nch
>= 9 && TYPE(CHILD(n
, nch
- 6)) == NAME
) {
2877 /* we can assume it's an "else",
2878 because nch >= 9 for try-else-finally and
2879 it would otherwise have a type of except_clause */
2880 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 4));
2886 finally
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2887 if (finally
== NULL
)
2892 /* we can assume it's an "else",
2893 otherwise it would have a type of except_clause */
2894 orelse
= ast_for_suite(c
, CHILD(n
, nch
- 1));
2900 else if (TYPE(CHILD(n
, nch
- 3)) != except_clause
) {
2901 ast_error(n
, "malformed 'try' statement");
2908 /* process except statements to create a try ... except */
2909 asdl_seq
*handlers
= asdl_seq_new(n_except
, c
->c_arena
);
2910 if (handlers
== NULL
)
2913 for (i
= 0; i
< n_except
; i
++) {
2914 excepthandler_ty e
= ast_for_except_clause(c
, CHILD(n
, 3 + i
* 3),
2915 CHILD(n
, 5 + i
* 3));
2918 asdl_seq_SET(handlers
, i
, e
);
2921 except_st
= TryExcept(body
, handlers
, orelse
, LINENO(n
),
2922 n
->n_col_offset
, c
->c_arena
);
2926 /* if a 'finally' is present too, we nest the TryExcept within a
2927 TryFinally to emulate try ... except ... finally */
2928 body
= asdl_seq_new(1, c
->c_arena
);
2931 asdl_seq_SET(body
, 0, except_st
);
2934 /* must be a try ... finally (except clauses are in body, if any exist) */
2935 assert(finally
!= NULL
);
2936 return TryFinally(body
, finally
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
2939 /* with_item: test ['as' expr] */
2941 ast_for_with_item(struct compiling
*c
, const node
*n
, asdl_seq
*content
)
2943 expr_ty context_expr
, optional_vars
= NULL
;
2946 context_expr
= ast_for_expr(c
, CHILD(n
, 0));
2948 optional_vars
= ast_for_expr(c
, CHILD(n
, 2));
2950 if (!optional_vars
) {
2953 if (!set_context(c
, optional_vars
, Store
, n
)) {
2958 return With(context_expr
, optional_vars
, content
, LINENO(n
),
2959 n
->n_col_offset
, c
->c_arena
);
2962 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */
2964 ast_for_with_stmt(struct compiling
*c
, const node
*n
)
2972 /* process the with items inside-out */
2974 /* the suite of the innermost with item is the suite of the with stmt */
2975 inner
= ast_for_suite(c
, CHILD(n
, i
));
2981 ret
= ast_for_with_item(c
, CHILD(n
, i
), inner
);
2984 /* was this the last item? */
2987 /* if not, wrap the result so far in a new sequence */
2988 inner
= asdl_seq_new(1, c
->c_arena
);
2991 asdl_seq_SET(inner
, 0, ret
);
2998 ast_for_classdef(struct compiling
*c
, const node
*n
, asdl_seq
*decorator_seq
)
3000 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
3001 PyObject
*classname
;
3007 if (NCH(n
) == 4) { /* class NAME ':' suite */
3008 s
= ast_for_suite(c
, CHILD(n
, 3));
3011 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3014 return ClassDef(classname
, NULL
, NULL
, NULL
, NULL
, s
, decorator_seq
,
3015 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3018 if (TYPE(CHILD(n
, 3)) == RPAR
) { /* class NAME '(' ')' ':' suite */
3019 s
= ast_for_suite(c
, CHILD(n
,5));
3022 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3025 return ClassDef(classname
, NULL
, NULL
, NULL
, NULL
, s
, decorator_seq
,
3026 LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3029 /* class NAME '(' arglist ')' ':' suite */
3030 /* build up a fake Call node so we can extract its pieces */
3032 PyObject
*dummy_name
;
3034 dummy_name
= NEW_IDENTIFIER(CHILD(n
, 1));
3037 dummy
= Name(dummy_name
, Load
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3038 call
= ast_for_call(c
, CHILD(n
, 3), dummy
);
3042 s
= ast_for_suite(c
, CHILD(n
, 6));
3045 classname
= NEW_IDENTIFIER(CHILD(n
, 1));
3049 return ClassDef(classname
, call
->v
.Call
.args
, call
->v
.Call
.keywords
,
3050 call
->v
.Call
.starargs
, call
->v
.Call
.kwargs
, s
,
3051 decorator_seq
, LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3055 ast_for_stmt(struct compiling
*c
, const node
*n
)
3057 if (TYPE(n
) == stmt
) {
3058 assert(NCH(n
) == 1);
3061 if (TYPE(n
) == simple_stmt
) {
3062 assert(num_stmts(n
) == 1);
3065 if (TYPE(n
) == small_stmt
) {
3067 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3068 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
3072 return ast_for_expr_stmt(c
, n
);
3074 return ast_for_del_stmt(c
, n
);
3076 return Pass(LINENO(n
), n
->n_col_offset
, c
->c_arena
);
3078 return ast_for_flow_stmt(c
, n
);
3080 return ast_for_import_stmt(c
, n
);
3082 return ast_for_global_stmt(c
, n
);
3084 return ast_for_nonlocal_stmt(c
, n
);
3086 return ast_for_assert_stmt(c
, n
);
3088 PyErr_Format(PyExc_SystemError
,
3089 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3095 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3096 | funcdef | classdef | decorated
3098 node
*ch
= CHILD(n
, 0);
3099 REQ(n
, compound_stmt
);
3102 return ast_for_if_stmt(c
, ch
);
3104 return ast_for_while_stmt(c
, ch
);
3106 return ast_for_for_stmt(c
, ch
);
3108 return ast_for_try_stmt(c
, ch
);
3110 return ast_for_with_stmt(c
, ch
);
3112 return ast_for_funcdef(c
, ch
, NULL
);
3114 return ast_for_classdef(c
, ch
, NULL
);
3116 return ast_for_decorated(c
, ch
);
3118 PyErr_Format(PyExc_SystemError
,
3119 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3127 parsenumber(struct compiling
*c
, const char *s
)
3132 #ifndef WITHOUT_COMPLEX
3139 end
= s
+ strlen(s
) - 1;
3140 #ifndef WITHOUT_COMPLEX
3141 imflag
= *end
== 'j' || *end
== 'J';
3144 x
= (long) PyOS_strtoul((char *)s
, (char **)&end
, 0);
3145 if (x
< 0 && errno
== 0) {
3146 return PyLong_FromString((char *)s
,
3152 x
= PyOS_strtol((char *)s
, (char **)&end
, 0);
3155 return PyLong_FromString((char *)s
, (char **)0, 0);
3156 return PyLong_FromLong(x
);
3158 /* XXX Huge floats may silently fail */
3159 #ifndef WITHOUT_COMPLEX
3162 compl.imag
= PyOS_string_to_double(s
, (char **)&end
, NULL
);
3163 if (compl.imag
== -1.0 && PyErr_Occurred())
3165 return PyComplex_FromCComplex(compl);
3170 dx
= PyOS_string_to_double(s
, NULL
, NULL
);
3171 if (dx
== -1.0 && PyErr_Occurred())
3173 return PyFloat_FromDouble(dx
);
3178 decode_utf8(struct compiling
*c
, const char **sPtr
, const char *end
, char* encoding
)
3182 t
= s
= (char *)*sPtr
;
3183 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3184 while (s
< end
&& (*s
& 0x80)) s
++;
3186 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
3189 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
3195 decode_unicode(struct compiling
*c
, const char *s
, size_t len
, int rawmode
, const char *encoding
)
3202 if (encoding
== NULL
) {
3206 /* check for integer overflow */
3207 if (len
> PY_SIZE_MAX
/ 4)
3209 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3210 u
= PyBytes_FromStringAndSize((char *)NULL
, len
* 4);
3213 p
= buf
= PyBytes_AsString(u
);
3223 if (*s
& 0x80) { /* XXX inefficient */
3227 w
= decode_utf8(c
, &s
, end
, "utf-16-be");
3232 r
= PyBytes_AS_STRING(w
);
3234 assert(rn
% 2 == 0);
3235 for (i
= 0; i
< rn
; i
+= 2) {
3236 sprintf(p
, "\\u%02x%02x",
3250 v
= PyUnicode_DecodeRawUnicodeEscape(s
, len
, NULL
);
3252 v
= PyUnicode_DecodeUnicodeEscape(s
, len
, NULL
);
3257 /* s is a Python string literal, including the bracketing quote characters,
3258 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
3259 * parsestr parses it, and returns the decoded Python string object.
3262 parsestr(struct compiling
*c
, const node
*n
, int *bytesmode
)
3265 const char *s
= STR(n
);
3266 int quote
= Py_CHARMASK(*s
);
3269 if (isalpha(quote
)) {
3270 if (quote
== 'b' || quote
== 'B') {
3274 if (quote
== 'r' || quote
== 'R') {
3279 if (quote
!= '\'' && quote
!= '\"') {
3280 PyErr_BadInternalCall();
3285 if (len
> INT_MAX
) {
3286 PyErr_SetString(PyExc_OverflowError
,
3287 "string to parse is too long");
3290 if (s
[--len
] != quote
) {
3291 PyErr_BadInternalCall();
3294 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
3297 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
3298 PyErr_BadInternalCall();
3302 if (!*bytesmode
&& !rawmode
) {
3303 return decode_unicode(c
, s
, len
, rawmode
, c
->c_encoding
);
3306 /* Disallow non-ascii characters (but not escapes) */
3308 for (c
= s
; *c
; c
++) {
3309 if (Py_CHARMASK(*c
) >= 0x80) {
3310 ast_error(n
, "bytes can only contain ASCII "
3311 "literal characters.");
3316 need_encoding
= (!*bytesmode
&& c
->c_encoding
!= NULL
&&
3317 strcmp(c
->c_encoding
, "utf-8") != 0);
3318 if (rawmode
|| strchr(s
, '\\') == NULL
) {
3319 if (need_encoding
) {
3320 PyObject
*v
, *u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
3321 if (u
== NULL
|| !*bytesmode
)
3323 v
= PyUnicode_AsEncodedString(u
, c
->c_encoding
, NULL
);
3326 } else if (*bytesmode
) {
3327 return PyBytes_FromStringAndSize(s
, len
);
3328 } else if (strcmp(c
->c_encoding
, "utf-8") == 0) {
3329 return PyUnicode_FromStringAndSize(s
, len
);
3331 return PyUnicode_DecodeLatin1(s
, len
, NULL
);
3334 return PyBytes_DecodeEscape(s
, len
, NULL
, 1,
3335 need_encoding
? c
->c_encoding
: NULL
);
3338 /* Build a Python string object out of a STRING+ atom. This takes care of
3339 * compile-time literal catenation, calling parsestr() on each piece, and
3340 * pasting the intermediate results together.
3343 parsestrplus(struct compiling
*c
, const node
*n
, int *bytesmode
)
3347 REQ(CHILD(n
, 0), STRING
);
3348 v
= parsestr(c
, CHILD(n
, 0), bytesmode
);
3350 /* String literal concatenation */
3351 for (i
= 1; i
< NCH(n
); i
++) {
3354 s
= parsestr(c
, CHILD(n
, i
), &subbm
);
3357 if (*bytesmode
!= subbm
) {
3358 ast_error(n
, "cannot mix bytes and nonbytes literals");
3361 if (PyBytes_Check(v
) && PyBytes_Check(s
)) {
3362 PyBytes_ConcatAndDel(&v
, s
);
3367 PyObject
*temp
= PyUnicode_Concat(v
, s
);