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