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"; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 97 | case NamedStore: |
| 98 | return "NamedStore"; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 99 | case Del: |
| 100 | return "Del"; |
| 101 | case AugLoad: |
| 102 | return "AugLoad"; |
| 103 | case AugStore: |
| 104 | return "AugStore"; |
| 105 | case Param: |
| 106 | return "Param"; |
| 107 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 108 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | validate_arguments(arguments_ty args) |
| 114 | { |
| 115 | if (!validate_args(args->args)) |
| 116 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 117 | if (args->vararg && args->vararg->annotation |
| 118 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | if (!validate_args(args->kwonlyargs)) |
| 122 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 123 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 124 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 128 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 129 | return 0; |
| 130 | } |
| 131 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 132 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 133 | "kw_defaults on arguments"); |
| 134 | return 0; |
| 135 | } |
| 136 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 137 | } |
| 138 | |
| 139 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 140 | validate_constant(PyObject *value) |
| 141 | { |
| 142 | if (value == Py_None || value == Py_Ellipsis) |
| 143 | return 1; |
| 144 | |
| 145 | if (PyLong_CheckExact(value) |
| 146 | || PyFloat_CheckExact(value) |
| 147 | || PyComplex_CheckExact(value) |
| 148 | || PyBool_Check(value) |
| 149 | || PyUnicode_CheckExact(value) |
| 150 | || PyBytes_CheckExact(value)) |
| 151 | return 1; |
| 152 | |
| 153 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 154 | PyObject *it; |
| 155 | |
| 156 | it = PyObject_GetIter(value); |
| 157 | if (it == NULL) |
| 158 | return 0; |
| 159 | |
| 160 | while (1) { |
| 161 | PyObject *item = PyIter_Next(it); |
| 162 | if (item == NULL) { |
| 163 | if (PyErr_Occurred()) { |
| 164 | Py_DECREF(it); |
| 165 | return 0; |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | if (!validate_constant(item)) { |
| 171 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 172 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 173 | return 0; |
| 174 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 175 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | Py_DECREF(it); |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 186 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 187 | { |
| 188 | int check_ctx = 1; |
| 189 | expr_context_ty actual_ctx; |
| 190 | |
| 191 | /* First check expression context. */ |
| 192 | switch (exp->kind) { |
| 193 | case Attribute_kind: |
| 194 | actual_ctx = exp->v.Attribute.ctx; |
| 195 | break; |
| 196 | case Subscript_kind: |
| 197 | actual_ctx = exp->v.Subscript.ctx; |
| 198 | break; |
| 199 | case Starred_kind: |
| 200 | actual_ctx = exp->v.Starred.ctx; |
| 201 | break; |
| 202 | case Name_kind: |
| 203 | actual_ctx = exp->v.Name.ctx; |
| 204 | break; |
| 205 | case List_kind: |
| 206 | actual_ctx = exp->v.List.ctx; |
| 207 | break; |
| 208 | case Tuple_kind: |
| 209 | actual_ctx = exp->v.Tuple.ctx; |
| 210 | break; |
| 211 | default: |
| 212 | if (ctx != Load) { |
| 213 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 214 | "assigned to in %s context", expr_context_name(ctx)); |
| 215 | return 0; |
| 216 | } |
| 217 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 218 | /* set actual_ctx to prevent gcc warning */ |
| 219 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 220 | } |
| 221 | if (check_ctx && actual_ctx != ctx) { |
| 222 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 223 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* Now validate expression. */ |
| 228 | switch (exp->kind) { |
| 229 | case BoolOp_kind: |
| 230 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 231 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 232 | return 0; |
| 233 | } |
| 234 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 235 | case BinOp_kind: |
| 236 | return validate_expr(exp->v.BinOp.left, Load) && |
| 237 | validate_expr(exp->v.BinOp.right, Load); |
| 238 | case UnaryOp_kind: |
| 239 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 240 | case Lambda_kind: |
| 241 | return validate_arguments(exp->v.Lambda.args) && |
| 242 | validate_expr(exp->v.Lambda.body, Load); |
| 243 | case IfExp_kind: |
| 244 | return validate_expr(exp->v.IfExp.test, Load) && |
| 245 | validate_expr(exp->v.IfExp.body, Load) && |
| 246 | validate_expr(exp->v.IfExp.orelse, Load); |
| 247 | case Dict_kind: |
| 248 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 249 | PyErr_SetString(PyExc_ValueError, |
| 250 | "Dict doesn't have the same number of keys as values"); |
| 251 | return 0; |
| 252 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 253 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 254 | dict literals, i.e. ``{**{a:b}}`` */ |
| 255 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 256 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 257 | case Set_kind: |
| 258 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 259 | #define COMP(NAME) \ |
| 260 | case NAME ## _kind: \ |
| 261 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 262 | validate_expr(exp->v.NAME.elt, Load); |
| 263 | COMP(ListComp) |
| 264 | COMP(SetComp) |
| 265 | COMP(GeneratorExp) |
| 266 | #undef COMP |
| 267 | case DictComp_kind: |
| 268 | return validate_comprehension(exp->v.DictComp.generators) && |
| 269 | validate_expr(exp->v.DictComp.key, Load) && |
| 270 | validate_expr(exp->v.DictComp.value, Load); |
| 271 | case Yield_kind: |
| 272 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 273 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 274 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 275 | case Await_kind: |
| 276 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 277 | case Compare_kind: |
| 278 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 279 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 280 | return 0; |
| 281 | } |
| 282 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 283 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 284 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 285 | "of comparators and operands"); |
| 286 | return 0; |
| 287 | } |
| 288 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 289 | validate_expr(exp->v.Compare.left, Load); |
| 290 | case Call_kind: |
| 291 | return validate_expr(exp->v.Call.func, Load) && |
| 292 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 293 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 294 | case Constant_kind: |
| 295 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 296 | PyErr_Format(PyExc_TypeError, |
| 297 | "got an invalid type in Constant: %s", |
| 298 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 302 | case JoinedStr_kind: |
| 303 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 304 | case FormattedValue_kind: |
| 305 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 306 | return 0; |
| 307 | if (exp->v.FormattedValue.format_spec) |
| 308 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 309 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 310 | case Attribute_kind: |
| 311 | return validate_expr(exp->v.Attribute.value, Load); |
| 312 | case Subscript_kind: |
| 313 | return validate_slice(exp->v.Subscript.slice) && |
| 314 | validate_expr(exp->v.Subscript.value, Load); |
| 315 | case Starred_kind: |
| 316 | return validate_expr(exp->v.Starred.value, ctx); |
| 317 | case List_kind: |
| 318 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 319 | case Tuple_kind: |
| 320 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 321 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 322 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 323 | return 1; |
| 324 | default: |
| 325 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 326 | return 0; |
| 327 | } |
| 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. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 572 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 573 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 574 | 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] | 575 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 576 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 577 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 578 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 579 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 580 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 581 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 582 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 583 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 584 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 585 | const node *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 586 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 587 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 588 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 589 | static void get_last_end_pos(asdl_seq *, int *, int *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 590 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 591 | #define COMP_GENEXP 0 |
| 592 | #define COMP_LISTCOMP 1 |
| 593 | #define COMP_SETCOMP 2 |
| 594 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 595 | static int |
| 596 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 597 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 598 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 599 | if (!m) |
| 600 | return 0; |
| 601 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 602 | Py_DECREF(m); |
| 603 | if (!c->c_normalize) |
| 604 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 609 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 610 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 611 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 612 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 613 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 614 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 615 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 616 | /* Check whether there are non-ASCII characters in the |
| 617 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 618 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 619 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 620 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 621 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 622 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 623 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 624 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 625 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 626 | if (form == NULL) { |
| 627 | Py_DECREF(id); |
| 628 | return NULL; |
| 629 | } |
| 630 | PyObject *args[2] = {form, id}; |
| 631 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 632 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 633 | if (!id2) |
| 634 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 635 | if (!PyUnicode_Check(id2)) { |
| 636 | PyErr_Format(PyExc_TypeError, |
| 637 | "unicodedata.normalize() must return a string, not " |
| 638 | "%.200s", |
| 639 | Py_TYPE(id2)->tp_name); |
| 640 | Py_DECREF(id2); |
| 641 | return NULL; |
| 642 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 643 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 644 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 645 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 646 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 647 | Py_DECREF(id); |
| 648 | return NULL; |
| 649 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 650 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 653 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 655 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 656 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 657 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 658 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 659 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 661 | va_start(va, errmsg); |
| 662 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 663 | va_end(va); |
| 664 | if (!errstr) { |
| 665 | return 0; |
| 666 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 667 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 669 | Py_INCREF(Py_None); |
| 670 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 671 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 672 | 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] | 673 | if (!tmp) { |
| 674 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 675 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 676 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 677 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 678 | Py_DECREF(errstr); |
| 679 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 680 | if (value) { |
| 681 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 682 | Py_DECREF(value); |
| 683 | } |
| 684 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* num_stmts() returns number of contained statements. |
| 688 | |
| 689 | Use this routine to determine how big a sequence is needed for |
| 690 | the statements in a parse tree. Its raison d'etre is this bit of |
| 691 | grammar: |
| 692 | |
| 693 | stmt: simple_stmt | compound_stmt |
| 694 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 695 | |
| 696 | A simple_stmt can contain multiple small_stmt elements joined |
| 697 | by semicolons. If the arg is a simple_stmt, the number of |
| 698 | small_stmt elements is returned. |
| 699 | */ |
| 700 | |
| 701 | static int |
| 702 | num_stmts(const node *n) |
| 703 | { |
| 704 | int i, l; |
| 705 | node *ch; |
| 706 | |
| 707 | switch (TYPE(n)) { |
| 708 | case single_input: |
| 709 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 710 | return 0; |
| 711 | else |
| 712 | return num_stmts(CHILD(n, 0)); |
| 713 | case file_input: |
| 714 | l = 0; |
| 715 | for (i = 0; i < NCH(n); i++) { |
| 716 | ch = CHILD(n, i); |
| 717 | if (TYPE(ch) == stmt) |
| 718 | l += num_stmts(ch); |
| 719 | } |
| 720 | return l; |
| 721 | case stmt: |
| 722 | return num_stmts(CHILD(n, 0)); |
| 723 | case compound_stmt: |
| 724 | return 1; |
| 725 | case simple_stmt: |
| 726 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 727 | case suite: |
| 728 | if (NCH(n) == 1) |
| 729 | return num_stmts(CHILD(n, 0)); |
| 730 | else { |
| 731 | l = 0; |
| 732 | for (i = 2; i < (NCH(n) - 1); i++) |
| 733 | l += num_stmts(CHILD(n, i)); |
| 734 | return l; |
| 735 | } |
| 736 | default: { |
| 737 | char buf[128]; |
| 738 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 739 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 740 | TYPE(n), NCH(n)); |
| 741 | Py_FatalError(buf); |
| 742 | } |
| 743 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 744 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* Transform the CST rooted at node * to the appropriate AST |
| 748 | */ |
| 749 | |
| 750 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 751 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 752 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 753 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 754 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 755 | asdl_seq *stmts = NULL; |
| 756 | stmt_ty s; |
| 757 | node *ch; |
| 758 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 759 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 760 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 761 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 762 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 763 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 764 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 765 | |
| 766 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 769 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 770 | switch (TYPE(n)) { |
| 771 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 772 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 774 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 775 | for (i = 0; i < NCH(n) - 1; i++) { |
| 776 | ch = CHILD(n, i); |
| 777 | if (TYPE(ch) == NEWLINE) |
| 778 | continue; |
| 779 | REQ(ch, stmt); |
| 780 | num = num_stmts(ch); |
| 781 | if (num == 1) { |
| 782 | s = ast_for_stmt(&c, ch); |
| 783 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 784 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 785 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 786 | } |
| 787 | else { |
| 788 | ch = CHILD(ch, 0); |
| 789 | REQ(ch, simple_stmt); |
| 790 | for (j = 0; j < num; j++) { |
| 791 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 792 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 793 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 794 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 798 | res = Module(stmts, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 799 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 800 | case eval_input: { |
| 801 | expr_ty testlist_ast; |
| 802 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 803 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 804 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 806 | goto out; |
| 807 | res = Expression(testlist_ast, arena); |
| 808 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 809 | } |
| 810 | case single_input: |
| 811 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 812 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 813 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 814 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 815 | 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] | 816 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 817 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 818 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 819 | goto out; |
| 820 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 821 | } |
| 822 | else { |
| 823 | n = CHILD(n, 0); |
| 824 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 825 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 826 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 827 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 828 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 829 | s = ast_for_stmt(&c, n); |
| 830 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 831 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 832 | asdl_seq_SET(stmts, 0, s); |
| 833 | } |
| 834 | else { |
| 835 | /* Only a simple_stmt can contain multiple statements. */ |
| 836 | REQ(n, simple_stmt); |
| 837 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 838 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 839 | break; |
| 840 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 841 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 842 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 843 | asdl_seq_SET(stmts, i / 2, s); |
| 844 | } |
| 845 | } |
| 846 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 847 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 848 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 849 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 850 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 851 | PyErr_Format(PyExc_SystemError, |
| 852 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 853 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 854 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 855 | out: |
| 856 | if (c.c_normalize) { |
| 857 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 858 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 859 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 862 | mod_ty |
| 863 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 864 | PyArena *arena) |
| 865 | { |
| 866 | mod_ty mod; |
| 867 | PyObject *filename; |
| 868 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 869 | if (filename == NULL) |
| 870 | return NULL; |
| 871 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 872 | Py_DECREF(filename); |
| 873 | return mod; |
| 874 | |
| 875 | } |
| 876 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 877 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 878 | */ |
| 879 | |
| 880 | static operator_ty |
| 881 | get_operator(const node *n) |
| 882 | { |
| 883 | switch (TYPE(n)) { |
| 884 | case VBAR: |
| 885 | return BitOr; |
| 886 | case CIRCUMFLEX: |
| 887 | return BitXor; |
| 888 | case AMPER: |
| 889 | return BitAnd; |
| 890 | case LEFTSHIFT: |
| 891 | return LShift; |
| 892 | case RIGHTSHIFT: |
| 893 | return RShift; |
| 894 | case PLUS: |
| 895 | return Add; |
| 896 | case MINUS: |
| 897 | return Sub; |
| 898 | case STAR: |
| 899 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 900 | case AT: |
| 901 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 902 | case SLASH: |
| 903 | return Div; |
| 904 | case DOUBLESLASH: |
| 905 | return FloorDiv; |
| 906 | case PERCENT: |
| 907 | return Mod; |
| 908 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 909 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 913 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 914 | "None", |
| 915 | "True", |
| 916 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 917 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 918 | NULL, |
| 919 | }; |
| 920 | |
| 921 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 922 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 923 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 924 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 925 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 926 | const char * const *p = FORBIDDEN; |
| 927 | if (!full_checks) { |
| 928 | /* In most cases, the parser will protect True, False, and None |
| 929 | from being assign to. */ |
| 930 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 931 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 932 | for (; *p; p++) { |
| 933 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 934 | ast_error(c, n, "cannot assign to %U", name); |
| 935 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 941 | static expr_ty |
| 942 | copy_location(expr_ty e, const node *n) |
| 943 | { |
| 944 | if (e) { |
| 945 | e->lineno = LINENO(n); |
| 946 | e->col_offset = n->n_col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 947 | e->end_lineno = n->n_end_lineno; |
| 948 | e->end_col_offset = n->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 949 | } |
| 950 | return e; |
| 951 | } |
| 952 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 953 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 954 | |
| 955 | Only sets context for expr kinds that "can appear in assignment context" |
| 956 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 957 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 958 | */ |
| 959 | |
| 960 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 961 | 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] | 962 | { |
| 963 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 964 | /* If a particular expression type can't be used for assign / delete, |
| 965 | set expr_name to its name and an error message will be generated. |
| 966 | */ |
| 967 | const char* expr_name = NULL; |
| 968 | |
| 969 | /* The ast defines augmented store and load contexts, but the |
| 970 | implementation here doesn't actually use them. The code may be |
| 971 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 972 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 973 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 974 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 975 | */ |
| 976 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 977 | |
| 978 | switch (e->kind) { |
| 979 | case Attribute_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 980 | if (ctx == NamedStore) { |
| 981 | expr_name = "attribute"; |
| 982 | break; |
| 983 | } |
| 984 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 985 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 986 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 987 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 988 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 989 | case Subscript_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 990 | if (ctx == NamedStore) { |
| 991 | expr_name = "subscript"; |
| 992 | break; |
| 993 | } |
| 994 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 995 | e->v.Subscript.ctx = ctx; |
| 996 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 997 | case Starred_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 998 | if (ctx == NamedStore) { |
| 999 | expr_name = "starred"; |
| 1000 | break; |
| 1001 | } |
| 1002 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1003 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1004 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1005 | return 0; |
| 1006 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1007 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1008 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1009 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1010 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1011 | } |
| 1012 | e->v.Name.ctx = ctx; |
| 1013 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1014 | case List_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1015 | if (ctx == NamedStore) { |
| 1016 | expr_name = "list"; |
| 1017 | break; |
| 1018 | } |
| 1019 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1020 | e->v.List.ctx = ctx; |
| 1021 | s = e->v.List.elts; |
| 1022 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1023 | case Tuple_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1024 | if (ctx == NamedStore) { |
| 1025 | expr_name = "tuple"; |
| 1026 | break; |
| 1027 | } |
| 1028 | |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1029 | e->v.Tuple.ctx = ctx; |
| 1030 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1031 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1032 | case Lambda_kind: |
| 1033 | expr_name = "lambda"; |
| 1034 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1035 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1036 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1037 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1038 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1039 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1040 | case UnaryOp_kind: |
| 1041 | expr_name = "operator"; |
| 1042 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1043 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1044 | expr_name = "generator expression"; |
| 1045 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1046 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1047 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1048 | expr_name = "yield expression"; |
| 1049 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1050 | case Await_kind: |
| 1051 | expr_name = "await expression"; |
| 1052 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1053 | case ListComp_kind: |
| 1054 | expr_name = "list comprehension"; |
| 1055 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1056 | case SetComp_kind: |
| 1057 | expr_name = "set comprehension"; |
| 1058 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1059 | case DictComp_kind: |
| 1060 | expr_name = "dict comprehension"; |
| 1061 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1062 | case Dict_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1063 | expr_name = "dict display"; |
| 1064 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1065 | case Set_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1066 | expr_name = "set display"; |
| 1067 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1068 | case JoinedStr_kind: |
| 1069 | case FormattedValue_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1070 | expr_name = "f-string expression"; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1071 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1072 | case Constant_kind: { |
| 1073 | PyObject *value = e->v.Constant.value; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1074 | if (value == Py_None || value == Py_False || value == Py_True |
| 1075 | || value == Py_Ellipsis) |
| 1076 | { |
| 1077 | return ast_error(c, n, "cannot %s %R", |
| 1078 | ctx == Store ? "assign to" : "delete", |
| 1079 | value); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1080 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1081 | expr_name = "literal"; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1082 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1083 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1084 | case Compare_kind: |
| 1085 | expr_name = "comparison"; |
| 1086 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1087 | case IfExp_kind: |
| 1088 | expr_name = "conditional expression"; |
| 1089 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1090 | case NamedExpr_kind: |
| 1091 | expr_name = "named expression"; |
| 1092 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1093 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | PyErr_Format(PyExc_SystemError, |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1095 | "unexpected expression in %sassignment %d (line %d)", |
| 1096 | ctx == NamedStore ? "named ": "", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1097 | e->kind, e->lineno); |
| 1098 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1099 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1100 | /* Check for error string set by switch */ |
| 1101 | if (expr_name) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1102 | if (ctx == NamedStore) { |
| 1103 | return ast_error(c, n, "cannot use named assignment with %s", |
| 1104 | expr_name); |
| 1105 | } |
| 1106 | else { |
| 1107 | return ast_error(c, n, "cannot %s %s", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1108 | ctx == Store ? "assign to" : "delete", |
| 1109 | expr_name); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1110 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1113 | /* 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] | 1114 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1115 | */ |
| 1116 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1117 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1118 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1119 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1120 | 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] | 1121 | return 0; |
| 1122 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | } |
| 1124 | return 1; |
| 1125 | } |
| 1126 | |
| 1127 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1128 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1129 | { |
| 1130 | REQ(n, augassign); |
| 1131 | n = CHILD(n, 0); |
| 1132 | switch (STR(n)[0]) { |
| 1133 | case '+': |
| 1134 | return Add; |
| 1135 | case '-': |
| 1136 | return Sub; |
| 1137 | case '/': |
| 1138 | if (STR(n)[1] == '/') |
| 1139 | return FloorDiv; |
| 1140 | else |
| 1141 | return Div; |
| 1142 | case '%': |
| 1143 | return Mod; |
| 1144 | case '<': |
| 1145 | return LShift; |
| 1146 | case '>': |
| 1147 | return RShift; |
| 1148 | case '&': |
| 1149 | return BitAnd; |
| 1150 | case '^': |
| 1151 | return BitXor; |
| 1152 | case '|': |
| 1153 | return BitOr; |
| 1154 | case '*': |
| 1155 | if (STR(n)[1] == '*') |
| 1156 | return Pow; |
| 1157 | else |
| 1158 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1159 | case '@': |
| 1160 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1161 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1162 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1163 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1168 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1169 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1170 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | |'is' 'not' |
| 1172 | */ |
| 1173 | REQ(n, comp_op); |
| 1174 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1175 | n = CHILD(n, 0); |
| 1176 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 | case LESS: |
| 1178 | return Lt; |
| 1179 | case GREATER: |
| 1180 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1181 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | return Eq; |
| 1183 | case LESSEQUAL: |
| 1184 | return LtE; |
| 1185 | case GREATEREQUAL: |
| 1186 | return GtE; |
| 1187 | case NOTEQUAL: |
| 1188 | return NotEq; |
| 1189 | case NAME: |
| 1190 | if (strcmp(STR(n), "in") == 0) |
| 1191 | return In; |
| 1192 | if (strcmp(STR(n), "is") == 0) |
| 1193 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1194 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1195 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1196 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1197 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1198 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1199 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1200 | } |
| 1201 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1202 | /* handle "not in" and "is not" */ |
| 1203 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1204 | case NAME: |
| 1205 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1206 | return NotIn; |
| 1207 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1208 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1209 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1210 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1211 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1212 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1213 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1214 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1216 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1217 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1218 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static asdl_seq * |
| 1222 | seq_for_testlist(struct compiling *c, const node *n) |
| 1223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1225 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1226 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1227 | asdl_seq *seq; |
| 1228 | expr_ty expression; |
| 1229 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1230 | 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] | 1231 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1232 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1233 | if (!seq) |
| 1234 | return NULL; |
| 1235 | |
| 1236 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1238 | 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] | 1239 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1240 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1241 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1242 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1243 | |
| 1244 | assert(i / 2 < seq->size); |
| 1245 | asdl_seq_SET(seq, i / 2, expression); |
| 1246 | } |
| 1247 | return seq; |
| 1248 | } |
| 1249 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1250 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1251 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1252 | { |
| 1253 | identifier name; |
| 1254 | expr_ty annotation = NULL; |
| 1255 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1256 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1258 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1259 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1260 | name = NEW_IDENTIFIER(ch); |
| 1261 | if (!name) |
| 1262 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1263 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1264 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1267 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1268 | if (!annotation) |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1272 | ret = arg(name, annotation, LINENO(n), n->n_col_offset, |
| 1273 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1274 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1275 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1276 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1279 | /* returns -1 if failed to handle keyword only arguments |
| 1280 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1281 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1282 | ^^^ |
| 1283 | start pointing here |
| 1284 | */ |
| 1285 | static int |
| 1286 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1287 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1288 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1289 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1290 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1291 | expr_ty expression, annotation; |
| 1292 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1293 | int i = start; |
| 1294 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1295 | |
| 1296 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1297 | 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] | 1298 | return -1; |
| 1299 | } |
| 1300 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1301 | while (i < NCH(n)) { |
| 1302 | ch = CHILD(n, i); |
| 1303 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1304 | case vfpdef: |
| 1305 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1306 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1307 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1308 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1309 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1310 | asdl_seq_SET(kwdefaults, j, expression); |
| 1311 | i += 2; /* '=' and test */ |
| 1312 | } |
| 1313 | else { /* setting NULL if no default value exists */ |
| 1314 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1315 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1316 | if (NCH(ch) == 3) { |
| 1317 | /* ch is NAME ':' test */ |
| 1318 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1319 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1320 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1321 | } |
| 1322 | else { |
| 1323 | annotation = NULL; |
| 1324 | } |
| 1325 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1326 | argname = NEW_IDENTIFIER(ch); |
| 1327 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1328 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1329 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1330 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1331 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1332 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1333 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1334 | if (!arg) |
| 1335 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1336 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1337 | i += 2; /* the name and the comma */ |
| 1338 | break; |
| 1339 | case DOUBLESTAR: |
| 1340 | return i; |
| 1341 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1342 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1343 | goto error; |
| 1344 | } |
| 1345 | } |
| 1346 | return i; |
| 1347 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1349 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1350 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1351 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1352 | |
| 1353 | static arguments_ty |
| 1354 | ast_for_arguments(struct compiling *c, const node *n) |
| 1355 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1356 | /* This function handles both typedargslist (function definition) |
| 1357 | and varargslist (lambda definition). |
| 1358 | |
| 1359 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1360 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1361 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1362 | | '**' tfpdef [',']]] |
| 1363 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1364 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1365 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1366 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1367 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1368 | | '**' vfpdef [',']]] |
| 1369 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1370 | | '**' vfpdef [','] |
| 1371 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1372 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1373 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1374 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1375 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1376 | int nposdefaults = 0, found_default = 0; |
| 1377 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1378 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1379 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1380 | node *ch; |
| 1381 | |
| 1382 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1383 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1384 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1385 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1386 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1387 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1388 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1389 | /* First count the number of positional args & defaults. The |
| 1390 | variable i is the loop index for this for loop and the next. |
| 1391 | The next loop picks up where the first leaves off. |
| 1392 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1393 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1394 | ch = CHILD(n, i); |
| 1395 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1396 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1397 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1398 | if (i < NCH(n) && /* skip argument following star */ |
| 1399 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1400 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1401 | i++; |
| 1402 | } |
| 1403 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1404 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1405 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1406 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1407 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1408 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1410 | defaults for keyword only args */ |
| 1411 | for ( ; i < NCH(n); ++i) { |
| 1412 | ch = CHILD(n, i); |
| 1413 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1414 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1415 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1416 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1417 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1418 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1420 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1421 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1422 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1424 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1425 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1426 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1428 | since we set NULL as default for keyword only argument w/o default |
| 1429 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1430 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1431 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1432 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1433 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1434 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1435 | /* tfpdef: NAME [':' test] |
| 1436 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | */ |
| 1438 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1439 | j = 0; /* index for defaults */ |
| 1440 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1441 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1442 | ch = CHILD(n, i); |
| 1443 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1444 | case tfpdef: |
| 1445 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1446 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1447 | anything other than EQUAL or a comma? */ |
| 1448 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1449 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1450 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1451 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1452 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1453 | assert(posdefaults != NULL); |
| 1454 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1455 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1456 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1457 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1458 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1459 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1460 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1461 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1462 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1463 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1464 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1465 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1466 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1467 | i += 2; /* the name and the comma */ |
| 1468 | break; |
| 1469 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1470 | if (i+1 >= NCH(n) || |
| 1471 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1472 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1473 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1474 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1475 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1476 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1477 | if (TYPE(ch) == COMMA) { |
| 1478 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1479 | i += 2; /* now follows keyword only arguments */ |
| 1480 | res = handle_keywordonly_args(c, n, i, |
| 1481 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1482 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1483 | i = res; /* res has new position to process */ |
| 1484 | } |
| 1485 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1486 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1487 | if (!vararg) |
| 1488 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1489 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1490 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1491 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1492 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1493 | int res = 0; |
| 1494 | res = handle_keywordonly_args(c, n, i, |
| 1495 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1496 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1497 | i = res; /* res has new position to process */ |
| 1498 | } |
| 1499 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1502 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1503 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1504 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1505 | if (!kwarg) |
| 1506 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1507 | i += 3; |
| 1508 | break; |
| 1509 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1510 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1511 | "unexpected node in varargslist: %d @ %d", |
| 1512 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1513 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1515 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1516 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | static expr_ty |
| 1520 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1521 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1522 | expr_ty e; |
| 1523 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1524 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1526 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1527 | |
| 1528 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1529 | |
| 1530 | lineno = LINENO(n); |
| 1531 | col_offset = n->n_col_offset; |
| 1532 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1533 | ch = CHILD(n, 0); |
| 1534 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1535 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1536 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1537 | e = Name(id, Load, lineno, col_offset, |
| 1538 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1540 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1541 | |
| 1542 | for (i = 2; i < NCH(n); i+=2) { |
| 1543 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1544 | if (!id) |
| 1545 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1546 | e = Attribute(e, id, Load, lineno, col_offset, |
| 1547 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1548 | if (!e) |
| 1549 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static expr_ty |
| 1556 | ast_for_decorator(struct compiling *c, const node *n) |
| 1557 | { |
| 1558 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1559 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1560 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1563 | REQ(CHILD(n, 0), AT); |
| 1564 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1566 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1567 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1568 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1570 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1571 | d = name_expr; |
| 1572 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1573 | } |
| 1574 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1575 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1576 | n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1577 | if (!d) |
| 1578 | return NULL; |
| 1579 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1580 | } |
| 1581 | else { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1582 | 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] | 1583 | if (!d) |
| 1584 | return NULL; |
| 1585 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | static asdl_seq* |
| 1592 | ast_for_decorators(struct compiling *c, const node *n) |
| 1593 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1594 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1595 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1596 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1598 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1599 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1600 | if (!decorator_seq) |
| 1601 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1603 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1604 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1605 | if (!d) |
| 1606 | return NULL; |
| 1607 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1608 | } |
| 1609 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1613 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1614 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1615 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1616 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1617 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1618 | identifier name; |
| 1619 | arguments_ty args; |
| 1620 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1621 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1622 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1623 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1624 | |
| 1625 | REQ(n, funcdef); |
| 1626 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1627 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1628 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1629 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1630 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1631 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1632 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1633 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1634 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1635 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1636 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1637 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1638 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1639 | name_i += 2; |
| 1640 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1641 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1642 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1643 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1644 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1645 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1646 | if (is_async) |
| 1647 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1648 | 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] | 1649 | else |
| 1650 | return FunctionDef(name, args, body, decorator_seq, returns, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1651 | 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] | 1652 | } |
| 1653 | |
| 1654 | static stmt_ty |
| 1655 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1656 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1657 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1658 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1659 | REQ(CHILD(n, 0), NAME); |
| 1660 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1661 | REQ(CHILD(n, 1), funcdef); |
| 1662 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1663 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1664 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | static stmt_ty |
| 1668 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1669 | { |
| 1670 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1671 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1672 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | |
| 1676 | static stmt_ty |
| 1677 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1678 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1679 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1680 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1681 | REQ(CHILD(n, 0), NAME); |
| 1682 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1683 | |
| 1684 | switch (TYPE(CHILD(n, 1))) { |
| 1685 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1686 | return ast_for_funcdef_impl(c, n, NULL, |
| 1687 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1688 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1689 | return ast_for_with_stmt(c, n, |
| 1690 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1691 | |
| 1692 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1693 | return ast_for_for_stmt(c, n, |
| 1694 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1695 | |
| 1696 | default: |
| 1697 | PyErr_Format(PyExc_SystemError, |
| 1698 | "invalid async stament: %s", |
| 1699 | STR(CHILD(n, 1))); |
| 1700 | return NULL; |
| 1701 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1704 | static stmt_ty |
| 1705 | ast_for_decorated(struct compiling *c, const node *n) |
| 1706 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1707 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1708 | stmt_ty thing = NULL; |
| 1709 | asdl_seq *decorator_seq = NULL; |
| 1710 | |
| 1711 | REQ(n, decorated); |
| 1712 | |
| 1713 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1714 | if (!decorator_seq) |
| 1715 | return NULL; |
| 1716 | |
| 1717 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1718 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1720 | |
| 1721 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1722 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1723 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1724 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1725 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1726 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1727 | } |
| 1728 | return thing; |
| 1729 | } |
| 1730 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1731 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1732 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1733 | { |
| 1734 | /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* |
| 1735 | ['else' ':' suite] |
| 1736 | namedexpr_test: test [':=' test] |
| 1737 | argument: ( test [comp_for] | |
| 1738 | test ':=' test | |
| 1739 | test '=' test | |
| 1740 | '**' test | |
| 1741 | '*' test ) |
| 1742 | */ |
| 1743 | expr_ty target, value; |
| 1744 | |
| 1745 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1746 | if (!target) |
| 1747 | return NULL; |
| 1748 | |
| 1749 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1750 | if (!value) |
| 1751 | return NULL; |
| 1752 | |
| 1753 | if (!set_context(c, target, NamedStore, n)) |
| 1754 | return NULL; |
| 1755 | |
| 1756 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1757 | n->n_end_col_offset, c->c_arena); |
| 1758 | } |
| 1759 | |
| 1760 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1761 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1762 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1763 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1764 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1765 | arguments_ty args; |
| 1766 | expr_ty expression; |
| 1767 | |
| 1768 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1769 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1770 | if (!args) |
| 1771 | return NULL; |
| 1772 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1773 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1774 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1775 | } |
| 1776 | else { |
| 1777 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1778 | if (!args) |
| 1779 | return NULL; |
| 1780 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1781 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1782 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1785 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1786 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1789 | static expr_ty |
| 1790 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1793 | expr_ty expression, body, orelse; |
| 1794 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1795 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1796 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1797 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1798 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1799 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1800 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1801 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1802 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1803 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1804 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1805 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1806 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1807 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1810 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1811 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1812 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1813 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1814 | */ |
| 1815 | |
| 1816 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1817 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1818 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1819 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1820 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1821 | count_comp_for: |
| 1822 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1823 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1824 | if (NCH(n) == 2) { |
| 1825 | REQ(CHILD(n, 0), NAME); |
| 1826 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1827 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1828 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1829 | else if (NCH(n) == 1) { |
| 1830 | n = CHILD(n, 0); |
| 1831 | } |
| 1832 | else { |
| 1833 | goto error; |
| 1834 | } |
| 1835 | if (NCH(n) == (5)) { |
| 1836 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1837 | } |
| 1838 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1839 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1840 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1841 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1842 | REQ(n, comp_iter); |
| 1843 | n = CHILD(n, 0); |
| 1844 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1845 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1846 | else if (TYPE(n) == comp_if) { |
| 1847 | if (NCH(n) == 3) { |
| 1848 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1849 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1850 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1851 | else |
| 1852 | return n_fors; |
| 1853 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1854 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1855 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1856 | /* Should never be reached */ |
| 1857 | PyErr_SetString(PyExc_SystemError, |
| 1858 | "logic error in count_comp_fors"); |
| 1859 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1862 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1863 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1864 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | */ |
| 1866 | |
| 1867 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1868 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1869 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1870 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1871 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1872 | while (1) { |
| 1873 | REQ(n, comp_iter); |
| 1874 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1875 | return n_ifs; |
| 1876 | n = CHILD(n, 0); |
| 1877 | REQ(n, comp_if); |
| 1878 | n_ifs++; |
| 1879 | if (NCH(n) == 2) |
| 1880 | return n_ifs; |
| 1881 | n = CHILD(n, 2); |
| 1882 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1885 | static asdl_seq * |
| 1886 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1887 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1888 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1889 | asdl_seq *comps; |
| 1890 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1891 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1892 | if (n_fors == -1) |
| 1893 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1894 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1895 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1896 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1897 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1898 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1899 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1900 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1901 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1902 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1903 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1904 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1905 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1907 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1909 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1910 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1911 | REQ(CHILD(n, 0), NAME); |
| 1912 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1913 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1914 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1915 | else { |
| 1916 | sync_n = CHILD(n, 0); |
| 1917 | } |
| 1918 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1919 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1920 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1921 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1922 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1923 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1924 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1925 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1926 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1927 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1928 | /* Check the # of children rather than the length of t, since |
| 1929 | (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] | 1930 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1931 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1932 | comp = comprehension(first, expression, NULL, |
| 1933 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1934 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1935 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 1936 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 1937 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1938 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1939 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1940 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1941 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1942 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1943 | int j, n_ifs; |
| 1944 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1946 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1947 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1948 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1949 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1950 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1951 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1952 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1953 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1954 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1955 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1956 | REQ(n, comp_iter); |
| 1957 | n = CHILD(n, 0); |
| 1958 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1960 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1961 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1962 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1963 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1964 | if (NCH(n) == 3) |
| 1965 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1966 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1967 | /* on exit, must guarantee that n is a comp_for */ |
| 1968 | if (TYPE(n) == comp_iter) |
| 1969 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1970 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1971 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1972 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1973 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1974 | return comps; |
| 1975 | } |
| 1976 | |
| 1977 | static expr_ty |
| 1978 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1979 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1980 | /* testlist_comp: (test|star_expr) |
| 1981 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1982 | expr_ty elt; |
| 1983 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1984 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1986 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1988 | ch = CHILD(n, 0); |
| 1989 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1990 | if (!elt) |
| 1991 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1992 | if (elt->kind == Starred_kind) { |
| 1993 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1994 | return NULL; |
| 1995 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1997 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1998 | if (!comps) |
| 1999 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2000 | |
| 2001 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2002 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2003 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2004 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2005 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2006 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2007 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2008 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2009 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2010 | else |
| 2011 | /* Should never happen */ |
| 2012 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2015 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2016 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2017 | * elements. Iff successful, nonzero is returned. |
| 2018 | */ |
| 2019 | static int |
| 2020 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2021 | expr_ty *key, expr_ty *value) |
| 2022 | { |
| 2023 | expr_ty expression; |
| 2024 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2025 | assert(NCH(n) - *i >= 2); |
| 2026 | |
| 2027 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2028 | if (!expression) |
| 2029 | return 0; |
| 2030 | *key = NULL; |
| 2031 | *value = expression; |
| 2032 | |
| 2033 | *i += 2; |
| 2034 | } |
| 2035 | else { |
| 2036 | assert(NCH(n) - *i >= 3); |
| 2037 | |
| 2038 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2039 | if (!expression) |
| 2040 | return 0; |
| 2041 | *key = expression; |
| 2042 | |
| 2043 | REQ(CHILD(n, *i + 1), COLON); |
| 2044 | |
| 2045 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2046 | if (!expression) |
| 2047 | return 0; |
| 2048 | *value = expression; |
| 2049 | |
| 2050 | *i += 3; |
| 2051 | } |
| 2052 | return 1; |
| 2053 | } |
| 2054 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2055 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2056 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2057 | { |
| 2058 | expr_ty key, value; |
| 2059 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2060 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2062 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2063 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2064 | assert(key); |
| 2065 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2067 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2068 | if (!comps) |
| 2069 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2071 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2072 | 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] | 2073 | } |
| 2074 | |
| 2075 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2076 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2077 | { |
| 2078 | int i; |
| 2079 | int j; |
| 2080 | int size; |
| 2081 | asdl_seq *keys, *values; |
| 2082 | |
| 2083 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2084 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2085 | if (!keys) |
| 2086 | return NULL; |
| 2087 | |
| 2088 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2089 | if (!values) |
| 2090 | return NULL; |
| 2091 | |
| 2092 | j = 0; |
| 2093 | for (i = 0; i < NCH(n); i++) { |
| 2094 | expr_ty key, value; |
| 2095 | |
| 2096 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2097 | return NULL; |
| 2098 | asdl_seq_SET(keys, j, key); |
| 2099 | asdl_seq_SET(values, j, value); |
| 2100 | |
| 2101 | j++; |
| 2102 | } |
| 2103 | keys->size = j; |
| 2104 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2105 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2106 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2110 | ast_for_genexp(struct compiling *c, const node *n) |
| 2111 | { |
| 2112 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2113 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | static expr_ty |
| 2117 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2118 | { |
| 2119 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2120 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | static expr_ty |
| 2124 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2125 | { |
| 2126 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2127 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2130 | static expr_ty |
| 2131 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2132 | { |
| 2133 | int i; |
| 2134 | int size; |
| 2135 | asdl_seq *elts; |
| 2136 | |
| 2137 | assert(TYPE(n) == (dictorsetmaker)); |
| 2138 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2139 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2140 | if (!elts) |
| 2141 | return NULL; |
| 2142 | for (i = 0; i < NCH(n); i += 2) { |
| 2143 | expr_ty expression; |
| 2144 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2145 | if (!expression) |
| 2146 | return NULL; |
| 2147 | asdl_seq_SET(elts, i / 2, expression); |
| 2148 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2149 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2150 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2151 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2152 | |
| 2153 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2154 | ast_for_atom(struct compiling *c, const node *n) |
| 2155 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2156 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2157 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2158 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2159 | */ |
| 2160 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2162 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2163 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2164 | PyObject *name; |
| 2165 | const char *s = STR(ch); |
| 2166 | size_t len = strlen(s); |
| 2167 | if (len >= 4 && len <= 5) { |
| 2168 | if (!strcmp(s, "None")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2169 | return Constant(Py_None, LINENO(n), n->n_col_offset, |
| 2170 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2171 | if (!strcmp(s, "True")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2172 | return Constant(Py_True, LINENO(n), n->n_col_offset, |
| 2173 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2174 | if (!strcmp(s, "False")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2175 | return Constant(Py_False, LINENO(n), n->n_col_offset, |
| 2176 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2177 | } |
| 2178 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2179 | if (!name) |
| 2180 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2181 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2182 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2183 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2184 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2185 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2186 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2187 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2188 | const char *errtype = NULL; |
| 2189 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2190 | errtype = "unicode error"; |
| 2191 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2192 | errtype = "value error"; |
| 2193 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2194 | PyObject *type, *value, *tback, *errstr; |
| 2195 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2196 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2197 | if (errstr) { |
| 2198 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2199 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2200 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2201 | else { |
| 2202 | PyErr_Clear(); |
| 2203 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2204 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2205 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2206 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2207 | Py_XDECREF(tback); |
| 2208 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2209 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2210 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2211 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2212 | } |
| 2213 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2214 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2215 | if (!pynum) |
| 2216 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2217 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2218 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2219 | Py_DECREF(pynum); |
| 2220 | return NULL; |
| 2221 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2222 | return Constant(pynum, LINENO(n), n->n_col_offset, |
| 2223 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2224 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2225 | case ELLIPSIS: /* Ellipsis */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2226 | return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, |
| 2227 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2228 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2229 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2231 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2232 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2233 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2235 | if (TYPE(ch) == yield_expr) |
| 2236 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2239 | if (NCH(ch) == 1) { |
| 2240 | return ast_for_testlist(c, ch); |
| 2241 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2242 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2243 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2244 | return copy_location(ast_for_genexp(c, ch), n); |
| 2245 | } |
| 2246 | else { |
| 2247 | return copy_location(ast_for_testlist(c, ch), n); |
| 2248 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2249 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2250 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2252 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2253 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2254 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2256 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2257 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2258 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2259 | if (!elts) |
| 2260 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2261 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2262 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2263 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2264 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2265 | else { |
| 2266 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2267 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2268 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2269 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2270 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2271 | * ((test | '*' test) |
| 2272 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2273 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2274 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2275 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2276 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2277 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2278 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2279 | } |
| 2280 | else { |
| 2281 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2282 | if (NCH(ch) == 1 || |
| 2283 | (NCH(ch) > 1 && |
| 2284 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2285 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2286 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2287 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2288 | else if (NCH(ch) > 1 && |
| 2289 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2290 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2291 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2292 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2293 | else if (NCH(ch) > 3 - is_dict && |
| 2294 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2295 | /* It's a dictionary comprehension. */ |
| 2296 | if (is_dict) { |
| 2297 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2298 | "dict comprehension"); |
| 2299 | return NULL; |
| 2300 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2301 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2302 | } |
| 2303 | else { |
| 2304 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2305 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2306 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2307 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2308 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2309 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2310 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2311 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2312 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | static slice_ty |
| 2317 | ast_for_slice(struct compiling *c, const node *n) |
| 2318 | { |
| 2319 | node *ch; |
| 2320 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2321 | |
| 2322 | REQ(n, subscript); |
| 2323 | |
| 2324 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2325 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2326 | sliceop: ':' [test] |
| 2327 | */ |
| 2328 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2329 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2330 | /* 'step' variable hold no significance in terms of being used over |
| 2331 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2333 | if (!step) |
| 2334 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2336 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2340 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2341 | if (!lower) |
| 2342 | return NULL; |
| 2343 | } |
| 2344 | |
| 2345 | /* If there's an upper bound it's in the second or third position. */ |
| 2346 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2347 | if (NCH(n) > 1) { |
| 2348 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2349 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2350 | if (TYPE(n2) == test) { |
| 2351 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2352 | if (!upper) |
| 2353 | return NULL; |
| 2354 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2355 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2356 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2357 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2358 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2359 | if (TYPE(n2) == test) { |
| 2360 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2361 | if (!upper) |
| 2362 | return NULL; |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | ch = CHILD(n, NCH(n) - 1); |
| 2367 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2368 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2369 | ch = CHILD(ch, 1); |
| 2370 | if (TYPE(ch) == test) { |
| 2371 | step = ast_for_expr(c, ch); |
| 2372 | if (!step) |
| 2373 | return NULL; |
| 2374 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2378 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | static expr_ty |
| 2382 | ast_for_binop(struct compiling *c, const node *n) |
| 2383 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2384 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2386 | BinOp(BinOp(A, op, B), op, C). |
| 2387 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2388 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2389 | int i, nops; |
| 2390 | expr_ty expr1, expr2, result; |
| 2391 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2392 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2393 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2394 | if (!expr1) |
| 2395 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2396 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2397 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2398 | if (!expr2) |
| 2399 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2400 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2401 | newoperator = get_operator(CHILD(n, 1)); |
| 2402 | if (!newoperator) |
| 2403 | return NULL; |
| 2404 | |
| 2405 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2406 | 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] | 2407 | c->c_arena); |
| 2408 | if (!result) |
| 2409 | return NULL; |
| 2410 | |
| 2411 | nops = (NCH(n) - 1) / 2; |
| 2412 | for (i = 1; i < nops; i++) { |
| 2413 | expr_ty tmp_result, tmp; |
| 2414 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2415 | |
| 2416 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2417 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2418 | return NULL; |
| 2419 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2420 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2421 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2422 | return NULL; |
| 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2425 | LINENO(next_oper), next_oper->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2426 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2427 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2428 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2430 | return NULL; |
| 2431 | result = tmp_result; |
| 2432 | } |
| 2433 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2436 | static expr_ty |
| 2437 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2440 | subscriptlist: subscript (',' subscript)* [','] |
| 2441 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2442 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2443 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2444 | REQ(n, trailer); |
| 2445 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2446 | if (NCH(n) == 2) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2447 | return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, |
| 2448 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2449 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2450 | 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] | 2451 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2452 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2453 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2454 | if (!attr_id) |
| 2455 | return NULL; |
| 2456 | return Attribute(left_expr, attr_id, Load, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2457 | LINENO(n), n->n_col_offset, |
| 2458 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2459 | } |
| 2460 | else { |
| 2461 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2462 | REQ(CHILD(n, 2), RSQB); |
| 2463 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2464 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2465 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2466 | if (!slc) |
| 2467 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2468 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2469 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2470 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2471 | } |
| 2472 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2474 | by treating the sequence as a tuple literal if there are |
| 2475 | no slice features. |
| 2476 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2477 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2478 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2479 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2480 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2481 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2482 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2483 | if (!slices) |
| 2484 | return NULL; |
| 2485 | for (j = 0; j < NCH(n); j += 2) { |
| 2486 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2487 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2488 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2489 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2490 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2491 | asdl_seq_SET(slices, j / 2, slc); |
| 2492 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2493 | if (!simple) { |
| 2494 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2495 | Load, LINENO(n), n->n_col_offset, |
| 2496 | 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] | 2497 | } |
| 2498 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2499 | 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] | 2500 | if (!elts) |
| 2501 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2502 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2503 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2504 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2505 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2506 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2507 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2508 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2509 | if (!e) |
| 2510 | return NULL; |
| 2511 | return Subscript(left_expr, Index(e, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2512 | Load, LINENO(n), n->n_col_offset, |
| 2513 | 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] | 2514 | } |
| 2515 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2519 | ast_for_factor(struct compiling *c, const node *n) |
| 2520 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2521 | expr_ty expression; |
| 2522 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2523 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2524 | if (!expression) |
| 2525 | return NULL; |
| 2526 | |
| 2527 | switch (TYPE(CHILD(n, 0))) { |
| 2528 | case PLUS: |
| 2529 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2530 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2531 | c->c_arena); |
| 2532 | case MINUS: |
| 2533 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2534 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2535 | c->c_arena); |
| 2536 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2537 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2538 | n->n_end_lineno, n->n_end_col_offset, |
| 2539 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2540 | } |
| 2541 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2542 | TYPE(CHILD(n, 0))); |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | |
| 2546 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2547 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2548 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2549 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2550 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2551 | |
| 2552 | REQ(n, atom_expr); |
| 2553 | nch = NCH(n); |
| 2554 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2555 | if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2556 | start = 1; |
| 2557 | assert(nch > 1); |
| 2558 | } |
| 2559 | |
| 2560 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2561 | if (!e) |
| 2562 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2563 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2564 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2565 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2566 | return Await(e, LINENO(n), n->n_col_offset, |
| 2567 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2571 | node *ch = CHILD(n, i); |
| 2572 | if (TYPE(ch) != trailer) |
| 2573 | break; |
| 2574 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2575 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2576 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2577 | tmp->lineno = e->lineno; |
| 2578 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2579 | e = tmp; |
| 2580 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2581 | |
| 2582 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2583 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2584 | return Await(e, LINENO(n), n->n_col_offset, |
| 2585 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2586 | } |
| 2587 | else { |
| 2588 | return e; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | static expr_ty |
| 2593 | ast_for_power(struct compiling *c, const node *n) |
| 2594 | { |
| 2595 | /* power: atom trailer* ('**' factor)* |
| 2596 | */ |
| 2597 | expr_ty e; |
| 2598 | REQ(n, power); |
| 2599 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2600 | if (!e) |
| 2601 | return NULL; |
| 2602 | if (NCH(n) == 1) |
| 2603 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2604 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2605 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2606 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2607 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2608 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2609 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2610 | } |
| 2611 | return e; |
| 2612 | } |
| 2613 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2614 | static expr_ty |
| 2615 | ast_for_starred(struct compiling *c, const node *n) |
| 2616 | { |
| 2617 | expr_ty tmp; |
| 2618 | REQ(n, star_expr); |
| 2619 | |
| 2620 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2621 | if (!tmp) |
| 2622 | return NULL; |
| 2623 | |
| 2624 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2625 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2626 | 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] | 2627 | } |
| 2628 | |
| 2629 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2630 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2631 | */ |
| 2632 | |
| 2633 | static expr_ty |
| 2634 | ast_for_expr(struct compiling *c, const node *n) |
| 2635 | { |
| 2636 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2637 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2638 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2639 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2641 | and_test: not_test ('and' not_test)* |
| 2642 | not_test: 'not' not_test | comparison |
| 2643 | comparison: expr (comp_op expr)* |
| 2644 | expr: xor_expr ('|' xor_expr)* |
| 2645 | xor_expr: and_expr ('^' and_expr)* |
| 2646 | and_expr: shift_expr ('&' shift_expr)* |
| 2647 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2648 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2649 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2650 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2651 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2652 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2653 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | */ |
| 2655 | |
| 2656 | asdl_seq *seq; |
| 2657 | int i; |
| 2658 | |
| 2659 | loop: |
| 2660 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2661 | case namedexpr_test: |
| 2662 | if (NCH(n) == 3) |
| 2663 | return ast_for_namedexpr(c, n); |
| 2664 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2665 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2666 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2667 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2668 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2669 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2670 | else if (NCH(n) > 1) |
| 2671 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2672 | /* Fallthrough */ |
| 2673 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2674 | case and_test: |
| 2675 | if (NCH(n) == 1) { |
| 2676 | n = CHILD(n, 0); |
| 2677 | goto loop; |
| 2678 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2679 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2680 | if (!seq) |
| 2681 | return NULL; |
| 2682 | for (i = 0; i < NCH(n); i += 2) { |
| 2683 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2684 | if (!e) |
| 2685 | return NULL; |
| 2686 | asdl_seq_SET(seq, i / 2, e); |
| 2687 | } |
| 2688 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2689 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2690 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2691 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2692 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2693 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2694 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2695 | case not_test: |
| 2696 | if (NCH(n) == 1) { |
| 2697 | n = CHILD(n, 0); |
| 2698 | goto loop; |
| 2699 | } |
| 2700 | else { |
| 2701 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2702 | if (!expression) |
| 2703 | return NULL; |
| 2704 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2705 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2706 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2707 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2708 | } |
| 2709 | case comparison: |
| 2710 | if (NCH(n) == 1) { |
| 2711 | n = CHILD(n, 0); |
| 2712 | goto loop; |
| 2713 | } |
| 2714 | else { |
| 2715 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2716 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2717 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2718 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2719 | if (!ops) |
| 2720 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2721 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2722 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2723 | return NULL; |
| 2724 | } |
| 2725 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2726 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2727 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2728 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2729 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2730 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2731 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2732 | |
| 2733 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2734 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2735 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2736 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2738 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2739 | asdl_seq_SET(cmps, i / 2, expression); |
| 2740 | } |
| 2741 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2742 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2743 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2744 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2746 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2747 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2748 | } |
| 2749 | break; |
| 2750 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2751 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2753 | /* The next five cases all handle BinOps. The main body of code |
| 2754 | is the same in each case, but the switch turned inside out to |
| 2755 | reuse the code for each type of operator. |
| 2756 | */ |
| 2757 | case expr: |
| 2758 | case xor_expr: |
| 2759 | case and_expr: |
| 2760 | case shift_expr: |
| 2761 | case arith_expr: |
| 2762 | case term: |
| 2763 | if (NCH(n) == 1) { |
| 2764 | n = CHILD(n, 0); |
| 2765 | goto loop; |
| 2766 | } |
| 2767 | return ast_for_binop(c, n); |
| 2768 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2769 | node *an = NULL; |
| 2770 | node *en = NULL; |
| 2771 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2772 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2773 | if (NCH(n) > 1) |
| 2774 | an = CHILD(n, 1); /* yield_arg */ |
| 2775 | if (an) { |
| 2776 | en = CHILD(an, NCH(an) - 1); |
| 2777 | if (NCH(an) == 2) { |
| 2778 | is_from = 1; |
| 2779 | exp = ast_for_expr(c, en); |
| 2780 | } |
| 2781 | else |
| 2782 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2783 | if (!exp) |
| 2784 | return NULL; |
| 2785 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2786 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2787 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2788 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2789 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2790 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2791 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2792 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2793 | if (NCH(n) == 1) { |
| 2794 | n = CHILD(n, 0); |
| 2795 | goto loop; |
| 2796 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2797 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2798 | case power: |
| 2799 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2800 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2801 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2802 | return NULL; |
| 2803 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2804 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2805 | return NULL; |
| 2806 | } |
| 2807 | |
| 2808 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2809 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2810 | const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2811 | { |
| 2812 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2813 | arglist: argument (',' argument)* [','] |
| 2814 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2815 | */ |
| 2816 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2817 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2818 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2819 | asdl_seq *args; |
| 2820 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2821 | |
| 2822 | REQ(n, arglist); |
| 2823 | |
| 2824 | nargs = 0; |
| 2825 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2826 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2827 | node *ch = CHILD(n, i); |
| 2828 | if (TYPE(ch) == argument) { |
| 2829 | if (NCH(ch) == 1) |
| 2830 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2831 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2832 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2833 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2834 | ast_error(c, ch, "invalid syntax"); |
| 2835 | return NULL; |
| 2836 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2837 | if (NCH(n) > 1) { |
| 2838 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2842 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2843 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2844 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 2845 | nargs++; |
| 2846 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2847 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2848 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2849 | nkeywords++; |
| 2850 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2851 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2852 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2853 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2854 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2855 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2856 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2857 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2858 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2859 | |
| 2860 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2861 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2862 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2863 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2864 | node *ch = CHILD(n, i); |
| 2865 | if (TYPE(ch) == argument) { |
| 2866 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2867 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2868 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2869 | /* a positional argument */ |
| 2870 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2871 | if (ndoublestars) { |
| 2872 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2873 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2874 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2875 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2876 | else { |
| 2877 | ast_error(c, chch, |
| 2878 | "positional argument follows " |
| 2879 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2880 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2881 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2882 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2883 | e = ast_for_expr(c, chch); |
| 2884 | if (!e) |
| 2885 | return NULL; |
| 2886 | asdl_seq_SET(args, nargs++, e); |
| 2887 | } |
| 2888 | else if (TYPE(chch) == STAR) { |
| 2889 | /* an iterable argument unpacking */ |
| 2890 | expr_ty starred; |
| 2891 | if (ndoublestars) { |
| 2892 | ast_error(c, chch, |
| 2893 | "iterable argument unpacking follows " |
| 2894 | "keyword argument unpacking"); |
| 2895 | return NULL; |
| 2896 | } |
| 2897 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2898 | if (!e) |
| 2899 | return NULL; |
| 2900 | starred = Starred(e, Load, LINENO(chch), |
| 2901 | chch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2902 | chch->n_end_lineno, chch->n_end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2903 | c->c_arena); |
| 2904 | if (!starred) |
| 2905 | return NULL; |
| 2906 | asdl_seq_SET(args, nargs++, starred); |
| 2907 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2908 | } |
| 2909 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2910 | /* a keyword argument unpacking */ |
| 2911 | keyword_ty kw; |
| 2912 | i++; |
| 2913 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2914 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2915 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2916 | kw = keyword(NULL, e, c->c_arena); |
| 2917 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2918 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2919 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2920 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2921 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2922 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2923 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2924 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2925 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2926 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2927 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 2928 | /* treat colon equal as positional argument */ |
| 2929 | if (nkeywords) { |
| 2930 | if (ndoublestars) { |
| 2931 | ast_error(c, chch, |
| 2932 | "positional argument follows " |
| 2933 | "keyword argument unpacking"); |
| 2934 | } |
| 2935 | else { |
| 2936 | ast_error(c, chch, |
| 2937 | "positional argument follows " |
| 2938 | "keyword argument"); |
| 2939 | } |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | e = ast_for_namedexpr(c, ch); |
| 2943 | if (!e) |
| 2944 | return NULL; |
| 2945 | asdl_seq_SET(args, nargs++, e); |
| 2946 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2947 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2948 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2949 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2950 | identifier key, tmp; |
| 2951 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2952 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2953 | // To remain LL(1), the grammar accepts any test (basically, any |
| 2954 | // expression) in the keyword slot of a call site. So, we need |
| 2955 | // to manually enforce that the keyword is a NAME here. |
| 2956 | static const int name_tree[] = { |
| 2957 | test, |
| 2958 | or_test, |
| 2959 | and_test, |
| 2960 | not_test, |
| 2961 | comparison, |
| 2962 | expr, |
| 2963 | xor_expr, |
| 2964 | and_expr, |
| 2965 | shift_expr, |
| 2966 | arith_expr, |
| 2967 | term, |
| 2968 | factor, |
| 2969 | power, |
| 2970 | atom_expr, |
| 2971 | atom, |
| 2972 | 0, |
| 2973 | }; |
| 2974 | node *expr_node = chch; |
| 2975 | for (int i = 0; name_tree[i]; i++) { |
| 2976 | if (TYPE(expr_node) != name_tree[i]) |
| 2977 | break; |
| 2978 | if (NCH(expr_node) != 1) |
| 2979 | break; |
| 2980 | expr_node = CHILD(expr_node, 0); |
| 2981 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2982 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2983 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2984 | "expression cannot contain assignment, " |
| 2985 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2986 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2987 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2988 | key = new_identifier(STR(expr_node), c); |
| 2989 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2990 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2991 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2992 | if (forbidden_name(c, key, chch, 1)) { |
| 2993 | return NULL; |
| 2994 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2995 | for (k = 0; k < nkeywords; k++) { |
| 2996 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2997 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2998 | ast_error(c, chch, |
| 2999 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3000 | return NULL; |
| 3001 | } |
| 3002 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3003 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3004 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3005 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3006 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3007 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3008 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3009 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3010 | } |
| 3011 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3014 | return Call(func, args, keywords, func->lineno, func->col_offset, |
| 3015 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3018 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3019 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3020 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3021 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3022 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3023 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3024 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3025 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3026 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3027 | } |
| 3028 | else { |
| 3029 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3030 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3031 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3032 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3033 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3034 | else { |
| 3035 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3036 | if (!tmp) |
| 3037 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3038 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3039 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3040 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3043 | static stmt_ty |
| 3044 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3045 | { |
| 3046 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3047 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 3048 | ('=' (yield_expr|testlist_star_expr))*) |
| 3049 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3050 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3051 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3052 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3053 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3054 | */ |
| 3055 | |
| 3056 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3057 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3058 | if (!e) |
| 3059 | return NULL; |
| 3060 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3061 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3062 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3063 | } |
| 3064 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3065 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3066 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3067 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3068 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3069 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3070 | if (!expr1) |
| 3071 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3072 | if(!set_context(c, expr1, Store, ch)) |
| 3073 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3074 | /* set_context checks that most expressions are not the left side. |
| 3075 | Augmented assignments can only have a name, a subscript, or an |
| 3076 | attribute on the left, though, so we have to explicitly check for |
| 3077 | those. */ |
| 3078 | switch (expr1->kind) { |
| 3079 | case Name_kind: |
| 3080 | case Attribute_kind: |
| 3081 | case Subscript_kind: |
| 3082 | break; |
| 3083 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3084 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3085 | return NULL; |
| 3086 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3087 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3088 | ch = CHILD(n, 2); |
| 3089 | if (TYPE(ch) == testlist) |
| 3090 | expr2 = ast_for_testlist(c, ch); |
| 3091 | else |
| 3092 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3093 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3094 | return NULL; |
| 3095 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3096 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3097 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3098 | return NULL; |
| 3099 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3100 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3101 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3102 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3103 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3104 | expr_ty expr1, expr2, expr3; |
| 3105 | node *ch = CHILD(n, 0); |
| 3106 | node *deep, *ann = CHILD(n, 1); |
| 3107 | int simple = 1; |
| 3108 | |
| 3109 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3110 | deep = ch; |
| 3111 | while (NCH(deep) == 1) { |
| 3112 | deep = CHILD(deep, 0); |
| 3113 | } |
| 3114 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3115 | simple = 0; |
| 3116 | } |
| 3117 | expr1 = ast_for_testlist(c, ch); |
| 3118 | if (!expr1) { |
| 3119 | return NULL; |
| 3120 | } |
| 3121 | switch (expr1->kind) { |
| 3122 | case Name_kind: |
| 3123 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3124 | return NULL; |
| 3125 | } |
| 3126 | expr1->v.Name.ctx = Store; |
| 3127 | break; |
| 3128 | case Attribute_kind: |
| 3129 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3130 | return NULL; |
| 3131 | } |
| 3132 | expr1->v.Attribute.ctx = Store; |
| 3133 | break; |
| 3134 | case Subscript_kind: |
| 3135 | expr1->v.Subscript.ctx = Store; |
| 3136 | break; |
| 3137 | case List_kind: |
| 3138 | ast_error(c, ch, |
| 3139 | "only single target (not list) can be annotated"); |
| 3140 | return NULL; |
| 3141 | case Tuple_kind: |
| 3142 | ast_error(c, ch, |
| 3143 | "only single target (not tuple) can be annotated"); |
| 3144 | return NULL; |
| 3145 | default: |
| 3146 | ast_error(c, ch, |
| 3147 | "illegal target for annotation"); |
| 3148 | return NULL; |
| 3149 | } |
| 3150 | |
| 3151 | if (expr1->kind != Name_kind) { |
| 3152 | simple = 0; |
| 3153 | } |
| 3154 | ch = CHILD(ann, 1); |
| 3155 | expr2 = ast_for_expr(c, ch); |
| 3156 | if (!expr2) { |
| 3157 | return NULL; |
| 3158 | } |
| 3159 | if (NCH(ann) == 2) { |
| 3160 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3161 | LINENO(n), n->n_col_offset, |
| 3162 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3163 | } |
| 3164 | else { |
| 3165 | ch = CHILD(ann, 3); |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame^] | 3166 | if (TYPE(ch) == testlist) { |
| 3167 | expr3 = ast_for_testlist(c, ch); |
| 3168 | } |
| 3169 | else { |
| 3170 | expr3 = ast_for_expr(c, ch); |
| 3171 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3172 | if (!expr3) { |
| 3173 | return NULL; |
| 3174 | } |
| 3175 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3176 | LINENO(n), n->n_col_offset, |
| 3177 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3178 | } |
| 3179 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3180 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3181 | int i; |
| 3182 | asdl_seq *targets; |
| 3183 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3184 | expr_ty expression; |
| 3185 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3186 | /* a normal assignment */ |
| 3187 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3188 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3189 | if (!targets) |
| 3190 | return NULL; |
| 3191 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3192 | expr_ty e; |
| 3193 | node *ch = CHILD(n, i); |
| 3194 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3195 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3196 | return NULL; |
| 3197 | } |
| 3198 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3200 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3202 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3203 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3204 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3205 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3206 | asdl_seq_SET(targets, i / 2, e); |
| 3207 | } |
| 3208 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3209 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3210 | expression = ast_for_testlist(c, value); |
| 3211 | else |
| 3212 | expression = ast_for_expr(c, value); |
| 3213 | if (!expression) |
| 3214 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3215 | return Assign(targets, expression, LINENO(n), n->n_col_offset, |
| 3216 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3217 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3220 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3221 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3222 | 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] | 3223 | { |
| 3224 | asdl_seq *seq; |
| 3225 | int i; |
| 3226 | expr_ty e; |
| 3227 | |
| 3228 | REQ(n, exprlist); |
| 3229 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3230 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3231 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3232 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3233 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3234 | e = ast_for_expr(c, CHILD(n, i)); |
| 3235 | if (!e) |
| 3236 | return NULL; |
| 3237 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3238 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3239 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | } |
| 3241 | return seq; |
| 3242 | } |
| 3243 | |
| 3244 | static stmt_ty |
| 3245 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3246 | { |
| 3247 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3249 | /* del_stmt: 'del' exprlist */ |
| 3250 | REQ(n, del_stmt); |
| 3251 | |
| 3252 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3253 | if (!expr_list) |
| 3254 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3255 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3256 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | static stmt_ty |
| 3260 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3261 | { |
| 3262 | /* |
| 3263 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3264 | | yield_stmt |
| 3265 | break_stmt: 'break' |
| 3266 | continue_stmt: 'continue' |
| 3267 | return_stmt: 'return' [testlist] |
| 3268 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3269 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3270 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3271 | */ |
| 3272 | node *ch; |
| 3273 | |
| 3274 | REQ(n, flow_stmt); |
| 3275 | ch = CHILD(n, 0); |
| 3276 | switch (TYPE(ch)) { |
| 3277 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3278 | return Break(LINENO(n), n->n_col_offset, |
| 3279 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3280 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3281 | return Continue(LINENO(n), n->n_col_offset, |
| 3282 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3283 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3284 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3285 | if (!exp) |
| 3286 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3287 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3288 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3289 | } |
| 3290 | case return_stmt: |
| 3291 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3292 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3293 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3294 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3295 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3296 | if (!expression) |
| 3297 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3298 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3299 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3300 | } |
| 3301 | case raise_stmt: |
| 3302 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3303 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3304 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3305 | else if (NCH(ch) >= 2) { |
| 3306 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3308 | if (!expression) |
| 3309 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3310 | if (NCH(ch) == 4) { |
| 3311 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3312 | if (!cause) |
| 3313 | return NULL; |
| 3314 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3315 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3316 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3317 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3318 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3319 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3320 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3321 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3322 | return NULL; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3327 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3328 | { |
| 3329 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3330 | import_as_name: NAME ['as' NAME] |
| 3331 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | dotted_name: NAME ('.' NAME)* |
| 3333 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3334 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3335 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3336 | loop: |
| 3337 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3338 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3339 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3340 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3341 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3342 | if (!name) |
| 3343 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3344 | if (NCH(n) == 3) { |
| 3345 | node *str_node = CHILD(n, 2); |
| 3346 | str = NEW_IDENTIFIER(str_node); |
| 3347 | if (!str) |
| 3348 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3349 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3350 | return NULL; |
| 3351 | } |
| 3352 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3353 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3354 | return NULL; |
| 3355 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3356 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3357 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3358 | case dotted_as_name: |
| 3359 | if (NCH(n) == 1) { |
| 3360 | n = CHILD(n, 0); |
| 3361 | goto loop; |
| 3362 | } |
| 3363 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3364 | node *asname_node = CHILD(n, 2); |
| 3365 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3366 | if (!a) |
| 3367 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3368 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3369 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3370 | if (!a->asname) |
| 3371 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3372 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3373 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3374 | return a; |
| 3375 | } |
| 3376 | break; |
| 3377 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3378 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3379 | node *name_node = CHILD(n, 0); |
| 3380 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3381 | if (!name) |
| 3382 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3383 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3384 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3385 | return alias(name, NULL, c->c_arena); |
| 3386 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3387 | else { |
| 3388 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3389 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3390 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3391 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3393 | |
| 3394 | len = 0; |
| 3395 | for (i = 0; i < NCH(n); i += 2) |
| 3396 | /* length of string plus one for the dot */ |
| 3397 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3398 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3399 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3400 | if (!str) |
| 3401 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3402 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3403 | if (!s) |
| 3404 | return NULL; |
| 3405 | for (i = 0; i < NCH(n); i += 2) { |
| 3406 | char *sch = STR(CHILD(n, i)); |
| 3407 | strcpy(s, STR(CHILD(n, i))); |
| 3408 | s += strlen(sch); |
| 3409 | *s++ = '.'; |
| 3410 | } |
| 3411 | --s; |
| 3412 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3413 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3414 | PyBytes_GET_SIZE(str), |
| 3415 | NULL); |
| 3416 | Py_DECREF(str); |
| 3417 | if (!uni) |
| 3418 | return NULL; |
| 3419 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3420 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3421 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3422 | Py_DECREF(str); |
| 3423 | return NULL; |
| 3424 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3425 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3426 | } |
| 3427 | break; |
| 3428 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3429 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3430 | if (!str) |
| 3431 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3432 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3433 | Py_DECREF(str); |
| 3434 | return NULL; |
| 3435 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3436 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3437 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3438 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3439 | "unexpected import name: %d", TYPE(n)); |
| 3440 | return NULL; |
| 3441 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3442 | |
| 3443 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3444 | return NULL; |
| 3445 | } |
| 3446 | |
| 3447 | static stmt_ty |
| 3448 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3449 | { |
| 3450 | /* |
| 3451 | import_stmt: import_name | import_from |
| 3452 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3453 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3454 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3455 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3456 | int lineno; |
| 3457 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | int i; |
| 3459 | asdl_seq *aliases; |
| 3460 | |
| 3461 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3462 | lineno = LINENO(n); |
| 3463 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3464 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3465 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3466 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3467 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3468 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3469 | if (!aliases) |
| 3470 | return NULL; |
| 3471 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3472 | 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] | 3473 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3474 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3475 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3476 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3477 | // Even though n is modified above, the end position is not changed |
| 3478 | return Import(aliases, lineno, col_offset, |
| 3479 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3480 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3481 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3482 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3483 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3484 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3485 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3486 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3488 | /* Count the number of dots (for relative imports) and check for the |
| 3489 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3490 | for (idx = 1; idx < NCH(n); idx++) { |
| 3491 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3492 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3493 | if (!mod) |
| 3494 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3495 | idx++; |
| 3496 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3497 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3499 | ndots += 3; |
| 3500 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3501 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3502 | break; |
| 3503 | } |
| 3504 | ndots++; |
| 3505 | } |
| 3506 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3507 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3508 | case STAR: |
| 3509 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3510 | n = CHILD(n, idx); |
| 3511 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3512 | break; |
| 3513 | case LPAR: |
| 3514 | /* from ... import (x, y, z) */ |
| 3515 | n = CHILD(n, idx + 1); |
| 3516 | n_children = NCH(n); |
| 3517 | break; |
| 3518 | case import_as_names: |
| 3519 | /* from ... import x, y, z */ |
| 3520 | n = CHILD(n, idx); |
| 3521 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3522 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3523 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3524 | " surrounding parentheses"); |
| 3525 | return NULL; |
| 3526 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3527 | break; |
| 3528 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3529 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3530 | return NULL; |
| 3531 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3532 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3533 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3534 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3535 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3536 | |
| 3537 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3538 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3539 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3540 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3541 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3542 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3543 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3544 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3545 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3546 | 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] | 3547 | if (!import_alias) |
| 3548 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3549 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3550 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3551 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3552 | if (mod != NULL) |
| 3553 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3554 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3555 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3556 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3557 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3558 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3559 | "unknown import statement: starts with command '%s'", |
| 3560 | STR(CHILD(n, 0))); |
| 3561 | return NULL; |
| 3562 | } |
| 3563 | |
| 3564 | static stmt_ty |
| 3565 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3566 | { |
| 3567 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3568 | identifier name; |
| 3569 | asdl_seq *s; |
| 3570 | int i; |
| 3571 | |
| 3572 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3573 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3574 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3575 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3577 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3578 | if (!name) |
| 3579 | return NULL; |
| 3580 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3581 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3582 | return Global(s, LINENO(n), n->n_col_offset, |
| 3583 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3587 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3588 | { |
| 3589 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3590 | identifier name; |
| 3591 | asdl_seq *s; |
| 3592 | int i; |
| 3593 | |
| 3594 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3595 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3596 | if (!s) |
| 3597 | return NULL; |
| 3598 | for (i = 1; i < NCH(n); i += 2) { |
| 3599 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3600 | if (!name) |
| 3601 | return NULL; |
| 3602 | asdl_seq_SET(s, i / 2, name); |
| 3603 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3604 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3605 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3609 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3610 | { |
| 3611 | /* assert_stmt: 'assert' test [',' test] */ |
| 3612 | REQ(n, assert_stmt); |
| 3613 | if (NCH(n) == 2) { |
| 3614 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3615 | if (!expression) |
| 3616 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3617 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3618 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3619 | } |
| 3620 | else if (NCH(n) == 4) { |
| 3621 | expr_ty expr1, expr2; |
| 3622 | |
| 3623 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3624 | if (!expr1) |
| 3625 | return NULL; |
| 3626 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3627 | if (!expr2) |
| 3628 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3630 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3631 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3632 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3633 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3634 | "improper number of parts to 'assert' statement: %d", |
| 3635 | NCH(n)); |
| 3636 | return NULL; |
| 3637 | } |
| 3638 | |
| 3639 | static asdl_seq * |
| 3640 | ast_for_suite(struct compiling *c, const node *n) |
| 3641 | { |
| 3642 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3643 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 | stmt_ty s; |
| 3645 | int i, total, num, end, pos = 0; |
| 3646 | node *ch; |
| 3647 | |
| 3648 | REQ(n, suite); |
| 3649 | |
| 3650 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3651 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3652 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3653 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3654 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3655 | n = CHILD(n, 0); |
| 3656 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3657 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3658 | */ |
| 3659 | end = NCH(n) - 1; |
| 3660 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3661 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3662 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3663 | for (i = 0; i < end; i += 2) { |
| 3664 | ch = CHILD(n, i); |
| 3665 | s = ast_for_stmt(c, ch); |
| 3666 | if (!s) |
| 3667 | return NULL; |
| 3668 | asdl_seq_SET(seq, pos++, s); |
| 3669 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3670 | } |
| 3671 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3672 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3673 | ch = CHILD(n, i); |
| 3674 | REQ(ch, stmt); |
| 3675 | num = num_stmts(ch); |
| 3676 | if (num == 1) { |
| 3677 | /* small_stmt or compound_stmt with only one child */ |
| 3678 | s = ast_for_stmt(c, ch); |
| 3679 | if (!s) |
| 3680 | return NULL; |
| 3681 | asdl_seq_SET(seq, pos++, s); |
| 3682 | } |
| 3683 | else { |
| 3684 | int j; |
| 3685 | ch = CHILD(ch, 0); |
| 3686 | REQ(ch, simple_stmt); |
| 3687 | for (j = 0; j < NCH(ch); j += 2) { |
| 3688 | /* statement terminates with a semi-colon ';' */ |
| 3689 | if (NCH(CHILD(ch, j)) == 0) { |
| 3690 | assert((j + 1) == NCH(ch)); |
| 3691 | break; |
| 3692 | } |
| 3693 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3694 | if (!s) |
| 3695 | return NULL; |
| 3696 | asdl_seq_SET(seq, pos++, s); |
| 3697 | } |
| 3698 | } |
| 3699 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | } |
| 3701 | assert(pos == seq->size); |
| 3702 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3705 | static void |
| 3706 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3707 | { |
| 3708 | int tot = asdl_seq_LEN(s); |
| 3709 | // Suite should not be empty, but it is safe to just ignore it |
| 3710 | // if it will ever occur. |
| 3711 | if (!tot) { |
| 3712 | return; |
| 3713 | } |
| 3714 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3715 | *end_lineno = last->end_lineno; |
| 3716 | *end_col_offset = last->end_col_offset; |
| 3717 | } |
| 3718 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | static stmt_ty |
| 3720 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3721 | { |
| 3722 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3723 | ['else' ':' suite] |
| 3724 | */ |
| 3725 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3726 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3727 | |
| 3728 | REQ(n, if_stmt); |
| 3729 | |
| 3730 | if (NCH(n) == 4) { |
| 3731 | expr_ty expression; |
| 3732 | asdl_seq *suite_seq; |
| 3733 | |
| 3734 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3735 | if (!expression) |
| 3736 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3737 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3738 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3739 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3740 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3742 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3743 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3744 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3745 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3746 | s = STR(CHILD(n, 4)); |
| 3747 | /* s[2], the third character in the string, will be |
| 3748 | 's' for el_s_e, or |
| 3749 | 'i' for el_i_f |
| 3750 | */ |
| 3751 | if (s[2] == 's') { |
| 3752 | expr_ty expression; |
| 3753 | asdl_seq *seq1, *seq2; |
| 3754 | |
| 3755 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3756 | if (!expression) |
| 3757 | return NULL; |
| 3758 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3759 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3760 | return NULL; |
| 3761 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3762 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3763 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3764 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3765 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3766 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3767 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3768 | } |
| 3769 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3770 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3771 | expr_ty expression; |
| 3772 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3773 | asdl_seq *orelse = NULL; |
| 3774 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3775 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3776 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3777 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3778 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3779 | has_else = 1; |
| 3780 | n_elif -= 3; |
| 3781 | } |
| 3782 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3783 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3784 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3785 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3786 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3787 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3788 | if (!orelse) |
| 3789 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3790 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3791 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3792 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3793 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3794 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3795 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3796 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3797 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3798 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3799 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3801 | asdl_seq_SET(orelse, 0, |
| 3802 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3803 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3804 | CHILD(n, NCH(n) - 6)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3805 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3806 | /* the just-created orelse handled the last elif */ |
| 3807 | n_elif--; |
| 3808 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3809 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3810 | for (i = 0; i < n_elif; i++) { |
| 3811 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3812 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3813 | if (!newobj) |
| 3814 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3815 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3816 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3817 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3818 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3819 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3820 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3821 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3822 | if (orelse != NULL) { |
| 3823 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3824 | } else { |
| 3825 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3826 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3827 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3828 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3829 | LINENO(CHILD(n, off)), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3830 | CHILD(n, off)->n_col_offset, |
| 3831 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3832 | orelse = newobj; |
| 3833 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3834 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3835 | if (!expression) |
| 3836 | return NULL; |
| 3837 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3838 | if (!suite_seq) |
| 3839 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3840 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3841 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3842 | LINENO(n), n->n_col_offset, |
| 3843 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3844 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3845 | |
| 3846 | PyErr_Format(PyExc_SystemError, |
| 3847 | "unexpected token in 'if' statement: %s", s); |
| 3848 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | static stmt_ty |
| 3852 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3853 | { |
| 3854 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3855 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3856 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3857 | |
| 3858 | if (NCH(n) == 4) { |
| 3859 | expr_ty expression; |
| 3860 | asdl_seq *suite_seq; |
| 3861 | |
| 3862 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3863 | if (!expression) |
| 3864 | return NULL; |
| 3865 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3866 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3867 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3868 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3869 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3870 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3871 | } |
| 3872 | else if (NCH(n) == 7) { |
| 3873 | expr_ty expression; |
| 3874 | asdl_seq *seq1, *seq2; |
| 3875 | |
| 3876 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3877 | if (!expression) |
| 3878 | return NULL; |
| 3879 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3880 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3881 | return NULL; |
| 3882 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3883 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3884 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3885 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3886 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3887 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3888 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3889 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3890 | |
| 3891 | PyErr_Format(PyExc_SystemError, |
| 3892 | "wrong number of tokens for 'while' statement: %d", |
| 3893 | NCH(n)); |
| 3894 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3898 | 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] | 3899 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3900 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3901 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3902 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3903 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3904 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3905 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3906 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3907 | REQ(n, for_stmt); |
| 3908 | |
| 3909 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3910 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3911 | if (!seq) |
| 3912 | return NULL; |
| 3913 | } |
| 3914 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3915 | node_target = CHILD(n, 1); |
| 3916 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3917 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3918 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3919 | /* Check the # of children rather than the length of _target, since |
| 3920 | 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] | 3921 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3922 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3923 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3924 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3925 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 3926 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 3927 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3928 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3929 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3930 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3931 | return NULL; |
| 3932 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3933 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3934 | return NULL; |
| 3935 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3936 | if (seq != NULL) { |
| 3937 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 3938 | } else { |
| 3939 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3940 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3941 | if (is_async) |
| 3942 | return AsyncFor(target, expression, suite_seq, seq, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3943 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3944 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3945 | else |
| 3946 | return For(target, expression, suite_seq, seq, |
| 3947 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3948 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | static excepthandler_ty |
| 3952 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3953 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3954 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3955 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3956 | REQ(exc, except_clause); |
| 3957 | REQ(body, suite); |
| 3958 | |
| 3959 | if (NCH(exc) == 1) { |
| 3960 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3961 | if (!suite_seq) |
| 3962 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3963 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3964 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3965 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3966 | exc->n_col_offset, |
| 3967 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3968 | } |
| 3969 | else if (NCH(exc) == 2) { |
| 3970 | expr_ty expression; |
| 3971 | asdl_seq *suite_seq; |
| 3972 | |
| 3973 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3974 | if (!expression) |
| 3975 | return NULL; |
| 3976 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3977 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3978 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3979 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3980 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3981 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3982 | exc->n_col_offset, |
| 3983 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3984 | } |
| 3985 | else if (NCH(exc) == 4) { |
| 3986 | asdl_seq *suite_seq; |
| 3987 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3988 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3989 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3990 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3991 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3992 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3993 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3994 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3995 | return NULL; |
| 3996 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3997 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3998 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3999 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4000 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4001 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4002 | exc->n_col_offset, |
| 4003 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4004 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4005 | |
| 4006 | PyErr_Format(PyExc_SystemError, |
| 4007 | "wrong number of children for 'except' clause: %d", |
| 4008 | NCH(exc)); |
| 4009 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | static stmt_ty |
| 4013 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4014 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4015 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4016 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4017 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4018 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4019 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4020 | REQ(n, try_stmt); |
| 4021 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4022 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4023 | if (body == NULL) |
| 4024 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4025 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4026 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4027 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4028 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4029 | /* we can assume it's an "else", |
| 4030 | because nch >= 9 for try-else-finally and |
| 4031 | it would otherwise have a type of except_clause */ |
| 4032 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4033 | if (orelse == NULL) |
| 4034 | return NULL; |
| 4035 | n_except--; |
| 4036 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4037 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4038 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4039 | if (finally == NULL) |
| 4040 | return NULL; |
| 4041 | n_except--; |
| 4042 | } |
| 4043 | else { |
| 4044 | /* we can assume it's an "else", |
| 4045 | otherwise it would have a type of except_clause */ |
| 4046 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4047 | if (orelse == NULL) |
| 4048 | return NULL; |
| 4049 | n_except--; |
| 4050 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4051 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4052 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4053 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4054 | return NULL; |
| 4055 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4057 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4058 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4059 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4060 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4061 | if (handlers == NULL) |
| 4062 | return NULL; |
| 4063 | |
| 4064 | for (i = 0; i < n_except; i++) { |
| 4065 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4066 | CHILD(n, 5 + i * 3)); |
| 4067 | if (!e) |
| 4068 | return NULL; |
| 4069 | asdl_seq_SET(handlers, i, e); |
| 4070 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4073 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4074 | if (finally != NULL) { |
| 4075 | // finally is always last |
| 4076 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4077 | } else if (orelse != NULL) { |
| 4078 | // otherwise else is last |
| 4079 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4080 | } else { |
| 4081 | // inline the get_last_end_pos logic due to layout mismatch |
| 4082 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4083 | end_lineno = last_handler->end_lineno; |
| 4084 | end_col_offset = last_handler->end_col_offset; |
| 4085 | } |
| 4086 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4087 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4090 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4091 | static withitem_ty |
| 4092 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4093 | { |
| 4094 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4095 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4096 | REQ(n, with_item); |
| 4097 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4098 | if (!context_expr) |
| 4099 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4100 | if (NCH(n) == 3) { |
| 4101 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4102 | |
| 4103 | if (!optional_vars) { |
| 4104 | return NULL; |
| 4105 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4106 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4107 | return NULL; |
| 4108 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4111 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4114 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 4115 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4116 | 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] | 4117 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4118 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4119 | int i, n_items, end_lineno, end_col_offset; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4120 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4121 | |
| 4122 | REQ(n, with_stmt); |
| 4123 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4124 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4125 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4126 | if (!items) |
| 4127 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4128 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 4129 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4130 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4131 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4132 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4135 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4136 | if (!body) |
| 4137 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4138 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4139 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4140 | if (is_async) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4141 | return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, |
| 4142 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4143 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4144 | return With(items, body, LINENO(n), n->n_col_offset, |
| 4145 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4148 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4149 | 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] | 4150 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4151 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4152 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4153 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4154 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4155 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4156 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4157 | REQ(n, classdef); |
| 4158 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4159 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4160 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4161 | if (!s) |
| 4162 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4163 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4164 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4165 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4166 | if (!classname) |
| 4167 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4168 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4169 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4170 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4171 | LINENO(n), n->n_col_offset, |
| 4172 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4173 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4174 | |
| 4175 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4176 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4177 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4178 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4179 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4180 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4181 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4182 | if (!classname) |
| 4183 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4184 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4185 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4186 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4187 | LINENO(n), n->n_col_offset, |
| 4188 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4191 | /* class NAME '(' arglist ')' ':' suite */ |
| 4192 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4193 | { |
| 4194 | PyObject *dummy_name; |
| 4195 | expr_ty dummy; |
| 4196 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4197 | if (!dummy_name) |
| 4198 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4199 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4200 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4201 | c->c_arena); |
| 4202 | 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] | 4203 | if (!call) |
| 4204 | return NULL; |
| 4205 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4206 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4207 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4208 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4209 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4210 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4211 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4212 | if (!classname) |
| 4213 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4214 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4215 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4216 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4217 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4218 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4219 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | static stmt_ty |
| 4223 | ast_for_stmt(struct compiling *c, const node *n) |
| 4224 | { |
| 4225 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4226 | assert(NCH(n) == 1); |
| 4227 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4228 | } |
| 4229 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4230 | assert(num_stmts(n) == 1); |
| 4231 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4232 | } |
| 4233 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4234 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4235 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4236 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4237 | */ |
| 4238 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4239 | case expr_stmt: |
| 4240 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4241 | case del_stmt: |
| 4242 | return ast_for_del_stmt(c, n); |
| 4243 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4244 | return Pass(LINENO(n), n->n_col_offset, |
| 4245 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4246 | case flow_stmt: |
| 4247 | return ast_for_flow_stmt(c, n); |
| 4248 | case import_stmt: |
| 4249 | return ast_for_import_stmt(c, n); |
| 4250 | case global_stmt: |
| 4251 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4252 | case nonlocal_stmt: |
| 4253 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4254 | case assert_stmt: |
| 4255 | return ast_for_assert_stmt(c, n); |
| 4256 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4257 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4258 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4259 | TYPE(n), NCH(n)); |
| 4260 | return NULL; |
| 4261 | } |
| 4262 | } |
| 4263 | else { |
| 4264 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4265 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4266 | */ |
| 4267 | node *ch = CHILD(n, 0); |
| 4268 | REQ(n, compound_stmt); |
| 4269 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4270 | case if_stmt: |
| 4271 | return ast_for_if_stmt(c, ch); |
| 4272 | case while_stmt: |
| 4273 | return ast_for_while_stmt(c, ch); |
| 4274 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4275 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4276 | case try_stmt: |
| 4277 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4278 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4279 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4280 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4281 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4282 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4283 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4284 | case decorated: |
| 4285 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4286 | case async_stmt: |
| 4287 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4288 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4289 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4290 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4291 | TYPE(n), NCH(n)); |
| 4292 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4293 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4298 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4299 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4300 | const char *end; |
| 4301 | long x; |
| 4302 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4303 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4304 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4305 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4306 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4307 | errno = 0; |
| 4308 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4309 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4310 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4311 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4312 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4313 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4314 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4315 | } |
| 4316 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4317 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4318 | if (*end == '\0') { |
| 4319 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4320 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4321 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4322 | } |
| 4323 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4324 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4325 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4326 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4327 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4328 | return NULL; |
| 4329 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4330 | } |
| 4331 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4332 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4333 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4334 | if (dx == -1.0 && PyErr_Occurred()) |
| 4335 | return NULL; |
| 4336 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4337 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4341 | parsenumber(struct compiling *c, const char *s) |
| 4342 | { |
| 4343 | char *dup, *end; |
| 4344 | PyObject *res = NULL; |
| 4345 | |
| 4346 | assert(s != NULL); |
| 4347 | |
| 4348 | if (strchr(s, '_') == NULL) { |
| 4349 | return parsenumber_raw(c, s); |
| 4350 | } |
| 4351 | /* Create a duplicate without underscores. */ |
| 4352 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4353 | if (dup == NULL) { |
| 4354 | return PyErr_NoMemory(); |
| 4355 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4356 | end = dup; |
| 4357 | for (; *s; s++) { |
| 4358 | if (*s != '_') { |
| 4359 | *end++ = *s; |
| 4360 | } |
| 4361 | } |
| 4362 | *end = '\0'; |
| 4363 | res = parsenumber_raw(c, dup); |
| 4364 | PyMem_Free(dup); |
| 4365 | return res; |
| 4366 | } |
| 4367 | |
| 4368 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4369 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4370 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4371 | const char *s, *t; |
| 4372 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4373 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4374 | while (s < end && (*s & 0x80)) s++; |
| 4375 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4376 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4379 | static int |
| 4380 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4381 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4382 | { |
| 4383 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4384 | first_invalid_escape_char); |
| 4385 | if (msg == NULL) { |
| 4386 | return -1; |
| 4387 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4388 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4389 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4390 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4391 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4392 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4393 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4394 | to get a more accurate error report */ |
| 4395 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4396 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4397 | } |
| 4398 | Py_DECREF(msg); |
| 4399 | return -1; |
| 4400 | } |
| 4401 | Py_DECREF(msg); |
| 4402 | return 0; |
| 4403 | } |
| 4404 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4405 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4406 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4407 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4408 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4409 | PyObject *v, *u; |
| 4410 | char *buf; |
| 4411 | char *p; |
| 4412 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4413 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4414 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4415 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4416 | return NULL; |
| 4417 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4418 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4419 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4420 | if (u == NULL) |
| 4421 | return NULL; |
| 4422 | p = buf = PyBytes_AsString(u); |
| 4423 | end = s + len; |
| 4424 | while (s < end) { |
| 4425 | if (*s == '\\') { |
| 4426 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4427 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4428 | strcpy(p, "u005c"); |
| 4429 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4430 | if (s >= end) |
| 4431 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4432 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4433 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4434 | if (*s & 0x80) { /* XXX inefficient */ |
| 4435 | PyObject *w; |
| 4436 | int kind; |
| 4437 | void *data; |
| 4438 | Py_ssize_t len, i; |
| 4439 | w = decode_utf8(c, &s, end); |
| 4440 | if (w == NULL) { |
| 4441 | Py_DECREF(u); |
| 4442 | return NULL; |
| 4443 | } |
| 4444 | kind = PyUnicode_KIND(w); |
| 4445 | data = PyUnicode_DATA(w); |
| 4446 | len = PyUnicode_GET_LENGTH(w); |
| 4447 | for (i = 0; i < len; i++) { |
| 4448 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4449 | sprintf(p, "\\U%08x", chr); |
| 4450 | p += 10; |
| 4451 | } |
| 4452 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4453 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4454 | Py_DECREF(w); |
| 4455 | } else { |
| 4456 | *p++ = *s++; |
| 4457 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4458 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4459 | len = p - buf; |
| 4460 | s = buf; |
| 4461 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4462 | const char *first_invalid_escape; |
| 4463 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4464 | |
| 4465 | if (v != NULL && first_invalid_escape != NULL) { |
| 4466 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4467 | /* We have not decref u before because first_invalid_escape points |
| 4468 | inside u. */ |
| 4469 | Py_XDECREF(u); |
| 4470 | Py_DECREF(v); |
| 4471 | return NULL; |
| 4472 | } |
| 4473 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4474 | Py_XDECREF(u); |
| 4475 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4478 | static PyObject * |
| 4479 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4480 | size_t len) |
| 4481 | { |
| 4482 | const char *first_invalid_escape; |
| 4483 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4484 | &first_invalid_escape); |
| 4485 | if (result == NULL) |
| 4486 | return NULL; |
| 4487 | |
| 4488 | if (first_invalid_escape != NULL) { |
| 4489 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4490 | Py_DECREF(result); |
| 4491 | return NULL; |
| 4492 | } |
| 4493 | } |
| 4494 | return result; |
| 4495 | } |
| 4496 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4497 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4498 | and `col_offset` to existing locations. */ |
| 4499 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4500 | { |
| 4501 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4502 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4503 | for (int i = 0; i < NCH(n); ++i) { |
| 4504 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4505 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4506 | col_offset = 0; |
| 4507 | } |
| 4508 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4509 | } |
| 4510 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4511 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | /* Fix locations for the given node and its children. |
| 4515 | |
| 4516 | `parent` is the enclosing node. |
| 4517 | `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] | 4518 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4519 | */ |
| 4520 | static void |
| 4521 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4522 | { |
| 4523 | char *substr = NULL; |
| 4524 | char *start; |
| 4525 | int lines = LINENO(parent) - 1; |
| 4526 | int cols = parent->n_col_offset; |
| 4527 | /* Find the full fstring to fix location information in `n`. */ |
| 4528 | while (parent && parent->n_type != STRING) |
| 4529 | parent = parent->n_child; |
| 4530 | if (parent && parent->n_str) { |
| 4531 | substr = strstr(parent->n_str, expr_str); |
| 4532 | if (substr) { |
| 4533 | start = substr; |
| 4534 | while (start > parent->n_str) { |
| 4535 | if (start[0] == '\n') |
| 4536 | break; |
| 4537 | start--; |
| 4538 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4539 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4540 | /* adjust the start based on the number of newlines encountered |
| 4541 | before the f-string expression */ |
| 4542 | for (char* p = parent->n_str; p < substr; p++) { |
| 4543 | if (*p == '\n') { |
| 4544 | lines++; |
| 4545 | } |
| 4546 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4547 | } |
| 4548 | } |
| 4549 | fstring_shift_node_locations(n, lines, cols); |
| 4550 | } |
| 4551 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4552 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4553 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4554 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4555 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4556 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4557 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4558 | { |
| 4559 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4560 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4561 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4562 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4563 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4564 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4565 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4566 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4567 | assert(*(expr_start-1) == '{'); |
| 4568 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4569 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4570 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4571 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4572 | because turning the expression '' in to '()' would go from being invalid |
| 4573 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4574 | for (s = expr_start; s != expr_end; s++) { |
| 4575 | char c = *s; |
| 4576 | /* The Python parser ignores only the following whitespace |
| 4577 | characters (\r already is converted to \n). */ |
| 4578 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4579 | break; |
| 4580 | } |
| 4581 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4582 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4583 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4584 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4585 | } |
| 4586 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4587 | len = expr_end - expr_start; |
| 4588 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4589 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4590 | if (str == NULL) { |
| 4591 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4592 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4593 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4594 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4595 | str[0] = '('; |
| 4596 | memcpy(str+1, expr_start, len); |
| 4597 | str[len+1] = ')'; |
| 4598 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4599 | |
| 4600 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4601 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4602 | Py_eval_input, 0); |
| 4603 | if (!mod_n) { |
| 4604 | PyMem_RawFree(str); |
| 4605 | return NULL; |
| 4606 | } |
| 4607 | /* Reuse str to find the correct column offset. */ |
| 4608 | str[0] = '{'; |
| 4609 | str[len+1] = '}'; |
| 4610 | fstring_fix_node_location(n, mod_n, str); |
| 4611 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4612 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4613 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4614 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4615 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4616 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | /* Return -1 on error. |
| 4620 | |
| 4621 | Return 0 if we reached the end of the literal. |
| 4622 | |
| 4623 | Return 1 if we haven't reached the end of the literal, but we want |
| 4624 | the caller to process the literal up to this point. Used for |
| 4625 | doubled braces. |
| 4626 | */ |
| 4627 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4628 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4629 | PyObject **literal, int recurse_lvl, |
| 4630 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4631 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4632 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4633 | brace (which isn't part of a unicode name escape such as |
| 4634 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4635 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4636 | const char *s = *str; |
| 4637 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4638 | int result = 0; |
| 4639 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4640 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4641 | while (s < end) { |
| 4642 | char ch = *s++; |
| 4643 | if (!raw && ch == '\\' && s < end) { |
| 4644 | ch = *s++; |
| 4645 | if (ch == 'N') { |
| 4646 | if (s < end && *s++ == '{') { |
| 4647 | while (s < end && *s++ != '}') { |
| 4648 | } |
| 4649 | continue; |
| 4650 | } |
| 4651 | break; |
| 4652 | } |
| 4653 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4654 | return -1; |
| 4655 | } |
| 4656 | } |
| 4657 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4658 | /* Check for doubled braces, but only at the top level. If |
| 4659 | we checked at every level, then f'{0:{3}}' would fail |
| 4660 | with the two closing braces. */ |
| 4661 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4662 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4663 | /* We're going to tell the caller that the literal ends |
| 4664 | here, but that they should continue scanning. But also |
| 4665 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4666 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4667 | result = 1; |
| 4668 | goto done; |
| 4669 | } |
| 4670 | |
| 4671 | /* Where a single '{' is the start of a new expression, a |
| 4672 | single '}' is not allowed. */ |
| 4673 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4674 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4675 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4676 | return -1; |
| 4677 | } |
| 4678 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4679 | /* We're either at a '{', which means we're starting another |
| 4680 | expression; or a '}', which means we're at the end of this |
| 4681 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4682 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4683 | break; |
| 4684 | } |
| 4685 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4686 | *str = s; |
| 4687 | assert(s <= end); |
| 4688 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4689 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4690 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4691 | if (raw) |
| 4692 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4693 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4694 | NULL, NULL); |
| 4695 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4696 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4697 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4698 | if (!*literal) |
| 4699 | return -1; |
| 4700 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4701 | return result; |
| 4702 | } |
| 4703 | |
| 4704 | /* Forward declaration because parsing is recursive. */ |
| 4705 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4706 | 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] | 4707 | struct compiling *c, const node *n); |
| 4708 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4709 | /* Parse the f-string at *str, ending at end. We know *str starts an |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4710 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4711 | which includes the expression, conversion character, and |
| 4712 | format_spec expression. |
| 4713 | |
| 4714 | Note that I don't do a perfect job here: I don't make sure that a |
| 4715 | closing brace doesn't match an opening paren, for example. It |
| 4716 | doesn't need to error on all invalid expressions, just correctly |
| 4717 | find the end of all valid ones. Any errors inside the expression |
| 4718 | will be caught when we parse it later. */ |
| 4719 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4720 | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4721 | expr_ty *expression, struct compiling *c, const node *n) |
| 4722 | { |
| 4723 | /* Return -1 on error, else 0. */ |
| 4724 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4725 | const char *expr_start; |
| 4726 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4727 | expr_ty simple_expression; |
| 4728 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4729 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4730 | |
| 4731 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4732 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4733 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4734 | |
| 4735 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4736 | int string_type = 0; |
| 4737 | |
| 4738 | /* Keep track of nesting level for braces/parens/brackets in |
| 4739 | expressions. */ |
| 4740 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4741 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4742 | |
| 4743 | /* Can only nest one level deep. */ |
| 4744 | if (recurse_lvl >= 2) { |
| 4745 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4746 | return -1; |
| 4747 | } |
| 4748 | |
| 4749 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4750 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4751 | assert(**str == '{'); |
| 4752 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4753 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4754 | expr_start = *str; |
| 4755 | for (; *str < end; (*str)++) { |
| 4756 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4757 | |
| 4758 | /* Loop invariants. */ |
| 4759 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4760 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4761 | if (quote_char) |
| 4762 | assert(string_type == 1 || string_type == 3); |
| 4763 | else |
| 4764 | assert(string_type == 0); |
| 4765 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4766 | ch = **str; |
| 4767 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4768 | if (ch == '\\') { |
| 4769 | /* Error: can't include a backslash character, inside |
| 4770 | parens or strings or not. */ |
| 4771 | ast_error(c, n, "f-string expression part " |
| 4772 | "cannot include a backslash"); |
| 4773 | return -1; |
| 4774 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4775 | if (quote_char) { |
| 4776 | /* We're inside a string. See if we're at the end. */ |
| 4777 | /* This code needs to implement the same non-error logic |
| 4778 | as tok_get from tokenizer.c, at the letter_quote |
| 4779 | label. To actually share that code would be a |
| 4780 | nightmare. But, it's unlikely to change and is small, |
| 4781 | so duplicate it here. Note we don't need to catch all |
| 4782 | of the errors, since they'll be caught when parsing the |
| 4783 | expression. We just need to match the non-error |
| 4784 | cases. Thus we can ignore \n in single-quoted strings, |
| 4785 | for example. Or non-terminated strings. */ |
| 4786 | if (ch == quote_char) { |
| 4787 | /* Does this match the string_type (single or triple |
| 4788 | quoted)? */ |
| 4789 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4790 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4791 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4792 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4793 | string_type = 0; |
| 4794 | quote_char = 0; |
| 4795 | continue; |
| 4796 | } |
| 4797 | } else { |
| 4798 | /* We're at the end of a normal string. */ |
| 4799 | quote_char = 0; |
| 4800 | string_type = 0; |
| 4801 | continue; |
| 4802 | } |
| 4803 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4804 | } else if (ch == '\'' || ch == '"') { |
| 4805 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4806 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4807 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4808 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4809 | } else { |
| 4810 | /* Start of a normal string. */ |
| 4811 | string_type = 1; |
| 4812 | } |
| 4813 | /* Start looking for the end of the string. */ |
| 4814 | quote_char = ch; |
| 4815 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4816 | if (nested_depth >= MAXLEVEL) { |
| 4817 | ast_error(c, n, "f-string: too many nested parenthesis"); |
| 4818 | return -1; |
| 4819 | } |
| 4820 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4821 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4822 | } else if (ch == '#') { |
| 4823 | /* Error: can't include a comment character, inside parens |
| 4824 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4825 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4826 | return -1; |
| 4827 | } else if (nested_depth == 0 && |
| 4828 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4829 | /* First, test for the special case of "!=". Since '=' is |
| 4830 | not an allowed conversion character, nothing is lost in |
| 4831 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4832 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4833 | /* This isn't a conversion character, just continue. */ |
| 4834 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4835 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4836 | /* Normal way out of this loop. */ |
| 4837 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4838 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 4839 | if (!nested_depth) { |
| 4840 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
| 4841 | return -1; |
| 4842 | } |
| 4843 | nested_depth--; |
| 4844 | int opening = parenstack[nested_depth]; |
| 4845 | if (!((opening == '(' && ch == ')') || |
| 4846 | (opening == '[' && ch == ']') || |
| 4847 | (opening == '{' && ch == '}'))) |
| 4848 | { |
| 4849 | ast_error(c, n, |
| 4850 | "f-string: closing parenthesis '%c' " |
| 4851 | "does not match opening parenthesis '%c'", |
| 4852 | ch, opening); |
| 4853 | return -1; |
| 4854 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4855 | } else { |
| 4856 | /* Just consume this char and loop around. */ |
| 4857 | } |
| 4858 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4859 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4860 | /* If we leave this loop in a string or with mismatched parens, we |
| 4861 | don't care. We'll get a syntax error when compiling the |
| 4862 | expression. But, we can produce a better error message, so |
| 4863 | let's just do that.*/ |
| 4864 | if (quote_char) { |
| 4865 | ast_error(c, n, "f-string: unterminated string"); |
| 4866 | return -1; |
| 4867 | } |
| 4868 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4869 | int opening = parenstack[nested_depth - 1]; |
| 4870 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4871 | return -1; |
| 4872 | } |
| 4873 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4874 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4875 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4876 | |
| 4877 | /* Compile the expression as soon as possible, so we show errors |
| 4878 | related to the expression before errors related to the |
| 4879 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4880 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4881 | if (!simple_expression) |
| 4882 | return -1; |
| 4883 | |
| 4884 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4885 | if (**str == '!') { |
| 4886 | *str += 1; |
| 4887 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4888 | goto unexpected_end_of_string; |
| 4889 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4890 | conversion = **str; |
| 4891 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4892 | |
| 4893 | /* Validate the conversion. */ |
| 4894 | if (!(conversion == 's' || conversion == 'r' |
| 4895 | || conversion == 'a')) { |
| 4896 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4897 | "expected 's', 'r', or 'a'"); |
| 4898 | return -1; |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4903 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4904 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4905 | if (**str == ':') { |
| 4906 | *str += 1; |
| 4907 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4908 | goto unexpected_end_of_string; |
| 4909 | |
| 4910 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4911 | 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] | 4912 | if (!format_spec) |
| 4913 | return -1; |
| 4914 | } |
| 4915 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4916 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4917 | goto unexpected_end_of_string; |
| 4918 | |
| 4919 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4920 | assert(*str < end); |
| 4921 | assert(**str == '}'); |
| 4922 | *str += 1; |
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 | /* And now create the FormattedValue node that represents this |
| 4925 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4926 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4927 | format_spec, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4928 | n->n_end_lineno, n->n_end_col_offset, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4929 | c->c_arena); |
| 4930 | if (!*expression) |
| 4931 | return -1; |
| 4932 | |
| 4933 | return 0; |
| 4934 | |
| 4935 | unexpected_end_of_string: |
| 4936 | ast_error(c, n, "f-string: expecting '}'"); |
| 4937 | return -1; |
| 4938 | } |
| 4939 | |
| 4940 | /* Return -1 on error. |
| 4941 | |
| 4942 | Return 0 if we have a literal (possible zero length) and an |
| 4943 | expression (zero length if at the end of the string. |
| 4944 | |
| 4945 | Return 1 if we have a literal, but no expression, and we want the |
| 4946 | caller to call us again. This is used to deal with doubled |
| 4947 | braces. |
| 4948 | |
| 4949 | When called multiple times on the string 'a{{b{0}c', this function |
| 4950 | will return: |
| 4951 | |
| 4952 | 1. the literal 'a{' with no expression, and a return value |
| 4953 | of 1. Despite the fact that there's no expression, the return |
| 4954 | value of 1 means we're not finished yet. |
| 4955 | |
| 4956 | 2. the literal 'b' and the expression '0', with a return value of |
| 4957 | 0. The fact that there's an expression means we're not finished. |
| 4958 | |
| 4959 | 3. literal 'c' with no expression and a return value of 0. The |
| 4960 | combination of the return value of 0 with no expression means |
| 4961 | we're finished. |
| 4962 | */ |
| 4963 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4964 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4965 | int recurse_lvl, PyObject **literal, |
| 4966 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4967 | struct compiling *c, const node *n) |
| 4968 | { |
| 4969 | int result; |
| 4970 | |
| 4971 | assert(*literal == NULL && *expression == NULL); |
| 4972 | |
| 4973 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4974 | 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] | 4975 | if (result < 0) |
| 4976 | goto error; |
| 4977 | |
| 4978 | assert(result == 0 || result == 1); |
| 4979 | |
| 4980 | if (result == 1) |
| 4981 | /* We have a literal, but don't look at the expression. */ |
| 4982 | return 1; |
| 4983 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4984 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4985 | /* We're at the end of the string or the end of a nested |
| 4986 | f-string: no expression. The top-level error case where we |
| 4987 | expect to be at the end of the string but we're at a '}' is |
| 4988 | handled later. */ |
| 4989 | return 0; |
| 4990 | |
| 4991 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4992 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4993 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4994 | if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4995 | goto error; |
| 4996 | |
| 4997 | return 0; |
| 4998 | |
| 4999 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5000 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5001 | return -1; |
| 5002 | } |
| 5003 | |
| 5004 | #define EXPRLIST_N_CACHED 64 |
| 5005 | |
| 5006 | typedef struct { |
| 5007 | /* Incrementally build an array of expr_ty, so be used in an |
| 5008 | asdl_seq. Cache some small but reasonably sized number of |
| 5009 | expr_ty's, and then after that start dynamically allocating, |
| 5010 | doubling the number allocated each time. Note that the f-string |
| 5011 | 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] | 5012 | Constant for the literal 'a'. So you add expr_ty's about twice as |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5013 | fast as you add exressions in an f-string. */ |
| 5014 | |
| 5015 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5016 | Py_ssize_t size; /* Number we've used. */ |
| 5017 | expr_ty *p; /* Pointer to the memory we're actually |
| 5018 | using. Will point to 'data' until we |
| 5019 | start dynamically allocating. */ |
| 5020 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5021 | } ExprList; |
| 5022 | |
| 5023 | #ifdef NDEBUG |
| 5024 | #define ExprList_check_invariants(l) |
| 5025 | #else |
| 5026 | static void |
| 5027 | ExprList_check_invariants(ExprList *l) |
| 5028 | { |
| 5029 | /* Check our invariants. Make sure this object is "live", and |
| 5030 | hasn't been deallocated. */ |
| 5031 | assert(l->size >= 0); |
| 5032 | assert(l->p != NULL); |
| 5033 | if (l->size <= EXPRLIST_N_CACHED) |
| 5034 | assert(l->data == l->p); |
| 5035 | } |
| 5036 | #endif |
| 5037 | |
| 5038 | static void |
| 5039 | ExprList_Init(ExprList *l) |
| 5040 | { |
| 5041 | l->allocated = EXPRLIST_N_CACHED; |
| 5042 | l->size = 0; |
| 5043 | |
| 5044 | /* Until we start allocating dynamically, p points to data. */ |
| 5045 | l->p = l->data; |
| 5046 | |
| 5047 | ExprList_check_invariants(l); |
| 5048 | } |
| 5049 | |
| 5050 | static int |
| 5051 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5052 | { |
| 5053 | ExprList_check_invariants(l); |
| 5054 | if (l->size >= l->allocated) { |
| 5055 | /* We need to alloc (or realloc) the memory. */ |
| 5056 | Py_ssize_t new_size = l->allocated * 2; |
| 5057 | |
| 5058 | /* See if we've ever allocated anything dynamically. */ |
| 5059 | if (l->p == l->data) { |
| 5060 | Py_ssize_t i; |
| 5061 | /* We're still using the cached data. Switch to |
| 5062 | alloc-ing. */ |
| 5063 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5064 | if (!l->p) |
| 5065 | return -1; |
| 5066 | /* Copy the cached data into the new buffer. */ |
| 5067 | for (i = 0; i < l->size; i++) |
| 5068 | l->p[i] = l->data[i]; |
| 5069 | } else { |
| 5070 | /* Just realloc. */ |
| 5071 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5072 | if (!tmp) { |
| 5073 | PyMem_RawFree(l->p); |
| 5074 | l->p = NULL; |
| 5075 | return -1; |
| 5076 | } |
| 5077 | l->p = tmp; |
| 5078 | } |
| 5079 | |
| 5080 | l->allocated = new_size; |
| 5081 | assert(l->allocated == 2 * l->size); |
| 5082 | } |
| 5083 | |
| 5084 | l->p[l->size++] = exp; |
| 5085 | |
| 5086 | ExprList_check_invariants(l); |
| 5087 | return 0; |
| 5088 | } |
| 5089 | |
| 5090 | static void |
| 5091 | ExprList_Dealloc(ExprList *l) |
| 5092 | { |
| 5093 | ExprList_check_invariants(l); |
| 5094 | |
| 5095 | /* If there's been an error, or we've never dynamically allocated, |
| 5096 | do nothing. */ |
| 5097 | if (!l->p || l->p == l->data) { |
| 5098 | /* Do nothing. */ |
| 5099 | } else { |
| 5100 | /* We have dynamically allocated. Free the memory. */ |
| 5101 | PyMem_RawFree(l->p); |
| 5102 | } |
| 5103 | l->p = NULL; |
| 5104 | l->size = -1; |
| 5105 | } |
| 5106 | |
| 5107 | static asdl_seq * |
| 5108 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5109 | { |
| 5110 | asdl_seq *seq; |
| 5111 | |
| 5112 | ExprList_check_invariants(l); |
| 5113 | |
| 5114 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5115 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5116 | if (seq) { |
| 5117 | Py_ssize_t i; |
| 5118 | for (i = 0; i < l->size; i++) |
| 5119 | asdl_seq_SET(seq, i, l->p[i]); |
| 5120 | } |
| 5121 | ExprList_Dealloc(l); |
| 5122 | return seq; |
| 5123 | } |
| 5124 | |
| 5125 | /* The FstringParser is designed to add a mix of strings and |
| 5126 | f-strings, and concat them together as needed. Ultimately, it |
| 5127 | generates an expr_ty. */ |
| 5128 | typedef struct { |
| 5129 | PyObject *last_str; |
| 5130 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5131 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5132 | } FstringParser; |
| 5133 | |
| 5134 | #ifdef NDEBUG |
| 5135 | #define FstringParser_check_invariants(state) |
| 5136 | #else |
| 5137 | static void |
| 5138 | FstringParser_check_invariants(FstringParser *state) |
| 5139 | { |
| 5140 | if (state->last_str) |
| 5141 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5142 | ExprList_check_invariants(&state->expr_list); |
| 5143 | } |
| 5144 | #endif |
| 5145 | |
| 5146 | static void |
| 5147 | FstringParser_Init(FstringParser *state) |
| 5148 | { |
| 5149 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5150 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5151 | ExprList_Init(&state->expr_list); |
| 5152 | FstringParser_check_invariants(state); |
| 5153 | } |
| 5154 | |
| 5155 | static void |
| 5156 | FstringParser_Dealloc(FstringParser *state) |
| 5157 | { |
| 5158 | FstringParser_check_invariants(state); |
| 5159 | |
| 5160 | Py_XDECREF(state->last_str); |
| 5161 | ExprList_Dealloc(&state->expr_list); |
| 5162 | } |
| 5163 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5164 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5165 | static expr_ty |
| 5166 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5167 | { |
| 5168 | PyObject *s = *str; |
| 5169 | *str = NULL; |
| 5170 | assert(PyUnicode_CheckExact(s)); |
| 5171 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5172 | Py_DECREF(s); |
| 5173 | return NULL; |
| 5174 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5175 | return Constant(s, LINENO(n), n->n_col_offset, |
| 5176 | 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] | 5177 | } |
| 5178 | |
| 5179 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5180 | decref'd. */ |
| 5181 | static int |
| 5182 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5183 | { |
| 5184 | FstringParser_check_invariants(state); |
| 5185 | |
| 5186 | assert(PyUnicode_CheckExact(str)); |
| 5187 | |
| 5188 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5189 | Py_DECREF(str); |
| 5190 | return 0; |
| 5191 | } |
| 5192 | |
| 5193 | if (!state->last_str) { |
| 5194 | /* We didn't have a string before, so just remember this one. */ |
| 5195 | state->last_str = str; |
| 5196 | } else { |
| 5197 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5198 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5199 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5200 | return -1; |
| 5201 | } |
| 5202 | FstringParser_check_invariants(state); |
| 5203 | return 0; |
| 5204 | } |
| 5205 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5206 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5207 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5208 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5209 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5210 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5211 | struct compiling *c, const node *n) |
| 5212 | { |
| 5213 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5214 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5215 | |
| 5216 | /* Parse the f-string. */ |
| 5217 | while (1) { |
| 5218 | PyObject *literal = NULL; |
| 5219 | expr_ty expression = NULL; |
| 5220 | |
| 5221 | /* If there's a zero length literal in front of the |
| 5222 | expression, literal will be NULL. If we're at the end of |
| 5223 | the f-string, expression will be NULL (unless result == 1, |
| 5224 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5225 | int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5226 | &literal, &expression, |
| 5227 | c, n); |
| 5228 | if (result < 0) |
| 5229 | return -1; |
| 5230 | |
| 5231 | /* Add the literal, if any. */ |
| 5232 | if (!literal) { |
| 5233 | /* Do nothing. Just leave last_str alone (and possibly |
| 5234 | NULL). */ |
| 5235 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 5236 | /* Note that the literal can be zero length, if the |
| 5237 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5238 | state->last_str = literal; |
| 5239 | literal = NULL; |
| 5240 | } else { |
| 5241 | /* We have a literal, concatenate it. */ |
| 5242 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 5243 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 5244 | return -1; |
| 5245 | literal = NULL; |
| 5246 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5247 | |
| 5248 | /* We've dealt with the literal now. It can't be leaked on further |
| 5249 | errors. */ |
| 5250 | assert(literal == NULL); |
| 5251 | |
| 5252 | /* See if we should just loop around to get the next literal |
| 5253 | and expression, while ignoring the expression this |
| 5254 | time. This is used for un-doubling braces, as an |
| 5255 | optimization. */ |
| 5256 | if (result == 1) |
| 5257 | continue; |
| 5258 | |
| 5259 | if (!expression) |
| 5260 | /* We're done with this f-string. */ |
| 5261 | break; |
| 5262 | |
| 5263 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5264 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5265 | if (!state->last_str) { |
| 5266 | /* Do nothing. No previous literal. */ |
| 5267 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5268 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5269 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5270 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5271 | return -1; |
| 5272 | } |
| 5273 | |
| 5274 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5275 | return -1; |
| 5276 | } |
| 5277 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5278 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5279 | string. Otherwise, we must be at a right brace. */ |
| 5280 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5281 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5282 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5283 | return -1; |
| 5284 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5285 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5286 | ast_error(c, n, "f-string: expecting '}'"); |
| 5287 | return -1; |
| 5288 | } |
| 5289 | |
| 5290 | FstringParser_check_invariants(state); |
| 5291 | return 0; |
| 5292 | } |
| 5293 | |
| 5294 | /* 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] | 5295 | 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] | 5296 | static expr_ty |
| 5297 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5298 | const node *n) |
| 5299 | { |
| 5300 | asdl_seq *seq; |
| 5301 | |
| 5302 | FstringParser_check_invariants(state); |
| 5303 | |
| 5304 | /* If we're just a constant string with no expressions, return |
| 5305 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5306 | if (!state->fmode) { |
| 5307 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5308 | if (!state->last_str) { |
| 5309 | /* Create a zero length string. */ |
| 5310 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5311 | if (!state->last_str) |
| 5312 | goto error; |
| 5313 | } |
| 5314 | return make_str_node_and_del(&state->last_str, c, n); |
| 5315 | } |
| 5316 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5317 | /* 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] | 5318 | last node in our expression list. */ |
| 5319 | if (state->last_str) { |
| 5320 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5321 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5322 | goto error; |
| 5323 | } |
| 5324 | /* This has already been freed. */ |
| 5325 | assert(state->last_str == NULL); |
| 5326 | |
| 5327 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5328 | if (!seq) |
| 5329 | goto error; |
| 5330 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5331 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5332 | 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] | 5333 | |
| 5334 | error: |
| 5335 | FstringParser_Dealloc(state); |
| 5336 | return NULL; |
| 5337 | } |
| 5338 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5339 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5340 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5341 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5342 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5343 | 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] | 5344 | struct compiling *c, const node *n) |
| 5345 | { |
| 5346 | FstringParser state; |
| 5347 | |
| 5348 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5349 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5350 | c, n) < 0) { |
| 5351 | FstringParser_Dealloc(&state); |
| 5352 | return NULL; |
| 5353 | } |
| 5354 | |
| 5355 | return FstringParser_Finish(&state, c, n); |
| 5356 | } |
| 5357 | |
| 5358 | /* n is a Python string literal, including the bracketing quote |
| 5359 | 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] | 5360 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5361 | 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] | 5362 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5363 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5364 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5365 | static int |
| 5366 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5367 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5368 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5369 | size_t len; |
| 5370 | const char *s = STR(n); |
| 5371 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5372 | int fmode = 0; |
| 5373 | *bytesmode = 0; |
| 5374 | *rawmode = 0; |
| 5375 | *result = NULL; |
| 5376 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5377 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5378 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5379 | if (quote == 'b' || quote == 'B') { |
| 5380 | quote = *++s; |
| 5381 | *bytesmode = 1; |
| 5382 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5383 | else if (quote == 'u' || quote == 'U') { |
| 5384 | quote = *++s; |
| 5385 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5386 | else if (quote == 'r' || quote == 'R') { |
| 5387 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5388 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5389 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5390 | else if (quote == 'f' || quote == 'F') { |
| 5391 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5392 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5393 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5394 | else { |
| 5395 | break; |
| 5396 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5397 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5398 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5399 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5400 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5401 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5402 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5403 | if (quote != '\'' && quote != '\"') { |
| 5404 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5405 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5406 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5407 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5408 | s++; |
| 5409 | len = strlen(s); |
| 5410 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5412 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5413 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5414 | } |
| 5415 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5416 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5417 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5418 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5419 | } |
| 5420 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5421 | /* A triple quoted string. We've already skipped one quote at |
| 5422 | the start and one at the end of the string. Now skip the |
| 5423 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5424 | s += 2; |
| 5425 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5426 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5427 | if (s[--len] != quote || s[--len] != quote) { |
| 5428 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5429 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5430 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5431 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5432 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5433 | if (fmode) { |
| 5434 | /* Just return the bytes. The caller will parse the resulting |
| 5435 | string. */ |
| 5436 | *fstr = s; |
| 5437 | *fstrlen = len; |
| 5438 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5439 | } |
| 5440 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5441 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5442 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5443 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5444 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5445 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5446 | const char *ch; |
| 5447 | for (ch = s; *ch; ch++) { |
| 5448 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5449 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5450 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5451 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5452 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5453 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5454 | if (*rawmode) |
| 5455 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5456 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5457 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5458 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5459 | if (*rawmode) |
| 5460 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5461 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5462 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5463 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5464 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5467 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5468 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5469 | 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] | 5470 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5471 | 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] | 5472 | node if there's just an f-string (with no leading or trailing |
| 5473 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5474 | any literals involved. */ |
| 5475 | static expr_ty |
| 5476 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5477 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5478 | int bytesmode = 0; |
| 5479 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5480 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5481 | |
| 5482 | FstringParser state; |
| 5483 | FstringParser_Init(&state); |
| 5484 | |
| 5485 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5486 | int this_bytesmode; |
| 5487 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5488 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5489 | const char *fstr; |
| 5490 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5491 | |
| 5492 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5493 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5494 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5495 | goto error; |
| 5496 | |
| 5497 | /* Check that we're not mixing bytes with unicode. */ |
| 5498 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5499 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5500 | /* s is NULL if the current string part is an f-string. */ |
| 5501 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5502 | goto error; |
| 5503 | } |
| 5504 | bytesmode = this_bytesmode; |
| 5505 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5506 | if (fstr != NULL) { |
| 5507 | int result; |
| 5508 | assert(s == NULL && !bytesmode); |
| 5509 | /* This is an f-string. Parse and concatenate it. */ |
| 5510 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5511 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5512 | if (result < 0) |
| 5513 | goto error; |
| 5514 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5515 | /* A string or byte string. */ |
| 5516 | assert(s != NULL && fstr == NULL); |
| 5517 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5518 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5519 | PyUnicode_CheckExact(s)); |
| 5520 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5521 | if (bytesmode) { |
| 5522 | /* For bytes, concat as we go. */ |
| 5523 | if (i == 0) { |
| 5524 | /* First time, just remember this value. */ |
| 5525 | bytes_str = s; |
| 5526 | } else { |
| 5527 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5528 | if (!bytes_str) |
| 5529 | goto error; |
| 5530 | } |
| 5531 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5532 | /* This is a regular string. Concatenate it. */ |
| 5533 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5534 | goto error; |
| 5535 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5536 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5537 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5538 | if (bytesmode) { |
| 5539 | /* Just return the bytes object and we're done. */ |
| 5540 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5541 | goto error; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5542 | return Constant(bytes_str, LINENO(n), n->n_col_offset, |
| 5543 | 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] | 5544 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5545 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5546 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5547 | assert(bytes_str == NULL); |
| 5548 | |
| 5549 | return FstringParser_Finish(&state, c, n); |
| 5550 | |
| 5551 | error: |
| 5552 | Py_XDECREF(bytes_str); |
| 5553 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5554 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5555 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5556 | |
| 5557 | PyObject * |
| 5558 | _PyAST_GetDocString(asdl_seq *body) |
| 5559 | { |
| 5560 | if (!asdl_seq_LEN(body)) { |
| 5561 | return NULL; |
| 5562 | } |
| 5563 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5564 | if (st->kind != Expr_kind) { |
| 5565 | return NULL; |
| 5566 | } |
| 5567 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5568 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5569 | return e->v.Constant.value; |
| 5570 | } |
| 5571 | return NULL; |
| 5572 | } |