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 |
| 135 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 136 | { |
| 137 | int check_ctx = 1; |
| 138 | expr_context_ty actual_ctx; |
| 139 | |
| 140 | /* First check expression context. */ |
| 141 | switch (exp->kind) { |
| 142 | case Attribute_kind: |
| 143 | actual_ctx = exp->v.Attribute.ctx; |
| 144 | break; |
| 145 | case Subscript_kind: |
| 146 | actual_ctx = exp->v.Subscript.ctx; |
| 147 | break; |
| 148 | case Starred_kind: |
| 149 | actual_ctx = exp->v.Starred.ctx; |
| 150 | break; |
| 151 | case Name_kind: |
| 152 | actual_ctx = exp->v.Name.ctx; |
| 153 | break; |
| 154 | case List_kind: |
| 155 | actual_ctx = exp->v.List.ctx; |
| 156 | break; |
| 157 | case Tuple_kind: |
| 158 | actual_ctx = exp->v.Tuple.ctx; |
| 159 | break; |
| 160 | default: |
| 161 | if (ctx != Load) { |
| 162 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 163 | "assigned to in %s context", expr_context_name(ctx)); |
| 164 | return 0; |
| 165 | } |
| 166 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 167 | /* set actual_ctx to prevent gcc warning */ |
| 168 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 169 | } |
| 170 | if (check_ctx && actual_ctx != ctx) { |
| 171 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 172 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* Now validate expression. */ |
| 177 | switch (exp->kind) { |
| 178 | case BoolOp_kind: |
| 179 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 180 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 181 | return 0; |
| 182 | } |
| 183 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 184 | case BinOp_kind: |
| 185 | return validate_expr(exp->v.BinOp.left, Load) && |
| 186 | validate_expr(exp->v.BinOp.right, Load); |
| 187 | case UnaryOp_kind: |
| 188 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 189 | case Lambda_kind: |
| 190 | return validate_arguments(exp->v.Lambda.args) && |
| 191 | validate_expr(exp->v.Lambda.body, Load); |
| 192 | case IfExp_kind: |
| 193 | return validate_expr(exp->v.IfExp.test, Load) && |
| 194 | validate_expr(exp->v.IfExp.body, Load) && |
| 195 | validate_expr(exp->v.IfExp.orelse, Load); |
| 196 | case Dict_kind: |
| 197 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 198 | PyErr_SetString(PyExc_ValueError, |
| 199 | "Dict doesn't have the same number of keys as values"); |
| 200 | return 0; |
| 201 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 202 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 203 | dict literals, i.e. ``{**{a:b}}`` */ |
| 204 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 205 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 206 | case Set_kind: |
| 207 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 208 | #define COMP(NAME) \ |
| 209 | case NAME ## _kind: \ |
| 210 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 211 | validate_expr(exp->v.NAME.elt, Load); |
| 212 | COMP(ListComp) |
| 213 | COMP(SetComp) |
| 214 | COMP(GeneratorExp) |
| 215 | #undef COMP |
| 216 | case DictComp_kind: |
| 217 | return validate_comprehension(exp->v.DictComp.generators) && |
| 218 | validate_expr(exp->v.DictComp.key, Load) && |
| 219 | validate_expr(exp->v.DictComp.value, Load); |
| 220 | case Yield_kind: |
| 221 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 222 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 223 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 224 | case Await_kind: |
| 225 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 226 | case Compare_kind: |
| 227 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 228 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 229 | return 0; |
| 230 | } |
| 231 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 232 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 233 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 234 | "of comparators and operands"); |
| 235 | return 0; |
| 236 | } |
| 237 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 238 | validate_expr(exp->v.Compare.left, Load); |
| 239 | case Call_kind: |
| 240 | return validate_expr(exp->v.Call.func, Load) && |
| 241 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 242 | validate_keywords(exp->v.Call.keywords); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 243 | case Num_kind: { |
| 244 | PyObject *n = exp->v.Num.n; |
| 245 | if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) && |
| 246 | !PyComplex_CheckExact(n)) { |
| 247 | PyErr_SetString(PyExc_TypeError, "non-numeric type in Num"); |
| 248 | return 0; |
| 249 | } |
| 250 | return 1; |
| 251 | } |
| 252 | case Str_kind: { |
| 253 | PyObject *s = exp->v.Str.s; |
| 254 | if (!PyUnicode_CheckExact(s)) { |
| 255 | PyErr_SetString(PyExc_TypeError, "non-string type in Str"); |
| 256 | return 0; |
| 257 | } |
| 258 | return 1; |
| 259 | } |
| 260 | case Bytes_kind: { |
| 261 | PyObject *b = exp->v.Bytes.s; |
| 262 | if (!PyBytes_CheckExact(b)) { |
| 263 | PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes"); |
| 264 | return 0; |
| 265 | } |
| 266 | return 1; |
| 267 | } |
| 268 | case Attribute_kind: |
| 269 | return validate_expr(exp->v.Attribute.value, Load); |
| 270 | case Subscript_kind: |
| 271 | return validate_slice(exp->v.Subscript.slice) && |
| 272 | validate_expr(exp->v.Subscript.value, Load); |
| 273 | case Starred_kind: |
| 274 | return validate_expr(exp->v.Starred.value, ctx); |
| 275 | case List_kind: |
| 276 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 277 | case Tuple_kind: |
| 278 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
| 279 | /* These last cases don't have any checking. */ |
| 280 | case Name_kind: |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 281 | case NameConstant_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 282 | case Ellipsis_kind: |
| 283 | return 1; |
| 284 | default: |
| 285 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 286 | return 0; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static int |
| 291 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 292 | { |
| 293 | if (asdl_seq_LEN(seq)) |
| 294 | return 1; |
| 295 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 301 | { |
| 302 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 303 | validate_exprs(targets, ctx, 0); |
| 304 | } |
| 305 | |
| 306 | static int |
| 307 | validate_body(asdl_seq *body, const char *owner) |
| 308 | { |
| 309 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | validate_stmt(stmt_ty stmt) |
| 314 | { |
| 315 | int i; |
| 316 | switch (stmt->kind) { |
| 317 | case FunctionDef_kind: |
| 318 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
| 319 | validate_arguments(stmt->v.FunctionDef.args) && |
| 320 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 321 | (!stmt->v.FunctionDef.returns || |
| 322 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 323 | case ClassDef_kind: |
| 324 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
| 325 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 326 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 327 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 328 | case Return_kind: |
| 329 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 330 | case Delete_kind: |
| 331 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 332 | case Assign_kind: |
| 333 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 334 | validate_expr(stmt->v.Assign.value, Load); |
| 335 | case AugAssign_kind: |
| 336 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 337 | validate_expr(stmt->v.AugAssign.value, Load); |
| 338 | case For_kind: |
| 339 | return validate_expr(stmt->v.For.target, Store) && |
| 340 | validate_expr(stmt->v.For.iter, Load) && |
| 341 | validate_body(stmt->v.For.body, "For") && |
| 342 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 343 | case AsyncFor_kind: |
| 344 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 345 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
| 346 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
| 347 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 348 | case While_kind: |
| 349 | return validate_expr(stmt->v.While.test, Load) && |
| 350 | validate_body(stmt->v.While.body, "While") && |
| 351 | validate_stmts(stmt->v.While.orelse); |
| 352 | case If_kind: |
| 353 | return validate_expr(stmt->v.If.test, Load) && |
| 354 | validate_body(stmt->v.If.body, "If") && |
| 355 | validate_stmts(stmt->v.If.orelse); |
| 356 | case With_kind: |
| 357 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 358 | return 0; |
| 359 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 360 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 361 | if (!validate_expr(item->context_expr, Load) || |
| 362 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 363 | return 0; |
| 364 | } |
| 365 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 366 | case AsyncWith_kind: |
| 367 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 368 | return 0; |
| 369 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 370 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 371 | if (!validate_expr(item->context_expr, Load) || |
| 372 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 373 | return 0; |
| 374 | } |
| 375 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 376 | case Raise_kind: |
| 377 | if (stmt->v.Raise.exc) { |
| 378 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 379 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 380 | } |
| 381 | if (stmt->v.Raise.cause) { |
| 382 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 383 | return 0; |
| 384 | } |
| 385 | return 1; |
| 386 | case Try_kind: |
| 387 | if (!validate_body(stmt->v.Try.body, "Try")) |
| 388 | return 0; |
| 389 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 390 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 391 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 392 | return 0; |
| 393 | } |
| 394 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 395 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 396 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 397 | return 0; |
| 398 | } |
| 399 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 400 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 401 | if ((handler->v.ExceptHandler.type && |
| 402 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
| 403 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
| 404 | return 0; |
| 405 | } |
| 406 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 407 | validate_stmts(stmt->v.Try.finalbody)) && |
| 408 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 409 | validate_stmts(stmt->v.Try.orelse)); |
| 410 | case Assert_kind: |
| 411 | return validate_expr(stmt->v.Assert.test, Load) && |
| 412 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 413 | case Import_kind: |
| 414 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 415 | case ImportFrom_kind: |
| 416 | if (stmt->v.ImportFrom.level < -1) { |
| 417 | PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1"); |
| 418 | return 0; |
| 419 | } |
| 420 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 421 | case Global_kind: |
| 422 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 423 | case Nonlocal_kind: |
| 424 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 425 | case Expr_kind: |
| 426 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 427 | case AsyncFunctionDef_kind: |
| 428 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
| 429 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 430 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 431 | (!stmt->v.AsyncFunctionDef.returns || |
| 432 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 433 | case Pass_kind: |
| 434 | case Break_kind: |
| 435 | case Continue_kind: |
| 436 | return 1; |
| 437 | default: |
| 438 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 439 | return 0; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static int |
| 444 | validate_stmts(asdl_seq *seq) |
| 445 | { |
| 446 | int i; |
| 447 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 448 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 449 | if (stmt) { |
| 450 | if (!validate_stmt(stmt)) |
| 451 | return 0; |
| 452 | } |
| 453 | else { |
| 454 | PyErr_SetString(PyExc_ValueError, |
| 455 | "None disallowed in statement list"); |
| 456 | return 0; |
| 457 | } |
| 458 | } |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | static int |
| 463 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 464 | { |
| 465 | int i; |
| 466 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 467 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 468 | if (expr) { |
| 469 | if (!validate_expr(expr, ctx)) |
| 470 | return 0; |
| 471 | } |
| 472 | else if (!null_ok) { |
| 473 | PyErr_SetString(PyExc_ValueError, |
| 474 | "None disallowed in expression list"); |
| 475 | return 0; |
| 476 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 477 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 478 | } |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | int |
| 483 | PyAST_Validate(mod_ty mod) |
| 484 | { |
| 485 | int res = 0; |
| 486 | |
| 487 | switch (mod->kind) { |
| 488 | case Module_kind: |
| 489 | res = validate_stmts(mod->v.Module.body); |
| 490 | break; |
| 491 | case Interactive_kind: |
| 492 | res = validate_stmts(mod->v.Interactive.body); |
| 493 | break; |
| 494 | case Expression_kind: |
| 495 | res = validate_expr(mod->v.Expression.body, Load); |
| 496 | break; |
| 497 | case Suite_kind: |
| 498 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 499 | break; |
| 500 | default: |
| 501 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 502 | res = 0; |
| 503 | break; |
| 504 | } |
| 505 | return res; |
| 506 | } |
| 507 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 508 | /* 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] | 509 | #include "grammar.h" |
| 510 | #include "parsetok.h" |
| 511 | #include "graminit.h" |
| 512 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 513 | /* Data structure used internally */ |
| 514 | struct compiling { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 515 | char *c_encoding; /* source encoding */ |
| 516 | PyArena *c_arena; /* arena for allocating memeory */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 517 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 518 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
| 519 | PyObject *c_normalize_args; /* Normalization argument tuple. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 523 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 524 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
| 525 | static asdl_seq *ast_for_suite(struct compiling *, const node *); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 526 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 527 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 528 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 529 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 530 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 531 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); |
| 532 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); |
| 533 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 534 | /* Note different signature for ast_for_call */ |
| 535 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); |
| 536 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 537 | static PyObject *parsenumber(struct compiling *, const char *); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 538 | static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode); |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 539 | static PyObject *parsestrplus(struct compiling *, const node *n, |
| 540 | int *bytesmode); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 541 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 542 | #define COMP_GENEXP 0 |
| 543 | #define COMP_LISTCOMP 1 |
| 544 | #define COMP_SETCOMP 2 |
| 545 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 546 | static int |
| 547 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 548 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 549 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 550 | if (!m) |
| 551 | return 0; |
| 552 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 553 | Py_DECREF(m); |
| 554 | if (!c->c_normalize) |
| 555 | return 0; |
| 556 | c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 557 | if (!c->c_normalize_args) { |
| 558 | Py_CLEAR(c->c_normalize); |
| 559 | return 0; |
| 560 | } |
Christian Heimes | 72f562f | 2013-07-24 21:02:17 +0200 | [diff] [blame] | 561 | PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 562 | return 1; |
| 563 | } |
| 564 | |
| 565 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 566 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 567 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 568 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 569 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 570 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 571 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 572 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 573 | /* Check whether there are non-ASCII characters in the |
| 574 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 575 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 576 | PyObject *id2; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 577 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 578 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 579 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 580 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 581 | PyTuple_SET_ITEM(c->c_normalize_args, 1, id); |
| 582 | id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 583 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 584 | if (!id2) |
| 585 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 586 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 587 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 588 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 589 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 590 | Py_DECREF(id); |
| 591 | return NULL; |
| 592 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 593 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 596 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 597 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 598 | static int |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 599 | ast_error(struct compiling *c, const node *n, const char *errmsg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 600 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 601 | PyObject *value, *errstr, *loc, *tmp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 602 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 603 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 604 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 605 | Py_INCREF(Py_None); |
| 606 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 607 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 608 | 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] | 609 | if (!tmp) |
| 610 | return 0; |
| 611 | errstr = PyUnicode_FromString(errmsg); |
| 612 | if (!errstr) { |
| 613 | Py_DECREF(tmp); |
| 614 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 615 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 616 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 617 | Py_DECREF(errstr); |
| 618 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 619 | if (value) { |
| 620 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 621 | Py_DECREF(value); |
| 622 | } |
| 623 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /* num_stmts() returns number of contained statements. |
| 627 | |
| 628 | Use this routine to determine how big a sequence is needed for |
| 629 | the statements in a parse tree. Its raison d'etre is this bit of |
| 630 | grammar: |
| 631 | |
| 632 | stmt: simple_stmt | compound_stmt |
| 633 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 634 | |
| 635 | A simple_stmt can contain multiple small_stmt elements joined |
| 636 | by semicolons. If the arg is a simple_stmt, the number of |
| 637 | small_stmt elements is returned. |
| 638 | */ |
| 639 | |
| 640 | static int |
| 641 | num_stmts(const node *n) |
| 642 | { |
| 643 | int i, l; |
| 644 | node *ch; |
| 645 | |
| 646 | switch (TYPE(n)) { |
| 647 | case single_input: |
| 648 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 649 | return 0; |
| 650 | else |
| 651 | return num_stmts(CHILD(n, 0)); |
| 652 | case file_input: |
| 653 | l = 0; |
| 654 | for (i = 0; i < NCH(n); i++) { |
| 655 | ch = CHILD(n, i); |
| 656 | if (TYPE(ch) == stmt) |
| 657 | l += num_stmts(ch); |
| 658 | } |
| 659 | return l; |
| 660 | case stmt: |
| 661 | return num_stmts(CHILD(n, 0)); |
| 662 | case compound_stmt: |
| 663 | return 1; |
| 664 | case simple_stmt: |
| 665 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 666 | case suite: |
| 667 | if (NCH(n) == 1) |
| 668 | return num_stmts(CHILD(n, 0)); |
| 669 | else { |
| 670 | l = 0; |
| 671 | for (i = 2; i < (NCH(n) - 1); i++) |
| 672 | l += num_stmts(CHILD(n, i)); |
| 673 | return l; |
| 674 | } |
| 675 | default: { |
| 676 | char buf[128]; |
| 677 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 678 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 679 | TYPE(n), NCH(n)); |
| 680 | Py_FatalError(buf); |
| 681 | } |
| 682 | } |
| 683 | assert(0); |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* Transform the CST rooted at node * to the appropriate AST |
| 688 | */ |
| 689 | |
| 690 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 691 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 692 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 693 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 694 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 695 | asdl_seq *stmts = NULL; |
| 696 | stmt_ty s; |
| 697 | node *ch; |
| 698 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 699 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 700 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 701 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 702 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 703 | c.c_filename = filename; |
| 704 | c.c_normalize = c.c_normalize_args = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 705 | if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 706 | c.c_encoding = "utf-8"; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 707 | if (TYPE(n) == encoding_decl) { |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 708 | #if 0 |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 709 | ast_error(c, n, "encoding declaration in Unicode string"); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 710 | goto out; |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 711 | #endif |
| 712 | n = CHILD(n, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 713 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 714 | } else if (TYPE(n) == encoding_decl) { |
| 715 | c.c_encoding = STR(n); |
| 716 | n = CHILD(n, 0); |
| 717 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | /* PEP 3120 */ |
Martin v. Löwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 719 | c.c_encoding = "utf-8"; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 722 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 723 | switch (TYPE(n)) { |
| 724 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 725 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 726 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 727 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 728 | for (i = 0; i < NCH(n) - 1; i++) { |
| 729 | ch = CHILD(n, i); |
| 730 | if (TYPE(ch) == NEWLINE) |
| 731 | continue; |
| 732 | REQ(ch, stmt); |
| 733 | num = num_stmts(ch); |
| 734 | if (num == 1) { |
| 735 | s = ast_for_stmt(&c, ch); |
| 736 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 737 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 738 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 739 | } |
| 740 | else { |
| 741 | ch = CHILD(ch, 0); |
| 742 | REQ(ch, simple_stmt); |
| 743 | for (j = 0; j < num; j++) { |
| 744 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 745 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 746 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 747 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 751 | res = Module(stmts, arena); |
| 752 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 753 | case eval_input: { |
| 754 | expr_ty testlist_ast; |
| 755 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 756 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 757 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 758 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 759 | goto out; |
| 760 | res = Expression(testlist_ast, arena); |
| 761 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 762 | } |
| 763 | case single_input: |
| 764 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 765 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 766 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 767 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 768 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 769 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 770 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 771 | goto out; |
| 772 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | } |
| 774 | else { |
| 775 | n = CHILD(n, 0); |
| 776 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 777 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 778 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 779 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 780 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 781 | s = ast_for_stmt(&c, n); |
| 782 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 783 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | asdl_seq_SET(stmts, 0, s); |
| 785 | } |
| 786 | else { |
| 787 | /* Only a simple_stmt can contain multiple statements. */ |
| 788 | REQ(n, simple_stmt); |
| 789 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 791 | break; |
| 792 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 793 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 794 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | asdl_seq_SET(stmts, i / 2, s); |
| 796 | } |
| 797 | } |
| 798 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 799 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 800 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 801 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 802 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 803 | PyErr_Format(PyExc_SystemError, |
| 804 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 805 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 806 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 807 | out: |
| 808 | if (c.c_normalize) { |
| 809 | Py_DECREF(c.c_normalize); |
| 810 | PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL); |
| 811 | Py_DECREF(c.c_normalize_args); |
| 812 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 813 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 816 | mod_ty |
| 817 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 818 | PyArena *arena) |
| 819 | { |
| 820 | mod_ty mod; |
| 821 | PyObject *filename; |
| 822 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 823 | if (filename == NULL) |
| 824 | return NULL; |
| 825 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 826 | Py_DECREF(filename); |
| 827 | return mod; |
| 828 | |
| 829 | } |
| 830 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 831 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 832 | */ |
| 833 | |
| 834 | static operator_ty |
| 835 | get_operator(const node *n) |
| 836 | { |
| 837 | switch (TYPE(n)) { |
| 838 | case VBAR: |
| 839 | return BitOr; |
| 840 | case CIRCUMFLEX: |
| 841 | return BitXor; |
| 842 | case AMPER: |
| 843 | return BitAnd; |
| 844 | case LEFTSHIFT: |
| 845 | return LShift; |
| 846 | case RIGHTSHIFT: |
| 847 | return RShift; |
| 848 | case PLUS: |
| 849 | return Add; |
| 850 | case MINUS: |
| 851 | return Sub; |
| 852 | case STAR: |
| 853 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 854 | case AT: |
| 855 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | case SLASH: |
| 857 | return Div; |
| 858 | case DOUBLESLASH: |
| 859 | return FloorDiv; |
| 860 | case PERCENT: |
| 861 | return Mod; |
| 862 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 863 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 867 | static const char* FORBIDDEN[] = { |
| 868 | "None", |
| 869 | "True", |
| 870 | "False", |
| 871 | NULL, |
| 872 | }; |
| 873 | |
| 874 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 875 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 876 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 877 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 878 | assert(PyUnicode_Check(name)); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 879 | if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 880 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 881 | return 1; |
| 882 | } |
| 883 | if (full_checks) { |
| 884 | const char **p; |
| 885 | for (p = FORBIDDEN; *p; p++) { |
| 886 | if (PyUnicode_CompareWithASCIIString(name, *p) == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 887 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 888 | return 1; |
| 889 | } |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | return 0; |
| 893 | } |
| 894 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 895 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 896 | |
| 897 | Only sets context for expr kinds that "can appear in assignment context" |
| 898 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 899 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 900 | */ |
| 901 | |
| 902 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 903 | 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] | 904 | { |
| 905 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 906 | /* If a particular expression type can't be used for assign / delete, |
| 907 | set expr_name to its name and an error message will be generated. |
| 908 | */ |
| 909 | const char* expr_name = NULL; |
| 910 | |
| 911 | /* The ast defines augmented store and load contexts, but the |
| 912 | implementation here doesn't actually use them. The code may be |
| 913 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 914 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 915 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 916 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 917 | */ |
| 918 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 919 | |
| 920 | switch (e->kind) { |
| 921 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 922 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 923 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 924 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 925 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 926 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 927 | e->v.Subscript.ctx = ctx; |
| 928 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 929 | case Starred_kind: |
| 930 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 931 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 932 | return 0; |
| 933 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 934 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 935 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 936 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 937 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 938 | } |
| 939 | e->v.Name.ctx = ctx; |
| 940 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 941 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 942 | e->v.List.ctx = ctx; |
| 943 | s = e->v.List.elts; |
| 944 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 945 | case Tuple_kind: |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 946 | if (asdl_seq_LEN(e->v.Tuple.elts)) { |
| 947 | e->v.Tuple.ctx = ctx; |
| 948 | s = e->v.Tuple.elts; |
| 949 | } |
| 950 | else { |
| 951 | expr_name = "()"; |
| 952 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 953 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 954 | case Lambda_kind: |
| 955 | expr_name = "lambda"; |
| 956 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 957 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 958 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 959 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 960 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 961 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 962 | case UnaryOp_kind: |
| 963 | expr_name = "operator"; |
| 964 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 965 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 966 | expr_name = "generator expression"; |
| 967 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 968 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 969 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 970 | expr_name = "yield expression"; |
| 971 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 972 | case Await_kind: |
| 973 | expr_name = "await expression"; |
| 974 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 975 | case ListComp_kind: |
| 976 | expr_name = "list comprehension"; |
| 977 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 978 | case SetComp_kind: |
| 979 | expr_name = "set comprehension"; |
| 980 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 981 | case DictComp_kind: |
| 982 | expr_name = "dict comprehension"; |
| 983 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 984 | case Dict_kind: |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 985 | case Set_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 986 | case Num_kind: |
| 987 | case Str_kind: |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 988 | case Bytes_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 989 | expr_name = "literal"; |
| 990 | break; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 991 | case NameConstant_kind: |
| 992 | expr_name = "keyword"; |
| 993 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 994 | case Ellipsis_kind: |
| 995 | expr_name = "Ellipsis"; |
| 996 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 997 | case Compare_kind: |
| 998 | expr_name = "comparison"; |
| 999 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1000 | case IfExp_kind: |
| 1001 | expr_name = "conditional expression"; |
| 1002 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1003 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | PyErr_Format(PyExc_SystemError, |
| 1005 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1006 | e->kind, e->lineno); |
| 1007 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1008 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1009 | /* Check for error string set by switch */ |
| 1010 | if (expr_name) { |
| 1011 | char buf[300]; |
| 1012 | PyOS_snprintf(buf, sizeof(buf), |
| 1013 | "can't %s %s", |
| 1014 | ctx == Store ? "assign to" : "delete", |
| 1015 | expr_name); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1016 | return ast_error(c, n, buf); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1019 | /* 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] | 1020 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1021 | */ |
| 1022 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1023 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1024 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1025 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1026 | 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] | 1027 | return 0; |
| 1028 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1029 | } |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
| 1033 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1034 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1035 | { |
| 1036 | REQ(n, augassign); |
| 1037 | n = CHILD(n, 0); |
| 1038 | switch (STR(n)[0]) { |
| 1039 | case '+': |
| 1040 | return Add; |
| 1041 | case '-': |
| 1042 | return Sub; |
| 1043 | case '/': |
| 1044 | if (STR(n)[1] == '/') |
| 1045 | return FloorDiv; |
| 1046 | else |
| 1047 | return Div; |
| 1048 | case '%': |
| 1049 | return Mod; |
| 1050 | case '<': |
| 1051 | return LShift; |
| 1052 | case '>': |
| 1053 | return RShift; |
| 1054 | case '&': |
| 1055 | return BitAnd; |
| 1056 | case '^': |
| 1057 | return BitXor; |
| 1058 | case '|': |
| 1059 | return BitOr; |
| 1060 | case '*': |
| 1061 | if (STR(n)[1] == '*') |
| 1062 | return Pow; |
| 1063 | else |
| 1064 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1065 | case '@': |
| 1066 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1067 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1068 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1069 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1074 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1075 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1076 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1077 | |'is' 'not' |
| 1078 | */ |
| 1079 | REQ(n, comp_op); |
| 1080 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1081 | n = CHILD(n, 0); |
| 1082 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1083 | case LESS: |
| 1084 | return Lt; |
| 1085 | case GREATER: |
| 1086 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1087 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1088 | return Eq; |
| 1089 | case LESSEQUAL: |
| 1090 | return LtE; |
| 1091 | case GREATEREQUAL: |
| 1092 | return GtE; |
| 1093 | case NOTEQUAL: |
| 1094 | return NotEq; |
| 1095 | case NAME: |
| 1096 | if (strcmp(STR(n), "in") == 0) |
| 1097 | return In; |
| 1098 | if (strcmp(STR(n), "is") == 0) |
| 1099 | return Is; |
| 1100 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1101 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1102 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1103 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1104 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1105 | } |
| 1106 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1107 | /* handle "not in" and "is not" */ |
| 1108 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1109 | case NAME: |
| 1110 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1111 | return NotIn; |
| 1112 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1113 | return IsNot; |
| 1114 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1115 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1116 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1117 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1118 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1119 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1120 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1121 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1122 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static asdl_seq * |
| 1126 | seq_for_testlist(struct compiling *c, const node *n) |
| 1127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1129 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1130 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1131 | asdl_seq *seq; |
| 1132 | expr_ty expression; |
| 1133 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1134 | 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] | 1135 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1136 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1137 | if (!seq) |
| 1138 | return NULL; |
| 1139 | |
| 1140 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1142 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1144 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1145 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1147 | |
| 1148 | assert(i / 2 < seq->size); |
| 1149 | asdl_seq_SET(seq, i / 2, expression); |
| 1150 | } |
| 1151 | return seq; |
| 1152 | } |
| 1153 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1154 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1155 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1156 | { |
| 1157 | identifier name; |
| 1158 | expr_ty annotation = NULL; |
| 1159 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1160 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1161 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1162 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1163 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1164 | name = NEW_IDENTIFIER(ch); |
| 1165 | if (!name) |
| 1166 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1167 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1168 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1169 | |
| 1170 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1171 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1172 | if (!annotation) |
| 1173 | return NULL; |
| 1174 | } |
| 1175 | |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1176 | ret = arg(name, annotation, c->c_arena); |
| 1177 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1178 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1179 | ret->lineno = LINENO(n); |
| 1180 | ret->col_offset = n->n_col_offset; |
| 1181 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1184 | /* returns -1 if failed to handle keyword only arguments |
| 1185 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1186 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1187 | ^^^ |
| 1188 | start pointing here |
| 1189 | */ |
| 1190 | static int |
| 1191 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1192 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1193 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1194 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1195 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1196 | expr_ty expression, annotation; |
| 1197 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1198 | int i = start; |
| 1199 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1202 | 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] | 1203 | return -1; |
| 1204 | } |
| 1205 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1206 | while (i < NCH(n)) { |
| 1207 | ch = CHILD(n, i); |
| 1208 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1209 | case vfpdef: |
| 1210 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1211 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1212 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1213 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1214 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1215 | asdl_seq_SET(kwdefaults, j, expression); |
| 1216 | i += 2; /* '=' and test */ |
| 1217 | } |
| 1218 | else { /* setting NULL if no default value exists */ |
| 1219 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1220 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1221 | if (NCH(ch) == 3) { |
| 1222 | /* ch is NAME ':' test */ |
| 1223 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1224 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1225 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1226 | } |
| 1227 | else { |
| 1228 | annotation = NULL; |
| 1229 | } |
| 1230 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1231 | argname = NEW_IDENTIFIER(ch); |
| 1232 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1233 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1234 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1235 | goto error; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1236 | arg = arg(argname, annotation, c->c_arena); |
| 1237 | if (!arg) |
| 1238 | goto error; |
Benjamin Peterson | 0714b8b | 2014-02-13 19:22:14 -0500 | [diff] [blame] | 1239 | arg->lineno = LINENO(ch); |
| 1240 | arg->col_offset = ch->n_col_offset; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1241 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1242 | i += 2; /* the name and the comma */ |
| 1243 | break; |
| 1244 | case DOUBLESTAR: |
| 1245 | return i; |
| 1246 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1247 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1248 | goto error; |
| 1249 | } |
| 1250 | } |
| 1251 | return i; |
| 1252 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1254 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1255 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1256 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1257 | |
| 1258 | static arguments_ty |
| 1259 | ast_for_arguments(struct compiling *c, const node *n) |
| 1260 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1261 | /* This function handles both typedargslist (function definition) |
| 1262 | and varargslist (lambda definition). |
| 1263 | |
| 1264 | parameters: '(' [typedargslist] ')' |
| 1265 | typedargslist: ((tfpdef ['=' test] ',')* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1267 | | '**' tfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1268 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1269 | tfpdef: NAME [':' test] |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1270 | varargslist: ((vfpdef ['=' test] ',')* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1272 | | '**' vfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1273 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1274 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1275 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1276 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1277 | int nposdefaults = 0, found_default = 0; |
| 1278 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1279 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1280 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1281 | node *ch; |
| 1282 | |
| 1283 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1284 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1285 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1286 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1287 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1288 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1289 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1290 | /* First count the number of positional args & defaults. The |
| 1291 | variable i is the loop index for this for loop and the next. |
| 1292 | The next loop picks up where the first leaves off. |
| 1293 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1294 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1295 | ch = CHILD(n, i); |
| 1296 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1297 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1298 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1299 | if (i < NCH(n) && /* skip argument following star */ |
| 1300 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1301 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1302 | i++; |
| 1303 | } |
| 1304 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1305 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1306 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1307 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1308 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1309 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1311 | defaults for keyword only args */ |
| 1312 | for ( ; i < NCH(n); ++i) { |
| 1313 | ch = CHILD(n, i); |
| 1314 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1315 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1316 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1317 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1318 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1319 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1320 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1321 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1322 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1323 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1325 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1326 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1327 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1329 | since we set NULL as default for keyword only argument w/o default |
| 1330 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1331 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1332 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1333 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1334 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (nposargs + nkwonlyargs > 255) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1337 | ast_error(c, n, "more than 255 arguments"); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1338 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1339 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1340 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1341 | /* tfpdef: NAME [':' test] |
| 1342 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1343 | */ |
| 1344 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1345 | j = 0; /* index for defaults */ |
| 1346 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1347 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1348 | ch = CHILD(n, i); |
| 1349 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1350 | case tfpdef: |
| 1351 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1352 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1353 | anything other than EQUAL or a comma? */ |
| 1354 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1355 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1356 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1357 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1358 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1359 | assert(posdefaults != NULL); |
| 1360 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1361 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1362 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1363 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1364 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1365 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1366 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1367 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1368 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1369 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1370 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1371 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1372 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1373 | i += 2; /* the name and the comma */ |
| 1374 | break; |
| 1375 | case STAR: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1376 | if (i+1 >= NCH(n)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1377 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1378 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1379 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1380 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1381 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1382 | if (TYPE(ch) == COMMA) { |
| 1383 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1384 | i += 2; /* now follows keyword only arguments */ |
| 1385 | res = handle_keywordonly_args(c, n, i, |
| 1386 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1387 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1388 | i = res; /* res has new position to process */ |
| 1389 | } |
| 1390 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1391 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1392 | if (!vararg) |
| 1393 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1394 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1395 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1396 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1397 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1398 | int res = 0; |
| 1399 | res = handle_keywordonly_args(c, n, i, |
| 1400 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1401 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1402 | i = res; /* res has new position to process */ |
| 1403 | } |
| 1404 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1405 | break; |
| 1406 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1407 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1408 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1409 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1410 | if (!kwarg) |
| 1411 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1412 | i += 3; |
| 1413 | break; |
| 1414 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1415 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1416 | "unexpected node in varargslist: %d @ %d", |
| 1417 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1418 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1420 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1421 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | static expr_ty |
| 1425 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1426 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1427 | expr_ty e; |
| 1428 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1429 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1430 | int i; |
| 1431 | |
| 1432 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1433 | |
| 1434 | lineno = LINENO(n); |
| 1435 | col_offset = n->n_col_offset; |
| 1436 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1438 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1439 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1440 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1441 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1442 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1443 | |
| 1444 | for (i = 2; i < NCH(n); i+=2) { |
| 1445 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1446 | if (!id) |
| 1447 | return NULL; |
| 1448 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1449 | if (!e) |
| 1450 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static expr_ty |
| 1457 | ast_for_decorator(struct compiling *c, const node *n) |
| 1458 | { |
| 1459 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1460 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1461 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1463 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1464 | REQ(CHILD(n, 0), AT); |
| 1465 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1467 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1468 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1469 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1471 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1472 | d = name_expr; |
| 1473 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1476 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1477 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1478 | if (!d) |
| 1479 | return NULL; |
| 1480 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1481 | } |
| 1482 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1483 | d = ast_for_call(c, CHILD(n, 3), name_expr); |
| 1484 | if (!d) |
| 1485 | return NULL; |
| 1486 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | static asdl_seq* |
| 1493 | ast_for_decorators(struct compiling *c, const node *n) |
| 1494 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1495 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1496 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1497 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1499 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1500 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1501 | if (!decorator_seq) |
| 1502 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1504 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1505 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1506 | if (!d) |
| 1507 | return NULL; |
| 1508 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1509 | } |
| 1510 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1514 | ast_for_funcdef_impl(struct compiling *c, const node *n, |
| 1515 | asdl_seq *decorator_seq, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1517 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1518 | identifier name; |
| 1519 | arguments_ty args; |
| 1520 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1521 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1522 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1523 | |
| 1524 | REQ(n, funcdef); |
| 1525 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1526 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1527 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1528 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1529 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1530 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1531 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1532 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1533 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1534 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1535 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1536 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1537 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1538 | name_i += 2; |
| 1539 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1540 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
| 1541 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1542 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1544 | if (is_async) |
| 1545 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
| 1546 | LINENO(n), |
| 1547 | n->n_col_offset, c->c_arena); |
| 1548 | else |
| 1549 | return FunctionDef(name, args, body, decorator_seq, returns, |
| 1550 | LINENO(n), |
| 1551 | n->n_col_offset, c->c_arena); |
| 1552 | } |
| 1553 | |
| 1554 | static stmt_ty |
| 1555 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1556 | { |
| 1557 | /* async_funcdef: ASYNC funcdef */ |
| 1558 | REQ(n, async_funcdef); |
| 1559 | REQ(CHILD(n, 0), ASYNC); |
| 1560 | REQ(CHILD(n, 1), funcdef); |
| 1561 | |
| 1562 | return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, |
| 1563 | 1 /* is_async */); |
| 1564 | } |
| 1565 | |
| 1566 | static stmt_ty |
| 1567 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1568 | { |
| 1569 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1570 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1571 | 0 /* is_async */); |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | static stmt_ty |
| 1576 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1577 | { |
| 1578 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
| 1579 | REQ(n, async_stmt); |
| 1580 | REQ(CHILD(n, 0), ASYNC); |
| 1581 | |
| 1582 | switch (TYPE(CHILD(n, 1))) { |
| 1583 | case funcdef: |
| 1584 | return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, |
| 1585 | 1 /* is_async */); |
| 1586 | case with_stmt: |
| 1587 | return ast_for_with_stmt(c, CHILD(n, 1), |
| 1588 | 1 /* is_async */); |
| 1589 | |
| 1590 | case for_stmt: |
| 1591 | return ast_for_for_stmt(c, CHILD(n, 1), |
| 1592 | 1 /* is_async */); |
| 1593 | |
| 1594 | default: |
| 1595 | PyErr_Format(PyExc_SystemError, |
| 1596 | "invalid async stament: %s", |
| 1597 | STR(CHILD(n, 1))); |
| 1598 | return NULL; |
| 1599 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1602 | static stmt_ty |
| 1603 | ast_for_decorated(struct compiling *c, const node *n) |
| 1604 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1605 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1606 | stmt_ty thing = NULL; |
| 1607 | asdl_seq *decorator_seq = NULL; |
| 1608 | |
| 1609 | REQ(n, decorated); |
| 1610 | |
| 1611 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1612 | if (!decorator_seq) |
| 1613 | return NULL; |
| 1614 | |
| 1615 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1616 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1618 | |
| 1619 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1620 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1621 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1622 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1623 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1624 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1625 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1626 | /* we count the decorators in when talking about the class' or |
| 1627 | * function's line number */ |
| 1628 | if (thing) { |
| 1629 | thing->lineno = LINENO(n); |
| 1630 | thing->col_offset = n->n_col_offset; |
| 1631 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1632 | return thing; |
| 1633 | } |
| 1634 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1635 | static expr_ty |
| 1636 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1637 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1638 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1639 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1640 | arguments_ty args; |
| 1641 | expr_ty expression; |
| 1642 | |
| 1643 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1644 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1645 | if (!args) |
| 1646 | return NULL; |
| 1647 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1648 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1649 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1650 | } |
| 1651 | else { |
| 1652 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1653 | if (!args) |
| 1654 | return NULL; |
| 1655 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1656 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1657 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1660 | 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] | 1661 | } |
| 1662 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1663 | static expr_ty |
| 1664 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1667 | expr_ty expression, body, orelse; |
| 1668 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1669 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1670 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1671 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1672 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1673 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1674 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1675 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1676 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1677 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1678 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1679 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1680 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1684 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1685 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1686 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1687 | */ |
| 1688 | |
| 1689 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1690 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1691 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1692 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1693 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1694 | count_comp_for: |
| 1695 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1696 | REQ(n, comp_for); |
| 1697 | if (NCH(n) == 5) |
| 1698 | n = CHILD(n, 4); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1699 | else |
| 1700 | return n_fors; |
| 1701 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1702 | REQ(n, comp_iter); |
| 1703 | n = CHILD(n, 0); |
| 1704 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1705 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1706 | else if (TYPE(n) == comp_if) { |
| 1707 | if (NCH(n) == 3) { |
| 1708 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1709 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1710 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1711 | else |
| 1712 | return n_fors; |
| 1713 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1714 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1715 | /* Should never be reached */ |
| 1716 | PyErr_SetString(PyExc_SystemError, |
| 1717 | "logic error in count_comp_fors"); |
| 1718 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1721 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1722 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1723 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1724 | */ |
| 1725 | |
| 1726 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1727 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1728 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1729 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1730 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1731 | while (1) { |
| 1732 | REQ(n, comp_iter); |
| 1733 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1734 | return n_ifs; |
| 1735 | n = CHILD(n, 0); |
| 1736 | REQ(n, comp_if); |
| 1737 | n_ifs++; |
| 1738 | if (NCH(n) == 2) |
| 1739 | return n_ifs; |
| 1740 | n = CHILD(n, 2); |
| 1741 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1744 | static asdl_seq * |
| 1745 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1746 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1747 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1748 | asdl_seq *comps; |
| 1749 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1750 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1751 | if (n_fors == -1) |
| 1752 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1754 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1755 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1756 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1757 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1758 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1759 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1760 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1761 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1762 | node *for_ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1764 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1766 | for_ch = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1767 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1768 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1769 | return NULL; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1770 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1771 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1772 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1773 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1774 | /* Check the # of children rather than the length of t, since |
| 1775 | (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] | 1776 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1777 | if (NCH(for_ch) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1778 | comp = comprehension(first, expression, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1779 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1780 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 1781 | c->c_arena), |
| 1782 | expression, NULL, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1783 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1784 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1785 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1786 | if (NCH(n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1787 | int j, n_ifs; |
| 1788 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1790 | n = CHILD(n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1791 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1792 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1793 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1795 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1796 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1798 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1799 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1800 | REQ(n, comp_iter); |
| 1801 | n = CHILD(n, 0); |
| 1802 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1804 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1805 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1806 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1807 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1808 | if (NCH(n) == 3) |
| 1809 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1810 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1811 | /* on exit, must guarantee that n is a comp_for */ |
| 1812 | if (TYPE(n) == comp_iter) |
| 1813 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1814 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1815 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1816 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1817 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1818 | return comps; |
| 1819 | } |
| 1820 | |
| 1821 | static expr_ty |
| 1822 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1823 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1824 | /* testlist_comp: (test|star_expr) |
| 1825 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1826 | expr_ty elt; |
| 1827 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1828 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1830 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1831 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1832 | ch = CHILD(n, 0); |
| 1833 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1834 | if (!elt) |
| 1835 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1836 | if (elt->kind == Starred_kind) { |
| 1837 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1838 | return NULL; |
| 1839 | } |
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 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1842 | if (!comps) |
| 1843 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1844 | |
| 1845 | if (type == COMP_GENEXP) |
| 1846 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1847 | else if (type == COMP_LISTCOMP) |
| 1848 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1849 | else if (type == COMP_SETCOMP) |
| 1850 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1851 | else |
| 1852 | /* Should never happen */ |
| 1853 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1856 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1857 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1858 | * elements. Iff successful, nonzero is returned. |
| 1859 | */ |
| 1860 | static int |
| 1861 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1862 | expr_ty *key, expr_ty *value) |
| 1863 | { |
| 1864 | expr_ty expression; |
| 1865 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1866 | assert(NCH(n) - *i >= 2); |
| 1867 | |
| 1868 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1869 | if (!expression) |
| 1870 | return 0; |
| 1871 | *key = NULL; |
| 1872 | *value = expression; |
| 1873 | |
| 1874 | *i += 2; |
| 1875 | } |
| 1876 | else { |
| 1877 | assert(NCH(n) - *i >= 3); |
| 1878 | |
| 1879 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1880 | if (!expression) |
| 1881 | return 0; |
| 1882 | *key = expression; |
| 1883 | |
| 1884 | REQ(CHILD(n, *i + 1), COLON); |
| 1885 | |
| 1886 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1887 | if (!expression) |
| 1888 | return 0; |
| 1889 | *value = expression; |
| 1890 | |
| 1891 | *i += 3; |
| 1892 | } |
| 1893 | return 1; |
| 1894 | } |
| 1895 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1896 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1897 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1898 | { |
| 1899 | expr_ty key, value; |
| 1900 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1901 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1903 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1904 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1905 | assert(key); |
| 1906 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1908 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1909 | if (!comps) |
| 1910 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1912 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1913 | } |
| 1914 | |
| 1915 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1916 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 1917 | { |
| 1918 | int i; |
| 1919 | int j; |
| 1920 | int size; |
| 1921 | asdl_seq *keys, *values; |
| 1922 | |
| 1923 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 1924 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 1925 | if (!keys) |
| 1926 | return NULL; |
| 1927 | |
| 1928 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 1929 | if (!values) |
| 1930 | return NULL; |
| 1931 | |
| 1932 | j = 0; |
| 1933 | for (i = 0; i < NCH(n); i++) { |
| 1934 | expr_ty key, value; |
| 1935 | |
| 1936 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 1937 | return NULL; |
| 1938 | asdl_seq_SET(keys, j, key); |
| 1939 | asdl_seq_SET(values, j, value); |
| 1940 | |
| 1941 | j++; |
| 1942 | } |
| 1943 | keys->size = j; |
| 1944 | values->size = j; |
| 1945 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 1946 | } |
| 1947 | |
| 1948 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1949 | ast_for_genexp(struct compiling *c, const node *n) |
| 1950 | { |
| 1951 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1952 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | static expr_ty |
| 1956 | ast_for_listcomp(struct compiling *c, const node *n) |
| 1957 | { |
| 1958 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1959 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | static expr_ty |
| 1963 | ast_for_setcomp(struct compiling *c, const node *n) |
| 1964 | { |
| 1965 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1966 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1969 | static expr_ty |
| 1970 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 1971 | { |
| 1972 | int i; |
| 1973 | int size; |
| 1974 | asdl_seq *elts; |
| 1975 | |
| 1976 | assert(TYPE(n) == (dictorsetmaker)); |
| 1977 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 1978 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 1979 | if (!elts) |
| 1980 | return NULL; |
| 1981 | for (i = 0; i < NCH(n); i += 2) { |
| 1982 | expr_ty expression; |
| 1983 | expression = ast_for_expr(c, CHILD(n, i)); |
| 1984 | if (!expression) |
| 1985 | return NULL; |
| 1986 | asdl_seq_SET(elts, i / 2, expression); |
| 1987 | } |
| 1988 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 1989 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1990 | |
| 1991 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1992 | ast_for_atom(struct compiling *c, const node *n) |
| 1993 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1994 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 1995 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1996 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1997 | */ |
| 1998 | node *ch = CHILD(n, 0); |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 1999 | int bytesmode = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2001 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2002 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2003 | PyObject *name; |
| 2004 | const char *s = STR(ch); |
| 2005 | size_t len = strlen(s); |
| 2006 | if (len >= 4 && len <= 5) { |
| 2007 | if (!strcmp(s, "None")) |
| 2008 | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
| 2009 | if (!strcmp(s, "True")) |
| 2010 | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
| 2011 | if (!strcmp(s, "False")) |
| 2012 | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
| 2013 | } |
| 2014 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2015 | if (!name) |
| 2016 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2017 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2018 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2019 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2020 | case STRING: { |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 2021 | PyObject *str = parsestrplus(c, n, &bytesmode); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2022 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2023 | const char *errtype = NULL; |
| 2024 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2025 | errtype = "unicode error"; |
| 2026 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2027 | errtype = "value error"; |
| 2028 | if (errtype) { |
| 2029 | char buf[128]; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2030 | PyObject *type, *value, *tback, *errstr; |
| 2031 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2032 | errstr = PyObject_Str(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2033 | if (errstr) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2034 | char *s = _PyUnicode_AsString(errstr); |
| 2035 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2036 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2037 | } else { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2038 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2039 | } |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2040 | ast_error(c, n, buf); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2041 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2042 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2043 | Py_XDECREF(tback); |
| 2044 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2045 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2046 | } |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2047 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 2048 | Py_DECREF(str); |
| 2049 | return NULL; |
| 2050 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 2051 | if (bytesmode) |
| 2052 | return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena); |
| 2053 | else |
| 2054 | return Str(str, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2055 | } |
| 2056 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2057 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2058 | if (!pynum) |
| 2059 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2060 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2061 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2062 | Py_DECREF(pynum); |
| 2063 | return NULL; |
| 2064 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2065 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2066 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2067 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2068 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2069 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2070 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2072 | if (TYPE(ch) == RPAR) |
| 2073 | 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] | 2074 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2075 | if (TYPE(ch) == yield_expr) |
| 2076 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2079 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2080 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2081 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2082 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2083 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2084 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2086 | if (TYPE(ch) == RSQB) |
| 2087 | 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] | 2088 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2089 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2090 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2091 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2092 | if (!elts) |
| 2093 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2094 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2095 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2096 | } |
| 2097 | else |
| 2098 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2099 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2100 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2101 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2102 | * ((test | '*' test) |
| 2103 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2104 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2105 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2106 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2107 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2108 | 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] | 2109 | } |
| 2110 | else { |
| 2111 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2112 | if (NCH(ch) == 1 || |
| 2113 | (NCH(ch) > 1 && |
| 2114 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2115 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2116 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2117 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2118 | else if (NCH(ch) > 1 && |
| 2119 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2120 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2121 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2122 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2123 | else if (NCH(ch) > 3 - is_dict && |
| 2124 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2125 | /* It's a dictionary comprehension. */ |
| 2126 | if (is_dict) { |
| 2127 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2128 | "dict comprehension"); |
| 2129 | return NULL; |
| 2130 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2131 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2132 | } |
| 2133 | else { |
| 2134 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2135 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2136 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2137 | if (res) { |
| 2138 | res->lineno = LINENO(n); |
| 2139 | res->col_offset = n->n_col_offset; |
| 2140 | } |
| 2141 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2142 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2143 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2144 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2145 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2146 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | static slice_ty |
| 2151 | ast_for_slice(struct compiling *c, const node *n) |
| 2152 | { |
| 2153 | node *ch; |
| 2154 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2155 | |
| 2156 | REQ(n, subscript); |
| 2157 | |
| 2158 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2159 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2160 | sliceop: ':' [test] |
| 2161 | */ |
| 2162 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2163 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2164 | /* 'step' variable hold no significance in terms of being used over |
| 2165 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2167 | if (!step) |
| 2168 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2170 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2174 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2175 | if (!lower) |
| 2176 | return NULL; |
| 2177 | } |
| 2178 | |
| 2179 | /* If there's an upper bound it's in the second or third position. */ |
| 2180 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2181 | if (NCH(n) > 1) { |
| 2182 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2183 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2184 | if (TYPE(n2) == test) { |
| 2185 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2186 | if (!upper) |
| 2187 | return NULL; |
| 2188 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2189 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2190 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2191 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2192 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2193 | if (TYPE(n2) == test) { |
| 2194 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2195 | if (!upper) |
| 2196 | return NULL; |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | ch = CHILD(n, NCH(n) - 1); |
| 2201 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2202 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2203 | ch = CHILD(ch, 1); |
| 2204 | if (TYPE(ch) == test) { |
| 2205 | step = ast_for_expr(c, ch); |
| 2206 | if (!step) |
| 2207 | return NULL; |
| 2208 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2209 | } |
| 2210 | } |
| 2211 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2212 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | static expr_ty |
| 2216 | ast_for_binop(struct compiling *c, const node *n) |
| 2217 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2218 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2220 | BinOp(BinOp(A, op, B), op, C). |
| 2221 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2222 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2223 | int i, nops; |
| 2224 | expr_ty expr1, expr2, result; |
| 2225 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2226 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2227 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2228 | if (!expr1) |
| 2229 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2230 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2231 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2232 | if (!expr2) |
| 2233 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2234 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2235 | newoperator = get_operator(CHILD(n, 1)); |
| 2236 | if (!newoperator) |
| 2237 | return NULL; |
| 2238 | |
| 2239 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2240 | c->c_arena); |
| 2241 | if (!result) |
| 2242 | return NULL; |
| 2243 | |
| 2244 | nops = (NCH(n) - 1) / 2; |
| 2245 | for (i = 1; i < nops; i++) { |
| 2246 | expr_ty tmp_result, tmp; |
| 2247 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2248 | |
| 2249 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2250 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2251 | return NULL; |
| 2252 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2253 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2254 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2255 | return NULL; |
| 2256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2258 | LINENO(next_oper), next_oper->n_col_offset, |
| 2259 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2261 | return NULL; |
| 2262 | result = tmp_result; |
| 2263 | } |
| 2264 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2267 | static expr_ty |
| 2268 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2271 | subscriptlist: subscript (',' subscript)* [','] |
| 2272 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2273 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2274 | REQ(n, trailer); |
| 2275 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2276 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2277 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2278 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2279 | else |
| 2280 | return ast_for_call(c, CHILD(n, 1), left_expr); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2281 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2282 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2283 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2284 | if (!attr_id) |
| 2285 | return NULL; |
| 2286 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2287 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2288 | } |
| 2289 | else { |
| 2290 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2291 | REQ(CHILD(n, 2), RSQB); |
| 2292 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2293 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2294 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2295 | if (!slc) |
| 2296 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2297 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2298 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2299 | } |
| 2300 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2302 | by treating the sequence as a tuple literal if there are |
| 2303 | no slice features. |
| 2304 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2305 | int j; |
| 2306 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2307 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2308 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2309 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2310 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2311 | if (!slices) |
| 2312 | return NULL; |
| 2313 | for (j = 0; j < NCH(n); j += 2) { |
| 2314 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2315 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2316 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2317 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2318 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2319 | asdl_seq_SET(slices, j / 2, slc); |
| 2320 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2321 | if (!simple) { |
| 2322 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2323 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2324 | } |
| 2325 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2326 | 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] | 2327 | if (!elts) |
| 2328 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2329 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2330 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2331 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2332 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2333 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2334 | 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] | 2335 | if (!e) |
| 2336 | return NULL; |
| 2337 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2338 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2339 | } |
| 2340 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2344 | ast_for_factor(struct compiling *c, const node *n) |
| 2345 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2346 | expr_ty expression; |
| 2347 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2348 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2349 | if (!expression) |
| 2350 | return NULL; |
| 2351 | |
| 2352 | switch (TYPE(CHILD(n, 0))) { |
| 2353 | case PLUS: |
| 2354 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2355 | c->c_arena); |
| 2356 | case MINUS: |
| 2357 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2358 | c->c_arena); |
| 2359 | case TILDE: |
| 2360 | return UnaryOp(Invert, expression, LINENO(n), |
| 2361 | n->n_col_offset, c->c_arena); |
| 2362 | } |
| 2363 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2364 | TYPE(CHILD(n, 0))); |
| 2365 | return NULL; |
| 2366 | } |
| 2367 | |
| 2368 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2369 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2370 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2371 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2372 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2373 | |
| 2374 | REQ(n, atom_expr); |
| 2375 | nch = NCH(n); |
| 2376 | |
| 2377 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2378 | start = 1; |
| 2379 | assert(nch > 1); |
| 2380 | } |
| 2381 | |
| 2382 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2383 | if (!e) |
| 2384 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2385 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2386 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2387 | if (start && nch == 2) { |
| 2388 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2389 | } |
| 2390 | |
| 2391 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2392 | node *ch = CHILD(n, i); |
| 2393 | if (TYPE(ch) != trailer) |
| 2394 | break; |
| 2395 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2396 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2397 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2398 | tmp->lineno = e->lineno; |
| 2399 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2400 | e = tmp; |
| 2401 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2402 | |
| 2403 | if (start) { |
| 2404 | /* there was an AWAIT */ |
| 2405 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2406 | } |
| 2407 | else { |
| 2408 | return e; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | static expr_ty |
| 2413 | ast_for_power(struct compiling *c, const node *n) |
| 2414 | { |
| 2415 | /* power: atom trailer* ('**' factor)* |
| 2416 | */ |
| 2417 | expr_ty e; |
| 2418 | REQ(n, power); |
| 2419 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2420 | if (!e) |
| 2421 | return NULL; |
| 2422 | if (NCH(n) == 1) |
| 2423 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2424 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2425 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2426 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2427 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2428 | 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] | 2429 | } |
| 2430 | return e; |
| 2431 | } |
| 2432 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2433 | static expr_ty |
| 2434 | ast_for_starred(struct compiling *c, const node *n) |
| 2435 | { |
| 2436 | expr_ty tmp; |
| 2437 | REQ(n, star_expr); |
| 2438 | |
| 2439 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2440 | if (!tmp) |
| 2441 | return NULL; |
| 2442 | |
| 2443 | /* The Load context is changed later. */ |
| 2444 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2445 | } |
| 2446 | |
| 2447 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2448 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2449 | */ |
| 2450 | |
| 2451 | static expr_ty |
| 2452 | ast_for_expr(struct compiling *c, const node *n) |
| 2453 | { |
| 2454 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2455 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2456 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2458 | and_test: not_test ('and' not_test)* |
| 2459 | not_test: 'not' not_test | comparison |
| 2460 | comparison: expr (comp_op expr)* |
| 2461 | expr: xor_expr ('|' xor_expr)* |
| 2462 | xor_expr: and_expr ('^' and_expr)* |
| 2463 | and_expr: shift_expr ('&' shift_expr)* |
| 2464 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2465 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2466 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2467 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2468 | power: atom_expr ['**' factor] |
| 2469 | atom_expr: [AWAIT] atom trailer* |
| 2470 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2471 | */ |
| 2472 | |
| 2473 | asdl_seq *seq; |
| 2474 | int i; |
| 2475 | |
| 2476 | loop: |
| 2477 | switch (TYPE(n)) { |
| 2478 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2479 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2480 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2481 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2482 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2483 | else if (NCH(n) > 1) |
| 2484 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2485 | /* Fallthrough */ |
| 2486 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2487 | case and_test: |
| 2488 | if (NCH(n) == 1) { |
| 2489 | n = CHILD(n, 0); |
| 2490 | goto loop; |
| 2491 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2492 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2493 | if (!seq) |
| 2494 | return NULL; |
| 2495 | for (i = 0; i < NCH(n); i += 2) { |
| 2496 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2497 | if (!e) |
| 2498 | return NULL; |
| 2499 | asdl_seq_SET(seq, i / 2, e); |
| 2500 | } |
| 2501 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2502 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2503 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2504 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2505 | 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] | 2506 | case not_test: |
| 2507 | if (NCH(n) == 1) { |
| 2508 | n = CHILD(n, 0); |
| 2509 | goto loop; |
| 2510 | } |
| 2511 | else { |
| 2512 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2513 | if (!expression) |
| 2514 | return NULL; |
| 2515 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2516 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2517 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2518 | } |
| 2519 | case comparison: |
| 2520 | if (NCH(n) == 1) { |
| 2521 | n = CHILD(n, 0); |
| 2522 | goto loop; |
| 2523 | } |
| 2524 | else { |
| 2525 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2526 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2527 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2528 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2529 | if (!ops) |
| 2530 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2531 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2532 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2533 | return NULL; |
| 2534 | } |
| 2535 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2536 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2537 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2538 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2539 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2540 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2541 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2542 | |
| 2543 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2544 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2545 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2546 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2548 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2549 | asdl_seq_SET(cmps, i / 2, expression); |
| 2550 | } |
| 2551 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2552 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2553 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2554 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2556 | return Compare(expression, ops, cmps, LINENO(n), |
| 2557 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2558 | } |
| 2559 | break; |
| 2560 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2561 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2563 | /* The next five cases all handle BinOps. The main body of code |
| 2564 | is the same in each case, but the switch turned inside out to |
| 2565 | reuse the code for each type of operator. |
| 2566 | */ |
| 2567 | case expr: |
| 2568 | case xor_expr: |
| 2569 | case and_expr: |
| 2570 | case shift_expr: |
| 2571 | case arith_expr: |
| 2572 | case term: |
| 2573 | if (NCH(n) == 1) { |
| 2574 | n = CHILD(n, 0); |
| 2575 | goto loop; |
| 2576 | } |
| 2577 | return ast_for_binop(c, n); |
| 2578 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2579 | node *an = NULL; |
| 2580 | node *en = NULL; |
| 2581 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2582 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2583 | if (NCH(n) > 1) |
| 2584 | an = CHILD(n, 1); /* yield_arg */ |
| 2585 | if (an) { |
| 2586 | en = CHILD(an, NCH(an) - 1); |
| 2587 | if (NCH(an) == 2) { |
| 2588 | is_from = 1; |
| 2589 | exp = ast_for_expr(c, en); |
| 2590 | } |
| 2591 | else |
| 2592 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2593 | if (!exp) |
| 2594 | return NULL; |
| 2595 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2596 | if (is_from) |
| 2597 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2598 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2599 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2600 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2601 | if (NCH(n) == 1) { |
| 2602 | n = CHILD(n, 0); |
| 2603 | goto loop; |
| 2604 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2605 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2606 | case power: |
| 2607 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2608 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2609 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2610 | return NULL; |
| 2611 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2612 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2613 | return NULL; |
| 2614 | } |
| 2615 | |
| 2616 | static expr_ty |
| 2617 | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
| 2618 | { |
| 2619 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2620 | arglist: argument (',' argument)* [','] |
| 2621 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2622 | */ |
| 2623 | |
| 2624 | int i, nargs, nkeywords, ngens; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2625 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2626 | asdl_seq *args; |
| 2627 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2628 | |
| 2629 | REQ(n, arglist); |
| 2630 | |
| 2631 | nargs = 0; |
| 2632 | nkeywords = 0; |
| 2633 | ngens = 0; |
| 2634 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2635 | node *ch = CHILD(n, i); |
| 2636 | if (TYPE(ch) == argument) { |
| 2637 | if (NCH(ch) == 1) |
| 2638 | nargs++; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2639 | else if (TYPE(CHILD(ch, 1)) == comp_for) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2640 | ngens++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2641 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2642 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2643 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2644 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2645 | nkeywords++; |
| 2646 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | } |
| 2648 | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2649 | ast_error(c, n, "Generator expression must be parenthesized " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2650 | "if not sole argument"); |
| 2651 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | if (nargs + nkeywords + ngens > 255) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2655 | ast_error(c, n, "more than 255 arguments"); |
| 2656 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2659 | args = _Py_asdl_seq_new(nargs + ngens, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2660 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2661 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2662 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2663 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2664 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2665 | |
| 2666 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2667 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2668 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2669 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2670 | node *ch = CHILD(n, i); |
| 2671 | if (TYPE(ch) == argument) { |
| 2672 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2673 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2674 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2675 | /* a positional argument */ |
| 2676 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2677 | if (ndoublestars) { |
| 2678 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2679 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2680 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2681 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2682 | else { |
| 2683 | ast_error(c, chch, |
| 2684 | "positional argument follows " |
| 2685 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2686 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2687 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2688 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2689 | e = ast_for_expr(c, chch); |
| 2690 | if (!e) |
| 2691 | return NULL; |
| 2692 | asdl_seq_SET(args, nargs++, e); |
| 2693 | } |
| 2694 | else if (TYPE(chch) == STAR) { |
| 2695 | /* an iterable argument unpacking */ |
| 2696 | expr_ty starred; |
| 2697 | if (ndoublestars) { |
| 2698 | ast_error(c, chch, |
| 2699 | "iterable argument unpacking follows " |
| 2700 | "keyword argument unpacking"); |
| 2701 | return NULL; |
| 2702 | } |
| 2703 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2704 | if (!e) |
| 2705 | return NULL; |
| 2706 | starred = Starred(e, Load, LINENO(chch), |
| 2707 | chch->n_col_offset, |
| 2708 | c->c_arena); |
| 2709 | if (!starred) |
| 2710 | return NULL; |
| 2711 | asdl_seq_SET(args, nargs++, starred); |
| 2712 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2713 | } |
| 2714 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2715 | /* a keyword argument unpacking */ |
| 2716 | keyword_ty kw; |
| 2717 | i++; |
| 2718 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2719 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2720 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2721 | kw = keyword(NULL, e, c->c_arena); |
| 2722 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2723 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2725 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2726 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2727 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2728 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2729 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2730 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2731 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2732 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2733 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2734 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2735 | identifier key, tmp; |
| 2736 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2737 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2738 | /* chch is test, but must be an identifier? */ |
| 2739 | e = ast_for_expr(c, chch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2740 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2741 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2742 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2743 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 2744 | * SF bug 132313 points out that complaining about a keyword |
| 2745 | * then is very confusing. |
| 2746 | */ |
| 2747 | if (e->kind == Lambda_kind) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2748 | ast_error(c, chch, |
| 2749 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2750 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2751 | } |
| 2752 | else if (e->kind != Name_kind) { |
| 2753 | ast_error(c, chch, |
| 2754 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2755 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2756 | } |
| 2757 | else if (forbidden_name(c, e->v.Name.id, ch, 1)) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2758 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2759 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2760 | key = e->v.Name.id; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2761 | for (k = 0; k < nkeywords; k++) { |
| 2762 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2763 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2764 | ast_error(c, chch, |
| 2765 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2766 | return NULL; |
| 2767 | } |
| 2768 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2769 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2770 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2771 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2772 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2773 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2774 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2775 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2776 | } |
| 2777 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2780 | 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] | 2781 | } |
| 2782 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2783 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2784 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2785 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2786 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2787 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2788 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2789 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2790 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2791 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2792 | } |
| 2793 | else { |
| 2794 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2795 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2796 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2797 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2798 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2799 | else { |
| 2800 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2801 | if (!tmp) |
| 2802 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2803 | 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] | 2804 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2807 | static stmt_ty |
| 2808 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2809 | { |
| 2810 | REQ(n, expr_stmt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2812 | | ('=' (yield_expr|testlist))*) |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2813 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2814 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2815 | | '<<=' | '>>=' | '**=' | '//=' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2816 | test: ... here starts the operator precendence dance |
| 2817 | */ |
| 2818 | |
| 2819 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2820 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2821 | if (!e) |
| 2822 | return NULL; |
| 2823 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2824 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | } |
| 2826 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2827 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2828 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2829 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2830 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2831 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2832 | if (!expr1) |
| 2833 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2834 | if(!set_context(c, expr1, Store, ch)) |
| 2835 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2836 | /* set_context checks that most expressions are not the left side. |
| 2837 | Augmented assignments can only have a name, a subscript, or an |
| 2838 | attribute on the left, though, so we have to explicitly check for |
| 2839 | those. */ |
| 2840 | switch (expr1->kind) { |
| 2841 | case Name_kind: |
| 2842 | case Attribute_kind: |
| 2843 | case Subscript_kind: |
| 2844 | break; |
| 2845 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2846 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2847 | return NULL; |
| 2848 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2849 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2850 | ch = CHILD(n, 2); |
| 2851 | if (TYPE(ch) == testlist) |
| 2852 | expr2 = ast_for_testlist(c, ch); |
| 2853 | else |
| 2854 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2855 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | return NULL; |
| 2857 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2858 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2859 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2860 | return NULL; |
| 2861 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2862 | 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] | 2863 | } |
| 2864 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2865 | int i; |
| 2866 | asdl_seq *targets; |
| 2867 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2868 | expr_ty expression; |
| 2869 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2870 | /* a normal assignment */ |
| 2871 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2872 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2873 | if (!targets) |
| 2874 | return NULL; |
| 2875 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 2876 | expr_ty e; |
| 2877 | node *ch = CHILD(n, i); |
| 2878 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2879 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2880 | return NULL; |
| 2881 | } |
| 2882 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2884 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2885 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 2886 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2887 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2888 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2889 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2890 | asdl_seq_SET(targets, i / 2, e); |
| 2891 | } |
| 2892 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2893 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2894 | expression = ast_for_testlist(c, value); |
| 2895 | else |
| 2896 | expression = ast_for_expr(c, value); |
| 2897 | if (!expression) |
| 2898 | return NULL; |
| 2899 | 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] | 2900 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2903 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2905 | 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] | 2906 | { |
| 2907 | asdl_seq *seq; |
| 2908 | int i; |
| 2909 | expr_ty e; |
| 2910 | |
| 2911 | REQ(n, exprlist); |
| 2912 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2913 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2914 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2915 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2916 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2917 | e = ast_for_expr(c, CHILD(n, i)); |
| 2918 | if (!e) |
| 2919 | return NULL; |
| 2920 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2921 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2922 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2923 | } |
| 2924 | return seq; |
| 2925 | } |
| 2926 | |
| 2927 | static stmt_ty |
| 2928 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 2929 | { |
| 2930 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2931 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2932 | /* del_stmt: 'del' exprlist */ |
| 2933 | REQ(n, del_stmt); |
| 2934 | |
| 2935 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 2936 | if (!expr_list) |
| 2937 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2938 | 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] | 2939 | } |
| 2940 | |
| 2941 | static stmt_ty |
| 2942 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 2943 | { |
| 2944 | /* |
| 2945 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 2946 | | yield_stmt |
| 2947 | break_stmt: 'break' |
| 2948 | continue_stmt: 'continue' |
| 2949 | return_stmt: 'return' [testlist] |
| 2950 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2951 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2952 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 2953 | */ |
| 2954 | node *ch; |
| 2955 | |
| 2956 | REQ(n, flow_stmt); |
| 2957 | ch = CHILD(n, 0); |
| 2958 | switch (TYPE(ch)) { |
| 2959 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2960 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2961 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2962 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2963 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2964 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 2965 | if (!exp) |
| 2966 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2967 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2968 | } |
| 2969 | case return_stmt: |
| 2970 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2971 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2973 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | if (!expression) |
| 2975 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2976 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2977 | } |
| 2978 | case raise_stmt: |
| 2979 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2980 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 2981 | else if (NCH(ch) >= 2) { |
| 2982 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 2984 | if (!expression) |
| 2985 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2986 | if (NCH(ch) == 4) { |
| 2987 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 2988 | if (!cause) |
| 2989 | return NULL; |
| 2990 | } |
| 2991 | 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] | 2992 | } |
| 2993 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2994 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2995 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 2996 | return NULL; |
| 2997 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2998 | |
| 2999 | PyErr_SetString(PyExc_SystemError, "unhandled flow statement"); |
| 3000 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3004 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3005 | { |
| 3006 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3007 | import_as_name: NAME ['as' NAME] |
| 3008 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3009 | dotted_name: NAME ('.' NAME)* |
| 3010 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3011 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3012 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | loop: |
| 3014 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3015 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3016 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3017 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3018 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3019 | if (!name) |
| 3020 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3021 | if (NCH(n) == 3) { |
| 3022 | node *str_node = CHILD(n, 2); |
| 3023 | str = NEW_IDENTIFIER(str_node); |
| 3024 | if (!str) |
| 3025 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3026 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3027 | return NULL; |
| 3028 | } |
| 3029 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3030 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3031 | return NULL; |
| 3032 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3033 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3034 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3035 | case dotted_as_name: |
| 3036 | if (NCH(n) == 1) { |
| 3037 | n = CHILD(n, 0); |
| 3038 | goto loop; |
| 3039 | } |
| 3040 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3041 | node *asname_node = CHILD(n, 2); |
| 3042 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3043 | if (!a) |
| 3044 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3045 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3046 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3047 | if (!a->asname) |
| 3048 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3049 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3050 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3051 | return a; |
| 3052 | } |
| 3053 | break; |
| 3054 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3055 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3056 | node *name_node = CHILD(n, 0); |
| 3057 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3058 | if (!name) |
| 3059 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3060 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3061 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3062 | return alias(name, NULL, c->c_arena); |
| 3063 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3064 | else { |
| 3065 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3066 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3067 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3068 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3070 | |
| 3071 | len = 0; |
| 3072 | for (i = 0; i < NCH(n); i += 2) |
| 3073 | /* length of string plus one for the dot */ |
| 3074 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3075 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3076 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3077 | if (!str) |
| 3078 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3079 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3080 | if (!s) |
| 3081 | return NULL; |
| 3082 | for (i = 0; i < NCH(n); i += 2) { |
| 3083 | char *sch = STR(CHILD(n, i)); |
| 3084 | strcpy(s, STR(CHILD(n, i))); |
| 3085 | s += strlen(sch); |
| 3086 | *s++ = '.'; |
| 3087 | } |
| 3088 | --s; |
| 3089 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3091 | PyBytes_GET_SIZE(str), |
| 3092 | NULL); |
| 3093 | Py_DECREF(str); |
| 3094 | if (!uni) |
| 3095 | return NULL; |
| 3096 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3097 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3098 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3099 | Py_DECREF(str); |
| 3100 | return NULL; |
| 3101 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3102 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3103 | } |
| 3104 | break; |
| 3105 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3106 | str = PyUnicode_InternFromString("*"); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3107 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3108 | Py_DECREF(str); |
| 3109 | return NULL; |
| 3110 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3111 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3113 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3114 | "unexpected import name: %d", TYPE(n)); |
| 3115 | return NULL; |
| 3116 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3117 | |
| 3118 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | return NULL; |
| 3120 | } |
| 3121 | |
| 3122 | static stmt_ty |
| 3123 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3124 | { |
| 3125 | /* |
| 3126 | import_stmt: import_name | import_from |
| 3127 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3128 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3129 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3130 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3131 | int lineno; |
| 3132 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3133 | int i; |
| 3134 | asdl_seq *aliases; |
| 3135 | |
| 3136 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3137 | lineno = LINENO(n); |
| 3138 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3139 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3140 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3141 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3142 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3143 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3144 | if (!aliases) |
| 3145 | return NULL; |
| 3146 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3147 | 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] | 3148 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3149 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3150 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3151 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3152 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3153 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3154 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3155 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3156 | int idx, ndots = 0; |
| 3157 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3158 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3159 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3160 | /* Count the number of dots (for relative imports) and check for the |
| 3161 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3162 | for (idx = 1; idx < NCH(n); idx++) { |
| 3163 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3164 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3165 | if (!mod) |
| 3166 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3167 | idx++; |
| 3168 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3169 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3171 | ndots += 3; |
| 3172 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3173 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3174 | break; |
| 3175 | } |
| 3176 | ndots++; |
| 3177 | } |
| 3178 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3179 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3180 | case STAR: |
| 3181 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3182 | n = CHILD(n, idx); |
| 3183 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3184 | break; |
| 3185 | case LPAR: |
| 3186 | /* from ... import (x, y, z) */ |
| 3187 | n = CHILD(n, idx + 1); |
| 3188 | n_children = NCH(n); |
| 3189 | break; |
| 3190 | case import_as_names: |
| 3191 | /* from ... import x, y, z */ |
| 3192 | n = CHILD(n, idx); |
| 3193 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3194 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3195 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3196 | " surrounding parentheses"); |
| 3197 | return NULL; |
| 3198 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3201 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3202 | return NULL; |
| 3203 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3204 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3205 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3206 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3208 | |
| 3209 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3210 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3211 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3212 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3213 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3214 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3215 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3216 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3217 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3218 | 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] | 3219 | if (!import_alias) |
| 3220 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3221 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3222 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3223 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3224 | if (mod != NULL) |
| 3225 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3226 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3227 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3228 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3229 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3230 | "unknown import statement: starts with command '%s'", |
| 3231 | STR(CHILD(n, 0))); |
| 3232 | return NULL; |
| 3233 | } |
| 3234 | |
| 3235 | static stmt_ty |
| 3236 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3237 | { |
| 3238 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3239 | identifier name; |
| 3240 | asdl_seq *s; |
| 3241 | int i; |
| 3242 | |
| 3243 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3244 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3245 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3246 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3247 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3248 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3249 | if (!name) |
| 3250 | return NULL; |
| 3251 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3252 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3253 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3257 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3258 | { |
| 3259 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3260 | identifier name; |
| 3261 | asdl_seq *s; |
| 3262 | int i; |
| 3263 | |
| 3264 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3265 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3266 | if (!s) |
| 3267 | return NULL; |
| 3268 | for (i = 1; i < NCH(n); i += 2) { |
| 3269 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3270 | if (!name) |
| 3271 | return NULL; |
| 3272 | asdl_seq_SET(s, i / 2, name); |
| 3273 | } |
| 3274 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3275 | } |
| 3276 | |
| 3277 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3278 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3279 | { |
| 3280 | /* assert_stmt: 'assert' test [',' test] */ |
| 3281 | REQ(n, assert_stmt); |
| 3282 | if (NCH(n) == 2) { |
| 3283 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3284 | if (!expression) |
| 3285 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3286 | 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] | 3287 | } |
| 3288 | else if (NCH(n) == 4) { |
| 3289 | expr_ty expr1, expr2; |
| 3290 | |
| 3291 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3292 | if (!expr1) |
| 3293 | return NULL; |
| 3294 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3295 | if (!expr2) |
| 3296 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3298 | 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] | 3299 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3300 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3301 | "improper number of parts to 'assert' statement: %d", |
| 3302 | NCH(n)); |
| 3303 | return NULL; |
| 3304 | } |
| 3305 | |
| 3306 | static asdl_seq * |
| 3307 | ast_for_suite(struct compiling *c, const node *n) |
| 3308 | { |
| 3309 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3310 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3311 | stmt_ty s; |
| 3312 | int i, total, num, end, pos = 0; |
| 3313 | node *ch; |
| 3314 | |
| 3315 | REQ(n, suite); |
| 3316 | |
| 3317 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3318 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3319 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3320 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3321 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3322 | n = CHILD(n, 0); |
| 3323 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3325 | */ |
| 3326 | end = NCH(n) - 1; |
| 3327 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3328 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3329 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3330 | for (i = 0; i < end; i += 2) { |
| 3331 | ch = CHILD(n, i); |
| 3332 | s = ast_for_stmt(c, ch); |
| 3333 | if (!s) |
| 3334 | return NULL; |
| 3335 | asdl_seq_SET(seq, pos++, s); |
| 3336 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3337 | } |
| 3338 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3339 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3340 | ch = CHILD(n, i); |
| 3341 | REQ(ch, stmt); |
| 3342 | num = num_stmts(ch); |
| 3343 | if (num == 1) { |
| 3344 | /* small_stmt or compound_stmt with only one child */ |
| 3345 | s = ast_for_stmt(c, ch); |
| 3346 | if (!s) |
| 3347 | return NULL; |
| 3348 | asdl_seq_SET(seq, pos++, s); |
| 3349 | } |
| 3350 | else { |
| 3351 | int j; |
| 3352 | ch = CHILD(ch, 0); |
| 3353 | REQ(ch, simple_stmt); |
| 3354 | for (j = 0; j < NCH(ch); j += 2) { |
| 3355 | /* statement terminates with a semi-colon ';' */ |
| 3356 | if (NCH(CHILD(ch, j)) == 0) { |
| 3357 | assert((j + 1) == NCH(ch)); |
| 3358 | break; |
| 3359 | } |
| 3360 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3361 | if (!s) |
| 3362 | return NULL; |
| 3363 | asdl_seq_SET(seq, pos++, s); |
| 3364 | } |
| 3365 | } |
| 3366 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3367 | } |
| 3368 | assert(pos == seq->size); |
| 3369 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | static stmt_ty |
| 3373 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3374 | { |
| 3375 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3376 | ['else' ':' suite] |
| 3377 | */ |
| 3378 | char *s; |
| 3379 | |
| 3380 | REQ(n, if_stmt); |
| 3381 | |
| 3382 | if (NCH(n) == 4) { |
| 3383 | expr_ty expression; |
| 3384 | asdl_seq *suite_seq; |
| 3385 | |
| 3386 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3387 | if (!expression) |
| 3388 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3390 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3391 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3393 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3394 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3395 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3396 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3397 | s = STR(CHILD(n, 4)); |
| 3398 | /* s[2], the third character in the string, will be |
| 3399 | 's' for el_s_e, or |
| 3400 | 'i' for el_i_f |
| 3401 | */ |
| 3402 | if (s[2] == 's') { |
| 3403 | expr_ty expression; |
| 3404 | asdl_seq *seq1, *seq2; |
| 3405 | |
| 3406 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3407 | if (!expression) |
| 3408 | return NULL; |
| 3409 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3410 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3411 | return NULL; |
| 3412 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3413 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3414 | return NULL; |
| 3415 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3416 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3417 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3418 | } |
| 3419 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3420 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3421 | expr_ty expression; |
| 3422 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3423 | asdl_seq *orelse = NULL; |
| 3424 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3425 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3426 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3427 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3428 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3429 | has_else = 1; |
| 3430 | n_elif -= 3; |
| 3431 | } |
| 3432 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3433 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3434 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3435 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3437 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3438 | if (!orelse) |
| 3439 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3440 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3441 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3442 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3443 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3444 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3445 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3446 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3447 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3448 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | asdl_seq_SET(orelse, 0, |
| 3451 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3452 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3453 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3454 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3455 | /* the just-created orelse handled the last elif */ |
| 3456 | n_elif--; |
| 3457 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3459 | for (i = 0; i < n_elif; i++) { |
| 3460 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3461 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3462 | if (!newobj) |
| 3463 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3464 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3465 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3466 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3468 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3469 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3470 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3471 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3473 | LINENO(CHILD(n, off)), |
| 3474 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3475 | orelse = newobj; |
| 3476 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3477 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3478 | if (!expression) |
| 3479 | return NULL; |
| 3480 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3481 | if (!suite_seq) |
| 3482 | return NULL; |
| 3483 | return If(expression, suite_seq, orelse, |
| 3484 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3485 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3486 | |
| 3487 | PyErr_Format(PyExc_SystemError, |
| 3488 | "unexpected token in 'if' statement: %s", s); |
| 3489 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | static stmt_ty |
| 3493 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3494 | { |
| 3495 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3496 | REQ(n, while_stmt); |
| 3497 | |
| 3498 | if (NCH(n) == 4) { |
| 3499 | expr_ty expression; |
| 3500 | asdl_seq *suite_seq; |
| 3501 | |
| 3502 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3503 | if (!expression) |
| 3504 | return NULL; |
| 3505 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3506 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3507 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3508 | 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] | 3509 | } |
| 3510 | else if (NCH(n) == 7) { |
| 3511 | expr_ty expression; |
| 3512 | asdl_seq *seq1, *seq2; |
| 3513 | |
| 3514 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3515 | if (!expression) |
| 3516 | return NULL; |
| 3517 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3518 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3519 | return NULL; |
| 3520 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3521 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3522 | return NULL; |
| 3523 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3524 | 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] | 3525 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3526 | |
| 3527 | PyErr_Format(PyExc_SystemError, |
| 3528 | "wrong number of tokens for 'while' statement: %d", |
| 3529 | NCH(n)); |
| 3530 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3534 | 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] | 3535 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3536 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3537 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3538 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3539 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3540 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3541 | REQ(n, for_stmt); |
| 3542 | |
| 3543 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3544 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | if (!seq) |
| 3546 | return NULL; |
| 3547 | } |
| 3548 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3549 | node_target = CHILD(n, 1); |
| 3550 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3551 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3552 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3553 | /* Check the # of children rather than the length of _target, since |
| 3554 | 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] | 3555 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3556 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3557 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3558 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3559 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3560 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3561 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3562 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3563 | return NULL; |
| 3564 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3565 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3566 | return NULL; |
| 3567 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3568 | if (is_async) |
| 3569 | return AsyncFor(target, expression, suite_seq, seq, |
| 3570 | LINENO(n), n->n_col_offset, |
| 3571 | c->c_arena); |
| 3572 | else |
| 3573 | return For(target, expression, suite_seq, seq, |
| 3574 | LINENO(n), n->n_col_offset, |
| 3575 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | static excepthandler_ty |
| 3579 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3580 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3581 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3582 | REQ(exc, except_clause); |
| 3583 | REQ(body, suite); |
| 3584 | |
| 3585 | if (NCH(exc) == 1) { |
| 3586 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3587 | if (!suite_seq) |
| 3588 | return NULL; |
| 3589 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3590 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3591 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3592 | } |
| 3593 | else if (NCH(exc) == 2) { |
| 3594 | expr_ty expression; |
| 3595 | asdl_seq *suite_seq; |
| 3596 | |
| 3597 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3598 | if (!expression) |
| 3599 | return NULL; |
| 3600 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3601 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | return NULL; |
| 3603 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3604 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3605 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 | } |
| 3607 | else if (NCH(exc) == 4) { |
| 3608 | asdl_seq *suite_seq; |
| 3609 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3610 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3611 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3612 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3613 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3614 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3615 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3616 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | return NULL; |
| 3618 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3619 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3620 | return NULL; |
| 3621 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3622 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3623 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3624 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3625 | |
| 3626 | PyErr_Format(PyExc_SystemError, |
| 3627 | "wrong number of children for 'except' clause: %d", |
| 3628 | NCH(exc)); |
| 3629 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | static stmt_ty |
| 3633 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3634 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3635 | const int nch = NCH(n); |
| 3636 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3637 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3638 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | REQ(n, try_stmt); |
| 3640 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3641 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3642 | if (body == NULL) |
| 3643 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3645 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3646 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3647 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3648 | /* we can assume it's an "else", |
| 3649 | because nch >= 9 for try-else-finally and |
| 3650 | it would otherwise have a type of except_clause */ |
| 3651 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3652 | if (orelse == NULL) |
| 3653 | return NULL; |
| 3654 | n_except--; |
| 3655 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3656 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3657 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3658 | if (finally == NULL) |
| 3659 | return NULL; |
| 3660 | n_except--; |
| 3661 | } |
| 3662 | else { |
| 3663 | /* we can assume it's an "else", |
| 3664 | otherwise it would have a type of except_clause */ |
| 3665 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3666 | if (orelse == NULL) |
| 3667 | return NULL; |
| 3668 | n_except--; |
| 3669 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3670 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3671 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3672 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3673 | return NULL; |
| 3674 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3676 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3677 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3678 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3679 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3680 | if (handlers == NULL) |
| 3681 | return NULL; |
| 3682 | |
| 3683 | for (i = 0; i < n_except; i++) { |
| 3684 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3685 | CHILD(n, 5 + i * 3)); |
| 3686 | if (!e) |
| 3687 | return NULL; |
| 3688 | asdl_seq_SET(handlers, i, e); |
| 3689 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3692 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3693 | 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] | 3694 | } |
| 3695 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3696 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3697 | static withitem_ty |
| 3698 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3699 | { |
| 3700 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3701 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3702 | REQ(n, with_item); |
| 3703 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3704 | if (!context_expr) |
| 3705 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3706 | if (NCH(n) == 3) { |
| 3707 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3708 | |
| 3709 | if (!optional_vars) { |
| 3710 | return NULL; |
| 3711 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3712 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3713 | return NULL; |
| 3714 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3717 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3720 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3721 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3722 | 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] | 3723 | { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3724 | int i, n_items; |
| 3725 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3726 | |
| 3727 | REQ(n, with_stmt); |
| 3728 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3729 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3730 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3731 | if (!items) |
| 3732 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3733 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3734 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3735 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3736 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3737 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3740 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3741 | if (!body) |
| 3742 | return NULL; |
| 3743 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3744 | if (is_async) |
| 3745 | return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
| 3746 | else |
| 3747 | 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] | 3748 | } |
| 3749 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3750 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3751 | 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] | 3752 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3753 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3754 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3755 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3756 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3757 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3758 | REQ(n, classdef); |
| 3759 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3760 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3761 | s = ast_for_suite(c, CHILD(n, 3)); |
| 3762 | if (!s) |
| 3763 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3764 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3765 | if (!classname) |
| 3766 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3767 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3768 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3769 | return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n), |
| 3770 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3771 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3772 | |
| 3773 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3774 | s = ast_for_suite(c, CHILD(n,5)); |
| 3775 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3776 | return NULL; |
| 3777 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3778 | if (!classname) |
| 3779 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3780 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3781 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3782 | return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n), |
| 3783 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3786 | /* class NAME '(' arglist ')' ':' suite */ |
| 3787 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3788 | { |
| 3789 | PyObject *dummy_name; |
| 3790 | expr_ty dummy; |
| 3791 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3792 | if (!dummy_name) |
| 3793 | return NULL; |
| 3794 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 3795 | call = ast_for_call(c, CHILD(n, 3), dummy); |
| 3796 | if (!call) |
| 3797 | return NULL; |
| 3798 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3799 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3800 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3801 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3802 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3803 | if (!classname) |
| 3804 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3805 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3806 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3807 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3808 | 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] | 3809 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | static stmt_ty |
| 3813 | ast_for_stmt(struct compiling *c, const node *n) |
| 3814 | { |
| 3815 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3816 | assert(NCH(n) == 1); |
| 3817 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3818 | } |
| 3819 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3820 | assert(num_stmts(n) == 1); |
| 3821 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3822 | } |
| 3823 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3824 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3825 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3826 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3827 | */ |
| 3828 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3829 | case expr_stmt: |
| 3830 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3831 | case del_stmt: |
| 3832 | return ast_for_del_stmt(c, n); |
| 3833 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3834 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3835 | case flow_stmt: |
| 3836 | return ast_for_flow_stmt(c, n); |
| 3837 | case import_stmt: |
| 3838 | return ast_for_import_stmt(c, n); |
| 3839 | case global_stmt: |
| 3840 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3841 | case nonlocal_stmt: |
| 3842 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3843 | case assert_stmt: |
| 3844 | return ast_for_assert_stmt(c, n); |
| 3845 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3846 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3847 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3848 | TYPE(n), NCH(n)); |
| 3849 | return NULL; |
| 3850 | } |
| 3851 | } |
| 3852 | else { |
| 3853 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3854 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3855 | */ |
| 3856 | node *ch = CHILD(n, 0); |
| 3857 | REQ(n, compound_stmt); |
| 3858 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3859 | case if_stmt: |
| 3860 | return ast_for_if_stmt(c, ch); |
| 3861 | case while_stmt: |
| 3862 | return ast_for_while_stmt(c, ch); |
| 3863 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3864 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3865 | case try_stmt: |
| 3866 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3867 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3868 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3869 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3870 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3871 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3872 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | case decorated: |
| 3874 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3875 | case async_stmt: |
| 3876 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3877 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3878 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3879 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3880 | TYPE(n), NCH(n)); |
| 3881 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3882 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3887 | parsenumber(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3888 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3889 | const char *end; |
| 3890 | long x; |
| 3891 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3892 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3893 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3894 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 3895 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3896 | errno = 0; |
| 3897 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3898 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3899 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3900 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3901 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3902 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3903 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3904 | } |
| 3905 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3906 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3907 | if (*end == '\0') { |
| 3908 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3909 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3910 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3911 | } |
| 3912 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3913 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3914 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3915 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 3916 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 3917 | return NULL; |
| 3918 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3919 | } |
| 3920 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3921 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3922 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 3923 | if (dx == -1.0 && PyErr_Occurred()) |
| 3924 | return NULL; |
| 3925 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3926 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3930 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3931 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 3932 | const char *s, *t; |
| 3933 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3934 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 3935 | while (s < end && (*s & 0x80)) s++; |
| 3936 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3937 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3941 | decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3942 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3943 | PyObject *v, *u; |
| 3944 | char *buf; |
| 3945 | char *p; |
| 3946 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3947 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3948 | if (encoding == NULL) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3949 | u = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3950 | } else { |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 3951 | /* check for integer overflow */ |
Benjamin Peterson | b2e796a | 2009-10-28 21:59:39 +0000 | [diff] [blame] | 3952 | if (len > PY_SIZE_MAX / 6) |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 3953 | return NULL; |
Benjamin Peterson | b2e796a | 2009-10-28 21:59:39 +0000 | [diff] [blame] | 3954 | /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 3955 | "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 3956 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3957 | if (u == NULL) |
| 3958 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3959 | p = buf = PyBytes_AsString(u); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3960 | end = s + len; |
| 3961 | while (s < end) { |
| 3962 | if (*s == '\\') { |
| 3963 | *p++ = *s++; |
| 3964 | if (*s & 0x80) { |
| 3965 | strcpy(p, "u005c"); |
| 3966 | p += 5; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3967 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3968 | } |
| 3969 | if (*s & 0x80) { /* XXX inefficient */ |
| 3970 | PyObject *w; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3971 | int kind; |
| 3972 | void *data; |
| 3973 | Py_ssize_t len, i; |
| 3974 | w = decode_utf8(c, &s, end); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3975 | if (w == NULL) { |
| 3976 | Py_DECREF(u); |
| 3977 | return NULL; |
| 3978 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3979 | kind = PyUnicode_KIND(w); |
| 3980 | data = PyUnicode_DATA(w); |
| 3981 | len = PyUnicode_GET_LENGTH(w); |
| 3982 | for (i = 0; i < len; i++) { |
| 3983 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 3984 | sprintf(p, "\\U%08x", chr); |
Benjamin Peterson | b2e796a | 2009-10-28 21:59:39 +0000 | [diff] [blame] | 3985 | p += 10; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3986 | } |
Benjamin Peterson | b2e796a | 2009-10-28 21:59:39 +0000 | [diff] [blame] | 3987 | /* Should be impossible to overflow */ |
| 3988 | assert(p - buf <= Py_SIZE(u)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3989 | Py_DECREF(w); |
| 3990 | } else { |
| 3991 | *p++ = *s++; |
| 3992 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3993 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3994 | len = p - buf; |
| 3995 | s = buf; |
| 3996 | } |
| 3997 | if (rawmode) |
| 3998 | v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); |
| 3999 | else |
| 4000 | v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
| 4001 | Py_XDECREF(u); |
| 4002 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | /* s is a Python string literal, including the bracketing quote characters, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4006 | * and r &/or b prefixes (if any), and embedded escape sequences (if any). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4007 | * parsestr parses it, and returns the decoded Python string object. |
| 4008 | */ |
| 4009 | static PyObject * |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4010 | parsestr(struct compiling *c, const node *n, int *bytesmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4011 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4012 | size_t len; |
| 4013 | const char *s = STR(n); |
| 4014 | int quote = Py_CHARMASK(*s); |
| 4015 | int rawmode = 0; |
| 4016 | int need_encoding; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 4017 | if (Py_ISALPHA(quote)) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 4018 | while (!*bytesmode || !rawmode) { |
| 4019 | if (quote == 'b' || quote == 'B') { |
| 4020 | quote = *++s; |
| 4021 | *bytesmode = 1; |
| 4022 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 4023 | else if (quote == 'u' || quote == 'U') { |
| 4024 | quote = *++s; |
| 4025 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 4026 | else if (quote == 'r' || quote == 'R') { |
| 4027 | quote = *++s; |
| 4028 | rawmode = 1; |
| 4029 | } |
| 4030 | else { |
| 4031 | break; |
| 4032 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4033 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4034 | } |
| 4035 | if (quote != '\'' && quote != '\"') { |
| 4036 | PyErr_BadInternalCall(); |
| 4037 | return NULL; |
| 4038 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4039 | s++; |
| 4040 | len = strlen(s); |
| 4041 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4042 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4043 | "string to parse is too long"); |
| 4044 | return NULL; |
| 4045 | } |
| 4046 | if (s[--len] != quote) { |
| 4047 | PyErr_BadInternalCall(); |
| 4048 | return NULL; |
| 4049 | } |
| 4050 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 4051 | s += 2; |
| 4052 | len -= 2; |
| 4053 | if (s[--len] != quote || s[--len] != quote) { |
| 4054 | PyErr_BadInternalCall(); |
| 4055 | return NULL; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 4056 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4057 | } |
Benjamin Peterson | 8dbca06 | 2008-04-05 14:49:54 +0000 | [diff] [blame] | 4058 | if (!*bytesmode && !rawmode) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4059 | return decode_unicode(c, s, len, rawmode, c->c_encoding); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4060 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4061 | if (*bytesmode) { |
| 4062 | /* Disallow non-ascii characters (but not escapes) */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4063 | const char *ch; |
| 4064 | for (ch = s; *ch; ch++) { |
| 4065 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 4066 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4067 | "literal characters."); |
| 4068 | return NULL; |
| 4069 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 4070 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4071 | } |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4072 | need_encoding = (!*bytesmode && c->c_encoding != NULL && |
Brett Cannon | da78043 | 2008-10-17 03:38:50 +0000 | [diff] [blame] | 4073 | strcmp(c->c_encoding, "utf-8") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4074 | if (rawmode || strchr(s, '\\') == NULL) { |
| 4075 | if (need_encoding) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4076 | PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL); |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4077 | if (u == NULL || !*bytesmode) |
| 4078 | return u; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4079 | v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4080 | Py_DECREF(u); |
| 4081 | return v; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4082 | } else if (*bytesmode) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4083 | return PyBytes_FromStringAndSize(s, len); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4084 | } else if (strcmp(c->c_encoding, "utf-8") == 0) { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4085 | return PyUnicode_FromStringAndSize(s, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4086 | } else { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4087 | return PyUnicode_DecodeLatin1(s, len, NULL); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4088 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4089 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4090 | return PyBytes_DecodeEscape(s, len, NULL, 1, |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4091 | need_encoding ? c->c_encoding : NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4094 | /* Build a Python string object out of a STRING+ atom. This takes care of |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4095 | * compile-time literal catenation, calling parsestr() on each piece, and |
| 4096 | * pasting the intermediate results together. |
| 4097 | */ |
| 4098 | static PyObject * |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 4099 | parsestrplus(struct compiling *c, const node *n, int *bytesmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4100 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4101 | PyObject *v; |
| 4102 | int i; |
| 4103 | REQ(CHILD(n, 0), STRING); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4104 | v = parsestr(c, CHILD(n, 0), bytesmode); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4105 | if (v != NULL) { |
| 4106 | /* String literal concatenation */ |
| 4107 | for (i = 1; i < NCH(n); i++) { |
| 4108 | PyObject *s; |
| 4109 | int subbm = 0; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 4110 | s = parsestr(c, CHILD(n, i), &subbm); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4111 | if (s == NULL) |
| 4112 | goto onError; |
| 4113 | if (*bytesmode != subbm) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4114 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Christian Heimes | 3d46339 | 2012-09-10 16:52:42 +0200 | [diff] [blame] | 4115 | Py_DECREF(s); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4116 | goto onError; |
| 4117 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4118 | if (PyBytes_Check(v) && PyBytes_Check(s)) { |
| 4119 | PyBytes_ConcatAndDel(&v, s); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4120 | if (v == NULL) |
| 4121 | goto onError; |
| 4122 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4123 | else { |
| 4124 | PyObject *temp = PyUnicode_Concat(v, s); |
| 4125 | Py_DECREF(s); |
| 4126 | Py_DECREF(v); |
| 4127 | v = temp; |
| 4128 | if (v == NULL) |
| 4129 | goto onError; |
| 4130 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4131 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4132 | } |
| 4133 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4134 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4135 | onError: |
| 4136 | Py_XDECREF(v); |
| 4137 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4138 | } |