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