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