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