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