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