Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file includes functions to transform a concrete syntax tree (CST) to |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 | * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4 | * |
| 5 | */ |
| 6 | #include "Python.h" |
| 7 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 8 | #include "node.h" |
| 9 | #include "ast.h" |
| 10 | #include "token.h" |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 11 | #include "pythonrun.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 14 | #include <stdbool.h> |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 | |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 16 | #define MAXLEVEL 200 /* Max parentheses level */ |
| 17 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 18 | static int validate_stmts(asdl_seq *); |
| 19 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 20 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 21 | static int validate_stmt(stmt_ty); |
| 22 | static int validate_expr(expr_ty, expr_context_ty); |
| 23 | |
| 24 | static int |
| 25 | validate_comprehension(asdl_seq *gens) |
| 26 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 27 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 28 | if (!asdl_seq_LEN(gens)) { |
| 29 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 30 | return 0; |
| 31 | } |
| 32 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 33 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 34 | if (!validate_expr(comp->target, Store) || |
| 35 | !validate_expr(comp->iter, Load) || |
| 36 | !validate_exprs(comp->ifs, Load, 0)) |
| 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | validate_slice(slice_ty slice) |
| 44 | { |
| 45 | switch (slice->kind) { |
| 46 | case Slice_kind: |
| 47 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 48 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 49 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 50 | case ExtSlice_kind: { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 51 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 52 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 53 | return 0; |
| 54 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 55 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 56 | return 0; |
| 57 | return 1; |
| 58 | } |
| 59 | case Index_kind: |
| 60 | return validate_expr(slice->v.Index.value, Load); |
| 61 | default: |
| 62 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | validate_keywords(asdl_seq *keywords) |
| 69 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 70 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 71 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 72 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 73 | return 0; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | validate_args(asdl_seq *args) |
| 79 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 80 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 81 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 82 | arg_ty arg = asdl_seq_GET(args, i); |
| 83 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 84 | return 0; |
| 85 | } |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static const char * |
| 90 | expr_context_name(expr_context_ty ctx) |
| 91 | { |
| 92 | switch (ctx) { |
| 93 | case Load: |
| 94 | return "Load"; |
| 95 | case Store: |
| 96 | return "Store"; |
| 97 | case Del: |
| 98 | return "Del"; |
| 99 | case AugLoad: |
| 100 | return "AugLoad"; |
| 101 | case AugStore: |
| 102 | return "AugStore"; |
| 103 | case Param: |
| 104 | return "Param"; |
| 105 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 106 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | validate_arguments(arguments_ty args) |
| 112 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 113 | if (!validate_args(args->posonlyargs) || !validate_args(args->args)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 114 | return 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 115 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 116 | if (args->vararg && args->vararg->annotation |
| 117 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | if (!validate_args(args->kwonlyargs)) |
| 121 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 122 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 123 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 124 | return 0; |
| 125 | } |
Pablo Galindo | 2f58a84 | 2019-05-31 14:09:49 +0100 | [diff] [blame] | 126 | 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] | 127 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 128 | return 0; |
| 129 | } |
| 130 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 131 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 132 | "kw_defaults on arguments"); |
| 133 | return 0; |
| 134 | } |
| 135 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 136 | } |
| 137 | |
| 138 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 139 | validate_constant(PyObject *value) |
| 140 | { |
| 141 | if (value == Py_None || value == Py_Ellipsis) |
| 142 | return 1; |
| 143 | |
| 144 | if (PyLong_CheckExact(value) |
| 145 | || PyFloat_CheckExact(value) |
| 146 | || PyComplex_CheckExact(value) |
| 147 | || PyBool_Check(value) |
| 148 | || PyUnicode_CheckExact(value) |
| 149 | || PyBytes_CheckExact(value)) |
| 150 | return 1; |
| 151 | |
| 152 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 153 | PyObject *it; |
| 154 | |
| 155 | it = PyObject_GetIter(value); |
| 156 | if (it == NULL) |
| 157 | return 0; |
| 158 | |
| 159 | while (1) { |
| 160 | PyObject *item = PyIter_Next(it); |
| 161 | if (item == NULL) { |
| 162 | if (PyErr_Occurred()) { |
| 163 | Py_DECREF(it); |
| 164 | return 0; |
| 165 | } |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (!validate_constant(item)) { |
| 170 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 171 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 172 | return 0; |
| 173 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 174 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Py_DECREF(it); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 185 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 186 | { |
| 187 | int check_ctx = 1; |
| 188 | expr_context_ty actual_ctx; |
| 189 | |
| 190 | /* First check expression context. */ |
| 191 | switch (exp->kind) { |
| 192 | case Attribute_kind: |
| 193 | actual_ctx = exp->v.Attribute.ctx; |
| 194 | break; |
| 195 | case Subscript_kind: |
| 196 | actual_ctx = exp->v.Subscript.ctx; |
| 197 | break; |
| 198 | case Starred_kind: |
| 199 | actual_ctx = exp->v.Starred.ctx; |
| 200 | break; |
| 201 | case Name_kind: |
| 202 | actual_ctx = exp->v.Name.ctx; |
| 203 | break; |
| 204 | case List_kind: |
| 205 | actual_ctx = exp->v.List.ctx; |
| 206 | break; |
| 207 | case Tuple_kind: |
| 208 | actual_ctx = exp->v.Tuple.ctx; |
| 209 | break; |
| 210 | default: |
| 211 | if (ctx != Load) { |
| 212 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 213 | "assigned to in %s context", expr_context_name(ctx)); |
| 214 | return 0; |
| 215 | } |
| 216 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 217 | /* set actual_ctx to prevent gcc warning */ |
| 218 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 219 | } |
| 220 | if (check_ctx && actual_ctx != ctx) { |
| 221 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 222 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* Now validate expression. */ |
| 227 | switch (exp->kind) { |
| 228 | case BoolOp_kind: |
| 229 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 230 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 231 | return 0; |
| 232 | } |
| 233 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 234 | case BinOp_kind: |
| 235 | return validate_expr(exp->v.BinOp.left, Load) && |
| 236 | validate_expr(exp->v.BinOp.right, Load); |
| 237 | case UnaryOp_kind: |
| 238 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 239 | case Lambda_kind: |
| 240 | return validate_arguments(exp->v.Lambda.args) && |
| 241 | validate_expr(exp->v.Lambda.body, Load); |
| 242 | case IfExp_kind: |
| 243 | return validate_expr(exp->v.IfExp.test, Load) && |
| 244 | validate_expr(exp->v.IfExp.body, Load) && |
| 245 | validate_expr(exp->v.IfExp.orelse, Load); |
| 246 | case Dict_kind: |
| 247 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 248 | PyErr_SetString(PyExc_ValueError, |
| 249 | "Dict doesn't have the same number of keys as values"); |
| 250 | return 0; |
| 251 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 252 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 253 | dict literals, i.e. ``{**{a:b}}`` */ |
| 254 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 255 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 256 | case Set_kind: |
| 257 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 258 | #define COMP(NAME) \ |
| 259 | case NAME ## _kind: \ |
| 260 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 261 | validate_expr(exp->v.NAME.elt, Load); |
| 262 | COMP(ListComp) |
| 263 | COMP(SetComp) |
| 264 | COMP(GeneratorExp) |
| 265 | #undef COMP |
| 266 | case DictComp_kind: |
| 267 | return validate_comprehension(exp->v.DictComp.generators) && |
| 268 | validate_expr(exp->v.DictComp.key, Load) && |
| 269 | validate_expr(exp->v.DictComp.value, Load); |
| 270 | case Yield_kind: |
| 271 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 272 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 273 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 274 | case Await_kind: |
| 275 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 276 | case Compare_kind: |
| 277 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 278 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 279 | return 0; |
| 280 | } |
| 281 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 282 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 283 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 284 | "of comparators and operands"); |
| 285 | return 0; |
| 286 | } |
| 287 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 288 | validate_expr(exp->v.Compare.left, Load); |
| 289 | case Call_kind: |
| 290 | return validate_expr(exp->v.Call.func, Load) && |
| 291 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 292 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 293 | case Constant_kind: |
| 294 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 295 | PyErr_Format(PyExc_TypeError, |
| 296 | "got an invalid type in Constant: %s", |
| 297 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 301 | case JoinedStr_kind: |
| 302 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 303 | case FormattedValue_kind: |
| 304 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 305 | return 0; |
| 306 | if (exp->v.FormattedValue.format_spec) |
| 307 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 308 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 309 | case Attribute_kind: |
| 310 | return validate_expr(exp->v.Attribute.value, Load); |
| 311 | case Subscript_kind: |
| 312 | return validate_slice(exp->v.Subscript.slice) && |
| 313 | validate_expr(exp->v.Subscript.value, Load); |
| 314 | case Starred_kind: |
| 315 | return validate_expr(exp->v.Starred.value, ctx); |
| 316 | case List_kind: |
| 317 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 318 | case Tuple_kind: |
| 319 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 320 | case NamedExpr_kind: |
| 321 | return validate_expr(exp->v.NamedExpr.value, Load); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 322 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 323 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 324 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 325 | } |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 326 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 327 | return 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static int |
| 331 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 332 | { |
| 333 | if (asdl_seq_LEN(seq)) |
| 334 | return 1; |
| 335 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int |
| 340 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 341 | { |
| 342 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 343 | validate_exprs(targets, ctx, 0); |
| 344 | } |
| 345 | |
| 346 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 347 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 348 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 349 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int |
| 353 | validate_stmt(stmt_ty stmt) |
| 354 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 355 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 356 | switch (stmt->kind) { |
| 357 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 358 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 359 | validate_arguments(stmt->v.FunctionDef.args) && |
| 360 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 361 | (!stmt->v.FunctionDef.returns || |
| 362 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 363 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 364 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 365 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 366 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 367 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 368 | case Return_kind: |
| 369 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 370 | case Delete_kind: |
| 371 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 372 | case Assign_kind: |
| 373 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 374 | validate_expr(stmt->v.Assign.value, Load); |
| 375 | case AugAssign_kind: |
| 376 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 377 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 378 | case AnnAssign_kind: |
| 379 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 380 | stmt->v.AnnAssign.simple) { |
| 381 | PyErr_SetString(PyExc_TypeError, |
| 382 | "AnnAssign with simple non-Name target"); |
| 383 | return 0; |
| 384 | } |
| 385 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 386 | (!stmt->v.AnnAssign.value || |
| 387 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 388 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 389 | case For_kind: |
| 390 | return validate_expr(stmt->v.For.target, Store) && |
| 391 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 392 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 393 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 394 | case AsyncFor_kind: |
| 395 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 396 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 397 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 398 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 399 | case While_kind: |
| 400 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 401 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 402 | validate_stmts(stmt->v.While.orelse); |
| 403 | case If_kind: |
| 404 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 405 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 406 | validate_stmts(stmt->v.If.orelse); |
| 407 | case With_kind: |
| 408 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 409 | return 0; |
| 410 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 411 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 412 | if (!validate_expr(item->context_expr, Load) || |
| 413 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 414 | return 0; |
| 415 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 416 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 417 | case AsyncWith_kind: |
| 418 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 419 | return 0; |
| 420 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 421 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 422 | if (!validate_expr(item->context_expr, Load) || |
| 423 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 424 | return 0; |
| 425 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 426 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 427 | case Raise_kind: |
| 428 | if (stmt->v.Raise.exc) { |
| 429 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 430 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 431 | } |
| 432 | if (stmt->v.Raise.cause) { |
| 433 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 434 | return 0; |
| 435 | } |
| 436 | return 1; |
| 437 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 438 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 439 | return 0; |
| 440 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 441 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 442 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 443 | return 0; |
| 444 | } |
| 445 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 446 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 447 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 448 | return 0; |
| 449 | } |
| 450 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 451 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 452 | if ((handler->v.ExceptHandler.type && |
| 453 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 454 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 458 | validate_stmts(stmt->v.Try.finalbody)) && |
| 459 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 460 | validate_stmts(stmt->v.Try.orelse)); |
| 461 | case Assert_kind: |
| 462 | return validate_expr(stmt->v.Assert.test, Load) && |
| 463 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 464 | case Import_kind: |
| 465 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 466 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 467 | if (stmt->v.ImportFrom.level < 0) { |
| 468 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 472 | case Global_kind: |
| 473 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 474 | case Nonlocal_kind: |
| 475 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 476 | case Expr_kind: |
| 477 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 478 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 479 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 480 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 481 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 482 | (!stmt->v.AsyncFunctionDef.returns || |
| 483 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 484 | case Pass_kind: |
| 485 | case Break_kind: |
| 486 | case Continue_kind: |
| 487 | return 1; |
| 488 | default: |
| 489 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 490 | return 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | static int |
| 495 | validate_stmts(asdl_seq *seq) |
| 496 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 497 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 498 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 499 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 500 | if (stmt) { |
| 501 | if (!validate_stmt(stmt)) |
| 502 | return 0; |
| 503 | } |
| 504 | else { |
| 505 | PyErr_SetString(PyExc_ValueError, |
| 506 | "None disallowed in statement list"); |
| 507 | return 0; |
| 508 | } |
| 509 | } |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | static int |
| 514 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 515 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 516 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 517 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 518 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 519 | if (expr) { |
| 520 | if (!validate_expr(expr, ctx)) |
| 521 | return 0; |
| 522 | } |
| 523 | else if (!null_ok) { |
| 524 | PyErr_SetString(PyExc_ValueError, |
| 525 | "None disallowed in expression list"); |
| 526 | return 0; |
| 527 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 528 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 529 | } |
| 530 | return 1; |
| 531 | } |
| 532 | |
| 533 | int |
| 534 | PyAST_Validate(mod_ty mod) |
| 535 | { |
| 536 | int res = 0; |
| 537 | |
| 538 | switch (mod->kind) { |
| 539 | case Module_kind: |
| 540 | res = validate_stmts(mod->v.Module.body); |
| 541 | break; |
| 542 | case Interactive_kind: |
| 543 | res = validate_stmts(mod->v.Interactive.body); |
| 544 | break; |
| 545 | case Expression_kind: |
| 546 | res = validate_expr(mod->v.Expression.body, Load); |
| 547 | break; |
| 548 | case Suite_kind: |
| 549 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 550 | break; |
| 551 | default: |
| 552 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 553 | res = 0; |
| 554 | break; |
| 555 | } |
| 556 | return res; |
| 557 | } |
| 558 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 559 | /* 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] | 560 | #include "grammar.h" |
| 561 | #include "parsetok.h" |
| 562 | #include "graminit.h" |
| 563 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 564 | /* Data structure used internally */ |
| 565 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 566 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 567 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 568 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 569 | int c_feature_version; /* Latest minor version of Python for allowed features */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 573 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 574 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 575 | 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] | 576 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 577 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 578 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 579 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 580 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 581 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 582 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 583 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 584 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 585 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 586 | const node *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 587 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 588 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 589 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 590 | static void get_last_end_pos(asdl_seq *, int *, int *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 591 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 592 | #define COMP_GENEXP 0 |
| 593 | #define COMP_LISTCOMP 1 |
| 594 | #define COMP_SETCOMP 2 |
| 595 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 596 | static int |
| 597 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 598 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 599 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 600 | if (!m) |
| 601 | return 0; |
| 602 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 603 | Py_DECREF(m); |
| 604 | if (!c->c_normalize) |
| 605 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 606 | return 1; |
| 607 | } |
| 608 | |
| 609 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 610 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 611 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 612 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 613 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 614 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 615 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 616 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 617 | /* Check whether there are non-ASCII characters in the |
| 618 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 619 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 620 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 621 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 622 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 623 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 624 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 625 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 626 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 627 | if (form == NULL) { |
| 628 | Py_DECREF(id); |
| 629 | return NULL; |
| 630 | } |
| 631 | PyObject *args[2] = {form, id}; |
| 632 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 633 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 634 | if (!id2) |
| 635 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 636 | if (!PyUnicode_Check(id2)) { |
| 637 | PyErr_Format(PyExc_TypeError, |
| 638 | "unicodedata.normalize() must return a string, not " |
| 639 | "%.200s", |
| 640 | Py_TYPE(id2)->tp_name); |
| 641 | Py_DECREF(id2); |
| 642 | return NULL; |
| 643 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 644 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 645 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 646 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 647 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 648 | Py_DECREF(id); |
| 649 | return NULL; |
| 650 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 651 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 654 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 655 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 657 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 659 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 660 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 661 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 662 | va_start(va, errmsg); |
| 663 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 664 | va_end(va); |
| 665 | if (!errstr) { |
| 666 | return 0; |
| 667 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 668 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 669 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 670 | Py_INCREF(Py_None); |
| 671 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 672 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 673 | 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] | 674 | if (!tmp) { |
| 675 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 676 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 677 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 678 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 679 | Py_DECREF(errstr); |
| 680 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 681 | if (value) { |
| 682 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 683 | Py_DECREF(value); |
| 684 | } |
| 685 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* num_stmts() returns number of contained statements. |
| 689 | |
| 690 | Use this routine to determine how big a sequence is needed for |
| 691 | the statements in a parse tree. Its raison d'etre is this bit of |
| 692 | grammar: |
| 693 | |
| 694 | stmt: simple_stmt | compound_stmt |
| 695 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 696 | |
| 697 | A simple_stmt can contain multiple small_stmt elements joined |
| 698 | by semicolons. If the arg is a simple_stmt, the number of |
| 699 | small_stmt elements is returned. |
| 700 | */ |
| 701 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 702 | static string |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 703 | new_type_comment(const char *s, struct compiling *c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 704 | { |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 705 | PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); |
Guido van Rossum | 4b250fc | 2019-02-11 08:10:42 -0800 | [diff] [blame] | 706 | if (res == NULL) |
| 707 | return NULL; |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 708 | if (PyArena_AddPyObject(c->c_arena, res) < 0) { |
| 709 | Py_DECREF(res); |
| 710 | return NULL; |
| 711 | } |
| 712 | return res; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 713 | } |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 714 | #define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 715 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 716 | static int |
| 717 | num_stmts(const node *n) |
| 718 | { |
| 719 | int i, l; |
| 720 | node *ch; |
| 721 | |
| 722 | switch (TYPE(n)) { |
| 723 | case single_input: |
| 724 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 725 | return 0; |
| 726 | else |
| 727 | return num_stmts(CHILD(n, 0)); |
| 728 | case file_input: |
| 729 | l = 0; |
| 730 | for (i = 0; i < NCH(n); i++) { |
| 731 | ch = CHILD(n, i); |
| 732 | if (TYPE(ch) == stmt) |
| 733 | l += num_stmts(ch); |
| 734 | } |
| 735 | return l; |
| 736 | case stmt: |
| 737 | return num_stmts(CHILD(n, 0)); |
| 738 | case compound_stmt: |
| 739 | return 1; |
| 740 | case simple_stmt: |
| 741 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 742 | case suite: |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 743 | case func_body_suite: |
| 744 | /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
| 745 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 746 | if (NCH(n) == 1) |
| 747 | return num_stmts(CHILD(n, 0)); |
| 748 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 749 | i = 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 750 | l = 0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 751 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) |
| 752 | i += 2; |
| 753 | for (; i < (NCH(n) - 1); i++) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 754 | l += num_stmts(CHILD(n, i)); |
| 755 | return l; |
| 756 | } |
| 757 | default: { |
| 758 | char buf[128]; |
| 759 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 760 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 761 | TYPE(n), NCH(n)); |
| 762 | Py_FatalError(buf); |
| 763 | } |
| 764 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 765 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* Transform the CST rooted at node * to the appropriate AST |
| 769 | */ |
| 770 | |
| 771 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 772 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 773 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 774 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 775 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 | asdl_seq *stmts = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 777 | asdl_seq *type_ignores = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 778 | stmt_ty s; |
| 779 | node *ch; |
| 780 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 781 | mod_ty res = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 782 | asdl_seq *argtypes = NULL; |
| 783 | expr_ty ret, arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 785 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 786 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 787 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 788 | c.c_normalize = NULL; |
Guido van Rossum | 77f0ed7 | 2019-05-28 16:44:58 -0700 | [diff] [blame] | 789 | c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 790 | |
| 791 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 794 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | switch (TYPE(n)) { |
| 796 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 797 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 799 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 800 | for (i = 0; i < NCH(n) - 1; i++) { |
| 801 | ch = CHILD(n, i); |
| 802 | if (TYPE(ch) == NEWLINE) |
| 803 | continue; |
| 804 | REQ(ch, stmt); |
| 805 | num = num_stmts(ch); |
| 806 | if (num == 1) { |
| 807 | s = ast_for_stmt(&c, ch); |
| 808 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 809 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 810 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 811 | } |
| 812 | else { |
| 813 | ch = CHILD(ch, 0); |
| 814 | REQ(ch, simple_stmt); |
| 815 | for (j = 0; j < num; j++) { |
| 816 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 817 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 818 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 819 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 823 | |
| 824 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 825 | ch = CHILD(n, NCH(n) - 1); |
| 826 | REQ(ch, ENDMARKER); |
| 827 | num = NCH(ch); |
| 828 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 829 | if (!type_ignores) |
| 830 | goto out; |
| 831 | |
| 832 | for (i = 0; i < num; i++) { |
Michael J. Sullivan | 933e150 | 2019-05-22 07:54:20 -0700 | [diff] [blame] | 833 | string type_comment = new_type_comment(STR(CHILD(ch, i)), &c); |
| 834 | if (!type_comment) |
| 835 | goto out; |
| 836 | 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] | 837 | if (!ti) |
| 838 | goto out; |
| 839 | asdl_seq_SET(type_ignores, i, ti); |
| 840 | } |
| 841 | |
| 842 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 843 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | case eval_input: { |
| 845 | expr_ty testlist_ast; |
| 846 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 847 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 848 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 850 | goto out; |
| 851 | res = Expression(testlist_ast, arena); |
| 852 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 853 | } |
| 854 | case single_input: |
| 855 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 856 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 857 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 858 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 859 | 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] | 860 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 861 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 862 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 863 | goto out; |
| 864 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | } |
| 866 | else { |
| 867 | n = CHILD(n, 0); |
| 868 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 869 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 870 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 871 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 872 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 873 | s = ast_for_stmt(&c, n); |
| 874 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 875 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 876 | asdl_seq_SET(stmts, 0, s); |
| 877 | } |
| 878 | else { |
| 879 | /* Only a simple_stmt can contain multiple statements. */ |
| 880 | REQ(n, simple_stmt); |
| 881 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 882 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 883 | break; |
| 884 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 885 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 886 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 887 | asdl_seq_SET(stmts, i / 2, s); |
| 888 | } |
| 889 | } |
| 890 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 891 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 892 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 893 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 894 | case func_type_input: |
| 895 | n = CHILD(n, 0); |
| 896 | REQ(n, func_type); |
| 897 | |
| 898 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 899 | ch = CHILD(n, 1); |
| 900 | /* this is overly permissive -- we don't pay any attention to |
| 901 | * stars on the args -- just parse them into an ordered list */ |
| 902 | num = 0; |
| 903 | for (i = 0; i < NCH(ch); i++) { |
| 904 | if (TYPE(CHILD(ch, i)) == test) { |
| 905 | num++; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | argtypes = _Py_asdl_seq_new(num, arena); |
| 910 | if (!argtypes) |
| 911 | goto out; |
| 912 | |
| 913 | j = 0; |
| 914 | for (i = 0; i < NCH(ch); i++) { |
| 915 | if (TYPE(CHILD(ch, i)) == test) { |
| 916 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 917 | if (!arg) |
| 918 | goto out; |
| 919 | asdl_seq_SET(argtypes, j++, arg); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | else { |
| 924 | argtypes = _Py_asdl_seq_new(0, arena); |
| 925 | if (!argtypes) |
| 926 | goto out; |
| 927 | } |
| 928 | |
| 929 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 930 | if (!ret) |
| 931 | goto out; |
| 932 | res = FunctionType(argtypes, ret, arena); |
| 933 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 934 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 935 | PyErr_Format(PyExc_SystemError, |
| 936 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 937 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 938 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 939 | out: |
| 940 | if (c.c_normalize) { |
| 941 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 942 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 943 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 946 | mod_ty |
| 947 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 948 | PyArena *arena) |
| 949 | { |
| 950 | mod_ty mod; |
| 951 | PyObject *filename; |
| 952 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 953 | if (filename == NULL) |
| 954 | return NULL; |
| 955 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 956 | Py_DECREF(filename); |
| 957 | return mod; |
| 958 | |
| 959 | } |
| 960 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 961 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 962 | */ |
| 963 | |
| 964 | static operator_ty |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 965 | get_operator(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 966 | { |
| 967 | switch (TYPE(n)) { |
| 968 | case VBAR: |
| 969 | return BitOr; |
| 970 | case CIRCUMFLEX: |
| 971 | return BitXor; |
| 972 | case AMPER: |
| 973 | return BitAnd; |
| 974 | case LEFTSHIFT: |
| 975 | return LShift; |
| 976 | case RIGHTSHIFT: |
| 977 | return RShift; |
| 978 | case PLUS: |
| 979 | return Add; |
| 980 | case MINUS: |
| 981 | return Sub; |
| 982 | case STAR: |
| 983 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 984 | case AT: |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 985 | if (c->c_feature_version < 5) { |
| 986 | ast_error(c, n, |
| 987 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 988 | return (operator_ty)0; |
| 989 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 990 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 991 | case SLASH: |
| 992 | return Div; |
| 993 | case DOUBLESLASH: |
| 994 | return FloorDiv; |
| 995 | case PERCENT: |
| 996 | return Mod; |
| 997 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 998 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1002 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1003 | "None", |
| 1004 | "True", |
| 1005 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1006 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1007 | NULL, |
| 1008 | }; |
| 1009 | |
| 1010 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1011 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1012 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1013 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1014 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1015 | const char * const *p = FORBIDDEN; |
| 1016 | if (!full_checks) { |
| 1017 | /* In most cases, the parser will protect True, False, and None |
| 1018 | from being assign to. */ |
| 1019 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1020 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1021 | for (; *p; p++) { |
| 1022 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1023 | ast_error(c, n, "cannot assign to %U", name); |
| 1024 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1030 | static expr_ty |
| 1031 | copy_location(expr_ty e, const node *n) |
| 1032 | { |
| 1033 | if (e) { |
| 1034 | e->lineno = LINENO(n); |
| 1035 | e->col_offset = n->n_col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1036 | e->end_lineno = n->n_end_lineno; |
| 1037 | e->end_col_offset = n->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1038 | } |
| 1039 | return e; |
| 1040 | } |
| 1041 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1042 | static const char * |
| 1043 | get_expr_name(expr_ty e) |
| 1044 | { |
| 1045 | switch (e->kind) { |
| 1046 | case Attribute_kind: |
| 1047 | return "attribute"; |
| 1048 | case Subscript_kind: |
| 1049 | return "subscript"; |
| 1050 | case Starred_kind: |
| 1051 | return "starred"; |
| 1052 | case Name_kind: |
| 1053 | return "name"; |
| 1054 | case List_kind: |
| 1055 | return "list"; |
| 1056 | case Tuple_kind: |
| 1057 | return "tuple"; |
| 1058 | case Lambda_kind: |
| 1059 | return "lambda"; |
| 1060 | case Call_kind: |
| 1061 | return "function call"; |
| 1062 | case BoolOp_kind: |
| 1063 | case BinOp_kind: |
| 1064 | case UnaryOp_kind: |
| 1065 | return "operator"; |
| 1066 | case GeneratorExp_kind: |
| 1067 | return "generator expression"; |
| 1068 | case Yield_kind: |
| 1069 | case YieldFrom_kind: |
| 1070 | return "yield expression"; |
| 1071 | case Await_kind: |
| 1072 | return "await expression"; |
| 1073 | case ListComp_kind: |
| 1074 | return "list comprehension"; |
| 1075 | case SetComp_kind: |
| 1076 | return "set comprehension"; |
| 1077 | case DictComp_kind: |
| 1078 | return "dict comprehension"; |
| 1079 | case Dict_kind: |
| 1080 | return "dict display"; |
| 1081 | case Set_kind: |
| 1082 | return "set display"; |
| 1083 | case JoinedStr_kind: |
| 1084 | case FormattedValue_kind: |
| 1085 | return "f-string expression"; |
| 1086 | case Constant_kind: { |
| 1087 | PyObject *value = e->v.Constant.value; |
| 1088 | if (value == Py_None) { |
| 1089 | return "None"; |
| 1090 | } |
| 1091 | if (value == Py_False) { |
| 1092 | return "False"; |
| 1093 | } |
| 1094 | if (value == Py_True) { |
| 1095 | return "True"; |
| 1096 | } |
| 1097 | if (value == Py_Ellipsis) { |
| 1098 | return "Ellipsis"; |
| 1099 | } |
| 1100 | return "literal"; |
| 1101 | } |
| 1102 | case Compare_kind: |
| 1103 | return "comparison"; |
| 1104 | case IfExp_kind: |
| 1105 | return "conditional expression"; |
| 1106 | case NamedExpr_kind: |
| 1107 | return "named expression"; |
| 1108 | default: |
| 1109 | PyErr_Format(PyExc_SystemError, |
| 1110 | "unexpected expression in assignment %d (line %d)", |
| 1111 | e->kind, e->lineno); |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | } |
| 1115 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1116 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1117 | |
| 1118 | Only sets context for expr kinds that "can appear in assignment context" |
| 1119 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1120 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1121 | */ |
| 1122 | |
| 1123 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1124 | 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] | 1125 | { |
| 1126 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* The ast defines augmented store and load contexts, but the |
| 1129 | implementation here doesn't actually use them. The code may be |
| 1130 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1131 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1132 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1133 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1134 | */ |
| 1135 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1136 | |
| 1137 | switch (e->kind) { |
| 1138 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1139 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1140 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1141 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1142 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1144 | e->v.Subscript.ctx = ctx; |
| 1145 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1146 | case Starred_kind: |
| 1147 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1148 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1149 | return 0; |
| 1150 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1151 | case Name_kind: |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1152 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1153 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1154 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1155 | } |
| 1156 | e->v.Name.ctx = ctx; |
| 1157 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1158 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1159 | e->v.List.ctx = ctx; |
| 1160 | s = e->v.List.elts; |
| 1161 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1163 | e->v.Tuple.ctx = ctx; |
| 1164 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1165 | break; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1166 | default: { |
| 1167 | const char *expr_name = get_expr_name(e); |
| 1168 | if (expr_name != NULL) { |
| 1169 | ast_error(c, n, "cannot %s %s", |
| 1170 | ctx == Store ? "assign to" : "delete", |
| 1171 | expr_name); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1172 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1173 | return 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1174 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 | /* 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] | 1178 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1179 | */ |
| 1180 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1181 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1183 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1184 | 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] | 1185 | return 0; |
| 1186 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1187 | } |
| 1188 | return 1; |
| 1189 | } |
| 1190 | |
| 1191 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1192 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1193 | { |
| 1194 | REQ(n, augassign); |
| 1195 | n = CHILD(n, 0); |
| 1196 | switch (STR(n)[0]) { |
| 1197 | case '+': |
| 1198 | return Add; |
| 1199 | case '-': |
| 1200 | return Sub; |
| 1201 | case '/': |
| 1202 | if (STR(n)[1] == '/') |
| 1203 | return FloorDiv; |
| 1204 | else |
| 1205 | return Div; |
| 1206 | case '%': |
| 1207 | return Mod; |
| 1208 | case '<': |
| 1209 | return LShift; |
| 1210 | case '>': |
| 1211 | return RShift; |
| 1212 | case '&': |
| 1213 | return BitAnd; |
| 1214 | case '^': |
| 1215 | return BitXor; |
| 1216 | case '|': |
| 1217 | return BitOr; |
| 1218 | case '*': |
| 1219 | if (STR(n)[1] == '*') |
| 1220 | return Pow; |
| 1221 | else |
| 1222 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1223 | case '@': |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1224 | if (c->c_feature_version < 5) { |
| 1225 | ast_error(c, n, |
| 1226 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 1227 | return (operator_ty)0; |
| 1228 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1229 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1230 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1231 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1232 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1237 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1238 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1239 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1240 | |'is' 'not' |
| 1241 | */ |
| 1242 | REQ(n, comp_op); |
| 1243 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1244 | n = CHILD(n, 0); |
| 1245 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1246 | case LESS: |
| 1247 | return Lt; |
| 1248 | case GREATER: |
| 1249 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1250 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1251 | return Eq; |
| 1252 | case LESSEQUAL: |
| 1253 | return LtE; |
| 1254 | case GREATEREQUAL: |
| 1255 | return GtE; |
| 1256 | case NOTEQUAL: |
| 1257 | return NotEq; |
| 1258 | case NAME: |
| 1259 | if (strcmp(STR(n), "in") == 0) |
| 1260 | return In; |
| 1261 | if (strcmp(STR(n), "is") == 0) |
| 1262 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1263 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1264 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1265 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1266 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1267 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1268 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1269 | } |
| 1270 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1271 | /* handle "not in" and "is not" */ |
| 1272 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1273 | case NAME: |
| 1274 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1275 | return NotIn; |
| 1276 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1277 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1278 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1279 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1280 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1281 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1282 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1283 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1284 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1285 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1286 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1287 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static asdl_seq * |
| 1291 | seq_for_testlist(struct compiling *c, const node *n) |
| 1292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1294 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1295 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1296 | asdl_seq *seq; |
| 1297 | expr_ty expression; |
| 1298 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1299 | 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] | 1300 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1301 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1302 | if (!seq) |
| 1303 | return NULL; |
| 1304 | |
| 1305 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1307 | 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] | 1308 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1309 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1310 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1311 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1312 | |
| 1313 | assert(i / 2 < seq->size); |
| 1314 | asdl_seq_SET(seq, i / 2, expression); |
| 1315 | } |
| 1316 | return seq; |
| 1317 | } |
| 1318 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1319 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1320 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1321 | { |
| 1322 | identifier name; |
| 1323 | expr_ty annotation = NULL; |
| 1324 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1325 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1326 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1327 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1328 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1329 | name = NEW_IDENTIFIER(ch); |
| 1330 | if (!name) |
| 1331 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1332 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1333 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1334 | |
| 1335 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1336 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1337 | if (!annotation) |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1341 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1342 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1343 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1344 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1345 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1348 | /* returns -1 if failed to handle keyword only arguments |
| 1349 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1350 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1351 | ^^^ |
| 1352 | start pointing here |
| 1353 | */ |
| 1354 | static int |
| 1355 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1356 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1357 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1358 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1359 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1360 | expr_ty expression, annotation; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1361 | arg_ty arg = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1362 | int i = start; |
| 1363 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1366 | 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] | 1367 | return -1; |
| 1368 | } |
| 1369 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1370 | while (i < NCH(n)) { |
| 1371 | ch = CHILD(n, i); |
| 1372 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1373 | case vfpdef: |
| 1374 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1375 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1376 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1377 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1378 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1379 | asdl_seq_SET(kwdefaults, j, expression); |
| 1380 | i += 2; /* '=' and test */ |
| 1381 | } |
| 1382 | else { /* setting NULL if no default value exists */ |
| 1383 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1384 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1385 | if (NCH(ch) == 3) { |
| 1386 | /* ch is NAME ':' test */ |
| 1387 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1388 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1389 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1390 | } |
| 1391 | else { |
| 1392 | annotation = NULL; |
| 1393 | } |
| 1394 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1395 | argname = NEW_IDENTIFIER(ch); |
| 1396 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1397 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1398 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1399 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1400 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1401 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1402 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1403 | if (!arg) |
| 1404 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1405 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1406 | i += 1; /* the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1407 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1408 | i += 1; /* the comma, if present */ |
| 1409 | break; |
| 1410 | case TYPE_COMMENT: |
| 1411 | /* arg will be equal to the last argument processed */ |
| 1412 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1413 | if (!arg->type_comment) |
| 1414 | goto error; |
| 1415 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1416 | break; |
| 1417 | case DOUBLESTAR: |
| 1418 | return i; |
| 1419 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1420 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1421 | goto error; |
| 1422 | } |
| 1423 | } |
| 1424 | return i; |
| 1425 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1427 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1429 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1430 | |
| 1431 | static arguments_ty |
| 1432 | ast_for_arguments(struct compiling *c, const node *n) |
| 1433 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1434 | /* This function handles both typedargslist (function definition) |
| 1435 | and varargslist (lambda definition). |
| 1436 | |
| 1437 | parameters: '(' [typedargslist] ')' |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1438 | |
| 1439 | The following definition for typedarglist is equivalent to this set of rules: |
| 1440 | |
| 1441 | arguments = argument (',' [TYPE_COMMENT] argument)* |
| 1442 | argument = tfpdef ['=' test] |
| 1443 | kwargs = '**' tfpdef [','] [TYPE_COMMENT] |
| 1444 | args = '*' [tfpdef] |
| 1445 | kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' |
| 1446 | [TYPE_COMMENT] [kwargs]]) |
| 1447 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1448 | poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' |
| 1449 | [TYPE_COMMENT] [args_kwonly_kwargs]]) |
| 1450 | typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1451 | typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] |
| 1452 | typedargslist_no_posonly]])|(typedargslist_no_posonly)" |
| 1453 | |
| 1454 | typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1455 | ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ',' |
| 1456 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1457 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1458 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1459 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1460 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1461 | '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (',' |
| 1462 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1463 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1464 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1465 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1466 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1467 | '**' tfpdef [','] [TYPE_COMMENT])) |
| 1468 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1469 | tfpdef: NAME [':' test] |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1470 | |
| 1471 | The following definition for varargslist is equivalent to this set of rules: |
| 1472 | |
| 1473 | arguments = argument (',' argument )* |
| 1474 | argument = vfpdef ['=' test] |
| 1475 | kwargs = '**' vfpdef [','] |
| 1476 | args = '*' [vfpdef] |
| 1477 | kwonly_kwargs = (',' argument )* [',' [kwargs]] |
| 1478 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1479 | poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 1480 | vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1481 | varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | |
| 1482 | (vararglist_no_posonly) |
| 1483 | |
| 1484 | varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' |
| 1485 | test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' |
| 1486 | ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* |
| 1487 | [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef |
| 1488 | ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1489 | | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef |
| 1490 | [',']]] | '**' vfpdef [',']) |
| 1491 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1492 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1493 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1494 | */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1495 | int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1496 | int nposdefaults = 0, found_default = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1497 | asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1498 | arg_ty vararg = NULL, kwarg = NULL; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1499 | arg_ty arg = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1500 | node *ch; |
| 1501 | |
| 1502 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1503 | if (NCH(n) == 2) /* () as argument list */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1504 | 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] | 1505 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1507 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1509 | /* First count the number of positional args & defaults. The |
| 1510 | variable i is the loop index for this for loop and the next. |
| 1511 | The next loop picks up where the first leaves off. |
| 1512 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1513 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1514 | ch = CHILD(n, i); |
| 1515 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1516 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1517 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1518 | if (i < NCH(n) && /* skip argument following star */ |
| 1519 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1520 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1521 | i++; |
| 1522 | } |
| 1523 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1524 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1525 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1526 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1527 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1528 | if (TYPE(ch) == SLASH ) { |
| 1529 | nposonlyargs = nposargs; |
| 1530 | nposargs = 0; |
| 1531 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1534 | defaults for keyword only args */ |
| 1535 | for ( ; i < NCH(n); ++i) { |
| 1536 | ch = CHILD(n, i); |
| 1537 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1538 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1539 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1540 | posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL); |
| 1541 | if (!posonlyargs && nposonlyargs) { |
| 1542 | return NULL; |
| 1543 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1544 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1545 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1546 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1547 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1548 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1549 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1550 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1552 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1553 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1554 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1556 | since we set NULL as default for keyword only argument w/o default |
| 1557 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1558 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1559 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1560 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1561 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1562 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1563 | /* tfpdef: NAME [':' test] |
| 1564 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1565 | */ |
| 1566 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1567 | j = 0; /* index for defaults */ |
| 1568 | k = 0; /* index for args */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1569 | l = 0; /* index for posonlyargs */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1570 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1571 | ch = CHILD(n, i); |
| 1572 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1573 | case tfpdef: |
| 1574 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1575 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1576 | anything other than EQUAL or a comma? */ |
| 1577 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1578 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1579 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1580 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1581 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1582 | assert(posdefaults != NULL); |
| 1583 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1584 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1585 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1586 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1587 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1588 | ast_error(c, n, |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1589 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1590 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1591 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1592 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1593 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1594 | return NULL; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1595 | if (l < nposonlyargs) { |
| 1596 | asdl_seq_SET(posonlyargs, l++, arg); |
| 1597 | } else { |
| 1598 | asdl_seq_SET(posargs, k++, arg); |
| 1599 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1600 | i += 1; /* the name */ |
| 1601 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1602 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1603 | break; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1604 | case SLASH: |
| 1605 | /* Advance the slash and the comma. If there are more names |
| 1606 | * after the slash there will be a comma so we are advancing |
| 1607 | * the correct number of nodes. If the slash is the last item, |
| 1608 | * we will be advancing an extra token but then * i > NCH(n) |
| 1609 | * and the enclosing while will finish correctly. */ |
| 1610 | i += 2; |
| 1611 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1612 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1613 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1614 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1615 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1616 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1617 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1618 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1619 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1620 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1621 | if (TYPE(ch) == COMMA) { |
| 1622 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1623 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1624 | |
| 1625 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1626 | ast_error(c, CHILD(n, i), |
| 1627 | "bare * has associated type comment"); |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1631 | res = handle_keywordonly_args(c, n, i, |
| 1632 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1633 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1634 | i = res; /* res has new position to process */ |
| 1635 | } |
| 1636 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1637 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1638 | if (!vararg) |
| 1639 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1640 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1641 | i += 2; /* the star and the name */ |
| 1642 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1643 | i += 1; /* the comma, if present */ |
| 1644 | |
| 1645 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1646 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1647 | if (!vararg->type_comment) |
| 1648 | return NULL; |
| 1649 | i += 1; |
| 1650 | } |
| 1651 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1652 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1653 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1654 | int res = 0; |
| 1655 | res = handle_keywordonly_args(c, n, i, |
| 1656 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1657 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1658 | i = res; /* res has new position to process */ |
| 1659 | } |
| 1660 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1663 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1664 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1665 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1666 | if (!kwarg) |
| 1667 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1668 | i += 2; /* the double star and the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1669 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1670 | i += 1; /* the comma, if present */ |
| 1671 | break; |
| 1672 | case TYPE_COMMENT: |
| 1673 | assert(i); |
| 1674 | |
| 1675 | if (kwarg) |
| 1676 | arg = kwarg; |
| 1677 | |
| 1678 | /* arg will be equal to the last argument processed */ |
| 1679 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1680 | if (!arg->type_comment) |
| 1681 | return NULL; |
| 1682 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1685 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1686 | "unexpected node in varargslist: %d @ %d", |
| 1687 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1688 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1689 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1690 | } |
Miss Islington (bot) | cf9a63c | 2019-07-14 16:49:52 -0700 | [diff] [blame] | 1691 | return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | static expr_ty |
| 1695 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1696 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1697 | expr_ty e; |
| 1698 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1699 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1700 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1701 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1702 | |
| 1703 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1704 | |
| 1705 | lineno = LINENO(n); |
| 1706 | col_offset = n->n_col_offset; |
| 1707 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1708 | ch = CHILD(n, 0); |
| 1709 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1710 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1711 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1712 | e = Name(id, Load, lineno, col_offset, |
| 1713 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1714 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1715 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | |
| 1717 | for (i = 2; i < NCH(n); i+=2) { |
| 1718 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1719 | if (!id) |
| 1720 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1721 | e = Attribute(e, id, Load, lineno, col_offset, |
| 1722 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1723 | if (!e) |
| 1724 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static expr_ty |
| 1731 | ast_for_decorator(struct compiling *c, const node *n) |
| 1732 | { |
| 1733 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1734 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1735 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1737 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1738 | REQ(CHILD(n, 0), AT); |
| 1739 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1741 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1742 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1743 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1745 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1746 | d = name_expr; |
| 1747 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1748 | } |
| 1749 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Miss Skeleton (bot) | ba3a566 | 2019-10-26 07:06:40 -0700 | [diff] [blame^] | 1750 | d = Call(name_expr, NULL, NULL, |
| 1751 | name_expr->lineno, name_expr->col_offset, |
| 1752 | CHILD(n, 3)->n_end_lineno, CHILD(n, 3)->n_end_col_offset, |
| 1753 | c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1754 | if (!d) |
| 1755 | return NULL; |
| 1756 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1757 | } |
| 1758 | else { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1759 | d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1760 | if (!d) |
| 1761 | return NULL; |
| 1762 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | static asdl_seq* |
| 1769 | ast_for_decorators(struct compiling *c, const node *n) |
| 1770 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1771 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1772 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1773 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1775 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1776 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1777 | if (!decorator_seq) |
| 1778 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1780 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1781 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1782 | if (!d) |
| 1783 | return NULL; |
| 1784 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1785 | } |
| 1786 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1790 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1791 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1792 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1793 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1794 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1795 | identifier name; |
| 1796 | arguments_ty args; |
| 1797 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1798 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1799 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1800 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1801 | node *tc; |
| 1802 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1803 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1804 | if (is_async && c->c_feature_version < 5) { |
| 1805 | ast_error(c, n, |
| 1806 | "Async functions are only supported in Python 3.5 and greater"); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1810 | REQ(n, funcdef); |
| 1811 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1812 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1813 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1814 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1815 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1816 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1817 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1818 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1819 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1820 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1821 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1822 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1823 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1824 | name_i += 2; |
| 1825 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1826 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1827 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1828 | if (!type_comment) |
| 1829 | return NULL; |
| 1830 | name_i += 1; |
| 1831 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1832 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1834 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1835 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1837 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1838 | /* Check if the suite has a type comment in it. */ |
| 1839 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1840 | |
| 1841 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1842 | if (type_comment != NULL) { |
| 1843 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1844 | return NULL; |
| 1845 | } |
| 1846 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1847 | if (!type_comment) |
| 1848 | return NULL; |
| 1849 | } |
| 1850 | } |
| 1851 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1852 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1853 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1854 | 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] | 1855 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1856 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1857 | 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] | 1858 | } |
| 1859 | |
| 1860 | static stmt_ty |
| 1861 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1862 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1863 | /* async_funcdef: ASYNC funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1864 | REQ(n, async_funcdef); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1865 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1866 | REQ(CHILD(n, 1), funcdef); |
| 1867 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1868 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1869 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static stmt_ty |
| 1873 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1874 | { |
| 1875 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1876 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1877 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | |
| 1881 | static stmt_ty |
| 1882 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1883 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1884 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1885 | REQ(n, async_stmt); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1886 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1887 | |
| 1888 | switch (TYPE(CHILD(n, 1))) { |
| 1889 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1890 | return ast_for_funcdef_impl(c, n, NULL, |
| 1891 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1892 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1893 | return ast_for_with_stmt(c, n, |
| 1894 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1895 | |
| 1896 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1897 | return ast_for_for_stmt(c, n, |
| 1898 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1899 | |
| 1900 | default: |
| 1901 | PyErr_Format(PyExc_SystemError, |
| 1902 | "invalid async stament: %s", |
| 1903 | STR(CHILD(n, 1))); |
| 1904 | return NULL; |
| 1905 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1908 | static stmt_ty |
| 1909 | ast_for_decorated(struct compiling *c, const node *n) |
| 1910 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1911 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1912 | stmt_ty thing = NULL; |
| 1913 | asdl_seq *decorator_seq = NULL; |
| 1914 | |
| 1915 | REQ(n, decorated); |
| 1916 | |
| 1917 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1918 | if (!decorator_seq) |
| 1919 | return NULL; |
| 1920 | |
| 1921 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1922 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1924 | |
| 1925 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1926 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1927 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1928 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1929 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1930 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1931 | } |
| 1932 | return thing; |
| 1933 | } |
| 1934 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1935 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1936 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1937 | { |
| 1938 | /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* |
| 1939 | ['else' ':' suite] |
| 1940 | namedexpr_test: test [':=' test] |
| 1941 | argument: ( test [comp_for] | |
| 1942 | test ':=' test | |
| 1943 | test '=' test | |
| 1944 | '**' test | |
| 1945 | '*' test ) |
| 1946 | */ |
| 1947 | expr_ty target, value; |
| 1948 | |
| 1949 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1950 | if (!target) |
| 1951 | return NULL; |
| 1952 | |
| 1953 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1954 | if (!value) |
| 1955 | return NULL; |
| 1956 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1957 | if (target->kind != Name_kind) { |
| 1958 | const char *expr_name = get_expr_name(target); |
| 1959 | if (expr_name != NULL) { |
| 1960 | ast_error(c, n, "cannot use named assignment with %s", expr_name); |
| 1961 | } |
| 1962 | return NULL; |
| 1963 | } |
| 1964 | |
| 1965 | if (!set_context(c, target, Store, n)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1966 | return NULL; |
| 1967 | |
| 1968 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1969 | n->n_end_col_offset, c->c_arena); |
| 1970 | } |
| 1971 | |
| 1972 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1973 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1974 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1975 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1976 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1977 | arguments_ty args; |
| 1978 | expr_ty expression; |
| 1979 | |
| 1980 | if (NCH(n) == 3) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1981 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1982 | if (!args) |
| 1983 | return NULL; |
| 1984 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1985 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1986 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1987 | } |
| 1988 | else { |
| 1989 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1990 | if (!args) |
| 1991 | return NULL; |
| 1992 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1993 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1994 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1997 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1998 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2001 | static expr_ty |
| 2002 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 2003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2005 | expr_ty expression, body, orelse; |
| 2006 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 2007 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2008 | body = ast_for_expr(c, CHILD(n, 0)); |
| 2009 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2010 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2011 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 2012 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2013 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2014 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 2015 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2016 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2017 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2018 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2019 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2022 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2023 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2024 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2025 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2026 | */ |
| 2027 | |
| 2028 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2029 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2030 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2031 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2032 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2033 | count_comp_for: |
| 2034 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2035 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2036 | if (NCH(n) == 2) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2037 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2038 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2039 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2040 | else if (NCH(n) == 1) { |
| 2041 | n = CHILD(n, 0); |
| 2042 | } |
| 2043 | else { |
| 2044 | goto error; |
| 2045 | } |
| 2046 | if (NCH(n) == (5)) { |
| 2047 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2048 | } |
| 2049 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2050 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2051 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2052 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2053 | REQ(n, comp_iter); |
| 2054 | n = CHILD(n, 0); |
| 2055 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2056 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2057 | else if (TYPE(n) == comp_if) { |
| 2058 | if (NCH(n) == 3) { |
| 2059 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2060 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2061 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2062 | else |
| 2063 | return n_fors; |
| 2064 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2065 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2066 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2067 | /* Should never be reached */ |
| 2068 | PyErr_SetString(PyExc_SystemError, |
| 2069 | "logic error in count_comp_fors"); |
| 2070 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2073 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2074 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2075 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2076 | */ |
| 2077 | |
| 2078 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2079 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2080 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2081 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2082 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2083 | while (1) { |
| 2084 | REQ(n, comp_iter); |
| 2085 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2086 | return n_ifs; |
| 2087 | n = CHILD(n, 0); |
| 2088 | REQ(n, comp_if); |
| 2089 | n_ifs++; |
| 2090 | if (NCH(n) == 2) |
| 2091 | return n_ifs; |
| 2092 | n = CHILD(n, 2); |
| 2093 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2096 | static asdl_seq * |
| 2097 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2098 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2099 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2100 | asdl_seq *comps; |
| 2101 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2102 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2103 | if (n_fors == -1) |
| 2104 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2106 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2107 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2108 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2109 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2110 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2111 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2112 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2113 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2114 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2115 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2116 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2118 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2120 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2121 | is_async = 1; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2122 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2123 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2124 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2125 | else { |
| 2126 | sync_n = CHILD(n, 0); |
| 2127 | } |
| 2128 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2129 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2130 | /* Async comprehensions only allowed in Python 3.6 and greater */ |
| 2131 | if (is_async && c->c_feature_version < 6) { |
| 2132 | ast_error(c, n, |
| 2133 | "Async comprehensions are only supported in Python 3.6 and greater"); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2137 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2138 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2139 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2140 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2141 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2142 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2143 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2144 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2145 | /* Check the # of children rather than the length of t, since |
| 2146 | (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] | 2147 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2148 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2149 | comp = comprehension(first, expression, NULL, |
| 2150 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2151 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2152 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2153 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2154 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2155 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2156 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2157 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2158 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2159 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2160 | int j, n_ifs; |
| 2161 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2163 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2164 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2165 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2166 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2168 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2169 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2170 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2171 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2172 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2173 | REQ(n, comp_iter); |
| 2174 | n = CHILD(n, 0); |
| 2175 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2177 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2178 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2179 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2180 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2181 | if (NCH(n) == 3) |
| 2182 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2183 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2184 | /* on exit, must guarantee that n is a comp_for */ |
| 2185 | if (TYPE(n) == comp_iter) |
| 2186 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2187 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2188 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2189 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2190 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2191 | return comps; |
| 2192 | } |
| 2193 | |
| 2194 | static expr_ty |
| 2195 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2196 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2197 | /* testlist_comp: (test|star_expr) |
| 2198 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2199 | expr_ty elt; |
| 2200 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2201 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2203 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2205 | ch = CHILD(n, 0); |
| 2206 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2207 | if (!elt) |
| 2208 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2209 | if (elt->kind == Starred_kind) { |
| 2210 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2211 | return NULL; |
| 2212 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2214 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2215 | if (!comps) |
| 2216 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2217 | |
| 2218 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2219 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2220 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2221 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2222 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2223 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2224 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2225 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2226 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2227 | else |
| 2228 | /* Should never happen */ |
| 2229 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2232 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2233 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2234 | * elements. Iff successful, nonzero is returned. |
| 2235 | */ |
| 2236 | static int |
| 2237 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2238 | expr_ty *key, expr_ty *value) |
| 2239 | { |
| 2240 | expr_ty expression; |
| 2241 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2242 | assert(NCH(n) - *i >= 2); |
| 2243 | |
| 2244 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2245 | if (!expression) |
| 2246 | return 0; |
| 2247 | *key = NULL; |
| 2248 | *value = expression; |
| 2249 | |
| 2250 | *i += 2; |
| 2251 | } |
| 2252 | else { |
| 2253 | assert(NCH(n) - *i >= 3); |
| 2254 | |
| 2255 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2256 | if (!expression) |
| 2257 | return 0; |
| 2258 | *key = expression; |
| 2259 | |
| 2260 | REQ(CHILD(n, *i + 1), COLON); |
| 2261 | |
| 2262 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2263 | if (!expression) |
| 2264 | return 0; |
| 2265 | *value = expression; |
| 2266 | |
| 2267 | *i += 3; |
| 2268 | } |
| 2269 | return 1; |
| 2270 | } |
| 2271 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2272 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2273 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2274 | { |
| 2275 | expr_ty key, value; |
| 2276 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2277 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2279 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2280 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2281 | assert(key); |
| 2282 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2284 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2285 | if (!comps) |
| 2286 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2288 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2289 | 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] | 2290 | } |
| 2291 | |
| 2292 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2293 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2294 | { |
| 2295 | int i; |
| 2296 | int j; |
| 2297 | int size; |
| 2298 | asdl_seq *keys, *values; |
| 2299 | |
| 2300 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2301 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2302 | if (!keys) |
| 2303 | return NULL; |
| 2304 | |
| 2305 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2306 | if (!values) |
| 2307 | return NULL; |
| 2308 | |
| 2309 | j = 0; |
| 2310 | for (i = 0; i < NCH(n); i++) { |
| 2311 | expr_ty key, value; |
| 2312 | |
| 2313 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2314 | return NULL; |
| 2315 | asdl_seq_SET(keys, j, key); |
| 2316 | asdl_seq_SET(values, j, value); |
| 2317 | |
| 2318 | j++; |
| 2319 | } |
| 2320 | keys->size = j; |
| 2321 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2322 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2323 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2327 | ast_for_genexp(struct compiling *c, const node *n) |
| 2328 | { |
| 2329 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2330 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | static expr_ty |
| 2334 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2335 | { |
| 2336 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2337 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | static expr_ty |
| 2341 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2342 | { |
| 2343 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2344 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2347 | static expr_ty |
| 2348 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2349 | { |
| 2350 | int i; |
| 2351 | int size; |
| 2352 | asdl_seq *elts; |
| 2353 | |
| 2354 | assert(TYPE(n) == (dictorsetmaker)); |
| 2355 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2356 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2357 | if (!elts) |
| 2358 | return NULL; |
| 2359 | for (i = 0; i < NCH(n); i += 2) { |
| 2360 | expr_ty expression; |
| 2361 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2362 | if (!expression) |
| 2363 | return NULL; |
| 2364 | asdl_seq_SET(elts, i / 2, expression); |
| 2365 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2366 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2367 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2368 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2369 | |
| 2370 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2371 | ast_for_atom(struct compiling *c, const node *n) |
| 2372 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2373 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2374 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2375 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2376 | */ |
| 2377 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2379 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2380 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2381 | PyObject *name; |
| 2382 | const char *s = STR(ch); |
| 2383 | size_t len = strlen(s); |
| 2384 | if (len >= 4 && len <= 5) { |
| 2385 | if (!strcmp(s, "None")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2386 | return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2387 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2388 | if (!strcmp(s, "True")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2389 | return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2390 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2391 | if (!strcmp(s, "False")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2392 | return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2393 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2394 | } |
| 2395 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2396 | if (!name) |
| 2397 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2398 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2399 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2400 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2401 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2402 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2403 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2404 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2405 | const char *errtype = NULL; |
| 2406 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2407 | errtype = "unicode error"; |
| 2408 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2409 | errtype = "value error"; |
| 2410 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2411 | PyObject *type, *value, *tback, *errstr; |
| 2412 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2413 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2414 | if (errstr) { |
| 2415 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2416 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2417 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2418 | else { |
| 2419 | PyErr_Clear(); |
| 2420 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2421 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2422 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2423 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2424 | Py_XDECREF(tback); |
| 2425 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2426 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2427 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2428 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2429 | } |
| 2430 | case NUMBER: { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2431 | PyObject *pynum; |
| 2432 | /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ |
| 2433 | /* Check for underscores here rather than in parse_number so we can report a line number on error */ |
| 2434 | if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { |
| 2435 | ast_error(c, ch, |
| 2436 | "Underscores in numeric literals are only supported in Python 3.6 and greater"); |
| 2437 | return NULL; |
| 2438 | } |
| 2439 | pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2440 | if (!pynum) |
| 2441 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2442 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2443 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2444 | Py_DECREF(pynum); |
| 2445 | return NULL; |
| 2446 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2447 | return Constant(pynum, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2448 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2449 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2450 | case ELLIPSIS: /* Ellipsis */ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2451 | return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2452 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2453 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2454 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2456 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2457 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2458 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2460 | if (TYPE(ch) == yield_expr) |
| 2461 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2464 | if (NCH(ch) == 1) { |
| 2465 | return ast_for_testlist(c, ch); |
| 2466 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2467 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2468 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2469 | return copy_location(ast_for_genexp(c, ch), n); |
| 2470 | } |
| 2471 | else { |
| 2472 | return copy_location(ast_for_testlist(c, ch), n); |
| 2473 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2474 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2475 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2477 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2478 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2479 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2481 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2482 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2483 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2484 | if (!elts) |
| 2485 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2486 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2487 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2488 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2489 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2490 | else { |
| 2491 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2492 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2493 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2494 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2495 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2496 | * ((test | '*' test) |
| 2497 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2498 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2499 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2500 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2501 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2502 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2503 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2504 | } |
| 2505 | else { |
| 2506 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2507 | if (NCH(ch) == 1 || |
| 2508 | (NCH(ch) > 1 && |
| 2509 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2510 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2511 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2512 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2513 | else if (NCH(ch) > 1 && |
| 2514 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2515 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2516 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2517 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2518 | else if (NCH(ch) > 3 - is_dict && |
| 2519 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2520 | /* It's a dictionary comprehension. */ |
| 2521 | if (is_dict) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2522 | ast_error(c, n, |
| 2523 | "dict unpacking cannot be used in dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2524 | return NULL; |
| 2525 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2526 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2527 | } |
| 2528 | else { |
| 2529 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2530 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2531 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2532 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2533 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2534 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2535 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2536 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2537 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | static slice_ty |
| 2542 | ast_for_slice(struct compiling *c, const node *n) |
| 2543 | { |
| 2544 | node *ch; |
| 2545 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2546 | |
| 2547 | REQ(n, subscript); |
| 2548 | |
| 2549 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2550 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2551 | sliceop: ':' [test] |
| 2552 | */ |
| 2553 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2554 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2555 | /* 'step' variable hold no significance in terms of being used over |
| 2556 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2558 | if (!step) |
| 2559 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2561 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2565 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2566 | if (!lower) |
| 2567 | return NULL; |
| 2568 | } |
| 2569 | |
| 2570 | /* If there's an upper bound it's in the second or third position. */ |
| 2571 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2572 | if (NCH(n) > 1) { |
| 2573 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2574 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2575 | if (TYPE(n2) == test) { |
| 2576 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2577 | if (!upper) |
| 2578 | return NULL; |
| 2579 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2580 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2581 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2582 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2583 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2584 | if (TYPE(n2) == test) { |
| 2585 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2586 | if (!upper) |
| 2587 | return NULL; |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | ch = CHILD(n, NCH(n) - 1); |
| 2592 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2593 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2594 | ch = CHILD(ch, 1); |
| 2595 | if (TYPE(ch) == test) { |
| 2596 | step = ast_for_expr(c, ch); |
| 2597 | if (!step) |
| 2598 | return NULL; |
| 2599 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2603 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | static expr_ty |
| 2607 | ast_for_binop(struct compiling *c, const node *n) |
| 2608 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2609 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2611 | BinOp(BinOp(A, op, B), op, C). |
| 2612 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2613 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2614 | int i, nops; |
| 2615 | expr_ty expr1, expr2, result; |
| 2616 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2617 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2618 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2619 | if (!expr1) |
| 2620 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2621 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2622 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2623 | if (!expr2) |
| 2624 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2625 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2626 | newoperator = get_operator(c, CHILD(n, 1)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2627 | if (!newoperator) |
| 2628 | return NULL; |
| 2629 | |
| 2630 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2631 | 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] | 2632 | c->c_arena); |
| 2633 | if (!result) |
| 2634 | return NULL; |
| 2635 | |
| 2636 | nops = (NCH(n) - 1) / 2; |
| 2637 | for (i = 1; i < nops; i++) { |
| 2638 | expr_ty tmp_result, tmp; |
| 2639 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2640 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2641 | newoperator = get_operator(c, next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2642 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2643 | return NULL; |
| 2644 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2645 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2646 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | return NULL; |
| 2648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | tmp_result = BinOp(result, newoperator, tmp, |
Miss Islington (bot) | c7be35c | 2019-07-08 14:41:34 -0700 | [diff] [blame] | 2650 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2651 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2652 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2653 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2655 | return NULL; |
| 2656 | result = tmp_result; |
| 2657 | } |
| 2658 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2661 | static expr_ty |
| 2662 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2665 | subscriptlist: subscript (',' subscript)* [','] |
| 2666 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2667 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2668 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2669 | REQ(n, trailer); |
| 2670 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2671 | if (NCH(n) == 2) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2672 | return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, |
| 2673 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2674 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2675 | return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2676 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2677 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2678 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2679 | if (!attr_id) |
| 2680 | return NULL; |
| 2681 | return Attribute(left_expr, attr_id, Load, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2682 | LINENO(n), n->n_col_offset, |
| 2683 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2684 | } |
| 2685 | else { |
| 2686 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2687 | REQ(CHILD(n, 2), RSQB); |
| 2688 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2689 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2690 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2691 | if (!slc) |
| 2692 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2693 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2694 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2695 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2696 | } |
| 2697 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2699 | by treating the sequence as a tuple literal if there are |
| 2700 | no slice features. |
| 2701 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2702 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2703 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2704 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2705 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2706 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2707 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2708 | if (!slices) |
| 2709 | return NULL; |
| 2710 | for (j = 0; j < NCH(n); j += 2) { |
| 2711 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2712 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2713 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2714 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2715 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2716 | asdl_seq_SET(slices, j / 2, slc); |
| 2717 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2718 | if (!simple) { |
| 2719 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2720 | Load, LINENO(n), n->n_col_offset, |
| 2721 | 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] | 2722 | } |
| 2723 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2724 | 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] | 2725 | if (!elts) |
| 2726 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2727 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2728 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2729 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2730 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2731 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2732 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2733 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2734 | if (!e) |
| 2735 | return NULL; |
| 2736 | return Subscript(left_expr, Index(e, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2737 | Load, LINENO(n), n->n_col_offset, |
| 2738 | 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] | 2739 | } |
| 2740 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2744 | ast_for_factor(struct compiling *c, const node *n) |
| 2745 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2746 | expr_ty expression; |
| 2747 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2748 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2749 | if (!expression) |
| 2750 | return NULL; |
| 2751 | |
| 2752 | switch (TYPE(CHILD(n, 0))) { |
| 2753 | case PLUS: |
| 2754 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2755 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2756 | c->c_arena); |
| 2757 | case MINUS: |
| 2758 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2759 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2760 | c->c_arena); |
| 2761 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2762 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2763 | n->n_end_lineno, n->n_end_col_offset, |
| 2764 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2765 | } |
| 2766 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2767 | TYPE(CHILD(n, 0))); |
| 2768 | return NULL; |
| 2769 | } |
| 2770 | |
| 2771 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2772 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2773 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2774 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2775 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2776 | |
| 2777 | REQ(n, atom_expr); |
| 2778 | nch = NCH(n); |
| 2779 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2780 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2781 | if (c->c_feature_version < 5) { |
| 2782 | ast_error(c, n, |
| 2783 | "Await expressions are only supported in Python 3.5 and greater"); |
| 2784 | return NULL; |
| 2785 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2786 | start = 1; |
| 2787 | assert(nch > 1); |
| 2788 | } |
| 2789 | |
| 2790 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2791 | if (!e) |
| 2792 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2793 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2794 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2795 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2796 | return Await(e, LINENO(n), n->n_col_offset, |
| 2797 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2801 | node *ch = CHILD(n, i); |
| 2802 | if (TYPE(ch) != trailer) |
| 2803 | break; |
| 2804 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2805 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2806 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2807 | tmp->lineno = e->lineno; |
| 2808 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2809 | e = tmp; |
| 2810 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2811 | |
| 2812 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2813 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2814 | return Await(e, LINENO(n), n->n_col_offset, |
| 2815 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2816 | } |
| 2817 | else { |
| 2818 | return e; |
| 2819 | } |
| 2820 | } |
| 2821 | |
| 2822 | static expr_ty |
| 2823 | ast_for_power(struct compiling *c, const node *n) |
| 2824 | { |
| 2825 | /* power: atom trailer* ('**' factor)* |
| 2826 | */ |
| 2827 | expr_ty e; |
| 2828 | REQ(n, power); |
| 2829 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2830 | if (!e) |
| 2831 | return NULL; |
| 2832 | if (NCH(n) == 1) |
| 2833 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2834 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2835 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2836 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2837 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2838 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2839 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2840 | } |
| 2841 | return e; |
| 2842 | } |
| 2843 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2844 | static expr_ty |
| 2845 | ast_for_starred(struct compiling *c, const node *n) |
| 2846 | { |
| 2847 | expr_ty tmp; |
| 2848 | REQ(n, star_expr); |
| 2849 | |
| 2850 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2851 | if (!tmp) |
| 2852 | return NULL; |
| 2853 | |
| 2854 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2855 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2856 | 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] | 2857 | } |
| 2858 | |
| 2859 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2860 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2861 | */ |
| 2862 | |
| 2863 | static expr_ty |
| 2864 | ast_for_expr(struct compiling *c, const node *n) |
| 2865 | { |
| 2866 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2867 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2868 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2869 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2871 | and_test: not_test ('and' not_test)* |
| 2872 | not_test: 'not' not_test | comparison |
| 2873 | comparison: expr (comp_op expr)* |
| 2874 | expr: xor_expr ('|' xor_expr)* |
| 2875 | xor_expr: and_expr ('^' and_expr)* |
| 2876 | and_expr: shift_expr ('&' shift_expr)* |
| 2877 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2878 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2879 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2881 | power: atom_expr ['**' factor] |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2882 | atom_expr: [AWAIT] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2883 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2884 | */ |
| 2885 | |
| 2886 | asdl_seq *seq; |
| 2887 | int i; |
| 2888 | |
| 2889 | loop: |
| 2890 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2891 | case namedexpr_test: |
| 2892 | if (NCH(n) == 3) |
| 2893 | return ast_for_namedexpr(c, n); |
| 2894 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2895 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2896 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2897 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2898 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2899 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2900 | else if (NCH(n) > 1) |
| 2901 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2902 | /* Fallthrough */ |
| 2903 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | case and_test: |
| 2905 | if (NCH(n) == 1) { |
| 2906 | n = CHILD(n, 0); |
| 2907 | goto loop; |
| 2908 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2909 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2910 | if (!seq) |
| 2911 | return NULL; |
| 2912 | for (i = 0; i < NCH(n); i += 2) { |
| 2913 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2914 | if (!e) |
| 2915 | return NULL; |
| 2916 | asdl_seq_SET(seq, i / 2, e); |
| 2917 | } |
| 2918 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2919 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2920 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2921 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2922 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2923 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2924 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2925 | case not_test: |
| 2926 | if (NCH(n) == 1) { |
| 2927 | n = CHILD(n, 0); |
| 2928 | goto loop; |
| 2929 | } |
| 2930 | else { |
| 2931 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2932 | if (!expression) |
| 2933 | return NULL; |
| 2934 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2935 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2936 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2937 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | } |
| 2939 | case comparison: |
| 2940 | if (NCH(n) == 1) { |
| 2941 | n = CHILD(n, 0); |
| 2942 | goto loop; |
| 2943 | } |
| 2944 | else { |
| 2945 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2946 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2947 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2948 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2949 | if (!ops) |
| 2950 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2951 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2952 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2953 | return NULL; |
| 2954 | } |
| 2955 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2956 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2957 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2958 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2959 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2960 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2961 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2962 | |
| 2963 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2964 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2965 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2966 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2968 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2969 | asdl_seq_SET(cmps, i / 2, expression); |
| 2970 | } |
| 2971 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2972 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2973 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2974 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2976 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2977 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2978 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2979 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2980 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2981 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | /* The next five cases all handle BinOps. The main body of code |
| 2983 | is the same in each case, but the switch turned inside out to |
| 2984 | reuse the code for each type of operator. |
| 2985 | */ |
| 2986 | case expr: |
| 2987 | case xor_expr: |
| 2988 | case and_expr: |
| 2989 | case shift_expr: |
| 2990 | case arith_expr: |
| 2991 | case term: |
| 2992 | if (NCH(n) == 1) { |
| 2993 | n = CHILD(n, 0); |
| 2994 | goto loop; |
| 2995 | } |
| 2996 | return ast_for_binop(c, n); |
| 2997 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2998 | node *an = NULL; |
| 2999 | node *en = NULL; |
| 3000 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3001 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3002 | if (NCH(n) > 1) |
| 3003 | an = CHILD(n, 1); /* yield_arg */ |
| 3004 | if (an) { |
| 3005 | en = CHILD(an, NCH(an) - 1); |
| 3006 | if (NCH(an) == 2) { |
| 3007 | is_from = 1; |
| 3008 | exp = ast_for_expr(c, en); |
| 3009 | } |
| 3010 | else |
| 3011 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3012 | if (!exp) |
| 3013 | return NULL; |
| 3014 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 3015 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3016 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 3017 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 3018 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 3019 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3020 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3021 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3022 | if (NCH(n) == 1) { |
| 3023 | n = CHILD(n, 0); |
| 3024 | goto loop; |
| 3025 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3026 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 3027 | case power: |
| 3028 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3029 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3030 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3031 | return NULL; |
| 3032 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3033 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3034 | return NULL; |
| 3035 | } |
| 3036 | |
| 3037 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3038 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3039 | const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3040 | { |
| 3041 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3042 | arglist: argument (',' argument)* [','] |
| 3043 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3044 | */ |
| 3045 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3046 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3047 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3048 | asdl_seq *args; |
| 3049 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | |
| 3051 | REQ(n, arglist); |
| 3052 | |
| 3053 | nargs = 0; |
| 3054 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3055 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3056 | node *ch = CHILD(n, i); |
| 3057 | if (TYPE(ch) == argument) { |
| 3058 | if (NCH(ch) == 1) |
| 3059 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3060 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 3061 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3062 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 3063 | ast_error(c, ch, "invalid syntax"); |
| 3064 | return NULL; |
| 3065 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3066 | if (NCH(n) > 1) { |
| 3067 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 3068 | return NULL; |
| 3069 | } |
| 3070 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3071 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 3072 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3073 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3074 | nargs++; |
| 3075 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3076 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3077 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3078 | nkeywords++; |
| 3079 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3080 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3081 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3082 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3083 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3084 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3085 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3086 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3087 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3088 | |
| 3089 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 3090 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 3091 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3092 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3093 | node *ch = CHILD(n, i); |
| 3094 | if (TYPE(ch) == argument) { |
| 3095 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3096 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3097 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3098 | /* a positional argument */ |
| 3099 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3100 | if (ndoublestars) { |
| 3101 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3102 | "positional argument follows " |
| 3103 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3104 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3105 | else { |
| 3106 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3107 | "positional argument follows " |
| 3108 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3109 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3110 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3111 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3112 | e = ast_for_expr(c, chch); |
| 3113 | if (!e) |
| 3114 | return NULL; |
| 3115 | asdl_seq_SET(args, nargs++, e); |
| 3116 | } |
| 3117 | else if (TYPE(chch) == STAR) { |
| 3118 | /* an iterable argument unpacking */ |
| 3119 | expr_ty starred; |
| 3120 | if (ndoublestars) { |
| 3121 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3122 | "iterable argument unpacking follows " |
| 3123 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3124 | return NULL; |
| 3125 | } |
| 3126 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3127 | if (!e) |
| 3128 | return NULL; |
| 3129 | starred = Starred(e, Load, LINENO(chch), |
| 3130 | chch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3131 | chch->n_end_lineno, chch->n_end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3132 | c->c_arena); |
| 3133 | if (!starred) |
| 3134 | return NULL; |
| 3135 | asdl_seq_SET(args, nargs++, starred); |
| 3136 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3137 | } |
| 3138 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3139 | /* a keyword argument unpacking */ |
| 3140 | keyword_ty kw; |
| 3141 | i++; |
| 3142 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3143 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3144 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3145 | kw = keyword(NULL, e, c->c_arena); |
| 3146 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3147 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3149 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3150 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3151 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3153 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3154 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3155 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3156 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3157 | /* treat colon equal as positional argument */ |
| 3158 | if (nkeywords) { |
| 3159 | if (ndoublestars) { |
| 3160 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3161 | "positional argument follows " |
| 3162 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3163 | } |
| 3164 | else { |
| 3165 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3166 | "positional argument follows " |
| 3167 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3168 | } |
| 3169 | return NULL; |
| 3170 | } |
| 3171 | e = ast_for_namedexpr(c, ch); |
| 3172 | if (!e) |
| 3173 | return NULL; |
| 3174 | asdl_seq_SET(args, nargs++, e); |
| 3175 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3176 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3177 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3178 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3179 | identifier key, tmp; |
| 3180 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3182 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3183 | // expression) in the keyword slot of a call site. So, we need |
| 3184 | // to manually enforce that the keyword is a NAME here. |
| 3185 | static const int name_tree[] = { |
| 3186 | test, |
| 3187 | or_test, |
| 3188 | and_test, |
| 3189 | not_test, |
| 3190 | comparison, |
| 3191 | expr, |
| 3192 | xor_expr, |
| 3193 | and_expr, |
| 3194 | shift_expr, |
| 3195 | arith_expr, |
| 3196 | term, |
| 3197 | factor, |
| 3198 | power, |
| 3199 | atom_expr, |
| 3200 | atom, |
| 3201 | 0, |
| 3202 | }; |
| 3203 | node *expr_node = chch; |
| 3204 | for (int i = 0; name_tree[i]; i++) { |
| 3205 | if (TYPE(expr_node) != name_tree[i]) |
| 3206 | break; |
| 3207 | if (NCH(expr_node) != 1) |
| 3208 | break; |
| 3209 | expr_node = CHILD(expr_node, 0); |
| 3210 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3211 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3212 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3213 | "expression cannot contain assignment, " |
| 3214 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3215 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3216 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3217 | key = new_identifier(STR(expr_node), c); |
| 3218 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3219 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3221 | if (forbidden_name(c, key, chch, 1)) { |
| 3222 | return NULL; |
| 3223 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3224 | for (k = 0; k < nkeywords; k++) { |
| 3225 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3226 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 3227 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3228 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3229 | return NULL; |
| 3230 | } |
| 3231 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3232 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3233 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3234 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3235 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3236 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3237 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3238 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3239 | } |
| 3240 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3243 | return Call(func, args, keywords, func->lineno, func->col_offset, |
| 3244 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3247 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3248 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3249 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3250 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3251 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3252 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3253 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3254 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3255 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3256 | } |
| 3257 | else { |
| 3258 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3259 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3260 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3261 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3262 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3263 | else { |
| 3264 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3265 | if (!tmp) |
| 3266 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3267 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3268 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3269 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3272 | static stmt_ty |
| 3273 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3274 | { |
| 3275 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3276 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3277 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3278 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3279 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3280 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3281 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3282 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3283 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3284 | int num = NCH(n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3285 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3286 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3287 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | if (!e) |
| 3289 | return NULL; |
| 3290 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3291 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3292 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | } |
| 3294 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3295 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3296 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3297 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3298 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3299 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3300 | if (!expr1) |
| 3301 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3302 | if(!set_context(c, expr1, Store, ch)) |
| 3303 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3304 | /* set_context checks that most expressions are not the left side. |
| 3305 | Augmented assignments can only have a name, a subscript, or an |
| 3306 | attribute on the left, though, so we have to explicitly check for |
| 3307 | those. */ |
| 3308 | switch (expr1->kind) { |
| 3309 | case Name_kind: |
| 3310 | case Attribute_kind: |
| 3311 | case Subscript_kind: |
| 3312 | break; |
| 3313 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3314 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3315 | return NULL; |
| 3316 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3317 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3318 | ch = CHILD(n, 2); |
| 3319 | if (TYPE(ch) == testlist) |
| 3320 | expr2 = ast_for_testlist(c, ch); |
| 3321 | else |
| 3322 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3323 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3324 | return NULL; |
| 3325 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3326 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3327 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3328 | return NULL; |
| 3329 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3330 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3331 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3333 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3334 | expr_ty expr1, expr2, expr3; |
| 3335 | node *ch = CHILD(n, 0); |
| 3336 | node *deep, *ann = CHILD(n, 1); |
| 3337 | int simple = 1; |
| 3338 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 3339 | /* AnnAssigns are only allowed in Python 3.6 or greater */ |
| 3340 | if (c->c_feature_version < 6) { |
| 3341 | ast_error(c, ch, |
| 3342 | "Variable annotation syntax is only supported in Python 3.6 and greater"); |
| 3343 | return NULL; |
| 3344 | } |
| 3345 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3346 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3347 | deep = ch; |
| 3348 | while (NCH(deep) == 1) { |
| 3349 | deep = CHILD(deep, 0); |
| 3350 | } |
| 3351 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3352 | simple = 0; |
| 3353 | } |
| 3354 | expr1 = ast_for_testlist(c, ch); |
| 3355 | if (!expr1) { |
| 3356 | return NULL; |
| 3357 | } |
| 3358 | switch (expr1->kind) { |
| 3359 | case Name_kind: |
| 3360 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3361 | return NULL; |
| 3362 | } |
| 3363 | expr1->v.Name.ctx = Store; |
| 3364 | break; |
| 3365 | case Attribute_kind: |
| 3366 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3367 | return NULL; |
| 3368 | } |
| 3369 | expr1->v.Attribute.ctx = Store; |
| 3370 | break; |
| 3371 | case Subscript_kind: |
| 3372 | expr1->v.Subscript.ctx = Store; |
| 3373 | break; |
| 3374 | case List_kind: |
| 3375 | ast_error(c, ch, |
| 3376 | "only single target (not list) can be annotated"); |
| 3377 | return NULL; |
| 3378 | case Tuple_kind: |
| 3379 | ast_error(c, ch, |
| 3380 | "only single target (not tuple) can be annotated"); |
| 3381 | return NULL; |
| 3382 | default: |
| 3383 | ast_error(c, ch, |
| 3384 | "illegal target for annotation"); |
| 3385 | return NULL; |
| 3386 | } |
| 3387 | |
| 3388 | if (expr1->kind != Name_kind) { |
| 3389 | simple = 0; |
| 3390 | } |
| 3391 | ch = CHILD(ann, 1); |
| 3392 | expr2 = ast_for_expr(c, ch); |
| 3393 | if (!expr2) { |
| 3394 | return NULL; |
| 3395 | } |
| 3396 | if (NCH(ann) == 2) { |
| 3397 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3398 | LINENO(n), n->n_col_offset, |
| 3399 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3400 | } |
| 3401 | else { |
| 3402 | ch = CHILD(ann, 3); |
Pablo Galindo | 8565f6b | 2019-06-03 08:34:20 +0100 | [diff] [blame] | 3403 | if (TYPE(ch) == testlist_star_expr) { |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3404 | expr3 = ast_for_testlist(c, ch); |
| 3405 | } |
| 3406 | else { |
| 3407 | expr3 = ast_for_expr(c, ch); |
| 3408 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3409 | if (!expr3) { |
| 3410 | return NULL; |
| 3411 | } |
| 3412 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3413 | LINENO(n), n->n_col_offset, |
| 3414 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3415 | } |
| 3416 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3417 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3418 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3419 | asdl_seq *targets; |
| 3420 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3421 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3422 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3423 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3424 | /* a normal assignment */ |
| 3425 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3426 | |
| 3427 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3428 | nch_minus_type = num - has_type_comment; |
| 3429 | |
| 3430 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3431 | if (!targets) |
| 3432 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3433 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3434 | expr_ty e; |
| 3435 | node *ch = CHILD(n, i); |
| 3436 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3437 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3438 | return NULL; |
| 3439 | } |
| 3440 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3442 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3443 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3444 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3445 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3446 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3447 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3448 | asdl_seq_SET(targets, i / 2, e); |
| 3449 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3450 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3451 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3452 | expression = ast_for_testlist(c, value); |
| 3453 | else |
| 3454 | expression = ast_for_expr(c, value); |
| 3455 | if (!expression) |
| 3456 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3457 | if (has_type_comment) { |
| 3458 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3459 | if (!type_comment) |
| 3460 | return NULL; |
| 3461 | } |
| 3462 | else |
| 3463 | type_comment = NULL; |
| 3464 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3465 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3466 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3469 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3470 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3471 | 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] | 3472 | { |
| 3473 | asdl_seq *seq; |
| 3474 | int i; |
| 3475 | expr_ty e; |
| 3476 | |
| 3477 | REQ(n, exprlist); |
| 3478 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3479 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3480 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3481 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3482 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3483 | e = ast_for_expr(c, CHILD(n, i)); |
| 3484 | if (!e) |
| 3485 | return NULL; |
| 3486 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3487 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3488 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3489 | } |
| 3490 | return seq; |
| 3491 | } |
| 3492 | |
| 3493 | static stmt_ty |
| 3494 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3495 | { |
| 3496 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3497 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3498 | /* del_stmt: 'del' exprlist */ |
| 3499 | REQ(n, del_stmt); |
| 3500 | |
| 3501 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3502 | if (!expr_list) |
| 3503 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3504 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3505 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | static stmt_ty |
| 3509 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3510 | { |
| 3511 | /* |
| 3512 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3513 | | yield_stmt |
| 3514 | break_stmt: 'break' |
| 3515 | continue_stmt: 'continue' |
| 3516 | return_stmt: 'return' [testlist] |
| 3517 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3518 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3519 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3520 | */ |
| 3521 | node *ch; |
| 3522 | |
| 3523 | REQ(n, flow_stmt); |
| 3524 | ch = CHILD(n, 0); |
| 3525 | switch (TYPE(ch)) { |
| 3526 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3527 | return Break(LINENO(n), n->n_col_offset, |
| 3528 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3529 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3530 | return Continue(LINENO(n), n->n_col_offset, |
| 3531 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3532 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3533 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3534 | if (!exp) |
| 3535 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3536 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3537 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3538 | } |
| 3539 | case return_stmt: |
| 3540 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3541 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3542 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3543 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3544 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | if (!expression) |
| 3546 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3547 | return Return(expression, 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 | } |
| 3550 | case raise_stmt: |
| 3551 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3552 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3553 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3554 | else if (NCH(ch) >= 2) { |
| 3555 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3556 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3557 | if (!expression) |
| 3558 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3559 | if (NCH(ch) == 4) { |
| 3560 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3561 | if (!cause) |
| 3562 | return NULL; |
| 3563 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3564 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3565 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3566 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3567 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3568 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3569 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3570 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3571 | return NULL; |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3576 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3577 | { |
| 3578 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3579 | import_as_name: NAME ['as' NAME] |
| 3580 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3581 | dotted_name: NAME ('.' NAME)* |
| 3582 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3583 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3584 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3585 | loop: |
| 3586 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3587 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3588 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3589 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3590 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3591 | if (!name) |
| 3592 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3593 | if (NCH(n) == 3) { |
| 3594 | node *str_node = CHILD(n, 2); |
| 3595 | str = NEW_IDENTIFIER(str_node); |
| 3596 | if (!str) |
| 3597 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3598 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3599 | return NULL; |
| 3600 | } |
| 3601 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3602 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3603 | return NULL; |
| 3604 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3605 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3606 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3607 | case dotted_as_name: |
| 3608 | if (NCH(n) == 1) { |
| 3609 | n = CHILD(n, 0); |
| 3610 | goto loop; |
| 3611 | } |
| 3612 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3613 | node *asname_node = CHILD(n, 2); |
| 3614 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3615 | if (!a) |
| 3616 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3618 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3619 | if (!a->asname) |
| 3620 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3621 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3622 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3623 | return a; |
| 3624 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3625 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3626 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3627 | node *name_node = CHILD(n, 0); |
| 3628 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3629 | if (!name) |
| 3630 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3631 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3632 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3633 | return alias(name, NULL, c->c_arena); |
| 3634 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3635 | else { |
| 3636 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3637 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3638 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3641 | |
| 3642 | len = 0; |
| 3643 | for (i = 0; i < NCH(n); i += 2) |
| 3644 | /* length of string plus one for the dot */ |
| 3645 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3646 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3647 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3648 | if (!str) |
| 3649 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3650 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3651 | if (!s) |
| 3652 | return NULL; |
| 3653 | for (i = 0; i < NCH(n); i += 2) { |
| 3654 | char *sch = STR(CHILD(n, i)); |
| 3655 | strcpy(s, STR(CHILD(n, i))); |
| 3656 | s += strlen(sch); |
| 3657 | *s++ = '.'; |
| 3658 | } |
| 3659 | --s; |
| 3660 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3661 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3662 | PyBytes_GET_SIZE(str), |
| 3663 | NULL); |
| 3664 | Py_DECREF(str); |
| 3665 | if (!uni) |
| 3666 | return NULL; |
| 3667 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3668 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3669 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3670 | Py_DECREF(str); |
| 3671 | return NULL; |
| 3672 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3673 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3674 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3675 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3676 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3677 | if (!str) |
| 3678 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3679 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3680 | Py_DECREF(str); |
| 3681 | return NULL; |
| 3682 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3683 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3684 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3685 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3686 | "unexpected import name: %d", TYPE(n)); |
| 3687 | return NULL; |
| 3688 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3689 | |
| 3690 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3691 | return NULL; |
| 3692 | } |
| 3693 | |
| 3694 | static stmt_ty |
| 3695 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3696 | { |
| 3697 | /* |
| 3698 | import_stmt: import_name | import_from |
| 3699 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3700 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3701 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3702 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3703 | int lineno; |
| 3704 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3705 | int i; |
| 3706 | asdl_seq *aliases; |
| 3707 | |
| 3708 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3709 | lineno = LINENO(n); |
| 3710 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3712 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3713 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3714 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3715 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3716 | if (!aliases) |
| 3717 | return NULL; |
| 3718 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3719 | 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] | 3720 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3721 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3722 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3723 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3724 | // Even though n is modified above, the end position is not changed |
| 3725 | return Import(aliases, lineno, col_offset, |
| 3726 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3727 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3728 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3729 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3730 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3731 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3732 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3733 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3735 | /* Count the number of dots (for relative imports) and check for the |
| 3736 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3737 | for (idx = 1; idx < NCH(n); idx++) { |
| 3738 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3739 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3740 | if (!mod) |
| 3741 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3742 | idx++; |
| 3743 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3744 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3746 | ndots += 3; |
| 3747 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3748 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3749 | break; |
| 3750 | } |
| 3751 | ndots++; |
| 3752 | } |
| 3753 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3754 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3755 | case STAR: |
| 3756 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3757 | n = CHILD(n, idx); |
| 3758 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3759 | break; |
| 3760 | case LPAR: |
| 3761 | /* from ... import (x, y, z) */ |
| 3762 | n = CHILD(n, idx + 1); |
| 3763 | n_children = NCH(n); |
| 3764 | break; |
| 3765 | case import_as_names: |
| 3766 | /* from ... import x, y, z */ |
| 3767 | n = CHILD(n, idx); |
| 3768 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3769 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3770 | ast_error(c, n, |
| 3771 | "trailing comma not allowed without" |
| 3772 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3773 | return NULL; |
| 3774 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3777 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3778 | return NULL; |
| 3779 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3780 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3781 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3782 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3783 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3784 | |
| 3785 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3786 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3787 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3788 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3790 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3791 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3792 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3793 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3794 | 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] | 3795 | if (!import_alias) |
| 3796 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3797 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3798 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3799 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3800 | if (mod != NULL) |
| 3801 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3802 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3803 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3804 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3805 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3806 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3807 | "unknown import statement: starts with command '%s'", |
| 3808 | STR(CHILD(n, 0))); |
| 3809 | return NULL; |
| 3810 | } |
| 3811 | |
| 3812 | static stmt_ty |
| 3813 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3814 | { |
| 3815 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3816 | identifier name; |
| 3817 | asdl_seq *s; |
| 3818 | int i; |
| 3819 | |
| 3820 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3821 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3822 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3823 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3824 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3825 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3826 | if (!name) |
| 3827 | return NULL; |
| 3828 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3829 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3830 | return Global(s, LINENO(n), n->n_col_offset, |
| 3831 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3835 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3836 | { |
| 3837 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3838 | identifier name; |
| 3839 | asdl_seq *s; |
| 3840 | int i; |
| 3841 | |
| 3842 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3843 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3844 | if (!s) |
| 3845 | return NULL; |
| 3846 | for (i = 1; i < NCH(n); i += 2) { |
| 3847 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3848 | if (!name) |
| 3849 | return NULL; |
| 3850 | asdl_seq_SET(s, i / 2, name); |
| 3851 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3852 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3853 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3857 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3858 | { |
| 3859 | /* assert_stmt: 'assert' test [',' test] */ |
| 3860 | REQ(n, assert_stmt); |
| 3861 | if (NCH(n) == 2) { |
| 3862 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3863 | if (!expression) |
| 3864 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3865 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3866 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3867 | } |
| 3868 | else if (NCH(n) == 4) { |
| 3869 | expr_ty expr1, expr2; |
| 3870 | |
| 3871 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3872 | if (!expr1) |
| 3873 | return NULL; |
| 3874 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3875 | if (!expr2) |
| 3876 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3878 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3879 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3880 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3881 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3882 | "improper number of parts to 'assert' statement: %d", |
| 3883 | NCH(n)); |
| 3884 | return NULL; |
| 3885 | } |
| 3886 | |
| 3887 | static asdl_seq * |
| 3888 | ast_for_suite(struct compiling *c, const node *n) |
| 3889 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3890 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3891 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3892 | stmt_ty s; |
| 3893 | int i, total, num, end, pos = 0; |
| 3894 | node *ch; |
| 3895 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3896 | if (TYPE(n) != func_body_suite) { |
| 3897 | REQ(n, suite); |
| 3898 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3899 | |
| 3900 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3901 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3902 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3903 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3904 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3905 | n = CHILD(n, 0); |
| 3906 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3907 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3908 | */ |
| 3909 | end = NCH(n) - 1; |
| 3910 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3911 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3912 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3913 | for (i = 0; i < end; i += 2) { |
| 3914 | ch = CHILD(n, i); |
| 3915 | s = ast_for_stmt(c, ch); |
| 3916 | if (!s) |
| 3917 | return NULL; |
| 3918 | asdl_seq_SET(seq, pos++, s); |
| 3919 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3920 | } |
| 3921 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3922 | i = 2; |
| 3923 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3924 | i += 2; |
| 3925 | REQ(CHILD(n, 2), NEWLINE); |
| 3926 | } |
| 3927 | |
| 3928 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3929 | ch = CHILD(n, i); |
| 3930 | REQ(ch, stmt); |
| 3931 | num = num_stmts(ch); |
| 3932 | if (num == 1) { |
| 3933 | /* small_stmt or compound_stmt with only one child */ |
| 3934 | s = ast_for_stmt(c, ch); |
| 3935 | if (!s) |
| 3936 | return NULL; |
| 3937 | asdl_seq_SET(seq, pos++, s); |
| 3938 | } |
| 3939 | else { |
| 3940 | int j; |
| 3941 | ch = CHILD(ch, 0); |
| 3942 | REQ(ch, simple_stmt); |
| 3943 | for (j = 0; j < NCH(ch); j += 2) { |
| 3944 | /* statement terminates with a semi-colon ';' */ |
| 3945 | if (NCH(CHILD(ch, j)) == 0) { |
| 3946 | assert((j + 1) == NCH(ch)); |
| 3947 | break; |
| 3948 | } |
| 3949 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3950 | if (!s) |
| 3951 | return NULL; |
| 3952 | asdl_seq_SET(seq, pos++, s); |
| 3953 | } |
| 3954 | } |
| 3955 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3956 | } |
| 3957 | assert(pos == seq->size); |
| 3958 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3961 | static void |
| 3962 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3963 | { |
Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame] | 3964 | Py_ssize_t tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3965 | // There must be no empty suites. |
| 3966 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3967 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3968 | *end_lineno = last->end_lineno; |
| 3969 | *end_col_offset = last->end_col_offset; |
| 3970 | } |
| 3971 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3972 | static stmt_ty |
| 3973 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3974 | { |
| 3975 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3976 | ['else' ':' suite] |
| 3977 | */ |
| 3978 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3979 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3980 | |
| 3981 | REQ(n, if_stmt); |
| 3982 | |
| 3983 | if (NCH(n) == 4) { |
| 3984 | expr_ty expression; |
| 3985 | asdl_seq *suite_seq; |
| 3986 | |
| 3987 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3988 | if (!expression) |
| 3989 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3990 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3991 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3992 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3993 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3994 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3995 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3996 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3997 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3998 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3999 | s = STR(CHILD(n, 4)); |
| 4000 | /* s[2], the third character in the string, will be |
| 4001 | 's' for el_s_e, or |
| 4002 | 'i' for el_i_f |
| 4003 | */ |
| 4004 | if (s[2] == 's') { |
| 4005 | expr_ty expression; |
| 4006 | asdl_seq *seq1, *seq2; |
| 4007 | |
| 4008 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4009 | if (!expression) |
| 4010 | return NULL; |
| 4011 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4012 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4013 | return NULL; |
| 4014 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4015 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4016 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4017 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4018 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4019 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4020 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4021 | } |
| 4022 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4023 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4024 | expr_ty expression; |
| 4025 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4026 | asdl_seq *orelse = NULL; |
| 4027 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4028 | /* must reference the child n_elif+1 since 'else' token is third, |
| 4029 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4030 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 4031 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 4032 | has_else = 1; |
| 4033 | n_elif -= 3; |
| 4034 | } |
| 4035 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4036 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4037 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4038 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4039 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4040 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4041 | if (!orelse) |
| 4042 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4043 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4044 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4045 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4046 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 4047 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4048 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4049 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4050 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4051 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4052 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | asdl_seq_SET(orelse, 0, |
| 4055 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4056 | LINENO(CHILD(n, NCH(n) - 6)), |
| 4057 | CHILD(n, NCH(n) - 6)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4058 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4059 | /* the just-created orelse handled the last elif */ |
| 4060 | n_elif--; |
| 4061 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4062 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4063 | for (i = 0; i < n_elif; i++) { |
| 4064 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4065 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4066 | if (!newobj) |
| 4067 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4068 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4069 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4070 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4071 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4072 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4073 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4074 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4075 | if (orelse != NULL) { |
| 4076 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4077 | } else { |
| 4078 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4079 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4080 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4081 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4082 | LINENO(CHILD(n, off)), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4083 | CHILD(n, off)->n_col_offset, |
| 4084 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4085 | orelse = newobj; |
| 4086 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4087 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4088 | if (!expression) |
| 4089 | return NULL; |
| 4090 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 4091 | if (!suite_seq) |
| 4092 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4093 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4094 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4095 | LINENO(n), n->n_col_offset, |
| 4096 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4097 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4098 | |
| 4099 | PyErr_Format(PyExc_SystemError, |
| 4100 | "unexpected token in 'if' statement: %s", s); |
| 4101 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | static stmt_ty |
| 4105 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 4106 | { |
| 4107 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 4108 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4109 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4110 | |
| 4111 | if (NCH(n) == 4) { |
| 4112 | expr_ty expression; |
| 4113 | asdl_seq *suite_seq; |
| 4114 | |
| 4115 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4116 | if (!expression) |
| 4117 | return NULL; |
| 4118 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4119 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4120 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4121 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4122 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4123 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4124 | } |
| 4125 | else if (NCH(n) == 7) { |
| 4126 | expr_ty expression; |
| 4127 | asdl_seq *seq1, *seq2; |
| 4128 | |
| 4129 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4130 | if (!expression) |
| 4131 | return NULL; |
| 4132 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4133 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4134 | return NULL; |
| 4135 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4136 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4137 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4138 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4139 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4140 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4141 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4142 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4143 | |
| 4144 | PyErr_Format(PyExc_SystemError, |
| 4145 | "wrong number of tokens for 'while' statement: %d", |
| 4146 | NCH(n)); |
| 4147 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4151 | 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] | 4152 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4153 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4154 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4155 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4156 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4157 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4158 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4159 | int has_type_comment; |
| 4160 | string type_comment; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4161 | |
| 4162 | if (is_async && c->c_feature_version < 5) { |
| 4163 | ast_error(c, n, |
| 4164 | "Async for loops are only supported in Python 3.5 and greater"); |
| 4165 | return NULL; |
| 4166 | } |
| 4167 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4168 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4169 | REQ(n, for_stmt); |
| 4170 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4171 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4172 | |
| 4173 | if (NCH(n) == 9 + has_type_comment) { |
| 4174 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4175 | if (!seq) |
| 4176 | return NULL; |
| 4177 | } |
| 4178 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4179 | node_target = CHILD(n, 1); |
| 4180 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4181 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4182 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4183 | /* Check the # of children rather than the length of _target, since |
| 4184 | 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] | 4185 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4186 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4187 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4188 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4189 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4190 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4191 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4192 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4193 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4194 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4196 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4197 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4198 | return NULL; |
| 4199 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4200 | if (seq != NULL) { |
| 4201 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4202 | } else { |
| 4203 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4204 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4205 | |
| 4206 | if (has_type_comment) { |
| 4207 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4208 | if (!type_comment) |
| 4209 | return NULL; |
| 4210 | } |
| 4211 | else |
| 4212 | type_comment = NULL; |
| 4213 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4214 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4215 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4216 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4217 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4218 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4219 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4220 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4221 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | static excepthandler_ty |
| 4225 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4226 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4227 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4228 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4229 | REQ(exc, except_clause); |
| 4230 | REQ(body, suite); |
| 4231 | |
| 4232 | if (NCH(exc) == 1) { |
| 4233 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4234 | if (!suite_seq) |
| 4235 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4236 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4237 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4238 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4239 | exc->n_col_offset, |
| 4240 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4241 | } |
| 4242 | else if (NCH(exc) == 2) { |
| 4243 | expr_ty expression; |
| 4244 | asdl_seq *suite_seq; |
| 4245 | |
| 4246 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4247 | if (!expression) |
| 4248 | return NULL; |
| 4249 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4250 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4251 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4252 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4253 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4254 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4255 | exc->n_col_offset, |
| 4256 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4257 | } |
| 4258 | else if (NCH(exc) == 4) { |
| 4259 | asdl_seq *suite_seq; |
| 4260 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4261 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4262 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4263 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4264 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4265 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4266 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4267 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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, e, 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 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4278 | |
| 4279 | PyErr_Format(PyExc_SystemError, |
| 4280 | "wrong number of children for 'except' clause: %d", |
| 4281 | NCH(exc)); |
| 4282 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | static stmt_ty |
| 4286 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4287 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4288 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4289 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4290 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4291 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4292 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4293 | REQ(n, try_stmt); |
| 4294 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4295 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4296 | if (body == NULL) |
| 4297 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4298 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4299 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4300 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4301 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4302 | /* we can assume it's an "else", |
| 4303 | because nch >= 9 for try-else-finally and |
| 4304 | it would otherwise have a type of except_clause */ |
| 4305 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4306 | if (orelse == NULL) |
| 4307 | return NULL; |
| 4308 | n_except--; |
| 4309 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4310 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4311 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4312 | if (finally == NULL) |
| 4313 | return NULL; |
| 4314 | n_except--; |
| 4315 | } |
| 4316 | else { |
| 4317 | /* we can assume it's an "else", |
| 4318 | otherwise it would have a type of except_clause */ |
| 4319 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4320 | if (orelse == NULL) |
| 4321 | return NULL; |
| 4322 | n_except--; |
| 4323 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4324 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4325 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4326 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4327 | return NULL; |
| 4328 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4329 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4330 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4331 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4332 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4333 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4334 | if (handlers == NULL) |
| 4335 | return NULL; |
| 4336 | |
| 4337 | for (i = 0; i < n_except; i++) { |
| 4338 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4339 | CHILD(n, 5 + i * 3)); |
| 4340 | if (!e) |
| 4341 | return NULL; |
| 4342 | asdl_seq_SET(handlers, i, e); |
| 4343 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4346 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4347 | if (finally != NULL) { |
| 4348 | // finally is always last |
| 4349 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4350 | } else if (orelse != NULL) { |
| 4351 | // otherwise else is last |
| 4352 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4353 | } else { |
| 4354 | // inline the get_last_end_pos logic due to layout mismatch |
| 4355 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4356 | end_lineno = last_handler->end_lineno; |
| 4357 | end_col_offset = last_handler->end_col_offset; |
| 4358 | } |
| 4359 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4360 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4363 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4364 | static withitem_ty |
| 4365 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4366 | { |
| 4367 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4368 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4369 | REQ(n, with_item); |
| 4370 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4371 | if (!context_expr) |
| 4372 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4373 | if (NCH(n) == 3) { |
| 4374 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4375 | |
| 4376 | if (!optional_vars) { |
| 4377 | return NULL; |
| 4378 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4379 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4380 | return NULL; |
| 4381 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4384 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4387 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4388 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4389 | 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] | 4390 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4391 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4392 | 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] | 4393 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4394 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4395 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4396 | if (is_async && c->c_feature_version < 5) { |
| 4397 | ast_error(c, n, |
| 4398 | "Async with statements are only supported in Python 3.5 and greater"); |
| 4399 | return NULL; |
| 4400 | } |
| 4401 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4402 | REQ(n, with_stmt); |
| 4403 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4404 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4405 | nch_minus_type = NCH(n) - has_type_comment; |
| 4406 | |
| 4407 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4408 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4409 | if (!items) |
| 4410 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4411 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4412 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4413 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4414 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4415 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4418 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4419 | if (!body) |
| 4420 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4421 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4422 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4423 | if (has_type_comment) { |
| 4424 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4425 | if (!type_comment) |
| 4426 | return NULL; |
| 4427 | } |
| 4428 | else |
| 4429 | type_comment = NULL; |
| 4430 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4431 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4432 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4433 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4434 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4435 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4436 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4439 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4440 | 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] | 4441 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4442 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4443 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4444 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4445 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4446 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4447 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4448 | REQ(n, classdef); |
| 4449 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4450 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4451 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4452 | if (!s) |
| 4453 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4454 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4455 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4456 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4457 | if (!classname) |
| 4458 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4459 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4460 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4461 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4462 | LINENO(n), n->n_col_offset, |
| 4463 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4464 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4465 | |
| 4466 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4467 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4468 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4469 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4470 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4471 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4472 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4473 | if (!classname) |
| 4474 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4475 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4476 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4477 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4478 | LINENO(n), n->n_col_offset, |
| 4479 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4482 | /* class NAME '(' arglist ')' ':' suite */ |
| 4483 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4484 | { |
| 4485 | PyObject *dummy_name; |
| 4486 | expr_ty dummy; |
| 4487 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4488 | if (!dummy_name) |
| 4489 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4490 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4491 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4492 | c->c_arena); |
| 4493 | call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4)); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4494 | if (!call) |
| 4495 | return NULL; |
| 4496 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4497 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4498 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4499 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4500 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4501 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4502 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4503 | if (!classname) |
| 4504 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4505 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4506 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4507 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4508 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4509 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4510 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | static stmt_ty |
| 4514 | ast_for_stmt(struct compiling *c, const node *n) |
| 4515 | { |
| 4516 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4517 | assert(NCH(n) == 1); |
| 4518 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4519 | } |
| 4520 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4521 | assert(num_stmts(n) == 1); |
| 4522 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4523 | } |
| 4524 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4525 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4526 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4527 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4528 | */ |
| 4529 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4530 | case expr_stmt: |
| 4531 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4532 | case del_stmt: |
| 4533 | return ast_for_del_stmt(c, n); |
| 4534 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4535 | return Pass(LINENO(n), n->n_col_offset, |
| 4536 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4537 | case flow_stmt: |
| 4538 | return ast_for_flow_stmt(c, n); |
| 4539 | case import_stmt: |
| 4540 | return ast_for_import_stmt(c, n); |
| 4541 | case global_stmt: |
| 4542 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4543 | case nonlocal_stmt: |
| 4544 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4545 | case assert_stmt: |
| 4546 | return ast_for_assert_stmt(c, n); |
| 4547 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4548 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4549 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4550 | TYPE(n), NCH(n)); |
| 4551 | return NULL; |
| 4552 | } |
| 4553 | } |
| 4554 | else { |
| 4555 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4556 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4557 | */ |
| 4558 | node *ch = CHILD(n, 0); |
| 4559 | REQ(n, compound_stmt); |
| 4560 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4561 | case if_stmt: |
| 4562 | return ast_for_if_stmt(c, ch); |
| 4563 | case while_stmt: |
| 4564 | return ast_for_while_stmt(c, ch); |
| 4565 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4566 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4567 | case try_stmt: |
| 4568 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4569 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4570 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4571 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4572 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4573 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4574 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | case decorated: |
| 4576 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4577 | case async_stmt: |
| 4578 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4579 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4580 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4581 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4582 | TYPE(n), NCH(n)); |
| 4583 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4584 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4589 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4590 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4591 | const char *end; |
| 4592 | long x; |
| 4593 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4594 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4595 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4596 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4597 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4598 | errno = 0; |
| 4599 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4600 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4601 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4602 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4603 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4604 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4605 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4606 | } |
| 4607 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4608 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4609 | if (*end == '\0') { |
| 4610 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4611 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4612 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4613 | } |
| 4614 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4615 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4616 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4617 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4618 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4619 | return NULL; |
| 4620 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4621 | } |
| 4622 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4623 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4624 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4625 | if (dx == -1.0 && PyErr_Occurred()) |
| 4626 | return NULL; |
| 4627 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4628 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4632 | parsenumber(struct compiling *c, const char *s) |
| 4633 | { |
| 4634 | char *dup, *end; |
| 4635 | PyObject *res = NULL; |
| 4636 | |
| 4637 | assert(s != NULL); |
| 4638 | |
| 4639 | if (strchr(s, '_') == NULL) { |
| 4640 | return parsenumber_raw(c, s); |
| 4641 | } |
| 4642 | /* Create a duplicate without underscores. */ |
| 4643 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4644 | if (dup == NULL) { |
| 4645 | return PyErr_NoMemory(); |
| 4646 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4647 | end = dup; |
| 4648 | for (; *s; s++) { |
| 4649 | if (*s != '_') { |
| 4650 | *end++ = *s; |
| 4651 | } |
| 4652 | } |
| 4653 | *end = '\0'; |
| 4654 | res = parsenumber_raw(c, dup); |
| 4655 | PyMem_Free(dup); |
| 4656 | return res; |
| 4657 | } |
| 4658 | |
| 4659 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4660 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4661 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4662 | const char *s, *t; |
| 4663 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4664 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4665 | while (s < end && (*s & 0x80)) s++; |
| 4666 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4667 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4670 | static int |
| 4671 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4672 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4673 | { |
| 4674 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4675 | first_invalid_escape_char); |
| 4676 | if (msg == NULL) { |
| 4677 | return -1; |
| 4678 | } |
Serhiy Storchaka | 4c5b6ba | 2019-08-10 01:34:22 +0300 | [diff] [blame] | 4679 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4680 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4681 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4682 | { |
Serhiy Storchaka | 4c5b6ba | 2019-08-10 01:34:22 +0300 | [diff] [blame] | 4683 | if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) { |
| 4684 | /* Replace the DeprecationWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4685 | to get a more accurate error report */ |
| 4686 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4687 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4688 | } |
| 4689 | Py_DECREF(msg); |
| 4690 | return -1; |
| 4691 | } |
| 4692 | Py_DECREF(msg); |
| 4693 | return 0; |
| 4694 | } |
| 4695 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4696 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4697 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4698 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4699 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4700 | PyObject *v, *u; |
| 4701 | char *buf; |
| 4702 | char *p; |
| 4703 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4704 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4705 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4706 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4707 | return NULL; |
| 4708 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4709 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4710 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4711 | if (u == NULL) |
| 4712 | return NULL; |
| 4713 | p = buf = PyBytes_AsString(u); |
| 4714 | end = s + len; |
| 4715 | while (s < end) { |
| 4716 | if (*s == '\\') { |
| 4717 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4718 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4719 | strcpy(p, "u005c"); |
| 4720 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4721 | if (s >= end) |
| 4722 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4723 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4724 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4725 | if (*s & 0x80) { /* XXX inefficient */ |
| 4726 | PyObject *w; |
| 4727 | int kind; |
| 4728 | void *data; |
| 4729 | Py_ssize_t len, i; |
| 4730 | w = decode_utf8(c, &s, end); |
| 4731 | if (w == NULL) { |
| 4732 | Py_DECREF(u); |
| 4733 | return NULL; |
| 4734 | } |
| 4735 | kind = PyUnicode_KIND(w); |
| 4736 | data = PyUnicode_DATA(w); |
| 4737 | len = PyUnicode_GET_LENGTH(w); |
| 4738 | for (i = 0; i < len; i++) { |
| 4739 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4740 | sprintf(p, "\\U%08x", chr); |
| 4741 | p += 10; |
| 4742 | } |
| 4743 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4744 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4745 | Py_DECREF(w); |
| 4746 | } else { |
| 4747 | *p++ = *s++; |
| 4748 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4749 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4750 | len = p - buf; |
| 4751 | s = buf; |
| 4752 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4753 | const char *first_invalid_escape; |
| 4754 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4755 | |
| 4756 | if (v != NULL && first_invalid_escape != NULL) { |
| 4757 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4758 | /* We have not decref u before because first_invalid_escape points |
| 4759 | inside u. */ |
| 4760 | Py_XDECREF(u); |
| 4761 | Py_DECREF(v); |
| 4762 | return NULL; |
| 4763 | } |
| 4764 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4765 | Py_XDECREF(u); |
| 4766 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4769 | static PyObject * |
| 4770 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4771 | size_t len) |
| 4772 | { |
| 4773 | const char *first_invalid_escape; |
| 4774 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4775 | &first_invalid_escape); |
| 4776 | if (result == NULL) |
| 4777 | return NULL; |
| 4778 | |
| 4779 | if (first_invalid_escape != NULL) { |
| 4780 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4781 | Py_DECREF(result); |
| 4782 | return NULL; |
| 4783 | } |
| 4784 | } |
| 4785 | return result; |
| 4786 | } |
| 4787 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4788 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4789 | and `col_offset` to existing locations. */ |
| 4790 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4791 | { |
| 4792 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4793 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4794 | for (int i = 0; i < NCH(n); ++i) { |
| 4795 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4796 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4797 | col_offset = 0; |
| 4798 | } |
| 4799 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4800 | } |
| 4801 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4802 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | /* Fix locations for the given node and its children. |
| 4806 | |
| 4807 | `parent` is the enclosing node. |
| 4808 | `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] | 4809 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4810 | */ |
| 4811 | static void |
| 4812 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4813 | { |
| 4814 | char *substr = NULL; |
| 4815 | char *start; |
| 4816 | int lines = LINENO(parent) - 1; |
| 4817 | int cols = parent->n_col_offset; |
| 4818 | /* Find the full fstring to fix location information in `n`. */ |
| 4819 | while (parent && parent->n_type != STRING) |
| 4820 | parent = parent->n_child; |
| 4821 | if (parent && parent->n_str) { |
| 4822 | substr = strstr(parent->n_str, expr_str); |
| 4823 | if (substr) { |
| 4824 | start = substr; |
| 4825 | while (start > parent->n_str) { |
| 4826 | if (start[0] == '\n') |
| 4827 | break; |
| 4828 | start--; |
| 4829 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4830 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4831 | /* adjust the start based on the number of newlines encountered |
| 4832 | before the f-string expression */ |
| 4833 | for (char* p = parent->n_str; p < substr; p++) { |
| 4834 | if (*p == '\n') { |
| 4835 | lines++; |
| 4836 | } |
| 4837 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | fstring_shift_node_locations(n, lines, cols); |
| 4841 | } |
| 4842 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4843 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4844 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4845 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4846 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4847 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4848 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4849 | { |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4850 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4851 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4852 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4853 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4854 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4855 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4856 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4857 | assert(*(expr_start-1) == '{'); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4858 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' || |
| 4859 | *expr_end == '='); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4860 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4861 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4862 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4863 | because turning the expression '' in to '()' would go from being invalid |
| 4864 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4865 | for (s = expr_start; s != expr_end; s++) { |
| 4866 | char c = *s; |
| 4867 | /* The Python parser ignores only the following whitespace |
| 4868 | characters (\r already is converted to \n). */ |
| 4869 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4870 | break; |
| 4871 | } |
| 4872 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4873 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4874 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4875 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4876 | } |
| 4877 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4878 | len = expr_end - expr_start; |
| 4879 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4880 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4881 | if (str == NULL) { |
| 4882 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4883 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4884 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4885 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4886 | str[0] = '('; |
| 4887 | memcpy(str+1, expr_start, len); |
| 4888 | str[len+1] = ')'; |
| 4889 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4890 | |
Miss Islington (bot) | 92e836c | 2019-06-12 17:36:03 -0700 | [diff] [blame] | 4891 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4892 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4893 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4894 | Py_eval_input, 0); |
| 4895 | if (!mod_n) { |
| 4896 | PyMem_RawFree(str); |
| 4897 | return NULL; |
| 4898 | } |
| 4899 | /* Reuse str to find the correct column offset. */ |
| 4900 | str[0] = '{'; |
| 4901 | str[len+1] = '}'; |
| 4902 | fstring_fix_node_location(n, mod_n, str); |
| 4903 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4904 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4905 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4906 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4907 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4908 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | /* Return -1 on error. |
| 4912 | |
| 4913 | Return 0 if we reached the end of the literal. |
| 4914 | |
| 4915 | Return 1 if we haven't reached the end of the literal, but we want |
| 4916 | the caller to process the literal up to this point. Used for |
| 4917 | doubled braces. |
| 4918 | */ |
| 4919 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4920 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4921 | PyObject **literal, int recurse_lvl, |
| 4922 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4923 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4924 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4925 | brace (which isn't part of a unicode name escape such as |
| 4926 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4927 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4928 | const char *s = *str; |
| 4929 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4930 | int result = 0; |
| 4931 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4932 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4933 | while (s < end) { |
| 4934 | char ch = *s++; |
| 4935 | if (!raw && ch == '\\' && s < end) { |
| 4936 | ch = *s++; |
| 4937 | if (ch == 'N') { |
| 4938 | if (s < end && *s++ == '{') { |
| 4939 | while (s < end && *s++ != '}') { |
| 4940 | } |
| 4941 | continue; |
| 4942 | } |
| 4943 | break; |
| 4944 | } |
| 4945 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4946 | return -1; |
| 4947 | } |
| 4948 | } |
| 4949 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4950 | /* Check for doubled braces, but only at the top level. If |
| 4951 | we checked at every level, then f'{0:{3}}' would fail |
| 4952 | with the two closing braces. */ |
| 4953 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4954 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4955 | /* We're going to tell the caller that the literal ends |
| 4956 | here, but that they should continue scanning. But also |
| 4957 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4958 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4959 | result = 1; |
| 4960 | goto done; |
| 4961 | } |
| 4962 | |
| 4963 | /* Where a single '{' is the start of a new expression, a |
| 4964 | single '}' is not allowed. */ |
| 4965 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4966 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4967 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4968 | return -1; |
| 4969 | } |
| 4970 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4971 | /* We're either at a '{', which means we're starting another |
| 4972 | expression; or a '}', which means we're at the end of this |
| 4973 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4974 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4975 | break; |
| 4976 | } |
| 4977 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4978 | *str = s; |
| 4979 | assert(s <= end); |
| 4980 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4981 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4982 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4983 | if (raw) |
| 4984 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4985 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4986 | NULL, NULL); |
| 4987 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4988 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4989 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4990 | if (!*literal) |
| 4991 | return -1; |
| 4992 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4993 | return result; |
| 4994 | } |
| 4995 | |
| 4996 | /* Forward declaration because parsing is recursive. */ |
| 4997 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4998 | 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] | 4999 | struct compiling *c, const node *n); |
| 5000 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5001 | /* 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] | 5002 | expression (so it must be a '{'). Returns the FormattedValue node, which |
| 5003 | includes the expression, conversion character, format_spec expression, and |
| 5004 | optionally the text of the expression (if = is used). |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5005 | |
| 5006 | Note that I don't do a perfect job here: I don't make sure that a |
| 5007 | closing brace doesn't match an opening paren, for example. It |
| 5008 | doesn't need to error on all invalid expressions, just correctly |
| 5009 | 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] | 5010 | will be caught when we parse it later. |
| 5011 | |
| 5012 | *expression is set to the expression. For an '=' "debug" expression, |
| 5013 | *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] | 5014 | including the '=' and any whitespace around it, as a string object). If |
| 5015 | not a debug expression, *expr_text set to NULL. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5016 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5017 | 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] | 5018 | PyObject **expr_text, expr_ty *expression, |
| 5019 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5020 | { |
| 5021 | /* Return -1 on error, else 0. */ |
| 5022 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5023 | const char *expr_start; |
| 5024 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5025 | expr_ty simple_expression; |
| 5026 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5027 | int conversion = -1; /* The conversion char. Use default if not |
| 5028 | specified, or !r if using = and no format |
| 5029 | spec. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5030 | |
| 5031 | /* 0 if we're not in a string, else the quote char we're trying to |
| 5032 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5033 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5034 | |
| 5035 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 5036 | int string_type = 0; |
| 5037 | |
| 5038 | /* Keep track of nesting level for braces/parens/brackets in |
| 5039 | expressions. */ |
| 5040 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5041 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5042 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5043 | *expr_text = NULL; |
| 5044 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5045 | /* Can only nest one level deep. */ |
| 5046 | if (recurse_lvl >= 2) { |
| 5047 | ast_error(c, n, "f-string: expressions nested too deeply"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5048 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | /* The first char must be a left brace, or we wouldn't have gotten |
| 5052 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5053 | assert(**str == '{'); |
| 5054 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5055 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5056 | expr_start = *str; |
| 5057 | for (; *str < end; (*str)++) { |
| 5058 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5059 | |
| 5060 | /* Loop invariants. */ |
| 5061 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5062 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5063 | if (quote_char) |
| 5064 | assert(string_type == 1 || string_type == 3); |
| 5065 | else |
| 5066 | assert(string_type == 0); |
| 5067 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5068 | ch = **str; |
| 5069 | /* Nowhere inside an expression is a backslash allowed. */ |
| 5070 | if (ch == '\\') { |
| 5071 | /* Error: can't include a backslash character, inside |
| 5072 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5073 | ast_error(c, n, |
| 5074 | "f-string expression part " |
| 5075 | "cannot include a backslash"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5076 | goto error; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5077 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5078 | if (quote_char) { |
| 5079 | /* We're inside a string. See if we're at the end. */ |
| 5080 | /* This code needs to implement the same non-error logic |
| 5081 | as tok_get from tokenizer.c, at the letter_quote |
| 5082 | label. To actually share that code would be a |
| 5083 | nightmare. But, it's unlikely to change and is small, |
| 5084 | so duplicate it here. Note we don't need to catch all |
| 5085 | of the errors, since they'll be caught when parsing the |
| 5086 | expression. We just need to match the non-error |
| 5087 | cases. Thus we can ignore \n in single-quoted strings, |
| 5088 | for example. Or non-terminated strings. */ |
| 5089 | if (ch == quote_char) { |
| 5090 | /* Does this match the string_type (single or triple |
| 5091 | quoted)? */ |
| 5092 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5093 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5094 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5095 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5096 | string_type = 0; |
| 5097 | quote_char = 0; |
| 5098 | continue; |
| 5099 | } |
| 5100 | } else { |
| 5101 | /* We're at the end of a normal string. */ |
| 5102 | quote_char = 0; |
| 5103 | string_type = 0; |
| 5104 | continue; |
| 5105 | } |
| 5106 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5107 | } else if (ch == '\'' || ch == '"') { |
| 5108 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5109 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5110 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5111 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5112 | } else { |
| 5113 | /* Start of a normal string. */ |
| 5114 | string_type = 1; |
| 5115 | } |
| 5116 | /* Start looking for the end of the string. */ |
| 5117 | quote_char = ch; |
| 5118 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5119 | if (nested_depth >= MAXLEVEL) { |
| 5120 | ast_error(c, n, "f-string: too many nested parenthesis"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5121 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5122 | } |
| 5123 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5124 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5125 | } else if (ch == '#') { |
| 5126 | /* Error: can't include a comment character, inside parens |
| 5127 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5128 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5129 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5130 | } else if (nested_depth == 0 && |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5131 | (ch == '!' || ch == ':' || ch == '}' || |
| 5132 | ch == '=' || ch == '>' || ch == '<')) { |
| 5133 | /* See if there's a next character. */ |
| 5134 | if (*str+1 < end) { |
| 5135 | char next = *(*str+1); |
| 5136 | |
| 5137 | /* For "!=". since '=' is not an allowed conversion character, |
| 5138 | nothing is lost in this test. */ |
| 5139 | if ((ch == '!' && next == '=') || /* != */ |
| 5140 | (ch == '=' && next == '=') || /* == */ |
| 5141 | (ch == '<' && next == '=') || /* <= */ |
| 5142 | (ch == '>' && next == '=') /* >= */ |
| 5143 | ) { |
| 5144 | *str += 1; |
| 5145 | continue; |
| 5146 | } |
| 5147 | /* Don't get out of the loop for these, if they're single |
| 5148 | chars (not part of 2-char tokens). If by themselves, they |
| 5149 | don't end an expression (unlike say '!'). */ |
| 5150 | if (ch == '>' || ch == '<') { |
| 5151 | continue; |
| 5152 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5153 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5154 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5155 | /* Normal way out of this loop. */ |
| 5156 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5157 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5158 | if (!nested_depth) { |
| 5159 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5160 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5161 | } |
| 5162 | nested_depth--; |
| 5163 | int opening = parenstack[nested_depth]; |
| 5164 | if (!((opening == '(' && ch == ')') || |
| 5165 | (opening == '[' && ch == ']') || |
| 5166 | (opening == '{' && ch == '}'))) |
| 5167 | { |
| 5168 | ast_error(c, n, |
| 5169 | "f-string: closing parenthesis '%c' " |
| 5170 | "does not match opening parenthesis '%c'", |
| 5171 | ch, opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5172 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5173 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5174 | } else { |
| 5175 | /* Just consume this char and loop around. */ |
| 5176 | } |
| 5177 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5178 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5179 | /* If we leave this loop in a string or with mismatched parens, we |
| 5180 | don't care. We'll get a syntax error when compiling the |
| 5181 | expression. But, we can produce a better error message, so |
| 5182 | let's just do that.*/ |
| 5183 | if (quote_char) { |
| 5184 | ast_error(c, n, "f-string: unterminated string"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5185 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5186 | } |
| 5187 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5188 | int opening = parenstack[nested_depth - 1]; |
| 5189 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5190 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5191 | } |
| 5192 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5193 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5194 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5195 | |
| 5196 | /* Compile the expression as soon as possible, so we show errors |
| 5197 | related to the expression before errors related to the |
| 5198 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5199 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5200 | if (!simple_expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5201 | goto error; |
| 5202 | |
| 5203 | /* Check for =, which puts the text value of the expression in |
| 5204 | expr_text. */ |
| 5205 | if (**str == '=') { |
| 5206 | *str += 1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5207 | |
| 5208 | /* Skip over ASCII whitespace. No need to test for end of string |
| 5209 | here, since we know there's at least a trailing quote somewhere |
| 5210 | ahead. */ |
| 5211 | while (Py_ISSPACE(**str)) { |
| 5212 | *str += 1; |
| 5213 | } |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5214 | |
| 5215 | /* Set *expr_text to the text of the expression. */ |
| 5216 | *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); |
| 5217 | if (!*expr_text) { |
| 5218 | goto error; |
| 5219 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5220 | } |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5221 | |
| 5222 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5223 | if (**str == '!') { |
| 5224 | *str += 1; |
| 5225 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5226 | goto unexpected_end_of_string; |
| 5227 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5228 | conversion = **str; |
| 5229 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5230 | |
| 5231 | /* Validate the conversion. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5232 | if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5233 | ast_error(c, n, |
| 5234 | "f-string: invalid conversion character: " |
| 5235 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5236 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5237 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5238 | |
| 5239 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5240 | |
| 5241 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5242 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5243 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5244 | if (**str == ':') { |
| 5245 | *str += 1; |
| 5246 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5247 | goto unexpected_end_of_string; |
| 5248 | |
| 5249 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5250 | 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] | 5251 | if (!format_spec) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5252 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5253 | } |
| 5254 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5255 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5256 | goto unexpected_end_of_string; |
| 5257 | |
| 5258 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5259 | assert(*str < end); |
| 5260 | assert(**str == '}'); |
| 5261 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5262 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5263 | /* If we're in = mode (detected by non-NULL expr_text), and have no format |
| 5264 | spec and no explict conversion, set the conversion to 'r'. */ |
| 5265 | if (*expr_text && format_spec == NULL && conversion == -1) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5266 | conversion = 'r'; |
| 5267 | } |
| 5268 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5269 | /* And now create the FormattedValue node that represents this |
| 5270 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5271 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5272 | format_spec, LINENO(n), |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5273 | n->n_col_offset, n->n_end_lineno, |
| 5274 | n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5275 | if (!*expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5276 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5277 | |
| 5278 | return 0; |
| 5279 | |
| 5280 | unexpected_end_of_string: |
| 5281 | ast_error(c, n, "f-string: expecting '}'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5282 | /* Falls through to error. */ |
| 5283 | |
| 5284 | error: |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5285 | Py_XDECREF(*expr_text); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5286 | return -1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5287 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | /* Return -1 on error. |
| 5291 | |
| 5292 | Return 0 if we have a literal (possible zero length) and an |
| 5293 | expression (zero length if at the end of the string. |
| 5294 | |
| 5295 | Return 1 if we have a literal, but no expression, and we want the |
| 5296 | caller to call us again. This is used to deal with doubled |
| 5297 | braces. |
| 5298 | |
| 5299 | When called multiple times on the string 'a{{b{0}c', this function |
| 5300 | will return: |
| 5301 | |
| 5302 | 1. the literal 'a{' with no expression, and a return value |
| 5303 | of 1. Despite the fact that there's no expression, the return |
| 5304 | value of 1 means we're not finished yet. |
| 5305 | |
| 5306 | 2. the literal 'b' and the expression '0', with a return value of |
| 5307 | 0. The fact that there's an expression means we're not finished. |
| 5308 | |
| 5309 | 3. literal 'c' with no expression and a return value of 0. The |
| 5310 | combination of the return value of 0 with no expression means |
| 5311 | we're finished. |
| 5312 | */ |
| 5313 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5314 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5315 | int recurse_lvl, PyObject **literal, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5316 | PyObject **expr_text, expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5317 | struct compiling *c, const node *n) |
| 5318 | { |
| 5319 | int result; |
| 5320 | |
| 5321 | assert(*literal == NULL && *expression == NULL); |
| 5322 | |
| 5323 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5324 | 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] | 5325 | if (result < 0) |
| 5326 | goto error; |
| 5327 | |
| 5328 | assert(result == 0 || result == 1); |
| 5329 | |
| 5330 | if (result == 1) |
| 5331 | /* We have a literal, but don't look at the expression. */ |
| 5332 | return 1; |
| 5333 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5334 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5335 | /* We're at the end of the string or the end of a nested |
| 5336 | f-string: no expression. The top-level error case where we |
| 5337 | expect to be at the end of the string but we're at a '}' is |
| 5338 | handled later. */ |
| 5339 | return 0; |
| 5340 | |
| 5341 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5342 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5343 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5344 | if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text, |
| 5345 | expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5346 | goto error; |
| 5347 | |
| 5348 | return 0; |
| 5349 | |
| 5350 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5351 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5352 | return -1; |
| 5353 | } |
| 5354 | |
| 5355 | #define EXPRLIST_N_CACHED 64 |
| 5356 | |
| 5357 | typedef struct { |
| 5358 | /* Incrementally build an array of expr_ty, so be used in an |
| 5359 | asdl_seq. Cache some small but reasonably sized number of |
| 5360 | expr_ty's, and then after that start dynamically allocating, |
| 5361 | doubling the number allocated each time. Note that the f-string |
| 5362 | 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] | 5363 | 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] | 5364 | fast as you add expressions in an f-string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5365 | |
| 5366 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5367 | Py_ssize_t size; /* Number we've used. */ |
| 5368 | expr_ty *p; /* Pointer to the memory we're actually |
| 5369 | using. Will point to 'data' until we |
| 5370 | start dynamically allocating. */ |
| 5371 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5372 | } ExprList; |
| 5373 | |
| 5374 | #ifdef NDEBUG |
| 5375 | #define ExprList_check_invariants(l) |
| 5376 | #else |
| 5377 | static void |
| 5378 | ExprList_check_invariants(ExprList *l) |
| 5379 | { |
| 5380 | /* Check our invariants. Make sure this object is "live", and |
| 5381 | hasn't been deallocated. */ |
| 5382 | assert(l->size >= 0); |
| 5383 | assert(l->p != NULL); |
| 5384 | if (l->size <= EXPRLIST_N_CACHED) |
| 5385 | assert(l->data == l->p); |
| 5386 | } |
| 5387 | #endif |
| 5388 | |
| 5389 | static void |
| 5390 | ExprList_Init(ExprList *l) |
| 5391 | { |
| 5392 | l->allocated = EXPRLIST_N_CACHED; |
| 5393 | l->size = 0; |
| 5394 | |
| 5395 | /* Until we start allocating dynamically, p points to data. */ |
| 5396 | l->p = l->data; |
| 5397 | |
| 5398 | ExprList_check_invariants(l); |
| 5399 | } |
| 5400 | |
| 5401 | static int |
| 5402 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5403 | { |
| 5404 | ExprList_check_invariants(l); |
| 5405 | if (l->size >= l->allocated) { |
| 5406 | /* We need to alloc (or realloc) the memory. */ |
| 5407 | Py_ssize_t new_size = l->allocated * 2; |
| 5408 | |
| 5409 | /* See if we've ever allocated anything dynamically. */ |
| 5410 | if (l->p == l->data) { |
| 5411 | Py_ssize_t i; |
| 5412 | /* We're still using the cached data. Switch to |
| 5413 | alloc-ing. */ |
| 5414 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5415 | if (!l->p) |
| 5416 | return -1; |
| 5417 | /* Copy the cached data into the new buffer. */ |
| 5418 | for (i = 0; i < l->size; i++) |
| 5419 | l->p[i] = l->data[i]; |
| 5420 | } else { |
| 5421 | /* Just realloc. */ |
| 5422 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5423 | if (!tmp) { |
| 5424 | PyMem_RawFree(l->p); |
| 5425 | l->p = NULL; |
| 5426 | return -1; |
| 5427 | } |
| 5428 | l->p = tmp; |
| 5429 | } |
| 5430 | |
| 5431 | l->allocated = new_size; |
| 5432 | assert(l->allocated == 2 * l->size); |
| 5433 | } |
| 5434 | |
| 5435 | l->p[l->size++] = exp; |
| 5436 | |
| 5437 | ExprList_check_invariants(l); |
| 5438 | return 0; |
| 5439 | } |
| 5440 | |
| 5441 | static void |
| 5442 | ExprList_Dealloc(ExprList *l) |
| 5443 | { |
| 5444 | ExprList_check_invariants(l); |
| 5445 | |
| 5446 | /* If there's been an error, or we've never dynamically allocated, |
| 5447 | do nothing. */ |
| 5448 | if (!l->p || l->p == l->data) { |
| 5449 | /* Do nothing. */ |
| 5450 | } else { |
| 5451 | /* We have dynamically allocated. Free the memory. */ |
| 5452 | PyMem_RawFree(l->p); |
| 5453 | } |
| 5454 | l->p = NULL; |
| 5455 | l->size = -1; |
| 5456 | } |
| 5457 | |
| 5458 | static asdl_seq * |
| 5459 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5460 | { |
| 5461 | asdl_seq *seq; |
| 5462 | |
| 5463 | ExprList_check_invariants(l); |
| 5464 | |
| 5465 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5466 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5467 | if (seq) { |
| 5468 | Py_ssize_t i; |
| 5469 | for (i = 0; i < l->size; i++) |
| 5470 | asdl_seq_SET(seq, i, l->p[i]); |
| 5471 | } |
| 5472 | ExprList_Dealloc(l); |
| 5473 | return seq; |
| 5474 | } |
| 5475 | |
| 5476 | /* The FstringParser is designed to add a mix of strings and |
| 5477 | f-strings, and concat them together as needed. Ultimately, it |
| 5478 | generates an expr_ty. */ |
| 5479 | typedef struct { |
| 5480 | PyObject *last_str; |
| 5481 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5482 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5483 | } FstringParser; |
| 5484 | |
| 5485 | #ifdef NDEBUG |
| 5486 | #define FstringParser_check_invariants(state) |
| 5487 | #else |
| 5488 | static void |
| 5489 | FstringParser_check_invariants(FstringParser *state) |
| 5490 | { |
| 5491 | if (state->last_str) |
| 5492 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5493 | ExprList_check_invariants(&state->expr_list); |
| 5494 | } |
| 5495 | #endif |
| 5496 | |
| 5497 | static void |
| 5498 | FstringParser_Init(FstringParser *state) |
| 5499 | { |
| 5500 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5501 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5502 | ExprList_Init(&state->expr_list); |
| 5503 | FstringParser_check_invariants(state); |
| 5504 | } |
| 5505 | |
| 5506 | static void |
| 5507 | FstringParser_Dealloc(FstringParser *state) |
| 5508 | { |
| 5509 | FstringParser_check_invariants(state); |
| 5510 | |
| 5511 | Py_XDECREF(state->last_str); |
| 5512 | ExprList_Dealloc(&state->expr_list); |
| 5513 | } |
| 5514 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5515 | /* Constants for the following */ |
| 5516 | static PyObject *u_kind; |
| 5517 | |
| 5518 | /* Compute 'kind' field for string Constant (either 'u' or None) */ |
| 5519 | static PyObject * |
| 5520 | make_kind(struct compiling *c, const node *n) |
| 5521 | { |
| 5522 | char *s = NULL; |
| 5523 | PyObject *kind = NULL; |
| 5524 | |
| 5525 | /* Find the first string literal, if any */ |
| 5526 | while (TYPE(n) != STRING) { |
| 5527 | if (NCH(n) == 0) |
| 5528 | return NULL; |
| 5529 | n = CHILD(n, 0); |
| 5530 | } |
| 5531 | REQ(n, STRING); |
| 5532 | |
| 5533 | /* If it starts with 'u', return a PyUnicode "u" string */ |
| 5534 | s = STR(n); |
| 5535 | if (s && *s == 'u') { |
| 5536 | if (!u_kind) { |
| 5537 | u_kind = PyUnicode_InternFromString("u"); |
| 5538 | if (!u_kind) |
| 5539 | return NULL; |
| 5540 | } |
| 5541 | kind = u_kind; |
| 5542 | if (PyArena_AddPyObject(c->c_arena, kind) < 0) { |
| 5543 | return NULL; |
| 5544 | } |
| 5545 | Py_INCREF(kind); |
| 5546 | } |
| 5547 | return kind; |
| 5548 | } |
| 5549 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5550 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5551 | static expr_ty |
| 5552 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5553 | { |
| 5554 | PyObject *s = *str; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5555 | PyObject *kind = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5556 | *str = NULL; |
| 5557 | assert(PyUnicode_CheckExact(s)); |
| 5558 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5559 | Py_DECREF(s); |
| 5560 | return NULL; |
| 5561 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5562 | kind = make_kind(c, n); |
| 5563 | if (kind == NULL && PyErr_Occurred()) |
| 5564 | return NULL; |
| 5565 | return Constant(s, kind, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5566 | 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] | 5567 | } |
| 5568 | |
| 5569 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5570 | decref'd. */ |
| 5571 | static int |
| 5572 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5573 | { |
| 5574 | FstringParser_check_invariants(state); |
| 5575 | |
| 5576 | assert(PyUnicode_CheckExact(str)); |
| 5577 | |
| 5578 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5579 | Py_DECREF(str); |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
| 5583 | if (!state->last_str) { |
| 5584 | /* We didn't have a string before, so just remember this one. */ |
| 5585 | state->last_str = str; |
| 5586 | } else { |
| 5587 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5588 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5589 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5590 | return -1; |
| 5591 | } |
| 5592 | FstringParser_check_invariants(state); |
| 5593 | return 0; |
| 5594 | } |
| 5595 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5596 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5597 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5598 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5599 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5600 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5601 | struct compiling *c, const node *n) |
| 5602 | { |
| 5603 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5604 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5605 | |
| 5606 | /* Parse the f-string. */ |
| 5607 | while (1) { |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5608 | PyObject *literal = NULL; |
| 5609 | PyObject *expr_text = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5610 | expr_ty expression = NULL; |
| 5611 | |
| 5612 | /* If there's a zero length literal in front of the |
| 5613 | expression, literal will be NULL. If we're at the end of |
| 5614 | the f-string, expression will be NULL (unless result == 1, |
| 5615 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5616 | 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] | 5617 | &literal, &expr_text, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5618 | &expression, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5619 | if (result < 0) |
| 5620 | return -1; |
| 5621 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5622 | /* Add the literal, if any. */ |
| 5623 | if (literal && FstringParser_ConcatAndDel(state, literal) < 0) { |
| 5624 | Py_XDECREF(expr_text); |
| 5625 | return -1; |
| 5626 | } |
| 5627 | /* Add the expr_text, if any. */ |
| 5628 | if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) { |
| 5629 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5630 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5631 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5632 | /* We've dealt with the literal and expr_text, their ownership has |
| 5633 | 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] | 5634 | |
| 5635 | /* See if we should just loop around to get the next literal |
| 5636 | and expression, while ignoring the expression this |
| 5637 | time. This is used for un-doubling braces, as an |
| 5638 | optimization. */ |
| 5639 | if (result == 1) |
| 5640 | continue; |
| 5641 | |
| 5642 | if (!expression) |
| 5643 | /* We're done with this f-string. */ |
| 5644 | break; |
| 5645 | |
| 5646 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5647 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5648 | if (!state->last_str) { |
| 5649 | /* Do nothing. No previous literal. */ |
| 5650 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5651 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5652 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5653 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5654 | return -1; |
| 5655 | } |
| 5656 | |
| 5657 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5658 | return -1; |
| 5659 | } |
| 5660 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5661 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5662 | string. Otherwise, we must be at a right brace. */ |
| 5663 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5664 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5665 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5666 | return -1; |
| 5667 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5668 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5669 | ast_error(c, n, "f-string: expecting '}'"); |
| 5670 | return -1; |
| 5671 | } |
| 5672 | |
| 5673 | FstringParser_check_invariants(state); |
| 5674 | return 0; |
| 5675 | } |
| 5676 | |
| 5677 | /* 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] | 5678 | 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] | 5679 | static expr_ty |
| 5680 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5681 | const node *n) |
| 5682 | { |
| 5683 | asdl_seq *seq; |
| 5684 | |
| 5685 | FstringParser_check_invariants(state); |
| 5686 | |
| 5687 | /* If we're just a constant string with no expressions, return |
| 5688 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5689 | if (!state->fmode) { |
| 5690 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5691 | if (!state->last_str) { |
| 5692 | /* Create a zero length string. */ |
| 5693 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5694 | if (!state->last_str) |
| 5695 | goto error; |
| 5696 | } |
| 5697 | return make_str_node_and_del(&state->last_str, c, n); |
| 5698 | } |
| 5699 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5700 | /* 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] | 5701 | last node in our expression list. */ |
| 5702 | if (state->last_str) { |
| 5703 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5704 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5705 | goto error; |
| 5706 | } |
| 5707 | /* This has already been freed. */ |
| 5708 | assert(state->last_str == NULL); |
| 5709 | |
| 5710 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5711 | if (!seq) |
| 5712 | goto error; |
| 5713 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5714 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5715 | 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] | 5716 | |
| 5717 | error: |
| 5718 | FstringParser_Dealloc(state); |
| 5719 | return NULL; |
| 5720 | } |
| 5721 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5722 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5723 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5724 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5725 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5726 | 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] | 5727 | struct compiling *c, const node *n) |
| 5728 | { |
| 5729 | FstringParser state; |
| 5730 | |
| 5731 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5732 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5733 | c, n) < 0) { |
| 5734 | FstringParser_Dealloc(&state); |
| 5735 | return NULL; |
| 5736 | } |
| 5737 | |
| 5738 | return FstringParser_Finish(&state, c, n); |
| 5739 | } |
| 5740 | |
| 5741 | /* n is a Python string literal, including the bracketing quote |
| 5742 | 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] | 5743 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5744 | 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] | 5745 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5746 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5747 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5748 | static int |
| 5749 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5750 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5751 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5752 | size_t len; |
| 5753 | const char *s = STR(n); |
| 5754 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5755 | int fmode = 0; |
| 5756 | *bytesmode = 0; |
| 5757 | *rawmode = 0; |
| 5758 | *result = NULL; |
| 5759 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5760 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5761 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5762 | if (quote == 'b' || quote == 'B') { |
| 5763 | quote = *++s; |
| 5764 | *bytesmode = 1; |
| 5765 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5766 | else if (quote == 'u' || quote == 'U') { |
| 5767 | quote = *++s; |
| 5768 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5769 | else if (quote == 'r' || quote == 'R') { |
| 5770 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5771 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5772 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5773 | else if (quote == 'f' || quote == 'F') { |
| 5774 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5775 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5776 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5777 | else { |
| 5778 | break; |
| 5779 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5780 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5781 | } |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 5782 | |
| 5783 | /* fstrings are only allowed in Python 3.6 and greater */ |
| 5784 | if (fmode && c->c_feature_version < 6) { |
| 5785 | ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); |
| 5786 | return -1; |
| 5787 | } |
| 5788 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5789 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5790 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5791 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5792 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5793 | if (quote != '\'' && quote != '\"') { |
| 5794 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5795 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5796 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5797 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5798 | s++; |
| 5799 | len = strlen(s); |
| 5800 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5801 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5802 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5803 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5804 | } |
| 5805 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5806 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5807 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5808 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5809 | } |
| 5810 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5811 | /* A triple quoted string. We've already skipped one quote at |
| 5812 | the start and one at the end of the string. Now skip the |
| 5813 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5814 | s += 2; |
| 5815 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5816 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5817 | if (s[--len] != quote || s[--len] != quote) { |
| 5818 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5819 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5820 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5821 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5822 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5823 | if (fmode) { |
| 5824 | /* Just return the bytes. The caller will parse the resulting |
| 5825 | string. */ |
| 5826 | *fstr = s; |
| 5827 | *fstrlen = len; |
| 5828 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5829 | } |
| 5830 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5831 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5832 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5833 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5834 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5835 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5836 | const char *ch; |
| 5837 | for (ch = s; *ch; ch++) { |
| 5838 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5839 | ast_error(c, n, |
| 5840 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5841 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5842 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5843 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5844 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5845 | if (*rawmode) |
| 5846 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5847 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5848 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5849 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5850 | if (*rawmode) |
| 5851 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5852 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5853 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5854 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5855 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5858 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5859 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5860 | 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] | 5861 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5862 | 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] | 5863 | node if there's just an f-string (with no leading or trailing |
| 5864 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5865 | any literals involved. */ |
| 5866 | static expr_ty |
| 5867 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5868 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5869 | int bytesmode = 0; |
| 5870 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5871 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5872 | |
| 5873 | FstringParser state; |
| 5874 | FstringParser_Init(&state); |
| 5875 | |
| 5876 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5877 | int this_bytesmode; |
| 5878 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5879 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5880 | const char *fstr; |
| 5881 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5882 | |
| 5883 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5884 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5885 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5886 | goto error; |
| 5887 | |
| 5888 | /* Check that we're not mixing bytes with unicode. */ |
| 5889 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5890 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5891 | /* s is NULL if the current string part is an f-string. */ |
| 5892 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5893 | goto error; |
| 5894 | } |
| 5895 | bytesmode = this_bytesmode; |
| 5896 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5897 | if (fstr != NULL) { |
| 5898 | int result; |
| 5899 | assert(s == NULL && !bytesmode); |
| 5900 | /* This is an f-string. Parse and concatenate it. */ |
| 5901 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5902 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5903 | if (result < 0) |
| 5904 | goto error; |
| 5905 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5906 | /* A string or byte string. */ |
| 5907 | assert(s != NULL && fstr == NULL); |
| 5908 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5909 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5910 | PyUnicode_CheckExact(s)); |
| 5911 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5912 | if (bytesmode) { |
| 5913 | /* For bytes, concat as we go. */ |
| 5914 | if (i == 0) { |
| 5915 | /* First time, just remember this value. */ |
| 5916 | bytes_str = s; |
| 5917 | } else { |
| 5918 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5919 | if (!bytes_str) |
| 5920 | goto error; |
| 5921 | } |
| 5922 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5923 | /* This is a regular string. Concatenate it. */ |
| 5924 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5925 | goto error; |
| 5926 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5927 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5928 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5929 | if (bytesmode) { |
| 5930 | /* Just return the bytes object and we're done. */ |
| 5931 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5932 | goto error; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5933 | return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5934 | 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] | 5935 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5936 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5937 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5938 | assert(bytes_str == NULL); |
| 5939 | |
| 5940 | return FstringParser_Finish(&state, c, n); |
| 5941 | |
| 5942 | error: |
| 5943 | Py_XDECREF(bytes_str); |
| 5944 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5945 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5946 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5947 | |
| 5948 | PyObject * |
| 5949 | _PyAST_GetDocString(asdl_seq *body) |
| 5950 | { |
| 5951 | if (!asdl_seq_LEN(body)) { |
| 5952 | return NULL; |
| 5953 | } |
| 5954 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5955 | if (st->kind != Expr_kind) { |
| 5956 | return NULL; |
| 5957 | } |
| 5958 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5959 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5960 | return e->v.Constant.value; |
| 5961 | } |
| 5962 | return NULL; |
| 5963 | } |