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