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