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