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 |
Miss Islington (bot) | 83a9ba4 | 2020-06-06 10:04:47 -0700 | [diff] [blame] | 25 | validate_name(PyObject *name) |
| 26 | { |
| 27 | assert(PyUnicode_Check(name)); |
| 28 | static const char * const forbidden[] = { |
| 29 | "None", |
| 30 | "True", |
| 31 | "False", |
| 32 | NULL |
| 33 | }; |
| 34 | for (int i = 0; forbidden[i] != NULL; i++) { |
| 35 | if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) { |
| 36 | PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]); |
| 37 | return 0; |
| 38 | } |
| 39 | } |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 44 | validate_comprehension(asdl_seq *gens) |
| 45 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 46 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 47 | if (!asdl_seq_LEN(gens)) { |
| 48 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 49 | return 0; |
| 50 | } |
| 51 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 52 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 53 | if (!validate_expr(comp->target, Store) || |
| 54 | !validate_expr(comp->iter, Load) || |
| 55 | !validate_exprs(comp->ifs, Load, 0)) |
| 56 | return 0; |
| 57 | } |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static int |
| 62 | validate_slice(slice_ty slice) |
| 63 | { |
| 64 | switch (slice->kind) { |
| 65 | case Slice_kind: |
| 66 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 67 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 68 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 69 | case ExtSlice_kind: { |
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 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 72 | return 0; |
| 73 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 74 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 75 | return 0; |
| 76 | return 1; |
| 77 | } |
| 78 | case Index_kind: |
| 79 | return validate_expr(slice->v.Index.value, Load); |
| 80 | default: |
| 81 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 82 | return 0; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static int |
| 87 | validate_keywords(asdl_seq *keywords) |
| 88 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 89 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 90 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 91 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 92 | return 0; |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | validate_args(asdl_seq *args) |
| 98 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 99 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 100 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 101 | arg_ty arg = asdl_seq_GET(args, i); |
| 102 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 103 | return 0; |
| 104 | } |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | static const char * |
| 109 | expr_context_name(expr_context_ty ctx) |
| 110 | { |
| 111 | switch (ctx) { |
| 112 | case Load: |
| 113 | return "Load"; |
| 114 | case Store: |
| 115 | return "Store"; |
| 116 | case Del: |
| 117 | return "Del"; |
| 118 | case AugLoad: |
| 119 | return "AugLoad"; |
| 120 | case AugStore: |
| 121 | return "AugStore"; |
| 122 | case Param: |
| 123 | return "Param"; |
| 124 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 125 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | static int |
| 130 | validate_arguments(arguments_ty args) |
| 131 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 132 | if (!validate_args(args->posonlyargs) || !validate_args(args->args)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 133 | return 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 134 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 135 | if (args->vararg && args->vararg->annotation |
| 136 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | if (!validate_args(args->kwonlyargs)) |
| 140 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 141 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 142 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 143 | return 0; |
| 144 | } |
Pablo Galindo | 2f58a84 | 2019-05-31 14:09:49 +0100 | [diff] [blame] | 145 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 146 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 147 | return 0; |
| 148 | } |
| 149 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 150 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 151 | "kw_defaults on arguments"); |
| 152 | return 0; |
| 153 | } |
| 154 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 155 | } |
| 156 | |
| 157 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 158 | validate_constant(PyObject *value) |
| 159 | { |
| 160 | if (value == Py_None || value == Py_Ellipsis) |
| 161 | return 1; |
| 162 | |
| 163 | if (PyLong_CheckExact(value) |
| 164 | || PyFloat_CheckExact(value) |
| 165 | || PyComplex_CheckExact(value) |
| 166 | || PyBool_Check(value) |
| 167 | || PyUnicode_CheckExact(value) |
| 168 | || PyBytes_CheckExact(value)) |
| 169 | return 1; |
| 170 | |
| 171 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 172 | PyObject *it; |
| 173 | |
| 174 | it = PyObject_GetIter(value); |
| 175 | if (it == NULL) |
| 176 | return 0; |
| 177 | |
| 178 | while (1) { |
| 179 | PyObject *item = PyIter_Next(it); |
| 180 | if (item == NULL) { |
| 181 | if (PyErr_Occurred()) { |
| 182 | Py_DECREF(it); |
| 183 | return 0; |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | if (!validate_constant(item)) { |
| 189 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 190 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 191 | return 0; |
| 192 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 193 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | Py_DECREF(it); |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 204 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 205 | { |
| 206 | int check_ctx = 1; |
| 207 | expr_context_ty actual_ctx; |
| 208 | |
| 209 | /* First check expression context. */ |
| 210 | switch (exp->kind) { |
| 211 | case Attribute_kind: |
| 212 | actual_ctx = exp->v.Attribute.ctx; |
| 213 | break; |
| 214 | case Subscript_kind: |
| 215 | actual_ctx = exp->v.Subscript.ctx; |
| 216 | break; |
| 217 | case Starred_kind: |
| 218 | actual_ctx = exp->v.Starred.ctx; |
| 219 | break; |
| 220 | case Name_kind: |
Miss Islington (bot) | 83a9ba4 | 2020-06-06 10:04:47 -0700 | [diff] [blame] | 221 | if (!validate_name(exp->v.Name.id)) { |
| 222 | return 0; |
| 223 | } |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 224 | actual_ctx = exp->v.Name.ctx; |
| 225 | break; |
| 226 | case List_kind: |
| 227 | actual_ctx = exp->v.List.ctx; |
| 228 | break; |
| 229 | case Tuple_kind: |
| 230 | actual_ctx = exp->v.Tuple.ctx; |
| 231 | break; |
| 232 | default: |
| 233 | if (ctx != Load) { |
| 234 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 235 | "assigned to in %s context", expr_context_name(ctx)); |
| 236 | return 0; |
| 237 | } |
| 238 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 239 | /* set actual_ctx to prevent gcc warning */ |
| 240 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 241 | } |
| 242 | if (check_ctx && actual_ctx != ctx) { |
| 243 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 244 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* Now validate expression. */ |
| 249 | switch (exp->kind) { |
| 250 | case BoolOp_kind: |
| 251 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 252 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 253 | return 0; |
| 254 | } |
| 255 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 256 | case BinOp_kind: |
| 257 | return validate_expr(exp->v.BinOp.left, Load) && |
| 258 | validate_expr(exp->v.BinOp.right, Load); |
| 259 | case UnaryOp_kind: |
| 260 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 261 | case Lambda_kind: |
| 262 | return validate_arguments(exp->v.Lambda.args) && |
| 263 | validate_expr(exp->v.Lambda.body, Load); |
| 264 | case IfExp_kind: |
| 265 | return validate_expr(exp->v.IfExp.test, Load) && |
| 266 | validate_expr(exp->v.IfExp.body, Load) && |
| 267 | validate_expr(exp->v.IfExp.orelse, Load); |
| 268 | case Dict_kind: |
| 269 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 270 | PyErr_SetString(PyExc_ValueError, |
| 271 | "Dict doesn't have the same number of keys as values"); |
| 272 | return 0; |
| 273 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 274 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 275 | dict literals, i.e. ``{**{a:b}}`` */ |
| 276 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 277 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 278 | case Set_kind: |
| 279 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 280 | #define COMP(NAME) \ |
| 281 | case NAME ## _kind: \ |
| 282 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 283 | validate_expr(exp->v.NAME.elt, Load); |
| 284 | COMP(ListComp) |
| 285 | COMP(SetComp) |
| 286 | COMP(GeneratorExp) |
| 287 | #undef COMP |
| 288 | case DictComp_kind: |
| 289 | return validate_comprehension(exp->v.DictComp.generators) && |
| 290 | validate_expr(exp->v.DictComp.key, Load) && |
| 291 | validate_expr(exp->v.DictComp.value, Load); |
| 292 | case Yield_kind: |
| 293 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 294 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 295 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 296 | case Await_kind: |
| 297 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 298 | case Compare_kind: |
| 299 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 300 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 301 | return 0; |
| 302 | } |
| 303 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 304 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 305 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 306 | "of comparators and operands"); |
| 307 | return 0; |
| 308 | } |
| 309 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 310 | validate_expr(exp->v.Compare.left, Load); |
| 311 | case Call_kind: |
| 312 | return validate_expr(exp->v.Call.func, Load) && |
| 313 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 314 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 315 | case Constant_kind: |
| 316 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 317 | PyErr_Format(PyExc_TypeError, |
| 318 | "got an invalid type in Constant: %s", |
| 319 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 323 | case JoinedStr_kind: |
| 324 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 325 | case FormattedValue_kind: |
| 326 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 327 | return 0; |
| 328 | if (exp->v.FormattedValue.format_spec) |
| 329 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 330 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 331 | case Attribute_kind: |
| 332 | return validate_expr(exp->v.Attribute.value, Load); |
| 333 | case Subscript_kind: |
| 334 | return validate_slice(exp->v.Subscript.slice) && |
| 335 | validate_expr(exp->v.Subscript.value, Load); |
| 336 | case Starred_kind: |
| 337 | return validate_expr(exp->v.Starred.value, ctx); |
| 338 | case List_kind: |
| 339 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 340 | case Tuple_kind: |
| 341 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 342 | case NamedExpr_kind: |
| 343 | return validate_expr(exp->v.NamedExpr.value, Load); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 344 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 345 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 346 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 347 | } |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 348 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 349 | return 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int |
| 353 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 354 | { |
| 355 | if (asdl_seq_LEN(seq)) |
| 356 | return 1; |
| 357 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int |
| 362 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 363 | { |
| 364 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 365 | validate_exprs(targets, ctx, 0); |
| 366 | } |
| 367 | |
| 368 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 369 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 370 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 371 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static int |
| 375 | validate_stmt(stmt_ty stmt) |
| 376 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 377 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 378 | switch (stmt->kind) { |
| 379 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 380 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 381 | validate_arguments(stmt->v.FunctionDef.args) && |
| 382 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 383 | (!stmt->v.FunctionDef.returns || |
| 384 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 385 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 386 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 387 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 388 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 389 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 390 | case Return_kind: |
| 391 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 392 | case Delete_kind: |
| 393 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 394 | case Assign_kind: |
| 395 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 396 | validate_expr(stmt->v.Assign.value, Load); |
| 397 | case AugAssign_kind: |
| 398 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 399 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 400 | case AnnAssign_kind: |
| 401 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 402 | stmt->v.AnnAssign.simple) { |
| 403 | PyErr_SetString(PyExc_TypeError, |
| 404 | "AnnAssign with simple non-Name target"); |
| 405 | return 0; |
| 406 | } |
| 407 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 408 | (!stmt->v.AnnAssign.value || |
| 409 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 410 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 411 | case For_kind: |
| 412 | return validate_expr(stmt->v.For.target, Store) && |
| 413 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 414 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 415 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 416 | case AsyncFor_kind: |
| 417 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 418 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 419 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 420 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 421 | case While_kind: |
| 422 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 423 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 424 | validate_stmts(stmt->v.While.orelse); |
| 425 | case If_kind: |
| 426 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 427 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 428 | validate_stmts(stmt->v.If.orelse); |
| 429 | case With_kind: |
| 430 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 431 | return 0; |
| 432 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 433 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 434 | if (!validate_expr(item->context_expr, Load) || |
| 435 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 436 | return 0; |
| 437 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 438 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 439 | case AsyncWith_kind: |
| 440 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 441 | return 0; |
| 442 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 443 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 444 | if (!validate_expr(item->context_expr, Load) || |
| 445 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 446 | return 0; |
| 447 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 448 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 449 | case Raise_kind: |
| 450 | if (stmt->v.Raise.exc) { |
| 451 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 452 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 453 | } |
| 454 | if (stmt->v.Raise.cause) { |
| 455 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 456 | return 0; |
| 457 | } |
| 458 | return 1; |
| 459 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 460 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 461 | return 0; |
| 462 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 463 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 464 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 465 | return 0; |
| 466 | } |
| 467 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 468 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 469 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 470 | return 0; |
| 471 | } |
| 472 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 473 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 474 | if ((handler->v.ExceptHandler.type && |
| 475 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 476 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 480 | validate_stmts(stmt->v.Try.finalbody)) && |
| 481 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 482 | validate_stmts(stmt->v.Try.orelse)); |
| 483 | case Assert_kind: |
| 484 | return validate_expr(stmt->v.Assert.test, Load) && |
| 485 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 486 | case Import_kind: |
| 487 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 488 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 489 | if (stmt->v.ImportFrom.level < 0) { |
| 490 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 494 | case Global_kind: |
| 495 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 496 | case Nonlocal_kind: |
| 497 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 498 | case Expr_kind: |
| 499 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 500 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 501 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 502 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 503 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 504 | (!stmt->v.AsyncFunctionDef.returns || |
| 505 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 506 | case Pass_kind: |
| 507 | case Break_kind: |
| 508 | case Continue_kind: |
| 509 | return 1; |
| 510 | default: |
| 511 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 512 | return 0; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | static int |
| 517 | validate_stmts(asdl_seq *seq) |
| 518 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 519 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 520 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 521 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 522 | if (stmt) { |
| 523 | if (!validate_stmt(stmt)) |
| 524 | return 0; |
| 525 | } |
| 526 | else { |
| 527 | PyErr_SetString(PyExc_ValueError, |
| 528 | "None disallowed in statement list"); |
| 529 | return 0; |
| 530 | } |
| 531 | } |
| 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | static int |
| 536 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 537 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 538 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 539 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 540 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 541 | if (expr) { |
| 542 | if (!validate_expr(expr, ctx)) |
| 543 | return 0; |
| 544 | } |
| 545 | else if (!null_ok) { |
| 546 | PyErr_SetString(PyExc_ValueError, |
| 547 | "None disallowed in expression list"); |
| 548 | return 0; |
| 549 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 550 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 551 | } |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | int |
| 556 | PyAST_Validate(mod_ty mod) |
| 557 | { |
| 558 | int res = 0; |
| 559 | |
| 560 | switch (mod->kind) { |
| 561 | case Module_kind: |
| 562 | res = validate_stmts(mod->v.Module.body); |
| 563 | break; |
| 564 | case Interactive_kind: |
| 565 | res = validate_stmts(mod->v.Interactive.body); |
| 566 | break; |
| 567 | case Expression_kind: |
| 568 | res = validate_expr(mod->v.Expression.body, Load); |
| 569 | break; |
| 570 | case Suite_kind: |
| 571 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 572 | break; |
| 573 | default: |
| 574 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 575 | res = 0; |
| 576 | break; |
| 577 | } |
| 578 | return res; |
| 579 | } |
| 580 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 581 | /* 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] | 582 | #include "grammar.h" |
| 583 | #include "parsetok.h" |
| 584 | #include "graminit.h" |
| 585 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 586 | /* Data structure used internally */ |
| 587 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 588 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 589 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 590 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 591 | int c_feature_version; /* Latest minor version of Python for allowed features */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 595 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 596 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 597 | 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] | 598 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 599 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 600 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 601 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 602 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 603 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 604 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 605 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 606 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 607 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 608 | const node *, const node *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 609 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 610 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 611 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 612 | static void get_last_end_pos(asdl_seq *, int *, int *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 613 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 614 | #define COMP_GENEXP 0 |
| 615 | #define COMP_LISTCOMP 1 |
| 616 | #define COMP_SETCOMP 2 |
| 617 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 618 | static int |
| 619 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 620 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 621 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 622 | if (!m) |
| 623 | return 0; |
| 624 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 625 | Py_DECREF(m); |
| 626 | if (!c->c_normalize) |
| 627 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 632 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 633 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 634 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 635 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 636 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 637 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 638 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 639 | /* Check whether there are non-ASCII characters in the |
| 640 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 641 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 642 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 643 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 644 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 645 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 646 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 647 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 648 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 649 | if (form == NULL) { |
| 650 | Py_DECREF(id); |
| 651 | return NULL; |
| 652 | } |
| 653 | PyObject *args[2] = {form, id}; |
| 654 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 655 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 656 | if (!id2) |
| 657 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 658 | if (!PyUnicode_Check(id2)) { |
| 659 | PyErr_Format(PyExc_TypeError, |
| 660 | "unicodedata.normalize() must return a string, not " |
| 661 | "%.200s", |
| 662 | Py_TYPE(id2)->tp_name); |
| 663 | Py_DECREF(id2); |
| 664 | return NULL; |
| 665 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 666 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 667 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 668 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 669 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 670 | Py_DECREF(id); |
| 671 | return NULL; |
| 672 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 673 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 676 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 677 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 678 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 679 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 680 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 681 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 682 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 683 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 684 | va_start(va, errmsg); |
| 685 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 686 | va_end(va); |
| 687 | if (!errstr) { |
| 688 | return 0; |
| 689 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 690 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 691 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 692 | Py_INCREF(Py_None); |
| 693 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 694 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 695 | 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] | 696 | if (!tmp) { |
| 697 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 698 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 699 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 700 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 701 | Py_DECREF(errstr); |
| 702 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 703 | if (value) { |
| 704 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 705 | Py_DECREF(value); |
| 706 | } |
| 707 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* num_stmts() returns number of contained statements. |
| 711 | |
| 712 | Use this routine to determine how big a sequence is needed for |
| 713 | the statements in a parse tree. Its raison d'etre is this bit of |
| 714 | grammar: |
| 715 | |
| 716 | stmt: simple_stmt | compound_stmt |
| 717 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 718 | |
| 719 | A simple_stmt can contain multiple small_stmt elements joined |
| 720 | by semicolons. If the arg is a simple_stmt, the number of |
| 721 | small_stmt elements is returned. |
| 722 | */ |
| 723 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 724 | static string |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 725 | new_type_comment(const char *s, struct compiling *c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 726 | { |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 727 | PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); |
Guido van Rossum | 4b250fc | 2019-02-11 08:10:42 -0800 | [diff] [blame] | 728 | if (res == NULL) |
| 729 | return NULL; |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 730 | if (PyArena_AddPyObject(c->c_arena, res) < 0) { |
| 731 | Py_DECREF(res); |
| 732 | return NULL; |
| 733 | } |
| 734 | return res; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 735 | } |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 736 | #define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 737 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 738 | static int |
| 739 | num_stmts(const node *n) |
| 740 | { |
| 741 | int i, l; |
| 742 | node *ch; |
| 743 | |
| 744 | switch (TYPE(n)) { |
| 745 | case single_input: |
| 746 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 747 | return 0; |
| 748 | else |
| 749 | return num_stmts(CHILD(n, 0)); |
| 750 | case file_input: |
| 751 | l = 0; |
| 752 | for (i = 0; i < NCH(n); i++) { |
| 753 | ch = CHILD(n, i); |
| 754 | if (TYPE(ch) == stmt) |
| 755 | l += num_stmts(ch); |
| 756 | } |
| 757 | return l; |
| 758 | case stmt: |
| 759 | return num_stmts(CHILD(n, 0)); |
| 760 | case compound_stmt: |
| 761 | return 1; |
| 762 | case simple_stmt: |
| 763 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 764 | case suite: |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 765 | case func_body_suite: |
| 766 | /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
| 767 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | if (NCH(n) == 1) |
| 769 | return num_stmts(CHILD(n, 0)); |
| 770 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 771 | i = 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 772 | l = 0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 773 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) |
| 774 | i += 2; |
| 775 | for (; i < (NCH(n) - 1); i++) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 | l += num_stmts(CHILD(n, i)); |
| 777 | return l; |
| 778 | } |
| 779 | default: { |
| 780 | char buf[128]; |
| 781 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 782 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | TYPE(n), NCH(n)); |
| 784 | Py_FatalError(buf); |
| 785 | } |
| 786 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 787 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* Transform the CST rooted at node * to the appropriate AST |
| 791 | */ |
| 792 | |
| 793 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 794 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 795 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 797 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 | asdl_seq *stmts = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 799 | asdl_seq *type_ignores = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 800 | stmt_ty s; |
| 801 | node *ch; |
| 802 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 803 | mod_ty res = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 804 | asdl_seq *argtypes = NULL; |
| 805 | expr_ty ret, arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 806 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 807 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 808 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 809 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 810 | c.c_normalize = NULL; |
Guido van Rossum | 77f0ed7 | 2019-05-28 16:44:58 -0700 | [diff] [blame] | 811 | c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 812 | |
| 813 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 814 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 815 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 816 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 817 | switch (TYPE(n)) { |
| 818 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 819 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 820 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 821 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 | for (i = 0; i < NCH(n) - 1; i++) { |
| 823 | ch = CHILD(n, i); |
| 824 | if (TYPE(ch) == NEWLINE) |
| 825 | continue; |
| 826 | REQ(ch, stmt); |
| 827 | num = num_stmts(ch); |
| 828 | if (num == 1) { |
| 829 | s = ast_for_stmt(&c, ch); |
| 830 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 831 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 832 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 833 | } |
| 834 | else { |
| 835 | ch = CHILD(ch, 0); |
| 836 | REQ(ch, simple_stmt); |
| 837 | for (j = 0; j < num; j++) { |
| 838 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 839 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 840 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 841 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 845 | |
| 846 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 847 | ch = CHILD(n, NCH(n) - 1); |
| 848 | REQ(ch, ENDMARKER); |
| 849 | num = NCH(ch); |
| 850 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 851 | if (!type_ignores) |
| 852 | goto out; |
| 853 | |
| 854 | for (i = 0; i < num; i++) { |
Michael J. Sullivan | 933e150 | 2019-05-22 07:54:20 -0700 | [diff] [blame] | 855 | string type_comment = new_type_comment(STR(CHILD(ch, i)), &c); |
| 856 | if (!type_comment) |
| 857 | goto out; |
| 858 | type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 859 | if (!ti) |
| 860 | goto out; |
| 861 | asdl_seq_SET(type_ignores, i, ti); |
| 862 | } |
| 863 | |
| 864 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 865 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | case eval_input: { |
| 867 | expr_ty testlist_ast; |
| 868 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 869 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 870 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 871 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 872 | goto out; |
| 873 | res = Expression(testlist_ast, arena); |
| 874 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 875 | } |
| 876 | case single_input: |
| 877 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 878 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 879 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 880 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 881 | 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] | 882 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 883 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 884 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 885 | goto out; |
| 886 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 887 | } |
| 888 | else { |
| 889 | n = CHILD(n, 0); |
| 890 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 891 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 892 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 893 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 894 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 895 | s = ast_for_stmt(&c, n); |
| 896 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 897 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 898 | asdl_seq_SET(stmts, 0, s); |
| 899 | } |
| 900 | else { |
| 901 | /* Only a simple_stmt can contain multiple statements. */ |
| 902 | REQ(n, simple_stmt); |
| 903 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 904 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 905 | break; |
| 906 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 907 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 908 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 909 | asdl_seq_SET(stmts, i / 2, s); |
| 910 | } |
| 911 | } |
| 912 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 913 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 914 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 915 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 916 | case func_type_input: |
| 917 | n = CHILD(n, 0); |
| 918 | REQ(n, func_type); |
| 919 | |
| 920 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 921 | ch = CHILD(n, 1); |
| 922 | /* this is overly permissive -- we don't pay any attention to |
| 923 | * stars on the args -- just parse them into an ordered list */ |
| 924 | num = 0; |
| 925 | for (i = 0; i < NCH(ch); i++) { |
| 926 | if (TYPE(CHILD(ch, i)) == test) { |
| 927 | num++; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | argtypes = _Py_asdl_seq_new(num, arena); |
| 932 | if (!argtypes) |
| 933 | goto out; |
| 934 | |
| 935 | j = 0; |
| 936 | for (i = 0; i < NCH(ch); i++) { |
| 937 | if (TYPE(CHILD(ch, i)) == test) { |
| 938 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 939 | if (!arg) |
| 940 | goto out; |
| 941 | asdl_seq_SET(argtypes, j++, arg); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | else { |
| 946 | argtypes = _Py_asdl_seq_new(0, arena); |
| 947 | if (!argtypes) |
| 948 | goto out; |
| 949 | } |
| 950 | |
| 951 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 952 | if (!ret) |
| 953 | goto out; |
| 954 | res = FunctionType(argtypes, ret, arena); |
| 955 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 956 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 957 | PyErr_Format(PyExc_SystemError, |
| 958 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 959 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 960 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 961 | out: |
| 962 | if (c.c_normalize) { |
| 963 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 964 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 965 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 968 | mod_ty |
| 969 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 970 | PyArena *arena) |
| 971 | { |
| 972 | mod_ty mod; |
| 973 | PyObject *filename; |
| 974 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 975 | if (filename == NULL) |
| 976 | return NULL; |
| 977 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 978 | Py_DECREF(filename); |
| 979 | return mod; |
| 980 | |
| 981 | } |
| 982 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 983 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 984 | */ |
| 985 | |
| 986 | static operator_ty |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 987 | get_operator(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 988 | { |
| 989 | switch (TYPE(n)) { |
| 990 | case VBAR: |
| 991 | return BitOr; |
| 992 | case CIRCUMFLEX: |
| 993 | return BitXor; |
| 994 | case AMPER: |
| 995 | return BitAnd; |
| 996 | case LEFTSHIFT: |
| 997 | return LShift; |
| 998 | case RIGHTSHIFT: |
| 999 | return RShift; |
| 1000 | case PLUS: |
| 1001 | return Add; |
| 1002 | case MINUS: |
| 1003 | return Sub; |
| 1004 | case STAR: |
| 1005 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1006 | case AT: |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1007 | if (c->c_feature_version < 5) { |
| 1008 | ast_error(c, n, |
| 1009 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 1010 | return (operator_ty)0; |
| 1011 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1012 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1013 | case SLASH: |
| 1014 | return Div; |
| 1015 | case DOUBLESLASH: |
| 1016 | return FloorDiv; |
| 1017 | case PERCENT: |
| 1018 | return Mod; |
| 1019 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1020 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1024 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1025 | "None", |
| 1026 | "True", |
| 1027 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1028 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1029 | NULL, |
| 1030 | }; |
| 1031 | |
| 1032 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1033 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1034 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1035 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1036 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1037 | const char * const *p = FORBIDDEN; |
| 1038 | if (!full_checks) { |
| 1039 | /* In most cases, the parser will protect True, False, and None |
| 1040 | from being assign to. */ |
| 1041 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1042 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1043 | for (; *p; p++) { |
| 1044 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1045 | ast_error(c, n, "cannot assign to %U", name); |
| 1046 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1052 | static expr_ty |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 1053 | copy_location(expr_ty e, const node *n, const node *end) |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1054 | { |
| 1055 | if (e) { |
| 1056 | e->lineno = LINENO(n); |
| 1057 | e->col_offset = n->n_col_offset; |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 1058 | e->end_lineno = end->n_end_lineno; |
| 1059 | e->end_col_offset = end->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1060 | } |
| 1061 | return e; |
| 1062 | } |
| 1063 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1064 | static const char * |
| 1065 | get_expr_name(expr_ty e) |
| 1066 | { |
| 1067 | switch (e->kind) { |
| 1068 | case Attribute_kind: |
| 1069 | return "attribute"; |
| 1070 | case Subscript_kind: |
| 1071 | return "subscript"; |
| 1072 | case Starred_kind: |
| 1073 | return "starred"; |
| 1074 | case Name_kind: |
| 1075 | return "name"; |
| 1076 | case List_kind: |
| 1077 | return "list"; |
| 1078 | case Tuple_kind: |
| 1079 | return "tuple"; |
| 1080 | case Lambda_kind: |
| 1081 | return "lambda"; |
| 1082 | case Call_kind: |
| 1083 | return "function call"; |
| 1084 | case BoolOp_kind: |
| 1085 | case BinOp_kind: |
| 1086 | case UnaryOp_kind: |
| 1087 | return "operator"; |
| 1088 | case GeneratorExp_kind: |
| 1089 | return "generator expression"; |
| 1090 | case Yield_kind: |
| 1091 | case YieldFrom_kind: |
| 1092 | return "yield expression"; |
| 1093 | case Await_kind: |
| 1094 | return "await expression"; |
| 1095 | case ListComp_kind: |
| 1096 | return "list comprehension"; |
| 1097 | case SetComp_kind: |
| 1098 | return "set comprehension"; |
| 1099 | case DictComp_kind: |
| 1100 | return "dict comprehension"; |
| 1101 | case Dict_kind: |
| 1102 | return "dict display"; |
| 1103 | case Set_kind: |
| 1104 | return "set display"; |
| 1105 | case JoinedStr_kind: |
| 1106 | case FormattedValue_kind: |
| 1107 | return "f-string expression"; |
| 1108 | case Constant_kind: { |
| 1109 | PyObject *value = e->v.Constant.value; |
| 1110 | if (value == Py_None) { |
| 1111 | return "None"; |
| 1112 | } |
| 1113 | if (value == Py_False) { |
| 1114 | return "False"; |
| 1115 | } |
| 1116 | if (value == Py_True) { |
| 1117 | return "True"; |
| 1118 | } |
| 1119 | if (value == Py_Ellipsis) { |
| 1120 | return "Ellipsis"; |
| 1121 | } |
| 1122 | return "literal"; |
| 1123 | } |
| 1124 | case Compare_kind: |
| 1125 | return "comparison"; |
| 1126 | case IfExp_kind: |
| 1127 | return "conditional expression"; |
| 1128 | case NamedExpr_kind: |
| 1129 | return "named expression"; |
| 1130 | default: |
| 1131 | PyErr_Format(PyExc_SystemError, |
| 1132 | "unexpected expression in assignment %d (line %d)", |
| 1133 | e->kind, e->lineno); |
| 1134 | return NULL; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1138 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1139 | |
| 1140 | Only sets context for expr kinds that "can appear in assignment context" |
| 1141 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1142 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | */ |
| 1144 | |
| 1145 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1146 | 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] | 1147 | { |
| 1148 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* The ast defines augmented store and load contexts, but the |
| 1151 | implementation here doesn't actually use them. The code may be |
| 1152 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1153 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1154 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1155 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1156 | */ |
| 1157 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1158 | |
| 1159 | switch (e->kind) { |
| 1160 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1161 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1162 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1163 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1164 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1165 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1166 | e->v.Subscript.ctx = ctx; |
| 1167 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1168 | case Starred_kind: |
| 1169 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1170 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1171 | return 0; |
| 1172 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 | case Name_kind: |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1174 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1175 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1176 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1177 | } |
| 1178 | e->v.Name.ctx = ctx; |
| 1179 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1180 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1181 | e->v.List.ctx = ctx; |
| 1182 | s = e->v.List.elts; |
| 1183 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1185 | e->v.Tuple.ctx = ctx; |
| 1186 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1187 | break; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1188 | default: { |
| 1189 | const char *expr_name = get_expr_name(e); |
| 1190 | if (expr_name != NULL) { |
| 1191 | ast_error(c, n, "cannot %s %s", |
| 1192 | ctx == Store ? "assign to" : "delete", |
| 1193 | expr_name); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1194 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1195 | return 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1196 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1199 | /* 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] | 1200 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1201 | */ |
| 1202 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1203 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1204 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1205 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1206 | 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] | 1207 | return 0; |
| 1208 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1209 | } |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
| 1213 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1214 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | { |
| 1216 | REQ(n, augassign); |
| 1217 | n = CHILD(n, 0); |
| 1218 | switch (STR(n)[0]) { |
| 1219 | case '+': |
| 1220 | return Add; |
| 1221 | case '-': |
| 1222 | return Sub; |
| 1223 | case '/': |
| 1224 | if (STR(n)[1] == '/') |
| 1225 | return FloorDiv; |
| 1226 | else |
| 1227 | return Div; |
| 1228 | case '%': |
| 1229 | return Mod; |
| 1230 | case '<': |
| 1231 | return LShift; |
| 1232 | case '>': |
| 1233 | return RShift; |
| 1234 | case '&': |
| 1235 | return BitAnd; |
| 1236 | case '^': |
| 1237 | return BitXor; |
| 1238 | case '|': |
| 1239 | return BitOr; |
| 1240 | case '*': |
| 1241 | if (STR(n)[1] == '*') |
| 1242 | return Pow; |
| 1243 | else |
| 1244 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1245 | case '@': |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1246 | if (c->c_feature_version < 5) { |
| 1247 | ast_error(c, n, |
| 1248 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 1249 | return (operator_ty)0; |
| 1250 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1251 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1252 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1253 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1254 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1259 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1260 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1261 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1262 | |'is' 'not' |
| 1263 | */ |
| 1264 | REQ(n, comp_op); |
| 1265 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1266 | n = CHILD(n, 0); |
| 1267 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1268 | case LESS: |
| 1269 | return Lt; |
| 1270 | case GREATER: |
| 1271 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1272 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1273 | return Eq; |
| 1274 | case LESSEQUAL: |
| 1275 | return LtE; |
| 1276 | case GREATEREQUAL: |
| 1277 | return GtE; |
| 1278 | case NOTEQUAL: |
| 1279 | return NotEq; |
| 1280 | case NAME: |
| 1281 | if (strcmp(STR(n), "in") == 0) |
| 1282 | return In; |
| 1283 | if (strcmp(STR(n), "is") == 0) |
| 1284 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1285 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1286 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1287 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1288 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1289 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1290 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1291 | } |
| 1292 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1293 | /* handle "not in" and "is not" */ |
| 1294 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1295 | case NAME: |
| 1296 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1297 | return NotIn; |
| 1298 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1299 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1300 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1301 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1302 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1303 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1304 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1305 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1306 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1307 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1308 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1309 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static asdl_seq * |
| 1313 | seq_for_testlist(struct compiling *c, const node *n) |
| 1314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1316 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1317 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1318 | asdl_seq *seq; |
| 1319 | expr_ty expression; |
| 1320 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1321 | 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] | 1322 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1323 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1324 | if (!seq) |
| 1325 | return NULL; |
| 1326 | |
| 1327 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1329 | 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] | 1330 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1331 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1332 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1333 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1334 | |
| 1335 | assert(i / 2 < seq->size); |
| 1336 | asdl_seq_SET(seq, i / 2, expression); |
| 1337 | } |
| 1338 | return seq; |
| 1339 | } |
| 1340 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1341 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1342 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1343 | { |
| 1344 | identifier name; |
| 1345 | expr_ty annotation = NULL; |
| 1346 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1347 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1348 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1349 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1350 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1351 | name = NEW_IDENTIFIER(ch); |
| 1352 | if (!name) |
| 1353 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1354 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1355 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1358 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1359 | if (!annotation) |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1363 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1364 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1365 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1366 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1367 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1370 | /* returns -1 if failed to handle keyword only arguments |
| 1371 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1372 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1373 | ^^^ |
| 1374 | start pointing here |
| 1375 | */ |
| 1376 | static int |
| 1377 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1378 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1379 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1380 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1381 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1382 | expr_ty expression, annotation; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1383 | arg_ty arg = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1384 | int i = start; |
| 1385 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1386 | |
| 1387 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1388 | 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] | 1389 | return -1; |
| 1390 | } |
| 1391 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1392 | while (i < NCH(n)) { |
| 1393 | ch = CHILD(n, i); |
| 1394 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1395 | case vfpdef: |
| 1396 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1397 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1398 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1399 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1400 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1401 | asdl_seq_SET(kwdefaults, j, expression); |
| 1402 | i += 2; /* '=' and test */ |
| 1403 | } |
| 1404 | else { /* setting NULL if no default value exists */ |
| 1405 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1406 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1407 | if (NCH(ch) == 3) { |
| 1408 | /* ch is NAME ':' test */ |
| 1409 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1410 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1411 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1412 | } |
| 1413 | else { |
| 1414 | annotation = NULL; |
| 1415 | } |
| 1416 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1417 | argname = NEW_IDENTIFIER(ch); |
| 1418 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1420 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1421 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1422 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1423 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1424 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1425 | if (!arg) |
| 1426 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1427 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1428 | i += 1; /* the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1429 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1430 | i += 1; /* the comma, if present */ |
| 1431 | break; |
| 1432 | case TYPE_COMMENT: |
| 1433 | /* arg will be equal to the last argument processed */ |
| 1434 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1435 | if (!arg->type_comment) |
| 1436 | goto error; |
| 1437 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1438 | break; |
| 1439 | case DOUBLESTAR: |
| 1440 | return i; |
| 1441 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1442 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1443 | goto error; |
| 1444 | } |
| 1445 | } |
| 1446 | return i; |
| 1447 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1449 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1450 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1451 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1452 | |
| 1453 | static arguments_ty |
| 1454 | ast_for_arguments(struct compiling *c, const node *n) |
| 1455 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1456 | /* This function handles both typedargslist (function definition) |
| 1457 | and varargslist (lambda definition). |
| 1458 | |
| 1459 | parameters: '(' [typedargslist] ')' |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1460 | |
| 1461 | The following definition for typedarglist is equivalent to this set of rules: |
| 1462 | |
| 1463 | arguments = argument (',' [TYPE_COMMENT] argument)* |
| 1464 | argument = tfpdef ['=' test] |
| 1465 | kwargs = '**' tfpdef [','] [TYPE_COMMENT] |
| 1466 | args = '*' [tfpdef] |
| 1467 | kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' |
| 1468 | [TYPE_COMMENT] [kwargs]]) |
| 1469 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1470 | poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' |
| 1471 | [TYPE_COMMENT] [args_kwonly_kwargs]]) |
| 1472 | typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1473 | typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] |
| 1474 | typedargslist_no_posonly]])|(typedargslist_no_posonly)" |
| 1475 | |
| 1476 | typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1477 | ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ',' |
| 1478 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1479 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1480 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1481 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1482 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1483 | '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (',' |
| 1484 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1485 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1486 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1487 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1488 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1489 | '**' tfpdef [','] [TYPE_COMMENT])) |
| 1490 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1491 | tfpdef: NAME [':' test] |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1492 | |
| 1493 | The following definition for varargslist is equivalent to this set of rules: |
| 1494 | |
| 1495 | arguments = argument (',' argument )* |
| 1496 | argument = vfpdef ['=' test] |
| 1497 | kwargs = '**' vfpdef [','] |
| 1498 | args = '*' [vfpdef] |
| 1499 | kwonly_kwargs = (',' argument )* [',' [kwargs]] |
| 1500 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1501 | poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 1502 | vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1503 | varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | |
| 1504 | (vararglist_no_posonly) |
| 1505 | |
| 1506 | varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' |
| 1507 | test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' |
| 1508 | ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* |
| 1509 | [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef |
| 1510 | ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1511 | | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef |
| 1512 | [',']]] | '**' vfpdef [',']) |
| 1513 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1514 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1515 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1517 | int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1518 | int nposdefaults = 0, found_default = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1519 | asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1520 | arg_ty vararg = NULL, kwarg = NULL; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1521 | arg_ty arg = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1522 | node *ch; |
| 1523 | |
| 1524 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1525 | if (NCH(n) == 2) /* () as argument list */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1526 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1527 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1528 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1529 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1530 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1531 | /* First count the number of positional args & defaults. The |
| 1532 | variable i is the loop index for this for loop and the next. |
| 1533 | The next loop picks up where the first leaves off. |
| 1534 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1535 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1536 | ch = CHILD(n, i); |
| 1537 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1538 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1539 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1540 | if (i < NCH(n) && /* skip argument following star */ |
| 1541 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1542 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1543 | i++; |
| 1544 | } |
| 1545 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1546 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1547 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1548 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1549 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1550 | if (TYPE(ch) == SLASH ) { |
| 1551 | nposonlyargs = nposargs; |
| 1552 | nposargs = 0; |
| 1553 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1554 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1556 | defaults for keyword only args */ |
| 1557 | for ( ; i < NCH(n); ++i) { |
| 1558 | ch = CHILD(n, i); |
| 1559 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1560 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1561 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1562 | posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL); |
| 1563 | if (!posonlyargs && nposonlyargs) { |
| 1564 | return NULL; |
| 1565 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1566 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1567 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1568 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1569 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1570 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1571 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1572 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1574 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1575 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1576 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1578 | since we set NULL as default for keyword only argument w/o default |
| 1579 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1580 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1581 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1582 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1583 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1584 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1585 | /* tfpdef: NAME [':' test] |
| 1586 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1587 | */ |
| 1588 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1589 | j = 0; /* index for defaults */ |
| 1590 | k = 0; /* index for args */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1591 | l = 0; /* index for posonlyargs */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1592 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1593 | ch = CHILD(n, i); |
| 1594 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1595 | case tfpdef: |
| 1596 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1597 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1598 | anything other than EQUAL or a comma? */ |
| 1599 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1600 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1601 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1602 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1603 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1604 | assert(posdefaults != NULL); |
| 1605 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1606 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1607 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1608 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1609 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1610 | ast_error(c, n, |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1611 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1612 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1613 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1614 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1615 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1616 | return NULL; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1617 | if (l < nposonlyargs) { |
| 1618 | asdl_seq_SET(posonlyargs, l++, arg); |
| 1619 | } else { |
| 1620 | asdl_seq_SET(posargs, k++, arg); |
| 1621 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1622 | i += 1; /* the name */ |
| 1623 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1624 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1625 | break; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1626 | case SLASH: |
| 1627 | /* Advance the slash and the comma. If there are more names |
| 1628 | * after the slash there will be a comma so we are advancing |
| 1629 | * the correct number of nodes. If the slash is the last item, |
| 1630 | * we will be advancing an extra token but then * i > NCH(n) |
| 1631 | * and the enclosing while will finish correctly. */ |
| 1632 | i += 2; |
| 1633 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1634 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1635 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1636 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1637 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1638 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1639 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1640 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1641 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1642 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1643 | if (TYPE(ch) == COMMA) { |
| 1644 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1645 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1646 | |
| 1647 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1648 | ast_error(c, CHILD(n, i), |
| 1649 | "bare * has associated type comment"); |
| 1650 | return NULL; |
| 1651 | } |
| 1652 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1653 | res = handle_keywordonly_args(c, n, i, |
| 1654 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1655 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1656 | i = res; /* res has new position to process */ |
| 1657 | } |
| 1658 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1659 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1660 | if (!vararg) |
| 1661 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1662 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1663 | i += 2; /* the star and the name */ |
| 1664 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1665 | i += 1; /* the comma, if present */ |
| 1666 | |
| 1667 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1668 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1669 | if (!vararg->type_comment) |
| 1670 | return NULL; |
| 1671 | i += 1; |
| 1672 | } |
| 1673 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1674 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1675 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1676 | int res = 0; |
| 1677 | res = handle_keywordonly_args(c, n, i, |
| 1678 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1679 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1680 | i = res; /* res has new position to process */ |
| 1681 | } |
| 1682 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1685 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1686 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1687 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1688 | if (!kwarg) |
| 1689 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1690 | i += 2; /* the double star and the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1691 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1692 | i += 1; /* the comma, if present */ |
| 1693 | break; |
| 1694 | case TYPE_COMMENT: |
| 1695 | assert(i); |
| 1696 | |
| 1697 | if (kwarg) |
| 1698 | arg = kwarg; |
| 1699 | |
| 1700 | /* arg will be equal to the last argument processed */ |
| 1701 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1702 | if (!arg->type_comment) |
| 1703 | return NULL; |
| 1704 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1705 | break; |
| 1706 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1707 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | "unexpected node in varargslist: %d @ %d", |
| 1709 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1710 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1711 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1712 | } |
Miss Islington (bot) | cf9a63c | 2019-07-14 16:49:52 -0700 | [diff] [blame] | 1713 | return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static expr_ty |
| 1717 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1718 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1719 | expr_ty e; |
| 1720 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1721 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1722 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1723 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1724 | |
| 1725 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1726 | |
| 1727 | lineno = LINENO(n); |
| 1728 | col_offset = n->n_col_offset; |
| 1729 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1730 | ch = CHILD(n, 0); |
| 1731 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1733 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1734 | e = Name(id, Load, lineno, col_offset, |
| 1735 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1736 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1737 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1738 | |
| 1739 | for (i = 2; i < NCH(n); i+=2) { |
Lysandros Nikolaou | 8b9cebc | 2020-02-08 01:21:38 +0100 | [diff] [blame] | 1740 | const node *child = CHILD(n, i); |
| 1741 | id = NEW_IDENTIFIER(child); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1742 | if (!id) |
| 1743 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1744 | e = Attribute(e, id, Load, lineno, col_offset, |
Lysandros Nikolaou | 8b9cebc | 2020-02-08 01:21:38 +0100 | [diff] [blame] | 1745 | child->n_end_lineno, child->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1746 | if (!e) |
| 1747 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | static expr_ty |
| 1754 | ast_for_decorator(struct compiling *c, const node *n) |
| 1755 | { |
| 1756 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1757 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1758 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1760 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1761 | REQ(CHILD(n, 0), AT); |
| 1762 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1764 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1765 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1766 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1768 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1769 | d = name_expr; |
| 1770 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1771 | } |
| 1772 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Miss Skeleton (bot) | ba3a566 | 2019-10-26 07:06:40 -0700 | [diff] [blame] | 1773 | d = Call(name_expr, NULL, NULL, |
| 1774 | name_expr->lineno, name_expr->col_offset, |
| 1775 | CHILD(n, 3)->n_end_lineno, CHILD(n, 3)->n_end_col_offset, |
| 1776 | c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1777 | if (!d) |
| 1778 | return NULL; |
| 1779 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1780 | } |
| 1781 | else { |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 1782 | d = ast_for_call(c, CHILD(n, 3), name_expr, |
| 1783 | CHILD(n, 1), CHILD(n, 2), CHILD(n, 4)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1784 | if (!d) |
| 1785 | return NULL; |
| 1786 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static asdl_seq* |
| 1793 | ast_for_decorators(struct compiling *c, const node *n) |
| 1794 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1795 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1796 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1799 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1800 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1801 | if (!decorator_seq) |
| 1802 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1804 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1805 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1806 | if (!d) |
| 1807 | return NULL; |
| 1808 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1809 | } |
| 1810 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1814 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1815 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1816 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1817 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1818 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1819 | identifier name; |
| 1820 | arguments_ty args; |
| 1821 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1822 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1823 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1824 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1825 | node *tc; |
| 1826 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1828 | if (is_async && c->c_feature_version < 5) { |
| 1829 | ast_error(c, n, |
| 1830 | "Async functions are only supported in Python 3.5 and greater"); |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1834 | REQ(n, funcdef); |
| 1835 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1836 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1837 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1838 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1839 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1840 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1841 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1842 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1843 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1844 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1845 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1846 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1847 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1848 | name_i += 2; |
| 1849 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1850 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1851 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1852 | if (!type_comment) |
| 1853 | return NULL; |
| 1854 | name_i += 1; |
| 1855 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1856 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1857 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1858 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1859 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1860 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1861 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1862 | /* Check if the suite has a type comment in it. */ |
| 1863 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1864 | |
| 1865 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1866 | if (type_comment != NULL) { |
| 1867 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1868 | return NULL; |
| 1869 | } |
| 1870 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1871 | if (!type_comment) |
| 1872 | return NULL; |
| 1873 | } |
| 1874 | } |
| 1875 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1876 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1877 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1878 | 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] | 1879 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1880 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1881 | 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] | 1882 | } |
| 1883 | |
| 1884 | static stmt_ty |
| 1885 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1886 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1887 | /* async_funcdef: ASYNC funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1888 | REQ(n, async_funcdef); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1889 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1890 | REQ(CHILD(n, 1), funcdef); |
| 1891 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1892 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1893 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | static stmt_ty |
| 1897 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1898 | { |
| 1899 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1900 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1901 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | static stmt_ty |
| 1906 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1907 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1908 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1909 | REQ(n, async_stmt); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1910 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1911 | |
| 1912 | switch (TYPE(CHILD(n, 1))) { |
| 1913 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1914 | return ast_for_funcdef_impl(c, n, NULL, |
| 1915 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1916 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1917 | return ast_for_with_stmt(c, n, |
| 1918 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1919 | |
| 1920 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1921 | return ast_for_for_stmt(c, n, |
| 1922 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1923 | |
| 1924 | default: |
| 1925 | PyErr_Format(PyExc_SystemError, |
| 1926 | "invalid async stament: %s", |
| 1927 | STR(CHILD(n, 1))); |
| 1928 | return NULL; |
| 1929 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1932 | static stmt_ty |
| 1933 | ast_for_decorated(struct compiling *c, const node *n) |
| 1934 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1935 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1936 | stmt_ty thing = NULL; |
| 1937 | asdl_seq *decorator_seq = NULL; |
| 1938 | |
| 1939 | REQ(n, decorated); |
| 1940 | |
| 1941 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1942 | if (!decorator_seq) |
| 1943 | return NULL; |
| 1944 | |
| 1945 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1946 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1948 | |
| 1949 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1950 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1951 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1952 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1953 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1954 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1955 | } |
| 1956 | return thing; |
| 1957 | } |
| 1958 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1959 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1960 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1961 | { |
Miss Islington (bot) | cd968de | 2019-12-15 12:04:07 -0800 | [diff] [blame] | 1962 | /* namedexpr_test: test [':=' test] |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1963 | argument: ( test [comp_for] | |
| 1964 | test ':=' test | |
| 1965 | test '=' test | |
| 1966 | '**' test | |
| 1967 | '*' test ) |
| 1968 | */ |
| 1969 | expr_ty target, value; |
| 1970 | |
| 1971 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1972 | if (!target) |
| 1973 | return NULL; |
| 1974 | |
| 1975 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1976 | if (!value) |
| 1977 | return NULL; |
| 1978 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1979 | if (target->kind != Name_kind) { |
| 1980 | const char *expr_name = get_expr_name(target); |
| 1981 | if (expr_name != NULL) { |
Miss Islington (bot) | 6c00495 | 2019-12-31 19:28:08 -0800 | [diff] [blame] | 1982 | ast_error(c, n, "cannot use assignment expressions with %s", expr_name); |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1983 | } |
| 1984 | return NULL; |
| 1985 | } |
| 1986 | |
| 1987 | if (!set_context(c, target, Store, n)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1988 | return NULL; |
| 1989 | |
| 1990 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1991 | n->n_end_col_offset, c->c_arena); |
| 1992 | } |
| 1993 | |
| 1994 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1995 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1996 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1997 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1998 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1999 | arguments_ty args; |
| 2000 | expr_ty expression; |
| 2001 | |
| 2002 | if (NCH(n) == 3) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2003 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2004 | if (!args) |
| 2005 | return NULL; |
| 2006 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2007 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2008 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2009 | } |
| 2010 | else { |
| 2011 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 2012 | if (!args) |
| 2013 | return NULL; |
| 2014 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2015 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2016 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2019 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 2020 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2023 | static expr_ty |
| 2024 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 2025 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2027 | expr_ty expression, body, orelse; |
| 2028 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 2029 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2030 | body = ast_for_expr(c, CHILD(n, 0)); |
| 2031 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2032 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2033 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 2034 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2035 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2036 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 2037 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2038 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2039 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2040 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2041 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2044 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2045 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2046 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2047 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2048 | */ |
| 2049 | |
| 2050 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2051 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2052 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2053 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2054 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2055 | count_comp_for: |
| 2056 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2057 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2058 | if (NCH(n) == 2) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2059 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2060 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2061 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2062 | else if (NCH(n) == 1) { |
| 2063 | n = CHILD(n, 0); |
| 2064 | } |
| 2065 | else { |
| 2066 | goto error; |
| 2067 | } |
| 2068 | if (NCH(n) == (5)) { |
| 2069 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2070 | } |
| 2071 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2072 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2073 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2074 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2075 | REQ(n, comp_iter); |
| 2076 | n = CHILD(n, 0); |
| 2077 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2078 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2079 | else if (TYPE(n) == comp_if) { |
| 2080 | if (NCH(n) == 3) { |
| 2081 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2082 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2083 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2084 | else |
| 2085 | return n_fors; |
| 2086 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2087 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2088 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2089 | /* Should never be reached */ |
| 2090 | PyErr_SetString(PyExc_SystemError, |
| 2091 | "logic error in count_comp_fors"); |
| 2092 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2095 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2096 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2097 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2098 | */ |
| 2099 | |
| 2100 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2101 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2102 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2103 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2104 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2105 | while (1) { |
| 2106 | REQ(n, comp_iter); |
| 2107 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2108 | return n_ifs; |
| 2109 | n = CHILD(n, 0); |
| 2110 | REQ(n, comp_if); |
| 2111 | n_ifs++; |
| 2112 | if (NCH(n) == 2) |
| 2113 | return n_ifs; |
| 2114 | n = CHILD(n, 2); |
| 2115 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2118 | static asdl_seq * |
| 2119 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2120 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2121 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2122 | asdl_seq *comps; |
| 2123 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2124 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2125 | if (n_fors == -1) |
| 2126 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2128 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2129 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2130 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2131 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2132 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2133 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2134 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2135 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2136 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2137 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2138 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2140 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2142 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2143 | is_async = 1; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2144 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2145 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2146 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2147 | else { |
| 2148 | sync_n = CHILD(n, 0); |
| 2149 | } |
| 2150 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2151 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2152 | /* Async comprehensions only allowed in Python 3.6 and greater */ |
| 2153 | if (is_async && c->c_feature_version < 6) { |
| 2154 | ast_error(c, n, |
| 2155 | "Async comprehensions are only supported in Python 3.6 and greater"); |
| 2156 | return NULL; |
| 2157 | } |
| 2158 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2159 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2160 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2161 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2162 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2163 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2164 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2165 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2166 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2167 | /* Check the # of children rather than the length of t, since |
| 2168 | (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] | 2169 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2170 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2171 | comp = comprehension(first, expression, NULL, |
| 2172 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2173 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2174 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2175 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2176 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2177 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2178 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2179 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2180 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2181 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2182 | int j, n_ifs; |
| 2183 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2185 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2186 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2187 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2188 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2190 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2191 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2192 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2193 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2194 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2195 | REQ(n, comp_iter); |
| 2196 | n = CHILD(n, 0); |
| 2197 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2199 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2200 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2201 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2202 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2203 | if (NCH(n) == 3) |
| 2204 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2205 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2206 | /* on exit, must guarantee that n is a comp_for */ |
| 2207 | if (TYPE(n) == comp_iter) |
| 2208 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2209 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2210 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2211 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2212 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2213 | return comps; |
| 2214 | } |
| 2215 | |
| 2216 | static expr_ty |
| 2217 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2218 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2219 | /* testlist_comp: (test|star_expr) |
| 2220 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2221 | expr_ty elt; |
| 2222 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2223 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2225 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2227 | ch = CHILD(n, 0); |
| 2228 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2229 | if (!elt) |
| 2230 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2231 | if (elt->kind == Starred_kind) { |
| 2232 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2233 | return NULL; |
| 2234 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2236 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2237 | if (!comps) |
| 2238 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2239 | |
| 2240 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2241 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2242 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2243 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2244 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2245 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2246 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2247 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2248 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2249 | else |
| 2250 | /* Should never happen */ |
| 2251 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2254 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2255 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2256 | * elements. Iff successful, nonzero is returned. |
| 2257 | */ |
| 2258 | static int |
| 2259 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2260 | expr_ty *key, expr_ty *value) |
| 2261 | { |
| 2262 | expr_ty expression; |
| 2263 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2264 | assert(NCH(n) - *i >= 2); |
| 2265 | |
| 2266 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2267 | if (!expression) |
| 2268 | return 0; |
| 2269 | *key = NULL; |
| 2270 | *value = expression; |
| 2271 | |
| 2272 | *i += 2; |
| 2273 | } |
| 2274 | else { |
| 2275 | assert(NCH(n) - *i >= 3); |
| 2276 | |
| 2277 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2278 | if (!expression) |
| 2279 | return 0; |
| 2280 | *key = expression; |
| 2281 | |
| 2282 | REQ(CHILD(n, *i + 1), COLON); |
| 2283 | |
| 2284 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2285 | if (!expression) |
| 2286 | return 0; |
| 2287 | *value = expression; |
| 2288 | |
| 2289 | *i += 3; |
| 2290 | } |
| 2291 | return 1; |
| 2292 | } |
| 2293 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2294 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2295 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2296 | { |
| 2297 | expr_ty key, value; |
| 2298 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2299 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2301 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2302 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2303 | assert(key); |
| 2304 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2306 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2307 | if (!comps) |
| 2308 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2310 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2311 | 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] | 2312 | } |
| 2313 | |
| 2314 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2315 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2316 | { |
| 2317 | int i; |
| 2318 | int j; |
| 2319 | int size; |
| 2320 | asdl_seq *keys, *values; |
| 2321 | |
| 2322 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2323 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2324 | if (!keys) |
| 2325 | return NULL; |
| 2326 | |
| 2327 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2328 | if (!values) |
| 2329 | return NULL; |
| 2330 | |
| 2331 | j = 0; |
| 2332 | for (i = 0; i < NCH(n); i++) { |
| 2333 | expr_ty key, value; |
| 2334 | |
| 2335 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2336 | return NULL; |
| 2337 | asdl_seq_SET(keys, j, key); |
| 2338 | asdl_seq_SET(values, j, value); |
| 2339 | |
| 2340 | j++; |
| 2341 | } |
| 2342 | keys->size = j; |
| 2343 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2344 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2345 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2349 | ast_for_genexp(struct compiling *c, const node *n) |
| 2350 | { |
| 2351 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2352 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | static expr_ty |
| 2356 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2357 | { |
| 2358 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2359 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | static expr_ty |
| 2363 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2364 | { |
| 2365 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2366 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2369 | static expr_ty |
| 2370 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2371 | { |
| 2372 | int i; |
| 2373 | int size; |
| 2374 | asdl_seq *elts; |
| 2375 | |
| 2376 | assert(TYPE(n) == (dictorsetmaker)); |
| 2377 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2378 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2379 | if (!elts) |
| 2380 | return NULL; |
| 2381 | for (i = 0; i < NCH(n); i += 2) { |
| 2382 | expr_ty expression; |
| 2383 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2384 | if (!expression) |
| 2385 | return NULL; |
| 2386 | asdl_seq_SET(elts, i / 2, expression); |
| 2387 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2388 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2389 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2390 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2391 | |
| 2392 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2393 | ast_for_atom(struct compiling *c, const node *n) |
| 2394 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2395 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2396 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2397 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2398 | */ |
| 2399 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2401 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2402 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2403 | PyObject *name; |
| 2404 | const char *s = STR(ch); |
| 2405 | size_t len = strlen(s); |
| 2406 | if (len >= 4 && len <= 5) { |
| 2407 | if (!strcmp(s, "None")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2408 | return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2409 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2410 | if (!strcmp(s, "True")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2411 | return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2412 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2413 | if (!strcmp(s, "False")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2414 | return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2415 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2416 | } |
| 2417 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2418 | if (!name) |
| 2419 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2420 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2421 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2422 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2423 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2424 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2425 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2426 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2427 | const char *errtype = NULL; |
| 2428 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2429 | errtype = "unicode error"; |
| 2430 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2431 | errtype = "value error"; |
| 2432 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2433 | PyObject *type, *value, *tback, *errstr; |
| 2434 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2435 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2436 | if (errstr) { |
| 2437 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2438 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2439 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2440 | else { |
| 2441 | PyErr_Clear(); |
| 2442 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2443 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2444 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2445 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2446 | Py_XDECREF(tback); |
| 2447 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2448 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2449 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2450 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2451 | } |
| 2452 | case NUMBER: { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2453 | PyObject *pynum; |
| 2454 | /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ |
| 2455 | /* Check for underscores here rather than in parse_number so we can report a line number on error */ |
| 2456 | if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { |
| 2457 | ast_error(c, ch, |
| 2458 | "Underscores in numeric literals are only supported in Python 3.6 and greater"); |
| 2459 | return NULL; |
| 2460 | } |
| 2461 | pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2462 | if (!pynum) |
| 2463 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2464 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2465 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2466 | Py_DECREF(pynum); |
| 2467 | return NULL; |
| 2468 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2469 | return Constant(pynum, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2470 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2471 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2472 | case ELLIPSIS: /* Ellipsis */ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2473 | return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2474 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2475 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2476 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2478 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2479 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2480 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2482 | if (TYPE(ch) == yield_expr) |
| 2483 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2485 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2486 | if (NCH(ch) == 1) { |
| 2487 | return ast_for_testlist(c, ch); |
| 2488 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2489 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2490 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 2491 | return copy_location(ast_for_genexp(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2492 | } |
| 2493 | else { |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 2494 | return copy_location(ast_for_testlist(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2495 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2496 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2497 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2499 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2500 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2501 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2503 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2504 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2505 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2506 | if (!elts) |
| 2507 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2508 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2509 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2510 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2511 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2512 | else { |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 2513 | return copy_location(ast_for_listcomp(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2515 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2516 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2517 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2518 | * ((test | '*' test) |
| 2519 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2520 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2521 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2522 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2523 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2524 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2525 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2526 | } |
| 2527 | else { |
| 2528 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2529 | if (NCH(ch) == 1 || |
| 2530 | (NCH(ch) > 1 && |
| 2531 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2532 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2533 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2534 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2535 | else if (NCH(ch) > 1 && |
| 2536 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2537 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2538 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2539 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2540 | else if (NCH(ch) > 3 - is_dict && |
| 2541 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2542 | /* It's a dictionary comprehension. */ |
| 2543 | if (is_dict) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2544 | ast_error(c, n, |
| 2545 | "dict unpacking cannot be used in dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2546 | return NULL; |
| 2547 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2548 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2549 | } |
| 2550 | else { |
| 2551 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2552 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2553 | } |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 2554 | return copy_location(res, n, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2555 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2556 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2557 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2558 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2559 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | static slice_ty |
| 2564 | ast_for_slice(struct compiling *c, const node *n) |
| 2565 | { |
| 2566 | node *ch; |
| 2567 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2568 | |
| 2569 | REQ(n, subscript); |
| 2570 | |
| 2571 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2572 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2573 | sliceop: ':' [test] |
| 2574 | */ |
| 2575 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2576 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2577 | /* 'step' variable hold no significance in terms of being used over |
| 2578 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2580 | if (!step) |
| 2581 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2583 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2587 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2588 | if (!lower) |
| 2589 | return NULL; |
| 2590 | } |
| 2591 | |
| 2592 | /* If there's an upper bound it's in the second or third position. */ |
| 2593 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2594 | if (NCH(n) > 1) { |
| 2595 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2596 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2597 | if (TYPE(n2) == test) { |
| 2598 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2599 | if (!upper) |
| 2600 | return NULL; |
| 2601 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2602 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2603 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2604 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2605 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2606 | if (TYPE(n2) == test) { |
| 2607 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2608 | if (!upper) |
| 2609 | return NULL; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | ch = CHILD(n, NCH(n) - 1); |
| 2614 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2615 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2616 | ch = CHILD(ch, 1); |
| 2617 | if (TYPE(ch) == test) { |
| 2618 | step = ast_for_expr(c, ch); |
| 2619 | if (!step) |
| 2620 | return NULL; |
| 2621 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2625 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | static expr_ty |
| 2629 | ast_for_binop(struct compiling *c, const node *n) |
| 2630 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2631 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2633 | BinOp(BinOp(A, op, B), op, C). |
| 2634 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2635 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2636 | int i, nops; |
| 2637 | expr_ty expr1, expr2, result; |
| 2638 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2639 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2640 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2641 | if (!expr1) |
| 2642 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2643 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2644 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2645 | if (!expr2) |
| 2646 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2648 | newoperator = get_operator(c, CHILD(n, 1)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2649 | if (!newoperator) |
| 2650 | return NULL; |
| 2651 | |
| 2652 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2653 | 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] | 2654 | c->c_arena); |
| 2655 | if (!result) |
| 2656 | return NULL; |
| 2657 | |
| 2658 | nops = (NCH(n) - 1) / 2; |
| 2659 | for (i = 1; i < nops; i++) { |
| 2660 | expr_ty tmp_result, tmp; |
| 2661 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2662 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2663 | newoperator = get_operator(c, next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2664 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2665 | return NULL; |
| 2666 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2667 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2668 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2669 | return NULL; |
| 2670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2671 | tmp_result = BinOp(result, newoperator, tmp, |
Miss Islington (bot) | c7be35c | 2019-07-08 14:41:34 -0700 | [diff] [blame] | 2672 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2673 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2674 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2675 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2677 | return NULL; |
| 2678 | result = tmp_result; |
| 2679 | } |
| 2680 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2683 | static expr_ty |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2684 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2687 | subscriptlist: subscript (',' subscript)* [','] |
| 2688 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2689 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2690 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2691 | REQ(n, trailer); |
| 2692 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2693 | if (NCH(n) == 2) |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2694 | return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2695 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2696 | else |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2697 | return ast_for_call(c, CHILD(n, 1), left_expr, |
| 2698 | start, CHILD(n, 0), CHILD(n, 2)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2699 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2700 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2701 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2702 | if (!attr_id) |
| 2703 | return NULL; |
| 2704 | return Attribute(left_expr, attr_id, Load, |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2705 | LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2706 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2707 | } |
| 2708 | else { |
| 2709 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2710 | REQ(CHILD(n, 2), RSQB); |
| 2711 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2712 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2713 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2714 | if (!slc) |
| 2715 | return NULL; |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2716 | return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2717 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2718 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2719 | } |
| 2720 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2722 | by treating the sequence as a tuple literal if there are |
| 2723 | no slice features. |
| 2724 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2725 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2726 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2727 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2728 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2729 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2730 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2731 | if (!slices) |
| 2732 | return NULL; |
| 2733 | for (j = 0; j < NCH(n); j += 2) { |
| 2734 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2735 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2736 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2737 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2738 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2739 | asdl_seq_SET(slices, j / 2, slc); |
| 2740 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2741 | if (!simple) { |
| 2742 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2743 | Load, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2744 | 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] | 2745 | } |
| 2746 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2747 | 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] | 2748 | if (!elts) |
| 2749 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2750 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2751 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2752 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2753 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2754 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2755 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2756 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2757 | if (!e) |
| 2758 | return NULL; |
| 2759 | return Subscript(left_expr, Index(e, c->c_arena), |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2760 | Load, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2761 | 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] | 2762 | } |
| 2763 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2767 | ast_for_factor(struct compiling *c, const node *n) |
| 2768 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2769 | expr_ty expression; |
| 2770 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2771 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2772 | if (!expression) |
| 2773 | return NULL; |
| 2774 | |
| 2775 | switch (TYPE(CHILD(n, 0))) { |
| 2776 | case PLUS: |
| 2777 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2778 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2779 | c->c_arena); |
| 2780 | case MINUS: |
| 2781 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2782 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2783 | c->c_arena); |
| 2784 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2785 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2786 | n->n_end_lineno, n->n_end_col_offset, |
| 2787 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2788 | } |
| 2789 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2790 | TYPE(CHILD(n, 0))); |
| 2791 | return NULL; |
| 2792 | } |
| 2793 | |
| 2794 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2795 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2796 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2797 | int i, nch, start = 0; |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2798 | expr_ty e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2799 | |
| 2800 | REQ(n, atom_expr); |
| 2801 | nch = NCH(n); |
| 2802 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2803 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2804 | if (c->c_feature_version < 5) { |
| 2805 | ast_error(c, n, |
| 2806 | "Await expressions are only supported in Python 3.5 and greater"); |
| 2807 | return NULL; |
| 2808 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2809 | start = 1; |
| 2810 | assert(nch > 1); |
| 2811 | } |
| 2812 | |
| 2813 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2814 | if (!e) |
| 2815 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2816 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2817 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2818 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2819 | return Await(e, LINENO(n), n->n_col_offset, |
| 2820 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2824 | node *ch = CHILD(n, i); |
| 2825 | if (TYPE(ch) != trailer) |
| 2826 | break; |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 2827 | e = ast_for_trailer(c, ch, e, CHILD(n, start)); |
| 2828 | if (!e) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2829 | return NULL; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2830 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2831 | |
| 2832 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2833 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2834 | return Await(e, LINENO(n), n->n_col_offset, |
| 2835 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2836 | } |
| 2837 | else { |
| 2838 | return e; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | static expr_ty |
| 2843 | ast_for_power(struct compiling *c, const node *n) |
| 2844 | { |
| 2845 | /* power: atom trailer* ('**' factor)* |
| 2846 | */ |
| 2847 | expr_ty e; |
| 2848 | REQ(n, power); |
| 2849 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2850 | if (!e) |
| 2851 | return NULL; |
| 2852 | if (NCH(n) == 1) |
| 2853 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2854 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2855 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2856 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2857 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2858 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2859 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2860 | } |
| 2861 | return e; |
| 2862 | } |
| 2863 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2864 | static expr_ty |
| 2865 | ast_for_starred(struct compiling *c, const node *n) |
| 2866 | { |
| 2867 | expr_ty tmp; |
| 2868 | REQ(n, star_expr); |
| 2869 | |
| 2870 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2871 | if (!tmp) |
| 2872 | return NULL; |
| 2873 | |
| 2874 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2875 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2876 | 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] | 2877 | } |
| 2878 | |
| 2879 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2881 | */ |
| 2882 | |
| 2883 | static expr_ty |
| 2884 | ast_for_expr(struct compiling *c, const node *n) |
| 2885 | { |
| 2886 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2887 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2888 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2889 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 | and_test: not_test ('and' not_test)* |
| 2892 | not_test: 'not' not_test | comparison |
| 2893 | comparison: expr (comp_op expr)* |
| 2894 | expr: xor_expr ('|' xor_expr)* |
| 2895 | xor_expr: and_expr ('^' and_expr)* |
| 2896 | and_expr: shift_expr ('&' shift_expr)* |
| 2897 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2898 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2899 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2900 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2901 | power: atom_expr ['**' factor] |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2902 | atom_expr: [AWAIT] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2903 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | */ |
| 2905 | |
| 2906 | asdl_seq *seq; |
| 2907 | int i; |
| 2908 | |
| 2909 | loop: |
| 2910 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2911 | case namedexpr_test: |
| 2912 | if (NCH(n) == 3) |
| 2913 | return ast_for_namedexpr(c, n); |
| 2914 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2915 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2916 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2917 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2918 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2919 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2920 | else if (NCH(n) > 1) |
| 2921 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2922 | /* Fallthrough */ |
| 2923 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2924 | case and_test: |
| 2925 | if (NCH(n) == 1) { |
| 2926 | n = CHILD(n, 0); |
| 2927 | goto loop; |
| 2928 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2929 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2930 | if (!seq) |
| 2931 | return NULL; |
| 2932 | for (i = 0; i < NCH(n); i += 2) { |
| 2933 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2934 | if (!e) |
| 2935 | return NULL; |
| 2936 | asdl_seq_SET(seq, i / 2, e); |
| 2937 | } |
| 2938 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2939 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2940 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2941 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2942 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2943 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2944 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2945 | case not_test: |
| 2946 | if (NCH(n) == 1) { |
| 2947 | n = CHILD(n, 0); |
| 2948 | goto loop; |
| 2949 | } |
| 2950 | else { |
| 2951 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2952 | if (!expression) |
| 2953 | return NULL; |
| 2954 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2955 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2956 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2957 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2958 | } |
| 2959 | case comparison: |
| 2960 | if (NCH(n) == 1) { |
| 2961 | n = CHILD(n, 0); |
| 2962 | goto loop; |
| 2963 | } |
| 2964 | else { |
| 2965 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2966 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2967 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2968 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2969 | if (!ops) |
| 2970 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2971 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2973 | return NULL; |
| 2974 | } |
| 2975 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2976 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2977 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2978 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2979 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2980 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2981 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | |
| 2983 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2984 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2986 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2988 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2989 | asdl_seq_SET(cmps, i / 2, expression); |
| 2990 | } |
| 2991 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2992 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2993 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2994 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2996 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2997 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2998 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3000 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3002 | /* The next five cases all handle BinOps. The main body of code |
| 3003 | is the same in each case, but the switch turned inside out to |
| 3004 | reuse the code for each type of operator. |
| 3005 | */ |
| 3006 | case expr: |
| 3007 | case xor_expr: |
| 3008 | case and_expr: |
| 3009 | case shift_expr: |
| 3010 | case arith_expr: |
| 3011 | case term: |
| 3012 | if (NCH(n) == 1) { |
| 3013 | n = CHILD(n, 0); |
| 3014 | goto loop; |
| 3015 | } |
| 3016 | return ast_for_binop(c, n); |
| 3017 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3018 | node *an = NULL; |
| 3019 | node *en = NULL; |
| 3020 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3021 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3022 | if (NCH(n) > 1) |
| 3023 | an = CHILD(n, 1); /* yield_arg */ |
| 3024 | if (an) { |
| 3025 | en = CHILD(an, NCH(an) - 1); |
| 3026 | if (NCH(an) == 2) { |
| 3027 | is_from = 1; |
| 3028 | exp = ast_for_expr(c, en); |
| 3029 | } |
| 3030 | else |
| 3031 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3032 | if (!exp) |
| 3033 | return NULL; |
| 3034 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 3035 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3036 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 3037 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 3038 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 3039 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3040 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3041 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3042 | if (NCH(n) == 1) { |
| 3043 | n = CHILD(n, 0); |
| 3044 | goto loop; |
| 3045 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3046 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 3047 | case power: |
| 3048 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3049 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3050 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3051 | return NULL; |
| 3052 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3053 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3054 | return NULL; |
| 3055 | } |
| 3056 | |
| 3057 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3058 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 3059 | const node *start, const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3060 | { |
| 3061 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3062 | arglist: argument (',' argument)* [','] |
| 3063 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3064 | */ |
| 3065 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3066 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3067 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3068 | asdl_seq *args; |
| 3069 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3070 | |
| 3071 | REQ(n, arglist); |
| 3072 | |
| 3073 | nargs = 0; |
| 3074 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3075 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3076 | node *ch = CHILD(n, i); |
| 3077 | if (TYPE(ch) == argument) { |
| 3078 | if (NCH(ch) == 1) |
| 3079 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3080 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 3081 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3082 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 3083 | ast_error(c, ch, "invalid syntax"); |
| 3084 | return NULL; |
| 3085 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3086 | if (NCH(n) > 1) { |
| 3087 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 3088 | return NULL; |
| 3089 | } |
| 3090 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3091 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 3092 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3093 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3094 | nargs++; |
| 3095 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3096 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3097 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3098 | nkeywords++; |
| 3099 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3100 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3101 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3102 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3103 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3104 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3105 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3106 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3107 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3108 | |
| 3109 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 3110 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 3111 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3113 | node *ch = CHILD(n, i); |
| 3114 | if (TYPE(ch) == argument) { |
| 3115 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3116 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3117 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3118 | /* a positional argument */ |
| 3119 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3120 | if (ndoublestars) { |
| 3121 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3122 | "positional argument follows " |
| 3123 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3124 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3125 | else { |
| 3126 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3127 | "positional argument follows " |
| 3128 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3129 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3130 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3131 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3132 | e = ast_for_expr(c, chch); |
| 3133 | if (!e) |
| 3134 | return NULL; |
| 3135 | asdl_seq_SET(args, nargs++, e); |
| 3136 | } |
| 3137 | else if (TYPE(chch) == STAR) { |
| 3138 | /* an iterable argument unpacking */ |
| 3139 | expr_ty starred; |
| 3140 | if (ndoublestars) { |
| 3141 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3142 | "iterable argument unpacking follows " |
| 3143 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3144 | return NULL; |
| 3145 | } |
| 3146 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3147 | if (!e) |
| 3148 | return NULL; |
| 3149 | starred = Starred(e, Load, LINENO(chch), |
| 3150 | chch->n_col_offset, |
Pablo Galindo | b1f2044 | 2019-12-18 01:41:58 +0000 | [diff] [blame] | 3151 | e->end_lineno, e->end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3152 | c->c_arena); |
| 3153 | if (!starred) |
| 3154 | return NULL; |
| 3155 | asdl_seq_SET(args, nargs++, starred); |
| 3156 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3157 | } |
| 3158 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3159 | /* a keyword argument unpacking */ |
| 3160 | keyword_ty kw; |
| 3161 | i++; |
| 3162 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3163 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3164 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3165 | kw = keyword(NULL, e, c->c_arena); |
| 3166 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3167 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3169 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3170 | /* the lone generator expression */ |
Miss Islington (bot) | 33e033d | 2020-01-09 11:39:00 -0800 | [diff] [blame] | 3171 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3172 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3173 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3174 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3175 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3176 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3177 | /* treat colon equal as positional argument */ |
| 3178 | if (nkeywords) { |
| 3179 | if (ndoublestars) { |
| 3180 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3181 | "positional argument follows " |
| 3182 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3183 | } |
| 3184 | else { |
| 3185 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3186 | "positional argument follows " |
| 3187 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3188 | } |
| 3189 | return NULL; |
| 3190 | } |
| 3191 | e = ast_for_namedexpr(c, ch); |
| 3192 | if (!e) |
| 3193 | return NULL; |
| 3194 | asdl_seq_SET(args, nargs++, e); |
| 3195 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3196 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3197 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3198 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3199 | identifier key, tmp; |
| 3200 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3202 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3203 | // expression) in the keyword slot of a call site. So, we need |
| 3204 | // to manually enforce that the keyword is a NAME here. |
| 3205 | static const int name_tree[] = { |
| 3206 | test, |
| 3207 | or_test, |
| 3208 | and_test, |
| 3209 | not_test, |
| 3210 | comparison, |
| 3211 | expr, |
| 3212 | xor_expr, |
| 3213 | and_expr, |
| 3214 | shift_expr, |
| 3215 | arith_expr, |
| 3216 | term, |
| 3217 | factor, |
| 3218 | power, |
| 3219 | atom_expr, |
| 3220 | atom, |
| 3221 | 0, |
| 3222 | }; |
| 3223 | node *expr_node = chch; |
| 3224 | for (int i = 0; name_tree[i]; i++) { |
| 3225 | if (TYPE(expr_node) != name_tree[i]) |
| 3226 | break; |
| 3227 | if (NCH(expr_node) != 1) |
| 3228 | break; |
| 3229 | expr_node = CHILD(expr_node, 0); |
| 3230 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3231 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3232 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3233 | "expression cannot contain assignment, " |
| 3234 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3235 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3236 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3237 | key = new_identifier(STR(expr_node), c); |
| 3238 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3239 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3241 | if (forbidden_name(c, key, chch, 1)) { |
| 3242 | return NULL; |
| 3243 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3244 | for (k = 0; k < nkeywords; k++) { |
| 3245 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3246 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 3247 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3248 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3249 | return NULL; |
| 3250 | } |
| 3251 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3252 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3253 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3254 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3255 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3256 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3257 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3258 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3259 | } |
| 3260 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 3263 | return Call(func, args, keywords, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3264 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3267 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3268 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3269 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3270 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3271 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3272 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3273 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3274 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3275 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3276 | } |
| 3277 | else { |
| 3278 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3279 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3280 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3281 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3282 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3283 | else { |
| 3284 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3285 | if (!tmp) |
| 3286 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3287 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3288 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3289 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3292 | static stmt_ty |
| 3293 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3294 | { |
| 3295 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3296 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3297 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3298 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3299 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3300 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3301 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3302 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3304 | int num = NCH(n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3305 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3306 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3307 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3308 | if (!e) |
| 3309 | return NULL; |
| 3310 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3311 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3312 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3313 | } |
| 3314 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3315 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3316 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3317 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3318 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3319 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3320 | if (!expr1) |
| 3321 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3322 | if(!set_context(c, expr1, Store, ch)) |
| 3323 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3324 | /* set_context checks that most expressions are not the left side. |
| 3325 | Augmented assignments can only have a name, a subscript, or an |
| 3326 | attribute on the left, though, so we have to explicitly check for |
| 3327 | those. */ |
| 3328 | switch (expr1->kind) { |
| 3329 | case Name_kind: |
| 3330 | case Attribute_kind: |
| 3331 | case Subscript_kind: |
| 3332 | break; |
| 3333 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3334 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3335 | return NULL; |
| 3336 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3337 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3338 | ch = CHILD(n, 2); |
| 3339 | if (TYPE(ch) == testlist) |
| 3340 | expr2 = ast_for_testlist(c, ch); |
| 3341 | else |
| 3342 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3343 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3344 | return NULL; |
| 3345 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3346 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3347 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3348 | return NULL; |
| 3349 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3350 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3351 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3352 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3353 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3354 | expr_ty expr1, expr2, expr3; |
| 3355 | node *ch = CHILD(n, 0); |
| 3356 | node *deep, *ann = CHILD(n, 1); |
| 3357 | int simple = 1; |
| 3358 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 3359 | /* AnnAssigns are only allowed in Python 3.6 or greater */ |
| 3360 | if (c->c_feature_version < 6) { |
| 3361 | ast_error(c, ch, |
| 3362 | "Variable annotation syntax is only supported in Python 3.6 and greater"); |
| 3363 | return NULL; |
| 3364 | } |
| 3365 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3366 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3367 | deep = ch; |
| 3368 | while (NCH(deep) == 1) { |
| 3369 | deep = CHILD(deep, 0); |
| 3370 | } |
| 3371 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3372 | simple = 0; |
| 3373 | } |
| 3374 | expr1 = ast_for_testlist(c, ch); |
| 3375 | if (!expr1) { |
| 3376 | return NULL; |
| 3377 | } |
| 3378 | switch (expr1->kind) { |
| 3379 | case Name_kind: |
| 3380 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3381 | return NULL; |
| 3382 | } |
| 3383 | expr1->v.Name.ctx = Store; |
| 3384 | break; |
| 3385 | case Attribute_kind: |
| 3386 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | expr1->v.Attribute.ctx = Store; |
| 3390 | break; |
| 3391 | case Subscript_kind: |
| 3392 | expr1->v.Subscript.ctx = Store; |
| 3393 | break; |
| 3394 | case List_kind: |
| 3395 | ast_error(c, ch, |
| 3396 | "only single target (not list) can be annotated"); |
| 3397 | return NULL; |
| 3398 | case Tuple_kind: |
| 3399 | ast_error(c, ch, |
| 3400 | "only single target (not tuple) can be annotated"); |
| 3401 | return NULL; |
| 3402 | default: |
| 3403 | ast_error(c, ch, |
| 3404 | "illegal target for annotation"); |
| 3405 | return NULL; |
| 3406 | } |
| 3407 | |
| 3408 | if (expr1->kind != Name_kind) { |
| 3409 | simple = 0; |
| 3410 | } |
| 3411 | ch = CHILD(ann, 1); |
| 3412 | expr2 = ast_for_expr(c, ch); |
| 3413 | if (!expr2) { |
| 3414 | return NULL; |
| 3415 | } |
| 3416 | if (NCH(ann) == 2) { |
| 3417 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3418 | LINENO(n), n->n_col_offset, |
| 3419 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3420 | } |
| 3421 | else { |
| 3422 | ch = CHILD(ann, 3); |
Pablo Galindo | 8565f6b | 2019-06-03 08:34:20 +0100 | [diff] [blame] | 3423 | if (TYPE(ch) == testlist_star_expr) { |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3424 | expr3 = ast_for_testlist(c, ch); |
| 3425 | } |
| 3426 | else { |
| 3427 | expr3 = ast_for_expr(c, ch); |
| 3428 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3429 | if (!expr3) { |
| 3430 | return NULL; |
| 3431 | } |
| 3432 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3433 | LINENO(n), n->n_col_offset, |
| 3434 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3435 | } |
| 3436 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3437 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3438 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3439 | asdl_seq *targets; |
| 3440 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3441 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3442 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3443 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3444 | /* a normal assignment */ |
| 3445 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3446 | |
| 3447 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3448 | nch_minus_type = num - has_type_comment; |
| 3449 | |
| 3450 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3451 | if (!targets) |
| 3452 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3453 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3454 | expr_ty e; |
| 3455 | node *ch = CHILD(n, i); |
| 3456 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3457 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3458 | return NULL; |
| 3459 | } |
| 3460 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3462 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3463 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3464 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3465 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3466 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3468 | asdl_seq_SET(targets, i / 2, e); |
| 3469 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3470 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3471 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3472 | expression = ast_for_testlist(c, value); |
| 3473 | else |
| 3474 | expression = ast_for_expr(c, value); |
| 3475 | if (!expression) |
| 3476 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3477 | if (has_type_comment) { |
| 3478 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3479 | if (!type_comment) |
| 3480 | return NULL; |
| 3481 | } |
| 3482 | else |
| 3483 | type_comment = NULL; |
| 3484 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3485 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3486 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3489 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3490 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3491 | 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] | 3492 | { |
| 3493 | asdl_seq *seq; |
| 3494 | int i; |
| 3495 | expr_ty e; |
| 3496 | |
| 3497 | REQ(n, exprlist); |
| 3498 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3499 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3500 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3501 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3502 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3503 | e = ast_for_expr(c, CHILD(n, i)); |
| 3504 | if (!e) |
| 3505 | return NULL; |
| 3506 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3507 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3508 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3509 | } |
| 3510 | return seq; |
| 3511 | } |
| 3512 | |
| 3513 | static stmt_ty |
| 3514 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3515 | { |
| 3516 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3518 | /* del_stmt: 'del' exprlist */ |
| 3519 | REQ(n, del_stmt); |
| 3520 | |
| 3521 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3522 | if (!expr_list) |
| 3523 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3524 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3525 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | static stmt_ty |
| 3529 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3530 | { |
| 3531 | /* |
| 3532 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3533 | | yield_stmt |
| 3534 | break_stmt: 'break' |
| 3535 | continue_stmt: 'continue' |
| 3536 | return_stmt: 'return' [testlist] |
| 3537 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3538 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3540 | */ |
| 3541 | node *ch; |
| 3542 | |
| 3543 | REQ(n, flow_stmt); |
| 3544 | ch = CHILD(n, 0); |
| 3545 | switch (TYPE(ch)) { |
| 3546 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3547 | return Break(LINENO(n), n->n_col_offset, |
| 3548 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3550 | return Continue(LINENO(n), n->n_col_offset, |
| 3551 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3552 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3553 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3554 | if (!exp) |
| 3555 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3556 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3557 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3558 | } |
| 3559 | case return_stmt: |
| 3560 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3561 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3562 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3563 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3564 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3565 | if (!expression) |
| 3566 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3567 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3568 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3569 | } |
| 3570 | case raise_stmt: |
| 3571 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3572 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3573 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3574 | else if (NCH(ch) >= 2) { |
| 3575 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3577 | if (!expression) |
| 3578 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3579 | if (NCH(ch) == 4) { |
| 3580 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3581 | if (!cause) |
| 3582 | return NULL; |
| 3583 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3584 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3585 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3586 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3587 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3588 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3589 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3590 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3591 | return NULL; |
| 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3596 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | { |
| 3598 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3599 | import_as_name: NAME ['as' NAME] |
| 3600 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3601 | dotted_name: NAME ('.' NAME)* |
| 3602 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3603 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3604 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3605 | loop: |
| 3606 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3607 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3608 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3609 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3610 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3611 | if (!name) |
| 3612 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3613 | if (NCH(n) == 3) { |
| 3614 | node *str_node = CHILD(n, 2); |
| 3615 | str = NEW_IDENTIFIER(str_node); |
| 3616 | if (!str) |
| 3617 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3618 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3619 | return NULL; |
| 3620 | } |
| 3621 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3622 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3623 | return NULL; |
| 3624 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3625 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3626 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3627 | case dotted_as_name: |
| 3628 | if (NCH(n) == 1) { |
| 3629 | n = CHILD(n, 0); |
| 3630 | goto loop; |
| 3631 | } |
| 3632 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3633 | node *asname_node = CHILD(n, 2); |
| 3634 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3635 | if (!a) |
| 3636 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3637 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3638 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3639 | if (!a->asname) |
| 3640 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3641 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3642 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3643 | return a; |
| 3644 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3646 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3647 | node *name_node = CHILD(n, 0); |
| 3648 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3649 | if (!name) |
| 3650 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3651 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3652 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3653 | return alias(name, NULL, c->c_arena); |
| 3654 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3655 | else { |
| 3656 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3657 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3658 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3659 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3660 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3661 | |
| 3662 | len = 0; |
| 3663 | for (i = 0; i < NCH(n); i += 2) |
| 3664 | /* length of string plus one for the dot */ |
| 3665 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3666 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3667 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | if (!str) |
| 3669 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3670 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3671 | if (!s) |
| 3672 | return NULL; |
| 3673 | for (i = 0; i < NCH(n); i += 2) { |
| 3674 | char *sch = STR(CHILD(n, i)); |
| 3675 | strcpy(s, STR(CHILD(n, i))); |
| 3676 | s += strlen(sch); |
| 3677 | *s++ = '.'; |
| 3678 | } |
| 3679 | --s; |
| 3680 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3682 | PyBytes_GET_SIZE(str), |
| 3683 | NULL); |
| 3684 | Py_DECREF(str); |
| 3685 | if (!uni) |
| 3686 | return NULL; |
| 3687 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3688 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3689 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3690 | Py_DECREF(str); |
| 3691 | return NULL; |
| 3692 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3693 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3694 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3695 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3696 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3697 | if (!str) |
| 3698 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3699 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3700 | Py_DECREF(str); |
| 3701 | return NULL; |
| 3702 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3703 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3704 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3705 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3706 | "unexpected import name: %d", TYPE(n)); |
| 3707 | return NULL; |
| 3708 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3709 | |
| 3710 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | return NULL; |
| 3712 | } |
| 3713 | |
| 3714 | static stmt_ty |
| 3715 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3716 | { |
| 3717 | /* |
| 3718 | import_stmt: import_name | import_from |
| 3719 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3720 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3721 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3722 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3723 | int lineno; |
| 3724 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | int i; |
| 3726 | asdl_seq *aliases; |
| 3727 | |
| 3728 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3729 | lineno = LINENO(n); |
| 3730 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3731 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3732 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3733 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3734 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3735 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3736 | if (!aliases) |
| 3737 | return NULL; |
| 3738 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3739 | 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] | 3740 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3741 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3742 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3743 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3744 | // Even though n is modified above, the end position is not changed |
| 3745 | return Import(aliases, lineno, col_offset, |
| 3746 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3747 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3748 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3749 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3750 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3751 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3752 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3753 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3755 | /* Count the number of dots (for relative imports) and check for the |
| 3756 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3757 | for (idx = 1; idx < NCH(n); idx++) { |
| 3758 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3759 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3760 | if (!mod) |
| 3761 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3762 | idx++; |
| 3763 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3764 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3766 | ndots += 3; |
| 3767 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3768 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3769 | break; |
| 3770 | } |
| 3771 | ndots++; |
| 3772 | } |
| 3773 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3774 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3775 | case STAR: |
| 3776 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3777 | n = CHILD(n, idx); |
| 3778 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3779 | break; |
| 3780 | case LPAR: |
| 3781 | /* from ... import (x, y, z) */ |
| 3782 | n = CHILD(n, idx + 1); |
| 3783 | n_children = NCH(n); |
| 3784 | break; |
| 3785 | case import_as_names: |
| 3786 | /* from ... import x, y, z */ |
| 3787 | n = CHILD(n, idx); |
| 3788 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3789 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3790 | ast_error(c, n, |
| 3791 | "trailing comma not allowed without" |
| 3792 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3793 | return NULL; |
| 3794 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3795 | break; |
| 3796 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3797 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3798 | return NULL; |
| 3799 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3800 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3801 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3802 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3803 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3804 | |
| 3805 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3806 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3807 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3808 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3809 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3810 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3811 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3812 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3813 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3814 | 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] | 3815 | if (!import_alias) |
| 3816 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3817 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3818 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3819 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3820 | if (mod != NULL) |
| 3821 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3822 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3823 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3824 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3825 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3826 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3827 | "unknown import statement: starts with command '%s'", |
| 3828 | STR(CHILD(n, 0))); |
| 3829 | return NULL; |
| 3830 | } |
| 3831 | |
| 3832 | static stmt_ty |
| 3833 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3834 | { |
| 3835 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3836 | identifier name; |
| 3837 | asdl_seq *s; |
| 3838 | int i; |
| 3839 | |
| 3840 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3841 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3842 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3843 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3844 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3845 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3846 | if (!name) |
| 3847 | return NULL; |
| 3848 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3849 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3850 | return Global(s, LINENO(n), n->n_col_offset, |
| 3851 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3855 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3856 | { |
| 3857 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3858 | identifier name; |
| 3859 | asdl_seq *s; |
| 3860 | int i; |
| 3861 | |
| 3862 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3863 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3864 | if (!s) |
| 3865 | return NULL; |
| 3866 | for (i = 1; i < NCH(n); i += 2) { |
| 3867 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3868 | if (!name) |
| 3869 | return NULL; |
| 3870 | asdl_seq_SET(s, i / 2, name); |
| 3871 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3872 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3873 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3877 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3878 | { |
| 3879 | /* assert_stmt: 'assert' test [',' test] */ |
| 3880 | REQ(n, assert_stmt); |
| 3881 | if (NCH(n) == 2) { |
| 3882 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3883 | if (!expression) |
| 3884 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3885 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3886 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3887 | } |
| 3888 | else if (NCH(n) == 4) { |
| 3889 | expr_ty expr1, expr2; |
| 3890 | |
| 3891 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3892 | if (!expr1) |
| 3893 | return NULL; |
| 3894 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3895 | if (!expr2) |
| 3896 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3897 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3898 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3899 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3900 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3901 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3902 | "improper number of parts to 'assert' statement: %d", |
| 3903 | NCH(n)); |
| 3904 | return NULL; |
| 3905 | } |
| 3906 | |
| 3907 | static asdl_seq * |
| 3908 | ast_for_suite(struct compiling *c, const node *n) |
| 3909 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3910 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3911 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3912 | stmt_ty s; |
| 3913 | int i, total, num, end, pos = 0; |
| 3914 | node *ch; |
| 3915 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3916 | if (TYPE(n) != func_body_suite) { |
| 3917 | REQ(n, suite); |
| 3918 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3919 | |
| 3920 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3921 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3922 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3923 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3924 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3925 | n = CHILD(n, 0); |
| 3926 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3927 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3928 | */ |
| 3929 | end = NCH(n) - 1; |
| 3930 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3931 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3932 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3933 | for (i = 0; i < end; i += 2) { |
| 3934 | ch = CHILD(n, i); |
| 3935 | s = ast_for_stmt(c, ch); |
| 3936 | if (!s) |
| 3937 | return NULL; |
| 3938 | asdl_seq_SET(seq, pos++, s); |
| 3939 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3940 | } |
| 3941 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3942 | i = 2; |
| 3943 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3944 | i += 2; |
| 3945 | REQ(CHILD(n, 2), NEWLINE); |
| 3946 | } |
| 3947 | |
| 3948 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3949 | ch = CHILD(n, i); |
| 3950 | REQ(ch, stmt); |
| 3951 | num = num_stmts(ch); |
| 3952 | if (num == 1) { |
| 3953 | /* small_stmt or compound_stmt with only one child */ |
| 3954 | s = ast_for_stmt(c, ch); |
| 3955 | if (!s) |
| 3956 | return NULL; |
| 3957 | asdl_seq_SET(seq, pos++, s); |
| 3958 | } |
| 3959 | else { |
| 3960 | int j; |
| 3961 | ch = CHILD(ch, 0); |
| 3962 | REQ(ch, simple_stmt); |
| 3963 | for (j = 0; j < NCH(ch); j += 2) { |
| 3964 | /* statement terminates with a semi-colon ';' */ |
| 3965 | if (NCH(CHILD(ch, j)) == 0) { |
| 3966 | assert((j + 1) == NCH(ch)); |
| 3967 | break; |
| 3968 | } |
| 3969 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3970 | if (!s) |
| 3971 | return NULL; |
| 3972 | asdl_seq_SET(seq, pos++, s); |
| 3973 | } |
| 3974 | } |
| 3975 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3976 | } |
| 3977 | assert(pos == seq->size); |
| 3978 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3981 | static void |
| 3982 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3983 | { |
Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame] | 3984 | Py_ssize_t tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3985 | // There must be no empty suites. |
| 3986 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3987 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3988 | *end_lineno = last->end_lineno; |
| 3989 | *end_col_offset = last->end_col_offset; |
| 3990 | } |
| 3991 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3992 | static stmt_ty |
| 3993 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3994 | { |
| 3995 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3996 | ['else' ':' suite] |
| 3997 | */ |
| 3998 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3999 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4000 | |
| 4001 | REQ(n, if_stmt); |
| 4002 | |
| 4003 | if (NCH(n) == 4) { |
| 4004 | expr_ty expression; |
| 4005 | asdl_seq *suite_seq; |
| 4006 | |
| 4007 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4008 | if (!expression) |
| 4009 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4011 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4012 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4013 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4014 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4015 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4016 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4017 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4018 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4019 | s = STR(CHILD(n, 4)); |
| 4020 | /* s[2], the third character in the string, will be |
| 4021 | 's' for el_s_e, or |
| 4022 | 'i' for el_i_f |
| 4023 | */ |
| 4024 | if (s[2] == 's') { |
| 4025 | expr_ty expression; |
| 4026 | asdl_seq *seq1, *seq2; |
| 4027 | |
| 4028 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4029 | if (!expression) |
| 4030 | return NULL; |
| 4031 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4032 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4033 | return NULL; |
| 4034 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4035 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4036 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4037 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4038 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4039 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4040 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4041 | } |
| 4042 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4043 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4044 | expr_ty expression; |
| 4045 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4046 | asdl_seq *orelse = NULL; |
| 4047 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4048 | /* must reference the child n_elif+1 since 'else' token is third, |
| 4049 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4050 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 4051 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 4052 | has_else = 1; |
| 4053 | n_elif -= 3; |
| 4054 | } |
| 4055 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4056 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4057 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4058 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4059 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4060 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4061 | if (!orelse) |
| 4062 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4063 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4064 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4065 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4066 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 4067 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4068 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4069 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4070 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4071 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4072 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4074 | asdl_seq_SET(orelse, 0, |
| 4075 | If(expression, suite_seq, suite_seq2, |
Miss Islington (bot) | ce333cd | 2019-12-14 02:43:42 -0800 | [diff] [blame] | 4076 | LINENO(CHILD(n, NCH(n) - 7)), |
| 4077 | CHILD(n, NCH(n) - 7)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4078 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4079 | /* the just-created orelse handled the last elif */ |
| 4080 | n_elif--; |
| 4081 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4082 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4083 | for (i = 0; i < n_elif; i++) { |
| 4084 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4085 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4086 | if (!newobj) |
| 4087 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4088 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4089 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4090 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4091 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4092 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4093 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4094 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4095 | if (orelse != NULL) { |
| 4096 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4097 | } else { |
| 4098 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4099 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4100 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | If(expression, suite_seq, orelse, |
Miss Islington (bot) | 3b18b17 | 2019-12-13 08:21:54 -0800 | [diff] [blame] | 4102 | LINENO(CHILD(n, off - 1)), |
| 4103 | CHILD(n, off - 1)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4104 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4105 | orelse = newobj; |
| 4106 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4107 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4108 | if (!expression) |
| 4109 | return NULL; |
| 4110 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 4111 | if (!suite_seq) |
| 4112 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4113 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4114 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4115 | LINENO(n), n->n_col_offset, |
| 4116 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4117 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4118 | |
| 4119 | PyErr_Format(PyExc_SystemError, |
| 4120 | "unexpected token in 'if' statement: %s", s); |
| 4121 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | static stmt_ty |
| 4125 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 4126 | { |
| 4127 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 4128 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4129 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4130 | |
| 4131 | if (NCH(n) == 4) { |
| 4132 | expr_ty expression; |
| 4133 | asdl_seq *suite_seq; |
| 4134 | |
| 4135 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4136 | if (!expression) |
| 4137 | return NULL; |
| 4138 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4139 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4140 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4141 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4142 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4143 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4144 | } |
| 4145 | else if (NCH(n) == 7) { |
| 4146 | expr_ty expression; |
| 4147 | asdl_seq *seq1, *seq2; |
| 4148 | |
| 4149 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4150 | if (!expression) |
| 4151 | return NULL; |
| 4152 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4153 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4154 | return NULL; |
| 4155 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4156 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4157 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4158 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4159 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4160 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4161 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4162 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4163 | |
| 4164 | PyErr_Format(PyExc_SystemError, |
| 4165 | "wrong number of tokens for 'while' statement: %d", |
| 4166 | NCH(n)); |
| 4167 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4171 | 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] | 4172 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4173 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4174 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4175 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4176 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4177 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4178 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4179 | int has_type_comment; |
| 4180 | string type_comment; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4181 | |
| 4182 | if (is_async && c->c_feature_version < 5) { |
| 4183 | ast_error(c, n, |
| 4184 | "Async for loops are only supported in Python 3.5 and greater"); |
| 4185 | return NULL; |
| 4186 | } |
| 4187 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4188 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4189 | REQ(n, for_stmt); |
| 4190 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4191 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4192 | |
| 4193 | if (NCH(n) == 9 + has_type_comment) { |
| 4194 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | if (!seq) |
| 4196 | return NULL; |
| 4197 | } |
| 4198 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4199 | node_target = CHILD(n, 1); |
| 4200 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4201 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4202 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4203 | /* Check the # of children rather than the length of _target, since |
| 4204 | 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] | 4205 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4206 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4207 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4208 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4209 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4210 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4211 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4212 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4213 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4214 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4215 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4216 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4217 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4218 | return NULL; |
| 4219 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4220 | if (seq != NULL) { |
| 4221 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4222 | } else { |
| 4223 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4224 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4225 | |
| 4226 | if (has_type_comment) { |
| 4227 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4228 | if (!type_comment) |
| 4229 | return NULL; |
| 4230 | } |
| 4231 | else |
| 4232 | type_comment = NULL; |
| 4233 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4234 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4235 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4236 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4237 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4238 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4239 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4240 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4241 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | static excepthandler_ty |
| 4245 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4246 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4247 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4248 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4249 | REQ(exc, except_clause); |
| 4250 | REQ(body, suite); |
| 4251 | |
| 4252 | if (NCH(exc) == 1) { |
| 4253 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4254 | if (!suite_seq) |
| 4255 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4256 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4257 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4258 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4259 | exc->n_col_offset, |
| 4260 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4261 | } |
| 4262 | else if (NCH(exc) == 2) { |
| 4263 | expr_ty expression; |
| 4264 | asdl_seq *suite_seq; |
| 4265 | |
| 4266 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4267 | if (!expression) |
| 4268 | return NULL; |
| 4269 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4270 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4271 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4272 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4273 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4274 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4275 | exc->n_col_offset, |
| 4276 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4277 | } |
| 4278 | else if (NCH(exc) == 4) { |
| 4279 | asdl_seq *suite_seq; |
| 4280 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4281 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4282 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4283 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4284 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4285 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4286 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4287 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4288 | return NULL; |
| 4289 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4290 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4291 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4292 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4293 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4294 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4295 | exc->n_col_offset, |
| 4296 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4297 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4298 | |
| 4299 | PyErr_Format(PyExc_SystemError, |
| 4300 | "wrong number of children for 'except' clause: %d", |
| 4301 | NCH(exc)); |
| 4302 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | static stmt_ty |
| 4306 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4307 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4308 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4309 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4310 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4311 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4312 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4313 | REQ(n, try_stmt); |
| 4314 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4315 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4316 | if (body == NULL) |
| 4317 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4318 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4319 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4320 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4321 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4322 | /* we can assume it's an "else", |
| 4323 | because nch >= 9 for try-else-finally and |
| 4324 | it would otherwise have a type of except_clause */ |
| 4325 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4326 | if (orelse == NULL) |
| 4327 | return NULL; |
| 4328 | n_except--; |
| 4329 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4330 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4331 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4332 | if (finally == NULL) |
| 4333 | return NULL; |
| 4334 | n_except--; |
| 4335 | } |
| 4336 | else { |
| 4337 | /* we can assume it's an "else", |
| 4338 | otherwise it would have a type of except_clause */ |
| 4339 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4340 | if (orelse == NULL) |
| 4341 | return NULL; |
| 4342 | n_except--; |
| 4343 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4344 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4345 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4346 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4347 | return NULL; |
| 4348 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4350 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4351 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4352 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4353 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4354 | if (handlers == NULL) |
| 4355 | return NULL; |
| 4356 | |
| 4357 | for (i = 0; i < n_except; i++) { |
| 4358 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4359 | CHILD(n, 5 + i * 3)); |
| 4360 | if (!e) |
| 4361 | return NULL; |
| 4362 | asdl_seq_SET(handlers, i, e); |
| 4363 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4366 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4367 | if (finally != NULL) { |
| 4368 | // finally is always last |
| 4369 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4370 | } else if (orelse != NULL) { |
| 4371 | // otherwise else is last |
| 4372 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4373 | } else { |
| 4374 | // inline the get_last_end_pos logic due to layout mismatch |
| 4375 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4376 | end_lineno = last_handler->end_lineno; |
| 4377 | end_col_offset = last_handler->end_col_offset; |
| 4378 | } |
| 4379 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4380 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4383 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4384 | static withitem_ty |
| 4385 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4386 | { |
| 4387 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4388 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4389 | REQ(n, with_item); |
| 4390 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4391 | if (!context_expr) |
| 4392 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4393 | if (NCH(n) == 3) { |
| 4394 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4395 | |
| 4396 | if (!optional_vars) { |
| 4397 | return NULL; |
| 4398 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4399 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4400 | return NULL; |
| 4401 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4404 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4407 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4408 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4409 | 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] | 4410 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4411 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4412 | 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] | 4413 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4414 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4415 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4416 | if (is_async && c->c_feature_version < 5) { |
| 4417 | ast_error(c, n, |
| 4418 | "Async with statements are only supported in Python 3.5 and greater"); |
| 4419 | return NULL; |
| 4420 | } |
| 4421 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4422 | REQ(n, with_stmt); |
| 4423 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4424 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4425 | nch_minus_type = NCH(n) - has_type_comment; |
| 4426 | |
| 4427 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4428 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4429 | if (!items) |
| 4430 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4431 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4432 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4433 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4434 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4435 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4438 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4439 | if (!body) |
| 4440 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4441 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4442 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4443 | if (has_type_comment) { |
| 4444 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4445 | if (!type_comment) |
| 4446 | return NULL; |
| 4447 | } |
| 4448 | else |
| 4449 | type_comment = NULL; |
| 4450 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4451 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4452 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4453 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4454 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4455 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4456 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4459 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4460 | 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] | 4461 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4462 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4463 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4464 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4465 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4466 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4467 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4468 | REQ(n, classdef); |
| 4469 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4470 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4471 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4472 | if (!s) |
| 4473 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4474 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4475 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4476 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4477 | if (!classname) |
| 4478 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4479 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4480 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4481 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4482 | LINENO(n), n->n_col_offset, |
| 4483 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4484 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4485 | |
| 4486 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4487 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4488 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4489 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4490 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4491 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4492 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4493 | if (!classname) |
| 4494 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4495 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4496 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4497 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4498 | LINENO(n), n->n_col_offset, |
| 4499 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4502 | /* class NAME '(' arglist ')' ':' suite */ |
| 4503 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4504 | { |
| 4505 | PyObject *dummy_name; |
| 4506 | expr_ty dummy; |
| 4507 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4508 | if (!dummy_name) |
| 4509 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4510 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4511 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4512 | c->c_arena); |
Miss Islington (bot) | 2076d4f | 2020-02-12 12:56:44 -0800 | [diff] [blame] | 4513 | call = ast_for_call(c, CHILD(n, 3), dummy, |
| 4514 | CHILD(n, 1), NULL, CHILD(n, 4)); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4515 | if (!call) |
| 4516 | return NULL; |
| 4517 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4518 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4519 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4520 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4521 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4522 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4523 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4524 | if (!classname) |
| 4525 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4526 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4527 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4528 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4529 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4530 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4531 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | static stmt_ty |
| 4535 | ast_for_stmt(struct compiling *c, const node *n) |
| 4536 | { |
| 4537 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4538 | assert(NCH(n) == 1); |
| 4539 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4540 | } |
| 4541 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4542 | assert(num_stmts(n) == 1); |
| 4543 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4544 | } |
| 4545 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4546 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4547 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4548 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4549 | */ |
| 4550 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4551 | case expr_stmt: |
| 4552 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4553 | case del_stmt: |
| 4554 | return ast_for_del_stmt(c, n); |
| 4555 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4556 | return Pass(LINENO(n), n->n_col_offset, |
| 4557 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4558 | case flow_stmt: |
| 4559 | return ast_for_flow_stmt(c, n); |
| 4560 | case import_stmt: |
| 4561 | return ast_for_import_stmt(c, n); |
| 4562 | case global_stmt: |
| 4563 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4564 | case nonlocal_stmt: |
| 4565 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4566 | case assert_stmt: |
| 4567 | return ast_for_assert_stmt(c, n); |
| 4568 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4569 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4570 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4571 | TYPE(n), NCH(n)); |
| 4572 | return NULL; |
| 4573 | } |
| 4574 | } |
| 4575 | else { |
| 4576 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4577 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4578 | */ |
| 4579 | node *ch = CHILD(n, 0); |
| 4580 | REQ(n, compound_stmt); |
| 4581 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4582 | case if_stmt: |
| 4583 | return ast_for_if_stmt(c, ch); |
| 4584 | case while_stmt: |
| 4585 | return ast_for_while_stmt(c, ch); |
| 4586 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4587 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4588 | case try_stmt: |
| 4589 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4590 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4591 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4592 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4593 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4594 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4595 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | case decorated: |
| 4597 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4598 | case async_stmt: |
| 4599 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4600 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4601 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4602 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4603 | TYPE(n), NCH(n)); |
| 4604 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4605 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4606 | } |
| 4607 | } |
| 4608 | |
| 4609 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4610 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4611 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4612 | const char *end; |
| 4613 | long x; |
| 4614 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4615 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4616 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4617 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4618 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4619 | errno = 0; |
| 4620 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4621 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4622 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4623 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4624 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4625 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4626 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4627 | } |
| 4628 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4629 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4630 | if (*end == '\0') { |
| 4631 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4632 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4633 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4634 | } |
| 4635 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4636 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4637 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4638 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4639 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4640 | return NULL; |
| 4641 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4642 | } |
| 4643 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4644 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4645 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4646 | if (dx == -1.0 && PyErr_Occurred()) |
| 4647 | return NULL; |
| 4648 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4649 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4653 | parsenumber(struct compiling *c, const char *s) |
| 4654 | { |
| 4655 | char *dup, *end; |
| 4656 | PyObject *res = NULL; |
| 4657 | |
| 4658 | assert(s != NULL); |
| 4659 | |
| 4660 | if (strchr(s, '_') == NULL) { |
| 4661 | return parsenumber_raw(c, s); |
| 4662 | } |
| 4663 | /* Create a duplicate without underscores. */ |
| 4664 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4665 | if (dup == NULL) { |
| 4666 | return PyErr_NoMemory(); |
| 4667 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4668 | end = dup; |
| 4669 | for (; *s; s++) { |
| 4670 | if (*s != '_') { |
| 4671 | *end++ = *s; |
| 4672 | } |
| 4673 | } |
| 4674 | *end = '\0'; |
| 4675 | res = parsenumber_raw(c, dup); |
| 4676 | PyMem_Free(dup); |
| 4677 | return res; |
| 4678 | } |
| 4679 | |
| 4680 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4681 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4682 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4683 | const char *s, *t; |
| 4684 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4685 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4686 | while (s < end && (*s & 0x80)) s++; |
| 4687 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4688 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4691 | static int |
| 4692 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4693 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4694 | { |
| 4695 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4696 | first_invalid_escape_char); |
| 4697 | if (msg == NULL) { |
| 4698 | return -1; |
| 4699 | } |
Serhiy Storchaka | 4c5b6ba | 2019-08-10 01:34:22 +0300 | [diff] [blame] | 4700 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4701 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4702 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4703 | { |
Serhiy Storchaka | 4c5b6ba | 2019-08-10 01:34:22 +0300 | [diff] [blame] | 4704 | if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) { |
| 4705 | /* Replace the DeprecationWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4706 | to get a more accurate error report */ |
| 4707 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4708 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4709 | } |
| 4710 | Py_DECREF(msg); |
| 4711 | return -1; |
| 4712 | } |
| 4713 | Py_DECREF(msg); |
| 4714 | return 0; |
| 4715 | } |
| 4716 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4717 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4718 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4719 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4720 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4721 | PyObject *v, *u; |
| 4722 | char *buf; |
| 4723 | char *p; |
| 4724 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4725 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4726 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4727 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4728 | return NULL; |
| 4729 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4730 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4731 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4732 | if (u == NULL) |
| 4733 | return NULL; |
| 4734 | p = buf = PyBytes_AsString(u); |
| 4735 | end = s + len; |
| 4736 | while (s < end) { |
| 4737 | if (*s == '\\') { |
| 4738 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4739 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4740 | strcpy(p, "u005c"); |
| 4741 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4742 | if (s >= end) |
| 4743 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4744 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4745 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4746 | if (*s & 0x80) { /* XXX inefficient */ |
| 4747 | PyObject *w; |
| 4748 | int kind; |
| 4749 | void *data; |
| 4750 | Py_ssize_t len, i; |
| 4751 | w = decode_utf8(c, &s, end); |
| 4752 | if (w == NULL) { |
| 4753 | Py_DECREF(u); |
| 4754 | return NULL; |
| 4755 | } |
| 4756 | kind = PyUnicode_KIND(w); |
| 4757 | data = PyUnicode_DATA(w); |
| 4758 | len = PyUnicode_GET_LENGTH(w); |
| 4759 | for (i = 0; i < len; i++) { |
| 4760 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4761 | sprintf(p, "\\U%08x", chr); |
| 4762 | p += 10; |
| 4763 | } |
| 4764 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4765 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4766 | Py_DECREF(w); |
| 4767 | } else { |
| 4768 | *p++ = *s++; |
| 4769 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4770 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4771 | len = p - buf; |
| 4772 | s = buf; |
| 4773 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4774 | const char *first_invalid_escape; |
| 4775 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4776 | |
| 4777 | if (v != NULL && first_invalid_escape != NULL) { |
| 4778 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4779 | /* We have not decref u before because first_invalid_escape points |
| 4780 | inside u. */ |
| 4781 | Py_XDECREF(u); |
| 4782 | Py_DECREF(v); |
| 4783 | return NULL; |
| 4784 | } |
| 4785 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4786 | Py_XDECREF(u); |
| 4787 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4790 | static PyObject * |
| 4791 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4792 | size_t len) |
| 4793 | { |
| 4794 | const char *first_invalid_escape; |
| 4795 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4796 | &first_invalid_escape); |
| 4797 | if (result == NULL) |
| 4798 | return NULL; |
| 4799 | |
| 4800 | if (first_invalid_escape != NULL) { |
| 4801 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4802 | Py_DECREF(result); |
| 4803 | return NULL; |
| 4804 | } |
| 4805 | } |
| 4806 | return result; |
| 4807 | } |
| 4808 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4809 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4810 | and `col_offset` to existing locations. */ |
| 4811 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4812 | { |
| 4813 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4814 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4815 | for (int i = 0; i < NCH(n); ++i) { |
| 4816 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4817 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4818 | col_offset = 0; |
| 4819 | } |
| 4820 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4821 | } |
| 4822 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4823 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | /* Fix locations for the given node and its children. |
| 4827 | |
| 4828 | `parent` is the enclosing node. |
| 4829 | `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] | 4830 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4831 | */ |
| 4832 | static void |
| 4833 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4834 | { |
| 4835 | char *substr = NULL; |
| 4836 | char *start; |
| 4837 | int lines = LINENO(parent) - 1; |
| 4838 | int cols = parent->n_col_offset; |
| 4839 | /* Find the full fstring to fix location information in `n`. */ |
| 4840 | while (parent && parent->n_type != STRING) |
| 4841 | parent = parent->n_child; |
| 4842 | if (parent && parent->n_str) { |
| 4843 | substr = strstr(parent->n_str, expr_str); |
| 4844 | if (substr) { |
| 4845 | start = substr; |
| 4846 | while (start > parent->n_str) { |
| 4847 | if (start[0] == '\n') |
| 4848 | break; |
| 4849 | start--; |
| 4850 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4851 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4852 | /* adjust the start based on the number of newlines encountered |
| 4853 | before the f-string expression */ |
| 4854 | for (char* p = parent->n_str; p < substr; p++) { |
| 4855 | if (*p == '\n') { |
| 4856 | lines++; |
| 4857 | } |
| 4858 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | fstring_shift_node_locations(n, lines, cols); |
| 4862 | } |
| 4863 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4864 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4865 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4866 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4867 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4868 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4869 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4870 | { |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4871 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4872 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4873 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4874 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4875 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4876 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4877 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4878 | assert(*(expr_start-1) == '{'); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4879 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' || |
| 4880 | *expr_end == '='); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4881 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4882 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4883 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4884 | because turning the expression '' in to '()' would go from being invalid |
| 4885 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4886 | for (s = expr_start; s != expr_end; s++) { |
| 4887 | char c = *s; |
| 4888 | /* The Python parser ignores only the following whitespace |
| 4889 | characters (\r already is converted to \n). */ |
| 4890 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4891 | break; |
| 4892 | } |
| 4893 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4894 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4895 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4896 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4897 | } |
| 4898 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4899 | len = expr_end - expr_start; |
| 4900 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4901 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4902 | if (str == NULL) { |
| 4903 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4904 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4905 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4906 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4907 | str[0] = '('; |
| 4908 | memcpy(str+1, expr_start, len); |
| 4909 | str[len+1] = ')'; |
| 4910 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4911 | |
Miss Islington (bot) | 92e836c | 2019-06-12 17:36:03 -0700 | [diff] [blame] | 4912 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4913 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4914 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4915 | Py_eval_input, 0); |
| 4916 | if (!mod_n) { |
| 4917 | PyMem_RawFree(str); |
| 4918 | return NULL; |
| 4919 | } |
| 4920 | /* Reuse str to find the correct column offset. */ |
| 4921 | str[0] = '{'; |
| 4922 | str[len+1] = '}'; |
| 4923 | fstring_fix_node_location(n, mod_n, str); |
| 4924 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4925 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4926 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4927 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4928 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4929 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | /* Return -1 on error. |
| 4933 | |
| 4934 | Return 0 if we reached the end of the literal. |
| 4935 | |
| 4936 | Return 1 if we haven't reached the end of the literal, but we want |
| 4937 | the caller to process the literal up to this point. Used for |
| 4938 | doubled braces. |
| 4939 | */ |
| 4940 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4941 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4942 | PyObject **literal, int recurse_lvl, |
| 4943 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4944 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4945 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4946 | brace (which isn't part of a unicode name escape such as |
| 4947 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4948 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4949 | const char *s = *str; |
| 4950 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4951 | int result = 0; |
| 4952 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4953 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4954 | while (s < end) { |
| 4955 | char ch = *s++; |
| 4956 | if (!raw && ch == '\\' && s < end) { |
| 4957 | ch = *s++; |
| 4958 | if (ch == 'N') { |
| 4959 | if (s < end && *s++ == '{') { |
| 4960 | while (s < end && *s++ != '}') { |
| 4961 | } |
| 4962 | continue; |
| 4963 | } |
| 4964 | break; |
| 4965 | } |
| 4966 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4967 | return -1; |
| 4968 | } |
| 4969 | } |
| 4970 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4971 | /* Check for doubled braces, but only at the top level. If |
| 4972 | we checked at every level, then f'{0:{3}}' would fail |
| 4973 | with the two closing braces. */ |
| 4974 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4975 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4976 | /* We're going to tell the caller that the literal ends |
| 4977 | here, but that they should continue scanning. But also |
| 4978 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4979 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4980 | result = 1; |
| 4981 | goto done; |
| 4982 | } |
| 4983 | |
| 4984 | /* Where a single '{' is the start of a new expression, a |
| 4985 | single '}' is not allowed. */ |
| 4986 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4987 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4988 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4989 | return -1; |
| 4990 | } |
| 4991 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4992 | /* We're either at a '{', which means we're starting another |
| 4993 | expression; or a '}', which means we're at the end of this |
| 4994 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4995 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4996 | break; |
| 4997 | } |
| 4998 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4999 | *str = s; |
| 5000 | assert(s <= end); |
| 5001 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5002 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 5003 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5004 | if (raw) |
| 5005 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 5006 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5007 | NULL, NULL); |
| 5008 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5009 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 5010 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5011 | if (!*literal) |
| 5012 | return -1; |
| 5013 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5014 | return result; |
| 5015 | } |
| 5016 | |
| 5017 | /* Forward declaration because parsing is recursive. */ |
| 5018 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5019 | 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] | 5020 | struct compiling *c, const node *n); |
| 5021 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5022 | /* Parse the f-string at *str, ending at end. We know *str starts an |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5023 | expression (so it must be a '{'). Returns the FormattedValue node, which |
| 5024 | includes the expression, conversion character, format_spec expression, and |
| 5025 | optionally the text of the expression (if = is used). |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5026 | |
| 5027 | Note that I don't do a perfect job here: I don't make sure that a |
| 5028 | closing brace doesn't match an opening paren, for example. It |
| 5029 | doesn't need to error on all invalid expressions, just correctly |
| 5030 | find the end of all valid ones. Any errors inside the expression |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5031 | will be caught when we parse it later. |
| 5032 | |
| 5033 | *expression is set to the expression. For an '=' "debug" expression, |
| 5034 | *expr_text is set to the debug text (the original text of the expression, |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5035 | including the '=' and any whitespace around it, as a string object). If |
| 5036 | not a debug expression, *expr_text set to NULL. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5037 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5038 | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5039 | PyObject **expr_text, expr_ty *expression, |
| 5040 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5041 | { |
| 5042 | /* Return -1 on error, else 0. */ |
| 5043 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5044 | const char *expr_start; |
| 5045 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5046 | expr_ty simple_expression; |
| 5047 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5048 | int conversion = -1; /* The conversion char. Use default if not |
| 5049 | specified, or !r if using = and no format |
| 5050 | spec. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5051 | |
| 5052 | /* 0 if we're not in a string, else the quote char we're trying to |
| 5053 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5054 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5055 | |
| 5056 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 5057 | int string_type = 0; |
| 5058 | |
| 5059 | /* Keep track of nesting level for braces/parens/brackets in |
| 5060 | expressions. */ |
| 5061 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5062 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5063 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5064 | *expr_text = NULL; |
| 5065 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5066 | /* Can only nest one level deep. */ |
| 5067 | if (recurse_lvl >= 2) { |
| 5068 | ast_error(c, n, "f-string: expressions nested too deeply"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5069 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | /* The first char must be a left brace, or we wouldn't have gotten |
| 5073 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5074 | assert(**str == '{'); |
| 5075 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5076 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5077 | expr_start = *str; |
| 5078 | for (; *str < end; (*str)++) { |
| 5079 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5080 | |
| 5081 | /* Loop invariants. */ |
| 5082 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5083 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5084 | if (quote_char) |
| 5085 | assert(string_type == 1 || string_type == 3); |
| 5086 | else |
| 5087 | assert(string_type == 0); |
| 5088 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5089 | ch = **str; |
| 5090 | /* Nowhere inside an expression is a backslash allowed. */ |
| 5091 | if (ch == '\\') { |
| 5092 | /* Error: can't include a backslash character, inside |
| 5093 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5094 | ast_error(c, n, |
| 5095 | "f-string expression part " |
| 5096 | "cannot include a backslash"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5097 | goto error; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5098 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5099 | if (quote_char) { |
| 5100 | /* We're inside a string. See if we're at the end. */ |
| 5101 | /* This code needs to implement the same non-error logic |
| 5102 | as tok_get from tokenizer.c, at the letter_quote |
| 5103 | label. To actually share that code would be a |
| 5104 | nightmare. But, it's unlikely to change and is small, |
| 5105 | so duplicate it here. Note we don't need to catch all |
| 5106 | of the errors, since they'll be caught when parsing the |
| 5107 | expression. We just need to match the non-error |
| 5108 | cases. Thus we can ignore \n in single-quoted strings, |
| 5109 | for example. Or non-terminated strings. */ |
| 5110 | if (ch == quote_char) { |
| 5111 | /* Does this match the string_type (single or triple |
| 5112 | quoted)? */ |
| 5113 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5114 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5115 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5116 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5117 | string_type = 0; |
| 5118 | quote_char = 0; |
| 5119 | continue; |
| 5120 | } |
| 5121 | } else { |
| 5122 | /* We're at the end of a normal string. */ |
| 5123 | quote_char = 0; |
| 5124 | string_type = 0; |
| 5125 | continue; |
| 5126 | } |
| 5127 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5128 | } else if (ch == '\'' || ch == '"') { |
| 5129 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5130 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5131 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5132 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5133 | } else { |
| 5134 | /* Start of a normal string. */ |
| 5135 | string_type = 1; |
| 5136 | } |
| 5137 | /* Start looking for the end of the string. */ |
| 5138 | quote_char = ch; |
| 5139 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5140 | if (nested_depth >= MAXLEVEL) { |
| 5141 | ast_error(c, n, "f-string: too many nested parenthesis"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5142 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5143 | } |
| 5144 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5145 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5146 | } else if (ch == '#') { |
| 5147 | /* Error: can't include a comment character, inside parens |
| 5148 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5149 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5150 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5151 | } else if (nested_depth == 0 && |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5152 | (ch == '!' || ch == ':' || ch == '}' || |
| 5153 | ch == '=' || ch == '>' || ch == '<')) { |
| 5154 | /* See if there's a next character. */ |
| 5155 | if (*str+1 < end) { |
| 5156 | char next = *(*str+1); |
| 5157 | |
| 5158 | /* For "!=". since '=' is not an allowed conversion character, |
| 5159 | nothing is lost in this test. */ |
| 5160 | if ((ch == '!' && next == '=') || /* != */ |
| 5161 | (ch == '=' && next == '=') || /* == */ |
| 5162 | (ch == '<' && next == '=') || /* <= */ |
| 5163 | (ch == '>' && next == '=') /* >= */ |
| 5164 | ) { |
| 5165 | *str += 1; |
| 5166 | continue; |
| 5167 | } |
| 5168 | /* Don't get out of the loop for these, if they're single |
| 5169 | chars (not part of 2-char tokens). If by themselves, they |
| 5170 | don't end an expression (unlike say '!'). */ |
| 5171 | if (ch == '>' || ch == '<') { |
| 5172 | continue; |
| 5173 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5174 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5175 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5176 | /* Normal way out of this loop. */ |
| 5177 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5178 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5179 | if (!nested_depth) { |
| 5180 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5181 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5182 | } |
| 5183 | nested_depth--; |
| 5184 | int opening = parenstack[nested_depth]; |
| 5185 | if (!((opening == '(' && ch == ')') || |
| 5186 | (opening == '[' && ch == ']') || |
| 5187 | (opening == '{' && ch == '}'))) |
| 5188 | { |
| 5189 | ast_error(c, n, |
| 5190 | "f-string: closing parenthesis '%c' " |
| 5191 | "does not match opening parenthesis '%c'", |
| 5192 | ch, opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5193 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5194 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5195 | } else { |
| 5196 | /* Just consume this char and loop around. */ |
| 5197 | } |
| 5198 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5199 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5200 | /* If we leave this loop in a string or with mismatched parens, we |
| 5201 | don't care. We'll get a syntax error when compiling the |
| 5202 | expression. But, we can produce a better error message, so |
| 5203 | let's just do that.*/ |
| 5204 | if (quote_char) { |
| 5205 | ast_error(c, n, "f-string: unterminated string"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5206 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5207 | } |
| 5208 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5209 | int opening = parenstack[nested_depth - 1]; |
| 5210 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5211 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5212 | } |
| 5213 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5214 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5215 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5216 | |
| 5217 | /* Compile the expression as soon as possible, so we show errors |
| 5218 | related to the expression before errors related to the |
| 5219 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5220 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5221 | if (!simple_expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5222 | goto error; |
| 5223 | |
| 5224 | /* Check for =, which puts the text value of the expression in |
| 5225 | expr_text. */ |
| 5226 | if (**str == '=') { |
Shantanu | f7ed4d4 | 2020-06-06 03:08:48 -0700 | [diff] [blame] | 5227 | if (c->c_feature_version < 8) { |
| 5228 | ast_error(c, n, |
| 5229 | "f-string: self documenting expressions are " |
| 5230 | "only supported in Python 3.8 and greater"); |
| 5231 | goto error; |
| 5232 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5233 | *str += 1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5234 | |
| 5235 | /* Skip over ASCII whitespace. No need to test for end of string |
| 5236 | here, since we know there's at least a trailing quote somewhere |
| 5237 | ahead. */ |
| 5238 | while (Py_ISSPACE(**str)) { |
| 5239 | *str += 1; |
| 5240 | } |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5241 | |
| 5242 | /* Set *expr_text to the text of the expression. */ |
| 5243 | *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); |
| 5244 | if (!*expr_text) { |
| 5245 | goto error; |
| 5246 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5247 | } |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5248 | |
| 5249 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5250 | if (**str == '!') { |
| 5251 | *str += 1; |
| 5252 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5253 | goto unexpected_end_of_string; |
| 5254 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5255 | conversion = **str; |
| 5256 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5257 | |
| 5258 | /* Validate the conversion. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5259 | if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5260 | ast_error(c, n, |
| 5261 | "f-string: invalid conversion character: " |
| 5262 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5263 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5264 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5265 | |
| 5266 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5267 | |
| 5268 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5269 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5270 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5271 | if (**str == ':') { |
| 5272 | *str += 1; |
| 5273 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5274 | goto unexpected_end_of_string; |
| 5275 | |
| 5276 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5277 | 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] | 5278 | if (!format_spec) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5279 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5280 | } |
| 5281 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5282 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5283 | goto unexpected_end_of_string; |
| 5284 | |
| 5285 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5286 | assert(*str < end); |
| 5287 | assert(**str == '}'); |
| 5288 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5289 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5290 | /* If we're in = mode (detected by non-NULL expr_text), and have no format |
| 5291 | spec and no explict conversion, set the conversion to 'r'. */ |
| 5292 | if (*expr_text && format_spec == NULL && conversion == -1) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5293 | conversion = 'r'; |
| 5294 | } |
| 5295 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5296 | /* And now create the FormattedValue node that represents this |
| 5297 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5298 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5299 | format_spec, LINENO(n), |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5300 | n->n_col_offset, n->n_end_lineno, |
| 5301 | n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5302 | if (!*expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5303 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5304 | |
| 5305 | return 0; |
| 5306 | |
| 5307 | unexpected_end_of_string: |
| 5308 | ast_error(c, n, "f-string: expecting '}'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5309 | /* Falls through to error. */ |
| 5310 | |
| 5311 | error: |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5312 | Py_XDECREF(*expr_text); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5313 | return -1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5314 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | /* Return -1 on error. |
| 5318 | |
| 5319 | Return 0 if we have a literal (possible zero length) and an |
| 5320 | expression (zero length if at the end of the string. |
| 5321 | |
| 5322 | Return 1 if we have a literal, but no expression, and we want the |
| 5323 | caller to call us again. This is used to deal with doubled |
| 5324 | braces. |
| 5325 | |
| 5326 | When called multiple times on the string 'a{{b{0}c', this function |
| 5327 | will return: |
| 5328 | |
| 5329 | 1. the literal 'a{' with no expression, and a return value |
| 5330 | of 1. Despite the fact that there's no expression, the return |
| 5331 | value of 1 means we're not finished yet. |
| 5332 | |
| 5333 | 2. the literal 'b' and the expression '0', with a return value of |
| 5334 | 0. The fact that there's an expression means we're not finished. |
| 5335 | |
| 5336 | 3. literal 'c' with no expression and a return value of 0. The |
| 5337 | combination of the return value of 0 with no expression means |
| 5338 | we're finished. |
| 5339 | */ |
| 5340 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5341 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5342 | int recurse_lvl, PyObject **literal, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5343 | PyObject **expr_text, expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5344 | struct compiling *c, const node *n) |
| 5345 | { |
| 5346 | int result; |
| 5347 | |
| 5348 | assert(*literal == NULL && *expression == NULL); |
| 5349 | |
| 5350 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5351 | 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] | 5352 | if (result < 0) |
| 5353 | goto error; |
| 5354 | |
| 5355 | assert(result == 0 || result == 1); |
| 5356 | |
| 5357 | if (result == 1) |
| 5358 | /* We have a literal, but don't look at the expression. */ |
| 5359 | return 1; |
| 5360 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5361 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5362 | /* We're at the end of the string or the end of a nested |
| 5363 | f-string: no expression. The top-level error case where we |
| 5364 | expect to be at the end of the string but we're at a '}' is |
| 5365 | handled later. */ |
| 5366 | return 0; |
| 5367 | |
| 5368 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5369 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5370 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5371 | if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text, |
| 5372 | expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5373 | goto error; |
| 5374 | |
| 5375 | return 0; |
| 5376 | |
| 5377 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5378 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5379 | return -1; |
| 5380 | } |
| 5381 | |
| 5382 | #define EXPRLIST_N_CACHED 64 |
| 5383 | |
| 5384 | typedef struct { |
| 5385 | /* Incrementally build an array of expr_ty, so be used in an |
| 5386 | asdl_seq. Cache some small but reasonably sized number of |
| 5387 | expr_ty's, and then after that start dynamically allocating, |
| 5388 | doubling the number allocated each time. Note that the f-string |
| 5389 | 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] | 5390 | Constant for the literal 'a'. So you add expr_ty's about twice as |
Miss Islington (bot) | 4bd1d05 | 2019-08-30 13:42:54 -0700 | [diff] [blame] | 5391 | fast as you add expressions in an f-string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5392 | |
| 5393 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5394 | Py_ssize_t size; /* Number we've used. */ |
| 5395 | expr_ty *p; /* Pointer to the memory we're actually |
| 5396 | using. Will point to 'data' until we |
| 5397 | start dynamically allocating. */ |
| 5398 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5399 | } ExprList; |
| 5400 | |
| 5401 | #ifdef NDEBUG |
| 5402 | #define ExprList_check_invariants(l) |
| 5403 | #else |
| 5404 | static void |
| 5405 | ExprList_check_invariants(ExprList *l) |
| 5406 | { |
| 5407 | /* Check our invariants. Make sure this object is "live", and |
| 5408 | hasn't been deallocated. */ |
| 5409 | assert(l->size >= 0); |
| 5410 | assert(l->p != NULL); |
| 5411 | if (l->size <= EXPRLIST_N_CACHED) |
| 5412 | assert(l->data == l->p); |
| 5413 | } |
| 5414 | #endif |
| 5415 | |
| 5416 | static void |
| 5417 | ExprList_Init(ExprList *l) |
| 5418 | { |
| 5419 | l->allocated = EXPRLIST_N_CACHED; |
| 5420 | l->size = 0; |
| 5421 | |
| 5422 | /* Until we start allocating dynamically, p points to data. */ |
| 5423 | l->p = l->data; |
| 5424 | |
| 5425 | ExprList_check_invariants(l); |
| 5426 | } |
| 5427 | |
| 5428 | static int |
| 5429 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5430 | { |
| 5431 | ExprList_check_invariants(l); |
| 5432 | if (l->size >= l->allocated) { |
| 5433 | /* We need to alloc (or realloc) the memory. */ |
| 5434 | Py_ssize_t new_size = l->allocated * 2; |
| 5435 | |
| 5436 | /* See if we've ever allocated anything dynamically. */ |
| 5437 | if (l->p == l->data) { |
| 5438 | Py_ssize_t i; |
| 5439 | /* We're still using the cached data. Switch to |
| 5440 | alloc-ing. */ |
| 5441 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5442 | if (!l->p) |
| 5443 | return -1; |
| 5444 | /* Copy the cached data into the new buffer. */ |
| 5445 | for (i = 0; i < l->size; i++) |
| 5446 | l->p[i] = l->data[i]; |
| 5447 | } else { |
| 5448 | /* Just realloc. */ |
| 5449 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5450 | if (!tmp) { |
| 5451 | PyMem_RawFree(l->p); |
| 5452 | l->p = NULL; |
| 5453 | return -1; |
| 5454 | } |
| 5455 | l->p = tmp; |
| 5456 | } |
| 5457 | |
| 5458 | l->allocated = new_size; |
| 5459 | assert(l->allocated == 2 * l->size); |
| 5460 | } |
| 5461 | |
| 5462 | l->p[l->size++] = exp; |
| 5463 | |
| 5464 | ExprList_check_invariants(l); |
| 5465 | return 0; |
| 5466 | } |
| 5467 | |
| 5468 | static void |
| 5469 | ExprList_Dealloc(ExprList *l) |
| 5470 | { |
| 5471 | ExprList_check_invariants(l); |
| 5472 | |
| 5473 | /* If there's been an error, or we've never dynamically allocated, |
| 5474 | do nothing. */ |
| 5475 | if (!l->p || l->p == l->data) { |
| 5476 | /* Do nothing. */ |
| 5477 | } else { |
| 5478 | /* We have dynamically allocated. Free the memory. */ |
| 5479 | PyMem_RawFree(l->p); |
| 5480 | } |
| 5481 | l->p = NULL; |
| 5482 | l->size = -1; |
| 5483 | } |
| 5484 | |
| 5485 | static asdl_seq * |
| 5486 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5487 | { |
| 5488 | asdl_seq *seq; |
| 5489 | |
| 5490 | ExprList_check_invariants(l); |
| 5491 | |
| 5492 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5493 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5494 | if (seq) { |
| 5495 | Py_ssize_t i; |
| 5496 | for (i = 0; i < l->size; i++) |
| 5497 | asdl_seq_SET(seq, i, l->p[i]); |
| 5498 | } |
| 5499 | ExprList_Dealloc(l); |
| 5500 | return seq; |
| 5501 | } |
| 5502 | |
| 5503 | /* The FstringParser is designed to add a mix of strings and |
| 5504 | f-strings, and concat them together as needed. Ultimately, it |
| 5505 | generates an expr_ty. */ |
| 5506 | typedef struct { |
| 5507 | PyObject *last_str; |
| 5508 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5509 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5510 | } FstringParser; |
| 5511 | |
| 5512 | #ifdef NDEBUG |
| 5513 | #define FstringParser_check_invariants(state) |
| 5514 | #else |
| 5515 | static void |
| 5516 | FstringParser_check_invariants(FstringParser *state) |
| 5517 | { |
| 5518 | if (state->last_str) |
| 5519 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5520 | ExprList_check_invariants(&state->expr_list); |
| 5521 | } |
| 5522 | #endif |
| 5523 | |
| 5524 | static void |
| 5525 | FstringParser_Init(FstringParser *state) |
| 5526 | { |
| 5527 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5528 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5529 | ExprList_Init(&state->expr_list); |
| 5530 | FstringParser_check_invariants(state); |
| 5531 | } |
| 5532 | |
| 5533 | static void |
| 5534 | FstringParser_Dealloc(FstringParser *state) |
| 5535 | { |
| 5536 | FstringParser_check_invariants(state); |
| 5537 | |
| 5538 | Py_XDECREF(state->last_str); |
| 5539 | ExprList_Dealloc(&state->expr_list); |
| 5540 | } |
| 5541 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5542 | /* Constants for the following */ |
| 5543 | static PyObject *u_kind; |
| 5544 | |
| 5545 | /* Compute 'kind' field for string Constant (either 'u' or None) */ |
| 5546 | static PyObject * |
| 5547 | make_kind(struct compiling *c, const node *n) |
| 5548 | { |
| 5549 | char *s = NULL; |
| 5550 | PyObject *kind = NULL; |
| 5551 | |
| 5552 | /* Find the first string literal, if any */ |
| 5553 | while (TYPE(n) != STRING) { |
| 5554 | if (NCH(n) == 0) |
| 5555 | return NULL; |
| 5556 | n = CHILD(n, 0); |
| 5557 | } |
| 5558 | REQ(n, STRING); |
| 5559 | |
| 5560 | /* If it starts with 'u', return a PyUnicode "u" string */ |
| 5561 | s = STR(n); |
| 5562 | if (s && *s == 'u') { |
| 5563 | if (!u_kind) { |
| 5564 | u_kind = PyUnicode_InternFromString("u"); |
| 5565 | if (!u_kind) |
| 5566 | return NULL; |
| 5567 | } |
| 5568 | kind = u_kind; |
| 5569 | if (PyArena_AddPyObject(c->c_arena, kind) < 0) { |
| 5570 | return NULL; |
| 5571 | } |
| 5572 | Py_INCREF(kind); |
| 5573 | } |
| 5574 | return kind; |
| 5575 | } |
| 5576 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5577 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5578 | static expr_ty |
| 5579 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5580 | { |
| 5581 | PyObject *s = *str; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5582 | PyObject *kind = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5583 | *str = NULL; |
| 5584 | assert(PyUnicode_CheckExact(s)); |
| 5585 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5586 | Py_DECREF(s); |
| 5587 | return NULL; |
| 5588 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5589 | kind = make_kind(c, n); |
| 5590 | if (kind == NULL && PyErr_Occurred()) |
| 5591 | return NULL; |
| 5592 | return Constant(s, kind, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5593 | 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] | 5594 | } |
| 5595 | |
| 5596 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5597 | decref'd. */ |
| 5598 | static int |
| 5599 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5600 | { |
| 5601 | FstringParser_check_invariants(state); |
| 5602 | |
| 5603 | assert(PyUnicode_CheckExact(str)); |
| 5604 | |
| 5605 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5606 | Py_DECREF(str); |
| 5607 | return 0; |
| 5608 | } |
| 5609 | |
| 5610 | if (!state->last_str) { |
| 5611 | /* We didn't have a string before, so just remember this one. */ |
| 5612 | state->last_str = str; |
| 5613 | } else { |
| 5614 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5615 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5616 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5617 | return -1; |
| 5618 | } |
| 5619 | FstringParser_check_invariants(state); |
| 5620 | return 0; |
| 5621 | } |
| 5622 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5623 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5624 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5625 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5626 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5627 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5628 | struct compiling *c, const node *n) |
| 5629 | { |
| 5630 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5631 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5632 | |
| 5633 | /* Parse the f-string. */ |
| 5634 | while (1) { |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5635 | PyObject *literal = NULL; |
| 5636 | PyObject *expr_text = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5637 | expr_ty expression = NULL; |
| 5638 | |
| 5639 | /* If there's a zero length literal in front of the |
| 5640 | expression, literal will be NULL. If we're at the end of |
| 5641 | the f-string, expression will be NULL (unless result == 1, |
| 5642 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5643 | int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5644 | &literal, &expr_text, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5645 | &expression, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5646 | if (result < 0) |
| 5647 | return -1; |
| 5648 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5649 | /* Add the literal, if any. */ |
| 5650 | if (literal && FstringParser_ConcatAndDel(state, literal) < 0) { |
| 5651 | Py_XDECREF(expr_text); |
| 5652 | return -1; |
| 5653 | } |
| 5654 | /* Add the expr_text, if any. */ |
| 5655 | if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) { |
| 5656 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5657 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5658 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5659 | /* We've dealt with the literal and expr_text, their ownership has |
| 5660 | been transferred to the state object. Don't look at them again. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5661 | |
| 5662 | /* See if we should just loop around to get the next literal |
| 5663 | and expression, while ignoring the expression this |
| 5664 | time. This is used for un-doubling braces, as an |
| 5665 | optimization. */ |
| 5666 | if (result == 1) |
| 5667 | continue; |
| 5668 | |
| 5669 | if (!expression) |
| 5670 | /* We're done with this f-string. */ |
| 5671 | break; |
| 5672 | |
| 5673 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5674 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5675 | if (!state->last_str) { |
| 5676 | /* Do nothing. No previous literal. */ |
| 5677 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5678 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5679 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5680 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5681 | return -1; |
| 5682 | } |
| 5683 | |
| 5684 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5685 | return -1; |
| 5686 | } |
| 5687 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5688 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5689 | string. Otherwise, we must be at a right brace. */ |
| 5690 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5691 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5692 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5693 | return -1; |
| 5694 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5695 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5696 | ast_error(c, n, "f-string: expecting '}'"); |
| 5697 | return -1; |
| 5698 | } |
| 5699 | |
| 5700 | FstringParser_check_invariants(state); |
| 5701 | return 0; |
| 5702 | } |
| 5703 | |
| 5704 | /* 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] | 5705 | 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] | 5706 | static expr_ty |
| 5707 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5708 | const node *n) |
| 5709 | { |
| 5710 | asdl_seq *seq; |
| 5711 | |
| 5712 | FstringParser_check_invariants(state); |
| 5713 | |
| 5714 | /* If we're just a constant string with no expressions, return |
| 5715 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5716 | if (!state->fmode) { |
| 5717 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5718 | if (!state->last_str) { |
| 5719 | /* Create a zero length string. */ |
| 5720 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5721 | if (!state->last_str) |
| 5722 | goto error; |
| 5723 | } |
| 5724 | return make_str_node_and_del(&state->last_str, c, n); |
| 5725 | } |
| 5726 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5727 | /* 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] | 5728 | last node in our expression list. */ |
| 5729 | if (state->last_str) { |
| 5730 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5731 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5732 | goto error; |
| 5733 | } |
| 5734 | /* This has already been freed. */ |
| 5735 | assert(state->last_str == NULL); |
| 5736 | |
| 5737 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5738 | if (!seq) |
| 5739 | goto error; |
| 5740 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5741 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5742 | 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] | 5743 | |
| 5744 | error: |
| 5745 | FstringParser_Dealloc(state); |
| 5746 | return NULL; |
| 5747 | } |
| 5748 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5749 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5750 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5751 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5752 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5753 | 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] | 5754 | struct compiling *c, const node *n) |
| 5755 | { |
| 5756 | FstringParser state; |
| 5757 | |
| 5758 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5759 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5760 | c, n) < 0) { |
| 5761 | FstringParser_Dealloc(&state); |
| 5762 | return NULL; |
| 5763 | } |
| 5764 | |
| 5765 | return FstringParser_Finish(&state, c, n); |
| 5766 | } |
| 5767 | |
| 5768 | /* n is a Python string literal, including the bracketing quote |
| 5769 | 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] | 5770 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5771 | 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] | 5772 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5773 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5774 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5775 | static int |
| 5776 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5777 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5778 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5779 | size_t len; |
| 5780 | const char *s = STR(n); |
| 5781 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5782 | int fmode = 0; |
| 5783 | *bytesmode = 0; |
| 5784 | *rawmode = 0; |
| 5785 | *result = NULL; |
| 5786 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5787 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5788 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5789 | if (quote == 'b' || quote == 'B') { |
| 5790 | quote = *++s; |
| 5791 | *bytesmode = 1; |
| 5792 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5793 | else if (quote == 'u' || quote == 'U') { |
| 5794 | quote = *++s; |
| 5795 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5796 | else if (quote == 'r' || quote == 'R') { |
| 5797 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5798 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5799 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5800 | else if (quote == 'f' || quote == 'F') { |
| 5801 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5802 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5803 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5804 | else { |
| 5805 | break; |
| 5806 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5807 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5808 | } |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 5809 | |
| 5810 | /* fstrings are only allowed in Python 3.6 and greater */ |
| 5811 | if (fmode && c->c_feature_version < 6) { |
| 5812 | ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); |
| 5813 | return -1; |
| 5814 | } |
| 5815 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5816 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5817 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5818 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5819 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5820 | if (quote != '\'' && quote != '\"') { |
| 5821 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5822 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5823 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5824 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5825 | s++; |
| 5826 | len = strlen(s); |
| 5827 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5828 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5829 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5830 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5831 | } |
| 5832 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5833 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5834 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5835 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5836 | } |
| 5837 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5838 | /* A triple quoted string. We've already skipped one quote at |
| 5839 | the start and one at the end of the string. Now skip the |
| 5840 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5841 | s += 2; |
| 5842 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5843 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5844 | if (s[--len] != quote || s[--len] != quote) { |
| 5845 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5846 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5847 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5848 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5849 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5850 | if (fmode) { |
| 5851 | /* Just return the bytes. The caller will parse the resulting |
| 5852 | string. */ |
| 5853 | *fstr = s; |
| 5854 | *fstrlen = len; |
| 5855 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5856 | } |
| 5857 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5858 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5859 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5860 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5861 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5862 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5863 | const char *ch; |
| 5864 | for (ch = s; *ch; ch++) { |
| 5865 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5866 | ast_error(c, n, |
| 5867 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5868 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5869 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5870 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5871 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5872 | if (*rawmode) |
| 5873 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5874 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5875 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5876 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5877 | if (*rawmode) |
| 5878 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5879 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5880 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5881 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5882 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5885 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5886 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5887 | 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] | 5888 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5889 | 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] | 5890 | node if there's just an f-string (with no leading or trailing |
| 5891 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5892 | any literals involved. */ |
| 5893 | static expr_ty |
| 5894 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5895 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5896 | int bytesmode = 0; |
| 5897 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5898 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5899 | |
| 5900 | FstringParser state; |
| 5901 | FstringParser_Init(&state); |
| 5902 | |
| 5903 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5904 | int this_bytesmode; |
| 5905 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5906 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5907 | const char *fstr; |
| 5908 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5909 | |
| 5910 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5911 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5912 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5913 | goto error; |
| 5914 | |
| 5915 | /* Check that we're not mixing bytes with unicode. */ |
| 5916 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5917 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5918 | /* s is NULL if the current string part is an f-string. */ |
| 5919 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5920 | goto error; |
| 5921 | } |
| 5922 | bytesmode = this_bytesmode; |
| 5923 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5924 | if (fstr != NULL) { |
| 5925 | int result; |
| 5926 | assert(s == NULL && !bytesmode); |
| 5927 | /* This is an f-string. Parse and concatenate it. */ |
| 5928 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5929 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5930 | if (result < 0) |
| 5931 | goto error; |
| 5932 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5933 | /* A string or byte string. */ |
| 5934 | assert(s != NULL && fstr == NULL); |
| 5935 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5936 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5937 | PyUnicode_CheckExact(s)); |
| 5938 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5939 | if (bytesmode) { |
| 5940 | /* For bytes, concat as we go. */ |
| 5941 | if (i == 0) { |
| 5942 | /* First time, just remember this value. */ |
| 5943 | bytes_str = s; |
| 5944 | } else { |
| 5945 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5946 | if (!bytes_str) |
| 5947 | goto error; |
| 5948 | } |
| 5949 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5950 | /* This is a regular string. Concatenate it. */ |
| 5951 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5952 | goto error; |
| 5953 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5954 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5955 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5956 | if (bytesmode) { |
| 5957 | /* Just return the bytes object and we're done. */ |
| 5958 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5959 | goto error; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5960 | return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5961 | 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] | 5962 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5963 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5964 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5965 | assert(bytes_str == NULL); |
| 5966 | |
| 5967 | return FstringParser_Finish(&state, c, n); |
| 5968 | |
| 5969 | error: |
| 5970 | Py_XDECREF(bytes_str); |
| 5971 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5972 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5973 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5974 | |
| 5975 | PyObject * |
| 5976 | _PyAST_GetDocString(asdl_seq *body) |
| 5977 | { |
| 5978 | if (!asdl_seq_LEN(body)) { |
| 5979 | return NULL; |
| 5980 | } |
| 5981 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5982 | if (st->kind != Expr_kind) { |
| 5983 | return NULL; |
| 5984 | } |
| 5985 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5986 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5987 | return e->v.Constant.value; |
| 5988 | } |
| 5989 | return NULL; |
| 5990 | } |