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