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 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 16 | static int validate_stmts(asdl_seq *); |
| 17 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 18 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 19 | static int validate_stmt(stmt_ty); |
| 20 | static int validate_expr(expr_ty, expr_context_ty); |
| 21 | |
| 22 | static int |
| 23 | validate_comprehension(asdl_seq *gens) |
| 24 | { |
| 25 | int i; |
| 26 | if (!asdl_seq_LEN(gens)) { |
| 27 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 28 | return 0; |
| 29 | } |
| 30 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 31 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 32 | if (!validate_expr(comp->target, Store) || |
| 33 | !validate_expr(comp->iter, Load) || |
| 34 | !validate_exprs(comp->ifs, Load, 0)) |
| 35 | return 0; |
| 36 | } |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | static int |
| 41 | validate_slice(slice_ty slice) |
| 42 | { |
| 43 | switch (slice->kind) { |
| 44 | case Slice_kind: |
| 45 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 46 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 47 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 48 | case ExtSlice_kind: { |
| 49 | int i; |
| 50 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 51 | return 0; |
| 52 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 53 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 54 | return 0; |
| 55 | return 1; |
| 56 | } |
| 57 | case Index_kind: |
| 58 | return validate_expr(slice->v.Index.value, Load); |
| 59 | default: |
| 60 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | validate_keywords(asdl_seq *keywords) |
| 67 | { |
| 68 | int i; |
| 69 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 70 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 71 | return 0; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static int |
| 76 | validate_args(asdl_seq *args) |
| 77 | { |
| 78 | int i; |
| 79 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 80 | arg_ty arg = asdl_seq_GET(args, i); |
| 81 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 82 | return 0; |
| 83 | } |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | static const char * |
| 88 | expr_context_name(expr_context_ty ctx) |
| 89 | { |
| 90 | switch (ctx) { |
| 91 | case Load: |
| 92 | return "Load"; |
| 93 | case Store: |
| 94 | return "Store"; |
| 95 | case Del: |
| 96 | return "Del"; |
| 97 | case AugLoad: |
| 98 | return "AugLoad"; |
| 99 | case AugStore: |
| 100 | return "AugStore"; |
| 101 | case Param: |
| 102 | return "Param"; |
| 103 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 104 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | validate_arguments(arguments_ty args) |
| 110 | { |
| 111 | if (!validate_args(args->args)) |
| 112 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 113 | if (args->vararg && args->vararg->annotation |
| 114 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | if (!validate_args(args->kwonlyargs)) |
| 118 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 119 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 120 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 124 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 125 | return 0; |
| 126 | } |
| 127 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 128 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 129 | "kw_defaults on arguments"); |
| 130 | return 0; |
| 131 | } |
| 132 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 133 | } |
| 134 | |
| 135 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 136 | validate_constant(PyObject *value) |
| 137 | { |
| 138 | if (value == Py_None || value == Py_Ellipsis) |
| 139 | return 1; |
| 140 | |
| 141 | if (PyLong_CheckExact(value) |
| 142 | || PyFloat_CheckExact(value) |
| 143 | || PyComplex_CheckExact(value) |
| 144 | || PyBool_Check(value) |
| 145 | || PyUnicode_CheckExact(value) |
| 146 | || PyBytes_CheckExact(value)) |
| 147 | return 1; |
| 148 | |
| 149 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 150 | PyObject *it; |
| 151 | |
| 152 | it = PyObject_GetIter(value); |
| 153 | if (it == NULL) |
| 154 | return 0; |
| 155 | |
| 156 | while (1) { |
| 157 | PyObject *item = PyIter_Next(it); |
| 158 | if (item == NULL) { |
| 159 | if (PyErr_Occurred()) { |
| 160 | Py_DECREF(it); |
| 161 | return 0; |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | if (!validate_constant(item)) { |
| 167 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 168 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 171 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Py_DECREF(it); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 182 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 183 | { |
| 184 | int check_ctx = 1; |
| 185 | expr_context_ty actual_ctx; |
| 186 | |
| 187 | /* First check expression context. */ |
| 188 | switch (exp->kind) { |
| 189 | case Attribute_kind: |
| 190 | actual_ctx = exp->v.Attribute.ctx; |
| 191 | break; |
| 192 | case Subscript_kind: |
| 193 | actual_ctx = exp->v.Subscript.ctx; |
| 194 | break; |
| 195 | case Starred_kind: |
| 196 | actual_ctx = exp->v.Starred.ctx; |
| 197 | break; |
| 198 | case Name_kind: |
| 199 | actual_ctx = exp->v.Name.ctx; |
| 200 | break; |
| 201 | case List_kind: |
| 202 | actual_ctx = exp->v.List.ctx; |
| 203 | break; |
| 204 | case Tuple_kind: |
| 205 | actual_ctx = exp->v.Tuple.ctx; |
| 206 | break; |
| 207 | default: |
| 208 | if (ctx != Load) { |
| 209 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 210 | "assigned to in %s context", expr_context_name(ctx)); |
| 211 | return 0; |
| 212 | } |
| 213 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 214 | /* set actual_ctx to prevent gcc warning */ |
| 215 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 216 | } |
| 217 | if (check_ctx && actual_ctx != ctx) { |
| 218 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 219 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* Now validate expression. */ |
| 224 | switch (exp->kind) { |
| 225 | case BoolOp_kind: |
| 226 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 227 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 228 | return 0; |
| 229 | } |
| 230 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 231 | case BinOp_kind: |
| 232 | return validate_expr(exp->v.BinOp.left, Load) && |
| 233 | validate_expr(exp->v.BinOp.right, Load); |
| 234 | case UnaryOp_kind: |
| 235 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 236 | case Lambda_kind: |
| 237 | return validate_arguments(exp->v.Lambda.args) && |
| 238 | validate_expr(exp->v.Lambda.body, Load); |
| 239 | case IfExp_kind: |
| 240 | return validate_expr(exp->v.IfExp.test, Load) && |
| 241 | validate_expr(exp->v.IfExp.body, Load) && |
| 242 | validate_expr(exp->v.IfExp.orelse, Load); |
| 243 | case Dict_kind: |
| 244 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 245 | PyErr_SetString(PyExc_ValueError, |
| 246 | "Dict doesn't have the same number of keys as values"); |
| 247 | return 0; |
| 248 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 249 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 250 | dict literals, i.e. ``{**{a:b}}`` */ |
| 251 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 252 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 253 | case Set_kind: |
| 254 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 255 | #define COMP(NAME) \ |
| 256 | case NAME ## _kind: \ |
| 257 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 258 | validate_expr(exp->v.NAME.elt, Load); |
| 259 | COMP(ListComp) |
| 260 | COMP(SetComp) |
| 261 | COMP(GeneratorExp) |
| 262 | #undef COMP |
| 263 | case DictComp_kind: |
| 264 | return validate_comprehension(exp->v.DictComp.generators) && |
| 265 | validate_expr(exp->v.DictComp.key, Load) && |
| 266 | validate_expr(exp->v.DictComp.value, Load); |
| 267 | case Yield_kind: |
| 268 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 269 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 270 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 271 | case Await_kind: |
| 272 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 273 | case Compare_kind: |
| 274 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 275 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 276 | return 0; |
| 277 | } |
| 278 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 279 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 280 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 281 | "of comparators and operands"); |
| 282 | return 0; |
| 283 | } |
| 284 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 285 | validate_expr(exp->v.Compare.left, Load); |
| 286 | case Call_kind: |
| 287 | return validate_expr(exp->v.Call.func, Load) && |
| 288 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 289 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 290 | case Constant_kind: |
| 291 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 292 | PyErr_Format(PyExc_TypeError, |
| 293 | "got an invalid type in Constant: %s", |
| 294 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 298 | case Num_kind: { |
| 299 | PyObject *n = exp->v.Num.n; |
| 300 | if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) && |
| 301 | !PyComplex_CheckExact(n)) { |
| 302 | PyErr_SetString(PyExc_TypeError, "non-numeric type in Num"); |
| 303 | return 0; |
| 304 | } |
| 305 | return 1; |
| 306 | } |
| 307 | case Str_kind: { |
| 308 | PyObject *s = exp->v.Str.s; |
| 309 | if (!PyUnicode_CheckExact(s)) { |
| 310 | PyErr_SetString(PyExc_TypeError, "non-string type in Str"); |
| 311 | return 0; |
| 312 | } |
| 313 | return 1; |
| 314 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 315 | case JoinedStr_kind: |
| 316 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 317 | case FormattedValue_kind: |
| 318 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 319 | return 0; |
| 320 | if (exp->v.FormattedValue.format_spec) |
| 321 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 322 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 323 | case Bytes_kind: { |
| 324 | PyObject *b = exp->v.Bytes.s; |
| 325 | if (!PyBytes_CheckExact(b)) { |
| 326 | PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes"); |
| 327 | return 0; |
| 328 | } |
| 329 | return 1; |
| 330 | } |
| 331 | case Attribute_kind: |
| 332 | return validate_expr(exp->v.Attribute.value, Load); |
| 333 | case Subscript_kind: |
| 334 | return validate_slice(exp->v.Subscript.slice) && |
| 335 | validate_expr(exp->v.Subscript.value, Load); |
| 336 | case Starred_kind: |
| 337 | return validate_expr(exp->v.Starred.value, ctx); |
| 338 | case List_kind: |
| 339 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 340 | case Tuple_kind: |
| 341 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
| 342 | /* These last cases don't have any checking. */ |
| 343 | case Name_kind: |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 344 | case NameConstant_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 345 | case Ellipsis_kind: |
| 346 | return 1; |
| 347 | default: |
| 348 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 349 | return 0; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static int |
| 354 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 355 | { |
| 356 | if (asdl_seq_LEN(seq)) |
| 357 | return 1; |
| 358 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int |
| 363 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 364 | { |
| 365 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 366 | validate_exprs(targets, ctx, 0); |
| 367 | } |
| 368 | |
| 369 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 370 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 371 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 372 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int |
| 376 | validate_stmt(stmt_ty stmt) |
| 377 | { |
| 378 | int i; |
| 379 | switch (stmt->kind) { |
| 380 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 381 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 382 | validate_arguments(stmt->v.FunctionDef.args) && |
| 383 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 384 | (!stmt->v.FunctionDef.returns || |
| 385 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 386 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 387 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 388 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 389 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 390 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 391 | case Return_kind: |
| 392 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 393 | case Delete_kind: |
| 394 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 395 | case Assign_kind: |
| 396 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 397 | validate_expr(stmt->v.Assign.value, Load); |
| 398 | case AugAssign_kind: |
| 399 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 400 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 401 | case AnnAssign_kind: |
| 402 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 403 | stmt->v.AnnAssign.simple) { |
| 404 | PyErr_SetString(PyExc_TypeError, |
| 405 | "AnnAssign with simple non-Name target"); |
| 406 | return 0; |
| 407 | } |
| 408 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 409 | (!stmt->v.AnnAssign.value || |
| 410 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 411 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 412 | case For_kind: |
| 413 | return validate_expr(stmt->v.For.target, Store) && |
| 414 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 415 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 416 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 417 | case AsyncFor_kind: |
| 418 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 419 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 420 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 421 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 422 | case While_kind: |
| 423 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 424 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 425 | validate_stmts(stmt->v.While.orelse); |
| 426 | case If_kind: |
| 427 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 428 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 429 | validate_stmts(stmt->v.If.orelse); |
| 430 | case With_kind: |
| 431 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 432 | return 0; |
| 433 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 434 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 435 | if (!validate_expr(item->context_expr, Load) || |
| 436 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 437 | return 0; |
| 438 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 439 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 440 | case AsyncWith_kind: |
| 441 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 442 | return 0; |
| 443 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 444 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 445 | if (!validate_expr(item->context_expr, Load) || |
| 446 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 447 | return 0; |
| 448 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 449 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 450 | case Raise_kind: |
| 451 | if (stmt->v.Raise.exc) { |
| 452 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 453 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 454 | } |
| 455 | if (stmt->v.Raise.cause) { |
| 456 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 457 | return 0; |
| 458 | } |
| 459 | return 1; |
| 460 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 461 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 462 | return 0; |
| 463 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 464 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 465 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 466 | return 0; |
| 467 | } |
| 468 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 469 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 470 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 471 | return 0; |
| 472 | } |
| 473 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 474 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 475 | if ((handler->v.ExceptHandler.type && |
| 476 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 477 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 481 | validate_stmts(stmt->v.Try.finalbody)) && |
| 482 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 483 | validate_stmts(stmt->v.Try.orelse)); |
| 484 | case Assert_kind: |
| 485 | return validate_expr(stmt->v.Assert.test, Load) && |
| 486 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 487 | case Import_kind: |
| 488 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 489 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 490 | if (stmt->v.ImportFrom.level < 0) { |
| 491 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 495 | case Global_kind: |
| 496 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 497 | case Nonlocal_kind: |
| 498 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 499 | case Expr_kind: |
| 500 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 501 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 502 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 503 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 504 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 505 | (!stmt->v.AsyncFunctionDef.returns || |
| 506 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 507 | case Pass_kind: |
| 508 | case Break_kind: |
| 509 | case Continue_kind: |
| 510 | return 1; |
| 511 | default: |
| 512 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 513 | return 0; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static int |
| 518 | validate_stmts(asdl_seq *seq) |
| 519 | { |
| 520 | int i; |
| 521 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 522 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 523 | if (stmt) { |
| 524 | if (!validate_stmt(stmt)) |
| 525 | return 0; |
| 526 | } |
| 527 | else { |
| 528 | PyErr_SetString(PyExc_ValueError, |
| 529 | "None disallowed in statement list"); |
| 530 | return 0; |
| 531 | } |
| 532 | } |
| 533 | return 1; |
| 534 | } |
| 535 | |
| 536 | static int |
| 537 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 538 | { |
| 539 | int i; |
| 540 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 541 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 542 | if (expr) { |
| 543 | if (!validate_expr(expr, ctx)) |
| 544 | return 0; |
| 545 | } |
| 546 | else if (!null_ok) { |
| 547 | PyErr_SetString(PyExc_ValueError, |
| 548 | "None disallowed in expression list"); |
| 549 | return 0; |
| 550 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 551 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 552 | } |
| 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | int |
| 557 | PyAST_Validate(mod_ty mod) |
| 558 | { |
| 559 | int res = 0; |
| 560 | |
| 561 | switch (mod->kind) { |
| 562 | case Module_kind: |
| 563 | res = validate_stmts(mod->v.Module.body); |
| 564 | break; |
| 565 | case Interactive_kind: |
| 566 | res = validate_stmts(mod->v.Interactive.body); |
| 567 | break; |
| 568 | case Expression_kind: |
| 569 | res = validate_expr(mod->v.Expression.body, Load); |
| 570 | break; |
| 571 | case Suite_kind: |
| 572 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 573 | break; |
| 574 | default: |
| 575 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 576 | res = 0; |
| 577 | break; |
| 578 | } |
| 579 | return res; |
| 580 | } |
| 581 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 582 | /* 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] | 583 | #include "grammar.h" |
| 584 | #include "parsetok.h" |
| 585 | #include "graminit.h" |
| 586 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 587 | /* Data structure used internally */ |
| 588 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 589 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 590 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 591 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 595 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 596 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 597 | static asdl_seq *ast_for_suite(struct compiling *c, const node *n); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 598 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 599 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 600 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 601 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 602 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 603 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); |
| 604 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); |
| 605 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 606 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 607 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 608 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 609 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 610 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 611 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 612 | #define COMP_GENEXP 0 |
| 613 | #define COMP_LISTCOMP 1 |
| 614 | #define COMP_SETCOMP 2 |
| 615 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 616 | static int |
| 617 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 618 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 619 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 620 | if (!m) |
| 621 | return 0; |
| 622 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 623 | Py_DECREF(m); |
| 624 | if (!c->c_normalize) |
| 625 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 630 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 631 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 632 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 633 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 634 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 635 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 636 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 637 | /* Check whether there are non-ASCII characters in the |
| 638 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 639 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 641 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 642 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 643 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 644 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 645 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 646 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 647 | if (form == NULL) { |
| 648 | Py_DECREF(id); |
| 649 | return NULL; |
| 650 | } |
| 651 | PyObject *args[2] = {form, id}; |
| 652 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 653 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 654 | if (!id2) |
| 655 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 656 | if (!PyUnicode_Check(id2)) { |
| 657 | PyErr_Format(PyExc_TypeError, |
| 658 | "unicodedata.normalize() must return a string, not " |
| 659 | "%.200s", |
| 660 | Py_TYPE(id2)->tp_name); |
| 661 | Py_DECREF(id2); |
| 662 | return NULL; |
| 663 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 664 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 665 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 666 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 667 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 668 | Py_DECREF(id); |
| 669 | return NULL; |
| 670 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 671 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 674 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 675 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 676 | static int |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 677 | ast_error(struct compiling *c, const node *n, const char *errmsg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 678 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 679 | PyObject *value, *errstr, *loc, *tmp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 680 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 681 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 683 | Py_INCREF(Py_None); |
| 684 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 685 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 686 | tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 687 | if (!tmp) |
| 688 | return 0; |
| 689 | errstr = PyUnicode_FromString(errmsg); |
| 690 | if (!errstr) { |
| 691 | Py_DECREF(tmp); |
| 692 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 693 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 694 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 695 | Py_DECREF(errstr); |
| 696 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 697 | if (value) { |
| 698 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 699 | Py_DECREF(value); |
| 700 | } |
| 701 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* num_stmts() returns number of contained statements. |
| 705 | |
| 706 | Use this routine to determine how big a sequence is needed for |
| 707 | the statements in a parse tree. Its raison d'etre is this bit of |
| 708 | grammar: |
| 709 | |
| 710 | stmt: simple_stmt | compound_stmt |
| 711 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 712 | |
| 713 | A simple_stmt can contain multiple small_stmt elements joined |
| 714 | by semicolons. If the arg is a simple_stmt, the number of |
| 715 | small_stmt elements is returned. |
| 716 | */ |
| 717 | |
| 718 | static int |
| 719 | num_stmts(const node *n) |
| 720 | { |
| 721 | int i, l; |
| 722 | node *ch; |
| 723 | |
| 724 | switch (TYPE(n)) { |
| 725 | case single_input: |
| 726 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 727 | return 0; |
| 728 | else |
| 729 | return num_stmts(CHILD(n, 0)); |
| 730 | case file_input: |
| 731 | l = 0; |
| 732 | for (i = 0; i < NCH(n); i++) { |
| 733 | ch = CHILD(n, i); |
| 734 | if (TYPE(ch) == stmt) |
| 735 | l += num_stmts(ch); |
| 736 | } |
| 737 | return l; |
| 738 | case stmt: |
| 739 | return num_stmts(CHILD(n, 0)); |
| 740 | case compound_stmt: |
| 741 | return 1; |
| 742 | case simple_stmt: |
| 743 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 744 | case suite: |
| 745 | if (NCH(n) == 1) |
| 746 | return num_stmts(CHILD(n, 0)); |
| 747 | else { |
| 748 | l = 0; |
| 749 | for (i = 2; i < (NCH(n) - 1); i++) |
| 750 | l += num_stmts(CHILD(n, i)); |
| 751 | return l; |
| 752 | } |
| 753 | default: { |
| 754 | char buf[128]; |
| 755 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 756 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 757 | TYPE(n), NCH(n)); |
| 758 | Py_FatalError(buf); |
| 759 | } |
| 760 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 761 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* Transform the CST rooted at node * to the appropriate AST |
| 765 | */ |
| 766 | |
| 767 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 768 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 769 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 770 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 771 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 772 | asdl_seq *stmts = NULL; |
| 773 | stmt_ty s; |
| 774 | node *ch; |
| 775 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 776 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 777 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 778 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 779 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 780 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 781 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 782 | |
| 783 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 785 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 786 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 787 | switch (TYPE(n)) { |
| 788 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 789 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 791 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | for (i = 0; i < NCH(n) - 1; i++) { |
| 793 | ch = CHILD(n, i); |
| 794 | if (TYPE(ch) == NEWLINE) |
| 795 | continue; |
| 796 | REQ(ch, stmt); |
| 797 | num = num_stmts(ch); |
| 798 | if (num == 1) { |
| 799 | s = ast_for_stmt(&c, ch); |
| 800 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 801 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 802 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | } |
| 804 | else { |
| 805 | ch = CHILD(ch, 0); |
| 806 | REQ(ch, simple_stmt); |
| 807 | for (j = 0; j < num; j++) { |
| 808 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 809 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 810 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 811 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 815 | res = Module(stmts, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 816 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 817 | case eval_input: { |
| 818 | expr_ty testlist_ast; |
| 819 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 820 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 821 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 823 | goto out; |
| 824 | res = Expression(testlist_ast, arena); |
| 825 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 826 | } |
| 827 | case single_input: |
| 828 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 829 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 830 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 831 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 832 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 833 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 834 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 835 | goto out; |
| 836 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | } |
| 838 | else { |
| 839 | n = CHILD(n, 0); |
| 840 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 841 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 842 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 843 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 845 | s = ast_for_stmt(&c, n); |
| 846 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 847 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 848 | asdl_seq_SET(stmts, 0, s); |
| 849 | } |
| 850 | else { |
| 851 | /* Only a simple_stmt can contain multiple statements. */ |
| 852 | REQ(n, simple_stmt); |
| 853 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 854 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 855 | break; |
| 856 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 857 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 858 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 859 | asdl_seq_SET(stmts, i / 2, s); |
| 860 | } |
| 861 | } |
| 862 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 863 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 864 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 865 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 867 | PyErr_Format(PyExc_SystemError, |
| 868 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 869 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 870 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 871 | out: |
| 872 | if (c.c_normalize) { |
| 873 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 874 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 875 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 878 | mod_ty |
| 879 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 880 | PyArena *arena) |
| 881 | { |
| 882 | mod_ty mod; |
| 883 | PyObject *filename; |
| 884 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 885 | if (filename == NULL) |
| 886 | return NULL; |
| 887 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 888 | Py_DECREF(filename); |
| 889 | return mod; |
| 890 | |
| 891 | } |
| 892 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 893 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 894 | */ |
| 895 | |
| 896 | static operator_ty |
| 897 | get_operator(const node *n) |
| 898 | { |
| 899 | switch (TYPE(n)) { |
| 900 | case VBAR: |
| 901 | return BitOr; |
| 902 | case CIRCUMFLEX: |
| 903 | return BitXor; |
| 904 | case AMPER: |
| 905 | return BitAnd; |
| 906 | case LEFTSHIFT: |
| 907 | return LShift; |
| 908 | case RIGHTSHIFT: |
| 909 | return RShift; |
| 910 | case PLUS: |
| 911 | return Add; |
| 912 | case MINUS: |
| 913 | return Sub; |
| 914 | case STAR: |
| 915 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 916 | case AT: |
| 917 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 918 | case SLASH: |
| 919 | return Div; |
| 920 | case DOUBLESLASH: |
| 921 | return FloorDiv; |
| 922 | case PERCENT: |
| 923 | return Mod; |
| 924 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 925 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 929 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 930 | "None", |
| 931 | "True", |
| 932 | "False", |
| 933 | NULL, |
| 934 | }; |
| 935 | |
| 936 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 937 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 938 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 939 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 940 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 941 | if (_PyUnicode_EqualToASCIIString(name, "__debug__")) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 942 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 943 | return 1; |
| 944 | } |
| 945 | if (full_checks) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 946 | const char * const *p; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 947 | for (p = FORBIDDEN; *p; p++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 948 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 949 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 950 | return 1; |
| 951 | } |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | return 0; |
| 955 | } |
| 956 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 957 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 958 | |
| 959 | Only sets context for expr kinds that "can appear in assignment context" |
| 960 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 961 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 962 | */ |
| 963 | |
| 964 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 965 | 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] | 966 | { |
| 967 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 968 | /* If a particular expression type can't be used for assign / delete, |
| 969 | set expr_name to its name and an error message will be generated. |
| 970 | */ |
| 971 | const char* expr_name = NULL; |
| 972 | |
| 973 | /* The ast defines augmented store and load contexts, but the |
| 974 | implementation here doesn't actually use them. The code may be |
| 975 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 976 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 977 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 978 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 979 | */ |
| 980 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 981 | |
| 982 | switch (e->kind) { |
| 983 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 984 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 985 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 986 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 987 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 988 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 989 | e->v.Subscript.ctx = ctx; |
| 990 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 991 | case Starred_kind: |
| 992 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 993 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 994 | return 0; |
| 995 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 996 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 997 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 998 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 999 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1000 | } |
| 1001 | e->v.Name.ctx = ctx; |
| 1002 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1003 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1004 | e->v.List.ctx = ctx; |
| 1005 | s = e->v.List.elts; |
| 1006 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1007 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1008 | e->v.Tuple.ctx = ctx; |
| 1009 | s = e->v.Tuple.elts; |
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 Lambda_kind: |
| 1012 | expr_name = "lambda"; |
| 1013 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1014 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1015 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1016 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1017 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1018 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1019 | case UnaryOp_kind: |
| 1020 | expr_name = "operator"; |
| 1021 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1022 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1023 | expr_name = "generator expression"; |
| 1024 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1025 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1026 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1027 | expr_name = "yield expression"; |
| 1028 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1029 | case Await_kind: |
| 1030 | expr_name = "await expression"; |
| 1031 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1032 | case ListComp_kind: |
| 1033 | expr_name = "list comprehension"; |
| 1034 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1035 | case SetComp_kind: |
| 1036 | expr_name = "set comprehension"; |
| 1037 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1038 | case DictComp_kind: |
| 1039 | expr_name = "dict comprehension"; |
| 1040 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1041 | case Dict_kind: |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1042 | case Set_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1043 | case Num_kind: |
| 1044 | case Str_kind: |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 1045 | case Bytes_kind: |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1046 | case JoinedStr_kind: |
| 1047 | case FormattedValue_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1048 | expr_name = "literal"; |
| 1049 | break; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1050 | case NameConstant_kind: |
| 1051 | expr_name = "keyword"; |
| 1052 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1053 | case Ellipsis_kind: |
| 1054 | expr_name = "Ellipsis"; |
| 1055 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1056 | case Compare_kind: |
| 1057 | expr_name = "comparison"; |
| 1058 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1059 | case IfExp_kind: |
| 1060 | expr_name = "conditional expression"; |
| 1061 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1062 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | PyErr_Format(PyExc_SystemError, |
| 1064 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1065 | e->kind, e->lineno); |
| 1066 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1067 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1068 | /* Check for error string set by switch */ |
| 1069 | if (expr_name) { |
| 1070 | char buf[300]; |
| 1071 | PyOS_snprintf(buf, sizeof(buf), |
| 1072 | "can't %s %s", |
| 1073 | ctx == Store ? "assign to" : "delete", |
| 1074 | expr_name); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1075 | return ast_error(c, n, buf); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1078 | /* 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] | 1079 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1080 | */ |
| 1081 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1082 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1083 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1084 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1085 | 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] | 1086 | return 0; |
| 1087 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1088 | } |
| 1089 | return 1; |
| 1090 | } |
| 1091 | |
| 1092 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1093 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1094 | { |
| 1095 | REQ(n, augassign); |
| 1096 | n = CHILD(n, 0); |
| 1097 | switch (STR(n)[0]) { |
| 1098 | case '+': |
| 1099 | return Add; |
| 1100 | case '-': |
| 1101 | return Sub; |
| 1102 | case '/': |
| 1103 | if (STR(n)[1] == '/') |
| 1104 | return FloorDiv; |
| 1105 | else |
| 1106 | return Div; |
| 1107 | case '%': |
| 1108 | return Mod; |
| 1109 | case '<': |
| 1110 | return LShift; |
| 1111 | case '>': |
| 1112 | return RShift; |
| 1113 | case '&': |
| 1114 | return BitAnd; |
| 1115 | case '^': |
| 1116 | return BitXor; |
| 1117 | case '|': |
| 1118 | return BitOr; |
| 1119 | case '*': |
| 1120 | if (STR(n)[1] == '*') |
| 1121 | return Pow; |
| 1122 | else |
| 1123 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1124 | case '@': |
| 1125 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1126 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1127 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1128 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1133 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1134 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1135 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1136 | |'is' 'not' |
| 1137 | */ |
| 1138 | REQ(n, comp_op); |
| 1139 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1140 | n = CHILD(n, 0); |
| 1141 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1142 | case LESS: |
| 1143 | return Lt; |
| 1144 | case GREATER: |
| 1145 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1146 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1147 | return Eq; |
| 1148 | case LESSEQUAL: |
| 1149 | return LtE; |
| 1150 | case GREATEREQUAL: |
| 1151 | return GtE; |
| 1152 | case NOTEQUAL: |
| 1153 | return NotEq; |
| 1154 | case NAME: |
| 1155 | if (strcmp(STR(n), "in") == 0) |
| 1156 | return In; |
| 1157 | if (strcmp(STR(n), "is") == 0) |
| 1158 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1159 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1160 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1161 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1163 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1164 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1165 | } |
| 1166 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1167 | /* handle "not in" and "is not" */ |
| 1168 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1169 | case NAME: |
| 1170 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1171 | return NotIn; |
| 1172 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1173 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1174 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1175 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1176 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1178 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1179 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1180 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1181 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1183 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static asdl_seq * |
| 1187 | seq_for_testlist(struct compiling *c, const node *n) |
| 1188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1190 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1191 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1192 | asdl_seq *seq; |
| 1193 | expr_ty expression; |
| 1194 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1195 | 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] | 1196 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1197 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1198 | if (!seq) |
| 1199 | return NULL; |
| 1200 | |
| 1201 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1203 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1204 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1205 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1206 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1207 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1208 | |
| 1209 | assert(i / 2 < seq->size); |
| 1210 | asdl_seq_SET(seq, i / 2, expression); |
| 1211 | } |
| 1212 | return seq; |
| 1213 | } |
| 1214 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1215 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1216 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1217 | { |
| 1218 | identifier name; |
| 1219 | expr_ty annotation = NULL; |
| 1220 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1221 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1223 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1224 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1225 | name = NEW_IDENTIFIER(ch); |
| 1226 | if (!name) |
| 1227 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1228 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1229 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1230 | |
| 1231 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1232 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1233 | if (!annotation) |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1237 | ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1238 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1239 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1240 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1243 | /* returns -1 if failed to handle keyword only arguments |
| 1244 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1245 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1246 | ^^^ |
| 1247 | start pointing here |
| 1248 | */ |
| 1249 | static int |
| 1250 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1251 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1252 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1253 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1254 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1255 | expr_ty expression, annotation; |
| 1256 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1257 | int i = start; |
| 1258 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1259 | |
| 1260 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1261 | 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] | 1262 | return -1; |
| 1263 | } |
| 1264 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1265 | while (i < NCH(n)) { |
| 1266 | ch = CHILD(n, i); |
| 1267 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1268 | case vfpdef: |
| 1269 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1270 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1271 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1272 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1273 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1274 | asdl_seq_SET(kwdefaults, j, expression); |
| 1275 | i += 2; /* '=' and test */ |
| 1276 | } |
| 1277 | else { /* setting NULL if no default value exists */ |
| 1278 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1279 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1280 | if (NCH(ch) == 3) { |
| 1281 | /* ch is NAME ':' test */ |
| 1282 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1283 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1284 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1285 | } |
| 1286 | else { |
| 1287 | annotation = NULL; |
| 1288 | } |
| 1289 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1290 | argname = NEW_IDENTIFIER(ch); |
| 1291 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1292 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1293 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1294 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1295 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 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; |
| 1489 | |
| 1490 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1491 | |
| 1492 | lineno = LINENO(n); |
| 1493 | col_offset = n->n_col_offset; |
| 1494 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1495 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1496 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1497 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1498 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1499 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1500 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1501 | |
| 1502 | for (i = 2; i < NCH(n); i+=2) { |
| 1503 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1504 | if (!id) |
| 1505 | return NULL; |
| 1506 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1507 | if (!e) |
| 1508 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | static expr_ty |
| 1515 | ast_for_decorator(struct compiling *c, const node *n) |
| 1516 | { |
| 1517 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1518 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1519 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1521 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1522 | REQ(CHILD(n, 0), AT); |
| 1523 | REQ(RCHILD(n, -1), NEWLINE); |
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 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1526 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1527 | return NULL; |
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 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1530 | d = name_expr; |
| 1531 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | } |
| 1533 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1534 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1535 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1536 | if (!d) |
| 1537 | return NULL; |
| 1538 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | else { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 1541 | d = ast_for_call(c, CHILD(n, 3), name_expr, true); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1542 | if (!d) |
| 1543 | return NULL; |
| 1544 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static asdl_seq* |
| 1551 | ast_for_decorators(struct compiling *c, const node *n) |
| 1552 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1553 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1554 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1555 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1557 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1558 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1559 | if (!decorator_seq) |
| 1560 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1563 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1564 | if (!d) |
| 1565 | return NULL; |
| 1566 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1567 | } |
| 1568 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1572 | ast_for_funcdef_impl(struct compiling *c, const node *n, |
| 1573 | asdl_seq *decorator_seq, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1574 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1575 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1576 | identifier name; |
| 1577 | arguments_ty args; |
| 1578 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1579 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1580 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1581 | |
| 1582 | REQ(n, funcdef); |
| 1583 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1584 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1585 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1586 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1587 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1588 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1589 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1590 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1591 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1592 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1593 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1594 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1595 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1596 | name_i += 2; |
| 1597 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 1598 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1599 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1600 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1601 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1602 | if (is_async) |
| 1603 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 1604 | LINENO(n), n->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1605 | else |
| 1606 | return FunctionDef(name, args, body, decorator_seq, returns, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 1607 | LINENO(n), n->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | static stmt_ty |
| 1611 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1612 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1613 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1614 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1615 | REQ(CHILD(n, 0), NAME); |
| 1616 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1617 | REQ(CHILD(n, 1), funcdef); |
| 1618 | |
| 1619 | return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, |
| 1620 | 1 /* is_async */); |
| 1621 | } |
| 1622 | |
| 1623 | static stmt_ty |
| 1624 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1625 | { |
| 1626 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1627 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1628 | 0 /* is_async */); |
| 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | static stmt_ty |
| 1633 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1634 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1635 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1636 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1637 | REQ(CHILD(n, 0), NAME); |
| 1638 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1639 | |
| 1640 | switch (TYPE(CHILD(n, 1))) { |
| 1641 | case funcdef: |
| 1642 | return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, |
| 1643 | 1 /* is_async */); |
| 1644 | case with_stmt: |
| 1645 | return ast_for_with_stmt(c, CHILD(n, 1), |
| 1646 | 1 /* is_async */); |
| 1647 | |
| 1648 | case for_stmt: |
| 1649 | return ast_for_for_stmt(c, CHILD(n, 1), |
| 1650 | 1 /* is_async */); |
| 1651 | |
| 1652 | default: |
| 1653 | PyErr_Format(PyExc_SystemError, |
| 1654 | "invalid async stament: %s", |
| 1655 | STR(CHILD(n, 1))); |
| 1656 | return NULL; |
| 1657 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1660 | static stmt_ty |
| 1661 | ast_for_decorated(struct compiling *c, const node *n) |
| 1662 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1663 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1664 | stmt_ty thing = NULL; |
| 1665 | asdl_seq *decorator_seq = NULL; |
| 1666 | |
| 1667 | REQ(n, decorated); |
| 1668 | |
| 1669 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1670 | if (!decorator_seq) |
| 1671 | return NULL; |
| 1672 | |
| 1673 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1674 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1676 | |
| 1677 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1678 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1679 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1680 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1681 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1682 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1683 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1684 | /* we count the decorators in when talking about the class' or |
| 1685 | * function's line number */ |
| 1686 | if (thing) { |
| 1687 | thing->lineno = LINENO(n); |
| 1688 | thing->col_offset = n->n_col_offset; |
| 1689 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1690 | return thing; |
| 1691 | } |
| 1692 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1693 | static expr_ty |
| 1694 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1695 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1696 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1697 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1698 | arguments_ty args; |
| 1699 | expr_ty expression; |
| 1700 | |
| 1701 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1702 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1703 | if (!args) |
| 1704 | return NULL; |
| 1705 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1706 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1707 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | } |
| 1709 | else { |
| 1710 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1711 | if (!args) |
| 1712 | return NULL; |
| 1713 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1714 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1715 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1718 | return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1721 | static expr_ty |
| 1722 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1723 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1725 | expr_ty expression, body, orelse; |
| 1726 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1727 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1728 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1729 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1730 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1731 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1732 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1733 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1734 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1735 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1736 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1737 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1738 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1741 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1742 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1743 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1744 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1745 | */ |
| 1746 | |
| 1747 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1748 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1749 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1750 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1751 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1752 | count_comp_for: |
| 1753 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1754 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1755 | if (NCH(n) == 2) { |
| 1756 | REQ(CHILD(n, 0), NAME); |
| 1757 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1758 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1759 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1760 | else if (NCH(n) == 1) { |
| 1761 | n = CHILD(n, 0); |
| 1762 | } |
| 1763 | else { |
| 1764 | goto error; |
| 1765 | } |
| 1766 | if (NCH(n) == (5)) { |
| 1767 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1768 | } |
| 1769 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1770 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1771 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1772 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1773 | REQ(n, comp_iter); |
| 1774 | n = CHILD(n, 0); |
| 1775 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1776 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1777 | else if (TYPE(n) == comp_if) { |
| 1778 | if (NCH(n) == 3) { |
| 1779 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1780 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1781 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1782 | else |
| 1783 | return n_fors; |
| 1784 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1785 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1786 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1787 | /* Should never be reached */ |
| 1788 | PyErr_SetString(PyExc_SystemError, |
| 1789 | "logic error in count_comp_fors"); |
| 1790 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1793 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1794 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1795 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1796 | */ |
| 1797 | |
| 1798 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1799 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1800 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1801 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1802 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1803 | while (1) { |
| 1804 | REQ(n, comp_iter); |
| 1805 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1806 | return n_ifs; |
| 1807 | n = CHILD(n, 0); |
| 1808 | REQ(n, comp_if); |
| 1809 | n_ifs++; |
| 1810 | if (NCH(n) == 2) |
| 1811 | return n_ifs; |
| 1812 | n = CHILD(n, 2); |
| 1813 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1816 | static asdl_seq * |
| 1817 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1818 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1820 | asdl_seq *comps; |
| 1821 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1822 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1823 | if (n_fors == -1) |
| 1824 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1826 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1827 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1828 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1829 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1830 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1831 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1832 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1833 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1834 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1835 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1836 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1838 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1840 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1841 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1842 | REQ(CHILD(n, 0), NAME); |
| 1843 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1844 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1845 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1846 | else { |
| 1847 | sync_n = CHILD(n, 0); |
| 1848 | } |
| 1849 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1850 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1851 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1852 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1853 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1854 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1855 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1856 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1857 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1858 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1859 | /* Check the # of children rather than the length of t, since |
| 1860 | (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] | 1861 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1862 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1863 | comp = comprehension(first, expression, NULL, |
| 1864 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1866 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1867 | first->col_offset, c->c_arena), |
| 1868 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1869 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1870 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1871 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1872 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1873 | int j, n_ifs; |
| 1874 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1876 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1877 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1878 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1879 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1880 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1881 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1882 | if (!ifs) |
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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1885 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1886 | REQ(n, comp_iter); |
| 1887 | n = CHILD(n, 0); |
| 1888 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1890 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1891 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1892 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1893 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1894 | if (NCH(n) == 3) |
| 1895 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1896 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1897 | /* on exit, must guarantee that n is a comp_for */ |
| 1898 | if (TYPE(n) == comp_iter) |
| 1899 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1900 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1901 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1902 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1903 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1904 | return comps; |
| 1905 | } |
| 1906 | |
| 1907 | static expr_ty |
| 1908 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1909 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1910 | /* testlist_comp: (test|star_expr) |
| 1911 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1912 | expr_ty elt; |
| 1913 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1914 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1916 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1917 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1918 | ch = CHILD(n, 0); |
| 1919 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1920 | if (!elt) |
| 1921 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1922 | if (elt->kind == Starred_kind) { |
| 1923 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1924 | return NULL; |
| 1925 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1927 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1928 | if (!comps) |
| 1929 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1930 | |
| 1931 | if (type == COMP_GENEXP) |
| 1932 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1933 | else if (type == COMP_LISTCOMP) |
| 1934 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1935 | else if (type == COMP_SETCOMP) |
| 1936 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1937 | else |
| 1938 | /* Should never happen */ |
| 1939 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1942 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1943 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1944 | * elements. Iff successful, nonzero is returned. |
| 1945 | */ |
| 1946 | static int |
| 1947 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1948 | expr_ty *key, expr_ty *value) |
| 1949 | { |
| 1950 | expr_ty expression; |
| 1951 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1952 | assert(NCH(n) - *i >= 2); |
| 1953 | |
| 1954 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1955 | if (!expression) |
| 1956 | return 0; |
| 1957 | *key = NULL; |
| 1958 | *value = expression; |
| 1959 | |
| 1960 | *i += 2; |
| 1961 | } |
| 1962 | else { |
| 1963 | assert(NCH(n) - *i >= 3); |
| 1964 | |
| 1965 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1966 | if (!expression) |
| 1967 | return 0; |
| 1968 | *key = expression; |
| 1969 | |
| 1970 | REQ(CHILD(n, *i + 1), COLON); |
| 1971 | |
| 1972 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1973 | if (!expression) |
| 1974 | return 0; |
| 1975 | *value = expression; |
| 1976 | |
| 1977 | *i += 3; |
| 1978 | } |
| 1979 | return 1; |
| 1980 | } |
| 1981 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1982 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1983 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1984 | { |
| 1985 | expr_ty key, value; |
| 1986 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1987 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1989 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1990 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1991 | assert(key); |
| 1992 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1994 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1995 | if (!comps) |
| 1996 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1998 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1999 | } |
| 2000 | |
| 2001 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2002 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2003 | { |
| 2004 | int i; |
| 2005 | int j; |
| 2006 | int size; |
| 2007 | asdl_seq *keys, *values; |
| 2008 | |
| 2009 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2010 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2011 | if (!keys) |
| 2012 | return NULL; |
| 2013 | |
| 2014 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2015 | if (!values) |
| 2016 | return NULL; |
| 2017 | |
| 2018 | j = 0; |
| 2019 | for (i = 0; i < NCH(n); i++) { |
| 2020 | expr_ty key, value; |
| 2021 | |
| 2022 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2023 | return NULL; |
| 2024 | asdl_seq_SET(keys, j, key); |
| 2025 | asdl_seq_SET(values, j, value); |
| 2026 | |
| 2027 | j++; |
| 2028 | } |
| 2029 | keys->size = j; |
| 2030 | values->size = j; |
| 2031 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2032 | } |
| 2033 | |
| 2034 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2035 | ast_for_genexp(struct compiling *c, const node *n) |
| 2036 | { |
| 2037 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2038 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | static expr_ty |
| 2042 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2043 | { |
| 2044 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2045 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | static expr_ty |
| 2049 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2050 | { |
| 2051 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2052 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2055 | static expr_ty |
| 2056 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2057 | { |
| 2058 | int i; |
| 2059 | int size; |
| 2060 | asdl_seq *elts; |
| 2061 | |
| 2062 | assert(TYPE(n) == (dictorsetmaker)); |
| 2063 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2064 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2065 | if (!elts) |
| 2066 | return NULL; |
| 2067 | for (i = 0; i < NCH(n); i += 2) { |
| 2068 | expr_ty expression; |
| 2069 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2070 | if (!expression) |
| 2071 | return NULL; |
| 2072 | asdl_seq_SET(elts, i / 2, expression); |
| 2073 | } |
| 2074 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2075 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2076 | |
| 2077 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2078 | ast_for_atom(struct compiling *c, const node *n) |
| 2079 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2080 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2081 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2082 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2083 | */ |
| 2084 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2086 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2087 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2088 | PyObject *name; |
| 2089 | const char *s = STR(ch); |
| 2090 | size_t len = strlen(s); |
| 2091 | if (len >= 4 && len <= 5) { |
| 2092 | if (!strcmp(s, "None")) |
| 2093 | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
| 2094 | if (!strcmp(s, "True")) |
| 2095 | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
| 2096 | if (!strcmp(s, "False")) |
| 2097 | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
| 2098 | } |
| 2099 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2100 | if (!name) |
| 2101 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2102 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2103 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2104 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2105 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2106 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2107 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2108 | const char *errtype = NULL; |
| 2109 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2110 | errtype = "unicode error"; |
| 2111 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2112 | errtype = "value error"; |
| 2113 | if (errtype) { |
| 2114 | char buf[128]; |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2115 | const char *s = NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2116 | PyObject *type, *value, *tback, *errstr; |
| 2117 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2118 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2119 | if (errstr) |
| 2120 | s = PyUnicode_AsUTF8(errstr); |
| 2121 | if (s) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2122 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2123 | } else { |
Victor Stinner | 00723e0 | 2015-09-03 12:57:11 +0200 | [diff] [blame] | 2124 | PyErr_Clear(); |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2125 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2126 | } |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2127 | Py_XDECREF(errstr); |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2128 | ast_error(c, n, buf); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2129 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2130 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2131 | Py_XDECREF(tback); |
| 2132 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2133 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2134 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2135 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2136 | } |
| 2137 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2138 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2139 | if (!pynum) |
| 2140 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2141 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2142 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2143 | Py_DECREF(pynum); |
| 2144 | return NULL; |
| 2145 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2146 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2147 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2148 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2149 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2150 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2151 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2153 | if (TYPE(ch) == RPAR) |
| 2154 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2156 | if (TYPE(ch) == yield_expr) |
| 2157 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2160 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2161 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2162 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2163 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2164 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2165 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2167 | if (TYPE(ch) == RSQB) |
| 2168 | return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2170 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2171 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2172 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2173 | if (!elts) |
| 2174 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2175 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2176 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2177 | } |
| 2178 | else |
| 2179 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2180 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2181 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2182 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2183 | * ((test | '*' test) |
| 2184 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2185 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2186 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2187 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2188 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2189 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2190 | } |
| 2191 | else { |
| 2192 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2193 | if (NCH(ch) == 1 || |
| 2194 | (NCH(ch) > 1 && |
| 2195 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2196 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2197 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2198 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2199 | else if (NCH(ch) > 1 && |
| 2200 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2201 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2202 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2203 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2204 | else if (NCH(ch) > 3 - is_dict && |
| 2205 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2206 | /* It's a dictionary comprehension. */ |
| 2207 | if (is_dict) { |
| 2208 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2209 | "dict comprehension"); |
| 2210 | return NULL; |
| 2211 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2212 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2213 | } |
| 2214 | else { |
| 2215 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2216 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2217 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2218 | if (res) { |
| 2219 | res->lineno = LINENO(n); |
| 2220 | res->col_offset = n->n_col_offset; |
| 2221 | } |
| 2222 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2223 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2224 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2225 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2226 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2227 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | static slice_ty |
| 2232 | ast_for_slice(struct compiling *c, const node *n) |
| 2233 | { |
| 2234 | node *ch; |
| 2235 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2236 | |
| 2237 | REQ(n, subscript); |
| 2238 | |
| 2239 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2240 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2241 | sliceop: ':' [test] |
| 2242 | */ |
| 2243 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2244 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2245 | /* 'step' variable hold no significance in terms of being used over |
| 2246 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2248 | if (!step) |
| 2249 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2251 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2255 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2256 | if (!lower) |
| 2257 | return NULL; |
| 2258 | } |
| 2259 | |
| 2260 | /* If there's an upper bound it's in the second or third position. */ |
| 2261 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2262 | if (NCH(n) > 1) { |
| 2263 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2264 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2265 | if (TYPE(n2) == test) { |
| 2266 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2267 | if (!upper) |
| 2268 | return NULL; |
| 2269 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2270 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2271 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2272 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2273 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2274 | if (TYPE(n2) == test) { |
| 2275 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2276 | if (!upper) |
| 2277 | return NULL; |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | ch = CHILD(n, NCH(n) - 1); |
| 2282 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2283 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2284 | ch = CHILD(ch, 1); |
| 2285 | if (TYPE(ch) == test) { |
| 2286 | step = ast_for_expr(c, ch); |
| 2287 | if (!step) |
| 2288 | return NULL; |
| 2289 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2293 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | static expr_ty |
| 2297 | ast_for_binop(struct compiling *c, const node *n) |
| 2298 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2299 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2301 | BinOp(BinOp(A, op, B), op, C). |
| 2302 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2304 | int i, nops; |
| 2305 | expr_ty expr1, expr2, result; |
| 2306 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2307 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2308 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2309 | if (!expr1) |
| 2310 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2311 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2312 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2313 | if (!expr2) |
| 2314 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2315 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2316 | newoperator = get_operator(CHILD(n, 1)); |
| 2317 | if (!newoperator) |
| 2318 | return NULL; |
| 2319 | |
| 2320 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2321 | c->c_arena); |
| 2322 | if (!result) |
| 2323 | return NULL; |
| 2324 | |
| 2325 | nops = (NCH(n) - 1) / 2; |
| 2326 | for (i = 1; i < nops; i++) { |
| 2327 | expr_ty tmp_result, tmp; |
| 2328 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2329 | |
| 2330 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2331 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2332 | return NULL; |
| 2333 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2334 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2335 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2336 | return NULL; |
| 2337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2339 | LINENO(next_oper), next_oper->n_col_offset, |
| 2340 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2342 | return NULL; |
| 2343 | result = tmp_result; |
| 2344 | } |
| 2345 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2348 | static expr_ty |
| 2349 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2350 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2352 | subscriptlist: subscript (',' subscript)* [','] |
| 2353 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2354 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2355 | REQ(n, trailer); |
| 2356 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2357 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2358 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2359 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2360 | else |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2361 | return ast_for_call(c, CHILD(n, 1), left_expr, true); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2362 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2363 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2364 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2365 | if (!attr_id) |
| 2366 | return NULL; |
| 2367 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2368 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2369 | } |
| 2370 | else { |
| 2371 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2372 | REQ(CHILD(n, 2), RSQB); |
| 2373 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2374 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2375 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2376 | if (!slc) |
| 2377 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2378 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2379 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2380 | } |
| 2381 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2383 | by treating the sequence as a tuple literal if there are |
| 2384 | no slice features. |
| 2385 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2386 | int j; |
| 2387 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2388 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2389 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2390 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2391 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2392 | if (!slices) |
| 2393 | return NULL; |
| 2394 | for (j = 0; j < NCH(n); j += 2) { |
| 2395 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2396 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2397 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2398 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2399 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2400 | asdl_seq_SET(slices, j / 2, slc); |
| 2401 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2402 | if (!simple) { |
| 2403 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2404 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2405 | } |
| 2406 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2407 | 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] | 2408 | if (!elts) |
| 2409 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2410 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2411 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2412 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2413 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2414 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2415 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2416 | if (!e) |
| 2417 | return NULL; |
| 2418 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2419 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2420 | } |
| 2421 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2425 | ast_for_factor(struct compiling *c, const node *n) |
| 2426 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2427 | expr_ty expression; |
| 2428 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2429 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2430 | if (!expression) |
| 2431 | return NULL; |
| 2432 | |
| 2433 | switch (TYPE(CHILD(n, 0))) { |
| 2434 | case PLUS: |
| 2435 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2436 | c->c_arena); |
| 2437 | case MINUS: |
| 2438 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2439 | c->c_arena); |
| 2440 | case TILDE: |
| 2441 | return UnaryOp(Invert, expression, LINENO(n), |
| 2442 | n->n_col_offset, c->c_arena); |
| 2443 | } |
| 2444 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2445 | TYPE(CHILD(n, 0))); |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | |
| 2449 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2450 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2451 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2452 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2453 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2454 | |
| 2455 | REQ(n, atom_expr); |
| 2456 | nch = NCH(n); |
| 2457 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2458 | 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] | 2459 | start = 1; |
| 2460 | assert(nch > 1); |
| 2461 | } |
| 2462 | |
| 2463 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2464 | if (!e) |
| 2465 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2466 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2467 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2468 | if (start && nch == 2) { |
| 2469 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2470 | } |
| 2471 | |
| 2472 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2473 | node *ch = CHILD(n, i); |
| 2474 | if (TYPE(ch) != trailer) |
| 2475 | break; |
| 2476 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2477 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2478 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2479 | tmp->lineno = e->lineno; |
| 2480 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2481 | e = tmp; |
| 2482 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2483 | |
| 2484 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2485 | /* there was an 'await' */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2486 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2487 | } |
| 2488 | else { |
| 2489 | return e; |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | static expr_ty |
| 2494 | ast_for_power(struct compiling *c, const node *n) |
| 2495 | { |
| 2496 | /* power: atom trailer* ('**' factor)* |
| 2497 | */ |
| 2498 | expr_ty e; |
| 2499 | REQ(n, power); |
| 2500 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2501 | if (!e) |
| 2502 | return NULL; |
| 2503 | if (NCH(n) == 1) |
| 2504 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2505 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2506 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2507 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2508 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2509 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2510 | } |
| 2511 | return e; |
| 2512 | } |
| 2513 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2514 | static expr_ty |
| 2515 | ast_for_starred(struct compiling *c, const node *n) |
| 2516 | { |
| 2517 | expr_ty tmp; |
| 2518 | REQ(n, star_expr); |
| 2519 | |
| 2520 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2521 | if (!tmp) |
| 2522 | return NULL; |
| 2523 | |
| 2524 | /* The Load context is changed later. */ |
| 2525 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2526 | } |
| 2527 | |
| 2528 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2529 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2530 | */ |
| 2531 | |
| 2532 | static expr_ty |
| 2533 | ast_for_expr(struct compiling *c, const node *n) |
| 2534 | { |
| 2535 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2536 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2537 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2539 | and_test: not_test ('and' not_test)* |
| 2540 | not_test: 'not' not_test | comparison |
| 2541 | comparison: expr (comp_op expr)* |
| 2542 | expr: xor_expr ('|' xor_expr)* |
| 2543 | xor_expr: and_expr ('^' and_expr)* |
| 2544 | and_expr: shift_expr ('&' shift_expr)* |
| 2545 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2546 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2547 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2548 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2549 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2550 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2551 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2552 | */ |
| 2553 | |
| 2554 | asdl_seq *seq; |
| 2555 | int i; |
| 2556 | |
| 2557 | loop: |
| 2558 | switch (TYPE(n)) { |
| 2559 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2560 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2561 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2562 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2563 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2564 | else if (NCH(n) > 1) |
| 2565 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2566 | /* Fallthrough */ |
| 2567 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2568 | case and_test: |
| 2569 | if (NCH(n) == 1) { |
| 2570 | n = CHILD(n, 0); |
| 2571 | goto loop; |
| 2572 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2573 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2574 | if (!seq) |
| 2575 | return NULL; |
| 2576 | for (i = 0; i < NCH(n); i += 2) { |
| 2577 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2578 | if (!e) |
| 2579 | return NULL; |
| 2580 | asdl_seq_SET(seq, i / 2, e); |
| 2581 | } |
| 2582 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2583 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2584 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2585 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2586 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2587 | case not_test: |
| 2588 | if (NCH(n) == 1) { |
| 2589 | n = CHILD(n, 0); |
| 2590 | goto loop; |
| 2591 | } |
| 2592 | else { |
| 2593 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2594 | if (!expression) |
| 2595 | return NULL; |
| 2596 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2597 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2598 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2599 | } |
| 2600 | case comparison: |
| 2601 | if (NCH(n) == 1) { |
| 2602 | n = CHILD(n, 0); |
| 2603 | goto loop; |
| 2604 | } |
| 2605 | else { |
| 2606 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2607 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2608 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2609 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2610 | if (!ops) |
| 2611 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2612 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2613 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2614 | return NULL; |
| 2615 | } |
| 2616 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2617 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2618 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2619 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2620 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2621 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2622 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2623 | |
| 2624 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2625 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2626 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2627 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2629 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2630 | asdl_seq_SET(cmps, i / 2, expression); |
| 2631 | } |
| 2632 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2633 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2634 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2635 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2637 | return Compare(expression, ops, cmps, LINENO(n), |
| 2638 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2639 | } |
| 2640 | break; |
| 2641 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2642 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2644 | /* The next five cases all handle BinOps. The main body of code |
| 2645 | is the same in each case, but the switch turned inside out to |
| 2646 | reuse the code for each type of operator. |
| 2647 | */ |
| 2648 | case expr: |
| 2649 | case xor_expr: |
| 2650 | case and_expr: |
| 2651 | case shift_expr: |
| 2652 | case arith_expr: |
| 2653 | case term: |
| 2654 | if (NCH(n) == 1) { |
| 2655 | n = CHILD(n, 0); |
| 2656 | goto loop; |
| 2657 | } |
| 2658 | return ast_for_binop(c, n); |
| 2659 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2660 | node *an = NULL; |
| 2661 | node *en = NULL; |
| 2662 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2663 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2664 | if (NCH(n) > 1) |
| 2665 | an = CHILD(n, 1); /* yield_arg */ |
| 2666 | if (an) { |
| 2667 | en = CHILD(an, NCH(an) - 1); |
| 2668 | if (NCH(an) == 2) { |
| 2669 | is_from = 1; |
| 2670 | exp = ast_for_expr(c, en); |
| 2671 | } |
| 2672 | else |
| 2673 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2674 | if (!exp) |
| 2675 | return NULL; |
| 2676 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2677 | if (is_from) |
| 2678 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2679 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2680 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2681 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2682 | if (NCH(n) == 1) { |
| 2683 | n = CHILD(n, 0); |
| 2684 | goto loop; |
| 2685 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2686 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2687 | case power: |
| 2688 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2689 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2690 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2691 | return NULL; |
| 2692 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2693 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2694 | return NULL; |
| 2695 | } |
| 2696 | |
| 2697 | static expr_ty |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2698 | ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | { |
| 2700 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2701 | arglist: argument (',' argument)* [','] |
| 2702 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2703 | */ |
| 2704 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2705 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2706 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2707 | asdl_seq *args; |
| 2708 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2709 | |
| 2710 | REQ(n, arglist); |
| 2711 | |
| 2712 | nargs = 0; |
| 2713 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2714 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2715 | node *ch = CHILD(n, i); |
| 2716 | if (TYPE(ch) == argument) { |
| 2717 | if (NCH(ch) == 1) |
| 2718 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2719 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2720 | nargs++; |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2721 | if (!allowgen) { |
| 2722 | ast_error(c, ch, "invalid syntax"); |
| 2723 | return NULL; |
| 2724 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2725 | if (NCH(n) > 1) { |
| 2726 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2727 | return NULL; |
| 2728 | } |
| 2729 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2730 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2731 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2732 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2733 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2734 | nkeywords++; |
| 2735 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2736 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2737 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2738 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2739 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2740 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2741 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2742 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2743 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2744 | |
| 2745 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2746 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2747 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2748 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2749 | node *ch = CHILD(n, i); |
| 2750 | if (TYPE(ch) == argument) { |
| 2751 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2752 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2753 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2754 | /* a positional argument */ |
| 2755 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2756 | if (ndoublestars) { |
| 2757 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2758 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2759 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2760 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2761 | else { |
| 2762 | ast_error(c, chch, |
| 2763 | "positional argument follows " |
| 2764 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2765 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2766 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2767 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2768 | e = ast_for_expr(c, chch); |
| 2769 | if (!e) |
| 2770 | return NULL; |
| 2771 | asdl_seq_SET(args, nargs++, e); |
| 2772 | } |
| 2773 | else if (TYPE(chch) == STAR) { |
| 2774 | /* an iterable argument unpacking */ |
| 2775 | expr_ty starred; |
| 2776 | if (ndoublestars) { |
| 2777 | ast_error(c, chch, |
| 2778 | "iterable argument unpacking follows " |
| 2779 | "keyword argument unpacking"); |
| 2780 | return NULL; |
| 2781 | } |
| 2782 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2783 | if (!e) |
| 2784 | return NULL; |
| 2785 | starred = Starred(e, Load, LINENO(chch), |
| 2786 | chch->n_col_offset, |
| 2787 | c->c_arena); |
| 2788 | if (!starred) |
| 2789 | return NULL; |
| 2790 | asdl_seq_SET(args, nargs++, starred); |
| 2791 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2792 | } |
| 2793 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2794 | /* a keyword argument unpacking */ |
| 2795 | keyword_ty kw; |
| 2796 | i++; |
| 2797 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2798 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2799 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2800 | kw = keyword(NULL, e, c->c_arena); |
| 2801 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2802 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2804 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2805 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2806 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2807 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2808 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2809 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2810 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2811 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2812 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2813 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2814 | identifier key, tmp; |
| 2815 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2816 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2817 | /* chch is test, but must be an identifier? */ |
| 2818 | e = ast_for_expr(c, chch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2819 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2820 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2821 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2822 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 2823 | * SF bug 132313 points out that complaining about a keyword |
| 2824 | * then is very confusing. |
| 2825 | */ |
| 2826 | if (e->kind == Lambda_kind) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2827 | ast_error(c, chch, |
| 2828 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2829 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2830 | } |
| 2831 | else if (e->kind != Name_kind) { |
| 2832 | ast_error(c, chch, |
| 2833 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2834 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2835 | } |
| 2836 | else if (forbidden_name(c, e->v.Name.id, ch, 1)) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2837 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2839 | key = e->v.Name.id; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2840 | for (k = 0; k < nkeywords; k++) { |
| 2841 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2842 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2843 | ast_error(c, chch, |
| 2844 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2845 | return NULL; |
| 2846 | } |
| 2847 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2848 | e = ast_for_expr(c, CHILD(ch, 2)); |
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 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2852 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2853 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2854 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2855 | } |
| 2856 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2859 | return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2863 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2865 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2866 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2867 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2868 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2869 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2870 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2871 | } |
| 2872 | else { |
| 2873 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2874 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2875 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2876 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2877 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2878 | else { |
| 2879 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2880 | if (!tmp) |
| 2881 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2882 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2886 | static stmt_ty |
| 2887 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2888 | { |
| 2889 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2890 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2891 | ('=' (yield_expr|testlist_star_expr))*) |
| 2892 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2893 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2894 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2895 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2896 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2897 | */ |
| 2898 | |
| 2899 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2900 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2901 | if (!e) |
| 2902 | return NULL; |
| 2903 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2904 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2905 | } |
| 2906 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2907 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2908 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2909 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2910 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2911 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2912 | if (!expr1) |
| 2913 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2914 | if(!set_context(c, expr1, Store, ch)) |
| 2915 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2916 | /* set_context checks that most expressions are not the left side. |
| 2917 | Augmented assignments can only have a name, a subscript, or an |
| 2918 | attribute on the left, though, so we have to explicitly check for |
| 2919 | those. */ |
| 2920 | switch (expr1->kind) { |
| 2921 | case Name_kind: |
| 2922 | case Attribute_kind: |
| 2923 | case Subscript_kind: |
| 2924 | break; |
| 2925 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2926 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2927 | return NULL; |
| 2928 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2929 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2930 | ch = CHILD(n, 2); |
| 2931 | if (TYPE(ch) == testlist) |
| 2932 | expr2 = ast_for_testlist(c, ch); |
| 2933 | else |
| 2934 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2935 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2936 | return NULL; |
| 2937 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2938 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2939 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2940 | return NULL; |
| 2941 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2942 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2944 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2945 | expr_ty expr1, expr2, expr3; |
| 2946 | node *ch = CHILD(n, 0); |
| 2947 | node *deep, *ann = CHILD(n, 1); |
| 2948 | int simple = 1; |
| 2949 | |
| 2950 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2951 | deep = ch; |
| 2952 | while (NCH(deep) == 1) { |
| 2953 | deep = CHILD(deep, 0); |
| 2954 | } |
| 2955 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2956 | simple = 0; |
| 2957 | } |
| 2958 | expr1 = ast_for_testlist(c, ch); |
| 2959 | if (!expr1) { |
| 2960 | return NULL; |
| 2961 | } |
| 2962 | switch (expr1->kind) { |
| 2963 | case Name_kind: |
| 2964 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2965 | return NULL; |
| 2966 | } |
| 2967 | expr1->v.Name.ctx = Store; |
| 2968 | break; |
| 2969 | case Attribute_kind: |
| 2970 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2971 | return NULL; |
| 2972 | } |
| 2973 | expr1->v.Attribute.ctx = Store; |
| 2974 | break; |
| 2975 | case Subscript_kind: |
| 2976 | expr1->v.Subscript.ctx = Store; |
| 2977 | break; |
| 2978 | case List_kind: |
| 2979 | ast_error(c, ch, |
| 2980 | "only single target (not list) can be annotated"); |
| 2981 | return NULL; |
| 2982 | case Tuple_kind: |
| 2983 | ast_error(c, ch, |
| 2984 | "only single target (not tuple) can be annotated"); |
| 2985 | return NULL; |
| 2986 | default: |
| 2987 | ast_error(c, ch, |
| 2988 | "illegal target for annotation"); |
| 2989 | return NULL; |
| 2990 | } |
| 2991 | |
| 2992 | if (expr1->kind != Name_kind) { |
| 2993 | simple = 0; |
| 2994 | } |
| 2995 | ch = CHILD(ann, 1); |
| 2996 | expr2 = ast_for_expr(c, ch); |
| 2997 | if (!expr2) { |
| 2998 | return NULL; |
| 2999 | } |
| 3000 | if (NCH(ann) == 2) { |
| 3001 | return AnnAssign(expr1, expr2, NULL, simple, |
| 3002 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3003 | } |
| 3004 | else { |
| 3005 | ch = CHILD(ann, 3); |
| 3006 | expr3 = ast_for_expr(c, ch); |
| 3007 | if (!expr3) { |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3011 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3012 | } |
| 3013 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3014 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3015 | int i; |
| 3016 | asdl_seq *targets; |
| 3017 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3018 | expr_ty expression; |
| 3019 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3020 | /* a normal assignment */ |
| 3021 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3022 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3023 | if (!targets) |
| 3024 | return NULL; |
| 3025 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3026 | expr_ty e; |
| 3027 | node *ch = CHILD(n, i); |
| 3028 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3029 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3030 | return NULL; |
| 3031 | } |
| 3032 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3034 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3035 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3036 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3037 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3038 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3039 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3040 | asdl_seq_SET(targets, i / 2, e); |
| 3041 | } |
| 3042 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3043 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3044 | expression = ast_for_testlist(c, value); |
| 3045 | else |
| 3046 | expression = ast_for_expr(c, value); |
| 3047 | if (!expression) |
| 3048 | return NULL; |
| 3049 | return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3053 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3054 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3055 | 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] | 3056 | { |
| 3057 | asdl_seq *seq; |
| 3058 | int i; |
| 3059 | expr_ty e; |
| 3060 | |
| 3061 | REQ(n, exprlist); |
| 3062 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3063 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3064 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3065 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3066 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3067 | e = ast_for_expr(c, CHILD(n, i)); |
| 3068 | if (!e) |
| 3069 | return NULL; |
| 3070 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3071 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3072 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3073 | } |
| 3074 | return seq; |
| 3075 | } |
| 3076 | |
| 3077 | static stmt_ty |
| 3078 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3079 | { |
| 3080 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3082 | /* del_stmt: 'del' exprlist */ |
| 3083 | REQ(n, del_stmt); |
| 3084 | |
| 3085 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3086 | if (!expr_list) |
| 3087 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3088 | return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | static stmt_ty |
| 3092 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3093 | { |
| 3094 | /* |
| 3095 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3096 | | yield_stmt |
| 3097 | break_stmt: 'break' |
| 3098 | continue_stmt: 'continue' |
| 3099 | return_stmt: 'return' [testlist] |
| 3100 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3101 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3102 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3103 | */ |
| 3104 | node *ch; |
| 3105 | |
| 3106 | REQ(n, flow_stmt); |
| 3107 | ch = CHILD(n, 0); |
| 3108 | switch (TYPE(ch)) { |
| 3109 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3110 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3111 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3112 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3113 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3114 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3115 | if (!exp) |
| 3116 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3117 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3118 | } |
| 3119 | case return_stmt: |
| 3120 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3121 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3122 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3123 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3124 | if (!expression) |
| 3125 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3126 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3127 | } |
| 3128 | case raise_stmt: |
| 3129 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3130 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3131 | else if (NCH(ch) >= 2) { |
| 3132 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3133 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3134 | if (!expression) |
| 3135 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3136 | if (NCH(ch) == 4) { |
| 3137 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3138 | if (!cause) |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3142 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3143 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3144 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3145 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3146 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3152 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3153 | { |
| 3154 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3155 | import_as_name: NAME ['as' NAME] |
| 3156 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3157 | dotted_name: NAME ('.' NAME)* |
| 3158 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3159 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3160 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3161 | loop: |
| 3162 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3163 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3164 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3165 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3166 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3167 | if (!name) |
| 3168 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3169 | if (NCH(n) == 3) { |
| 3170 | node *str_node = CHILD(n, 2); |
| 3171 | str = NEW_IDENTIFIER(str_node); |
| 3172 | if (!str) |
| 3173 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3174 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3175 | return NULL; |
| 3176 | } |
| 3177 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3178 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3179 | return NULL; |
| 3180 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3181 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3182 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3183 | case dotted_as_name: |
| 3184 | if (NCH(n) == 1) { |
| 3185 | n = CHILD(n, 0); |
| 3186 | goto loop; |
| 3187 | } |
| 3188 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3189 | node *asname_node = CHILD(n, 2); |
| 3190 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3191 | if (!a) |
| 3192 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3193 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3194 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3195 | if (!a->asname) |
| 3196 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3197 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3198 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3199 | return a; |
| 3200 | } |
| 3201 | break; |
| 3202 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3203 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3204 | node *name_node = CHILD(n, 0); |
| 3205 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3206 | if (!name) |
| 3207 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3208 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3209 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3210 | return alias(name, NULL, c->c_arena); |
| 3211 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3212 | else { |
| 3213 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3214 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3215 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3216 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3218 | |
| 3219 | len = 0; |
| 3220 | for (i = 0; i < NCH(n); i += 2) |
| 3221 | /* length of string plus one for the dot */ |
| 3222 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3223 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3224 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3225 | if (!str) |
| 3226 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3227 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3228 | if (!s) |
| 3229 | return NULL; |
| 3230 | for (i = 0; i < NCH(n); i += 2) { |
| 3231 | char *sch = STR(CHILD(n, i)); |
| 3232 | strcpy(s, STR(CHILD(n, i))); |
| 3233 | s += strlen(sch); |
| 3234 | *s++ = '.'; |
| 3235 | } |
| 3236 | --s; |
| 3237 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3239 | PyBytes_GET_SIZE(str), |
| 3240 | NULL); |
| 3241 | Py_DECREF(str); |
| 3242 | if (!uni) |
| 3243 | return NULL; |
| 3244 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3245 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3246 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3247 | Py_DECREF(str); |
| 3248 | return NULL; |
| 3249 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3250 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3251 | } |
| 3252 | break; |
| 3253 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3254 | str = PyUnicode_InternFromString("*"); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3255 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3256 | Py_DECREF(str); |
| 3257 | return NULL; |
| 3258 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3259 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3260 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3261 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3262 | "unexpected import name: %d", TYPE(n)); |
| 3263 | return NULL; |
| 3264 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3265 | |
| 3266 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3267 | return NULL; |
| 3268 | } |
| 3269 | |
| 3270 | static stmt_ty |
| 3271 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3272 | { |
| 3273 | /* |
| 3274 | import_stmt: import_name | import_from |
| 3275 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3276 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3277 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3278 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3279 | int lineno; |
| 3280 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3281 | int i; |
| 3282 | asdl_seq *aliases; |
| 3283 | |
| 3284 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3285 | lineno = LINENO(n); |
| 3286 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3287 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3288 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3289 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3290 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3291 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3292 | if (!aliases) |
| 3293 | return NULL; |
| 3294 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3295 | 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] | 3296 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3297 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3298 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3299 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3300 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3301 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3302 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3304 | int idx, ndots = 0; |
| 3305 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3306 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3308 | /* Count the number of dots (for relative imports) and check for the |
| 3309 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3310 | for (idx = 1; idx < NCH(n); idx++) { |
| 3311 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3312 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3313 | if (!mod) |
| 3314 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3315 | idx++; |
| 3316 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3317 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3318 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3319 | ndots += 3; |
| 3320 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3321 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3322 | break; |
| 3323 | } |
| 3324 | ndots++; |
| 3325 | } |
| 3326 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3327 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3328 | case STAR: |
| 3329 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3330 | n = CHILD(n, idx); |
| 3331 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3332 | break; |
| 3333 | case LPAR: |
| 3334 | /* from ... import (x, y, z) */ |
| 3335 | n = CHILD(n, idx + 1); |
| 3336 | n_children = NCH(n); |
| 3337 | break; |
| 3338 | case import_as_names: |
| 3339 | /* from ... import x, y, z */ |
| 3340 | n = CHILD(n, idx); |
| 3341 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3342 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3343 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3344 | " surrounding parentheses"); |
| 3345 | return NULL; |
| 3346 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3347 | break; |
| 3348 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3349 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3350 | return NULL; |
| 3351 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3352 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3353 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3354 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3355 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | |
| 3357 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3358 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3359 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3360 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3361 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3362 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3363 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3364 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3365 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3366 | 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] | 3367 | if (!import_alias) |
| 3368 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3369 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3370 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3371 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3372 | if (mod != NULL) |
| 3373 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3374 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3375 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3376 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3377 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3378 | "unknown import statement: starts with command '%s'", |
| 3379 | STR(CHILD(n, 0))); |
| 3380 | return NULL; |
| 3381 | } |
| 3382 | |
| 3383 | static stmt_ty |
| 3384 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3385 | { |
| 3386 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3387 | identifier name; |
| 3388 | asdl_seq *s; |
| 3389 | int i; |
| 3390 | |
| 3391 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3392 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3393 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3394 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3395 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3396 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3397 | if (!name) |
| 3398 | return NULL; |
| 3399 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3400 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3401 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3405 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3406 | { |
| 3407 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3408 | identifier name; |
| 3409 | asdl_seq *s; |
| 3410 | int i; |
| 3411 | |
| 3412 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3413 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3414 | if (!s) |
| 3415 | return NULL; |
| 3416 | for (i = 1; i < NCH(n); i += 2) { |
| 3417 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3418 | if (!name) |
| 3419 | return NULL; |
| 3420 | asdl_seq_SET(s, i / 2, name); |
| 3421 | } |
| 3422 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3423 | } |
| 3424 | |
| 3425 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3426 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3427 | { |
| 3428 | /* assert_stmt: 'assert' test [',' test] */ |
| 3429 | REQ(n, assert_stmt); |
| 3430 | if (NCH(n) == 2) { |
| 3431 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3432 | if (!expression) |
| 3433 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3434 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3435 | } |
| 3436 | else if (NCH(n) == 4) { |
| 3437 | expr_ty expr1, expr2; |
| 3438 | |
| 3439 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3440 | if (!expr1) |
| 3441 | return NULL; |
| 3442 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3443 | if (!expr2) |
| 3444 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3446 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3447 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3448 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3449 | "improper number of parts to 'assert' statement: %d", |
| 3450 | NCH(n)); |
| 3451 | return NULL; |
| 3452 | } |
| 3453 | |
| 3454 | static asdl_seq * |
| 3455 | ast_for_suite(struct compiling *c, const node *n) |
| 3456 | { |
| 3457 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3458 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3459 | stmt_ty s; |
| 3460 | int i, total, num, end, pos = 0; |
| 3461 | node *ch; |
| 3462 | |
| 3463 | REQ(n, suite); |
| 3464 | |
| 3465 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3466 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3468 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3469 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3470 | n = CHILD(n, 0); |
| 3471 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3473 | */ |
| 3474 | end = NCH(n) - 1; |
| 3475 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3476 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3478 | for (i = 0; i < end; i += 2) { |
| 3479 | ch = CHILD(n, i); |
| 3480 | s = ast_for_stmt(c, ch); |
| 3481 | if (!s) |
| 3482 | return NULL; |
| 3483 | asdl_seq_SET(seq, pos++, s); |
| 3484 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3485 | } |
| 3486 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3487 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3488 | ch = CHILD(n, i); |
| 3489 | REQ(ch, stmt); |
| 3490 | num = num_stmts(ch); |
| 3491 | if (num == 1) { |
| 3492 | /* small_stmt or compound_stmt with only one child */ |
| 3493 | s = ast_for_stmt(c, ch); |
| 3494 | if (!s) |
| 3495 | return NULL; |
| 3496 | asdl_seq_SET(seq, pos++, s); |
| 3497 | } |
| 3498 | else { |
| 3499 | int j; |
| 3500 | ch = CHILD(ch, 0); |
| 3501 | REQ(ch, simple_stmt); |
| 3502 | for (j = 0; j < NCH(ch); j += 2) { |
| 3503 | /* statement terminates with a semi-colon ';' */ |
| 3504 | if (NCH(CHILD(ch, j)) == 0) { |
| 3505 | assert((j + 1) == NCH(ch)); |
| 3506 | break; |
| 3507 | } |
| 3508 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3509 | if (!s) |
| 3510 | return NULL; |
| 3511 | asdl_seq_SET(seq, pos++, s); |
| 3512 | } |
| 3513 | } |
| 3514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3515 | } |
| 3516 | assert(pos == seq->size); |
| 3517 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | static stmt_ty |
| 3521 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3522 | { |
| 3523 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3524 | ['else' ':' suite] |
| 3525 | */ |
| 3526 | char *s; |
| 3527 | |
| 3528 | REQ(n, if_stmt); |
| 3529 | |
| 3530 | if (NCH(n) == 4) { |
| 3531 | expr_ty expression; |
| 3532 | asdl_seq *suite_seq; |
| 3533 | |
| 3534 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3535 | if (!expression) |
| 3536 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3538 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3541 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3542 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3543 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3544 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | s = STR(CHILD(n, 4)); |
| 3546 | /* s[2], the third character in the string, will be |
| 3547 | 's' for el_s_e, or |
| 3548 | 'i' for el_i_f |
| 3549 | */ |
| 3550 | if (s[2] == 's') { |
| 3551 | expr_ty expression; |
| 3552 | asdl_seq *seq1, *seq2; |
| 3553 | |
| 3554 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3555 | if (!expression) |
| 3556 | return NULL; |
| 3557 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3558 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3559 | return NULL; |
| 3560 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3561 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3562 | return NULL; |
| 3563 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3564 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3565 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3566 | } |
| 3567 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3568 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3569 | expr_ty expression; |
| 3570 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3571 | asdl_seq *orelse = NULL; |
| 3572 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3573 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3574 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3575 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3576 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3577 | has_else = 1; |
| 3578 | n_elif -= 3; |
| 3579 | } |
| 3580 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3581 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3582 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3583 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3585 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3586 | if (!orelse) |
| 3587 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3588 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3589 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3590 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3591 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3592 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3593 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3594 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3595 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3596 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | asdl_seq_SET(orelse, 0, |
| 3599 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3600 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3601 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3602 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3603 | /* the just-created orelse handled the last elif */ |
| 3604 | n_elif--; |
| 3605 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3607 | for (i = 0; i < n_elif; i++) { |
| 3608 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3609 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3610 | if (!newobj) |
| 3611 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3612 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3613 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3614 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3615 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3616 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3618 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3619 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3620 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3621 | LINENO(CHILD(n, off)), |
| 3622 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3623 | orelse = newobj; |
| 3624 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3625 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3626 | if (!expression) |
| 3627 | return NULL; |
| 3628 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3629 | if (!suite_seq) |
| 3630 | return NULL; |
| 3631 | return If(expression, suite_seq, orelse, |
| 3632 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3633 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3634 | |
| 3635 | PyErr_Format(PyExc_SystemError, |
| 3636 | "unexpected token in 'if' statement: %s", s); |
| 3637 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | static stmt_ty |
| 3641 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3642 | { |
| 3643 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3644 | REQ(n, while_stmt); |
| 3645 | |
| 3646 | if (NCH(n) == 4) { |
| 3647 | expr_ty expression; |
| 3648 | asdl_seq *suite_seq; |
| 3649 | |
| 3650 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3651 | if (!expression) |
| 3652 | return NULL; |
| 3653 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3654 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3655 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3656 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3657 | } |
| 3658 | else if (NCH(n) == 7) { |
| 3659 | expr_ty expression; |
| 3660 | asdl_seq *seq1, *seq2; |
| 3661 | |
| 3662 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3663 | if (!expression) |
| 3664 | return NULL; |
| 3665 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3666 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3667 | return NULL; |
| 3668 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3669 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3670 | return NULL; |
| 3671 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3672 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3673 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3674 | |
| 3675 | PyErr_Format(PyExc_SystemError, |
| 3676 | "wrong number of tokens for 'while' statement: %d", |
| 3677 | NCH(n)); |
| 3678 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3682 | ast_for_for_stmt(struct compiling *c, const node *n, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3683 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3684 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3685 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3686 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3687 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3688 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3689 | REQ(n, for_stmt); |
| 3690 | |
| 3691 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3692 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3693 | if (!seq) |
| 3694 | return NULL; |
| 3695 | } |
| 3696 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3697 | node_target = CHILD(n, 1); |
| 3698 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3699 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3701 | /* Check the # of children rather than the length of _target, since |
| 3702 | 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] | 3703 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3704 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3705 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3706 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3707 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3708 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3709 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3710 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | return NULL; |
| 3712 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3713 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3714 | return NULL; |
| 3715 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3716 | if (is_async) |
| 3717 | return AsyncFor(target, expression, suite_seq, seq, |
| 3718 | LINENO(n), n->n_col_offset, |
| 3719 | c->c_arena); |
| 3720 | else |
| 3721 | return For(target, expression, suite_seq, seq, |
| 3722 | LINENO(n), n->n_col_offset, |
| 3723 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | static excepthandler_ty |
| 3727 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3728 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3729 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3730 | REQ(exc, except_clause); |
| 3731 | REQ(body, suite); |
| 3732 | |
| 3733 | if (NCH(exc) == 1) { |
| 3734 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3735 | if (!suite_seq) |
| 3736 | return NULL; |
| 3737 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3738 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3739 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3740 | } |
| 3741 | else if (NCH(exc) == 2) { |
| 3742 | expr_ty expression; |
| 3743 | asdl_seq *suite_seq; |
| 3744 | |
| 3745 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3746 | if (!expression) |
| 3747 | return NULL; |
| 3748 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3749 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3750 | return NULL; |
| 3751 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3752 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3753 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3754 | } |
| 3755 | else if (NCH(exc) == 4) { |
| 3756 | asdl_seq *suite_seq; |
| 3757 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3758 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3759 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3760 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3761 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3762 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3763 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3764 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3765 | return NULL; |
| 3766 | suite_seq = ast_for_suite(c, body); |
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; |
| 3769 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3770 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3771 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3772 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3773 | |
| 3774 | PyErr_Format(PyExc_SystemError, |
| 3775 | "wrong number of children for 'except' clause: %d", |
| 3776 | NCH(exc)); |
| 3777 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | static stmt_ty |
| 3781 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3782 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3783 | const int nch = NCH(n); |
| 3784 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3785 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3786 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3787 | REQ(n, try_stmt); |
| 3788 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3789 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3790 | if (body == NULL) |
| 3791 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3792 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3793 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3794 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3795 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3796 | /* we can assume it's an "else", |
| 3797 | because nch >= 9 for try-else-finally and |
| 3798 | it would otherwise have a type of except_clause */ |
| 3799 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3800 | if (orelse == NULL) |
| 3801 | return NULL; |
| 3802 | n_except--; |
| 3803 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3804 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3805 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3806 | if (finally == NULL) |
| 3807 | return NULL; |
| 3808 | n_except--; |
| 3809 | } |
| 3810 | else { |
| 3811 | /* we can assume it's an "else", |
| 3812 | otherwise it would have a type of except_clause */ |
| 3813 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3814 | if (orelse == NULL) |
| 3815 | return NULL; |
| 3816 | n_except--; |
| 3817 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3818 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3819 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3820 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3821 | return NULL; |
| 3822 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3824 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3825 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3826 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3827 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3828 | if (handlers == NULL) |
| 3829 | return NULL; |
| 3830 | |
| 3831 | for (i = 0; i < n_except; i++) { |
| 3832 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3833 | CHILD(n, 5 + i * 3)); |
| 3834 | if (!e) |
| 3835 | return NULL; |
| 3836 | asdl_seq_SET(handlers, i, e); |
| 3837 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3840 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3841 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3844 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3845 | static withitem_ty |
| 3846 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3847 | { |
| 3848 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3849 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3850 | REQ(n, with_item); |
| 3851 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3852 | if (!context_expr) |
| 3853 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3854 | if (NCH(n) == 3) { |
| 3855 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3856 | |
| 3857 | if (!optional_vars) { |
| 3858 | return NULL; |
| 3859 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3860 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3861 | return NULL; |
| 3862 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3865 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3868 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3869 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3870 | ast_for_with_stmt(struct compiling *c, const node *n, int is_async) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3871 | { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3872 | int i, n_items; |
| 3873 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3874 | |
| 3875 | REQ(n, with_stmt); |
| 3876 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3877 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3878 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3879 | if (!items) |
| 3880 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3881 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3882 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3883 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3884 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3885 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3888 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3889 | if (!body) |
| 3890 | return NULL; |
| 3891 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3892 | if (is_async) |
| 3893 | return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
| 3894 | else |
| 3895 | return With(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3898 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3899 | 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] | 3900 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3901 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3902 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3903 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3904 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3905 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3906 | REQ(n, classdef); |
| 3907 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3908 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 3909 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3910 | if (!s) |
| 3911 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3912 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3913 | if (!classname) |
| 3914 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3915 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3916 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 3917 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3918 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3919 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3920 | |
| 3921 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 3922 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3923 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3924 | return NULL; |
| 3925 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3926 | if (!classname) |
| 3927 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3928 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3929 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 3930 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3931 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3934 | /* class NAME '(' arglist ')' ':' suite */ |
| 3935 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3936 | { |
| 3937 | PyObject *dummy_name; |
| 3938 | expr_ty dummy; |
| 3939 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3940 | if (!dummy_name) |
| 3941 | return NULL; |
| 3942 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 3943 | call = ast_for_call(c, CHILD(n, 3), dummy, false); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3944 | if (!call) |
| 3945 | return NULL; |
| 3946 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 3947 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3948 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3949 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3950 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3951 | if (!classname) |
| 3952 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3953 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3954 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3955 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3956 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame^] | 3957 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | static stmt_ty |
| 3961 | ast_for_stmt(struct compiling *c, const node *n) |
| 3962 | { |
| 3963 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3964 | assert(NCH(n) == 1); |
| 3965 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3966 | } |
| 3967 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3968 | assert(num_stmts(n) == 1); |
| 3969 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3970 | } |
| 3971 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3972 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3973 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3974 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3975 | */ |
| 3976 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3977 | case expr_stmt: |
| 3978 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3979 | case del_stmt: |
| 3980 | return ast_for_del_stmt(c, n); |
| 3981 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3982 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3983 | case flow_stmt: |
| 3984 | return ast_for_flow_stmt(c, n); |
| 3985 | case import_stmt: |
| 3986 | return ast_for_import_stmt(c, n); |
| 3987 | case global_stmt: |
| 3988 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3989 | case nonlocal_stmt: |
| 3990 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3991 | case assert_stmt: |
| 3992 | return ast_for_assert_stmt(c, n); |
| 3993 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3994 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3995 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3996 | TYPE(n), NCH(n)); |
| 3997 | return NULL; |
| 3998 | } |
| 3999 | } |
| 4000 | else { |
| 4001 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4002 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4003 | */ |
| 4004 | node *ch = CHILD(n, 0); |
| 4005 | REQ(n, compound_stmt); |
| 4006 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4007 | case if_stmt: |
| 4008 | return ast_for_if_stmt(c, ch); |
| 4009 | case while_stmt: |
| 4010 | return ast_for_while_stmt(c, ch); |
| 4011 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4012 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4013 | case try_stmt: |
| 4014 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4015 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4016 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4017 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4018 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4019 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4020 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4021 | case decorated: |
| 4022 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4023 | case async_stmt: |
| 4024 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4025 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4026 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4027 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4028 | TYPE(n), NCH(n)); |
| 4029 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4030 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4035 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4036 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4037 | const char *end; |
| 4038 | long x; |
| 4039 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4040 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4041 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4042 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4043 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4044 | errno = 0; |
| 4045 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4046 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4047 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4048 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4049 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4050 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4051 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4052 | } |
| 4053 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4054 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4055 | if (*end == '\0') { |
| 4056 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4057 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4058 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4059 | } |
| 4060 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4061 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4062 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4063 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4064 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4065 | return NULL; |
| 4066 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4067 | } |
| 4068 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4069 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4070 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4071 | if (dx == -1.0 && PyErr_Occurred()) |
| 4072 | return NULL; |
| 4073 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4074 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4078 | parsenumber(struct compiling *c, const char *s) |
| 4079 | { |
| 4080 | char *dup, *end; |
| 4081 | PyObject *res = NULL; |
| 4082 | |
| 4083 | assert(s != NULL); |
| 4084 | |
| 4085 | if (strchr(s, '_') == NULL) { |
| 4086 | return parsenumber_raw(c, s); |
| 4087 | } |
| 4088 | /* Create a duplicate without underscores. */ |
| 4089 | dup = PyMem_Malloc(strlen(s) + 1); |
| 4090 | end = dup; |
| 4091 | for (; *s; s++) { |
| 4092 | if (*s != '_') { |
| 4093 | *end++ = *s; |
| 4094 | } |
| 4095 | } |
| 4096 | *end = '\0'; |
| 4097 | res = parsenumber_raw(c, dup); |
| 4098 | PyMem_Free(dup); |
| 4099 | return res; |
| 4100 | } |
| 4101 | |
| 4102 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4103 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4104 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4105 | const char *s, *t; |
| 4106 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4107 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4108 | while (s < end && (*s & 0x80)) s++; |
| 4109 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4110 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4113 | static int |
| 4114 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4115 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4116 | { |
| 4117 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4118 | first_invalid_escape_char); |
| 4119 | if (msg == NULL) { |
| 4120 | return -1; |
| 4121 | } |
| 4122 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
| 4123 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4124 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4125 | { |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4126 | if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) { |
| 4127 | const char *s; |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4128 | |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4129 | /* Replace the DeprecationWarning exception with a SyntaxError |
| 4130 | to get a more accurate error report */ |
| 4131 | PyErr_Clear(); |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4132 | |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4133 | s = PyUnicode_AsUTF8(msg); |
| 4134 | if (s != NULL) { |
| 4135 | ast_error(c, n, s); |
| 4136 | } |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4137 | } |
| 4138 | Py_DECREF(msg); |
| 4139 | return -1; |
| 4140 | } |
| 4141 | Py_DECREF(msg); |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4145 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4146 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4147 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4148 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4149 | PyObject *v, *u; |
| 4150 | char *buf; |
| 4151 | char *p; |
| 4152 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4153 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4154 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4155 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4156 | return NULL; |
| 4157 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4158 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4159 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4160 | if (u == NULL) |
| 4161 | return NULL; |
| 4162 | p = buf = PyBytes_AsString(u); |
| 4163 | end = s + len; |
| 4164 | while (s < end) { |
| 4165 | if (*s == '\\') { |
| 4166 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4167 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4168 | strcpy(p, "u005c"); |
| 4169 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4170 | if (s >= end) |
| 4171 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4172 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4173 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4174 | if (*s & 0x80) { /* XXX inefficient */ |
| 4175 | PyObject *w; |
| 4176 | int kind; |
| 4177 | void *data; |
| 4178 | Py_ssize_t len, i; |
| 4179 | w = decode_utf8(c, &s, end); |
| 4180 | if (w == NULL) { |
| 4181 | Py_DECREF(u); |
| 4182 | return NULL; |
| 4183 | } |
| 4184 | kind = PyUnicode_KIND(w); |
| 4185 | data = PyUnicode_DATA(w); |
| 4186 | len = PyUnicode_GET_LENGTH(w); |
| 4187 | for (i = 0; i < len; i++) { |
| 4188 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4189 | sprintf(p, "\\U%08x", chr); |
| 4190 | p += 10; |
| 4191 | } |
| 4192 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4193 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4194 | Py_DECREF(w); |
| 4195 | } else { |
| 4196 | *p++ = *s++; |
| 4197 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4198 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4199 | len = p - buf; |
| 4200 | s = buf; |
| 4201 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4202 | const char *first_invalid_escape; |
| 4203 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4204 | |
| 4205 | if (v != NULL && first_invalid_escape != NULL) { |
| 4206 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4207 | /* We have not decref u before because first_invalid_escape points |
| 4208 | inside u. */ |
| 4209 | Py_XDECREF(u); |
| 4210 | Py_DECREF(v); |
| 4211 | return NULL; |
| 4212 | } |
| 4213 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4214 | Py_XDECREF(u); |
| 4215 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4218 | static PyObject * |
| 4219 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4220 | size_t len) |
| 4221 | { |
| 4222 | const char *first_invalid_escape; |
| 4223 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4224 | &first_invalid_escape); |
| 4225 | if (result == NULL) |
| 4226 | return NULL; |
| 4227 | |
| 4228 | if (first_invalid_escape != NULL) { |
| 4229 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4230 | Py_DECREF(result); |
| 4231 | return NULL; |
| 4232 | } |
| 4233 | } |
| 4234 | return result; |
| 4235 | } |
| 4236 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4237 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4238 | and `col_offset` to existing locations. */ |
| 4239 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4240 | { |
| 4241 | n->n_col_offset = n->n_col_offset + col_offset; |
| 4242 | for (int i = 0; i < NCH(n); ++i) { |
| 4243 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4244 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4245 | col_offset = 0; |
| 4246 | } |
| 4247 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4248 | } |
| 4249 | n->n_lineno = n->n_lineno + lineno; |
| 4250 | } |
| 4251 | |
| 4252 | /* Fix locations for the given node and its children. |
| 4253 | |
| 4254 | `parent` is the enclosing node. |
| 4255 | `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] | 4256 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4257 | */ |
| 4258 | static void |
| 4259 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4260 | { |
| 4261 | char *substr = NULL; |
| 4262 | char *start; |
| 4263 | int lines = LINENO(parent) - 1; |
| 4264 | int cols = parent->n_col_offset; |
| 4265 | /* Find the full fstring to fix location information in `n`. */ |
| 4266 | while (parent && parent->n_type != STRING) |
| 4267 | parent = parent->n_child; |
| 4268 | if (parent && parent->n_str) { |
| 4269 | substr = strstr(parent->n_str, expr_str); |
| 4270 | if (substr) { |
| 4271 | start = substr; |
| 4272 | while (start > parent->n_str) { |
| 4273 | if (start[0] == '\n') |
| 4274 | break; |
| 4275 | start--; |
| 4276 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4277 | cols += (int)(substr - start); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4278 | /* Fix lineno in mulitline strings. */ |
| 4279 | while ((substr = strchr(substr + 1, '\n'))) |
| 4280 | lines--; |
| 4281 | } |
| 4282 | } |
| 4283 | fstring_shift_node_locations(n, lines, cols); |
| 4284 | } |
| 4285 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4286 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4287 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4288 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4289 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4290 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4291 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4292 | { |
| 4293 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4294 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4295 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4296 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4297 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4298 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4299 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4300 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4301 | assert(*(expr_start-1) == '{'); |
| 4302 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4303 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4304 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4305 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4306 | because turning the expression '' in to '()' would go from being invalid |
| 4307 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4308 | for (s = expr_start; s != expr_end; s++) { |
| 4309 | char c = *s; |
| 4310 | /* The Python parser ignores only the following whitespace |
| 4311 | characters (\r already is converted to \n). */ |
| 4312 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4313 | break; |
| 4314 | } |
| 4315 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4316 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4317 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4318 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4319 | } |
| 4320 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4321 | len = expr_end - expr_start; |
| 4322 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4323 | str = PyMem_RawMalloc(len + 3); |
| 4324 | if (str == NULL) |
| 4325 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4326 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4327 | str[0] = '('; |
| 4328 | memcpy(str+1, expr_start, len); |
| 4329 | str[len+1] = ')'; |
| 4330 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4331 | |
| 4332 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4333 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4334 | Py_eval_input, 0); |
| 4335 | if (!mod_n) { |
| 4336 | PyMem_RawFree(str); |
| 4337 | return NULL; |
| 4338 | } |
| 4339 | /* Reuse str to find the correct column offset. */ |
| 4340 | str[0] = '{'; |
| 4341 | str[len+1] = '}'; |
| 4342 | fstring_fix_node_location(n, mod_n, str); |
| 4343 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4344 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4345 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4346 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4347 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4348 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | /* Return -1 on error. |
| 4352 | |
| 4353 | Return 0 if we reached the end of the literal. |
| 4354 | |
| 4355 | Return 1 if we haven't reached the end of the literal, but we want |
| 4356 | the caller to process the literal up to this point. Used for |
| 4357 | doubled braces. |
| 4358 | */ |
| 4359 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4360 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4361 | PyObject **literal, int recurse_lvl, |
| 4362 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4363 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4364 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4365 | brace (which isn't part of a unicode name escape such as |
| 4366 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4367 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4368 | const char *s = *str; |
| 4369 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4370 | int result = 0; |
| 4371 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4372 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4373 | while (s < end) { |
| 4374 | char ch = *s++; |
| 4375 | if (!raw && ch == '\\' && s < end) { |
| 4376 | ch = *s++; |
| 4377 | if (ch == 'N') { |
| 4378 | if (s < end && *s++ == '{') { |
| 4379 | while (s < end && *s++ != '}') { |
| 4380 | } |
| 4381 | continue; |
| 4382 | } |
| 4383 | break; |
| 4384 | } |
| 4385 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4386 | return -1; |
| 4387 | } |
| 4388 | } |
| 4389 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4390 | /* Check for doubled braces, but only at the top level. If |
| 4391 | we checked at every level, then f'{0:{3}}' would fail |
| 4392 | with the two closing braces. */ |
| 4393 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4394 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4395 | /* We're going to tell the caller that the literal ends |
| 4396 | here, but that they should continue scanning. But also |
| 4397 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4398 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4399 | result = 1; |
| 4400 | goto done; |
| 4401 | } |
| 4402 | |
| 4403 | /* Where a single '{' is the start of a new expression, a |
| 4404 | single '}' is not allowed. */ |
| 4405 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4406 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4407 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4408 | return -1; |
| 4409 | } |
| 4410 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4411 | /* We're either at a '{', which means we're starting another |
| 4412 | expression; or a '}', which means we're at the end of this |
| 4413 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4414 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4415 | break; |
| 4416 | } |
| 4417 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4418 | *str = s; |
| 4419 | assert(s <= end); |
| 4420 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4421 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4422 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4423 | if (raw) |
| 4424 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4425 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4426 | NULL, NULL); |
| 4427 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4428 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4429 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4430 | if (!*literal) |
| 4431 | return -1; |
| 4432 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4433 | return result; |
| 4434 | } |
| 4435 | |
| 4436 | /* Forward declaration because parsing is recursive. */ |
| 4437 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4438 | 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] | 4439 | struct compiling *c, const node *n); |
| 4440 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4441 | /* 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] | 4442 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4443 | which includes the expression, conversion character, and |
| 4444 | format_spec expression. |
| 4445 | |
| 4446 | Note that I don't do a perfect job here: I don't make sure that a |
| 4447 | closing brace doesn't match an opening paren, for example. It |
| 4448 | doesn't need to error on all invalid expressions, just correctly |
| 4449 | find the end of all valid ones. Any errors inside the expression |
| 4450 | will be caught when we parse it later. */ |
| 4451 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4452 | 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] | 4453 | expr_ty *expression, struct compiling *c, const node *n) |
| 4454 | { |
| 4455 | /* Return -1 on error, else 0. */ |
| 4456 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4457 | const char *expr_start; |
| 4458 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4459 | expr_ty simple_expression; |
| 4460 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4461 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4462 | |
| 4463 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4464 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4465 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4466 | |
| 4467 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4468 | int string_type = 0; |
| 4469 | |
| 4470 | /* Keep track of nesting level for braces/parens/brackets in |
| 4471 | expressions. */ |
| 4472 | Py_ssize_t nested_depth = 0; |
| 4473 | |
| 4474 | /* Can only nest one level deep. */ |
| 4475 | if (recurse_lvl >= 2) { |
| 4476 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4477 | return -1; |
| 4478 | } |
| 4479 | |
| 4480 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4481 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4482 | assert(**str == '{'); |
| 4483 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4484 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4485 | expr_start = *str; |
| 4486 | for (; *str < end; (*str)++) { |
| 4487 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4488 | |
| 4489 | /* Loop invariants. */ |
| 4490 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4491 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4492 | if (quote_char) |
| 4493 | assert(string_type == 1 || string_type == 3); |
| 4494 | else |
| 4495 | assert(string_type == 0); |
| 4496 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4497 | ch = **str; |
| 4498 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4499 | if (ch == '\\') { |
| 4500 | /* Error: can't include a backslash character, inside |
| 4501 | parens or strings or not. */ |
| 4502 | ast_error(c, n, "f-string expression part " |
| 4503 | "cannot include a backslash"); |
| 4504 | return -1; |
| 4505 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4506 | if (quote_char) { |
| 4507 | /* We're inside a string. See if we're at the end. */ |
| 4508 | /* This code needs to implement the same non-error logic |
| 4509 | as tok_get from tokenizer.c, at the letter_quote |
| 4510 | label. To actually share that code would be a |
| 4511 | nightmare. But, it's unlikely to change and is small, |
| 4512 | so duplicate it here. Note we don't need to catch all |
| 4513 | of the errors, since they'll be caught when parsing the |
| 4514 | expression. We just need to match the non-error |
| 4515 | cases. Thus we can ignore \n in single-quoted strings, |
| 4516 | for example. Or non-terminated strings. */ |
| 4517 | if (ch == quote_char) { |
| 4518 | /* Does this match the string_type (single or triple |
| 4519 | quoted)? */ |
| 4520 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4521 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4522 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4523 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4524 | string_type = 0; |
| 4525 | quote_char = 0; |
| 4526 | continue; |
| 4527 | } |
| 4528 | } else { |
| 4529 | /* We're at the end of a normal string. */ |
| 4530 | quote_char = 0; |
| 4531 | string_type = 0; |
| 4532 | continue; |
| 4533 | } |
| 4534 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4535 | } else if (ch == '\'' || ch == '"') { |
| 4536 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4537 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4538 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4539 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4540 | } else { |
| 4541 | /* Start of a normal string. */ |
| 4542 | string_type = 1; |
| 4543 | } |
| 4544 | /* Start looking for the end of the string. */ |
| 4545 | quote_char = ch; |
| 4546 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4547 | nested_depth++; |
| 4548 | } else if (nested_depth != 0 && |
| 4549 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4550 | nested_depth--; |
| 4551 | } else if (ch == '#') { |
| 4552 | /* Error: can't include a comment character, inside parens |
| 4553 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4554 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4555 | return -1; |
| 4556 | } else if (nested_depth == 0 && |
| 4557 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4558 | /* First, test for the special case of "!=". Since '=' is |
| 4559 | not an allowed conversion character, nothing is lost in |
| 4560 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4561 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4562 | /* This isn't a conversion character, just continue. */ |
| 4563 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4564 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4565 | /* Normal way out of this loop. */ |
| 4566 | break; |
| 4567 | } else { |
| 4568 | /* Just consume this char and loop around. */ |
| 4569 | } |
| 4570 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4571 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4572 | /* If we leave this loop in a string or with mismatched parens, we |
| 4573 | don't care. We'll get a syntax error when compiling the |
| 4574 | expression. But, we can produce a better error message, so |
| 4575 | let's just do that.*/ |
| 4576 | if (quote_char) { |
| 4577 | ast_error(c, n, "f-string: unterminated string"); |
| 4578 | return -1; |
| 4579 | } |
| 4580 | if (nested_depth) { |
| 4581 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4582 | return -1; |
| 4583 | } |
| 4584 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4585 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4586 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4587 | |
| 4588 | /* Compile the expression as soon as possible, so we show errors |
| 4589 | related to the expression before errors related to the |
| 4590 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4591 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4592 | if (!simple_expression) |
| 4593 | return -1; |
| 4594 | |
| 4595 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4596 | if (**str == '!') { |
| 4597 | *str += 1; |
| 4598 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4599 | goto unexpected_end_of_string; |
| 4600 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4601 | conversion = **str; |
| 4602 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4603 | |
| 4604 | /* Validate the conversion. */ |
| 4605 | if (!(conversion == 's' || conversion == 'r' |
| 4606 | || conversion == 'a')) { |
| 4607 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4608 | "expected 's', 'r', or 'a'"); |
| 4609 | return -1; |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4614 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4615 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4616 | if (**str == ':') { |
| 4617 | *str += 1; |
| 4618 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4619 | goto unexpected_end_of_string; |
| 4620 | |
| 4621 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4622 | 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] | 4623 | if (!format_spec) |
| 4624 | return -1; |
| 4625 | } |
| 4626 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4627 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4628 | goto unexpected_end_of_string; |
| 4629 | |
| 4630 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4631 | assert(*str < end); |
| 4632 | assert(**str == '}'); |
| 4633 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4634 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4635 | /* And now create the FormattedValue node that represents this |
| 4636 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4637 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4638 | format_spec, LINENO(n), n->n_col_offset, |
| 4639 | c->c_arena); |
| 4640 | if (!*expression) |
| 4641 | return -1; |
| 4642 | |
| 4643 | return 0; |
| 4644 | |
| 4645 | unexpected_end_of_string: |
| 4646 | ast_error(c, n, "f-string: expecting '}'"); |
| 4647 | return -1; |
| 4648 | } |
| 4649 | |
| 4650 | /* Return -1 on error. |
| 4651 | |
| 4652 | Return 0 if we have a literal (possible zero length) and an |
| 4653 | expression (zero length if at the end of the string. |
| 4654 | |
| 4655 | Return 1 if we have a literal, but no expression, and we want the |
| 4656 | caller to call us again. This is used to deal with doubled |
| 4657 | braces. |
| 4658 | |
| 4659 | When called multiple times on the string 'a{{b{0}c', this function |
| 4660 | will return: |
| 4661 | |
| 4662 | 1. the literal 'a{' with no expression, and a return value |
| 4663 | of 1. Despite the fact that there's no expression, the return |
| 4664 | value of 1 means we're not finished yet. |
| 4665 | |
| 4666 | 2. the literal 'b' and the expression '0', with a return value of |
| 4667 | 0. The fact that there's an expression means we're not finished. |
| 4668 | |
| 4669 | 3. literal 'c' with no expression and a return value of 0. The |
| 4670 | combination of the return value of 0 with no expression means |
| 4671 | we're finished. |
| 4672 | */ |
| 4673 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4674 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4675 | int recurse_lvl, PyObject **literal, |
| 4676 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4677 | struct compiling *c, const node *n) |
| 4678 | { |
| 4679 | int result; |
| 4680 | |
| 4681 | assert(*literal == NULL && *expression == NULL); |
| 4682 | |
| 4683 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4684 | 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] | 4685 | if (result < 0) |
| 4686 | goto error; |
| 4687 | |
| 4688 | assert(result == 0 || result == 1); |
| 4689 | |
| 4690 | if (result == 1) |
| 4691 | /* We have a literal, but don't look at the expression. */ |
| 4692 | return 1; |
| 4693 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4694 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4695 | /* We're at the end of the string or the end of a nested |
| 4696 | f-string: no expression. The top-level error case where we |
| 4697 | expect to be at the end of the string but we're at a '}' is |
| 4698 | handled later. */ |
| 4699 | return 0; |
| 4700 | |
| 4701 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4702 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4703 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4704 | 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] | 4705 | goto error; |
| 4706 | |
| 4707 | return 0; |
| 4708 | |
| 4709 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4710 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4711 | return -1; |
| 4712 | } |
| 4713 | |
| 4714 | #define EXPRLIST_N_CACHED 64 |
| 4715 | |
| 4716 | typedef struct { |
| 4717 | /* Incrementally build an array of expr_ty, so be used in an |
| 4718 | asdl_seq. Cache some small but reasonably sized number of |
| 4719 | expr_ty's, and then after that start dynamically allocating, |
| 4720 | doubling the number allocated each time. Note that the f-string |
| 4721 | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
| 4722 | Str for the literal 'a'. So you add expr_ty's about twice as |
| 4723 | fast as you add exressions in an f-string. */ |
| 4724 | |
| 4725 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4726 | Py_ssize_t size; /* Number we've used. */ |
| 4727 | expr_ty *p; /* Pointer to the memory we're actually |
| 4728 | using. Will point to 'data' until we |
| 4729 | start dynamically allocating. */ |
| 4730 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4731 | } ExprList; |
| 4732 | |
| 4733 | #ifdef NDEBUG |
| 4734 | #define ExprList_check_invariants(l) |
| 4735 | #else |
| 4736 | static void |
| 4737 | ExprList_check_invariants(ExprList *l) |
| 4738 | { |
| 4739 | /* Check our invariants. Make sure this object is "live", and |
| 4740 | hasn't been deallocated. */ |
| 4741 | assert(l->size >= 0); |
| 4742 | assert(l->p != NULL); |
| 4743 | if (l->size <= EXPRLIST_N_CACHED) |
| 4744 | assert(l->data == l->p); |
| 4745 | } |
| 4746 | #endif |
| 4747 | |
| 4748 | static void |
| 4749 | ExprList_Init(ExprList *l) |
| 4750 | { |
| 4751 | l->allocated = EXPRLIST_N_CACHED; |
| 4752 | l->size = 0; |
| 4753 | |
| 4754 | /* Until we start allocating dynamically, p points to data. */ |
| 4755 | l->p = l->data; |
| 4756 | |
| 4757 | ExprList_check_invariants(l); |
| 4758 | } |
| 4759 | |
| 4760 | static int |
| 4761 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4762 | { |
| 4763 | ExprList_check_invariants(l); |
| 4764 | if (l->size >= l->allocated) { |
| 4765 | /* We need to alloc (or realloc) the memory. */ |
| 4766 | Py_ssize_t new_size = l->allocated * 2; |
| 4767 | |
| 4768 | /* See if we've ever allocated anything dynamically. */ |
| 4769 | if (l->p == l->data) { |
| 4770 | Py_ssize_t i; |
| 4771 | /* We're still using the cached data. Switch to |
| 4772 | alloc-ing. */ |
| 4773 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4774 | if (!l->p) |
| 4775 | return -1; |
| 4776 | /* Copy the cached data into the new buffer. */ |
| 4777 | for (i = 0; i < l->size; i++) |
| 4778 | l->p[i] = l->data[i]; |
| 4779 | } else { |
| 4780 | /* Just realloc. */ |
| 4781 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4782 | if (!tmp) { |
| 4783 | PyMem_RawFree(l->p); |
| 4784 | l->p = NULL; |
| 4785 | return -1; |
| 4786 | } |
| 4787 | l->p = tmp; |
| 4788 | } |
| 4789 | |
| 4790 | l->allocated = new_size; |
| 4791 | assert(l->allocated == 2 * l->size); |
| 4792 | } |
| 4793 | |
| 4794 | l->p[l->size++] = exp; |
| 4795 | |
| 4796 | ExprList_check_invariants(l); |
| 4797 | return 0; |
| 4798 | } |
| 4799 | |
| 4800 | static void |
| 4801 | ExprList_Dealloc(ExprList *l) |
| 4802 | { |
| 4803 | ExprList_check_invariants(l); |
| 4804 | |
| 4805 | /* If there's been an error, or we've never dynamically allocated, |
| 4806 | do nothing. */ |
| 4807 | if (!l->p || l->p == l->data) { |
| 4808 | /* Do nothing. */ |
| 4809 | } else { |
| 4810 | /* We have dynamically allocated. Free the memory. */ |
| 4811 | PyMem_RawFree(l->p); |
| 4812 | } |
| 4813 | l->p = NULL; |
| 4814 | l->size = -1; |
| 4815 | } |
| 4816 | |
| 4817 | static asdl_seq * |
| 4818 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4819 | { |
| 4820 | asdl_seq *seq; |
| 4821 | |
| 4822 | ExprList_check_invariants(l); |
| 4823 | |
| 4824 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4825 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4826 | if (seq) { |
| 4827 | Py_ssize_t i; |
| 4828 | for (i = 0; i < l->size; i++) |
| 4829 | asdl_seq_SET(seq, i, l->p[i]); |
| 4830 | } |
| 4831 | ExprList_Dealloc(l); |
| 4832 | return seq; |
| 4833 | } |
| 4834 | |
| 4835 | /* The FstringParser is designed to add a mix of strings and |
| 4836 | f-strings, and concat them together as needed. Ultimately, it |
| 4837 | generates an expr_ty. */ |
| 4838 | typedef struct { |
| 4839 | PyObject *last_str; |
| 4840 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4841 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4842 | } FstringParser; |
| 4843 | |
| 4844 | #ifdef NDEBUG |
| 4845 | #define FstringParser_check_invariants(state) |
| 4846 | #else |
| 4847 | static void |
| 4848 | FstringParser_check_invariants(FstringParser *state) |
| 4849 | { |
| 4850 | if (state->last_str) |
| 4851 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4852 | ExprList_check_invariants(&state->expr_list); |
| 4853 | } |
| 4854 | #endif |
| 4855 | |
| 4856 | static void |
| 4857 | FstringParser_Init(FstringParser *state) |
| 4858 | { |
| 4859 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4860 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4861 | ExprList_Init(&state->expr_list); |
| 4862 | FstringParser_check_invariants(state); |
| 4863 | } |
| 4864 | |
| 4865 | static void |
| 4866 | FstringParser_Dealloc(FstringParser *state) |
| 4867 | { |
| 4868 | FstringParser_check_invariants(state); |
| 4869 | |
| 4870 | Py_XDECREF(state->last_str); |
| 4871 | ExprList_Dealloc(&state->expr_list); |
| 4872 | } |
| 4873 | |
| 4874 | /* Make a Str node, but decref the PyUnicode object being added. */ |
| 4875 | static expr_ty |
| 4876 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4877 | { |
| 4878 | PyObject *s = *str; |
| 4879 | *str = NULL; |
| 4880 | assert(PyUnicode_CheckExact(s)); |
| 4881 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4882 | Py_DECREF(s); |
| 4883 | return NULL; |
| 4884 | } |
| 4885 | return Str(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 4886 | } |
| 4887 | |
| 4888 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4889 | decref'd. */ |
| 4890 | static int |
| 4891 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4892 | { |
| 4893 | FstringParser_check_invariants(state); |
| 4894 | |
| 4895 | assert(PyUnicode_CheckExact(str)); |
| 4896 | |
| 4897 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4898 | Py_DECREF(str); |
| 4899 | return 0; |
| 4900 | } |
| 4901 | |
| 4902 | if (!state->last_str) { |
| 4903 | /* We didn't have a string before, so just remember this one. */ |
| 4904 | state->last_str = str; |
| 4905 | } else { |
| 4906 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4907 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4908 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4909 | return -1; |
| 4910 | } |
| 4911 | FstringParser_check_invariants(state); |
| 4912 | return 0; |
| 4913 | } |
| 4914 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4915 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4916 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4917 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4918 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4919 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4920 | struct compiling *c, const node *n) |
| 4921 | { |
| 4922 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4923 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4924 | |
| 4925 | /* Parse the f-string. */ |
| 4926 | while (1) { |
| 4927 | PyObject *literal = NULL; |
| 4928 | expr_ty expression = NULL; |
| 4929 | |
| 4930 | /* If there's a zero length literal in front of the |
| 4931 | expression, literal will be NULL. If we're at the end of |
| 4932 | the f-string, expression will be NULL (unless result == 1, |
| 4933 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4934 | 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] | 4935 | &literal, &expression, |
| 4936 | c, n); |
| 4937 | if (result < 0) |
| 4938 | return -1; |
| 4939 | |
| 4940 | /* Add the literal, if any. */ |
| 4941 | if (!literal) { |
| 4942 | /* Do nothing. Just leave last_str alone (and possibly |
| 4943 | NULL). */ |
| 4944 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 4945 | /* Note that the literal can be zero length, if the |
| 4946 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4947 | state->last_str = literal; |
| 4948 | literal = NULL; |
| 4949 | } else { |
| 4950 | /* We have a literal, concatenate it. */ |
| 4951 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4952 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4953 | return -1; |
| 4954 | literal = NULL; |
| 4955 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4956 | |
| 4957 | /* We've dealt with the literal now. It can't be leaked on further |
| 4958 | errors. */ |
| 4959 | assert(literal == NULL); |
| 4960 | |
| 4961 | /* See if we should just loop around to get the next literal |
| 4962 | and expression, while ignoring the expression this |
| 4963 | time. This is used for un-doubling braces, as an |
| 4964 | optimization. */ |
| 4965 | if (result == 1) |
| 4966 | continue; |
| 4967 | |
| 4968 | if (!expression) |
| 4969 | /* We're done with this f-string. */ |
| 4970 | break; |
| 4971 | |
| 4972 | /* We know we have an expression. Convert any existing string |
| 4973 | to a Str node. */ |
| 4974 | if (!state->last_str) { |
| 4975 | /* Do nothing. No previous literal. */ |
| 4976 | } else { |
| 4977 | /* Convert the existing last_str literal to a Str node. */ |
| 4978 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 4979 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 4980 | return -1; |
| 4981 | } |
| 4982 | |
| 4983 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 4984 | return -1; |
| 4985 | } |
| 4986 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4987 | /* If recurse_lvl is zero, then we must be at the end of the |
| 4988 | string. Otherwise, we must be at a right brace. */ |
| 4989 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4990 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4991 | ast_error(c, n, "f-string: unexpected end of string"); |
| 4992 | return -1; |
| 4993 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4994 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4995 | ast_error(c, n, "f-string: expecting '}'"); |
| 4996 | return -1; |
| 4997 | } |
| 4998 | |
| 4999 | FstringParser_check_invariants(state); |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | /* Convert the partial state reflected in last_str and expr_list to an |
| 5004 | expr_ty. The expr_ty can be a Str, or a JoinedStr. */ |
| 5005 | static expr_ty |
| 5006 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5007 | const node *n) |
| 5008 | { |
| 5009 | asdl_seq *seq; |
| 5010 | |
| 5011 | FstringParser_check_invariants(state); |
| 5012 | |
| 5013 | /* If we're just a constant string with no expressions, return |
| 5014 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5015 | if (!state->fmode) { |
| 5016 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5017 | if (!state->last_str) { |
| 5018 | /* Create a zero length string. */ |
| 5019 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5020 | if (!state->last_str) |
| 5021 | goto error; |
| 5022 | } |
| 5023 | return make_str_node_and_del(&state->last_str, c, n); |
| 5024 | } |
| 5025 | |
| 5026 | /* Create a Str node out of last_str, if needed. It will be the |
| 5027 | last node in our expression list. */ |
| 5028 | if (state->last_str) { |
| 5029 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5030 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5031 | goto error; |
| 5032 | } |
| 5033 | /* This has already been freed. */ |
| 5034 | assert(state->last_str == NULL); |
| 5035 | |
| 5036 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5037 | if (!seq) |
| 5038 | goto error; |
| 5039 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5040 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5041 | |
| 5042 | error: |
| 5043 | FstringParser_Dealloc(state); |
| 5044 | return NULL; |
| 5045 | } |
| 5046 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5047 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5048 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5049 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5050 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5051 | 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] | 5052 | struct compiling *c, const node *n) |
| 5053 | { |
| 5054 | FstringParser state; |
| 5055 | |
| 5056 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5057 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5058 | c, n) < 0) { |
| 5059 | FstringParser_Dealloc(&state); |
| 5060 | return NULL; |
| 5061 | } |
| 5062 | |
| 5063 | return FstringParser_Finish(&state, c, n); |
| 5064 | } |
| 5065 | |
| 5066 | /* n is a Python string literal, including the bracketing quote |
| 5067 | 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] | 5068 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5069 | 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] | 5070 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5071 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5072 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5073 | static int |
| 5074 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5075 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5076 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5077 | size_t len; |
| 5078 | const char *s = STR(n); |
| 5079 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5080 | int fmode = 0; |
| 5081 | *bytesmode = 0; |
| 5082 | *rawmode = 0; |
| 5083 | *result = NULL; |
| 5084 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5085 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5086 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5087 | if (quote == 'b' || quote == 'B') { |
| 5088 | quote = *++s; |
| 5089 | *bytesmode = 1; |
| 5090 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5091 | else if (quote == 'u' || quote == 'U') { |
| 5092 | quote = *++s; |
| 5093 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5094 | else if (quote == 'r' || quote == 'R') { |
| 5095 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5096 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5097 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5098 | else if (quote == 'f' || quote == 'F') { |
| 5099 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5100 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5101 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5102 | else { |
| 5103 | break; |
| 5104 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5105 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5106 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5107 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5108 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5109 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5110 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5111 | if (quote != '\'' && quote != '\"') { |
| 5112 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5113 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5114 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5115 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5116 | s++; |
| 5117 | len = strlen(s); |
| 5118 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5120 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5121 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5122 | } |
| 5123 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5124 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5125 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5126 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5127 | } |
| 5128 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5129 | /* A triple quoted string. We've already skipped one quote at |
| 5130 | the start and one at the end of the string. Now skip the |
| 5131 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5132 | s += 2; |
| 5133 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5134 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5135 | if (s[--len] != quote || s[--len] != quote) { |
| 5136 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5137 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5138 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5139 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5140 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5141 | if (fmode) { |
| 5142 | /* Just return the bytes. The caller will parse the resulting |
| 5143 | string. */ |
| 5144 | *fstr = s; |
| 5145 | *fstrlen = len; |
| 5146 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5147 | } |
| 5148 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5149 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5150 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5151 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5152 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5153 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5154 | const char *ch; |
| 5155 | for (ch = s; *ch; ch++) { |
| 5156 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5157 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5158 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5159 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5160 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5161 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5162 | if (*rawmode) |
| 5163 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5164 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5165 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5166 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5167 | if (*rawmode) |
| 5168 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5169 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5170 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5171 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5172 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5175 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5176 | each STRING atom, and process it as needed. For bytes, just |
| 5177 | concatenate them together, and the result will be a Bytes node. For |
| 5178 | normal strings and f-strings, concatenate them together. The result |
| 5179 | will be a Str node if there were no f-strings; a FormattedValue |
| 5180 | node if there's just an f-string (with no leading or trailing |
| 5181 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5182 | any literals involved. */ |
| 5183 | static expr_ty |
| 5184 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5185 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5186 | int bytesmode = 0; |
| 5187 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5188 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5189 | |
| 5190 | FstringParser state; |
| 5191 | FstringParser_Init(&state); |
| 5192 | |
| 5193 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5194 | int this_bytesmode; |
| 5195 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5196 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5197 | const char *fstr; |
| 5198 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5199 | |
| 5200 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5201 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5202 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5203 | goto error; |
| 5204 | |
| 5205 | /* Check that we're not mixing bytes with unicode. */ |
| 5206 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5207 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5208 | /* s is NULL if the current string part is an f-string. */ |
| 5209 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5210 | goto error; |
| 5211 | } |
| 5212 | bytesmode = this_bytesmode; |
| 5213 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5214 | if (fstr != NULL) { |
| 5215 | int result; |
| 5216 | assert(s == NULL && !bytesmode); |
| 5217 | /* This is an f-string. Parse and concatenate it. */ |
| 5218 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5219 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5220 | if (result < 0) |
| 5221 | goto error; |
| 5222 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5223 | /* A string or byte string. */ |
| 5224 | assert(s != NULL && fstr == NULL); |
| 5225 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5226 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5227 | PyUnicode_CheckExact(s)); |
| 5228 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | if (bytesmode) { |
| 5230 | /* For bytes, concat as we go. */ |
| 5231 | if (i == 0) { |
| 5232 | /* First time, just remember this value. */ |
| 5233 | bytes_str = s; |
| 5234 | } else { |
| 5235 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5236 | if (!bytes_str) |
| 5237 | goto error; |
| 5238 | } |
| 5239 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5240 | /* This is a regular string. Concatenate it. */ |
| 5241 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5242 | goto error; |
| 5243 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5244 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5245 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5246 | if (bytesmode) { |
| 5247 | /* Just return the bytes object and we're done. */ |
| 5248 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5249 | goto error; |
| 5250 | return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
| 5251 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5252 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5253 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5254 | assert(bytes_str == NULL); |
| 5255 | |
| 5256 | return FstringParser_Finish(&state, c, n); |
| 5257 | |
| 5258 | error: |
| 5259 | Py_XDECREF(bytes_str); |
| 5260 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5261 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5262 | } |