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