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