Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file includes functions to transform a concrete syntax tree (CST) to |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 | * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4 | * |
| 5 | */ |
| 6 | #include "Python.h" |
| 7 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 8 | #include "node.h" |
| 9 | #include "ast.h" |
| 10 | #include "token.h" |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 11 | #include "pythonrun.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 14 | #include <stdbool.h> |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 | |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 16 | #define MAXLEVEL 200 /* Max parentheses level */ |
| 17 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 18 | static int validate_stmts(asdl_seq *); |
| 19 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 20 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 21 | static int validate_stmt(stmt_ty); |
| 22 | static int validate_expr(expr_ty, expr_context_ty); |
| 23 | |
| 24 | static int |
| 25 | validate_comprehension(asdl_seq *gens) |
| 26 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 27 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 28 | if (!asdl_seq_LEN(gens)) { |
| 29 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 30 | return 0; |
| 31 | } |
| 32 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 33 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 34 | if (!validate_expr(comp->target, Store) || |
| 35 | !validate_expr(comp->iter, Load) || |
| 36 | !validate_exprs(comp->ifs, Load, 0)) |
| 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | validate_slice(slice_ty slice) |
| 44 | { |
| 45 | switch (slice->kind) { |
| 46 | case Slice_kind: |
| 47 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 48 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 49 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 50 | case ExtSlice_kind: { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 51 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 52 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 53 | return 0; |
| 54 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 55 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 56 | return 0; |
| 57 | return 1; |
| 58 | } |
| 59 | case Index_kind: |
| 60 | return validate_expr(slice->v.Index.value, Load); |
| 61 | default: |
| 62 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | validate_keywords(asdl_seq *keywords) |
| 69 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 70 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 71 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 72 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 73 | return 0; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | validate_args(asdl_seq *args) |
| 79 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 80 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 81 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 82 | arg_ty arg = asdl_seq_GET(args, i); |
| 83 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 84 | return 0; |
| 85 | } |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static const char * |
| 90 | expr_context_name(expr_context_ty ctx) |
| 91 | { |
| 92 | switch (ctx) { |
| 93 | case Load: |
| 94 | return "Load"; |
| 95 | case Store: |
| 96 | return "Store"; |
| 97 | case Del: |
| 98 | return "Del"; |
| 99 | case AugLoad: |
| 100 | return "AugLoad"; |
| 101 | case AugStore: |
| 102 | return "AugStore"; |
| 103 | case Param: |
| 104 | return "Param"; |
| 105 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 106 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | validate_arguments(arguments_ty args) |
| 112 | { |
| 113 | if (!validate_args(args->args)) |
| 114 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 115 | if (args->vararg && args->vararg->annotation |
| 116 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | if (!validate_args(args->kwonlyargs)) |
| 120 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 121 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 122 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 126 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 127 | return 0; |
| 128 | } |
| 129 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 130 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 131 | "kw_defaults on arguments"); |
| 132 | return 0; |
| 133 | } |
| 134 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 135 | } |
| 136 | |
| 137 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 138 | validate_constant(PyObject *value) |
| 139 | { |
| 140 | if (value == Py_None || value == Py_Ellipsis) |
| 141 | return 1; |
| 142 | |
| 143 | if (PyLong_CheckExact(value) |
| 144 | || PyFloat_CheckExact(value) |
| 145 | || PyComplex_CheckExact(value) |
| 146 | || PyBool_Check(value) |
| 147 | || PyUnicode_CheckExact(value) |
| 148 | || PyBytes_CheckExact(value)) |
| 149 | return 1; |
| 150 | |
| 151 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 152 | PyObject *it; |
| 153 | |
| 154 | it = PyObject_GetIter(value); |
| 155 | if (it == NULL) |
| 156 | return 0; |
| 157 | |
| 158 | while (1) { |
| 159 | PyObject *item = PyIter_Next(it); |
| 160 | if (item == NULL) { |
| 161 | if (PyErr_Occurred()) { |
| 162 | Py_DECREF(it); |
| 163 | return 0; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (!validate_constant(item)) { |
| 169 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 170 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 173 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | Py_DECREF(it); |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 184 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 185 | { |
| 186 | int check_ctx = 1; |
| 187 | expr_context_ty actual_ctx; |
| 188 | |
| 189 | /* First check expression context. */ |
| 190 | switch (exp->kind) { |
| 191 | case Attribute_kind: |
| 192 | actual_ctx = exp->v.Attribute.ctx; |
| 193 | break; |
| 194 | case Subscript_kind: |
| 195 | actual_ctx = exp->v.Subscript.ctx; |
| 196 | break; |
| 197 | case Starred_kind: |
| 198 | actual_ctx = exp->v.Starred.ctx; |
| 199 | break; |
| 200 | case Name_kind: |
| 201 | actual_ctx = exp->v.Name.ctx; |
| 202 | break; |
| 203 | case List_kind: |
| 204 | actual_ctx = exp->v.List.ctx; |
| 205 | break; |
| 206 | case Tuple_kind: |
| 207 | actual_ctx = exp->v.Tuple.ctx; |
| 208 | break; |
| 209 | default: |
| 210 | if (ctx != Load) { |
| 211 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 212 | "assigned to in %s context", expr_context_name(ctx)); |
| 213 | return 0; |
| 214 | } |
| 215 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 216 | /* set actual_ctx to prevent gcc warning */ |
| 217 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 218 | } |
| 219 | if (check_ctx && actual_ctx != ctx) { |
| 220 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 221 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* Now validate expression. */ |
| 226 | switch (exp->kind) { |
| 227 | case BoolOp_kind: |
| 228 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 229 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 230 | return 0; |
| 231 | } |
| 232 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 233 | case BinOp_kind: |
| 234 | return validate_expr(exp->v.BinOp.left, Load) && |
| 235 | validate_expr(exp->v.BinOp.right, Load); |
| 236 | case UnaryOp_kind: |
| 237 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 238 | case Lambda_kind: |
| 239 | return validate_arguments(exp->v.Lambda.args) && |
| 240 | validate_expr(exp->v.Lambda.body, Load); |
| 241 | case IfExp_kind: |
| 242 | return validate_expr(exp->v.IfExp.test, Load) && |
| 243 | validate_expr(exp->v.IfExp.body, Load) && |
| 244 | validate_expr(exp->v.IfExp.orelse, Load); |
| 245 | case Dict_kind: |
| 246 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 247 | PyErr_SetString(PyExc_ValueError, |
| 248 | "Dict doesn't have the same number of keys as values"); |
| 249 | return 0; |
| 250 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 251 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 252 | dict literals, i.e. ``{**{a:b}}`` */ |
| 253 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 254 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 255 | case Set_kind: |
| 256 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 257 | #define COMP(NAME) \ |
| 258 | case NAME ## _kind: \ |
| 259 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 260 | validate_expr(exp->v.NAME.elt, Load); |
| 261 | COMP(ListComp) |
| 262 | COMP(SetComp) |
| 263 | COMP(GeneratorExp) |
| 264 | #undef COMP |
| 265 | case DictComp_kind: |
| 266 | return validate_comprehension(exp->v.DictComp.generators) && |
| 267 | validate_expr(exp->v.DictComp.key, Load) && |
| 268 | validate_expr(exp->v.DictComp.value, Load); |
| 269 | case Yield_kind: |
| 270 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 271 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 272 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 273 | case Await_kind: |
| 274 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 275 | case Compare_kind: |
| 276 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 277 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 278 | return 0; |
| 279 | } |
| 280 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 281 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 282 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 283 | "of comparators and operands"); |
| 284 | return 0; |
| 285 | } |
| 286 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 287 | validate_expr(exp->v.Compare.left, Load); |
| 288 | case Call_kind: |
| 289 | return validate_expr(exp->v.Call.func, Load) && |
| 290 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 291 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 292 | case Constant_kind: |
| 293 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 294 | PyErr_Format(PyExc_TypeError, |
| 295 | "got an invalid type in Constant: %s", |
| 296 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 300 | case JoinedStr_kind: |
| 301 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 302 | case FormattedValue_kind: |
| 303 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 304 | return 0; |
| 305 | if (exp->v.FormattedValue.format_spec) |
| 306 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 307 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 308 | case Attribute_kind: |
| 309 | return validate_expr(exp->v.Attribute.value, Load); |
| 310 | case Subscript_kind: |
| 311 | return validate_slice(exp->v.Subscript.slice) && |
| 312 | validate_expr(exp->v.Subscript.value, Load); |
| 313 | case Starred_kind: |
| 314 | return validate_expr(exp->v.Starred.value, ctx); |
| 315 | case List_kind: |
| 316 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 317 | case Tuple_kind: |
| 318 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 319 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 320 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 321 | return 1; |
| 322 | default: |
| 323 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static int |
| 329 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 330 | { |
| 331 | if (asdl_seq_LEN(seq)) |
| 332 | return 1; |
| 333 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int |
| 338 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 339 | { |
| 340 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 341 | validate_exprs(targets, ctx, 0); |
| 342 | } |
| 343 | |
| 344 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 345 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 346 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 347 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static int |
| 351 | validate_stmt(stmt_ty stmt) |
| 352 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 353 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 354 | switch (stmt->kind) { |
| 355 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 356 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 357 | validate_arguments(stmt->v.FunctionDef.args) && |
| 358 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 359 | (!stmt->v.FunctionDef.returns || |
| 360 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 361 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 362 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 363 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 364 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 365 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 366 | case Return_kind: |
| 367 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 368 | case Delete_kind: |
| 369 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 370 | case Assign_kind: |
| 371 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 372 | validate_expr(stmt->v.Assign.value, Load); |
| 373 | case AugAssign_kind: |
| 374 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 375 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 376 | case AnnAssign_kind: |
| 377 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 378 | stmt->v.AnnAssign.simple) { |
| 379 | PyErr_SetString(PyExc_TypeError, |
| 380 | "AnnAssign with simple non-Name target"); |
| 381 | return 0; |
| 382 | } |
| 383 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 384 | (!stmt->v.AnnAssign.value || |
| 385 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 386 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 387 | case For_kind: |
| 388 | return validate_expr(stmt->v.For.target, Store) && |
| 389 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 390 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 391 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 392 | case AsyncFor_kind: |
| 393 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 394 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 395 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 396 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 397 | case While_kind: |
| 398 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 399 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 400 | validate_stmts(stmt->v.While.orelse); |
| 401 | case If_kind: |
| 402 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 403 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 404 | validate_stmts(stmt->v.If.orelse); |
| 405 | case With_kind: |
| 406 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 407 | return 0; |
| 408 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 409 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 410 | if (!validate_expr(item->context_expr, Load) || |
| 411 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 412 | return 0; |
| 413 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 414 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 415 | case AsyncWith_kind: |
| 416 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 417 | return 0; |
| 418 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 419 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 420 | if (!validate_expr(item->context_expr, Load) || |
| 421 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 422 | return 0; |
| 423 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 424 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 425 | case Raise_kind: |
| 426 | if (stmt->v.Raise.exc) { |
| 427 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 428 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 429 | } |
| 430 | if (stmt->v.Raise.cause) { |
| 431 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 432 | return 0; |
| 433 | } |
| 434 | return 1; |
| 435 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 436 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 437 | return 0; |
| 438 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 439 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 440 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 441 | return 0; |
| 442 | } |
| 443 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 444 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 445 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 446 | return 0; |
| 447 | } |
| 448 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 449 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 450 | if ((handler->v.ExceptHandler.type && |
| 451 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 452 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 456 | validate_stmts(stmt->v.Try.finalbody)) && |
| 457 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 458 | validate_stmts(stmt->v.Try.orelse)); |
| 459 | case Assert_kind: |
| 460 | return validate_expr(stmt->v.Assert.test, Load) && |
| 461 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 462 | case Import_kind: |
| 463 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 464 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 465 | if (stmt->v.ImportFrom.level < 0) { |
| 466 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 470 | case Global_kind: |
| 471 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 472 | case Nonlocal_kind: |
| 473 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 474 | case Expr_kind: |
| 475 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 476 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 477 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 478 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 479 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 480 | (!stmt->v.AsyncFunctionDef.returns || |
| 481 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 482 | case Pass_kind: |
| 483 | case Break_kind: |
| 484 | case Continue_kind: |
| 485 | return 1; |
| 486 | default: |
| 487 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 488 | return 0; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static int |
| 493 | validate_stmts(asdl_seq *seq) |
| 494 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 495 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 496 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 497 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 498 | if (stmt) { |
| 499 | if (!validate_stmt(stmt)) |
| 500 | return 0; |
| 501 | } |
| 502 | else { |
| 503 | PyErr_SetString(PyExc_ValueError, |
| 504 | "None disallowed in statement list"); |
| 505 | return 0; |
| 506 | } |
| 507 | } |
| 508 | return 1; |
| 509 | } |
| 510 | |
| 511 | static int |
| 512 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 513 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 514 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 515 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 516 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 517 | if (expr) { |
| 518 | if (!validate_expr(expr, ctx)) |
| 519 | return 0; |
| 520 | } |
| 521 | else if (!null_ok) { |
| 522 | PyErr_SetString(PyExc_ValueError, |
| 523 | "None disallowed in expression list"); |
| 524 | return 0; |
| 525 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 526 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 527 | } |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | int |
| 532 | PyAST_Validate(mod_ty mod) |
| 533 | { |
| 534 | int res = 0; |
| 535 | |
| 536 | switch (mod->kind) { |
| 537 | case Module_kind: |
| 538 | res = validate_stmts(mod->v.Module.body); |
| 539 | break; |
| 540 | case Interactive_kind: |
| 541 | res = validate_stmts(mod->v.Interactive.body); |
| 542 | break; |
| 543 | case Expression_kind: |
| 544 | res = validate_expr(mod->v.Expression.body, Load); |
| 545 | break; |
| 546 | case Suite_kind: |
| 547 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 548 | break; |
| 549 | default: |
| 550 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 551 | res = 0; |
| 552 | break; |
| 553 | } |
| 554 | return res; |
| 555 | } |
| 556 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 557 | /* This is done here, so defines like "test" don't interfere with AST use above. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 558 | #include "grammar.h" |
| 559 | #include "parsetok.h" |
| 560 | #include "graminit.h" |
| 561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | /* Data structure used internally */ |
| 563 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 564 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 565 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 566 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 570 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 571 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 572 | 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] | 573 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 574 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 575 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 576 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 577 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 578 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 579 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 580 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 581 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 582 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 583 | const node *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 584 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 585 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 586 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 587 | static void get_last_end_pos(asdl_seq *, int *, int *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 588 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 589 | #define COMP_GENEXP 0 |
| 590 | #define COMP_LISTCOMP 1 |
| 591 | #define COMP_SETCOMP 2 |
| 592 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 593 | static int |
| 594 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 595 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 596 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 597 | if (!m) |
| 598 | return 0; |
| 599 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 600 | Py_DECREF(m); |
| 601 | if (!c->c_normalize) |
| 602 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 603 | return 1; |
| 604 | } |
| 605 | |
| 606 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 607 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 608 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 609 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 610 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 611 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 612 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 613 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 614 | /* Check whether there are non-ASCII characters in the |
| 615 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 616 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 617 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 618 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 619 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 620 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 621 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 622 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 623 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 624 | if (form == NULL) { |
| 625 | Py_DECREF(id); |
| 626 | return NULL; |
| 627 | } |
| 628 | PyObject *args[2] = {form, id}; |
| 629 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 630 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 631 | if (!id2) |
| 632 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 633 | if (!PyUnicode_Check(id2)) { |
| 634 | PyErr_Format(PyExc_TypeError, |
| 635 | "unicodedata.normalize() must return a string, not " |
| 636 | "%.200s", |
| 637 | Py_TYPE(id2)->tp_name); |
| 638 | Py_DECREF(id2); |
| 639 | return NULL; |
| 640 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 641 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 642 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 643 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 644 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 645 | Py_DECREF(id); |
| 646 | return NULL; |
| 647 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 648 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 651 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 653 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 654 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 655 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 656 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 657 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 659 | va_start(va, errmsg); |
| 660 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 661 | va_end(va); |
| 662 | if (!errstr) { |
| 663 | return 0; |
| 664 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 665 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 666 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 667 | Py_INCREF(Py_None); |
| 668 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 669 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 670 | 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] | 671 | if (!tmp) { |
| 672 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 673 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 674 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 675 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 676 | Py_DECREF(errstr); |
| 677 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 678 | if (value) { |
| 679 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 680 | Py_DECREF(value); |
| 681 | } |
| 682 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* num_stmts() returns number of contained statements. |
| 686 | |
| 687 | Use this routine to determine how big a sequence is needed for |
| 688 | the statements in a parse tree. Its raison d'etre is this bit of |
| 689 | grammar: |
| 690 | |
| 691 | stmt: simple_stmt | compound_stmt |
| 692 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 693 | |
| 694 | A simple_stmt can contain multiple small_stmt elements joined |
| 695 | by semicolons. If the arg is a simple_stmt, the number of |
| 696 | small_stmt elements is returned. |
| 697 | */ |
| 698 | |
| 699 | static int |
| 700 | num_stmts(const node *n) |
| 701 | { |
| 702 | int i, l; |
| 703 | node *ch; |
| 704 | |
| 705 | switch (TYPE(n)) { |
| 706 | case single_input: |
| 707 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 708 | return 0; |
| 709 | else |
| 710 | return num_stmts(CHILD(n, 0)); |
| 711 | case file_input: |
| 712 | l = 0; |
| 713 | for (i = 0; i < NCH(n); i++) { |
| 714 | ch = CHILD(n, i); |
| 715 | if (TYPE(ch) == stmt) |
| 716 | l += num_stmts(ch); |
| 717 | } |
| 718 | return l; |
| 719 | case stmt: |
| 720 | return num_stmts(CHILD(n, 0)); |
| 721 | case compound_stmt: |
| 722 | return 1; |
| 723 | case simple_stmt: |
| 724 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 725 | case suite: |
| 726 | if (NCH(n) == 1) |
| 727 | return num_stmts(CHILD(n, 0)); |
| 728 | else { |
| 729 | l = 0; |
| 730 | for (i = 2; i < (NCH(n) - 1); i++) |
| 731 | l += num_stmts(CHILD(n, i)); |
| 732 | return l; |
| 733 | } |
| 734 | default: { |
| 735 | char buf[128]; |
| 736 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 737 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 738 | TYPE(n), NCH(n)); |
| 739 | Py_FatalError(buf); |
| 740 | } |
| 741 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 742 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* Transform the CST rooted at node * to the appropriate AST |
| 746 | */ |
| 747 | |
| 748 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 749 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 750 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 751 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 752 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 753 | asdl_seq *stmts = NULL; |
| 754 | stmt_ty s; |
| 755 | node *ch; |
| 756 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 757 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 758 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 759 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 760 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 761 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 762 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 763 | |
| 764 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 766 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 767 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | switch (TYPE(n)) { |
| 769 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 770 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 771 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 772 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | for (i = 0; i < NCH(n) - 1; i++) { |
| 774 | ch = CHILD(n, i); |
| 775 | if (TYPE(ch) == NEWLINE) |
| 776 | continue; |
| 777 | REQ(ch, stmt); |
| 778 | num = num_stmts(ch); |
| 779 | if (num == 1) { |
| 780 | s = ast_for_stmt(&c, ch); |
| 781 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 782 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 783 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | } |
| 785 | else { |
| 786 | ch = CHILD(ch, 0); |
| 787 | REQ(ch, simple_stmt); |
| 788 | for (j = 0; j < num; j++) { |
| 789 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 790 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 791 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 792 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 796 | res = Module(stmts, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 797 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 | case eval_input: { |
| 799 | expr_ty testlist_ast; |
| 800 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 801 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 802 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 804 | goto out; |
| 805 | res = Expression(testlist_ast, arena); |
| 806 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 807 | } |
| 808 | case single_input: |
| 809 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 810 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 811 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 812 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 813 | 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] | 814 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 815 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 816 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 817 | goto out; |
| 818 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | } |
| 820 | else { |
| 821 | n = CHILD(n, 0); |
| 822 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 823 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 824 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 825 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 826 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 827 | s = ast_for_stmt(&c, n); |
| 828 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 829 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 830 | asdl_seq_SET(stmts, 0, s); |
| 831 | } |
| 832 | else { |
| 833 | /* Only a simple_stmt can contain multiple statements. */ |
| 834 | REQ(n, simple_stmt); |
| 835 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 837 | break; |
| 838 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 839 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 840 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 841 | asdl_seq_SET(stmts, i / 2, s); |
| 842 | } |
| 843 | } |
| 844 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 845 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 846 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 847 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 848 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 849 | PyErr_Format(PyExc_SystemError, |
| 850 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 851 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 852 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 853 | out: |
| 854 | if (c.c_normalize) { |
| 855 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 856 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 857 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 860 | mod_ty |
| 861 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 862 | PyArena *arena) |
| 863 | { |
| 864 | mod_ty mod; |
| 865 | PyObject *filename; |
| 866 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 867 | if (filename == NULL) |
| 868 | return NULL; |
| 869 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 870 | Py_DECREF(filename); |
| 871 | return mod; |
| 872 | |
| 873 | } |
| 874 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 875 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 876 | */ |
| 877 | |
| 878 | static operator_ty |
| 879 | get_operator(const node *n) |
| 880 | { |
| 881 | switch (TYPE(n)) { |
| 882 | case VBAR: |
| 883 | return BitOr; |
| 884 | case CIRCUMFLEX: |
| 885 | return BitXor; |
| 886 | case AMPER: |
| 887 | return BitAnd; |
| 888 | case LEFTSHIFT: |
| 889 | return LShift; |
| 890 | case RIGHTSHIFT: |
| 891 | return RShift; |
| 892 | case PLUS: |
| 893 | return Add; |
| 894 | case MINUS: |
| 895 | return Sub; |
| 896 | case STAR: |
| 897 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 898 | case AT: |
| 899 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 900 | case SLASH: |
| 901 | return Div; |
| 902 | case DOUBLESLASH: |
| 903 | return FloorDiv; |
| 904 | case PERCENT: |
| 905 | return Mod; |
| 906 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 907 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 911 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 912 | "None", |
| 913 | "True", |
| 914 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 915 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 916 | NULL, |
| 917 | }; |
| 918 | |
| 919 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 920 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 921 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 922 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 923 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 924 | const char * const *p = FORBIDDEN; |
| 925 | if (!full_checks) { |
| 926 | /* In most cases, the parser will protect True, False, and None |
| 927 | from being assign to. */ |
| 928 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 929 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 930 | for (; *p; p++) { |
| 931 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 932 | ast_error(c, n, "cannot assign to %U", name); |
| 933 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | return 0; |
| 937 | } |
| 938 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 939 | static expr_ty |
| 940 | copy_location(expr_ty e, const node *n) |
| 941 | { |
| 942 | if (e) { |
| 943 | e->lineno = LINENO(n); |
| 944 | e->col_offset = n->n_col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 945 | e->end_lineno = n->n_end_lineno; |
| 946 | e->end_col_offset = n->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 947 | } |
| 948 | return e; |
| 949 | } |
| 950 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 951 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 952 | |
| 953 | Only sets context for expr kinds that "can appear in assignment context" |
| 954 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 955 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 956 | */ |
| 957 | |
| 958 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 959 | 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] | 960 | { |
| 961 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 962 | /* If a particular expression type can't be used for assign / delete, |
| 963 | set expr_name to its name and an error message will be generated. |
| 964 | */ |
| 965 | const char* expr_name = NULL; |
| 966 | |
| 967 | /* The ast defines augmented store and load contexts, but the |
| 968 | implementation here doesn't actually use them. The code may be |
| 969 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 970 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 971 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 972 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 973 | */ |
| 974 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 975 | |
| 976 | switch (e->kind) { |
| 977 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 978 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 979 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 980 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 981 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 982 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 983 | e->v.Subscript.ctx = ctx; |
| 984 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 985 | case Starred_kind: |
| 986 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 987 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 988 | return 0; |
| 989 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 990 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 991 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 992 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 993 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 994 | } |
| 995 | e->v.Name.ctx = ctx; |
| 996 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 997 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 998 | e->v.List.ctx = ctx; |
| 999 | s = e->v.List.elts; |
| 1000 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1001 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1002 | e->v.Tuple.ctx = ctx; |
| 1003 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1004 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1005 | case Lambda_kind: |
| 1006 | expr_name = "lambda"; |
| 1007 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1008 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1009 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1010 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1011 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1012 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1013 | case UnaryOp_kind: |
| 1014 | expr_name = "operator"; |
| 1015 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1016 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1017 | expr_name = "generator expression"; |
| 1018 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1019 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1020 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1021 | expr_name = "yield expression"; |
| 1022 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1023 | case Await_kind: |
| 1024 | expr_name = "await expression"; |
| 1025 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1026 | case ListComp_kind: |
| 1027 | expr_name = "list comprehension"; |
| 1028 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1029 | case SetComp_kind: |
| 1030 | expr_name = "set comprehension"; |
| 1031 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1032 | case DictComp_kind: |
| 1033 | expr_name = "dict comprehension"; |
| 1034 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1035 | case Dict_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1036 | expr_name = "dict display"; |
| 1037 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1038 | case Set_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1039 | expr_name = "set display"; |
| 1040 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1041 | case JoinedStr_kind: |
| 1042 | case FormattedValue_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1043 | expr_name = "f-string expression"; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1044 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1045 | case Constant_kind: { |
| 1046 | PyObject *value = e->v.Constant.value; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1047 | if (value == Py_None || value == Py_False || value == Py_True |
| 1048 | || value == Py_Ellipsis) |
| 1049 | { |
| 1050 | return ast_error(c, n, "cannot %s %R", |
| 1051 | ctx == Store ? "assign to" : "delete", |
| 1052 | value); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1053 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1054 | expr_name = "literal"; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1055 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1056 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1057 | case Compare_kind: |
| 1058 | expr_name = "comparison"; |
| 1059 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1060 | case IfExp_kind: |
| 1061 | expr_name = "conditional expression"; |
| 1062 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1063 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | PyErr_Format(PyExc_SystemError, |
| 1065 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1066 | e->kind, e->lineno); |
| 1067 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1068 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1069 | /* Check for error string set by switch */ |
| 1070 | if (expr_name) { |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1071 | return ast_error(c, n, "cannot %s %s", |
| 1072 | ctx == Store ? "assign to" : "delete", |
| 1073 | expr_name); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1076 | /* 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] | 1077 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1078 | */ |
| 1079 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1080 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1081 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1082 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1083 | 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] | 1084 | return 0; |
| 1085 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1086 | } |
| 1087 | return 1; |
| 1088 | } |
| 1089 | |
| 1090 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1091 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1092 | { |
| 1093 | REQ(n, augassign); |
| 1094 | n = CHILD(n, 0); |
| 1095 | switch (STR(n)[0]) { |
| 1096 | case '+': |
| 1097 | return Add; |
| 1098 | case '-': |
| 1099 | return Sub; |
| 1100 | case '/': |
| 1101 | if (STR(n)[1] == '/') |
| 1102 | return FloorDiv; |
| 1103 | else |
| 1104 | return Div; |
| 1105 | case '%': |
| 1106 | return Mod; |
| 1107 | case '<': |
| 1108 | return LShift; |
| 1109 | case '>': |
| 1110 | return RShift; |
| 1111 | case '&': |
| 1112 | return BitAnd; |
| 1113 | case '^': |
| 1114 | return BitXor; |
| 1115 | case '|': |
| 1116 | return BitOr; |
| 1117 | case '*': |
| 1118 | if (STR(n)[1] == '*') |
| 1119 | return Pow; |
| 1120 | else |
| 1121 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1122 | case '@': |
| 1123 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1124 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1125 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1126 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1131 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1132 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1133 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1134 | |'is' 'not' |
| 1135 | */ |
| 1136 | REQ(n, comp_op); |
| 1137 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1138 | n = CHILD(n, 0); |
| 1139 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1140 | case LESS: |
| 1141 | return Lt; |
| 1142 | case GREATER: |
| 1143 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1144 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1145 | return Eq; |
| 1146 | case LESSEQUAL: |
| 1147 | return LtE; |
| 1148 | case GREATEREQUAL: |
| 1149 | return GtE; |
| 1150 | case NOTEQUAL: |
| 1151 | return NotEq; |
| 1152 | case NAME: |
| 1153 | if (strcmp(STR(n), "in") == 0) |
| 1154 | return In; |
| 1155 | if (strcmp(STR(n), "is") == 0) |
| 1156 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1157 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1158 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1159 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1160 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1161 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1162 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1163 | } |
| 1164 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1165 | /* handle "not in" and "is not" */ |
| 1166 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1167 | case NAME: |
| 1168 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1169 | return NotIn; |
| 1170 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1171 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1172 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1174 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1175 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1176 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1177 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1179 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1180 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1181 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static asdl_seq * |
| 1185 | seq_for_testlist(struct compiling *c, const node *n) |
| 1186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1188 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1189 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1190 | asdl_seq *seq; |
| 1191 | expr_ty expression; |
| 1192 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1193 | 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] | 1194 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1195 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1196 | if (!seq) |
| 1197 | return NULL; |
| 1198 | |
| 1199 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1201 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1202 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1203 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1204 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1205 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1206 | |
| 1207 | assert(i / 2 < seq->size); |
| 1208 | asdl_seq_SET(seq, i / 2, expression); |
| 1209 | } |
| 1210 | return seq; |
| 1211 | } |
| 1212 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1213 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1214 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1215 | { |
| 1216 | identifier name; |
| 1217 | expr_ty annotation = NULL; |
| 1218 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1219 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1220 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1221 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1222 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1223 | name = NEW_IDENTIFIER(ch); |
| 1224 | if (!name) |
| 1225 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1226 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1227 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1228 | |
| 1229 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1230 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1231 | if (!annotation) |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1235 | ret = arg(name, annotation, LINENO(n), n->n_col_offset, |
| 1236 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1237 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1238 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1239 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1242 | /* returns -1 if failed to handle keyword only arguments |
| 1243 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1244 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1245 | ^^^ |
| 1246 | start pointing here |
| 1247 | */ |
| 1248 | static int |
| 1249 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1250 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1251 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1252 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1253 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1254 | expr_ty expression, annotation; |
| 1255 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1256 | int i = start; |
| 1257 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1260 | 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] | 1261 | return -1; |
| 1262 | } |
| 1263 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1264 | while (i < NCH(n)) { |
| 1265 | ch = CHILD(n, i); |
| 1266 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1267 | case vfpdef: |
| 1268 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1269 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1270 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1271 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1272 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1273 | asdl_seq_SET(kwdefaults, j, expression); |
| 1274 | i += 2; /* '=' and test */ |
| 1275 | } |
| 1276 | else { /* setting NULL if no default value exists */ |
| 1277 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1278 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1279 | if (NCH(ch) == 3) { |
| 1280 | /* ch is NAME ':' test */ |
| 1281 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1282 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1283 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1284 | } |
| 1285 | else { |
| 1286 | annotation = NULL; |
| 1287 | } |
| 1288 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1289 | argname = NEW_IDENTIFIER(ch); |
| 1290 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1291 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1292 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1293 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1294 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1295 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1296 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1297 | if (!arg) |
| 1298 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1299 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1300 | i += 2; /* the name and the comma */ |
| 1301 | break; |
| 1302 | case DOUBLESTAR: |
| 1303 | return i; |
| 1304 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1305 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1306 | goto error; |
| 1307 | } |
| 1308 | } |
| 1309 | return i; |
| 1310 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1312 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1313 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1314 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1315 | |
| 1316 | static arguments_ty |
| 1317 | ast_for_arguments(struct compiling *c, const node *n) |
| 1318 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1319 | /* This function handles both typedargslist (function definition) |
| 1320 | and varargslist (lambda definition). |
| 1321 | |
| 1322 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1323 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1324 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1325 | | '**' tfpdef [',']]] |
| 1326 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1327 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1328 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1329 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1330 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1331 | | '**' vfpdef [',']]] |
| 1332 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1333 | | '**' vfpdef [','] |
| 1334 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1335 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1336 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1337 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1338 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1339 | int nposdefaults = 0, found_default = 0; |
| 1340 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1341 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1342 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1343 | node *ch; |
| 1344 | |
| 1345 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1346 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1347 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1348 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1349 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1350 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1351 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1352 | /* First count the number of positional args & defaults. The |
| 1353 | variable i is the loop index for this for loop and the next. |
| 1354 | The next loop picks up where the first leaves off. |
| 1355 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1357 | ch = CHILD(n, i); |
| 1358 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1359 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1360 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1361 | if (i < NCH(n) && /* skip argument following star */ |
| 1362 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1363 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1364 | i++; |
| 1365 | } |
| 1366 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1367 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1368 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1369 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1370 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1371 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1373 | defaults for keyword only args */ |
| 1374 | for ( ; i < NCH(n); ++i) { |
| 1375 | ch = CHILD(n, i); |
| 1376 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1377 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1378 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1379 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1380 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1381 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1382 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1383 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1384 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1385 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1387 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1388 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1389 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1391 | since we set NULL as default for keyword only argument w/o default |
| 1392 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1393 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1394 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1395 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1396 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1398 | /* tfpdef: NAME [':' test] |
| 1399 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1400 | */ |
| 1401 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1402 | j = 0; /* index for defaults */ |
| 1403 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1404 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1405 | ch = CHILD(n, i); |
| 1406 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1407 | case tfpdef: |
| 1408 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1409 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1410 | anything other than EQUAL or a comma? */ |
| 1411 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1412 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1413 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1414 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1415 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1416 | assert(posdefaults != NULL); |
| 1417 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1420 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1421 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1422 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1423 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1424 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1425 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1426 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1427 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1428 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1429 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1430 | i += 2; /* the name and the comma */ |
| 1431 | break; |
| 1432 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1433 | if (i+1 >= NCH(n) || |
| 1434 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1435 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1436 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1437 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1438 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1439 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1440 | if (TYPE(ch) == COMMA) { |
| 1441 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1442 | i += 2; /* now follows keyword only arguments */ |
| 1443 | res = handle_keywordonly_args(c, n, i, |
| 1444 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1445 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1446 | i = res; /* res has new position to process */ |
| 1447 | } |
| 1448 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1449 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1450 | if (!vararg) |
| 1451 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1452 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1453 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1454 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1455 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1456 | int res = 0; |
| 1457 | res = handle_keywordonly_args(c, n, i, |
| 1458 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1459 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1460 | i = res; /* res has new position to process */ |
| 1461 | } |
| 1462 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1463 | break; |
| 1464 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1465 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1466 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1467 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1468 | if (!kwarg) |
| 1469 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1470 | i += 3; |
| 1471 | break; |
| 1472 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1473 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | "unexpected node in varargslist: %d @ %d", |
| 1475 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1476 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1477 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1478 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1479 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | static expr_ty |
| 1483 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1484 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1485 | expr_ty e; |
| 1486 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1487 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1488 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1489 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | |
| 1491 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1492 | |
| 1493 | lineno = LINENO(n); |
| 1494 | col_offset = n->n_col_offset; |
| 1495 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1496 | ch = CHILD(n, 0); |
| 1497 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1499 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1500 | e = Name(id, Load, lineno, col_offset, |
| 1501 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1502 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1503 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1504 | |
| 1505 | for (i = 2; i < NCH(n); i+=2) { |
| 1506 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1507 | if (!id) |
| 1508 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1509 | e = Attribute(e, id, Load, lineno, col_offset, |
| 1510 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1511 | if (!e) |
| 1512 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | static expr_ty |
| 1519 | ast_for_decorator(struct compiling *c, const node *n) |
| 1520 | { |
| 1521 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1522 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1523 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1526 | REQ(CHILD(n, 0), AT); |
| 1527 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1530 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1531 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1533 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1534 | d = name_expr; |
| 1535 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1536 | } |
| 1537 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1538 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1539 | 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] | 1540 | if (!d) |
| 1541 | return NULL; |
| 1542 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 | } |
| 1544 | else { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1545 | 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] | 1546 | if (!d) |
| 1547 | return NULL; |
| 1548 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | static asdl_seq* |
| 1555 | ast_for_decorators(struct compiling *c, const node *n) |
| 1556 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1557 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1558 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1559 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1561 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1562 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1563 | if (!decorator_seq) |
| 1564 | return NULL; |
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 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1567 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1568 | if (!d) |
| 1569 | return NULL; |
| 1570 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1571 | } |
| 1572 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1576 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1577 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1578 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1579 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1580 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1581 | identifier name; |
| 1582 | arguments_ty args; |
| 1583 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1584 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1585 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1586 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1587 | |
| 1588 | REQ(n, funcdef); |
| 1589 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1590 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1591 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1592 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1593 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1594 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1595 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1596 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1597 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1598 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1599 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1600 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1601 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1602 | name_i += 2; |
| 1603 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1604 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1605 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1606 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1607 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1608 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1609 | if (is_async) |
| 1610 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1611 | 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] | 1612 | else |
| 1613 | return FunctionDef(name, args, body, decorator_seq, returns, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1614 | 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] | 1615 | } |
| 1616 | |
| 1617 | static stmt_ty |
| 1618 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1619 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1620 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1621 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1622 | REQ(CHILD(n, 0), NAME); |
| 1623 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1624 | REQ(CHILD(n, 1), funcdef); |
| 1625 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1626 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1627 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | static stmt_ty |
| 1631 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1632 | { |
| 1633 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1634 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1635 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | static stmt_ty |
| 1640 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1641 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1642 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1643 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1644 | REQ(CHILD(n, 0), NAME); |
| 1645 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1646 | |
| 1647 | switch (TYPE(CHILD(n, 1))) { |
| 1648 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1649 | return ast_for_funcdef_impl(c, n, NULL, |
| 1650 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1651 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1652 | return ast_for_with_stmt(c, n, |
| 1653 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1654 | |
| 1655 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1656 | return ast_for_for_stmt(c, n, |
| 1657 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1658 | |
| 1659 | default: |
| 1660 | PyErr_Format(PyExc_SystemError, |
| 1661 | "invalid async stament: %s", |
| 1662 | STR(CHILD(n, 1))); |
| 1663 | return NULL; |
| 1664 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1667 | static stmt_ty |
| 1668 | ast_for_decorated(struct compiling *c, const node *n) |
| 1669 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1670 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1671 | stmt_ty thing = NULL; |
| 1672 | asdl_seq *decorator_seq = NULL; |
| 1673 | |
| 1674 | REQ(n, decorated); |
| 1675 | |
| 1676 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1677 | if (!decorator_seq) |
| 1678 | return NULL; |
| 1679 | |
| 1680 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1681 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1683 | |
| 1684 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1685 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1686 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1687 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1688 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1689 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1690 | } |
| 1691 | return thing; |
| 1692 | } |
| 1693 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1694 | static expr_ty |
| 1695 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1696 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1697 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1698 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1699 | arguments_ty args; |
| 1700 | expr_ty expression; |
| 1701 | |
| 1702 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1703 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1704 | if (!args) |
| 1705 | return NULL; |
| 1706 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1707 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1709 | } |
| 1710 | else { |
| 1711 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1712 | if (!args) |
| 1713 | return NULL; |
| 1714 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1715 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1719 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1720 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1723 | static expr_ty |
| 1724 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1727 | expr_ty expression, body, orelse; |
| 1728 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1729 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1730 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1731 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1732 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1733 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1734 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1735 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1736 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1737 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1738 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1739 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1740 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1741 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1744 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1745 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1746 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1747 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1748 | */ |
| 1749 | |
| 1750 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1751 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1752 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1753 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1754 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1755 | count_comp_for: |
| 1756 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1757 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1758 | if (NCH(n) == 2) { |
| 1759 | REQ(CHILD(n, 0), NAME); |
| 1760 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1761 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1762 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1763 | else if (NCH(n) == 1) { |
| 1764 | n = CHILD(n, 0); |
| 1765 | } |
| 1766 | else { |
| 1767 | goto error; |
| 1768 | } |
| 1769 | if (NCH(n) == (5)) { |
| 1770 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1771 | } |
| 1772 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1773 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1774 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1775 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1776 | REQ(n, comp_iter); |
| 1777 | n = CHILD(n, 0); |
| 1778 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1779 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1780 | else if (TYPE(n) == comp_if) { |
| 1781 | if (NCH(n) == 3) { |
| 1782 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1783 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1784 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1785 | else |
| 1786 | return n_fors; |
| 1787 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1788 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1789 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1790 | /* Should never be reached */ |
| 1791 | PyErr_SetString(PyExc_SystemError, |
| 1792 | "logic error in count_comp_fors"); |
| 1793 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1796 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1798 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1799 | */ |
| 1800 | |
| 1801 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1802 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1803 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1804 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1805 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1806 | while (1) { |
| 1807 | REQ(n, comp_iter); |
| 1808 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1809 | return n_ifs; |
| 1810 | n = CHILD(n, 0); |
| 1811 | REQ(n, comp_if); |
| 1812 | n_ifs++; |
| 1813 | if (NCH(n) == 2) |
| 1814 | return n_ifs; |
| 1815 | n = CHILD(n, 2); |
| 1816 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1819 | static asdl_seq * |
| 1820 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1821 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1822 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1823 | asdl_seq *comps; |
| 1824 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1825 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1826 | if (n_fors == -1) |
| 1827 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1828 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1829 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1830 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1831 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1832 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1834 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1835 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1836 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1837 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1838 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1839 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1841 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1843 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1844 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1845 | REQ(CHILD(n, 0), NAME); |
| 1846 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1847 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1848 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1849 | else { |
| 1850 | sync_n = CHILD(n, 0); |
| 1851 | } |
| 1852 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1853 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1854 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1855 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1856 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1857 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1858 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1859 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1860 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1861 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1862 | /* Check the # of children rather than the length of t, since |
| 1863 | (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] | 1864 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1865 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1866 | comp = comprehension(first, expression, NULL, |
| 1867 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1868 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1869 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 1870 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 1871 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1872 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1873 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1874 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1875 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1876 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1877 | int j, n_ifs; |
| 1878 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1880 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1881 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1882 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1883 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1885 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1886 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1887 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1888 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1889 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1890 | REQ(n, comp_iter); |
| 1891 | n = CHILD(n, 0); |
| 1892 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1894 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1895 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1896 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1897 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1898 | if (NCH(n) == 3) |
| 1899 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1900 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1901 | /* on exit, must guarantee that n is a comp_for */ |
| 1902 | if (TYPE(n) == comp_iter) |
| 1903 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1904 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1905 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1906 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1907 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1908 | return comps; |
| 1909 | } |
| 1910 | |
| 1911 | static expr_ty |
| 1912 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1913 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1914 | /* testlist_comp: (test|star_expr) |
| 1915 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1916 | expr_ty elt; |
| 1917 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1918 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1920 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1921 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1922 | ch = CHILD(n, 0); |
| 1923 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1924 | if (!elt) |
| 1925 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1926 | if (elt->kind == Starred_kind) { |
| 1927 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1928 | return NULL; |
| 1929 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1931 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1932 | if (!comps) |
| 1933 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1934 | |
| 1935 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1936 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 1937 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1938 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1939 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 1940 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1941 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1942 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 1943 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1944 | else |
| 1945 | /* Should never happen */ |
| 1946 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1949 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1950 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1951 | * elements. Iff successful, nonzero is returned. |
| 1952 | */ |
| 1953 | static int |
| 1954 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1955 | expr_ty *key, expr_ty *value) |
| 1956 | { |
| 1957 | expr_ty expression; |
| 1958 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1959 | assert(NCH(n) - *i >= 2); |
| 1960 | |
| 1961 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1962 | if (!expression) |
| 1963 | return 0; |
| 1964 | *key = NULL; |
| 1965 | *value = expression; |
| 1966 | |
| 1967 | *i += 2; |
| 1968 | } |
| 1969 | else { |
| 1970 | assert(NCH(n) - *i >= 3); |
| 1971 | |
| 1972 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1973 | if (!expression) |
| 1974 | return 0; |
| 1975 | *key = expression; |
| 1976 | |
| 1977 | REQ(CHILD(n, *i + 1), COLON); |
| 1978 | |
| 1979 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1980 | if (!expression) |
| 1981 | return 0; |
| 1982 | *value = expression; |
| 1983 | |
| 1984 | *i += 3; |
| 1985 | } |
| 1986 | return 1; |
| 1987 | } |
| 1988 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1989 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1990 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1991 | { |
| 1992 | expr_ty key, value; |
| 1993 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1994 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1996 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1997 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1998 | assert(key); |
| 1999 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2001 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2002 | if (!comps) |
| 2003 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2005 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2006 | 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] | 2007 | } |
| 2008 | |
| 2009 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2010 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2011 | { |
| 2012 | int i; |
| 2013 | int j; |
| 2014 | int size; |
| 2015 | asdl_seq *keys, *values; |
| 2016 | |
| 2017 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2018 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2019 | if (!keys) |
| 2020 | return NULL; |
| 2021 | |
| 2022 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2023 | if (!values) |
| 2024 | return NULL; |
| 2025 | |
| 2026 | j = 0; |
| 2027 | for (i = 0; i < NCH(n); i++) { |
| 2028 | expr_ty key, value; |
| 2029 | |
| 2030 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2031 | return NULL; |
| 2032 | asdl_seq_SET(keys, j, key); |
| 2033 | asdl_seq_SET(values, j, value); |
| 2034 | |
| 2035 | j++; |
| 2036 | } |
| 2037 | keys->size = j; |
| 2038 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2039 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2040 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2044 | ast_for_genexp(struct compiling *c, const node *n) |
| 2045 | { |
| 2046 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2047 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | static expr_ty |
| 2051 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2052 | { |
| 2053 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2054 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | static expr_ty |
| 2058 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2059 | { |
| 2060 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2061 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2064 | static expr_ty |
| 2065 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2066 | { |
| 2067 | int i; |
| 2068 | int size; |
| 2069 | asdl_seq *elts; |
| 2070 | |
| 2071 | assert(TYPE(n) == (dictorsetmaker)); |
| 2072 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2073 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2074 | if (!elts) |
| 2075 | return NULL; |
| 2076 | for (i = 0; i < NCH(n); i += 2) { |
| 2077 | expr_ty expression; |
| 2078 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2079 | if (!expression) |
| 2080 | return NULL; |
| 2081 | asdl_seq_SET(elts, i / 2, expression); |
| 2082 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2083 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2084 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2085 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2086 | |
| 2087 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2088 | ast_for_atom(struct compiling *c, const node *n) |
| 2089 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2090 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2091 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2092 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2093 | */ |
| 2094 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2096 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2097 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2098 | PyObject *name; |
| 2099 | const char *s = STR(ch); |
| 2100 | size_t len = strlen(s); |
| 2101 | if (len >= 4 && len <= 5) { |
| 2102 | if (!strcmp(s, "None")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2103 | return Constant(Py_None, LINENO(n), n->n_col_offset, |
| 2104 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2105 | if (!strcmp(s, "True")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2106 | return Constant(Py_True, LINENO(n), n->n_col_offset, |
| 2107 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2108 | if (!strcmp(s, "False")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2109 | return Constant(Py_False, LINENO(n), n->n_col_offset, |
| 2110 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2111 | } |
| 2112 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2113 | if (!name) |
| 2114 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2115 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2116 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2117 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2118 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2119 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2120 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2121 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2122 | const char *errtype = NULL; |
| 2123 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2124 | errtype = "unicode error"; |
| 2125 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2126 | errtype = "value error"; |
| 2127 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2128 | PyObject *type, *value, *tback, *errstr; |
| 2129 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2130 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2131 | if (errstr) { |
| 2132 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2133 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2134 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2135 | else { |
| 2136 | PyErr_Clear(); |
| 2137 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2138 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2139 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2140 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2141 | Py_XDECREF(tback); |
| 2142 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2143 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2144 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2145 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2146 | } |
| 2147 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2148 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2149 | if (!pynum) |
| 2150 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2151 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2152 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2153 | Py_DECREF(pynum); |
| 2154 | return NULL; |
| 2155 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2156 | return Constant(pynum, LINENO(n), n->n_col_offset, |
| 2157 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2158 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2159 | case ELLIPSIS: /* Ellipsis */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2160 | return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, |
| 2161 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2162 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2163 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2165 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2166 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2167 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2169 | if (TYPE(ch) == yield_expr) |
| 2170 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2173 | if (NCH(ch) == 1) { |
| 2174 | return ast_for_testlist(c, ch); |
| 2175 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2176 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2177 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2178 | return copy_location(ast_for_genexp(c, ch), n); |
| 2179 | } |
| 2180 | else { |
| 2181 | return copy_location(ast_for_testlist(c, ch), n); |
| 2182 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2183 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2184 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2186 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2187 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2188 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2190 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2191 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2192 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2193 | if (!elts) |
| 2194 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2195 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2196 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2197 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2198 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2199 | else { |
| 2200 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2201 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2202 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2203 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2204 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2205 | * ((test | '*' test) |
| 2206 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2207 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2208 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2209 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2210 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2211 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2212 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2213 | } |
| 2214 | else { |
| 2215 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2216 | if (NCH(ch) == 1 || |
| 2217 | (NCH(ch) > 1 && |
| 2218 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2219 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2220 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2221 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2222 | else if (NCH(ch) > 1 && |
| 2223 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2224 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2225 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2226 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2227 | else if (NCH(ch) > 3 - is_dict && |
| 2228 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2229 | /* It's a dictionary comprehension. */ |
| 2230 | if (is_dict) { |
| 2231 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2232 | "dict comprehension"); |
| 2233 | return NULL; |
| 2234 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2235 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2236 | } |
| 2237 | else { |
| 2238 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2239 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2240 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2241 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2242 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2243 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2244 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2245 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2246 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | static slice_ty |
| 2251 | ast_for_slice(struct compiling *c, const node *n) |
| 2252 | { |
| 2253 | node *ch; |
| 2254 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2255 | |
| 2256 | REQ(n, subscript); |
| 2257 | |
| 2258 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2259 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2260 | sliceop: ':' [test] |
| 2261 | */ |
| 2262 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2263 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2264 | /* 'step' variable hold no significance in terms of being used over |
| 2265 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2267 | if (!step) |
| 2268 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2270 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2274 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2275 | if (!lower) |
| 2276 | return NULL; |
| 2277 | } |
| 2278 | |
| 2279 | /* If there's an upper bound it's in the second or third position. */ |
| 2280 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2281 | if (NCH(n) > 1) { |
| 2282 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2283 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2284 | if (TYPE(n2) == test) { |
| 2285 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2286 | if (!upper) |
| 2287 | return NULL; |
| 2288 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2289 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2290 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2291 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2292 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2293 | if (TYPE(n2) == test) { |
| 2294 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2295 | if (!upper) |
| 2296 | return NULL; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | ch = CHILD(n, NCH(n) - 1); |
| 2301 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2302 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2303 | ch = CHILD(ch, 1); |
| 2304 | if (TYPE(ch) == test) { |
| 2305 | step = ast_for_expr(c, ch); |
| 2306 | if (!step) |
| 2307 | return NULL; |
| 2308 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2312 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | static expr_ty |
| 2316 | ast_for_binop(struct compiling *c, const node *n) |
| 2317 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2318 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2320 | BinOp(BinOp(A, op, B), op, C). |
| 2321 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2322 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2323 | int i, nops; |
| 2324 | expr_ty expr1, expr2, result; |
| 2325 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2326 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2327 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2328 | if (!expr1) |
| 2329 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2330 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2331 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2332 | if (!expr2) |
| 2333 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2334 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2335 | newoperator = get_operator(CHILD(n, 1)); |
| 2336 | if (!newoperator) |
| 2337 | return NULL; |
| 2338 | |
| 2339 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2340 | 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] | 2341 | c->c_arena); |
| 2342 | if (!result) |
| 2343 | return NULL; |
| 2344 | |
| 2345 | nops = (NCH(n) - 1) / 2; |
| 2346 | for (i = 1; i < nops; i++) { |
| 2347 | expr_ty tmp_result, tmp; |
| 2348 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2349 | |
| 2350 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2351 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2352 | return NULL; |
| 2353 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2354 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2355 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2356 | return NULL; |
| 2357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2359 | LINENO(next_oper), next_oper->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2360 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2361 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2362 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2364 | return NULL; |
| 2365 | result = tmp_result; |
| 2366 | } |
| 2367 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2370 | static expr_ty |
| 2371 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2374 | subscriptlist: subscript (',' subscript)* [','] |
| 2375 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2376 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2377 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2378 | REQ(n, trailer); |
| 2379 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2380 | if (NCH(n) == 2) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2381 | return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, |
| 2382 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2383 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2384 | 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] | 2385 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2386 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2387 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2388 | if (!attr_id) |
| 2389 | return NULL; |
| 2390 | return Attribute(left_expr, attr_id, Load, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2391 | LINENO(n), n->n_col_offset, |
| 2392 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2393 | } |
| 2394 | else { |
| 2395 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2396 | REQ(CHILD(n, 2), RSQB); |
| 2397 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2398 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2399 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2400 | if (!slc) |
| 2401 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2402 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2403 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2404 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2405 | } |
| 2406 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2408 | by treating the sequence as a tuple literal if there are |
| 2409 | no slice features. |
| 2410 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2411 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2412 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2413 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2414 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2415 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2416 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2417 | if (!slices) |
| 2418 | return NULL; |
| 2419 | for (j = 0; j < NCH(n); j += 2) { |
| 2420 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2421 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2422 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2423 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2424 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2425 | asdl_seq_SET(slices, j / 2, slc); |
| 2426 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2427 | if (!simple) { |
| 2428 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2429 | Load, LINENO(n), n->n_col_offset, |
| 2430 | 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] | 2431 | } |
| 2432 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2433 | 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] | 2434 | if (!elts) |
| 2435 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2436 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2437 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2438 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2439 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2440 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2441 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2442 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2443 | if (!e) |
| 2444 | return NULL; |
| 2445 | return Subscript(left_expr, Index(e, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2446 | Load, LINENO(n), n->n_col_offset, |
| 2447 | 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] | 2448 | } |
| 2449 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2453 | ast_for_factor(struct compiling *c, const node *n) |
| 2454 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2455 | expr_ty expression; |
| 2456 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2457 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2458 | if (!expression) |
| 2459 | return NULL; |
| 2460 | |
| 2461 | switch (TYPE(CHILD(n, 0))) { |
| 2462 | case PLUS: |
| 2463 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2464 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2465 | c->c_arena); |
| 2466 | case MINUS: |
| 2467 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2468 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2469 | c->c_arena); |
| 2470 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2471 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2472 | n->n_end_lineno, n->n_end_col_offset, |
| 2473 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2474 | } |
| 2475 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2476 | TYPE(CHILD(n, 0))); |
| 2477 | return NULL; |
| 2478 | } |
| 2479 | |
| 2480 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2481 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2482 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2483 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2484 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2485 | |
| 2486 | REQ(n, atom_expr); |
| 2487 | nch = NCH(n); |
| 2488 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2489 | 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] | 2490 | start = 1; |
| 2491 | assert(nch > 1); |
| 2492 | } |
| 2493 | |
| 2494 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2495 | if (!e) |
| 2496 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2497 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2498 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2499 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2500 | return Await(e, LINENO(n), n->n_col_offset, |
| 2501 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2505 | node *ch = CHILD(n, i); |
| 2506 | if (TYPE(ch) != trailer) |
| 2507 | break; |
| 2508 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2509 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2510 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2511 | tmp->lineno = e->lineno; |
| 2512 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2513 | e = tmp; |
| 2514 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2515 | |
| 2516 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2517 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2518 | return Await(e, LINENO(n), n->n_col_offset, |
| 2519 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2520 | } |
| 2521 | else { |
| 2522 | return e; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | static expr_ty |
| 2527 | ast_for_power(struct compiling *c, const node *n) |
| 2528 | { |
| 2529 | /* power: atom trailer* ('**' factor)* |
| 2530 | */ |
| 2531 | expr_ty e; |
| 2532 | REQ(n, power); |
| 2533 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2534 | if (!e) |
| 2535 | return NULL; |
| 2536 | if (NCH(n) == 1) |
| 2537 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2538 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2539 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2540 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2541 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2542 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2543 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2544 | } |
| 2545 | return e; |
| 2546 | } |
| 2547 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2548 | static expr_ty |
| 2549 | ast_for_starred(struct compiling *c, const node *n) |
| 2550 | { |
| 2551 | expr_ty tmp; |
| 2552 | REQ(n, star_expr); |
| 2553 | |
| 2554 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2555 | if (!tmp) |
| 2556 | return NULL; |
| 2557 | |
| 2558 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2559 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2560 | 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] | 2561 | } |
| 2562 | |
| 2563 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2564 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2565 | */ |
| 2566 | |
| 2567 | static expr_ty |
| 2568 | ast_for_expr(struct compiling *c, const node *n) |
| 2569 | { |
| 2570 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2571 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2572 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2573 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2574 | and_test: not_test ('and' not_test)* |
| 2575 | not_test: 'not' not_test | comparison |
| 2576 | comparison: expr (comp_op expr)* |
| 2577 | expr: xor_expr ('|' xor_expr)* |
| 2578 | xor_expr: and_expr ('^' and_expr)* |
| 2579 | and_expr: shift_expr ('&' shift_expr)* |
| 2580 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2581 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2582 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2583 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2584 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2585 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2586 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2587 | */ |
| 2588 | |
| 2589 | asdl_seq *seq; |
| 2590 | int i; |
| 2591 | |
| 2592 | loop: |
| 2593 | switch (TYPE(n)) { |
| 2594 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2595 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2596 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2597 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2598 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2599 | else if (NCH(n) > 1) |
| 2600 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2601 | /* Fallthrough */ |
| 2602 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2603 | case and_test: |
| 2604 | if (NCH(n) == 1) { |
| 2605 | n = CHILD(n, 0); |
| 2606 | goto loop; |
| 2607 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2608 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2609 | if (!seq) |
| 2610 | return NULL; |
| 2611 | for (i = 0; i < NCH(n); i += 2) { |
| 2612 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2613 | if (!e) |
| 2614 | return NULL; |
| 2615 | asdl_seq_SET(seq, i / 2, e); |
| 2616 | } |
| 2617 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2618 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2619 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2620 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2621 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2622 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2623 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2624 | case not_test: |
| 2625 | if (NCH(n) == 1) { |
| 2626 | n = CHILD(n, 0); |
| 2627 | goto loop; |
| 2628 | } |
| 2629 | else { |
| 2630 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2631 | if (!expression) |
| 2632 | return NULL; |
| 2633 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2634 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2635 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2636 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2637 | } |
| 2638 | case comparison: |
| 2639 | if (NCH(n) == 1) { |
| 2640 | n = CHILD(n, 0); |
| 2641 | goto loop; |
| 2642 | } |
| 2643 | else { |
| 2644 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2645 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2646 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2647 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2648 | if (!ops) |
| 2649 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2650 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2651 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2652 | return NULL; |
| 2653 | } |
| 2654 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2655 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2656 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2657 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2658 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2659 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2660 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2661 | |
| 2662 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2663 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2664 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2665 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2667 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2668 | asdl_seq_SET(cmps, i / 2, expression); |
| 2669 | } |
| 2670 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2671 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2672 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2673 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2675 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2676 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2677 | } |
| 2678 | break; |
| 2679 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2680 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2682 | /* The next five cases all handle BinOps. The main body of code |
| 2683 | is the same in each case, but the switch turned inside out to |
| 2684 | reuse the code for each type of operator. |
| 2685 | */ |
| 2686 | case expr: |
| 2687 | case xor_expr: |
| 2688 | case and_expr: |
| 2689 | case shift_expr: |
| 2690 | case arith_expr: |
| 2691 | case term: |
| 2692 | if (NCH(n) == 1) { |
| 2693 | n = CHILD(n, 0); |
| 2694 | goto loop; |
| 2695 | } |
| 2696 | return ast_for_binop(c, n); |
| 2697 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2698 | node *an = NULL; |
| 2699 | node *en = NULL; |
| 2700 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2701 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2702 | if (NCH(n) > 1) |
| 2703 | an = CHILD(n, 1); /* yield_arg */ |
| 2704 | if (an) { |
| 2705 | en = CHILD(an, NCH(an) - 1); |
| 2706 | if (NCH(an) == 2) { |
| 2707 | is_from = 1; |
| 2708 | exp = ast_for_expr(c, en); |
| 2709 | } |
| 2710 | else |
| 2711 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2712 | if (!exp) |
| 2713 | return NULL; |
| 2714 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2715 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2716 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2717 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2718 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2719 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2720 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2721 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2722 | if (NCH(n) == 1) { |
| 2723 | n = CHILD(n, 0); |
| 2724 | goto loop; |
| 2725 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2726 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2727 | case power: |
| 2728 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2729 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2730 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2731 | return NULL; |
| 2732 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2733 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2734 | return NULL; |
| 2735 | } |
| 2736 | |
| 2737 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2738 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2739 | const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2740 | { |
| 2741 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2742 | arglist: argument (',' argument)* [','] |
| 2743 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2744 | */ |
| 2745 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2746 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2747 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2748 | asdl_seq *args; |
| 2749 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2750 | |
| 2751 | REQ(n, arglist); |
| 2752 | |
| 2753 | nargs = 0; |
| 2754 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2755 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2756 | node *ch = CHILD(n, i); |
| 2757 | if (TYPE(ch) == argument) { |
| 2758 | if (NCH(ch) == 1) |
| 2759 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2760 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2761 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2762 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2763 | ast_error(c, ch, "invalid syntax"); |
| 2764 | return NULL; |
| 2765 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2766 | if (NCH(n) > 1) { |
| 2767 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2768 | return NULL; |
| 2769 | } |
| 2770 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2771 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2772 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2773 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2774 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2775 | nkeywords++; |
| 2776 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2777 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2778 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2779 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2780 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2781 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2782 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2783 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2784 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2785 | |
| 2786 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2787 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2788 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2789 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2790 | node *ch = CHILD(n, i); |
| 2791 | if (TYPE(ch) == argument) { |
| 2792 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2793 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2794 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2795 | /* a positional argument */ |
| 2796 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2797 | if (ndoublestars) { |
| 2798 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2799 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2800 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2801 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2802 | else { |
| 2803 | ast_error(c, chch, |
| 2804 | "positional argument follows " |
| 2805 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2806 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2807 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2808 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2809 | e = ast_for_expr(c, chch); |
| 2810 | if (!e) |
| 2811 | return NULL; |
| 2812 | asdl_seq_SET(args, nargs++, e); |
| 2813 | } |
| 2814 | else if (TYPE(chch) == STAR) { |
| 2815 | /* an iterable argument unpacking */ |
| 2816 | expr_ty starred; |
| 2817 | if (ndoublestars) { |
| 2818 | ast_error(c, chch, |
| 2819 | "iterable argument unpacking follows " |
| 2820 | "keyword argument unpacking"); |
| 2821 | return NULL; |
| 2822 | } |
| 2823 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2824 | if (!e) |
| 2825 | return NULL; |
| 2826 | starred = Starred(e, Load, LINENO(chch), |
| 2827 | chch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2828 | chch->n_end_lineno, chch->n_end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2829 | c->c_arena); |
| 2830 | if (!starred) |
| 2831 | return NULL; |
| 2832 | asdl_seq_SET(args, nargs++, starred); |
| 2833 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2834 | } |
| 2835 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2836 | /* a keyword argument unpacking */ |
| 2837 | keyword_ty kw; |
| 2838 | i++; |
| 2839 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2840 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2841 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2842 | kw = keyword(NULL, e, c->c_arena); |
| 2843 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2844 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2846 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2847 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2848 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2849 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2850 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2851 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2852 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2853 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2854 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2855 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2856 | identifier key, tmp; |
| 2857 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2858 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2859 | // To remain LL(1), the grammar accepts any test (basically, any |
| 2860 | // expression) in the keyword slot of a call site. So, we need |
| 2861 | // to manually enforce that the keyword is a NAME here. |
| 2862 | static const int name_tree[] = { |
| 2863 | test, |
| 2864 | or_test, |
| 2865 | and_test, |
| 2866 | not_test, |
| 2867 | comparison, |
| 2868 | expr, |
| 2869 | xor_expr, |
| 2870 | and_expr, |
| 2871 | shift_expr, |
| 2872 | arith_expr, |
| 2873 | term, |
| 2874 | factor, |
| 2875 | power, |
| 2876 | atom_expr, |
| 2877 | atom, |
| 2878 | 0, |
| 2879 | }; |
| 2880 | node *expr_node = chch; |
| 2881 | for (int i = 0; name_tree[i]; i++) { |
| 2882 | if (TYPE(expr_node) != name_tree[i]) |
| 2883 | break; |
| 2884 | if (NCH(expr_node) != 1) |
| 2885 | break; |
| 2886 | expr_node = CHILD(expr_node, 0); |
| 2887 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2888 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2889 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2890 | "expression cannot contain assignment, " |
| 2891 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2892 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2893 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2894 | key = new_identifier(STR(expr_node), c); |
| 2895 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2896 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2898 | if (forbidden_name(c, key, chch, 1)) { |
| 2899 | return NULL; |
| 2900 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2901 | for (k = 0; k < nkeywords; k++) { |
| 2902 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2903 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2904 | ast_error(c, chch, |
| 2905 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2906 | return NULL; |
| 2907 | } |
| 2908 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2909 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2910 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2911 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2912 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2913 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2914 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2915 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2916 | } |
| 2917 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2920 | return Call(func, args, keywords, func->lineno, func->col_offset, |
| 2921 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2924 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2925 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2926 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2927 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2928 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2929 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2930 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2931 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2932 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2933 | } |
| 2934 | else { |
| 2935 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2936 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2937 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2939 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2940 | else { |
| 2941 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2942 | if (!tmp) |
| 2943 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2944 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 2945 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2946 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2949 | static stmt_ty |
| 2950 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2951 | { |
| 2952 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2953 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2954 | ('=' (yield_expr|testlist_star_expr))*) |
| 2955 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2956 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2957 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2958 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2959 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2960 | */ |
| 2961 | |
| 2962 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2963 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2964 | if (!e) |
| 2965 | return NULL; |
| 2966 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2967 | return Expr(e, LINENO(n), n->n_col_offset, |
| 2968 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2969 | } |
| 2970 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2971 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2972 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2973 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2975 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2976 | if (!expr1) |
| 2977 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2978 | if(!set_context(c, expr1, Store, ch)) |
| 2979 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2980 | /* set_context checks that most expressions are not the left side. |
| 2981 | Augmented assignments can only have a name, a subscript, or an |
| 2982 | attribute on the left, though, so we have to explicitly check for |
| 2983 | those. */ |
| 2984 | switch (expr1->kind) { |
| 2985 | case Name_kind: |
| 2986 | case Attribute_kind: |
| 2987 | case Subscript_kind: |
| 2988 | break; |
| 2989 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2990 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2991 | return NULL; |
| 2992 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2993 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2994 | ch = CHILD(n, 2); |
| 2995 | if (TYPE(ch) == testlist) |
| 2996 | expr2 = ast_for_testlist(c, ch); |
| 2997 | else |
| 2998 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2999 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 | return NULL; |
| 3001 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3002 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3003 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3004 | return NULL; |
| 3005 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3006 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3007 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3008 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3009 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3010 | expr_ty expr1, expr2, expr3; |
| 3011 | node *ch = CHILD(n, 0); |
| 3012 | node *deep, *ann = CHILD(n, 1); |
| 3013 | int simple = 1; |
| 3014 | |
| 3015 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3016 | deep = ch; |
| 3017 | while (NCH(deep) == 1) { |
| 3018 | deep = CHILD(deep, 0); |
| 3019 | } |
| 3020 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3021 | simple = 0; |
| 3022 | } |
| 3023 | expr1 = ast_for_testlist(c, ch); |
| 3024 | if (!expr1) { |
| 3025 | return NULL; |
| 3026 | } |
| 3027 | switch (expr1->kind) { |
| 3028 | case Name_kind: |
| 3029 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | expr1->v.Name.ctx = Store; |
| 3033 | break; |
| 3034 | case Attribute_kind: |
| 3035 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3036 | return NULL; |
| 3037 | } |
| 3038 | expr1->v.Attribute.ctx = Store; |
| 3039 | break; |
| 3040 | case Subscript_kind: |
| 3041 | expr1->v.Subscript.ctx = Store; |
| 3042 | break; |
| 3043 | case List_kind: |
| 3044 | ast_error(c, ch, |
| 3045 | "only single target (not list) can be annotated"); |
| 3046 | return NULL; |
| 3047 | case Tuple_kind: |
| 3048 | ast_error(c, ch, |
| 3049 | "only single target (not tuple) can be annotated"); |
| 3050 | return NULL; |
| 3051 | default: |
| 3052 | ast_error(c, ch, |
| 3053 | "illegal target for annotation"); |
| 3054 | return NULL; |
| 3055 | } |
| 3056 | |
| 3057 | if (expr1->kind != Name_kind) { |
| 3058 | simple = 0; |
| 3059 | } |
| 3060 | ch = CHILD(ann, 1); |
| 3061 | expr2 = ast_for_expr(c, ch); |
| 3062 | if (!expr2) { |
| 3063 | return NULL; |
| 3064 | } |
| 3065 | if (NCH(ann) == 2) { |
| 3066 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3067 | LINENO(n), n->n_col_offset, |
| 3068 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3069 | } |
| 3070 | else { |
| 3071 | ch = CHILD(ann, 3); |
| 3072 | expr3 = ast_for_expr(c, ch); |
| 3073 | if (!expr3) { |
| 3074 | return NULL; |
| 3075 | } |
| 3076 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3077 | LINENO(n), n->n_col_offset, |
| 3078 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3079 | } |
| 3080 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3081 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3082 | int i; |
| 3083 | asdl_seq *targets; |
| 3084 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3085 | expr_ty expression; |
| 3086 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3087 | /* a normal assignment */ |
| 3088 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3089 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3090 | if (!targets) |
| 3091 | return NULL; |
| 3092 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3093 | expr_ty e; |
| 3094 | node *ch = CHILD(n, i); |
| 3095 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3096 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3097 | return NULL; |
| 3098 | } |
| 3099 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3101 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3102 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3103 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3104 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3105 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3106 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3107 | asdl_seq_SET(targets, i / 2, e); |
| 3108 | } |
| 3109 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3110 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3111 | expression = ast_for_testlist(c, value); |
| 3112 | else |
| 3113 | expression = ast_for_expr(c, value); |
| 3114 | if (!expression) |
| 3115 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3116 | return Assign(targets, expression, LINENO(n), n->n_col_offset, |
| 3117 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3118 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3121 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3122 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3123 | 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] | 3124 | { |
| 3125 | asdl_seq *seq; |
| 3126 | int i; |
| 3127 | expr_ty e; |
| 3128 | |
| 3129 | REQ(n, exprlist); |
| 3130 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3131 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3132 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3133 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3134 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3135 | e = ast_for_expr(c, CHILD(n, i)); |
| 3136 | if (!e) |
| 3137 | return NULL; |
| 3138 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3139 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3140 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3141 | } |
| 3142 | return seq; |
| 3143 | } |
| 3144 | |
| 3145 | static stmt_ty |
| 3146 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3147 | { |
| 3148 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3150 | /* del_stmt: 'del' exprlist */ |
| 3151 | REQ(n, del_stmt); |
| 3152 | |
| 3153 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3154 | if (!expr_list) |
| 3155 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3156 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3157 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | static stmt_ty |
| 3161 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3162 | { |
| 3163 | /* |
| 3164 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3165 | | yield_stmt |
| 3166 | break_stmt: 'break' |
| 3167 | continue_stmt: 'continue' |
| 3168 | return_stmt: 'return' [testlist] |
| 3169 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3170 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3171 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3172 | */ |
| 3173 | node *ch; |
| 3174 | |
| 3175 | REQ(n, flow_stmt); |
| 3176 | ch = CHILD(n, 0); |
| 3177 | switch (TYPE(ch)) { |
| 3178 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3179 | return Break(LINENO(n), n->n_col_offset, |
| 3180 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3182 | return Continue(LINENO(n), n->n_col_offset, |
| 3183 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3184 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3185 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3186 | if (!exp) |
| 3187 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3188 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3189 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3190 | } |
| 3191 | case return_stmt: |
| 3192 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3193 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3194 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3195 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3196 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3197 | if (!expression) |
| 3198 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3199 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3200 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | } |
| 3202 | case raise_stmt: |
| 3203 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3204 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3205 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3206 | else if (NCH(ch) >= 2) { |
| 3207 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3208 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3209 | if (!expression) |
| 3210 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3211 | if (NCH(ch) == 4) { |
| 3212 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3213 | if (!cause) |
| 3214 | return NULL; |
| 3215 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3216 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3217 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3218 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3219 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3220 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3221 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3222 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3223 | return NULL; |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3228 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3229 | { |
| 3230 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3231 | import_as_name: NAME ['as' NAME] |
| 3232 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3233 | dotted_name: NAME ('.' NAME)* |
| 3234 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3235 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3236 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3237 | loop: |
| 3238 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3239 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3240 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3241 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3242 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3243 | if (!name) |
| 3244 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3245 | if (NCH(n) == 3) { |
| 3246 | node *str_node = CHILD(n, 2); |
| 3247 | str = NEW_IDENTIFIER(str_node); |
| 3248 | if (!str) |
| 3249 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3250 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3251 | return NULL; |
| 3252 | } |
| 3253 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3254 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3255 | return NULL; |
| 3256 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3257 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3258 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3259 | case dotted_as_name: |
| 3260 | if (NCH(n) == 1) { |
| 3261 | n = CHILD(n, 0); |
| 3262 | goto loop; |
| 3263 | } |
| 3264 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3265 | node *asname_node = CHILD(n, 2); |
| 3266 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3267 | if (!a) |
| 3268 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3269 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3270 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3271 | if (!a->asname) |
| 3272 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3273 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3274 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3275 | return a; |
| 3276 | } |
| 3277 | break; |
| 3278 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3279 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3280 | node *name_node = CHILD(n, 0); |
| 3281 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3282 | if (!name) |
| 3283 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3284 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3285 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3286 | return alias(name, NULL, c->c_arena); |
| 3287 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | else { |
| 3289 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3290 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3291 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3292 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3294 | |
| 3295 | len = 0; |
| 3296 | for (i = 0; i < NCH(n); i += 2) |
| 3297 | /* length of string plus one for the dot */ |
| 3298 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3299 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3300 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3301 | if (!str) |
| 3302 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3303 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3304 | if (!s) |
| 3305 | return NULL; |
| 3306 | for (i = 0; i < NCH(n); i += 2) { |
| 3307 | char *sch = STR(CHILD(n, i)); |
| 3308 | strcpy(s, STR(CHILD(n, i))); |
| 3309 | s += strlen(sch); |
| 3310 | *s++ = '.'; |
| 3311 | } |
| 3312 | --s; |
| 3313 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3315 | PyBytes_GET_SIZE(str), |
| 3316 | NULL); |
| 3317 | Py_DECREF(str); |
| 3318 | if (!uni) |
| 3319 | return NULL; |
| 3320 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3321 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3322 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3323 | Py_DECREF(str); |
| 3324 | return NULL; |
| 3325 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3326 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3327 | } |
| 3328 | break; |
| 3329 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3330 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3331 | if (!str) |
| 3332 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3333 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3334 | Py_DECREF(str); |
| 3335 | return NULL; |
| 3336 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3337 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3338 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3339 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3340 | "unexpected import name: %d", TYPE(n)); |
| 3341 | return NULL; |
| 3342 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3343 | |
| 3344 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3345 | return NULL; |
| 3346 | } |
| 3347 | |
| 3348 | static stmt_ty |
| 3349 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3350 | { |
| 3351 | /* |
| 3352 | import_stmt: import_name | import_from |
| 3353 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3354 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3355 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3357 | int lineno; |
| 3358 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3359 | int i; |
| 3360 | asdl_seq *aliases; |
| 3361 | |
| 3362 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3363 | lineno = LINENO(n); |
| 3364 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3365 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3366 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3367 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3368 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3369 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3370 | if (!aliases) |
| 3371 | return NULL; |
| 3372 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3373 | 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] | 3374 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3375 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3376 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3377 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3378 | // Even though n is modified above, the end position is not changed |
| 3379 | return Import(aliases, lineno, col_offset, |
| 3380 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3381 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3382 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3383 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3384 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3385 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3386 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3387 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3389 | /* Count the number of dots (for relative imports) and check for the |
| 3390 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3391 | for (idx = 1; idx < NCH(n); idx++) { |
| 3392 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3393 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3394 | if (!mod) |
| 3395 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3396 | idx++; |
| 3397 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3398 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3400 | ndots += 3; |
| 3401 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3402 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3403 | break; |
| 3404 | } |
| 3405 | ndots++; |
| 3406 | } |
| 3407 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3408 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3409 | case STAR: |
| 3410 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3411 | n = CHILD(n, idx); |
| 3412 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3413 | break; |
| 3414 | case LPAR: |
| 3415 | /* from ... import (x, y, z) */ |
| 3416 | n = CHILD(n, idx + 1); |
| 3417 | n_children = NCH(n); |
| 3418 | break; |
| 3419 | case import_as_names: |
| 3420 | /* from ... import x, y, z */ |
| 3421 | n = CHILD(n, idx); |
| 3422 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3423 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3424 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3425 | " surrounding parentheses"); |
| 3426 | return NULL; |
| 3427 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3428 | break; |
| 3429 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3430 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3431 | return NULL; |
| 3432 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3433 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3434 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3435 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3437 | |
| 3438 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3439 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3440 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3441 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3442 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3443 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3444 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3445 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3446 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3447 | 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] | 3448 | if (!import_alias) |
| 3449 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3450 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3451 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3452 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3453 | if (mod != NULL) |
| 3454 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3455 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3456 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3457 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3459 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3460 | "unknown import statement: starts with command '%s'", |
| 3461 | STR(CHILD(n, 0))); |
| 3462 | return NULL; |
| 3463 | } |
| 3464 | |
| 3465 | static stmt_ty |
| 3466 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3467 | { |
| 3468 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3469 | identifier name; |
| 3470 | asdl_seq *s; |
| 3471 | int i; |
| 3472 | |
| 3473 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3474 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3476 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3478 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3479 | if (!name) |
| 3480 | return NULL; |
| 3481 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3482 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3483 | return Global(s, LINENO(n), n->n_col_offset, |
| 3484 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3488 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3489 | { |
| 3490 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3491 | identifier name; |
| 3492 | asdl_seq *s; |
| 3493 | int i; |
| 3494 | |
| 3495 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3496 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3497 | if (!s) |
| 3498 | return NULL; |
| 3499 | for (i = 1; i < NCH(n); i += 2) { |
| 3500 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3501 | if (!name) |
| 3502 | return NULL; |
| 3503 | asdl_seq_SET(s, i / 2, name); |
| 3504 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3505 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3506 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3510 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3511 | { |
| 3512 | /* assert_stmt: 'assert' test [',' test] */ |
| 3513 | REQ(n, assert_stmt); |
| 3514 | if (NCH(n) == 2) { |
| 3515 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3516 | if (!expression) |
| 3517 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3518 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3519 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3520 | } |
| 3521 | else if (NCH(n) == 4) { |
| 3522 | expr_ty expr1, expr2; |
| 3523 | |
| 3524 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3525 | if (!expr1) |
| 3526 | return NULL; |
| 3527 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3528 | if (!expr2) |
| 3529 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3531 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3532 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3533 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3534 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3535 | "improper number of parts to 'assert' statement: %d", |
| 3536 | NCH(n)); |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | |
| 3540 | static asdl_seq * |
| 3541 | ast_for_suite(struct compiling *c, const node *n) |
| 3542 | { |
| 3543 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3544 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | stmt_ty s; |
| 3546 | int i, total, num, end, pos = 0; |
| 3547 | node *ch; |
| 3548 | |
| 3549 | REQ(n, suite); |
| 3550 | |
| 3551 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3552 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3553 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3554 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3555 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3556 | n = CHILD(n, 0); |
| 3557 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3558 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3559 | */ |
| 3560 | end = NCH(n) - 1; |
| 3561 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3562 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3563 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3564 | for (i = 0; i < end; i += 2) { |
| 3565 | ch = CHILD(n, i); |
| 3566 | s = ast_for_stmt(c, ch); |
| 3567 | if (!s) |
| 3568 | return NULL; |
| 3569 | asdl_seq_SET(seq, pos++, s); |
| 3570 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3571 | } |
| 3572 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3573 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3574 | ch = CHILD(n, i); |
| 3575 | REQ(ch, stmt); |
| 3576 | num = num_stmts(ch); |
| 3577 | if (num == 1) { |
| 3578 | /* small_stmt or compound_stmt with only one child */ |
| 3579 | s = ast_for_stmt(c, ch); |
| 3580 | if (!s) |
| 3581 | return NULL; |
| 3582 | asdl_seq_SET(seq, pos++, s); |
| 3583 | } |
| 3584 | else { |
| 3585 | int j; |
| 3586 | ch = CHILD(ch, 0); |
| 3587 | REQ(ch, simple_stmt); |
| 3588 | for (j = 0; j < NCH(ch); j += 2) { |
| 3589 | /* statement terminates with a semi-colon ';' */ |
| 3590 | if (NCH(CHILD(ch, j)) == 0) { |
| 3591 | assert((j + 1) == NCH(ch)); |
| 3592 | break; |
| 3593 | } |
| 3594 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3595 | if (!s) |
| 3596 | return NULL; |
| 3597 | asdl_seq_SET(seq, pos++, s); |
| 3598 | } |
| 3599 | } |
| 3600 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3601 | } |
| 3602 | assert(pos == seq->size); |
| 3603 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3606 | static void |
| 3607 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3608 | { |
| 3609 | int tot = asdl_seq_LEN(s); |
| 3610 | // Suite should not be empty, but it is safe to just ignore it |
| 3611 | // if it will ever occur. |
| 3612 | if (!tot) { |
| 3613 | return; |
| 3614 | } |
| 3615 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3616 | *end_lineno = last->end_lineno; |
| 3617 | *end_col_offset = last->end_col_offset; |
| 3618 | } |
| 3619 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3620 | static stmt_ty |
| 3621 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3622 | { |
| 3623 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3624 | ['else' ':' suite] |
| 3625 | */ |
| 3626 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3627 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3628 | |
| 3629 | REQ(n, if_stmt); |
| 3630 | |
| 3631 | if (NCH(n) == 4) { |
| 3632 | expr_ty expression; |
| 3633 | asdl_seq *suite_seq; |
| 3634 | |
| 3635 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3636 | if (!expression) |
| 3637 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3639 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3640 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3641 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3643 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3644 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3646 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3647 | s = STR(CHILD(n, 4)); |
| 3648 | /* s[2], the third character in the string, will be |
| 3649 | 's' for el_s_e, or |
| 3650 | 'i' for el_i_f |
| 3651 | */ |
| 3652 | if (s[2] == 's') { |
| 3653 | expr_ty expression; |
| 3654 | asdl_seq *seq1, *seq2; |
| 3655 | |
| 3656 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3657 | if (!expression) |
| 3658 | return NULL; |
| 3659 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3660 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3661 | return NULL; |
| 3662 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3663 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3664 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3665 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3666 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3667 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3668 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3669 | } |
| 3670 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3671 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3672 | expr_ty expression; |
| 3673 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3674 | asdl_seq *orelse = NULL; |
| 3675 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3676 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3677 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3678 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3679 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3680 | has_else = 1; |
| 3681 | n_elif -= 3; |
| 3682 | } |
| 3683 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3684 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3685 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3686 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3688 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3689 | if (!orelse) |
| 3690 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3691 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3692 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3693 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3694 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3695 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3696 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3697 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3698 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3699 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3700 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3702 | asdl_seq_SET(orelse, 0, |
| 3703 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3704 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3705 | CHILD(n, NCH(n) - 6)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3706 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3707 | /* the just-created orelse handled the last elif */ |
| 3708 | n_elif--; |
| 3709 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3710 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3711 | for (i = 0; i < n_elif; i++) { |
| 3712 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3713 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3714 | if (!newobj) |
| 3715 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3717 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3718 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3720 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3721 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3722 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3723 | if (orelse != NULL) { |
| 3724 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3725 | } else { |
| 3726 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3727 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3728 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3730 | LINENO(CHILD(n, off)), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3731 | CHILD(n, off)->n_col_offset, |
| 3732 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3733 | orelse = newobj; |
| 3734 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3735 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3736 | if (!expression) |
| 3737 | return NULL; |
| 3738 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3739 | if (!suite_seq) |
| 3740 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3741 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3742 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3743 | LINENO(n), n->n_col_offset, |
| 3744 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3745 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3746 | |
| 3747 | PyErr_Format(PyExc_SystemError, |
| 3748 | "unexpected token in 'if' statement: %s", s); |
| 3749 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | static stmt_ty |
| 3753 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3754 | { |
| 3755 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3756 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3757 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3758 | |
| 3759 | if (NCH(n) == 4) { |
| 3760 | expr_ty expression; |
| 3761 | asdl_seq *suite_seq; |
| 3762 | |
| 3763 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3764 | if (!expression) |
| 3765 | return NULL; |
| 3766 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3767 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3768 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3769 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3770 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3771 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3772 | } |
| 3773 | else if (NCH(n) == 7) { |
| 3774 | expr_ty expression; |
| 3775 | asdl_seq *seq1, *seq2; |
| 3776 | |
| 3777 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3778 | if (!expression) |
| 3779 | return NULL; |
| 3780 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3781 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3782 | return NULL; |
| 3783 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3784 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3785 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3786 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3787 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3788 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3789 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3790 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3791 | |
| 3792 | PyErr_Format(PyExc_SystemError, |
| 3793 | "wrong number of tokens for 'while' statement: %d", |
| 3794 | NCH(n)); |
| 3795 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3799 | 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] | 3800 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3801 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3802 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3803 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3804 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3805 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3806 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3807 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3808 | REQ(n, for_stmt); |
| 3809 | |
| 3810 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3811 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3812 | if (!seq) |
| 3813 | return NULL; |
| 3814 | } |
| 3815 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3816 | node_target = CHILD(n, 1); |
| 3817 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3818 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3819 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3820 | /* Check the # of children rather than the length of _target, since |
| 3821 | 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] | 3822 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3823 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3824 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3825 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3826 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 3827 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 3828 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3829 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3830 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3831 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3832 | return NULL; |
| 3833 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3834 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3835 | return NULL; |
| 3836 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3837 | if (seq != NULL) { |
| 3838 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 3839 | } else { |
| 3840 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3841 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3842 | if (is_async) |
| 3843 | return AsyncFor(target, expression, suite_seq, seq, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3844 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3845 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3846 | else |
| 3847 | return For(target, expression, suite_seq, seq, |
| 3848 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3849 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | static excepthandler_ty |
| 3853 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3854 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3855 | /* except_clause: 'except' [test ['as' test]] */ |
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 | REQ(exc, except_clause); |
| 3858 | REQ(body, suite); |
| 3859 | |
| 3860 | if (NCH(exc) == 1) { |
| 3861 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3862 | if (!suite_seq) |
| 3863 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3864 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3865 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3866 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3867 | exc->n_col_offset, |
| 3868 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3869 | } |
| 3870 | else if (NCH(exc) == 2) { |
| 3871 | expr_ty expression; |
| 3872 | asdl_seq *suite_seq; |
| 3873 | |
| 3874 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3875 | if (!expression) |
| 3876 | return NULL; |
| 3877 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3878 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3879 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3880 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3881 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3882 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3883 | exc->n_col_offset, |
| 3884 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3885 | } |
| 3886 | else if (NCH(exc) == 4) { |
| 3887 | asdl_seq *suite_seq; |
| 3888 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3889 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3890 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3891 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3892 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3893 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3894 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3895 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3896 | return NULL; |
| 3897 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3898 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3899 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3900 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3901 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3902 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3903 | exc->n_col_offset, |
| 3904 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3905 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3906 | |
| 3907 | PyErr_Format(PyExc_SystemError, |
| 3908 | "wrong number of children for 'except' clause: %d", |
| 3909 | NCH(exc)); |
| 3910 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | static stmt_ty |
| 3914 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3915 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3916 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3917 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3918 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3919 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3920 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3921 | REQ(n, try_stmt); |
| 3922 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3923 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3924 | if (body == NULL) |
| 3925 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3926 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3927 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3928 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3929 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3930 | /* we can assume it's an "else", |
| 3931 | because nch >= 9 for try-else-finally and |
| 3932 | it would otherwise have a type of except_clause */ |
| 3933 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3934 | if (orelse == NULL) |
| 3935 | return NULL; |
| 3936 | n_except--; |
| 3937 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3938 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3939 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3940 | if (finally == NULL) |
| 3941 | return NULL; |
| 3942 | n_except--; |
| 3943 | } |
| 3944 | else { |
| 3945 | /* we can assume it's an "else", |
| 3946 | otherwise it would have a type of except_clause */ |
| 3947 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3948 | if (orelse == NULL) |
| 3949 | return NULL; |
| 3950 | n_except--; |
| 3951 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3952 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3953 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3954 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3955 | return NULL; |
| 3956 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3958 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3959 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3960 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3961 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3962 | if (handlers == NULL) |
| 3963 | return NULL; |
| 3964 | |
| 3965 | for (i = 0; i < n_except; i++) { |
| 3966 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3967 | CHILD(n, 5 + i * 3)); |
| 3968 | if (!e) |
| 3969 | return NULL; |
| 3970 | asdl_seq_SET(handlers, i, e); |
| 3971 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3974 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3975 | if (finally != NULL) { |
| 3976 | // finally is always last |
| 3977 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 3978 | } else if (orelse != NULL) { |
| 3979 | // otherwise else is last |
| 3980 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3981 | } else { |
| 3982 | // inline the get_last_end_pos logic due to layout mismatch |
| 3983 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 3984 | end_lineno = last_handler->end_lineno; |
| 3985 | end_col_offset = last_handler->end_col_offset; |
| 3986 | } |
| 3987 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 3988 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3991 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3992 | static withitem_ty |
| 3993 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3994 | { |
| 3995 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3996 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3997 | REQ(n, with_item); |
| 3998 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3999 | if (!context_expr) |
| 4000 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4001 | if (NCH(n) == 3) { |
| 4002 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4003 | |
| 4004 | if (!optional_vars) { |
| 4005 | return NULL; |
| 4006 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4007 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4008 | return NULL; |
| 4009 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4012 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4015 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 4016 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4017 | 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] | 4018 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4019 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4020 | int i, n_items, end_lineno, end_col_offset; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4021 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4022 | |
| 4023 | REQ(n, with_stmt); |
| 4024 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4025 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4026 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4027 | if (!items) |
| 4028 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4029 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 4030 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4031 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4032 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4033 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4036 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4037 | if (!body) |
| 4038 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4039 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4040 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4041 | if (is_async) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4042 | return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, |
| 4043 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4044 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4045 | return With(items, body, LINENO(n), n->n_col_offset, |
| 4046 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4049 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4050 | 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] | 4051 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4052 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4053 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4054 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4055 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4056 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4057 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4058 | REQ(n, classdef); |
| 4059 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4060 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4061 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4062 | if (!s) |
| 4063 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4064 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4065 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4066 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4067 | if (!classname) |
| 4068 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4069 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4070 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4071 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4072 | LINENO(n), n->n_col_offset, |
| 4073 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4074 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4075 | |
| 4076 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4077 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4078 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4079 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4080 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4081 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4082 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4083 | if (!classname) |
| 4084 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4085 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4086 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4087 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4088 | LINENO(n), n->n_col_offset, |
| 4089 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4092 | /* class NAME '(' arglist ')' ':' suite */ |
| 4093 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4094 | { |
| 4095 | PyObject *dummy_name; |
| 4096 | expr_ty dummy; |
| 4097 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4098 | if (!dummy_name) |
| 4099 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4100 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4101 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4102 | c->c_arena); |
| 4103 | 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] | 4104 | if (!call) |
| 4105 | return NULL; |
| 4106 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4107 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4108 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4109 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4110 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4111 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4112 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4113 | if (!classname) |
| 4114 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4115 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4116 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4117 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4118 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4119 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4120 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | static stmt_ty |
| 4124 | ast_for_stmt(struct compiling *c, const node *n) |
| 4125 | { |
| 4126 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4127 | assert(NCH(n) == 1); |
| 4128 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4129 | } |
| 4130 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4131 | assert(num_stmts(n) == 1); |
| 4132 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4133 | } |
| 4134 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4135 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4136 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4137 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4138 | */ |
| 4139 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4140 | case expr_stmt: |
| 4141 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4142 | case del_stmt: |
| 4143 | return ast_for_del_stmt(c, n); |
| 4144 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4145 | return Pass(LINENO(n), n->n_col_offset, |
| 4146 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4147 | case flow_stmt: |
| 4148 | return ast_for_flow_stmt(c, n); |
| 4149 | case import_stmt: |
| 4150 | return ast_for_import_stmt(c, n); |
| 4151 | case global_stmt: |
| 4152 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4153 | case nonlocal_stmt: |
| 4154 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4155 | case assert_stmt: |
| 4156 | return ast_for_assert_stmt(c, n); |
| 4157 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4158 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4159 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4160 | TYPE(n), NCH(n)); |
| 4161 | return NULL; |
| 4162 | } |
| 4163 | } |
| 4164 | else { |
| 4165 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4166 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4167 | */ |
| 4168 | node *ch = CHILD(n, 0); |
| 4169 | REQ(n, compound_stmt); |
| 4170 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4171 | case if_stmt: |
| 4172 | return ast_for_if_stmt(c, ch); |
| 4173 | case while_stmt: |
| 4174 | return ast_for_while_stmt(c, ch); |
| 4175 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4176 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4177 | case try_stmt: |
| 4178 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4179 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4180 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4181 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4182 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4183 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4184 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | case decorated: |
| 4186 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4187 | case async_stmt: |
| 4188 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4189 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4190 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4191 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4192 | TYPE(n), NCH(n)); |
| 4193 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4194 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | } |
| 4196 | } |
| 4197 | |
| 4198 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4199 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4200 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4201 | const char *end; |
| 4202 | long x; |
| 4203 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4204 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4205 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4206 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4207 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4208 | errno = 0; |
| 4209 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4210 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4211 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4212 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4213 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4214 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4215 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4216 | } |
| 4217 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4218 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4219 | if (*end == '\0') { |
| 4220 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4221 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4222 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4223 | } |
| 4224 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4225 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4226 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4227 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4228 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4229 | return NULL; |
| 4230 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4231 | } |
| 4232 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4233 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4234 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4235 | if (dx == -1.0 && PyErr_Occurred()) |
| 4236 | return NULL; |
| 4237 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4238 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4242 | parsenumber(struct compiling *c, const char *s) |
| 4243 | { |
| 4244 | char *dup, *end; |
| 4245 | PyObject *res = NULL; |
| 4246 | |
| 4247 | assert(s != NULL); |
| 4248 | |
| 4249 | if (strchr(s, '_') == NULL) { |
| 4250 | return parsenumber_raw(c, s); |
| 4251 | } |
| 4252 | /* Create a duplicate without underscores. */ |
| 4253 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4254 | if (dup == NULL) { |
| 4255 | return PyErr_NoMemory(); |
| 4256 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4257 | end = dup; |
| 4258 | for (; *s; s++) { |
| 4259 | if (*s != '_') { |
| 4260 | *end++ = *s; |
| 4261 | } |
| 4262 | } |
| 4263 | *end = '\0'; |
| 4264 | res = parsenumber_raw(c, dup); |
| 4265 | PyMem_Free(dup); |
| 4266 | return res; |
| 4267 | } |
| 4268 | |
| 4269 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4270 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4271 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4272 | const char *s, *t; |
| 4273 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4274 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4275 | while (s < end && (*s & 0x80)) s++; |
| 4276 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4277 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4280 | static int |
| 4281 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4282 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4283 | { |
| 4284 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4285 | first_invalid_escape_char); |
| 4286 | if (msg == NULL) { |
| 4287 | return -1; |
| 4288 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4289 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4290 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4291 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4292 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4293 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4294 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4295 | to get a more accurate error report */ |
| 4296 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4297 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4298 | } |
| 4299 | Py_DECREF(msg); |
| 4300 | return -1; |
| 4301 | } |
| 4302 | Py_DECREF(msg); |
| 4303 | return 0; |
| 4304 | } |
| 4305 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4306 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4307 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4308 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4309 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4310 | PyObject *v, *u; |
| 4311 | char *buf; |
| 4312 | char *p; |
| 4313 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4314 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4315 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4316 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4317 | return NULL; |
| 4318 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4319 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4320 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4321 | if (u == NULL) |
| 4322 | return NULL; |
| 4323 | p = buf = PyBytes_AsString(u); |
| 4324 | end = s + len; |
| 4325 | while (s < end) { |
| 4326 | if (*s == '\\') { |
| 4327 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4328 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4329 | strcpy(p, "u005c"); |
| 4330 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4331 | if (s >= end) |
| 4332 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4333 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4334 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4335 | if (*s & 0x80) { /* XXX inefficient */ |
| 4336 | PyObject *w; |
| 4337 | int kind; |
| 4338 | void *data; |
| 4339 | Py_ssize_t len, i; |
| 4340 | w = decode_utf8(c, &s, end); |
| 4341 | if (w == NULL) { |
| 4342 | Py_DECREF(u); |
| 4343 | return NULL; |
| 4344 | } |
| 4345 | kind = PyUnicode_KIND(w); |
| 4346 | data = PyUnicode_DATA(w); |
| 4347 | len = PyUnicode_GET_LENGTH(w); |
| 4348 | for (i = 0; i < len; i++) { |
| 4349 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4350 | sprintf(p, "\\U%08x", chr); |
| 4351 | p += 10; |
| 4352 | } |
| 4353 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4354 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4355 | Py_DECREF(w); |
| 4356 | } else { |
| 4357 | *p++ = *s++; |
| 4358 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4359 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4360 | len = p - buf; |
| 4361 | s = buf; |
| 4362 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4363 | const char *first_invalid_escape; |
| 4364 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4365 | |
| 4366 | if (v != NULL && first_invalid_escape != NULL) { |
| 4367 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4368 | /* We have not decref u before because first_invalid_escape points |
| 4369 | inside u. */ |
| 4370 | Py_XDECREF(u); |
| 4371 | Py_DECREF(v); |
| 4372 | return NULL; |
| 4373 | } |
| 4374 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4375 | Py_XDECREF(u); |
| 4376 | return v; |
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 PyObject * |
| 4380 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4381 | size_t len) |
| 4382 | { |
| 4383 | const char *first_invalid_escape; |
| 4384 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4385 | &first_invalid_escape); |
| 4386 | if (result == NULL) |
| 4387 | return NULL; |
| 4388 | |
| 4389 | if (first_invalid_escape != NULL) { |
| 4390 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4391 | Py_DECREF(result); |
| 4392 | return NULL; |
| 4393 | } |
| 4394 | } |
| 4395 | return result; |
| 4396 | } |
| 4397 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4398 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4399 | and `col_offset` to existing locations. */ |
| 4400 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4401 | { |
| 4402 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4403 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4404 | for (int i = 0; i < NCH(n); ++i) { |
| 4405 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4406 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4407 | col_offset = 0; |
| 4408 | } |
| 4409 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4410 | } |
| 4411 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4412 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | /* Fix locations for the given node and its children. |
| 4416 | |
| 4417 | `parent` is the enclosing node. |
| 4418 | `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] | 4419 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4420 | */ |
| 4421 | static void |
| 4422 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4423 | { |
| 4424 | char *substr = NULL; |
| 4425 | char *start; |
| 4426 | int lines = LINENO(parent) - 1; |
| 4427 | int cols = parent->n_col_offset; |
| 4428 | /* Find the full fstring to fix location information in `n`. */ |
| 4429 | while (parent && parent->n_type != STRING) |
| 4430 | parent = parent->n_child; |
| 4431 | if (parent && parent->n_str) { |
| 4432 | substr = strstr(parent->n_str, expr_str); |
| 4433 | if (substr) { |
| 4434 | start = substr; |
| 4435 | while (start > parent->n_str) { |
| 4436 | if (start[0] == '\n') |
| 4437 | break; |
| 4438 | start--; |
| 4439 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4440 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4441 | /* adjust the start based on the number of newlines encountered |
| 4442 | before the f-string expression */ |
| 4443 | for (char* p = parent->n_str; p < substr; p++) { |
| 4444 | if (*p == '\n') { |
| 4445 | lines++; |
| 4446 | } |
| 4447 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | fstring_shift_node_locations(n, lines, cols); |
| 4451 | } |
| 4452 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4453 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4454 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4455 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4456 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4457 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4458 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4459 | { |
| 4460 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4461 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4462 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4463 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4464 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4465 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4466 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4467 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4468 | assert(*(expr_start-1) == '{'); |
| 4469 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4470 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4471 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4472 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4473 | because turning the expression '' in to '()' would go from being invalid |
| 4474 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4475 | for (s = expr_start; s != expr_end; s++) { |
| 4476 | char c = *s; |
| 4477 | /* The Python parser ignores only the following whitespace |
| 4478 | characters (\r already is converted to \n). */ |
| 4479 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4480 | break; |
| 4481 | } |
| 4482 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4483 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4484 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4485 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4486 | } |
| 4487 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4488 | len = expr_end - expr_start; |
| 4489 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4490 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4491 | if (str == NULL) { |
| 4492 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4493 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4494 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4495 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4496 | str[0] = '('; |
| 4497 | memcpy(str+1, expr_start, len); |
| 4498 | str[len+1] = ')'; |
| 4499 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4500 | |
| 4501 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4502 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4503 | Py_eval_input, 0); |
| 4504 | if (!mod_n) { |
| 4505 | PyMem_RawFree(str); |
| 4506 | return NULL; |
| 4507 | } |
| 4508 | /* Reuse str to find the correct column offset. */ |
| 4509 | str[0] = '{'; |
| 4510 | str[len+1] = '}'; |
| 4511 | fstring_fix_node_location(n, mod_n, str); |
| 4512 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4513 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4514 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4515 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4516 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4517 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | /* Return -1 on error. |
| 4521 | |
| 4522 | Return 0 if we reached the end of the literal. |
| 4523 | |
| 4524 | Return 1 if we haven't reached the end of the literal, but we want |
| 4525 | the caller to process the literal up to this point. Used for |
| 4526 | doubled braces. |
| 4527 | */ |
| 4528 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4529 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4530 | PyObject **literal, int recurse_lvl, |
| 4531 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4532 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4533 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4534 | brace (which isn't part of a unicode name escape such as |
| 4535 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4536 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4537 | const char *s = *str; |
| 4538 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4539 | int result = 0; |
| 4540 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4541 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4542 | while (s < end) { |
| 4543 | char ch = *s++; |
| 4544 | if (!raw && ch == '\\' && s < end) { |
| 4545 | ch = *s++; |
| 4546 | if (ch == 'N') { |
| 4547 | if (s < end && *s++ == '{') { |
| 4548 | while (s < end && *s++ != '}') { |
| 4549 | } |
| 4550 | continue; |
| 4551 | } |
| 4552 | break; |
| 4553 | } |
| 4554 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4555 | return -1; |
| 4556 | } |
| 4557 | } |
| 4558 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4559 | /* Check for doubled braces, but only at the top level. If |
| 4560 | we checked at every level, then f'{0:{3}}' would fail |
| 4561 | with the two closing braces. */ |
| 4562 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4563 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4564 | /* We're going to tell the caller that the literal ends |
| 4565 | here, but that they should continue scanning. But also |
| 4566 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4567 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4568 | result = 1; |
| 4569 | goto done; |
| 4570 | } |
| 4571 | |
| 4572 | /* Where a single '{' is the start of a new expression, a |
| 4573 | single '}' is not allowed. */ |
| 4574 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4575 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4576 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4577 | return -1; |
| 4578 | } |
| 4579 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4580 | /* We're either at a '{', which means we're starting another |
| 4581 | expression; or a '}', which means we're at the end of this |
| 4582 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4583 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4584 | break; |
| 4585 | } |
| 4586 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4587 | *str = s; |
| 4588 | assert(s <= end); |
| 4589 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4590 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4591 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4592 | if (raw) |
| 4593 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4594 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4595 | NULL, NULL); |
| 4596 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4597 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4598 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4599 | if (!*literal) |
| 4600 | return -1; |
| 4601 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4602 | return result; |
| 4603 | } |
| 4604 | |
| 4605 | /* Forward declaration because parsing is recursive. */ |
| 4606 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4607 | 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] | 4608 | struct compiling *c, const node *n); |
| 4609 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4610 | /* 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] | 4611 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4612 | which includes the expression, conversion character, and |
| 4613 | format_spec expression. |
| 4614 | |
| 4615 | Note that I don't do a perfect job here: I don't make sure that a |
| 4616 | closing brace doesn't match an opening paren, for example. It |
| 4617 | doesn't need to error on all invalid expressions, just correctly |
| 4618 | find the end of all valid ones. Any errors inside the expression |
| 4619 | will be caught when we parse it later. */ |
| 4620 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4621 | 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] | 4622 | expr_ty *expression, struct compiling *c, const node *n) |
| 4623 | { |
| 4624 | /* Return -1 on error, else 0. */ |
| 4625 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4626 | const char *expr_start; |
| 4627 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4628 | expr_ty simple_expression; |
| 4629 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4630 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4631 | |
| 4632 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4633 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4634 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4635 | |
| 4636 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4637 | int string_type = 0; |
| 4638 | |
| 4639 | /* Keep track of nesting level for braces/parens/brackets in |
| 4640 | expressions. */ |
| 4641 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4642 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4643 | |
| 4644 | /* Can only nest one level deep. */ |
| 4645 | if (recurse_lvl >= 2) { |
| 4646 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4647 | return -1; |
| 4648 | } |
| 4649 | |
| 4650 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4651 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4652 | assert(**str == '{'); |
| 4653 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4654 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4655 | expr_start = *str; |
| 4656 | for (; *str < end; (*str)++) { |
| 4657 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4658 | |
| 4659 | /* Loop invariants. */ |
| 4660 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4661 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4662 | if (quote_char) |
| 4663 | assert(string_type == 1 || string_type == 3); |
| 4664 | else |
| 4665 | assert(string_type == 0); |
| 4666 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4667 | ch = **str; |
| 4668 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4669 | if (ch == '\\') { |
| 4670 | /* Error: can't include a backslash character, inside |
| 4671 | parens or strings or not. */ |
| 4672 | ast_error(c, n, "f-string expression part " |
| 4673 | "cannot include a backslash"); |
| 4674 | return -1; |
| 4675 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4676 | if (quote_char) { |
| 4677 | /* We're inside a string. See if we're at the end. */ |
| 4678 | /* This code needs to implement the same non-error logic |
| 4679 | as tok_get from tokenizer.c, at the letter_quote |
| 4680 | label. To actually share that code would be a |
| 4681 | nightmare. But, it's unlikely to change and is small, |
| 4682 | so duplicate it here. Note we don't need to catch all |
| 4683 | of the errors, since they'll be caught when parsing the |
| 4684 | expression. We just need to match the non-error |
| 4685 | cases. Thus we can ignore \n in single-quoted strings, |
| 4686 | for example. Or non-terminated strings. */ |
| 4687 | if (ch == quote_char) { |
| 4688 | /* Does this match the string_type (single or triple |
| 4689 | quoted)? */ |
| 4690 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4691 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4692 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4693 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4694 | string_type = 0; |
| 4695 | quote_char = 0; |
| 4696 | continue; |
| 4697 | } |
| 4698 | } else { |
| 4699 | /* We're at the end of a normal string. */ |
| 4700 | quote_char = 0; |
| 4701 | string_type = 0; |
| 4702 | continue; |
| 4703 | } |
| 4704 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4705 | } else if (ch == '\'' || ch == '"') { |
| 4706 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4707 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4708 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4709 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4710 | } else { |
| 4711 | /* Start of a normal string. */ |
| 4712 | string_type = 1; |
| 4713 | } |
| 4714 | /* Start looking for the end of the string. */ |
| 4715 | quote_char = ch; |
| 4716 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4717 | if (nested_depth >= MAXLEVEL) { |
| 4718 | ast_error(c, n, "f-string: too many nested parenthesis"); |
| 4719 | return -1; |
| 4720 | } |
| 4721 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4722 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4723 | } else if (ch == '#') { |
| 4724 | /* Error: can't include a comment character, inside parens |
| 4725 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4726 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4727 | return -1; |
| 4728 | } else if (nested_depth == 0 && |
| 4729 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4730 | /* First, test for the special case of "!=". Since '=' is |
| 4731 | not an allowed conversion character, nothing is lost in |
| 4732 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4733 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4734 | /* This isn't a conversion character, just continue. */ |
| 4735 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4736 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4737 | /* Normal way out of this loop. */ |
| 4738 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4739 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 4740 | if (!nested_depth) { |
| 4741 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
| 4742 | return -1; |
| 4743 | } |
| 4744 | nested_depth--; |
| 4745 | int opening = parenstack[nested_depth]; |
| 4746 | if (!((opening == '(' && ch == ')') || |
| 4747 | (opening == '[' && ch == ']') || |
| 4748 | (opening == '{' && ch == '}'))) |
| 4749 | { |
| 4750 | ast_error(c, n, |
| 4751 | "f-string: closing parenthesis '%c' " |
| 4752 | "does not match opening parenthesis '%c'", |
| 4753 | ch, opening); |
| 4754 | return -1; |
| 4755 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4756 | } else { |
| 4757 | /* Just consume this char and loop around. */ |
| 4758 | } |
| 4759 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4760 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4761 | /* If we leave this loop in a string or with mismatched parens, we |
| 4762 | don't care. We'll get a syntax error when compiling the |
| 4763 | expression. But, we can produce a better error message, so |
| 4764 | let's just do that.*/ |
| 4765 | if (quote_char) { |
| 4766 | ast_error(c, n, "f-string: unterminated string"); |
| 4767 | return -1; |
| 4768 | } |
| 4769 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4770 | int opening = parenstack[nested_depth - 1]; |
| 4771 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4772 | return -1; |
| 4773 | } |
| 4774 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4775 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4776 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4777 | |
| 4778 | /* Compile the expression as soon as possible, so we show errors |
| 4779 | related to the expression before errors related to the |
| 4780 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4781 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4782 | if (!simple_expression) |
| 4783 | return -1; |
| 4784 | |
| 4785 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4786 | if (**str == '!') { |
| 4787 | *str += 1; |
| 4788 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4789 | goto unexpected_end_of_string; |
| 4790 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4791 | conversion = **str; |
| 4792 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4793 | |
| 4794 | /* Validate the conversion. */ |
| 4795 | if (!(conversion == 's' || conversion == 'r' |
| 4796 | || conversion == 'a')) { |
| 4797 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4798 | "expected 's', 'r', or 'a'"); |
| 4799 | return -1; |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4804 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4805 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4806 | if (**str == ':') { |
| 4807 | *str += 1; |
| 4808 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4809 | goto unexpected_end_of_string; |
| 4810 | |
| 4811 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4812 | 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] | 4813 | if (!format_spec) |
| 4814 | return -1; |
| 4815 | } |
| 4816 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4817 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4818 | goto unexpected_end_of_string; |
| 4819 | |
| 4820 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4821 | assert(*str < end); |
| 4822 | assert(**str == '}'); |
| 4823 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4824 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4825 | /* And now create the FormattedValue node that represents this |
| 4826 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4827 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4828 | format_spec, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4829 | n->n_end_lineno, n->n_end_col_offset, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4830 | c->c_arena); |
| 4831 | if (!*expression) |
| 4832 | return -1; |
| 4833 | |
| 4834 | return 0; |
| 4835 | |
| 4836 | unexpected_end_of_string: |
| 4837 | ast_error(c, n, "f-string: expecting '}'"); |
| 4838 | return -1; |
| 4839 | } |
| 4840 | |
| 4841 | /* Return -1 on error. |
| 4842 | |
| 4843 | Return 0 if we have a literal (possible zero length) and an |
| 4844 | expression (zero length if at the end of the string. |
| 4845 | |
| 4846 | Return 1 if we have a literal, but no expression, and we want the |
| 4847 | caller to call us again. This is used to deal with doubled |
| 4848 | braces. |
| 4849 | |
| 4850 | When called multiple times on the string 'a{{b{0}c', this function |
| 4851 | will return: |
| 4852 | |
| 4853 | 1. the literal 'a{' with no expression, and a return value |
| 4854 | of 1. Despite the fact that there's no expression, the return |
| 4855 | value of 1 means we're not finished yet. |
| 4856 | |
| 4857 | 2. the literal 'b' and the expression '0', with a return value of |
| 4858 | 0. The fact that there's an expression means we're not finished. |
| 4859 | |
| 4860 | 3. literal 'c' with no expression and a return value of 0. The |
| 4861 | combination of the return value of 0 with no expression means |
| 4862 | we're finished. |
| 4863 | */ |
| 4864 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4865 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4866 | int recurse_lvl, PyObject **literal, |
| 4867 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4868 | struct compiling *c, const node *n) |
| 4869 | { |
| 4870 | int result; |
| 4871 | |
| 4872 | assert(*literal == NULL && *expression == NULL); |
| 4873 | |
| 4874 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4875 | 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] | 4876 | if (result < 0) |
| 4877 | goto error; |
| 4878 | |
| 4879 | assert(result == 0 || result == 1); |
| 4880 | |
| 4881 | if (result == 1) |
| 4882 | /* We have a literal, but don't look at the expression. */ |
| 4883 | return 1; |
| 4884 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4885 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4886 | /* We're at the end of the string or the end of a nested |
| 4887 | f-string: no expression. The top-level error case where we |
| 4888 | expect to be at the end of the string but we're at a '}' is |
| 4889 | handled later. */ |
| 4890 | return 0; |
| 4891 | |
| 4892 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4893 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4894 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4895 | 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] | 4896 | goto error; |
| 4897 | |
| 4898 | return 0; |
| 4899 | |
| 4900 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4901 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4902 | return -1; |
| 4903 | } |
| 4904 | |
| 4905 | #define EXPRLIST_N_CACHED 64 |
| 4906 | |
| 4907 | typedef struct { |
| 4908 | /* Incrementally build an array of expr_ty, so be used in an |
| 4909 | asdl_seq. Cache some small but reasonably sized number of |
| 4910 | expr_ty's, and then after that start dynamically allocating, |
| 4911 | doubling the number allocated each time. Note that the f-string |
| 4912 | 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] | 4913 | 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] | 4914 | fast as you add exressions in an f-string. */ |
| 4915 | |
| 4916 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4917 | Py_ssize_t size; /* Number we've used. */ |
| 4918 | expr_ty *p; /* Pointer to the memory we're actually |
| 4919 | using. Will point to 'data' until we |
| 4920 | start dynamically allocating. */ |
| 4921 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4922 | } ExprList; |
| 4923 | |
| 4924 | #ifdef NDEBUG |
| 4925 | #define ExprList_check_invariants(l) |
| 4926 | #else |
| 4927 | static void |
| 4928 | ExprList_check_invariants(ExprList *l) |
| 4929 | { |
| 4930 | /* Check our invariants. Make sure this object is "live", and |
| 4931 | hasn't been deallocated. */ |
| 4932 | assert(l->size >= 0); |
| 4933 | assert(l->p != NULL); |
| 4934 | if (l->size <= EXPRLIST_N_CACHED) |
| 4935 | assert(l->data == l->p); |
| 4936 | } |
| 4937 | #endif |
| 4938 | |
| 4939 | static void |
| 4940 | ExprList_Init(ExprList *l) |
| 4941 | { |
| 4942 | l->allocated = EXPRLIST_N_CACHED; |
| 4943 | l->size = 0; |
| 4944 | |
| 4945 | /* Until we start allocating dynamically, p points to data. */ |
| 4946 | l->p = l->data; |
| 4947 | |
| 4948 | ExprList_check_invariants(l); |
| 4949 | } |
| 4950 | |
| 4951 | static int |
| 4952 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4953 | { |
| 4954 | ExprList_check_invariants(l); |
| 4955 | if (l->size >= l->allocated) { |
| 4956 | /* We need to alloc (or realloc) the memory. */ |
| 4957 | Py_ssize_t new_size = l->allocated * 2; |
| 4958 | |
| 4959 | /* See if we've ever allocated anything dynamically. */ |
| 4960 | if (l->p == l->data) { |
| 4961 | Py_ssize_t i; |
| 4962 | /* We're still using the cached data. Switch to |
| 4963 | alloc-ing. */ |
| 4964 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4965 | if (!l->p) |
| 4966 | return -1; |
| 4967 | /* Copy the cached data into the new buffer. */ |
| 4968 | for (i = 0; i < l->size; i++) |
| 4969 | l->p[i] = l->data[i]; |
| 4970 | } else { |
| 4971 | /* Just realloc. */ |
| 4972 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4973 | if (!tmp) { |
| 4974 | PyMem_RawFree(l->p); |
| 4975 | l->p = NULL; |
| 4976 | return -1; |
| 4977 | } |
| 4978 | l->p = tmp; |
| 4979 | } |
| 4980 | |
| 4981 | l->allocated = new_size; |
| 4982 | assert(l->allocated == 2 * l->size); |
| 4983 | } |
| 4984 | |
| 4985 | l->p[l->size++] = exp; |
| 4986 | |
| 4987 | ExprList_check_invariants(l); |
| 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | static void |
| 4992 | ExprList_Dealloc(ExprList *l) |
| 4993 | { |
| 4994 | ExprList_check_invariants(l); |
| 4995 | |
| 4996 | /* If there's been an error, or we've never dynamically allocated, |
| 4997 | do nothing. */ |
| 4998 | if (!l->p || l->p == l->data) { |
| 4999 | /* Do nothing. */ |
| 5000 | } else { |
| 5001 | /* We have dynamically allocated. Free the memory. */ |
| 5002 | PyMem_RawFree(l->p); |
| 5003 | } |
| 5004 | l->p = NULL; |
| 5005 | l->size = -1; |
| 5006 | } |
| 5007 | |
| 5008 | static asdl_seq * |
| 5009 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5010 | { |
| 5011 | asdl_seq *seq; |
| 5012 | |
| 5013 | ExprList_check_invariants(l); |
| 5014 | |
| 5015 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5016 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5017 | if (seq) { |
| 5018 | Py_ssize_t i; |
| 5019 | for (i = 0; i < l->size; i++) |
| 5020 | asdl_seq_SET(seq, i, l->p[i]); |
| 5021 | } |
| 5022 | ExprList_Dealloc(l); |
| 5023 | return seq; |
| 5024 | } |
| 5025 | |
| 5026 | /* The FstringParser is designed to add a mix of strings and |
| 5027 | f-strings, and concat them together as needed. Ultimately, it |
| 5028 | generates an expr_ty. */ |
| 5029 | typedef struct { |
| 5030 | PyObject *last_str; |
| 5031 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5032 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5033 | } FstringParser; |
| 5034 | |
| 5035 | #ifdef NDEBUG |
| 5036 | #define FstringParser_check_invariants(state) |
| 5037 | #else |
| 5038 | static void |
| 5039 | FstringParser_check_invariants(FstringParser *state) |
| 5040 | { |
| 5041 | if (state->last_str) |
| 5042 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5043 | ExprList_check_invariants(&state->expr_list); |
| 5044 | } |
| 5045 | #endif |
| 5046 | |
| 5047 | static void |
| 5048 | FstringParser_Init(FstringParser *state) |
| 5049 | { |
| 5050 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5051 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5052 | ExprList_Init(&state->expr_list); |
| 5053 | FstringParser_check_invariants(state); |
| 5054 | } |
| 5055 | |
| 5056 | static void |
| 5057 | FstringParser_Dealloc(FstringParser *state) |
| 5058 | { |
| 5059 | FstringParser_check_invariants(state); |
| 5060 | |
| 5061 | Py_XDECREF(state->last_str); |
| 5062 | ExprList_Dealloc(&state->expr_list); |
| 5063 | } |
| 5064 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5065 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5066 | static expr_ty |
| 5067 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5068 | { |
| 5069 | PyObject *s = *str; |
| 5070 | *str = NULL; |
| 5071 | assert(PyUnicode_CheckExact(s)); |
| 5072 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5073 | Py_DECREF(s); |
| 5074 | return NULL; |
| 5075 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5076 | return Constant(s, LINENO(n), n->n_col_offset, |
| 5077 | 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] | 5078 | } |
| 5079 | |
| 5080 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5081 | decref'd. */ |
| 5082 | static int |
| 5083 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5084 | { |
| 5085 | FstringParser_check_invariants(state); |
| 5086 | |
| 5087 | assert(PyUnicode_CheckExact(str)); |
| 5088 | |
| 5089 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5090 | Py_DECREF(str); |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
| 5094 | if (!state->last_str) { |
| 5095 | /* We didn't have a string before, so just remember this one. */ |
| 5096 | state->last_str = str; |
| 5097 | } else { |
| 5098 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5099 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5100 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5101 | return -1; |
| 5102 | } |
| 5103 | FstringParser_check_invariants(state); |
| 5104 | return 0; |
| 5105 | } |
| 5106 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5107 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5108 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5109 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5110 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5111 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5112 | struct compiling *c, const node *n) |
| 5113 | { |
| 5114 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5115 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5116 | |
| 5117 | /* Parse the f-string. */ |
| 5118 | while (1) { |
| 5119 | PyObject *literal = NULL; |
| 5120 | expr_ty expression = NULL; |
| 5121 | |
| 5122 | /* If there's a zero length literal in front of the |
| 5123 | expression, literal will be NULL. If we're at the end of |
| 5124 | the f-string, expression will be NULL (unless result == 1, |
| 5125 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5126 | 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] | 5127 | &literal, &expression, |
| 5128 | c, n); |
| 5129 | if (result < 0) |
| 5130 | return -1; |
| 5131 | |
| 5132 | /* Add the literal, if any. */ |
| 5133 | if (!literal) { |
| 5134 | /* Do nothing. Just leave last_str alone (and possibly |
| 5135 | NULL). */ |
| 5136 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 5137 | /* Note that the literal can be zero length, if the |
| 5138 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5139 | state->last_str = literal; |
| 5140 | literal = NULL; |
| 5141 | } else { |
| 5142 | /* We have a literal, concatenate it. */ |
| 5143 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 5144 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 5145 | return -1; |
| 5146 | literal = NULL; |
| 5147 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5148 | |
| 5149 | /* We've dealt with the literal now. It can't be leaked on further |
| 5150 | errors. */ |
| 5151 | assert(literal == NULL); |
| 5152 | |
| 5153 | /* See if we should just loop around to get the next literal |
| 5154 | and expression, while ignoring the expression this |
| 5155 | time. This is used for un-doubling braces, as an |
| 5156 | optimization. */ |
| 5157 | if (result == 1) |
| 5158 | continue; |
| 5159 | |
| 5160 | if (!expression) |
| 5161 | /* We're done with this f-string. */ |
| 5162 | break; |
| 5163 | |
| 5164 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5165 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5166 | if (!state->last_str) { |
| 5167 | /* Do nothing. No previous literal. */ |
| 5168 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5169 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5170 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5171 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5172 | return -1; |
| 5173 | } |
| 5174 | |
| 5175 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5176 | return -1; |
| 5177 | } |
| 5178 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5179 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5180 | string. Otherwise, we must be at a right brace. */ |
| 5181 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5182 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5183 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5184 | return -1; |
| 5185 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5186 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5187 | ast_error(c, n, "f-string: expecting '}'"); |
| 5188 | return -1; |
| 5189 | } |
| 5190 | |
| 5191 | FstringParser_check_invariants(state); |
| 5192 | return 0; |
| 5193 | } |
| 5194 | |
| 5195 | /* 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] | 5196 | 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] | 5197 | static expr_ty |
| 5198 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5199 | const node *n) |
| 5200 | { |
| 5201 | asdl_seq *seq; |
| 5202 | |
| 5203 | FstringParser_check_invariants(state); |
| 5204 | |
| 5205 | /* If we're just a constant string with no expressions, return |
| 5206 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5207 | if (!state->fmode) { |
| 5208 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5209 | if (!state->last_str) { |
| 5210 | /* Create a zero length string. */ |
| 5211 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5212 | if (!state->last_str) |
| 5213 | goto error; |
| 5214 | } |
| 5215 | return make_str_node_and_del(&state->last_str, c, n); |
| 5216 | } |
| 5217 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5218 | /* 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] | 5219 | last node in our expression list. */ |
| 5220 | if (state->last_str) { |
| 5221 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5222 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5223 | goto error; |
| 5224 | } |
| 5225 | /* This has already been freed. */ |
| 5226 | assert(state->last_str == NULL); |
| 5227 | |
| 5228 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5229 | if (!seq) |
| 5230 | goto error; |
| 5231 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5232 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5233 | 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] | 5234 | |
| 5235 | error: |
| 5236 | FstringParser_Dealloc(state); |
| 5237 | return NULL; |
| 5238 | } |
| 5239 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5240 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5241 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5242 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5243 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5244 | 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] | 5245 | struct compiling *c, const node *n) |
| 5246 | { |
| 5247 | FstringParser state; |
| 5248 | |
| 5249 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5250 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5251 | c, n) < 0) { |
| 5252 | FstringParser_Dealloc(&state); |
| 5253 | return NULL; |
| 5254 | } |
| 5255 | |
| 5256 | return FstringParser_Finish(&state, c, n); |
| 5257 | } |
| 5258 | |
| 5259 | /* n is a Python string literal, including the bracketing quote |
| 5260 | 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] | 5261 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5262 | 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] | 5263 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5264 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5265 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5266 | static int |
| 5267 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5268 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5269 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5270 | size_t len; |
| 5271 | const char *s = STR(n); |
| 5272 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5273 | int fmode = 0; |
| 5274 | *bytesmode = 0; |
| 5275 | *rawmode = 0; |
| 5276 | *result = NULL; |
| 5277 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5278 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5279 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5280 | if (quote == 'b' || quote == 'B') { |
| 5281 | quote = *++s; |
| 5282 | *bytesmode = 1; |
| 5283 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5284 | else if (quote == 'u' || quote == 'U') { |
| 5285 | quote = *++s; |
| 5286 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5287 | else if (quote == 'r' || quote == 'R') { |
| 5288 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5289 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5290 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5291 | else if (quote == 'f' || quote == 'F') { |
| 5292 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5293 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5294 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5295 | else { |
| 5296 | break; |
| 5297 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5298 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5299 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5300 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5301 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5302 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5303 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5304 | if (quote != '\'' && quote != '\"') { |
| 5305 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5306 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5307 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5308 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5309 | s++; |
| 5310 | len = strlen(s); |
| 5311 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5313 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5314 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5315 | } |
| 5316 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5317 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5318 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5319 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5320 | } |
| 5321 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5322 | /* A triple quoted string. We've already skipped one quote at |
| 5323 | the start and one at the end of the string. Now skip the |
| 5324 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5325 | s += 2; |
| 5326 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5327 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5328 | if (s[--len] != quote || s[--len] != quote) { |
| 5329 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5330 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5331 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5332 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5333 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5334 | if (fmode) { |
| 5335 | /* Just return the bytes. The caller will parse the resulting |
| 5336 | string. */ |
| 5337 | *fstr = s; |
| 5338 | *fstrlen = len; |
| 5339 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5340 | } |
| 5341 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5342 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5343 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5344 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5345 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5346 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5347 | const char *ch; |
| 5348 | for (ch = s; *ch; ch++) { |
| 5349 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5350 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5351 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5352 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5353 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5354 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5355 | if (*rawmode) |
| 5356 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5357 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5358 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5359 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5360 | if (*rawmode) |
| 5361 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5362 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5363 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5364 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5365 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5368 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5369 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5370 | 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] | 5371 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5372 | 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] | 5373 | node if there's just an f-string (with no leading or trailing |
| 5374 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5375 | any literals involved. */ |
| 5376 | static expr_ty |
| 5377 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5378 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5379 | int bytesmode = 0; |
| 5380 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5381 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5382 | |
| 5383 | FstringParser state; |
| 5384 | FstringParser_Init(&state); |
| 5385 | |
| 5386 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5387 | int this_bytesmode; |
| 5388 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5389 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5390 | const char *fstr; |
| 5391 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5392 | |
| 5393 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5394 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5395 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5396 | goto error; |
| 5397 | |
| 5398 | /* Check that we're not mixing bytes with unicode. */ |
| 5399 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5400 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5401 | /* s is NULL if the current string part is an f-string. */ |
| 5402 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5403 | goto error; |
| 5404 | } |
| 5405 | bytesmode = this_bytesmode; |
| 5406 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5407 | if (fstr != NULL) { |
| 5408 | int result; |
| 5409 | assert(s == NULL && !bytesmode); |
| 5410 | /* This is an f-string. Parse and concatenate it. */ |
| 5411 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5412 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5413 | if (result < 0) |
| 5414 | goto error; |
| 5415 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5416 | /* A string or byte string. */ |
| 5417 | assert(s != NULL && fstr == NULL); |
| 5418 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5419 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5420 | PyUnicode_CheckExact(s)); |
| 5421 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5422 | if (bytesmode) { |
| 5423 | /* For bytes, concat as we go. */ |
| 5424 | if (i == 0) { |
| 5425 | /* First time, just remember this value. */ |
| 5426 | bytes_str = s; |
| 5427 | } else { |
| 5428 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5429 | if (!bytes_str) |
| 5430 | goto error; |
| 5431 | } |
| 5432 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5433 | /* This is a regular string. Concatenate it. */ |
| 5434 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5435 | goto error; |
| 5436 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5437 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5438 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5439 | if (bytesmode) { |
| 5440 | /* Just return the bytes object and we're done. */ |
| 5441 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5442 | goto error; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5443 | return Constant(bytes_str, LINENO(n), n->n_col_offset, |
| 5444 | 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] | 5445 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5446 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5447 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5448 | assert(bytes_str == NULL); |
| 5449 | |
| 5450 | return FstringParser_Finish(&state, c, n); |
| 5451 | |
| 5452 | error: |
| 5453 | Py_XDECREF(bytes_str); |
| 5454 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5455 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5456 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5457 | |
| 5458 | PyObject * |
| 5459 | _PyAST_GetDocString(asdl_seq *body) |
| 5460 | { |
| 5461 | if (!asdl_seq_LEN(body)) { |
| 5462 | return NULL; |
| 5463 | } |
| 5464 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5465 | if (st->kind != Expr_kind) { |
| 5466 | return NULL; |
| 5467 | } |
| 5468 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5469 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5470 | return e->v.Constant.value; |
| 5471 | } |
| 5472 | return NULL; |
| 5473 | } |