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 |
| 369 | validate_body(asdl_seq *body, const char *owner) |
| 370 | { |
| 371 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
| 372 | } |
| 373 | |
| 374 | static int |
| 375 | validate_stmt(stmt_ty stmt) |
| 376 | { |
| 377 | int i; |
| 378 | switch (stmt->kind) { |
| 379 | case FunctionDef_kind: |
| 380 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
| 381 | validate_arguments(stmt->v.FunctionDef.args) && |
| 382 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 383 | (!stmt->v.FunctionDef.returns || |
| 384 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 385 | case ClassDef_kind: |
| 386 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
| 387 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 388 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 389 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 390 | case Return_kind: |
| 391 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 392 | case Delete_kind: |
| 393 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 394 | case Assign_kind: |
| 395 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 396 | validate_expr(stmt->v.Assign.value, Load); |
| 397 | case AugAssign_kind: |
| 398 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 399 | validate_expr(stmt->v.AugAssign.value, Load); |
| 400 | case For_kind: |
| 401 | return validate_expr(stmt->v.For.target, Store) && |
| 402 | validate_expr(stmt->v.For.iter, Load) && |
| 403 | validate_body(stmt->v.For.body, "For") && |
| 404 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 405 | case AsyncFor_kind: |
| 406 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 407 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
| 408 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
| 409 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 410 | case While_kind: |
| 411 | return validate_expr(stmt->v.While.test, Load) && |
| 412 | validate_body(stmt->v.While.body, "While") && |
| 413 | validate_stmts(stmt->v.While.orelse); |
| 414 | case If_kind: |
| 415 | return validate_expr(stmt->v.If.test, Load) && |
| 416 | validate_body(stmt->v.If.body, "If") && |
| 417 | validate_stmts(stmt->v.If.orelse); |
| 418 | case With_kind: |
| 419 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 420 | return 0; |
| 421 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 422 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 423 | if (!validate_expr(item->context_expr, Load) || |
| 424 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 425 | return 0; |
| 426 | } |
| 427 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 428 | case AsyncWith_kind: |
| 429 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 430 | return 0; |
| 431 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 432 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 433 | if (!validate_expr(item->context_expr, Load) || |
| 434 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 435 | return 0; |
| 436 | } |
| 437 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 438 | case Raise_kind: |
| 439 | if (stmt->v.Raise.exc) { |
| 440 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 441 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 442 | } |
| 443 | if (stmt->v.Raise.cause) { |
| 444 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 445 | return 0; |
| 446 | } |
| 447 | return 1; |
| 448 | case Try_kind: |
| 449 | if (!validate_body(stmt->v.Try.body, "Try")) |
| 450 | return 0; |
| 451 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 452 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 453 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 454 | return 0; |
| 455 | } |
| 456 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 457 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 458 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 459 | return 0; |
| 460 | } |
| 461 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 462 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 463 | if ((handler->v.ExceptHandler.type && |
| 464 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
| 465 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
| 466 | return 0; |
| 467 | } |
| 468 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 469 | validate_stmts(stmt->v.Try.finalbody)) && |
| 470 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 471 | validate_stmts(stmt->v.Try.orelse)); |
| 472 | case Assert_kind: |
| 473 | return validate_expr(stmt->v.Assert.test, Load) && |
| 474 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 475 | case Import_kind: |
| 476 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 477 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 478 | if (stmt->v.ImportFrom.level < 0) { |
| 479 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 483 | case Global_kind: |
| 484 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 485 | case Nonlocal_kind: |
| 486 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 487 | case Expr_kind: |
| 488 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 489 | case AsyncFunctionDef_kind: |
| 490 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
| 491 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 492 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 493 | (!stmt->v.AsyncFunctionDef.returns || |
| 494 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 495 | case Pass_kind: |
| 496 | case Break_kind: |
| 497 | case Continue_kind: |
| 498 | return 1; |
| 499 | default: |
| 500 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 501 | return 0; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | static int |
| 506 | validate_stmts(asdl_seq *seq) |
| 507 | { |
| 508 | int i; |
| 509 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 510 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 511 | if (stmt) { |
| 512 | if (!validate_stmt(stmt)) |
| 513 | return 0; |
| 514 | } |
| 515 | else { |
| 516 | PyErr_SetString(PyExc_ValueError, |
| 517 | "None disallowed in statement list"); |
| 518 | return 0; |
| 519 | } |
| 520 | } |
| 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | static int |
| 525 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 526 | { |
| 527 | int i; |
| 528 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 529 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 530 | if (expr) { |
| 531 | if (!validate_expr(expr, ctx)) |
| 532 | return 0; |
| 533 | } |
| 534 | else if (!null_ok) { |
| 535 | PyErr_SetString(PyExc_ValueError, |
| 536 | "None disallowed in expression list"); |
| 537 | return 0; |
| 538 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 539 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 540 | } |
| 541 | return 1; |
| 542 | } |
| 543 | |
| 544 | int |
| 545 | PyAST_Validate(mod_ty mod) |
| 546 | { |
| 547 | int res = 0; |
| 548 | |
| 549 | switch (mod->kind) { |
| 550 | case Module_kind: |
| 551 | res = validate_stmts(mod->v.Module.body); |
| 552 | break; |
| 553 | case Interactive_kind: |
| 554 | res = validate_stmts(mod->v.Interactive.body); |
| 555 | break; |
| 556 | case Expression_kind: |
| 557 | res = validate_expr(mod->v.Expression.body, Load); |
| 558 | break; |
| 559 | case Suite_kind: |
| 560 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 561 | break; |
| 562 | default: |
| 563 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 564 | res = 0; |
| 565 | break; |
| 566 | } |
| 567 | return res; |
| 568 | } |
| 569 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 570 | /* 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] | 571 | #include "grammar.h" |
| 572 | #include "parsetok.h" |
| 573 | #include "graminit.h" |
| 574 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 575 | /* Data structure used internally */ |
| 576 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 577 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 578 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 579 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
| 580 | PyObject *c_normalize_args; /* Normalization argument tuple. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 581 | }; |
| 582 | |
| 583 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 584 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 585 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
| 586 | static asdl_seq *ast_for_suite(struct compiling *, const node *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 587 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 588 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 589 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 590 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 591 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 592 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); |
| 593 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); |
| 594 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 595 | /* Note different signature for ast_for_call */ |
| 596 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); |
| 597 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 598 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 599 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 600 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 601 | #define COMP_GENEXP 0 |
| 602 | #define COMP_LISTCOMP 1 |
| 603 | #define COMP_SETCOMP 2 |
| 604 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 605 | static int |
| 606 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 607 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 608 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 609 | if (!m) |
| 610 | return 0; |
| 611 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 612 | Py_DECREF(m); |
| 613 | if (!c->c_normalize) |
| 614 | return 0; |
| 615 | c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 616 | if (!c->c_normalize_args) { |
| 617 | Py_CLEAR(c->c_normalize); |
| 618 | return 0; |
| 619 | } |
Christian Heimes | 72f562f | 2013-07-24 21:02:17 +0200 | [diff] [blame] | 620 | PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | |
| 624 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 625 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 626 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 627 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 628 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 629 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 630 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 631 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 632 | /* Check whether there are non-ASCII characters in the |
| 633 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 634 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 635 | PyObject *id2; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 636 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 637 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 638 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 639 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 640 | PyTuple_SET_ITEM(c->c_normalize_args, 1, id); |
| 641 | id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 642 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 643 | if (!id2) |
| 644 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 646 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 647 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 648 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 649 | Py_DECREF(id); |
| 650 | return NULL; |
| 651 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 652 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 655 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 657 | static int |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 658 | ast_error(struct compiling *c, const node *n, const char *errmsg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 660 | PyObject *value, *errstr, *loc, *tmp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 661 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 662 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 663 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 664 | Py_INCREF(Py_None); |
| 665 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 666 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 667 | 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] | 668 | if (!tmp) |
| 669 | return 0; |
| 670 | errstr = PyUnicode_FromString(errmsg); |
| 671 | if (!errstr) { |
| 672 | Py_DECREF(tmp); |
| 673 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 674 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 675 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 676 | Py_DECREF(errstr); |
| 677 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 678 | if (value) { |
| 679 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 680 | Py_DECREF(value); |
| 681 | } |
| 682 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* num_stmts() returns number of contained statements. |
| 686 | |
| 687 | Use this routine to determine how big a sequence is needed for |
| 688 | the statements in a parse tree. Its raison d'etre is this bit of |
| 689 | grammar: |
| 690 | |
| 691 | stmt: simple_stmt | compound_stmt |
| 692 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 693 | |
| 694 | A simple_stmt can contain multiple small_stmt elements joined |
| 695 | by semicolons. If the arg is a simple_stmt, the number of |
| 696 | small_stmt elements is returned. |
| 697 | */ |
| 698 | |
| 699 | static int |
| 700 | num_stmts(const node *n) |
| 701 | { |
| 702 | int i, l; |
| 703 | node *ch; |
| 704 | |
| 705 | switch (TYPE(n)) { |
| 706 | case single_input: |
| 707 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 708 | return 0; |
| 709 | else |
| 710 | return num_stmts(CHILD(n, 0)); |
| 711 | case file_input: |
| 712 | l = 0; |
| 713 | for (i = 0; i < NCH(n); i++) { |
| 714 | ch = CHILD(n, i); |
| 715 | if (TYPE(ch) == stmt) |
| 716 | l += num_stmts(ch); |
| 717 | } |
| 718 | return l; |
| 719 | case stmt: |
| 720 | return num_stmts(CHILD(n, 0)); |
| 721 | case compound_stmt: |
| 722 | return 1; |
| 723 | case simple_stmt: |
| 724 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 725 | case suite: |
| 726 | if (NCH(n) == 1) |
| 727 | return num_stmts(CHILD(n, 0)); |
| 728 | else { |
| 729 | l = 0; |
| 730 | for (i = 2; i < (NCH(n) - 1); i++) |
| 731 | l += num_stmts(CHILD(n, i)); |
| 732 | return l; |
| 733 | } |
| 734 | default: { |
| 735 | char buf[128]; |
| 736 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 737 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 738 | TYPE(n), NCH(n)); |
| 739 | Py_FatalError(buf); |
| 740 | } |
| 741 | } |
| 742 | assert(0); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* Transform the CST rooted at node * to the appropriate AST |
| 747 | */ |
| 748 | |
| 749 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 750 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 751 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 752 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 753 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 754 | asdl_seq *stmts = NULL; |
| 755 | stmt_ty s; |
| 756 | node *ch; |
| 757 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 758 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 759 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 760 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 761 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 762 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 763 | c.c_normalize = NULL; |
| 764 | c.c_normalize_args = NULL; |
| 765 | |
| 766 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 769 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 770 | switch (TYPE(n)) { |
| 771 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 772 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 774 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 775 | for (i = 0; i < NCH(n) - 1; i++) { |
| 776 | ch = CHILD(n, i); |
| 777 | if (TYPE(ch) == NEWLINE) |
| 778 | continue; |
| 779 | REQ(ch, stmt); |
| 780 | num = num_stmts(ch); |
| 781 | if (num == 1) { |
| 782 | s = ast_for_stmt(&c, ch); |
| 783 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 784 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 785 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 786 | } |
| 787 | else { |
| 788 | ch = CHILD(ch, 0); |
| 789 | REQ(ch, simple_stmt); |
| 790 | for (j = 0; j < num; j++) { |
| 791 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 792 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 793 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 794 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 798 | res = Module(stmts, arena); |
| 799 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 800 | case eval_input: { |
| 801 | expr_ty testlist_ast; |
| 802 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 803 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 804 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 806 | goto out; |
| 807 | res = Expression(testlist_ast, arena); |
| 808 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 809 | } |
| 810 | case single_input: |
| 811 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 812 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 813 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 814 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 815 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 816 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 817 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 818 | goto out; |
| 819 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 820 | } |
| 821 | else { |
| 822 | n = CHILD(n, 0); |
| 823 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 824 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 825 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 826 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 827 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 828 | s = ast_for_stmt(&c, n); |
| 829 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 830 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 831 | asdl_seq_SET(stmts, 0, s); |
| 832 | } |
| 833 | else { |
| 834 | /* Only a simple_stmt can contain multiple statements. */ |
| 835 | REQ(n, simple_stmt); |
| 836 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 838 | break; |
| 839 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 840 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 841 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 842 | asdl_seq_SET(stmts, i / 2, s); |
| 843 | } |
| 844 | } |
| 845 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 846 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 847 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 848 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 850 | PyErr_Format(PyExc_SystemError, |
| 851 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 852 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 853 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 854 | out: |
| 855 | if (c.c_normalize) { |
| 856 | Py_DECREF(c.c_normalize); |
| 857 | PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL); |
| 858 | Py_DECREF(c.c_normalize_args); |
| 859 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 860 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 863 | mod_ty |
| 864 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 865 | PyArena *arena) |
| 866 | { |
| 867 | mod_ty mod; |
| 868 | PyObject *filename; |
| 869 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 870 | if (filename == NULL) |
| 871 | return NULL; |
| 872 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 873 | Py_DECREF(filename); |
| 874 | return mod; |
| 875 | |
| 876 | } |
| 877 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 878 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 879 | */ |
| 880 | |
| 881 | static operator_ty |
| 882 | get_operator(const node *n) |
| 883 | { |
| 884 | switch (TYPE(n)) { |
| 885 | case VBAR: |
| 886 | return BitOr; |
| 887 | case CIRCUMFLEX: |
| 888 | return BitXor; |
| 889 | case AMPER: |
| 890 | return BitAnd; |
| 891 | case LEFTSHIFT: |
| 892 | return LShift; |
| 893 | case RIGHTSHIFT: |
| 894 | return RShift; |
| 895 | case PLUS: |
| 896 | return Add; |
| 897 | case MINUS: |
| 898 | return Sub; |
| 899 | case STAR: |
| 900 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 901 | case AT: |
| 902 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 903 | case SLASH: |
| 904 | return Div; |
| 905 | case DOUBLESLASH: |
| 906 | return FloorDiv; |
| 907 | case PERCENT: |
| 908 | return Mod; |
| 909 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 910 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 914 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 915 | "None", |
| 916 | "True", |
| 917 | "False", |
| 918 | NULL, |
| 919 | }; |
| 920 | |
| 921 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 922 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 923 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 924 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 925 | assert(PyUnicode_Check(name)); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 926 | if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 927 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 928 | return 1; |
| 929 | } |
| 930 | if (full_checks) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 931 | const char * const *p; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 932 | for (p = FORBIDDEN; *p; p++) { |
| 933 | if (PyUnicode_CompareWithASCIIString(name, *p) == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 934 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 935 | return 1; |
| 936 | } |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | return 0; |
| 940 | } |
| 941 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 942 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 943 | |
| 944 | Only sets context for expr kinds that "can appear in assignment context" |
| 945 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 946 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 947 | */ |
| 948 | |
| 949 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 950 | 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] | 951 | { |
| 952 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 953 | /* If a particular expression type can't be used for assign / delete, |
| 954 | set expr_name to its name and an error message will be generated. |
| 955 | */ |
| 956 | const char* expr_name = NULL; |
| 957 | |
| 958 | /* The ast defines augmented store and load contexts, but the |
| 959 | implementation here doesn't actually use them. The code may be |
| 960 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 961 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 962 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 963 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 964 | */ |
| 965 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 966 | |
| 967 | switch (e->kind) { |
| 968 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 969 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 970 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 971 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 972 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 973 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 974 | e->v.Subscript.ctx = ctx; |
| 975 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 976 | case Starred_kind: |
| 977 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 978 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 979 | return 0; |
| 980 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 981 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 982 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 983 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 984 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 985 | } |
| 986 | e->v.Name.ctx = ctx; |
| 987 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 988 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 989 | e->v.List.ctx = ctx; |
| 990 | s = e->v.List.elts; |
| 991 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 992 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 993 | e->v.Tuple.ctx = ctx; |
| 994 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 995 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 996 | case Lambda_kind: |
| 997 | expr_name = "lambda"; |
| 998 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 999 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1000 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1001 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1002 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1003 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1004 | case UnaryOp_kind: |
| 1005 | expr_name = "operator"; |
| 1006 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1007 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1008 | expr_name = "generator expression"; |
| 1009 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1010 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1011 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1012 | expr_name = "yield expression"; |
| 1013 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1014 | case Await_kind: |
| 1015 | expr_name = "await expression"; |
| 1016 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1017 | case ListComp_kind: |
| 1018 | expr_name = "list comprehension"; |
| 1019 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1020 | case SetComp_kind: |
| 1021 | expr_name = "set comprehension"; |
| 1022 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1023 | case DictComp_kind: |
| 1024 | expr_name = "dict comprehension"; |
| 1025 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1026 | case Dict_kind: |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1027 | case Set_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1028 | case Num_kind: |
| 1029 | case Str_kind: |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 1030 | case Bytes_kind: |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1031 | case JoinedStr_kind: |
| 1032 | case FormattedValue_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1033 | expr_name = "literal"; |
| 1034 | break; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1035 | case NameConstant_kind: |
| 1036 | expr_name = "keyword"; |
| 1037 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1038 | case Ellipsis_kind: |
| 1039 | expr_name = "Ellipsis"; |
| 1040 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1041 | case Compare_kind: |
| 1042 | expr_name = "comparison"; |
| 1043 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1044 | case IfExp_kind: |
| 1045 | expr_name = "conditional expression"; |
| 1046 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1047 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | PyErr_Format(PyExc_SystemError, |
| 1049 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1050 | e->kind, e->lineno); |
| 1051 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1052 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1053 | /* Check for error string set by switch */ |
| 1054 | if (expr_name) { |
| 1055 | char buf[300]; |
| 1056 | PyOS_snprintf(buf, sizeof(buf), |
| 1057 | "can't %s %s", |
| 1058 | ctx == Store ? "assign to" : "delete", |
| 1059 | expr_name); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1060 | return ast_error(c, n, buf); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1063 | /* 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] | 1064 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1065 | */ |
| 1066 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1067 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1068 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1069 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1070 | 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] | 1071 | return 0; |
| 1072 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1073 | } |
| 1074 | return 1; |
| 1075 | } |
| 1076 | |
| 1077 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1078 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1079 | { |
| 1080 | REQ(n, augassign); |
| 1081 | n = CHILD(n, 0); |
| 1082 | switch (STR(n)[0]) { |
| 1083 | case '+': |
| 1084 | return Add; |
| 1085 | case '-': |
| 1086 | return Sub; |
| 1087 | case '/': |
| 1088 | if (STR(n)[1] == '/') |
| 1089 | return FloorDiv; |
| 1090 | else |
| 1091 | return Div; |
| 1092 | case '%': |
| 1093 | return Mod; |
| 1094 | case '<': |
| 1095 | return LShift; |
| 1096 | case '>': |
| 1097 | return RShift; |
| 1098 | case '&': |
| 1099 | return BitAnd; |
| 1100 | case '^': |
| 1101 | return BitXor; |
| 1102 | case '|': |
| 1103 | return BitOr; |
| 1104 | case '*': |
| 1105 | if (STR(n)[1] == '*') |
| 1106 | return Pow; |
| 1107 | else |
| 1108 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1109 | case '@': |
| 1110 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1111 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1112 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1113 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1118 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1119 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1120 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1121 | |'is' 'not' |
| 1122 | */ |
| 1123 | REQ(n, comp_op); |
| 1124 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1125 | n = CHILD(n, 0); |
| 1126 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1127 | case LESS: |
| 1128 | return Lt; |
| 1129 | case GREATER: |
| 1130 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1131 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1132 | return Eq; |
| 1133 | case LESSEQUAL: |
| 1134 | return LtE; |
| 1135 | case GREATEREQUAL: |
| 1136 | return GtE; |
| 1137 | case NOTEQUAL: |
| 1138 | return NotEq; |
| 1139 | case NAME: |
| 1140 | if (strcmp(STR(n), "in") == 0) |
| 1141 | return In; |
| 1142 | if (strcmp(STR(n), "is") == 0) |
| 1143 | return Is; |
| 1144 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1145 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1147 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1148 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1149 | } |
| 1150 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1151 | /* handle "not in" and "is not" */ |
| 1152 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1153 | case NAME: |
| 1154 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1155 | return NotIn; |
| 1156 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1157 | return IsNot; |
| 1158 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1159 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1160 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1161 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1162 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1163 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1164 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1165 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1166 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static asdl_seq * |
| 1170 | seq_for_testlist(struct compiling *c, const node *n) |
| 1171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1173 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1174 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1175 | asdl_seq *seq; |
| 1176 | expr_ty expression; |
| 1177 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1178 | 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] | 1179 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1180 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1181 | if (!seq) |
| 1182 | return NULL; |
| 1183 | |
| 1184 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1186 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1187 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1188 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1189 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1190 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1191 | |
| 1192 | assert(i / 2 < seq->size); |
| 1193 | asdl_seq_SET(seq, i / 2, expression); |
| 1194 | } |
| 1195 | return seq; |
| 1196 | } |
| 1197 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1198 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1199 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1200 | { |
| 1201 | identifier name; |
| 1202 | expr_ty annotation = NULL; |
| 1203 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1204 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1205 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1206 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1207 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1208 | name = NEW_IDENTIFIER(ch); |
| 1209 | if (!name) |
| 1210 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1211 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1212 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1213 | |
| 1214 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1215 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1216 | if (!annotation) |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1220 | 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] | 1221 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1222 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1223 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1226 | /* returns -1 if failed to handle keyword only arguments |
| 1227 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1228 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1229 | ^^^ |
| 1230 | start pointing here |
| 1231 | */ |
| 1232 | static int |
| 1233 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1234 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1235 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1236 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1237 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1238 | expr_ty expression, annotation; |
| 1239 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1240 | int i = start; |
| 1241 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1244 | 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] | 1245 | return -1; |
| 1246 | } |
| 1247 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1248 | while (i < NCH(n)) { |
| 1249 | ch = CHILD(n, i); |
| 1250 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1251 | case vfpdef: |
| 1252 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1253 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1254 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1255 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1256 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1257 | asdl_seq_SET(kwdefaults, j, expression); |
| 1258 | i += 2; /* '=' and test */ |
| 1259 | } |
| 1260 | else { /* setting NULL if no default value exists */ |
| 1261 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1262 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1263 | if (NCH(ch) == 3) { |
| 1264 | /* ch is NAME ':' test */ |
| 1265 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1266 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1267 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1268 | } |
| 1269 | else { |
| 1270 | annotation = NULL; |
| 1271 | } |
| 1272 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1273 | argname = NEW_IDENTIFIER(ch); |
| 1274 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1275 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1276 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1277 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1278 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 1279 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1280 | if (!arg) |
| 1281 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1282 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1283 | i += 2; /* the name and the comma */ |
| 1284 | break; |
| 1285 | case DOUBLESTAR: |
| 1286 | return i; |
| 1287 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1288 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1289 | goto error; |
| 1290 | } |
| 1291 | } |
| 1292 | return i; |
| 1293 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1295 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1296 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1297 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1298 | |
| 1299 | static arguments_ty |
| 1300 | ast_for_arguments(struct compiling *c, const node *n) |
| 1301 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1302 | /* This function handles both typedargslist (function definition) |
| 1303 | and varargslist (lambda definition). |
| 1304 | |
| 1305 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1306 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1307 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1308 | | '**' tfpdef [',']]] |
| 1309 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1310 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1311 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1312 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1313 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1314 | | '**' vfpdef [',']]] |
| 1315 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1316 | | '**' vfpdef [','] |
| 1317 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1318 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1319 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1320 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1321 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1322 | int nposdefaults = 0, found_default = 0; |
| 1323 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1324 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1325 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1326 | node *ch; |
| 1327 | |
| 1328 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1329 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1330 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1331 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1332 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1333 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1334 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1335 | /* First count the number of positional args & defaults. The |
| 1336 | variable i is the loop index for this for loop and the next. |
| 1337 | The next loop picks up where the first leaves off. |
| 1338 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1339 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1340 | ch = CHILD(n, i); |
| 1341 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1342 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1343 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1344 | if (i < NCH(n) && /* skip argument following star */ |
| 1345 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1346 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1347 | i++; |
| 1348 | } |
| 1349 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1350 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1351 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1352 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1353 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1354 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1356 | defaults for keyword only args */ |
| 1357 | for ( ; i < NCH(n); ++i) { |
| 1358 | ch = CHILD(n, i); |
| 1359 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1360 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1361 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1362 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1363 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1364 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1365 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1366 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1367 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1368 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1370 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1371 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1372 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1374 | since we set NULL as default for keyword only argument w/o default |
| 1375 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1376 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1377 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1378 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1379 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (nposargs + nkwonlyargs > 255) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1382 | ast_error(c, n, "more than 255 arguments"); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1383 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1384 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1385 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1386 | /* tfpdef: NAME [':' test] |
| 1387 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1388 | */ |
| 1389 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1390 | j = 0; /* index for defaults */ |
| 1391 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1392 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1393 | ch = CHILD(n, i); |
| 1394 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1395 | case tfpdef: |
| 1396 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1397 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1398 | anything other than EQUAL or a comma? */ |
| 1399 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1400 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1401 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1402 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1403 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1404 | assert(posdefaults != NULL); |
| 1405 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1407 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1408 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1409 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1410 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1411 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1412 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1413 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1414 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1415 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1416 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1417 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | i += 2; /* the name and the comma */ |
| 1419 | break; |
| 1420 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1421 | if (i+1 >= NCH(n) || |
| 1422 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1423 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1424 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1425 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1426 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1427 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1428 | if (TYPE(ch) == COMMA) { |
| 1429 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1430 | i += 2; /* now follows keyword only arguments */ |
| 1431 | res = handle_keywordonly_args(c, n, i, |
| 1432 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1433 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1434 | i = res; /* res has new position to process */ |
| 1435 | } |
| 1436 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1437 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1438 | if (!vararg) |
| 1439 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1440 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1441 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1442 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1443 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1444 | int res = 0; |
| 1445 | res = handle_keywordonly_args(c, n, i, |
| 1446 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1447 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1448 | i = res; /* res has new position to process */ |
| 1449 | } |
| 1450 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1451 | break; |
| 1452 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1453 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1454 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1455 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1456 | if (!kwarg) |
| 1457 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1458 | i += 3; |
| 1459 | break; |
| 1460 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1461 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1462 | "unexpected node in varargslist: %d @ %d", |
| 1463 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1464 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1465 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1467 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | static expr_ty |
| 1471 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1472 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1473 | expr_ty e; |
| 1474 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1475 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1476 | int i; |
| 1477 | |
| 1478 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1479 | |
| 1480 | lineno = LINENO(n); |
| 1481 | col_offset = n->n_col_offset; |
| 1482 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1483 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1484 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1485 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1486 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1487 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1488 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1489 | |
| 1490 | for (i = 2; i < NCH(n); i+=2) { |
| 1491 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1492 | if (!id) |
| 1493 | return NULL; |
| 1494 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1495 | if (!e) |
| 1496 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | static expr_ty |
| 1503 | ast_for_decorator(struct compiling *c, const node *n) |
| 1504 | { |
| 1505 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1506 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1507 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1509 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1510 | REQ(CHILD(n, 0), AT); |
| 1511 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1513 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1514 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1515 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1517 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1518 | d = name_expr; |
| 1519 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | } |
| 1521 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1522 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1523 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1524 | if (!d) |
| 1525 | return NULL; |
| 1526 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1527 | } |
| 1528 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1529 | d = ast_for_call(c, CHILD(n, 3), name_expr); |
| 1530 | if (!d) |
| 1531 | return NULL; |
| 1532 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static asdl_seq* |
| 1539 | ast_for_decorators(struct compiling *c, const node *n) |
| 1540 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1541 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1542 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 | int i; |
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, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1546 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1547 | if (!decorator_seq) |
| 1548 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1550 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1551 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1552 | if (!d) |
| 1553 | return NULL; |
| 1554 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1555 | } |
| 1556 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1560 | ast_for_funcdef_impl(struct compiling *c, const node *n, |
| 1561 | asdl_seq *decorator_seq, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1563 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1564 | identifier name; |
| 1565 | arguments_ty args; |
| 1566 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1567 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1568 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1569 | |
| 1570 | REQ(n, funcdef); |
| 1571 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1572 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1573 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1574 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1575 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1576 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1577 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1578 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1579 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1580 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1581 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1582 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1583 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1584 | name_i += 2; |
| 1585 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1586 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
| 1587 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1588 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1589 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1590 | if (is_async) |
| 1591 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
| 1592 | LINENO(n), |
| 1593 | n->n_col_offset, c->c_arena); |
| 1594 | else |
| 1595 | return FunctionDef(name, args, body, decorator_seq, returns, |
| 1596 | LINENO(n), |
| 1597 | n->n_col_offset, c->c_arena); |
| 1598 | } |
| 1599 | |
| 1600 | static stmt_ty |
| 1601 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1602 | { |
| 1603 | /* async_funcdef: ASYNC funcdef */ |
| 1604 | REQ(n, async_funcdef); |
| 1605 | REQ(CHILD(n, 0), ASYNC); |
| 1606 | REQ(CHILD(n, 1), funcdef); |
| 1607 | |
| 1608 | return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, |
| 1609 | 1 /* is_async */); |
| 1610 | } |
| 1611 | |
| 1612 | static stmt_ty |
| 1613 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1614 | { |
| 1615 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1616 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1617 | 0 /* is_async */); |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | static stmt_ty |
| 1622 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1623 | { |
| 1624 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
| 1625 | REQ(n, async_stmt); |
| 1626 | REQ(CHILD(n, 0), ASYNC); |
| 1627 | |
| 1628 | switch (TYPE(CHILD(n, 1))) { |
| 1629 | case funcdef: |
| 1630 | return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, |
| 1631 | 1 /* is_async */); |
| 1632 | case with_stmt: |
| 1633 | return ast_for_with_stmt(c, CHILD(n, 1), |
| 1634 | 1 /* is_async */); |
| 1635 | |
| 1636 | case for_stmt: |
| 1637 | return ast_for_for_stmt(c, CHILD(n, 1), |
| 1638 | 1 /* is_async */); |
| 1639 | |
| 1640 | default: |
| 1641 | PyErr_Format(PyExc_SystemError, |
| 1642 | "invalid async stament: %s", |
| 1643 | STR(CHILD(n, 1))); |
| 1644 | return NULL; |
| 1645 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1648 | static stmt_ty |
| 1649 | ast_for_decorated(struct compiling *c, const node *n) |
| 1650 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1651 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1652 | stmt_ty thing = NULL; |
| 1653 | asdl_seq *decorator_seq = NULL; |
| 1654 | |
| 1655 | REQ(n, decorated); |
| 1656 | |
| 1657 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1658 | if (!decorator_seq) |
| 1659 | return NULL; |
| 1660 | |
| 1661 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1662 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1666 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1667 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1668 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1669 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1670 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1671 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1672 | /* we count the decorators in when talking about the class' or |
| 1673 | * function's line number */ |
| 1674 | if (thing) { |
| 1675 | thing->lineno = LINENO(n); |
| 1676 | thing->col_offset = n->n_col_offset; |
| 1677 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1678 | return thing; |
| 1679 | } |
| 1680 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1681 | static expr_ty |
| 1682 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1683 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1684 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1685 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1686 | arguments_ty args; |
| 1687 | expr_ty expression; |
| 1688 | |
| 1689 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1690 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1691 | if (!args) |
| 1692 | return NULL; |
| 1693 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1694 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1695 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1696 | } |
| 1697 | else { |
| 1698 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1699 | if (!args) |
| 1700 | return NULL; |
| 1701 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1702 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1703 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1706 | 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] | 1707 | } |
| 1708 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1709 | static expr_ty |
| 1710 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1713 | expr_ty expression, body, orelse; |
| 1714 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1715 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1716 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1717 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1718 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1719 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1720 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1721 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1722 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1723 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1724 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1725 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1726 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1729 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1730 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1731 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1732 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1733 | */ |
| 1734 | |
| 1735 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1736 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1737 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1738 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1739 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1740 | count_comp_for: |
| 1741 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1742 | REQ(n, comp_for); |
| 1743 | if (NCH(n) == 5) |
| 1744 | n = CHILD(n, 4); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1745 | else |
| 1746 | return n_fors; |
| 1747 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1748 | REQ(n, comp_iter); |
| 1749 | n = CHILD(n, 0); |
| 1750 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1751 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1752 | else if (TYPE(n) == comp_if) { |
| 1753 | if (NCH(n) == 3) { |
| 1754 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1755 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1756 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1757 | else |
| 1758 | return n_fors; |
| 1759 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1760 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1761 | /* Should never be reached */ |
| 1762 | PyErr_SetString(PyExc_SystemError, |
| 1763 | "logic error in count_comp_fors"); |
| 1764 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1767 | /* Count the number of 'if' statements 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_ifs(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_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1776 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1777 | while (1) { |
| 1778 | REQ(n, comp_iter); |
| 1779 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1780 | return n_ifs; |
| 1781 | n = CHILD(n, 0); |
| 1782 | REQ(n, comp_if); |
| 1783 | n_ifs++; |
| 1784 | if (NCH(n) == 2) |
| 1785 | return n_ifs; |
| 1786 | n = CHILD(n, 2); |
| 1787 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1790 | static asdl_seq * |
| 1791 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1792 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1793 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1794 | asdl_seq *comps; |
| 1795 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1796 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | if (n_fors == -1) |
| 1798 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1799 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1800 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1801 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1802 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1803 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1804 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1805 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1806 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1807 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1808 | node *for_ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1810 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1812 | for_ch = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1813 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1814 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1815 | return NULL; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1816 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1817 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1818 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1819 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1820 | /* Check the # of children rather than the length of t, since |
| 1821 | (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] | 1822 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1823 | if (NCH(for_ch) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1824 | comp = comprehension(first, expression, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1825 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1826 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 1827 | c->c_arena), |
| 1828 | expression, NULL, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1829 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1830 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1831 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1832 | if (NCH(n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | int j, n_ifs; |
| 1834 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1836 | n = CHILD(n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1837 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1838 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1839 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1840 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1841 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1842 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1843 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1844 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1845 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1846 | REQ(n, comp_iter); |
| 1847 | n = CHILD(n, 0); |
| 1848 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1850 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1851 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1852 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1853 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1854 | if (NCH(n) == 3) |
| 1855 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1856 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1857 | /* on exit, must guarantee that n is a comp_for */ |
| 1858 | if (TYPE(n) == comp_iter) |
| 1859 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1860 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1861 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1862 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1863 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1864 | return comps; |
| 1865 | } |
| 1866 | |
| 1867 | static expr_ty |
| 1868 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1869 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1870 | /* testlist_comp: (test|star_expr) |
| 1871 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1872 | expr_ty elt; |
| 1873 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1874 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1876 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1878 | ch = CHILD(n, 0); |
| 1879 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1880 | if (!elt) |
| 1881 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1882 | if (elt->kind == Starred_kind) { |
| 1883 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1884 | return NULL; |
| 1885 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1887 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1888 | if (!comps) |
| 1889 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1890 | |
| 1891 | if (type == COMP_GENEXP) |
| 1892 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1893 | else if (type == COMP_LISTCOMP) |
| 1894 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1895 | else if (type == COMP_SETCOMP) |
| 1896 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1897 | else |
| 1898 | /* Should never happen */ |
| 1899 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1902 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1903 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1904 | * elements. Iff successful, nonzero is returned. |
| 1905 | */ |
| 1906 | static int |
| 1907 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1908 | expr_ty *key, expr_ty *value) |
| 1909 | { |
| 1910 | expr_ty expression; |
| 1911 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1912 | assert(NCH(n) - *i >= 2); |
| 1913 | |
| 1914 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1915 | if (!expression) |
| 1916 | return 0; |
| 1917 | *key = NULL; |
| 1918 | *value = expression; |
| 1919 | |
| 1920 | *i += 2; |
| 1921 | } |
| 1922 | else { |
| 1923 | assert(NCH(n) - *i >= 3); |
| 1924 | |
| 1925 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1926 | if (!expression) |
| 1927 | return 0; |
| 1928 | *key = expression; |
| 1929 | |
| 1930 | REQ(CHILD(n, *i + 1), COLON); |
| 1931 | |
| 1932 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1933 | if (!expression) |
| 1934 | return 0; |
| 1935 | *value = expression; |
| 1936 | |
| 1937 | *i += 3; |
| 1938 | } |
| 1939 | return 1; |
| 1940 | } |
| 1941 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1942 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1943 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1944 | { |
| 1945 | expr_ty key, value; |
| 1946 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1947 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1949 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1950 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1951 | assert(key); |
| 1952 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1954 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1955 | if (!comps) |
| 1956 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1958 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1959 | } |
| 1960 | |
| 1961 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1962 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 1963 | { |
| 1964 | int i; |
| 1965 | int j; |
| 1966 | int size; |
| 1967 | asdl_seq *keys, *values; |
| 1968 | |
| 1969 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 1970 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 1971 | if (!keys) |
| 1972 | return NULL; |
| 1973 | |
| 1974 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 1975 | if (!values) |
| 1976 | return NULL; |
| 1977 | |
| 1978 | j = 0; |
| 1979 | for (i = 0; i < NCH(n); i++) { |
| 1980 | expr_ty key, value; |
| 1981 | |
| 1982 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 1983 | return NULL; |
| 1984 | asdl_seq_SET(keys, j, key); |
| 1985 | asdl_seq_SET(values, j, value); |
| 1986 | |
| 1987 | j++; |
| 1988 | } |
| 1989 | keys->size = j; |
| 1990 | values->size = j; |
| 1991 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 1992 | } |
| 1993 | |
| 1994 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1995 | ast_for_genexp(struct compiling *c, const node *n) |
| 1996 | { |
| 1997 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1998 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | static expr_ty |
| 2002 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2003 | { |
| 2004 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2005 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | static expr_ty |
| 2009 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2010 | { |
| 2011 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2012 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2015 | static expr_ty |
| 2016 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2017 | { |
| 2018 | int i; |
| 2019 | int size; |
| 2020 | asdl_seq *elts; |
| 2021 | |
| 2022 | assert(TYPE(n) == (dictorsetmaker)); |
| 2023 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2024 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2025 | if (!elts) |
| 2026 | return NULL; |
| 2027 | for (i = 0; i < NCH(n); i += 2) { |
| 2028 | expr_ty expression; |
| 2029 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2030 | if (!expression) |
| 2031 | return NULL; |
| 2032 | asdl_seq_SET(elts, i / 2, expression); |
| 2033 | } |
| 2034 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2035 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2036 | |
| 2037 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2038 | ast_for_atom(struct compiling *c, const node *n) |
| 2039 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2040 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2041 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2042 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2043 | */ |
| 2044 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2046 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2047 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2048 | PyObject *name; |
| 2049 | const char *s = STR(ch); |
| 2050 | size_t len = strlen(s); |
| 2051 | if (len >= 4 && len <= 5) { |
| 2052 | if (!strcmp(s, "None")) |
| 2053 | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
| 2054 | if (!strcmp(s, "True")) |
| 2055 | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
| 2056 | if (!strcmp(s, "False")) |
| 2057 | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
| 2058 | } |
| 2059 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2060 | if (!name) |
| 2061 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2062 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2063 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2064 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2065 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2066 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2067 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2068 | const char *errtype = NULL; |
| 2069 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2070 | errtype = "unicode error"; |
| 2071 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2072 | errtype = "value error"; |
| 2073 | if (errtype) { |
| 2074 | char buf[128]; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2075 | PyObject *type, *value, *tback, *errstr; |
| 2076 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2077 | errstr = PyObject_Str(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2078 | if (errstr) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2079 | char *s = _PyUnicode_AsString(errstr); |
| 2080 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2081 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2082 | } else { |
Victor Stinner | 00723e0 | 2015-09-03 12:57:11 +0200 | [diff] [blame] | 2083 | PyErr_Clear(); |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2084 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2085 | } |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2086 | ast_error(c, n, buf); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2087 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2088 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2089 | Py_XDECREF(tback); |
| 2090 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2091 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2092 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2093 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2094 | } |
| 2095 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2096 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2097 | if (!pynum) |
| 2098 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2099 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2100 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2101 | Py_DECREF(pynum); |
| 2102 | return NULL; |
| 2103 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2104 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2105 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2106 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2107 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2108 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2109 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2111 | if (TYPE(ch) == RPAR) |
| 2112 | 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] | 2113 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2114 | if (TYPE(ch) == yield_expr) |
| 2115 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2118 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2119 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2120 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2121 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2122 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2123 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2125 | if (TYPE(ch) == RSQB) |
| 2126 | 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] | 2127 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2128 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2129 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2130 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2131 | if (!elts) |
| 2132 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2133 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2134 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2135 | } |
| 2136 | else |
| 2137 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2138 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2139 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2140 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2141 | * ((test | '*' test) |
| 2142 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2143 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2144 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2145 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2146 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2147 | 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] | 2148 | } |
| 2149 | else { |
| 2150 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2151 | if (NCH(ch) == 1 || |
| 2152 | (NCH(ch) > 1 && |
| 2153 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2154 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2155 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2156 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2157 | else if (NCH(ch) > 1 && |
| 2158 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2159 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2160 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2161 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2162 | else if (NCH(ch) > 3 - is_dict && |
| 2163 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2164 | /* It's a dictionary comprehension. */ |
| 2165 | if (is_dict) { |
| 2166 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2167 | "dict comprehension"); |
| 2168 | return NULL; |
| 2169 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2170 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2171 | } |
| 2172 | else { |
| 2173 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2174 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2175 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2176 | if (res) { |
| 2177 | res->lineno = LINENO(n); |
| 2178 | res->col_offset = n->n_col_offset; |
| 2179 | } |
| 2180 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2181 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2182 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2183 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2184 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2185 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | static slice_ty |
| 2190 | ast_for_slice(struct compiling *c, const node *n) |
| 2191 | { |
| 2192 | node *ch; |
| 2193 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2194 | |
| 2195 | REQ(n, subscript); |
| 2196 | |
| 2197 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2198 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2199 | sliceop: ':' [test] |
| 2200 | */ |
| 2201 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2202 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2203 | /* 'step' variable hold no significance in terms of being used over |
| 2204 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2206 | if (!step) |
| 2207 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2209 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2213 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2214 | if (!lower) |
| 2215 | return NULL; |
| 2216 | } |
| 2217 | |
| 2218 | /* If there's an upper bound it's in the second or third position. */ |
| 2219 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2220 | if (NCH(n) > 1) { |
| 2221 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2222 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2223 | if (TYPE(n2) == test) { |
| 2224 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2225 | if (!upper) |
| 2226 | return NULL; |
| 2227 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2228 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2229 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2230 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2231 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2232 | if (TYPE(n2) == test) { |
| 2233 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2234 | if (!upper) |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | ch = CHILD(n, NCH(n) - 1); |
| 2240 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2241 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2242 | ch = CHILD(ch, 1); |
| 2243 | if (TYPE(ch) == test) { |
| 2244 | step = ast_for_expr(c, ch); |
| 2245 | if (!step) |
| 2246 | return NULL; |
| 2247 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2251 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | static expr_ty |
| 2255 | ast_for_binop(struct compiling *c, const node *n) |
| 2256 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2257 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2259 | BinOp(BinOp(A, op, B), op, C). |
| 2260 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2261 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2262 | int i, nops; |
| 2263 | expr_ty expr1, expr2, result; |
| 2264 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2265 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2266 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2267 | if (!expr1) |
| 2268 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2270 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2271 | if (!expr2) |
| 2272 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2273 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2274 | newoperator = get_operator(CHILD(n, 1)); |
| 2275 | if (!newoperator) |
| 2276 | return NULL; |
| 2277 | |
| 2278 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2279 | c->c_arena); |
| 2280 | if (!result) |
| 2281 | return NULL; |
| 2282 | |
| 2283 | nops = (NCH(n) - 1) / 2; |
| 2284 | for (i = 1; i < nops; i++) { |
| 2285 | expr_ty tmp_result, tmp; |
| 2286 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2287 | |
| 2288 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2289 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2290 | return NULL; |
| 2291 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2292 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2293 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2294 | return NULL; |
| 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2297 | LINENO(next_oper), next_oper->n_col_offset, |
| 2298 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2300 | return NULL; |
| 2301 | result = tmp_result; |
| 2302 | } |
| 2303 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2306 | static expr_ty |
| 2307 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2310 | subscriptlist: subscript (',' subscript)* [','] |
| 2311 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2312 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2313 | REQ(n, trailer); |
| 2314 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2315 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2316 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2317 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2318 | else |
| 2319 | return ast_for_call(c, CHILD(n, 1), left_expr); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2320 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2321 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2322 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2323 | if (!attr_id) |
| 2324 | return NULL; |
| 2325 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2326 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2327 | } |
| 2328 | else { |
| 2329 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2330 | REQ(CHILD(n, 2), RSQB); |
| 2331 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2332 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2333 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2334 | if (!slc) |
| 2335 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2336 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2337 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2338 | } |
| 2339 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2341 | by treating the sequence as a tuple literal if there are |
| 2342 | no slice features. |
| 2343 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2344 | int j; |
| 2345 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2346 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2347 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2348 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2349 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2350 | if (!slices) |
| 2351 | return NULL; |
| 2352 | for (j = 0; j < NCH(n); j += 2) { |
| 2353 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2354 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2355 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2356 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2357 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2358 | asdl_seq_SET(slices, j / 2, slc); |
| 2359 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2360 | if (!simple) { |
| 2361 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2362 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2363 | } |
| 2364 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2365 | 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] | 2366 | if (!elts) |
| 2367 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2368 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2369 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2370 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2371 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2372 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2373 | 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] | 2374 | if (!e) |
| 2375 | return NULL; |
| 2376 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2377 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2383 | ast_for_factor(struct compiling *c, const node *n) |
| 2384 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2385 | expr_ty expression; |
| 2386 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2387 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2388 | if (!expression) |
| 2389 | return NULL; |
| 2390 | |
| 2391 | switch (TYPE(CHILD(n, 0))) { |
| 2392 | case PLUS: |
| 2393 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2394 | c->c_arena); |
| 2395 | case MINUS: |
| 2396 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2397 | c->c_arena); |
| 2398 | case TILDE: |
| 2399 | return UnaryOp(Invert, expression, LINENO(n), |
| 2400 | n->n_col_offset, c->c_arena); |
| 2401 | } |
| 2402 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2403 | TYPE(CHILD(n, 0))); |
| 2404 | return NULL; |
| 2405 | } |
| 2406 | |
| 2407 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2408 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2409 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2410 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2411 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2412 | |
| 2413 | REQ(n, atom_expr); |
| 2414 | nch = NCH(n); |
| 2415 | |
| 2416 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2417 | start = 1; |
| 2418 | assert(nch > 1); |
| 2419 | } |
| 2420 | |
| 2421 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2422 | if (!e) |
| 2423 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2424 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2425 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2426 | if (start && nch == 2) { |
| 2427 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2428 | } |
| 2429 | |
| 2430 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2431 | node *ch = CHILD(n, i); |
| 2432 | if (TYPE(ch) != trailer) |
| 2433 | break; |
| 2434 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2435 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2436 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2437 | tmp->lineno = e->lineno; |
| 2438 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2439 | e = tmp; |
| 2440 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2441 | |
| 2442 | if (start) { |
| 2443 | /* there was an AWAIT */ |
| 2444 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2445 | } |
| 2446 | else { |
| 2447 | return e; |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | static expr_ty |
| 2452 | ast_for_power(struct compiling *c, const node *n) |
| 2453 | { |
| 2454 | /* power: atom trailer* ('**' factor)* |
| 2455 | */ |
| 2456 | expr_ty e; |
| 2457 | REQ(n, power); |
| 2458 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2459 | if (!e) |
| 2460 | return NULL; |
| 2461 | if (NCH(n) == 1) |
| 2462 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2463 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2464 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2465 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2466 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2467 | 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] | 2468 | } |
| 2469 | return e; |
| 2470 | } |
| 2471 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2472 | static expr_ty |
| 2473 | ast_for_starred(struct compiling *c, const node *n) |
| 2474 | { |
| 2475 | expr_ty tmp; |
| 2476 | REQ(n, star_expr); |
| 2477 | |
| 2478 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2479 | if (!tmp) |
| 2480 | return NULL; |
| 2481 | |
| 2482 | /* The Load context is changed later. */ |
| 2483 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2484 | } |
| 2485 | |
| 2486 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2487 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2488 | */ |
| 2489 | |
| 2490 | static expr_ty |
| 2491 | ast_for_expr(struct compiling *c, const node *n) |
| 2492 | { |
| 2493 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2494 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2495 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2497 | and_test: not_test ('and' not_test)* |
| 2498 | not_test: 'not' not_test | comparison |
| 2499 | comparison: expr (comp_op expr)* |
| 2500 | expr: xor_expr ('|' xor_expr)* |
| 2501 | xor_expr: and_expr ('^' and_expr)* |
| 2502 | and_expr: shift_expr ('&' shift_expr)* |
| 2503 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2504 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2505 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2506 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2507 | power: atom_expr ['**' factor] |
| 2508 | atom_expr: [AWAIT] atom trailer* |
| 2509 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2510 | */ |
| 2511 | |
| 2512 | asdl_seq *seq; |
| 2513 | int i; |
| 2514 | |
| 2515 | loop: |
| 2516 | switch (TYPE(n)) { |
| 2517 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2518 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2519 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2520 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2521 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2522 | else if (NCH(n) > 1) |
| 2523 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2524 | /* Fallthrough */ |
| 2525 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2526 | case and_test: |
| 2527 | if (NCH(n) == 1) { |
| 2528 | n = CHILD(n, 0); |
| 2529 | goto loop; |
| 2530 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2531 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2532 | if (!seq) |
| 2533 | return NULL; |
| 2534 | for (i = 0; i < NCH(n); i += 2) { |
| 2535 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2536 | if (!e) |
| 2537 | return NULL; |
| 2538 | asdl_seq_SET(seq, i / 2, e); |
| 2539 | } |
| 2540 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2541 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2542 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2543 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2544 | 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] | 2545 | case not_test: |
| 2546 | if (NCH(n) == 1) { |
| 2547 | n = CHILD(n, 0); |
| 2548 | goto loop; |
| 2549 | } |
| 2550 | else { |
| 2551 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2552 | if (!expression) |
| 2553 | return NULL; |
| 2554 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2555 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2556 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2557 | } |
| 2558 | case comparison: |
| 2559 | if (NCH(n) == 1) { |
| 2560 | n = CHILD(n, 0); |
| 2561 | goto loop; |
| 2562 | } |
| 2563 | else { |
| 2564 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2565 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2566 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2567 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2568 | if (!ops) |
| 2569 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2570 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2571 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2572 | return NULL; |
| 2573 | } |
| 2574 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2575 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2576 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2577 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2578 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2579 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2580 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2581 | |
| 2582 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2583 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2584 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2585 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2586 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2587 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2588 | asdl_seq_SET(cmps, i / 2, expression); |
| 2589 | } |
| 2590 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2591 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2592 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2593 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2595 | return Compare(expression, ops, cmps, LINENO(n), |
| 2596 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2597 | } |
| 2598 | break; |
| 2599 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2600 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2601 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2602 | /* The next five cases all handle BinOps. The main body of code |
| 2603 | is the same in each case, but the switch turned inside out to |
| 2604 | reuse the code for each type of operator. |
| 2605 | */ |
| 2606 | case expr: |
| 2607 | case xor_expr: |
| 2608 | case and_expr: |
| 2609 | case shift_expr: |
| 2610 | case arith_expr: |
| 2611 | case term: |
| 2612 | if (NCH(n) == 1) { |
| 2613 | n = CHILD(n, 0); |
| 2614 | goto loop; |
| 2615 | } |
| 2616 | return ast_for_binop(c, n); |
| 2617 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2618 | node *an = NULL; |
| 2619 | node *en = NULL; |
| 2620 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2621 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2622 | if (NCH(n) > 1) |
| 2623 | an = CHILD(n, 1); /* yield_arg */ |
| 2624 | if (an) { |
| 2625 | en = CHILD(an, NCH(an) - 1); |
| 2626 | if (NCH(an) == 2) { |
| 2627 | is_from = 1; |
| 2628 | exp = ast_for_expr(c, en); |
| 2629 | } |
| 2630 | else |
| 2631 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2632 | if (!exp) |
| 2633 | return NULL; |
| 2634 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2635 | if (is_from) |
| 2636 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2637 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2638 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2639 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 | if (NCH(n) == 1) { |
| 2641 | n = CHILD(n, 0); |
| 2642 | goto loop; |
| 2643 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2644 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2645 | case power: |
| 2646 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2648 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2649 | return NULL; |
| 2650 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2651 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2652 | return NULL; |
| 2653 | } |
| 2654 | |
| 2655 | static expr_ty |
| 2656 | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
| 2657 | { |
| 2658 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2659 | arglist: argument (',' argument)* [','] |
| 2660 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2661 | */ |
| 2662 | |
| 2663 | int i, nargs, nkeywords, ngens; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2664 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2665 | asdl_seq *args; |
| 2666 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2667 | |
| 2668 | REQ(n, arglist); |
| 2669 | |
| 2670 | nargs = 0; |
| 2671 | nkeywords = 0; |
| 2672 | ngens = 0; |
| 2673 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2674 | node *ch = CHILD(n, i); |
| 2675 | if (TYPE(ch) == argument) { |
| 2676 | if (NCH(ch) == 1) |
| 2677 | nargs++; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2678 | else if (TYPE(CHILD(ch, 1)) == comp_for) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2679 | ngens++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2680 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2681 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2682 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2683 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2684 | nkeywords++; |
| 2685 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2686 | } |
| 2687 | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2688 | ast_error(c, n, "Generator expression must be parenthesized " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2689 | "if not sole argument"); |
| 2690 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | if (nargs + nkeywords + ngens > 255) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2694 | ast_error(c, n, "more than 255 arguments"); |
| 2695 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2698 | args = _Py_asdl_seq_new(nargs + ngens, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2700 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2701 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2702 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2703 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2704 | |
| 2705 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2706 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2707 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2708 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2709 | node *ch = CHILD(n, i); |
| 2710 | if (TYPE(ch) == argument) { |
| 2711 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2712 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2713 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2714 | /* a positional argument */ |
| 2715 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2716 | if (ndoublestars) { |
| 2717 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2718 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2719 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2720 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2721 | else { |
| 2722 | ast_error(c, chch, |
| 2723 | "positional argument follows " |
| 2724 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2725 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2726 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2727 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2728 | e = ast_for_expr(c, chch); |
| 2729 | if (!e) |
| 2730 | return NULL; |
| 2731 | asdl_seq_SET(args, nargs++, e); |
| 2732 | } |
| 2733 | else if (TYPE(chch) == STAR) { |
| 2734 | /* an iterable argument unpacking */ |
| 2735 | expr_ty starred; |
| 2736 | if (ndoublestars) { |
| 2737 | ast_error(c, chch, |
| 2738 | "iterable argument unpacking follows " |
| 2739 | "keyword argument unpacking"); |
| 2740 | return NULL; |
| 2741 | } |
| 2742 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2743 | if (!e) |
| 2744 | return NULL; |
| 2745 | starred = Starred(e, Load, LINENO(chch), |
| 2746 | chch->n_col_offset, |
| 2747 | c->c_arena); |
| 2748 | if (!starred) |
| 2749 | return NULL; |
| 2750 | asdl_seq_SET(args, nargs++, starred); |
| 2751 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2752 | } |
| 2753 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2754 | /* a keyword argument unpacking */ |
| 2755 | keyword_ty kw; |
| 2756 | i++; |
| 2757 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2758 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2759 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2760 | kw = keyword(NULL, e, c->c_arena); |
| 2761 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2762 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2764 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2765 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2766 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2767 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2768 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2769 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2770 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2771 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2772 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2773 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2774 | identifier key, tmp; |
| 2775 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2776 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2777 | /* chch is test, but must be an identifier? */ |
| 2778 | e = ast_for_expr(c, chch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2779 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2780 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2781 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2782 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 2783 | * SF bug 132313 points out that complaining about a keyword |
| 2784 | * then is very confusing. |
| 2785 | */ |
| 2786 | if (e->kind == Lambda_kind) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2787 | ast_error(c, chch, |
| 2788 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2789 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2790 | } |
| 2791 | else if (e->kind != Name_kind) { |
| 2792 | ast_error(c, chch, |
| 2793 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2794 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2795 | } |
| 2796 | else if (forbidden_name(c, e->v.Name.id, ch, 1)) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2797 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2799 | key = e->v.Name.id; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2800 | for (k = 0; k < nkeywords; k++) { |
| 2801 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2802 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2803 | ast_error(c, chch, |
| 2804 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2805 | return NULL; |
| 2806 | } |
| 2807 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2808 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2809 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2810 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2811 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2812 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2813 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2814 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2815 | } |
| 2816 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2819 | 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] | 2820 | } |
| 2821 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2822 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2823 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2824 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2825 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2826 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2827 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2828 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2829 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2830 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2831 | } |
| 2832 | else { |
| 2833 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2834 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2835 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2836 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2837 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2838 | else { |
| 2839 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2840 | if (!tmp) |
| 2841 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2842 | 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] | 2843 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2846 | static stmt_ty |
| 2847 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2848 | { |
| 2849 | REQ(n, expr_stmt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2851 | | ('=' (yield_expr|testlist))*) |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2852 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2853 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2854 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2855 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | */ |
| 2857 | |
| 2858 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2859 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2860 | if (!e) |
| 2861 | return NULL; |
| 2862 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2863 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | } |
| 2865 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2866 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2867 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2868 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2870 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2871 | if (!expr1) |
| 2872 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2873 | if(!set_context(c, expr1, Store, ch)) |
| 2874 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2875 | /* set_context checks that most expressions are not the left side. |
| 2876 | Augmented assignments can only have a name, a subscript, or an |
| 2877 | attribute on the left, though, so we have to explicitly check for |
| 2878 | those. */ |
| 2879 | switch (expr1->kind) { |
| 2880 | case Name_kind: |
| 2881 | case Attribute_kind: |
| 2882 | case Subscript_kind: |
| 2883 | break; |
| 2884 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2885 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2886 | return NULL; |
| 2887 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2889 | ch = CHILD(n, 2); |
| 2890 | if (TYPE(ch) == testlist) |
| 2891 | expr2 = ast_for_testlist(c, ch); |
| 2892 | else |
| 2893 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2894 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2895 | return NULL; |
| 2896 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2897 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2898 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2899 | return NULL; |
| 2900 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2901 | 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] | 2902 | } |
| 2903 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2904 | int i; |
| 2905 | asdl_seq *targets; |
| 2906 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2907 | expr_ty expression; |
| 2908 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2909 | /* a normal assignment */ |
| 2910 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2911 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2912 | if (!targets) |
| 2913 | return NULL; |
| 2914 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 2915 | expr_ty e; |
| 2916 | node *ch = CHILD(n, i); |
| 2917 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2918 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2919 | return NULL; |
| 2920 | } |
| 2921 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2923 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2924 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 2925 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2926 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2927 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2928 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2929 | asdl_seq_SET(targets, i / 2, e); |
| 2930 | } |
| 2931 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2932 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2933 | expression = ast_for_testlist(c, value); |
| 2934 | else |
| 2935 | expression = ast_for_expr(c, value); |
| 2936 | if (!expression) |
| 2937 | return NULL; |
| 2938 | 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] | 2939 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2942 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2944 | 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] | 2945 | { |
| 2946 | asdl_seq *seq; |
| 2947 | int i; |
| 2948 | expr_ty e; |
| 2949 | |
| 2950 | REQ(n, exprlist); |
| 2951 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2952 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2953 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2954 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2955 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2956 | e = ast_for_expr(c, CHILD(n, i)); |
| 2957 | if (!e) |
| 2958 | return NULL; |
| 2959 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2960 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2961 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2962 | } |
| 2963 | return seq; |
| 2964 | } |
| 2965 | |
| 2966 | static stmt_ty |
| 2967 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 2968 | { |
| 2969 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2971 | /* del_stmt: 'del' exprlist */ |
| 2972 | REQ(n, del_stmt); |
| 2973 | |
| 2974 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 2975 | if (!expr_list) |
| 2976 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2977 | 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] | 2978 | } |
| 2979 | |
| 2980 | static stmt_ty |
| 2981 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 2982 | { |
| 2983 | /* |
| 2984 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 2985 | | yield_stmt |
| 2986 | break_stmt: 'break' |
| 2987 | continue_stmt: 'continue' |
| 2988 | return_stmt: 'return' [testlist] |
| 2989 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2990 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2991 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 2992 | */ |
| 2993 | node *ch; |
| 2994 | |
| 2995 | REQ(n, flow_stmt); |
| 2996 | ch = CHILD(n, 0); |
| 2997 | switch (TYPE(ch)) { |
| 2998 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2999 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3001 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3002 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3003 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3004 | if (!exp) |
| 3005 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3006 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3007 | } |
| 3008 | case return_stmt: |
| 3009 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3010 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3011 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3012 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | if (!expression) |
| 3014 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3015 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3016 | } |
| 3017 | case raise_stmt: |
| 3018 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3019 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3020 | else if (NCH(ch) >= 2) { |
| 3021 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3022 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3023 | if (!expression) |
| 3024 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3025 | if (NCH(ch) == 4) { |
| 3026 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3027 | if (!cause) |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | 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] | 3031 | } |
| 3032 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3033 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3034 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3035 | return NULL; |
| 3036 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3037 | |
| 3038 | PyErr_SetString(PyExc_SystemError, "unhandled flow statement"); |
| 3039 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3043 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3044 | { |
| 3045 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3046 | import_as_name: NAME ['as' NAME] |
| 3047 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3048 | dotted_name: NAME ('.' NAME)* |
| 3049 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3050 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3051 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3052 | loop: |
| 3053 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3054 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3055 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3056 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3057 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3058 | if (!name) |
| 3059 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3060 | if (NCH(n) == 3) { |
| 3061 | node *str_node = CHILD(n, 2); |
| 3062 | str = NEW_IDENTIFIER(str_node); |
| 3063 | if (!str) |
| 3064 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3065 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3066 | return NULL; |
| 3067 | } |
| 3068 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3069 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3070 | return NULL; |
| 3071 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3072 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3073 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3074 | case dotted_as_name: |
| 3075 | if (NCH(n) == 1) { |
| 3076 | n = CHILD(n, 0); |
| 3077 | goto loop; |
| 3078 | } |
| 3079 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3080 | node *asname_node = CHILD(n, 2); |
| 3081 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3082 | if (!a) |
| 3083 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3084 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3085 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3086 | if (!a->asname) |
| 3087 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3088 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3089 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3090 | return a; |
| 3091 | } |
| 3092 | break; |
| 3093 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3094 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3095 | node *name_node = CHILD(n, 0); |
| 3096 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3097 | if (!name) |
| 3098 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3099 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3100 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3101 | return alias(name, NULL, c->c_arena); |
| 3102 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3103 | else { |
| 3104 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3105 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3106 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3107 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3109 | |
| 3110 | len = 0; |
| 3111 | for (i = 0; i < NCH(n); i += 2) |
| 3112 | /* length of string plus one for the dot */ |
| 3113 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3114 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3115 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3116 | if (!str) |
| 3117 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3118 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | if (!s) |
| 3120 | return NULL; |
| 3121 | for (i = 0; i < NCH(n); i += 2) { |
| 3122 | char *sch = STR(CHILD(n, i)); |
| 3123 | strcpy(s, STR(CHILD(n, i))); |
| 3124 | s += strlen(sch); |
| 3125 | *s++ = '.'; |
| 3126 | } |
| 3127 | --s; |
| 3128 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3130 | PyBytes_GET_SIZE(str), |
| 3131 | NULL); |
| 3132 | Py_DECREF(str); |
| 3133 | if (!uni) |
| 3134 | return NULL; |
| 3135 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3136 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3137 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3138 | Py_DECREF(str); |
| 3139 | return NULL; |
| 3140 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3141 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3142 | } |
| 3143 | break; |
| 3144 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3145 | str = PyUnicode_InternFromString("*"); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3146 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3147 | Py_DECREF(str); |
| 3148 | return NULL; |
| 3149 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3150 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3151 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3152 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3153 | "unexpected import name: %d", TYPE(n)); |
| 3154 | return NULL; |
| 3155 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3156 | |
| 3157 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3158 | return NULL; |
| 3159 | } |
| 3160 | |
| 3161 | static stmt_ty |
| 3162 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3163 | { |
| 3164 | /* |
| 3165 | import_stmt: import_name | import_from |
| 3166 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3167 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3168 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3169 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3170 | int lineno; |
| 3171 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3172 | int i; |
| 3173 | asdl_seq *aliases; |
| 3174 | |
| 3175 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3176 | lineno = LINENO(n); |
| 3177 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3178 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3179 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3180 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3181 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3182 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3183 | if (!aliases) |
| 3184 | return NULL; |
| 3185 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3186 | 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] | 3187 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3188 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3189 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3190 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3191 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3192 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3193 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3194 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3195 | int idx, ndots = 0; |
| 3196 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3197 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3199 | /* Count the number of dots (for relative imports) and check for the |
| 3200 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3201 | for (idx = 1; idx < NCH(n); idx++) { |
| 3202 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3203 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3204 | if (!mod) |
| 3205 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3206 | idx++; |
| 3207 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3208 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3210 | ndots += 3; |
| 3211 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3212 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3213 | break; |
| 3214 | } |
| 3215 | ndots++; |
| 3216 | } |
| 3217 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3218 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3219 | case STAR: |
| 3220 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3221 | n = CHILD(n, idx); |
| 3222 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3223 | break; |
| 3224 | case LPAR: |
| 3225 | /* from ... import (x, y, z) */ |
| 3226 | n = CHILD(n, idx + 1); |
| 3227 | n_children = NCH(n); |
| 3228 | break; |
| 3229 | case import_as_names: |
| 3230 | /* from ... import x, y, z */ |
| 3231 | n = CHILD(n, idx); |
| 3232 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3233 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3234 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3235 | " surrounding parentheses"); |
| 3236 | return NULL; |
| 3237 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3238 | break; |
| 3239 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3240 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3241 | return NULL; |
| 3242 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3243 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3244 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3245 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3246 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3247 | |
| 3248 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3249 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3250 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3251 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3252 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3253 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3254 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3255 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3256 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3257 | 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] | 3258 | if (!import_alias) |
| 3259 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3260 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3261 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3262 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3263 | if (mod != NULL) |
| 3264 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3265 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3266 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3267 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3268 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3269 | "unknown import statement: starts with command '%s'", |
| 3270 | STR(CHILD(n, 0))); |
| 3271 | return NULL; |
| 3272 | } |
| 3273 | |
| 3274 | static stmt_ty |
| 3275 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3276 | { |
| 3277 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3278 | identifier name; |
| 3279 | asdl_seq *s; |
| 3280 | int i; |
| 3281 | |
| 3282 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3283 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3284 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3285 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3286 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3287 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3288 | if (!name) |
| 3289 | return NULL; |
| 3290 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3291 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3292 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3296 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3297 | { |
| 3298 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3299 | identifier name; |
| 3300 | asdl_seq *s; |
| 3301 | int i; |
| 3302 | |
| 3303 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3304 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3305 | if (!s) |
| 3306 | return NULL; |
| 3307 | for (i = 1; i < NCH(n); i += 2) { |
| 3308 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3309 | if (!name) |
| 3310 | return NULL; |
| 3311 | asdl_seq_SET(s, i / 2, name); |
| 3312 | } |
| 3313 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3314 | } |
| 3315 | |
| 3316 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3317 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3318 | { |
| 3319 | /* assert_stmt: 'assert' test [',' test] */ |
| 3320 | REQ(n, assert_stmt); |
| 3321 | if (NCH(n) == 2) { |
| 3322 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3323 | if (!expression) |
| 3324 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3325 | 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] | 3326 | } |
| 3327 | else if (NCH(n) == 4) { |
| 3328 | expr_ty expr1, expr2; |
| 3329 | |
| 3330 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3331 | if (!expr1) |
| 3332 | return NULL; |
| 3333 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3334 | if (!expr2) |
| 3335 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3337 | 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] | 3338 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3339 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3340 | "improper number of parts to 'assert' statement: %d", |
| 3341 | NCH(n)); |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | |
| 3345 | static asdl_seq * |
| 3346 | ast_for_suite(struct compiling *c, const node *n) |
| 3347 | { |
| 3348 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3349 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3350 | stmt_ty s; |
| 3351 | int i, total, num, end, pos = 0; |
| 3352 | node *ch; |
| 3353 | |
| 3354 | REQ(n, suite); |
| 3355 | |
| 3356 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3357 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3358 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3359 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3360 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3361 | n = CHILD(n, 0); |
| 3362 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3364 | */ |
| 3365 | end = NCH(n) - 1; |
| 3366 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3367 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3368 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3369 | for (i = 0; i < end; i += 2) { |
| 3370 | ch = CHILD(n, i); |
| 3371 | s = ast_for_stmt(c, ch); |
| 3372 | if (!s) |
| 3373 | return NULL; |
| 3374 | asdl_seq_SET(seq, pos++, s); |
| 3375 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3376 | } |
| 3377 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3378 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3379 | ch = CHILD(n, i); |
| 3380 | REQ(ch, stmt); |
| 3381 | num = num_stmts(ch); |
| 3382 | if (num == 1) { |
| 3383 | /* small_stmt or compound_stmt with only one child */ |
| 3384 | s = ast_for_stmt(c, ch); |
| 3385 | if (!s) |
| 3386 | return NULL; |
| 3387 | asdl_seq_SET(seq, pos++, s); |
| 3388 | } |
| 3389 | else { |
| 3390 | int j; |
| 3391 | ch = CHILD(ch, 0); |
| 3392 | REQ(ch, simple_stmt); |
| 3393 | for (j = 0; j < NCH(ch); j += 2) { |
| 3394 | /* statement terminates with a semi-colon ';' */ |
| 3395 | if (NCH(CHILD(ch, j)) == 0) { |
| 3396 | assert((j + 1) == NCH(ch)); |
| 3397 | break; |
| 3398 | } |
| 3399 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3400 | if (!s) |
| 3401 | return NULL; |
| 3402 | asdl_seq_SET(seq, pos++, s); |
| 3403 | } |
| 3404 | } |
| 3405 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3406 | } |
| 3407 | assert(pos == seq->size); |
| 3408 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | static stmt_ty |
| 3412 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3413 | { |
| 3414 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3415 | ['else' ':' suite] |
| 3416 | */ |
| 3417 | char *s; |
| 3418 | |
| 3419 | REQ(n, if_stmt); |
| 3420 | |
| 3421 | if (NCH(n) == 4) { |
| 3422 | expr_ty expression; |
| 3423 | asdl_seq *suite_seq; |
| 3424 | |
| 3425 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3426 | if (!expression) |
| 3427 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3429 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3430 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3432 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3433 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3434 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3435 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | s = STR(CHILD(n, 4)); |
| 3437 | /* s[2], the third character in the string, will be |
| 3438 | 's' for el_s_e, or |
| 3439 | 'i' for el_i_f |
| 3440 | */ |
| 3441 | if (s[2] == 's') { |
| 3442 | expr_ty expression; |
| 3443 | asdl_seq *seq1, *seq2; |
| 3444 | |
| 3445 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3446 | if (!expression) |
| 3447 | return NULL; |
| 3448 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3449 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3450 | return NULL; |
| 3451 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3452 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3453 | return NULL; |
| 3454 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3455 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3456 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3457 | } |
| 3458 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3459 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3460 | expr_ty expression; |
| 3461 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3462 | asdl_seq *orelse = NULL; |
| 3463 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3464 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3465 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3466 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3467 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3468 | has_else = 1; |
| 3469 | n_elif -= 3; |
| 3470 | } |
| 3471 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3472 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3473 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3474 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3476 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3477 | if (!orelse) |
| 3478 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3479 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3480 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3481 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3482 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3483 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3484 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3485 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3486 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3487 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | asdl_seq_SET(orelse, 0, |
| 3490 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3491 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3492 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3493 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3494 | /* the just-created orelse handled the last elif */ |
| 3495 | n_elif--; |
| 3496 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3497 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3498 | for (i = 0; i < n_elif; i++) { |
| 3499 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3500 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3501 | if (!newobj) |
| 3502 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3503 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3504 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3505 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3506 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3507 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3508 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3509 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3510 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3512 | LINENO(CHILD(n, off)), |
| 3513 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3514 | orelse = newobj; |
| 3515 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3516 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3517 | if (!expression) |
| 3518 | return NULL; |
| 3519 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3520 | if (!suite_seq) |
| 3521 | return NULL; |
| 3522 | return If(expression, suite_seq, orelse, |
| 3523 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3524 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3525 | |
| 3526 | PyErr_Format(PyExc_SystemError, |
| 3527 | "unexpected token in 'if' statement: %s", s); |
| 3528 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | static stmt_ty |
| 3532 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3533 | { |
| 3534 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3535 | REQ(n, while_stmt); |
| 3536 | |
| 3537 | if (NCH(n) == 4) { |
| 3538 | expr_ty expression; |
| 3539 | asdl_seq *suite_seq; |
| 3540 | |
| 3541 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3542 | if (!expression) |
| 3543 | return NULL; |
| 3544 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3545 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3546 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3547 | 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] | 3548 | } |
| 3549 | else if (NCH(n) == 7) { |
| 3550 | expr_ty expression; |
| 3551 | asdl_seq *seq1, *seq2; |
| 3552 | |
| 3553 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3554 | if (!expression) |
| 3555 | return NULL; |
| 3556 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3557 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3558 | return NULL; |
| 3559 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3560 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3561 | return NULL; |
| 3562 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3563 | 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] | 3564 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3565 | |
| 3566 | PyErr_Format(PyExc_SystemError, |
| 3567 | "wrong number of tokens for 'while' statement: %d", |
| 3568 | NCH(n)); |
| 3569 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3573 | 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] | 3574 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3575 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3577 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3578 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3579 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3580 | REQ(n, for_stmt); |
| 3581 | |
| 3582 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3583 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | if (!seq) |
| 3585 | return NULL; |
| 3586 | } |
| 3587 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3588 | node_target = CHILD(n, 1); |
| 3589 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3590 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3591 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3592 | /* Check the # of children rather than the length of _target, since |
| 3593 | 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] | 3594 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3595 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3596 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3598 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3599 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3600 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3601 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | return NULL; |
| 3603 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3604 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3605 | return NULL; |
| 3606 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3607 | if (is_async) |
| 3608 | return AsyncFor(target, expression, suite_seq, seq, |
| 3609 | LINENO(n), n->n_col_offset, |
| 3610 | c->c_arena); |
| 3611 | else |
| 3612 | return For(target, expression, suite_seq, seq, |
| 3613 | LINENO(n), n->n_col_offset, |
| 3614 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | static excepthandler_ty |
| 3618 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3619 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3620 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3621 | REQ(exc, except_clause); |
| 3622 | REQ(body, suite); |
| 3623 | |
| 3624 | if (NCH(exc) == 1) { |
| 3625 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3626 | if (!suite_seq) |
| 3627 | return NULL; |
| 3628 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3629 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3630 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3631 | } |
| 3632 | else if (NCH(exc) == 2) { |
| 3633 | expr_ty expression; |
| 3634 | asdl_seq *suite_seq; |
| 3635 | |
| 3636 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3637 | if (!expression) |
| 3638 | return NULL; |
| 3639 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3640 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3641 | return NULL; |
| 3642 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3643 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3644 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | } |
| 3646 | else if (NCH(exc) == 4) { |
| 3647 | asdl_seq *suite_seq; |
| 3648 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3649 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3650 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3651 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3652 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3653 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3654 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3655 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3656 | return NULL; |
| 3657 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3658 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3659 | return NULL; |
| 3660 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3661 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3662 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3663 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3664 | |
| 3665 | PyErr_Format(PyExc_SystemError, |
| 3666 | "wrong number of children for 'except' clause: %d", |
| 3667 | NCH(exc)); |
| 3668 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | static stmt_ty |
| 3672 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3673 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3674 | const int nch = NCH(n); |
| 3675 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3676 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3677 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3678 | REQ(n, try_stmt); |
| 3679 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3680 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3681 | if (body == NULL) |
| 3682 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3683 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3684 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3685 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3686 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3687 | /* we can assume it's an "else", |
| 3688 | because nch >= 9 for try-else-finally and |
| 3689 | it would otherwise have a type of except_clause */ |
| 3690 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3691 | if (orelse == NULL) |
| 3692 | return NULL; |
| 3693 | n_except--; |
| 3694 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3695 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3696 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3697 | if (finally == NULL) |
| 3698 | return NULL; |
| 3699 | n_except--; |
| 3700 | } |
| 3701 | else { |
| 3702 | /* we can assume it's an "else", |
| 3703 | otherwise it would have a type of except_clause */ |
| 3704 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3705 | if (orelse == NULL) |
| 3706 | return NULL; |
| 3707 | n_except--; |
| 3708 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3709 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3710 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3711 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3712 | return NULL; |
| 3713 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3715 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3716 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3717 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3718 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3719 | if (handlers == NULL) |
| 3720 | return NULL; |
| 3721 | |
| 3722 | for (i = 0; i < n_except; i++) { |
| 3723 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3724 | CHILD(n, 5 + i * 3)); |
| 3725 | if (!e) |
| 3726 | return NULL; |
| 3727 | asdl_seq_SET(handlers, i, e); |
| 3728 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3731 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3732 | 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] | 3733 | } |
| 3734 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3735 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3736 | static withitem_ty |
| 3737 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3738 | { |
| 3739 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3740 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3741 | REQ(n, with_item); |
| 3742 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3743 | if (!context_expr) |
| 3744 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3745 | if (NCH(n) == 3) { |
| 3746 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3747 | |
| 3748 | if (!optional_vars) { |
| 3749 | return NULL; |
| 3750 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3751 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3752 | return NULL; |
| 3753 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3756 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3759 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3760 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3761 | 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] | 3762 | { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3763 | int i, n_items; |
| 3764 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3765 | |
| 3766 | REQ(n, with_stmt); |
| 3767 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3768 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3769 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3770 | if (!items) |
| 3771 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3772 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3773 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3774 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3775 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3776 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3779 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3780 | if (!body) |
| 3781 | return NULL; |
| 3782 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3783 | if (is_async) |
| 3784 | return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
| 3785 | else |
| 3786 | 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] | 3787 | } |
| 3788 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3790 | 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] | 3791 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3792 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3793 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3794 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3795 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3796 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3797 | REQ(n, classdef); |
| 3798 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3799 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3800 | s = ast_for_suite(c, CHILD(n, 3)); |
| 3801 | if (!s) |
| 3802 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3803 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3804 | if (!classname) |
| 3805 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3806 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3807 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3808 | return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n), |
| 3809 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3810 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3811 | |
| 3812 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3813 | s = ast_for_suite(c, CHILD(n,5)); |
| 3814 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3815 | return NULL; |
| 3816 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3817 | if (!classname) |
| 3818 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3819 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3820 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3821 | return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n), |
| 3822 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3825 | /* class NAME '(' arglist ')' ':' suite */ |
| 3826 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3827 | { |
| 3828 | PyObject *dummy_name; |
| 3829 | expr_ty dummy; |
| 3830 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3831 | if (!dummy_name) |
| 3832 | return NULL; |
| 3833 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 3834 | call = ast_for_call(c, CHILD(n, 3), dummy); |
| 3835 | if (!call) |
| 3836 | return NULL; |
| 3837 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3838 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3839 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3840 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3841 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3842 | if (!classname) |
| 3843 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3844 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3845 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3846 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3847 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3848 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | static stmt_ty |
| 3852 | ast_for_stmt(struct compiling *c, const node *n) |
| 3853 | { |
| 3854 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3855 | assert(NCH(n) == 1); |
| 3856 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3857 | } |
| 3858 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3859 | assert(num_stmts(n) == 1); |
| 3860 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3861 | } |
| 3862 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3863 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3864 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3865 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3866 | */ |
| 3867 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3868 | case expr_stmt: |
| 3869 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3870 | case del_stmt: |
| 3871 | return ast_for_del_stmt(c, n); |
| 3872 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3873 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3874 | case flow_stmt: |
| 3875 | return ast_for_flow_stmt(c, n); |
| 3876 | case import_stmt: |
| 3877 | return ast_for_import_stmt(c, n); |
| 3878 | case global_stmt: |
| 3879 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3880 | case nonlocal_stmt: |
| 3881 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3882 | case assert_stmt: |
| 3883 | return ast_for_assert_stmt(c, n); |
| 3884 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3885 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3886 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3887 | TYPE(n), NCH(n)); |
| 3888 | return NULL; |
| 3889 | } |
| 3890 | } |
| 3891 | else { |
| 3892 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3893 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3894 | */ |
| 3895 | node *ch = CHILD(n, 0); |
| 3896 | REQ(n, compound_stmt); |
| 3897 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3898 | case if_stmt: |
| 3899 | return ast_for_if_stmt(c, ch); |
| 3900 | case while_stmt: |
| 3901 | return ast_for_while_stmt(c, ch); |
| 3902 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3903 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3904 | case try_stmt: |
| 3905 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3906 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3907 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3908 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3909 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3910 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3911 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | case decorated: |
| 3913 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3914 | case async_stmt: |
| 3915 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3916 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3917 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3918 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3919 | TYPE(n), NCH(n)); |
| 3920 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3921 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3926 | parsenumber(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3927 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3928 | const char *end; |
| 3929 | long x; |
| 3930 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3931 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3932 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3933 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 3934 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3935 | errno = 0; |
| 3936 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3937 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3938 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3939 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3940 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3941 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3942 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3943 | } |
| 3944 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3945 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3946 | if (*end == '\0') { |
| 3947 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3948 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3949 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3950 | } |
| 3951 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3952 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3953 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3954 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 3955 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 3956 | return NULL; |
| 3957 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3958 | } |
| 3959 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3960 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3961 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 3962 | if (dx == -1.0 && PyErr_Occurred()) |
| 3963 | return NULL; |
| 3964 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3965 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3969 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3970 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3971 | const char *s, *t; |
| 3972 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3973 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 3974 | while (s < end && (*s & 0x80)) s++; |
| 3975 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3976 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | static PyObject * |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 3980 | decode_unicode_with_escapes(struct compiling *c, const char *s, size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3981 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3982 | PyObject *v, *u; |
| 3983 | char *buf; |
| 3984 | char *p; |
| 3985 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3986 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 3987 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 3988 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 3989 | return NULL; |
| 3990 | /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 3991 | "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 3992 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 3993 | if (u == NULL) |
| 3994 | return NULL; |
| 3995 | p = buf = PyBytes_AsString(u); |
| 3996 | end = s + len; |
| 3997 | while (s < end) { |
| 3998 | if (*s == '\\') { |
| 3999 | *p++ = *s++; |
| 4000 | if (*s & 0x80) { |
| 4001 | strcpy(p, "u005c"); |
| 4002 | p += 5; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4003 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4004 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4005 | if (*s & 0x80) { /* XXX inefficient */ |
| 4006 | PyObject *w; |
| 4007 | int kind; |
| 4008 | void *data; |
| 4009 | Py_ssize_t len, i; |
| 4010 | w = decode_utf8(c, &s, end); |
| 4011 | if (w == NULL) { |
| 4012 | Py_DECREF(u); |
| 4013 | return NULL; |
| 4014 | } |
| 4015 | kind = PyUnicode_KIND(w); |
| 4016 | data = PyUnicode_DATA(w); |
| 4017 | len = PyUnicode_GET_LENGTH(w); |
| 4018 | for (i = 0; i < len; i++) { |
| 4019 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4020 | sprintf(p, "\\U%08x", chr); |
| 4021 | p += 10; |
| 4022 | } |
| 4023 | /* Should be impossible to overflow */ |
| 4024 | assert(p - buf <= Py_SIZE(u)); |
| 4025 | Py_DECREF(w); |
| 4026 | } else { |
| 4027 | *p++ = *s++; |
| 4028 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4029 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4030 | len = p - buf; |
| 4031 | s = buf; |
| 4032 | |
Eric V. Smith | 5567f89 | 2015-09-21 13:36:09 -0400 | [diff] [blame] | 4033 | v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4034 | Py_XDECREF(u); |
| 4035 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4038 | /* Compile this expression in to an expr_ty. We know that we can |
| 4039 | temporarily modify the character before the start of this string |
| 4040 | (it's '{'), and we know we can temporarily modify the character |
| 4041 | after this string (it is a '}'). Leverage this to create a |
| 4042 | sub-string with enough room for us to add parens around the |
| 4043 | expression. This is to allow strings with embedded newlines, for |
| 4044 | example. */ |
| 4045 | static expr_ty |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4046 | fstring_compile_expr(PyObject *str, Py_ssize_t expr_start, |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4047 | Py_ssize_t expr_end, struct compiling *c, const node *n) |
| 4048 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4049 | { |
| 4050 | PyCompilerFlags cf; |
| 4051 | mod_ty mod; |
| 4052 | char *utf_expr; |
| 4053 | Py_ssize_t i; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4054 | Py_UCS4 end_ch = -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4055 | int all_whitespace; |
| 4056 | PyObject *sub = NULL; |
| 4057 | |
| 4058 | /* We only decref sub if we allocated it with a PyUnicode_Substring. |
| 4059 | decref_sub records that. */ |
| 4060 | int decref_sub = 0; |
| 4061 | |
| 4062 | assert(str); |
| 4063 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4064 | assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str)); |
| 4065 | assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str)); |
| 4066 | assert(expr_end >= expr_start); |
| 4067 | |
Martin Panter | c2432f6 | 2015-10-07 11:15:15 +0000 | [diff] [blame] | 4068 | /* There has to be at least one character on each side of the |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4069 | expression inside this str. This will have been caught before |
| 4070 | we're called. */ |
| 4071 | assert(expr_start >= 1); |
| 4072 | assert(expr_end <= PyUnicode_GET_LENGTH(str)-1); |
| 4073 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4074 | /* If the substring is all whitespace, it's an error. We need to |
| 4075 | catch this here, and not when we call PyParser_ASTFromString, |
| 4076 | because turning the expression '' in to '()' would go from |
| 4077 | being invalid to valid. */ |
| 4078 | /* Note that this code says an empty string is all |
| 4079 | whitespace. That's important. There's a test for it: f'{}'. */ |
| 4080 | all_whitespace = 1; |
| 4081 | for (i = expr_start; i < expr_end; i++) { |
| 4082 | if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) { |
| 4083 | all_whitespace = 0; |
| 4084 | break; |
| 4085 | } |
| 4086 | } |
| 4087 | if (all_whitespace) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4088 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4089 | goto error; |
| 4090 | } |
| 4091 | |
| 4092 | /* If the substring will be the entire source string, we can't use |
| 4093 | PyUnicode_Substring, since it will return another reference to |
| 4094 | our original string. Because we're modifying the string in |
| 4095 | place, that's a no-no. So, detect that case and just use our |
| 4096 | string directly. */ |
| 4097 | |
| 4098 | if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) { |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4099 | /* If str is well formed, then the first and last chars must |
| 4100 | be '{' and '}', respectively. But, if there's a syntax |
| 4101 | error, for example f'{3!', then the last char won't be a |
| 4102 | closing brace. So, remember the last character we read in |
| 4103 | order for us to restore it. */ |
| 4104 | end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1); |
| 4105 | assert(end_ch != (Py_UCS4)-1); |
| 4106 | |
| 4107 | /* In all cases, however, start_ch must be '{'. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4108 | assert(PyUnicode_ReadChar(str, 0) == '{'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4109 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4110 | sub = str; |
| 4111 | } else { |
| 4112 | /* Create a substring object. It must be a new object, with |
| 4113 | refcount==1, so that we can modify it. */ |
| 4114 | sub = PyUnicode_Substring(str, expr_start-1, expr_end+1); |
| 4115 | if (!sub) |
| 4116 | goto error; |
| 4117 | assert(sub != str); /* Make sure it's a new string. */ |
| 4118 | decref_sub = 1; /* Remember to deallocate it on error. */ |
| 4119 | } |
| 4120 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4121 | /* Put () around the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4122 | if (PyUnicode_WriteChar(sub, 0, '(') < 0 || |
| 4123 | PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0) |
| 4124 | goto error; |
| 4125 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4126 | /* No need to free the memory returned here: it's managed by the |
| 4127 | string. */ |
| 4128 | utf_expr = PyUnicode_AsUTF8(sub); |
| 4129 | if (!utf_expr) |
| 4130 | goto error; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4131 | |
| 4132 | cf.cf_flags = PyCF_ONLY_AST; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4133 | mod = PyParser_ASTFromString(utf_expr, "<fstring>", |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4134 | Py_eval_input, &cf, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4135 | if (!mod) |
| 4136 | goto error; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4137 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4138 | if (sub != str) |
| 4139 | /* Clear instead of decref in case we ever modify this code to change |
| 4140 | the error handling: this is safest because the XDECREF won't try |
| 4141 | and decref it when it's NULL. */ |
| 4142 | /* No need to restore the chars in sub, since we know it's getting |
| 4143 | ready to get deleted (refcount must be 1, since we got a new string |
| 4144 | in PyUnicode_Substring). */ |
| 4145 | Py_CLEAR(sub); |
| 4146 | else { |
| 4147 | assert(!decref_sub); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4148 | assert(end_ch != (Py_UCS4)-1); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4149 | /* Restore str, which we earlier modified directly. */ |
| 4150 | if (PyUnicode_WriteChar(str, 0, '{') < 0 || |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4151 | PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4152 | goto error; |
| 4153 | } |
| 4154 | return mod->v.Expression.body; |
| 4155 | |
| 4156 | error: |
| 4157 | /* Only decref sub if it was the result of a call to SubString. */ |
| 4158 | if (decref_sub) |
| 4159 | Py_XDECREF(sub); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4160 | |
| 4161 | if (end_ch != (Py_UCS4)-1) { |
| 4162 | /* We only get here if we modified str. Make sure that's the |
| 4163 | case: str will be equal to sub. */ |
| 4164 | if (str == sub) { |
| 4165 | /* Don't check the error, because we've already set the |
| 4166 | error state (that's why we're in 'error', after |
| 4167 | all). */ |
| 4168 | PyUnicode_WriteChar(str, 0, '{'); |
| 4169 | PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch); |
| 4170 | } |
| 4171 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4172 | return NULL; |
| 4173 | } |
| 4174 | |
| 4175 | /* Return -1 on error. |
| 4176 | |
| 4177 | Return 0 if we reached the end of the literal. |
| 4178 | |
| 4179 | Return 1 if we haven't reached the end of the literal, but we want |
| 4180 | the caller to process the literal up to this point. Used for |
| 4181 | doubled braces. |
| 4182 | */ |
| 4183 | static int |
| 4184 | fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal, |
| 4185 | int recurse_lvl, struct compiling *c, const node *n) |
| 4186 | { |
| 4187 | /* Get any literal string. It ends when we hit an un-doubled brace, or the |
| 4188 | end of the string. */ |
| 4189 | |
| 4190 | Py_ssize_t literal_start, literal_end; |
| 4191 | int result = 0; |
| 4192 | |
| 4193 | enum PyUnicode_Kind kind = PyUnicode_KIND(str); |
| 4194 | void *data = PyUnicode_DATA(str); |
| 4195 | |
| 4196 | assert(*literal == NULL); |
| 4197 | |
| 4198 | literal_start = *ofs; |
| 4199 | for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) { |
| 4200 | Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs); |
| 4201 | if (ch == '{' || ch == '}') { |
| 4202 | /* Check for doubled braces, but only at the top level. If |
| 4203 | we checked at every level, then f'{0:{3}}' would fail |
| 4204 | with the two closing braces. */ |
| 4205 | if (recurse_lvl == 0) { |
| 4206 | if (*ofs + 1 < PyUnicode_GET_LENGTH(str) && |
| 4207 | PyUnicode_READ(kind, data, *ofs + 1) == ch) { |
| 4208 | /* We're going to tell the caller that the literal ends |
| 4209 | here, but that they should continue scanning. But also |
| 4210 | skip over the second brace when we resume scanning. */ |
| 4211 | literal_end = *ofs + 1; |
| 4212 | *ofs += 2; |
| 4213 | result = 1; |
| 4214 | goto done; |
| 4215 | } |
| 4216 | |
| 4217 | /* Where a single '{' is the start of a new expression, a |
| 4218 | single '}' is not allowed. */ |
| 4219 | if (ch == '}') { |
| 4220 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4221 | return -1; |
| 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | /* We're either at a '{', which means we're starting another |
| 4226 | expression; or a '}', which means we're at the end of this |
| 4227 | f-string (for a nested format_spec). */ |
| 4228 | break; |
| 4229 | } |
| 4230 | } |
| 4231 | literal_end = *ofs; |
| 4232 | |
| 4233 | assert(*ofs == PyUnicode_GET_LENGTH(str) || |
| 4234 | PyUnicode_READ(kind, data, *ofs) == '{' || |
| 4235 | PyUnicode_READ(kind, data, *ofs) == '}'); |
| 4236 | done: |
| 4237 | if (literal_start != literal_end) { |
| 4238 | *literal = PyUnicode_Substring(str, literal_start, literal_end); |
| 4239 | if (!*literal) |
| 4240 | return -1; |
| 4241 | } |
| 4242 | |
| 4243 | return result; |
| 4244 | } |
| 4245 | |
| 4246 | /* Forward declaration because parsing is recursive. */ |
| 4247 | static expr_ty |
| 4248 | fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl, |
| 4249 | struct compiling *c, const node *n); |
| 4250 | |
| 4251 | /* Parse the f-string str, starting at ofs. We know *ofs starts an |
| 4252 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4253 | which includes the expression, conversion character, and |
| 4254 | format_spec expression. |
| 4255 | |
| 4256 | Note that I don't do a perfect job here: I don't make sure that a |
| 4257 | closing brace doesn't match an opening paren, for example. It |
| 4258 | doesn't need to error on all invalid expressions, just correctly |
| 4259 | find the end of all valid ones. Any errors inside the expression |
| 4260 | will be caught when we parse it later. */ |
| 4261 | static int |
| 4262 | fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl, |
| 4263 | expr_ty *expression, struct compiling *c, const node *n) |
| 4264 | { |
| 4265 | /* Return -1 on error, else 0. */ |
| 4266 | |
| 4267 | Py_ssize_t expr_start; |
| 4268 | Py_ssize_t expr_end; |
| 4269 | expr_ty simple_expression; |
| 4270 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
| 4271 | Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */ |
| 4272 | |
| 4273 | enum PyUnicode_Kind kind = PyUnicode_KIND(str); |
| 4274 | void *data = PyUnicode_DATA(str); |
| 4275 | |
| 4276 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4277 | match (single or double quote). */ |
| 4278 | Py_UCS4 quote_char = 0; |
| 4279 | |
| 4280 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4281 | int string_type = 0; |
| 4282 | |
| 4283 | /* Keep track of nesting level for braces/parens/brackets in |
| 4284 | expressions. */ |
| 4285 | Py_ssize_t nested_depth = 0; |
| 4286 | |
| 4287 | /* Can only nest one level deep. */ |
| 4288 | if (recurse_lvl >= 2) { |
| 4289 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4290 | return -1; |
| 4291 | } |
| 4292 | |
| 4293 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4294 | here. Skip over it. */ |
| 4295 | assert(PyUnicode_READ(kind, data, *ofs) == '{'); |
| 4296 | *ofs += 1; |
| 4297 | |
| 4298 | expr_start = *ofs; |
| 4299 | for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) { |
| 4300 | Py_UCS4 ch; |
| 4301 | |
| 4302 | /* Loop invariants. */ |
| 4303 | assert(nested_depth >= 0); |
| 4304 | assert(*ofs >= expr_start); |
| 4305 | if (quote_char) |
| 4306 | assert(string_type == 1 || string_type == 3); |
| 4307 | else |
| 4308 | assert(string_type == 0); |
| 4309 | |
| 4310 | ch = PyUnicode_READ(kind, data, *ofs); |
| 4311 | if (quote_char) { |
| 4312 | /* We're inside a string. See if we're at the end. */ |
| 4313 | /* This code needs to implement the same non-error logic |
| 4314 | as tok_get from tokenizer.c, at the letter_quote |
| 4315 | label. To actually share that code would be a |
| 4316 | nightmare. But, it's unlikely to change and is small, |
| 4317 | so duplicate it here. Note we don't need to catch all |
| 4318 | of the errors, since they'll be caught when parsing the |
| 4319 | expression. We just need to match the non-error |
| 4320 | cases. Thus we can ignore \n in single-quoted strings, |
| 4321 | for example. Or non-terminated strings. */ |
| 4322 | if (ch == quote_char) { |
| 4323 | /* Does this match the string_type (single or triple |
| 4324 | quoted)? */ |
| 4325 | if (string_type == 3) { |
| 4326 | if (*ofs+2 < PyUnicode_GET_LENGTH(str) && |
| 4327 | PyUnicode_READ(kind, data, *ofs+1) == ch && |
| 4328 | PyUnicode_READ(kind, data, *ofs+2) == ch) { |
| 4329 | /* We're at the end of a triple quoted string. */ |
| 4330 | *ofs += 2; |
| 4331 | string_type = 0; |
| 4332 | quote_char = 0; |
| 4333 | continue; |
| 4334 | } |
| 4335 | } else { |
| 4336 | /* We're at the end of a normal string. */ |
| 4337 | quote_char = 0; |
| 4338 | string_type = 0; |
| 4339 | continue; |
| 4340 | } |
| 4341 | } |
| 4342 | /* We're inside a string, and not finished with the |
| 4343 | string. If this is a backslash, skip the next char (it |
| 4344 | might be an end quote that needs skipping). Otherwise, |
| 4345 | just consume this character normally. */ |
| 4346 | if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) { |
| 4347 | /* Just skip the next char, whatever it is. */ |
| 4348 | *ofs += 1; |
| 4349 | } |
| 4350 | } else if (ch == '\'' || ch == '"') { |
| 4351 | /* Is this a triple quoted string? */ |
| 4352 | if (*ofs+2 < PyUnicode_GET_LENGTH(str) && |
| 4353 | PyUnicode_READ(kind, data, *ofs+1) == ch && |
| 4354 | PyUnicode_READ(kind, data, *ofs+2) == ch) { |
| 4355 | string_type = 3; |
| 4356 | *ofs += 2; |
| 4357 | } else { |
| 4358 | /* Start of a normal string. */ |
| 4359 | string_type = 1; |
| 4360 | } |
| 4361 | /* Start looking for the end of the string. */ |
| 4362 | quote_char = ch; |
| 4363 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4364 | nested_depth++; |
| 4365 | } else if (nested_depth != 0 && |
| 4366 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4367 | nested_depth--; |
| 4368 | } else if (ch == '#') { |
| 4369 | /* Error: can't include a comment character, inside parens |
| 4370 | or not. */ |
| 4371 | ast_error(c, n, "f-string cannot include '#'"); |
| 4372 | return -1; |
| 4373 | } else if (nested_depth == 0 && |
| 4374 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4375 | /* First, test for the special case of "!=". Since '=' is |
| 4376 | not an allowed conversion character, nothing is lost in |
| 4377 | this test. */ |
| 4378 | if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) && |
| 4379 | PyUnicode_READ(kind, data, *ofs+1) == '=') |
| 4380 | /* This isn't a conversion character, just continue. */ |
| 4381 | continue; |
| 4382 | |
| 4383 | /* Normal way out of this loop. */ |
| 4384 | break; |
| 4385 | } else { |
| 4386 | /* Just consume this char and loop around. */ |
| 4387 | } |
| 4388 | } |
| 4389 | expr_end = *ofs; |
| 4390 | /* If we leave this loop in a string or with mismatched parens, we |
| 4391 | don't care. We'll get a syntax error when compiling the |
| 4392 | expression. But, we can produce a better error message, so |
| 4393 | let's just do that.*/ |
| 4394 | if (quote_char) { |
| 4395 | ast_error(c, n, "f-string: unterminated string"); |
| 4396 | return -1; |
| 4397 | } |
| 4398 | if (nested_depth) { |
| 4399 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4400 | return -1; |
| 4401 | } |
| 4402 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4403 | if (*ofs >= PyUnicode_GET_LENGTH(str)) |
| 4404 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4405 | |
| 4406 | /* Compile the expression as soon as possible, so we show errors |
| 4407 | related to the expression before errors related to the |
| 4408 | conversion or format_spec. */ |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4409 | simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4410 | if (!simple_expression) |
| 4411 | return -1; |
| 4412 | |
| 4413 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4414 | if (PyUnicode_READ(kind, data, *ofs) == '!') { |
| 4415 | *ofs += 1; |
| 4416 | if (*ofs >= PyUnicode_GET_LENGTH(str)) |
| 4417 | goto unexpected_end_of_string; |
| 4418 | |
| 4419 | conversion = PyUnicode_READ(kind, data, *ofs); |
| 4420 | *ofs += 1; |
| 4421 | |
| 4422 | /* Validate the conversion. */ |
| 4423 | if (!(conversion == 's' || conversion == 'r' |
| 4424 | || conversion == 'a')) { |
| 4425 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4426 | "expected 's', 'r', or 'a'"); |
| 4427 | return -1; |
| 4428 | } |
| 4429 | } |
| 4430 | |
| 4431 | /* Check for the format spec, if present. */ |
| 4432 | if (*ofs >= PyUnicode_GET_LENGTH(str)) |
| 4433 | goto unexpected_end_of_string; |
| 4434 | if (PyUnicode_READ(kind, data, *ofs) == ':') { |
| 4435 | *ofs += 1; |
| 4436 | if (*ofs >= PyUnicode_GET_LENGTH(str)) |
| 4437 | goto unexpected_end_of_string; |
| 4438 | |
| 4439 | /* Parse the format spec. */ |
| 4440 | format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n); |
| 4441 | if (!format_spec) |
| 4442 | return -1; |
| 4443 | } |
| 4444 | |
| 4445 | if (*ofs >= PyUnicode_GET_LENGTH(str) || |
| 4446 | PyUnicode_READ(kind, data, *ofs) != '}') |
| 4447 | goto unexpected_end_of_string; |
| 4448 | |
| 4449 | /* We're at a right brace. Consume it. */ |
| 4450 | assert(*ofs < PyUnicode_GET_LENGTH(str)); |
| 4451 | assert(PyUnicode_READ(kind, data, *ofs) == '}'); |
| 4452 | *ofs += 1; |
| 4453 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4454 | /* And now create the FormattedValue node that represents this entire |
| 4455 | expression with the conversion and format spec. */ |
| 4456 | *expression = FormattedValue(simple_expression, (int)conversion, |
| 4457 | format_spec, LINENO(n), n->n_col_offset, |
| 4458 | c->c_arena); |
| 4459 | if (!*expression) |
| 4460 | return -1; |
| 4461 | |
| 4462 | return 0; |
| 4463 | |
| 4464 | unexpected_end_of_string: |
| 4465 | ast_error(c, n, "f-string: expecting '}'"); |
| 4466 | return -1; |
| 4467 | } |
| 4468 | |
| 4469 | /* Return -1 on error. |
| 4470 | |
| 4471 | Return 0 if we have a literal (possible zero length) and an |
| 4472 | expression (zero length if at the end of the string. |
| 4473 | |
| 4474 | Return 1 if we have a literal, but no expression, and we want the |
| 4475 | caller to call us again. This is used to deal with doubled |
| 4476 | braces. |
| 4477 | |
| 4478 | When called multiple times on the string 'a{{b{0}c', this function |
| 4479 | will return: |
| 4480 | |
| 4481 | 1. the literal 'a{' with no expression, and a return value |
| 4482 | of 1. Despite the fact that there's no expression, the return |
| 4483 | value of 1 means we're not finished yet. |
| 4484 | |
| 4485 | 2. the literal 'b' and the expression '0', with a return value of |
| 4486 | 0. The fact that there's an expression means we're not finished. |
| 4487 | |
| 4488 | 3. literal 'c' with no expression and a return value of 0. The |
| 4489 | combination of the return value of 0 with no expression means |
| 4490 | we're finished. |
| 4491 | */ |
| 4492 | static int |
| 4493 | fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl, |
| 4494 | PyObject **literal, expr_ty *expression, |
| 4495 | struct compiling *c, const node *n) |
| 4496 | { |
| 4497 | int result; |
| 4498 | |
| 4499 | assert(*literal == NULL && *expression == NULL); |
| 4500 | |
| 4501 | /* Get any literal string. */ |
| 4502 | result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n); |
| 4503 | if (result < 0) |
| 4504 | goto error; |
| 4505 | |
| 4506 | assert(result == 0 || result == 1); |
| 4507 | |
| 4508 | if (result == 1) |
| 4509 | /* We have a literal, but don't look at the expression. */ |
| 4510 | return 1; |
| 4511 | |
| 4512 | assert(*ofs <= PyUnicode_GET_LENGTH(str)); |
| 4513 | |
| 4514 | if (*ofs >= PyUnicode_GET_LENGTH(str) || |
| 4515 | PyUnicode_READ_CHAR(str, *ofs) == '}') |
| 4516 | /* We're at the end of the string or the end of a nested |
| 4517 | f-string: no expression. The top-level error case where we |
| 4518 | expect to be at the end of the string but we're at a '}' is |
| 4519 | handled later. */ |
| 4520 | return 0; |
| 4521 | |
| 4522 | /* We must now be the start of an expression, on a '{'. */ |
| 4523 | assert(*ofs < PyUnicode_GET_LENGTH(str) && |
| 4524 | PyUnicode_READ_CHAR(str, *ofs) == '{'); |
| 4525 | |
| 4526 | if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0) |
| 4527 | goto error; |
| 4528 | |
| 4529 | return 0; |
| 4530 | |
| 4531 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4532 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4533 | return -1; |
| 4534 | } |
| 4535 | |
| 4536 | #define EXPRLIST_N_CACHED 64 |
| 4537 | |
| 4538 | typedef struct { |
| 4539 | /* Incrementally build an array of expr_ty, so be used in an |
| 4540 | asdl_seq. Cache some small but reasonably sized number of |
| 4541 | expr_ty's, and then after that start dynamically allocating, |
| 4542 | doubling the number allocated each time. Note that the f-string |
| 4543 | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
| 4544 | Str for the literal 'a'. So you add expr_ty's about twice as |
| 4545 | fast as you add exressions in an f-string. */ |
| 4546 | |
| 4547 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4548 | Py_ssize_t size; /* Number we've used. */ |
| 4549 | expr_ty *p; /* Pointer to the memory we're actually |
| 4550 | using. Will point to 'data' until we |
| 4551 | start dynamically allocating. */ |
| 4552 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4553 | } ExprList; |
| 4554 | |
| 4555 | #ifdef NDEBUG |
| 4556 | #define ExprList_check_invariants(l) |
| 4557 | #else |
| 4558 | static void |
| 4559 | ExprList_check_invariants(ExprList *l) |
| 4560 | { |
| 4561 | /* Check our invariants. Make sure this object is "live", and |
| 4562 | hasn't been deallocated. */ |
| 4563 | assert(l->size >= 0); |
| 4564 | assert(l->p != NULL); |
| 4565 | if (l->size <= EXPRLIST_N_CACHED) |
| 4566 | assert(l->data == l->p); |
| 4567 | } |
| 4568 | #endif |
| 4569 | |
| 4570 | static void |
| 4571 | ExprList_Init(ExprList *l) |
| 4572 | { |
| 4573 | l->allocated = EXPRLIST_N_CACHED; |
| 4574 | l->size = 0; |
| 4575 | |
| 4576 | /* Until we start allocating dynamically, p points to data. */ |
| 4577 | l->p = l->data; |
| 4578 | |
| 4579 | ExprList_check_invariants(l); |
| 4580 | } |
| 4581 | |
| 4582 | static int |
| 4583 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4584 | { |
| 4585 | ExprList_check_invariants(l); |
| 4586 | if (l->size >= l->allocated) { |
| 4587 | /* We need to alloc (or realloc) the memory. */ |
| 4588 | Py_ssize_t new_size = l->allocated * 2; |
| 4589 | |
| 4590 | /* See if we've ever allocated anything dynamically. */ |
| 4591 | if (l->p == l->data) { |
| 4592 | Py_ssize_t i; |
| 4593 | /* We're still using the cached data. Switch to |
| 4594 | alloc-ing. */ |
| 4595 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4596 | if (!l->p) |
| 4597 | return -1; |
| 4598 | /* Copy the cached data into the new buffer. */ |
| 4599 | for (i = 0; i < l->size; i++) |
| 4600 | l->p[i] = l->data[i]; |
| 4601 | } else { |
| 4602 | /* Just realloc. */ |
| 4603 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4604 | if (!tmp) { |
| 4605 | PyMem_RawFree(l->p); |
| 4606 | l->p = NULL; |
| 4607 | return -1; |
| 4608 | } |
| 4609 | l->p = tmp; |
| 4610 | } |
| 4611 | |
| 4612 | l->allocated = new_size; |
| 4613 | assert(l->allocated == 2 * l->size); |
| 4614 | } |
| 4615 | |
| 4616 | l->p[l->size++] = exp; |
| 4617 | |
| 4618 | ExprList_check_invariants(l); |
| 4619 | return 0; |
| 4620 | } |
| 4621 | |
| 4622 | static void |
| 4623 | ExprList_Dealloc(ExprList *l) |
| 4624 | { |
| 4625 | ExprList_check_invariants(l); |
| 4626 | |
| 4627 | /* If there's been an error, or we've never dynamically allocated, |
| 4628 | do nothing. */ |
| 4629 | if (!l->p || l->p == l->data) { |
| 4630 | /* Do nothing. */ |
| 4631 | } else { |
| 4632 | /* We have dynamically allocated. Free the memory. */ |
| 4633 | PyMem_RawFree(l->p); |
| 4634 | } |
| 4635 | l->p = NULL; |
| 4636 | l->size = -1; |
| 4637 | } |
| 4638 | |
| 4639 | static asdl_seq * |
| 4640 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4641 | { |
| 4642 | asdl_seq *seq; |
| 4643 | |
| 4644 | ExprList_check_invariants(l); |
| 4645 | |
| 4646 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4647 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4648 | if (seq) { |
| 4649 | Py_ssize_t i; |
| 4650 | for (i = 0; i < l->size; i++) |
| 4651 | asdl_seq_SET(seq, i, l->p[i]); |
| 4652 | } |
| 4653 | ExprList_Dealloc(l); |
| 4654 | return seq; |
| 4655 | } |
| 4656 | |
| 4657 | /* The FstringParser is designed to add a mix of strings and |
| 4658 | f-strings, and concat them together as needed. Ultimately, it |
| 4659 | generates an expr_ty. */ |
| 4660 | typedef struct { |
| 4661 | PyObject *last_str; |
| 4662 | ExprList expr_list; |
| 4663 | } FstringParser; |
| 4664 | |
| 4665 | #ifdef NDEBUG |
| 4666 | #define FstringParser_check_invariants(state) |
| 4667 | #else |
| 4668 | static void |
| 4669 | FstringParser_check_invariants(FstringParser *state) |
| 4670 | { |
| 4671 | if (state->last_str) |
| 4672 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4673 | ExprList_check_invariants(&state->expr_list); |
| 4674 | } |
| 4675 | #endif |
| 4676 | |
| 4677 | static void |
| 4678 | FstringParser_Init(FstringParser *state) |
| 4679 | { |
| 4680 | state->last_str = NULL; |
| 4681 | ExprList_Init(&state->expr_list); |
| 4682 | FstringParser_check_invariants(state); |
| 4683 | } |
| 4684 | |
| 4685 | static void |
| 4686 | FstringParser_Dealloc(FstringParser *state) |
| 4687 | { |
| 4688 | FstringParser_check_invariants(state); |
| 4689 | |
| 4690 | Py_XDECREF(state->last_str); |
| 4691 | ExprList_Dealloc(&state->expr_list); |
| 4692 | } |
| 4693 | |
| 4694 | /* Make a Str node, but decref the PyUnicode object being added. */ |
| 4695 | static expr_ty |
| 4696 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4697 | { |
| 4698 | PyObject *s = *str; |
| 4699 | *str = NULL; |
| 4700 | assert(PyUnicode_CheckExact(s)); |
| 4701 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4702 | Py_DECREF(s); |
| 4703 | return NULL; |
| 4704 | } |
| 4705 | return Str(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 4706 | } |
| 4707 | |
| 4708 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4709 | decref'd. */ |
| 4710 | static int |
| 4711 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4712 | { |
| 4713 | FstringParser_check_invariants(state); |
| 4714 | |
| 4715 | assert(PyUnicode_CheckExact(str)); |
| 4716 | |
| 4717 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4718 | Py_DECREF(str); |
| 4719 | return 0; |
| 4720 | } |
| 4721 | |
| 4722 | if (!state->last_str) { |
| 4723 | /* We didn't have a string before, so just remember this one. */ |
| 4724 | state->last_str = str; |
| 4725 | } else { |
| 4726 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4727 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4728 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4729 | return -1; |
| 4730 | } |
| 4731 | FstringParser_check_invariants(state); |
| 4732 | return 0; |
| 4733 | } |
| 4734 | |
| 4735 | /* Parse an f-string. The f-string is in str, starting at ofs, with no 'f' |
| 4736 | or quotes. str is not decref'd, since we don't know if it's used elsewhere. |
| 4737 | And if we're only looking at a part of a string, then decref'ing is |
| 4738 | definitely not the right thing to do! */ |
| 4739 | static int |
| 4740 | FstringParser_ConcatFstring(FstringParser *state, PyObject *str, |
| 4741 | Py_ssize_t *ofs, int recurse_lvl, |
| 4742 | struct compiling *c, const node *n) |
| 4743 | { |
| 4744 | FstringParser_check_invariants(state); |
| 4745 | |
| 4746 | /* Parse the f-string. */ |
| 4747 | while (1) { |
| 4748 | PyObject *literal = NULL; |
| 4749 | expr_ty expression = NULL; |
| 4750 | |
| 4751 | /* If there's a zero length literal in front of the |
| 4752 | expression, literal will be NULL. If we're at the end of |
| 4753 | the f-string, expression will be NULL (unless result == 1, |
| 4754 | see below). */ |
| 4755 | int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl, |
| 4756 | &literal, &expression, |
| 4757 | c, n); |
| 4758 | if (result < 0) |
| 4759 | return -1; |
| 4760 | |
| 4761 | /* Add the literal, if any. */ |
| 4762 | if (!literal) { |
| 4763 | /* Do nothing. Just leave last_str alone (and possibly |
| 4764 | NULL). */ |
| 4765 | } else if (!state->last_str) { |
| 4766 | state->last_str = literal; |
| 4767 | literal = NULL; |
| 4768 | } else { |
| 4769 | /* We have a literal, concatenate it. */ |
| 4770 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4771 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4772 | return -1; |
| 4773 | literal = NULL; |
| 4774 | } |
| 4775 | assert(!state->last_str || |
| 4776 | PyUnicode_GET_LENGTH(state->last_str) != 0); |
| 4777 | |
| 4778 | /* We've dealt with the literal now. It can't be leaked on further |
| 4779 | errors. */ |
| 4780 | assert(literal == NULL); |
| 4781 | |
| 4782 | /* See if we should just loop around to get the next literal |
| 4783 | and expression, while ignoring the expression this |
| 4784 | time. This is used for un-doubling braces, as an |
| 4785 | optimization. */ |
| 4786 | if (result == 1) |
| 4787 | continue; |
| 4788 | |
| 4789 | if (!expression) |
| 4790 | /* We're done with this f-string. */ |
| 4791 | break; |
| 4792 | |
| 4793 | /* We know we have an expression. Convert any existing string |
| 4794 | to a Str node. */ |
| 4795 | if (!state->last_str) { |
| 4796 | /* Do nothing. No previous literal. */ |
| 4797 | } else { |
| 4798 | /* Convert the existing last_str literal to a Str node. */ |
| 4799 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 4800 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 4801 | return -1; |
| 4802 | } |
| 4803 | |
| 4804 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 4805 | return -1; |
| 4806 | } |
| 4807 | |
| 4808 | assert(*ofs <= PyUnicode_GET_LENGTH(str)); |
| 4809 | |
| 4810 | /* If recurse_lvl is zero, then we must be at the end of the |
| 4811 | string. Otherwise, we must be at a right brace. */ |
| 4812 | |
| 4813 | if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) { |
| 4814 | ast_error(c, n, "f-string: unexpected end of string"); |
| 4815 | return -1; |
| 4816 | } |
| 4817 | if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') { |
| 4818 | ast_error(c, n, "f-string: expecting '}'"); |
| 4819 | return -1; |
| 4820 | } |
| 4821 | |
| 4822 | FstringParser_check_invariants(state); |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
| 4826 | /* Convert the partial state reflected in last_str and expr_list to an |
| 4827 | expr_ty. The expr_ty can be a Str, or a JoinedStr. */ |
| 4828 | static expr_ty |
| 4829 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 4830 | const node *n) |
| 4831 | { |
| 4832 | asdl_seq *seq; |
| 4833 | |
| 4834 | FstringParser_check_invariants(state); |
| 4835 | |
| 4836 | /* If we're just a constant string with no expressions, return |
| 4837 | that. */ |
| 4838 | if(state->expr_list.size == 0) { |
| 4839 | if (!state->last_str) { |
| 4840 | /* Create a zero length string. */ |
| 4841 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 4842 | if (!state->last_str) |
| 4843 | goto error; |
| 4844 | } |
| 4845 | return make_str_node_and_del(&state->last_str, c, n); |
| 4846 | } |
| 4847 | |
| 4848 | /* Create a Str node out of last_str, if needed. It will be the |
| 4849 | last node in our expression list. */ |
| 4850 | if (state->last_str) { |
| 4851 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 4852 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 4853 | goto error; |
| 4854 | } |
| 4855 | /* This has already been freed. */ |
| 4856 | assert(state->last_str == NULL); |
| 4857 | |
| 4858 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 4859 | if (!seq) |
| 4860 | goto error; |
| 4861 | |
| 4862 | /* If there's only one expression, return it. Otherwise, we need |
| 4863 | to join them together. */ |
| 4864 | if (seq->size == 1) |
| 4865 | return seq->elements[0]; |
| 4866 | |
| 4867 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 4868 | |
| 4869 | error: |
| 4870 | FstringParser_Dealloc(state); |
| 4871 | return NULL; |
| 4872 | } |
| 4873 | |
| 4874 | /* Given an f-string (with no 'f' or quotes) that's in str starting at |
| 4875 | ofs, parse it into an expr_ty. Return NULL on error. Does not |
| 4876 | decref str. */ |
| 4877 | static expr_ty |
| 4878 | fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl, |
| 4879 | struct compiling *c, const node *n) |
| 4880 | { |
| 4881 | FstringParser state; |
| 4882 | |
| 4883 | FstringParser_Init(&state); |
| 4884 | if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl, |
| 4885 | c, n) < 0) { |
| 4886 | FstringParser_Dealloc(&state); |
| 4887 | return NULL; |
| 4888 | } |
| 4889 | |
| 4890 | return FstringParser_Finish(&state, c, n); |
| 4891 | } |
| 4892 | |
| 4893 | /* n is a Python string literal, including the bracketing quote |
| 4894 | characters, and r, b, u, &/or f prefixes (if any), and embedded |
| 4895 | escape sequences (if any). parsestr parses it, and returns the |
| 4896 | decoded Python string object. If the string is an f-string, set |
| 4897 | *fmode and return the unparsed string object. |
| 4898 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4899 | static PyObject * |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4900 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4901 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4902 | size_t len; |
| 4903 | const char *s = STR(n); |
| 4904 | int quote = Py_CHARMASK(*s); |
| 4905 | int rawmode = 0; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 4906 | if (Py_ISALPHA(quote)) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 4907 | while (!*bytesmode || !rawmode) { |
| 4908 | if (quote == 'b' || quote == 'B') { |
| 4909 | quote = *++s; |
| 4910 | *bytesmode = 1; |
| 4911 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 4912 | else if (quote == 'u' || quote == 'U') { |
| 4913 | quote = *++s; |
| 4914 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 4915 | else if (quote == 'r' || quote == 'R') { |
| 4916 | quote = *++s; |
| 4917 | rawmode = 1; |
| 4918 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4919 | else if (quote == 'f' || quote == 'F') { |
| 4920 | quote = *++s; |
| 4921 | *fmode = 1; |
| 4922 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 4923 | else { |
| 4924 | break; |
| 4925 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4926 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4927 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4928 | if (*fmode && *bytesmode) { |
| 4929 | PyErr_BadInternalCall(); |
| 4930 | return NULL; |
| 4931 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4932 | if (quote != '\'' && quote != '\"') { |
| 4933 | PyErr_BadInternalCall(); |
| 4934 | return NULL; |
| 4935 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4936 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4937 | s++; |
| 4938 | len = strlen(s); |
| 4939 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4940 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4941 | "string to parse is too long"); |
| 4942 | return NULL; |
| 4943 | } |
| 4944 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4945 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4946 | PyErr_BadInternalCall(); |
| 4947 | return NULL; |
| 4948 | } |
| 4949 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4950 | /* A triple quoted string. We've already skipped one quote at |
| 4951 | the start and one at the end of the string. Now skip the |
| 4952 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4953 | s += 2; |
| 4954 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4955 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4956 | if (s[--len] != quote || s[--len] != quote) { |
| 4957 | PyErr_BadInternalCall(); |
| 4958 | return NULL; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 4959 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4960 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 4961 | |
| 4962 | /* Temporary hack: if this is an f-string, no backslashes are allowed. */ |
| 4963 | /* See issue 27921. */ |
| 4964 | if (*fmode && strchr(s, '\\') != NULL) { |
| 4965 | /* Syntax error. At a later date fix this so it only checks for |
| 4966 | backslashes within the braces. */ |
| 4967 | ast_error(c, n, "backslashes not allowed in f-strings"); |
| 4968 | return NULL; |
| 4969 | } |
| 4970 | |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 4971 | /* Avoid invoking escape decoding routines if possible. */ |
| 4972 | rawmode = rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4973 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 4974 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4975 | const char *ch; |
| 4976 | for (ch = s; *ch; ch++) { |
| 4977 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 4978 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4979 | "literal characters."); |
| 4980 | return NULL; |
| 4981 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 4982 | } |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 4983 | if (rawmode) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4984 | return PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 4985 | else |
| 4986 | return PyBytes_DecodeEscape(s, len, NULL, /* ignored */ 0, NULL); |
| 4987 | } else { |
| 4988 | if (rawmode) |
| 4989 | return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
| 4990 | else |
| 4991 | return decode_unicode_with_escapes(c, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4992 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4995 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 4996 | each STRING atom, and process it as needed. For bytes, just |
| 4997 | concatenate them together, and the result will be a Bytes node. For |
| 4998 | normal strings and f-strings, concatenate them together. The result |
| 4999 | will be a Str node if there were no f-strings; a FormattedValue |
| 5000 | node if there's just an f-string (with no leading or trailing |
| 5001 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5002 | any literals involved. */ |
| 5003 | static expr_ty |
| 5004 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5005 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5006 | int bytesmode = 0; |
| 5007 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5008 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5009 | |
| 5010 | FstringParser state; |
| 5011 | FstringParser_Init(&state); |
| 5012 | |
| 5013 | for (i = 0; i < NCH(n); i++) { |
| 5014 | int this_bytesmode = 0; |
| 5015 | int this_fmode = 0; |
| 5016 | PyObject *s; |
| 5017 | |
| 5018 | REQ(CHILD(n, i), STRING); |
| 5019 | s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode); |
| 5020 | if (!s) |
| 5021 | goto error; |
| 5022 | |
| 5023 | /* Check that we're not mixing bytes with unicode. */ |
| 5024 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5025 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
| 5026 | Py_DECREF(s); |
| 5027 | goto error; |
| 5028 | } |
| 5029 | bytesmode = this_bytesmode; |
| 5030 | |
| 5031 | assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s)); |
| 5032 | |
| 5033 | if (bytesmode) { |
| 5034 | /* For bytes, concat as we go. */ |
| 5035 | if (i == 0) { |
| 5036 | /* First time, just remember this value. */ |
| 5037 | bytes_str = s; |
| 5038 | } else { |
| 5039 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5040 | if (!bytes_str) |
| 5041 | goto error; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5042 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5043 | } else if (this_fmode) { |
| 5044 | /* This is an f-string. Concatenate and decref it. */ |
| 5045 | Py_ssize_t ofs = 0; |
| 5046 | int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n); |
| 5047 | Py_DECREF(s); |
| 5048 | if (result < 0) |
| 5049 | goto error; |
| 5050 | } else { |
| 5051 | /* This is a regular string. Concatenate it. */ |
| 5052 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5053 | goto error; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5054 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5055 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5056 | if (bytesmode) { |
| 5057 | /* Just return the bytes object and we're done. */ |
| 5058 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5059 | goto error; |
| 5060 | return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
| 5061 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5062 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5063 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5064 | assert(bytes_str == NULL); |
| 5065 | |
| 5066 | return FstringParser_Finish(&state, c, n); |
| 5067 | |
| 5068 | error: |
| 5069 | Py_XDECREF(bytes_str); |
| 5070 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5071 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5072 | } |