| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * This file includes functions to transform a concrete syntax tree (CST) to | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 |  * an abstract syntax tree (AST). The main function is PyAST_FromNode(). | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4 |  * | 
 | 5 |  */ | 
 | 6 | #include "Python.h" | 
 | 7 | #include "Python-ast.h" | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 8 | #include "node.h" | 
 | 9 | #include "ast.h" | 
 | 10 | #include "token.h" | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 11 | #include "pythonrun.h" | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 |  | 
 | 13 | #include <assert.h> | 
| Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 14 | #include <stdbool.h> | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 |  | 
| Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 16 | #define MAXLEVEL 200    /* Max parentheses level */ | 
 | 17 |  | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 18 | static int validate_stmts(asdl_seq *); | 
 | 19 | static int validate_exprs(asdl_seq *, expr_context_ty, int); | 
 | 20 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); | 
 | 21 | static int validate_stmt(stmt_ty); | 
 | 22 | static int validate_expr(expr_ty, expr_context_ty); | 
 | 23 |  | 
 | 24 | static int | 
 | 25 | validate_comprehension(asdl_seq *gens) | 
 | 26 | { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 27 |     Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 28 |     if (!asdl_seq_LEN(gens)) { | 
 | 29 |         PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); | 
 | 30 |         return 0; | 
 | 31 |     } | 
 | 32 |     for (i = 0; i < asdl_seq_LEN(gens); i++) { | 
 | 33 |         comprehension_ty comp = asdl_seq_GET(gens, i); | 
 | 34 |         if (!validate_expr(comp->target, Store) || | 
 | 35 |             !validate_expr(comp->iter, Load) || | 
 | 36 |             !validate_exprs(comp->ifs, Load, 0)) | 
 | 37 |             return 0; | 
 | 38 |     } | 
 | 39 |     return 1; | 
 | 40 | } | 
 | 41 |  | 
 | 42 | static int | 
 | 43 | validate_slice(slice_ty slice) | 
 | 44 | { | 
 | 45 |     switch (slice->kind) { | 
 | 46 |     case Slice_kind: | 
 | 47 |         return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && | 
 | 48 |             (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && | 
 | 49 |             (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); | 
 | 50 |     case ExtSlice_kind: { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 51 |         Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 52 |         if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) | 
 | 53 |             return 0; | 
 | 54 |         for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) | 
 | 55 |             if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) | 
 | 56 |                 return 0; | 
 | 57 |         return 1; | 
 | 58 |     } | 
 | 59 |     case Index_kind: | 
 | 60 |         return validate_expr(slice->v.Index.value, Load); | 
 | 61 |     default: | 
 | 62 |         PyErr_SetString(PyExc_SystemError, "unknown slice node"); | 
 | 63 |         return 0; | 
 | 64 |     } | 
 | 65 | } | 
 | 66 |  | 
 | 67 | static int | 
 | 68 | validate_keywords(asdl_seq *keywords) | 
 | 69 | { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 70 |     Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 71 |     for (i = 0; i < asdl_seq_LEN(keywords); i++) | 
 | 72 |         if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) | 
 | 73 |             return 0; | 
 | 74 |     return 1; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static int | 
 | 78 | validate_args(asdl_seq *args) | 
 | 79 | { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 80 |     Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 81 |     for (i = 0; i < asdl_seq_LEN(args); i++) { | 
 | 82 |         arg_ty arg = asdl_seq_GET(args, i); | 
 | 83 |         if (arg->annotation && !validate_expr(arg->annotation, Load)) | 
 | 84 |             return 0; | 
 | 85 |     } | 
 | 86 |     return 1; | 
 | 87 | } | 
 | 88 |  | 
 | 89 | static const char * | 
 | 90 | expr_context_name(expr_context_ty ctx) | 
 | 91 | { | 
 | 92 |     switch (ctx) { | 
 | 93 |     case Load: | 
 | 94 |         return "Load"; | 
 | 95 |     case Store: | 
 | 96 |         return "Store"; | 
 | 97 |     case Del: | 
 | 98 |         return "Del"; | 
 | 99 |     case AugLoad: | 
 | 100 |         return "AugLoad"; | 
 | 101 |     case AugStore: | 
 | 102 |         return "AugStore"; | 
 | 103 |     case Param: | 
 | 104 |         return "Param"; | 
 | 105 |     default: | 
| Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 106 |         Py_UNREACHABLE(); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 107 |     } | 
 | 108 | } | 
 | 109 |  | 
 | 110 | static int | 
 | 111 | validate_arguments(arguments_ty args) | 
 | 112 | { | 
 | 113 |     if (!validate_args(args->args)) | 
 | 114 |         return 0; | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 115 |     if (args->vararg && args->vararg->annotation | 
 | 116 |         && !validate_expr(args->vararg->annotation, Load)) { | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 117 |             return 0; | 
 | 118 |     } | 
 | 119 |     if (!validate_args(args->kwonlyargs)) | 
 | 120 |         return 0; | 
| Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 121 |     if (args->kwarg && args->kwarg->annotation | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 122 |         && !validate_expr(args->kwarg->annotation, Load)) { | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 123 |             return 0; | 
 | 124 |     } | 
 | 125 |     if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { | 
 | 126 |         PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); | 
 | 127 |         return 0; | 
 | 128 |     } | 
 | 129 |     if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { | 
 | 130 |         PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " | 
 | 131 |                         "kw_defaults on arguments"); | 
 | 132 |         return 0; | 
 | 133 |     } | 
 | 134 |     return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); | 
 | 135 | } | 
 | 136 |  | 
 | 137 | static int | 
| Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 138 | validate_constant(PyObject *value) | 
 | 139 | { | 
 | 140 |     if (value == Py_None || value == Py_Ellipsis) | 
 | 141 |         return 1; | 
 | 142 |  | 
 | 143 |     if (PyLong_CheckExact(value) | 
 | 144 |             || PyFloat_CheckExact(value) | 
 | 145 |             || PyComplex_CheckExact(value) | 
 | 146 |             || PyBool_Check(value) | 
 | 147 |             || PyUnicode_CheckExact(value) | 
 | 148 |             || PyBytes_CheckExact(value)) | 
 | 149 |         return 1; | 
 | 150 |  | 
 | 151 |     if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { | 
 | 152 |         PyObject *it; | 
 | 153 |  | 
 | 154 |         it = PyObject_GetIter(value); | 
 | 155 |         if (it == NULL) | 
 | 156 |             return 0; | 
 | 157 |  | 
 | 158 |         while (1) { | 
 | 159 |             PyObject *item = PyIter_Next(it); | 
 | 160 |             if (item == NULL) { | 
 | 161 |                 if (PyErr_Occurred()) { | 
 | 162 |                     Py_DECREF(it); | 
 | 163 |                     return 0; | 
 | 164 |                 } | 
 | 165 |                 break; | 
 | 166 |             } | 
 | 167 |  | 
 | 168 |             if (!validate_constant(item)) { | 
 | 169 |                 Py_DECREF(it); | 
| Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 170 |                 Py_DECREF(item); | 
| Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 171 |                 return 0; | 
 | 172 |             } | 
| Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 173 |             Py_DECREF(item); | 
| Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 174 |         } | 
 | 175 |  | 
 | 176 |         Py_DECREF(it); | 
 | 177 |         return 1; | 
 | 178 |     } | 
 | 179 |  | 
 | 180 |     return 0; | 
 | 181 | } | 
 | 182 |  | 
 | 183 | static int | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 184 | validate_expr(expr_ty exp, expr_context_ty ctx) | 
 | 185 | { | 
 | 186 |     int check_ctx = 1; | 
 | 187 |     expr_context_ty actual_ctx; | 
 | 188 |  | 
 | 189 |     /* First check expression context. */ | 
 | 190 |     switch (exp->kind) { | 
 | 191 |     case Attribute_kind: | 
 | 192 |         actual_ctx = exp->v.Attribute.ctx; | 
 | 193 |         break; | 
 | 194 |     case Subscript_kind: | 
 | 195 |         actual_ctx = exp->v.Subscript.ctx; | 
 | 196 |         break; | 
 | 197 |     case Starred_kind: | 
 | 198 |         actual_ctx = exp->v.Starred.ctx; | 
 | 199 |         break; | 
 | 200 |     case Name_kind: | 
 | 201 |         actual_ctx = exp->v.Name.ctx; | 
 | 202 |         break; | 
 | 203 |     case List_kind: | 
 | 204 |         actual_ctx = exp->v.List.ctx; | 
 | 205 |         break; | 
 | 206 |     case Tuple_kind: | 
 | 207 |         actual_ctx = exp->v.Tuple.ctx; | 
 | 208 |         break; | 
 | 209 |     default: | 
 | 210 |         if (ctx != Load) { | 
 | 211 |             PyErr_Format(PyExc_ValueError, "expression which can't be " | 
 | 212 |                          "assigned to in %s context", expr_context_name(ctx)); | 
 | 213 |             return 0; | 
 | 214 |         } | 
 | 215 |         check_ctx = 0; | 
| Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 216 |         /* set actual_ctx to prevent gcc warning */ | 
 | 217 |         actual_ctx = 0; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 218 |     } | 
 | 219 |     if (check_ctx && actual_ctx != ctx) { | 
 | 220 |         PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", | 
 | 221 |                      expr_context_name(ctx), expr_context_name(actual_ctx)); | 
 | 222 |         return 0; | 
 | 223 |     } | 
 | 224 |  | 
 | 225 |     /* Now validate expression. */ | 
 | 226 |     switch (exp->kind) { | 
 | 227 |     case BoolOp_kind: | 
 | 228 |         if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { | 
 | 229 |             PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); | 
 | 230 |             return 0; | 
 | 231 |         } | 
 | 232 |         return validate_exprs(exp->v.BoolOp.values, Load, 0); | 
 | 233 |     case BinOp_kind: | 
 | 234 |         return validate_expr(exp->v.BinOp.left, Load) && | 
 | 235 |             validate_expr(exp->v.BinOp.right, Load); | 
 | 236 |     case UnaryOp_kind: | 
 | 237 |         return validate_expr(exp->v.UnaryOp.operand, Load); | 
 | 238 |     case Lambda_kind: | 
 | 239 |         return validate_arguments(exp->v.Lambda.args) && | 
 | 240 |             validate_expr(exp->v.Lambda.body, Load); | 
 | 241 |     case IfExp_kind: | 
 | 242 |         return validate_expr(exp->v.IfExp.test, Load) && | 
 | 243 |             validate_expr(exp->v.IfExp.body, Load) && | 
 | 244 |             validate_expr(exp->v.IfExp.orelse, Load); | 
 | 245 |     case Dict_kind: | 
 | 246 |         if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { | 
 | 247 |             PyErr_SetString(PyExc_ValueError, | 
 | 248 |                             "Dict doesn't have the same number of keys as values"); | 
 | 249 |             return 0; | 
 | 250 |         } | 
| Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 251 |         /* null_ok=1 for keys expressions to allow dict unpacking to work in | 
 | 252 |            dict literals, i.e. ``{**{a:b}}`` */ | 
 | 253 |         return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && | 
 | 254 |             validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 255 |     case Set_kind: | 
 | 256 |         return validate_exprs(exp->v.Set.elts, Load, 0); | 
 | 257 | #define COMP(NAME) \ | 
 | 258 |         case NAME ## _kind: \ | 
 | 259 |             return validate_comprehension(exp->v.NAME.generators) && \ | 
 | 260 |                 validate_expr(exp->v.NAME.elt, Load); | 
 | 261 |     COMP(ListComp) | 
 | 262 |     COMP(SetComp) | 
 | 263 |     COMP(GeneratorExp) | 
 | 264 | #undef COMP | 
 | 265 |     case DictComp_kind: | 
 | 266 |         return validate_comprehension(exp->v.DictComp.generators) && | 
 | 267 |             validate_expr(exp->v.DictComp.key, Load) && | 
 | 268 |             validate_expr(exp->v.DictComp.value, Load); | 
 | 269 |     case Yield_kind: | 
 | 270 |         return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); | 
| Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 271 |     case YieldFrom_kind: | 
| Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 272 |         return validate_expr(exp->v.YieldFrom.value, Load); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 273 |     case Await_kind: | 
 | 274 |         return validate_expr(exp->v.Await.value, Load); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 275 |     case Compare_kind: | 
 | 276 |         if (!asdl_seq_LEN(exp->v.Compare.comparators)) { | 
 | 277 |             PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); | 
 | 278 |             return 0; | 
 | 279 |         } | 
 | 280 |         if (asdl_seq_LEN(exp->v.Compare.comparators) != | 
 | 281 |             asdl_seq_LEN(exp->v.Compare.ops)) { | 
 | 282 |             PyErr_SetString(PyExc_ValueError, "Compare has a different number " | 
 | 283 |                             "of comparators and operands"); | 
 | 284 |             return 0; | 
 | 285 |         } | 
 | 286 |         return validate_exprs(exp->v.Compare.comparators, Load, 0) && | 
 | 287 |             validate_expr(exp->v.Compare.left, Load); | 
 | 288 |     case Call_kind: | 
 | 289 |         return validate_expr(exp->v.Call.func, Load) && | 
 | 290 |             validate_exprs(exp->v.Call.args, Load, 0) && | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 291 |             validate_keywords(exp->v.Call.keywords); | 
| Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 292 |     case Constant_kind: | 
 | 293 |         if (!validate_constant(exp->v.Constant.value)) { | 
| Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 294 |             PyErr_Format(PyExc_TypeError, | 
 | 295 |                          "got an invalid type in Constant: %s", | 
 | 296 |                          Py_TYPE(exp->v.Constant.value)->tp_name); | 
| Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 297 |             return 0; | 
 | 298 |         } | 
 | 299 |         return 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 300 |     case JoinedStr_kind: | 
 | 301 |         return validate_exprs(exp->v.JoinedStr.values, Load, 0); | 
 | 302 |     case FormattedValue_kind: | 
 | 303 |         if (validate_expr(exp->v.FormattedValue.value, Load) == 0) | 
 | 304 |             return 0; | 
 | 305 |         if (exp->v.FormattedValue.format_spec) | 
 | 306 |             return validate_expr(exp->v.FormattedValue.format_spec, Load); | 
 | 307 |         return 1; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 308 |     case Attribute_kind: | 
 | 309 |         return validate_expr(exp->v.Attribute.value, Load); | 
 | 310 |     case Subscript_kind: | 
 | 311 |         return validate_slice(exp->v.Subscript.slice) && | 
 | 312 |             validate_expr(exp->v.Subscript.value, Load); | 
 | 313 |     case Starred_kind: | 
 | 314 |         return validate_expr(exp->v.Starred.value, ctx); | 
 | 315 |     case List_kind: | 
 | 316 |         return validate_exprs(exp->v.List.elts, ctx, 0); | 
 | 317 |     case Tuple_kind: | 
 | 318 |         return validate_exprs(exp->v.Tuple.elts, ctx, 0); | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 319 |     /* This last case doesn't have any checking. */ | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 320 |     case Name_kind: | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 321 |         return 1; | 
 | 322 |     default: | 
 | 323 |         PyErr_SetString(PyExc_SystemError, "unexpected expression"); | 
 | 324 |         return 0; | 
 | 325 |     } | 
 | 326 | } | 
 | 327 |  | 
 | 328 | static int | 
 | 329 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) | 
 | 330 | { | 
 | 331 |     if (asdl_seq_LEN(seq)) | 
 | 332 |         return 1; | 
 | 333 |     PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); | 
 | 334 |     return 0; | 
 | 335 | } | 
 | 336 |  | 
 | 337 | static int | 
 | 338 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) | 
 | 339 | { | 
 | 340 |     return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && | 
 | 341 |         validate_exprs(targets, ctx, 0); | 
 | 342 | } | 
 | 343 |  | 
 | 344 | static int | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 345 | validate_body(asdl_seq *body, const char *owner) | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 346 | { | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 347 |     return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 348 | } | 
 | 349 |  | 
 | 350 | static int | 
 | 351 | validate_stmt(stmt_ty stmt) | 
 | 352 | { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 353 |     Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 354 |     switch (stmt->kind) { | 
 | 355 |     case FunctionDef_kind: | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 356 |         return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 357 |             validate_arguments(stmt->v.FunctionDef.args) && | 
 | 358 |             validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && | 
 | 359 |             (!stmt->v.FunctionDef.returns || | 
 | 360 |              validate_expr(stmt->v.FunctionDef.returns, Load)); | 
 | 361 |     case ClassDef_kind: | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 362 |         return validate_body(stmt->v.ClassDef.body, "ClassDef") && | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 363 |             validate_exprs(stmt->v.ClassDef.bases, Load, 0) && | 
 | 364 |             validate_keywords(stmt->v.ClassDef.keywords) && | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 365 |             validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 366 |     case Return_kind: | 
 | 367 |         return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); | 
 | 368 |     case Delete_kind: | 
 | 369 |         return validate_assignlist(stmt->v.Delete.targets, Del); | 
 | 370 |     case Assign_kind: | 
 | 371 |         return validate_assignlist(stmt->v.Assign.targets, Store) && | 
 | 372 |             validate_expr(stmt->v.Assign.value, Load); | 
 | 373 |     case AugAssign_kind: | 
 | 374 |         return validate_expr(stmt->v.AugAssign.target, Store) && | 
 | 375 |             validate_expr(stmt->v.AugAssign.value, Load); | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 376 |     case AnnAssign_kind: | 
 | 377 |         if (stmt->v.AnnAssign.target->kind != Name_kind && | 
 | 378 |             stmt->v.AnnAssign.simple) { | 
 | 379 |             PyErr_SetString(PyExc_TypeError, | 
 | 380 |                             "AnnAssign with simple non-Name target"); | 
 | 381 |             return 0; | 
 | 382 |         } | 
 | 383 |         return validate_expr(stmt->v.AnnAssign.target, Store) && | 
 | 384 |                (!stmt->v.AnnAssign.value || | 
 | 385 |                 validate_expr(stmt->v.AnnAssign.value, Load)) && | 
 | 386 |                validate_expr(stmt->v.AnnAssign.annotation, Load); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 387 |     case For_kind: | 
 | 388 |         return validate_expr(stmt->v.For.target, Store) && | 
 | 389 |             validate_expr(stmt->v.For.iter, Load) && | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 390 |             validate_body(stmt->v.For.body, "For") && | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 391 |             validate_stmts(stmt->v.For.orelse); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 392 |     case AsyncFor_kind: | 
 | 393 |         return validate_expr(stmt->v.AsyncFor.target, Store) && | 
 | 394 |             validate_expr(stmt->v.AsyncFor.iter, Load) && | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 395 |             validate_body(stmt->v.AsyncFor.body, "AsyncFor") && | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 396 |             validate_stmts(stmt->v.AsyncFor.orelse); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 397 |     case While_kind: | 
 | 398 |         return validate_expr(stmt->v.While.test, Load) && | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 399 |             validate_body(stmt->v.While.body, "While") && | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 400 |             validate_stmts(stmt->v.While.orelse); | 
 | 401 |     case If_kind: | 
 | 402 |         return validate_expr(stmt->v.If.test, Load) && | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 403 |             validate_body(stmt->v.If.body, "If") && | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 404 |             validate_stmts(stmt->v.If.orelse); | 
 | 405 |     case With_kind: | 
 | 406 |         if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) | 
 | 407 |             return 0; | 
 | 408 |         for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { | 
 | 409 |             withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); | 
 | 410 |             if (!validate_expr(item->context_expr, Load) || | 
 | 411 |                 (item->optional_vars && !validate_expr(item->optional_vars, Store))) | 
 | 412 |                 return 0; | 
 | 413 |         } | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 414 |         return validate_body(stmt->v.With.body, "With"); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 415 |     case AsyncWith_kind: | 
 | 416 |         if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) | 
 | 417 |             return 0; | 
 | 418 |         for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { | 
 | 419 |             withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); | 
 | 420 |             if (!validate_expr(item->context_expr, Load) || | 
 | 421 |                 (item->optional_vars && !validate_expr(item->optional_vars, Store))) | 
 | 422 |                 return 0; | 
 | 423 |         } | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 424 |         return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 425 |     case Raise_kind: | 
 | 426 |         if (stmt->v.Raise.exc) { | 
 | 427 |             return validate_expr(stmt->v.Raise.exc, Load) && | 
 | 428 |                 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); | 
 | 429 |         } | 
 | 430 |         if (stmt->v.Raise.cause) { | 
 | 431 |             PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); | 
 | 432 |             return 0; | 
 | 433 |         } | 
 | 434 |         return 1; | 
 | 435 |     case Try_kind: | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 436 |         if (!validate_body(stmt->v.Try.body, "Try")) | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 437 |             return 0; | 
 | 438 |         if (!asdl_seq_LEN(stmt->v.Try.handlers) && | 
 | 439 |             !asdl_seq_LEN(stmt->v.Try.finalbody)) { | 
 | 440 |             PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); | 
 | 441 |             return 0; | 
 | 442 |         } | 
 | 443 |         if (!asdl_seq_LEN(stmt->v.Try.handlers) && | 
 | 444 |             asdl_seq_LEN(stmt->v.Try.orelse)) { | 
 | 445 |             PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); | 
 | 446 |             return 0; | 
 | 447 |         } | 
 | 448 |         for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { | 
 | 449 |             excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); | 
 | 450 |             if ((handler->v.ExceptHandler.type && | 
 | 451 |                  !validate_expr(handler->v.ExceptHandler.type, Load)) || | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 452 |                 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 453 |                 return 0; | 
 | 454 |         } | 
 | 455 |         return (!asdl_seq_LEN(stmt->v.Try.finalbody) || | 
 | 456 |                 validate_stmts(stmt->v.Try.finalbody)) && | 
 | 457 |             (!asdl_seq_LEN(stmt->v.Try.orelse) || | 
 | 458 |              validate_stmts(stmt->v.Try.orelse)); | 
 | 459 |     case Assert_kind: | 
 | 460 |         return validate_expr(stmt->v.Assert.test, Load) && | 
 | 461 |             (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); | 
 | 462 |     case Import_kind: | 
 | 463 |         return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); | 
 | 464 |     case ImportFrom_kind: | 
| Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 465 |         if (stmt->v.ImportFrom.level < 0) { | 
 | 466 |             PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 467 |             return 0; | 
 | 468 |         } | 
 | 469 |         return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); | 
 | 470 |     case Global_kind: | 
 | 471 |         return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); | 
 | 472 |     case Nonlocal_kind: | 
 | 473 |         return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); | 
 | 474 |     case Expr_kind: | 
 | 475 |         return validate_expr(stmt->v.Expr.value, Load); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 476 |     case AsyncFunctionDef_kind: | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 477 |         return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 478 |             validate_arguments(stmt->v.AsyncFunctionDef.args) && | 
 | 479 |             validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && | 
 | 480 |             (!stmt->v.AsyncFunctionDef.returns || | 
 | 481 |              validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 482 |     case Pass_kind: | 
 | 483 |     case Break_kind: | 
 | 484 |     case Continue_kind: | 
 | 485 |         return 1; | 
 | 486 |     default: | 
 | 487 |         PyErr_SetString(PyExc_SystemError, "unexpected statement"); | 
 | 488 |         return 0; | 
 | 489 |     } | 
 | 490 | } | 
 | 491 |  | 
 | 492 | static int | 
 | 493 | validate_stmts(asdl_seq *seq) | 
 | 494 | { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 495 |     Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 496 |     for (i = 0; i < asdl_seq_LEN(seq); i++) { | 
 | 497 |         stmt_ty stmt = asdl_seq_GET(seq, i); | 
 | 498 |         if (stmt) { | 
 | 499 |             if (!validate_stmt(stmt)) | 
 | 500 |                 return 0; | 
 | 501 |         } | 
 | 502 |         else { | 
 | 503 |             PyErr_SetString(PyExc_ValueError, | 
 | 504 |                             "None disallowed in statement list"); | 
 | 505 |             return 0; | 
 | 506 |         } | 
 | 507 |     } | 
 | 508 |     return 1; | 
 | 509 | } | 
 | 510 |  | 
 | 511 | static int | 
 | 512 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) | 
 | 513 | { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 514 |     Py_ssize_t i; | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 515 |     for (i = 0; i < asdl_seq_LEN(exprs); i++) { | 
 | 516 |         expr_ty expr = asdl_seq_GET(exprs, i); | 
 | 517 |         if (expr) { | 
 | 518 |             if (!validate_expr(expr, ctx)) | 
 | 519 |                 return 0; | 
 | 520 |         } | 
 | 521 |         else if (!null_ok) { | 
 | 522 |             PyErr_SetString(PyExc_ValueError, | 
 | 523 |                             "None disallowed in expression list"); | 
 | 524 |             return 0; | 
 | 525 |         } | 
| Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 526 |  | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 527 |     } | 
 | 528 |     return 1; | 
 | 529 | } | 
 | 530 |  | 
 | 531 | int | 
 | 532 | PyAST_Validate(mod_ty mod) | 
 | 533 | { | 
 | 534 |     int res = 0; | 
 | 535 |  | 
 | 536 |     switch (mod->kind) { | 
 | 537 |     case Module_kind: | 
 | 538 |         res = validate_stmts(mod->v.Module.body); | 
 | 539 |         break; | 
 | 540 |     case Interactive_kind: | 
 | 541 |         res = validate_stmts(mod->v.Interactive.body); | 
 | 542 |         break; | 
 | 543 |     case Expression_kind: | 
 | 544 |         res = validate_expr(mod->v.Expression.body, Load); | 
 | 545 |         break; | 
 | 546 |     case Suite_kind: | 
 | 547 |         PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); | 
 | 548 |         break; | 
 | 549 |     default: | 
 | 550 |         PyErr_SetString(PyExc_SystemError, "impossible module node"); | 
 | 551 |         res = 0; | 
 | 552 |         break; | 
 | 553 |     } | 
 | 554 |     return res; | 
 | 555 | } | 
 | 556 |  | 
| Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 557 | /* This is done here, so defines like "test" don't interfere with AST use above. */ | 
| Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 558 | #include "grammar.h" | 
 | 559 | #include "parsetok.h" | 
 | 560 | #include "graminit.h" | 
 | 561 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | /* Data structure used internally */ | 
 | 563 | struct compiling { | 
| Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 564 |     PyArena *c_arena; /* Arena for allocating memory. */ | 
| Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 565 |     PyObject *c_filename; /* filename */ | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 566 |     PyObject *c_normalize; /* Normalization function from unicodedata. */ | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 567 |     int c_feature_version; /* Latest minor version of Python for allowed features */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 568 | }; | 
 | 569 |  | 
 | 570 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); | 
 | 571 | static expr_ty ast_for_expr(struct compiling *, const node *); | 
 | 572 | static stmt_ty ast_for_stmt(struct compiling *, const node *); | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 573 | static asdl_seq *ast_for_suite(struct compiling *c, const node *n); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 574 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, | 
 | 575 |                                   expr_context_ty); | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 576 | static expr_ty ast_for_testlist(struct compiling *, const node *); | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 577 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 578 |  | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 579 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); | 
 | 580 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 581 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 582 | /* Note different signature for ast_for_call */ | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 583 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 584 |                             const node *, const node *); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 585 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 586 | static PyObject *parsenumber(struct compiling *, const char *); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 587 | static expr_ty parsestrplus(struct compiling *, const node *n); | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 588 | static void get_last_end_pos(asdl_seq *, int *, int *); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 589 |  | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 590 | #define COMP_GENEXP   0 | 
 | 591 | #define COMP_LISTCOMP 1 | 
 | 592 | #define COMP_SETCOMP  2 | 
 | 593 |  | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 594 | static int | 
 | 595 | init_normalization(struct compiling *c) | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 596 | { | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 597 |     PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); | 
 | 598 |     if (!m) | 
 | 599 |         return 0; | 
 | 600 |     c->c_normalize = PyObject_GetAttrString(m, "normalize"); | 
 | 601 |     Py_DECREF(m); | 
 | 602 |     if (!c->c_normalize) | 
 | 603 |         return 0; | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 604 |     return 1; | 
 | 605 | } | 
 | 606 |  | 
 | 607 | static identifier | 
| Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 608 | new_identifier(const char *n, struct compiling *c) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 609 | { | 
| Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 610 |     PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); | 
| Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 611 |     if (!id) | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 612 |         return NULL; | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 613 |     /* PyUnicode_DecodeUTF8 should always return a ready string. */ | 
| Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 614 |     assert(PyUnicode_IS_READY(id)); | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 615 |     /* Check whether there are non-ASCII characters in the | 
 | 616 |        identifier; if so, normalize to NFKC. */ | 
| Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 617 |     if (!PyUnicode_IS_ASCII(id)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 618 |         PyObject *id2; | 
| Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 619 |         _Py_IDENTIFIER(NFKC); | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 620 |         if (!c->c_normalize && !init_normalization(c)) { | 
| Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 621 |             Py_DECREF(id); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 622 |             return NULL; | 
| Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 623 |         } | 
| Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 624 |         PyObject *form = _PyUnicode_FromId(&PyId_NFKC); | 
 | 625 |         if (form == NULL) { | 
 | 626 |             Py_DECREF(id); | 
 | 627 |             return NULL; | 
 | 628 |         } | 
 | 629 |         PyObject *args[2] = {form, id}; | 
 | 630 |         id2 = _PyObject_FastCall(c->c_normalize, args, 2); | 
| Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 631 |         Py_DECREF(id); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 632 |         if (!id2) | 
 | 633 |             return NULL; | 
| Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 634 |         if (!PyUnicode_Check(id2)) { | 
 | 635 |             PyErr_Format(PyExc_TypeError, | 
 | 636 |                          "unicodedata.normalize() must return a string, not " | 
 | 637 |                          "%.200s", | 
 | 638 |                          Py_TYPE(id2)->tp_name); | 
 | 639 |             Py_DECREF(id2); | 
 | 640 |             return NULL; | 
 | 641 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 642 |         id = id2; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 643 |     } | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 644 |     PyUnicode_InternInPlace(&id); | 
| Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 645 |     if (PyArena_AddPyObject(c->c_arena, id) < 0) { | 
 | 646 |         Py_DECREF(id); | 
 | 647 |         return NULL; | 
 | 648 |     } | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 649 |     return id; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 650 | } | 
 | 651 |  | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 652 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 653 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | static int | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 655 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 657 |     PyObject *value, *errstr, *loc, *tmp; | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 658 |     va_list va; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 |  | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 660 |     va_start(va, errmsg); | 
 | 661 |     errstr = PyUnicode_FromFormatV(errmsg, va); | 
 | 662 |     va_end(va); | 
 | 663 |     if (!errstr) { | 
 | 664 |         return 0; | 
 | 665 |     } | 
| Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 666 |     loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 667 |     if (!loc) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 668 |         Py_INCREF(Py_None); | 
 | 669 |         loc = Py_None; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 670 |     } | 
| Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 671 |     tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc); | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 672 |     if (!tmp) { | 
 | 673 |         Py_DECREF(errstr); | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 674 |         return 0; | 
| Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 675 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 676 |     value = PyTuple_Pack(2, errstr, tmp); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 677 |     Py_DECREF(errstr); | 
 | 678 |     Py_DECREF(tmp); | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 679 |     if (value) { | 
 | 680 |         PyErr_SetObject(PyExc_SyntaxError, value); | 
 | 681 |         Py_DECREF(value); | 
 | 682 |     } | 
 | 683 |     return 0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 684 | } | 
 | 685 |  | 
 | 686 | /* num_stmts() returns number of contained statements. | 
 | 687 |  | 
 | 688 |    Use this routine to determine how big a sequence is needed for | 
 | 689 |    the statements in a parse tree.  Its raison d'etre is this bit of | 
 | 690 |    grammar: | 
 | 691 |  | 
 | 692 |    stmt: simple_stmt | compound_stmt | 
 | 693 |    simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | 
 | 694 |  | 
 | 695 |    A simple_stmt can contain multiple small_stmt elements joined | 
 | 696 |    by semicolons.  If the arg is a simple_stmt, the number of | 
 | 697 |    small_stmt elements is returned. | 
 | 698 | */ | 
 | 699 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 700 | static string | 
| Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 701 | new_type_comment(const char *s, struct compiling *c) | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 702 | { | 
| Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 703 |     PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); | 
| Guido van Rossum | 4b250fc | 2019-02-11 08:10:42 -0800 | [diff] [blame] | 704 |     if (res == NULL) | 
 | 705 |         return NULL; | 
| Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 706 |     if (PyArena_AddPyObject(c->c_arena, res) < 0) { | 
 | 707 |         Py_DECREF(res); | 
 | 708 |         return NULL; | 
 | 709 |     } | 
 | 710 |     return res; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 711 | } | 
| Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 712 | #define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 713 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 714 | static int | 
 | 715 | num_stmts(const node *n) | 
 | 716 | { | 
 | 717 |     int i, l; | 
 | 718 |     node *ch; | 
 | 719 |  | 
 | 720 |     switch (TYPE(n)) { | 
 | 721 |         case single_input: | 
 | 722 |             if (TYPE(CHILD(n, 0)) == NEWLINE) | 
 | 723 |                 return 0; | 
 | 724 |             else | 
 | 725 |                 return num_stmts(CHILD(n, 0)); | 
 | 726 |         case file_input: | 
 | 727 |             l = 0; | 
 | 728 |             for (i = 0; i < NCH(n); i++) { | 
 | 729 |                 ch = CHILD(n, i); | 
 | 730 |                 if (TYPE(ch) == stmt) | 
 | 731 |                     l += num_stmts(ch); | 
 | 732 |             } | 
 | 733 |             return l; | 
 | 734 |         case stmt: | 
 | 735 |             return num_stmts(CHILD(n, 0)); | 
 | 736 |         case compound_stmt: | 
 | 737 |             return 1; | 
 | 738 |         case simple_stmt: | 
 | 739 |             return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ | 
 | 740 |         case suite: | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 741 |         case func_body_suite: | 
 | 742 |             /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ | 
 | 743 |             /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 744 |             if (NCH(n) == 1) | 
 | 745 |                 return num_stmts(CHILD(n, 0)); | 
 | 746 |             else { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 747 |                 i = 2; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 748 |                 l = 0; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 749 |                 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) | 
 | 750 |                     i += 2; | 
 | 751 |                 for (; i < (NCH(n) - 1); i++) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 752 |                     l += num_stmts(CHILD(n, i)); | 
 | 753 |                 return l; | 
 | 754 |             } | 
 | 755 |         default: { | 
 | 756 |             char buf[128]; | 
 | 757 |  | 
| Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 758 |             sprintf(buf, "Non-statement found: %d %d", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 759 |                     TYPE(n), NCH(n)); | 
 | 760 |             Py_FatalError(buf); | 
 | 761 |         } | 
 | 762 |     } | 
| Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 763 |     Py_UNREACHABLE(); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 764 | } | 
 | 765 |  | 
 | 766 | /* Transform the CST rooted at node * to the appropriate AST | 
 | 767 | */ | 
 | 768 |  | 
 | 769 | mod_ty | 
| Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 770 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, | 
 | 771 |                      PyObject *filename, PyArena *arena) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 772 | { | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 773 |     int i, j, k, num; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 774 |     asdl_seq *stmts = NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 775 |     asdl_seq *type_ignores = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 |     stmt_ty s; | 
 | 777 |     node *ch; | 
 | 778 |     struct compiling c; | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 779 |     mod_ty res = NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 780 |     asdl_seq *argtypes = NULL; | 
 | 781 |     expr_ty ret, arg; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 782 |  | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 783 |     c.c_arena = arena; | 
| Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 784 |     /* borrowed reference */ | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 785 |     c.c_filename = filename; | 
| Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 786 |     c.c_normalize = NULL; | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 787 |     c.c_feature_version = flags->cf_feature_version; | 
| Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 788 |  | 
 | 789 |     if (TYPE(n) == encoding_decl) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 |         n = CHILD(n, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 |  | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 792 |     k = 0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 |     switch (TYPE(n)) { | 
 | 794 |         case file_input: | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 795 |             stmts = _Py_asdl_seq_new(num_stmts(n), arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 |             if (!stmts) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 797 |                 goto out; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 |             for (i = 0; i < NCH(n) - 1; i++) { | 
 | 799 |                 ch = CHILD(n, i); | 
 | 800 |                 if (TYPE(ch) == NEWLINE) | 
 | 801 |                     continue; | 
 | 802 |                 REQ(ch, stmt); | 
 | 803 |                 num = num_stmts(ch); | 
 | 804 |                 if (num == 1) { | 
 | 805 |                     s = ast_for_stmt(&c, ch); | 
 | 806 |                     if (!s) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 807 |                         goto out; | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 808 |                     asdl_seq_SET(stmts, k++, s); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 809 |                 } | 
 | 810 |                 else { | 
 | 811 |                     ch = CHILD(ch, 0); | 
 | 812 |                     REQ(ch, simple_stmt); | 
 | 813 |                     for (j = 0; j < num; j++) { | 
 | 814 |                         s = ast_for_stmt(&c, CHILD(ch, j * 2)); | 
 | 815 |                         if (!s) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 816 |                             goto out; | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 817 |                         asdl_seq_SET(stmts, k++, s); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 818 |                     } | 
 | 819 |                 } | 
 | 820 |             } | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 821 |  | 
 | 822 |             /* Type ignores are stored under the ENDMARKER in file_input. */ | 
 | 823 |             ch = CHILD(n, NCH(n) - 1); | 
 | 824 |             REQ(ch, ENDMARKER); | 
 | 825 |             num = NCH(ch); | 
 | 826 |             type_ignores = _Py_asdl_seq_new(num, arena); | 
 | 827 |             if (!type_ignores) | 
 | 828 |                 goto out; | 
 | 829 |  | 
 | 830 |             for (i = 0; i < num; i++) { | 
 | 831 |                 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena); | 
 | 832 |                 if (!ti) | 
 | 833 |                    goto out; | 
 | 834 |                asdl_seq_SET(type_ignores, i, ti); | 
 | 835 |             } | 
 | 836 |  | 
 | 837 |             res = Module(stmts, type_ignores, arena); | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 838 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 |         case eval_input: { | 
 | 840 |             expr_ty testlist_ast; | 
 | 841 |  | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 842 |             /* XXX Why not comp_for here? */ | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 843 |             testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 |             if (!testlist_ast) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 845 |                 goto out; | 
 | 846 |             res = Expression(testlist_ast, arena); | 
 | 847 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 848 |         } | 
 | 849 |         case single_input: | 
 | 850 |             if (TYPE(CHILD(n, 0)) == NEWLINE) { | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 851 |                 stmts = _Py_asdl_seq_new(1, arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 852 |                 if (!stmts) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 853 |                     goto out; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 854 |                 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 855 |                                             n->n_end_lineno, n->n_end_col_offset, | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 856 |                                             arena)); | 
| Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 857 |                 if (!asdl_seq_GET(stmts, 0)) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 858 |                     goto out; | 
 | 859 |                 res = Interactive(stmts, arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 860 |             } | 
 | 861 |             else { | 
 | 862 |                 n = CHILD(n, 0); | 
 | 863 |                 num = num_stmts(n); | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 864 |                 stmts = _Py_asdl_seq_new(num, arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 |                 if (!stmts) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 866 |                     goto out; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 867 |                 if (num == 1) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 868 |                     s = ast_for_stmt(&c, n); | 
 | 869 |                     if (!s) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 870 |                         goto out; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 871 |                     asdl_seq_SET(stmts, 0, s); | 
 | 872 |                 } | 
 | 873 |                 else { | 
 | 874 |                     /* Only a simple_stmt can contain multiple statements. */ | 
 | 875 |                     REQ(n, simple_stmt); | 
 | 876 |                     for (i = 0; i < NCH(n); i += 2) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 877 |                         if (TYPE(CHILD(n, i)) == NEWLINE) | 
 | 878 |                             break; | 
 | 879 |                         s = ast_for_stmt(&c, CHILD(n, i)); | 
 | 880 |                         if (!s) | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 881 |                             goto out; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 882 |                         asdl_seq_SET(stmts, i / 2, s); | 
 | 883 |                     } | 
 | 884 |                 } | 
 | 885 |  | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 886 |                 res = Interactive(stmts, arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 887 |             } | 
| Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 888 |             break; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 889 |         case func_type_input: | 
 | 890 |             n = CHILD(n, 0); | 
 | 891 |             REQ(n, func_type); | 
 | 892 |  | 
 | 893 |             if (TYPE(CHILD(n, 1)) == typelist) { | 
 | 894 |                 ch = CHILD(n, 1); | 
 | 895 |                 /* this is overly permissive -- we don't pay any attention to | 
 | 896 |                  * stars on the args -- just parse them into an ordered list */ | 
 | 897 |                 num = 0; | 
 | 898 |                 for (i = 0; i < NCH(ch); i++) { | 
 | 899 |                     if (TYPE(CHILD(ch, i)) == test) { | 
 | 900 |                         num++; | 
 | 901 |                     } | 
 | 902 |                 } | 
 | 903 |  | 
 | 904 |                 argtypes = _Py_asdl_seq_new(num, arena); | 
 | 905 |                 if (!argtypes) | 
 | 906 |                     goto out; | 
 | 907 |  | 
 | 908 |                 j = 0; | 
 | 909 |                 for (i = 0; i < NCH(ch); i++) { | 
 | 910 |                     if (TYPE(CHILD(ch, i)) == test) { | 
 | 911 |                         arg = ast_for_expr(&c, CHILD(ch, i)); | 
 | 912 |                         if (!arg) | 
 | 913 |                             goto out; | 
 | 914 |                         asdl_seq_SET(argtypes, j++, arg); | 
 | 915 |                     } | 
 | 916 |                 } | 
 | 917 |             } | 
 | 918 |             else { | 
 | 919 |                 argtypes = _Py_asdl_seq_new(0, arena); | 
 | 920 |                 if (!argtypes) | 
 | 921 |                     goto out; | 
 | 922 |             } | 
 | 923 |  | 
 | 924 |             ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); | 
 | 925 |             if (!ret) | 
 | 926 |                 goto out; | 
 | 927 |             res = FunctionType(argtypes, ret, arena); | 
 | 928 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 |         default: | 
| Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 930 |             PyErr_Format(PyExc_SystemError, | 
 | 931 |                          "invalid node %d for PyAST_FromNode", TYPE(n)); | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 932 |             goto out; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 |     } | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 934 |  out: | 
 | 935 |     if (c.c_normalize) { | 
 | 936 |         Py_DECREF(c.c_normalize); | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 937 |     } | 
| Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 938 |     return res; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 939 | } | 
 | 940 |  | 
| Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 941 | mod_ty | 
 | 942 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, | 
 | 943 |                PyArena *arena) | 
 | 944 | { | 
 | 945 |     mod_ty mod; | 
 | 946 |     PyObject *filename; | 
 | 947 |     filename = PyUnicode_DecodeFSDefault(filename_str); | 
 | 948 |     if (filename == NULL) | 
 | 949 |         return NULL; | 
 | 950 |     mod = PyAST_FromNodeObject(n, flags, filename, arena); | 
 | 951 |     Py_DECREF(filename); | 
 | 952 |     return mod; | 
 | 953 |  | 
 | 954 | } | 
 | 955 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 956 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) | 
 | 957 | */ | 
 | 958 |  | 
 | 959 | static operator_ty | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 960 | get_operator(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 961 | { | 
 | 962 |     switch (TYPE(n)) { | 
 | 963 |         case VBAR: | 
 | 964 |             return BitOr; | 
 | 965 |         case CIRCUMFLEX: | 
 | 966 |             return BitXor; | 
 | 967 |         case AMPER: | 
 | 968 |             return BitAnd; | 
 | 969 |         case LEFTSHIFT: | 
 | 970 |             return LShift; | 
 | 971 |         case RIGHTSHIFT: | 
 | 972 |             return RShift; | 
 | 973 |         case PLUS: | 
 | 974 |             return Add; | 
 | 975 |         case MINUS: | 
 | 976 |             return Sub; | 
 | 977 |         case STAR: | 
 | 978 |             return Mult; | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 979 |         case AT: | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 980 |             if (c->c_feature_version < 5) { | 
 | 981 |                 ast_error(c, n, | 
 | 982 |                           "The '@' operator is only supported in Python 3.5 and greater"); | 
 | 983 |                 return (operator_ty)0; | 
 | 984 |             } | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 985 |             return MatMult; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 986 |         case SLASH: | 
 | 987 |             return Div; | 
 | 988 |         case DOUBLESLASH: | 
 | 989 |             return FloorDiv; | 
 | 990 |         case PERCENT: | 
 | 991 |             return Mod; | 
 | 992 |         default: | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 993 |             return (operator_ty)0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 994 |     } | 
 | 995 | } | 
 | 996 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 997 | static const char * const FORBIDDEN[] = { | 
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 998 |     "None", | 
 | 999 |     "True", | 
 | 1000 |     "False", | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1001 |     "__debug__", | 
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1002 |     NULL, | 
 | 1003 | }; | 
 | 1004 |  | 
 | 1005 | static int | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1006 | forbidden_name(struct compiling *c, identifier name, const node *n, | 
 | 1007 |                int full_checks) | 
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1008 | { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1009 |     assert(PyUnicode_Check(name)); | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1010 |     const char * const *p = FORBIDDEN; | 
 | 1011 |     if (!full_checks) { | 
 | 1012 |         /* In most cases, the parser will protect True, False, and None | 
 | 1013 |            from being assign to. */ | 
 | 1014 |         p += 3; | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1015 |     } | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1016 |     for (; *p; p++) { | 
 | 1017 |         if (_PyUnicode_EqualToASCIIString(name, *p)) { | 
 | 1018 |             ast_error(c, n, "cannot assign to %U", name); | 
 | 1019 |             return 1; | 
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1020 |         } | 
 | 1021 |     } | 
 | 1022 |     return 0; | 
 | 1023 | } | 
 | 1024 |  | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1025 | static expr_ty | 
 | 1026 | copy_location(expr_ty e, const node *n) | 
 | 1027 | { | 
 | 1028 |     if (e) { | 
 | 1029 |         e->lineno = LINENO(n); | 
 | 1030 |         e->col_offset = n->n_col_offset; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1031 |         e->end_lineno = n->n_end_lineno; | 
 | 1032 |         e->end_col_offset = n->n_end_col_offset; | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1033 |     } | 
 | 1034 |     return e; | 
 | 1035 | } | 
 | 1036 |  | 
| Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1037 | static const char * | 
 | 1038 | get_expr_name(expr_ty e) | 
 | 1039 | { | 
 | 1040 |     switch (e->kind) { | 
 | 1041 |         case Attribute_kind: | 
 | 1042 |             return "attribute"; | 
 | 1043 |         case Subscript_kind: | 
 | 1044 |             return "subscript"; | 
 | 1045 |         case Starred_kind: | 
 | 1046 |             return "starred"; | 
 | 1047 |         case Name_kind: | 
 | 1048 |             return "name"; | 
 | 1049 |         case List_kind: | 
 | 1050 |             return "list"; | 
 | 1051 |         case Tuple_kind: | 
 | 1052 |             return "tuple"; | 
 | 1053 |         case Lambda_kind: | 
 | 1054 |             return "lambda"; | 
 | 1055 |         case Call_kind: | 
 | 1056 |             return "function call"; | 
 | 1057 |         case BoolOp_kind: | 
 | 1058 |         case BinOp_kind: | 
 | 1059 |         case UnaryOp_kind: | 
 | 1060 |             return "operator"; | 
 | 1061 |         case GeneratorExp_kind: | 
 | 1062 |             return "generator expression"; | 
 | 1063 |         case Yield_kind: | 
 | 1064 |         case YieldFrom_kind: | 
 | 1065 |             return "yield expression"; | 
 | 1066 |         case Await_kind: | 
 | 1067 |             return "await expression"; | 
 | 1068 |         case ListComp_kind: | 
 | 1069 |             return "list comprehension"; | 
 | 1070 |         case SetComp_kind: | 
 | 1071 |             return "set comprehension"; | 
 | 1072 |         case DictComp_kind: | 
 | 1073 |             return "dict comprehension"; | 
 | 1074 |         case Dict_kind: | 
 | 1075 |             return "dict display"; | 
 | 1076 |         case Set_kind: | 
 | 1077 |             return "set display"; | 
 | 1078 |         case JoinedStr_kind: | 
 | 1079 |         case FormattedValue_kind: | 
 | 1080 |             return "f-string expression"; | 
 | 1081 |         case Constant_kind: { | 
 | 1082 |             PyObject *value = e->v.Constant.value; | 
 | 1083 |             if (value == Py_None) { | 
 | 1084 |                 return "None"; | 
 | 1085 |             } | 
 | 1086 |             if (value == Py_False) { | 
 | 1087 |                 return "False"; | 
 | 1088 |             } | 
 | 1089 |             if (value == Py_True) { | 
 | 1090 |                 return "True"; | 
 | 1091 |             } | 
 | 1092 |             if (value == Py_Ellipsis) { | 
 | 1093 |                 return "Ellipsis"; | 
 | 1094 |             } | 
 | 1095 |             return "literal"; | 
 | 1096 |         } | 
 | 1097 |         case Compare_kind: | 
 | 1098 |             return "comparison"; | 
 | 1099 |         case IfExp_kind: | 
 | 1100 |             return "conditional expression"; | 
 | 1101 |         case NamedExpr_kind: | 
 | 1102 |             return "named expression"; | 
 | 1103 |         default: | 
 | 1104 |             PyErr_Format(PyExc_SystemError, | 
 | 1105 |                          "unexpected expression in assignment %d (line %d)", | 
 | 1106 |                          e->kind, e->lineno); | 
 | 1107 |             return NULL; | 
 | 1108 |     } | 
 | 1109 | } | 
 | 1110 |  | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1111 | /* Set the context ctx for expr_ty e, recursively traversing e. | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1112 |  | 
 | 1113 |    Only sets context for expr kinds that "can appear in assignment context" | 
 | 1114 |    (according to ../Parser/Python.asdl).  For other expr kinds, it sets | 
 | 1115 |    an appropriate syntax error and returns false. | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1116 | */ | 
 | 1117 |  | 
 | 1118 | static int | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1119 | set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1120 | { | 
 | 1121 |     asdl_seq *s = NULL; | 
| Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1122 |  | 
 | 1123 |     /* The ast defines augmented store and load contexts, but the | 
 | 1124 |        implementation here doesn't actually use them.  The code may be | 
 | 1125 |        a little more complex than necessary as a result.  It also means | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1126 |        that expressions in an augmented assignment have a Store context. | 
| Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1127 |        Consider restructuring so that augmented assignment uses | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1128 |        set_context(), too. | 
| Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1129 |     */ | 
 | 1130 |     assert(ctx != AugStore && ctx != AugLoad); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1131 |  | 
 | 1132 |     switch (e->kind) { | 
 | 1133 |         case Attribute_kind: | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1134 |             e->v.Attribute.ctx = ctx; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1135 |             if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1136 |                 return 0; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1137 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1138 |         case Subscript_kind: | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1139 |             e->v.Subscript.ctx = ctx; | 
 | 1140 |             break; | 
| Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1141 |         case Starred_kind: | 
 | 1142 |             e->v.Starred.ctx = ctx; | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1143 |             if (!set_context(c, e->v.Starred.value, ctx, n)) | 
| Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1144 |                 return 0; | 
 | 1145 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 |         case Name_kind: | 
| Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1147 |             if (ctx == Store) { | 
| Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1148 |                 if (forbidden_name(c, e->v.Name.id, n, 0)) | 
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1149 |                     return 0; /* forbidden_name() calls ast_error() */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1150 |             } | 
 | 1151 |             e->v.Name.ctx = ctx; | 
 | 1152 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1153 |         case List_kind: | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1154 |             e->v.List.ctx = ctx; | 
 | 1155 |             s = e->v.List.elts; | 
 | 1156 |             break; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1157 |         case Tuple_kind: | 
| Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1158 |             e->v.Tuple.ctx = ctx; | 
 | 1159 |             s = e->v.Tuple.elts; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1160 |             break; | 
| Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1161 |         default: { | 
 | 1162 |             const char *expr_name = get_expr_name(e); | 
 | 1163 |             if (expr_name != NULL) { | 
 | 1164 |                 ast_error(c, n, "cannot %s %s", | 
 | 1165 |                           ctx == Store ? "assign to" : "delete", | 
 | 1166 |                           expr_name); | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1167 |             } | 
| Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1168 |             return 0; | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1169 |         } | 
| Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1170 |     } | 
 | 1171 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1172 |     /* If the LHS is a list or tuple, we need to set the assignment | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 |        context for all the contained elements. | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1174 |     */ | 
 | 1175 |     if (s) { | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1176 |         Py_ssize_t i; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1178 |         for (i = 0; i < asdl_seq_LEN(s); i++) { | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1179 |             if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n)) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1180 |                 return 0; | 
 | 1181 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 |     } | 
 | 1183 |     return 1; | 
 | 1184 | } | 
 | 1185 |  | 
 | 1186 | static operator_ty | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1187 | ast_for_augassign(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1188 | { | 
 | 1189 |     REQ(n, augassign); | 
 | 1190 |     n = CHILD(n, 0); | 
 | 1191 |     switch (STR(n)[0]) { | 
 | 1192 |         case '+': | 
 | 1193 |             return Add; | 
 | 1194 |         case '-': | 
 | 1195 |             return Sub; | 
 | 1196 |         case '/': | 
 | 1197 |             if (STR(n)[1] == '/') | 
 | 1198 |                 return FloorDiv; | 
 | 1199 |             else | 
 | 1200 |                 return Div; | 
 | 1201 |         case '%': | 
 | 1202 |             return Mod; | 
 | 1203 |         case '<': | 
 | 1204 |             return LShift; | 
 | 1205 |         case '>': | 
 | 1206 |             return RShift; | 
 | 1207 |         case '&': | 
 | 1208 |             return BitAnd; | 
 | 1209 |         case '^': | 
 | 1210 |             return BitXor; | 
 | 1211 |         case '|': | 
 | 1212 |             return BitOr; | 
 | 1213 |         case '*': | 
 | 1214 |             if (STR(n)[1] == '*') | 
 | 1215 |                 return Pow; | 
 | 1216 |             else | 
 | 1217 |                 return Mult; | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1218 |         case '@': | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1219 |             if (c->c_feature_version < 5) { | 
 | 1220 |                 ast_error(c, n, | 
 | 1221 |                           "The '@' operator is only supported in Python 3.5 and greater"); | 
 | 1222 |                 return (operator_ty)0; | 
 | 1223 |             } | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1224 |             return MatMult; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1225 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1226 |             PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1227 |             return (operator_ty)0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1228 |     } | 
 | 1229 | } | 
 | 1230 |  | 
 | 1231 | static cmpop_ty | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1232 | ast_for_comp_op(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1233 | { | 
| Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1234 |     /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1235 |                |'is' 'not' | 
 | 1236 |     */ | 
 | 1237 |     REQ(n, comp_op); | 
 | 1238 |     if (NCH(n) == 1) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1239 |         n = CHILD(n, 0); | 
 | 1240 |         switch (TYPE(n)) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1241 |             case LESS: | 
 | 1242 |                 return Lt; | 
 | 1243 |             case GREATER: | 
 | 1244 |                 return Gt; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1245 |             case EQEQUAL:                       /* == */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1246 |                 return Eq; | 
 | 1247 |             case LESSEQUAL: | 
 | 1248 |                 return LtE; | 
 | 1249 |             case GREATEREQUAL: | 
 | 1250 |                 return GtE; | 
 | 1251 |             case NOTEQUAL: | 
 | 1252 |                 return NotEq; | 
 | 1253 |             case NAME: | 
 | 1254 |                 if (strcmp(STR(n), "in") == 0) | 
 | 1255 |                     return In; | 
 | 1256 |                 if (strcmp(STR(n), "is") == 0) | 
 | 1257 |                     return Is; | 
| Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1258 |                 /* fall through */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1259 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1260 |                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1261 |                              STR(n)); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1262 |                 return (cmpop_ty)0; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1263 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1264 |     } | 
 | 1265 |     else if (NCH(n) == 2) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1266 |         /* handle "not in" and "is not" */ | 
 | 1267 |         switch (TYPE(CHILD(n, 0))) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1268 |             case NAME: | 
 | 1269 |                 if (strcmp(STR(CHILD(n, 1)), "in") == 0) | 
 | 1270 |                     return NotIn; | 
 | 1271 |                 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | 
 | 1272 |                     return IsNot; | 
| Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1273 |                 /* fall through */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1274 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1275 |                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1276 |                              STR(CHILD(n, 0)), STR(CHILD(n, 1))); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1277 |                 return (cmpop_ty)0; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1278 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1279 |     } | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1280 |     PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1281 |                  NCH(n)); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1282 |     return (cmpop_ty)0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1283 | } | 
 | 1284 |  | 
 | 1285 | static asdl_seq * | 
 | 1286 | seq_for_testlist(struct compiling *c, const node *n) | 
 | 1287 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 |     /* testlist: test (',' test)* [','] | 
| Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1289 |        testlist_star_expr: test|star_expr (',' test|star_expr)* [','] | 
 | 1290 |     */ | 
| Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1291 |     asdl_seq *seq; | 
 | 1292 |     expr_ty expression; | 
 | 1293 |     int i; | 
| Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1294 |     assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1295 |  | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1296 |     seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1297 |     if (!seq) | 
 | 1298 |         return NULL; | 
 | 1299 |  | 
 | 1300 |     for (i = 0; i < NCH(n); i += 2) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 |         const node *ch = CHILD(n, i); | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1302 |         assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1303 |  | 
| Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1304 |         expression = ast_for_expr(c, ch); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1305 |         if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1306 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1307 |  | 
 | 1308 |         assert(i / 2 < seq->size); | 
 | 1309 |         asdl_seq_SET(seq, i / 2, expression); | 
 | 1310 |     } | 
 | 1311 |     return seq; | 
 | 1312 | } | 
 | 1313 |  | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1314 | static arg_ty | 
| Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1315 | ast_for_arg(struct compiling *c, const node *n) | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1316 | { | 
 | 1317 |     identifier name; | 
 | 1318 |     expr_ty annotation = NULL; | 
 | 1319 |     node *ch; | 
| Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1320 |     arg_ty ret; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1321 |  | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1322 |     assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1323 |     ch = CHILD(n, 0); | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1324 |     name = NEW_IDENTIFIER(ch); | 
 | 1325 |     if (!name) | 
 | 1326 |         return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1327 |     if (forbidden_name(c, name, ch, 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1328 |         return NULL; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1329 |  | 
 | 1330 |     if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { | 
 | 1331 |         annotation = ast_for_expr(c, CHILD(n, 2)); | 
 | 1332 |         if (!annotation) | 
 | 1333 |             return NULL; | 
 | 1334 |     } | 
 | 1335 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1336 |     ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1337 |               n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1338 |     if (!ret) | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1339 |         return NULL; | 
| Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1340 |     return ret; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1341 | } | 
 | 1342 |  | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1343 | /* returns -1 if failed to handle keyword only arguments | 
 | 1344 |    returns new position to keep processing if successful | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1345 |                (',' tfpdef ['=' test])* | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1346 |                      ^^^ | 
 | 1347 |    start pointing here | 
 | 1348 |  */ | 
 | 1349 | static int | 
 | 1350 | handle_keywordonly_args(struct compiling *c, const node *n, int start, | 
 | 1351 |                         asdl_seq *kwonlyargs, asdl_seq *kwdefaults) | 
 | 1352 | { | 
| Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1353 |     PyObject *argname; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1354 |     node *ch; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1355 |     expr_ty expression, annotation; | 
| Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1356 |     arg_ty arg = NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1357 |     int i = start; | 
 | 1358 |     int j = 0; /* index for kwdefaults and kwonlyargs */ | 
| Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1359 |  | 
 | 1360 |     if (kwonlyargs == NULL) { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1361 |         ast_error(c, CHILD(n, start), "named arguments must follow bare *"); | 
| Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1362 |         return -1; | 
 | 1363 |     } | 
 | 1364 |     assert(kwdefaults != NULL); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1365 |     while (i < NCH(n)) { | 
 | 1366 |         ch = CHILD(n, i); | 
 | 1367 |         switch (TYPE(ch)) { | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1368 |             case vfpdef: | 
 | 1369 |             case tfpdef: | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1370 |                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1371 |                     expression = ast_for_expr(c, CHILD(n, i + 2)); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1372 |                     if (!expression) | 
| Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1373 |                         goto error; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1374 |                     asdl_seq_SET(kwdefaults, j, expression); | 
 | 1375 |                     i += 2; /* '=' and test */ | 
 | 1376 |                 } | 
 | 1377 |                 else { /* setting NULL if no default value exists */ | 
 | 1378 |                     asdl_seq_SET(kwdefaults, j, NULL); | 
 | 1379 |                 } | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1380 |                 if (NCH(ch) == 3) { | 
 | 1381 |                     /* ch is NAME ':' test */ | 
 | 1382 |                     annotation = ast_for_expr(c, CHILD(ch, 2)); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1383 |                     if (!annotation) | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1384 |                         goto error; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1385 |                 } | 
 | 1386 |                 else { | 
 | 1387 |                     annotation = NULL; | 
 | 1388 |                 } | 
 | 1389 |                 ch = CHILD(ch, 0); | 
| Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1390 |                 argname = NEW_IDENTIFIER(ch); | 
 | 1391 |                 if (!argname) | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1392 |                     goto error; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1393 |                 if (forbidden_name(c, argname, ch, 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1394 |                     goto error; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1395 |                 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1396 |                           ch->n_end_lineno, ch->n_end_col_offset, | 
| Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1397 |                           c->c_arena); | 
| Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1398 |                 if (!arg) | 
 | 1399 |                     goto error; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1400 |                 asdl_seq_SET(kwonlyargs, j++, arg); | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1401 |                 i += 1; /* the name */ | 
 | 1402 |                 if (TYPE(CHILD(n, i)) == COMMA) | 
 | 1403 |                     i += 1; /* the comma, if present */ | 
 | 1404 |                 break; | 
 | 1405 |             case TYPE_COMMENT: | 
 | 1406 |                 /* arg will be equal to the last argument processed */ | 
 | 1407 |                 arg->type_comment = NEW_TYPE_COMMENT(ch); | 
 | 1408 |                 if (!arg->type_comment) | 
 | 1409 |                     goto error; | 
 | 1410 |                 i += 1; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1411 |                 break; | 
 | 1412 |             case DOUBLESTAR: | 
 | 1413 |                 return i; | 
 | 1414 |             default: | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1415 |                 ast_error(c, ch, "unexpected node"); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1416 |                 goto error; | 
 | 1417 |         } | 
 | 1418 |     } | 
 | 1419 |     return i; | 
 | 1420 |  error: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 |     return -1; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1422 | } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1423 |  | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1424 | /* Create AST for argument list. */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1425 |  | 
 | 1426 | static arguments_ty | 
 | 1427 | ast_for_arguments(struct compiling *c, const node *n) | 
 | 1428 | { | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1429 |     /* This function handles both typedargslist (function definition) | 
 | 1430 |        and varargslist (lambda definition). | 
 | 1431 |  | 
 | 1432 |        parameters: '(' [typedargslist] ')' | 
| Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1433 |        typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ | 
 | 1434 |                '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | 
 | 1435 |              | '**' tfpdef [',']]] | 
 | 1436 |          | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | 
 | 1437 |          | '**' tfpdef [',']) | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1438 |        tfpdef: NAME [':' test] | 
| Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1439 |        varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ | 
 | 1440 |                '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | 
 | 1441 |              | '**' vfpdef [',']]] | 
 | 1442 |          | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | 
 | 1443 |          | '**' vfpdef [','] | 
 | 1444 |        ) | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1445 |        vfpdef: NAME | 
| Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1446 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1447 |     */ | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1448 |     int i, j, k, nposargs = 0, nkwonlyargs = 0; | 
 | 1449 |     int nposdefaults = 0, found_default = 0; | 
 | 1450 |     asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1451 |     arg_ty vararg = NULL, kwarg = NULL; | 
| Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1452 |     arg_ty arg = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1453 |     node *ch; | 
 | 1454 |  | 
 | 1455 |     if (TYPE(n) == parameters) { | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1456 |         if (NCH(n) == 2) /* () as argument list */ | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1457 |             return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1458 |         n = CHILD(n, 1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1459 |     } | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1460 |     assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1461 |  | 
| Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1462 |     /* First count the number of positional args & defaults.  The | 
 | 1463 |        variable i is the loop index for this for loop and the next. | 
 | 1464 |        The next loop picks up where the first leaves off. | 
 | 1465 |     */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 |     for (i = 0; i < NCH(n); i++) { | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1467 |         ch = CHILD(n, i); | 
 | 1468 |         if (TYPE(ch) == STAR) { | 
| Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1469 |             /* skip star */ | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1470 |             i++; | 
| Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1471 |             if (i < NCH(n) && /* skip argument following star */ | 
 | 1472 |                 (TYPE(CHILD(n, i)) == tfpdef || | 
 | 1473 |                  TYPE(CHILD(n, i)) == vfpdef)) { | 
 | 1474 |                 i++; | 
 | 1475 |             } | 
 | 1476 |             break; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1477 |         } | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1478 |         if (TYPE(ch) == DOUBLESTAR) break; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1479 |         if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1480 |         if (TYPE(ch) == EQUAL) nposdefaults++; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1481 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 |     /* count the number of keyword only args & | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1483 |        defaults for keyword only args */ | 
 | 1484 |     for ( ; i < NCH(n); ++i) { | 
 | 1485 |         ch = CHILD(n, i); | 
 | 1486 |         if (TYPE(ch) == DOUBLESTAR) break; | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1487 |         if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1488 |     } | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1489 |     posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1490 |     if (!posargs && nposargs) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1491 |         return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1492 |     kwonlyargs = (nkwonlyargs ? | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1493 |                    _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1494 |     if (!kwonlyargs && nkwonlyargs) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1495 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 |     posdefaults = (nposdefaults ? | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1497 |                     _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1498 |     if (!posdefaults && nposdefaults) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1499 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 |     /* The length of kwonlyargs and kwdefaults are same | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1501 |        since we set NULL as default for keyword only argument w/o default | 
 | 1502 |        - we have sequence data structure, but no dictionary */ | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1503 |     kwdefaults = (nkwonlyargs ? | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1504 |                    _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1505 |     if (!kwdefaults && nkwonlyargs) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1506 |         return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1507 |  | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1508 |     /* tfpdef: NAME [':' test] | 
 | 1509 |        vfpdef: NAME | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1510 |     */ | 
 | 1511 |     i = 0; | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1512 |     j = 0;  /* index for defaults */ | 
 | 1513 |     k = 0;  /* index for args */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1514 |     while (i < NCH(n)) { | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1515 |         ch = CHILD(n, i); | 
 | 1516 |         switch (TYPE(ch)) { | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1517 |             case tfpdef: | 
 | 1518 |             case vfpdef: | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1519 |                 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is | 
 | 1520 |                    anything other than EQUAL or a comma? */ | 
 | 1521 |                 /* XXX Should NCH(n) check be made a separate check? */ | 
 | 1522 |                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1523 |                     expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); | 
 | 1524 |                     if (!expression) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1525 |                         return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1526 |                     assert(posdefaults != NULL); | 
 | 1527 |                     asdl_seq_SET(posdefaults, j++, expression); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1528 |                     i += 2; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1529 |                     found_default = 1; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1530 |                 } | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1531 |                 else if (found_default) { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1532 |                     ast_error(c, n, | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1533 |                               "non-default argument follows default argument"); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1534 |                     return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1535 |                 } | 
| Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1536 |                 arg = ast_for_arg(c, ch); | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1537 |                 if (!arg) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1538 |                     return NULL; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1539 |                 asdl_seq_SET(posargs, k++, arg); | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1540 |                 i += 1; /* the name */ | 
 | 1541 |                 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) | 
 | 1542 |                     i += 1; /* the comma, if present */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 |                 break; | 
 | 1544 |             case STAR: | 
| Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1545 |                 if (i+1 >= NCH(n) || | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1546 |                     (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA | 
 | 1547 |                                        || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1548 |                     ast_error(c, CHILD(n, i), | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1549 |                               "named arguments must follow bare *"); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1550 |                     return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1551 |                 } | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1552 |                 ch = CHILD(n, i+1);  /* tfpdef or COMMA */ | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1553 |                 if (TYPE(ch) == COMMA) { | 
 | 1554 |                     int res = 0; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1555 |                     i += 2; /* now follows keyword only arguments */ | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1556 |  | 
 | 1557 |                     if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { | 
 | 1558 |                         ast_error(c, CHILD(n, i), | 
 | 1559 |                                   "bare * has associated type comment"); | 
 | 1560 |                         return NULL; | 
 | 1561 |                     } | 
 | 1562 |  | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1563 |                     res = handle_keywordonly_args(c, n, i, | 
 | 1564 |                                                   kwonlyargs, kwdefaults); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1565 |                     if (res == -1) return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1566 |                     i = res; /* res has new position to process */ | 
 | 1567 |                 } | 
 | 1568 |                 else { | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1569 |                     vararg = ast_for_arg(c, ch); | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1570 |                     if (!vararg) | 
 | 1571 |                         return NULL; | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1572 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1573 |                 i += 2; /* the star and the name */ | 
 | 1574 |                 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) | 
 | 1575 |                     i += 1; /* the comma, if present */ | 
 | 1576 |  | 
 | 1577 |                 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { | 
 | 1578 |                         vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); | 
 | 1579 |                         if (!vararg->type_comment) | 
 | 1580 |                             return NULL; | 
 | 1581 |                         i += 1; | 
 | 1582 |                     } | 
 | 1583 |  | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1584 |                     if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef | 
 | 1585 |                                     || TYPE(CHILD(n, i)) == vfpdef)) { | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1586 |                         int res = 0; | 
 | 1587 |                         res = handle_keywordonly_args(c, n, i, | 
 | 1588 |                                                       kwonlyargs, kwdefaults); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1589 |                         if (res == -1) return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1590 |                         i = res; /* res has new position to process */ | 
 | 1591 |                     } | 
 | 1592 |                 } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1593 |                 break; | 
 | 1594 |             case DOUBLESTAR: | 
| Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1595 |                 ch = CHILD(n, i+1);  /* tfpdef */ | 
 | 1596 |                 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1597 |                 kwarg = ast_for_arg(c, ch); | 
| Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1598 |                 if (!kwarg) | 
 | 1599 |                     return NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1600 |                 i += 2; /* the double star and the name */ | 
 | 1601 |                 if (TYPE(CHILD(n, i)) == COMMA) | 
 | 1602 |                     i += 1; /* the comma, if present */ | 
 | 1603 |                 break; | 
 | 1604 |             case TYPE_COMMENT: | 
 | 1605 |                 assert(i); | 
 | 1606 |  | 
 | 1607 |                 if (kwarg) | 
 | 1608 |                     arg = kwarg; | 
 | 1609 |  | 
 | 1610 |                 /* arg will be equal to the last argument processed */ | 
 | 1611 |                 arg->type_comment = NEW_TYPE_COMMENT(ch); | 
 | 1612 |                 if (!arg->type_comment) | 
 | 1613 |                     return NULL; | 
 | 1614 |                 i += 1; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1615 |                 break; | 
 | 1616 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1617 |                 PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1618 |                              "unexpected node in varargslist: %d @ %d", | 
 | 1619 |                              TYPE(ch), i); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1620 |                 return NULL; | 
| Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1621 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1622 |     } | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1623 |     return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1624 | } | 
 | 1625 |  | 
 | 1626 | static expr_ty | 
 | 1627 | ast_for_dotted_name(struct compiling *c, const node *n) | 
 | 1628 | { | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1629 |     expr_ty e; | 
 | 1630 |     identifier id; | 
| Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1631 |     int lineno, col_offset; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1632 |     int i; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1633 |     node *ch; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1634 |  | 
 | 1635 |     REQ(n, dotted_name); | 
| Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1636 |  | 
 | 1637 |     lineno = LINENO(n); | 
 | 1638 |     col_offset = n->n_col_offset; | 
 | 1639 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1640 |     ch = CHILD(n, 0); | 
 | 1641 |     id = NEW_IDENTIFIER(ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1642 |     if (!id) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1643 |         return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1644 |     e = Name(id, Load, lineno, col_offset, | 
 | 1645 |              ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1646 |     if (!e) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1647 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1648 |  | 
 | 1649 |     for (i = 2; i < NCH(n); i+=2) { | 
 | 1650 |         id = NEW_IDENTIFIER(CHILD(n, i)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1651 |         if (!id) | 
 | 1652 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1653 |         e = Attribute(e, id, Load, lineno, col_offset, | 
 | 1654 |                       n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1655 |         if (!e) | 
 | 1656 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1657 |     } | 
 | 1658 |  | 
 | 1659 |     return e; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1660 | } | 
 | 1661 |  | 
 | 1662 | static expr_ty | 
 | 1663 | ast_for_decorator(struct compiling *c, const node *n) | 
 | 1664 | { | 
 | 1665 |     /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ | 
 | 1666 |     expr_ty d = NULL; | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1667 |     expr_ty name_expr; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1669 |     REQ(n, decorator); | 
| Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1670 |     REQ(CHILD(n, 0), AT); | 
 | 1671 |     REQ(RCHILD(n, -1), NEWLINE); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1673 |     name_expr = ast_for_dotted_name(c, CHILD(n, 1)); | 
 | 1674 |     if (!name_expr) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1675 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1677 |     if (NCH(n) == 3) { /* No arguments */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1678 |         d = name_expr; | 
 | 1679 |         name_expr = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1680 |     } | 
 | 1681 |     else if (NCH(n) == 5) { /* Call with no arguments */ | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1682 |         d = Call(name_expr, NULL, NULL, LINENO(n), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1683 |                  n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1684 |         if (!d) | 
 | 1685 |             return NULL; | 
 | 1686 |         name_expr = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1687 |     } | 
 | 1688 |     else { | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1689 |         d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1690 |         if (!d) | 
 | 1691 |             return NULL; | 
 | 1692 |         name_expr = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1693 |     } | 
 | 1694 |  | 
 | 1695 |     return d; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1696 | } | 
 | 1697 |  | 
 | 1698 | static asdl_seq* | 
 | 1699 | ast_for_decorators(struct compiling *c, const node *n) | 
 | 1700 | { | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1701 |     asdl_seq* decorator_seq; | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1702 |     expr_ty d; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1703 |     int i; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1705 |     REQ(n, decorators); | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1706 |     decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1707 |     if (!decorator_seq) | 
 | 1708 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1710 |     for (i = 0; i < NCH(n); i++) { | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1711 |         d = ast_for_decorator(c, CHILD(n, i)); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1712 |         if (!d) | 
 | 1713 |             return NULL; | 
 | 1714 |         asdl_seq_SET(decorator_seq, i, d); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1715 |     } | 
 | 1716 |     return decorator_seq; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1717 | } | 
 | 1718 |  | 
 | 1719 | static stmt_ty | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1720 | ast_for_funcdef_impl(struct compiling *c, const node *n0, | 
 | 1721 |                      asdl_seq *decorator_seq, bool is_async) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1722 | { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1723 |     /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1724 |     const node * const n = is_async ? CHILD(n0, 1) : n0; | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1725 |     identifier name; | 
 | 1726 |     arguments_ty args; | 
 | 1727 |     asdl_seq *body; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1728 |     expr_ty returns = NULL; | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1729 |     int name_i = 1; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1730 |     int end_lineno, end_col_offset; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1731 |     node *tc; | 
 | 1732 |     string type_comment = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1733 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1734 |     if (is_async && c->c_feature_version < 5) { | 
 | 1735 |         ast_error(c, n, | 
 | 1736 |                   "Async functions are only supported in Python 3.5 and greater"); | 
 | 1737 |         return NULL; | 
 | 1738 |     } | 
 | 1739 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1740 |     REQ(n, funcdef); | 
 | 1741 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1742 |     name = NEW_IDENTIFIER(CHILD(n, name_i)); | 
 | 1743 |     if (!name) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1744 |         return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1745 |     if (forbidden_name(c, name, CHILD(n, name_i), 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1746 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1747 |     args = ast_for_arguments(c, CHILD(n, name_i + 1)); | 
 | 1748 |     if (!args) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1749 |         return NULL; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1750 |     if (TYPE(CHILD(n, name_i+2)) == RARROW) { | 
 | 1751 |         returns = ast_for_expr(c, CHILD(n, name_i + 3)); | 
 | 1752 |         if (!returns) | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1753 |             return NULL; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1754 |         name_i += 2; | 
 | 1755 |     } | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1756 |     if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { | 
 | 1757 |         type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); | 
 | 1758 |         if (!type_comment) | 
 | 1759 |             return NULL; | 
 | 1760 |         name_i += 1; | 
 | 1761 |     } | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1762 |     body = ast_for_suite(c, CHILD(n, name_i + 3)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1763 |     if (!body) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1764 |         return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1765 |     get_last_end_pos(body, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1766 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1767 |     if (NCH(CHILD(n, name_i + 3)) > 1) { | 
 | 1768 |         /* Check if the suite has a type comment in it. */ | 
 | 1769 |         tc = CHILD(CHILD(n, name_i + 3), 1); | 
 | 1770 |  | 
 | 1771 |         if (TYPE(tc) == TYPE_COMMENT) { | 
 | 1772 |             if (type_comment != NULL) { | 
 | 1773 |                 ast_error(c, n, "Cannot have two type comments on def"); | 
 | 1774 |                 return NULL; | 
 | 1775 |             } | 
 | 1776 |             type_comment = NEW_TYPE_COMMENT(tc); | 
 | 1777 |             if (!type_comment) | 
 | 1778 |                 return NULL; | 
 | 1779 |         } | 
 | 1780 |     } | 
 | 1781 |  | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1782 |     if (is_async) | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1783 |         return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1784 |                                 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1785 |     else | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1786 |         return FunctionDef(name, args, body, decorator_seq, returns, type_comment, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1787 |                            LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1788 | } | 
 | 1789 |  | 
 | 1790 | static stmt_ty | 
 | 1791 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) | 
 | 1792 | { | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1793 |     /* async_funcdef: ASYNC funcdef */ | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1794 |     REQ(n, async_funcdef); | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1795 |     REQ(CHILD(n, 0), ASYNC); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1796 |     REQ(CHILD(n, 1), funcdef); | 
 | 1797 |  | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1798 |     return ast_for_funcdef_impl(c, n, decorator_seq, | 
 | 1799 |                                 true /* is_async */); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1800 | } | 
 | 1801 |  | 
 | 1802 | static stmt_ty | 
 | 1803 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) | 
 | 1804 | { | 
 | 1805 |     /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ | 
 | 1806 |     return ast_for_funcdef_impl(c, n, decorator_seq, | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1807 |                                 false /* is_async */); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1808 | } | 
 | 1809 |  | 
 | 1810 |  | 
 | 1811 | static stmt_ty | 
 | 1812 | ast_for_async_stmt(struct compiling *c, const node *n) | 
 | 1813 | { | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1814 |     /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1815 |     REQ(n, async_stmt); | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1816 |     REQ(CHILD(n, 0), ASYNC); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1817 |  | 
 | 1818 |     switch (TYPE(CHILD(n, 1))) { | 
 | 1819 |         case funcdef: | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1820 |             return ast_for_funcdef_impl(c, n, NULL, | 
 | 1821 |                                         true /* is_async */); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1822 |         case with_stmt: | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1823 |             return ast_for_with_stmt(c, n, | 
 | 1824 |                                      true /* is_async */); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1825 |  | 
 | 1826 |         case for_stmt: | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1827 |             return ast_for_for_stmt(c, n, | 
 | 1828 |                                     true /* is_async */); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1829 |  | 
 | 1830 |         default: | 
 | 1831 |             PyErr_Format(PyExc_SystemError, | 
 | 1832 |                          "invalid async stament: %s", | 
 | 1833 |                          STR(CHILD(n, 1))); | 
 | 1834 |             return NULL; | 
 | 1835 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1836 | } | 
 | 1837 |  | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1838 | static stmt_ty | 
 | 1839 | ast_for_decorated(struct compiling *c, const node *n) | 
 | 1840 | { | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1841 |     /* decorated: decorators (classdef | funcdef | async_funcdef) */ | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1842 |     stmt_ty thing = NULL; | 
 | 1843 |     asdl_seq *decorator_seq = NULL; | 
 | 1844 |  | 
 | 1845 |     REQ(n, decorated); | 
 | 1846 |  | 
 | 1847 |     decorator_seq = ast_for_decorators(c, CHILD(n, 0)); | 
 | 1848 |     if (!decorator_seq) | 
 | 1849 |       return NULL; | 
 | 1850 |  | 
 | 1851 |     assert(TYPE(CHILD(n, 1)) == funcdef || | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1852 |            TYPE(CHILD(n, 1)) == async_funcdef || | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 |            TYPE(CHILD(n, 1)) == classdef); | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1854 |  | 
 | 1855 |     if (TYPE(CHILD(n, 1)) == funcdef) { | 
 | 1856 |       thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); | 
 | 1857 |     } else if (TYPE(CHILD(n, 1)) == classdef) { | 
 | 1858 |       thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1859 |     } else if (TYPE(CHILD(n, 1)) == async_funcdef) { | 
 | 1860 |       thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1861 |     } | 
 | 1862 |     return thing; | 
 | 1863 | } | 
 | 1864 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | static expr_ty | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1866 | ast_for_namedexpr(struct compiling *c, const node *n) | 
 | 1867 | { | 
 | 1868 |     /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* | 
 | 1869 |          ['else' ':' suite] | 
 | 1870 |        namedexpr_test: test [':=' test] | 
 | 1871 |        argument: ( test [comp_for] | | 
 | 1872 |             test ':=' test | | 
 | 1873 |             test '=' test | | 
 | 1874 |             '**' test | | 
 | 1875 |             '*' test ) | 
 | 1876 |     */ | 
 | 1877 |     expr_ty target, value; | 
 | 1878 |  | 
 | 1879 |     target = ast_for_expr(c, CHILD(n, 0)); | 
 | 1880 |     if (!target) | 
 | 1881 |         return NULL; | 
 | 1882 |  | 
 | 1883 |     value = ast_for_expr(c, CHILD(n, 2)); | 
 | 1884 |     if (!value) | 
 | 1885 |         return NULL; | 
 | 1886 |  | 
| Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1887 |     if (target->kind != Name_kind) { | 
 | 1888 |         const char *expr_name = get_expr_name(target); | 
 | 1889 |         if (expr_name != NULL) { | 
 | 1890 |             ast_error(c, n, "cannot use named assignment with %s", expr_name); | 
 | 1891 |         } | 
 | 1892 |         return NULL; | 
 | 1893 |     } | 
 | 1894 |  | 
 | 1895 |     if (!set_context(c, target, Store, n)) | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1896 |         return NULL; | 
 | 1897 |  | 
 | 1898 |     return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, | 
 | 1899 |                      n->n_end_col_offset, c->c_arena); | 
 | 1900 | } | 
 | 1901 |  | 
 | 1902 | static expr_ty | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1903 | ast_for_lambdef(struct compiling *c, const node *n) | 
 | 1904 | { | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1905 |     /* lambdef: 'lambda' [varargslist] ':' test | 
 | 1906 |        lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1907 |     arguments_ty args; | 
 | 1908 |     expr_ty expression; | 
 | 1909 |  | 
 | 1910 |     if (NCH(n) == 3) { | 
| Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1911 |         args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1912 |         if (!args) | 
 | 1913 |             return NULL; | 
 | 1914 |         expression = ast_for_expr(c, CHILD(n, 2)); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1915 |         if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1916 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1917 |     } | 
 | 1918 |     else { | 
 | 1919 |         args = ast_for_arguments(c, CHILD(n, 1)); | 
 | 1920 |         if (!args) | 
 | 1921 |             return NULL; | 
 | 1922 |         expression = ast_for_expr(c, CHILD(n, 3)); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1923 |         if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1924 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 |     } | 
 | 1926 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1927 |     return Lambda(args, expression, LINENO(n), n->n_col_offset, | 
 | 1928 |                   n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1929 | } | 
 | 1930 |  | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1931 | static expr_ty | 
 | 1932 | ast_for_ifexpr(struct compiling *c, const node *n) | 
 | 1933 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 |     /* test: or_test 'if' or_test 'else' test */ | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1935 |     expr_ty expression, body, orelse; | 
 | 1936 |  | 
| Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1937 |     assert(NCH(n) == 5); | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1938 |     body = ast_for_expr(c, CHILD(n, 0)); | 
 | 1939 |     if (!body) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1940 |         return NULL; | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1941 |     expression = ast_for_expr(c, CHILD(n, 2)); | 
 | 1942 |     if (!expression) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1943 |         return NULL; | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1944 |     orelse = ast_for_expr(c, CHILD(n, 4)); | 
 | 1945 |     if (!orelse) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1946 |         return NULL; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1947 |     return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1948 |                  n->n_end_lineno, n->n_end_col_offset, | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1949 |                  c->c_arena); | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1950 | } | 
 | 1951 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | /* | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1953 |    Count the number of 'for' loops in a comprehension. | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1954 |  | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1955 |    Helper for ast_for_comprehension(). | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1956 | */ | 
 | 1957 |  | 
 | 1958 | static int | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1959 | count_comp_fors(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1960 | { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1961 |     int n_fors = 0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1962 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1963 |   count_comp_for: | 
 | 1964 |     n_fors++; | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1965 |     REQ(n, comp_for); | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1966 |     if (NCH(n) == 2) { | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1967 |         REQ(CHILD(n, 0), ASYNC); | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1968 |         n = CHILD(n, 1); | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1969 |     } | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1970 |     else if (NCH(n) == 1) { | 
 | 1971 |         n = CHILD(n, 0); | 
 | 1972 |     } | 
 | 1973 |     else { | 
 | 1974 |         goto error; | 
 | 1975 |     } | 
 | 1976 |     if (NCH(n) == (5)) { | 
 | 1977 |         n = CHILD(n, 4); | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1978 |     } | 
 | 1979 |     else { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1980 |         return n_fors; | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1981 |     } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1982 |   count_comp_iter: | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1983 |     REQ(n, comp_iter); | 
 | 1984 |     n = CHILD(n, 0); | 
 | 1985 |     if (TYPE(n) == comp_for) | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1986 |         goto count_comp_for; | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1987 |     else if (TYPE(n) == comp_if) { | 
 | 1988 |         if (NCH(n) == 3) { | 
 | 1989 |             n = CHILD(n, 2); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1990 |             goto count_comp_iter; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1991 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1992 |         else | 
 | 1993 |             return n_fors; | 
 | 1994 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1995 |  | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1996 |   error: | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1997 |     /* Should never be reached */ | 
 | 1998 |     PyErr_SetString(PyExc_SystemError, | 
 | 1999 |                     "logic error in count_comp_fors"); | 
 | 2000 |     return -1; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2001 | } | 
 | 2002 |  | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2003 | /* Count the number of 'if' statements in a comprehension. | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2004 |  | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2005 |    Helper for ast_for_comprehension(). | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2006 | */ | 
 | 2007 |  | 
 | 2008 | static int | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2009 | count_comp_ifs(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2010 | { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2011 |     int n_ifs = 0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2012 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2013 |     while (1) { | 
 | 2014 |         REQ(n, comp_iter); | 
 | 2015 |         if (TYPE(CHILD(n, 0)) == comp_for) | 
 | 2016 |             return n_ifs; | 
 | 2017 |         n = CHILD(n, 0); | 
 | 2018 |         REQ(n, comp_if); | 
 | 2019 |         n_ifs++; | 
 | 2020 |         if (NCH(n) == 2) | 
 | 2021 |             return n_ifs; | 
 | 2022 |         n = CHILD(n, 2); | 
 | 2023 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2024 | } | 
 | 2025 |  | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2026 | static asdl_seq * | 
 | 2027 | ast_for_comprehension(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2028 | { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2029 |     int i, n_fors; | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2030 |     asdl_seq *comps; | 
 | 2031 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2032 |     n_fors = count_comp_fors(c, n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2033 |     if (n_fors == -1) | 
 | 2034 |         return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2035 |  | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2036 |     comps = _Py_asdl_seq_new(n_fors, c->c_arena); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2037 |     if (!comps) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2038 |         return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2039 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2040 |     for (i = 0; i < n_fors; i++) { | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2041 |         comprehension_ty comp; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2042 |         asdl_seq *t; | 
| Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2043 |         expr_ty expression, first; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2044 |         node *for_ch; | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2045 |         node *sync_n; | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2046 |         int is_async = 0; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 |  | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2048 |         REQ(n, comp_for); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 |  | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2050 |         if (NCH(n) == 2) { | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2051 |             is_async = 1; | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2052 |             REQ(CHILD(n, 0), ASYNC); | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2053 |             sync_n = CHILD(n, 1); | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2054 |         } | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2055 |         else { | 
 | 2056 |             sync_n = CHILD(n, 0); | 
 | 2057 |         } | 
 | 2058 |         REQ(sync_n, sync_comp_for); | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2059 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2060 |         /* Async comprehensions only allowed in Python 3.6 and greater */ | 
 | 2061 |         if (is_async && c->c_feature_version < 6) { | 
 | 2062 |             ast_error(c, n, | 
 | 2063 |                       "Async comprehensions are only supported in Python 3.6 and greater"); | 
 | 2064 |             return NULL; | 
 | 2065 |         } | 
 | 2066 |  | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2067 |         for_ch = CHILD(sync_n, 1); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2068 |         t = ast_for_exprlist(c, for_ch, Store); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2069 |         if (!t) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2070 |             return NULL; | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2071 |         expression = ast_for_expr(c, CHILD(sync_n, 3)); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2072 |         if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2073 |             return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2074 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2075 |         /* Check the # of children rather than the length of t, since | 
 | 2076 |            (x for x, in ...) has 1 element in t, but still requires a Tuple. */ | 
| Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2077 |         first = (expr_ty)asdl_seq_GET(t, 0); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2078 |         if (NCH(for_ch) == 1) | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2079 |             comp = comprehension(first, expression, NULL, | 
 | 2080 |                                  is_async, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2081 |         else | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2082 |             comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, | 
 | 2083 |                                        for_ch->n_end_lineno, for_ch->n_end_col_offset, | 
 | 2084 |                                        c->c_arena), | 
| Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2085 |                                  expression, NULL, is_async, c->c_arena); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2086 |         if (!comp) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2087 |             return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2088 |  | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2089 |         if (NCH(sync_n) == 5) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2090 |             int j, n_ifs; | 
 | 2091 |             asdl_seq *ifs; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 |  | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2093 |             n = CHILD(sync_n, 4); | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2094 |             n_ifs = count_comp_ifs(c, n); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2095 |             if (n_ifs == -1) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2096 |                 return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2097 |  | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2098 |             ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2099 |             if (!ifs) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2100 |                 return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2101 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2102 |             for (j = 0; j < n_ifs; j++) { | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2103 |                 REQ(n, comp_iter); | 
 | 2104 |                 n = CHILD(n, 0); | 
 | 2105 |                 REQ(n, comp_if); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 |  | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2107 |                 expression = ast_for_expr(c, CHILD(n, 1)); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2108 |                 if (!expression) | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2109 |                     return NULL; | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2110 |                 asdl_seq_SET(ifs, j, expression); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2111 |                 if (NCH(n) == 3) | 
 | 2112 |                     n = CHILD(n, 2); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2113 |             } | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2114 |             /* on exit, must guarantee that n is a comp_for */ | 
 | 2115 |             if (TYPE(n) == comp_iter) | 
 | 2116 |                 n = CHILD(n, 0); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2117 |             comp->ifs = ifs; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2118 |         } | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2119 |         asdl_seq_SET(comps, i, comp); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2120 |     } | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2121 |     return comps; | 
 | 2122 | } | 
 | 2123 |  | 
 | 2124 | static expr_ty | 
 | 2125 | ast_for_itercomp(struct compiling *c, const node *n, int type) | 
 | 2126 | { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2127 |     /* testlist_comp: (test|star_expr) | 
 | 2128 |      *                ( comp_for | (',' (test|star_expr))* [','] ) */ | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2129 |     expr_ty elt; | 
 | 2130 |     asdl_seq *comps; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2131 |     node *ch; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 |  | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2133 |     assert(NCH(n) > 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2135 |     ch = CHILD(n, 0); | 
 | 2136 |     elt = ast_for_expr(c, ch); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2137 |     if (!elt) | 
 | 2138 |         return NULL; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2139 |     if (elt->kind == Starred_kind) { | 
 | 2140 |         ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); | 
 | 2141 |         return NULL; | 
 | 2142 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 |  | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2144 |     comps = ast_for_comprehension(c, CHILD(n, 1)); | 
 | 2145 |     if (!comps) | 
 | 2146 |         return NULL; | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2147 |  | 
 | 2148 |     if (type == COMP_GENEXP) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2149 |         return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, | 
 | 2150 |                             n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2151 |     else if (type == COMP_LISTCOMP) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2152 |         return ListComp(elt, comps, LINENO(n), n->n_col_offset, | 
 | 2153 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2154 |     else if (type == COMP_SETCOMP) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2155 |         return SetComp(elt, comps, LINENO(n), n->n_col_offset, | 
 | 2156 |                        n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2157 |     else | 
 | 2158 |         /* Should never happen */ | 
 | 2159 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2160 | } | 
 | 2161 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2162 | /* Fills in the key, value pair corresponding to the dict element.  In case | 
 | 2163 |  * of an unpacking, key is NULL.  *i is advanced by the number of ast | 
 | 2164 |  * elements.  Iff successful, nonzero is returned. | 
 | 2165 |  */ | 
 | 2166 | static int | 
 | 2167 | ast_for_dictelement(struct compiling *c, const node *n, int *i, | 
 | 2168 |                     expr_ty *key, expr_ty *value) | 
 | 2169 | { | 
 | 2170 |     expr_ty expression; | 
 | 2171 |     if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { | 
 | 2172 |         assert(NCH(n) - *i >= 2); | 
 | 2173 |  | 
 | 2174 |         expression = ast_for_expr(c, CHILD(n, *i + 1)); | 
 | 2175 |         if (!expression) | 
 | 2176 |             return 0; | 
 | 2177 |         *key = NULL; | 
 | 2178 |         *value = expression; | 
 | 2179 |  | 
 | 2180 |         *i += 2; | 
 | 2181 |     } | 
 | 2182 |     else { | 
 | 2183 |         assert(NCH(n) - *i >= 3); | 
 | 2184 |  | 
 | 2185 |         expression = ast_for_expr(c, CHILD(n, *i)); | 
 | 2186 |         if (!expression) | 
 | 2187 |             return 0; | 
 | 2188 |         *key = expression; | 
 | 2189 |  | 
 | 2190 |         REQ(CHILD(n, *i + 1), COLON); | 
 | 2191 |  | 
 | 2192 |         expression = ast_for_expr(c, CHILD(n, *i + 2)); | 
 | 2193 |         if (!expression) | 
 | 2194 |             return 0; | 
 | 2195 |         *value = expression; | 
 | 2196 |  | 
 | 2197 |         *i += 3; | 
 | 2198 |     } | 
 | 2199 |     return 1; | 
 | 2200 | } | 
 | 2201 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2202 | static expr_ty | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2203 | ast_for_dictcomp(struct compiling *c, const node *n) | 
 | 2204 | { | 
 | 2205 |     expr_ty key, value; | 
 | 2206 |     asdl_seq *comps; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2207 |     int i = 0; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2209 |     if (!ast_for_dictelement(c, n, &i, &key, &value)) | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2210 |         return NULL; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2211 |     assert(key); | 
 | 2212 |     assert(NCH(n) - i >= 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2214 |     comps = ast_for_comprehension(c, CHILD(n, i)); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2215 |     if (!comps) | 
 | 2216 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2218 |     return DictComp(key, value, comps, LINENO(n), n->n_col_offset, | 
 | 2219 |                     n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2220 | } | 
 | 2221 |  | 
 | 2222 | static expr_ty | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2223 | ast_for_dictdisplay(struct compiling *c, const node *n) | 
 | 2224 | { | 
 | 2225 |     int i; | 
 | 2226 |     int j; | 
 | 2227 |     int size; | 
 | 2228 |     asdl_seq *keys, *values; | 
 | 2229 |  | 
 | 2230 |     size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ | 
 | 2231 |     keys = _Py_asdl_seq_new(size, c->c_arena); | 
 | 2232 |     if (!keys) | 
 | 2233 |         return NULL; | 
 | 2234 |  | 
 | 2235 |     values = _Py_asdl_seq_new(size, c->c_arena); | 
 | 2236 |     if (!values) | 
 | 2237 |         return NULL; | 
 | 2238 |  | 
 | 2239 |     j = 0; | 
 | 2240 |     for (i = 0; i < NCH(n); i++) { | 
 | 2241 |         expr_ty key, value; | 
 | 2242 |  | 
 | 2243 |         if (!ast_for_dictelement(c, n, &i, &key, &value)) | 
 | 2244 |             return NULL; | 
 | 2245 |         asdl_seq_SET(keys, j, key); | 
 | 2246 |         asdl_seq_SET(values, j, value); | 
 | 2247 |  | 
 | 2248 |         j++; | 
 | 2249 |     } | 
 | 2250 |     keys->size = j; | 
 | 2251 |     values->size = j; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2252 |     return Dict(keys, values, LINENO(n), n->n_col_offset, | 
 | 2253 |                 n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2254 | } | 
 | 2255 |  | 
 | 2256 | static expr_ty | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2257 | ast_for_genexp(struct compiling *c, const node *n) | 
 | 2258 | { | 
 | 2259 |     assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2260 |     return ast_for_itercomp(c, n, COMP_GENEXP); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2261 | } | 
 | 2262 |  | 
 | 2263 | static expr_ty | 
 | 2264 | ast_for_listcomp(struct compiling *c, const node *n) | 
 | 2265 | { | 
 | 2266 |     assert(TYPE(n) == (testlist_comp)); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2267 |     return ast_for_itercomp(c, n, COMP_LISTCOMP); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2268 | } | 
 | 2269 |  | 
 | 2270 | static expr_ty | 
 | 2271 | ast_for_setcomp(struct compiling *c, const node *n) | 
 | 2272 | { | 
 | 2273 |     assert(TYPE(n) == (dictorsetmaker)); | 
| Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2274 |     return ast_for_itercomp(c, n, COMP_SETCOMP); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2275 | } | 
 | 2276 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2277 | static expr_ty | 
 | 2278 | ast_for_setdisplay(struct compiling *c, const node *n) | 
 | 2279 | { | 
 | 2280 |     int i; | 
 | 2281 |     int size; | 
 | 2282 |     asdl_seq *elts; | 
 | 2283 |  | 
 | 2284 |     assert(TYPE(n) == (dictorsetmaker)); | 
 | 2285 |     size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ | 
 | 2286 |     elts = _Py_asdl_seq_new(size, c->c_arena); | 
 | 2287 |     if (!elts) | 
 | 2288 |         return NULL; | 
 | 2289 |     for (i = 0; i < NCH(n); i += 2) { | 
 | 2290 |         expr_ty expression; | 
 | 2291 |         expression = ast_for_expr(c, CHILD(n, i)); | 
 | 2292 |         if (!expression) | 
 | 2293 |             return NULL; | 
 | 2294 |         asdl_seq_SET(elts, i / 2, expression); | 
 | 2295 |     } | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2296 |     return Set(elts, LINENO(n), n->n_col_offset, | 
 | 2297 |                n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2298 | } | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2299 |  | 
 | 2300 | static expr_ty | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2301 | ast_for_atom(struct compiling *c, const node *n) | 
 | 2302 | { | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2303 |     /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' | 
 | 2304 |        | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ | 
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2305 |        | '...' | 'None' | 'True' | 'False' | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2306 |     */ | 
 | 2307 |     node *ch = CHILD(n, 0); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2309 |     switch (TYPE(ch)) { | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2310 |     case NAME: { | 
| Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2311 |         PyObject *name; | 
 | 2312 |         const char *s = STR(ch); | 
 | 2313 |         size_t len = strlen(s); | 
 | 2314 |         if (len >= 4 && len <= 5) { | 
 | 2315 |             if (!strcmp(s, "None")) | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2316 |                 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2317 |                                 n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2318 |             if (!strcmp(s, "True")) | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2319 |                 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2320 |                                 n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2321 |             if (!strcmp(s, "False")) | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2322 |                 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2323 |                                 n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2324 |         } | 
 | 2325 |         name = new_identifier(s, c); | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2326 |         if (!name) | 
 | 2327 |             return NULL; | 
| Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2328 |         /* All names start in Load context, but may later be changed. */ | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2329 |         return Name(name, Load, LINENO(n), n->n_col_offset, | 
 | 2330 |                     n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2331 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2332 |     case STRING: { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2333 |         expr_ty str = parsestrplus(c, n); | 
| Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2334 |         if (!str) { | 
| Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2335 |             const char *errtype = NULL; | 
 | 2336 |             if (PyErr_ExceptionMatches(PyExc_UnicodeError)) | 
 | 2337 |                 errtype = "unicode error"; | 
 | 2338 |             else if (PyErr_ExceptionMatches(PyExc_ValueError)) | 
 | 2339 |                 errtype = "value error"; | 
 | 2340 |             if (errtype) { | 
| Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2341 |                 PyObject *type, *value, *tback, *errstr; | 
 | 2342 |                 PyErr_Fetch(&type, &value, &tback); | 
| Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2343 |                 errstr = PyObject_Str(value); | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2344 |                 if (errstr) { | 
 | 2345 |                     ast_error(c, n, "(%s) %U", errtype, errstr); | 
 | 2346 |                     Py_DECREF(errstr); | 
| Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2347 |                 } | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2348 |                 else { | 
 | 2349 |                     PyErr_Clear(); | 
 | 2350 |                     ast_error(c, n, "(%s) unknown error", errtype); | 
 | 2351 |                 } | 
| Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2352 |                 Py_DECREF(type); | 
| Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2353 |                 Py_XDECREF(value); | 
| Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2354 |                 Py_XDECREF(tback); | 
 | 2355 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2356 |             return NULL; | 
| Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2357 |         } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2358 |         return str; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2359 |     } | 
 | 2360 |     case NUMBER: { | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2361 |         PyObject *pynum; | 
 | 2362 |         /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ | 
 | 2363 |         /* Check for underscores here rather than in parse_number so we can report a line number on error */ | 
 | 2364 |         if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { | 
 | 2365 |             ast_error(c, ch, | 
 | 2366 |                       "Underscores in numeric literals are only supported in Python 3.6 and greater"); | 
 | 2367 |             return NULL; | 
 | 2368 |         } | 
 | 2369 |         pynum = parsenumber(c, STR(ch)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2370 |         if (!pynum) | 
 | 2371 |             return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2372 |  | 
| Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2373 |         if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { | 
 | 2374 |             Py_DECREF(pynum); | 
 | 2375 |             return NULL; | 
 | 2376 |         } | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2377 |         return Constant(pynum, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2378 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2379 |     } | 
| Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2380 |     case ELLIPSIS: /* Ellipsis */ | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2381 |         return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2382 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2383 |     case LPAR: /* some parenthesized expressions */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2384 |         ch = CHILD(n, 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2386 |         if (TYPE(ch) == RPAR) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2387 |             return Tuple(NULL, Load, LINENO(n), n->n_col_offset, | 
 | 2388 |                          n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2389 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2390 |         if (TYPE(ch) == yield_expr) | 
 | 2391 |             return ast_for_expr(c, ch); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2392 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 |         /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2394 |         if (NCH(ch) == 1) { | 
 | 2395 |             return ast_for_testlist(c, ch); | 
 | 2396 |         } | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2397 |  | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2398 |         if (TYPE(CHILD(ch, 1)) == comp_for) { | 
 | 2399 |             return copy_location(ast_for_genexp(c, ch), n); | 
 | 2400 |         } | 
 | 2401 |         else { | 
 | 2402 |             return copy_location(ast_for_testlist(c, ch), n); | 
 | 2403 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2404 |     case LSQB: /* list (or list comprehension) */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2405 |         ch = CHILD(n, 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2407 |         if (TYPE(ch) == RSQB) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2408 |             return List(NULL, Load, LINENO(n), n->n_col_offset, | 
 | 2409 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 |  | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2411 |         REQ(ch, testlist_comp); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2412 |         if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { | 
 | 2413 |             asdl_seq *elts = seq_for_testlist(c, ch); | 
 | 2414 |             if (!elts) | 
 | 2415 |                 return NULL; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2416 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2417 |             return List(elts, Load, LINENO(n), n->n_col_offset, | 
 | 2418 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2419 |         } | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2420 |         else { | 
 | 2421 |             return copy_location(ast_for_listcomp(c, ch), n); | 
 | 2422 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2423 |     case LBRACE: { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2424 |         /* dictorsetmaker: ( ((test ':' test | '**' test) | 
 | 2425 |          *                    (comp_for | (',' (test ':' test | '**' test))* [','])) | | 
 | 2426 |          *                   ((test | '*' test) | 
 | 2427 |          *                    (comp_for | (',' (test | '*' test))* [','])) ) */ | 
| Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2428 |         expr_ty res; | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2429 |         ch = CHILD(n, 1); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2430 |         if (TYPE(ch) == RBRACE) { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2431 |             /* It's an empty dict. */ | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2432 |             return Dict(NULL, NULL, LINENO(n), n->n_col_offset, | 
 | 2433 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2434 |         } | 
 | 2435 |         else { | 
 | 2436 |             int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); | 
 | 2437 |             if (NCH(ch) == 1 || | 
 | 2438 |                     (NCH(ch) > 1 && | 
 | 2439 |                      TYPE(CHILD(ch, 1)) == COMMA)) { | 
 | 2440 |                 /* It's a set display. */ | 
| Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2441 |                 res = ast_for_setdisplay(c, ch); | 
| Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2442 |             } | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2443 |             else if (NCH(ch) > 1 && | 
 | 2444 |                     TYPE(CHILD(ch, 1)) == comp_for) { | 
 | 2445 |                 /* It's a set comprehension. */ | 
| Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2446 |                 res = ast_for_setcomp(c, ch); | 
| Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2447 |             } | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2448 |             else if (NCH(ch) > 3 - is_dict && | 
 | 2449 |                     TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { | 
 | 2450 |                 /* It's a dictionary comprehension. */ | 
 | 2451 |                 if (is_dict) { | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2452 |                     ast_error(c, n, | 
 | 2453 |                               "dict unpacking cannot be used in dict comprehension"); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2454 |                     return NULL; | 
 | 2455 |                 } | 
| Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2456 |                 res = ast_for_dictcomp(c, ch); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2457 |             } | 
 | 2458 |             else { | 
 | 2459 |                 /* It's a dictionary display. */ | 
| Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2460 |                 res = ast_for_dictdisplay(c, ch); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2461 |             } | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2462 |             return copy_location(res, n); | 
| Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2463 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2464 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2465 |     default: | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2466 |         PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); | 
 | 2467 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2468 |     } | 
 | 2469 | } | 
 | 2470 |  | 
 | 2471 | static slice_ty | 
 | 2472 | ast_for_slice(struct compiling *c, const node *n) | 
 | 2473 | { | 
 | 2474 |     node *ch; | 
 | 2475 |     expr_ty lower = NULL, upper = NULL, step = NULL; | 
 | 2476 |  | 
 | 2477 |     REQ(n, subscript); | 
 | 2478 |  | 
 | 2479 |     /* | 
| Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2480 |        subscript: test | [test] ':' [test] [sliceop] | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2481 |        sliceop: ':' [test] | 
 | 2482 |     */ | 
 | 2483 |     ch = CHILD(n, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2484 |     if (NCH(n) == 1 && TYPE(ch) == test) { | 
 | 2485 |         /* 'step' variable hold no significance in terms of being used over | 
 | 2486 |            other vars */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 |         step = ast_for_expr(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2488 |         if (!step) | 
 | 2489 |             return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2490 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2491 |         return Index(step, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2492 |     } | 
 | 2493 |  | 
 | 2494 |     if (TYPE(ch) == test) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2495 |         lower = ast_for_expr(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2496 |         if (!lower) | 
 | 2497 |             return NULL; | 
 | 2498 |     } | 
 | 2499 |  | 
 | 2500 |     /* If there's an upper bound it's in the second or third position. */ | 
 | 2501 |     if (TYPE(ch) == COLON) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2502 |         if (NCH(n) > 1) { | 
 | 2503 |             node *n2 = CHILD(n, 1); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2504 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2505 |             if (TYPE(n2) == test) { | 
 | 2506 |                 upper = ast_for_expr(c, n2); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2507 |                 if (!upper) | 
 | 2508 |                     return NULL; | 
 | 2509 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2510 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2511 |     } else if (NCH(n) > 2) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2512 |         node *n2 = CHILD(n, 2); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2513 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2514 |         if (TYPE(n2) == test) { | 
 | 2515 |             upper = ast_for_expr(c, n2); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2516 |             if (!upper) | 
 | 2517 |                 return NULL; | 
 | 2518 |         } | 
 | 2519 |     } | 
 | 2520 |  | 
 | 2521 |     ch = CHILD(n, NCH(n) - 1); | 
 | 2522 |     if (TYPE(ch) == sliceop) { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2523 |         if (NCH(ch) != 1) { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2524 |             ch = CHILD(ch, 1); | 
 | 2525 |             if (TYPE(ch) == test) { | 
 | 2526 |                 step = ast_for_expr(c, ch); | 
 | 2527 |                 if (!step) | 
 | 2528 |                     return NULL; | 
 | 2529 |             } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2530 |         } | 
 | 2531 |     } | 
 | 2532 |  | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2533 |     return Slice(lower, upper, step, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2534 | } | 
 | 2535 |  | 
 | 2536 | static expr_ty | 
 | 2537 | ast_for_binop(struct compiling *c, const node *n) | 
 | 2538 | { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2539 |     /* Must account for a sequence of expressions. | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 |        How should A op B op C by represented? | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2541 |        BinOp(BinOp(A, op, B), op, C). | 
 | 2542 |     */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2543 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2544 |     int i, nops; | 
 | 2545 |     expr_ty expr1, expr2, result; | 
 | 2546 |     operator_ty newoperator; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2547 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2548 |     expr1 = ast_for_expr(c, CHILD(n, 0)); | 
 | 2549 |     if (!expr1) | 
 | 2550 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2551 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2552 |     expr2 = ast_for_expr(c, CHILD(n, 2)); | 
 | 2553 |     if (!expr2) | 
 | 2554 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2555 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2556 |     newoperator = get_operator(c, CHILD(n, 1)); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2557 |     if (!newoperator) | 
 | 2558 |         return NULL; | 
 | 2559 |  | 
 | 2560 |     result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2561 |                    CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset, | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2562 |                    c->c_arena); | 
 | 2563 |     if (!result) | 
 | 2564 |         return NULL; | 
 | 2565 |  | 
 | 2566 |     nops = (NCH(n) - 1) / 2; | 
 | 2567 |     for (i = 1; i < nops; i++) { | 
 | 2568 |         expr_ty tmp_result, tmp; | 
 | 2569 |         const node* next_oper = CHILD(n, i * 2 + 1); | 
 | 2570 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2571 |         newoperator = get_operator(c, next_oper); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2572 |         if (!newoperator) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2573 |             return NULL; | 
 | 2574 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2575 |         tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); | 
 | 2576 |         if (!tmp) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2577 |             return NULL; | 
 | 2578 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 |         tmp_result = BinOp(result, newoperator, tmp, | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2580 |                            LINENO(next_oper), next_oper->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2581 |                            CHILD(n, i * 2 + 2)->n_end_lineno, | 
 | 2582 |                            CHILD(n, i * 2 + 2)->n_end_col_offset, | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2583 |                            c->c_arena); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 |         if (!tmp_result) | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2585 |             return NULL; | 
 | 2586 |         result = tmp_result; | 
 | 2587 |     } | 
 | 2588 |     return result; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2589 | } | 
 | 2590 |  | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2591 | static expr_ty | 
 | 2592 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) | 
 | 2593 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 |     /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2595 |        subscriptlist: subscript (',' subscript)* [','] | 
 | 2596 |        subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] | 
 | 2597 |      */ | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2598 |     const node *n_copy = n; | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2599 |     REQ(n, trailer); | 
 | 2600 |     if (TYPE(CHILD(n, 0)) == LPAR) { | 
 | 2601 |         if (NCH(n) == 2) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2602 |             return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, | 
 | 2603 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2604 |         else | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2605 |             return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2)); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2606 |     } | 
| Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2607 |     else if (TYPE(CHILD(n, 0)) == DOT) { | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2608 |         PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); | 
 | 2609 |         if (!attr_id) | 
 | 2610 |             return NULL; | 
 | 2611 |         return Attribute(left_expr, attr_id, Load, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2612 |                          LINENO(n), n->n_col_offset, | 
 | 2613 |                          n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2614 |     } | 
 | 2615 |     else { | 
 | 2616 |         REQ(CHILD(n, 0), LSQB); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2617 |         REQ(CHILD(n, 2), RSQB); | 
 | 2618 |         n = CHILD(n, 1); | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2619 |         if (NCH(n) == 1) { | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2620 |             slice_ty slc = ast_for_slice(c, CHILD(n, 0)); | 
 | 2621 |             if (!slc) | 
 | 2622 |                 return NULL; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2623 |             return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2624 |                              n_copy->n_end_lineno, n_copy->n_end_col_offset, | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2625 |                              c->c_arena); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2626 |         } | 
 | 2627 |         else { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 |             /* The grammar is ambiguous here. The ambiguity is resolved | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2629 |                by treating the sequence as a tuple literal if there are | 
 | 2630 |                no slice features. | 
 | 2631 |             */ | 
| Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2632 |             Py_ssize_t j; | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2633 |             slice_ty slc; | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2634 |             expr_ty e; | 
| Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2635 |             int simple = 1; | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2636 |             asdl_seq *slices, *elts; | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2637 |             slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2638 |             if (!slices) | 
 | 2639 |                 return NULL; | 
 | 2640 |             for (j = 0; j < NCH(n); j += 2) { | 
 | 2641 |                 slc = ast_for_slice(c, CHILD(n, j)); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2642 |                 if (!slc) | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2643 |                     return NULL; | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2644 |                 if (slc->kind != Index_kind) | 
| Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2645 |                     simple = 0; | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2646 |                 asdl_seq_SET(slices, j / 2, slc); | 
 | 2647 |             } | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2648 |             if (!simple) { | 
 | 2649 |                 return Subscript(left_expr, ExtSlice(slices, c->c_arena), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2650 |                                  Load, LINENO(n), n->n_col_offset, | 
 | 2651 |                                  n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2652 |             } | 
 | 2653 |             /* extract Index values and put them in a Tuple */ | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2654 |             elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2655 |             if (!elts) | 
 | 2656 |                 return NULL; | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2657 |             for (j = 0; j < asdl_seq_LEN(slices); ++j) { | 
 | 2658 |                 slc = (slice_ty)asdl_seq_GET(slices, j); | 
 | 2659 |                 assert(slc->kind == Index_kind  && slc->v.Index.value); | 
 | 2660 |                 asdl_seq_SET(elts, j, slc->v.Index.value); | 
 | 2661 |             } | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2662 |             e = Tuple(elts, Load, LINENO(n), n->n_col_offset, | 
 | 2663 |                       n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2664 |             if (!e) | 
 | 2665 |                 return NULL; | 
 | 2666 |             return Subscript(left_expr, Index(e, c->c_arena), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2667 |                              Load, LINENO(n), n->n_col_offset, | 
 | 2668 |                              n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2669 |         } | 
 | 2670 |     } | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2671 | } | 
 | 2672 |  | 
 | 2673 | static expr_ty | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2674 | ast_for_factor(struct compiling *c, const node *n) | 
 | 2675 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2676 |     expr_ty expression; | 
 | 2677 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2678 |     expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2679 |     if (!expression) | 
 | 2680 |         return NULL; | 
 | 2681 |  | 
 | 2682 |     switch (TYPE(CHILD(n, 0))) { | 
 | 2683 |         case PLUS: | 
 | 2684 |             return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2685 |                            n->n_end_lineno, n->n_end_col_offset, | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2686 |                            c->c_arena); | 
 | 2687 |         case MINUS: | 
 | 2688 |             return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2689 |                            n->n_end_lineno, n->n_end_col_offset, | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2690 |                            c->c_arena); | 
 | 2691 |         case TILDE: | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2692 |             return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, | 
 | 2693 |                            n->n_end_lineno, n->n_end_col_offset, | 
 | 2694 |                            c->c_arena); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2695 |     } | 
 | 2696 |     PyErr_Format(PyExc_SystemError, "unhandled factor: %d", | 
 | 2697 |                  TYPE(CHILD(n, 0))); | 
 | 2698 |     return NULL; | 
 | 2699 | } | 
 | 2700 |  | 
 | 2701 | static expr_ty | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2702 | ast_for_atom_expr(struct compiling *c, const node *n) | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2703 | { | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2704 |     int i, nch, start = 0; | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2705 |     expr_ty e, tmp; | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2706 |  | 
 | 2707 |     REQ(n, atom_expr); | 
 | 2708 |     nch = NCH(n); | 
 | 2709 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2710 |     if (TYPE(CHILD(n, 0)) == AWAIT) { | 
 | 2711 |         if (c->c_feature_version < 5) { | 
 | 2712 |             ast_error(c, n, | 
 | 2713 |                       "Await expressions are only supported in Python 3.5 and greater"); | 
 | 2714 |             return NULL; | 
 | 2715 |         } | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2716 |         start = 1; | 
 | 2717 |         assert(nch > 1); | 
 | 2718 |     } | 
 | 2719 |  | 
 | 2720 |     e = ast_for_atom(c, CHILD(n, start)); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2721 |     if (!e) | 
 | 2722 |         return NULL; | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2723 |     if (nch == 1) | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2724 |         return e; | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2725 |     if (start && nch == 2) { | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2726 |         return Await(e, LINENO(n), n->n_col_offset, | 
 | 2727 |                      n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2728 |     } | 
 | 2729 |  | 
 | 2730 |     for (i = start + 1; i < nch; i++) { | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2731 |         node *ch = CHILD(n, i); | 
 | 2732 |         if (TYPE(ch) != trailer) | 
 | 2733 |             break; | 
 | 2734 |         tmp = ast_for_trailer(c, ch, e); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2735 |         if (!tmp) | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2736 |             return NULL; | 
| Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2737 |         tmp->lineno = e->lineno; | 
 | 2738 |         tmp->col_offset = e->col_offset; | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2739 |         e = tmp; | 
 | 2740 |     } | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2741 |  | 
 | 2742 |     if (start) { | 
| Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2743 |         /* there was an 'await' */ | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2744 |         return Await(e, LINENO(n), n->n_col_offset, | 
 | 2745 |                      n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2746 |     } | 
 | 2747 |     else { | 
 | 2748 |         return e; | 
 | 2749 |     } | 
 | 2750 | } | 
 | 2751 |  | 
 | 2752 | static expr_ty | 
 | 2753 | ast_for_power(struct compiling *c, const node *n) | 
 | 2754 | { | 
 | 2755 |     /* power: atom trailer* ('**' factor)* | 
 | 2756 |      */ | 
 | 2757 |     expr_ty e; | 
 | 2758 |     REQ(n, power); | 
 | 2759 |     e = ast_for_atom_expr(c, CHILD(n, 0)); | 
 | 2760 |     if (!e) | 
 | 2761 |         return NULL; | 
 | 2762 |     if (NCH(n) == 1) | 
 | 2763 |         return e; | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2764 |     if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { | 
 | 2765 |         expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2766 |         if (!f) | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2767 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2768 |         e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, | 
 | 2769 |                   n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2770 |     } | 
 | 2771 |     return e; | 
 | 2772 | } | 
 | 2773 |  | 
| Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2774 | static expr_ty | 
 | 2775 | ast_for_starred(struct compiling *c, const node *n) | 
 | 2776 | { | 
 | 2777 |     expr_ty tmp; | 
 | 2778 |     REQ(n, star_expr); | 
 | 2779 |  | 
 | 2780 |     tmp = ast_for_expr(c, CHILD(n, 1)); | 
 | 2781 |     if (!tmp) | 
 | 2782 |         return NULL; | 
 | 2783 |  | 
 | 2784 |     /* The Load context is changed later. */ | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2785 |     return Starred(tmp, Load, LINENO(n), n->n_col_offset, | 
 | 2786 |                    n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2787 | } | 
 | 2788 |  | 
 | 2789 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2790 | /* Do not name a variable 'expr'!  Will cause a compile error. | 
 | 2791 | */ | 
 | 2792 |  | 
 | 2793 | static expr_ty | 
 | 2794 | ast_for_expr(struct compiling *c, const node *n) | 
 | 2795 | { | 
 | 2796 |     /* handle the full range of simple expressions | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2797 |        namedexpr_test: test [':=' test] | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2798 |        test: or_test ['if' or_test 'else' test] | lambdef | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2799 |        test_nocond: or_test | lambdef_nocond | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 |        or_test: and_test ('or' and_test)* | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2801 |        and_test: not_test ('and' not_test)* | 
 | 2802 |        not_test: 'not' not_test | comparison | 
 | 2803 |        comparison: expr (comp_op expr)* | 
 | 2804 |        expr: xor_expr ('|' xor_expr)* | 
 | 2805 |        xor_expr: and_expr ('^' and_expr)* | 
 | 2806 |        and_expr: shift_expr ('&' shift_expr)* | 
 | 2807 |        shift_expr: arith_expr (('<<'|'>>') arith_expr)* | 
 | 2808 |        arith_expr: term (('+'|'-') term)* | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2809 |        term: factor (('*'|'@'|'/'|'%'|'//') factor)* | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2810 |        factor: ('+'|'-'|'~') factor | power | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2811 |        power: atom_expr ['**' factor] | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2812 |        atom_expr: [AWAIT] atom trailer* | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2813 |        yield_expr: 'yield' [yield_arg] | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2814 |     */ | 
 | 2815 |  | 
 | 2816 |     asdl_seq *seq; | 
 | 2817 |     int i; | 
 | 2818 |  | 
 | 2819 |  loop: | 
 | 2820 |     switch (TYPE(n)) { | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2821 |         case namedexpr_test: | 
 | 2822 |             if (NCH(n) == 3) | 
 | 2823 |                 return ast_for_namedexpr(c, n); | 
 | 2824 |             /* Fallthrough */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 |         case test: | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2826 |         case test_nocond: | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2827 |             if (TYPE(CHILD(n, 0)) == lambdef || | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2828 |                 TYPE(CHILD(n, 0)) == lambdef_nocond) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2829 |                 return ast_for_lambdef(c, CHILD(n, 0)); | 
| Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2830 |             else if (NCH(n) > 1) | 
 | 2831 |                 return ast_for_ifexpr(c, n); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2832 |             /* Fallthrough */ | 
 | 2833 |         case or_test: | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2834 |         case and_test: | 
 | 2835 |             if (NCH(n) == 1) { | 
 | 2836 |                 n = CHILD(n, 0); | 
 | 2837 |                 goto loop; | 
 | 2838 |             } | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2839 |             seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2840 |             if (!seq) | 
 | 2841 |                 return NULL; | 
 | 2842 |             for (i = 0; i < NCH(n); i += 2) { | 
 | 2843 |                 expr_ty e = ast_for_expr(c, CHILD(n, i)); | 
 | 2844 |                 if (!e) | 
 | 2845 |                     return NULL; | 
 | 2846 |                 asdl_seq_SET(seq, i / 2, e); | 
 | 2847 |             } | 
 | 2848 |             if (!strcmp(STR(CHILD(n, 1)), "and")) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2849 |                 return BoolOp(And, seq, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2850 |                               n->n_end_lineno, n->n_end_col_offset, | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2851 |                               c->c_arena); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2852 |             assert(!strcmp(STR(CHILD(n, 1)), "or")); | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2853 |             return BoolOp(Or, seq, LINENO(n), n->n_col_offset, | 
 | 2854 |                           n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2855 |         case not_test: | 
 | 2856 |             if (NCH(n) == 1) { | 
 | 2857 |                 n = CHILD(n, 0); | 
 | 2858 |                 goto loop; | 
 | 2859 |             } | 
 | 2860 |             else { | 
 | 2861 |                 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 2862 |                 if (!expression) | 
 | 2863 |                     return NULL; | 
 | 2864 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2865 |                 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2866 |                                n->n_end_lineno, n->n_end_col_offset, | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2867 |                                c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2868 |             } | 
 | 2869 |         case comparison: | 
 | 2870 |             if (NCH(n) == 1) { | 
 | 2871 |                 n = CHILD(n, 0); | 
 | 2872 |                 goto loop; | 
 | 2873 |             } | 
 | 2874 |             else { | 
 | 2875 |                 expr_ty expression; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2876 |                 asdl_int_seq *ops; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2877 |                 asdl_seq *cmps; | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2878 |                 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2879 |                 if (!ops) | 
 | 2880 |                     return NULL; | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2881 |                 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2882 |                 if (!cmps) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 |                     return NULL; | 
 | 2884 |                 } | 
 | 2885 |                 for (i = 1; i < NCH(n); i += 2) { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2886 |                     cmpop_ty newoperator; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2887 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2888 |                     newoperator = ast_for_comp_op(c, CHILD(n, i)); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2889 |                     if (!newoperator) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 |                         return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2891 |                     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2892 |  | 
 | 2893 |                     expression = ast_for_expr(c, CHILD(n, i + 1)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2894 |                     if (!expression) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2895 |                         return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2896 |                     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2898 |                     asdl_seq_SET(ops, i / 2, newoperator); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2899 |                     asdl_seq_SET(cmps, i / 2, expression); | 
 | 2900 |                 } | 
 | 2901 |                 expression = ast_for_expr(c, CHILD(n, 0)); | 
| Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2902 |                 if (!expression) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2903 |                     return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2904 |                 } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2906 |                 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, | 
 | 2907 |                                n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2908 |             } | 
 | 2909 |             break; | 
 | 2910 |  | 
| Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2911 |         case star_expr: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 |             return ast_for_starred(c, n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2913 |         /* The next five cases all handle BinOps.  The main body of code | 
 | 2914 |            is the same in each case, but the switch turned inside out to | 
 | 2915 |            reuse the code for each type of operator. | 
 | 2916 |          */ | 
 | 2917 |         case expr: | 
 | 2918 |         case xor_expr: | 
 | 2919 |         case and_expr: | 
 | 2920 |         case shift_expr: | 
 | 2921 |         case arith_expr: | 
 | 2922 |         case term: | 
 | 2923 |             if (NCH(n) == 1) { | 
 | 2924 |                 n = CHILD(n, 0); | 
 | 2925 |                 goto loop; | 
 | 2926 |             } | 
 | 2927 |             return ast_for_binop(c, n); | 
 | 2928 |         case yield_expr: { | 
| Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2929 |             node *an = NULL; | 
 | 2930 |             node *en = NULL; | 
 | 2931 |             int is_from = 0; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2932 |             expr_ty exp = NULL; | 
| Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2933 |             if (NCH(n) > 1) | 
 | 2934 |                 an = CHILD(n, 1); /* yield_arg */ | 
 | 2935 |             if (an) { | 
 | 2936 |                 en = CHILD(an, NCH(an) - 1); | 
 | 2937 |                 if (NCH(an) == 2) { | 
 | 2938 |                     is_from = 1; | 
 | 2939 |                     exp = ast_for_expr(c, en); | 
 | 2940 |                 } | 
 | 2941 |                 else | 
 | 2942 |                     exp = ast_for_testlist(c, en); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2943 |                 if (!exp) | 
 | 2944 |                     return NULL; | 
 | 2945 |             } | 
| Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2946 |             if (is_from) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2947 |                 return YieldFrom(exp, LINENO(n), n->n_col_offset, | 
 | 2948 |                                  n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
 | 2949 |             return Yield(exp, LINENO(n), n->n_col_offset, | 
 | 2950 |                          n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2951 |         } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2952 |         case factor: | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2953 |             if (NCH(n) == 1) { | 
 | 2954 |                 n = CHILD(n, 0); | 
 | 2955 |                 goto loop; | 
 | 2956 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2957 |             return ast_for_factor(c, n); | 
| Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2958 |         case power: | 
 | 2959 |             return ast_for_power(c, n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2960 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2961 |             PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2962 |             return NULL; | 
 | 2963 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2964 |     /* should never get here unless if error is set */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2965 |     return NULL; | 
 | 2966 | } | 
 | 2967 |  | 
 | 2968 | static expr_ty | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2969 | ast_for_call(struct compiling *c, const node *n, expr_ty func, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2970 |              const node *maybegenbeg, const node *closepar) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2971 | { | 
 | 2972 |     /* | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2973 |       arglist: argument (',' argument)*  [','] | 
 | 2974 |       argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2975 |     */ | 
 | 2976 |  | 
| Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2977 |     int i, nargs, nkeywords; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2978 |     int ndoublestars; | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2979 |     asdl_seq *args; | 
 | 2980 |     asdl_seq *keywords; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2981 |  | 
 | 2982 |     REQ(n, arglist); | 
 | 2983 |  | 
 | 2984 |     nargs = 0; | 
 | 2985 |     nkeywords = 0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2986 |     for (i = 0; i < NCH(n); i++) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2987 |         node *ch = CHILD(n, i); | 
 | 2988 |         if (TYPE(ch) == argument) { | 
 | 2989 |             if (NCH(ch) == 1) | 
 | 2990 |                 nargs++; | 
| Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2991 |             else if (TYPE(CHILD(ch, 1)) == comp_for) { | 
 | 2992 |                 nargs++; | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2993 |                 if (!maybegenbeg) { | 
| Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2994 |                     ast_error(c, ch, "invalid syntax"); | 
 | 2995 |                     return NULL; | 
 | 2996 |                 } | 
| Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2997 |                 if (NCH(n) > 1) { | 
 | 2998 |                     ast_error(c, ch, "Generator expression must be parenthesized"); | 
 | 2999 |                     return NULL; | 
 | 3000 |                 } | 
 | 3001 |             } | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3002 |             else if (TYPE(CHILD(ch, 0)) == STAR) | 
 | 3003 |                 nargs++; | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3004 |             else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { | 
 | 3005 |                 nargs++; | 
 | 3006 |             } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3007 |             else | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3008 |                 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3009 |                 nkeywords++; | 
 | 3010 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3011 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3012 |  | 
| Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3013 |     args = _Py_asdl_seq_new(nargs, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3014 |     if (!args) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3015 |         return NULL; | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3016 |     keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3017 |     if (!keywords) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3018 |         return NULL; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3019 |  | 
 | 3020 |     nargs = 0;  /* positional arguments + iterable argument unpackings */ | 
 | 3021 |     nkeywords = 0;  /* keyword arguments + keyword argument unpackings */ | 
 | 3022 |     ndoublestars = 0;  /* just keyword argument unpackings */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3023 |     for (i = 0; i < NCH(n); i++) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3024 |         node *ch = CHILD(n, i); | 
 | 3025 |         if (TYPE(ch) == argument) { | 
 | 3026 |             expr_ty e; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3027 |             node *chch = CHILD(ch, 0); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3028 |             if (NCH(ch) == 1) { | 
| Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3029 |                 /* a positional argument */ | 
 | 3030 |                 if (nkeywords) { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3031 |                     if (ndoublestars) { | 
 | 3032 |                         ast_error(c, chch, | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3033 |                                   "positional argument follows " | 
 | 3034 |                                   "keyword argument unpacking"); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3035 |                     } | 
| Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3036 |                     else { | 
 | 3037 |                         ast_error(c, chch, | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3038 |                                   "positional argument follows " | 
 | 3039 |                                   "keyword argument"); | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3040 |                     } | 
| Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3041 |                     return NULL; | 
| Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3042 |                 } | 
| Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3043 |                 e = ast_for_expr(c, chch); | 
 | 3044 |                 if (!e) | 
 | 3045 |                     return NULL; | 
 | 3046 |                 asdl_seq_SET(args, nargs++, e); | 
 | 3047 |             } | 
 | 3048 |             else if (TYPE(chch) == STAR) { | 
 | 3049 |                 /* an iterable argument unpacking */ | 
 | 3050 |                 expr_ty starred; | 
 | 3051 |                 if (ndoublestars) { | 
 | 3052 |                     ast_error(c, chch, | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3053 |                               "iterable argument unpacking follows " | 
 | 3054 |                               "keyword argument unpacking"); | 
| Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3055 |                     return NULL; | 
 | 3056 |                 } | 
 | 3057 |                 e = ast_for_expr(c, CHILD(ch, 1)); | 
 | 3058 |                 if (!e) | 
 | 3059 |                     return NULL; | 
 | 3060 |                 starred = Starred(e, Load, LINENO(chch), | 
 | 3061 |                         chch->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3062 |                         chch->n_end_lineno, chch->n_end_col_offset, | 
| Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3063 |                         c->c_arena); | 
 | 3064 |                 if (!starred) | 
 | 3065 |                     return NULL; | 
 | 3066 |                 asdl_seq_SET(args, nargs++, starred); | 
 | 3067 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3068 |             } | 
 | 3069 |             else if (TYPE(chch) == DOUBLESTAR) { | 
 | 3070 |                 /* a keyword argument unpacking */ | 
 | 3071 |                 keyword_ty kw; | 
 | 3072 |                 i++; | 
 | 3073 |                 e = ast_for_expr(c, CHILD(ch, 1)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3074 |                 if (!e) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3075 |                     return NULL; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3076 |                 kw = keyword(NULL, e, c->c_arena); | 
 | 3077 |                 asdl_seq_SET(keywords, nkeywords++, kw); | 
 | 3078 |                 ndoublestars++; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 |             } | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3080 |             else if (TYPE(CHILD(ch, 1)) == comp_for) { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3081 |                 /* the lone generator expression */ | 
| Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3082 |                 e = copy_location(ast_for_genexp(c, ch), maybegenbeg); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3083 |                 if (!e) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3084 |                     return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3085 |                 asdl_seq_SET(args, nargs++, e); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3086 |             } | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3087 |             else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { | 
 | 3088 |                 /* treat colon equal as positional argument */ | 
 | 3089 |                 if (nkeywords) { | 
 | 3090 |                     if (ndoublestars) { | 
 | 3091 |                         ast_error(c, chch, | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3092 |                                   "positional argument follows " | 
 | 3093 |                                   "keyword argument unpacking"); | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3094 |                     } | 
 | 3095 |                     else { | 
 | 3096 |                         ast_error(c, chch, | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3097 |                                   "positional argument follows " | 
 | 3098 |                                   "keyword argument"); | 
| Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3099 |                     } | 
 | 3100 |                     return NULL; | 
 | 3101 |                 } | 
 | 3102 |                 e = ast_for_namedexpr(c, ch); | 
 | 3103 |                 if (!e) | 
 | 3104 |                     return NULL; | 
 | 3105 |                 asdl_seq_SET(args, nargs++, e); | 
 | 3106 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3107 |             else { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3108 |                 /* a keyword argument */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3109 |                 keyword_ty kw; | 
| Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3110 |                 identifier key, tmp; | 
 | 3111 |                 int k; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 |  | 
| Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3113 |                 // To remain LL(1), the grammar accepts any test (basically, any | 
 | 3114 |                 // expression) in the keyword slot of a call site.  So, we need | 
 | 3115 |                 // to manually enforce that the keyword is a NAME here. | 
 | 3116 |                 static const int name_tree[] = { | 
 | 3117 |                     test, | 
 | 3118 |                     or_test, | 
 | 3119 |                     and_test, | 
 | 3120 |                     not_test, | 
 | 3121 |                     comparison, | 
 | 3122 |                     expr, | 
 | 3123 |                     xor_expr, | 
 | 3124 |                     and_expr, | 
 | 3125 |                     shift_expr, | 
 | 3126 |                     arith_expr, | 
 | 3127 |                     term, | 
 | 3128 |                     factor, | 
 | 3129 |                     power, | 
 | 3130 |                     atom_expr, | 
 | 3131 |                     atom, | 
 | 3132 |                     0, | 
 | 3133 |                 }; | 
 | 3134 |                 node *expr_node = chch; | 
 | 3135 |                 for (int i = 0; name_tree[i]; i++) { | 
 | 3136 |                     if (TYPE(expr_node) != name_tree[i]) | 
 | 3137 |                         break; | 
 | 3138 |                     if (NCH(expr_node) != 1) | 
 | 3139 |                         break; | 
 | 3140 |                     expr_node = CHILD(expr_node, 0); | 
 | 3141 |                 } | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3142 |                 if (TYPE(expr_node) != NAME) { | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3143 |                     ast_error(c, chch, | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3144 |                               "expression cannot contain assignment, " | 
 | 3145 |                               "perhaps you meant \"==\"?"); | 
| Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3146 |                     return NULL; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3147 |                 } | 
| Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3148 |                 key = new_identifier(STR(expr_node), c); | 
 | 3149 |                 if (key == NULL) { | 
| Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3150 |                     return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 |                 } | 
| Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3152 |                 if (forbidden_name(c, key, chch, 1)) { | 
 | 3153 |                     return NULL; | 
 | 3154 |                 } | 
| Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3155 |                 for (k = 0; k < nkeywords; k++) { | 
 | 3156 |                     tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3157 |                     if (tmp && !PyUnicode_Compare(tmp, key)) { | 
 | 3158 |                         ast_error(c, chch, | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3159 |                                   "keyword argument repeated"); | 
| Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3160 |                         return NULL; | 
 | 3161 |                     } | 
 | 3162 |                 } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3163 |                 e = ast_for_expr(c, CHILD(ch, 2)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3164 |                 if (!e) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3165 |                     return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3166 |                 kw = keyword(key, e, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3167 |                 if (!kw) | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3168 |                     return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3169 |                 asdl_seq_SET(keywords, nkeywords++, kw); | 
 | 3170 |             } | 
 | 3171 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3172 |     } | 
 | 3173 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3174 |     return Call(func, args, keywords, func->lineno, func->col_offset, | 
 | 3175 |                 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3176 | } | 
 | 3177 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3178 | static expr_ty | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3179 | ast_for_testlist(struct compiling *c, const node* n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3180 | { | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3181 |     /* testlist_comp: test (comp_for | (',' test)* [',']) */ | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3182 |     /* testlist: test (',' test)* [','] */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3183 |     assert(NCH(n) > 0); | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3184 |     if (TYPE(n) == testlist_comp) { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3185 |         if (NCH(n) > 1) | 
| Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3186 |             assert(TYPE(CHILD(n, 1)) != comp_for); | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3187 |     } | 
 | 3188 |     else { | 
 | 3189 |         assert(TYPE(n) == testlist || | 
| Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3190 |                TYPE(n) == testlist_star_expr); | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3191 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3192 |     if (NCH(n) == 1) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3193 |         return ast_for_expr(c, CHILD(n, 0)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3194 |     else { | 
 | 3195 |         asdl_seq *tmp = seq_for_testlist(c, n); | 
 | 3196 |         if (!tmp) | 
 | 3197 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3198 |         return Tuple(tmp, Load, LINENO(n), n->n_col_offset, | 
 | 3199 |                      n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3200 |     } | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3201 | } | 
 | 3202 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3203 | static stmt_ty | 
 | 3204 | ast_for_expr_stmt(struct compiling *c, const node *n) | 
 | 3205 | { | 
 | 3206 |     REQ(n, expr_stmt); | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3207 |     /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3208 |                      [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) | 
 | 3209 |        annassign: ':' test ['=' (yield_expr|testlist)] | 
 | 3210 |        testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] | 
 | 3211 |        augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | | 
 | 3212 |                    '<<=' | '>>=' | '**=' | '//=') | 
| Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3213 |        test: ... here starts the operator precedence dance | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3214 |      */ | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3215 |     int num = NCH(n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3216 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3217 |     if (num == 1) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3218 |         expr_ty e = ast_for_testlist(c, CHILD(n, 0)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3219 |         if (!e) | 
 | 3220 |             return NULL; | 
 | 3221 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3222 |         return Expr(e, LINENO(n), n->n_col_offset, | 
 | 3223 |                     n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3224 |     } | 
 | 3225 |     else if (TYPE(CHILD(n, 1)) == augassign) { | 
 | 3226 |         expr_ty expr1, expr2; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3227 |         operator_ty newoperator; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3228 |         node *ch = CHILD(n, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3229 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3230 |         expr1 = ast_for_testlist(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3231 |         if (!expr1) | 
 | 3232 |             return NULL; | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3233 |         if(!set_context(c, expr1, Store, ch)) | 
 | 3234 |             return NULL; | 
| Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3235 |         /* set_context checks that most expressions are not the left side. | 
 | 3236 |           Augmented assignments can only have a name, a subscript, or an | 
 | 3237 |           attribute on the left, though, so we have to explicitly check for | 
 | 3238 |           those. */ | 
 | 3239 |         switch (expr1->kind) { | 
 | 3240 |             case Name_kind: | 
 | 3241 |             case Attribute_kind: | 
 | 3242 |             case Subscript_kind: | 
 | 3243 |                 break; | 
 | 3244 |             default: | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3245 |                 ast_error(c, ch, "illegal expression for augmented assignment"); | 
| Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3246 |                 return NULL; | 
 | 3247 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3248 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3249 |         ch = CHILD(n, 2); | 
 | 3250 |         if (TYPE(ch) == testlist) | 
 | 3251 |             expr2 = ast_for_testlist(c, ch); | 
 | 3252 |         else | 
 | 3253 |             expr2 = ast_for_expr(c, ch); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3254 |         if (!expr2) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3255 |             return NULL; | 
 | 3256 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3257 |         newoperator = ast_for_augassign(c, CHILD(n, 1)); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3258 |         if (!newoperator) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3259 |             return NULL; | 
 | 3260 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3261 |         return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, | 
 | 3262 |                          n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3263 |     } | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3264 |     else if (TYPE(CHILD(n, 1)) == annassign) { | 
 | 3265 |         expr_ty expr1, expr2, expr3; | 
 | 3266 |         node *ch = CHILD(n, 0); | 
 | 3267 |         node *deep, *ann = CHILD(n, 1); | 
 | 3268 |         int simple = 1; | 
 | 3269 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 3270 |         /* AnnAssigns are only allowed in Python 3.6 or greater */ | 
 | 3271 |         if (c->c_feature_version < 6) { | 
 | 3272 |             ast_error(c, ch, | 
 | 3273 |                       "Variable annotation syntax is only supported in Python 3.6 and greater"); | 
 | 3274 |             return NULL; | 
 | 3275 |         } | 
 | 3276 |  | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3277 |         /* we keep track of parens to qualify (x) as expression not name */ | 
 | 3278 |         deep = ch; | 
 | 3279 |         while (NCH(deep) == 1) { | 
 | 3280 |             deep = CHILD(deep, 0); | 
 | 3281 |         } | 
 | 3282 |         if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { | 
 | 3283 |             simple = 0; | 
 | 3284 |         } | 
 | 3285 |         expr1 = ast_for_testlist(c, ch); | 
 | 3286 |         if (!expr1) { | 
 | 3287 |             return NULL; | 
 | 3288 |         } | 
 | 3289 |         switch (expr1->kind) { | 
 | 3290 |             case Name_kind: | 
 | 3291 |                 if (forbidden_name(c, expr1->v.Name.id, n, 0)) { | 
 | 3292 |                     return NULL; | 
 | 3293 |                 } | 
 | 3294 |                 expr1->v.Name.ctx = Store; | 
 | 3295 |                 break; | 
 | 3296 |             case Attribute_kind: | 
 | 3297 |                 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { | 
 | 3298 |                     return NULL; | 
 | 3299 |                 } | 
 | 3300 |                 expr1->v.Attribute.ctx = Store; | 
 | 3301 |                 break; | 
 | 3302 |             case Subscript_kind: | 
 | 3303 |                 expr1->v.Subscript.ctx = Store; | 
 | 3304 |                 break; | 
 | 3305 |             case List_kind: | 
 | 3306 |                 ast_error(c, ch, | 
 | 3307 |                           "only single target (not list) can be annotated"); | 
 | 3308 |                 return NULL; | 
 | 3309 |             case Tuple_kind: | 
 | 3310 |                 ast_error(c, ch, | 
 | 3311 |                           "only single target (not tuple) can be annotated"); | 
 | 3312 |                 return NULL; | 
 | 3313 |             default: | 
 | 3314 |                 ast_error(c, ch, | 
 | 3315 |                           "illegal target for annotation"); | 
 | 3316 |                 return NULL; | 
 | 3317 |         } | 
 | 3318 |  | 
 | 3319 |         if (expr1->kind != Name_kind) { | 
 | 3320 |             simple = 0; | 
 | 3321 |         } | 
 | 3322 |         ch = CHILD(ann, 1); | 
 | 3323 |         expr2 = ast_for_expr(c, ch); | 
 | 3324 |         if (!expr2) { | 
 | 3325 |             return NULL; | 
 | 3326 |         } | 
 | 3327 |         if (NCH(ann) == 2) { | 
 | 3328 |             return AnnAssign(expr1, expr2, NULL, simple, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3329 |                              LINENO(n), n->n_col_offset, | 
 | 3330 |                              n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3331 |         } | 
 | 3332 |         else { | 
 | 3333 |             ch = CHILD(ann, 3); | 
| Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3334 |             if (TYPE(ch) == testlist) { | 
 | 3335 |                 expr3 = ast_for_testlist(c, ch); | 
 | 3336 |             } | 
 | 3337 |             else { | 
 | 3338 |                 expr3 = ast_for_expr(c, ch); | 
 | 3339 |             } | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3340 |             if (!expr3) { | 
 | 3341 |                 return NULL; | 
 | 3342 |             } | 
 | 3343 |             return AnnAssign(expr1, expr2, expr3, simple, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3344 |                              LINENO(n), n->n_col_offset, | 
 | 3345 |                              n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3346 |         } | 
 | 3347 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3348 |     else { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3349 |         int i, nch_minus_type, has_type_comment; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3350 |         asdl_seq *targets; | 
 | 3351 |         node *value; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3352 |         expr_ty expression; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3353 |         string type_comment; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3354 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3355 |         /* a normal assignment */ | 
 | 3356 |         REQ(CHILD(n, 1), EQUAL); | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3357 |  | 
 | 3358 |         has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; | 
 | 3359 |         nch_minus_type = num - has_type_comment; | 
 | 3360 |  | 
 | 3361 |         targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3362 |         if (!targets) | 
 | 3363 |             return NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3364 |         for (i = 0; i < nch_minus_type - 2; i += 2) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3365 |             expr_ty e; | 
 | 3366 |             node *ch = CHILD(n, i); | 
 | 3367 |             if (TYPE(ch) == yield_expr) { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3368 |                 ast_error(c, ch, "assignment to yield expression not possible"); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3369 |                 return NULL; | 
 | 3370 |             } | 
 | 3371 |             e = ast_for_testlist(c, ch); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 |             if (!e) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3373 |               return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3374 |  | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3375 |             /* set context to assign */ | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3376 |             if (!set_context(c, e, Store, CHILD(n, i))) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3377 |               return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3378 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3379 |             asdl_seq_SET(targets, i / 2, e); | 
 | 3380 |         } | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3381 |         value = CHILD(n, nch_minus_type - 1); | 
| Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3382 |         if (TYPE(value) == testlist_star_expr) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3383 |             expression = ast_for_testlist(c, value); | 
 | 3384 |         else | 
 | 3385 |             expression = ast_for_expr(c, value); | 
 | 3386 |         if (!expression) | 
 | 3387 |             return NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3388 |         if (has_type_comment) { | 
 | 3389 |             type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); | 
 | 3390 |             if (!type_comment) | 
 | 3391 |                 return NULL; | 
 | 3392 |         } | 
 | 3393 |         else | 
 | 3394 |             type_comment = NULL; | 
 | 3395 |         return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3396 |                       n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3397 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3398 | } | 
 | 3399 |  | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3400 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3401 | static asdl_seq * | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3402 | ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3403 | { | 
 | 3404 |     asdl_seq *seq; | 
 | 3405 |     int i; | 
 | 3406 |     expr_ty e; | 
 | 3407 |  | 
 | 3408 |     REQ(n, exprlist); | 
 | 3409 |  | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3410 |     seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3411 |     if (!seq) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3412 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3413 |     for (i = 0; i < NCH(n); i += 2) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3414 |         e = ast_for_expr(c, CHILD(n, i)); | 
 | 3415 |         if (!e) | 
 | 3416 |             return NULL; | 
 | 3417 |         asdl_seq_SET(seq, i / 2, e); | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3418 |         if (context && !set_context(c, e, context, CHILD(n, i))) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3419 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3420 |     } | 
 | 3421 |     return seq; | 
 | 3422 | } | 
 | 3423 |  | 
 | 3424 | static stmt_ty | 
 | 3425 | ast_for_del_stmt(struct compiling *c, const node *n) | 
 | 3426 | { | 
 | 3427 |     asdl_seq *expr_list; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3429 |     /* del_stmt: 'del' exprlist */ | 
 | 3430 |     REQ(n, del_stmt); | 
 | 3431 |  | 
 | 3432 |     expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); | 
 | 3433 |     if (!expr_list) | 
 | 3434 |         return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3435 |     return Delete(expr_list, LINENO(n), n->n_col_offset, | 
 | 3436 |                   n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3437 | } | 
 | 3438 |  | 
 | 3439 | static stmt_ty | 
 | 3440 | ast_for_flow_stmt(struct compiling *c, const node *n) | 
 | 3441 | { | 
 | 3442 |     /* | 
 | 3443 |       flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | 
 | 3444 |                  | yield_stmt | 
 | 3445 |       break_stmt: 'break' | 
 | 3446 |       continue_stmt: 'continue' | 
 | 3447 |       return_stmt: 'return' [testlist] | 
 | 3448 |       yield_stmt: yield_expr | 
| Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3449 |       yield_expr: 'yield' testlist | 'yield' 'from' test | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3450 |       raise_stmt: 'raise' [test [',' test [',' test]]] | 
 | 3451 |     */ | 
 | 3452 |     node *ch; | 
 | 3453 |  | 
 | 3454 |     REQ(n, flow_stmt); | 
 | 3455 |     ch = CHILD(n, 0); | 
 | 3456 |     switch (TYPE(ch)) { | 
 | 3457 |         case break_stmt: | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3458 |             return Break(LINENO(n), n->n_col_offset, | 
 | 3459 |                          n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3460 |         case continue_stmt: | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3461 |             return Continue(LINENO(n), n->n_col_offset, | 
 | 3462 |                             n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3463 |         case yield_stmt: { /* will reduce to yield_expr */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3464 |             expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); | 
 | 3465 |             if (!exp) | 
 | 3466 |                 return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3467 |             return Expr(exp, LINENO(n), n->n_col_offset, | 
 | 3468 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3469 |         } | 
 | 3470 |         case return_stmt: | 
 | 3471 |             if (NCH(ch) == 1) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3472 |                 return Return(NULL, LINENO(n), n->n_col_offset, | 
 | 3473 |                               n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3474 |             else { | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3475 |                 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3476 |                 if (!expression) | 
 | 3477 |                     return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3478 |                 return Return(expression, LINENO(n), n->n_col_offset, | 
 | 3479 |                               n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3480 |             } | 
 | 3481 |         case raise_stmt: | 
 | 3482 |             if (NCH(ch) == 1) | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3483 |                 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, | 
 | 3484 |                              n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3485 |             else if (NCH(ch) >= 2) { | 
 | 3486 |                 expr_ty cause = NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3487 |                 expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); | 
 | 3488 |                 if (!expression) | 
 | 3489 |                     return NULL; | 
| Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3490 |                 if (NCH(ch) == 4) { | 
 | 3491 |                     cause = ast_for_expr(c, CHILD(ch, 3)); | 
 | 3492 |                     if (!cause) | 
 | 3493 |                         return NULL; | 
 | 3494 |                 } | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3495 |                 return Raise(expression, cause, LINENO(n), n->n_col_offset, | 
 | 3496 |                              n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3497 |             } | 
| Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3498 |             /* fall through */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3499 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3500 |             PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3501 |                          "unexpected flow_stmt: %d", TYPE(ch)); | 
 | 3502 |             return NULL; | 
 | 3503 |     } | 
 | 3504 | } | 
 | 3505 |  | 
 | 3506 | static alias_ty | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3507 | alias_for_import_name(struct compiling *c, const node *n, int store) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3508 | { | 
 | 3509 |     /* | 
| Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3510 |       import_as_name: NAME ['as' NAME] | 
 | 3511 |       dotted_as_name: dotted_name ['as' NAME] | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3512 |       dotted_name: NAME ('.' NAME)* | 
 | 3513 |     */ | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3514 |     identifier str, name; | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3515 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3516 |  loop: | 
 | 3517 |     switch (TYPE(n)) { | 
| Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3518 |         case import_as_name: { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3519 |             node *name_node = CHILD(n, 0); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3520 |             str = NULL; | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3521 |             name = NEW_IDENTIFIER(name_node); | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3522 |             if (!name) | 
 | 3523 |                 return NULL; | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3524 |             if (NCH(n) == 3) { | 
 | 3525 |                 node *str_node = CHILD(n, 2); | 
 | 3526 |                 str = NEW_IDENTIFIER(str_node); | 
 | 3527 |                 if (!str) | 
 | 3528 |                     return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3529 |                 if (store && forbidden_name(c, str, str_node, 0)) | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3530 |                     return NULL; | 
 | 3531 |             } | 
 | 3532 |             else { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3533 |                 if (forbidden_name(c, name, name_node, 0)) | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3534 |                     return NULL; | 
 | 3535 |             } | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3536 |             return alias(name, str, c->c_arena); | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3537 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3538 |         case dotted_as_name: | 
 | 3539 |             if (NCH(n) == 1) { | 
 | 3540 |                 n = CHILD(n, 0); | 
 | 3541 |                 goto loop; | 
 | 3542 |             } | 
 | 3543 |             else { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3544 |                 node *asname_node = CHILD(n, 2); | 
 | 3545 |                 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3546 |                 if (!a) | 
 | 3547 |                     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3548 |                 assert(!a->asname); | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3549 |                 a->asname = NEW_IDENTIFIER(asname_node); | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3550 |                 if (!a->asname) | 
 | 3551 |                     return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3552 |                 if (forbidden_name(c, a->asname, asname_node, 0)) | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3553 |                     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3554 |                 return a; | 
 | 3555 |             } | 
 | 3556 |             break; | 
 | 3557 |         case dotted_name: | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3558 |             if (NCH(n) == 1) { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3559 |                 node *name_node = CHILD(n, 0); | 
 | 3560 |                 name = NEW_IDENTIFIER(name_node); | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3561 |                 if (!name) | 
 | 3562 |                     return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3563 |                 if (store && forbidden_name(c, name, name_node, 0)) | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3564 |                     return NULL; | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3565 |                 return alias(name, NULL, c->c_arena); | 
 | 3566 |             } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3567 |             else { | 
 | 3568 |                 /* Create a string of the form "a.b.c" */ | 
| Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3569 |                 int i; | 
| Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3570 |                 size_t len; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3571 |                 char *s; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 |                 PyObject *uni; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3573 |  | 
 | 3574 |                 len = 0; | 
 | 3575 |                 for (i = 0; i < NCH(n); i += 2) | 
 | 3576 |                     /* length of string plus one for the dot */ | 
 | 3577 |                     len += strlen(STR(CHILD(n, i))) + 1; | 
 | 3578 |                 len--; /* the last name doesn't have a dot */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3579 |                 str = PyBytes_FromStringAndSize(NULL, len); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3580 |                 if (!str) | 
 | 3581 |                     return NULL; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3582 |                 s = PyBytes_AS_STRING(str); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3583 |                 if (!s) | 
 | 3584 |                     return NULL; | 
 | 3585 |                 for (i = 0; i < NCH(n); i += 2) { | 
 | 3586 |                     char *sch = STR(CHILD(n, i)); | 
 | 3587 |                     strcpy(s, STR(CHILD(n, i))); | 
 | 3588 |                     s += strlen(sch); | 
 | 3589 |                     *s++ = '.'; | 
 | 3590 |                 } | 
 | 3591 |                 --s; | 
 | 3592 |                 *s = '\0'; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3593 |                 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), | 
 | 3594 |                                            PyBytes_GET_SIZE(str), | 
 | 3595 |                                            NULL); | 
 | 3596 |                 Py_DECREF(str); | 
 | 3597 |                 if (!uni) | 
 | 3598 |                     return NULL; | 
 | 3599 |                 str = uni; | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3600 |                 PyUnicode_InternInPlace(&str); | 
| Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3601 |                 if (PyArena_AddPyObject(c->c_arena, str) < 0) { | 
 | 3602 |                     Py_DECREF(str); | 
 | 3603 |                     return NULL; | 
 | 3604 |                 } | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3605 |                 return alias(str, NULL, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 |             } | 
 | 3607 |             break; | 
 | 3608 |         case STAR: | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3609 |             str = PyUnicode_InternFromString("*"); | 
| Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3610 |             if (!str) | 
 | 3611 |                 return NULL; | 
| Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3612 |             if (PyArena_AddPyObject(c->c_arena, str) < 0) { | 
 | 3613 |                 Py_DECREF(str); | 
 | 3614 |                 return NULL; | 
 | 3615 |             } | 
| Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3616 |             return alias(str, NULL, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 |         default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3618 |             PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3619 |                          "unexpected import name: %d", TYPE(n)); | 
 | 3620 |             return NULL; | 
 | 3621 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3622 |  | 
 | 3623 |     PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3624 |     return NULL; | 
 | 3625 | } | 
 | 3626 |  | 
 | 3627 | static stmt_ty | 
 | 3628 | ast_for_import_stmt(struct compiling *c, const node *n) | 
 | 3629 | { | 
 | 3630 |     /* | 
 | 3631 |       import_stmt: import_name | import_from | 
 | 3632 |       import_name: 'import' dotted_as_names | 
| Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3633 |       import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) | 
 | 3634 |                    'import' ('*' | '(' import_as_names ')' | import_as_names) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3635 |     */ | 
| Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3636 |     int lineno; | 
 | 3637 |     int col_offset; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3638 |     int i; | 
 | 3639 |     asdl_seq *aliases; | 
 | 3640 |  | 
 | 3641 |     REQ(n, import_stmt); | 
| Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3642 |     lineno = LINENO(n); | 
 | 3643 |     col_offset = n->n_col_offset; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 |     n = CHILD(n, 0); | 
| Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3645 |     if (TYPE(n) == import_name) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3646 |         n = CHILD(n, 1); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3647 |         REQ(n, dotted_as_names); | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3648 |         aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3649 |         if (!aliases) | 
 | 3650 |                 return NULL; | 
 | 3651 |         for (i = 0; i < NCH(n); i += 2) { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3652 |             alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3653 |             if (!import_alias) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3654 |                 return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3655 |             asdl_seq_SET(aliases, i / 2, import_alias); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3656 |         } | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3657 |         // Even though n is modified above, the end position is not changed | 
 | 3658 |         return Import(aliases, lineno, col_offset, | 
 | 3659 |                       n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3660 |     } | 
| Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3661 |     else if (TYPE(n) == import_from) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3662 |         int n_children; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3663 |         int idx, ndots = 0; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3664 |         const node *n_copy = n; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3665 |         alias_ty mod = NULL; | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3666 |         identifier modname = NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3667 |  | 
| Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3668 |        /* Count the number of dots (for relative imports) and check for the | 
 | 3669 |           optional module name */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3670 |         for (idx = 1; idx < NCH(n); idx++) { | 
 | 3671 |             if (TYPE(CHILD(n, idx)) == dotted_name) { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3672 |                 mod = alias_for_import_name(c, CHILD(n, idx), 0); | 
 | 3673 |                 if (!mod) | 
 | 3674 |                     return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3675 |                 idx++; | 
 | 3676 |                 break; | 
| Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3677 |             } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 |                 /* three consecutive dots are tokenized as one ELLIPSIS */ | 
| Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3679 |                 ndots += 3; | 
 | 3680 |                 continue; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3681 |             } else if (TYPE(CHILD(n, idx)) != DOT) { | 
 | 3682 |                 break; | 
 | 3683 |             } | 
 | 3684 |             ndots++; | 
 | 3685 |         } | 
 | 3686 |         idx++; /* skip over the 'import' keyword */ | 
| Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3687 |         switch (TYPE(CHILD(n, idx))) { | 
| Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3688 |         case STAR: | 
 | 3689 |             /* from ... import * */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3690 |             n = CHILD(n, idx); | 
 | 3691 |             n_children = 1; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3692 |             break; | 
 | 3693 |         case LPAR: | 
 | 3694 |             /* from ... import (x, y, z) */ | 
 | 3695 |             n = CHILD(n, idx + 1); | 
 | 3696 |             n_children = NCH(n); | 
 | 3697 |             break; | 
 | 3698 |         case import_as_names: | 
 | 3699 |             /* from ... import x, y, z */ | 
 | 3700 |             n = CHILD(n, idx); | 
 | 3701 |             n_children = NCH(n); | 
| Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3702 |             if (n_children % 2 == 0) { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3703 |                 ast_error(c, n, | 
 | 3704 |                           "trailing comma not allowed without" | 
 | 3705 |                           " surrounding parentheses"); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3706 |                 return NULL; | 
 | 3707 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3708 |             break; | 
 | 3709 |         default: | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3710 |             ast_error(c, n, "Unexpected node-type in from-import"); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3711 |             return NULL; | 
 | 3712 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3713 |  | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3714 |         aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3715 |         if (!aliases) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3717 |  | 
 | 3718 |         /* handle "from ... import *" special b/c there's no children */ | 
| Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3719 |         if (TYPE(n) == STAR) { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3720 |             alias_ty import_alias = alias_for_import_name(c, n, 1); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3721 |             if (!import_alias) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3722 |                 return NULL; | 
| Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3723 |             asdl_seq_SET(aliases, 0, import_alias); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3724 |         } | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3725 |         else { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3726 |             for (i = 0; i < NCH(n); i += 2) { | 
| Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3727 |                 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3728 |                 if (!import_alias) | 
 | 3729 |                     return NULL; | 
| Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3730 |                 asdl_seq_SET(aliases, i / 2, import_alias); | 
| Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3731 |             } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3732 |         } | 
| Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3733 |         if (mod != NULL) | 
 | 3734 |             modname = mod->name; | 
| Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3735 |         return ImportFrom(modname, aliases, ndots, lineno, col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3736 |                           n_copy->n_end_lineno, n_copy->n_end_col_offset, | 
| Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3737 |                           c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3738 |     } | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3739 |     PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3740 |                  "unknown import statement: starts with command '%s'", | 
 | 3741 |                  STR(CHILD(n, 0))); | 
 | 3742 |     return NULL; | 
 | 3743 | } | 
 | 3744 |  | 
 | 3745 | static stmt_ty | 
 | 3746 | ast_for_global_stmt(struct compiling *c, const node *n) | 
 | 3747 | { | 
 | 3748 |     /* global_stmt: 'global' NAME (',' NAME)* */ | 
 | 3749 |     identifier name; | 
 | 3750 |     asdl_seq *s; | 
 | 3751 |     int i; | 
 | 3752 |  | 
 | 3753 |     REQ(n, global_stmt); | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3754 |     s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3755 |     if (!s) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3756 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3757 |     for (i = 1; i < NCH(n); i += 2) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3758 |         name = NEW_IDENTIFIER(CHILD(n, i)); | 
 | 3759 |         if (!name) | 
 | 3760 |             return NULL; | 
 | 3761 |         asdl_seq_SET(s, i / 2, name); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3762 |     } | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3763 |     return Global(s, LINENO(n), n->n_col_offset, | 
 | 3764 |                   n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3765 | } | 
 | 3766 |  | 
 | 3767 | static stmt_ty | 
| Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3768 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) | 
 | 3769 | { | 
 | 3770 |     /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ | 
 | 3771 |     identifier name; | 
 | 3772 |     asdl_seq *s; | 
 | 3773 |     int i; | 
 | 3774 |  | 
 | 3775 |     REQ(n, nonlocal_stmt); | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3776 |     s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); | 
| Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3777 |     if (!s) | 
 | 3778 |         return NULL; | 
 | 3779 |     for (i = 1; i < NCH(n); i += 2) { | 
 | 3780 |         name = NEW_IDENTIFIER(CHILD(n, i)); | 
 | 3781 |         if (!name) | 
 | 3782 |             return NULL; | 
 | 3783 |         asdl_seq_SET(s, i / 2, name); | 
 | 3784 |     } | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3785 |     return Nonlocal(s, LINENO(n), n->n_col_offset, | 
 | 3786 |                     n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3787 | } | 
 | 3788 |  | 
 | 3789 | static stmt_ty | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3790 | ast_for_assert_stmt(struct compiling *c, const node *n) | 
 | 3791 | { | 
 | 3792 |     /* assert_stmt: 'assert' test [',' test] */ | 
 | 3793 |     REQ(n, assert_stmt); | 
 | 3794 |     if (NCH(n) == 2) { | 
 | 3795 |         expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 3796 |         if (!expression) | 
 | 3797 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3798 |         return Assert(expression, NULL, LINENO(n), n->n_col_offset, | 
 | 3799 |                       n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3800 |     } | 
 | 3801 |     else if (NCH(n) == 4) { | 
 | 3802 |         expr_ty expr1, expr2; | 
 | 3803 |  | 
 | 3804 |         expr1 = ast_for_expr(c, CHILD(n, 1)); | 
 | 3805 |         if (!expr1) | 
 | 3806 |             return NULL; | 
 | 3807 |         expr2 = ast_for_expr(c, CHILD(n, 3)); | 
 | 3808 |         if (!expr2) | 
 | 3809 |             return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3811 |         return Assert(expr1, expr2, LINENO(n), n->n_col_offset, | 
 | 3812 |                       n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3813 |     } | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3814 |     PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3815 |                  "improper number of parts to 'assert' statement: %d", | 
 | 3816 |                  NCH(n)); | 
 | 3817 |     return NULL; | 
 | 3818 | } | 
 | 3819 |  | 
 | 3820 | static asdl_seq * | 
 | 3821 | ast_for_suite(struct compiling *c, const node *n) | 
 | 3822 | { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3823 |     /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3824 |     asdl_seq *seq; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3825 |     stmt_ty s; | 
 | 3826 |     int i, total, num, end, pos = 0; | 
 | 3827 |     node *ch; | 
 | 3828 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3829 |     if (TYPE(n) != func_body_suite) { | 
 | 3830 |         REQ(n, suite); | 
 | 3831 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3832 |  | 
 | 3833 |     total = num_stmts(n); | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3834 |     seq = _Py_asdl_seq_new(total, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3835 |     if (!seq) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3836 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3837 |     if (TYPE(CHILD(n, 0)) == simple_stmt) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3838 |         n = CHILD(n, 0); | 
 | 3839 |         /* simple_stmt always ends with a NEWLINE, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3840 |            and may have a trailing SEMI | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3841 |         */ | 
 | 3842 |         end = NCH(n) - 1; | 
 | 3843 |         if (TYPE(CHILD(n, end - 1)) == SEMI) | 
 | 3844 |             end--; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3845 |         /* loop by 2 to skip semi-colons */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3846 |         for (i = 0; i < end; i += 2) { | 
 | 3847 |             ch = CHILD(n, i); | 
 | 3848 |             s = ast_for_stmt(c, ch); | 
 | 3849 |             if (!s) | 
 | 3850 |                 return NULL; | 
 | 3851 |             asdl_seq_SET(seq, pos++, s); | 
 | 3852 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3853 |     } | 
 | 3854 |     else { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3855 |         i = 2; | 
 | 3856 |         if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { | 
 | 3857 |             i += 2; | 
 | 3858 |             REQ(CHILD(n, 2), NEWLINE); | 
 | 3859 |         } | 
 | 3860 |  | 
 | 3861 |         for (; i < (NCH(n) - 1); i++) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3862 |             ch = CHILD(n, i); | 
 | 3863 |             REQ(ch, stmt); | 
 | 3864 |             num = num_stmts(ch); | 
 | 3865 |             if (num == 1) { | 
 | 3866 |                 /* small_stmt or compound_stmt with only one child */ | 
 | 3867 |                 s = ast_for_stmt(c, ch); | 
 | 3868 |                 if (!s) | 
 | 3869 |                     return NULL; | 
 | 3870 |                 asdl_seq_SET(seq, pos++, s); | 
 | 3871 |             } | 
 | 3872 |             else { | 
 | 3873 |                 int j; | 
 | 3874 |                 ch = CHILD(ch, 0); | 
 | 3875 |                 REQ(ch, simple_stmt); | 
 | 3876 |                 for (j = 0; j < NCH(ch); j += 2) { | 
 | 3877 |                     /* statement terminates with a semi-colon ';' */ | 
 | 3878 |                     if (NCH(CHILD(ch, j)) == 0) { | 
 | 3879 |                         assert((j + 1) == NCH(ch)); | 
 | 3880 |                         break; | 
 | 3881 |                     } | 
 | 3882 |                     s = ast_for_stmt(c, CHILD(ch, j)); | 
 | 3883 |                     if (!s) | 
 | 3884 |                         return NULL; | 
 | 3885 |                     asdl_seq_SET(seq, pos++, s); | 
 | 3886 |                 } | 
 | 3887 |             } | 
 | 3888 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3889 |     } | 
 | 3890 |     assert(pos == seq->size); | 
 | 3891 |     return seq; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3892 | } | 
 | 3893 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3894 | static void | 
 | 3895 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) | 
 | 3896 | { | 
| Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame] | 3897 |     Py_ssize_t tot = asdl_seq_LEN(s); | 
| Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3898 |     // There must be no empty suites. | 
 | 3899 |     assert(tot > 0); | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3900 |     stmt_ty last = asdl_seq_GET(s, tot - 1); | 
 | 3901 |     *end_lineno = last->end_lineno; | 
 | 3902 |     *end_col_offset = last->end_col_offset; | 
 | 3903 | } | 
 | 3904 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3905 | static stmt_ty | 
 | 3906 | ast_for_if_stmt(struct compiling *c, const node *n) | 
 | 3907 | { | 
 | 3908 |     /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* | 
 | 3909 |        ['else' ':' suite] | 
 | 3910 |     */ | 
 | 3911 |     char *s; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3912 |     int end_lineno, end_col_offset; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3913 |  | 
 | 3914 |     REQ(n, if_stmt); | 
 | 3915 |  | 
 | 3916 |     if (NCH(n) == 4) { | 
 | 3917 |         expr_ty expression; | 
 | 3918 |         asdl_seq *suite_seq; | 
 | 3919 |  | 
 | 3920 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 3921 |         if (!expression) | 
 | 3922 |             return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3923 |         suite_seq = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3924 |         if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3925 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3926 |         get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3927 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3928 |         return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3929 |                   end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3930 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3931 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3932 |     s = STR(CHILD(n, 4)); | 
 | 3933 |     /* s[2], the third character in the string, will be | 
 | 3934 |        's' for el_s_e, or | 
 | 3935 |        'i' for el_i_f | 
 | 3936 |     */ | 
 | 3937 |     if (s[2] == 's') { | 
 | 3938 |         expr_ty expression; | 
 | 3939 |         asdl_seq *seq1, *seq2; | 
 | 3940 |  | 
 | 3941 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 3942 |         if (!expression) | 
 | 3943 |             return NULL; | 
 | 3944 |         seq1 = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3945 |         if (!seq1) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3946 |             return NULL; | 
 | 3947 |         seq2 = ast_for_suite(c, CHILD(n, 6)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3948 |         if (!seq2) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3949 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3950 |         get_last_end_pos(seq2, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3951 |  | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3952 |         return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3953 |                   end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3954 |     } | 
 | 3955 |     else if (s[2] == 'i') { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3956 |         int i, n_elif, has_else = 0; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3957 |         expr_ty expression; | 
 | 3958 |         asdl_seq *suite_seq; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3959 |         asdl_seq *orelse = NULL; | 
 | 3960 |         n_elif = NCH(n) - 4; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 |         /* must reference the child n_elif+1 since 'else' token is third, | 
 | 3962 |            not fourth, child from the end. */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3963 |         if (TYPE(CHILD(n, (n_elif + 1))) == NAME | 
 | 3964 |             && STR(CHILD(n, (n_elif + 1)))[2] == 's') { | 
 | 3965 |             has_else = 1; | 
 | 3966 |             n_elif -= 3; | 
 | 3967 |         } | 
 | 3968 |         n_elif /= 4; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3969 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3970 |         if (has_else) { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3971 |             asdl_seq *suite_seq2; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3972 |  | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3973 |             orelse = _Py_asdl_seq_new(1, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3974 |             if (!orelse) | 
 | 3975 |                 return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3976 |             expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3977 |             if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3978 |                 return NULL; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3979 |             suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); | 
 | 3980 |             if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3981 |                 return NULL; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3982 |             suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | 
 | 3983 |             if (!suite_seq2) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3984 |                 return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3985 |             get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3986 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3987 |             asdl_seq_SET(orelse, 0, | 
 | 3988 |                          If(expression, suite_seq, suite_seq2, | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3989 |                             LINENO(CHILD(n, NCH(n) - 6)), | 
 | 3990 |                             CHILD(n, NCH(n) - 6)->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3991 |                             end_lineno, end_col_offset, c->c_arena)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3992 |             /* the just-created orelse handled the last elif */ | 
 | 3993 |             n_elif--; | 
 | 3994 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3995 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3996 |         for (i = 0; i < n_elif; i++) { | 
 | 3997 |             int off = 5 + (n_elif - i - 1) * 4; | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3998 |             asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3999 |             if (!newobj) | 
 | 4000 |                 return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4001 |             expression = ast_for_expr(c, CHILD(n, off)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4002 |             if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4003 |                 return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4004 |             suite_seq = ast_for_suite(c, CHILD(n, off + 2)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4005 |             if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4006 |                 return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4007 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4008 |             if (orelse != NULL) { | 
 | 4009 |                 get_last_end_pos(orelse, &end_lineno, &end_col_offset); | 
 | 4010 |             } else { | 
 | 4011 |                 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
 | 4012 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4013 |             asdl_seq_SET(newobj, 0, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4014 |                          If(expression, suite_seq, orelse, | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4015 |                             LINENO(CHILD(n, off)), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4016 |                             CHILD(n, off)->n_col_offset, | 
 | 4017 |                             end_lineno, end_col_offset, c->c_arena)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4018 |             orelse = newobj; | 
 | 4019 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4020 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 4021 |         if (!expression) | 
 | 4022 |             return NULL; | 
 | 4023 |         suite_seq = ast_for_suite(c, CHILD(n, 3)); | 
 | 4024 |         if (!suite_seq) | 
 | 4025 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4026 |         get_last_end_pos(orelse, &end_lineno, &end_col_offset); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4027 |         return If(expression, suite_seq, orelse, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4028 |                   LINENO(n), n->n_col_offset, | 
 | 4029 |                   end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4030 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4031 |  | 
 | 4032 |     PyErr_Format(PyExc_SystemError, | 
 | 4033 |                  "unexpected token in 'if' statement: %s", s); | 
 | 4034 |     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4035 | } | 
 | 4036 |  | 
 | 4037 | static stmt_ty | 
 | 4038 | ast_for_while_stmt(struct compiling *c, const node *n) | 
 | 4039 | { | 
 | 4040 |     /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ | 
 | 4041 |     REQ(n, while_stmt); | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4042 |     int end_lineno, end_col_offset; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4043 |  | 
 | 4044 |     if (NCH(n) == 4) { | 
 | 4045 |         expr_ty expression; | 
 | 4046 |         asdl_seq *suite_seq; | 
 | 4047 |  | 
 | 4048 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 4049 |         if (!expression) | 
 | 4050 |             return NULL; | 
 | 4051 |         suite_seq = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4052 |         if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4053 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4054 |         get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
 | 4055 |         return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, | 
 | 4056 |                      end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4057 |     } | 
 | 4058 |     else if (NCH(n) == 7) { | 
 | 4059 |         expr_ty expression; | 
 | 4060 |         asdl_seq *seq1, *seq2; | 
 | 4061 |  | 
 | 4062 |         expression = ast_for_expr(c, CHILD(n, 1)); | 
 | 4063 |         if (!expression) | 
 | 4064 |             return NULL; | 
 | 4065 |         seq1 = ast_for_suite(c, CHILD(n, 3)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4066 |         if (!seq1) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4067 |             return NULL; | 
 | 4068 |         seq2 = ast_for_suite(c, CHILD(n, 6)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4069 |         if (!seq2) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4070 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4071 |         get_last_end_pos(seq2, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4072 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4073 |         return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, | 
 | 4074 |                      end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4075 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4076 |  | 
 | 4077 |     PyErr_Format(PyExc_SystemError, | 
 | 4078 |                  "wrong number of tokens for 'while' statement: %d", | 
 | 4079 |                  NCH(n)); | 
 | 4080 |     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4081 | } | 
 | 4082 |  | 
 | 4083 | static stmt_ty | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4084 | ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4085 | { | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4086 |     const node * const n = is_async ? CHILD(n0, 1) : n0; | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4087 |     asdl_seq *_target, *seq = NULL, *suite_seq; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4088 |     expr_ty expression; | 
| Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4089 |     expr_ty target, first; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4090 |     const node *node_target; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4091 |     int end_lineno, end_col_offset; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4092 |     int has_type_comment; | 
 | 4093 |     string type_comment; | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4094 |  | 
 | 4095 |     if (is_async && c->c_feature_version < 5) { | 
 | 4096 |         ast_error(c, n, | 
 | 4097 |                   "Async for loops are only supported in Python 3.5 and greater"); | 
 | 4098 |         return NULL; | 
 | 4099 |     } | 
 | 4100 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4101 |     /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4102 |     REQ(n, for_stmt); | 
 | 4103 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4104 |     has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; | 
 | 4105 |  | 
 | 4106 |     if (NCH(n) == 9 + has_type_comment) { | 
 | 4107 |         seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4108 |         if (!seq) | 
 | 4109 |             return NULL; | 
 | 4110 |     } | 
 | 4111 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4112 |     node_target = CHILD(n, 1); | 
 | 4113 |     _target = ast_for_exprlist(c, node_target, Store); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4114 |     if (!_target) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4115 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4116 |     /* Check the # of children rather than the length of _target, since | 
 | 4117 |        for x, in ... has 1 element in _target, but still requires a Tuple. */ | 
| Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4118 |     first = (expr_ty)asdl_seq_GET(_target, 0); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4119 |     if (NCH(node_target) == 1) | 
| Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4120 |         target = first; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4121 |     else | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4122 |         target = Tuple(_target, Store, first->lineno, first->col_offset, | 
 | 4123 |                        node_target->n_end_lineno, node_target->n_end_col_offset, | 
 | 4124 |                        c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4125 |  | 
| Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4126 |     expression = ast_for_testlist(c, CHILD(n, 3)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4127 |     if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4128 |         return NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4129 |     suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4130 |     if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4131 |         return NULL; | 
 | 4132 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4133 |     if (seq != NULL) { | 
 | 4134 |         get_last_end_pos(seq, &end_lineno, &end_col_offset); | 
 | 4135 |     } else { | 
 | 4136 |         get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
 | 4137 |     } | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4138 |  | 
 | 4139 |     if (has_type_comment) { | 
 | 4140 |         type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); | 
 | 4141 |         if (!type_comment) | 
 | 4142 |             return NULL; | 
 | 4143 |     } | 
 | 4144 |     else | 
 | 4145 |         type_comment = NULL; | 
 | 4146 |  | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4147 |     if (is_async) | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4148 |         return AsyncFor(target, expression, suite_seq, seq, type_comment, | 
| Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4149 |                         LINENO(n0), n0->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4150 |                         end_lineno, end_col_offset, c->c_arena); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4151 |     else | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4152 |         return For(target, expression, suite_seq, seq, type_comment, | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4153 |                    LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4154 |                    end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4155 | } | 
 | 4156 |  | 
 | 4157 | static excepthandler_ty | 
 | 4158 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) | 
 | 4159 | { | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4160 |     /* except_clause: 'except' [test ['as' test]] */ | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4161 |     int end_lineno, end_col_offset; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4162 |     REQ(exc, except_clause); | 
 | 4163 |     REQ(body, suite); | 
 | 4164 |  | 
 | 4165 |     if (NCH(exc) == 1) { | 
 | 4166 |         asdl_seq *suite_seq = ast_for_suite(c, body); | 
 | 4167 |         if (!suite_seq) | 
 | 4168 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4169 |         get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4170 |  | 
| Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4171 |         return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4172 |                              exc->n_col_offset, | 
 | 4173 |                              end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4174 |     } | 
 | 4175 |     else if (NCH(exc) == 2) { | 
 | 4176 |         expr_ty expression; | 
 | 4177 |         asdl_seq *suite_seq; | 
 | 4178 |  | 
 | 4179 |         expression = ast_for_expr(c, CHILD(exc, 1)); | 
 | 4180 |         if (!expression) | 
 | 4181 |             return NULL; | 
 | 4182 |         suite_seq = ast_for_suite(c, body); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4183 |         if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4184 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4185 |         get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4186 |  | 
| Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4187 |         return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4188 |                              exc->n_col_offset, | 
 | 4189 |                              end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4190 |     } | 
 | 4191 |     else if (NCH(exc) == 4) { | 
 | 4192 |         asdl_seq *suite_seq; | 
 | 4193 |         expr_ty expression; | 
| Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4194 |         identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4195 |         if (!e) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4196 |             return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4197 |         if (forbidden_name(c, e, CHILD(exc, 3), 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4198 |             return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4199 |         expression = ast_for_expr(c, CHILD(exc, 1)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4200 |         if (!expression) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4201 |             return NULL; | 
 | 4202 |         suite_seq = ast_for_suite(c, body); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4203 |         if (!suite_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4204 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4205 |         get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4206 |  | 
| Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4207 |         return ExceptHandler(expression, e, suite_seq, LINENO(exc), | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4208 |                              exc->n_col_offset, | 
 | 4209 |                              end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4210 |     } | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4211 |  | 
 | 4212 |     PyErr_Format(PyExc_SystemError, | 
 | 4213 |                  "wrong number of children for 'except' clause: %d", | 
 | 4214 |                  NCH(exc)); | 
 | 4215 |     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4216 | } | 
 | 4217 |  | 
 | 4218 | static stmt_ty | 
 | 4219 | ast_for_try_stmt(struct compiling *c, const node *n) | 
 | 4220 | { | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4221 |     const int nch = NCH(n); | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4222 |     int end_lineno, end_col_offset, n_except = (nch - 3)/3; | 
| Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4223 |     asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4224 |     excepthandler_ty last_handler; | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4225 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4226 |     REQ(n, try_stmt); | 
 | 4227 |  | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4228 |     body = ast_for_suite(c, CHILD(n, 2)); | 
 | 4229 |     if (body == NULL) | 
 | 4230 |         return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4231 |  | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4232 |     if (TYPE(CHILD(n, nch - 3)) == NAME) { | 
 | 4233 |         if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { | 
 | 4234 |             if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { | 
 | 4235 |                 /* we can assume it's an "else", | 
 | 4236 |                    because nch >= 9 for try-else-finally and | 
 | 4237 |                    it would otherwise have a type of except_clause */ | 
 | 4238 |                 orelse = ast_for_suite(c, CHILD(n, nch - 4)); | 
 | 4239 |                 if (orelse == NULL) | 
 | 4240 |                     return NULL; | 
 | 4241 |                 n_except--; | 
 | 4242 |             } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4243 |  | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4244 |             finally = ast_for_suite(c, CHILD(n, nch - 1)); | 
 | 4245 |             if (finally == NULL) | 
 | 4246 |                 return NULL; | 
 | 4247 |             n_except--; | 
 | 4248 |         } | 
 | 4249 |         else { | 
 | 4250 |             /* we can assume it's an "else", | 
 | 4251 |                otherwise it would have a type of except_clause */ | 
 | 4252 |             orelse = ast_for_suite(c, CHILD(n, nch - 1)); | 
 | 4253 |             if (orelse == NULL) | 
 | 4254 |                 return NULL; | 
 | 4255 |             n_except--; | 
 | 4256 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4257 |     } | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4258 |     else if (TYPE(CHILD(n, nch - 3)) != except_clause) { | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4259 |         ast_error(c, n, "malformed 'try' statement"); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4260 |         return NULL; | 
 | 4261 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4262 |  | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4263 |     if (n_except > 0) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4264 |         int i; | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4265 |         /* process except statements to create a try ... except */ | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4266 |         handlers = _Py_asdl_seq_new(n_except, c->c_arena); | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4267 |         if (handlers == NULL) | 
 | 4268 |             return NULL; | 
 | 4269 |  | 
 | 4270 |         for (i = 0; i < n_except; i++) { | 
 | 4271 |             excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), | 
 | 4272 |                                                        CHILD(n, 5 + i * 3)); | 
 | 4273 |             if (!e) | 
 | 4274 |                 return NULL; | 
 | 4275 |             asdl_seq_SET(handlers, i, e); | 
 | 4276 |         } | 
| Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4277 |     } | 
 | 4278 |  | 
| Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4279 |     assert(finally != NULL || asdl_seq_LEN(handlers)); | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4280 |         if (finally != NULL) { | 
 | 4281 |         // finally is always last | 
 | 4282 |         get_last_end_pos(finally, &end_lineno, &end_col_offset); | 
 | 4283 |     } else if (orelse != NULL) { | 
 | 4284 |         // otherwise else is last | 
 | 4285 |         get_last_end_pos(orelse, &end_lineno, &end_col_offset); | 
 | 4286 |     } else { | 
 | 4287 |         // inline the get_last_end_pos logic due to layout mismatch | 
 | 4288 |         last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); | 
 | 4289 |         end_lineno = last_handler->end_lineno; | 
 | 4290 |         end_col_offset = last_handler->end_col_offset; | 
 | 4291 |     } | 
 | 4292 |     return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, | 
 | 4293 |                end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4294 | } | 
 | 4295 |  | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4296 | /* with_item: test ['as' expr] */ | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4297 | static withitem_ty | 
 | 4298 | ast_for_with_item(struct compiling *c, const node *n) | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4299 | { | 
 | 4300 |     expr_ty context_expr, optional_vars = NULL; | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4301 |  | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4302 |     REQ(n, with_item); | 
 | 4303 |     context_expr = ast_for_expr(c, CHILD(n, 0)); | 
| Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4304 |     if (!context_expr) | 
 | 4305 |         return NULL; | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4306 |     if (NCH(n) == 3) { | 
 | 4307 |         optional_vars = ast_for_expr(c, CHILD(n, 2)); | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4308 |  | 
 | 4309 |         if (!optional_vars) { | 
 | 4310 |             return NULL; | 
 | 4311 |         } | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4312 |         if (!set_context(c, optional_vars, Store, n)) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4313 |             return NULL; | 
 | 4314 |         } | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4315 |     } | 
 | 4316 |  | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4317 |     return withitem(context_expr, optional_vars, c->c_arena); | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4318 | } | 
 | 4319 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4320 | /* with_stmt: 'with' with_item (',' with_item)*  ':' [TYPE_COMMENT] suite */ | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4321 | static stmt_ty | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4322 | ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async) | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4323 | { | 
| guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4324 |     const node * const n = is_async ? CHILD(n0, 1) : n0; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4325 |     int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset; | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4326 |     asdl_seq *items, *body; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4327 |     string type_comment; | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4328 |  | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4329 |     if (is_async && c->c_feature_version < 5) { | 
 | 4330 |         ast_error(c, n, | 
 | 4331 |                   "Async with statements are only supported in Python 3.5 and greater"); | 
 | 4332 |         return NULL; | 
 | 4333 |     } | 
 | 4334 |  | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4335 |     REQ(n, with_stmt); | 
 | 4336 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4337 |     has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; | 
 | 4338 |     nch_minus_type = NCH(n) - has_type_comment; | 
 | 4339 |  | 
 | 4340 |     n_items = (nch_minus_type - 2) / 2; | 
| Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4341 |     items = _Py_asdl_seq_new(n_items, c->c_arena); | 
| Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4342 |     if (!items) | 
 | 4343 |         return NULL; | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4344 |     for (i = 1; i < nch_minus_type - 2; i += 2) { | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4345 |         withitem_ty item = ast_for_with_item(c, CHILD(n, i)); | 
 | 4346 |         if (!item) | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4347 |             return NULL; | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4348 |         asdl_seq_SET(items, (i - 1) / 2, item); | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4349 |     } | 
 | 4350 |  | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4351 |     body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | 
 | 4352 |     if (!body) | 
 | 4353 |         return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4354 |     get_last_end_pos(body, &end_lineno, &end_col_offset); | 
| Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4355 |  | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4356 |     if (has_type_comment) { | 
 | 4357 |         type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); | 
 | 4358 |         if (!type_comment) | 
 | 4359 |             return NULL; | 
 | 4360 |     } | 
 | 4361 |     else | 
 | 4362 |         type_comment = NULL; | 
 | 4363 |  | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4364 |     if (is_async) | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4365 |         return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4366 |                          end_lineno, end_col_offset, c->c_arena); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4367 |     else | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4368 |         return With(items, body, type_comment, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4369 |                     end_lineno, end_col_offset, c->c_arena); | 
| Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4370 | } | 
 | 4371 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4372 | static stmt_ty | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4373 | ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4374 | { | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4375 |     /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4376 |     PyObject *classname; | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4377 |     asdl_seq *s; | 
| Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4378 |     expr_ty call; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4379 |     int end_lineno, end_col_offset; | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4380 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4381 |     REQ(n, classdef); | 
 | 4382 |  | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4383 |     if (NCH(n) == 4) { /* class NAME ':' suite */ | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4384 |         s = ast_for_suite(c, CHILD(n, 3)); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4385 |         if (!s) | 
 | 4386 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4387 |         get_last_end_pos(s, &end_lineno, &end_col_offset); | 
 | 4388 |  | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4389 |         classname = NEW_IDENTIFIER(CHILD(n, 1)); | 
 | 4390 |         if (!classname) | 
 | 4391 |             return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4392 |         if (forbidden_name(c, classname, CHILD(n, 3), 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4393 |             return NULL; | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4394 |         return ClassDef(classname, NULL, NULL, s, decorator_seq, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4395 |                         LINENO(n), n->n_col_offset, | 
 | 4396 |                         end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4397 |     } | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4398 |  | 
 | 4399 |     if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4400 |         s = ast_for_suite(c, CHILD(n, 5)); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4401 |         if (!s) | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4402 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4403 |         get_last_end_pos(s, &end_lineno, &end_col_offset); | 
 | 4404 |  | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4405 |         classname = NEW_IDENTIFIER(CHILD(n, 1)); | 
 | 4406 |         if (!classname) | 
 | 4407 |             return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4408 |         if (forbidden_name(c, classname, CHILD(n, 3), 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4409 |             return NULL; | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4410 |         return ClassDef(classname, NULL, NULL, s, decorator_seq, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4411 |                         LINENO(n), n->n_col_offset, | 
 | 4412 |                         end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4413 |     } | 
 | 4414 |  | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4415 |     /* class NAME '(' arglist ')' ':' suite */ | 
 | 4416 |     /* build up a fake Call node so we can extract its pieces */ | 
| Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4417 |     { | 
 | 4418 |         PyObject *dummy_name; | 
 | 4419 |         expr_ty dummy; | 
 | 4420 |         dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); | 
 | 4421 |         if (!dummy_name) | 
 | 4422 |             return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4423 |         dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, | 
 | 4424 |                      CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, | 
 | 4425 |                      c->c_arena); | 
 | 4426 |         call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4)); | 
| Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4427 |         if (!call) | 
 | 4428 |             return NULL; | 
 | 4429 |     } | 
| Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4430 |     s = ast_for_suite(c, CHILD(n, 6)); | 
| Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4431 |     if (!s) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4432 |         return NULL; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4433 |     get_last_end_pos(s, &end_lineno, &end_col_offset); | 
 | 4434 |  | 
| Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4435 |     classname = NEW_IDENTIFIER(CHILD(n, 1)); | 
 | 4436 |     if (!classname) | 
 | 4437 |         return NULL; | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4438 |     if (forbidden_name(c, classname, CHILD(n, 1), 0)) | 
| Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4439 |         return NULL; | 
| Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4440 |  | 
| Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4441 |     return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4442 |                     decorator_seq, LINENO(n), n->n_col_offset, | 
 | 4443 |                     end_lineno, end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4444 | } | 
 | 4445 |  | 
 | 4446 | static stmt_ty | 
 | 4447 | ast_for_stmt(struct compiling *c, const node *n) | 
 | 4448 | { | 
 | 4449 |     if (TYPE(n) == stmt) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4450 |         assert(NCH(n) == 1); | 
 | 4451 |         n = CHILD(n, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4452 |     } | 
 | 4453 |     if (TYPE(n) == simple_stmt) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4454 |         assert(num_stmts(n) == 1); | 
 | 4455 |         n = CHILD(n, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4456 |     } | 
 | 4457 |     if (TYPE(n) == small_stmt) { | 
| Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4458 |         n = CHILD(n, 0); | 
| Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4459 |         /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt | 
 | 4460 |                   | import_stmt | global_stmt | nonlocal_stmt | assert_stmt | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4461 |         */ | 
 | 4462 |         switch (TYPE(n)) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4463 |             case expr_stmt: | 
 | 4464 |                 return ast_for_expr_stmt(c, n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4465 |             case del_stmt: | 
 | 4466 |                 return ast_for_del_stmt(c, n); | 
 | 4467 |             case pass_stmt: | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4468 |                 return Pass(LINENO(n), n->n_col_offset, | 
 | 4469 |                             n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4470 |             case flow_stmt: | 
 | 4471 |                 return ast_for_flow_stmt(c, n); | 
 | 4472 |             case import_stmt: | 
 | 4473 |                 return ast_for_import_stmt(c, n); | 
 | 4474 |             case global_stmt: | 
 | 4475 |                 return ast_for_global_stmt(c, n); | 
| Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4476 |             case nonlocal_stmt: | 
 | 4477 |                 return ast_for_nonlocal_stmt(c, n); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4478 |             case assert_stmt: | 
 | 4479 |                 return ast_for_assert_stmt(c, n); | 
 | 4480 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4481 |                 PyErr_Format(PyExc_SystemError, | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4482 |                              "unhandled small_stmt: TYPE=%d NCH=%d\n", | 
 | 4483 |                              TYPE(n), NCH(n)); | 
 | 4484 |                 return NULL; | 
 | 4485 |         } | 
 | 4486 |     } | 
 | 4487 |     else { | 
 | 4488 |         /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4489 |                         | funcdef | classdef | decorated | async_stmt | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4490 |         */ | 
 | 4491 |         node *ch = CHILD(n, 0); | 
 | 4492 |         REQ(n, compound_stmt); | 
 | 4493 |         switch (TYPE(ch)) { | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4494 |             case if_stmt: | 
 | 4495 |                 return ast_for_if_stmt(c, ch); | 
 | 4496 |             case while_stmt: | 
 | 4497 |                 return ast_for_while_stmt(c, ch); | 
 | 4498 |             case for_stmt: | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4499 |                 return ast_for_for_stmt(c, ch, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4500 |             case try_stmt: | 
 | 4501 |                 return ast_for_try_stmt(c, ch); | 
| Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4502 |             case with_stmt: | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4503 |                 return ast_for_with_stmt(c, ch, 0); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4504 |             case funcdef: | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4505 |                 return ast_for_funcdef(c, ch, NULL); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4506 |             case classdef: | 
| Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4507 |                 return ast_for_classdef(c, ch, NULL); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 |             case decorated: | 
 | 4509 |                 return ast_for_decorated(c, ch); | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4510 |             case async_stmt: | 
 | 4511 |                 return ast_for_async_stmt(c, ch); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4512 |             default: | 
| Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4513 |                 PyErr_Format(PyExc_SystemError, | 
| Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4514 |                              "unhandled compound_stmt: TYPE=%d NCH=%d\n", | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4515 |                              TYPE(n), NCH(n)); | 
 | 4516 |                 return NULL; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4517 |         } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4518 |     } | 
 | 4519 | } | 
 | 4520 |  | 
 | 4521 | static PyObject * | 
| Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4522 | parsenumber_raw(struct compiling *c, const char *s) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4523 | { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4524 |     const char *end; | 
 | 4525 |     long x; | 
 | 4526 |     double dx; | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4527 |     Py_complex compl; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4528 |     int imflag; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4529 |  | 
| Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4530 |     assert(s != NULL); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4531 |     errno = 0; | 
 | 4532 |     end = s + strlen(s) - 1; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4533 |     imflag = *end == 'j' || *end == 'J'; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4534 |     if (s[0] == '0') { | 
| Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4535 |         x = (long) PyOS_strtoul(s, (char **)&end, 0); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4536 |         if (x < 0 && errno == 0) { | 
| Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4537 |             return PyLong_FromString(s, (char **)0, 0); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4538 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4539 |     } | 
 | 4540 |     else | 
| Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4541 |         x = PyOS_strtol(s, (char **)&end, 0); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4542 |     if (*end == '\0') { | 
 | 4543 |         if (errno != 0) | 
| Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4544 |             return PyLong_FromString(s, (char **)0, 0); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4545 |         return PyLong_FromLong(x); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4546 |     } | 
 | 4547 |     /* XXX Huge floats may silently fail */ | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4548 |     if (imflag) { | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4549 |         compl.real = 0.; | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4550 |         compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); | 
 | 4551 |         if (compl.imag == -1.0 && PyErr_Occurred()) | 
 | 4552 |             return NULL; | 
 | 4553 |         return PyComplex_FromCComplex(compl); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4554 |     } | 
 | 4555 |     else | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4556 |     { | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4557 |         dx = PyOS_string_to_double(s, NULL, NULL); | 
 | 4558 |         if (dx == -1.0 && PyErr_Occurred()) | 
 | 4559 |             return NULL; | 
 | 4560 |         return PyFloat_FromDouble(dx); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4561 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4562 | } | 
 | 4563 |  | 
 | 4564 | static PyObject * | 
| Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4565 | parsenumber(struct compiling *c, const char *s) | 
 | 4566 | { | 
 | 4567 |     char *dup, *end; | 
 | 4568 |     PyObject *res = NULL; | 
 | 4569 |  | 
 | 4570 |     assert(s != NULL); | 
 | 4571 |  | 
 | 4572 |     if (strchr(s, '_') == NULL) { | 
 | 4573 |         return parsenumber_raw(c, s); | 
 | 4574 |     } | 
 | 4575 |     /* Create a duplicate without underscores. */ | 
 | 4576 |     dup = PyMem_Malloc(strlen(s) + 1); | 
| Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4577 |     if (dup == NULL) { | 
 | 4578 |         return PyErr_NoMemory(); | 
 | 4579 |     } | 
| Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4580 |     end = dup; | 
 | 4581 |     for (; *s; s++) { | 
 | 4582 |         if (*s != '_') { | 
 | 4583 |             *end++ = *s; | 
 | 4584 |         } | 
 | 4585 |     } | 
 | 4586 |     *end = '\0'; | 
 | 4587 |     res = parsenumber_raw(c, dup); | 
 | 4588 |     PyMem_Free(dup); | 
 | 4589 |     return res; | 
 | 4590 | } | 
 | 4591 |  | 
 | 4592 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4593 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4594 | { | 
| Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4595 |     const char *s, *t; | 
 | 4596 |     t = s = *sPtr; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4597 |     /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ | 
 | 4598 |     while (s < end && (*s & 0x80)) s++; | 
 | 4599 |     *sPtr = s; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4600 |     return PyUnicode_DecodeUTF8(t, s - t, NULL); | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4601 | } | 
 | 4602 |  | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4603 | static int | 
 | 4604 | warn_invalid_escape_sequence(struct compiling *c, const node *n, | 
| Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4605 |                              unsigned char first_invalid_escape_char) | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4606 | { | 
 | 4607 |     PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", | 
 | 4608 |                                          first_invalid_escape_char); | 
 | 4609 |     if (msg == NULL) { | 
 | 4610 |         return -1; | 
 | 4611 |     } | 
| Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4612 |     if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4613 |                                    c->c_filename, LINENO(n), | 
| Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4614 |                                    NULL, NULL) < 0) | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4615 |     { | 
| Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4616 |         if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { | 
| Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4617 |             /* Replace the SyntaxWarning exception with a SyntaxError | 
| Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4618 |                to get a more accurate error report */ | 
 | 4619 |             PyErr_Clear(); | 
| Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4620 |             ast_error(c, n, "%U", msg); | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4621 |         } | 
 | 4622 |         Py_DECREF(msg); | 
 | 4623 |         return -1; | 
 | 4624 |     } | 
 | 4625 |     Py_DECREF(msg); | 
 | 4626 |     return 0; | 
 | 4627 | } | 
 | 4628 |  | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4629 | static PyObject * | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4630 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, | 
 | 4631 |                             size_t len) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4632 | { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4633 |     PyObject *v, *u; | 
 | 4634 |     char *buf; | 
 | 4635 |     char *p; | 
 | 4636 |     const char *end; | 
| Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4637 |  | 
| Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4638 |     /* check for integer overflow */ | 
| Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4639 |     if (len > SIZE_MAX / 6) | 
| Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4640 |         return NULL; | 
 | 4641 |     /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 | 
 | 4642 |        "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ | 
 | 4643 |     u = PyBytes_FromStringAndSize((char *)NULL, len * 6); | 
 | 4644 |     if (u == NULL) | 
 | 4645 |         return NULL; | 
 | 4646 |     p = buf = PyBytes_AsString(u); | 
 | 4647 |     end = s + len; | 
 | 4648 |     while (s < end) { | 
 | 4649 |         if (*s == '\\') { | 
 | 4650 |             *p++ = *s++; | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4651 |             if (s >= end || *s & 0x80) { | 
| Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4652 |                 strcpy(p, "u005c"); | 
 | 4653 |                 p += 5; | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4654 |                 if (s >= end) | 
 | 4655 |                     break; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4656 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4657 |         } | 
| Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4658 |         if (*s & 0x80) { /* XXX inefficient */ | 
 | 4659 |             PyObject *w; | 
 | 4660 |             int kind; | 
 | 4661 |             void *data; | 
 | 4662 |             Py_ssize_t len, i; | 
 | 4663 |             w = decode_utf8(c, &s, end); | 
 | 4664 |             if (w == NULL) { | 
 | 4665 |                 Py_DECREF(u); | 
 | 4666 |                 return NULL; | 
 | 4667 |             } | 
 | 4668 |             kind = PyUnicode_KIND(w); | 
 | 4669 |             data = PyUnicode_DATA(w); | 
 | 4670 |             len = PyUnicode_GET_LENGTH(w); | 
 | 4671 |             for (i = 0; i < len; i++) { | 
 | 4672 |                 Py_UCS4 chr = PyUnicode_READ(kind, data, i); | 
 | 4673 |                 sprintf(p, "\\U%08x", chr); | 
 | 4674 |                 p += 10; | 
 | 4675 |             } | 
 | 4676 |             /* Should be impossible to overflow */ | 
| Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4677 |             assert(p - buf <= PyBytes_GET_SIZE(u)); | 
| Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4678 |             Py_DECREF(w); | 
 | 4679 |         } else { | 
 | 4680 |             *p++ = *s++; | 
 | 4681 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4682 |     } | 
| Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4683 |     len = p - buf; | 
 | 4684 |     s = buf; | 
 | 4685 |  | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4686 |     const char *first_invalid_escape; | 
 | 4687 |     v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); | 
 | 4688 |  | 
 | 4689 |     if (v != NULL && first_invalid_escape != NULL) { | 
 | 4690 |         if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { | 
 | 4691 |             /* We have not decref u before because first_invalid_escape points | 
 | 4692 |                inside u. */ | 
 | 4693 |             Py_XDECREF(u); | 
 | 4694 |             Py_DECREF(v); | 
 | 4695 |             return NULL; | 
 | 4696 |         } | 
 | 4697 |     } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4698 |     Py_XDECREF(u); | 
 | 4699 |     return v; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4700 | } | 
 | 4701 |  | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4702 | static PyObject * | 
 | 4703 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, | 
 | 4704 |                           size_t len) | 
 | 4705 | { | 
 | 4706 |     const char *first_invalid_escape; | 
 | 4707 |     PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, | 
 | 4708 |                                              &first_invalid_escape); | 
 | 4709 |     if (result == NULL) | 
 | 4710 |         return NULL; | 
 | 4711 |  | 
 | 4712 |     if (first_invalid_escape != NULL) { | 
 | 4713 |         if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { | 
 | 4714 |             Py_DECREF(result); | 
 | 4715 |             return NULL; | 
 | 4716 |         } | 
 | 4717 |     } | 
 | 4718 |     return result; | 
 | 4719 | } | 
 | 4720 |  | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4721 | /* Shift locations for the given node and all its children by adding `lineno` | 
 | 4722 |    and `col_offset` to existing locations. */ | 
 | 4723 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) | 
 | 4724 | { | 
 | 4725 |     n->n_col_offset = n->n_col_offset + col_offset; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4726 |     n->n_end_col_offset = n->n_end_col_offset + col_offset; | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4727 |     for (int i = 0; i < NCH(n); ++i) { | 
 | 4728 |         if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { | 
 | 4729 |             /* Shifting column offsets unnecessary if there's been newlines. */ | 
 | 4730 |             col_offset = 0; | 
 | 4731 |         } | 
 | 4732 |         fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); | 
 | 4733 |     } | 
 | 4734 |     n->n_lineno = n->n_lineno + lineno; | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4735 |     n->n_end_lineno = n->n_end_lineno + lineno; | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4736 | } | 
 | 4737 |  | 
 | 4738 | /* Fix locations for the given node and its children. | 
 | 4739 |  | 
 | 4740 |    `parent` is the enclosing node. | 
 | 4741 |    `n` is the node which locations are going to be fixed relative to parent. | 
| luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 4742 |    `expr_str` is the child node's string representation, including braces. | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4743 | */ | 
 | 4744 | static void | 
 | 4745 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) | 
 | 4746 | { | 
 | 4747 |     char *substr = NULL; | 
 | 4748 |     char *start; | 
 | 4749 |     int lines = LINENO(parent) - 1; | 
 | 4750 |     int cols = parent->n_col_offset; | 
 | 4751 |     /* Find the full fstring to fix location information in `n`. */ | 
 | 4752 |     while (parent && parent->n_type != STRING) | 
 | 4753 |         parent = parent->n_child; | 
 | 4754 |     if (parent && parent->n_str) { | 
 | 4755 |         substr = strstr(parent->n_str, expr_str); | 
 | 4756 |         if (substr) { | 
 | 4757 |             start = substr; | 
 | 4758 |             while (start > parent->n_str) { | 
 | 4759 |                 if (start[0] == '\n') | 
 | 4760 |                     break; | 
 | 4761 |                 start--; | 
 | 4762 |             } | 
| Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4763 |             cols += (int)(substr - start); | 
| Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4764 |             /* adjust the start based on the number of newlines encountered | 
 | 4765 |                before the f-string expression */ | 
 | 4766 |             for (char* p = parent->n_str; p < substr; p++) { | 
 | 4767 |                 if (*p == '\n') { | 
 | 4768 |                     lines++; | 
 | 4769 |                 } | 
 | 4770 |             } | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4771 |         } | 
 | 4772 |     } | 
 | 4773 |     fstring_shift_node_locations(n, lines, cols); | 
 | 4774 | } | 
 | 4775 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4776 | /* Compile this expression in to an expr_ty.  Add parens around the | 
 | 4777 |    expression, in order to allow leading spaces in the expression. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4778 | static expr_ty | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4779 | fstring_compile_expr(const char *expr_start, const char *expr_end, | 
 | 4780 |                      struct compiling *c, const node *n) | 
| Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4781 |  | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4782 | { | 
 | 4783 |     PyCompilerFlags cf; | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4784 |     node *mod_n; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4785 |     mod_ty mod; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4786 |     char *str; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4787 |     Py_ssize_t len; | 
| Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4788 |     const char *s; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4789 |  | 
| Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4790 |     assert(expr_end >= expr_start); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4791 |     assert(*(expr_start-1) == '{'); | 
 | 4792 |     assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); | 
| Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4793 |  | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4794 |     /* If the substring is all whitespace, it's an error.  We need to catch this | 
 | 4795 |        here, and not when we call PyParser_SimpleParseStringFlagsFilename, | 
 | 4796 |        because turning the expression '' in to '()' would go from being invalid | 
 | 4797 |        to valid. */ | 
| Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4798 |     for (s = expr_start; s != expr_end; s++) { | 
 | 4799 |         char c = *s; | 
 | 4800 |         /* The Python parser ignores only the following whitespace | 
 | 4801 |            characters (\r already is converted to \n). */ | 
 | 4802 |         if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4803 |             break; | 
 | 4804 |         } | 
 | 4805 |     } | 
| Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4806 |     if (s == expr_end) { | 
| Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4807 |         ast_error(c, n, "f-string: empty expression not allowed"); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4808 |         return NULL; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4809 |     } | 
 | 4810 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4811 |     len = expr_end - expr_start; | 
 | 4812 |     /* Allocate 3 extra bytes: open paren, close paren, null byte. */ | 
 | 4813 |     str = PyMem_RawMalloc(len + 3); | 
| Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4814 |     if (str == NULL) { | 
 | 4815 |         PyErr_NoMemory(); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4816 |         return NULL; | 
| Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4817 |     } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4818 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4819 |     str[0] = '('; | 
 | 4820 |     memcpy(str+1, expr_start, len); | 
 | 4821 |     str[len+1] = ')'; | 
 | 4822 |     str[len+2] = 0; | 
| Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4823 |  | 
 | 4824 |     cf.cf_flags = PyCF_ONLY_AST; | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4825 |     cf.cf_feature_version = PY_MINOR_VERSION; | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4826 |     mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", | 
 | 4827 |                                                     Py_eval_input, 0); | 
 | 4828 |     if (!mod_n) { | 
 | 4829 |         PyMem_RawFree(str); | 
 | 4830 |         return NULL; | 
 | 4831 |     } | 
 | 4832 |     /* Reuse str to find the correct column offset. */ | 
 | 4833 |     str[0] = '{'; | 
 | 4834 |     str[len+1] = '}'; | 
 | 4835 |     fstring_fix_node_location(n, mod_n, str); | 
 | 4836 |     mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4837 |     PyMem_RawFree(str); | 
| Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4838 |     PyNode_Free(mod_n); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4839 |     if (!mod) | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4840 |         return NULL; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4841 |     return mod->v.Expression.body; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4842 | } | 
 | 4843 |  | 
 | 4844 | /* Return -1 on error. | 
 | 4845 |  | 
 | 4846 |    Return 0 if we reached the end of the literal. | 
 | 4847 |  | 
 | 4848 |    Return 1 if we haven't reached the end of the literal, but we want | 
 | 4849 |    the caller to process the literal up to this point. Used for | 
 | 4850 |    doubled braces. | 
 | 4851 | */ | 
 | 4852 | static int | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4853 | fstring_find_literal(const char **str, const char *end, int raw, | 
 | 4854 |                      PyObject **literal, int recurse_lvl, | 
 | 4855 |                      struct compiling *c, const node *n) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4856 | { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4857 |     /* Get any literal string. It ends when we hit an un-doubled left | 
 | 4858 |        brace (which isn't part of a unicode name escape such as | 
 | 4859 |        "\N{EULER CONSTANT}"), or the end of the string. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4860 |  | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4861 |     const char *s = *str; | 
 | 4862 |     const char *literal_start = s; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4863 |     int result = 0; | 
 | 4864 |  | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4865 |     assert(*literal == NULL); | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4866 |     while (s < end) { | 
 | 4867 |         char ch = *s++; | 
 | 4868 |         if (!raw && ch == '\\' && s < end) { | 
 | 4869 |             ch = *s++; | 
 | 4870 |             if (ch == 'N') { | 
 | 4871 |                 if (s < end && *s++ == '{') { | 
 | 4872 |                     while (s < end && *s++ != '}') { | 
 | 4873 |                     } | 
 | 4874 |                     continue; | 
 | 4875 |                 } | 
 | 4876 |                 break; | 
 | 4877 |             } | 
 | 4878 |             if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { | 
 | 4879 |                 return -1; | 
 | 4880 |             } | 
 | 4881 |         } | 
 | 4882 |         if (ch == '{' || ch == '}') { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4883 |             /* Check for doubled braces, but only at the top level. If | 
 | 4884 |                we checked at every level, then f'{0:{3}}' would fail | 
 | 4885 |                with the two closing braces. */ | 
 | 4886 |             if (recurse_lvl == 0) { | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4887 |                 if (s < end && *s == ch) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4888 |                     /* We're going to tell the caller that the literal ends | 
 | 4889 |                        here, but that they should continue scanning. But also | 
 | 4890 |                        skip over the second brace when we resume scanning. */ | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4891 |                     *str = s + 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4892 |                     result = 1; | 
 | 4893 |                     goto done; | 
 | 4894 |                 } | 
 | 4895 |  | 
 | 4896 |                 /* Where a single '{' is the start of a new expression, a | 
 | 4897 |                    single '}' is not allowed. */ | 
 | 4898 |                 if (ch == '}') { | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4899 |                     *str = s - 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4900 |                     ast_error(c, n, "f-string: single '}' is not allowed"); | 
 | 4901 |                     return -1; | 
 | 4902 |                 } | 
 | 4903 |             } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4904 |             /* We're either at a '{', which means we're starting another | 
 | 4905 |                expression; or a '}', which means we're at the end of this | 
 | 4906 |                f-string (for a nested format_spec). */ | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4907 |             s--; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4908 |             break; | 
 | 4909 |         } | 
 | 4910 |     } | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4911 |     *str = s; | 
 | 4912 |     assert(s <= end); | 
 | 4913 |     assert(s == end || *s == '{' || *s == '}'); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4914 | done: | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4915 |     if (literal_start != s) { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4916 |         if (raw) | 
 | 4917 |             *literal = PyUnicode_DecodeUTF8Stateful(literal_start, | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4918 |                                                     s - literal_start, | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4919 |                                                     NULL, NULL); | 
 | 4920 |         else | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4921 |             *literal = decode_unicode_with_escapes(c, n, literal_start, | 
| Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4922 |                                                    s - literal_start); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4923 |         if (!*literal) | 
 | 4924 |             return -1; | 
 | 4925 |     } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4926 |     return result; | 
 | 4927 | } | 
 | 4928 |  | 
 | 4929 | /* Forward declaration because parsing is recursive. */ | 
 | 4930 | static expr_ty | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4931 | fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4932 |               struct compiling *c, const node *n); | 
 | 4933 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4934 | /* Parse the f-string at *str, ending at end.  We know *str starts an | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4935 |    expression (so it must be a '{'). Returns the FormattedValue node, | 
 | 4936 |    which includes the expression, conversion character, and | 
 | 4937 |    format_spec expression. | 
 | 4938 |  | 
 | 4939 |    Note that I don't do a perfect job here: I don't make sure that a | 
 | 4940 |    closing brace doesn't match an opening paren, for example. It | 
 | 4941 |    doesn't need to error on all invalid expressions, just correctly | 
 | 4942 |    find the end of all valid ones. Any errors inside the expression | 
 | 4943 |    will be caught when we parse it later. */ | 
 | 4944 | static int | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4945 | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4946 |                   expr_ty *expression, struct compiling *c, const node *n) | 
 | 4947 | { | 
 | 4948 |     /* Return -1 on error, else 0. */ | 
 | 4949 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4950 |     const char *expr_start; | 
 | 4951 |     const char *expr_end; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4952 |     expr_ty simple_expression; | 
 | 4953 |     expr_ty format_spec = NULL; /* Optional format specifier. */ | 
| Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4954 |     int conversion = -1; /* The conversion char. -1 if not specified. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4955 |  | 
 | 4956 |     /* 0 if we're not in a string, else the quote char we're trying to | 
 | 4957 |        match (single or double quote). */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4958 |     char quote_char = 0; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4959 |  | 
 | 4960 |     /* If we're inside a string, 1=normal, 3=triple-quoted. */ | 
 | 4961 |     int string_type = 0; | 
 | 4962 |  | 
 | 4963 |     /* Keep track of nesting level for braces/parens/brackets in | 
 | 4964 |        expressions. */ | 
 | 4965 |     Py_ssize_t nested_depth = 0; | 
| Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4966 |     char parenstack[MAXLEVEL]; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4967 |  | 
 | 4968 |     /* Can only nest one level deep. */ | 
 | 4969 |     if (recurse_lvl >= 2) { | 
 | 4970 |         ast_error(c, n, "f-string: expressions nested too deeply"); | 
 | 4971 |         return -1; | 
 | 4972 |     } | 
 | 4973 |  | 
 | 4974 |     /* The first char must be a left brace, or we wouldn't have gotten | 
 | 4975 |        here. Skip over it. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4976 |     assert(**str == '{'); | 
 | 4977 |     *str += 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4978 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4979 |     expr_start = *str; | 
 | 4980 |     for (; *str < end; (*str)++) { | 
 | 4981 |         char ch; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4982 |  | 
 | 4983 |         /* Loop invariants. */ | 
 | 4984 |         assert(nested_depth >= 0); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4985 |         assert(*str >= expr_start && *str < end); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4986 |         if (quote_char) | 
 | 4987 |             assert(string_type == 1 || string_type == 3); | 
 | 4988 |         else | 
 | 4989 |             assert(string_type == 0); | 
 | 4990 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4991 |         ch = **str; | 
 | 4992 |         /* Nowhere inside an expression is a backslash allowed. */ | 
 | 4993 |         if (ch == '\\') { | 
 | 4994 |             /* Error: can't include a backslash character, inside | 
 | 4995 |                parens or strings or not. */ | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4996 |             ast_error(c, n, | 
 | 4997 |                       "f-string expression part " | 
 | 4998 |                       "cannot include a backslash"); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4999 |             return -1; | 
 | 5000 |         } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5001 |         if (quote_char) { | 
 | 5002 |             /* We're inside a string. See if we're at the end. */ | 
 | 5003 |             /* This code needs to implement the same non-error logic | 
 | 5004 |                as tok_get from tokenizer.c, at the letter_quote | 
 | 5005 |                label. To actually share that code would be a | 
 | 5006 |                nightmare. But, it's unlikely to change and is small, | 
 | 5007 |                so duplicate it here. Note we don't need to catch all | 
 | 5008 |                of the errors, since they'll be caught when parsing the | 
 | 5009 |                expression. We just need to match the non-error | 
 | 5010 |                cases. Thus we can ignore \n in single-quoted strings, | 
 | 5011 |                for example. Or non-terminated strings. */ | 
 | 5012 |             if (ch == quote_char) { | 
 | 5013 |                 /* Does this match the string_type (single or triple | 
 | 5014 |                    quoted)? */ | 
 | 5015 |                 if (string_type == 3) { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5016 |                     if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5017 |                         /* We're at the end of a triple quoted string. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5018 |                         *str += 2; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5019 |                         string_type = 0; | 
 | 5020 |                         quote_char = 0; | 
 | 5021 |                         continue; | 
 | 5022 |                     } | 
 | 5023 |                 } else { | 
 | 5024 |                     /* We're at the end of a normal string. */ | 
 | 5025 |                     quote_char = 0; | 
 | 5026 |                     string_type = 0; | 
 | 5027 |                     continue; | 
 | 5028 |                 } | 
 | 5029 |             } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5030 |         } else if (ch == '\'' || ch == '"') { | 
 | 5031 |             /* Is this a triple quoted string? */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5032 |             if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5033 |                 string_type = 3; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5034 |                 *str += 2; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5035 |             } else { | 
 | 5036 |                 /* Start of a normal string. */ | 
 | 5037 |                 string_type = 1; | 
 | 5038 |             } | 
 | 5039 |             /* Start looking for the end of the string. */ | 
 | 5040 |             quote_char = ch; | 
 | 5041 |         } else if (ch == '[' || ch == '{' || ch == '(') { | 
| Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5042 |             if (nested_depth >= MAXLEVEL) { | 
 | 5043 |                 ast_error(c, n, "f-string: too many nested parenthesis"); | 
 | 5044 |                 return -1; | 
 | 5045 |             } | 
 | 5046 |             parenstack[nested_depth] = ch; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5047 |             nested_depth++; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5048 |         } else if (ch == '#') { | 
 | 5049 |             /* Error: can't include a comment character, inside parens | 
 | 5050 |                or not. */ | 
| Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5051 |             ast_error(c, n, "f-string expression part cannot include '#'"); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5052 |             return -1; | 
 | 5053 |         } else if (nested_depth == 0 && | 
 | 5054 |                    (ch == '!' || ch == ':' || ch == '}')) { | 
 | 5055 |             /* First, test for the special case of "!=". Since '=' is | 
 | 5056 |                not an allowed conversion character, nothing is lost in | 
 | 5057 |                this test. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5058 |             if (ch == '!' && *str+1 < end && *(*str+1) == '=') { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5059 |                 /* This isn't a conversion character, just continue. */ | 
 | 5060 |                 continue; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5061 |             } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5062 |             /* Normal way out of this loop. */ | 
 | 5063 |             break; | 
| Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5064 |         } else if (ch == ']' || ch == '}' || ch == ')') { | 
 | 5065 |             if (!nested_depth) { | 
 | 5066 |                 ast_error(c, n, "f-string: unmatched '%c'", ch); | 
 | 5067 |                 return -1; | 
 | 5068 |             } | 
 | 5069 |             nested_depth--; | 
 | 5070 |             int opening = parenstack[nested_depth]; | 
 | 5071 |             if (!((opening == '(' && ch == ')') || | 
 | 5072 |                   (opening == '[' && ch == ']') || | 
 | 5073 |                   (opening == '{' && ch == '}'))) | 
 | 5074 |             { | 
 | 5075 |                 ast_error(c, n, | 
 | 5076 |                           "f-string: closing parenthesis '%c' " | 
 | 5077 |                           "does not match opening parenthesis '%c'", | 
 | 5078 |                           ch, opening); | 
 | 5079 |                 return -1; | 
 | 5080 |             } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5081 |         } else { | 
 | 5082 |             /* Just consume this char and loop around. */ | 
 | 5083 |         } | 
 | 5084 |     } | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5085 |     expr_end = *str; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5086 |     /* If we leave this loop in a string or with mismatched parens, we | 
 | 5087 |        don't care. We'll get a syntax error when compiling the | 
 | 5088 |        expression. But, we can produce a better error message, so | 
 | 5089 |        let's just do that.*/ | 
 | 5090 |     if (quote_char) { | 
 | 5091 |         ast_error(c, n, "f-string: unterminated string"); | 
 | 5092 |         return -1; | 
 | 5093 |     } | 
 | 5094 |     if (nested_depth) { | 
| Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5095 |         int opening = parenstack[nested_depth - 1]; | 
 | 5096 |         ast_error(c, n, "f-string: unmatched '%c'", opening); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5097 |         return -1; | 
 | 5098 |     } | 
 | 5099 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5100 |     if (*str >= end) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5101 |         goto unexpected_end_of_string; | 
| Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5102 |  | 
 | 5103 |     /* Compile the expression as soon as possible, so we show errors | 
 | 5104 |        related to the expression before errors related to the | 
 | 5105 |        conversion or format_spec. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5106 |     simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); | 
| Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5107 |     if (!simple_expression) | 
 | 5108 |         return -1; | 
 | 5109 |  | 
 | 5110 |     /* Check for a conversion char, if present. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5111 |     if (**str == '!') { | 
 | 5112 |         *str += 1; | 
 | 5113 |         if (*str >= end) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5114 |             goto unexpected_end_of_string; | 
 | 5115 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5116 |         conversion = **str; | 
 | 5117 |         *str += 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5118 |  | 
 | 5119 |         /* Validate the conversion. */ | 
 | 5120 |         if (!(conversion == 's' || conversion == 'r' | 
 | 5121 |               || conversion == 'a')) { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5122 |             ast_error(c, n, | 
 | 5123 |                       "f-string: invalid conversion character: " | 
 | 5124 |                       "expected 's', 'r', or 'a'"); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5125 |             return -1; | 
 | 5126 |         } | 
 | 5127 |     } | 
 | 5128 |  | 
 | 5129 |     /* Check for the format spec, if present. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5130 |     if (*str >= end) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5131 |         goto unexpected_end_of_string; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5132 |     if (**str == ':') { | 
 | 5133 |         *str += 1; | 
 | 5134 |         if (*str >= end) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5135 |             goto unexpected_end_of_string; | 
 | 5136 |  | 
 | 5137 |         /* Parse the format spec. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5138 |         format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5139 |         if (!format_spec) | 
 | 5140 |             return -1; | 
 | 5141 |     } | 
 | 5142 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5143 |     if (*str >= end || **str != '}') | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5144 |         goto unexpected_end_of_string; | 
 | 5145 |  | 
 | 5146 |     /* We're at a right brace. Consume it. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5147 |     assert(*str < end); | 
 | 5148 |     assert(**str == '}'); | 
 | 5149 |     *str += 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5150 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5151 |     /* And now create the FormattedValue node that represents this | 
 | 5152 |        entire expression with the conversion and format spec. */ | 
| Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5153 |     *expression = FormattedValue(simple_expression, conversion, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5154 |                                  format_spec, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5155 |                                  n->n_end_lineno, n->n_end_col_offset, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5156 |                                  c->c_arena); | 
 | 5157 |     if (!*expression) | 
 | 5158 |         return -1; | 
 | 5159 |  | 
 | 5160 |     return 0; | 
 | 5161 |  | 
 | 5162 | unexpected_end_of_string: | 
 | 5163 |     ast_error(c, n, "f-string: expecting '}'"); | 
 | 5164 |     return -1; | 
 | 5165 | } | 
 | 5166 |  | 
 | 5167 | /* Return -1 on error. | 
 | 5168 |  | 
 | 5169 |    Return 0 if we have a literal (possible zero length) and an | 
 | 5170 |    expression (zero length if at the end of the string. | 
 | 5171 |  | 
 | 5172 |    Return 1 if we have a literal, but no expression, and we want the | 
 | 5173 |    caller to call us again. This is used to deal with doubled | 
 | 5174 |    braces. | 
 | 5175 |  | 
 | 5176 |    When called multiple times on the string 'a{{b{0}c', this function | 
 | 5177 |    will return: | 
 | 5178 |  | 
 | 5179 |    1. the literal 'a{' with no expression, and a return value | 
 | 5180 |       of 1. Despite the fact that there's no expression, the return | 
 | 5181 |       value of 1 means we're not finished yet. | 
 | 5182 |  | 
 | 5183 |    2. the literal 'b' and the expression '0', with a return value of | 
 | 5184 |       0. The fact that there's an expression means we're not finished. | 
 | 5185 |  | 
 | 5186 |    3. literal 'c' with no expression and a return value of 0. The | 
 | 5187 |       combination of the return value of 0 with no expression means | 
 | 5188 |       we're finished. | 
 | 5189 | */ | 
 | 5190 | static int | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5191 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, | 
 | 5192 |                               int recurse_lvl, PyObject **literal, | 
 | 5193 |                               expr_ty *expression, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5194 |                               struct compiling *c, const node *n) | 
 | 5195 | { | 
 | 5196 |     int result; | 
 | 5197 |  | 
 | 5198 |     assert(*literal == NULL && *expression == NULL); | 
 | 5199 |  | 
 | 5200 |     /* Get any literal string. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5201 |     result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5202 |     if (result < 0) | 
 | 5203 |         goto error; | 
 | 5204 |  | 
 | 5205 |     assert(result == 0 || result == 1); | 
 | 5206 |  | 
 | 5207 |     if (result == 1) | 
 | 5208 |         /* We have a literal, but don't look at the expression. */ | 
 | 5209 |         return 1; | 
 | 5210 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5211 |     if (*str >= end || **str == '}') | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5212 |         /* We're at the end of the string or the end of a nested | 
 | 5213 |            f-string: no expression. The top-level error case where we | 
 | 5214 |            expect to be at the end of the string but we're at a '}' is | 
 | 5215 |            handled later. */ | 
 | 5216 |         return 0; | 
 | 5217 |  | 
 | 5218 |     /* We must now be the start of an expression, on a '{'. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5219 |     assert(**str == '{'); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5220 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5221 |     if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5222 |         goto error; | 
 | 5223 |  | 
 | 5224 |     return 0; | 
 | 5225 |  | 
 | 5226 | error: | 
| Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5227 |     Py_CLEAR(*literal); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5228 |     return -1; | 
 | 5229 | } | 
 | 5230 |  | 
 | 5231 | #define EXPRLIST_N_CACHED  64 | 
 | 5232 |  | 
 | 5233 | typedef struct { | 
 | 5234 |     /* Incrementally build an array of expr_ty, so be used in an | 
 | 5235 |        asdl_seq. Cache some small but reasonably sized number of | 
 | 5236 |        expr_ty's, and then after that start dynamically allocating, | 
 | 5237 |        doubling the number allocated each time. Note that the f-string | 
 | 5238 |        f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5239 |        Constant for the literal 'a'. So you add expr_ty's about twice as | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5240 |        fast as you add exressions in an f-string. */ | 
 | 5241 |  | 
 | 5242 |     Py_ssize_t allocated;  /* Number we've allocated. */ | 
 | 5243 |     Py_ssize_t size;       /* Number we've used. */ | 
 | 5244 |     expr_ty    *p;         /* Pointer to the memory we're actually | 
 | 5245 |                               using. Will point to 'data' until we | 
 | 5246 |                               start dynamically allocating. */ | 
 | 5247 |     expr_ty    data[EXPRLIST_N_CACHED]; | 
 | 5248 | } ExprList; | 
 | 5249 |  | 
 | 5250 | #ifdef NDEBUG | 
 | 5251 | #define ExprList_check_invariants(l) | 
 | 5252 | #else | 
 | 5253 | static void | 
 | 5254 | ExprList_check_invariants(ExprList *l) | 
 | 5255 | { | 
 | 5256 |     /* Check our invariants. Make sure this object is "live", and | 
 | 5257 |        hasn't been deallocated. */ | 
 | 5258 |     assert(l->size >= 0); | 
 | 5259 |     assert(l->p != NULL); | 
 | 5260 |     if (l->size <= EXPRLIST_N_CACHED) | 
 | 5261 |         assert(l->data == l->p); | 
 | 5262 | } | 
 | 5263 | #endif | 
 | 5264 |  | 
 | 5265 | static void | 
 | 5266 | ExprList_Init(ExprList *l) | 
 | 5267 | { | 
 | 5268 |     l->allocated = EXPRLIST_N_CACHED; | 
 | 5269 |     l->size = 0; | 
 | 5270 |  | 
 | 5271 |     /* Until we start allocating dynamically, p points to data. */ | 
 | 5272 |     l->p = l->data; | 
 | 5273 |  | 
 | 5274 |     ExprList_check_invariants(l); | 
 | 5275 | } | 
 | 5276 |  | 
 | 5277 | static int | 
 | 5278 | ExprList_Append(ExprList *l, expr_ty exp) | 
 | 5279 | { | 
 | 5280 |     ExprList_check_invariants(l); | 
 | 5281 |     if (l->size >= l->allocated) { | 
 | 5282 |         /* We need to alloc (or realloc) the memory. */ | 
 | 5283 |         Py_ssize_t new_size = l->allocated * 2; | 
 | 5284 |  | 
 | 5285 |         /* See if we've ever allocated anything dynamically. */ | 
 | 5286 |         if (l->p == l->data) { | 
 | 5287 |             Py_ssize_t i; | 
 | 5288 |             /* We're still using the cached data. Switch to | 
 | 5289 |                alloc-ing. */ | 
 | 5290 |             l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); | 
 | 5291 |             if (!l->p) | 
 | 5292 |                 return -1; | 
 | 5293 |             /* Copy the cached data into the new buffer. */ | 
 | 5294 |             for (i = 0; i < l->size; i++) | 
 | 5295 |                 l->p[i] = l->data[i]; | 
 | 5296 |         } else { | 
 | 5297 |             /* Just realloc. */ | 
 | 5298 |             expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); | 
 | 5299 |             if (!tmp) { | 
 | 5300 |                 PyMem_RawFree(l->p); | 
 | 5301 |                 l->p = NULL; | 
 | 5302 |                 return -1; | 
 | 5303 |             } | 
 | 5304 |             l->p = tmp; | 
 | 5305 |         } | 
 | 5306 |  | 
 | 5307 |         l->allocated = new_size; | 
 | 5308 |         assert(l->allocated == 2 * l->size); | 
 | 5309 |     } | 
 | 5310 |  | 
 | 5311 |     l->p[l->size++] = exp; | 
 | 5312 |  | 
 | 5313 |     ExprList_check_invariants(l); | 
 | 5314 |     return 0; | 
 | 5315 | } | 
 | 5316 |  | 
 | 5317 | static void | 
 | 5318 | ExprList_Dealloc(ExprList *l) | 
 | 5319 | { | 
 | 5320 |     ExprList_check_invariants(l); | 
 | 5321 |  | 
 | 5322 |     /* If there's been an error, or we've never dynamically allocated, | 
 | 5323 |        do nothing. */ | 
 | 5324 |     if (!l->p || l->p == l->data) { | 
 | 5325 |         /* Do nothing. */ | 
 | 5326 |     } else { | 
 | 5327 |         /* We have dynamically allocated. Free the memory. */ | 
 | 5328 |         PyMem_RawFree(l->p); | 
 | 5329 |     } | 
 | 5330 |     l->p = NULL; | 
 | 5331 |     l->size = -1; | 
 | 5332 | } | 
 | 5333 |  | 
 | 5334 | static asdl_seq * | 
 | 5335 | ExprList_Finish(ExprList *l, PyArena *arena) | 
 | 5336 | { | 
 | 5337 |     asdl_seq *seq; | 
 | 5338 |  | 
 | 5339 |     ExprList_check_invariants(l); | 
 | 5340 |  | 
 | 5341 |     /* Allocate the asdl_seq and copy the expressions in to it. */ | 
 | 5342 |     seq = _Py_asdl_seq_new(l->size, arena); | 
 | 5343 |     if (seq) { | 
 | 5344 |         Py_ssize_t i; | 
 | 5345 |         for (i = 0; i < l->size; i++) | 
 | 5346 |             asdl_seq_SET(seq, i, l->p[i]); | 
 | 5347 |     } | 
 | 5348 |     ExprList_Dealloc(l); | 
 | 5349 |     return seq; | 
 | 5350 | } | 
 | 5351 |  | 
 | 5352 | /* The FstringParser is designed to add a mix of strings and | 
 | 5353 |    f-strings, and concat them together as needed. Ultimately, it | 
 | 5354 |    generates an expr_ty. */ | 
 | 5355 | typedef struct { | 
 | 5356 |     PyObject *last_str; | 
 | 5357 |     ExprList expr_list; | 
| Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5358 |     int fmode; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5359 | } FstringParser; | 
 | 5360 |  | 
 | 5361 | #ifdef NDEBUG | 
 | 5362 | #define FstringParser_check_invariants(state) | 
 | 5363 | #else | 
 | 5364 | static void | 
 | 5365 | FstringParser_check_invariants(FstringParser *state) | 
 | 5366 | { | 
 | 5367 |     if (state->last_str) | 
 | 5368 |         assert(PyUnicode_CheckExact(state->last_str)); | 
 | 5369 |     ExprList_check_invariants(&state->expr_list); | 
 | 5370 | } | 
 | 5371 | #endif | 
 | 5372 |  | 
 | 5373 | static void | 
 | 5374 | FstringParser_Init(FstringParser *state) | 
 | 5375 | { | 
 | 5376 |     state->last_str = NULL; | 
| Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5377 |     state->fmode = 0; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5378 |     ExprList_Init(&state->expr_list); | 
 | 5379 |     FstringParser_check_invariants(state); | 
 | 5380 | } | 
 | 5381 |  | 
 | 5382 | static void | 
 | 5383 | FstringParser_Dealloc(FstringParser *state) | 
 | 5384 | { | 
 | 5385 |     FstringParser_check_invariants(state); | 
 | 5386 |  | 
 | 5387 |     Py_XDECREF(state->last_str); | 
 | 5388 |     ExprList_Dealloc(&state->expr_list); | 
 | 5389 | } | 
 | 5390 |  | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5391 | /* Constants for the following */ | 
 | 5392 | static PyObject *u_kind; | 
 | 5393 |  | 
 | 5394 | /* Compute 'kind' field for string Constant (either 'u' or None) */ | 
 | 5395 | static PyObject * | 
 | 5396 | make_kind(struct compiling *c, const node *n) | 
 | 5397 | { | 
 | 5398 |     char *s = NULL; | 
 | 5399 |     PyObject *kind = NULL; | 
 | 5400 |  | 
 | 5401 |     /* Find the first string literal, if any */ | 
 | 5402 |     while (TYPE(n) != STRING) { | 
 | 5403 |         if (NCH(n) == 0) | 
 | 5404 |             return NULL; | 
 | 5405 |         n = CHILD(n, 0); | 
 | 5406 |     } | 
 | 5407 |     REQ(n, STRING); | 
 | 5408 |  | 
 | 5409 |     /* If it starts with 'u', return a PyUnicode "u" string */ | 
 | 5410 |     s = STR(n); | 
 | 5411 |     if (s && *s == 'u') { | 
 | 5412 |         if (!u_kind) { | 
 | 5413 |             u_kind = PyUnicode_InternFromString("u"); | 
 | 5414 |             if (!u_kind) | 
 | 5415 |                 return NULL; | 
 | 5416 |         } | 
 | 5417 |         kind = u_kind; | 
 | 5418 |         if (PyArena_AddPyObject(c->c_arena, kind) < 0) { | 
 | 5419 |             return NULL; | 
 | 5420 |         } | 
 | 5421 |         Py_INCREF(kind); | 
 | 5422 |     } | 
 | 5423 |     return kind; | 
 | 5424 | } | 
 | 5425 |  | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5426 | /* Make a Constant node, but decref the PyUnicode object being added. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5427 | static expr_ty | 
 | 5428 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) | 
 | 5429 | { | 
 | 5430 |     PyObject *s = *str; | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5431 |     PyObject *kind = NULL; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5432 |     *str = NULL; | 
 | 5433 |     assert(PyUnicode_CheckExact(s)); | 
 | 5434 |     if (PyArena_AddPyObject(c->c_arena, s) < 0) { | 
 | 5435 |         Py_DECREF(s); | 
 | 5436 |         return NULL; | 
 | 5437 |     } | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5438 |     kind = make_kind(c, n); | 
 | 5439 |     if (kind == NULL && PyErr_Occurred()) | 
 | 5440 |         return NULL; | 
 | 5441 |     return Constant(s, kind, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5442 |                     n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5443 | } | 
 | 5444 |  | 
 | 5445 | /* Add a non-f-string (that is, a regular literal string). str is | 
 | 5446 |    decref'd. */ | 
 | 5447 | static int | 
 | 5448 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) | 
 | 5449 | { | 
 | 5450 |     FstringParser_check_invariants(state); | 
 | 5451 |  | 
 | 5452 |     assert(PyUnicode_CheckExact(str)); | 
 | 5453 |  | 
 | 5454 |     if (PyUnicode_GET_LENGTH(str) == 0) { | 
 | 5455 |         Py_DECREF(str); | 
 | 5456 |         return 0; | 
 | 5457 |     } | 
 | 5458 |  | 
 | 5459 |     if (!state->last_str) { | 
 | 5460 |         /* We didn't have a string before, so just remember this one. */ | 
 | 5461 |         state->last_str = str; | 
 | 5462 |     } else { | 
 | 5463 |         /* Concatenate this with the previous string. */ | 
| Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5464 |         PyUnicode_AppendAndDel(&state->last_str, str); | 
 | 5465 |         if (!state->last_str) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5466 |             return -1; | 
 | 5467 |     } | 
 | 5468 |     FstringParser_check_invariants(state); | 
 | 5469 |     return 0; | 
 | 5470 | } | 
 | 5471 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5472 | /* Parse an f-string. The f-string is in *str to end, with no | 
 | 5473 |    'f' or quotes. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5474 | static int | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5475 | FstringParser_ConcatFstring(FstringParser *state, const char **str, | 
 | 5476 |                             const char *end, int raw, int recurse_lvl, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5477 |                             struct compiling *c, const node *n) | 
 | 5478 | { | 
 | 5479 |     FstringParser_check_invariants(state); | 
| Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5480 |     state->fmode = 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5481 |  | 
 | 5482 |     /* Parse the f-string. */ | 
 | 5483 |     while (1) { | 
 | 5484 |         PyObject *literal = NULL; | 
 | 5485 |         expr_ty expression = NULL; | 
 | 5486 |  | 
 | 5487 |         /* If there's a zero length literal in front of the | 
 | 5488 |            expression, literal will be NULL. If we're at the end of | 
 | 5489 |            the f-string, expression will be NULL (unless result == 1, | 
 | 5490 |            see below). */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5491 |         int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5492 |                                                    &literal, &expression, | 
 | 5493 |                                                    c, n); | 
 | 5494 |         if (result < 0) | 
 | 5495 |             return -1; | 
 | 5496 |  | 
 | 5497 |         /* Add the literal, if any. */ | 
 | 5498 |         if (!literal) { | 
 | 5499 |             /* Do nothing. Just leave last_str alone (and possibly | 
 | 5500 |                NULL). */ | 
 | 5501 |         } else if (!state->last_str) { | 
| ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 5502 |             /*  Note that the literal can be zero length, if the | 
 | 5503 |                 input string is "\\\n" or "\\\r", among others. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5504 |             state->last_str = literal; | 
 | 5505 |             literal = NULL; | 
 | 5506 |         } else { | 
 | 5507 |             /* We have a literal, concatenate it. */ | 
 | 5508 |             assert(PyUnicode_GET_LENGTH(literal) != 0); | 
 | 5509 |             if (FstringParser_ConcatAndDel(state, literal) < 0) | 
 | 5510 |                 return -1; | 
 | 5511 |             literal = NULL; | 
 | 5512 |         } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5513 |  | 
 | 5514 |         /* We've dealt with the literal now. It can't be leaked on further | 
 | 5515 |            errors. */ | 
 | 5516 |         assert(literal == NULL); | 
 | 5517 |  | 
 | 5518 |         /* See if we should just loop around to get the next literal | 
 | 5519 |            and expression, while ignoring the expression this | 
 | 5520 |            time. This is used for un-doubling braces, as an | 
 | 5521 |            optimization. */ | 
 | 5522 |         if (result == 1) | 
 | 5523 |             continue; | 
 | 5524 |  | 
 | 5525 |         if (!expression) | 
 | 5526 |             /* We're done with this f-string. */ | 
 | 5527 |             break; | 
 | 5528 |  | 
 | 5529 |         /* We know we have an expression. Convert any existing string | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5530 |            to a Constant node. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5531 |         if (!state->last_str) { | 
 | 5532 |             /* Do nothing. No previous literal. */ | 
 | 5533 |         } else { | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5534 |             /* Convert the existing last_str literal to a Constant node. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5535 |             expr_ty str = make_str_node_and_del(&state->last_str, c, n); | 
 | 5536 |             if (!str || ExprList_Append(&state->expr_list, str) < 0) | 
 | 5537 |                 return -1; | 
 | 5538 |         } | 
 | 5539 |  | 
 | 5540 |         if (ExprList_Append(&state->expr_list, expression) < 0) | 
 | 5541 |             return -1; | 
 | 5542 |     } | 
 | 5543 |  | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5544 |     /* If recurse_lvl is zero, then we must be at the end of the | 
 | 5545 |        string. Otherwise, we must be at a right brace. */ | 
 | 5546 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5547 |     if (recurse_lvl == 0 && *str < end-1) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5548 |         ast_error(c, n, "f-string: unexpected end of string"); | 
 | 5549 |         return -1; | 
 | 5550 |     } | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5551 |     if (recurse_lvl != 0 && **str != '}') { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5552 |         ast_error(c, n, "f-string: expecting '}'"); | 
 | 5553 |         return -1; | 
 | 5554 |     } | 
 | 5555 |  | 
 | 5556 |     FstringParser_check_invariants(state); | 
 | 5557 |     return 0; | 
 | 5558 | } | 
 | 5559 |  | 
 | 5560 | /* Convert the partial state reflected in last_str and expr_list to an | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5561 |    expr_ty. The expr_ty can be a Constant, or a JoinedStr. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5562 | static expr_ty | 
 | 5563 | FstringParser_Finish(FstringParser *state, struct compiling *c, | 
 | 5564 |                      const node *n) | 
 | 5565 | { | 
 | 5566 |     asdl_seq *seq; | 
 | 5567 |  | 
 | 5568 |     FstringParser_check_invariants(state); | 
 | 5569 |  | 
 | 5570 |     /* If we're just a constant string with no expressions, return | 
 | 5571 |        that. */ | 
| Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5572 |     if (!state->fmode) { | 
 | 5573 |         assert(!state->expr_list.size); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5574 |         if (!state->last_str) { | 
 | 5575 |             /* Create a zero length string. */ | 
 | 5576 |             state->last_str = PyUnicode_FromStringAndSize(NULL, 0); | 
 | 5577 |             if (!state->last_str) | 
 | 5578 |                 goto error; | 
 | 5579 |         } | 
 | 5580 |         return make_str_node_and_del(&state->last_str, c, n); | 
 | 5581 |     } | 
 | 5582 |  | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5583 |     /* Create a Constant node out of last_str, if needed. It will be the | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5584 |        last node in our expression list. */ | 
 | 5585 |     if (state->last_str) { | 
 | 5586 |         expr_ty str = make_str_node_and_del(&state->last_str, c, n); | 
 | 5587 |         if (!str || ExprList_Append(&state->expr_list, str) < 0) | 
 | 5588 |             goto error; | 
 | 5589 |     } | 
 | 5590 |     /* This has already been freed. */ | 
 | 5591 |     assert(state->last_str == NULL); | 
 | 5592 |  | 
 | 5593 |     seq = ExprList_Finish(&state->expr_list, c->c_arena); | 
 | 5594 |     if (!seq) | 
 | 5595 |         goto error; | 
 | 5596 |  | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5597 |     return JoinedStr(seq, LINENO(n), n->n_col_offset, | 
 | 5598 |                      n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5599 |  | 
 | 5600 | error: | 
 | 5601 |     FstringParser_Dealloc(state); | 
 | 5602 |     return NULL; | 
 | 5603 | } | 
 | 5604 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5605 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends | 
 | 5606 |    at end, parse it into an expr_ty.  Return NULL on error.  Adjust | 
 | 5607 |    str to point past the parsed portion. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5608 | static expr_ty | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5609 | fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5610 |               struct compiling *c, const node *n) | 
 | 5611 | { | 
 | 5612 |     FstringParser state; | 
 | 5613 |  | 
 | 5614 |     FstringParser_Init(&state); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5615 |     if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5616 |                                     c, n) < 0) { | 
 | 5617 |         FstringParser_Dealloc(&state); | 
 | 5618 |         return NULL; | 
 | 5619 |     } | 
 | 5620 |  | 
 | 5621 |     return FstringParser_Finish(&state, c, n); | 
 | 5622 | } | 
 | 5623 |  | 
 | 5624 | /* n is a Python string literal, including the bracketing quote | 
 | 5625 |    characters, and r, b, u, &/or f prefixes (if any), and embedded | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5626 |    escape sequences (if any). parsestr parses it, and sets *result to | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5627 |    decoded Python string object.  If the string is an f-string, set | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5628 |    *fstr and *fstrlen to the unparsed string object.  Return 0 if no | 
 | 5629 |    errors occurred. | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5630 | */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5631 | static int | 
 | 5632 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, | 
 | 5633 |          PyObject **result, const char **fstr, Py_ssize_t *fstrlen) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5634 | { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5635 |     size_t len; | 
 | 5636 |     const char *s = STR(n); | 
 | 5637 |     int quote = Py_CHARMASK(*s); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5638 |     int fmode = 0; | 
 | 5639 |     *bytesmode = 0; | 
 | 5640 |     *rawmode = 0; | 
 | 5641 |     *result = NULL; | 
 | 5642 |     *fstr = NULL; | 
| Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5643 |     if (Py_ISALPHA(quote)) { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5644 |         while (!*bytesmode || !*rawmode) { | 
| Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5645 |             if (quote == 'b' || quote == 'B') { | 
 | 5646 |                 quote = *++s; | 
 | 5647 |                 *bytesmode = 1; | 
 | 5648 |             } | 
| Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5649 |             else if (quote == 'u' || quote == 'U') { | 
 | 5650 |                 quote = *++s; | 
 | 5651 |             } | 
| Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5652 |             else if (quote == 'r' || quote == 'R') { | 
 | 5653 |                 quote = *++s; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5654 |                 *rawmode = 1; | 
| Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5655 |             } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5656 |             else if (quote == 'f' || quote == 'F') { | 
 | 5657 |                 quote = *++s; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5658 |                 fmode = 1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5659 |             } | 
| Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5660 |             else { | 
 | 5661 |                 break; | 
 | 5662 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5663 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5664 |     } | 
| Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 5665 |  | 
 | 5666 |     /* fstrings are only allowed in Python 3.6 and greater */ | 
 | 5667 |     if (fmode && c->c_feature_version < 6) { | 
 | 5668 |         ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); | 
 | 5669 |         return -1; | 
 | 5670 |     } | 
 | 5671 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5672 |     if (fmode && *bytesmode) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5673 |         PyErr_BadInternalCall(); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5674 |         return -1; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5675 |     } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5676 |     if (quote != '\'' && quote != '\"') { | 
 | 5677 |         PyErr_BadInternalCall(); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5678 |         return -1; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5679 |     } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5680 |     /* Skip the leading quote char. */ | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5681 |     s++; | 
 | 5682 |     len = strlen(s); | 
 | 5683 |     if (len > INT_MAX) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 |         PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5685 |                         "string to parse is too long"); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5686 |         return -1; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5687 |     } | 
 | 5688 |     if (s[--len] != quote) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5689 |         /* Last quote char must match the first. */ | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5690 |         PyErr_BadInternalCall(); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5691 |         return -1; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5692 |     } | 
 | 5693 |     if (len >= 4 && s[0] == quote && s[1] == quote) { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5694 |         /* A triple quoted string. We've already skipped one quote at | 
 | 5695 |            the start and one at the end of the string. Now skip the | 
 | 5696 |            two at the start. */ | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5697 |         s += 2; | 
 | 5698 |         len -= 2; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5699 |         /* And check that the last two match. */ | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5700 |         if (s[--len] != quote || s[--len] != quote) { | 
 | 5701 |             PyErr_BadInternalCall(); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5702 |             return -1; | 
| Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5703 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5704 |     } | 
| Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5705 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5706 |     if (fmode) { | 
 | 5707 |         /* Just return the bytes. The caller will parse the resulting | 
 | 5708 |            string. */ | 
 | 5709 |         *fstr = s; | 
 | 5710 |         *fstrlen = len; | 
 | 5711 |         return 0; | 
| Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5712 |     } | 
 | 5713 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5714 |     /* Not an f-string. */ | 
| Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5715 |     /* Avoid invoking escape decoding routines if possible. */ | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5716 |     *rawmode = *rawmode || strchr(s, '\\') == NULL; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5717 |     if (*bytesmode) { | 
| Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5718 |         /* Disallow non-ASCII characters. */ | 
| Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5719 |         const char *ch; | 
 | 5720 |         for (ch = s; *ch; ch++) { | 
 | 5721 |             if (Py_CHARMASK(*ch) >= 0x80) { | 
| Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5722 |                 ast_error(c, n, | 
 | 5723 |                           "bytes can only contain ASCII " | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5724 |                           "literal characters."); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5725 |                 return -1; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5726 |             } | 
| Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5727 |         } | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5728 |         if (*rawmode) | 
 | 5729 |             *result = PyBytes_FromStringAndSize(s, len); | 
| Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5730 |         else | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5731 |             *result = decode_bytes_with_escapes(c, n, s, len); | 
| Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5732 |     } else { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5733 |         if (*rawmode) | 
 | 5734 |             *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); | 
| Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5735 |         else | 
| Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5736 |             *result = decode_unicode_with_escapes(c, n, s, len); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5737 |     } | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5738 |     return *result == NULL ? -1 : 0; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5739 | } | 
 | 5740 |  | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5741 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through | 
 | 5742 |    each STRING atom, and process it as needed. For bytes, just | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5743 |    concatenate them together, and the result will be a Constant node. For | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5744 |    normal strings and f-strings, concatenate them together. The result | 
| Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5745 |    will be a Constant node if there were no f-strings; a FormattedValue | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5746 |    node if there's just an f-string (with no leading or trailing | 
 | 5747 |    literals), or a JoinedStr node if there are multiple f-strings or | 
 | 5748 |    any literals involved. */ | 
 | 5749 | static expr_ty | 
 | 5750 | parsestrplus(struct compiling *c, const node *n) | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5751 | { | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5752 |     int bytesmode = 0; | 
 | 5753 |     PyObject *bytes_str = NULL; | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5754 |     int i; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5755 |  | 
 | 5756 |     FstringParser state; | 
 | 5757 |     FstringParser_Init(&state); | 
 | 5758 |  | 
 | 5759 |     for (i = 0; i < NCH(n); i++) { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5760 |         int this_bytesmode; | 
 | 5761 |         int this_rawmode; | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5762 |         PyObject *s; | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5763 |         const char *fstr; | 
 | 5764 |         Py_ssize_t fstrlen = -1;  /* Silence a compiler warning. */ | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5765 |  | 
 | 5766 |         REQ(CHILD(n, i), STRING); | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5767 |         if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, | 
 | 5768 |                      &fstr, &fstrlen) != 0) | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5769 |             goto error; | 
 | 5770 |  | 
 | 5771 |         /* Check that we're not mixing bytes with unicode. */ | 
 | 5772 |         if (i != 0 && bytesmode != this_bytesmode) { | 
 | 5773 |             ast_error(c, n, "cannot mix bytes and nonbytes literals"); | 
| Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5774 |             /* s is NULL if the current string part is an f-string. */ | 
 | 5775 |             Py_XDECREF(s); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5776 |             goto error; | 
 | 5777 |         } | 
 | 5778 |         bytesmode = this_bytesmode; | 
 | 5779 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5780 |         if (fstr != NULL) { | 
 | 5781 |             int result; | 
 | 5782 |             assert(s == NULL && !bytesmode); | 
 | 5783 |             /* This is an f-string. Parse and concatenate it. */ | 
 | 5784 |             result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, | 
 | 5785 |                                                  this_rawmode, 0, c, n); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5786 |             if (result < 0) | 
 | 5787 |                 goto error; | 
 | 5788 |         } else { | 
| Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5789 |             /* A string or byte string. */ | 
 | 5790 |             assert(s != NULL && fstr == NULL); | 
 | 5791 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5792 |             assert(bytesmode ? PyBytes_CheckExact(s) : | 
 | 5793 |                    PyUnicode_CheckExact(s)); | 
 | 5794 |  | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5795 |             if (bytesmode) { | 
 | 5796 |                 /* For bytes, concat as we go. */ | 
 | 5797 |                 if (i == 0) { | 
 | 5798 |                     /* First time, just remember this value. */ | 
 | 5799 |                     bytes_str = s; | 
 | 5800 |                 } else { | 
 | 5801 |                     PyBytes_ConcatAndDel(&bytes_str, s); | 
 | 5802 |                     if (!bytes_str) | 
 | 5803 |                         goto error; | 
 | 5804 |                 } | 
 | 5805 |             } else { | 
| Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5806 |                 /* This is a regular string. Concatenate it. */ | 
 | 5807 |                 if (FstringParser_ConcatAndDel(&state, s) < 0) | 
 | 5808 |                     goto error; | 
 | 5809 |             } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5810 |         } | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5811 |     } | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5812 |     if (bytesmode) { | 
 | 5813 |         /* Just return the bytes object and we're done. */ | 
 | 5814 |         if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) | 
 | 5815 |             goto error; | 
| Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5816 |         return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, | 
| Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5817 |                         n->n_end_lineno, n->n_end_col_offset, c->c_arena); | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5818 |     } | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5819 |  | 
| Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5820 |     /* We're not a bytes string, bytes_str should never have been set. */ | 
 | 5821 |     assert(bytes_str == NULL); | 
 | 5822 |  | 
 | 5823 |     return FstringParser_Finish(&state, c, n); | 
 | 5824 |  | 
 | 5825 | error: | 
 | 5826 |     Py_XDECREF(bytes_str); | 
 | 5827 |     FstringParser_Dealloc(&state); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5828 |     return NULL; | 
| Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5829 | } | 
| Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5830 |  | 
 | 5831 | PyObject * | 
 | 5832 | _PyAST_GetDocString(asdl_seq *body) | 
 | 5833 | { | 
 | 5834 |     if (!asdl_seq_LEN(body)) { | 
 | 5835 |         return NULL; | 
 | 5836 |     } | 
 | 5837 |     stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); | 
 | 5838 |     if (st->kind != Expr_kind) { | 
 | 5839 |         return NULL; | 
 | 5840 |     } | 
 | 5841 |     expr_ty e = st->v.Expr.value; | 
| Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5842 |     if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { | 
 | 5843 |         return e->v.Constant.value; | 
 | 5844 |     } | 
 | 5845 |     return NULL; | 
 | 5846 | } |