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