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 |
| 3 | * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
| 4 | * |
| 5 | */ |
| 6 | #include "Python.h" |
| 7 | #include "Python-ast.h" |
| 8 | #include "grammar.h" |
| 9 | #include "node.h" |
| 10 | #include "ast.h" |
| 11 | #include "token.h" |
| 12 | #include "parsetok.h" |
| 13 | #include "graminit.h" |
| 14 | |
| 15 | #include <assert.h> |
| 16 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 17 | /* XXX TO DO |
| 18 | - re-indent this file (should be done) |
| 19 | - internal error checking (freeing memory, etc.) |
| 20 | - syntax errors |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | /* Data structure used internally */ |
| 25 | struct compiling { |
| 26 | char *c_encoding; /* source encoding */ |
| 27 | }; |
| 28 | |
| 29 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 30 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 31 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
| 32 | static asdl_seq *ast_for_suite(struct compiling *, const node *); |
| 33 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 34 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
| 35 | static expr_ty ast_for_testlist_gexp(struct compiling *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 36 | |
| 37 | /* Note different signature for ast_for_call */ |
| 38 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); |
| 39 | |
| 40 | static PyObject *parsenumber(const char *); |
| 41 | static PyObject *parsestr(const char *s, const char *encoding); |
| 42 | static PyObject *parsestrplus(struct compiling *, const node *n); |
| 43 | |
| 44 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
| 45 | |
| 46 | #ifndef LINENO |
| 47 | #define LINENO(n) ((n)->n_lineno) |
| 48 | #endif |
| 49 | |
| 50 | #define NEW_IDENTIFIER(n) PyString_InternFromString(STR(n)) |
| 51 | |
| 52 | static void |
| 53 | asdl_stmt_seq_free(asdl_seq* seq) |
| 54 | { |
| 55 | int n, i; |
| 56 | |
| 57 | if (!seq) |
| 58 | return; |
| 59 | |
| 60 | n = asdl_seq_LEN(seq); |
| 61 | for (i = 0; i < n; i++) |
| 62 | free_stmt(asdl_seq_GET(seq, i)); |
| 63 | asdl_seq_free(seq); |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | asdl_expr_seq_free(asdl_seq* seq) |
| 68 | { |
| 69 | int n, i; |
| 70 | |
| 71 | if (!seq) |
| 72 | return; |
| 73 | |
| 74 | n = asdl_seq_LEN(seq); |
| 75 | for (i = 0; i < n; i++) |
| 76 | free_expr(asdl_seq_GET(seq, i)); |
| 77 | asdl_seq_free(seq); |
| 78 | } |
| 79 | |
| 80 | /* This routine provides an invalid object for the syntax error. |
| 81 | The outermost routine must unpack this error and create the |
| 82 | proper object. We do this so that we don't have to pass |
| 83 | the filename to everything function. |
| 84 | |
| 85 | XXX Maybe we should just pass the filename... |
| 86 | */ |
| 87 | |
| 88 | static int |
| 89 | ast_error(const node *n, const char *errstr) |
| 90 | { |
| 91 | PyObject *u = Py_BuildValue("zi", errstr, LINENO(n)); |
| 92 | if (!u) |
| 93 | return 0; |
| 94 | PyErr_SetObject(PyExc_SyntaxError, u); |
| 95 | Py_DECREF(u); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | ast_error_finish(const char *filename) |
| 101 | { |
| 102 | PyObject *type, *value, *tback, *errstr, *loc, *tmp; |
| 103 | int lineno; |
| 104 | |
| 105 | assert(PyErr_Occurred()); |
| 106 | if (!PyErr_ExceptionMatches(PyExc_SyntaxError)) |
| 107 | return; |
| 108 | |
| 109 | PyErr_Fetch(&type, &value, &tback); |
| 110 | errstr = PyTuple_GetItem(value, 0); |
| 111 | if (!errstr) |
| 112 | return; |
| 113 | Py_INCREF(errstr); |
| 114 | lineno = PyInt_AsLong(PyTuple_GetItem(value, 1)); |
| 115 | if (lineno == -1) |
| 116 | return; |
| 117 | Py_DECREF(value); |
| 118 | |
| 119 | loc = PyErr_ProgramText(filename, lineno); |
| 120 | if (!loc) { |
| 121 | Py_INCREF(Py_None); |
| 122 | loc = Py_None; |
| 123 | } |
| 124 | tmp = Py_BuildValue("(ziOO)", filename, lineno, Py_None, loc); |
| 125 | Py_DECREF(loc); |
| 126 | if (!tmp) |
| 127 | return; |
| 128 | value = Py_BuildValue("(OO)", errstr, tmp); |
| 129 | Py_DECREF(errstr); |
| 130 | Py_DECREF(tmp); |
| 131 | if (!value) |
| 132 | return; |
| 133 | PyErr_Restore(type, value, tback); |
| 134 | } |
| 135 | |
| 136 | /* num_stmts() returns number of contained statements. |
| 137 | |
| 138 | Use this routine to determine how big a sequence is needed for |
| 139 | the statements in a parse tree. Its raison d'etre is this bit of |
| 140 | grammar: |
| 141 | |
| 142 | stmt: simple_stmt | compound_stmt |
| 143 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 144 | |
| 145 | A simple_stmt can contain multiple small_stmt elements joined |
| 146 | by semicolons. If the arg is a simple_stmt, the number of |
| 147 | small_stmt elements is returned. |
| 148 | */ |
| 149 | |
| 150 | static int |
| 151 | num_stmts(const node *n) |
| 152 | { |
| 153 | int i, l; |
| 154 | node *ch; |
| 155 | |
| 156 | switch (TYPE(n)) { |
| 157 | case single_input: |
| 158 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 159 | return 0; |
| 160 | else |
| 161 | return num_stmts(CHILD(n, 0)); |
| 162 | case file_input: |
| 163 | l = 0; |
| 164 | for (i = 0; i < NCH(n); i++) { |
| 165 | ch = CHILD(n, i); |
| 166 | if (TYPE(ch) == stmt) |
| 167 | l += num_stmts(ch); |
| 168 | } |
| 169 | return l; |
| 170 | case stmt: |
| 171 | return num_stmts(CHILD(n, 0)); |
| 172 | case compound_stmt: |
| 173 | return 1; |
| 174 | case simple_stmt: |
| 175 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 176 | case suite: |
| 177 | if (NCH(n) == 1) |
| 178 | return num_stmts(CHILD(n, 0)); |
| 179 | else { |
| 180 | l = 0; |
| 181 | for (i = 2; i < (NCH(n) - 1); i++) |
| 182 | l += num_stmts(CHILD(n, i)); |
| 183 | return l; |
| 184 | } |
| 185 | default: { |
| 186 | char buf[128]; |
| 187 | |
| 188 | sprintf(buf, "Non-statement found: %d %d\n", |
| 189 | TYPE(n), NCH(n)); |
| 190 | Py_FatalError(buf); |
| 191 | } |
| 192 | } |
| 193 | assert(0); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* Transform the CST rooted at node * to the appropriate AST |
| 198 | */ |
| 199 | |
| 200 | mod_ty |
| 201 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename) |
| 202 | { |
| 203 | int i, j, num; |
| 204 | asdl_seq *stmts = NULL; |
| 205 | stmt_ty s; |
| 206 | node *ch; |
| 207 | struct compiling c; |
| 208 | |
| 209 | if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { |
| 210 | c.c_encoding = "utf-8"; |
| 211 | } else if (TYPE(n) == encoding_decl) { |
| 212 | c.c_encoding = STR(n); |
| 213 | n = CHILD(n, 0); |
| 214 | } else { |
| 215 | c.c_encoding = NULL; |
| 216 | } |
| 217 | |
| 218 | switch (TYPE(n)) { |
| 219 | case file_input: |
| 220 | stmts = asdl_seq_new(num_stmts(n)); |
| 221 | if (!stmts) |
| 222 | return NULL; |
| 223 | for (i = 0; i < NCH(n) - 1; i++) { |
| 224 | ch = CHILD(n, i); |
| 225 | if (TYPE(ch) == NEWLINE) |
| 226 | continue; |
| 227 | REQ(ch, stmt); |
| 228 | num = num_stmts(ch); |
| 229 | if (num == 1) { |
| 230 | s = ast_for_stmt(&c, ch); |
| 231 | if (!s) |
| 232 | goto error; |
| 233 | asdl_seq_APPEND(stmts, s); |
| 234 | } |
| 235 | else { |
| 236 | ch = CHILD(ch, 0); |
| 237 | REQ(ch, simple_stmt); |
| 238 | for (j = 0; j < num; j++) { |
| 239 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 240 | if (!s) |
| 241 | goto error; |
| 242 | asdl_seq_APPEND(stmts, s); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | return Module(stmts); |
| 247 | case eval_input: { |
| 248 | expr_ty testlist_ast; |
| 249 | |
| 250 | /* XXX Why not gen_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 251 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 252 | if (!testlist_ast) |
| 253 | goto error; |
| 254 | return Expression(testlist_ast); |
| 255 | } |
| 256 | case single_input: |
| 257 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
| 258 | stmts = asdl_seq_new(1); |
| 259 | if (!stmts) |
| 260 | goto error; |
| 261 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno)); |
| 262 | return Interactive(stmts); |
| 263 | } |
| 264 | else { |
| 265 | n = CHILD(n, 0); |
| 266 | num = num_stmts(n); |
| 267 | stmts = asdl_seq_new(num); |
| 268 | if (!stmts) |
| 269 | goto error; |
| 270 | if (num == 1) { |
| 271 | stmt_ty s = ast_for_stmt(&c, n); |
| 272 | if (!s) |
| 273 | goto error; |
| 274 | asdl_seq_SET(stmts, 0, s); |
| 275 | } |
| 276 | else { |
| 277 | /* Only a simple_stmt can contain multiple statements. */ |
| 278 | REQ(n, simple_stmt); |
| 279 | for (i = 0; i < NCH(n); i += 2) { |
| 280 | stmt_ty s; |
| 281 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 282 | break; |
| 283 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 284 | if (!s) |
| 285 | goto error; |
| 286 | asdl_seq_SET(stmts, i / 2, s); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return Interactive(stmts); |
| 291 | } |
| 292 | default: |
| 293 | goto error; |
| 294 | } |
| 295 | error: |
| 296 | if (stmts) |
| 297 | asdl_stmt_seq_free(stmts); |
| 298 | ast_error_finish(filename); |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 303 | */ |
| 304 | |
| 305 | static operator_ty |
| 306 | get_operator(const node *n) |
| 307 | { |
| 308 | switch (TYPE(n)) { |
| 309 | case VBAR: |
| 310 | return BitOr; |
| 311 | case CIRCUMFLEX: |
| 312 | return BitXor; |
| 313 | case AMPER: |
| 314 | return BitAnd; |
| 315 | case LEFTSHIFT: |
| 316 | return LShift; |
| 317 | case RIGHTSHIFT: |
| 318 | return RShift; |
| 319 | case PLUS: |
| 320 | return Add; |
| 321 | case MINUS: |
| 322 | return Sub; |
| 323 | case STAR: |
| 324 | return Mult; |
| 325 | case SLASH: |
| 326 | return Div; |
| 327 | case DOUBLESLASH: |
| 328 | return FloorDiv; |
| 329 | case PERCENT: |
| 330 | return Mod; |
| 331 | default: |
| 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* Set the context ctx for expr_ty e returning 0 on success, -1 on error. |
| 337 | |
| 338 | Only sets context for expr kinds that "can appear in assignment context" |
| 339 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 340 | an appropriate syntax error and returns false. |
| 341 | |
| 342 | If e is a sequential type, items in sequence will also have their context |
| 343 | set. |
| 344 | |
| 345 | */ |
| 346 | |
| 347 | static int |
| 348 | set_context(expr_ty e, expr_context_ty ctx, const node *n) |
| 349 | { |
| 350 | asdl_seq *s = NULL; |
| 351 | |
| 352 | switch (e->kind) { |
| 353 | case Attribute_kind: |
| 354 | if (ctx == Store && |
| 355 | !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) { |
| 356 | return ast_error(n, "assignment to None"); |
| 357 | } |
| 358 | e->v.Attribute.ctx = ctx; |
| 359 | break; |
| 360 | case Subscript_kind: |
| 361 | e->v.Subscript.ctx = ctx; |
| 362 | break; |
| 363 | case Name_kind: |
| 364 | if (ctx == Store && |
| 365 | !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) { |
| 366 | return ast_error(n, "assignment to None"); |
| 367 | } |
| 368 | e->v.Name.ctx = ctx; |
| 369 | break; |
| 370 | case List_kind: |
| 371 | e->v.List.ctx = ctx; |
| 372 | s = e->v.List.elts; |
| 373 | break; |
| 374 | case Tuple_kind: |
| 375 | if (asdl_seq_LEN(e->v.Tuple.elts) == 0) |
| 376 | return ast_error(n, "can't assign to ()"); |
| 377 | e->v.Tuple.ctx = ctx; |
| 378 | s = e->v.Tuple.elts; |
| 379 | break; |
| 380 | case Call_kind: |
| 381 | if (ctx == Store) |
| 382 | return ast_error(n, "can't assign to function call"); |
| 383 | else if (ctx == Del) |
| 384 | return ast_error(n, "can't delete function call"); |
| 385 | else |
| 386 | return ast_error(n, "unexpected operation on function call"); |
| 387 | break; |
| 388 | case BinOp_kind: |
| 389 | return ast_error(n, "can't assign to operator"); |
| 390 | case GeneratorExp_kind: |
| 391 | return ast_error(n, "assignment to generator expression " |
| 392 | "not possible"); |
| 393 | case Num_kind: |
| 394 | case Str_kind: |
| 395 | return ast_error(n, "can't assign to literal"); |
| 396 | default: { |
| 397 | char buf[300]; |
| 398 | PyOS_snprintf(buf, sizeof(buf), |
| 399 | "unexpected expression in assignment %d (line %d)", |
| 400 | e->kind, e->lineno); |
| 401 | return ast_error(n, buf); |
| 402 | } |
| 403 | } |
| 404 | /* If the LHS is a list or tuple, we need to set the assignment |
| 405 | context for all the tuple elements. |
| 406 | */ |
| 407 | if (s) { |
| 408 | int i; |
| 409 | |
| 410 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
| 411 | if (!set_context(asdl_seq_GET(s, i), ctx, n)) |
| 412 | return 0; |
| 413 | } |
| 414 | } |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | static operator_ty |
| 419 | ast_for_augassign(const node *n) |
| 420 | { |
| 421 | REQ(n, augassign); |
| 422 | n = CHILD(n, 0); |
| 423 | switch (STR(n)[0]) { |
| 424 | case '+': |
| 425 | return Add; |
| 426 | case '-': |
| 427 | return Sub; |
| 428 | case '/': |
| 429 | if (STR(n)[1] == '/') |
| 430 | return FloorDiv; |
| 431 | else |
| 432 | return Div; |
| 433 | case '%': |
| 434 | return Mod; |
| 435 | case '<': |
| 436 | return LShift; |
| 437 | case '>': |
| 438 | return RShift; |
| 439 | case '&': |
| 440 | return BitAnd; |
| 441 | case '^': |
| 442 | return BitXor; |
| 443 | case '|': |
| 444 | return BitOr; |
| 445 | case '*': |
| 446 | if (STR(n)[1] == '*') |
| 447 | return Pow; |
| 448 | else |
| 449 | return Mult; |
| 450 | default: |
| 451 | PyErr_Format(PyExc_Exception, "invalid augassign: %s", STR(n)); |
| 452 | return 0; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static cmpop_ty |
| 457 | ast_for_comp_op(const node *n) |
| 458 | { |
| 459 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is' |
| 460 | |'is' 'not' |
| 461 | */ |
| 462 | REQ(n, comp_op); |
| 463 | if (NCH(n) == 1) { |
| 464 | n = CHILD(n, 0); |
| 465 | switch (TYPE(n)) { |
| 466 | case LESS: |
| 467 | return Lt; |
| 468 | case GREATER: |
| 469 | return Gt; |
| 470 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 471 | return Eq; |
| 472 | case LESSEQUAL: |
| 473 | return LtE; |
| 474 | case GREATEREQUAL: |
| 475 | return GtE; |
| 476 | case NOTEQUAL: |
| 477 | return NotEq; |
| 478 | case NAME: |
| 479 | if (strcmp(STR(n), "in") == 0) |
| 480 | return In; |
| 481 | if (strcmp(STR(n), "is") == 0) |
| 482 | return Is; |
| 483 | default: |
| 484 | PyErr_Format(PyExc_Exception, "invalid comp_op: %s", |
| 485 | STR(n)); |
| 486 | return 0; |
| 487 | } |
| 488 | } |
| 489 | else if (NCH(n) == 2) { |
| 490 | /* handle "not in" and "is not" */ |
| 491 | switch (TYPE(CHILD(n, 0))) { |
| 492 | case NAME: |
| 493 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 494 | return NotIn; |
| 495 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 496 | return IsNot; |
| 497 | default: |
| 498 | PyErr_Format(PyExc_Exception, "invalid comp_op: %s %s", |
| 499 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
| 500 | return 0; |
| 501 | } |
| 502 | } |
| 503 | PyErr_Format(PyExc_Exception, "invalid comp_op: has %d children", |
| 504 | NCH(n)); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static asdl_seq * |
| 509 | seq_for_testlist(struct compiling *c, const node *n) |
| 510 | { |
| 511 | /* testlist: test (',' test)* [','] */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 512 | asdl_seq *seq; |
| 513 | expr_ty expression; |
| 514 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 515 | assert(TYPE(n) == testlist |
| 516 | || TYPE(n) == listmaker |
| 517 | || TYPE(n) == testlist_gexp |
| 518 | || TYPE(n) == testlist_safe |
| 519 | ); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 520 | |
| 521 | seq = asdl_seq_new((NCH(n) + 1) / 2); |
| 522 | if (!seq) |
| 523 | return NULL; |
| 524 | |
| 525 | for (i = 0; i < NCH(n); i += 2) { |
| 526 | REQ(CHILD(n, i), test); |
| 527 | |
| 528 | expression = ast_for_expr(c, CHILD(n, i)); |
| 529 | if (!expression) { |
| 530 | asdl_seq_free(seq); |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | assert(i / 2 < seq->size); |
| 535 | asdl_seq_SET(seq, i / 2, expression); |
| 536 | } |
| 537 | return seq; |
| 538 | } |
| 539 | |
| 540 | static expr_ty |
| 541 | compiler_complex_args(const node *n) |
| 542 | { |
| 543 | int i, len = (NCH(n) + 1) / 2; |
| 544 | expr_ty result; |
| 545 | asdl_seq *args = asdl_seq_new(len); |
| 546 | if (!args) |
| 547 | return NULL; |
| 548 | |
| 549 | REQ(n, fplist); |
| 550 | |
| 551 | for (i = 0; i < len; i++) { |
| 552 | const node *child = CHILD(CHILD(n, 2*i), 0); |
| 553 | expr_ty arg; |
| 554 | if (TYPE(child) == NAME) { |
| 555 | if (!strcmp(STR(child), "None")) { |
| 556 | ast_error(child, "assignment to None"); |
| 557 | return NULL; |
| 558 | } |
| 559 | arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child)); |
| 560 | } |
| 561 | else |
| 562 | arg = compiler_complex_args(CHILD(CHILD(n, 2*i), 1)); |
| 563 | set_context(arg, Store, n); |
| 564 | asdl_seq_SET(args, i, arg); |
| 565 | } |
| 566 | |
| 567 | result = Tuple(args, Store, LINENO(n)); |
| 568 | set_context(result, Store, n); |
| 569 | return result; |
| 570 | } |
| 571 | |
| 572 | /* Create AST for argument list. |
| 573 | |
| 574 | XXX TO DO: |
| 575 | - check for invalid argument lists like normal after default |
| 576 | */ |
| 577 | |
| 578 | static arguments_ty |
| 579 | ast_for_arguments(struct compiling *c, const node *n) |
| 580 | { |
| 581 | /* parameters: '(' [varargslist] ')' |
| 582 | varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
| 583 | | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 584 | */ |
| 585 | int i, n_args = 0, n_defaults = 0, found_default = 0; |
| 586 | asdl_seq *args, *defaults; |
| 587 | identifier vararg = NULL, kwarg = NULL; |
| 588 | node *ch; |
| 589 | |
| 590 | if (TYPE(n) == parameters) { |
| 591 | if (NCH(n) == 2) /* () as argument list */ |
| 592 | return arguments(NULL, NULL, NULL, NULL); |
| 593 | n = CHILD(n, 1); |
| 594 | } |
| 595 | REQ(n, varargslist); |
| 596 | |
| 597 | /* first count the number of normal args & defaults */ |
| 598 | for (i = 0; i < NCH(n); i++) { |
| 599 | ch = CHILD(n, i); |
| 600 | if (TYPE(ch) == fpdef) { |
| 601 | n_args++; |
| 602 | } |
| 603 | if (TYPE(ch) == EQUAL) |
| 604 | n_defaults++; |
| 605 | } |
| 606 | args = (n_args ? asdl_seq_new(n_args) : NULL); |
| 607 | if (!args && n_args) |
| 608 | return NULL; /* Don't need to go to NULL; nothing allocated */ |
| 609 | defaults = (n_defaults ? asdl_seq_new(n_defaults) : NULL); |
| 610 | if (!defaults && n_defaults) |
| 611 | goto error; |
| 612 | |
| 613 | /* fpdef: NAME | '(' fplist ')' |
| 614 | fplist: fpdef (',' fpdef)* [','] |
| 615 | */ |
| 616 | i = 0; |
| 617 | while (i < NCH(n)) { |
| 618 | ch = CHILD(n, i); |
| 619 | switch (TYPE(ch)) { |
| 620 | case fpdef: |
| 621 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 622 | anything other than EQUAL or a comma? */ |
| 623 | /* XXX Should NCH(n) check be made a separate check? */ |
| 624 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
| 625 | asdl_seq_APPEND(defaults, |
| 626 | ast_for_expr(c, CHILD(n, i + 2))); |
| 627 | i += 2; |
| 628 | found_default = 1; |
| 629 | } |
| 630 | else if (found_default) { |
| 631 | ast_error(n, |
| 632 | "non-default argument follows default argument"); |
| 633 | goto error; |
| 634 | } |
| 635 | |
| 636 | if (NCH(ch) == 3) { |
| 637 | asdl_seq_APPEND(args, |
| 638 | compiler_complex_args(CHILD(ch, 1))); |
| 639 | } |
| 640 | else if (TYPE(CHILD(ch, 0)) == NAME) { |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 641 | expr_ty name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 642 | if (!strcmp(STR(CHILD(ch, 0)), "None")) { |
| 643 | ast_error(CHILD(ch, 0), "assignment to None"); |
| 644 | goto error; |
| 645 | } |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 646 | name = Name(NEW_IDENTIFIER(CHILD(ch, 0)), |
| 647 | Param, LINENO(ch)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 648 | if (!name) |
| 649 | goto error; |
| 650 | asdl_seq_APPEND(args, name); |
| 651 | |
| 652 | } |
| 653 | i += 2; /* the name and the comma */ |
| 654 | break; |
| 655 | case STAR: |
| 656 | if (!strcmp(STR(CHILD(n, i+1)), "None")) { |
| 657 | ast_error(CHILD(n, i+1), "assignment to None"); |
| 658 | goto error; |
| 659 | } |
| 660 | vararg = NEW_IDENTIFIER(CHILD(n, i+1)); |
| 661 | i += 3; |
| 662 | break; |
| 663 | case DOUBLESTAR: |
| 664 | if (!strcmp(STR(CHILD(n, i+1)), "None")) { |
| 665 | ast_error(CHILD(n, i+1), "assignment to None"); |
| 666 | goto error; |
| 667 | } |
| 668 | kwarg = NEW_IDENTIFIER(CHILD(n, i+1)); |
| 669 | i += 3; |
| 670 | break; |
| 671 | default: |
| 672 | PyErr_Format(PyExc_Exception, |
| 673 | "unexpected node in varargslist: %d @ %d", |
| 674 | TYPE(ch), i); |
| 675 | goto error; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return arguments(args, vararg, kwarg, defaults); |
| 680 | |
| 681 | error: |
| 682 | if (args) |
| 683 | asdl_seq_free(args); |
| 684 | if (defaults) |
| 685 | asdl_seq_free(defaults); |
| 686 | return NULL; |
| 687 | } |
| 688 | |
| 689 | static expr_ty |
| 690 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 691 | { |
| 692 | expr_ty e = NULL; |
| 693 | expr_ty attrib = NULL; |
| 694 | identifier id = NULL; |
| 695 | int i; |
| 696 | |
| 697 | REQ(n, dotted_name); |
| 698 | |
| 699 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 700 | if (!id) |
| 701 | goto error; |
| 702 | e = Name(id, Load, LINENO(n)); |
| 703 | if (!e) |
| 704 | goto error; |
| 705 | id = NULL; |
| 706 | |
| 707 | for (i = 2; i < NCH(n); i+=2) { |
| 708 | id = NEW_IDENTIFIER(CHILD(n, i)); |
| 709 | if (!id) |
| 710 | goto error; |
| 711 | attrib = Attribute(e, id, Load, LINENO(CHILD(n, i))); |
| 712 | if (!attrib) |
| 713 | goto error; |
| 714 | e = attrib; |
| 715 | attrib = NULL; |
| 716 | } |
| 717 | |
| 718 | return e; |
| 719 | |
| 720 | error: |
| 721 | Py_XDECREF(id); |
| 722 | free_expr(e); |
| 723 | return NULL; |
| 724 | } |
| 725 | |
| 726 | static expr_ty |
| 727 | ast_for_decorator(struct compiling *c, const node *n) |
| 728 | { |
| 729 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 730 | expr_ty d = NULL; |
| 731 | expr_ty name_expr = NULL; |
| 732 | |
| 733 | REQ(n, decorator); |
| 734 | |
| 735 | if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6) |
| 736 | || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) { |
| 737 | ast_error(n, "Invalid decorator node"); |
| 738 | goto error; |
| 739 | } |
| 740 | |
| 741 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 742 | if (!name_expr) |
| 743 | goto error; |
| 744 | |
| 745 | if (NCH(n) == 3) { /* No arguments */ |
| 746 | d = name_expr; |
| 747 | name_expr = NULL; |
| 748 | } |
| 749 | else if (NCH(n) == 5) { /* Call with no arguments */ |
| 750 | d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n)); |
| 751 | if (!d) |
| 752 | goto error; |
| 753 | name_expr = NULL; |
| 754 | } |
| 755 | else { |
| 756 | d = ast_for_call(c, CHILD(n, 3), name_expr); |
| 757 | if (!d) |
| 758 | goto error; |
| 759 | name_expr = NULL; |
| 760 | } |
| 761 | |
| 762 | return d; |
| 763 | |
| 764 | error: |
| 765 | free_expr(name_expr); |
| 766 | free_expr(d); |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | static asdl_seq* |
| 771 | ast_for_decorators(struct compiling *c, const node *n) |
| 772 | { |
| 773 | asdl_seq* decorator_seq = NULL; |
| 774 | expr_ty d = NULL; |
| 775 | int i; |
| 776 | |
| 777 | REQ(n, decorators); |
| 778 | |
| 779 | decorator_seq = asdl_seq_new(NCH(n)); |
| 780 | if (!decorator_seq) |
| 781 | return NULL; |
| 782 | |
| 783 | for (i = 0; i < NCH(n); i++) { |
| 784 | d = ast_for_decorator(c, CHILD(n, i)); |
| 785 | if (!d) |
| 786 | goto error; |
| 787 | asdl_seq_APPEND(decorator_seq, d); |
| 788 | d = NULL; |
| 789 | } |
| 790 | return decorator_seq; |
| 791 | error: |
| 792 | asdl_expr_seq_free(decorator_seq); |
| 793 | free_expr(d); |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| 797 | static stmt_ty |
| 798 | ast_for_funcdef(struct compiling *c, const node *n) |
| 799 | { |
| 800 | /* funcdef: 'def' [decorators] NAME parameters ':' suite */ |
| 801 | identifier name = NULL; |
| 802 | arguments_ty args = NULL; |
| 803 | asdl_seq *body = NULL; |
| 804 | asdl_seq *decorator_seq = NULL; |
| 805 | int name_i; |
| 806 | |
| 807 | REQ(n, funcdef); |
| 808 | |
| 809 | if (NCH(n) == 6) { /* decorators are present */ |
| 810 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 811 | if (!decorator_seq) |
| 812 | goto error; |
| 813 | name_i = 2; |
| 814 | } |
| 815 | else { |
| 816 | name_i = 1; |
| 817 | } |
| 818 | |
| 819 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 820 | if (!name) |
| 821 | goto error; |
| 822 | else if (!strcmp(STR(CHILD(n, name_i)), "None")) { |
| 823 | ast_error(CHILD(n, name_i), "assignment to None"); |
| 824 | goto error; |
| 825 | } |
| 826 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 827 | if (!args) |
| 828 | goto error; |
| 829 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
| 830 | if (!body) |
| 831 | goto error; |
| 832 | |
| 833 | return FunctionDef(name, args, body, decorator_seq, LINENO(n)); |
| 834 | |
| 835 | error: |
| 836 | asdl_stmt_seq_free(body); |
| 837 | asdl_expr_seq_free(decorator_seq); |
| 838 | free_arguments(args); |
| 839 | Py_XDECREF(name); |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | static expr_ty |
| 844 | ast_for_lambdef(struct compiling *c, const node *n) |
| 845 | { |
| 846 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 847 | arguments_ty args; |
| 848 | expr_ty expression; |
| 849 | |
| 850 | if (NCH(n) == 3) { |
| 851 | args = arguments(NULL, NULL, NULL, NULL); |
| 852 | if (!args) |
| 853 | return NULL; |
| 854 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 855 | if (!expression) { |
| 856 | free_arguments(args); |
| 857 | return NULL; |
| 858 | } |
| 859 | } |
| 860 | else { |
| 861 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 862 | if (!args) |
| 863 | return NULL; |
| 864 | expression = ast_for_expr(c, CHILD(n, 3)); |
| 865 | if (!expression) { |
| 866 | free_arguments(args); |
| 867 | return NULL; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return Lambda(args, expression, LINENO(n)); |
| 872 | } |
| 873 | |
| 874 | /* Count the number of 'for' loop in a list comprehension. |
| 875 | |
| 876 | Helper for ast_for_listcomp(). |
| 877 | */ |
| 878 | |
| 879 | static int |
| 880 | count_list_fors(const node *n) |
| 881 | { |
| 882 | int n_fors = 0; |
| 883 | node *ch = CHILD(n, 1); |
| 884 | |
| 885 | count_list_for: |
| 886 | n_fors++; |
| 887 | REQ(ch, list_for); |
| 888 | if (NCH(ch) == 5) |
| 889 | ch = CHILD(ch, 4); |
| 890 | else |
| 891 | return n_fors; |
| 892 | count_list_iter: |
| 893 | REQ(ch, list_iter); |
| 894 | ch = CHILD(ch, 0); |
| 895 | if (TYPE(ch) == list_for) |
| 896 | goto count_list_for; |
| 897 | else if (TYPE(ch) == list_if) { |
| 898 | if (NCH(ch) == 3) { |
| 899 | ch = CHILD(ch, 2); |
| 900 | goto count_list_iter; |
| 901 | } |
| 902 | else |
| 903 | return n_fors; |
| 904 | } |
| 905 | else { |
| 906 | /* Should never be reached */ |
Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 907 | PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 908 | return -1; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | /* Count the number of 'if' statements in a list comprehension. |
| 913 | |
| 914 | Helper for ast_for_listcomp(). |
| 915 | */ |
| 916 | |
| 917 | static int |
| 918 | count_list_ifs(const node *n) |
| 919 | { |
| 920 | int n_ifs = 0; |
| 921 | |
| 922 | count_list_iter: |
| 923 | REQ(n, list_iter); |
| 924 | if (TYPE(CHILD(n, 0)) == list_for) |
| 925 | return n_ifs; |
| 926 | n = CHILD(n, 0); |
| 927 | REQ(n, list_if); |
| 928 | n_ifs++; |
| 929 | if (NCH(n) == 2) |
| 930 | return n_ifs; |
| 931 | n = CHILD(n, 2); |
| 932 | goto count_list_iter; |
| 933 | } |
| 934 | |
| 935 | static expr_ty |
| 936 | ast_for_listcomp(struct compiling *c, const node *n) |
| 937 | { |
| 938 | /* listmaker: test ( list_for | (',' test)* [','] ) |
| 939 | list_for: 'for' exprlist 'in' testlist_safe [list_iter] |
| 940 | list_iter: list_for | list_if |
| 941 | list_if: 'if' test [list_iter] |
| 942 | testlist_safe: test [(',' test)+ [',']] |
| 943 | */ |
| 944 | expr_ty elt; |
| 945 | asdl_seq *listcomps; |
| 946 | int i, n_fors; |
| 947 | node *ch; |
| 948 | |
| 949 | REQ(n, listmaker); |
| 950 | assert(NCH(n) > 1); |
| 951 | |
| 952 | elt = ast_for_expr(c, CHILD(n, 0)); |
| 953 | if (!elt) |
| 954 | return NULL; |
| 955 | |
| 956 | n_fors = count_list_fors(n); |
| 957 | if (n_fors == -1) |
| 958 | return NULL; |
| 959 | |
| 960 | listcomps = asdl_seq_new(n_fors); |
| 961 | if (!listcomps) { |
| 962 | free_expr(elt); |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | ch = CHILD(n, 1); |
| 967 | for (i = 0; i < n_fors; i++) { |
| 968 | comprehension_ty lc; |
| 969 | asdl_seq *t; |
| 970 | expr_ty expression; |
| 971 | |
| 972 | REQ(ch, list_for); |
| 973 | |
| 974 | t = ast_for_exprlist(c, CHILD(ch, 1), Store); |
| 975 | if (!t) { |
| 976 | asdl_seq_free(listcomps); |
| 977 | free_expr(elt); |
| 978 | return NULL; |
| 979 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 980 | expression = ast_for_testlist(c, CHILD(ch, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 981 | if (!expression) { |
| 982 | asdl_seq_free(t); |
| 983 | asdl_seq_free(listcomps); |
| 984 | free_expr(elt); |
| 985 | return NULL; |
| 986 | } |
| 987 | |
| 988 | if (asdl_seq_LEN(t) == 1) |
| 989 | lc = comprehension(asdl_seq_GET(t, 0), expression, NULL); |
| 990 | else |
| 991 | lc = comprehension(Tuple(t, Store, LINENO(ch)), expression, NULL); |
| 992 | |
| 993 | if (!lc) { |
| 994 | asdl_seq_free(t); |
| 995 | asdl_seq_free(listcomps); |
| 996 | free_expr(expression); |
| 997 | free_expr(elt); |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
| 1001 | if (NCH(ch) == 5) { |
| 1002 | int j, n_ifs; |
| 1003 | asdl_seq *ifs; |
| 1004 | |
| 1005 | ch = CHILD(ch, 4); |
| 1006 | n_ifs = count_list_ifs(ch); |
| 1007 | if (n_ifs == -1) { |
| 1008 | asdl_seq_free(listcomps); |
| 1009 | free_expr(elt); |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
| 1013 | ifs = asdl_seq_new(n_ifs); |
| 1014 | if (!ifs) { |
| 1015 | asdl_seq_free(listcomps); |
| 1016 | free_expr(elt); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | |
| 1020 | for (j = 0; j < n_ifs; j++) { |
| 1021 | REQ(ch, list_iter); |
| 1022 | |
| 1023 | ch = CHILD(ch, 0); |
| 1024 | REQ(ch, list_if); |
| 1025 | |
| 1026 | asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1))); |
| 1027 | if (NCH(ch) == 3) |
| 1028 | ch = CHILD(ch, 2); |
| 1029 | } |
| 1030 | /* on exit, must guarantee that ch is a list_for */ |
| 1031 | if (TYPE(ch) == list_iter) |
| 1032 | ch = CHILD(ch, 0); |
| 1033 | lc->ifs = ifs; |
| 1034 | } |
| 1035 | asdl_seq_APPEND(listcomps, lc); |
| 1036 | } |
| 1037 | |
| 1038 | return ListComp(elt, listcomps, LINENO(n)); |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | Count the number of 'for' loops in a generator expression. |
| 1043 | |
| 1044 | Helper for ast_for_genexp(). |
| 1045 | */ |
| 1046 | |
| 1047 | static int |
| 1048 | count_gen_fors(const node *n) |
| 1049 | { |
| 1050 | int n_fors = 0; |
| 1051 | node *ch = CHILD(n, 1); |
| 1052 | |
| 1053 | count_gen_for: |
| 1054 | n_fors++; |
| 1055 | REQ(ch, gen_for); |
| 1056 | if (NCH(ch) == 5) |
| 1057 | ch = CHILD(ch, 4); |
| 1058 | else |
| 1059 | return n_fors; |
| 1060 | count_gen_iter: |
| 1061 | REQ(ch, gen_iter); |
| 1062 | ch = CHILD(ch, 0); |
| 1063 | if (TYPE(ch) == gen_for) |
| 1064 | goto count_gen_for; |
| 1065 | else if (TYPE(ch) == gen_if) { |
| 1066 | if (NCH(ch) == 3) { |
| 1067 | ch = CHILD(ch, 2); |
| 1068 | goto count_gen_iter; |
| 1069 | } |
| 1070 | else |
| 1071 | return n_fors; |
| 1072 | } |
| 1073 | else { |
| 1074 | /* Should never be reached */ |
Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 1075 | PyErr_SetString(PyExc_SystemError, |
| 1076 | "logic error in count_gen_fors"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1077 | return -1; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* Count the number of 'if' statements in a generator expression. |
| 1082 | |
| 1083 | Helper for ast_for_genexp(). |
| 1084 | */ |
| 1085 | |
| 1086 | static int |
| 1087 | count_gen_ifs(const node *n) |
| 1088 | { |
| 1089 | int n_ifs = 0; |
| 1090 | |
| 1091 | while (1) { |
| 1092 | REQ(n, gen_iter); |
| 1093 | if (TYPE(CHILD(n, 0)) == gen_for) |
| 1094 | return n_ifs; |
| 1095 | n = CHILD(n, 0); |
| 1096 | REQ(n, gen_if); |
| 1097 | n_ifs++; |
| 1098 | if (NCH(n) == 2) |
| 1099 | return n_ifs; |
| 1100 | n = CHILD(n, 2); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | static expr_ty |
| 1105 | ast_for_genexp(struct compiling *c, const node *n) |
| 1106 | { |
| 1107 | /* testlist_gexp: test ( gen_for | (',' test)* [','] ) |
| 1108 | argument: [test '='] test [gen_for] # Really [keyword '='] test */ |
| 1109 | expr_ty elt; |
| 1110 | asdl_seq *genexps; |
| 1111 | int i, n_fors; |
| 1112 | node *ch; |
| 1113 | |
| 1114 | assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument)); |
| 1115 | assert(NCH(n) > 1); |
| 1116 | |
| 1117 | elt = ast_for_expr(c, CHILD(n, 0)); |
| 1118 | if (!elt) |
| 1119 | return NULL; |
| 1120 | |
| 1121 | n_fors = count_gen_fors(n); |
| 1122 | if (n_fors == -1) |
| 1123 | return NULL; |
| 1124 | |
| 1125 | genexps = asdl_seq_new(n_fors); |
| 1126 | if (!genexps) { |
| 1127 | free_expr(elt); |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
| 1131 | ch = CHILD(n, 1); |
| 1132 | for (i = 0; i < n_fors; i++) { |
| 1133 | comprehension_ty ge; |
| 1134 | asdl_seq *t; |
| 1135 | expr_ty expression; |
| 1136 | |
| 1137 | REQ(ch, gen_for); |
| 1138 | |
| 1139 | t = ast_for_exprlist(c, CHILD(ch, 1), Store); |
| 1140 | if (!t) { |
| 1141 | asdl_seq_free(genexps); |
| 1142 | free_expr(elt); |
| 1143 | return NULL; |
| 1144 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1145 | expression = ast_for_expr(c, CHILD(ch, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | if (!expression) { |
| 1147 | asdl_seq_free(genexps); |
| 1148 | free_expr(elt); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
| 1152 | if (asdl_seq_LEN(t) == 1) |
| 1153 | ge = comprehension(asdl_seq_GET(t, 0), expression, |
| 1154 | NULL); |
| 1155 | else |
| 1156 | ge = comprehension(Tuple(t, Store, LINENO(ch)), |
| 1157 | expression, NULL); |
| 1158 | |
| 1159 | if (!ge) { |
| 1160 | asdl_seq_free(genexps); |
| 1161 | free_expr(elt); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | |
| 1165 | if (NCH(ch) == 5) { |
| 1166 | int j, n_ifs; |
| 1167 | asdl_seq *ifs; |
| 1168 | |
| 1169 | ch = CHILD(ch, 4); |
| 1170 | n_ifs = count_gen_ifs(ch); |
| 1171 | if (n_ifs == -1) { |
| 1172 | asdl_seq_free(genexps); |
| 1173 | free_expr(elt); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | ifs = asdl_seq_new(n_ifs); |
| 1178 | if (!ifs) { |
| 1179 | asdl_seq_free(genexps); |
| 1180 | free_expr(elt); |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | |
| 1184 | for (j = 0; j < n_ifs; j++) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1185 | expr_ty expression; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1186 | REQ(ch, gen_iter); |
| 1187 | ch = CHILD(ch, 0); |
| 1188 | REQ(ch, gen_if); |
| 1189 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1190 | expression = ast_for_expr(c, CHILD(ch, 1)); |
| 1191 | if (!expression) { |
| 1192 | asdl_seq_free(genexps); |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | asdl_seq_APPEND(ifs, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1196 | if (NCH(ch) == 3) |
| 1197 | ch = CHILD(ch, 2); |
| 1198 | } |
| 1199 | /* on exit, must guarantee that ch is a gen_for */ |
| 1200 | if (TYPE(ch) == gen_iter) |
| 1201 | ch = CHILD(ch, 0); |
| 1202 | ge->ifs = ifs; |
| 1203 | } |
| 1204 | asdl_seq_APPEND(genexps, ge); |
| 1205 | } |
| 1206 | |
| 1207 | return GeneratorExp(elt, genexps, LINENO(n)); |
| 1208 | } |
| 1209 | |
| 1210 | static expr_ty |
| 1211 | ast_for_atom(struct compiling *c, const node *n) |
| 1212 | { |
| 1213 | /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']' |
| 1214 | | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+ |
| 1215 | */ |
| 1216 | node *ch = CHILD(n, 0); |
| 1217 | |
| 1218 | switch (TYPE(ch)) { |
| 1219 | case NAME: |
| 1220 | /* All names start in Load context, but may later be |
| 1221 | changed. */ |
| 1222 | return Name(NEW_IDENTIFIER(ch), Load, LINENO(n)); |
| 1223 | case STRING: { |
| 1224 | PyObject *str = parsestrplus(c, n); |
| 1225 | |
| 1226 | if (!str) |
| 1227 | return NULL; |
| 1228 | |
| 1229 | return Str(str, LINENO(n)); |
| 1230 | } |
| 1231 | case NUMBER: { |
| 1232 | PyObject *pynum = parsenumber(STR(ch)); |
| 1233 | |
| 1234 | if (!pynum) |
| 1235 | return NULL; |
| 1236 | |
| 1237 | return Num(pynum, LINENO(n)); |
| 1238 | } |
| 1239 | case LPAR: /* some parenthesized expressions */ |
| 1240 | ch = CHILD(n, 1); |
| 1241 | |
| 1242 | if (TYPE(ch) == RPAR) |
| 1243 | return Tuple(NULL, Load, LINENO(n)); |
| 1244 | |
| 1245 | if (TYPE(ch) == yield_expr) |
| 1246 | return ast_for_expr(c, ch); |
| 1247 | |
| 1248 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for)) |
| 1249 | return ast_for_genexp(c, ch); |
| 1250 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1251 | return ast_for_testlist_gexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1252 | case LSQB: /* list (or list comprehension) */ |
| 1253 | ch = CHILD(n, 1); |
| 1254 | |
| 1255 | if (TYPE(ch) == RSQB) |
| 1256 | return List(NULL, Load, LINENO(n)); |
| 1257 | |
| 1258 | REQ(ch, listmaker); |
| 1259 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 1260 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 1261 | |
| 1262 | if (!elts) |
| 1263 | return NULL; |
| 1264 | |
| 1265 | return List(elts, Load, LINENO(n)); |
| 1266 | } |
| 1267 | else |
| 1268 | return ast_for_listcomp(c, ch); |
| 1269 | case LBRACE: { |
| 1270 | /* dictmaker: test ':' test (',' test ':' test)* [','] */ |
| 1271 | int i, size; |
| 1272 | asdl_seq *keys, *values; |
| 1273 | |
| 1274 | ch = CHILD(n, 1); |
| 1275 | size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ |
| 1276 | keys = asdl_seq_new(size); |
| 1277 | if (!keys) |
| 1278 | return NULL; |
| 1279 | |
| 1280 | values = asdl_seq_new(size); |
| 1281 | if (!values) { |
| 1282 | asdl_seq_free(keys); |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | |
| 1286 | for (i = 0; i < NCH(ch); i += 4) { |
| 1287 | expr_ty expression; |
| 1288 | |
| 1289 | expression = ast_for_expr(c, CHILD(ch, i)); |
| 1290 | if (!expression) |
| 1291 | return NULL; |
| 1292 | |
| 1293 | asdl_seq_SET(keys, i / 4, expression); |
| 1294 | |
| 1295 | expression = ast_for_expr(c, CHILD(ch, i + 2)); |
| 1296 | if (!expression) |
| 1297 | return NULL; |
| 1298 | |
| 1299 | asdl_seq_SET(values, i / 4, expression); |
| 1300 | } |
| 1301 | return Dict(keys, values, LINENO(n)); |
| 1302 | } |
| 1303 | case BACKQUOTE: { /* repr */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1304 | expr_ty expression = ast_for_testlist(c, CHILD(n, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (!expression) |
| 1307 | return NULL; |
| 1308 | |
| 1309 | return Repr(expression, LINENO(n)); |
| 1310 | } |
| 1311 | default: |
| 1312 | PyErr_Format(PyExc_Exception, "unhandled atom %d", |
| 1313 | TYPE(ch)); |
| 1314 | return NULL; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | static slice_ty |
| 1319 | ast_for_slice(struct compiling *c, const node *n) |
| 1320 | { |
| 1321 | node *ch; |
| 1322 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 1323 | |
| 1324 | REQ(n, subscript); |
| 1325 | |
| 1326 | /* |
| 1327 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 1328 | sliceop: ':' [test] |
| 1329 | */ |
| 1330 | ch = CHILD(n, 0); |
| 1331 | if (TYPE(ch) == DOT) |
| 1332 | return Ellipsis(); |
| 1333 | |
| 1334 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 1335 | /* 'step' variable hold no significance in terms of being used over |
| 1336 | other vars */ |
| 1337 | step = ast_for_expr(c, ch); |
| 1338 | if (!step) |
| 1339 | return NULL; |
| 1340 | |
| 1341 | return Index(step); |
| 1342 | } |
| 1343 | |
| 1344 | if (TYPE(ch) == test) { |
| 1345 | lower = ast_for_expr(c, ch); |
| 1346 | if (!lower) |
| 1347 | return NULL; |
| 1348 | } |
| 1349 | |
| 1350 | /* If there's an upper bound it's in the second or third position. */ |
| 1351 | if (TYPE(ch) == COLON) { |
| 1352 | if (NCH(n) > 1) { |
| 1353 | node *n2 = CHILD(n, 1); |
| 1354 | |
| 1355 | if (TYPE(n2) == test) { |
| 1356 | upper = ast_for_expr(c, n2); |
| 1357 | if (!upper) |
| 1358 | return NULL; |
| 1359 | } |
| 1360 | } |
| 1361 | } else if (NCH(n) > 2) { |
| 1362 | node *n2 = CHILD(n, 2); |
| 1363 | |
| 1364 | if (TYPE(n2) == test) { |
| 1365 | upper = ast_for_expr(c, n2); |
| 1366 | if (!upper) |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | ch = CHILD(n, NCH(n) - 1); |
| 1372 | if (TYPE(ch) == sliceop) { |
| 1373 | if (NCH(ch) == 1) |
| 1374 | /* XXX: If only 1 child, then should just be a colon. Should we |
| 1375 | just skip assigning and just get to the return? */ |
| 1376 | ch = CHILD(ch, 0); |
| 1377 | else |
| 1378 | ch = CHILD(ch, 1); |
| 1379 | if (TYPE(ch) == test) { |
| 1380 | step = ast_for_expr(c, ch); |
| 1381 | if (!step) |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | return Slice(lower, upper, step); |
| 1387 | } |
| 1388 | |
| 1389 | static expr_ty |
| 1390 | ast_for_binop(struct compiling *c, const node *n) |
| 1391 | { |
| 1392 | /* Must account for a sequence of expressions. |
| 1393 | How should A op B op C by represented? |
| 1394 | BinOp(BinOp(A, op, B), op, C). |
| 1395 | */ |
| 1396 | |
| 1397 | int i, nops; |
| 1398 | expr_ty expr1, expr2, result; |
| 1399 | operator_ty operator; |
| 1400 | |
| 1401 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 1402 | if (!expr1) |
| 1403 | return NULL; |
| 1404 | |
| 1405 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 1406 | if (!expr2) |
| 1407 | return NULL; |
| 1408 | |
| 1409 | operator = get_operator(CHILD(n, 1)); |
| 1410 | if (!operator) |
| 1411 | return NULL; |
| 1412 | |
| 1413 | result = BinOp(expr1, operator, expr2, LINENO(n)); |
| 1414 | if (!result) |
| 1415 | return NULL; |
| 1416 | |
| 1417 | nops = (NCH(n) - 1) / 2; |
| 1418 | for (i = 1; i < nops; i++) { |
| 1419 | expr_ty tmp_result, tmp; |
| 1420 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 1421 | |
| 1422 | operator = get_operator(next_oper); |
| 1423 | if (!operator) |
| 1424 | return NULL; |
| 1425 | |
| 1426 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 1427 | if (!tmp) |
| 1428 | return NULL; |
| 1429 | |
| 1430 | tmp_result = BinOp(result, operator, tmp, |
| 1431 | LINENO(next_oper)); |
| 1432 | if (!tmp) |
| 1433 | return NULL; |
| 1434 | result = tmp_result; |
| 1435 | } |
| 1436 | return result; |
| 1437 | } |
| 1438 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1439 | static expr_ty |
| 1440 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 1441 | { |
| 1442 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */ |
| 1443 | expr_ty e; |
| 1444 | REQ(n, trailer); |
| 1445 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 1446 | if (NCH(n) == 2) |
| 1447 | e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n)); |
| 1448 | else |
| 1449 | e = ast_for_call(c, CHILD(n, 1), left_expr); |
| 1450 | } |
| 1451 | else if (TYPE(CHILD(n, 0)) == LSQB) { |
| 1452 | REQ(CHILD(n, 2), RSQB); |
| 1453 | n = CHILD(n, 1); |
| 1454 | if (NCH(n) <= 2) { |
| 1455 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 1456 | if (!slc) |
| 1457 | return NULL; |
| 1458 | e = Subscript(left_expr, slc, Load, LINENO(n)); |
| 1459 | if (!e) { |
| 1460 | free_slice(slc); |
| 1461 | return NULL; |
| 1462 | } |
| 1463 | } |
| 1464 | else { |
| 1465 | int j; |
| 1466 | slice_ty slc; |
| 1467 | asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2); |
| 1468 | if (!slices) |
| 1469 | return NULL; |
| 1470 | for (j = 0; j < NCH(n); j += 2) { |
| 1471 | slc = ast_for_slice(c, CHILD(n, j)); |
| 1472 | if (!slc) { |
| 1473 | asdl_seq_free(slices); |
| 1474 | return NULL; |
| 1475 | } |
| 1476 | asdl_seq_SET(slices, j / 2, slc); |
| 1477 | } |
| 1478 | e = Subscript(left_expr, ExtSlice(slices), Load, LINENO(n)); |
| 1479 | if (!e) { |
| 1480 | asdl_seq_free(slices); |
| 1481 | return NULL; |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | else { |
| 1486 | assert(TYPE(CHILD(n, 0)) == DOT); |
| 1487 | e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n)); |
| 1488 | } |
| 1489 | return e; |
| 1490 | } |
| 1491 | |
| 1492 | static expr_ty |
| 1493 | ast_for_power(struct compiling *c, const node *n) |
| 1494 | { |
| 1495 | /* power: atom trailer* ('**' factor)* |
| 1496 | */ |
| 1497 | int i; |
| 1498 | expr_ty e, tmp; |
| 1499 | REQ(n, power); |
| 1500 | e = ast_for_atom(c, CHILD(n, 0)); |
| 1501 | if (!e) |
| 1502 | return NULL; |
| 1503 | if (NCH(n) == 1) |
| 1504 | return e; |
| 1505 | for (i = 1; i < NCH(n); i++) { |
| 1506 | node *ch = CHILD(n, i); |
| 1507 | if (TYPE(ch) != trailer) |
| 1508 | break; |
| 1509 | tmp = ast_for_trailer(c, ch, e); |
| 1510 | if (!tmp) { |
| 1511 | free_expr(e); |
| 1512 | return NULL; |
| 1513 | } |
| 1514 | e = tmp; |
| 1515 | } |
| 1516 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 1517 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
| 1518 | if (!f) { |
| 1519 | free_expr(e); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | tmp = BinOp(e, Pow, f, LINENO(n)); |
| 1523 | if (!tmp) { |
| 1524 | free_expr(e); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | e = tmp; |
| 1528 | } |
| 1529 | return e; |
| 1530 | } |
| 1531 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 1533 | */ |
| 1534 | |
| 1535 | static expr_ty |
| 1536 | ast_for_expr(struct compiling *c, const node *n) |
| 1537 | { |
| 1538 | /* handle the full range of simple expressions |
| 1539 | test: and_test ('or' and_test)* | lambdef |
| 1540 | and_test: not_test ('and' not_test)* |
| 1541 | not_test: 'not' not_test | comparison |
| 1542 | comparison: expr (comp_op expr)* |
| 1543 | expr: xor_expr ('|' xor_expr)* |
| 1544 | xor_expr: and_expr ('^' and_expr)* |
| 1545 | and_expr: shift_expr ('&' shift_expr)* |
| 1546 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 1547 | arith_expr: term (('+'|'-') term)* |
| 1548 | term: factor (('*'|'/'|'%'|'//') factor)* |
| 1549 | factor: ('+'|'-'|'~') factor | power |
| 1550 | power: atom trailer* ('**' factor)* |
| 1551 | */ |
| 1552 | |
| 1553 | asdl_seq *seq; |
| 1554 | int i; |
| 1555 | |
| 1556 | loop: |
| 1557 | switch (TYPE(n)) { |
| 1558 | case test: |
| 1559 | if (TYPE(CHILD(n, 0)) == lambdef) |
| 1560 | return ast_for_lambdef(c, CHILD(n, 0)); |
| 1561 | /* Fall through to and_test */ |
| 1562 | case and_test: |
| 1563 | if (NCH(n) == 1) { |
| 1564 | n = CHILD(n, 0); |
| 1565 | goto loop; |
| 1566 | } |
| 1567 | seq = asdl_seq_new((NCH(n) + 1) / 2); |
| 1568 | if (!seq) |
| 1569 | return NULL; |
| 1570 | for (i = 0; i < NCH(n); i += 2) { |
| 1571 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 1572 | if (!e) |
| 1573 | return NULL; |
| 1574 | asdl_seq_SET(seq, i / 2, e); |
| 1575 | } |
| 1576 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
| 1577 | return BoolOp(And, seq, LINENO(n)); |
| 1578 | else { |
| 1579 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
| 1580 | return BoolOp(Or, seq, LINENO(n)); |
| 1581 | } |
| 1582 | break; |
| 1583 | case not_test: |
| 1584 | if (NCH(n) == 1) { |
| 1585 | n = CHILD(n, 0); |
| 1586 | goto loop; |
| 1587 | } |
| 1588 | else { |
| 1589 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 1590 | if (!expression) |
| 1591 | return NULL; |
| 1592 | |
| 1593 | return UnaryOp(Not, expression, LINENO(n)); |
| 1594 | } |
| 1595 | case comparison: |
| 1596 | if (NCH(n) == 1) { |
| 1597 | n = CHILD(n, 0); |
| 1598 | goto loop; |
| 1599 | } |
| 1600 | else { |
| 1601 | expr_ty expression; |
| 1602 | asdl_seq *ops, *cmps; |
| 1603 | ops = asdl_seq_new(NCH(n) / 2); |
| 1604 | if (!ops) |
| 1605 | return NULL; |
| 1606 | cmps = asdl_seq_new(NCH(n) / 2); |
| 1607 | if (!cmps) { |
| 1608 | asdl_seq_free(ops); |
| 1609 | return NULL; |
| 1610 | } |
| 1611 | for (i = 1; i < NCH(n); i += 2) { |
| 1612 | /* XXX cmpop_ty is just an enum */ |
| 1613 | cmpop_ty operator; |
| 1614 | |
| 1615 | operator = ast_for_comp_op(CHILD(n, i)); |
| 1616 | if (!operator) |
| 1617 | return NULL; |
| 1618 | |
| 1619 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
| 1620 | if (!expression) |
| 1621 | return NULL; |
| 1622 | |
| 1623 | asdl_seq_SET(ops, i / 2, (void *)operator); |
| 1624 | asdl_seq_SET(cmps, i / 2, expression); |
| 1625 | } |
| 1626 | expression = ast_for_expr(c, CHILD(n, 0)); |
| 1627 | if (!expression) |
| 1628 | return NULL; |
| 1629 | |
| 1630 | return Compare(expression, ops, cmps, LINENO(n)); |
| 1631 | } |
| 1632 | break; |
| 1633 | |
| 1634 | /* The next five cases all handle BinOps. The main body of code |
| 1635 | is the same in each case, but the switch turned inside out to |
| 1636 | reuse the code for each type of operator. |
| 1637 | */ |
| 1638 | case expr: |
| 1639 | case xor_expr: |
| 1640 | case and_expr: |
| 1641 | case shift_expr: |
| 1642 | case arith_expr: |
| 1643 | case term: |
| 1644 | if (NCH(n) == 1) { |
| 1645 | n = CHILD(n, 0); |
| 1646 | goto loop; |
| 1647 | } |
| 1648 | return ast_for_binop(c, n); |
| 1649 | case yield_expr: { |
| 1650 | expr_ty exp = NULL; |
| 1651 | if (NCH(n) == 2) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1652 | exp = ast_for_testlist(c, CHILD(n, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1653 | if (!exp) |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | return Yield(exp, LINENO(n)); |
| 1657 | } |
| 1658 | case factor: { |
| 1659 | expr_ty expression; |
| 1660 | |
| 1661 | if (NCH(n) == 1) { |
| 1662 | n = CHILD(n, 0); |
| 1663 | goto loop; |
| 1664 | } |
| 1665 | |
| 1666 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 1667 | if (!expression) |
| 1668 | return NULL; |
| 1669 | |
| 1670 | switch (TYPE(CHILD(n, 0))) { |
| 1671 | case PLUS: |
| 1672 | return UnaryOp(UAdd, expression, LINENO(n)); |
| 1673 | case MINUS: |
| 1674 | return UnaryOp(USub, expression, LINENO(n)); |
| 1675 | case TILDE: |
| 1676 | return UnaryOp(Invert, expression, LINENO(n)); |
| 1677 | } |
| 1678 | break; |
| 1679 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1680 | case power: |
| 1681 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1682 | default: |
| 1683 | abort(); |
| 1684 | PyErr_Format(PyExc_Exception, "unhandled expr: %d", TYPE(n)); |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | /* should never get here */ |
| 1688 | return NULL; |
| 1689 | } |
| 1690 | |
| 1691 | static expr_ty |
| 1692 | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
| 1693 | { |
| 1694 | /* |
| 1695 | arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] |
| 1696 | | '**' test) |
| 1697 | argument: [test '='] test [gen_for] # Really [keyword '='] test |
| 1698 | */ |
| 1699 | |
| 1700 | int i, nargs, nkeywords, ngens; |
| 1701 | asdl_seq *args = NULL; |
| 1702 | asdl_seq *keywords = NULL; |
| 1703 | expr_ty vararg = NULL, kwarg = NULL; |
| 1704 | |
| 1705 | REQ(n, arglist); |
| 1706 | |
| 1707 | nargs = 0; |
| 1708 | nkeywords = 0; |
| 1709 | ngens = 0; |
| 1710 | for (i = 0; i < NCH(n); i++) { |
| 1711 | node *ch = CHILD(n, i); |
| 1712 | if (TYPE(ch) == argument) { |
| 1713 | if (NCH(ch) == 1) |
| 1714 | nargs++; |
| 1715 | else if (TYPE(CHILD(ch, 1)) == gen_for) |
| 1716 | ngens++; |
| 1717 | else |
| 1718 | nkeywords++; |
| 1719 | } |
| 1720 | } |
| 1721 | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
| 1722 | ast_error(n, "Generator expression must be parenthesised " |
| 1723 | "if not sole argument"); |
| 1724 | return NULL; |
| 1725 | } |
| 1726 | |
| 1727 | if (nargs + nkeywords + ngens > 255) { |
| 1728 | ast_error(n, "more than 255 arguments"); |
| 1729 | return NULL; |
| 1730 | } |
| 1731 | |
| 1732 | args = asdl_seq_new(nargs + ngens); |
| 1733 | if (!args) |
| 1734 | goto error; |
| 1735 | keywords = asdl_seq_new(nkeywords); |
| 1736 | if (!keywords) |
| 1737 | goto error; |
| 1738 | nargs = 0; |
| 1739 | nkeywords = 0; |
| 1740 | for (i = 0; i < NCH(n); i++) { |
| 1741 | node *ch = CHILD(n, i); |
| 1742 | if (TYPE(ch) == argument) { |
| 1743 | expr_ty e; |
| 1744 | if (NCH(ch) == 1) { |
| 1745 | e = ast_for_expr(c, CHILD(ch, 0)); |
| 1746 | if (!e) |
| 1747 | goto error; |
| 1748 | asdl_seq_SET(args, nargs++, e); |
| 1749 | } |
| 1750 | else if (TYPE(CHILD(ch, 1)) == gen_for) { |
| 1751 | e = ast_for_genexp(c, ch); |
| 1752 | if (!e) |
| 1753 | goto error; |
| 1754 | asdl_seq_SET(args, nargs++, e); |
| 1755 | } |
| 1756 | else { |
| 1757 | keyword_ty kw; |
| 1758 | identifier key; |
| 1759 | |
| 1760 | /* CHILD(ch, 0) is test, but must be an identifier? */ |
| 1761 | e = ast_for_expr(c, CHILD(ch, 0)); |
| 1762 | if (!e) |
| 1763 | goto error; |
| 1764 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1765 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1766 | * SF bug 132313 points out that complaining about a keyword |
| 1767 | * then is very confusing. |
| 1768 | */ |
| 1769 | if (e->kind == Lambda_kind) { |
| 1770 | ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); |
| 1771 | goto error; |
| 1772 | } else if (e->kind != Name_kind) { |
| 1773 | ast_error(CHILD(ch, 0), "keyword can't be an expression"); |
| 1774 | goto error; |
| 1775 | } |
| 1776 | key = e->v.Name.id; |
| 1777 | free(e); |
| 1778 | e = ast_for_expr(c, CHILD(ch, 2)); |
| 1779 | if (!e) |
| 1780 | goto error; |
| 1781 | kw = keyword(key, e); |
| 1782 | if (!kw) |
| 1783 | goto error; |
| 1784 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 1785 | } |
| 1786 | } |
| 1787 | else if (TYPE(ch) == STAR) { |
| 1788 | vararg = ast_for_expr(c, CHILD(n, i+1)); |
| 1789 | i++; |
| 1790 | } |
| 1791 | else if (TYPE(ch) == DOUBLESTAR) { |
| 1792 | kwarg = ast_for_expr(c, CHILD(n, i+1)); |
| 1793 | i++; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | return Call(func, args, keywords, vararg, kwarg, LINENO(n)); |
| 1798 | |
| 1799 | error: |
| 1800 | if (args) |
| 1801 | asdl_seq_free(args); |
| 1802 | if (keywords) |
| 1803 | asdl_seq_free(keywords); |
| 1804 | return NULL; |
| 1805 | } |
| 1806 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1807 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1808 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1809 | { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1810 | /* testlist_gexp: test (',' test)* [','] */ |
| 1811 | /* testlist: test (',' test)* [','] */ |
| 1812 | /* testlist_safe: test (',' test)+ [','] */ |
| 1813 | /* testlist1: test (',' test)* */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1814 | assert(NCH(n) > 0); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1815 | if (TYPE(n) == testlist_gexp) { |
| 1816 | if (NCH(n) > 1) |
| 1817 | assert(TYPE(CHILD(n, 1)) != gen_for); |
| 1818 | } |
| 1819 | else { |
| 1820 | assert(TYPE(n) == testlist || |
| 1821 | TYPE(n) == testlist_safe || |
| 1822 | TYPE(n) == testlist1); |
| 1823 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1824 | if (NCH(n) == 1) |
| 1825 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1826 | else { |
| 1827 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 1828 | if (!tmp) |
| 1829 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1830 | return Tuple(tmp, Load, LINENO(n)); |
| 1831 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | static expr_ty |
| 1835 | ast_for_testlist_gexp(struct compiling *c, const node* n) |
| 1836 | { |
| 1837 | /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */ |
| 1838 | /* argument: test [ gen_for ] */ |
| 1839 | assert(TYPE(n) == testlist_gexp || TYPE(n) == argument); |
| 1840 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) { |
| 1841 | return ast_for_genexp(c, n); |
| 1842 | } |
| 1843 | else |
| 1844 | return ast_for_testlist(c, n); |
| 1845 | } |
| 1846 | |
| 1847 | /* like ast_for_testlist() but returns a sequence */ |
| 1848 | static asdl_seq* |
| 1849 | ast_for_class_bases(struct compiling *c, const node* n) |
| 1850 | { |
| 1851 | /* testlist: test (',' test)* [','] */ |
| 1852 | assert(NCH(n) > 0); |
| 1853 | REQ(n, testlist); |
| 1854 | if (NCH(n) == 1) { |
| 1855 | expr_ty base; |
| 1856 | asdl_seq *bases = asdl_seq_new(1); |
| 1857 | if (!bases) |
| 1858 | return NULL; |
| 1859 | base = ast_for_expr(c, CHILD(n, 0)); |
| 1860 | if (!base) { |
| 1861 | asdl_seq_free(bases); |
| 1862 | return NULL; |
| 1863 | } |
| 1864 | asdl_seq_SET(bases, 0, base); |
| 1865 | return bases; |
| 1866 | } |
| 1867 | else { |
| 1868 | return seq_for_testlist(c, n); |
| 1869 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static stmt_ty |
| 1873 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 1874 | { |
| 1875 | REQ(n, expr_stmt); |
| 1876 | /* expr_stmt: testlist (augassign (yield_expr|testlist) |
| 1877 | | ('=' (yield_expr|testlist))*) |
| 1878 | testlist: test (',' test)* [','] |
| 1879 | augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
| 1880 | | '<<=' | '>>=' | '**=' | '//=' |
| 1881 | test: ... here starts the operator precendence dance |
| 1882 | */ |
| 1883 | |
| 1884 | if (NCH(n) == 1) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1885 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1886 | if (!e) |
| 1887 | return NULL; |
| 1888 | |
| 1889 | return Expr(e, LINENO(n)); |
| 1890 | } |
| 1891 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 1892 | expr_ty expr1, expr2; |
| 1893 | operator_ty operator; |
| 1894 | node *ch = CHILD(n, 0); |
| 1895 | |
| 1896 | if (TYPE(ch) == testlist) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1897 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1898 | else |
| 1899 | expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch)); |
| 1900 | |
| 1901 | if (!expr1) |
| 1902 | return NULL; |
| 1903 | if (expr1->kind == GeneratorExp_kind) { |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1904 | free_expr(expr1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1905 | ast_error(ch, "augmented assignment to generator " |
| 1906 | "expression not possible"); |
| 1907 | return NULL; |
| 1908 | } |
| 1909 | if (expr1->kind == Name_kind) { |
| 1910 | char *var_name = PyString_AS_STRING(expr1->v.Name.id); |
| 1911 | if (var_name[0] == 'N' && !strcmp(var_name, "None")) { |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1912 | free_expr(expr1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1913 | ast_error(ch, "assignment to None"); |
| 1914 | return NULL; |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | ch = CHILD(n, 2); |
| 1919 | if (TYPE(ch) == testlist) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1920 | expr2 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1921 | else |
| 1922 | expr2 = Yield(ast_for_expr(c, ch), LINENO(ch)); |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1923 | if (!expr2) { |
| 1924 | free_expr(expr1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 | return NULL; |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1926 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1927 | |
| 1928 | operator = ast_for_augassign(CHILD(n, 1)); |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1929 | if (!operator) { |
| 1930 | free_expr(expr1); |
| 1931 | free_expr(expr2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1932 | return NULL; |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1933 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1934 | |
| 1935 | return AugAssign(expr1, operator, expr2, LINENO(n)); |
| 1936 | } |
| 1937 | else { |
| 1938 | int i; |
| 1939 | asdl_seq *targets; |
| 1940 | node *value; |
| 1941 | expr_ty expression; |
| 1942 | |
| 1943 | /* a normal assignment */ |
| 1944 | REQ(CHILD(n, 1), EQUAL); |
| 1945 | targets = asdl_seq_new(NCH(n) / 2); |
| 1946 | if (!targets) |
| 1947 | return NULL; |
| 1948 | for (i = 0; i < NCH(n) - 2; i += 2) { |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1949 | expr_ty e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1950 | node *ch = CHILD(n, i); |
| 1951 | if (TYPE(ch) == yield_expr) { |
| 1952 | ast_error(ch, "assignment to yield expression not possible"); |
| 1953 | goto error; |
| 1954 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1955 | e = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1956 | |
| 1957 | /* set context to assign */ |
| 1958 | if (!e) |
| 1959 | goto error; |
| 1960 | |
| 1961 | if (!set_context(e, Store, CHILD(n, i))) { |
| 1962 | free_expr(e); |
| 1963 | goto error; |
| 1964 | } |
| 1965 | |
| 1966 | asdl_seq_SET(targets, i / 2, e); |
| 1967 | } |
| 1968 | value = CHILD(n, NCH(n) - 1); |
| 1969 | if (TYPE(value) == testlist) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1970 | expression = ast_for_testlist(c, value); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1971 | else |
| 1972 | expression = ast_for_expr(c, value); |
| 1973 | if (!expression) |
Neal Norwitz | e8c0536 | 2005-11-14 00:18:03 +0000 | [diff] [blame^] | 1974 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1975 | return Assign(targets, expression, LINENO(n)); |
| 1976 | error: |
| 1977 | for (i = i / 2; i >= 0; i--) |
| 1978 | free_expr((expr_ty)asdl_seq_GET(targets, i)); |
| 1979 | asdl_seq_free(targets); |
| 1980 | return NULL; |
| 1981 | } |
| 1982 | return NULL; |
| 1983 | } |
| 1984 | |
| 1985 | static stmt_ty |
| 1986 | ast_for_print_stmt(struct compiling *c, const node *n) |
| 1987 | { |
| 1988 | /* print_stmt: 'print' ( [ test (',' test)* [','] ] |
| 1989 | | '>>' test [ (',' test)+ [','] ] ) |
| 1990 | */ |
| 1991 | expr_ty dest = NULL, expression; |
| 1992 | asdl_seq *seq; |
| 1993 | bool nl; |
| 1994 | int i, start = 1; |
| 1995 | |
| 1996 | REQ(n, print_stmt); |
| 1997 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 1998 | dest = ast_for_expr(c, CHILD(n, 2)); |
| 1999 | if (!dest) |
| 2000 | return NULL; |
| 2001 | start = 4; |
| 2002 | } |
| 2003 | seq = asdl_seq_new((NCH(n) + 1 - start) / 2); |
| 2004 | if (!seq) |
| 2005 | return NULL; |
| 2006 | for (i = start; i < NCH(n); i += 2) { |
| 2007 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2008 | if (!expression) { |
| 2009 | asdl_seq_free(seq); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | asdl_seq_APPEND(seq, expression); |
| 2014 | } |
| 2015 | nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true; |
| 2016 | return Print(dest, seq, nl, LINENO(n)); |
| 2017 | } |
| 2018 | |
| 2019 | static asdl_seq * |
| 2020 | ast_for_exprlist(struct compiling *c, const node *n, int context) |
| 2021 | { |
| 2022 | asdl_seq *seq; |
| 2023 | int i; |
| 2024 | expr_ty e; |
| 2025 | |
| 2026 | REQ(n, exprlist); |
| 2027 | |
| 2028 | seq = asdl_seq_new((NCH(n) + 1) / 2); |
| 2029 | if (!seq) |
| 2030 | return NULL; |
| 2031 | for (i = 0; i < NCH(n); i += 2) { |
| 2032 | e = ast_for_expr(c, CHILD(n, i)); |
| 2033 | if (!e) { |
| 2034 | asdl_seq_free(seq); |
| 2035 | return NULL; |
| 2036 | } |
| 2037 | if (context) { |
| 2038 | if (!set_context(e, context, CHILD(n, i))) |
| 2039 | return NULL; |
| 2040 | } |
| 2041 | asdl_seq_SET(seq, i / 2, e); |
| 2042 | } |
| 2043 | return seq; |
| 2044 | } |
| 2045 | |
| 2046 | static stmt_ty |
| 2047 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 2048 | { |
| 2049 | asdl_seq *expr_list; |
| 2050 | |
| 2051 | /* del_stmt: 'del' exprlist */ |
| 2052 | REQ(n, del_stmt); |
| 2053 | |
| 2054 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 2055 | if (!expr_list) |
| 2056 | return NULL; |
| 2057 | return Delete(expr_list, LINENO(n)); |
| 2058 | } |
| 2059 | |
| 2060 | static stmt_ty |
| 2061 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 2062 | { |
| 2063 | /* |
| 2064 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 2065 | | yield_stmt |
| 2066 | break_stmt: 'break' |
| 2067 | continue_stmt: 'continue' |
| 2068 | return_stmt: 'return' [testlist] |
| 2069 | yield_stmt: yield_expr |
| 2070 | yield_expr: 'yield' testlist |
| 2071 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 2072 | */ |
| 2073 | node *ch; |
| 2074 | |
| 2075 | REQ(n, flow_stmt); |
| 2076 | ch = CHILD(n, 0); |
| 2077 | switch (TYPE(ch)) { |
| 2078 | case break_stmt: |
| 2079 | return Break(LINENO(n)); |
| 2080 | case continue_stmt: |
| 2081 | return Continue(LINENO(n)); |
| 2082 | case yield_stmt: { /* will reduce to yield_expr */ |
| 2083 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 2084 | if (!exp) |
| 2085 | return NULL; |
| 2086 | return Expr(exp, LINENO(n)); |
| 2087 | } |
| 2088 | case return_stmt: |
| 2089 | if (NCH(ch) == 1) |
| 2090 | return Return(NULL, LINENO(n)); |
| 2091 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2092 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2093 | if (!expression) |
| 2094 | return NULL; |
| 2095 | return Return(expression, LINENO(n)); |
| 2096 | } |
| 2097 | case raise_stmt: |
| 2098 | if (NCH(ch) == 1) |
| 2099 | return Raise(NULL, NULL, NULL, LINENO(n)); |
| 2100 | else if (NCH(ch) == 2) { |
| 2101 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 2102 | if (!expression) |
| 2103 | return NULL; |
| 2104 | return Raise(expression, NULL, NULL, LINENO(n)); |
| 2105 | } |
| 2106 | else if (NCH(ch) == 4) { |
| 2107 | expr_ty expr1, expr2; |
| 2108 | |
| 2109 | expr1 = ast_for_expr(c, CHILD(ch, 1)); |
| 2110 | if (!expr1) |
| 2111 | return NULL; |
| 2112 | expr2 = ast_for_expr(c, CHILD(ch, 3)); |
| 2113 | if (!expr2) |
| 2114 | return NULL; |
| 2115 | |
| 2116 | return Raise(expr1, expr2, NULL, LINENO(n)); |
| 2117 | } |
| 2118 | else if (NCH(ch) == 6) { |
| 2119 | expr_ty expr1, expr2, expr3; |
| 2120 | |
| 2121 | expr1 = ast_for_expr(c, CHILD(ch, 1)); |
| 2122 | if (!expr1) |
| 2123 | return NULL; |
| 2124 | expr2 = ast_for_expr(c, CHILD(ch, 3)); |
| 2125 | if (!expr2) |
| 2126 | return NULL; |
| 2127 | expr3 = ast_for_expr(c, CHILD(ch, 5)); |
| 2128 | if (!expr3) |
| 2129 | return NULL; |
| 2130 | |
| 2131 | return Raise(expr1, expr2, expr3, LINENO(n)); |
| 2132 | } |
| 2133 | default: |
| 2134 | PyErr_Format(PyExc_Exception, |
| 2135 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | static alias_ty |
| 2141 | alias_for_import_name(const node *n) |
| 2142 | { |
| 2143 | /* |
| 2144 | import_as_name: NAME [NAME NAME] |
| 2145 | dotted_as_name: dotted_name [NAME NAME] |
| 2146 | dotted_name: NAME ('.' NAME)* |
| 2147 | */ |
| 2148 | loop: |
| 2149 | switch (TYPE(n)) { |
| 2150 | case import_as_name: |
| 2151 | if (NCH(n) == 3) |
| 2152 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), |
| 2153 | NEW_IDENTIFIER(CHILD(n, 2))); |
| 2154 | else |
| 2155 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), |
| 2156 | NULL); |
| 2157 | break; |
| 2158 | case dotted_as_name: |
| 2159 | if (NCH(n) == 1) { |
| 2160 | n = CHILD(n, 0); |
| 2161 | goto loop; |
| 2162 | } |
| 2163 | else { |
| 2164 | alias_ty a = alias_for_import_name(CHILD(n, 0)); |
| 2165 | assert(!a->asname); |
| 2166 | a->asname = NEW_IDENTIFIER(CHILD(n, 2)); |
| 2167 | return a; |
| 2168 | } |
| 2169 | break; |
| 2170 | case dotted_name: |
| 2171 | if (NCH(n) == 1) |
| 2172 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL); |
| 2173 | else { |
| 2174 | /* Create a string of the form "a.b.c" */ |
| 2175 | int i, len; |
| 2176 | PyObject *str; |
| 2177 | char *s; |
| 2178 | |
| 2179 | len = 0; |
| 2180 | for (i = 0; i < NCH(n); i += 2) |
| 2181 | /* length of string plus one for the dot */ |
| 2182 | len += strlen(STR(CHILD(n, i))) + 1; |
| 2183 | len--; /* the last name doesn't have a dot */ |
| 2184 | str = PyString_FromStringAndSize(NULL, len); |
| 2185 | if (!str) |
| 2186 | return NULL; |
| 2187 | s = PyString_AS_STRING(str); |
| 2188 | if (!s) |
| 2189 | return NULL; |
| 2190 | for (i = 0; i < NCH(n); i += 2) { |
| 2191 | char *sch = STR(CHILD(n, i)); |
| 2192 | strcpy(s, STR(CHILD(n, i))); |
| 2193 | s += strlen(sch); |
| 2194 | *s++ = '.'; |
| 2195 | } |
| 2196 | --s; |
| 2197 | *s = '\0'; |
| 2198 | PyString_InternInPlace(&str); |
| 2199 | return alias(str, NULL); |
| 2200 | } |
| 2201 | break; |
| 2202 | case STAR: |
| 2203 | return alias(PyString_InternFromString("*"), NULL); |
| 2204 | default: |
| 2205 | PyErr_Format(PyExc_Exception, |
| 2206 | "unexpected import name: %d", TYPE(n)); |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | |
| 2212 | static stmt_ty |
| 2213 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 2214 | { |
| 2215 | /* |
| 2216 | import_stmt: import_name | import_from |
| 2217 | import_name: 'import' dotted_as_names |
| 2218 | import_from: 'from' dotted_name 'import' ('*' | |
| 2219 | '(' import_as_names ')' | |
| 2220 | import_as_names) |
| 2221 | */ |
| 2222 | int i; |
| 2223 | asdl_seq *aliases; |
| 2224 | |
| 2225 | REQ(n, import_stmt); |
| 2226 | n = CHILD(n, 0); |
| 2227 | if (STR(CHILD(n, 0))[0] == 'i') { /* import */ |
| 2228 | n = CHILD(n, 1); |
Neil Schemenauer | 147b759 | 2005-10-23 03:38:19 +0000 | [diff] [blame] | 2229 | REQ(n, dotted_as_names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2230 | aliases = asdl_seq_new((NCH(n) + 1) / 2); |
| 2231 | if (!aliases) |
| 2232 | return NULL; |
| 2233 | for (i = 0; i < NCH(n); i += 2) { |
| 2234 | alias_ty import_alias = alias_for_import_name(CHILD(n, i)); |
| 2235 | if (!import_alias) { |
| 2236 | asdl_seq_free(aliases); |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | asdl_seq_SET(aliases, i / 2, import_alias); |
| 2240 | } |
| 2241 | return Import(aliases, LINENO(n)); |
| 2242 | } |
| 2243 | else if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
| 2244 | stmt_ty import; |
| 2245 | int n_children; |
| 2246 | const char *from_modules; |
| 2247 | int lineno = LINENO(n); |
| 2248 | alias_ty mod = alias_for_import_name(CHILD(n, 1)); |
| 2249 | if (!mod) |
| 2250 | return NULL; |
| 2251 | |
| 2252 | /* XXX this needs to be cleaned up */ |
| 2253 | |
| 2254 | from_modules = STR(CHILD(n, 3)); |
| 2255 | if (!from_modules) { |
| 2256 | n = CHILD(n, 3); /* from ... import x, y, z */ |
| 2257 | if (NCH(n) % 2 == 0) { |
| 2258 | /* it ends with a comma, not valid but the parser allows it */ |
| 2259 | ast_error(n, "trailing comma not allowed without" |
| 2260 | " surrounding parentheses"); |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | } |
| 2264 | else if (from_modules[0] == '*') { |
| 2265 | n = CHILD(n, 3); /* from ... import * */ |
| 2266 | } |
| 2267 | else if (from_modules[0] == '(') |
| 2268 | n = CHILD(n, 4); /* from ... import (x, y, z) */ |
| 2269 | else |
| 2270 | return NULL; |
| 2271 | |
| 2272 | n_children = NCH(n); |
| 2273 | if (from_modules && from_modules[0] == '*') |
| 2274 | n_children = 1; |
| 2275 | |
| 2276 | aliases = asdl_seq_new((n_children + 1) / 2); |
| 2277 | if (!aliases) { |
| 2278 | free_alias(mod); |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | |
| 2282 | /* handle "from ... import *" special b/c there's no children */ |
| 2283 | if (from_modules && from_modules[0] == '*') { |
| 2284 | alias_ty import_alias = alias_for_import_name(n); |
| 2285 | if (!import_alias) { |
| 2286 | asdl_seq_free(aliases); |
| 2287 | free_alias(mod); |
| 2288 | return NULL; |
| 2289 | } |
| 2290 | asdl_seq_APPEND(aliases, import_alias); |
| 2291 | } |
| 2292 | |
| 2293 | for (i = 0; i < NCH(n); i += 2) { |
| 2294 | alias_ty import_alias = alias_for_import_name(CHILD(n, i)); |
| 2295 | if (!import_alias) { |
| 2296 | asdl_seq_free(aliases); |
| 2297 | free_alias(mod); |
| 2298 | return NULL; |
| 2299 | } |
| 2300 | asdl_seq_APPEND(aliases, import_alias); |
| 2301 | } |
| 2302 | Py_INCREF(mod->name); |
| 2303 | import = ImportFrom(mod->name, aliases, lineno); |
| 2304 | free_alias(mod); |
| 2305 | return import; |
| 2306 | } |
| 2307 | PyErr_Format(PyExc_Exception, |
| 2308 | "unknown import statement: starts with command '%s'", |
| 2309 | STR(CHILD(n, 0))); |
| 2310 | return NULL; |
| 2311 | } |
| 2312 | |
| 2313 | static stmt_ty |
| 2314 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 2315 | { |
| 2316 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 2317 | identifier name; |
| 2318 | asdl_seq *s; |
| 2319 | int i; |
| 2320 | |
| 2321 | REQ(n, global_stmt); |
| 2322 | s = asdl_seq_new(NCH(n) / 2); |
| 2323 | if (!s) |
| 2324 | return NULL; |
| 2325 | for (i = 1; i < NCH(n); i += 2) { |
| 2326 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 2327 | if (!name) { |
| 2328 | asdl_seq_free(s); |
| 2329 | return NULL; |
| 2330 | } |
| 2331 | asdl_seq_SET(s, i / 2, name); |
| 2332 | } |
| 2333 | return Global(s, LINENO(n)); |
| 2334 | } |
| 2335 | |
| 2336 | static stmt_ty |
| 2337 | ast_for_exec_stmt(struct compiling *c, const node *n) |
| 2338 | { |
| 2339 | expr_ty expr1, globals = NULL, locals = NULL; |
| 2340 | int n_children = NCH(n); |
| 2341 | if (n_children != 2 && n_children != 4 && n_children != 6) { |
| 2342 | PyErr_Format(PyExc_Exception, |
| 2343 | "poorly formed 'exec' statement: %d parts to statement", |
| 2344 | n_children); |
| 2345 | return NULL; |
| 2346 | } |
| 2347 | |
| 2348 | /* exec_stmt: 'exec' expr ['in' test [',' test]] */ |
| 2349 | REQ(n, exec_stmt); |
| 2350 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 2351 | if (!expr1) |
| 2352 | return NULL; |
| 2353 | if (n_children >= 4) { |
| 2354 | globals = ast_for_expr(c, CHILD(n, 3)); |
| 2355 | if (!globals) |
| 2356 | return NULL; |
| 2357 | } |
| 2358 | if (n_children == 6) { |
| 2359 | locals = ast_for_expr(c, CHILD(n, 5)); |
| 2360 | if (!locals) |
| 2361 | return NULL; |
| 2362 | } |
| 2363 | |
| 2364 | return Exec(expr1, globals, locals, LINENO(n)); |
| 2365 | } |
| 2366 | |
| 2367 | static stmt_ty |
| 2368 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 2369 | { |
| 2370 | /* assert_stmt: 'assert' test [',' test] */ |
| 2371 | REQ(n, assert_stmt); |
| 2372 | if (NCH(n) == 2) { |
| 2373 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2374 | if (!expression) |
| 2375 | return NULL; |
| 2376 | return Assert(expression, NULL, LINENO(n)); |
| 2377 | } |
| 2378 | else if (NCH(n) == 4) { |
| 2379 | expr_ty expr1, expr2; |
| 2380 | |
| 2381 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 2382 | if (!expr1) |
| 2383 | return NULL; |
| 2384 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 2385 | if (!expr2) |
| 2386 | return NULL; |
| 2387 | |
| 2388 | return Assert(expr1, expr2, LINENO(n)); |
| 2389 | } |
| 2390 | PyErr_Format(PyExc_Exception, |
| 2391 | "improper number of parts to 'assert' statement: %d", |
| 2392 | NCH(n)); |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | |
| 2396 | static asdl_seq * |
| 2397 | ast_for_suite(struct compiling *c, const node *n) |
| 2398 | { |
| 2399 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
| 2400 | asdl_seq *seq = NULL; |
| 2401 | stmt_ty s; |
| 2402 | int i, total, num, end, pos = 0; |
| 2403 | node *ch; |
| 2404 | |
| 2405 | REQ(n, suite); |
| 2406 | |
| 2407 | total = num_stmts(n); |
| 2408 | seq = asdl_seq_new(total); |
| 2409 | if (!seq) |
| 2410 | return NULL; |
| 2411 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
| 2412 | n = CHILD(n, 0); |
| 2413 | /* simple_stmt always ends with a NEWLINE, |
| 2414 | and may have a trailing SEMI |
| 2415 | */ |
| 2416 | end = NCH(n) - 1; |
| 2417 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 2418 | end--; |
| 2419 | /* loop by 2 to skip semi-colons */ |
| 2420 | for (i = 0; i < end; i += 2) { |
| 2421 | ch = CHILD(n, i); |
| 2422 | s = ast_for_stmt(c, ch); |
| 2423 | if (!s) |
| 2424 | goto error; |
| 2425 | asdl_seq_SET(seq, pos++, s); |
| 2426 | } |
| 2427 | } |
| 2428 | else { |
| 2429 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 2430 | ch = CHILD(n, i); |
| 2431 | REQ(ch, stmt); |
| 2432 | num = num_stmts(ch); |
| 2433 | if (num == 1) { |
| 2434 | /* small_stmt or compound_stmt with only one child */ |
| 2435 | s = ast_for_stmt(c, ch); |
| 2436 | if (!s) |
| 2437 | goto error; |
| 2438 | asdl_seq_SET(seq, pos++, s); |
| 2439 | } |
| 2440 | else { |
| 2441 | int j; |
| 2442 | ch = CHILD(ch, 0); |
| 2443 | REQ(ch, simple_stmt); |
| 2444 | for (j = 0; j < NCH(ch); j += 2) { |
| 2445 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 2446 | if (!s) |
| 2447 | goto error; |
| 2448 | asdl_seq_SET(seq, pos++, s); |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | assert(pos == seq->size); |
| 2454 | return seq; |
| 2455 | error: |
| 2456 | if (seq) |
| 2457 | asdl_seq_free(seq); |
| 2458 | return NULL; |
| 2459 | } |
| 2460 | |
| 2461 | static stmt_ty |
| 2462 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 2463 | { |
| 2464 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 2465 | ['else' ':' suite] |
| 2466 | */ |
| 2467 | char *s; |
| 2468 | |
| 2469 | REQ(n, if_stmt); |
| 2470 | |
| 2471 | if (NCH(n) == 4) { |
| 2472 | expr_ty expression; |
| 2473 | asdl_seq *suite_seq; |
| 2474 | |
| 2475 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2476 | if (!expression) |
| 2477 | return NULL; |
| 2478 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 2479 | if (!suite_seq) |
| 2480 | return NULL; |
| 2481 | |
| 2482 | return If(expression, suite_seq, NULL, LINENO(n)); |
| 2483 | } |
| 2484 | s = STR(CHILD(n, 4)); |
| 2485 | /* s[2], the third character in the string, will be |
| 2486 | 's' for el_s_e, or |
| 2487 | 'i' for el_i_f |
| 2488 | */ |
| 2489 | if (s[2] == 's') { |
| 2490 | expr_ty expression; |
| 2491 | asdl_seq *seq1, *seq2; |
| 2492 | |
| 2493 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2494 | if (!expression) |
| 2495 | return NULL; |
| 2496 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
| 2497 | if (!seq1) |
| 2498 | return NULL; |
| 2499 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
| 2500 | if (!seq2) |
| 2501 | return NULL; |
| 2502 | |
| 2503 | return If(expression, seq1, seq2, LINENO(n)); |
| 2504 | } |
| 2505 | else if (s[2] == 'i') { |
| 2506 | int i, n_elif, has_else = 0; |
| 2507 | asdl_seq *orelse = NULL; |
| 2508 | n_elif = NCH(n) - 4; |
| 2509 | /* must reference the child n_elif+1 since 'else' token is third, |
| 2510 | not fourth, child from the end. */ |
| 2511 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 2512 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 2513 | has_else = 1; |
| 2514 | n_elif -= 3; |
| 2515 | } |
| 2516 | n_elif /= 4; |
| 2517 | |
| 2518 | if (has_else) { |
| 2519 | expr_ty expression; |
| 2520 | asdl_seq *seq1, *seq2; |
| 2521 | |
| 2522 | orelse = asdl_seq_new(1); |
| 2523 | if (!orelse) |
| 2524 | return NULL; |
| 2525 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
| 2526 | if (!expression) { |
| 2527 | asdl_seq_free(orelse); |
| 2528 | return NULL; |
| 2529 | } |
| 2530 | seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 2531 | if (!seq1) { |
| 2532 | asdl_seq_free(orelse); |
| 2533 | return NULL; |
| 2534 | } |
| 2535 | seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 2536 | if (!seq2) { |
| 2537 | asdl_seq_free(orelse); |
| 2538 | return NULL; |
| 2539 | } |
| 2540 | |
| 2541 | asdl_seq_SET(orelse, 0, If(expression, seq1, seq2, |
| 2542 | LINENO(CHILD(n, NCH(n) - 6)))); |
| 2543 | /* the just-created orelse handled the last elif */ |
| 2544 | n_elif--; |
| 2545 | } |
| 2546 | else |
| 2547 | orelse = NULL; |
| 2548 | |
| 2549 | for (i = 0; i < n_elif; i++) { |
| 2550 | int off = 5 + (n_elif - i - 1) * 4; |
| 2551 | expr_ty expression; |
| 2552 | asdl_seq *suite_seq; |
| 2553 | asdl_seq *new = asdl_seq_new(1); |
| 2554 | if (!new) |
| 2555 | return NULL; |
| 2556 | expression = ast_for_expr(c, CHILD(n, off)); |
| 2557 | if (!expression) { |
| 2558 | asdl_seq_free(new); |
| 2559 | return NULL; |
| 2560 | } |
| 2561 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
| 2562 | if (!suite_seq) { |
| 2563 | asdl_seq_free(new); |
| 2564 | return NULL; |
| 2565 | } |
| 2566 | |
| 2567 | asdl_seq_SET(new, 0, |
| 2568 | If(expression, suite_seq, orelse, |
| 2569 | LINENO(CHILD(n, off)))); |
| 2570 | orelse = new; |
| 2571 | } |
| 2572 | return If(ast_for_expr(c, CHILD(n, 1)), |
| 2573 | ast_for_suite(c, CHILD(n, 3)), |
| 2574 | orelse, LINENO(n)); |
| 2575 | } |
| 2576 | else { |
| 2577 | PyErr_Format(PyExc_Exception, |
| 2578 | "unexpected token in 'if' statement: %s", s); |
| 2579 | return NULL; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | static stmt_ty |
| 2584 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 2585 | { |
| 2586 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 2587 | REQ(n, while_stmt); |
| 2588 | |
| 2589 | if (NCH(n) == 4) { |
| 2590 | expr_ty expression; |
| 2591 | asdl_seq *suite_seq; |
| 2592 | |
| 2593 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2594 | if (!expression) |
| 2595 | return NULL; |
| 2596 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 2597 | if (!suite_seq) |
| 2598 | return NULL; |
| 2599 | return While(expression, suite_seq, NULL, LINENO(n)); |
| 2600 | } |
| 2601 | else if (NCH(n) == 7) { |
| 2602 | expr_ty expression; |
| 2603 | asdl_seq *seq1, *seq2; |
| 2604 | |
| 2605 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2606 | if (!expression) |
| 2607 | return NULL; |
| 2608 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
| 2609 | if (!seq1) |
| 2610 | return NULL; |
| 2611 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
| 2612 | if (!seq2) |
| 2613 | return NULL; |
| 2614 | |
| 2615 | return While(expression, seq1, seq2, LINENO(n)); |
| 2616 | } |
| 2617 | else { |
| 2618 | PyErr_Format(PyExc_Exception, |
| 2619 | "wrong number of tokens for 'while' statement: %d", |
| 2620 | NCH(n)); |
| 2621 | return NULL; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | static stmt_ty |
| 2626 | ast_for_for_stmt(struct compiling *c, const node *n) |
| 2627 | { |
| 2628 | asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL; |
| 2629 | expr_ty expression; |
| 2630 | expr_ty target; |
| 2631 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 2632 | REQ(n, for_stmt); |
| 2633 | |
| 2634 | if (NCH(n) == 9) { |
| 2635 | seq = ast_for_suite(c, CHILD(n, 8)); |
| 2636 | if (!seq) |
| 2637 | return NULL; |
| 2638 | } |
| 2639 | |
| 2640 | _target = ast_for_exprlist(c, CHILD(n, 1), Store); |
| 2641 | if (!_target) |
| 2642 | return NULL; |
| 2643 | if (asdl_seq_LEN(_target) == 1) { |
| 2644 | target = asdl_seq_GET(_target, 0); |
| 2645 | asdl_seq_free(_target); |
| 2646 | } |
| 2647 | else |
| 2648 | target = Tuple(_target, Store, LINENO(n)); |
| 2649 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2650 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2651 | if (!expression) |
| 2652 | return NULL; |
| 2653 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
| 2654 | if (!suite_seq) |
| 2655 | return NULL; |
| 2656 | |
| 2657 | return For(target, expression, suite_seq, seq, LINENO(n)); |
| 2658 | } |
| 2659 | |
| 2660 | static excepthandler_ty |
| 2661 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 2662 | { |
| 2663 | /* except_clause: 'except' [test [',' test]] */ |
| 2664 | REQ(exc, except_clause); |
| 2665 | REQ(body, suite); |
| 2666 | |
| 2667 | if (NCH(exc) == 1) { |
| 2668 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 2669 | if (!suite_seq) |
| 2670 | return NULL; |
| 2671 | |
| 2672 | return excepthandler(NULL, NULL, suite_seq); |
| 2673 | } |
| 2674 | else if (NCH(exc) == 2) { |
| 2675 | expr_ty expression; |
| 2676 | asdl_seq *suite_seq; |
| 2677 | |
| 2678 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 2679 | if (!expression) |
| 2680 | return NULL; |
| 2681 | suite_seq = ast_for_suite(c, body); |
| 2682 | if (!suite_seq) |
| 2683 | return NULL; |
| 2684 | |
| 2685 | return excepthandler(expression, NULL, suite_seq); |
| 2686 | } |
| 2687 | else if (NCH(exc) == 4) { |
| 2688 | asdl_seq *suite_seq; |
| 2689 | expr_ty expression; |
| 2690 | expr_ty e = ast_for_expr(c, CHILD(exc, 3)); |
| 2691 | if (!e) |
| 2692 | return NULL; |
| 2693 | if (!set_context(e, Store, CHILD(exc, 3))) |
| 2694 | return NULL; |
| 2695 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 2696 | if (!expression) |
| 2697 | return NULL; |
| 2698 | suite_seq = ast_for_suite(c, body); |
| 2699 | if (!suite_seq) |
| 2700 | return NULL; |
| 2701 | |
| 2702 | return excepthandler(expression, e, suite_seq); |
| 2703 | } |
| 2704 | else { |
| 2705 | PyErr_Format(PyExc_Exception, |
| 2706 | "wrong number of children for 'except' clause: %d", |
| 2707 | NCH(exc)); |
| 2708 | return NULL; |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | static stmt_ty |
| 2713 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 2714 | { |
| 2715 | REQ(n, try_stmt); |
| 2716 | |
| 2717 | if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */ |
| 2718 | /* try_stmt: 'try' ':' suite 'finally' ':' suite) */ |
| 2719 | asdl_seq *s1, *s2; |
| 2720 | s1 = ast_for_suite(c, CHILD(n, 2)); |
| 2721 | if (!s1) |
| 2722 | return NULL; |
| 2723 | s2 = ast_for_suite(c, CHILD(n, 5)); |
| 2724 | if (!s2) |
| 2725 | return NULL; |
| 2726 | |
| 2727 | return TryFinally(s1, s2, LINENO(n)); |
| 2728 | } |
| 2729 | else if (TYPE(CHILD(n, 3)) == except_clause) { |
| 2730 | /* try_stmt: ('try' ':' suite (except_clause ':' suite)+ |
| 2731 | ['else' ':' suite] |
| 2732 | */ |
| 2733 | asdl_seq *suite_seq1, *suite_seq2; |
| 2734 | asdl_seq *handlers; |
| 2735 | int i, has_else = 0, n_except = NCH(n) - 3; |
| 2736 | if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) { |
| 2737 | has_else = 1; |
| 2738 | n_except -= 3; |
| 2739 | } |
| 2740 | n_except /= 3; |
| 2741 | handlers = asdl_seq_new(n_except); |
| 2742 | if (!handlers) |
| 2743 | return NULL; |
| 2744 | for (i = 0; i < n_except; i++) { |
| 2745 | excepthandler_ty e = ast_for_except_clause(c, |
| 2746 | CHILD(n, 3 + i * 3), |
| 2747 | CHILD(n, 5 + i * 3)); |
| 2748 | if (!e) |
| 2749 | return NULL; |
| 2750 | asdl_seq_SET(handlers, i, e); |
| 2751 | } |
| 2752 | |
| 2753 | suite_seq1 = ast_for_suite(c, CHILD(n, 2)); |
| 2754 | if (!suite_seq1) |
| 2755 | return NULL; |
| 2756 | if (has_else) { |
| 2757 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 2758 | if (!suite_seq2) |
| 2759 | return NULL; |
| 2760 | } |
| 2761 | else |
| 2762 | suite_seq2 = NULL; |
| 2763 | |
| 2764 | return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n)); |
| 2765 | } |
| 2766 | else { |
Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 2767 | ast_error(n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2768 | return NULL; |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | static stmt_ty |
| 2773 | ast_for_classdef(struct compiling *c, const node *n) |
| 2774 | { |
| 2775 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2776 | asdl_seq *bases, *s; |
| 2777 | |
| 2778 | REQ(n, classdef); |
| 2779 | |
| 2780 | if (!strcmp(STR(CHILD(n, 1)), "None")) { |
| 2781 | ast_error(n, "assignment to None"); |
| 2782 | return NULL; |
| 2783 | } |
| 2784 | |
| 2785 | if (NCH(n) == 4) { |
| 2786 | s = ast_for_suite(c, CHILD(n, 3)); |
| 2787 | if (!s) |
| 2788 | return NULL; |
| 2789 | return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n)); |
| 2790 | } |
| 2791 | /* check for empty base list */ |
| 2792 | if (TYPE(CHILD(n,3)) == RPAR) { |
| 2793 | s = ast_for_suite(c, CHILD(n,5)); |
| 2794 | if (!s) |
| 2795 | return NULL; |
| 2796 | return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n)); |
| 2797 | } |
| 2798 | |
| 2799 | /* else handle the base class list */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2800 | bases = ast_for_class_bases(c, CHILD(n, 3)); |
| 2801 | if (!bases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2802 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2803 | |
| 2804 | s = ast_for_suite(c, CHILD(n, 6)); |
| 2805 | if (!s) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2806 | asdl_seq_free(bases); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2807 | return NULL; |
| 2808 | } |
| 2809 | return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n)); |
| 2810 | } |
| 2811 | |
| 2812 | static stmt_ty |
| 2813 | ast_for_stmt(struct compiling *c, const node *n) |
| 2814 | { |
| 2815 | if (TYPE(n) == stmt) { |
| 2816 | assert(NCH(n) == 1); |
| 2817 | n = CHILD(n, 0); |
| 2818 | } |
| 2819 | if (TYPE(n) == simple_stmt) { |
| 2820 | assert(num_stmts(n) == 1); |
| 2821 | n = CHILD(n, 0); |
| 2822 | } |
| 2823 | if (TYPE(n) == small_stmt) { |
| 2824 | REQ(n, small_stmt); |
| 2825 | n = CHILD(n, 0); |
| 2826 | /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt |
| 2827 | | flow_stmt | import_stmt | global_stmt | exec_stmt |
| 2828 | | assert_stmt |
| 2829 | */ |
| 2830 | switch (TYPE(n)) { |
| 2831 | case expr_stmt: |
| 2832 | return ast_for_expr_stmt(c, n); |
| 2833 | case print_stmt: |
| 2834 | return ast_for_print_stmt(c, n); |
| 2835 | case del_stmt: |
| 2836 | return ast_for_del_stmt(c, n); |
| 2837 | case pass_stmt: |
| 2838 | return Pass(LINENO(n)); |
| 2839 | case flow_stmt: |
| 2840 | return ast_for_flow_stmt(c, n); |
| 2841 | case import_stmt: |
| 2842 | return ast_for_import_stmt(c, n); |
| 2843 | case global_stmt: |
| 2844 | return ast_for_global_stmt(c, n); |
| 2845 | case exec_stmt: |
| 2846 | return ast_for_exec_stmt(c, n); |
| 2847 | case assert_stmt: |
| 2848 | return ast_for_assert_stmt(c, n); |
| 2849 | default: |
| 2850 | PyErr_Format(PyExc_Exception, |
| 2851 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 2852 | TYPE(n), NCH(n)); |
| 2853 | return NULL; |
| 2854 | } |
| 2855 | } |
| 2856 | else { |
| 2857 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
| 2858 | | funcdef | classdef |
| 2859 | */ |
| 2860 | node *ch = CHILD(n, 0); |
| 2861 | REQ(n, compound_stmt); |
| 2862 | switch (TYPE(ch)) { |
| 2863 | case if_stmt: |
| 2864 | return ast_for_if_stmt(c, ch); |
| 2865 | case while_stmt: |
| 2866 | return ast_for_while_stmt(c, ch); |
| 2867 | case for_stmt: |
| 2868 | return ast_for_for_stmt(c, ch); |
| 2869 | case try_stmt: |
| 2870 | return ast_for_try_stmt(c, ch); |
| 2871 | case funcdef: |
| 2872 | return ast_for_funcdef(c, ch); |
| 2873 | case classdef: |
| 2874 | return ast_for_classdef(c, ch); |
| 2875 | default: |
| 2876 | PyErr_Format(PyExc_Exception, |
| 2877 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 2878 | TYPE(n), NCH(n)); |
| 2879 | return NULL; |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | static PyObject * |
| 2885 | parsenumber(const char *s) |
| 2886 | { |
| 2887 | const char *end; |
| 2888 | long x; |
| 2889 | double dx; |
| 2890 | #ifndef WITHOUT_COMPLEX |
| 2891 | Py_complex c; |
| 2892 | int imflag; |
| 2893 | #endif |
| 2894 | |
| 2895 | errno = 0; |
| 2896 | end = s + strlen(s) - 1; |
| 2897 | #ifndef WITHOUT_COMPLEX |
| 2898 | imflag = *end == 'j' || *end == 'J'; |
| 2899 | #endif |
| 2900 | if (*end == 'l' || *end == 'L') |
| 2901 | return PyLong_FromString((char *)s, (char **)0, 0); |
| 2902 | if (s[0] == '0') { |
| 2903 | x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); |
| 2904 | if (x < 0 && errno == 0) { |
| 2905 | return PyLong_FromString((char *)s, |
| 2906 | (char **)0, |
| 2907 | 0); |
| 2908 | } |
| 2909 | } |
| 2910 | else |
| 2911 | x = PyOS_strtol((char *)s, (char **)&end, 0); |
| 2912 | if (*end == '\0') { |
| 2913 | if (errno != 0) |
| 2914 | return PyLong_FromString((char *)s, (char **)0, 0); |
| 2915 | return PyInt_FromLong(x); |
| 2916 | } |
| 2917 | /* XXX Huge floats may silently fail */ |
| 2918 | #ifndef WITHOUT_COMPLEX |
| 2919 | if (imflag) { |
| 2920 | c.real = 0.; |
| 2921 | PyFPE_START_PROTECT("atof", return 0) |
| 2922 | c.imag = atof(s); |
| 2923 | PyFPE_END_PROTECT(c) |
| 2924 | return PyComplex_FromCComplex(c); |
| 2925 | } |
| 2926 | else |
| 2927 | #endif |
| 2928 | { |
| 2929 | PyFPE_START_PROTECT("atof", return 0) |
| 2930 | dx = atof(s); |
| 2931 | PyFPE_END_PROTECT(dx) |
| 2932 | return PyFloat_FromDouble(dx); |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | static PyObject * |
| 2937 | decode_utf8(const char **sPtr, const char *end, char* encoding) |
| 2938 | { |
| 2939 | #ifndef Py_USING_UNICODE |
| 2940 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 2941 | return NULL; |
| 2942 | #else |
| 2943 | PyObject *u, *v; |
| 2944 | char *s, *t; |
| 2945 | t = s = (char *)*sPtr; |
| 2946 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 2947 | while (s < end && (*s & 0x80)) s++; |
| 2948 | *sPtr = s; |
| 2949 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 2950 | if (u == NULL) |
| 2951 | return NULL; |
| 2952 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 2953 | Py_DECREF(u); |
| 2954 | return v; |
| 2955 | #endif |
| 2956 | } |
| 2957 | |
| 2958 | static PyObject * |
| 2959 | decode_unicode(const char *s, size_t len, int rawmode, const char *encoding) |
| 2960 | { |
| 2961 | PyObject *v, *u; |
| 2962 | char *buf; |
| 2963 | char *p; |
| 2964 | const char *end; |
| 2965 | if (encoding == NULL) { |
| 2966 | buf = (char *)s; |
| 2967 | u = NULL; |
| 2968 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 2969 | buf = (char *)s; |
| 2970 | u = NULL; |
| 2971 | } else { |
| 2972 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 2973 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 2974 | if (u == NULL) |
| 2975 | return NULL; |
| 2976 | p = buf = PyString_AsString(u); |
| 2977 | end = s + len; |
| 2978 | while (s < end) { |
| 2979 | if (*s == '\\') { |
| 2980 | *p++ = *s++; |
| 2981 | if (*s & 0x80) { |
| 2982 | strcpy(p, "u005c"); |
| 2983 | p += 5; |
| 2984 | } |
| 2985 | } |
| 2986 | if (*s & 0x80) { /* XXX inefficient */ |
| 2987 | PyObject *w; |
| 2988 | char *r; |
| 2989 | int rn, i; |
| 2990 | w = decode_utf8(&s, end, "utf-16-be"); |
| 2991 | if (w == NULL) { |
| 2992 | Py_DECREF(u); |
| 2993 | return NULL; |
| 2994 | } |
| 2995 | r = PyString_AsString(w); |
| 2996 | rn = PyString_Size(w); |
| 2997 | assert(rn % 2 == 0); |
| 2998 | for (i = 0; i < rn; i += 2) { |
| 2999 | sprintf(p, "\\u%02x%02x", |
| 3000 | r[i + 0] & 0xFF, |
| 3001 | r[i + 1] & 0xFF); |
| 3002 | p += 6; |
| 3003 | } |
| 3004 | Py_DECREF(w); |
| 3005 | } else { |
| 3006 | *p++ = *s++; |
| 3007 | } |
| 3008 | } |
| 3009 | len = p - buf; |
| 3010 | s = buf; |
| 3011 | } |
| 3012 | if (rawmode) |
| 3013 | v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); |
| 3014 | else |
| 3015 | v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
| 3016 | Py_XDECREF(u); |
| 3017 | return v; |
| 3018 | } |
| 3019 | |
| 3020 | /* s is a Python string literal, including the bracketing quote characters, |
| 3021 | * and r &/or u prefixes (if any), and embedded escape sequences (if any). |
| 3022 | * parsestr parses it, and returns the decoded Python string object. |
| 3023 | */ |
| 3024 | static PyObject * |
| 3025 | parsestr(const char *s, const char *encoding) |
| 3026 | { |
| 3027 | PyObject *v; |
| 3028 | size_t len; |
| 3029 | int quote = *s; |
| 3030 | int rawmode = 0; |
| 3031 | int need_encoding; |
| 3032 | int unicode = 0; |
| 3033 | |
| 3034 | if (isalpha(quote) || quote == '_') { |
| 3035 | if (quote == 'u' || quote == 'U') { |
| 3036 | quote = *++s; |
| 3037 | unicode = 1; |
| 3038 | } |
| 3039 | if (quote == 'r' || quote == 'R') { |
| 3040 | quote = *++s; |
| 3041 | rawmode = 1; |
| 3042 | } |
| 3043 | } |
| 3044 | if (quote != '\'' && quote != '\"') { |
| 3045 | PyErr_BadInternalCall(); |
| 3046 | return NULL; |
| 3047 | } |
| 3048 | s++; |
| 3049 | len = strlen(s); |
| 3050 | if (len > INT_MAX) { |
| 3051 | PyErr_SetString(PyExc_OverflowError, |
| 3052 | "string to parse is too long"); |
| 3053 | return NULL; |
| 3054 | } |
| 3055 | if (s[--len] != quote) { |
| 3056 | PyErr_BadInternalCall(); |
| 3057 | return NULL; |
| 3058 | } |
| 3059 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 3060 | s += 2; |
| 3061 | len -= 2; |
| 3062 | if (s[--len] != quote || s[--len] != quote) { |
| 3063 | PyErr_BadInternalCall(); |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | } |
| 3067 | #ifdef Py_USING_UNICODE |
| 3068 | if (unicode || Py_UnicodeFlag) { |
| 3069 | return decode_unicode(s, len, rawmode, encoding); |
| 3070 | } |
| 3071 | #endif |
| 3072 | need_encoding = (encoding != NULL && |
| 3073 | strcmp(encoding, "utf-8") != 0 && |
| 3074 | strcmp(encoding, "iso-8859-1") != 0); |
| 3075 | if (rawmode || strchr(s, '\\') == NULL) { |
| 3076 | if (need_encoding) { |
| 3077 | #ifndef Py_USING_UNICODE |
| 3078 | /* This should not happen - we never see any other |
| 3079 | encoding. */ |
| 3080 | Py_FatalError("cannot deal with encodings in this build."); |
| 3081 | #else |
| 3082 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 3083 | if (u == NULL) |
| 3084 | return NULL; |
| 3085 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 3086 | Py_DECREF(u); |
| 3087 | return v; |
| 3088 | #endif |
| 3089 | } else { |
| 3090 | return PyString_FromStringAndSize(s, len); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 3095 | need_encoding ? encoding : NULL); |
| 3096 | return v; |
| 3097 | } |
| 3098 | |
| 3099 | /* Build a Python string object out of a STRING atom. This takes care of |
| 3100 | * compile-time literal catenation, calling parsestr() on each piece, and |
| 3101 | * pasting the intermediate results together. |
| 3102 | */ |
| 3103 | static PyObject * |
| 3104 | parsestrplus(struct compiling *c, const node *n) |
| 3105 | { |
| 3106 | PyObject *v; |
| 3107 | int i; |
| 3108 | REQ(CHILD(n, 0), STRING); |
| 3109 | if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) { |
| 3110 | /* String literal concatenation */ |
| 3111 | for (i = 1; i < NCH(n); i++) { |
| 3112 | PyObject *s; |
| 3113 | s = parsestr(STR(CHILD(n, i)), c->c_encoding); |
| 3114 | if (s == NULL) |
| 3115 | goto onError; |
| 3116 | if (PyString_Check(v) && PyString_Check(s)) { |
| 3117 | PyString_ConcatAndDel(&v, s); |
| 3118 | if (v == NULL) |
| 3119 | goto onError; |
| 3120 | } |
| 3121 | #ifdef Py_USING_UNICODE |
| 3122 | else { |
| 3123 | PyObject *temp; |
| 3124 | temp = PyUnicode_Concat(v, s); |
| 3125 | Py_DECREF(s); |
| 3126 | if (temp == NULL) |
| 3127 | goto onError; |
| 3128 | Py_DECREF(v); |
| 3129 | v = temp; |
| 3130 | } |
| 3131 | #endif |
| 3132 | } |
| 3133 | } |
| 3134 | return v; |
| 3135 | |
| 3136 | onError: |
| 3137 | Py_XDECREF(v); |
| 3138 | return NULL; |
| 3139 | } |