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