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" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 11 | |
| 12 | #include <assert.h> |
| 13 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 14 | static int validate_stmts(asdl_seq *); |
| 15 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 16 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 17 | static int validate_stmt(stmt_ty); |
| 18 | static int validate_expr(expr_ty, expr_context_ty); |
| 19 | |
| 20 | static int |
| 21 | validate_comprehension(asdl_seq *gens) |
| 22 | { |
| 23 | int i; |
| 24 | if (!asdl_seq_LEN(gens)) { |
| 25 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 26 | return 0; |
| 27 | } |
| 28 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 29 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 30 | if (!validate_expr(comp->target, Store) || |
| 31 | !validate_expr(comp->iter, Load) || |
| 32 | !validate_exprs(comp->ifs, Load, 0)) |
| 33 | return 0; |
| 34 | } |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | static int |
| 39 | validate_slice(slice_ty slice) |
| 40 | { |
| 41 | switch (slice->kind) { |
| 42 | case Slice_kind: |
| 43 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 44 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 45 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 46 | case ExtSlice_kind: { |
| 47 | int i; |
| 48 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 49 | return 0; |
| 50 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 51 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 52 | return 0; |
| 53 | return 1; |
| 54 | } |
| 55 | case Index_kind: |
| 56 | return validate_expr(slice->v.Index.value, Load); |
| 57 | default: |
| 58 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 59 | return 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static int |
| 64 | validate_keywords(asdl_seq *keywords) |
| 65 | { |
| 66 | int i; |
| 67 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 68 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 69 | return 0; |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | static int |
| 74 | validate_args(asdl_seq *args) |
| 75 | { |
| 76 | int i; |
| 77 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 78 | arg_ty arg = asdl_seq_GET(args, i); |
| 79 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 80 | return 0; |
| 81 | } |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | static const char * |
| 86 | expr_context_name(expr_context_ty ctx) |
| 87 | { |
| 88 | switch (ctx) { |
| 89 | case Load: |
| 90 | return "Load"; |
| 91 | case Store: |
| 92 | return "Store"; |
| 93 | case Del: |
| 94 | return "Del"; |
| 95 | case AugLoad: |
| 96 | return "AugLoad"; |
| 97 | case AugStore: |
| 98 | return "AugStore"; |
| 99 | case Param: |
| 100 | return "Param"; |
| 101 | default: |
| 102 | assert(0); |
| 103 | return "(unknown)"; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | validate_arguments(arguments_ty args) |
| 109 | { |
| 110 | if (!validate_args(args->args)) |
| 111 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 112 | if (args->vararg && args->vararg->annotation |
| 113 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | if (!validate_args(args->kwonlyargs)) |
| 117 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 118 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 119 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 123 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 124 | return 0; |
| 125 | } |
| 126 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 127 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 128 | "kw_defaults on arguments"); |
| 129 | return 0; |
| 130 | } |
| 131 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 132 | } |
| 133 | |
| 134 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 135 | validate_constant(PyObject *value) |
| 136 | { |
| 137 | if (value == Py_None || value == Py_Ellipsis) |
| 138 | return 1; |
| 139 | |
| 140 | if (PyLong_CheckExact(value) |
| 141 | || PyFloat_CheckExact(value) |
| 142 | || PyComplex_CheckExact(value) |
| 143 | || PyBool_Check(value) |
| 144 | || PyUnicode_CheckExact(value) |
| 145 | || PyBytes_CheckExact(value)) |
| 146 | return 1; |
| 147 | |
| 148 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 149 | PyObject *it; |
| 150 | |
| 151 | it = PyObject_GetIter(value); |
| 152 | if (it == NULL) |
| 153 | return 0; |
| 154 | |
| 155 | while (1) { |
| 156 | PyObject *item = PyIter_Next(it); |
| 157 | if (item == NULL) { |
| 158 | if (PyErr_Occurred()) { |
| 159 | Py_DECREF(it); |
| 160 | return 0; |
| 161 | } |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (!validate_constant(item)) { |
| 166 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 167 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 168 | return 0; |
| 169 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 170 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | Py_DECREF(it); |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 181 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 182 | { |
| 183 | int check_ctx = 1; |
| 184 | expr_context_ty actual_ctx; |
| 185 | |
| 186 | /* First check expression context. */ |
| 187 | switch (exp->kind) { |
| 188 | case Attribute_kind: |
| 189 | actual_ctx = exp->v.Attribute.ctx; |
| 190 | break; |
| 191 | case Subscript_kind: |
| 192 | actual_ctx = exp->v.Subscript.ctx; |
| 193 | break; |
| 194 | case Starred_kind: |
| 195 | actual_ctx = exp->v.Starred.ctx; |
| 196 | break; |
| 197 | case Name_kind: |
| 198 | actual_ctx = exp->v.Name.ctx; |
| 199 | break; |
| 200 | case List_kind: |
| 201 | actual_ctx = exp->v.List.ctx; |
| 202 | break; |
| 203 | case Tuple_kind: |
| 204 | actual_ctx = exp->v.Tuple.ctx; |
| 205 | break; |
| 206 | default: |
| 207 | if (ctx != Load) { |
| 208 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 209 | "assigned to in %s context", expr_context_name(ctx)); |
| 210 | return 0; |
| 211 | } |
| 212 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 213 | /* set actual_ctx to prevent gcc warning */ |
| 214 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 215 | } |
| 216 | if (check_ctx && actual_ctx != ctx) { |
| 217 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 218 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Now validate expression. */ |
| 223 | switch (exp->kind) { |
| 224 | case BoolOp_kind: |
| 225 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 226 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 227 | return 0; |
| 228 | } |
| 229 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 230 | case BinOp_kind: |
| 231 | return validate_expr(exp->v.BinOp.left, Load) && |
| 232 | validate_expr(exp->v.BinOp.right, Load); |
| 233 | case UnaryOp_kind: |
| 234 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 235 | case Lambda_kind: |
| 236 | return validate_arguments(exp->v.Lambda.args) && |
| 237 | validate_expr(exp->v.Lambda.body, Load); |
| 238 | case IfExp_kind: |
| 239 | return validate_expr(exp->v.IfExp.test, Load) && |
| 240 | validate_expr(exp->v.IfExp.body, Load) && |
| 241 | validate_expr(exp->v.IfExp.orelse, Load); |
| 242 | case Dict_kind: |
| 243 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 244 | PyErr_SetString(PyExc_ValueError, |
| 245 | "Dict doesn't have the same number of keys as values"); |
| 246 | return 0; |
| 247 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 248 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 249 | dict literals, i.e. ``{**{a:b}}`` */ |
| 250 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 251 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 252 | case Set_kind: |
| 253 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 254 | #define COMP(NAME) \ |
| 255 | case NAME ## _kind: \ |
| 256 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 257 | validate_expr(exp->v.NAME.elt, Load); |
| 258 | COMP(ListComp) |
| 259 | COMP(SetComp) |
| 260 | COMP(GeneratorExp) |
| 261 | #undef COMP |
| 262 | case DictComp_kind: |
| 263 | return validate_comprehension(exp->v.DictComp.generators) && |
| 264 | validate_expr(exp->v.DictComp.key, Load) && |
| 265 | validate_expr(exp->v.DictComp.value, Load); |
| 266 | case Yield_kind: |
| 267 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 268 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 269 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 270 | case Await_kind: |
| 271 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 272 | case Compare_kind: |
| 273 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 274 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 275 | return 0; |
| 276 | } |
| 277 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 278 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 279 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 280 | "of comparators and operands"); |
| 281 | return 0; |
| 282 | } |
| 283 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 284 | validate_expr(exp->v.Compare.left, Load); |
| 285 | case Call_kind: |
| 286 | return validate_expr(exp->v.Call.func, Load) && |
| 287 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 288 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 289 | case Constant_kind: |
| 290 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 291 | PyErr_Format(PyExc_TypeError, |
| 292 | "got an invalid type in Constant: %s", |
| 293 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 297 | case Num_kind: { |
| 298 | PyObject *n = exp->v.Num.n; |
| 299 | if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) && |
| 300 | !PyComplex_CheckExact(n)) { |
| 301 | PyErr_SetString(PyExc_TypeError, "non-numeric type in Num"); |
| 302 | return 0; |
| 303 | } |
| 304 | return 1; |
| 305 | } |
| 306 | case Str_kind: { |
| 307 | PyObject *s = exp->v.Str.s; |
| 308 | if (!PyUnicode_CheckExact(s)) { |
| 309 | PyErr_SetString(PyExc_TypeError, "non-string type in Str"); |
| 310 | return 0; |
| 311 | } |
| 312 | return 1; |
| 313 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 314 | case JoinedStr_kind: |
| 315 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 316 | case FormattedValue_kind: |
| 317 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 318 | return 0; |
| 319 | if (exp->v.FormattedValue.format_spec) |
| 320 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 321 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 322 | case Bytes_kind: { |
| 323 | PyObject *b = exp->v.Bytes.s; |
| 324 | if (!PyBytes_CheckExact(b)) { |
| 325 | PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes"); |
| 326 | return 0; |
| 327 | } |
| 328 | return 1; |
| 329 | } |
| 330 | case Attribute_kind: |
| 331 | return validate_expr(exp->v.Attribute.value, Load); |
| 332 | case Subscript_kind: |
| 333 | return validate_slice(exp->v.Subscript.slice) && |
| 334 | validate_expr(exp->v.Subscript.value, Load); |
| 335 | case Starred_kind: |
| 336 | return validate_expr(exp->v.Starred.value, ctx); |
| 337 | case List_kind: |
| 338 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 339 | case Tuple_kind: |
| 340 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
| 341 | /* These last cases don't have any checking. */ |
| 342 | case Name_kind: |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 343 | case NameConstant_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 344 | case Ellipsis_kind: |
| 345 | return 1; |
| 346 | default: |
| 347 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 348 | return 0; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static int |
| 353 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 354 | { |
| 355 | if (asdl_seq_LEN(seq)) |
| 356 | return 1; |
| 357 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int |
| 362 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 363 | { |
| 364 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 365 | validate_exprs(targets, ctx, 0); |
| 366 | } |
| 367 | |
| 368 | static int |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 369 | validate_body(asdl_seq *body, const char *owner, int allowempty) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 370 | { |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 371 | if (!allowempty && !validate_nonempty_seq(body, "body", owner)) { |
| 372 | return 0; |
| 373 | } |
| 374 | return validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int |
| 378 | validate_stmt(stmt_ty stmt) |
| 379 | { |
| 380 | int i; |
| 381 | switch (stmt->kind) { |
| 382 | case FunctionDef_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 383 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef", |
| 384 | stmt->v.FunctionDef.docstring != NULL) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 385 | validate_arguments(stmt->v.FunctionDef.args) && |
| 386 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 387 | (!stmt->v.FunctionDef.returns || |
| 388 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 389 | case ClassDef_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 390 | return validate_body(stmt->v.ClassDef.body, "ClassDef", |
| 391 | stmt->v.ClassDef.docstring != NULL) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 392 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 393 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 394 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 395 | case Return_kind: |
| 396 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 397 | case Delete_kind: |
| 398 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 399 | case Assign_kind: |
| 400 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 401 | validate_expr(stmt->v.Assign.value, Load); |
| 402 | case AugAssign_kind: |
| 403 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 404 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 405 | case AnnAssign_kind: |
| 406 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 407 | stmt->v.AnnAssign.simple) { |
| 408 | PyErr_SetString(PyExc_TypeError, |
| 409 | "AnnAssign with simple non-Name target"); |
| 410 | return 0; |
| 411 | } |
| 412 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 413 | (!stmt->v.AnnAssign.value || |
| 414 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 415 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 416 | case For_kind: |
| 417 | return validate_expr(stmt->v.For.target, Store) && |
| 418 | validate_expr(stmt->v.For.iter, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 419 | validate_body(stmt->v.For.body, "For", 0) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 420 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 421 | case AsyncFor_kind: |
| 422 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 423 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 424 | validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 425 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 426 | case While_kind: |
| 427 | return validate_expr(stmt->v.While.test, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 428 | validate_body(stmt->v.While.body, "While", 0) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 429 | validate_stmts(stmt->v.While.orelse); |
| 430 | case If_kind: |
| 431 | return validate_expr(stmt->v.If.test, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 432 | validate_body(stmt->v.If.body, "If", 0) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 433 | validate_stmts(stmt->v.If.orelse); |
| 434 | case With_kind: |
| 435 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 436 | return 0; |
| 437 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 438 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 439 | if (!validate_expr(item->context_expr, Load) || |
| 440 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 441 | return 0; |
| 442 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 443 | return validate_body(stmt->v.With.body, "With", 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 444 | case AsyncWith_kind: |
| 445 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 446 | return 0; |
| 447 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 448 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 449 | if (!validate_expr(item->context_expr, Load) || |
| 450 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 451 | return 0; |
| 452 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 453 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 454 | case Raise_kind: |
| 455 | if (stmt->v.Raise.exc) { |
| 456 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 457 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 458 | } |
| 459 | if (stmt->v.Raise.cause) { |
| 460 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 461 | return 0; |
| 462 | } |
| 463 | return 1; |
| 464 | case Try_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 465 | if (!validate_body(stmt->v.Try.body, "Try", 0)) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 466 | return 0; |
| 467 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 468 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 469 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 470 | return 0; |
| 471 | } |
| 472 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 473 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 474 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 475 | return 0; |
| 476 | } |
| 477 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 478 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 479 | if ((handler->v.ExceptHandler.type && |
| 480 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 481 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0)) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 485 | validate_stmts(stmt->v.Try.finalbody)) && |
| 486 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 487 | validate_stmts(stmt->v.Try.orelse)); |
| 488 | case Assert_kind: |
| 489 | return validate_expr(stmt->v.Assert.test, Load) && |
| 490 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 491 | case Import_kind: |
| 492 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 493 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 494 | if (stmt->v.ImportFrom.level < 0) { |
| 495 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 499 | case Global_kind: |
| 500 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 501 | case Nonlocal_kind: |
| 502 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 503 | case Expr_kind: |
| 504 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 505 | case AsyncFunctionDef_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 506 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef", |
| 507 | stmt->v.AsyncFunctionDef.docstring != NULL) && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 508 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 509 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 510 | (!stmt->v.AsyncFunctionDef.returns || |
| 511 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 512 | case Pass_kind: |
| 513 | case Break_kind: |
| 514 | case Continue_kind: |
| 515 | return 1; |
| 516 | default: |
| 517 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 518 | return 0; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static int |
| 523 | validate_stmts(asdl_seq *seq) |
| 524 | { |
| 525 | int i; |
| 526 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 527 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 528 | if (stmt) { |
| 529 | if (!validate_stmt(stmt)) |
| 530 | return 0; |
| 531 | } |
| 532 | else { |
| 533 | PyErr_SetString(PyExc_ValueError, |
| 534 | "None disallowed in statement list"); |
| 535 | return 0; |
| 536 | } |
| 537 | } |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | static int |
| 542 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 543 | { |
| 544 | int i; |
| 545 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 546 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 547 | if (expr) { |
| 548 | if (!validate_expr(expr, ctx)) |
| 549 | return 0; |
| 550 | } |
| 551 | else if (!null_ok) { |
| 552 | PyErr_SetString(PyExc_ValueError, |
| 553 | "None disallowed in expression list"); |
| 554 | return 0; |
| 555 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 556 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 557 | } |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | int |
| 562 | PyAST_Validate(mod_ty mod) |
| 563 | { |
| 564 | int res = 0; |
| 565 | |
| 566 | switch (mod->kind) { |
| 567 | case Module_kind: |
| 568 | res = validate_stmts(mod->v.Module.body); |
| 569 | break; |
| 570 | case Interactive_kind: |
| 571 | res = validate_stmts(mod->v.Interactive.body); |
| 572 | break; |
| 573 | case Expression_kind: |
| 574 | res = validate_expr(mod->v.Expression.body, Load); |
| 575 | break; |
| 576 | case Suite_kind: |
| 577 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 578 | break; |
| 579 | default: |
| 580 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 581 | res = 0; |
| 582 | break; |
| 583 | } |
| 584 | return res; |
| 585 | } |
| 586 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 587 | /* 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] | 588 | #include "grammar.h" |
| 589 | #include "parsetok.h" |
| 590 | #include "graminit.h" |
| 591 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 592 | /* Data structure used internally */ |
| 593 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 594 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 595 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 596 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
| 597 | PyObject *c_normalize_args; /* Normalization argument tuple. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 598 | }; |
| 599 | |
| 600 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 601 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 602 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 603 | static asdl_seq *ast_for_body(struct compiling *c, const node *n, |
| 604 | string *docstring); |
| 605 | static string docstring_from_stmts(asdl_seq *stmts); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 606 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 607 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 608 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 609 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 610 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 611 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); |
| 612 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); |
| 613 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 614 | /* Note different signature for ast_for_call */ |
| 615 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); |
| 616 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 617 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 618 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 619 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 620 | #define COMP_GENEXP 0 |
| 621 | #define COMP_LISTCOMP 1 |
| 622 | #define COMP_SETCOMP 2 |
| 623 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 624 | static int |
| 625 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 626 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 627 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 628 | if (!m) |
| 629 | return 0; |
| 630 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 631 | Py_DECREF(m); |
| 632 | if (!c->c_normalize) |
| 633 | return 0; |
| 634 | c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 635 | if (!c->c_normalize_args) { |
| 636 | Py_CLEAR(c->c_normalize); |
| 637 | return 0; |
| 638 | } |
Christian Heimes | 72f562f | 2013-07-24 21:02:17 +0200 | [diff] [blame] | 639 | PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 640 | return 1; |
| 641 | } |
| 642 | |
| 643 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 644 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 645 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 646 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 647 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 648 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 649 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 650 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 651 | /* Check whether there are non-ASCII characters in the |
| 652 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 653 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 654 | PyObject *id2; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 655 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 656 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 657 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 658 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 659 | PyTuple_SET_ITEM(c->c_normalize_args, 1, id); |
| 660 | id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 661 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 662 | if (!id2) |
| 663 | return NULL; |
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 | } |
| 761 | assert(0); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | /* Transform the CST rooted at node * to the appropriate AST |
| 766 | */ |
| 767 | |
| 768 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 769 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 770 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 771 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 772 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | asdl_seq *stmts = NULL; |
| 774 | stmt_ty s; |
| 775 | node *ch; |
| 776 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 777 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 778 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 779 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 780 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 781 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 782 | c.c_normalize = NULL; |
| 783 | c.c_normalize_args = NULL; |
| 784 | |
| 785 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 786 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 787 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 788 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | switch (TYPE(n)) { |
| 790 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 791 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 793 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | for (i = 0; i < NCH(n) - 1; i++) { |
| 795 | ch = CHILD(n, i); |
| 796 | if (TYPE(ch) == NEWLINE) |
| 797 | continue; |
| 798 | REQ(ch, stmt); |
| 799 | num = num_stmts(ch); |
| 800 | if (num == 1) { |
| 801 | s = ast_for_stmt(&c, ch); |
| 802 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 803 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 804 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | } |
| 806 | else { |
| 807 | ch = CHILD(ch, 0); |
| 808 | REQ(ch, simple_stmt); |
| 809 | for (j = 0; j < num; j++) { |
| 810 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 811 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 812 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 813 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 817 | res = Module(stmts, docstring_from_stmts(stmts), arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 818 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | case eval_input: { |
| 820 | expr_ty testlist_ast; |
| 821 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 822 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 823 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 824 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 825 | goto out; |
| 826 | res = Expression(testlist_ast, arena); |
| 827 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 828 | } |
| 829 | case single_input: |
| 830 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 831 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 832 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 833 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 834 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 835 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 836 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 837 | goto out; |
| 838 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 | } |
| 840 | else { |
| 841 | n = CHILD(n, 0); |
| 842 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 843 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 845 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 846 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 847 | s = ast_for_stmt(&c, n); |
| 848 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 849 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 850 | asdl_seq_SET(stmts, 0, s); |
| 851 | } |
| 852 | else { |
| 853 | /* Only a simple_stmt can contain multiple statements. */ |
| 854 | REQ(n, simple_stmt); |
| 855 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 857 | break; |
| 858 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 859 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 860 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | asdl_seq_SET(stmts, i / 2, s); |
| 862 | } |
| 863 | } |
| 864 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 865 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 867 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 869 | PyErr_Format(PyExc_SystemError, |
| 870 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 871 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 872 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 873 | out: |
| 874 | if (c.c_normalize) { |
| 875 | Py_DECREF(c.c_normalize); |
| 876 | PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL); |
| 877 | Py_DECREF(c.c_normalize_args); |
| 878 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 879 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 882 | mod_ty |
| 883 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 884 | PyArena *arena) |
| 885 | { |
| 886 | mod_ty mod; |
| 887 | PyObject *filename; |
| 888 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 889 | if (filename == NULL) |
| 890 | return NULL; |
| 891 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 892 | Py_DECREF(filename); |
| 893 | return mod; |
| 894 | |
| 895 | } |
| 896 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 897 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 898 | */ |
| 899 | |
| 900 | static operator_ty |
| 901 | get_operator(const node *n) |
| 902 | { |
| 903 | switch (TYPE(n)) { |
| 904 | case VBAR: |
| 905 | return BitOr; |
| 906 | case CIRCUMFLEX: |
| 907 | return BitXor; |
| 908 | case AMPER: |
| 909 | return BitAnd; |
| 910 | case LEFTSHIFT: |
| 911 | return LShift; |
| 912 | case RIGHTSHIFT: |
| 913 | return RShift; |
| 914 | case PLUS: |
| 915 | return Add; |
| 916 | case MINUS: |
| 917 | return Sub; |
| 918 | case STAR: |
| 919 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 920 | case AT: |
| 921 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 922 | case SLASH: |
| 923 | return Div; |
| 924 | case DOUBLESLASH: |
| 925 | return FloorDiv; |
| 926 | case PERCENT: |
| 927 | return Mod; |
| 928 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 929 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 933 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 934 | "None", |
| 935 | "True", |
| 936 | "False", |
| 937 | NULL, |
| 938 | }; |
| 939 | |
| 940 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 941 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 942 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 943 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 944 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 945 | if (_PyUnicode_EqualToASCIIString(name, "__debug__")) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 946 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 947 | return 1; |
| 948 | } |
Serhiy Storchaka | 3b73ea1 | 2016-11-16 10:19:20 +0200 | [diff] [blame] | 949 | if (_PyUnicode_EqualToASCIIString(name, "async") || |
| 950 | _PyUnicode_EqualToASCIIString(name, "await")) |
Yury Selivanov | 8987c9d | 2016-09-15 12:50:23 -0400 | [diff] [blame] | 951 | { |
| 952 | PyObject *message = PyUnicode_FromString( |
| 953 | "'async' and 'await' will become reserved keywords" |
| 954 | " in Python 3.7"); |
Yury Selivanov | 1a9d687 | 2016-11-08 16:54:18 -0500 | [diff] [blame] | 955 | int ret; |
Yury Selivanov | 8987c9d | 2016-09-15 12:50:23 -0400 | [diff] [blame] | 956 | if (message == NULL) { |
| 957 | return 1; |
| 958 | } |
Yury Selivanov | 1a9d687 | 2016-11-08 16:54:18 -0500 | [diff] [blame] | 959 | ret = PyErr_WarnExplicitObject( |
Yury Selivanov | 8987c9d | 2016-09-15 12:50:23 -0400 | [diff] [blame] | 960 | PyExc_DeprecationWarning, |
| 961 | message, |
| 962 | c->c_filename, |
| 963 | LINENO(n), |
| 964 | NULL, |
Yury Selivanov | 1a9d687 | 2016-11-08 16:54:18 -0500 | [diff] [blame] | 965 | NULL); |
| 966 | Py_DECREF(message); |
| 967 | if (ret < 0) { |
Yury Selivanov | 8987c9d | 2016-09-15 12:50:23 -0400 | [diff] [blame] | 968 | return 1; |
| 969 | } |
| 970 | } |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 971 | if (full_checks) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 972 | const char * const *p; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 973 | for (p = FORBIDDEN; *p; p++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 974 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 975 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 976 | return 1; |
| 977 | } |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | return 0; |
| 981 | } |
| 982 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 983 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 984 | |
| 985 | Only sets context for expr kinds that "can appear in assignment context" |
| 986 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 987 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 988 | */ |
| 989 | |
| 990 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 991 | 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] | 992 | { |
| 993 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 994 | /* If a particular expression type can't be used for assign / delete, |
| 995 | set expr_name to its name and an error message will be generated. |
| 996 | */ |
| 997 | const char* expr_name = NULL; |
| 998 | |
| 999 | /* The ast defines augmented store and load contexts, but the |
| 1000 | implementation here doesn't actually use them. The code may be |
| 1001 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1002 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1003 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1004 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1005 | */ |
| 1006 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1007 | |
| 1008 | switch (e->kind) { |
| 1009 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1010 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1011 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1012 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1013 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1014 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1015 | e->v.Subscript.ctx = ctx; |
| 1016 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1017 | case Starred_kind: |
| 1018 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1019 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1020 | return 0; |
| 1021 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1022 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1023 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1024 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1025 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1026 | } |
| 1027 | e->v.Name.ctx = ctx; |
| 1028 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1029 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1030 | e->v.List.ctx = ctx; |
| 1031 | s = e->v.List.elts; |
| 1032 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1033 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1034 | e->v.Tuple.ctx = ctx; |
| 1035 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1036 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1037 | case Lambda_kind: |
| 1038 | expr_name = "lambda"; |
| 1039 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1040 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1041 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1042 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1043 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1044 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1045 | case UnaryOp_kind: |
| 1046 | expr_name = "operator"; |
| 1047 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1048 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1049 | expr_name = "generator expression"; |
| 1050 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1051 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1052 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1053 | expr_name = "yield expression"; |
| 1054 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1055 | case Await_kind: |
| 1056 | expr_name = "await expression"; |
| 1057 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1058 | case ListComp_kind: |
| 1059 | expr_name = "list comprehension"; |
| 1060 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1061 | case SetComp_kind: |
| 1062 | expr_name = "set comprehension"; |
| 1063 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1064 | case DictComp_kind: |
| 1065 | expr_name = "dict comprehension"; |
| 1066 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1067 | case Dict_kind: |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1068 | case Set_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1069 | case Num_kind: |
| 1070 | case Str_kind: |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 1071 | case Bytes_kind: |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1072 | case JoinedStr_kind: |
| 1073 | case FormattedValue_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1074 | expr_name = "literal"; |
| 1075 | break; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1076 | case NameConstant_kind: |
| 1077 | expr_name = "keyword"; |
| 1078 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1079 | case Ellipsis_kind: |
| 1080 | expr_name = "Ellipsis"; |
| 1081 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1082 | case Compare_kind: |
| 1083 | expr_name = "comparison"; |
| 1084 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1085 | case IfExp_kind: |
| 1086 | expr_name = "conditional expression"; |
| 1087 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1088 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | PyErr_Format(PyExc_SystemError, |
| 1090 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1091 | e->kind, e->lineno); |
| 1092 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1093 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1094 | /* Check for error string set by switch */ |
| 1095 | if (expr_name) { |
| 1096 | char buf[300]; |
| 1097 | PyOS_snprintf(buf, sizeof(buf), |
| 1098 | "can't %s %s", |
| 1099 | ctx == Store ? "assign to" : "delete", |
| 1100 | expr_name); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1101 | return ast_error(c, n, buf); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1104 | /* 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] | 1105 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1106 | */ |
| 1107 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1108 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1109 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1110 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1111 | 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] | 1112 | return 0; |
| 1113 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1114 | } |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1119 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1120 | { |
| 1121 | REQ(n, augassign); |
| 1122 | n = CHILD(n, 0); |
| 1123 | switch (STR(n)[0]) { |
| 1124 | case '+': |
| 1125 | return Add; |
| 1126 | case '-': |
| 1127 | return Sub; |
| 1128 | case '/': |
| 1129 | if (STR(n)[1] == '/') |
| 1130 | return FloorDiv; |
| 1131 | else |
| 1132 | return Div; |
| 1133 | case '%': |
| 1134 | return Mod; |
| 1135 | case '<': |
| 1136 | return LShift; |
| 1137 | case '>': |
| 1138 | return RShift; |
| 1139 | case '&': |
| 1140 | return BitAnd; |
| 1141 | case '^': |
| 1142 | return BitXor; |
| 1143 | case '|': |
| 1144 | return BitOr; |
| 1145 | case '*': |
| 1146 | if (STR(n)[1] == '*') |
| 1147 | return Pow; |
| 1148 | else |
| 1149 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1150 | case '@': |
| 1151 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1152 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1153 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1154 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1159 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1160 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1161 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | |'is' 'not' |
| 1163 | */ |
| 1164 | REQ(n, comp_op); |
| 1165 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1166 | n = CHILD(n, 0); |
| 1167 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1168 | case LESS: |
| 1169 | return Lt; |
| 1170 | case GREATER: |
| 1171 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1172 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 | return Eq; |
| 1174 | case LESSEQUAL: |
| 1175 | return LtE; |
| 1176 | case GREATEREQUAL: |
| 1177 | return GtE; |
| 1178 | case NOTEQUAL: |
| 1179 | return NotEq; |
| 1180 | case NAME: |
| 1181 | if (strcmp(STR(n), "in") == 0) |
| 1182 | return In; |
| 1183 | if (strcmp(STR(n), "is") == 0) |
| 1184 | return Is; |
| 1185 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1186 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1187 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1188 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1189 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1190 | } |
| 1191 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1192 | /* handle "not in" and "is not" */ |
| 1193 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1194 | case NAME: |
| 1195 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1196 | return NotIn; |
| 1197 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1198 | return IsNot; |
| 1199 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1200 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1201 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1202 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1203 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1204 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1205 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1206 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1207 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | static asdl_seq * |
| 1211 | seq_for_testlist(struct compiling *c, const node *n) |
| 1212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1214 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1215 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1216 | asdl_seq *seq; |
| 1217 | expr_ty expression; |
| 1218 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1219 | 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] | 1220 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1221 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1222 | if (!seq) |
| 1223 | return NULL; |
| 1224 | |
| 1225 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1227 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1228 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1229 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1230 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1231 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1232 | |
| 1233 | assert(i / 2 < seq->size); |
| 1234 | asdl_seq_SET(seq, i / 2, expression); |
| 1235 | } |
| 1236 | return seq; |
| 1237 | } |
| 1238 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1239 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1240 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1241 | { |
| 1242 | identifier name; |
| 1243 | expr_ty annotation = NULL; |
| 1244 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1245 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1246 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1247 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1248 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1249 | name = NEW_IDENTIFIER(ch); |
| 1250 | if (!name) |
| 1251 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1252 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1253 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1256 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1257 | if (!annotation) |
| 1258 | return NULL; |
| 1259 | } |
| 1260 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1261 | 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] | 1262 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1263 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1264 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1267 | /* returns -1 if failed to handle keyword only arguments |
| 1268 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1269 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1270 | ^^^ |
| 1271 | start pointing here |
| 1272 | */ |
| 1273 | static int |
| 1274 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1275 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1276 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1277 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1278 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1279 | expr_ty expression, annotation; |
| 1280 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1281 | int i = start; |
| 1282 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1285 | 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] | 1286 | return -1; |
| 1287 | } |
| 1288 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1289 | while (i < NCH(n)) { |
| 1290 | ch = CHILD(n, i); |
| 1291 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1292 | case vfpdef: |
| 1293 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1294 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1295 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1296 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1297 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1298 | asdl_seq_SET(kwdefaults, j, expression); |
| 1299 | i += 2; /* '=' and test */ |
| 1300 | } |
| 1301 | else { /* setting NULL if no default value exists */ |
| 1302 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1303 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1304 | if (NCH(ch) == 3) { |
| 1305 | /* ch is NAME ':' test */ |
| 1306 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1307 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1308 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1309 | } |
| 1310 | else { |
| 1311 | annotation = NULL; |
| 1312 | } |
| 1313 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1314 | argname = NEW_IDENTIFIER(ch); |
| 1315 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1316 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1317 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1318 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1319 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 1320 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1321 | if (!arg) |
| 1322 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1323 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1324 | i += 2; /* the name and the comma */ |
| 1325 | break; |
| 1326 | case DOUBLESTAR: |
| 1327 | return i; |
| 1328 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1329 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1330 | goto error; |
| 1331 | } |
| 1332 | } |
| 1333 | return i; |
| 1334 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1336 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1337 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1338 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1339 | |
| 1340 | static arguments_ty |
| 1341 | ast_for_arguments(struct compiling *c, const node *n) |
| 1342 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1343 | /* This function handles both typedargslist (function definition) |
| 1344 | and varargslist (lambda definition). |
| 1345 | |
| 1346 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1347 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1348 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1349 | | '**' tfpdef [',']]] |
| 1350 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1351 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1352 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1353 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1354 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1355 | | '**' vfpdef [',']]] |
| 1356 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1357 | | '**' vfpdef [','] |
| 1358 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1359 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1360 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1361 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1362 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1363 | int nposdefaults = 0, found_default = 0; |
| 1364 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1365 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1366 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1367 | node *ch; |
| 1368 | |
| 1369 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1370 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1371 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1372 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1373 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1374 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1375 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1376 | /* First count the number of positional args & defaults. The |
| 1377 | variable i is the loop index for this for loop and the next. |
| 1378 | The next loop picks up where the first leaves off. |
| 1379 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1380 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1381 | ch = CHILD(n, i); |
| 1382 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1383 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1384 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1385 | if (i < NCH(n) && /* skip argument following star */ |
| 1386 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1387 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1388 | i++; |
| 1389 | } |
| 1390 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1391 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1392 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1393 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1394 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1395 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1397 | defaults for keyword only args */ |
| 1398 | for ( ; i < NCH(n); ++i) { |
| 1399 | ch = CHILD(n, i); |
| 1400 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1401 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1402 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1403 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1404 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1405 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1406 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1407 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1408 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1409 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1411 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1412 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1413 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1415 | since we set NULL as default for keyword only argument w/o default |
| 1416 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1417 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1418 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1420 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1421 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1422 | /* tfpdef: NAME [':' test] |
| 1423 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1424 | */ |
| 1425 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1426 | j = 0; /* index for defaults */ |
| 1427 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1429 | ch = CHILD(n, i); |
| 1430 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1431 | case tfpdef: |
| 1432 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1433 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1434 | anything other than EQUAL or a comma? */ |
| 1435 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1436 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1437 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1438 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1439 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1440 | assert(posdefaults != NULL); |
| 1441 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1442 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1443 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1444 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1445 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1446 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1447 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1448 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1449 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1450 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1451 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1452 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1453 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1454 | i += 2; /* the name and the comma */ |
| 1455 | break; |
| 1456 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1457 | if (i+1 >= NCH(n) || |
| 1458 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1459 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1460 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1461 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1462 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1463 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1464 | if (TYPE(ch) == COMMA) { |
| 1465 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1466 | i += 2; /* now follows keyword only arguments */ |
| 1467 | res = handle_keywordonly_args(c, n, i, |
| 1468 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1469 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1470 | i = res; /* res has new position to process */ |
| 1471 | } |
| 1472 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1473 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1474 | if (!vararg) |
| 1475 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1476 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1477 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1478 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1479 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1480 | int res = 0; |
| 1481 | res = handle_keywordonly_args(c, n, i, |
| 1482 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1483 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1484 | i = res; /* res has new position to process */ |
| 1485 | } |
| 1486 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1489 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1490 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1491 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1492 | if (!kwarg) |
| 1493 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1494 | i += 3; |
| 1495 | break; |
| 1496 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1497 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | "unexpected node in varargslist: %d @ %d", |
| 1499 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1500 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1501 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1502 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1503 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static expr_ty |
| 1507 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1508 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1509 | expr_ty e; |
| 1510 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1511 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1512 | int i; |
| 1513 | |
| 1514 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1515 | |
| 1516 | lineno = LINENO(n); |
| 1517 | col_offset = n->n_col_offset; |
| 1518 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1519 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1520 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1521 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1522 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1523 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1524 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | |
| 1526 | for (i = 2; i < NCH(n); i+=2) { |
| 1527 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1528 | if (!id) |
| 1529 | return NULL; |
| 1530 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1531 | if (!e) |
| 1532 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static expr_ty |
| 1539 | ast_for_decorator(struct compiling *c, const node *n) |
| 1540 | { |
| 1541 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1542 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1543 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1545 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1546 | REQ(CHILD(n, 0), AT); |
| 1547 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1549 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1550 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1551 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1553 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1554 | d = name_expr; |
| 1555 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1556 | } |
| 1557 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1558 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1559 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1560 | if (!d) |
| 1561 | return NULL; |
| 1562 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1563 | } |
| 1564 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1565 | d = ast_for_call(c, CHILD(n, 3), name_expr); |
| 1566 | if (!d) |
| 1567 | return NULL; |
| 1568 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | static asdl_seq* |
| 1575 | ast_for_decorators(struct compiling *c, const node *n) |
| 1576 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1577 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1578 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1579 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1581 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1582 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1583 | if (!decorator_seq) |
| 1584 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1586 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1587 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1588 | if (!d) |
| 1589 | return NULL; |
| 1590 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1591 | } |
| 1592 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1596 | ast_for_funcdef_impl(struct compiling *c, const node *n, |
| 1597 | asdl_seq *decorator_seq, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1598 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1599 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1600 | identifier name; |
| 1601 | arguments_ty args; |
| 1602 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1603 | expr_ty returns = NULL; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1604 | string docstring; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1605 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1606 | |
| 1607 | REQ(n, funcdef); |
| 1608 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1609 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1610 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1611 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1612 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1613 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1614 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1615 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1616 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1617 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1618 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1619 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1620 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1621 | name_i += 2; |
| 1622 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1623 | body = ast_for_body(c, CHILD(n, name_i + 3), &docstring); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1624 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1625 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1626 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1627 | if (is_async) |
| 1628 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1629 | docstring, LINENO(n), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1630 | n->n_col_offset, c->c_arena); |
| 1631 | else |
| 1632 | return FunctionDef(name, args, body, decorator_seq, returns, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1633 | docstring, LINENO(n), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1634 | n->n_col_offset, c->c_arena); |
| 1635 | } |
| 1636 | |
| 1637 | static stmt_ty |
| 1638 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1639 | { |
| 1640 | /* async_funcdef: ASYNC funcdef */ |
| 1641 | REQ(n, async_funcdef); |
| 1642 | REQ(CHILD(n, 0), ASYNC); |
| 1643 | REQ(CHILD(n, 1), funcdef); |
| 1644 | |
| 1645 | return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, |
| 1646 | 1 /* is_async */); |
| 1647 | } |
| 1648 | |
| 1649 | static stmt_ty |
| 1650 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1651 | { |
| 1652 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1653 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1654 | 0 /* is_async */); |
| 1655 | } |
| 1656 | |
| 1657 | |
| 1658 | static stmt_ty |
| 1659 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1660 | { |
| 1661 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
| 1662 | REQ(n, async_stmt); |
| 1663 | REQ(CHILD(n, 0), ASYNC); |
| 1664 | |
| 1665 | switch (TYPE(CHILD(n, 1))) { |
| 1666 | case funcdef: |
| 1667 | return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, |
| 1668 | 1 /* is_async */); |
| 1669 | case with_stmt: |
| 1670 | return ast_for_with_stmt(c, CHILD(n, 1), |
| 1671 | 1 /* is_async */); |
| 1672 | |
| 1673 | case for_stmt: |
| 1674 | return ast_for_for_stmt(c, CHILD(n, 1), |
| 1675 | 1 /* is_async */); |
| 1676 | |
| 1677 | default: |
| 1678 | PyErr_Format(PyExc_SystemError, |
| 1679 | "invalid async stament: %s", |
| 1680 | STR(CHILD(n, 1))); |
| 1681 | return NULL; |
| 1682 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1685 | static stmt_ty |
| 1686 | ast_for_decorated(struct compiling *c, const node *n) |
| 1687 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1688 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1689 | stmt_ty thing = NULL; |
| 1690 | asdl_seq *decorator_seq = NULL; |
| 1691 | |
| 1692 | REQ(n, decorated); |
| 1693 | |
| 1694 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1695 | if (!decorator_seq) |
| 1696 | return NULL; |
| 1697 | |
| 1698 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1699 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1701 | |
| 1702 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1703 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1704 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1705 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1706 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1707 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1708 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1709 | /* we count the decorators in when talking about the class' or |
| 1710 | * function's line number */ |
| 1711 | if (thing) { |
| 1712 | thing->lineno = LINENO(n); |
| 1713 | thing->col_offset = n->n_col_offset; |
| 1714 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1715 | return thing; |
| 1716 | } |
| 1717 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1718 | static expr_ty |
| 1719 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1720 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1721 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1722 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1723 | arguments_ty args; |
| 1724 | expr_ty expression; |
| 1725 | |
| 1726 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1727 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1728 | if (!args) |
| 1729 | return NULL; |
| 1730 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1731 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1733 | } |
| 1734 | else { |
| 1735 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1736 | if (!args) |
| 1737 | return NULL; |
| 1738 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1739 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1740 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1743 | 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] | 1744 | } |
| 1745 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1746 | static expr_ty |
| 1747 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1750 | expr_ty expression, body, orelse; |
| 1751 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1752 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1753 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1754 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1755 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1756 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1757 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1758 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1759 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1760 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1761 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1762 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1763 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1766 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1767 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1768 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1769 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1770 | */ |
| 1771 | |
| 1772 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1773 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1774 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1775 | int n_fors = 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1776 | int is_async; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1777 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1778 | count_comp_for: |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1779 | is_async = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1780 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1781 | REQ(n, comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1782 | if (TYPE(CHILD(n, 0)) == ASYNC) { |
| 1783 | is_async = 1; |
| 1784 | } |
| 1785 | if (NCH(n) == (5 + is_async)) { |
| 1786 | n = CHILD(n, 4 + is_async); |
| 1787 | } |
| 1788 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1789 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1790 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1791 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1792 | REQ(n, comp_iter); |
| 1793 | n = CHILD(n, 0); |
| 1794 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1795 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1796 | else if (TYPE(n) == comp_if) { |
| 1797 | if (NCH(n) == 3) { |
| 1798 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1799 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1800 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1801 | else |
| 1802 | return n_fors; |
| 1803 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1804 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1805 | /* Should never be reached */ |
| 1806 | PyErr_SetString(PyExc_SystemError, |
| 1807 | "logic error in count_comp_fors"); |
| 1808 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1811 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1812 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1813 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1814 | */ |
| 1815 | |
| 1816 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1817 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1818 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1819 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1820 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1821 | while (1) { |
| 1822 | REQ(n, comp_iter); |
| 1823 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1824 | return n_ifs; |
| 1825 | n = CHILD(n, 0); |
| 1826 | REQ(n, comp_if); |
| 1827 | n_ifs++; |
| 1828 | if (NCH(n) == 2) |
| 1829 | return n_ifs; |
| 1830 | n = CHILD(n, 2); |
| 1831 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1834 | static asdl_seq * |
| 1835 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1836 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1837 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1838 | asdl_seq *comps; |
| 1839 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1840 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1841 | if (n_fors == -1) |
| 1842 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1844 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1845 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1846 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1847 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1848 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1849 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1850 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1851 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1852 | node *for_ch; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1853 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1855 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1857 | if (TYPE(CHILD(n, 0)) == ASYNC) { |
| 1858 | is_async = 1; |
| 1859 | } |
| 1860 | |
| 1861 | for_ch = CHILD(n, 1 + is_async); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1862 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1863 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1864 | return NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1865 | expression = ast_for_expr(c, CHILD(n, 3 + is_async)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1866 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1867 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1868 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1869 | /* Check the # of children rather than the length of t, since |
| 1870 | (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] | 1871 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1872 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1873 | comp = comprehension(first, expression, NULL, |
| 1874 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1875 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1876 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1877 | first->col_offset, c->c_arena), |
| 1878 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1879 | if (!comp) |
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 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1882 | if (NCH(n) == (5 + is_async)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1883 | int j, n_ifs; |
| 1884 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1886 | n = CHILD(n, 4 + is_async); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1887 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1888 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1889 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1891 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1892 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1893 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1894 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1895 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1896 | REQ(n, comp_iter); |
| 1897 | n = CHILD(n, 0); |
| 1898 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1900 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1901 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1902 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1903 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1904 | if (NCH(n) == 3) |
| 1905 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1907 | /* on exit, must guarantee that n is a comp_for */ |
| 1908 | if (TYPE(n) == comp_iter) |
| 1909 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1910 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1911 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1912 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1913 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1914 | return comps; |
| 1915 | } |
| 1916 | |
| 1917 | static expr_ty |
| 1918 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1919 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1920 | /* testlist_comp: (test|star_expr) |
| 1921 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1922 | expr_ty elt; |
| 1923 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1924 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1926 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1928 | ch = CHILD(n, 0); |
| 1929 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1930 | if (!elt) |
| 1931 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1932 | if (elt->kind == Starred_kind) { |
| 1933 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1934 | return NULL; |
| 1935 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1937 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1938 | if (!comps) |
| 1939 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1940 | |
| 1941 | if (type == COMP_GENEXP) |
| 1942 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1943 | else if (type == COMP_LISTCOMP) |
| 1944 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1945 | else if (type == COMP_SETCOMP) |
| 1946 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1947 | else |
| 1948 | /* Should never happen */ |
| 1949 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1952 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1953 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1954 | * elements. Iff successful, nonzero is returned. |
| 1955 | */ |
| 1956 | static int |
| 1957 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1958 | expr_ty *key, expr_ty *value) |
| 1959 | { |
| 1960 | expr_ty expression; |
| 1961 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1962 | assert(NCH(n) - *i >= 2); |
| 1963 | |
| 1964 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1965 | if (!expression) |
| 1966 | return 0; |
| 1967 | *key = NULL; |
| 1968 | *value = expression; |
| 1969 | |
| 1970 | *i += 2; |
| 1971 | } |
| 1972 | else { |
| 1973 | assert(NCH(n) - *i >= 3); |
| 1974 | |
| 1975 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1976 | if (!expression) |
| 1977 | return 0; |
| 1978 | *key = expression; |
| 1979 | |
| 1980 | REQ(CHILD(n, *i + 1), COLON); |
| 1981 | |
| 1982 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1983 | if (!expression) |
| 1984 | return 0; |
| 1985 | *value = expression; |
| 1986 | |
| 1987 | *i += 3; |
| 1988 | } |
| 1989 | return 1; |
| 1990 | } |
| 1991 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1992 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1993 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1994 | { |
| 1995 | expr_ty key, value; |
| 1996 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1997 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1999 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2000 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2001 | assert(key); |
| 2002 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2004 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2005 | if (!comps) |
| 2006 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2008 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 2009 | } |
| 2010 | |
| 2011 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2012 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2013 | { |
| 2014 | int i; |
| 2015 | int j; |
| 2016 | int size; |
| 2017 | asdl_seq *keys, *values; |
| 2018 | |
| 2019 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2020 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2021 | if (!keys) |
| 2022 | return NULL; |
| 2023 | |
| 2024 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2025 | if (!values) |
| 2026 | return NULL; |
| 2027 | |
| 2028 | j = 0; |
| 2029 | for (i = 0; i < NCH(n); i++) { |
| 2030 | expr_ty key, value; |
| 2031 | |
| 2032 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2033 | return NULL; |
| 2034 | asdl_seq_SET(keys, j, key); |
| 2035 | asdl_seq_SET(values, j, value); |
| 2036 | |
| 2037 | j++; |
| 2038 | } |
| 2039 | keys->size = j; |
| 2040 | values->size = j; |
| 2041 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2042 | } |
| 2043 | |
| 2044 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2045 | ast_for_genexp(struct compiling *c, const node *n) |
| 2046 | { |
| 2047 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2048 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | static expr_ty |
| 2052 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2053 | { |
| 2054 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2055 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | static expr_ty |
| 2059 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2060 | { |
| 2061 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2062 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2065 | static expr_ty |
| 2066 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2067 | { |
| 2068 | int i; |
| 2069 | int size; |
| 2070 | asdl_seq *elts; |
| 2071 | |
| 2072 | assert(TYPE(n) == (dictorsetmaker)); |
| 2073 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2074 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2075 | if (!elts) |
| 2076 | return NULL; |
| 2077 | for (i = 0; i < NCH(n); i += 2) { |
| 2078 | expr_ty expression; |
| 2079 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2080 | if (!expression) |
| 2081 | return NULL; |
| 2082 | asdl_seq_SET(elts, i / 2, expression); |
| 2083 | } |
| 2084 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2085 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2086 | |
| 2087 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2088 | ast_for_atom(struct compiling *c, const node *n) |
| 2089 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2090 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2091 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2092 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2093 | */ |
| 2094 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2096 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2097 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2098 | PyObject *name; |
| 2099 | const char *s = STR(ch); |
| 2100 | size_t len = strlen(s); |
| 2101 | if (len >= 4 && len <= 5) { |
| 2102 | if (!strcmp(s, "None")) |
| 2103 | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
| 2104 | if (!strcmp(s, "True")) |
| 2105 | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
| 2106 | if (!strcmp(s, "False")) |
| 2107 | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
| 2108 | } |
| 2109 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2110 | if (!name) |
| 2111 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2112 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2113 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2114 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2115 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2116 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2117 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2118 | const char *errtype = NULL; |
| 2119 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2120 | errtype = "unicode error"; |
| 2121 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2122 | errtype = "value error"; |
| 2123 | if (errtype) { |
| 2124 | char buf[128]; |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2125 | const char *s = NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2126 | PyObject *type, *value, *tback, *errstr; |
| 2127 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2128 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2129 | if (errstr) |
| 2130 | s = PyUnicode_AsUTF8(errstr); |
| 2131 | if (s) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2132 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2133 | } else { |
Victor Stinner | 00723e0 | 2015-09-03 12:57:11 +0200 | [diff] [blame] | 2134 | PyErr_Clear(); |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2135 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2136 | } |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2137 | Py_XDECREF(errstr); |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2138 | ast_error(c, n, buf); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2139 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2140 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2141 | Py_XDECREF(tback); |
| 2142 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2143 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2144 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2145 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2146 | } |
| 2147 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2148 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2149 | if (!pynum) |
| 2150 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2151 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2152 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2153 | Py_DECREF(pynum); |
| 2154 | return NULL; |
| 2155 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2156 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2157 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2158 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2159 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2160 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2161 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2163 | if (TYPE(ch) == RPAR) |
| 2164 | 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] | 2165 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2166 | if (TYPE(ch) == yield_expr) |
| 2167 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2170 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2171 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2172 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2173 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2174 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2175 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2177 | if (TYPE(ch) == RSQB) |
| 2178 | 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] | 2179 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2180 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2181 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2182 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2183 | if (!elts) |
| 2184 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2185 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2186 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2187 | } |
| 2188 | else |
| 2189 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2190 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2191 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2192 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2193 | * ((test | '*' test) |
| 2194 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2195 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2196 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2197 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2198 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2199 | 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] | 2200 | } |
| 2201 | else { |
| 2202 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2203 | if (NCH(ch) == 1 || |
| 2204 | (NCH(ch) > 1 && |
| 2205 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2206 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2207 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2208 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2209 | else if (NCH(ch) > 1 && |
| 2210 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2211 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2212 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2213 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2214 | else if (NCH(ch) > 3 - is_dict && |
| 2215 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2216 | /* It's a dictionary comprehension. */ |
| 2217 | if (is_dict) { |
| 2218 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2219 | "dict comprehension"); |
| 2220 | return NULL; |
| 2221 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2222 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2223 | } |
| 2224 | else { |
| 2225 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2226 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2227 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2228 | if (res) { |
| 2229 | res->lineno = LINENO(n); |
| 2230 | res->col_offset = n->n_col_offset; |
| 2231 | } |
| 2232 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2233 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2234 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2235 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2236 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2237 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | static slice_ty |
| 2242 | ast_for_slice(struct compiling *c, const node *n) |
| 2243 | { |
| 2244 | node *ch; |
| 2245 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2246 | |
| 2247 | REQ(n, subscript); |
| 2248 | |
| 2249 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2250 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2251 | sliceop: ':' [test] |
| 2252 | */ |
| 2253 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2254 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2255 | /* 'step' variable hold no significance in terms of being used over |
| 2256 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2258 | if (!step) |
| 2259 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2261 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2265 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2266 | if (!lower) |
| 2267 | return NULL; |
| 2268 | } |
| 2269 | |
| 2270 | /* If there's an upper bound it's in the second or third position. */ |
| 2271 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2272 | if (NCH(n) > 1) { |
| 2273 | node *n2 = CHILD(n, 1); |
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 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2280 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2281 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2282 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2283 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2284 | if (TYPE(n2) == test) { |
| 2285 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2286 | if (!upper) |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | ch = CHILD(n, NCH(n) - 1); |
| 2292 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2293 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2294 | ch = CHILD(ch, 1); |
| 2295 | if (TYPE(ch) == test) { |
| 2296 | step = ast_for_expr(c, ch); |
| 2297 | if (!step) |
| 2298 | return NULL; |
| 2299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2303 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | static expr_ty |
| 2307 | ast_for_binop(struct compiling *c, const node *n) |
| 2308 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2309 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2311 | BinOp(BinOp(A, op, B), op, C). |
| 2312 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2313 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2314 | int i, nops; |
| 2315 | expr_ty expr1, expr2, result; |
| 2316 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2317 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2318 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2319 | if (!expr1) |
| 2320 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2321 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2322 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2323 | if (!expr2) |
| 2324 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2325 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2326 | newoperator = get_operator(CHILD(n, 1)); |
| 2327 | if (!newoperator) |
| 2328 | return NULL; |
| 2329 | |
| 2330 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2331 | c->c_arena); |
| 2332 | if (!result) |
| 2333 | return NULL; |
| 2334 | |
| 2335 | nops = (NCH(n) - 1) / 2; |
| 2336 | for (i = 1; i < nops; i++) { |
| 2337 | expr_ty tmp_result, tmp; |
| 2338 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2339 | |
| 2340 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2341 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2342 | return NULL; |
| 2343 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2344 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2345 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2346 | return NULL; |
| 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2349 | LINENO(next_oper), next_oper->n_col_offset, |
| 2350 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2352 | return NULL; |
| 2353 | result = tmp_result; |
| 2354 | } |
| 2355 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2358 | static expr_ty |
| 2359 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2362 | subscriptlist: subscript (',' subscript)* [','] |
| 2363 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2364 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2365 | REQ(n, trailer); |
| 2366 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2367 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2368 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2369 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2370 | else |
| 2371 | return ast_for_call(c, CHILD(n, 1), left_expr); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2372 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2373 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2374 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2375 | if (!attr_id) |
| 2376 | return NULL; |
| 2377 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2378 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2379 | } |
| 2380 | else { |
| 2381 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2382 | REQ(CHILD(n, 2), RSQB); |
| 2383 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2384 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2385 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2386 | if (!slc) |
| 2387 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2388 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2389 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2390 | } |
| 2391 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2393 | by treating the sequence as a tuple literal if there are |
| 2394 | no slice features. |
| 2395 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2396 | int j; |
| 2397 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2398 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2399 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2400 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2401 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2402 | if (!slices) |
| 2403 | return NULL; |
| 2404 | for (j = 0; j < NCH(n); j += 2) { |
| 2405 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2406 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2407 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2408 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2409 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2410 | asdl_seq_SET(slices, j / 2, slc); |
| 2411 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2412 | if (!simple) { |
| 2413 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2414 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2415 | } |
| 2416 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2417 | 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] | 2418 | if (!elts) |
| 2419 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2420 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2421 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2422 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2423 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2424 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2425 | 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] | 2426 | if (!e) |
| 2427 | return NULL; |
| 2428 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2429 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2430 | } |
| 2431 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2435 | ast_for_factor(struct compiling *c, const node *n) |
| 2436 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2437 | expr_ty expression; |
| 2438 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2439 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2440 | if (!expression) |
| 2441 | return NULL; |
| 2442 | |
| 2443 | switch (TYPE(CHILD(n, 0))) { |
| 2444 | case PLUS: |
| 2445 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2446 | c->c_arena); |
| 2447 | case MINUS: |
| 2448 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2449 | c->c_arena); |
| 2450 | case TILDE: |
| 2451 | return UnaryOp(Invert, expression, LINENO(n), |
| 2452 | n->n_col_offset, c->c_arena); |
| 2453 | } |
| 2454 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2455 | TYPE(CHILD(n, 0))); |
| 2456 | return NULL; |
| 2457 | } |
| 2458 | |
| 2459 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2460 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2461 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2462 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2463 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2464 | |
| 2465 | REQ(n, atom_expr); |
| 2466 | nch = NCH(n); |
| 2467 | |
| 2468 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2469 | start = 1; |
| 2470 | assert(nch > 1); |
| 2471 | } |
| 2472 | |
| 2473 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2474 | if (!e) |
| 2475 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2476 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2477 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2478 | if (start && nch == 2) { |
| 2479 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2480 | } |
| 2481 | |
| 2482 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2483 | node *ch = CHILD(n, i); |
| 2484 | if (TYPE(ch) != trailer) |
| 2485 | break; |
| 2486 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2487 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2488 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2489 | tmp->lineno = e->lineno; |
| 2490 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2491 | e = tmp; |
| 2492 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2493 | |
| 2494 | if (start) { |
| 2495 | /* there was an AWAIT */ |
| 2496 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2497 | } |
| 2498 | else { |
| 2499 | return e; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | static expr_ty |
| 2504 | ast_for_power(struct compiling *c, const node *n) |
| 2505 | { |
| 2506 | /* power: atom trailer* ('**' factor)* |
| 2507 | */ |
| 2508 | expr_ty e; |
| 2509 | REQ(n, power); |
| 2510 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2511 | if (!e) |
| 2512 | return NULL; |
| 2513 | if (NCH(n) == 1) |
| 2514 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2515 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2516 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2517 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2518 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2519 | 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] | 2520 | } |
| 2521 | return e; |
| 2522 | } |
| 2523 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2524 | static expr_ty |
| 2525 | ast_for_starred(struct compiling *c, const node *n) |
| 2526 | { |
| 2527 | expr_ty tmp; |
| 2528 | REQ(n, star_expr); |
| 2529 | |
| 2530 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2531 | if (!tmp) |
| 2532 | return NULL; |
| 2533 | |
| 2534 | /* The Load context is changed later. */ |
| 2535 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2536 | } |
| 2537 | |
| 2538 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2539 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2540 | */ |
| 2541 | |
| 2542 | static expr_ty |
| 2543 | ast_for_expr(struct compiling *c, const node *n) |
| 2544 | { |
| 2545 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2546 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2547 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2549 | and_test: not_test ('and' not_test)* |
| 2550 | not_test: 'not' not_test | comparison |
| 2551 | comparison: expr (comp_op expr)* |
| 2552 | expr: xor_expr ('|' xor_expr)* |
| 2553 | xor_expr: and_expr ('^' and_expr)* |
| 2554 | and_expr: shift_expr ('&' shift_expr)* |
| 2555 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2556 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2557 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2558 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2559 | power: atom_expr ['**' factor] |
| 2560 | atom_expr: [AWAIT] atom trailer* |
| 2561 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2562 | */ |
| 2563 | |
| 2564 | asdl_seq *seq; |
| 2565 | int i; |
| 2566 | |
| 2567 | loop: |
| 2568 | switch (TYPE(n)) { |
| 2569 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2570 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2571 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2572 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2573 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2574 | else if (NCH(n) > 1) |
| 2575 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2576 | /* Fallthrough */ |
| 2577 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2578 | case and_test: |
| 2579 | if (NCH(n) == 1) { |
| 2580 | n = CHILD(n, 0); |
| 2581 | goto loop; |
| 2582 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2583 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2584 | if (!seq) |
| 2585 | return NULL; |
| 2586 | for (i = 0; i < NCH(n); i += 2) { |
| 2587 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2588 | if (!e) |
| 2589 | return NULL; |
| 2590 | asdl_seq_SET(seq, i / 2, e); |
| 2591 | } |
| 2592 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2593 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2594 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2595 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2596 | 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] | 2597 | case not_test: |
| 2598 | if (NCH(n) == 1) { |
| 2599 | n = CHILD(n, 0); |
| 2600 | goto loop; |
| 2601 | } |
| 2602 | else { |
| 2603 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2604 | if (!expression) |
| 2605 | return NULL; |
| 2606 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2607 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2608 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2609 | } |
| 2610 | case comparison: |
| 2611 | if (NCH(n) == 1) { |
| 2612 | n = CHILD(n, 0); |
| 2613 | goto loop; |
| 2614 | } |
| 2615 | else { |
| 2616 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2617 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2618 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2619 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2620 | if (!ops) |
| 2621 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2622 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2623 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2624 | return NULL; |
| 2625 | } |
| 2626 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2627 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2628 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2629 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2630 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2631 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2632 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2633 | |
| 2634 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2635 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2636 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2637 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2639 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 | asdl_seq_SET(cmps, i / 2, expression); |
| 2641 | } |
| 2642 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2643 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2644 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2645 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2646 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2647 | return Compare(expression, ops, cmps, LINENO(n), |
| 2648 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2649 | } |
| 2650 | break; |
| 2651 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2652 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | /* The next five cases all handle BinOps. The main body of code |
| 2655 | is the same in each case, but the switch turned inside out to |
| 2656 | reuse the code for each type of operator. |
| 2657 | */ |
| 2658 | case expr: |
| 2659 | case xor_expr: |
| 2660 | case and_expr: |
| 2661 | case shift_expr: |
| 2662 | case arith_expr: |
| 2663 | case term: |
| 2664 | if (NCH(n) == 1) { |
| 2665 | n = CHILD(n, 0); |
| 2666 | goto loop; |
| 2667 | } |
| 2668 | return ast_for_binop(c, n); |
| 2669 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2670 | node *an = NULL; |
| 2671 | node *en = NULL; |
| 2672 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2673 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2674 | if (NCH(n) > 1) |
| 2675 | an = CHILD(n, 1); /* yield_arg */ |
| 2676 | if (an) { |
| 2677 | en = CHILD(an, NCH(an) - 1); |
| 2678 | if (NCH(an) == 2) { |
| 2679 | is_from = 1; |
| 2680 | exp = ast_for_expr(c, en); |
| 2681 | } |
| 2682 | else |
| 2683 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2684 | if (!exp) |
| 2685 | return NULL; |
| 2686 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2687 | if (is_from) |
| 2688 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2689 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2690 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2691 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | if (NCH(n) == 1) { |
| 2693 | n = CHILD(n, 0); |
| 2694 | goto loop; |
| 2695 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2696 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2697 | case power: |
| 2698 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2700 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2701 | return NULL; |
| 2702 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2703 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | return NULL; |
| 2705 | } |
| 2706 | |
| 2707 | static expr_ty |
| 2708 | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
| 2709 | { |
| 2710 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2711 | arglist: argument (',' argument)* [','] |
| 2712 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2713 | */ |
| 2714 | |
| 2715 | int i, nargs, nkeywords, ngens; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2716 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2717 | asdl_seq *args; |
| 2718 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2719 | |
| 2720 | REQ(n, arglist); |
| 2721 | |
| 2722 | nargs = 0; |
| 2723 | nkeywords = 0; |
| 2724 | ngens = 0; |
| 2725 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2726 | node *ch = CHILD(n, i); |
| 2727 | if (TYPE(ch) == argument) { |
| 2728 | if (NCH(ch) == 1) |
| 2729 | nargs++; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2730 | else if (TYPE(CHILD(ch, 1)) == comp_for) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2731 | ngens++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2732 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2733 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2734 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2735 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2736 | nkeywords++; |
| 2737 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 | } |
| 2739 | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2740 | ast_error(c, n, "Generator expression must be parenthesized " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2741 | "if not sole argument"); |
| 2742 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2745 | args = _Py_asdl_seq_new(nargs + ngens, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2746 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2747 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2748 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2749 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2750 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2751 | |
| 2752 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2753 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2754 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2755 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2756 | node *ch = CHILD(n, i); |
| 2757 | if (TYPE(ch) == argument) { |
| 2758 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2759 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2760 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2761 | /* a positional argument */ |
| 2762 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2763 | if (ndoublestars) { |
| 2764 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2765 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2766 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2767 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2768 | else { |
| 2769 | ast_error(c, chch, |
| 2770 | "positional argument follows " |
| 2771 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2772 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2773 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2774 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2775 | e = ast_for_expr(c, chch); |
| 2776 | if (!e) |
| 2777 | return NULL; |
| 2778 | asdl_seq_SET(args, nargs++, e); |
| 2779 | } |
| 2780 | else if (TYPE(chch) == STAR) { |
| 2781 | /* an iterable argument unpacking */ |
| 2782 | expr_ty starred; |
| 2783 | if (ndoublestars) { |
| 2784 | ast_error(c, chch, |
| 2785 | "iterable argument unpacking follows " |
| 2786 | "keyword argument unpacking"); |
| 2787 | return NULL; |
| 2788 | } |
| 2789 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2790 | if (!e) |
| 2791 | return NULL; |
| 2792 | starred = Starred(e, Load, LINENO(chch), |
| 2793 | chch->n_col_offset, |
| 2794 | c->c_arena); |
| 2795 | if (!starred) |
| 2796 | return NULL; |
| 2797 | asdl_seq_SET(args, nargs++, starred); |
| 2798 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2799 | } |
| 2800 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2801 | /* a keyword argument unpacking */ |
| 2802 | keyword_ty kw; |
| 2803 | i++; |
| 2804 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2805 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2806 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2807 | kw = keyword(NULL, e, c->c_arena); |
| 2808 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2809 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2811 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2812 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2813 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2814 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2815 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2816 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2818 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2819 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2820 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2821 | identifier key, tmp; |
| 2822 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2823 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2824 | /* chch is test, but must be an identifier? */ |
| 2825 | e = ast_for_expr(c, chch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2826 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2827 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2828 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2829 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 2830 | * SF bug 132313 points out that complaining about a keyword |
| 2831 | * then is very confusing. |
| 2832 | */ |
| 2833 | if (e->kind == Lambda_kind) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2834 | ast_error(c, chch, |
| 2835 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2836 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2837 | } |
| 2838 | else if (e->kind != Name_kind) { |
| 2839 | ast_error(c, chch, |
| 2840 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2841 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2842 | } |
| 2843 | else if (forbidden_name(c, e->v.Name.id, ch, 1)) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2844 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2846 | key = e->v.Name.id; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2847 | for (k = 0; k < nkeywords; k++) { |
| 2848 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2849 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2850 | ast_error(c, chch, |
| 2851 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2852 | return NULL; |
| 2853 | } |
| 2854 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2855 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2857 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2858 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2859 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2860 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2861 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2862 | } |
| 2863 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2866 | 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] | 2867 | } |
| 2868 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2870 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2871 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2872 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2873 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2874 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2875 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2876 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2877 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2878 | } |
| 2879 | else { |
| 2880 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2881 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2882 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2884 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2885 | else { |
| 2886 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2887 | if (!tmp) |
| 2888 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2889 | 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] | 2890 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2893 | static stmt_ty |
| 2894 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2895 | { |
| 2896 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2897 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2898 | ('=' (yield_expr|testlist_star_expr))*) |
| 2899 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2900 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2901 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2902 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2903 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | */ |
| 2905 | |
| 2906 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2907 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2908 | if (!e) |
| 2909 | return NULL; |
| 2910 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2911 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2912 | } |
| 2913 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2914 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2915 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2916 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2917 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2918 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2919 | if (!expr1) |
| 2920 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2921 | if(!set_context(c, expr1, Store, ch)) |
| 2922 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2923 | /* set_context checks that most expressions are not the left side. |
| 2924 | Augmented assignments can only have a name, a subscript, or an |
| 2925 | attribute on the left, though, so we have to explicitly check for |
| 2926 | those. */ |
| 2927 | switch (expr1->kind) { |
| 2928 | case Name_kind: |
| 2929 | case Attribute_kind: |
| 2930 | case Subscript_kind: |
| 2931 | break; |
| 2932 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2933 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2934 | return NULL; |
| 2935 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2936 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2937 | ch = CHILD(n, 2); |
| 2938 | if (TYPE(ch) == testlist) |
| 2939 | expr2 = ast_for_testlist(c, ch); |
| 2940 | else |
| 2941 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2942 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | return NULL; |
| 2944 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2945 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2946 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | return NULL; |
| 2948 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2949 | 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] | 2950 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2951 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2952 | expr_ty expr1, expr2, expr3; |
| 2953 | node *ch = CHILD(n, 0); |
| 2954 | node *deep, *ann = CHILD(n, 1); |
| 2955 | int simple = 1; |
| 2956 | |
| 2957 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2958 | deep = ch; |
| 2959 | while (NCH(deep) == 1) { |
| 2960 | deep = CHILD(deep, 0); |
| 2961 | } |
| 2962 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2963 | simple = 0; |
| 2964 | } |
| 2965 | expr1 = ast_for_testlist(c, ch); |
| 2966 | if (!expr1) { |
| 2967 | return NULL; |
| 2968 | } |
| 2969 | switch (expr1->kind) { |
| 2970 | case Name_kind: |
| 2971 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2972 | return NULL; |
| 2973 | } |
| 2974 | expr1->v.Name.ctx = Store; |
| 2975 | break; |
| 2976 | case Attribute_kind: |
| 2977 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | expr1->v.Attribute.ctx = Store; |
| 2981 | break; |
| 2982 | case Subscript_kind: |
| 2983 | expr1->v.Subscript.ctx = Store; |
| 2984 | break; |
| 2985 | case List_kind: |
| 2986 | ast_error(c, ch, |
| 2987 | "only single target (not list) can be annotated"); |
| 2988 | return NULL; |
| 2989 | case Tuple_kind: |
| 2990 | ast_error(c, ch, |
| 2991 | "only single target (not tuple) can be annotated"); |
| 2992 | return NULL; |
| 2993 | default: |
| 2994 | ast_error(c, ch, |
| 2995 | "illegal target for annotation"); |
| 2996 | return NULL; |
| 2997 | } |
| 2998 | |
| 2999 | if (expr1->kind != Name_kind) { |
| 3000 | simple = 0; |
| 3001 | } |
| 3002 | ch = CHILD(ann, 1); |
| 3003 | expr2 = ast_for_expr(c, ch); |
| 3004 | if (!expr2) { |
| 3005 | return NULL; |
| 3006 | } |
| 3007 | if (NCH(ann) == 2) { |
| 3008 | return AnnAssign(expr1, expr2, NULL, simple, |
| 3009 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3010 | } |
| 3011 | else { |
| 3012 | ch = CHILD(ann, 3); |
| 3013 | expr3 = ast_for_expr(c, ch); |
| 3014 | if (!expr3) { |
| 3015 | return NULL; |
| 3016 | } |
| 3017 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3018 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3019 | } |
| 3020 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3021 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3022 | int i; |
| 3023 | asdl_seq *targets; |
| 3024 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3025 | expr_ty expression; |
| 3026 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3027 | /* a normal assignment */ |
| 3028 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3029 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3030 | if (!targets) |
| 3031 | return NULL; |
| 3032 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3033 | expr_ty e; |
| 3034 | node *ch = CHILD(n, i); |
| 3035 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3036 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3037 | return NULL; |
| 3038 | } |
| 3039 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3041 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3042 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3043 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3044 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3045 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3046 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3047 | asdl_seq_SET(targets, i / 2, e); |
| 3048 | } |
| 3049 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3050 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3051 | expression = ast_for_testlist(c, value); |
| 3052 | else |
| 3053 | expression = ast_for_expr(c, value); |
| 3054 | if (!expression) |
| 3055 | return NULL; |
| 3056 | 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] | 3057 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3060 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3061 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3062 | 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] | 3063 | { |
| 3064 | asdl_seq *seq; |
| 3065 | int i; |
| 3066 | expr_ty e; |
| 3067 | |
| 3068 | REQ(n, exprlist); |
| 3069 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3070 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3071 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3072 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3073 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3074 | e = ast_for_expr(c, CHILD(n, i)); |
| 3075 | if (!e) |
| 3076 | return NULL; |
| 3077 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3078 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3079 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3080 | } |
| 3081 | return seq; |
| 3082 | } |
| 3083 | |
| 3084 | static stmt_ty |
| 3085 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3086 | { |
| 3087 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3089 | /* del_stmt: 'del' exprlist */ |
| 3090 | REQ(n, del_stmt); |
| 3091 | |
| 3092 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3093 | if (!expr_list) |
| 3094 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3095 | 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] | 3096 | } |
| 3097 | |
| 3098 | static stmt_ty |
| 3099 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3100 | { |
| 3101 | /* |
| 3102 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3103 | | yield_stmt |
| 3104 | break_stmt: 'break' |
| 3105 | continue_stmt: 'continue' |
| 3106 | return_stmt: 'return' [testlist] |
| 3107 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3108 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3109 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3110 | */ |
| 3111 | node *ch; |
| 3112 | |
| 3113 | REQ(n, flow_stmt); |
| 3114 | ch = CHILD(n, 0); |
| 3115 | switch (TYPE(ch)) { |
| 3116 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3117 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3118 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3119 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3120 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3121 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3122 | if (!exp) |
| 3123 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3124 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3125 | } |
| 3126 | case return_stmt: |
| 3127 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3128 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3129 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3130 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3131 | if (!expression) |
| 3132 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3133 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3134 | } |
| 3135 | case raise_stmt: |
| 3136 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3137 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3138 | else if (NCH(ch) >= 2) { |
| 3139 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3140 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3141 | if (!expression) |
| 3142 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3143 | if (NCH(ch) == 4) { |
| 3144 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3145 | if (!cause) |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | 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] | 3149 | } |
| 3150 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3151 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3153 | return NULL; |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3158 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3159 | { |
| 3160 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3161 | import_as_name: NAME ['as' NAME] |
| 3162 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3163 | dotted_name: NAME ('.' NAME)* |
| 3164 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3165 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3166 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3167 | loop: |
| 3168 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3169 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3170 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3171 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3172 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3173 | if (!name) |
| 3174 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3175 | if (NCH(n) == 3) { |
| 3176 | node *str_node = CHILD(n, 2); |
| 3177 | str = NEW_IDENTIFIER(str_node); |
| 3178 | if (!str) |
| 3179 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3180 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3181 | return NULL; |
| 3182 | } |
| 3183 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3184 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3185 | return NULL; |
| 3186 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3187 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3188 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3189 | case dotted_as_name: |
| 3190 | if (NCH(n) == 1) { |
| 3191 | n = CHILD(n, 0); |
| 3192 | goto loop; |
| 3193 | } |
| 3194 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3195 | node *asname_node = CHILD(n, 2); |
| 3196 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3197 | if (!a) |
| 3198 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3199 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3200 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3201 | if (!a->asname) |
| 3202 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3203 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3204 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3205 | return a; |
| 3206 | } |
| 3207 | break; |
| 3208 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3209 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3210 | node *name_node = CHILD(n, 0); |
| 3211 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3212 | if (!name) |
| 3213 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3214 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3215 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3216 | return alias(name, NULL, c->c_arena); |
| 3217 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3218 | else { |
| 3219 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3220 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3221 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3222 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3224 | |
| 3225 | len = 0; |
| 3226 | for (i = 0; i < NCH(n); i += 2) |
| 3227 | /* length of string plus one for the dot */ |
| 3228 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3229 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3230 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3231 | if (!str) |
| 3232 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3233 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3234 | if (!s) |
| 3235 | return NULL; |
| 3236 | for (i = 0; i < NCH(n); i += 2) { |
| 3237 | char *sch = STR(CHILD(n, i)); |
| 3238 | strcpy(s, STR(CHILD(n, i))); |
| 3239 | s += strlen(sch); |
| 3240 | *s++ = '.'; |
| 3241 | } |
| 3242 | --s; |
| 3243 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3245 | PyBytes_GET_SIZE(str), |
| 3246 | NULL); |
| 3247 | Py_DECREF(str); |
| 3248 | if (!uni) |
| 3249 | return NULL; |
| 3250 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3251 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3252 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3253 | Py_DECREF(str); |
| 3254 | return NULL; |
| 3255 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3256 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3257 | } |
| 3258 | break; |
| 3259 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3260 | str = PyUnicode_InternFromString("*"); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3261 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3262 | Py_DECREF(str); |
| 3263 | return NULL; |
| 3264 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3265 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3266 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3267 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3268 | "unexpected import name: %d", TYPE(n)); |
| 3269 | return NULL; |
| 3270 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3271 | |
| 3272 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3273 | return NULL; |
| 3274 | } |
| 3275 | |
| 3276 | static stmt_ty |
| 3277 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3278 | { |
| 3279 | /* |
| 3280 | import_stmt: import_name | import_from |
| 3281 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3282 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3283 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3284 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3285 | int lineno; |
| 3286 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3287 | int i; |
| 3288 | asdl_seq *aliases; |
| 3289 | |
| 3290 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3291 | lineno = LINENO(n); |
| 3292 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3294 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3295 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3296 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3297 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3298 | if (!aliases) |
| 3299 | return NULL; |
| 3300 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3301 | 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] | 3302 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3304 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3305 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3306 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3308 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3309 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3310 | int idx, ndots = 0; |
| 3311 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3312 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3313 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3314 | /* Count the number of dots (for relative imports) and check for the |
| 3315 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3316 | for (idx = 1; idx < NCH(n); idx++) { |
| 3317 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3318 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3319 | if (!mod) |
| 3320 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3321 | idx++; |
| 3322 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3323 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3325 | ndots += 3; |
| 3326 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3327 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3328 | break; |
| 3329 | } |
| 3330 | ndots++; |
| 3331 | } |
| 3332 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3333 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3334 | case STAR: |
| 3335 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3336 | n = CHILD(n, idx); |
| 3337 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3338 | break; |
| 3339 | case LPAR: |
| 3340 | /* from ... import (x, y, z) */ |
| 3341 | n = CHILD(n, idx + 1); |
| 3342 | n_children = NCH(n); |
| 3343 | break; |
| 3344 | case import_as_names: |
| 3345 | /* from ... import x, y, z */ |
| 3346 | n = CHILD(n, idx); |
| 3347 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3348 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3349 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3350 | " surrounding parentheses"); |
| 3351 | return NULL; |
| 3352 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3353 | break; |
| 3354 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3355 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3356 | return NULL; |
| 3357 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3358 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3359 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3360 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3361 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3362 | |
| 3363 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3364 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3365 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3366 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3367 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3368 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3369 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3370 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3371 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3372 | 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] | 3373 | if (!import_alias) |
| 3374 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3375 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3376 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3377 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3378 | if (mod != NULL) |
| 3379 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3380 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3381 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3382 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3383 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3384 | "unknown import statement: starts with command '%s'", |
| 3385 | STR(CHILD(n, 0))); |
| 3386 | return NULL; |
| 3387 | } |
| 3388 | |
| 3389 | static stmt_ty |
| 3390 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3391 | { |
| 3392 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3393 | identifier name; |
| 3394 | asdl_seq *s; |
| 3395 | int i; |
| 3396 | |
| 3397 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3398 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3399 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3400 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3401 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3402 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3403 | if (!name) |
| 3404 | return NULL; |
| 3405 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3406 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3407 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3411 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3412 | { |
| 3413 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3414 | identifier name; |
| 3415 | asdl_seq *s; |
| 3416 | int i; |
| 3417 | |
| 3418 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3419 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3420 | if (!s) |
| 3421 | return NULL; |
| 3422 | for (i = 1; i < NCH(n); i += 2) { |
| 3423 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3424 | if (!name) |
| 3425 | return NULL; |
| 3426 | asdl_seq_SET(s, i / 2, name); |
| 3427 | } |
| 3428 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3429 | } |
| 3430 | |
| 3431 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3432 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3433 | { |
| 3434 | /* assert_stmt: 'assert' test [',' test] */ |
| 3435 | REQ(n, assert_stmt); |
| 3436 | if (NCH(n) == 2) { |
| 3437 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3438 | if (!expression) |
| 3439 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3440 | 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] | 3441 | } |
| 3442 | else if (NCH(n) == 4) { |
| 3443 | expr_ty expr1, expr2; |
| 3444 | |
| 3445 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3446 | if (!expr1) |
| 3447 | return NULL; |
| 3448 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3449 | if (!expr2) |
| 3450 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3452 | 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] | 3453 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3454 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3455 | "improper number of parts to 'assert' statement: %d", |
| 3456 | NCH(n)); |
| 3457 | return NULL; |
| 3458 | } |
| 3459 | |
| 3460 | static asdl_seq * |
| 3461 | ast_for_suite(struct compiling *c, const node *n) |
| 3462 | { |
| 3463 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3464 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3465 | stmt_ty s; |
| 3466 | int i, total, num, end, pos = 0; |
| 3467 | node *ch; |
| 3468 | |
| 3469 | REQ(n, suite); |
| 3470 | |
| 3471 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3472 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3473 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3474 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3476 | n = CHILD(n, 0); |
| 3477 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3478 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3479 | */ |
| 3480 | end = NCH(n) - 1; |
| 3481 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3482 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3483 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3484 | for (i = 0; i < end; i += 2) { |
| 3485 | ch = CHILD(n, i); |
| 3486 | s = ast_for_stmt(c, ch); |
| 3487 | if (!s) |
| 3488 | return NULL; |
| 3489 | asdl_seq_SET(seq, pos++, s); |
| 3490 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3491 | } |
| 3492 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3493 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3494 | ch = CHILD(n, i); |
| 3495 | REQ(ch, stmt); |
| 3496 | num = num_stmts(ch); |
| 3497 | if (num == 1) { |
| 3498 | /* small_stmt or compound_stmt with only one child */ |
| 3499 | s = ast_for_stmt(c, ch); |
| 3500 | if (!s) |
| 3501 | return NULL; |
| 3502 | asdl_seq_SET(seq, pos++, s); |
| 3503 | } |
| 3504 | else { |
| 3505 | int j; |
| 3506 | ch = CHILD(ch, 0); |
| 3507 | REQ(ch, simple_stmt); |
| 3508 | for (j = 0; j < NCH(ch); j += 2) { |
| 3509 | /* statement terminates with a semi-colon ';' */ |
| 3510 | if (NCH(CHILD(ch, j)) == 0) { |
| 3511 | assert((j + 1) == NCH(ch)); |
| 3512 | break; |
| 3513 | } |
| 3514 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3515 | if (!s) |
| 3516 | return NULL; |
| 3517 | asdl_seq_SET(seq, pos++, s); |
| 3518 | } |
| 3519 | } |
| 3520 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3521 | } |
| 3522 | assert(pos == seq->size); |
| 3523 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3526 | static string |
| 3527 | docstring_from_stmts(asdl_seq *stmts) |
| 3528 | { |
| 3529 | if (stmts && stmts->size) { |
| 3530 | stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 3531 | /* If first statement is a literal string, it's the doc string. */ |
| 3532 | if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) { |
| 3533 | string doc = s->v.Expr.value->v.Str.s; |
| 3534 | /* not very efficient, but simple */ |
| 3535 | memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1), |
| 3536 | (stmts->size - 1) * sizeof(void*)); |
| 3537 | stmts->size--; |
| 3538 | return doc; |
| 3539 | } |
| 3540 | } |
| 3541 | return NULL; |
| 3542 | } |
| 3543 | |
| 3544 | static asdl_seq * |
| 3545 | ast_for_body(struct compiling *c, const node *n, string *docstring) |
| 3546 | { |
| 3547 | asdl_seq *stmts = ast_for_suite(c, n); |
| 3548 | *docstring = docstring_from_stmts(stmts); |
| 3549 | return stmts; |
| 3550 | } |
| 3551 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3552 | static stmt_ty |
| 3553 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3554 | { |
| 3555 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3556 | ['else' ':' suite] |
| 3557 | */ |
| 3558 | char *s; |
| 3559 | |
| 3560 | REQ(n, if_stmt); |
| 3561 | |
| 3562 | if (NCH(n) == 4) { |
| 3563 | expr_ty expression; |
| 3564 | asdl_seq *suite_seq; |
| 3565 | |
| 3566 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3567 | if (!expression) |
| 3568 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3570 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3571 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3573 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3574 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3575 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3576 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3577 | s = STR(CHILD(n, 4)); |
| 3578 | /* s[2], the third character in the string, will be |
| 3579 | 's' for el_s_e, or |
| 3580 | 'i' for el_i_f |
| 3581 | */ |
| 3582 | if (s[2] == 's') { |
| 3583 | expr_ty expression; |
| 3584 | asdl_seq *seq1, *seq2; |
| 3585 | |
| 3586 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3587 | if (!expression) |
| 3588 | return NULL; |
| 3589 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3590 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3591 | return NULL; |
| 3592 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3593 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3594 | return NULL; |
| 3595 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3596 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3597 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | } |
| 3599 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3600 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3601 | expr_ty expression; |
| 3602 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3603 | asdl_seq *orelse = NULL; |
| 3604 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3605 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3606 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3607 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3608 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3609 | has_else = 1; |
| 3610 | n_elif -= 3; |
| 3611 | } |
| 3612 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3613 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3614 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3615 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3616 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3617 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3618 | if (!orelse) |
| 3619 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3620 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3621 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3622 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3623 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3624 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3625 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3626 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3627 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3628 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | asdl_seq_SET(orelse, 0, |
| 3631 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3632 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3633 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3634 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3635 | /* the just-created orelse handled the last elif */ |
| 3636 | n_elif--; |
| 3637 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3638 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3639 | for (i = 0; i < n_elif; i++) { |
| 3640 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3641 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3642 | if (!newobj) |
| 3643 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3645 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3646 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3647 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3648 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3649 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3650 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3651 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3652 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3653 | LINENO(CHILD(n, off)), |
| 3654 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3655 | orelse = newobj; |
| 3656 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3657 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3658 | if (!expression) |
| 3659 | return NULL; |
| 3660 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3661 | if (!suite_seq) |
| 3662 | return NULL; |
| 3663 | return If(expression, suite_seq, orelse, |
| 3664 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3665 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3666 | |
| 3667 | PyErr_Format(PyExc_SystemError, |
| 3668 | "unexpected token in 'if' statement: %s", s); |
| 3669 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | static stmt_ty |
| 3673 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3674 | { |
| 3675 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3676 | REQ(n, while_stmt); |
| 3677 | |
| 3678 | if (NCH(n) == 4) { |
| 3679 | expr_ty expression; |
| 3680 | asdl_seq *suite_seq; |
| 3681 | |
| 3682 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3683 | if (!expression) |
| 3684 | return NULL; |
| 3685 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3686 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3688 | 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] | 3689 | } |
| 3690 | else if (NCH(n) == 7) { |
| 3691 | expr_ty expression; |
| 3692 | asdl_seq *seq1, *seq2; |
| 3693 | |
| 3694 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3695 | if (!expression) |
| 3696 | return NULL; |
| 3697 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3698 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3699 | return NULL; |
| 3700 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3701 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3702 | return NULL; |
| 3703 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3704 | 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] | 3705 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3706 | |
| 3707 | PyErr_Format(PyExc_SystemError, |
| 3708 | "wrong number of tokens for 'while' statement: %d", |
| 3709 | NCH(n)); |
| 3710 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3714 | ast_for_for_stmt(struct compiling *c, const node *n, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3715 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3716 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3717 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3718 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3719 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3720 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3721 | REQ(n, for_stmt); |
| 3722 | |
| 3723 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3724 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | if (!seq) |
| 3726 | return NULL; |
| 3727 | } |
| 3728 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3729 | node_target = CHILD(n, 1); |
| 3730 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3731 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3732 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3733 | /* Check the # of children rather than the length of _target, since |
| 3734 | 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] | 3735 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3736 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3737 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3738 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3739 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3740 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3741 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3742 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3743 | return NULL; |
| 3744 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3745 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3746 | return NULL; |
| 3747 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3748 | if (is_async) |
| 3749 | return AsyncFor(target, expression, suite_seq, seq, |
| 3750 | LINENO(n), n->n_col_offset, |
| 3751 | c->c_arena); |
| 3752 | else |
| 3753 | return For(target, expression, suite_seq, seq, |
| 3754 | LINENO(n), n->n_col_offset, |
| 3755 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | static excepthandler_ty |
| 3759 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3760 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3761 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3762 | REQ(exc, except_clause); |
| 3763 | REQ(body, suite); |
| 3764 | |
| 3765 | if (NCH(exc) == 1) { |
| 3766 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3767 | if (!suite_seq) |
| 3768 | return NULL; |
| 3769 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3770 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3771 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3772 | } |
| 3773 | else if (NCH(exc) == 2) { |
| 3774 | expr_ty expression; |
| 3775 | asdl_seq *suite_seq; |
| 3776 | |
| 3777 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3778 | if (!expression) |
| 3779 | return NULL; |
| 3780 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3781 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3782 | return NULL; |
| 3783 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3784 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3785 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3786 | } |
| 3787 | else if (NCH(exc) == 4) { |
| 3788 | asdl_seq *suite_seq; |
| 3789 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3790 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3791 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3792 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3793 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3794 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3795 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3796 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3797 | return NULL; |
| 3798 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3799 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3800 | return NULL; |
| 3801 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3802 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3803 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3804 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3805 | |
| 3806 | PyErr_Format(PyExc_SystemError, |
| 3807 | "wrong number of children for 'except' clause: %d", |
| 3808 | NCH(exc)); |
| 3809 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | static stmt_ty |
| 3813 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3814 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3815 | const int nch = NCH(n); |
| 3816 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3817 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3818 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3819 | REQ(n, try_stmt); |
| 3820 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3821 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3822 | if (body == NULL) |
| 3823 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3824 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3825 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3826 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3827 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3828 | /* we can assume it's an "else", |
| 3829 | because nch >= 9 for try-else-finally and |
| 3830 | it would otherwise have a type of except_clause */ |
| 3831 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3832 | if (orelse == NULL) |
| 3833 | return NULL; |
| 3834 | n_except--; |
| 3835 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3836 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3837 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3838 | if (finally == NULL) |
| 3839 | return NULL; |
| 3840 | n_except--; |
| 3841 | } |
| 3842 | else { |
| 3843 | /* we can assume it's an "else", |
| 3844 | otherwise it would have a type of except_clause */ |
| 3845 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3846 | if (orelse == NULL) |
| 3847 | return NULL; |
| 3848 | n_except--; |
| 3849 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3850 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3851 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3852 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3853 | return NULL; |
| 3854 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3855 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3856 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3857 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3858 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3859 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3860 | if (handlers == NULL) |
| 3861 | return NULL; |
| 3862 | |
| 3863 | for (i = 0; i < n_except; i++) { |
| 3864 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3865 | CHILD(n, 5 + i * 3)); |
| 3866 | if (!e) |
| 3867 | return NULL; |
| 3868 | asdl_seq_SET(handlers, i, e); |
| 3869 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3872 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3873 | 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] | 3874 | } |
| 3875 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3876 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3877 | static withitem_ty |
| 3878 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3879 | { |
| 3880 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3881 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3882 | REQ(n, with_item); |
| 3883 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3884 | if (!context_expr) |
| 3885 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3886 | if (NCH(n) == 3) { |
| 3887 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3888 | |
| 3889 | if (!optional_vars) { |
| 3890 | return NULL; |
| 3891 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3892 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3893 | return NULL; |
| 3894 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3897 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3900 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3901 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3902 | ast_for_with_stmt(struct compiling *c, const node *n, int is_async) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3903 | { |
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) |
| 3925 | return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
| 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; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3936 | string docstring; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3937 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3938 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3939 | REQ(n, classdef); |
| 3940 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3941 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3942 | s = ast_for_body(c, CHILD(n, 3), &docstring); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3943 | if (!s) |
| 3944 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3945 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3946 | if (!classname) |
| 3947 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3948 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3949 | return NULL; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3950 | return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring, |
| 3951 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3952 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3953 | |
| 3954 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3955 | s = ast_for_body(c, CHILD(n, 5), &docstring); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3956 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3957 | return NULL; |
| 3958 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3959 | if (!classname) |
| 3960 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3961 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3962 | return NULL; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3963 | return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring, |
| 3964 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3967 | /* class NAME '(' arglist ')' ':' suite */ |
| 3968 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3969 | { |
| 3970 | PyObject *dummy_name; |
| 3971 | expr_ty dummy; |
| 3972 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3973 | if (!dummy_name) |
| 3974 | return NULL; |
| 3975 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 3976 | call = ast_for_call(c, CHILD(n, 3), dummy); |
| 3977 | if (!call) |
| 3978 | return NULL; |
| 3979 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3980 | s = ast_for_body(c, CHILD(n, 6), &docstring); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3981 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3982 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3983 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3984 | if (!classname) |
| 3985 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3986 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3987 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3988 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3989 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3990 | decorator_seq, docstring, LINENO(n), n->n_col_offset, |
| 3991 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | static stmt_ty |
| 3995 | ast_for_stmt(struct compiling *c, const node *n) |
| 3996 | { |
| 3997 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3998 | assert(NCH(n) == 1); |
| 3999 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4000 | } |
| 4001 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4002 | assert(num_stmts(n) == 1); |
| 4003 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4004 | } |
| 4005 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4006 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4007 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4008 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4009 | */ |
| 4010 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4011 | case expr_stmt: |
| 4012 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4013 | case del_stmt: |
| 4014 | return ast_for_del_stmt(c, n); |
| 4015 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 4016 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4017 | case flow_stmt: |
| 4018 | return ast_for_flow_stmt(c, n); |
| 4019 | case import_stmt: |
| 4020 | return ast_for_import_stmt(c, n); |
| 4021 | case global_stmt: |
| 4022 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4023 | case nonlocal_stmt: |
| 4024 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4025 | case assert_stmt: |
| 4026 | return ast_for_assert_stmt(c, n); |
| 4027 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4028 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4029 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4030 | TYPE(n), NCH(n)); |
| 4031 | return NULL; |
| 4032 | } |
| 4033 | } |
| 4034 | else { |
| 4035 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4036 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4037 | */ |
| 4038 | node *ch = CHILD(n, 0); |
| 4039 | REQ(n, compound_stmt); |
| 4040 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4041 | case if_stmt: |
| 4042 | return ast_for_if_stmt(c, ch); |
| 4043 | case while_stmt: |
| 4044 | return ast_for_while_stmt(c, ch); |
| 4045 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4046 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4047 | case try_stmt: |
| 4048 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4049 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4050 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4051 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4052 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4053 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4054 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4055 | case decorated: |
| 4056 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4057 | case async_stmt: |
| 4058 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4059 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4060 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4061 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4062 | TYPE(n), NCH(n)); |
| 4063 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4064 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4065 | } |
| 4066 | } |
| 4067 | |
| 4068 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4069 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4070 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4071 | const char *end; |
| 4072 | long x; |
| 4073 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4074 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4075 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4076 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4077 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4078 | errno = 0; |
| 4079 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4080 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4081 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4082 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4083 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4084 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4085 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4086 | } |
| 4087 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4088 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4089 | if (*end == '\0') { |
| 4090 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4091 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4092 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4093 | } |
| 4094 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4095 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4096 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4097 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4098 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4099 | return NULL; |
| 4100 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4101 | } |
| 4102 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4103 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4104 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4105 | if (dx == -1.0 && PyErr_Occurred()) |
| 4106 | return NULL; |
| 4107 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4108 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4112 | parsenumber(struct compiling *c, const char *s) |
| 4113 | { |
| 4114 | char *dup, *end; |
| 4115 | PyObject *res = NULL; |
| 4116 | |
| 4117 | assert(s != NULL); |
| 4118 | |
| 4119 | if (strchr(s, '_') == NULL) { |
| 4120 | return parsenumber_raw(c, s); |
| 4121 | } |
| 4122 | /* Create a duplicate without underscores. */ |
| 4123 | dup = PyMem_Malloc(strlen(s) + 1); |
| 4124 | end = dup; |
| 4125 | for (; *s; s++) { |
| 4126 | if (*s != '_') { |
| 4127 | *end++ = *s; |
| 4128 | } |
| 4129 | } |
| 4130 | *end = '\0'; |
| 4131 | res = parsenumber_raw(c, dup); |
| 4132 | PyMem_Free(dup); |
| 4133 | return res; |
| 4134 | } |
| 4135 | |
| 4136 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4137 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4138 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4139 | const char *s, *t; |
| 4140 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4141 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4142 | while (s < end && (*s & 0x80)) s++; |
| 4143 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4144 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4147 | static int |
| 4148 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
| 4149 | char first_invalid_escape_char) |
| 4150 | { |
| 4151 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4152 | first_invalid_escape_char); |
| 4153 | if (msg == NULL) { |
| 4154 | return -1; |
| 4155 | } |
| 4156 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
| 4157 | c->c_filename, LINENO(n), |
| 4158 | NULL, NULL) < 0 && |
| 4159 | PyErr_ExceptionMatches(PyExc_DeprecationWarning)) |
| 4160 | { |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4161 | const char *s; |
| 4162 | |
| 4163 | /* Replace the DeprecationWarning exception with a SyntaxError |
| 4164 | to get a more accurate error report */ |
| 4165 | PyErr_Clear(); |
| 4166 | |
| 4167 | s = PyUnicode_AsUTF8(msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4168 | if (s != NULL) { |
| 4169 | ast_error(c, n, s); |
| 4170 | } |
| 4171 | Py_DECREF(msg); |
| 4172 | return -1; |
| 4173 | } |
| 4174 | Py_DECREF(msg); |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4178 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4179 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4180 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4181 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4182 | PyObject *v, *u; |
| 4183 | char *buf; |
| 4184 | char *p; |
| 4185 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4186 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4187 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4188 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4189 | return NULL; |
| 4190 | /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4191 | "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4192 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4193 | if (u == NULL) |
| 4194 | return NULL; |
| 4195 | p = buf = PyBytes_AsString(u); |
| 4196 | end = s + len; |
| 4197 | while (s < end) { |
| 4198 | if (*s == '\\') { |
| 4199 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4200 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4201 | strcpy(p, "u005c"); |
| 4202 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4203 | if (s >= end) |
| 4204 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4205 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4206 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4207 | if (*s & 0x80) { /* XXX inefficient */ |
| 4208 | PyObject *w; |
| 4209 | int kind; |
| 4210 | void *data; |
| 4211 | Py_ssize_t len, i; |
| 4212 | w = decode_utf8(c, &s, end); |
| 4213 | if (w == NULL) { |
| 4214 | Py_DECREF(u); |
| 4215 | return NULL; |
| 4216 | } |
| 4217 | kind = PyUnicode_KIND(w); |
| 4218 | data = PyUnicode_DATA(w); |
| 4219 | len = PyUnicode_GET_LENGTH(w); |
| 4220 | for (i = 0; i < len; i++) { |
| 4221 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4222 | sprintf(p, "\\U%08x", chr); |
| 4223 | p += 10; |
| 4224 | } |
| 4225 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4226 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4227 | Py_DECREF(w); |
| 4228 | } else { |
| 4229 | *p++ = *s++; |
| 4230 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4231 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4232 | len = p - buf; |
| 4233 | s = buf; |
| 4234 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4235 | const char *first_invalid_escape; |
| 4236 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4237 | |
| 4238 | if (v != NULL && first_invalid_escape != NULL) { |
| 4239 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4240 | /* We have not decref u before because first_invalid_escape points |
| 4241 | inside u. */ |
| 4242 | Py_XDECREF(u); |
| 4243 | Py_DECREF(v); |
| 4244 | return NULL; |
| 4245 | } |
| 4246 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4247 | Py_XDECREF(u); |
| 4248 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4251 | static PyObject * |
| 4252 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4253 | size_t len) |
| 4254 | { |
| 4255 | const char *first_invalid_escape; |
| 4256 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4257 | &first_invalid_escape); |
| 4258 | if (result == NULL) |
| 4259 | return NULL; |
| 4260 | |
| 4261 | if (first_invalid_escape != NULL) { |
| 4262 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4263 | Py_DECREF(result); |
| 4264 | return NULL; |
| 4265 | } |
| 4266 | } |
| 4267 | return result; |
| 4268 | } |
| 4269 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4270 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4271 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4272 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4273 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4274 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4275 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4276 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4277 | int all_whitespace = 1; |
| 4278 | int kind; |
| 4279 | void *data; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4280 | PyCompilerFlags cf; |
| 4281 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4282 | char *str; |
| 4283 | PyObject *o; |
| 4284 | Py_ssize_t len; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4285 | Py_ssize_t i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4286 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4287 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4288 | assert(*(expr_start-1) == '{'); |
| 4289 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4290 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4291 | /* We know there are no escapes here, because backslashes are not allowed, |
| 4292 | and we know it's utf-8 encoded (per PEP 263). But, in order to check |
| 4293 | that each char is not whitespace, we need to decode it to unicode. |
| 4294 | Which is unfortunate, but such is life. */ |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4295 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4296 | /* If the substring is all whitespace, it's an error. We need to catch |
| 4297 | this here, and not when we call PyParser_ASTFromString, because turning |
| 4298 | the expression '' in to '()' would go from being invalid to valid. */ |
| 4299 | /* Note that this code says an empty string is all whitespace. That's |
| 4300 | important. There's a test for it: f'{}'. */ |
| 4301 | o = PyUnicode_DecodeUTF8(expr_start, expr_end-expr_start, NULL); |
| 4302 | if (o == NULL) |
| 4303 | return NULL; |
| 4304 | len = PyUnicode_GET_LENGTH(o); |
| 4305 | kind = PyUnicode_KIND(o); |
| 4306 | data = PyUnicode_DATA(o); |
| 4307 | for (i = 0; i < len; i++) { |
| 4308 | if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4309 | all_whitespace = 0; |
| 4310 | break; |
| 4311 | } |
| 4312 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4313 | Py_DECREF(o); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4314 | if (all_whitespace) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4315 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4316 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4317 | } |
| 4318 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4319 | /* Reuse len to be the length of the utf-8 input string. */ |
| 4320 | len = expr_end - expr_start; |
| 4321 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4322 | str = PyMem_RawMalloc(len + 3); |
| 4323 | if (str == NULL) |
| 4324 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4325 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4326 | str[0] = '('; |
| 4327 | memcpy(str+1, expr_start, len); |
| 4328 | str[len+1] = ')'; |
| 4329 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4330 | |
| 4331 | cf.cf_flags = PyCF_ONLY_AST; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4332 | mod = PyParser_ASTFromString(str, "<fstring>", |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4333 | Py_eval_input, &cf, c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4334 | PyMem_RawFree(str); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4335 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4336 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4337 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | /* Return -1 on error. |
| 4341 | |
| 4342 | Return 0 if we reached the end of the literal. |
| 4343 | |
| 4344 | Return 1 if we haven't reached the end of the literal, but we want |
| 4345 | the caller to process the literal up to this point. Used for |
| 4346 | doubled braces. |
| 4347 | */ |
| 4348 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4349 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4350 | PyObject **literal, int recurse_lvl, |
| 4351 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4352 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4353 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4354 | brace (which isn't part of a unicode name escape such as |
| 4355 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4356 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4357 | const char *s = *str; |
| 4358 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4359 | int result = 0; |
| 4360 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4361 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4362 | while (s < end) { |
| 4363 | char ch = *s++; |
| 4364 | if (!raw && ch == '\\' && s < end) { |
| 4365 | ch = *s++; |
| 4366 | if (ch == 'N') { |
| 4367 | if (s < end && *s++ == '{') { |
| 4368 | while (s < end && *s++ != '}') { |
| 4369 | } |
| 4370 | continue; |
| 4371 | } |
| 4372 | break; |
| 4373 | } |
| 4374 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4375 | return -1; |
| 4376 | } |
| 4377 | } |
| 4378 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4379 | /* Check for doubled braces, but only at the top level. If |
| 4380 | we checked at every level, then f'{0:{3}}' would fail |
| 4381 | with the two closing braces. */ |
| 4382 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4383 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4384 | /* We're going to tell the caller that the literal ends |
| 4385 | here, but that they should continue scanning. But also |
| 4386 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4387 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4388 | result = 1; |
| 4389 | goto done; |
| 4390 | } |
| 4391 | |
| 4392 | /* Where a single '{' is the start of a new expression, a |
| 4393 | single '}' is not allowed. */ |
| 4394 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4395 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4396 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4397 | return -1; |
| 4398 | } |
| 4399 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4400 | /* We're either at a '{', which means we're starting another |
| 4401 | expression; or a '}', which means we're at the end of this |
| 4402 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4403 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4407 | *str = s; |
| 4408 | assert(s <= end); |
| 4409 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4410 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4411 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4412 | if (raw) |
| 4413 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4414 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4415 | NULL, NULL); |
| 4416 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4417 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame^] | 4418 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4419 | if (!*literal) |
| 4420 | return -1; |
| 4421 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4422 | return result; |
| 4423 | } |
| 4424 | |
| 4425 | /* Forward declaration because parsing is recursive. */ |
| 4426 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4427 | 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] | 4428 | struct compiling *c, const node *n); |
| 4429 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4430 | /* 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] | 4431 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4432 | which includes the expression, conversion character, and |
| 4433 | format_spec expression. |
| 4434 | |
| 4435 | Note that I don't do a perfect job here: I don't make sure that a |
| 4436 | closing brace doesn't match an opening paren, for example. It |
| 4437 | doesn't need to error on all invalid expressions, just correctly |
| 4438 | find the end of all valid ones. Any errors inside the expression |
| 4439 | will be caught when we parse it later. */ |
| 4440 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4441 | 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] | 4442 | expr_ty *expression, struct compiling *c, const node *n) |
| 4443 | { |
| 4444 | /* Return -1 on error, else 0. */ |
| 4445 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4446 | const char *expr_start; |
| 4447 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4448 | expr_ty simple_expression; |
| 4449 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4450 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4451 | |
| 4452 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4453 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4454 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4455 | |
| 4456 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4457 | int string_type = 0; |
| 4458 | |
| 4459 | /* Keep track of nesting level for braces/parens/brackets in |
| 4460 | expressions. */ |
| 4461 | Py_ssize_t nested_depth = 0; |
| 4462 | |
| 4463 | /* Can only nest one level deep. */ |
| 4464 | if (recurse_lvl >= 2) { |
| 4465 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4466 | return -1; |
| 4467 | } |
| 4468 | |
| 4469 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4470 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4471 | assert(**str == '{'); |
| 4472 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4473 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4474 | expr_start = *str; |
| 4475 | for (; *str < end; (*str)++) { |
| 4476 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4477 | |
| 4478 | /* Loop invariants. */ |
| 4479 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4480 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4481 | if (quote_char) |
| 4482 | assert(string_type == 1 || string_type == 3); |
| 4483 | else |
| 4484 | assert(string_type == 0); |
| 4485 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4486 | ch = **str; |
| 4487 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4488 | if (ch == '\\') { |
| 4489 | /* Error: can't include a backslash character, inside |
| 4490 | parens or strings or not. */ |
| 4491 | ast_error(c, n, "f-string expression part " |
| 4492 | "cannot include a backslash"); |
| 4493 | return -1; |
| 4494 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4495 | if (quote_char) { |
| 4496 | /* We're inside a string. See if we're at the end. */ |
| 4497 | /* This code needs to implement the same non-error logic |
| 4498 | as tok_get from tokenizer.c, at the letter_quote |
| 4499 | label. To actually share that code would be a |
| 4500 | nightmare. But, it's unlikely to change and is small, |
| 4501 | so duplicate it here. Note we don't need to catch all |
| 4502 | of the errors, since they'll be caught when parsing the |
| 4503 | expression. We just need to match the non-error |
| 4504 | cases. Thus we can ignore \n in single-quoted strings, |
| 4505 | for example. Or non-terminated strings. */ |
| 4506 | if (ch == quote_char) { |
| 4507 | /* Does this match the string_type (single or triple |
| 4508 | quoted)? */ |
| 4509 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4510 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4511 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4512 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4513 | string_type = 0; |
| 4514 | quote_char = 0; |
| 4515 | continue; |
| 4516 | } |
| 4517 | } else { |
| 4518 | /* We're at the end of a normal string. */ |
| 4519 | quote_char = 0; |
| 4520 | string_type = 0; |
| 4521 | continue; |
| 4522 | } |
| 4523 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4524 | } else if (ch == '\'' || ch == '"') { |
| 4525 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4526 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4527 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4528 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4529 | } else { |
| 4530 | /* Start of a normal string. */ |
| 4531 | string_type = 1; |
| 4532 | } |
| 4533 | /* Start looking for the end of the string. */ |
| 4534 | quote_char = ch; |
| 4535 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4536 | nested_depth++; |
| 4537 | } else if (nested_depth != 0 && |
| 4538 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4539 | nested_depth--; |
| 4540 | } else if (ch == '#') { |
| 4541 | /* Error: can't include a comment character, inside parens |
| 4542 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4543 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4544 | return -1; |
| 4545 | } else if (nested_depth == 0 && |
| 4546 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4547 | /* First, test for the special case of "!=". Since '=' is |
| 4548 | not an allowed conversion character, nothing is lost in |
| 4549 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4550 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4551 | /* This isn't a conversion character, just continue. */ |
| 4552 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4553 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4554 | /* Normal way out of this loop. */ |
| 4555 | break; |
| 4556 | } else { |
| 4557 | /* Just consume this char and loop around. */ |
| 4558 | } |
| 4559 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4560 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4561 | /* If we leave this loop in a string or with mismatched parens, we |
| 4562 | don't care. We'll get a syntax error when compiling the |
| 4563 | expression. But, we can produce a better error message, so |
| 4564 | let's just do that.*/ |
| 4565 | if (quote_char) { |
| 4566 | ast_error(c, n, "f-string: unterminated string"); |
| 4567 | return -1; |
| 4568 | } |
| 4569 | if (nested_depth) { |
| 4570 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4571 | return -1; |
| 4572 | } |
| 4573 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4574 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4575 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4576 | |
| 4577 | /* Compile the expression as soon as possible, so we show errors |
| 4578 | related to the expression before errors related to the |
| 4579 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4580 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4581 | if (!simple_expression) |
| 4582 | return -1; |
| 4583 | |
| 4584 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4585 | if (**str == '!') { |
| 4586 | *str += 1; |
| 4587 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4588 | goto unexpected_end_of_string; |
| 4589 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4590 | conversion = **str; |
| 4591 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4592 | |
| 4593 | /* Validate the conversion. */ |
| 4594 | if (!(conversion == 's' || conversion == 'r' |
| 4595 | || conversion == 'a')) { |
| 4596 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4597 | "expected 's', 'r', or 'a'"); |
| 4598 | return -1; |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4603 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4604 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4605 | if (**str == ':') { |
| 4606 | *str += 1; |
| 4607 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4608 | goto unexpected_end_of_string; |
| 4609 | |
| 4610 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4611 | 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] | 4612 | if (!format_spec) |
| 4613 | return -1; |
| 4614 | } |
| 4615 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4616 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4617 | goto unexpected_end_of_string; |
| 4618 | |
| 4619 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4620 | assert(*str < end); |
| 4621 | assert(**str == '}'); |
| 4622 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4623 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4624 | /* And now create the FormattedValue node that represents this |
| 4625 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4626 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4627 | format_spec, LINENO(n), n->n_col_offset, |
| 4628 | c->c_arena); |
| 4629 | if (!*expression) |
| 4630 | return -1; |
| 4631 | |
| 4632 | return 0; |
| 4633 | |
| 4634 | unexpected_end_of_string: |
| 4635 | ast_error(c, n, "f-string: expecting '}'"); |
| 4636 | return -1; |
| 4637 | } |
| 4638 | |
| 4639 | /* Return -1 on error. |
| 4640 | |
| 4641 | Return 0 if we have a literal (possible zero length) and an |
| 4642 | expression (zero length if at the end of the string. |
| 4643 | |
| 4644 | Return 1 if we have a literal, but no expression, and we want the |
| 4645 | caller to call us again. This is used to deal with doubled |
| 4646 | braces. |
| 4647 | |
| 4648 | When called multiple times on the string 'a{{b{0}c', this function |
| 4649 | will return: |
| 4650 | |
| 4651 | 1. the literal 'a{' with no expression, and a return value |
| 4652 | of 1. Despite the fact that there's no expression, the return |
| 4653 | value of 1 means we're not finished yet. |
| 4654 | |
| 4655 | 2. the literal 'b' and the expression '0', with a return value of |
| 4656 | 0. The fact that there's an expression means we're not finished. |
| 4657 | |
| 4658 | 3. literal 'c' with no expression and a return value of 0. The |
| 4659 | combination of the return value of 0 with no expression means |
| 4660 | we're finished. |
| 4661 | */ |
| 4662 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4663 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4664 | int recurse_lvl, PyObject **literal, |
| 4665 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4666 | struct compiling *c, const node *n) |
| 4667 | { |
| 4668 | int result; |
| 4669 | |
| 4670 | assert(*literal == NULL && *expression == NULL); |
| 4671 | |
| 4672 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4673 | 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] | 4674 | if (result < 0) |
| 4675 | goto error; |
| 4676 | |
| 4677 | assert(result == 0 || result == 1); |
| 4678 | |
| 4679 | if (result == 1) |
| 4680 | /* We have a literal, but don't look at the expression. */ |
| 4681 | return 1; |
| 4682 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4683 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4684 | /* We're at the end of the string or the end of a nested |
| 4685 | f-string: no expression. The top-level error case where we |
| 4686 | expect to be at the end of the string but we're at a '}' is |
| 4687 | handled later. */ |
| 4688 | return 0; |
| 4689 | |
| 4690 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4691 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4692 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4693 | 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] | 4694 | goto error; |
| 4695 | |
| 4696 | return 0; |
| 4697 | |
| 4698 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4699 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4700 | return -1; |
| 4701 | } |
| 4702 | |
| 4703 | #define EXPRLIST_N_CACHED 64 |
| 4704 | |
| 4705 | typedef struct { |
| 4706 | /* Incrementally build an array of expr_ty, so be used in an |
| 4707 | asdl_seq. Cache some small but reasonably sized number of |
| 4708 | expr_ty's, and then after that start dynamically allocating, |
| 4709 | doubling the number allocated each time. Note that the f-string |
| 4710 | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
| 4711 | Str for the literal 'a'. So you add expr_ty's about twice as |
| 4712 | fast as you add exressions in an f-string. */ |
| 4713 | |
| 4714 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4715 | Py_ssize_t size; /* Number we've used. */ |
| 4716 | expr_ty *p; /* Pointer to the memory we're actually |
| 4717 | using. Will point to 'data' until we |
| 4718 | start dynamically allocating. */ |
| 4719 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4720 | } ExprList; |
| 4721 | |
| 4722 | #ifdef NDEBUG |
| 4723 | #define ExprList_check_invariants(l) |
| 4724 | #else |
| 4725 | static void |
| 4726 | ExprList_check_invariants(ExprList *l) |
| 4727 | { |
| 4728 | /* Check our invariants. Make sure this object is "live", and |
| 4729 | hasn't been deallocated. */ |
| 4730 | assert(l->size >= 0); |
| 4731 | assert(l->p != NULL); |
| 4732 | if (l->size <= EXPRLIST_N_CACHED) |
| 4733 | assert(l->data == l->p); |
| 4734 | } |
| 4735 | #endif |
| 4736 | |
| 4737 | static void |
| 4738 | ExprList_Init(ExprList *l) |
| 4739 | { |
| 4740 | l->allocated = EXPRLIST_N_CACHED; |
| 4741 | l->size = 0; |
| 4742 | |
| 4743 | /* Until we start allocating dynamically, p points to data. */ |
| 4744 | l->p = l->data; |
| 4745 | |
| 4746 | ExprList_check_invariants(l); |
| 4747 | } |
| 4748 | |
| 4749 | static int |
| 4750 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4751 | { |
| 4752 | ExprList_check_invariants(l); |
| 4753 | if (l->size >= l->allocated) { |
| 4754 | /* We need to alloc (or realloc) the memory. */ |
| 4755 | Py_ssize_t new_size = l->allocated * 2; |
| 4756 | |
| 4757 | /* See if we've ever allocated anything dynamically. */ |
| 4758 | if (l->p == l->data) { |
| 4759 | Py_ssize_t i; |
| 4760 | /* We're still using the cached data. Switch to |
| 4761 | alloc-ing. */ |
| 4762 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4763 | if (!l->p) |
| 4764 | return -1; |
| 4765 | /* Copy the cached data into the new buffer. */ |
| 4766 | for (i = 0; i < l->size; i++) |
| 4767 | l->p[i] = l->data[i]; |
| 4768 | } else { |
| 4769 | /* Just realloc. */ |
| 4770 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4771 | if (!tmp) { |
| 4772 | PyMem_RawFree(l->p); |
| 4773 | l->p = NULL; |
| 4774 | return -1; |
| 4775 | } |
| 4776 | l->p = tmp; |
| 4777 | } |
| 4778 | |
| 4779 | l->allocated = new_size; |
| 4780 | assert(l->allocated == 2 * l->size); |
| 4781 | } |
| 4782 | |
| 4783 | l->p[l->size++] = exp; |
| 4784 | |
| 4785 | ExprList_check_invariants(l); |
| 4786 | return 0; |
| 4787 | } |
| 4788 | |
| 4789 | static void |
| 4790 | ExprList_Dealloc(ExprList *l) |
| 4791 | { |
| 4792 | ExprList_check_invariants(l); |
| 4793 | |
| 4794 | /* If there's been an error, or we've never dynamically allocated, |
| 4795 | do nothing. */ |
| 4796 | if (!l->p || l->p == l->data) { |
| 4797 | /* Do nothing. */ |
| 4798 | } else { |
| 4799 | /* We have dynamically allocated. Free the memory. */ |
| 4800 | PyMem_RawFree(l->p); |
| 4801 | } |
| 4802 | l->p = NULL; |
| 4803 | l->size = -1; |
| 4804 | } |
| 4805 | |
| 4806 | static asdl_seq * |
| 4807 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4808 | { |
| 4809 | asdl_seq *seq; |
| 4810 | |
| 4811 | ExprList_check_invariants(l); |
| 4812 | |
| 4813 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4814 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4815 | if (seq) { |
| 4816 | Py_ssize_t i; |
| 4817 | for (i = 0; i < l->size; i++) |
| 4818 | asdl_seq_SET(seq, i, l->p[i]); |
| 4819 | } |
| 4820 | ExprList_Dealloc(l); |
| 4821 | return seq; |
| 4822 | } |
| 4823 | |
| 4824 | /* The FstringParser is designed to add a mix of strings and |
| 4825 | f-strings, and concat them together as needed. Ultimately, it |
| 4826 | generates an expr_ty. */ |
| 4827 | typedef struct { |
| 4828 | PyObject *last_str; |
| 4829 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4830 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4831 | } FstringParser; |
| 4832 | |
| 4833 | #ifdef NDEBUG |
| 4834 | #define FstringParser_check_invariants(state) |
| 4835 | #else |
| 4836 | static void |
| 4837 | FstringParser_check_invariants(FstringParser *state) |
| 4838 | { |
| 4839 | if (state->last_str) |
| 4840 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4841 | ExprList_check_invariants(&state->expr_list); |
| 4842 | } |
| 4843 | #endif |
| 4844 | |
| 4845 | static void |
| 4846 | FstringParser_Init(FstringParser *state) |
| 4847 | { |
| 4848 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4849 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4850 | ExprList_Init(&state->expr_list); |
| 4851 | FstringParser_check_invariants(state); |
| 4852 | } |
| 4853 | |
| 4854 | static void |
| 4855 | FstringParser_Dealloc(FstringParser *state) |
| 4856 | { |
| 4857 | FstringParser_check_invariants(state); |
| 4858 | |
| 4859 | Py_XDECREF(state->last_str); |
| 4860 | ExprList_Dealloc(&state->expr_list); |
| 4861 | } |
| 4862 | |
| 4863 | /* Make a Str node, but decref the PyUnicode object being added. */ |
| 4864 | static expr_ty |
| 4865 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4866 | { |
| 4867 | PyObject *s = *str; |
| 4868 | *str = NULL; |
| 4869 | assert(PyUnicode_CheckExact(s)); |
| 4870 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4871 | Py_DECREF(s); |
| 4872 | return NULL; |
| 4873 | } |
| 4874 | return Str(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 4875 | } |
| 4876 | |
| 4877 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4878 | decref'd. */ |
| 4879 | static int |
| 4880 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4881 | { |
| 4882 | FstringParser_check_invariants(state); |
| 4883 | |
| 4884 | assert(PyUnicode_CheckExact(str)); |
| 4885 | |
| 4886 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4887 | Py_DECREF(str); |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | if (!state->last_str) { |
| 4892 | /* We didn't have a string before, so just remember this one. */ |
| 4893 | state->last_str = str; |
| 4894 | } else { |
| 4895 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4896 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4897 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4898 | return -1; |
| 4899 | } |
| 4900 | FstringParser_check_invariants(state); |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4904 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4905 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4906 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4907 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4908 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4909 | struct compiling *c, const node *n) |
| 4910 | { |
| 4911 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4912 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4913 | |
| 4914 | /* Parse the f-string. */ |
| 4915 | while (1) { |
| 4916 | PyObject *literal = NULL; |
| 4917 | expr_ty expression = NULL; |
| 4918 | |
| 4919 | /* If there's a zero length literal in front of the |
| 4920 | expression, literal will be NULL. If we're at the end of |
| 4921 | the f-string, expression will be NULL (unless result == 1, |
| 4922 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4923 | 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] | 4924 | &literal, &expression, |
| 4925 | c, n); |
| 4926 | if (result < 0) |
| 4927 | return -1; |
| 4928 | |
| 4929 | /* Add the literal, if any. */ |
| 4930 | if (!literal) { |
| 4931 | /* Do nothing. Just leave last_str alone (and possibly |
| 4932 | NULL). */ |
| 4933 | } else if (!state->last_str) { |
| 4934 | state->last_str = literal; |
| 4935 | literal = NULL; |
| 4936 | } else { |
| 4937 | /* We have a literal, concatenate it. */ |
| 4938 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4939 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4940 | return -1; |
| 4941 | literal = NULL; |
| 4942 | } |
| 4943 | assert(!state->last_str || |
| 4944 | PyUnicode_GET_LENGTH(state->last_str) != 0); |
| 4945 | |
| 4946 | /* We've dealt with the literal now. It can't be leaked on further |
| 4947 | errors. */ |
| 4948 | assert(literal == NULL); |
| 4949 | |
| 4950 | /* See if we should just loop around to get the next literal |
| 4951 | and expression, while ignoring the expression this |
| 4952 | time. This is used for un-doubling braces, as an |
| 4953 | optimization. */ |
| 4954 | if (result == 1) |
| 4955 | continue; |
| 4956 | |
| 4957 | if (!expression) |
| 4958 | /* We're done with this f-string. */ |
| 4959 | break; |
| 4960 | |
| 4961 | /* We know we have an expression. Convert any existing string |
| 4962 | to a Str node. */ |
| 4963 | if (!state->last_str) { |
| 4964 | /* Do nothing. No previous literal. */ |
| 4965 | } else { |
| 4966 | /* Convert the existing last_str literal to a Str node. */ |
| 4967 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 4968 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 4969 | return -1; |
| 4970 | } |
| 4971 | |
| 4972 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 4973 | return -1; |
| 4974 | } |
| 4975 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4976 | /* If recurse_lvl is zero, then we must be at the end of the |
| 4977 | string. Otherwise, we must be at a right brace. */ |
| 4978 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4979 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4980 | ast_error(c, n, "f-string: unexpected end of string"); |
| 4981 | return -1; |
| 4982 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4983 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4984 | ast_error(c, n, "f-string: expecting '}'"); |
| 4985 | return -1; |
| 4986 | } |
| 4987 | |
| 4988 | FstringParser_check_invariants(state); |
| 4989 | return 0; |
| 4990 | } |
| 4991 | |
| 4992 | /* Convert the partial state reflected in last_str and expr_list to an |
| 4993 | expr_ty. The expr_ty can be a Str, or a JoinedStr. */ |
| 4994 | static expr_ty |
| 4995 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 4996 | const node *n) |
| 4997 | { |
| 4998 | asdl_seq *seq; |
| 4999 | |
| 5000 | FstringParser_check_invariants(state); |
| 5001 | |
| 5002 | /* If we're just a constant string with no expressions, return |
| 5003 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5004 | if (!state->fmode) { |
| 5005 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5006 | if (!state->last_str) { |
| 5007 | /* Create a zero length string. */ |
| 5008 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5009 | if (!state->last_str) |
| 5010 | goto error; |
| 5011 | } |
| 5012 | return make_str_node_and_del(&state->last_str, c, n); |
| 5013 | } |
| 5014 | |
| 5015 | /* Create a Str node out of last_str, if needed. It will be the |
| 5016 | last node in our expression list. */ |
| 5017 | if (state->last_str) { |
| 5018 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5019 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5020 | goto error; |
| 5021 | } |
| 5022 | /* This has already been freed. */ |
| 5023 | assert(state->last_str == NULL); |
| 5024 | |
| 5025 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5026 | if (!seq) |
| 5027 | goto error; |
| 5028 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5029 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5030 | |
| 5031 | error: |
| 5032 | FstringParser_Dealloc(state); |
| 5033 | return NULL; |
| 5034 | } |
| 5035 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5036 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5037 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5038 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5039 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5040 | 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] | 5041 | struct compiling *c, const node *n) |
| 5042 | { |
| 5043 | FstringParser state; |
| 5044 | |
| 5045 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5046 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5047 | c, n) < 0) { |
| 5048 | FstringParser_Dealloc(&state); |
| 5049 | return NULL; |
| 5050 | } |
| 5051 | |
| 5052 | return FstringParser_Finish(&state, c, n); |
| 5053 | } |
| 5054 | |
| 5055 | /* n is a Python string literal, including the bracketing quote |
| 5056 | 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] | 5057 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5058 | 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] | 5059 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5060 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5061 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5062 | static int |
| 5063 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5064 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5065 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5066 | size_t len; |
| 5067 | const char *s = STR(n); |
| 5068 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5069 | int fmode = 0; |
| 5070 | *bytesmode = 0; |
| 5071 | *rawmode = 0; |
| 5072 | *result = NULL; |
| 5073 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5074 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5075 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5076 | if (quote == 'b' || quote == 'B') { |
| 5077 | quote = *++s; |
| 5078 | *bytesmode = 1; |
| 5079 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5080 | else if (quote == 'u' || quote == 'U') { |
| 5081 | quote = *++s; |
| 5082 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5083 | else if (quote == 'r' || quote == 'R') { |
| 5084 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5085 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5086 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5087 | else if (quote == 'f' || quote == 'F') { |
| 5088 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5089 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5090 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5091 | else { |
| 5092 | break; |
| 5093 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5094 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5095 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5096 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5097 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5098 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5099 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5100 | if (quote != '\'' && quote != '\"') { |
| 5101 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5102 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5103 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5104 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5105 | s++; |
| 5106 | len = strlen(s); |
| 5107 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5109 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5110 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5111 | } |
| 5112 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5113 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5114 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5115 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5116 | } |
| 5117 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5118 | /* A triple quoted string. We've already skipped one quote at |
| 5119 | the start and one at the end of the string. Now skip the |
| 5120 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5121 | s += 2; |
| 5122 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5123 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5124 | if (s[--len] != quote || s[--len] != quote) { |
| 5125 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5126 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5127 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5128 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5129 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5130 | if (fmode) { |
| 5131 | /* Just return the bytes. The caller will parse the resulting |
| 5132 | string. */ |
| 5133 | *fstr = s; |
| 5134 | *fstrlen = len; |
| 5135 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5136 | } |
| 5137 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5138 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5139 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5140 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5141 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5142 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5143 | const char *ch; |
| 5144 | for (ch = s; *ch; ch++) { |
| 5145 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5146 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5147 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5148 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5149 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5150 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5151 | if (*rawmode) |
| 5152 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5153 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5154 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5155 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5156 | if (*rawmode) |
| 5157 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5158 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5159 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5160 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5161 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5164 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5165 | each STRING atom, and process it as needed. For bytes, just |
| 5166 | concatenate them together, and the result will be a Bytes node. For |
| 5167 | normal strings and f-strings, concatenate them together. The result |
| 5168 | will be a Str node if there were no f-strings; a FormattedValue |
| 5169 | node if there's just an f-string (with no leading or trailing |
| 5170 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5171 | any literals involved. */ |
| 5172 | static expr_ty |
| 5173 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5174 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5175 | int bytesmode = 0; |
| 5176 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5177 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5178 | |
| 5179 | FstringParser state; |
| 5180 | FstringParser_Init(&state); |
| 5181 | |
| 5182 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | int this_bytesmode; |
| 5184 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5185 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5186 | const char *fstr; |
| 5187 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5188 | |
| 5189 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5190 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5191 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5192 | goto error; |
| 5193 | |
| 5194 | /* Check that we're not mixing bytes with unicode. */ |
| 5195 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5196 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5197 | /* s is NULL if the current string part is an f-string. */ |
| 5198 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5199 | goto error; |
| 5200 | } |
| 5201 | bytesmode = this_bytesmode; |
| 5202 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5203 | if (fstr != NULL) { |
| 5204 | int result; |
| 5205 | assert(s == NULL && !bytesmode); |
| 5206 | /* This is an f-string. Parse and concatenate it. */ |
| 5207 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5208 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5209 | if (result < 0) |
| 5210 | goto error; |
| 5211 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5212 | /* A string or byte string. */ |
| 5213 | assert(s != NULL && fstr == NULL); |
| 5214 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5215 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5216 | PyUnicode_CheckExact(s)); |
| 5217 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5218 | if (bytesmode) { |
| 5219 | /* For bytes, concat as we go. */ |
| 5220 | if (i == 0) { |
| 5221 | /* First time, just remember this value. */ |
| 5222 | bytes_str = s; |
| 5223 | } else { |
| 5224 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5225 | if (!bytes_str) |
| 5226 | goto error; |
| 5227 | } |
| 5228 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | /* This is a regular string. Concatenate it. */ |
| 5230 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5231 | goto error; |
| 5232 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5233 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5234 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5235 | if (bytesmode) { |
| 5236 | /* Just return the bytes object and we're done. */ |
| 5237 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5238 | goto error; |
| 5239 | return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
| 5240 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5241 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5242 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5243 | assert(bytes_str == NULL); |
| 5244 | |
| 5245 | return FstringParser_Finish(&state, c, n); |
| 5246 | |
| 5247 | error: |
| 5248 | Py_XDECREF(bytes_str); |
| 5249 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5250 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5251 | } |