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 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 603 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 604 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 605 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 606 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | 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 |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1572 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1573 | asdl_seq *decorator_seq, bool 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 */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1576 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1577 | identifier name; |
| 1578 | arguments_ty args; |
| 1579 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1580 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1581 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1582 | |
| 1583 | REQ(n, funcdef); |
| 1584 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1585 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1586 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1587 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1588 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1589 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1590 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1591 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1592 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1593 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1594 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1595 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1596 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1597 | name_i += 2; |
| 1598 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1599 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1600 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1601 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1602 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1603 | if (is_async) |
| 1604 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 1605 | LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1606 | else |
| 1607 | return FunctionDef(name, args, body, decorator_seq, returns, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1608 | LINENO(n), n->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | static stmt_ty |
| 1612 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1613 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1614 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1615 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1616 | REQ(CHILD(n, 0), NAME); |
| 1617 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1618 | REQ(CHILD(n, 1), funcdef); |
| 1619 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1620 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1621 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | static stmt_ty |
| 1625 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1626 | { |
| 1627 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1628 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1629 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | |
| 1633 | static stmt_ty |
| 1634 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1635 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1636 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1637 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1638 | REQ(CHILD(n, 0), NAME); |
| 1639 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1640 | |
| 1641 | switch (TYPE(CHILD(n, 1))) { |
| 1642 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1643 | return ast_for_funcdef_impl(c, n, NULL, |
| 1644 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1645 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1646 | return ast_for_with_stmt(c, n, |
| 1647 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1648 | |
| 1649 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1650 | return ast_for_for_stmt(c, n, |
| 1651 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1652 | |
| 1653 | default: |
| 1654 | PyErr_Format(PyExc_SystemError, |
| 1655 | "invalid async stament: %s", |
| 1656 | STR(CHILD(n, 1))); |
| 1657 | return NULL; |
| 1658 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1661 | static stmt_ty |
| 1662 | ast_for_decorated(struct compiling *c, const node *n) |
| 1663 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1664 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1665 | stmt_ty thing = NULL; |
| 1666 | asdl_seq *decorator_seq = NULL; |
| 1667 | |
| 1668 | REQ(n, decorated); |
| 1669 | |
| 1670 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1671 | if (!decorator_seq) |
| 1672 | return NULL; |
| 1673 | |
| 1674 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1675 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1677 | |
| 1678 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1679 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1680 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1681 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1682 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1683 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1684 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1685 | /* we count the decorators in when talking about the class' or |
| 1686 | * function's line number */ |
| 1687 | if (thing) { |
| 1688 | thing->lineno = LINENO(n); |
| 1689 | thing->col_offset = n->n_col_offset; |
| 1690 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1691 | return thing; |
| 1692 | } |
| 1693 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1694 | static expr_ty |
| 1695 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1696 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1697 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1698 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1699 | arguments_ty args; |
| 1700 | expr_ty expression; |
| 1701 | |
| 1702 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1703 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1704 | if (!args) |
| 1705 | return NULL; |
| 1706 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1707 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1709 | } |
| 1710 | else { |
| 1711 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1712 | if (!args) |
| 1713 | return NULL; |
| 1714 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1715 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1719 | 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] | 1720 | } |
| 1721 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1722 | static expr_ty |
| 1723 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1726 | expr_ty expression, body, orelse; |
| 1727 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1728 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1729 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1730 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1731 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1732 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1733 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1734 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1735 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1736 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1737 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1738 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1739 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1742 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1743 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1744 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1745 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1746 | */ |
| 1747 | |
| 1748 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1749 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1750 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1751 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1752 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1753 | count_comp_for: |
| 1754 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1755 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1756 | if (NCH(n) == 2) { |
| 1757 | REQ(CHILD(n, 0), NAME); |
| 1758 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1759 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1760 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1761 | else if (NCH(n) == 1) { |
| 1762 | n = CHILD(n, 0); |
| 1763 | } |
| 1764 | else { |
| 1765 | goto error; |
| 1766 | } |
| 1767 | if (NCH(n) == (5)) { |
| 1768 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1769 | } |
| 1770 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1771 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1772 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1773 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1774 | REQ(n, comp_iter); |
| 1775 | n = CHILD(n, 0); |
| 1776 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1777 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1778 | else if (TYPE(n) == comp_if) { |
| 1779 | if (NCH(n) == 3) { |
| 1780 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1781 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1782 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1783 | else |
| 1784 | return n_fors; |
| 1785 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1786 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1787 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1788 | /* Should never be reached */ |
| 1789 | PyErr_SetString(PyExc_SystemError, |
| 1790 | "logic error in count_comp_fors"); |
| 1791 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1794 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1795 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1796 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | */ |
| 1798 | |
| 1799 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1800 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1801 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1802 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1803 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1804 | while (1) { |
| 1805 | REQ(n, comp_iter); |
| 1806 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1807 | return n_ifs; |
| 1808 | n = CHILD(n, 0); |
| 1809 | REQ(n, comp_if); |
| 1810 | n_ifs++; |
| 1811 | if (NCH(n) == 2) |
| 1812 | return n_ifs; |
| 1813 | n = CHILD(n, 2); |
| 1814 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1817 | static asdl_seq * |
| 1818 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1820 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1821 | asdl_seq *comps; |
| 1822 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1823 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1824 | if (n_fors == -1) |
| 1825 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1826 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1827 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1828 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1829 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1830 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1831 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1832 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1834 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1835 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1836 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1837 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1839 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1841 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1842 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1843 | REQ(CHILD(n, 0), NAME); |
| 1844 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1845 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1846 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1847 | else { |
| 1848 | sync_n = CHILD(n, 0); |
| 1849 | } |
| 1850 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1851 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1852 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1853 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1854 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1855 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1856 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1857 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1858 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1859 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1860 | /* Check the # of children rather than the length of t, since |
| 1861 | (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] | 1862 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1863 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1864 | comp = comprehension(first, expression, NULL, |
| 1865 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1866 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1867 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1868 | first->col_offset, c->c_arena), |
| 1869 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1870 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1871 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1872 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1873 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1874 | int j, n_ifs; |
| 1875 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1877 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1878 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1879 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1880 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1882 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1883 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1884 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1885 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1886 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1887 | REQ(n, comp_iter); |
| 1888 | n = CHILD(n, 0); |
| 1889 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1891 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1892 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1893 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1894 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1895 | if (NCH(n) == 3) |
| 1896 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1897 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1898 | /* on exit, must guarantee that n is a comp_for */ |
| 1899 | if (TYPE(n) == comp_iter) |
| 1900 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1901 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1902 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1903 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1904 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1905 | return comps; |
| 1906 | } |
| 1907 | |
| 1908 | static expr_ty |
| 1909 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1910 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1911 | /* testlist_comp: (test|star_expr) |
| 1912 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1913 | expr_ty elt; |
| 1914 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1915 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1917 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1919 | ch = CHILD(n, 0); |
| 1920 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1921 | if (!elt) |
| 1922 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1923 | if (elt->kind == Starred_kind) { |
| 1924 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1925 | return NULL; |
| 1926 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1928 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1929 | if (!comps) |
| 1930 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1931 | |
| 1932 | if (type == COMP_GENEXP) |
| 1933 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1934 | else if (type == COMP_LISTCOMP) |
| 1935 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1936 | else if (type == COMP_SETCOMP) |
| 1937 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1938 | else |
| 1939 | /* Should never happen */ |
| 1940 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1943 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1944 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1945 | * elements. Iff successful, nonzero is returned. |
| 1946 | */ |
| 1947 | static int |
| 1948 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1949 | expr_ty *key, expr_ty *value) |
| 1950 | { |
| 1951 | expr_ty expression; |
| 1952 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1953 | assert(NCH(n) - *i >= 2); |
| 1954 | |
| 1955 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1956 | if (!expression) |
| 1957 | return 0; |
| 1958 | *key = NULL; |
| 1959 | *value = expression; |
| 1960 | |
| 1961 | *i += 2; |
| 1962 | } |
| 1963 | else { |
| 1964 | assert(NCH(n) - *i >= 3); |
| 1965 | |
| 1966 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1967 | if (!expression) |
| 1968 | return 0; |
| 1969 | *key = expression; |
| 1970 | |
| 1971 | REQ(CHILD(n, *i + 1), COLON); |
| 1972 | |
| 1973 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1974 | if (!expression) |
| 1975 | return 0; |
| 1976 | *value = expression; |
| 1977 | |
| 1978 | *i += 3; |
| 1979 | } |
| 1980 | return 1; |
| 1981 | } |
| 1982 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1983 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1984 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1985 | { |
| 1986 | expr_ty key, value; |
| 1987 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1988 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1990 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1991 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1992 | assert(key); |
| 1993 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1995 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1996 | if (!comps) |
| 1997 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1999 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 2000 | } |
| 2001 | |
| 2002 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2003 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2004 | { |
| 2005 | int i; |
| 2006 | int j; |
| 2007 | int size; |
| 2008 | asdl_seq *keys, *values; |
| 2009 | |
| 2010 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2011 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2012 | if (!keys) |
| 2013 | return NULL; |
| 2014 | |
| 2015 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2016 | if (!values) |
| 2017 | return NULL; |
| 2018 | |
| 2019 | j = 0; |
| 2020 | for (i = 0; i < NCH(n); i++) { |
| 2021 | expr_ty key, value; |
| 2022 | |
| 2023 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2024 | return NULL; |
| 2025 | asdl_seq_SET(keys, j, key); |
| 2026 | asdl_seq_SET(values, j, value); |
| 2027 | |
| 2028 | j++; |
| 2029 | } |
| 2030 | keys->size = j; |
| 2031 | values->size = j; |
| 2032 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2033 | } |
| 2034 | |
| 2035 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2036 | ast_for_genexp(struct compiling *c, const node *n) |
| 2037 | { |
| 2038 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2039 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static expr_ty |
| 2043 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2044 | { |
| 2045 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2046 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | static expr_ty |
| 2050 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2051 | { |
| 2052 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2053 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2056 | static expr_ty |
| 2057 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2058 | { |
| 2059 | int i; |
| 2060 | int size; |
| 2061 | asdl_seq *elts; |
| 2062 | |
| 2063 | assert(TYPE(n) == (dictorsetmaker)); |
| 2064 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2065 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2066 | if (!elts) |
| 2067 | return NULL; |
| 2068 | for (i = 0; i < NCH(n); i += 2) { |
| 2069 | expr_ty expression; |
| 2070 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2071 | if (!expression) |
| 2072 | return NULL; |
| 2073 | asdl_seq_SET(elts, i / 2, expression); |
| 2074 | } |
| 2075 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2076 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2077 | |
| 2078 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2079 | ast_for_atom(struct compiling *c, const node *n) |
| 2080 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2081 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2082 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2083 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2084 | */ |
| 2085 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2087 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2088 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2089 | PyObject *name; |
| 2090 | const char *s = STR(ch); |
| 2091 | size_t len = strlen(s); |
| 2092 | if (len >= 4 && len <= 5) { |
| 2093 | if (!strcmp(s, "None")) |
| 2094 | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
| 2095 | if (!strcmp(s, "True")) |
| 2096 | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
| 2097 | if (!strcmp(s, "False")) |
| 2098 | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
| 2099 | } |
| 2100 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2101 | if (!name) |
| 2102 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2103 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2104 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2105 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2106 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2107 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2108 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2109 | const char *errtype = NULL; |
| 2110 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2111 | errtype = "unicode error"; |
| 2112 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2113 | errtype = "value error"; |
| 2114 | if (errtype) { |
| 2115 | char buf[128]; |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2116 | const char *s = NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2117 | PyObject *type, *value, *tback, *errstr; |
| 2118 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2119 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2120 | if (errstr) |
| 2121 | s = PyUnicode_AsUTF8(errstr); |
| 2122 | if (s) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2123 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2124 | } else { |
Victor Stinner | 00723e0 | 2015-09-03 12:57:11 +0200 | [diff] [blame] | 2125 | PyErr_Clear(); |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2126 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2127 | } |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2128 | Py_XDECREF(errstr); |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2129 | ast_error(c, n, buf); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2130 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2131 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2132 | Py_XDECREF(tback); |
| 2133 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2134 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2135 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2136 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2137 | } |
| 2138 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2139 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2140 | if (!pynum) |
| 2141 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2142 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2143 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2144 | Py_DECREF(pynum); |
| 2145 | return NULL; |
| 2146 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2147 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2148 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2149 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2150 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2151 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2152 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2154 | if (TYPE(ch) == RPAR) |
| 2155 | 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] | 2156 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2157 | if (TYPE(ch) == yield_expr) |
| 2158 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2161 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2162 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2163 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2164 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2165 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2166 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2168 | if (TYPE(ch) == RSQB) |
| 2169 | 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] | 2170 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2171 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2172 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2173 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2174 | if (!elts) |
| 2175 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2176 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2177 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2178 | } |
| 2179 | else |
| 2180 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2181 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2182 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2183 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2184 | * ((test | '*' test) |
| 2185 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2186 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2187 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2188 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2189 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2190 | 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] | 2191 | } |
| 2192 | else { |
| 2193 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2194 | if (NCH(ch) == 1 || |
| 2195 | (NCH(ch) > 1 && |
| 2196 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2197 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2198 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2199 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2200 | else if (NCH(ch) > 1 && |
| 2201 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2202 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2203 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2204 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2205 | else if (NCH(ch) > 3 - is_dict && |
| 2206 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2207 | /* It's a dictionary comprehension. */ |
| 2208 | if (is_dict) { |
| 2209 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2210 | "dict comprehension"); |
| 2211 | return NULL; |
| 2212 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2213 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2214 | } |
| 2215 | else { |
| 2216 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2217 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2218 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2219 | if (res) { |
| 2220 | res->lineno = LINENO(n); |
| 2221 | res->col_offset = n->n_col_offset; |
| 2222 | } |
| 2223 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2224 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2225 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2226 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2227 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2228 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | static slice_ty |
| 2233 | ast_for_slice(struct compiling *c, const node *n) |
| 2234 | { |
| 2235 | node *ch; |
| 2236 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2237 | |
| 2238 | REQ(n, subscript); |
| 2239 | |
| 2240 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2241 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2242 | sliceop: ':' [test] |
| 2243 | */ |
| 2244 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2245 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2246 | /* 'step' variable hold no significance in terms of being used over |
| 2247 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2249 | if (!step) |
| 2250 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2252 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2256 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2257 | if (!lower) |
| 2258 | return NULL; |
| 2259 | } |
| 2260 | |
| 2261 | /* If there's an upper bound it's in the second or third position. */ |
| 2262 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2263 | if (NCH(n) > 1) { |
| 2264 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2265 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2266 | if (TYPE(n2) == test) { |
| 2267 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2268 | if (!upper) |
| 2269 | return NULL; |
| 2270 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2271 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2272 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2273 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2274 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2275 | if (TYPE(n2) == test) { |
| 2276 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2277 | if (!upper) |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | ch = CHILD(n, NCH(n) - 1); |
| 2283 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2284 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2285 | ch = CHILD(ch, 1); |
| 2286 | if (TYPE(ch) == test) { |
| 2287 | step = ast_for_expr(c, ch); |
| 2288 | if (!step) |
| 2289 | return NULL; |
| 2290 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2294 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | static expr_ty |
| 2298 | ast_for_binop(struct compiling *c, const node *n) |
| 2299 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2300 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2302 | BinOp(BinOp(A, op, B), op, C). |
| 2303 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2304 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2305 | int i, nops; |
| 2306 | expr_ty expr1, expr2, result; |
| 2307 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2308 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2309 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2310 | if (!expr1) |
| 2311 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2313 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2314 | if (!expr2) |
| 2315 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2316 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2317 | newoperator = get_operator(CHILD(n, 1)); |
| 2318 | if (!newoperator) |
| 2319 | return NULL; |
| 2320 | |
| 2321 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2322 | c->c_arena); |
| 2323 | if (!result) |
| 2324 | return NULL; |
| 2325 | |
| 2326 | nops = (NCH(n) - 1) / 2; |
| 2327 | for (i = 1; i < nops; i++) { |
| 2328 | expr_ty tmp_result, tmp; |
| 2329 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2330 | |
| 2331 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2332 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2333 | return NULL; |
| 2334 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2335 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2336 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2337 | return NULL; |
| 2338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2340 | LINENO(next_oper), next_oper->n_col_offset, |
| 2341 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2343 | return NULL; |
| 2344 | result = tmp_result; |
| 2345 | } |
| 2346 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2349 | static expr_ty |
| 2350 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2353 | subscriptlist: subscript (',' subscript)* [','] |
| 2354 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2355 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2356 | REQ(n, trailer); |
| 2357 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2358 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2359 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2360 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2361 | else |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2362 | return ast_for_call(c, CHILD(n, 1), left_expr, true); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2363 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2364 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2365 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2366 | if (!attr_id) |
| 2367 | return NULL; |
| 2368 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2369 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2370 | } |
| 2371 | else { |
| 2372 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2373 | REQ(CHILD(n, 2), RSQB); |
| 2374 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2375 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2376 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2377 | if (!slc) |
| 2378 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2379 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2380 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2381 | } |
| 2382 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2383 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2384 | by treating the sequence as a tuple literal if there are |
| 2385 | no slice features. |
| 2386 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2387 | int j; |
| 2388 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2389 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2390 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2391 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2392 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2393 | if (!slices) |
| 2394 | return NULL; |
| 2395 | for (j = 0; j < NCH(n); j += 2) { |
| 2396 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2397 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2398 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2399 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2400 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2401 | asdl_seq_SET(slices, j / 2, slc); |
| 2402 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2403 | if (!simple) { |
| 2404 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2405 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2406 | } |
| 2407 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2408 | 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] | 2409 | if (!elts) |
| 2410 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2411 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2412 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2413 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2414 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2415 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2416 | 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] | 2417 | if (!e) |
| 2418 | return NULL; |
| 2419 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2420 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2421 | } |
| 2422 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2426 | ast_for_factor(struct compiling *c, const node *n) |
| 2427 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2428 | expr_ty expression; |
| 2429 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2430 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2431 | if (!expression) |
| 2432 | return NULL; |
| 2433 | |
| 2434 | switch (TYPE(CHILD(n, 0))) { |
| 2435 | case PLUS: |
| 2436 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2437 | c->c_arena); |
| 2438 | case MINUS: |
| 2439 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2440 | c->c_arena); |
| 2441 | case TILDE: |
| 2442 | return UnaryOp(Invert, expression, LINENO(n), |
| 2443 | n->n_col_offset, c->c_arena); |
| 2444 | } |
| 2445 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2446 | TYPE(CHILD(n, 0))); |
| 2447 | return NULL; |
| 2448 | } |
| 2449 | |
| 2450 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2451 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2452 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2453 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2454 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2455 | |
| 2456 | REQ(n, atom_expr); |
| 2457 | nch = NCH(n); |
| 2458 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2459 | 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] | 2460 | start = 1; |
| 2461 | assert(nch > 1); |
| 2462 | } |
| 2463 | |
| 2464 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2465 | if (!e) |
| 2466 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2467 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2468 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2469 | if (start && nch == 2) { |
| 2470 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2471 | } |
| 2472 | |
| 2473 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2474 | node *ch = CHILD(n, i); |
| 2475 | if (TYPE(ch) != trailer) |
| 2476 | break; |
| 2477 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2478 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2479 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2480 | tmp->lineno = e->lineno; |
| 2481 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2482 | e = tmp; |
| 2483 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2484 | |
| 2485 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2486 | /* there was an 'await' */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2487 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2488 | } |
| 2489 | else { |
| 2490 | return e; |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | static expr_ty |
| 2495 | ast_for_power(struct compiling *c, const node *n) |
| 2496 | { |
| 2497 | /* power: atom trailer* ('**' factor)* |
| 2498 | */ |
| 2499 | expr_ty e; |
| 2500 | REQ(n, power); |
| 2501 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2502 | if (!e) |
| 2503 | return NULL; |
| 2504 | if (NCH(n) == 1) |
| 2505 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2506 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2507 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2508 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2509 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2510 | 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] | 2511 | } |
| 2512 | return e; |
| 2513 | } |
| 2514 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2515 | static expr_ty |
| 2516 | ast_for_starred(struct compiling *c, const node *n) |
| 2517 | { |
| 2518 | expr_ty tmp; |
| 2519 | REQ(n, star_expr); |
| 2520 | |
| 2521 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2522 | if (!tmp) |
| 2523 | return NULL; |
| 2524 | |
| 2525 | /* The Load context is changed later. */ |
| 2526 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2527 | } |
| 2528 | |
| 2529 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2530 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2531 | */ |
| 2532 | |
| 2533 | static expr_ty |
| 2534 | ast_for_expr(struct compiling *c, const node *n) |
| 2535 | { |
| 2536 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2537 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2538 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2540 | and_test: not_test ('and' not_test)* |
| 2541 | not_test: 'not' not_test | comparison |
| 2542 | comparison: expr (comp_op expr)* |
| 2543 | expr: xor_expr ('|' xor_expr)* |
| 2544 | xor_expr: and_expr ('^' and_expr)* |
| 2545 | and_expr: shift_expr ('&' shift_expr)* |
| 2546 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2547 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2548 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2549 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2550 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2551 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2552 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2553 | */ |
| 2554 | |
| 2555 | asdl_seq *seq; |
| 2556 | int i; |
| 2557 | |
| 2558 | loop: |
| 2559 | switch (TYPE(n)) { |
| 2560 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2561 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2562 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2563 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2564 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2565 | else if (NCH(n) > 1) |
| 2566 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2567 | /* Fallthrough */ |
| 2568 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2569 | case and_test: |
| 2570 | if (NCH(n) == 1) { |
| 2571 | n = CHILD(n, 0); |
| 2572 | goto loop; |
| 2573 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2574 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2575 | if (!seq) |
| 2576 | return NULL; |
| 2577 | for (i = 0; i < NCH(n); i += 2) { |
| 2578 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2579 | if (!e) |
| 2580 | return NULL; |
| 2581 | asdl_seq_SET(seq, i / 2, e); |
| 2582 | } |
| 2583 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2584 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2585 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2586 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2587 | 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] | 2588 | case not_test: |
| 2589 | if (NCH(n) == 1) { |
| 2590 | n = CHILD(n, 0); |
| 2591 | goto loop; |
| 2592 | } |
| 2593 | else { |
| 2594 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2595 | if (!expression) |
| 2596 | return NULL; |
| 2597 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2598 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2599 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2600 | } |
| 2601 | case comparison: |
| 2602 | if (NCH(n) == 1) { |
| 2603 | n = CHILD(n, 0); |
| 2604 | goto loop; |
| 2605 | } |
| 2606 | else { |
| 2607 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2608 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2609 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2610 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2611 | if (!ops) |
| 2612 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2613 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2614 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2615 | return NULL; |
| 2616 | } |
| 2617 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2618 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2619 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2620 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2621 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2622 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2623 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2624 | |
| 2625 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2626 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2627 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2628 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2630 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2631 | asdl_seq_SET(cmps, i / 2, expression); |
| 2632 | } |
| 2633 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2634 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2635 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2636 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2637 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2638 | return Compare(expression, ops, cmps, LINENO(n), |
| 2639 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 | } |
| 2641 | break; |
| 2642 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2643 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2645 | /* The next five cases all handle BinOps. The main body of code |
| 2646 | is the same in each case, but the switch turned inside out to |
| 2647 | reuse the code for each type of operator. |
| 2648 | */ |
| 2649 | case expr: |
| 2650 | case xor_expr: |
| 2651 | case and_expr: |
| 2652 | case shift_expr: |
| 2653 | case arith_expr: |
| 2654 | case term: |
| 2655 | if (NCH(n) == 1) { |
| 2656 | n = CHILD(n, 0); |
| 2657 | goto loop; |
| 2658 | } |
| 2659 | return ast_for_binop(c, n); |
| 2660 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2661 | node *an = NULL; |
| 2662 | node *en = NULL; |
| 2663 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2664 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2665 | if (NCH(n) > 1) |
| 2666 | an = CHILD(n, 1); /* yield_arg */ |
| 2667 | if (an) { |
| 2668 | en = CHILD(an, NCH(an) - 1); |
| 2669 | if (NCH(an) == 2) { |
| 2670 | is_from = 1; |
| 2671 | exp = ast_for_expr(c, en); |
| 2672 | } |
| 2673 | else |
| 2674 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2675 | if (!exp) |
| 2676 | return NULL; |
| 2677 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2678 | if (is_from) |
| 2679 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2680 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2681 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2682 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2683 | if (NCH(n) == 1) { |
| 2684 | n = CHILD(n, 0); |
| 2685 | goto loop; |
| 2686 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2687 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2688 | case power: |
| 2689 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2690 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2691 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | return NULL; |
| 2693 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2694 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2695 | return NULL; |
| 2696 | } |
| 2697 | |
| 2698 | static expr_ty |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2699 | 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] | 2700 | { |
| 2701 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2702 | arglist: argument (',' argument)* [','] |
| 2703 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | */ |
| 2705 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2706 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2707 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2708 | asdl_seq *args; |
| 2709 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2710 | |
| 2711 | REQ(n, arglist); |
| 2712 | |
| 2713 | nargs = 0; |
| 2714 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2715 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2716 | node *ch = CHILD(n, i); |
| 2717 | if (TYPE(ch) == argument) { |
| 2718 | if (NCH(ch) == 1) |
| 2719 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2720 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2721 | nargs++; |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2722 | if (!allowgen) { |
| 2723 | ast_error(c, ch, "invalid syntax"); |
| 2724 | return NULL; |
| 2725 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2726 | if (NCH(n) > 1) { |
| 2727 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2728 | return NULL; |
| 2729 | } |
| 2730 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2731 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2732 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2733 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2734 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2735 | nkeywords++; |
| 2736 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2737 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2739 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2740 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2741 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2742 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2743 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2744 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2745 | |
| 2746 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2747 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2748 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2749 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2750 | node *ch = CHILD(n, i); |
| 2751 | if (TYPE(ch) == argument) { |
| 2752 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2753 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2754 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2755 | /* a positional argument */ |
| 2756 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2757 | if (ndoublestars) { |
| 2758 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2759 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2760 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2761 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2762 | else { |
| 2763 | ast_error(c, chch, |
| 2764 | "positional argument follows " |
| 2765 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2766 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2767 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2768 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2769 | e = ast_for_expr(c, chch); |
| 2770 | if (!e) |
| 2771 | return NULL; |
| 2772 | asdl_seq_SET(args, nargs++, e); |
| 2773 | } |
| 2774 | else if (TYPE(chch) == STAR) { |
| 2775 | /* an iterable argument unpacking */ |
| 2776 | expr_ty starred; |
| 2777 | if (ndoublestars) { |
| 2778 | ast_error(c, chch, |
| 2779 | "iterable argument unpacking follows " |
| 2780 | "keyword argument unpacking"); |
| 2781 | return NULL; |
| 2782 | } |
| 2783 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2784 | if (!e) |
| 2785 | return NULL; |
| 2786 | starred = Starred(e, Load, LINENO(chch), |
| 2787 | chch->n_col_offset, |
| 2788 | c->c_arena); |
| 2789 | if (!starred) |
| 2790 | return NULL; |
| 2791 | asdl_seq_SET(args, nargs++, starred); |
| 2792 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2793 | } |
| 2794 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2795 | /* a keyword argument unpacking */ |
| 2796 | keyword_ty kw; |
| 2797 | i++; |
| 2798 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2799 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2800 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2801 | kw = keyword(NULL, e, c->c_arena); |
| 2802 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2803 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2805 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2806 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2807 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2808 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2809 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2810 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2811 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2812 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2813 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2814 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2815 | identifier key, tmp; |
| 2816 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2818 | // To remain LL(1), the grammar accepts any test (basically, any |
| 2819 | // expression) in the keyword slot of a call site. So, we need |
| 2820 | // to manually enforce that the keyword is a NAME here. |
| 2821 | static const int name_tree[] = { |
| 2822 | test, |
| 2823 | or_test, |
| 2824 | and_test, |
| 2825 | not_test, |
| 2826 | comparison, |
| 2827 | expr, |
| 2828 | xor_expr, |
| 2829 | and_expr, |
| 2830 | shift_expr, |
| 2831 | arith_expr, |
| 2832 | term, |
| 2833 | factor, |
| 2834 | power, |
| 2835 | atom_expr, |
| 2836 | atom, |
| 2837 | 0, |
| 2838 | }; |
| 2839 | node *expr_node = chch; |
| 2840 | for (int i = 0; name_tree[i]; i++) { |
| 2841 | if (TYPE(expr_node) != name_tree[i]) |
| 2842 | break; |
| 2843 | if (NCH(expr_node) != 1) |
| 2844 | break; |
| 2845 | expr_node = CHILD(expr_node, 0); |
| 2846 | } |
| 2847 | if (TYPE(expr_node) == lambdef) { |
| 2848 | // f(lambda x: x[0] = 3) ends up getting parsed with LHS |
| 2849 | // test = lambda x: x[0], and RHS test = 3. Issue #132313 |
| 2850 | // points out that complaining about a keyword then is very |
| 2851 | // confusing. |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2852 | ast_error(c, chch, |
| 2853 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2854 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2855 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2856 | else if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2857 | ast_error(c, chch, |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2858 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2859 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2860 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2861 | key = new_identifier(STR(expr_node), c); |
| 2862 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2863 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2865 | if (forbidden_name(c, key, chch, 1)) { |
| 2866 | return NULL; |
| 2867 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2868 | for (k = 0; k < nkeywords; k++) { |
| 2869 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2870 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2871 | ast_error(c, chch, |
| 2872 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2873 | return NULL; |
| 2874 | } |
| 2875 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2876 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2877 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2878 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2879 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2881 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2882 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2883 | } |
| 2884 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2887 | 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] | 2888 | } |
| 2889 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2891 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2892 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2893 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2894 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2895 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2896 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2897 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2898 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2899 | } |
| 2900 | else { |
| 2901 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2902 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2903 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2905 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2906 | else { |
| 2907 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2908 | if (!tmp) |
| 2909 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2910 | 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] | 2911 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2914 | static stmt_ty |
| 2915 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2916 | { |
| 2917 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2918 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2919 | ('=' (yield_expr|testlist_star_expr))*) |
| 2920 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2921 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2922 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2923 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2924 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2925 | */ |
| 2926 | |
| 2927 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2928 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2929 | if (!e) |
| 2930 | return NULL; |
| 2931 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2932 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2933 | } |
| 2934 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2935 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2936 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2937 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2939 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2940 | if (!expr1) |
| 2941 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2942 | if(!set_context(c, expr1, Store, ch)) |
| 2943 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2944 | /* set_context checks that most expressions are not the left side. |
| 2945 | Augmented assignments can only have a name, a subscript, or an |
| 2946 | attribute on the left, though, so we have to explicitly check for |
| 2947 | those. */ |
| 2948 | switch (expr1->kind) { |
| 2949 | case Name_kind: |
| 2950 | case Attribute_kind: |
| 2951 | case Subscript_kind: |
| 2952 | break; |
| 2953 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2954 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2955 | return NULL; |
| 2956 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2957 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2958 | ch = CHILD(n, 2); |
| 2959 | if (TYPE(ch) == testlist) |
| 2960 | expr2 = ast_for_testlist(c, ch); |
| 2961 | else |
| 2962 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2963 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2964 | return NULL; |
| 2965 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2966 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2967 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2968 | return NULL; |
| 2969 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2970 | 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] | 2971 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2972 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2973 | expr_ty expr1, expr2, expr3; |
| 2974 | node *ch = CHILD(n, 0); |
| 2975 | node *deep, *ann = CHILD(n, 1); |
| 2976 | int simple = 1; |
| 2977 | |
| 2978 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2979 | deep = ch; |
| 2980 | while (NCH(deep) == 1) { |
| 2981 | deep = CHILD(deep, 0); |
| 2982 | } |
| 2983 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2984 | simple = 0; |
| 2985 | } |
| 2986 | expr1 = ast_for_testlist(c, ch); |
| 2987 | if (!expr1) { |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | switch (expr1->kind) { |
| 2991 | case Name_kind: |
| 2992 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2993 | return NULL; |
| 2994 | } |
| 2995 | expr1->v.Name.ctx = Store; |
| 2996 | break; |
| 2997 | case Attribute_kind: |
| 2998 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2999 | return NULL; |
| 3000 | } |
| 3001 | expr1->v.Attribute.ctx = Store; |
| 3002 | break; |
| 3003 | case Subscript_kind: |
| 3004 | expr1->v.Subscript.ctx = Store; |
| 3005 | break; |
| 3006 | case List_kind: |
| 3007 | ast_error(c, ch, |
| 3008 | "only single target (not list) can be annotated"); |
| 3009 | return NULL; |
| 3010 | case Tuple_kind: |
| 3011 | ast_error(c, ch, |
| 3012 | "only single target (not tuple) can be annotated"); |
| 3013 | return NULL; |
| 3014 | default: |
| 3015 | ast_error(c, ch, |
| 3016 | "illegal target for annotation"); |
| 3017 | return NULL; |
| 3018 | } |
| 3019 | |
| 3020 | if (expr1->kind != Name_kind) { |
| 3021 | simple = 0; |
| 3022 | } |
| 3023 | ch = CHILD(ann, 1); |
| 3024 | expr2 = ast_for_expr(c, ch); |
| 3025 | if (!expr2) { |
| 3026 | return NULL; |
| 3027 | } |
| 3028 | if (NCH(ann) == 2) { |
| 3029 | return AnnAssign(expr1, expr2, NULL, simple, |
| 3030 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3031 | } |
| 3032 | else { |
| 3033 | ch = CHILD(ann, 3); |
| 3034 | expr3 = ast_for_expr(c, ch); |
| 3035 | if (!expr3) { |
| 3036 | return NULL; |
| 3037 | } |
| 3038 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3039 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3040 | } |
| 3041 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3042 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3043 | int i; |
| 3044 | asdl_seq *targets; |
| 3045 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3046 | expr_ty expression; |
| 3047 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3048 | /* a normal assignment */ |
| 3049 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3050 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3051 | if (!targets) |
| 3052 | return NULL; |
| 3053 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3054 | expr_ty e; |
| 3055 | node *ch = CHILD(n, i); |
| 3056 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3057 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3058 | return NULL; |
| 3059 | } |
| 3060 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3062 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3063 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3064 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3065 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3066 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3067 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3068 | asdl_seq_SET(targets, i / 2, e); |
| 3069 | } |
| 3070 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3071 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3072 | expression = ast_for_testlist(c, value); |
| 3073 | else |
| 3074 | expression = ast_for_expr(c, value); |
| 3075 | if (!expression) |
| 3076 | return NULL; |
| 3077 | 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] | 3078 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3081 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3082 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3083 | 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] | 3084 | { |
| 3085 | asdl_seq *seq; |
| 3086 | int i; |
| 3087 | expr_ty e; |
| 3088 | |
| 3089 | REQ(n, exprlist); |
| 3090 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3091 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3092 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3093 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3094 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3095 | e = ast_for_expr(c, CHILD(n, i)); |
| 3096 | if (!e) |
| 3097 | return NULL; |
| 3098 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3099 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3100 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3101 | } |
| 3102 | return seq; |
| 3103 | } |
| 3104 | |
| 3105 | static stmt_ty |
| 3106 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3107 | { |
| 3108 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3110 | /* del_stmt: 'del' exprlist */ |
| 3111 | REQ(n, del_stmt); |
| 3112 | |
| 3113 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3114 | if (!expr_list) |
| 3115 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3116 | 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] | 3117 | } |
| 3118 | |
| 3119 | static stmt_ty |
| 3120 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3121 | { |
| 3122 | /* |
| 3123 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3124 | | yield_stmt |
| 3125 | break_stmt: 'break' |
| 3126 | continue_stmt: 'continue' |
| 3127 | return_stmt: 'return' [testlist] |
| 3128 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3129 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3130 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3131 | */ |
| 3132 | node *ch; |
| 3133 | |
| 3134 | REQ(n, flow_stmt); |
| 3135 | ch = CHILD(n, 0); |
| 3136 | switch (TYPE(ch)) { |
| 3137 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3138 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3139 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3140 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3141 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3142 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3143 | if (!exp) |
| 3144 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3145 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3146 | } |
| 3147 | case return_stmt: |
| 3148 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3149 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3150 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3151 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | if (!expression) |
| 3153 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3154 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3155 | } |
| 3156 | case raise_stmt: |
| 3157 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3158 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3159 | else if (NCH(ch) >= 2) { |
| 3160 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3161 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3162 | if (!expression) |
| 3163 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3164 | if (NCH(ch) == 4) { |
| 3165 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3166 | if (!cause) |
| 3167 | return NULL; |
| 3168 | } |
| 3169 | 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] | 3170 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3171 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3172 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3173 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3174 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3175 | return NULL; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3180 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | { |
| 3182 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3183 | import_as_name: NAME ['as' NAME] |
| 3184 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | dotted_name: NAME ('.' NAME)* |
| 3186 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3187 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3188 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3189 | loop: |
| 3190 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3191 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3192 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3193 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3194 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3195 | if (!name) |
| 3196 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3197 | if (NCH(n) == 3) { |
| 3198 | node *str_node = CHILD(n, 2); |
| 3199 | str = NEW_IDENTIFIER(str_node); |
| 3200 | if (!str) |
| 3201 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3202 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3203 | return NULL; |
| 3204 | } |
| 3205 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3206 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3207 | return NULL; |
| 3208 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3209 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3210 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3211 | case dotted_as_name: |
| 3212 | if (NCH(n) == 1) { |
| 3213 | n = CHILD(n, 0); |
| 3214 | goto loop; |
| 3215 | } |
| 3216 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3217 | node *asname_node = CHILD(n, 2); |
| 3218 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3219 | if (!a) |
| 3220 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3221 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3222 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3223 | if (!a->asname) |
| 3224 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3225 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3226 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3227 | return a; |
| 3228 | } |
| 3229 | break; |
| 3230 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3231 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3232 | node *name_node = CHILD(n, 0); |
| 3233 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3234 | if (!name) |
| 3235 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3236 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3237 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3238 | return alias(name, NULL, c->c_arena); |
| 3239 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | else { |
| 3241 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3242 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3243 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3244 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3246 | |
| 3247 | len = 0; |
| 3248 | for (i = 0; i < NCH(n); i += 2) |
| 3249 | /* length of string plus one for the dot */ |
| 3250 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3251 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3252 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3253 | if (!str) |
| 3254 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3255 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3256 | if (!s) |
| 3257 | return NULL; |
| 3258 | for (i = 0; i < NCH(n); i += 2) { |
| 3259 | char *sch = STR(CHILD(n, i)); |
| 3260 | strcpy(s, STR(CHILD(n, i))); |
| 3261 | s += strlen(sch); |
| 3262 | *s++ = '.'; |
| 3263 | } |
| 3264 | --s; |
| 3265 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3267 | PyBytes_GET_SIZE(str), |
| 3268 | NULL); |
| 3269 | Py_DECREF(str); |
| 3270 | if (!uni) |
| 3271 | return NULL; |
| 3272 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3273 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3274 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3275 | Py_DECREF(str); |
| 3276 | return NULL; |
| 3277 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3278 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3279 | } |
| 3280 | break; |
| 3281 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3282 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3283 | if (!str) |
| 3284 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3285 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3286 | Py_DECREF(str); |
| 3287 | return NULL; |
| 3288 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3289 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3290 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3291 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3292 | "unexpected import name: %d", TYPE(n)); |
| 3293 | return NULL; |
| 3294 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3295 | |
| 3296 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3297 | return NULL; |
| 3298 | } |
| 3299 | |
| 3300 | static stmt_ty |
| 3301 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3302 | { |
| 3303 | /* |
| 3304 | import_stmt: import_name | import_from |
| 3305 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3306 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3307 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3308 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3309 | int lineno; |
| 3310 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3311 | int i; |
| 3312 | asdl_seq *aliases; |
| 3313 | |
| 3314 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3315 | lineno = LINENO(n); |
| 3316 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3317 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3318 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3319 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3320 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3321 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3322 | if (!aliases) |
| 3323 | return NULL; |
| 3324 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3325 | 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] | 3326 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3327 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3328 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3329 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3330 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3331 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3332 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3333 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3334 | int idx, ndots = 0; |
| 3335 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3336 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3338 | /* Count the number of dots (for relative imports) and check for the |
| 3339 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3340 | for (idx = 1; idx < NCH(n); idx++) { |
| 3341 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3342 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3343 | if (!mod) |
| 3344 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3345 | idx++; |
| 3346 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3347 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3349 | ndots += 3; |
| 3350 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3351 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3352 | break; |
| 3353 | } |
| 3354 | ndots++; |
| 3355 | } |
| 3356 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3357 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3358 | case STAR: |
| 3359 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3360 | n = CHILD(n, idx); |
| 3361 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3362 | break; |
| 3363 | case LPAR: |
| 3364 | /* from ... import (x, y, z) */ |
| 3365 | n = CHILD(n, idx + 1); |
| 3366 | n_children = NCH(n); |
| 3367 | break; |
| 3368 | case import_as_names: |
| 3369 | /* from ... import x, y, z */ |
| 3370 | n = CHILD(n, idx); |
| 3371 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3372 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3373 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3374 | " surrounding parentheses"); |
| 3375 | return NULL; |
| 3376 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3377 | break; |
| 3378 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3379 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3380 | return NULL; |
| 3381 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3382 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3383 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3384 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3385 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3386 | |
| 3387 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3388 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3389 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3390 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3391 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3392 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3393 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3394 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3395 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3396 | 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] | 3397 | if (!import_alias) |
| 3398 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3399 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3400 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3401 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3402 | if (mod != NULL) |
| 3403 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3404 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3405 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3406 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3407 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3408 | "unknown import statement: starts with command '%s'", |
| 3409 | STR(CHILD(n, 0))); |
| 3410 | return NULL; |
| 3411 | } |
| 3412 | |
| 3413 | static stmt_ty |
| 3414 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3415 | { |
| 3416 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3417 | identifier name; |
| 3418 | asdl_seq *s; |
| 3419 | int i; |
| 3420 | |
| 3421 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3422 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3423 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3424 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3425 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3426 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3427 | if (!name) |
| 3428 | return NULL; |
| 3429 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3430 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3431 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3435 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3436 | { |
| 3437 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3438 | identifier name; |
| 3439 | asdl_seq *s; |
| 3440 | int i; |
| 3441 | |
| 3442 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3443 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3444 | if (!s) |
| 3445 | return NULL; |
| 3446 | for (i = 1; i < NCH(n); i += 2) { |
| 3447 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3448 | if (!name) |
| 3449 | return NULL; |
| 3450 | asdl_seq_SET(s, i / 2, name); |
| 3451 | } |
| 3452 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3453 | } |
| 3454 | |
| 3455 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3456 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3457 | { |
| 3458 | /* assert_stmt: 'assert' test [',' test] */ |
| 3459 | REQ(n, assert_stmt); |
| 3460 | if (NCH(n) == 2) { |
| 3461 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3462 | if (!expression) |
| 3463 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3464 | 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] | 3465 | } |
| 3466 | else if (NCH(n) == 4) { |
| 3467 | expr_ty expr1, expr2; |
| 3468 | |
| 3469 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3470 | if (!expr1) |
| 3471 | return NULL; |
| 3472 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3473 | if (!expr2) |
| 3474 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3475 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3476 | 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] | 3477 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3478 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3479 | "improper number of parts to 'assert' statement: %d", |
| 3480 | NCH(n)); |
| 3481 | return NULL; |
| 3482 | } |
| 3483 | |
| 3484 | static asdl_seq * |
| 3485 | ast_for_suite(struct compiling *c, const node *n) |
| 3486 | { |
| 3487 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3488 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3489 | stmt_ty s; |
| 3490 | int i, total, num, end, pos = 0; |
| 3491 | node *ch; |
| 3492 | |
| 3493 | REQ(n, suite); |
| 3494 | |
| 3495 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3496 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3497 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3498 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3499 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3500 | n = CHILD(n, 0); |
| 3501 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3502 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3503 | */ |
| 3504 | end = NCH(n) - 1; |
| 3505 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3506 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3507 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3508 | for (i = 0; i < end; i += 2) { |
| 3509 | ch = CHILD(n, i); |
| 3510 | s = ast_for_stmt(c, ch); |
| 3511 | if (!s) |
| 3512 | return NULL; |
| 3513 | asdl_seq_SET(seq, pos++, s); |
| 3514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3515 | } |
| 3516 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3517 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3518 | ch = CHILD(n, i); |
| 3519 | REQ(ch, stmt); |
| 3520 | num = num_stmts(ch); |
| 3521 | if (num == 1) { |
| 3522 | /* small_stmt or compound_stmt with only one child */ |
| 3523 | s = ast_for_stmt(c, ch); |
| 3524 | if (!s) |
| 3525 | return NULL; |
| 3526 | asdl_seq_SET(seq, pos++, s); |
| 3527 | } |
| 3528 | else { |
| 3529 | int j; |
| 3530 | ch = CHILD(ch, 0); |
| 3531 | REQ(ch, simple_stmt); |
| 3532 | for (j = 0; j < NCH(ch); j += 2) { |
| 3533 | /* statement terminates with a semi-colon ';' */ |
| 3534 | if (NCH(CHILD(ch, j)) == 0) { |
| 3535 | assert((j + 1) == NCH(ch)); |
| 3536 | break; |
| 3537 | } |
| 3538 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3539 | if (!s) |
| 3540 | return NULL; |
| 3541 | asdl_seq_SET(seq, pos++, s); |
| 3542 | } |
| 3543 | } |
| 3544 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | } |
| 3546 | assert(pos == seq->size); |
| 3547 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | static stmt_ty |
| 3551 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3552 | { |
| 3553 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3554 | ['else' ':' suite] |
| 3555 | */ |
| 3556 | char *s; |
| 3557 | |
| 3558 | REQ(n, if_stmt); |
| 3559 | |
| 3560 | if (NCH(n) == 4) { |
| 3561 | expr_ty expression; |
| 3562 | asdl_seq *suite_seq; |
| 3563 | |
| 3564 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3565 | if (!expression) |
| 3566 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3568 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3569 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3571 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3572 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3573 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3574 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3575 | s = STR(CHILD(n, 4)); |
| 3576 | /* s[2], the third character in the string, will be |
| 3577 | 's' for el_s_e, or |
| 3578 | 'i' for el_i_f |
| 3579 | */ |
| 3580 | if (s[2] == 's') { |
| 3581 | expr_ty expression; |
| 3582 | asdl_seq *seq1, *seq2; |
| 3583 | |
| 3584 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3585 | if (!expression) |
| 3586 | return NULL; |
| 3587 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3588 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3589 | return NULL; |
| 3590 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3591 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3592 | return NULL; |
| 3593 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3594 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3595 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3596 | } |
| 3597 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3598 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3599 | expr_ty expression; |
| 3600 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3601 | asdl_seq *orelse = NULL; |
| 3602 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3603 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3604 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3605 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3606 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3607 | has_else = 1; |
| 3608 | n_elif -= 3; |
| 3609 | } |
| 3610 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3611 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3612 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3613 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3614 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3615 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3616 | if (!orelse) |
| 3617 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3618 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3619 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3620 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3621 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3622 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3623 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3624 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3625 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3626 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | asdl_seq_SET(orelse, 0, |
| 3629 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3630 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3631 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3632 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3633 | /* the just-created orelse handled the last elif */ |
| 3634 | n_elif--; |
| 3635 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3636 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3637 | for (i = 0; i < n_elif; i++) { |
| 3638 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3639 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3640 | if (!newobj) |
| 3641 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3642 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3643 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3646 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3647 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3648 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3649 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3650 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3651 | LINENO(CHILD(n, off)), |
| 3652 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3653 | orelse = newobj; |
| 3654 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3655 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3656 | if (!expression) |
| 3657 | return NULL; |
| 3658 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3659 | if (!suite_seq) |
| 3660 | return NULL; |
| 3661 | return If(expression, suite_seq, orelse, |
| 3662 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3663 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3664 | |
| 3665 | PyErr_Format(PyExc_SystemError, |
| 3666 | "unexpected token in 'if' statement: %s", s); |
| 3667 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | static stmt_ty |
| 3671 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3672 | { |
| 3673 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3674 | REQ(n, while_stmt); |
| 3675 | |
| 3676 | if (NCH(n) == 4) { |
| 3677 | expr_ty expression; |
| 3678 | asdl_seq *suite_seq; |
| 3679 | |
| 3680 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3681 | if (!expression) |
| 3682 | return NULL; |
| 3683 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3684 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3685 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3686 | 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] | 3687 | } |
| 3688 | else if (NCH(n) == 7) { |
| 3689 | expr_ty expression; |
| 3690 | asdl_seq *seq1, *seq2; |
| 3691 | |
| 3692 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3693 | if (!expression) |
| 3694 | return NULL; |
| 3695 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3696 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3697 | return NULL; |
| 3698 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3699 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | return NULL; |
| 3701 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3702 | 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] | 3703 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3704 | |
| 3705 | PyErr_Format(PyExc_SystemError, |
| 3706 | "wrong number of tokens for 'while' statement: %d", |
| 3707 | NCH(n)); |
| 3708 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3712 | ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3713 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3714 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3715 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3717 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3718 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3720 | REQ(n, for_stmt); |
| 3721 | |
| 3722 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3723 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3724 | if (!seq) |
| 3725 | return NULL; |
| 3726 | } |
| 3727 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3728 | node_target = CHILD(n, 1); |
| 3729 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3730 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3731 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3732 | /* Check the # of children rather than the length of _target, since |
| 3733 | 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] | 3734 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3735 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3736 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3737 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3738 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3739 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3740 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3741 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3742 | return NULL; |
| 3743 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3744 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3745 | return NULL; |
| 3746 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3747 | if (is_async) |
| 3748 | return AsyncFor(target, expression, suite_seq, seq, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3749 | LINENO(n0), n0->n_col_offset, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3750 | c->c_arena); |
| 3751 | else |
| 3752 | return For(target, expression, suite_seq, seq, |
| 3753 | LINENO(n), n->n_col_offset, |
| 3754 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | static excepthandler_ty |
| 3758 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3759 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3760 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3761 | REQ(exc, except_clause); |
| 3762 | REQ(body, suite); |
| 3763 | |
| 3764 | if (NCH(exc) == 1) { |
| 3765 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3766 | if (!suite_seq) |
| 3767 | return NULL; |
| 3768 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3769 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3770 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3771 | } |
| 3772 | else if (NCH(exc) == 2) { |
| 3773 | expr_ty expression; |
| 3774 | asdl_seq *suite_seq; |
| 3775 | |
| 3776 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3777 | if (!expression) |
| 3778 | return NULL; |
| 3779 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3780 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3781 | return NULL; |
| 3782 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3783 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3784 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3785 | } |
| 3786 | else if (NCH(exc) == 4) { |
| 3787 | asdl_seq *suite_seq; |
| 3788 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3789 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3790 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3791 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3792 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3793 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3794 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3795 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3796 | return NULL; |
| 3797 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3798 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3799 | return NULL; |
| 3800 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3801 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3802 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3803 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3804 | |
| 3805 | PyErr_Format(PyExc_SystemError, |
| 3806 | "wrong number of children for 'except' clause: %d", |
| 3807 | NCH(exc)); |
| 3808 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | static stmt_ty |
| 3812 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3813 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3814 | const int nch = NCH(n); |
| 3815 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3816 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3817 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3818 | REQ(n, try_stmt); |
| 3819 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3820 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3821 | if (body == NULL) |
| 3822 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3823 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3824 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3825 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3826 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3827 | /* we can assume it's an "else", |
| 3828 | because nch >= 9 for try-else-finally and |
| 3829 | it would otherwise have a type of except_clause */ |
| 3830 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3831 | if (orelse == NULL) |
| 3832 | return NULL; |
| 3833 | n_except--; |
| 3834 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3835 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3836 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3837 | if (finally == NULL) |
| 3838 | return NULL; |
| 3839 | n_except--; |
| 3840 | } |
| 3841 | else { |
| 3842 | /* we can assume it's an "else", |
| 3843 | otherwise it would have a type of except_clause */ |
| 3844 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3845 | if (orelse == NULL) |
| 3846 | return NULL; |
| 3847 | n_except--; |
| 3848 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3849 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3850 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3851 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3852 | return NULL; |
| 3853 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3854 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3855 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3856 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3857 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3858 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3859 | if (handlers == NULL) |
| 3860 | return NULL; |
| 3861 | |
| 3862 | for (i = 0; i < n_except; i++) { |
| 3863 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3864 | CHILD(n, 5 + i * 3)); |
| 3865 | if (!e) |
| 3866 | return NULL; |
| 3867 | asdl_seq_SET(handlers, i, e); |
| 3868 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3871 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3872 | 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] | 3873 | } |
| 3874 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3875 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3876 | static withitem_ty |
| 3877 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3878 | { |
| 3879 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3880 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3881 | REQ(n, with_item); |
| 3882 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3883 | if (!context_expr) |
| 3884 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3885 | if (NCH(n) == 3) { |
| 3886 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3887 | |
| 3888 | if (!optional_vars) { |
| 3889 | return NULL; |
| 3890 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3891 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3892 | return NULL; |
| 3893 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3896 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3899 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3900 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3901 | ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3902 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3903 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3904 | int i, n_items; |
| 3905 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3906 | |
| 3907 | REQ(n, with_stmt); |
| 3908 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3909 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3910 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3911 | if (!items) |
| 3912 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3913 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3914 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3915 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3916 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3917 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3920 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3921 | if (!body) |
| 3922 | return NULL; |
| 3923 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3924 | if (is_async) |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3925 | return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3926 | else |
| 3927 | 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] | 3928 | } |
| 3929 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3930 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3931 | 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] | 3932 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3933 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3934 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3935 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3936 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3937 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3938 | REQ(n, classdef); |
| 3939 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3940 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3941 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3942 | if (!s) |
| 3943 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3944 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3945 | if (!classname) |
| 3946 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3947 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3948 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3949 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3950 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3951 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3952 | |
| 3953 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3954 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3955 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3956 | return NULL; |
| 3957 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3958 | if (!classname) |
| 3959 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3960 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3961 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3962 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3963 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3966 | /* class NAME '(' arglist ')' ':' suite */ |
| 3967 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3968 | { |
| 3969 | PyObject *dummy_name; |
| 3970 | expr_ty dummy; |
| 3971 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3972 | if (!dummy_name) |
| 3973 | return NULL; |
| 3974 | 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] | 3975 | call = ast_for_call(c, CHILD(n, 3), dummy, false); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3976 | if (!call) |
| 3977 | return NULL; |
| 3978 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3979 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3980 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3981 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3982 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3983 | if (!classname) |
| 3984 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3985 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3986 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3987 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3988 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3989 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | static stmt_ty |
| 3993 | ast_for_stmt(struct compiling *c, const node *n) |
| 3994 | { |
| 3995 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3996 | assert(NCH(n) == 1); |
| 3997 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3998 | } |
| 3999 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4000 | assert(num_stmts(n) == 1); |
| 4001 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4002 | } |
| 4003 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4004 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4005 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4006 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4007 | */ |
| 4008 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4009 | case expr_stmt: |
| 4010 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4011 | case del_stmt: |
| 4012 | return ast_for_del_stmt(c, n); |
| 4013 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 4014 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4015 | case flow_stmt: |
| 4016 | return ast_for_flow_stmt(c, n); |
| 4017 | case import_stmt: |
| 4018 | return ast_for_import_stmt(c, n); |
| 4019 | case global_stmt: |
| 4020 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4021 | case nonlocal_stmt: |
| 4022 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4023 | case assert_stmt: |
| 4024 | return ast_for_assert_stmt(c, n); |
| 4025 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4026 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4027 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4028 | TYPE(n), NCH(n)); |
| 4029 | return NULL; |
| 4030 | } |
| 4031 | } |
| 4032 | else { |
| 4033 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4034 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4035 | */ |
| 4036 | node *ch = CHILD(n, 0); |
| 4037 | REQ(n, compound_stmt); |
| 4038 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4039 | case if_stmt: |
| 4040 | return ast_for_if_stmt(c, ch); |
| 4041 | case while_stmt: |
| 4042 | return ast_for_while_stmt(c, ch); |
| 4043 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4044 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4045 | case try_stmt: |
| 4046 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4047 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4048 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4049 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4050 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4051 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4052 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | case decorated: |
| 4054 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4055 | case async_stmt: |
| 4056 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4057 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4058 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4059 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4060 | TYPE(n), NCH(n)); |
| 4061 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4062 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | |
| 4066 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4067 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4068 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4069 | const char *end; |
| 4070 | long x; |
| 4071 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4072 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4073 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4074 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4075 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4076 | errno = 0; |
| 4077 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4078 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4079 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4080 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4081 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4082 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4083 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4084 | } |
| 4085 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4086 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4087 | if (*end == '\0') { |
| 4088 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4089 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4090 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4091 | } |
| 4092 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4093 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4094 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4095 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4096 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4097 | return NULL; |
| 4098 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4099 | } |
| 4100 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4101 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4102 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4103 | if (dx == -1.0 && PyErr_Occurred()) |
| 4104 | return NULL; |
| 4105 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4106 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4110 | parsenumber(struct compiling *c, const char *s) |
| 4111 | { |
| 4112 | char *dup, *end; |
| 4113 | PyObject *res = NULL; |
| 4114 | |
| 4115 | assert(s != NULL); |
| 4116 | |
| 4117 | if (strchr(s, '_') == NULL) { |
| 4118 | return parsenumber_raw(c, s); |
| 4119 | } |
| 4120 | /* Create a duplicate without underscores. */ |
| 4121 | dup = PyMem_Malloc(strlen(s) + 1); |
| 4122 | end = dup; |
| 4123 | for (; *s; s++) { |
| 4124 | if (*s != '_') { |
| 4125 | *end++ = *s; |
| 4126 | } |
| 4127 | } |
| 4128 | *end = '\0'; |
| 4129 | res = parsenumber_raw(c, dup); |
| 4130 | PyMem_Free(dup); |
| 4131 | return res; |
| 4132 | } |
| 4133 | |
| 4134 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4135 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4136 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4137 | const char *s, *t; |
| 4138 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4139 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4140 | while (s < end && (*s & 0x80)) s++; |
| 4141 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4142 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4145 | static int |
| 4146 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4147 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4148 | { |
| 4149 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4150 | first_invalid_escape_char); |
| 4151 | if (msg == NULL) { |
| 4152 | return -1; |
| 4153 | } |
| 4154 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
| 4155 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4156 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4157 | { |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4158 | if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) { |
| 4159 | const char *s; |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4160 | |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4161 | /* Replace the DeprecationWarning exception with a SyntaxError |
| 4162 | to get a more accurate error report */ |
| 4163 | PyErr_Clear(); |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4164 | |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4165 | s = PyUnicode_AsUTF8(msg); |
| 4166 | if (s != NULL) { |
| 4167 | ast_error(c, n, s); |
| 4168 | } |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4169 | } |
| 4170 | Py_DECREF(msg); |
| 4171 | return -1; |
| 4172 | } |
| 4173 | Py_DECREF(msg); |
| 4174 | return 0; |
| 4175 | } |
| 4176 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4177 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4178 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4179 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4180 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4181 | PyObject *v, *u; |
| 4182 | char *buf; |
| 4183 | char *p; |
| 4184 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4185 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4186 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4187 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4188 | return NULL; |
| 4189 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4190 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4191 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4192 | if (u == NULL) |
| 4193 | return NULL; |
| 4194 | p = buf = PyBytes_AsString(u); |
| 4195 | end = s + len; |
| 4196 | while (s < end) { |
| 4197 | if (*s == '\\') { |
| 4198 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4199 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4200 | strcpy(p, "u005c"); |
| 4201 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4202 | if (s >= end) |
| 4203 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4204 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4205 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4206 | if (*s & 0x80) { /* XXX inefficient */ |
| 4207 | PyObject *w; |
| 4208 | int kind; |
| 4209 | void *data; |
| 4210 | Py_ssize_t len, i; |
| 4211 | w = decode_utf8(c, &s, end); |
| 4212 | if (w == NULL) { |
| 4213 | Py_DECREF(u); |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | kind = PyUnicode_KIND(w); |
| 4217 | data = PyUnicode_DATA(w); |
| 4218 | len = PyUnicode_GET_LENGTH(w); |
| 4219 | for (i = 0; i < len; i++) { |
| 4220 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4221 | sprintf(p, "\\U%08x", chr); |
| 4222 | p += 10; |
| 4223 | } |
| 4224 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4225 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4226 | Py_DECREF(w); |
| 4227 | } else { |
| 4228 | *p++ = *s++; |
| 4229 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4230 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4231 | len = p - buf; |
| 4232 | s = buf; |
| 4233 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4234 | const char *first_invalid_escape; |
| 4235 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4236 | |
| 4237 | if (v != NULL && first_invalid_escape != NULL) { |
| 4238 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4239 | /* We have not decref u before because first_invalid_escape points |
| 4240 | inside u. */ |
| 4241 | Py_XDECREF(u); |
| 4242 | Py_DECREF(v); |
| 4243 | return NULL; |
| 4244 | } |
| 4245 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4246 | Py_XDECREF(u); |
| 4247 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4250 | static PyObject * |
| 4251 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4252 | size_t len) |
| 4253 | { |
| 4254 | const char *first_invalid_escape; |
| 4255 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4256 | &first_invalid_escape); |
| 4257 | if (result == NULL) |
| 4258 | return NULL; |
| 4259 | |
| 4260 | if (first_invalid_escape != NULL) { |
| 4261 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4262 | Py_DECREF(result); |
| 4263 | return NULL; |
| 4264 | } |
| 4265 | } |
| 4266 | return result; |
| 4267 | } |
| 4268 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4269 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4270 | and `col_offset` to existing locations. */ |
| 4271 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4272 | { |
| 4273 | n->n_col_offset = n->n_col_offset + col_offset; |
| 4274 | for (int i = 0; i < NCH(n); ++i) { |
| 4275 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4276 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4277 | col_offset = 0; |
| 4278 | } |
| 4279 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4280 | } |
| 4281 | n->n_lineno = n->n_lineno + lineno; |
| 4282 | } |
| 4283 | |
| 4284 | /* Fix locations for the given node and its children. |
| 4285 | |
| 4286 | `parent` is the enclosing node. |
| 4287 | `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] | 4288 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4289 | */ |
| 4290 | static void |
| 4291 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4292 | { |
| 4293 | char *substr = NULL; |
| 4294 | char *start; |
| 4295 | int lines = LINENO(parent) - 1; |
| 4296 | int cols = parent->n_col_offset; |
| 4297 | /* Find the full fstring to fix location information in `n`. */ |
| 4298 | while (parent && parent->n_type != STRING) |
| 4299 | parent = parent->n_child; |
| 4300 | if (parent && parent->n_str) { |
| 4301 | substr = strstr(parent->n_str, expr_str); |
| 4302 | if (substr) { |
| 4303 | start = substr; |
| 4304 | while (start > parent->n_str) { |
| 4305 | if (start[0] == '\n') |
| 4306 | break; |
| 4307 | start--; |
| 4308 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4309 | cols += (int)(substr - start); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4310 | /* Fix lineno in mulitline strings. */ |
| 4311 | while ((substr = strchr(substr + 1, '\n'))) |
| 4312 | lines--; |
| 4313 | } |
| 4314 | } |
| 4315 | fstring_shift_node_locations(n, lines, cols); |
| 4316 | } |
| 4317 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4318 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4319 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4320 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4321 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4322 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4323 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4324 | { |
| 4325 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4326 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4327 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4328 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4329 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4330 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4331 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4332 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4333 | assert(*(expr_start-1) == '{'); |
| 4334 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4335 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4336 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4337 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4338 | because turning the expression '' in to '()' would go from being invalid |
| 4339 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4340 | for (s = expr_start; s != expr_end; s++) { |
| 4341 | char c = *s; |
| 4342 | /* The Python parser ignores only the following whitespace |
| 4343 | characters (\r already is converted to \n). */ |
| 4344 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4345 | break; |
| 4346 | } |
| 4347 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4348 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4349 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4350 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4351 | } |
| 4352 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4353 | len = expr_end - expr_start; |
| 4354 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4355 | str = PyMem_RawMalloc(len + 3); |
| 4356 | if (str == NULL) |
| 4357 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4358 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4359 | str[0] = '('; |
| 4360 | memcpy(str+1, expr_start, len); |
| 4361 | str[len+1] = ')'; |
| 4362 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4363 | |
| 4364 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4365 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4366 | Py_eval_input, 0); |
| 4367 | if (!mod_n) { |
| 4368 | PyMem_RawFree(str); |
| 4369 | return NULL; |
| 4370 | } |
| 4371 | /* Reuse str to find the correct column offset. */ |
| 4372 | str[0] = '{'; |
| 4373 | str[len+1] = '}'; |
| 4374 | fstring_fix_node_location(n, mod_n, str); |
| 4375 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4376 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4377 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4378 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4379 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4380 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | /* Return -1 on error. |
| 4384 | |
| 4385 | Return 0 if we reached the end of the literal. |
| 4386 | |
| 4387 | Return 1 if we haven't reached the end of the literal, but we want |
| 4388 | the caller to process the literal up to this point. Used for |
| 4389 | doubled braces. |
| 4390 | */ |
| 4391 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4392 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4393 | PyObject **literal, int recurse_lvl, |
| 4394 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4395 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4396 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4397 | brace (which isn't part of a unicode name escape such as |
| 4398 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4399 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4400 | const char *s = *str; |
| 4401 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4402 | int result = 0; |
| 4403 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4404 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4405 | while (s < end) { |
| 4406 | char ch = *s++; |
| 4407 | if (!raw && ch == '\\' && s < end) { |
| 4408 | ch = *s++; |
| 4409 | if (ch == 'N') { |
| 4410 | if (s < end && *s++ == '{') { |
| 4411 | while (s < end && *s++ != '}') { |
| 4412 | } |
| 4413 | continue; |
| 4414 | } |
| 4415 | break; |
| 4416 | } |
| 4417 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4418 | return -1; |
| 4419 | } |
| 4420 | } |
| 4421 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4422 | /* Check for doubled braces, but only at the top level. If |
| 4423 | we checked at every level, then f'{0:{3}}' would fail |
| 4424 | with the two closing braces. */ |
| 4425 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4426 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4427 | /* We're going to tell the caller that the literal ends |
| 4428 | here, but that they should continue scanning. But also |
| 4429 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4430 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4431 | result = 1; |
| 4432 | goto done; |
| 4433 | } |
| 4434 | |
| 4435 | /* Where a single '{' is the start of a new expression, a |
| 4436 | single '}' is not allowed. */ |
| 4437 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4438 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4439 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4440 | return -1; |
| 4441 | } |
| 4442 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4443 | /* We're either at a '{', which means we're starting another |
| 4444 | expression; or a '}', which means we're at the end of this |
| 4445 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4446 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4447 | break; |
| 4448 | } |
| 4449 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4450 | *str = s; |
| 4451 | assert(s <= end); |
| 4452 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4453 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4454 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4455 | if (raw) |
| 4456 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4457 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4458 | NULL, NULL); |
| 4459 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4460 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4461 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4462 | if (!*literal) |
| 4463 | return -1; |
| 4464 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4465 | return result; |
| 4466 | } |
| 4467 | |
| 4468 | /* Forward declaration because parsing is recursive. */ |
| 4469 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4470 | 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] | 4471 | struct compiling *c, const node *n); |
| 4472 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4473 | /* 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] | 4474 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4475 | which includes the expression, conversion character, and |
| 4476 | format_spec expression. |
| 4477 | |
| 4478 | Note that I don't do a perfect job here: I don't make sure that a |
| 4479 | closing brace doesn't match an opening paren, for example. It |
| 4480 | doesn't need to error on all invalid expressions, just correctly |
| 4481 | find the end of all valid ones. Any errors inside the expression |
| 4482 | will be caught when we parse it later. */ |
| 4483 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4484 | 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] | 4485 | expr_ty *expression, struct compiling *c, const node *n) |
| 4486 | { |
| 4487 | /* Return -1 on error, else 0. */ |
| 4488 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4489 | const char *expr_start; |
| 4490 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4491 | expr_ty simple_expression; |
| 4492 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4493 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4494 | |
| 4495 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4496 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4497 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4498 | |
| 4499 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4500 | int string_type = 0; |
| 4501 | |
| 4502 | /* Keep track of nesting level for braces/parens/brackets in |
| 4503 | expressions. */ |
| 4504 | Py_ssize_t nested_depth = 0; |
| 4505 | |
| 4506 | /* Can only nest one level deep. */ |
| 4507 | if (recurse_lvl >= 2) { |
| 4508 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4509 | return -1; |
| 4510 | } |
| 4511 | |
| 4512 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4513 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4514 | assert(**str == '{'); |
| 4515 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4516 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4517 | expr_start = *str; |
| 4518 | for (; *str < end; (*str)++) { |
| 4519 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4520 | |
| 4521 | /* Loop invariants. */ |
| 4522 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4523 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4524 | if (quote_char) |
| 4525 | assert(string_type == 1 || string_type == 3); |
| 4526 | else |
| 4527 | assert(string_type == 0); |
| 4528 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4529 | ch = **str; |
| 4530 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4531 | if (ch == '\\') { |
| 4532 | /* Error: can't include a backslash character, inside |
| 4533 | parens or strings or not. */ |
| 4534 | ast_error(c, n, "f-string expression part " |
| 4535 | "cannot include a backslash"); |
| 4536 | return -1; |
| 4537 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4538 | if (quote_char) { |
| 4539 | /* We're inside a string. See if we're at the end. */ |
| 4540 | /* This code needs to implement the same non-error logic |
| 4541 | as tok_get from tokenizer.c, at the letter_quote |
| 4542 | label. To actually share that code would be a |
| 4543 | nightmare. But, it's unlikely to change and is small, |
| 4544 | so duplicate it here. Note we don't need to catch all |
| 4545 | of the errors, since they'll be caught when parsing the |
| 4546 | expression. We just need to match the non-error |
| 4547 | cases. Thus we can ignore \n in single-quoted strings, |
| 4548 | for example. Or non-terminated strings. */ |
| 4549 | if (ch == quote_char) { |
| 4550 | /* Does this match the string_type (single or triple |
| 4551 | quoted)? */ |
| 4552 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4553 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4554 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4555 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4556 | string_type = 0; |
| 4557 | quote_char = 0; |
| 4558 | continue; |
| 4559 | } |
| 4560 | } else { |
| 4561 | /* We're at the end of a normal string. */ |
| 4562 | quote_char = 0; |
| 4563 | string_type = 0; |
| 4564 | continue; |
| 4565 | } |
| 4566 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4567 | } else if (ch == '\'' || ch == '"') { |
| 4568 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4569 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4570 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4571 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4572 | } else { |
| 4573 | /* Start of a normal string. */ |
| 4574 | string_type = 1; |
| 4575 | } |
| 4576 | /* Start looking for the end of the string. */ |
| 4577 | quote_char = ch; |
| 4578 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4579 | nested_depth++; |
| 4580 | } else if (nested_depth != 0 && |
| 4581 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4582 | nested_depth--; |
| 4583 | } else if (ch == '#') { |
| 4584 | /* Error: can't include a comment character, inside parens |
| 4585 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4586 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4587 | return -1; |
| 4588 | } else if (nested_depth == 0 && |
| 4589 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4590 | /* First, test for the special case of "!=". Since '=' is |
| 4591 | not an allowed conversion character, nothing is lost in |
| 4592 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4593 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4594 | /* This isn't a conversion character, just continue. */ |
| 4595 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4596 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4597 | /* Normal way out of this loop. */ |
| 4598 | break; |
| 4599 | } else { |
| 4600 | /* Just consume this char and loop around. */ |
| 4601 | } |
| 4602 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4603 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4604 | /* If we leave this loop in a string or with mismatched parens, we |
| 4605 | don't care. We'll get a syntax error when compiling the |
| 4606 | expression. But, we can produce a better error message, so |
| 4607 | let's just do that.*/ |
| 4608 | if (quote_char) { |
| 4609 | ast_error(c, n, "f-string: unterminated string"); |
| 4610 | return -1; |
| 4611 | } |
| 4612 | if (nested_depth) { |
| 4613 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4614 | return -1; |
| 4615 | } |
| 4616 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4617 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4618 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4619 | |
| 4620 | /* Compile the expression as soon as possible, so we show errors |
| 4621 | related to the expression before errors related to the |
| 4622 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4623 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4624 | if (!simple_expression) |
| 4625 | return -1; |
| 4626 | |
| 4627 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4628 | if (**str == '!') { |
| 4629 | *str += 1; |
| 4630 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4631 | goto unexpected_end_of_string; |
| 4632 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4633 | conversion = **str; |
| 4634 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4635 | |
| 4636 | /* Validate the conversion. */ |
| 4637 | if (!(conversion == 's' || conversion == 'r' |
| 4638 | || conversion == 'a')) { |
| 4639 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4640 | "expected 's', 'r', or 'a'"); |
| 4641 | return -1; |
| 4642 | } |
| 4643 | } |
| 4644 | |
| 4645 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4646 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4647 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4648 | if (**str == ':') { |
| 4649 | *str += 1; |
| 4650 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4651 | goto unexpected_end_of_string; |
| 4652 | |
| 4653 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4654 | 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] | 4655 | if (!format_spec) |
| 4656 | return -1; |
| 4657 | } |
| 4658 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4659 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4660 | goto unexpected_end_of_string; |
| 4661 | |
| 4662 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4663 | assert(*str < end); |
| 4664 | assert(**str == '}'); |
| 4665 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4666 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4667 | /* And now create the FormattedValue node that represents this |
| 4668 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4669 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4670 | format_spec, LINENO(n), n->n_col_offset, |
| 4671 | c->c_arena); |
| 4672 | if (!*expression) |
| 4673 | return -1; |
| 4674 | |
| 4675 | return 0; |
| 4676 | |
| 4677 | unexpected_end_of_string: |
| 4678 | ast_error(c, n, "f-string: expecting '}'"); |
| 4679 | return -1; |
| 4680 | } |
| 4681 | |
| 4682 | /* Return -1 on error. |
| 4683 | |
| 4684 | Return 0 if we have a literal (possible zero length) and an |
| 4685 | expression (zero length if at the end of the string. |
| 4686 | |
| 4687 | Return 1 if we have a literal, but no expression, and we want the |
| 4688 | caller to call us again. This is used to deal with doubled |
| 4689 | braces. |
| 4690 | |
| 4691 | When called multiple times on the string 'a{{b{0}c', this function |
| 4692 | will return: |
| 4693 | |
| 4694 | 1. the literal 'a{' with no expression, and a return value |
| 4695 | of 1. Despite the fact that there's no expression, the return |
| 4696 | value of 1 means we're not finished yet. |
| 4697 | |
| 4698 | 2. the literal 'b' and the expression '0', with a return value of |
| 4699 | 0. The fact that there's an expression means we're not finished. |
| 4700 | |
| 4701 | 3. literal 'c' with no expression and a return value of 0. The |
| 4702 | combination of the return value of 0 with no expression means |
| 4703 | we're finished. |
| 4704 | */ |
| 4705 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4706 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4707 | int recurse_lvl, PyObject **literal, |
| 4708 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4709 | struct compiling *c, const node *n) |
| 4710 | { |
| 4711 | int result; |
| 4712 | |
| 4713 | assert(*literal == NULL && *expression == NULL); |
| 4714 | |
| 4715 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4716 | 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] | 4717 | if (result < 0) |
| 4718 | goto error; |
| 4719 | |
| 4720 | assert(result == 0 || result == 1); |
| 4721 | |
| 4722 | if (result == 1) |
| 4723 | /* We have a literal, but don't look at the expression. */ |
| 4724 | return 1; |
| 4725 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4726 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4727 | /* We're at the end of the string or the end of a nested |
| 4728 | f-string: no expression. The top-level error case where we |
| 4729 | expect to be at the end of the string but we're at a '}' is |
| 4730 | handled later. */ |
| 4731 | return 0; |
| 4732 | |
| 4733 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4734 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4735 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4736 | 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] | 4737 | goto error; |
| 4738 | |
| 4739 | return 0; |
| 4740 | |
| 4741 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4742 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4743 | return -1; |
| 4744 | } |
| 4745 | |
| 4746 | #define EXPRLIST_N_CACHED 64 |
| 4747 | |
| 4748 | typedef struct { |
| 4749 | /* Incrementally build an array of expr_ty, so be used in an |
| 4750 | asdl_seq. Cache some small but reasonably sized number of |
| 4751 | expr_ty's, and then after that start dynamically allocating, |
| 4752 | doubling the number allocated each time. Note that the f-string |
| 4753 | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
| 4754 | Str for the literal 'a'. So you add expr_ty's about twice as |
| 4755 | fast as you add exressions in an f-string. */ |
| 4756 | |
| 4757 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4758 | Py_ssize_t size; /* Number we've used. */ |
| 4759 | expr_ty *p; /* Pointer to the memory we're actually |
| 4760 | using. Will point to 'data' until we |
| 4761 | start dynamically allocating. */ |
| 4762 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4763 | } ExprList; |
| 4764 | |
| 4765 | #ifdef NDEBUG |
| 4766 | #define ExprList_check_invariants(l) |
| 4767 | #else |
| 4768 | static void |
| 4769 | ExprList_check_invariants(ExprList *l) |
| 4770 | { |
| 4771 | /* Check our invariants. Make sure this object is "live", and |
| 4772 | hasn't been deallocated. */ |
| 4773 | assert(l->size >= 0); |
| 4774 | assert(l->p != NULL); |
| 4775 | if (l->size <= EXPRLIST_N_CACHED) |
| 4776 | assert(l->data == l->p); |
| 4777 | } |
| 4778 | #endif |
| 4779 | |
| 4780 | static void |
| 4781 | ExprList_Init(ExprList *l) |
| 4782 | { |
| 4783 | l->allocated = EXPRLIST_N_CACHED; |
| 4784 | l->size = 0; |
| 4785 | |
| 4786 | /* Until we start allocating dynamically, p points to data. */ |
| 4787 | l->p = l->data; |
| 4788 | |
| 4789 | ExprList_check_invariants(l); |
| 4790 | } |
| 4791 | |
| 4792 | static int |
| 4793 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4794 | { |
| 4795 | ExprList_check_invariants(l); |
| 4796 | if (l->size >= l->allocated) { |
| 4797 | /* We need to alloc (or realloc) the memory. */ |
| 4798 | Py_ssize_t new_size = l->allocated * 2; |
| 4799 | |
| 4800 | /* See if we've ever allocated anything dynamically. */ |
| 4801 | if (l->p == l->data) { |
| 4802 | Py_ssize_t i; |
| 4803 | /* We're still using the cached data. Switch to |
| 4804 | alloc-ing. */ |
| 4805 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4806 | if (!l->p) |
| 4807 | return -1; |
| 4808 | /* Copy the cached data into the new buffer. */ |
| 4809 | for (i = 0; i < l->size; i++) |
| 4810 | l->p[i] = l->data[i]; |
| 4811 | } else { |
| 4812 | /* Just realloc. */ |
| 4813 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4814 | if (!tmp) { |
| 4815 | PyMem_RawFree(l->p); |
| 4816 | l->p = NULL; |
| 4817 | return -1; |
| 4818 | } |
| 4819 | l->p = tmp; |
| 4820 | } |
| 4821 | |
| 4822 | l->allocated = new_size; |
| 4823 | assert(l->allocated == 2 * l->size); |
| 4824 | } |
| 4825 | |
| 4826 | l->p[l->size++] = exp; |
| 4827 | |
| 4828 | ExprList_check_invariants(l); |
| 4829 | return 0; |
| 4830 | } |
| 4831 | |
| 4832 | static void |
| 4833 | ExprList_Dealloc(ExprList *l) |
| 4834 | { |
| 4835 | ExprList_check_invariants(l); |
| 4836 | |
| 4837 | /* If there's been an error, or we've never dynamically allocated, |
| 4838 | do nothing. */ |
| 4839 | if (!l->p || l->p == l->data) { |
| 4840 | /* Do nothing. */ |
| 4841 | } else { |
| 4842 | /* We have dynamically allocated. Free the memory. */ |
| 4843 | PyMem_RawFree(l->p); |
| 4844 | } |
| 4845 | l->p = NULL; |
| 4846 | l->size = -1; |
| 4847 | } |
| 4848 | |
| 4849 | static asdl_seq * |
| 4850 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4851 | { |
| 4852 | asdl_seq *seq; |
| 4853 | |
| 4854 | ExprList_check_invariants(l); |
| 4855 | |
| 4856 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4857 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4858 | if (seq) { |
| 4859 | Py_ssize_t i; |
| 4860 | for (i = 0; i < l->size; i++) |
| 4861 | asdl_seq_SET(seq, i, l->p[i]); |
| 4862 | } |
| 4863 | ExprList_Dealloc(l); |
| 4864 | return seq; |
| 4865 | } |
| 4866 | |
| 4867 | /* The FstringParser is designed to add a mix of strings and |
| 4868 | f-strings, and concat them together as needed. Ultimately, it |
| 4869 | generates an expr_ty. */ |
| 4870 | typedef struct { |
| 4871 | PyObject *last_str; |
| 4872 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4873 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4874 | } FstringParser; |
| 4875 | |
| 4876 | #ifdef NDEBUG |
| 4877 | #define FstringParser_check_invariants(state) |
| 4878 | #else |
| 4879 | static void |
| 4880 | FstringParser_check_invariants(FstringParser *state) |
| 4881 | { |
| 4882 | if (state->last_str) |
| 4883 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4884 | ExprList_check_invariants(&state->expr_list); |
| 4885 | } |
| 4886 | #endif |
| 4887 | |
| 4888 | static void |
| 4889 | FstringParser_Init(FstringParser *state) |
| 4890 | { |
| 4891 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4892 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4893 | ExprList_Init(&state->expr_list); |
| 4894 | FstringParser_check_invariants(state); |
| 4895 | } |
| 4896 | |
| 4897 | static void |
| 4898 | FstringParser_Dealloc(FstringParser *state) |
| 4899 | { |
| 4900 | FstringParser_check_invariants(state); |
| 4901 | |
| 4902 | Py_XDECREF(state->last_str); |
| 4903 | ExprList_Dealloc(&state->expr_list); |
| 4904 | } |
| 4905 | |
| 4906 | /* Make a Str node, but decref the PyUnicode object being added. */ |
| 4907 | static expr_ty |
| 4908 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4909 | { |
| 4910 | PyObject *s = *str; |
| 4911 | *str = NULL; |
| 4912 | assert(PyUnicode_CheckExact(s)); |
| 4913 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4914 | Py_DECREF(s); |
| 4915 | return NULL; |
| 4916 | } |
| 4917 | return Str(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 4918 | } |
| 4919 | |
| 4920 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4921 | decref'd. */ |
| 4922 | static int |
| 4923 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4924 | { |
| 4925 | FstringParser_check_invariants(state); |
| 4926 | |
| 4927 | assert(PyUnicode_CheckExact(str)); |
| 4928 | |
| 4929 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4930 | Py_DECREF(str); |
| 4931 | return 0; |
| 4932 | } |
| 4933 | |
| 4934 | if (!state->last_str) { |
| 4935 | /* We didn't have a string before, so just remember this one. */ |
| 4936 | state->last_str = str; |
| 4937 | } else { |
| 4938 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4939 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4940 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4941 | return -1; |
| 4942 | } |
| 4943 | FstringParser_check_invariants(state); |
| 4944 | return 0; |
| 4945 | } |
| 4946 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4947 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4948 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4949 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4950 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4951 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4952 | struct compiling *c, const node *n) |
| 4953 | { |
| 4954 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4955 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4956 | |
| 4957 | /* Parse the f-string. */ |
| 4958 | while (1) { |
| 4959 | PyObject *literal = NULL; |
| 4960 | expr_ty expression = NULL; |
| 4961 | |
| 4962 | /* If there's a zero length literal in front of the |
| 4963 | expression, literal will be NULL. If we're at the end of |
| 4964 | the f-string, expression will be NULL (unless result == 1, |
| 4965 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4966 | 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] | 4967 | &literal, &expression, |
| 4968 | c, n); |
| 4969 | if (result < 0) |
| 4970 | return -1; |
| 4971 | |
| 4972 | /* Add the literal, if any. */ |
| 4973 | if (!literal) { |
| 4974 | /* Do nothing. Just leave last_str alone (and possibly |
| 4975 | NULL). */ |
| 4976 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 4977 | /* Note that the literal can be zero length, if the |
| 4978 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4979 | state->last_str = literal; |
| 4980 | literal = NULL; |
| 4981 | } else { |
| 4982 | /* We have a literal, concatenate it. */ |
| 4983 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4984 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4985 | return -1; |
| 4986 | literal = NULL; |
| 4987 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4988 | |
| 4989 | /* We've dealt with the literal now. It can't be leaked on further |
| 4990 | errors. */ |
| 4991 | assert(literal == NULL); |
| 4992 | |
| 4993 | /* See if we should just loop around to get the next literal |
| 4994 | and expression, while ignoring the expression this |
| 4995 | time. This is used for un-doubling braces, as an |
| 4996 | optimization. */ |
| 4997 | if (result == 1) |
| 4998 | continue; |
| 4999 | |
| 5000 | if (!expression) |
| 5001 | /* We're done with this f-string. */ |
| 5002 | break; |
| 5003 | |
| 5004 | /* We know we have an expression. Convert any existing string |
| 5005 | to a Str node. */ |
| 5006 | if (!state->last_str) { |
| 5007 | /* Do nothing. No previous literal. */ |
| 5008 | } else { |
| 5009 | /* Convert the existing last_str literal to a Str node. */ |
| 5010 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5011 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5012 | return -1; |
| 5013 | } |
| 5014 | |
| 5015 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5016 | return -1; |
| 5017 | } |
| 5018 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5019 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5020 | string. Otherwise, we must be at a right brace. */ |
| 5021 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5022 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5023 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5024 | return -1; |
| 5025 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5026 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5027 | ast_error(c, n, "f-string: expecting '}'"); |
| 5028 | return -1; |
| 5029 | } |
| 5030 | |
| 5031 | FstringParser_check_invariants(state); |
| 5032 | return 0; |
| 5033 | } |
| 5034 | |
| 5035 | /* Convert the partial state reflected in last_str and expr_list to an |
| 5036 | expr_ty. The expr_ty can be a Str, or a JoinedStr. */ |
| 5037 | static expr_ty |
| 5038 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5039 | const node *n) |
| 5040 | { |
| 5041 | asdl_seq *seq; |
| 5042 | |
| 5043 | FstringParser_check_invariants(state); |
| 5044 | |
| 5045 | /* If we're just a constant string with no expressions, return |
| 5046 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5047 | if (!state->fmode) { |
| 5048 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5049 | if (!state->last_str) { |
| 5050 | /* Create a zero length string. */ |
| 5051 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5052 | if (!state->last_str) |
| 5053 | goto error; |
| 5054 | } |
| 5055 | return make_str_node_and_del(&state->last_str, c, n); |
| 5056 | } |
| 5057 | |
| 5058 | /* Create a Str node out of last_str, if needed. It will be the |
| 5059 | last node in our expression list. */ |
| 5060 | if (state->last_str) { |
| 5061 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5062 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5063 | goto error; |
| 5064 | } |
| 5065 | /* This has already been freed. */ |
| 5066 | assert(state->last_str == NULL); |
| 5067 | |
| 5068 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5069 | if (!seq) |
| 5070 | goto error; |
| 5071 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5072 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5073 | |
| 5074 | error: |
| 5075 | FstringParser_Dealloc(state); |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5079 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5080 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5081 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5082 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5083 | 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] | 5084 | struct compiling *c, const node *n) |
| 5085 | { |
| 5086 | FstringParser state; |
| 5087 | |
| 5088 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5089 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5090 | c, n) < 0) { |
| 5091 | FstringParser_Dealloc(&state); |
| 5092 | return NULL; |
| 5093 | } |
| 5094 | |
| 5095 | return FstringParser_Finish(&state, c, n); |
| 5096 | } |
| 5097 | |
| 5098 | /* n is a Python string literal, including the bracketing quote |
| 5099 | 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] | 5100 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5101 | 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] | 5102 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5103 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5104 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5105 | static int |
| 5106 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5107 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5108 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5109 | size_t len; |
| 5110 | const char *s = STR(n); |
| 5111 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5112 | int fmode = 0; |
| 5113 | *bytesmode = 0; |
| 5114 | *rawmode = 0; |
| 5115 | *result = NULL; |
| 5116 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5117 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5118 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5119 | if (quote == 'b' || quote == 'B') { |
| 5120 | quote = *++s; |
| 5121 | *bytesmode = 1; |
| 5122 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5123 | else if (quote == 'u' || quote == 'U') { |
| 5124 | quote = *++s; |
| 5125 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5126 | else if (quote == 'r' || quote == 'R') { |
| 5127 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5128 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5129 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5130 | else if (quote == 'f' || quote == 'F') { |
| 5131 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5132 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5133 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5134 | else { |
| 5135 | break; |
| 5136 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5137 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5138 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5139 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5140 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5141 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5142 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5143 | if (quote != '\'' && quote != '\"') { |
| 5144 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5145 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5146 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5147 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5148 | s++; |
| 5149 | len = strlen(s); |
| 5150 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5151 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5152 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5153 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5154 | } |
| 5155 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5156 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5157 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5158 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5159 | } |
| 5160 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5161 | /* A triple quoted string. We've already skipped one quote at |
| 5162 | the start and one at the end of the string. Now skip the |
| 5163 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5164 | s += 2; |
| 5165 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5166 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5167 | if (s[--len] != quote || s[--len] != quote) { |
| 5168 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5169 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5170 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5171 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5172 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5173 | if (fmode) { |
| 5174 | /* Just return the bytes. The caller will parse the resulting |
| 5175 | string. */ |
| 5176 | *fstr = s; |
| 5177 | *fstrlen = len; |
| 5178 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5179 | } |
| 5180 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5181 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5182 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5184 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5185 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5186 | const char *ch; |
| 5187 | for (ch = s; *ch; ch++) { |
| 5188 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5189 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5190 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5191 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5192 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5193 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5194 | if (*rawmode) |
| 5195 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5196 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5197 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5198 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5199 | if (*rawmode) |
| 5200 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5201 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5202 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5203 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5204 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5207 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5208 | each STRING atom, and process it as needed. For bytes, just |
| 5209 | concatenate them together, and the result will be a Bytes node. For |
| 5210 | normal strings and f-strings, concatenate them together. The result |
| 5211 | will be a Str node if there were no f-strings; a FormattedValue |
| 5212 | node if there's just an f-string (with no leading or trailing |
| 5213 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5214 | any literals involved. */ |
| 5215 | static expr_ty |
| 5216 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5217 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5218 | int bytesmode = 0; |
| 5219 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5220 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5221 | |
| 5222 | FstringParser state; |
| 5223 | FstringParser_Init(&state); |
| 5224 | |
| 5225 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5226 | int this_bytesmode; |
| 5227 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5228 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | const char *fstr; |
| 5230 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5231 | |
| 5232 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5233 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5234 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5235 | goto error; |
| 5236 | |
| 5237 | /* Check that we're not mixing bytes with unicode. */ |
| 5238 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5239 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5240 | /* s is NULL if the current string part is an f-string. */ |
| 5241 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5242 | goto error; |
| 5243 | } |
| 5244 | bytesmode = this_bytesmode; |
| 5245 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5246 | if (fstr != NULL) { |
| 5247 | int result; |
| 5248 | assert(s == NULL && !bytesmode); |
| 5249 | /* This is an f-string. Parse and concatenate it. */ |
| 5250 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5251 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5252 | if (result < 0) |
| 5253 | goto error; |
| 5254 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5255 | /* A string or byte string. */ |
| 5256 | assert(s != NULL && fstr == NULL); |
| 5257 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5258 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5259 | PyUnicode_CheckExact(s)); |
| 5260 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5261 | if (bytesmode) { |
| 5262 | /* For bytes, concat as we go. */ |
| 5263 | if (i == 0) { |
| 5264 | /* First time, just remember this value. */ |
| 5265 | bytes_str = s; |
| 5266 | } else { |
| 5267 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5268 | if (!bytes_str) |
| 5269 | goto error; |
| 5270 | } |
| 5271 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5272 | /* This is a regular string. Concatenate it. */ |
| 5273 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5274 | goto error; |
| 5275 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5276 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5277 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5278 | if (bytesmode) { |
| 5279 | /* Just return the bytes object and we're done. */ |
| 5280 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5281 | goto error; |
| 5282 | return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
| 5283 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5284 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5285 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5286 | assert(bytes_str == NULL); |
| 5287 | |
| 5288 | return FstringParser_Finish(&state, c, n); |
| 5289 | |
| 5290 | error: |
| 5291 | Py_XDECREF(bytes_str); |
| 5292 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5293 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5294 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5295 | |
| 5296 | PyObject * |
| 5297 | _PyAST_GetDocString(asdl_seq *body) |
| 5298 | { |
| 5299 | if (!asdl_seq_LEN(body)) { |
| 5300 | return NULL; |
| 5301 | } |
| 5302 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5303 | if (st->kind != Expr_kind) { |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | expr_ty e = st->v.Expr.value; |
| 5307 | if (e->kind == Str_kind) { |
| 5308 | return e->v.Str.s; |
| 5309 | } |
| 5310 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5311 | return e->v.Constant.value; |
| 5312 | } |
| 5313 | return NULL; |
| 5314 | } |