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 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 745 | /* first count the number of positional args & defaults */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 746 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 747 | ch = CHILD(n, i); |
| 748 | if (TYPE(ch) == STAR) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 749 | /* skip star and possible argument */ |
| 750 | i++; |
| 751 | i += (TYPE(CHILD(n, i)) == tfpdef |
| 752 | || TYPE(CHILD(n, i)) == vfpdef); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 755 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 756 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 757 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 758 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 759 | /* count the number of keyword only args & |
| 760 | defaults for keyword only args */ |
| 761 | for ( ; i < NCH(n); ++i) { |
| 762 | ch = CHILD(n, i); |
| 763 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 764 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 765 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 766 | posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL); |
| 767 | if (!posargs && nposargs) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 768 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 769 | kwonlyargs = (nkwonlyargs ? |
| 770 | asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
| 771 | if (!kwonlyargs && nkwonlyargs) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 772 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 773 | posdefaults = (nposdefaults ? |
| 774 | asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
| 775 | if (!posdefaults && nposdefaults) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 776 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 777 | /* The length of kwonlyargs and kwdefaults are same |
| 778 | since we set NULL as default for keyword only argument w/o default |
| 779 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 780 | kwdefaults = (nkwonlyargs ? |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 781 | asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
| 782 | if (!kwdefaults && nkwonlyargs) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 783 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 784 | |
| 785 | if (nposargs + nkwonlyargs > 255) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 786 | ast_error(n, "more than 255 arguments"); |
| 787 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 788 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 790 | /* tfpdef: NAME [':' test] |
| 791 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | */ |
| 793 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 794 | j = 0; /* index for defaults */ |
| 795 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 797 | ch = CHILD(n, i); |
| 798 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 799 | case tfpdef: |
| 800 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 801 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 802 | anything other than EQUAL or a comma? */ |
| 803 | /* XXX Should NCH(n) check be made a separate check? */ |
| 804 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 805 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 806 | if (!expression) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 807 | goto error; |
| 808 | assert(posdefaults != NULL); |
| 809 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 810 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 811 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 812 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 813 | else if (found_default) { |
| 814 | ast_error(n, |
| 815 | "non-default argument follows default argument"); |
| 816 | goto error; |
| 817 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 818 | arg = compiler_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 819 | if (!arg) |
| 820 | goto error; |
| 821 | asdl_seq_SET(posargs, k++, arg); |
| 822 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 823 | i += 2; /* the name and the comma */ |
| 824 | break; |
| 825 | case STAR: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 826 | if (i+1 >= NCH(n)) { |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 827 | ast_error(CHILD(n, i), |
| 828 | "named arguments must follow bare *"); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 829 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 830 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 831 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 832 | if (TYPE(ch) == COMMA) { |
| 833 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 834 | i += 2; /* now follows keyword only arguments */ |
| 835 | res = handle_keywordonly_args(c, n, i, |
| 836 | kwonlyargs, kwdefaults); |
| 837 | if (res == -1) goto error; |
| 838 | i = res; /* res has new position to process */ |
| 839 | } |
| 840 | else { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 841 | vararg = NEW_IDENTIFIER(CHILD(ch, 0)); |
| 842 | if (NCH(ch) > 1) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 843 | /* there is an annotation on the vararg */ |
| 844 | varargannotation = ast_for_expr(c, CHILD(ch, 2)); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 845 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 846 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 847 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 848 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 849 | int res = 0; |
| 850 | res = handle_keywordonly_args(c, n, i, |
| 851 | kwonlyargs, kwdefaults); |
| 852 | if (res == -1) goto error; |
| 853 | i = res; /* res has new position to process */ |
| 854 | } |
| 855 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | break; |
| 857 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 858 | ch = CHILD(n, i+1); /* tfpdef */ |
| 859 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 860 | kwarg = NEW_IDENTIFIER(CHILD(ch, 0)); |
| 861 | if (NCH(ch) > 1) { |
| 862 | /* there is an annotation on the kwarg */ |
| 863 | kwargannotation = ast_for_expr(c, CHILD(ch, 2)); |
| 864 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | i += 3; |
| 866 | break; |
| 867 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 868 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 869 | "unexpected node in varargslist: %d @ %d", |
| 870 | TYPE(ch), i); |
| 871 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 872 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 874 | return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg, |
| 875 | kwargannotation, posdefaults, kwdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 876 | error: |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 877 | Py_XDECREF(vararg); |
| 878 | Py_XDECREF(kwarg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | static expr_ty |
| 883 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 884 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 885 | expr_ty e; |
| 886 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 887 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 888 | int i; |
| 889 | |
| 890 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 891 | |
| 892 | lineno = LINENO(n); |
| 893 | col_offset = n->n_col_offset; |
| 894 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 895 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 896 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 897 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 898 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 899 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 900 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 901 | |
| 902 | for (i = 2; i < NCH(n); i+=2) { |
| 903 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 904 | if (!id) |
| 905 | return NULL; |
| 906 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 907 | if (!e) |
| 908 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | static expr_ty |
| 915 | ast_for_decorator(struct compiling *c, const node *n) |
| 916 | { |
| 917 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 918 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 919 | expr_ty name_expr; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 920 | |
| 921 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 922 | REQ(CHILD(n, 0), AT); |
| 923 | REQ(RCHILD(n, -1), NEWLINE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 924 | |
| 925 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 926 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 927 | return NULL; |
| 928 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 930 | d = name_expr; |
| 931 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 932 | } |
| 933 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 934 | d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 935 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 936 | if (!d) |
| 937 | return NULL; |
| 938 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 939 | } |
| 940 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 941 | d = ast_for_call(c, CHILD(n, 3), name_expr); |
| 942 | if (!d) |
| 943 | return NULL; |
| 944 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static asdl_seq* |
| 951 | ast_for_decorators(struct compiling *c, const node *n) |
| 952 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 953 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 954 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 955 | int i; |
| 956 | |
| 957 | REQ(n, decorators); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 958 | decorator_seq = asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 959 | if (!decorator_seq) |
| 960 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 961 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 962 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 963 | d = ast_for_decorator(c, CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 964 | if (!d) |
| 965 | return NULL; |
| 966 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 967 | } |
| 968 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 972 | 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] | 973 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 974 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 975 | identifier name; |
| 976 | arguments_ty args; |
| 977 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 978 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 979 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 980 | |
| 981 | REQ(n, funcdef); |
| 982 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 983 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 984 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 985 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 986 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 987 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 988 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 989 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 990 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 991 | if (!returns) |
| 992 | return NULL; |
| 993 | name_i += 2; |
| 994 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 995 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
| 996 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 997 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 998 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 999 | return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1000 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1003 | static stmt_ty |
| 1004 | ast_for_decorated(struct compiling *c, const node *n) |
| 1005 | { |
| 1006 | /* decorated: decorators (classdef | funcdef) */ |
| 1007 | stmt_ty thing = NULL; |
| 1008 | asdl_seq *decorator_seq = NULL; |
| 1009 | |
| 1010 | REQ(n, decorated); |
| 1011 | |
| 1012 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1013 | if (!decorator_seq) |
| 1014 | return NULL; |
| 1015 | |
| 1016 | assert(TYPE(CHILD(n, 1)) == funcdef || |
| 1017 | TYPE(CHILD(n, 1)) == classdef); |
| 1018 | |
| 1019 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1020 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1021 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1022 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
| 1023 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1024 | /* we count the decorators in when talking about the class' or |
| 1025 | * function's line number */ |
| 1026 | if (thing) { |
| 1027 | thing->lineno = LINENO(n); |
| 1028 | thing->col_offset = n->n_col_offset; |
| 1029 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1030 | return thing; |
| 1031 | } |
| 1032 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1033 | static expr_ty |
| 1034 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1035 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1036 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1037 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1038 | arguments_ty args; |
| 1039 | expr_ty expression; |
| 1040 | |
| 1041 | if (NCH(n) == 3) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1042 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1043 | NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1044 | if (!args) |
| 1045 | return NULL; |
| 1046 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1047 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1048 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1049 | } |
| 1050 | else { |
| 1051 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1052 | if (!args) |
| 1053 | return NULL; |
| 1054 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1055 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1056 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1059 | 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] | 1060 | } |
| 1061 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1062 | static expr_ty |
| 1063 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1064 | { |
| 1065 | /* test: or_test 'if' or_test 'else' test */ |
| 1066 | expr_ty expression, body, orelse; |
| 1067 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1068 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1069 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1070 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1071 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1072 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1073 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1074 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1075 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1076 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1077 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1078 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1079 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1082 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1083 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1084 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1085 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1086 | */ |
| 1087 | |
| 1088 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1089 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1090 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1091 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1092 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1093 | count_comp_for: |
| 1094 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1095 | REQ(n, comp_for); |
| 1096 | if (NCH(n) == 5) |
| 1097 | n = CHILD(n, 4); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1098 | else |
| 1099 | return n_fors; |
| 1100 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1101 | REQ(n, comp_iter); |
| 1102 | n = CHILD(n, 0); |
| 1103 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1104 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1105 | else if (TYPE(n) == comp_if) { |
| 1106 | if (NCH(n) == 3) { |
| 1107 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1108 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1109 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1110 | else |
| 1111 | return n_fors; |
| 1112 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1113 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1114 | /* Should never be reached */ |
| 1115 | PyErr_SetString(PyExc_SystemError, |
| 1116 | "logic error in count_comp_fors"); |
| 1117 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1120 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1121 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1122 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | */ |
| 1124 | |
| 1125 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1126 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1127 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1128 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1129 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1130 | while (1) { |
| 1131 | REQ(n, comp_iter); |
| 1132 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1133 | return n_ifs; |
| 1134 | n = CHILD(n, 0); |
| 1135 | REQ(n, comp_if); |
| 1136 | n_ifs++; |
| 1137 | if (NCH(n) == 2) |
| 1138 | return n_ifs; |
| 1139 | n = CHILD(n, 2); |
| 1140 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1143 | static asdl_seq * |
| 1144 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1145 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1147 | asdl_seq *comps; |
| 1148 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1149 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1150 | if (n_fors == -1) |
| 1151 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1152 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1153 | comps = asdl_seq_new(n_fors, c->c_arena); |
| 1154 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1155 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1156 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1157 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1158 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1159 | asdl_seq *t; |
| 1160 | expr_ty expression; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1161 | node *for_ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1163 | REQ(n, comp_for); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1165 | for_ch = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1166 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1167 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1168 | return NULL; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1169 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1170 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1172 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1173 | /* Check the # of children rather than the length of t, since |
| 1174 | (x for x, in ...) has 1 element in t, but still requires a Tuple. */ |
| 1175 | if (NCH(for_ch) == 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1176 | comp = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, |
| 1177 | NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | else |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1179 | comp = comprehension(Tuple(t, Store, LINENO(n), n->n_col_offset, |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1180 | c->c_arena), |
| 1181 | expression, NULL, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1182 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1183 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1185 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1186 | if (NCH(n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1187 | int j, n_ifs; |
| 1188 | asdl_seq *ifs; |
| 1189 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1190 | n = CHILD(n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1191 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1192 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1193 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1194 | |
| 1195 | ifs = asdl_seq_new(n_ifs, c->c_arena); |
| 1196 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1197 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1198 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1199 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1200 | REQ(n, comp_iter); |
| 1201 | n = CHILD(n, 0); |
| 1202 | REQ(n, comp_if); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1203 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1204 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1205 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1206 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1207 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1208 | if (NCH(n) == 3) |
| 1209 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1210 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1211 | /* on exit, must guarantee that n is a comp_for */ |
| 1212 | if (TYPE(n) == comp_iter) |
| 1213 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1214 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1216 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1217 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1218 | return comps; |
| 1219 | } |
| 1220 | |
| 1221 | static expr_ty |
| 1222 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1223 | { |
| 1224 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) |
| 1225 | argument: [test '='] test [comp_for] # Really [keyword '='] test */ |
| 1226 | expr_ty elt; |
| 1227 | asdl_seq *comps; |
| 1228 | |
| 1229 | assert(NCH(n) > 1); |
| 1230 | |
| 1231 | elt = ast_for_expr(c, CHILD(n, 0)); |
| 1232 | if (!elt) |
| 1233 | return NULL; |
| 1234 | |
| 1235 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1236 | if (!comps) |
| 1237 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1238 | |
| 1239 | if (type == COMP_GENEXP) |
| 1240 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1241 | else if (type == COMP_LISTCOMP) |
| 1242 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1243 | else if (type == COMP_SETCOMP) |
| 1244 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1245 | else |
| 1246 | /* Should never happen */ |
| 1247 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1251 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1252 | { |
| 1253 | expr_ty key, value; |
| 1254 | asdl_seq *comps; |
| 1255 | |
| 1256 | assert(NCH(n) > 3); |
| 1257 | REQ(CHILD(n, 1), COLON); |
| 1258 | |
| 1259 | key = ast_for_expr(c, CHILD(n, 0)); |
| 1260 | if (!key) |
| 1261 | return NULL; |
| 1262 | |
| 1263 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1264 | if (!value) |
| 1265 | return NULL; |
| 1266 | |
| 1267 | comps = ast_for_comprehension(c, CHILD(n, 3)); |
| 1268 | if (!comps) |
| 1269 | return NULL; |
| 1270 | |
| 1271 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1272 | } |
| 1273 | |
| 1274 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1275 | ast_for_genexp(struct compiling *c, const node *n) |
| 1276 | { |
| 1277 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1278 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | static expr_ty |
| 1282 | ast_for_listcomp(struct compiling *c, const node *n) |
| 1283 | { |
| 1284 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1285 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static expr_ty |
| 1289 | ast_for_setcomp(struct compiling *c, const node *n) |
| 1290 | { |
| 1291 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1292 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1297 | ast_for_atom(struct compiling *c, const node *n) |
| 1298 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1299 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 1300 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1301 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1302 | */ |
| 1303 | node *ch = CHILD(n, 0); |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 1304 | int bytesmode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1305 | |
| 1306 | switch (TYPE(ch)) { |
| 1307 | case NAME: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1308 | /* All names start in Load context, but may later be |
| 1309 | changed. */ |
| 1310 | 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] | 1311 | case STRING: { |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 1312 | PyObject *str = parsestrplus(c, n, &bytesmode); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1313 | if (!str) { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 1314 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1315 | PyObject *type, *value, *tback, *errstr; |
| 1316 | PyErr_Fetch(&type, &value, &tback); |
| 1317 | errstr = ((PyUnicodeErrorObject *)value)->reason; |
| 1318 | if (errstr) { |
| 1319 | char *s = ""; |
| 1320 | char buf[128]; |
Amaury Forgeot d'Arc | 39599dc | 2007-11-22 02:48:12 +0000 | [diff] [blame] | 1321 | s = PyUnicode_AsString(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1322 | PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s); |
| 1323 | ast_error(n, buf); |
| 1324 | } else { |
| 1325 | ast_error(n, "(unicode error) unknown error"); |
| 1326 | } |
| 1327 | Py_DECREF(type); |
| 1328 | Py_DECREF(value); |
| 1329 | Py_XDECREF(tback); |
| 1330 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1331 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1332 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1333 | PyArena_AddPyObject(c->c_arena, str); |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 1334 | if (bytesmode) |
| 1335 | return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena); |
| 1336 | else |
| 1337 | return Str(str, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1338 | } |
| 1339 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1340 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1341 | if (!pynum) |
| 1342 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1343 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1344 | PyArena_AddPyObject(c->c_arena, pynum); |
| 1345 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1346 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 1347 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1348 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1349 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1350 | ch = CHILD(n, 1); |
| 1351 | |
| 1352 | if (TYPE(ch) == RPAR) |
| 1353 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 1354 | |
| 1355 | if (TYPE(ch) == yield_expr) |
| 1356 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1357 | |
| 1358 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
| 1359 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1360 | return ast_for_genexp(c, ch); |
| 1361 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1362 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1363 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1364 | ch = CHILD(n, 1); |
| 1365 | |
| 1366 | if (TYPE(ch) == RSQB) |
| 1367 | return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 1368 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1369 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1370 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 1371 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 1372 | if (!elts) |
| 1373 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1374 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1375 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 1376 | } |
| 1377 | else |
| 1378 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1379 | case LBRACE: { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1380 | /* dictorsetmaker: test ':' test (',' test ':' test)* [','] | |
| 1381 | * test (gen_for | (',' test)* [',']) */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1382 | int i, size; |
| 1383 | asdl_seq *keys, *values; |
| 1384 | |
| 1385 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1386 | if (TYPE(ch) == RBRACE) { |
| 1387 | /* it's an empty dict */ |
| 1388 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 1389 | } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 1390 | /* it's a simple set */ |
Guido van Rossum | 4d2adcc | 2007-04-17 21:58:50 +0000 | [diff] [blame] | 1391 | asdl_seq *elts; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1392 | size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */ |
Guido van Rossum | 4d2adcc | 2007-04-17 21:58:50 +0000 | [diff] [blame] | 1393 | elts = asdl_seq_new(size, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1394 | if (!elts) |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1395 | return NULL; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1396 | for (i = 0; i < NCH(ch); i += 2) { |
| 1397 | expr_ty expression; |
| 1398 | expression = ast_for_expr(c, CHILD(ch, i)); |
| 1399 | if (!expression) |
| 1400 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1401 | asdl_seq_SET(elts, i / 2, expression); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1402 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1403 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 1404 | } else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 1405 | /* it's a set comprehension */ |
| 1406 | return ast_for_setcomp(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1407 | } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) { |
| 1408 | return ast_for_dictcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1409 | } else { |
| 1410 | /* it's a dict */ |
| 1411 | size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ |
| 1412 | keys = asdl_seq_new(size, c->c_arena); |
| 1413 | if (!keys) |
| 1414 | return NULL; |
| 1415 | |
| 1416 | values = asdl_seq_new(size, c->c_arena); |
| 1417 | if (!values) |
| 1418 | return NULL; |
| 1419 | |
| 1420 | for (i = 0; i < NCH(ch); i += 4) { |
| 1421 | expr_ty expression; |
| 1422 | |
| 1423 | expression = ast_for_expr(c, CHILD(ch, i)); |
| 1424 | if (!expression) |
| 1425 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1426 | |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1427 | asdl_seq_SET(keys, i / 4, expression); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1428 | |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1429 | expression = ast_for_expr(c, CHILD(ch, i + 2)); |
| 1430 | if (!expression) |
| 1431 | return NULL; |
| 1432 | |
| 1433 | asdl_seq_SET(values, i / 4, expression); |
| 1434 | } |
| 1435 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 1436 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1438 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1439 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 1440 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | static slice_ty |
| 1445 | ast_for_slice(struct compiling *c, const node *n) |
| 1446 | { |
| 1447 | node *ch; |
| 1448 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 1449 | |
| 1450 | REQ(n, subscript); |
| 1451 | |
| 1452 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 1453 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1454 | sliceop: ':' [test] |
| 1455 | */ |
| 1456 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1457 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 1458 | /* 'step' variable hold no significance in terms of being used over |
| 1459 | other vars */ |
| 1460 | step = ast_for_expr(c, ch); |
| 1461 | if (!step) |
| 1462 | return NULL; |
| 1463 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1464 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1468 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1469 | if (!lower) |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | |
| 1473 | /* If there's an upper bound it's in the second or third position. */ |
| 1474 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1475 | if (NCH(n) > 1) { |
| 1476 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1477 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1478 | if (TYPE(n2) == test) { |
| 1479 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1480 | if (!upper) |
| 1481 | return NULL; |
| 1482 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1483 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1484 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1485 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1486 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1487 | if (TYPE(n2) == test) { |
| 1488 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1489 | if (!upper) |
| 1490 | return NULL; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | ch = CHILD(n, NCH(n) - 1); |
| 1495 | if (TYPE(ch) == sliceop) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1496 | if (NCH(ch) == 1) { |
| 1497 | /* No expression, so step is None */ |
| 1498 | ch = CHILD(ch, 0); |
| 1499 | step = Name(new_identifier("None", c->c_arena), Load, |
| 1500 | LINENO(ch), ch->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1501 | if (!step) |
| 1502 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1503 | } else { |
| 1504 | ch = CHILD(ch, 1); |
| 1505 | if (TYPE(ch) == test) { |
| 1506 | step = ast_for_expr(c, ch); |
| 1507 | if (!step) |
| 1508 | return NULL; |
| 1509 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1513 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | static expr_ty |
| 1517 | ast_for_binop(struct compiling *c, const node *n) |
| 1518 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1519 | /* Must account for a sequence of expressions. |
| 1520 | How should A op B op C by represented? |
| 1521 | BinOp(BinOp(A, op, B), op, C). |
| 1522 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1523 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1524 | int i, nops; |
| 1525 | expr_ty expr1, expr2, result; |
| 1526 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1527 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1528 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 1529 | if (!expr1) |
| 1530 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1531 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1532 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 1533 | if (!expr2) |
| 1534 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1535 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1536 | newoperator = get_operator(CHILD(n, 1)); |
| 1537 | if (!newoperator) |
| 1538 | return NULL; |
| 1539 | |
| 1540 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 1541 | c->c_arena); |
| 1542 | if (!result) |
| 1543 | return NULL; |
| 1544 | |
| 1545 | nops = (NCH(n) - 1) / 2; |
| 1546 | for (i = 1; i < nops; i++) { |
| 1547 | expr_ty tmp_result, tmp; |
| 1548 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 1549 | |
| 1550 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1551 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1552 | return NULL; |
| 1553 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1554 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 1555 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1556 | return NULL; |
| 1557 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1558 | tmp_result = BinOp(result, newoperator, tmp, |
| 1559 | LINENO(next_oper), next_oper->n_col_offset, |
| 1560 | c->c_arena); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1561 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1562 | return NULL; |
| 1563 | result = tmp_result; |
| 1564 | } |
| 1565 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1568 | static expr_ty |
| 1569 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 1570 | { |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1571 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 1572 | subscriptlist: subscript (',' subscript)* [','] |
| 1573 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 1574 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1575 | REQ(n, trailer); |
| 1576 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 1577 | if (NCH(n) == 2) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1578 | return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), |
| 1579 | n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1580 | else |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 1581 | return ast_for_call(c, CHILD(n, 1), left_expr); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1582 | } |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 1583 | else if (TYPE(CHILD(n, 0)) == DOT ) { |
| 1584 | return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1585 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 1586 | } |
| 1587 | else { |
| 1588 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1589 | REQ(CHILD(n, 2), RSQB); |
| 1590 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1591 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1592 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 1593 | if (!slc) |
| 1594 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1595 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 1596 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1597 | } |
| 1598 | else { |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1599 | /* The grammar is ambiguous here. The ambiguity is resolved |
| 1600 | by treating the sequence as a tuple literal if there are |
| 1601 | no slice features. |
| 1602 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1603 | int j; |
| 1604 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1605 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 1606 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1607 | asdl_seq *slices, *elts; |
| 1608 | slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1609 | if (!slices) |
| 1610 | return NULL; |
| 1611 | for (j = 0; j < NCH(n); j += 2) { |
| 1612 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1613 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1614 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1615 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 1616 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1617 | asdl_seq_SET(slices, j / 2, slc); |
| 1618 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1619 | if (!simple) { |
| 1620 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1621 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1622 | } |
| 1623 | /* extract Index values and put them in a Tuple */ |
| 1624 | elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1625 | if (!elts) |
| 1626 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 1627 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 1628 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 1629 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 1630 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 1631 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1632 | 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] | 1633 | if (!e) |
| 1634 | return NULL; |
| 1635 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1636 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1642 | ast_for_factor(struct compiling *c, const node *n) |
| 1643 | { |
| 1644 | node *pfactor, *ppower, *patom, *pnum; |
| 1645 | expr_ty expression; |
| 1646 | |
| 1647 | /* If the unary - operator is applied to a constant, don't generate |
| 1648 | a UNARY_NEGATIVE opcode. Just store the approriate value as a |
| 1649 | constant. The peephole optimizer already does something like |
| 1650 | this but it doesn't handle the case where the constant is |
| 1651 | (sys.maxint - 1). In that case, we want a PyIntObject, not a |
| 1652 | PyLongObject. |
| 1653 | */ |
| 1654 | if (TYPE(CHILD(n, 0)) == MINUS |
| 1655 | && NCH(n) == 2 |
| 1656 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 1657 | && NCH(pfactor) == 1 |
| 1658 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 1659 | && NCH(ppower) == 1 |
| 1660 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 1661 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER) { |
| 1662 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
| 1663 | if (s == NULL) |
| 1664 | return NULL; |
| 1665 | s[0] = '-'; |
| 1666 | strcpy(s + 1, STR(pnum)); |
| 1667 | PyObject_FREE(STR(pnum)); |
| 1668 | STR(pnum) = s; |
| 1669 | return ast_for_atom(c, patom); |
| 1670 | } |
| 1671 | |
| 1672 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 1673 | if (!expression) |
| 1674 | return NULL; |
| 1675 | |
| 1676 | switch (TYPE(CHILD(n, 0))) { |
| 1677 | case PLUS: |
| 1678 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 1679 | c->c_arena); |
| 1680 | case MINUS: |
| 1681 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 1682 | c->c_arena); |
| 1683 | case TILDE: |
| 1684 | return UnaryOp(Invert, expression, LINENO(n), |
| 1685 | n->n_col_offset, c->c_arena); |
| 1686 | } |
| 1687 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 1688 | TYPE(CHILD(n, 0))); |
| 1689 | return NULL; |
| 1690 | } |
| 1691 | |
| 1692 | static expr_ty |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1693 | ast_for_power(struct compiling *c, const node *n) |
| 1694 | { |
| 1695 | /* power: atom trailer* ('**' factor)* |
| 1696 | */ |
| 1697 | int i; |
| 1698 | expr_ty e, tmp; |
| 1699 | REQ(n, power); |
| 1700 | e = ast_for_atom(c, CHILD(n, 0)); |
| 1701 | if (!e) |
| 1702 | return NULL; |
| 1703 | if (NCH(n) == 1) |
| 1704 | return e; |
| 1705 | for (i = 1; i < NCH(n); i++) { |
| 1706 | node *ch = CHILD(n, i); |
| 1707 | if (TYPE(ch) != trailer) |
| 1708 | break; |
| 1709 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1710 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1711 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1712 | tmp->lineno = e->lineno; |
| 1713 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1714 | e = tmp; |
| 1715 | } |
| 1716 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 1717 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1718 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1719 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1720 | 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] | 1721 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1722 | return NULL; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1723 | e = tmp; |
| 1724 | } |
| 1725 | return e; |
| 1726 | } |
| 1727 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1728 | static expr_ty |
| 1729 | ast_for_starred(struct compiling *c, const node *n) |
| 1730 | { |
| 1731 | expr_ty tmp; |
| 1732 | REQ(n, star_expr); |
| 1733 | |
| 1734 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 1735 | if (!tmp) |
| 1736 | return NULL; |
| 1737 | |
| 1738 | /* The Load context is changed later. */ |
| 1739 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 1740 | } |
| 1741 | |
| 1742 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1743 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 1744 | */ |
| 1745 | |
| 1746 | static expr_ty |
| 1747 | ast_for_expr(struct compiling *c, const node *n) |
| 1748 | { |
| 1749 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1750 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1751 | test_nocond: or_test | lambdef_nocond |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1752 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1753 | and_test: not_test ('and' not_test)* |
| 1754 | not_test: 'not' not_test | comparison |
| 1755 | comparison: expr (comp_op expr)* |
| 1756 | expr: xor_expr ('|' xor_expr)* |
| 1757 | xor_expr: and_expr ('^' and_expr)* |
| 1758 | and_expr: shift_expr ('&' shift_expr)* |
| 1759 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 1760 | arith_expr: term (('+'|'-') term)* |
| 1761 | term: factor (('*'|'/'|'%'|'//') factor)* |
| 1762 | factor: ('+'|'-'|'~') factor | power |
| 1763 | power: atom trailer* ('**' factor)* |
| 1764 | */ |
| 1765 | |
| 1766 | asdl_seq *seq; |
| 1767 | int i; |
| 1768 | |
| 1769 | loop: |
| 1770 | switch (TYPE(n)) { |
| 1771 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1772 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1773 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1774 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1775 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1776 | else if (NCH(n) > 1) |
| 1777 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1778 | /* Fallthrough */ |
| 1779 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1780 | case and_test: |
| 1781 | if (NCH(n) == 1) { |
| 1782 | n = CHILD(n, 0); |
| 1783 | goto loop; |
| 1784 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1785 | seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1786 | if (!seq) |
| 1787 | return NULL; |
| 1788 | for (i = 0; i < NCH(n); i += 2) { |
| 1789 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 1790 | if (!e) |
| 1791 | return NULL; |
| 1792 | asdl_seq_SET(seq, i / 2, e); |
| 1793 | } |
| 1794 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1795 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 1796 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1797 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1798 | 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] | 1799 | case not_test: |
| 1800 | if (NCH(n) == 1) { |
| 1801 | n = CHILD(n, 0); |
| 1802 | goto loop; |
| 1803 | } |
| 1804 | else { |
| 1805 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 1806 | if (!expression) |
| 1807 | return NULL; |
| 1808 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1809 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 1810 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1811 | } |
| 1812 | case comparison: |
| 1813 | if (NCH(n) == 1) { |
| 1814 | n = CHILD(n, 0); |
| 1815 | goto loop; |
| 1816 | } |
| 1817 | else { |
| 1818 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1819 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1820 | asdl_seq *cmps; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1821 | ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1822 | if (!ops) |
| 1823 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1824 | cmps = asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1825 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1826 | return NULL; |
| 1827 | } |
| 1828 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1829 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1830 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1831 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1832 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1834 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1835 | |
| 1836 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1837 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1838 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1839 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1840 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1841 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1842 | asdl_seq_SET(cmps, i / 2, expression); |
| 1843 | } |
| 1844 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1845 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1846 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1847 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1848 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1849 | return Compare(expression, ops, cmps, LINENO(n), |
| 1850 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1851 | } |
| 1852 | break; |
| 1853 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1854 | case star_expr: |
| 1855 | if (TYPE(CHILD(n, 0)) == STAR) { |
| 1856 | return ast_for_starred(c, n); |
| 1857 | } |
| 1858 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1859 | /* The next five cases all handle BinOps. The main body of code |
| 1860 | is the same in each case, but the switch turned inside out to |
| 1861 | reuse the code for each type of operator. |
| 1862 | */ |
| 1863 | case expr: |
| 1864 | case xor_expr: |
| 1865 | case and_expr: |
| 1866 | case shift_expr: |
| 1867 | case arith_expr: |
| 1868 | case term: |
| 1869 | if (NCH(n) == 1) { |
| 1870 | n = CHILD(n, 0); |
| 1871 | goto loop; |
| 1872 | } |
| 1873 | return ast_for_binop(c, n); |
| 1874 | case yield_expr: { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1875 | expr_ty exp = NULL; |
| 1876 | if (NCH(n) == 2) { |
| 1877 | exp = ast_for_testlist(c, CHILD(n, 1)); |
| 1878 | if (!exp) |
| 1879 | return NULL; |
| 1880 | } |
| 1881 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 1882 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1883 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1884 | if (NCH(n) == 1) { |
| 1885 | n = CHILD(n, 0); |
| 1886 | goto loop; |
| 1887 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1888 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 1889 | case power: |
| 1890 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1891 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1892 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1893 | return NULL; |
| 1894 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1895 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1896 | return NULL; |
| 1897 | } |
| 1898 | |
| 1899 | static expr_ty |
| 1900 | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
| 1901 | { |
| 1902 | /* |
| 1903 | arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] |
| 1904 | | '**' test) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1905 | argument: [test '='] test [comp_for] # Really [keyword '='] test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | */ |
| 1907 | |
| 1908 | int i, nargs, nkeywords, ngens; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1909 | asdl_seq *args; |
| 1910 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1911 | expr_ty vararg = NULL, kwarg = NULL; |
| 1912 | |
| 1913 | REQ(n, arglist); |
| 1914 | |
| 1915 | nargs = 0; |
| 1916 | nkeywords = 0; |
| 1917 | ngens = 0; |
| 1918 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1919 | node *ch = CHILD(n, i); |
| 1920 | if (TYPE(ch) == argument) { |
| 1921 | if (NCH(ch) == 1) |
| 1922 | nargs++; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1923 | else if (TYPE(CHILD(ch, 1)) == comp_for) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1924 | ngens++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 | else |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1926 | nkeywords++; |
| 1927 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1928 | } |
| 1929 | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1930 | ast_error(n, "Generator expression must be parenthesized " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1931 | "if not sole argument"); |
| 1932 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | if (nargs + nkeywords + ngens > 255) { |
| 1936 | ast_error(n, "more than 255 arguments"); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1940 | args = asdl_seq_new(nargs + ngens, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1941 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1942 | return NULL; |
| 1943 | keywords = asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1944 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1945 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1946 | nargs = 0; |
| 1947 | nkeywords = 0; |
| 1948 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1949 | node *ch = CHILD(n, i); |
| 1950 | if (TYPE(ch) == argument) { |
| 1951 | expr_ty e; |
| 1952 | if (NCH(ch) == 1) { |
| 1953 | if (nkeywords) { |
| 1954 | ast_error(CHILD(ch, 0), |
| 1955 | "non-keyword arg after keyword arg"); |
| 1956 | return NULL; |
| 1957 | } |
| 1958 | e = ast_for_expr(c, CHILD(ch, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1959 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1960 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1961 | asdl_seq_SET(args, nargs++, e); |
| 1962 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1963 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1964 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1965 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1966 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1967 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1968 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1969 | else { |
| 1970 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame^] | 1971 | identifier key, tmp; |
| 1972 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1973 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1974 | /* CHILD(ch, 0) is test, but must be an identifier? */ |
| 1975 | e = ast_for_expr(c, CHILD(ch, 0)); |
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; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1978 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1979 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1980 | * SF bug 132313 points out that complaining about a keyword |
| 1981 | * then is very confusing. |
| 1982 | */ |
| 1983 | if (e->kind == Lambda_kind) { |
| 1984 | ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1985 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1986 | } else if (e->kind != Name_kind) { |
| 1987 | ast_error(CHILD(ch, 0), "keyword can't be an expression"); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1988 | return NULL; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1989 | } else if (forbidden_name(e, ch)) { |
| 1990 | return NULL; |
| 1991 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1992 | key = e->v.Name.id; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame^] | 1993 | for (k = 0; k < nkeywords; k++) { |
| 1994 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
| 1995 | if (!PyUnicode_Compare(tmp, key)) { |
| 1996 | ast_error(CHILD(ch, 0), "keyword argument repeated"); |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2000 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2001 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2002 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2003 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2004 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2005 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2006 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2007 | } |
| 2008 | } |
| 2009 | else if (TYPE(ch) == STAR) { |
| 2010 | vararg = ast_for_expr(c, CHILD(n, i+1)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2011 | if (!vararg) |
| 2012 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2013 | i++; |
| 2014 | } |
| 2015 | else if (TYPE(ch) == DOUBLESTAR) { |
| 2016 | kwarg = ast_for_expr(c, CHILD(n, i+1)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2017 | if (!kwarg) |
| 2018 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2019 | i++; |
| 2020 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2023 | 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] | 2024 | } |
| 2025 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2026 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2027 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2028 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2029 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2030 | /* testlist: test (',' test)* [','] */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2031 | /* testlist1: test (',' test)* */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2032 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2033 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2034 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2035 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2036 | } |
| 2037 | else { |
| 2038 | assert(TYPE(n) == testlist || |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2039 | TYPE(n) == testlist1); |
| 2040 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2041 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2042 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2043 | else { |
| 2044 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2045 | if (!tmp) |
| 2046 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2047 | 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] | 2048 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2051 | static stmt_ty |
| 2052 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2053 | { |
| 2054 | REQ(n, expr_stmt); |
| 2055 | /* expr_stmt: testlist (augassign (yield_expr|testlist) |
| 2056 | | ('=' (yield_expr|testlist))*) |
| 2057 | testlist: test (',' test)* [','] |
| 2058 | augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2059 | | '<<=' | '>>=' | '**=' | '//=' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2060 | test: ... here starts the operator precendence dance |
| 2061 | */ |
| 2062 | |
| 2063 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2064 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2065 | if (!e) |
| 2066 | return NULL; |
| 2067 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2068 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2069 | } |
| 2070 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2071 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2072 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2073 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2074 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2075 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2076 | if (!expr1) |
| 2077 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2078 | /* TODO(nas): Remove duplicated error checks (set_context does it) */ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2079 | switch (expr1->kind) { |
| 2080 | case GeneratorExp_kind: |
| 2081 | ast_error(ch, "augmented assignment to generator " |
| 2082 | "expression not possible"); |
| 2083 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2084 | case Yield_kind: |
| 2085 | ast_error(ch, "augmented assignment to yield " |
| 2086 | "expression not possible"); |
| 2087 | return NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2088 | case Name_kind: { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2089 | if (forbidden_name(expr1, ch)) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2090 | return NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2091 | break; |
| 2092 | } |
| 2093 | case Attribute_kind: |
| 2094 | case Subscript_kind: |
| 2095 | break; |
| 2096 | default: |
| 2097 | ast_error(ch, "illegal expression for augmented " |
| 2098 | "assignment"); |
| 2099 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2100 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2101 | if(!set_context(c, expr1, Store, ch)) |
| 2102 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2103 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2104 | ch = CHILD(n, 2); |
| 2105 | if (TYPE(ch) == testlist) |
| 2106 | expr2 = ast_for_testlist(c, ch); |
| 2107 | else |
| 2108 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2109 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2110 | return NULL; |
| 2111 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2112 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2113 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2114 | return NULL; |
| 2115 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2116 | 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] | 2117 | } |
| 2118 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2119 | int i; |
| 2120 | asdl_seq *targets; |
| 2121 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2122 | expr_ty expression; |
| 2123 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2124 | /* a normal assignment */ |
| 2125 | REQ(CHILD(n, 1), EQUAL); |
| 2126 | targets = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 2127 | if (!targets) |
| 2128 | return NULL; |
| 2129 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 2130 | expr_ty e; |
| 2131 | node *ch = CHILD(n, i); |
| 2132 | if (TYPE(ch) == yield_expr) { |
| 2133 | ast_error(ch, "assignment to yield expression not possible"); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | e = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2137 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2138 | /* set context to assign */ |
| 2139 | if (!e) |
| 2140 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2141 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2142 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2143 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2144 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2145 | asdl_seq_SET(targets, i / 2, e); |
| 2146 | } |
| 2147 | value = CHILD(n, NCH(n) - 1); |
| 2148 | if (TYPE(value) == testlist) |
| 2149 | expression = ast_for_testlist(c, value); |
| 2150 | else |
| 2151 | expression = ast_for_expr(c, value); |
| 2152 | if (!expression) |
| 2153 | return NULL; |
| 2154 | 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] | 2155 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2158 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2159 | 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] | 2160 | { |
| 2161 | asdl_seq *seq; |
| 2162 | int i; |
| 2163 | expr_ty e; |
| 2164 | |
| 2165 | REQ(n, exprlist); |
| 2166 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2167 | seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2168 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2169 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2170 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2171 | e = ast_for_expr(c, CHILD(n, i)); |
| 2172 | if (!e) |
| 2173 | return NULL; |
| 2174 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2175 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2176 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2177 | } |
| 2178 | return seq; |
| 2179 | } |
| 2180 | |
| 2181 | static stmt_ty |
| 2182 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 2183 | { |
| 2184 | asdl_seq *expr_list; |
| 2185 | |
| 2186 | /* del_stmt: 'del' exprlist */ |
| 2187 | REQ(n, del_stmt); |
| 2188 | |
| 2189 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 2190 | if (!expr_list) |
| 2191 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2192 | 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] | 2193 | } |
| 2194 | |
| 2195 | static stmt_ty |
| 2196 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 2197 | { |
| 2198 | /* |
| 2199 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 2200 | | yield_stmt |
| 2201 | break_stmt: 'break' |
| 2202 | continue_stmt: 'continue' |
| 2203 | return_stmt: 'return' [testlist] |
| 2204 | yield_stmt: yield_expr |
| 2205 | yield_expr: 'yield' testlist |
| 2206 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 2207 | */ |
| 2208 | node *ch; |
| 2209 | |
| 2210 | REQ(n, flow_stmt); |
| 2211 | ch = CHILD(n, 0); |
| 2212 | switch (TYPE(ch)) { |
| 2213 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2214 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2215 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2216 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2217 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2218 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 2219 | if (!exp) |
| 2220 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2221 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2222 | } |
| 2223 | case return_stmt: |
| 2224 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2225 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2226 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2227 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2228 | if (!expression) |
| 2229 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2230 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2231 | } |
| 2232 | case raise_stmt: |
| 2233 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2234 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 2235 | else if (NCH(ch) >= 2) { |
| 2236 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2237 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 2238 | if (!expression) |
| 2239 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2240 | if (NCH(ch) == 4) { |
| 2241 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 2242 | if (!cause) |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | 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] | 2246 | } |
| 2247 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2248 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2249 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 2250 | return NULL; |
| 2251 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2252 | |
| 2253 | PyErr_SetString(PyExc_SystemError, "unhandled flow statement"); |
| 2254 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | static alias_ty |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2258 | alias_for_import_name(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2259 | { |
| 2260 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 2261 | import_as_name: NAME ['as' NAME] |
| 2262 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2263 | dotted_name: NAME ('.' NAME)* |
| 2264 | */ |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2265 | PyObject *str; |
| 2266 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2267 | loop: |
| 2268 | switch (TYPE(n)) { |
| 2269 | case import_as_name: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2270 | str = NULL; |
| 2271 | if (NCH(n) == 3) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2272 | str = NEW_IDENTIFIER(CHILD(n, 2)); |
| 2273 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2274 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2275 | case dotted_as_name: |
| 2276 | if (NCH(n) == 1) { |
| 2277 | n = CHILD(n, 0); |
| 2278 | goto loop; |
| 2279 | } |
| 2280 | else { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2281 | alias_ty a = alias_for_import_name(c, CHILD(n, 0)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2282 | if (!a) |
| 2283 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2284 | assert(!a->asname); |
| 2285 | a->asname = NEW_IDENTIFIER(CHILD(n, 2)); |
| 2286 | return a; |
| 2287 | } |
| 2288 | break; |
| 2289 | case dotted_name: |
| 2290 | if (NCH(n) == 1) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2291 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2292 | else { |
| 2293 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 2294 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 2295 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2296 | char *s; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2297 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2298 | |
| 2299 | len = 0; |
| 2300 | for (i = 0; i < NCH(n); i += 2) |
| 2301 | /* length of string plus one for the dot */ |
| 2302 | len += strlen(STR(CHILD(n, i))) + 1; |
| 2303 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2304 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2305 | if (!str) |
| 2306 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2307 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2308 | if (!s) |
| 2309 | return NULL; |
| 2310 | for (i = 0; i < NCH(n); i += 2) { |
| 2311 | char *sch = STR(CHILD(n, i)); |
| 2312 | strcpy(s, STR(CHILD(n, i))); |
| 2313 | s += strlen(sch); |
| 2314 | *s++ = '.'; |
| 2315 | } |
| 2316 | --s; |
| 2317 | *s = '\0'; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2318 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 2319 | PyBytes_GET_SIZE(str), |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2320 | NULL); |
| 2321 | Py_DECREF(str); |
| 2322 | if (!uni) |
| 2323 | return NULL; |
| 2324 | str = uni; |
| 2325 | PyUnicode_InternInPlace(&str); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2326 | PyArena_AddPyObject(c->c_arena, str); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2327 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2328 | } |
| 2329 | break; |
| 2330 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2331 | str = PyUnicode_InternFromString("*"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2332 | PyArena_AddPyObject(c->c_arena, str); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2333 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2334 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2335 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2336 | "unexpected import name: %d", TYPE(n)); |
| 2337 | return NULL; |
| 2338 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2339 | |
| 2340 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2341 | return NULL; |
| 2342 | } |
| 2343 | |
| 2344 | static stmt_ty |
| 2345 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 2346 | { |
| 2347 | /* |
| 2348 | import_stmt: import_name | import_from |
| 2349 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 2350 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 2351 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2352 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2353 | int lineno; |
| 2354 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2355 | int i; |
| 2356 | asdl_seq *aliases; |
| 2357 | |
| 2358 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2359 | lineno = LINENO(n); |
| 2360 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2361 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 2362 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2363 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2364 | REQ(n, dotted_as_names); |
| 2365 | aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 2366 | if (!aliases) |
| 2367 | return NULL; |
| 2368 | for (i = 0; i < NCH(n); i += 2) { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2369 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2370 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2371 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2372 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2373 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2374 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2375 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 2376 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2377 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2378 | int idx, ndots = 0; |
| 2379 | alias_ty mod = NULL; |
| 2380 | identifier modname; |
| 2381 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2382 | /* Count the number of dots (for relative imports) and check for the |
| 2383 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2384 | for (idx = 1; idx < NCH(n); idx++) { |
| 2385 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
| 2386 | mod = alias_for_import_name(c, CHILD(n, idx)); |
| 2387 | idx++; |
| 2388 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 2389 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
| 2390 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
| 2391 | ndots += 3; |
| 2392 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2393 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 2394 | break; |
| 2395 | } |
| 2396 | ndots++; |
| 2397 | } |
| 2398 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2399 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 2400 | case STAR: |
| 2401 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2402 | n = CHILD(n, idx); |
| 2403 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2404 | break; |
| 2405 | case LPAR: |
| 2406 | /* from ... import (x, y, z) */ |
| 2407 | n = CHILD(n, idx + 1); |
| 2408 | n_children = NCH(n); |
| 2409 | break; |
| 2410 | case import_as_names: |
| 2411 | /* from ... import x, y, z */ |
| 2412 | n = CHILD(n, idx); |
| 2413 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 2414 | if (n_children % 2 == 0) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2415 | ast_error(n, "trailing comma not allowed without" |
| 2416 | " surrounding parentheses"); |
| 2417 | return NULL; |
| 2418 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2419 | break; |
| 2420 | default: |
| 2421 | ast_error(n, "Unexpected node-type in from-import"); |
| 2422 | return NULL; |
| 2423 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2424 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2425 | aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena); |
| 2426 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2427 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2428 | |
| 2429 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 2430 | if (TYPE(n) == STAR) { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2431 | alias_ty import_alias = alias_for_import_name(c, n); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2432 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2433 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2434 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2435 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2436 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2437 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2438 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); |
| 2439 | if (!import_alias) |
| 2440 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2441 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2442 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2443 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2444 | if (mod != NULL) |
| 2445 | modname = mod->name; |
| 2446 | else |
| 2447 | modname = new_identifier("", c->c_arena); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2448 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2449 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2450 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2451 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2452 | "unknown import statement: starts with command '%s'", |
| 2453 | STR(CHILD(n, 0))); |
| 2454 | return NULL; |
| 2455 | } |
| 2456 | |
| 2457 | static stmt_ty |
| 2458 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 2459 | { |
| 2460 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 2461 | identifier name; |
| 2462 | asdl_seq *s; |
| 2463 | int i; |
| 2464 | |
| 2465 | REQ(n, global_stmt); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2466 | s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2467 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2468 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2469 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2470 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 2471 | if (!name) |
| 2472 | return NULL; |
| 2473 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2474 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2475 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 2479 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 2480 | { |
| 2481 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 2482 | identifier name; |
| 2483 | asdl_seq *s; |
| 2484 | int i; |
| 2485 | |
| 2486 | REQ(n, nonlocal_stmt); |
| 2487 | s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 2488 | if (!s) |
| 2489 | return NULL; |
| 2490 | for (i = 1; i < NCH(n); i += 2) { |
| 2491 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 2492 | if (!name) |
| 2493 | return NULL; |
| 2494 | asdl_seq_SET(s, i / 2, name); |
| 2495 | } |
| 2496 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 2497 | } |
| 2498 | |
| 2499 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2500 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 2501 | { |
| 2502 | /* assert_stmt: 'assert' test [',' test] */ |
| 2503 | REQ(n, assert_stmt); |
| 2504 | if (NCH(n) == 2) { |
| 2505 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2506 | if (!expression) |
| 2507 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2508 | 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] | 2509 | } |
| 2510 | else if (NCH(n) == 4) { |
| 2511 | expr_ty expr1, expr2; |
| 2512 | |
| 2513 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 2514 | if (!expr1) |
| 2515 | return NULL; |
| 2516 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 2517 | if (!expr2) |
| 2518 | return NULL; |
| 2519 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2520 | 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] | 2521 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2522 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2523 | "improper number of parts to 'assert' statement: %d", |
| 2524 | NCH(n)); |
| 2525 | return NULL; |
| 2526 | } |
| 2527 | |
| 2528 | static asdl_seq * |
| 2529 | ast_for_suite(struct compiling *c, const node *n) |
| 2530 | { |
| 2531 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2532 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2533 | stmt_ty s; |
| 2534 | int i, total, num, end, pos = 0; |
| 2535 | node *ch; |
| 2536 | |
| 2537 | REQ(n, suite); |
| 2538 | |
| 2539 | total = num_stmts(n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2540 | seq = asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2541 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2542 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2543 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2544 | n = CHILD(n, 0); |
| 2545 | /* simple_stmt always ends with a NEWLINE, |
| 2546 | and may have a trailing SEMI |
| 2547 | */ |
| 2548 | end = NCH(n) - 1; |
| 2549 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 2550 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2551 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2552 | for (i = 0; i < end; i += 2) { |
| 2553 | ch = CHILD(n, i); |
| 2554 | s = ast_for_stmt(c, ch); |
| 2555 | if (!s) |
| 2556 | return NULL; |
| 2557 | asdl_seq_SET(seq, pos++, s); |
| 2558 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2559 | } |
| 2560 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2561 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 2562 | ch = CHILD(n, i); |
| 2563 | REQ(ch, stmt); |
| 2564 | num = num_stmts(ch); |
| 2565 | if (num == 1) { |
| 2566 | /* small_stmt or compound_stmt with only one child */ |
| 2567 | s = ast_for_stmt(c, ch); |
| 2568 | if (!s) |
| 2569 | return NULL; |
| 2570 | asdl_seq_SET(seq, pos++, s); |
| 2571 | } |
| 2572 | else { |
| 2573 | int j; |
| 2574 | ch = CHILD(ch, 0); |
| 2575 | REQ(ch, simple_stmt); |
| 2576 | for (j = 0; j < NCH(ch); j += 2) { |
| 2577 | /* statement terminates with a semi-colon ';' */ |
| 2578 | if (NCH(CHILD(ch, j)) == 0) { |
| 2579 | assert((j + 1) == NCH(ch)); |
| 2580 | break; |
| 2581 | } |
| 2582 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 2583 | if (!s) |
| 2584 | return NULL; |
| 2585 | asdl_seq_SET(seq, pos++, s); |
| 2586 | } |
| 2587 | } |
| 2588 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2589 | } |
| 2590 | assert(pos == seq->size); |
| 2591 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | static stmt_ty |
| 2595 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 2596 | { |
| 2597 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 2598 | ['else' ':' suite] |
| 2599 | */ |
| 2600 | char *s; |
| 2601 | |
| 2602 | REQ(n, if_stmt); |
| 2603 | |
| 2604 | if (NCH(n) == 4) { |
| 2605 | expr_ty expression; |
| 2606 | asdl_seq *suite_seq; |
| 2607 | |
| 2608 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2609 | if (!expression) |
| 2610 | return NULL; |
| 2611 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2612 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2613 | return NULL; |
| 2614 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2615 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 2616 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2617 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2618 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2619 | s = STR(CHILD(n, 4)); |
| 2620 | /* s[2], the third character in the string, will be |
| 2621 | 's' for el_s_e, or |
| 2622 | 'i' for el_i_f |
| 2623 | */ |
| 2624 | if (s[2] == 's') { |
| 2625 | expr_ty expression; |
| 2626 | asdl_seq *seq1, *seq2; |
| 2627 | |
| 2628 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2629 | if (!expression) |
| 2630 | return NULL; |
| 2631 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2632 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2633 | return NULL; |
| 2634 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2635 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2636 | return NULL; |
| 2637 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2638 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 2639 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 | } |
| 2641 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2642 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2643 | expr_ty expression; |
| 2644 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2645 | asdl_seq *orelse = NULL; |
| 2646 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | /* must reference the child n_elif+1 since 'else' token is third, |
| 2648 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2649 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 2650 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 2651 | has_else = 1; |
| 2652 | n_elif -= 3; |
| 2653 | } |
| 2654 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2655 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2656 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2657 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2658 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2659 | orelse = asdl_seq_new(1, c->c_arena); |
| 2660 | if (!orelse) |
| 2661 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2662 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2663 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2664 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2665 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 2666 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2667 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2668 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 2669 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2670 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2671 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2672 | asdl_seq_SET(orelse, 0, |
| 2673 | If(expression, suite_seq, suite_seq2, |
| 2674 | LINENO(CHILD(n, NCH(n) - 6)), |
| 2675 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 2676 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2677 | /* the just-created orelse handled the last elif */ |
| 2678 | n_elif--; |
| 2679 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2680 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2681 | for (i = 0; i < n_elif; i++) { |
| 2682 | int off = 5 + (n_elif - i - 1) * 4; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2683 | asdl_seq *newobj = asdl_seq_new(1, c->c_arena); |
| 2684 | if (!newobj) |
| 2685 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2686 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2687 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2688 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2689 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2690 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2691 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2693 | asdl_seq_SET(newobj, 0, |
| 2694 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2695 | LINENO(CHILD(n, off)), |
| 2696 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2697 | orelse = newobj; |
| 2698 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2699 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2700 | if (!expression) |
| 2701 | return NULL; |
| 2702 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 2703 | if (!suite_seq) |
| 2704 | return NULL; |
| 2705 | return If(expression, suite_seq, orelse, |
| 2706 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2707 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2708 | |
| 2709 | PyErr_Format(PyExc_SystemError, |
| 2710 | "unexpected token in 'if' statement: %s", s); |
| 2711 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | static stmt_ty |
| 2715 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 2716 | { |
| 2717 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 2718 | REQ(n, while_stmt); |
| 2719 | |
| 2720 | if (NCH(n) == 4) { |
| 2721 | expr_ty expression; |
| 2722 | asdl_seq *suite_seq; |
| 2723 | |
| 2724 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2725 | if (!expression) |
| 2726 | return NULL; |
| 2727 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2728 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2729 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2730 | 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] | 2731 | } |
| 2732 | else if (NCH(n) == 7) { |
| 2733 | expr_ty expression; |
| 2734 | asdl_seq *seq1, *seq2; |
| 2735 | |
| 2736 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2737 | if (!expression) |
| 2738 | return NULL; |
| 2739 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2740 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2741 | return NULL; |
| 2742 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2743 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2744 | return NULL; |
| 2745 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2746 | 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] | 2747 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2748 | |
| 2749 | PyErr_Format(PyExc_SystemError, |
| 2750 | "wrong number of tokens for 'while' statement: %d", |
| 2751 | NCH(n)); |
| 2752 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | static stmt_ty |
| 2756 | ast_for_for_stmt(struct compiling *c, const node *n) |
| 2757 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2758 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2759 | expr_ty expression; |
| 2760 | expr_ty target; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2761 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2762 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 2763 | REQ(n, for_stmt); |
| 2764 | |
| 2765 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2766 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2767 | if (!seq) |
| 2768 | return NULL; |
| 2769 | } |
| 2770 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2771 | node_target = CHILD(n, 1); |
| 2772 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2773 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2774 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2775 | /* Check the # of children rather than the length of _target, since |
| 2776 | for x, in ... has 1 element in _target, but still requires a Tuple. */ |
| 2777 | if (NCH(node_target) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2778 | target = (expr_ty)asdl_seq_GET(_target, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2779 | else |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2780 | 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] | 2781 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2782 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2783 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2784 | return NULL; |
| 2785 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2786 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2787 | return NULL; |
| 2788 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2789 | return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, |
| 2790 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | static excepthandler_ty |
| 2794 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 2795 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2796 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2797 | REQ(exc, except_clause); |
| 2798 | REQ(body, suite); |
| 2799 | |
| 2800 | if (NCH(exc) == 1) { |
| 2801 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 2802 | if (!suite_seq) |
| 2803 | return NULL; |
| 2804 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 2805 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2806 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2807 | } |
| 2808 | else if (NCH(exc) == 2) { |
| 2809 | expr_ty expression; |
| 2810 | asdl_seq *suite_seq; |
| 2811 | |
| 2812 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 2813 | if (!expression) |
| 2814 | return NULL; |
| 2815 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2816 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | return NULL; |
| 2818 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 2819 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2820 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2821 | } |
| 2822 | else if (NCH(exc) == 4) { |
| 2823 | asdl_seq *suite_seq; |
| 2824 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 2825 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2826 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2827 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2828 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2829 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2830 | return NULL; |
| 2831 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2832 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2833 | return NULL; |
| 2834 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 2835 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2836 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2837 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2838 | |
| 2839 | PyErr_Format(PyExc_SystemError, |
| 2840 | "wrong number of children for 'except' clause: %d", |
| 2841 | NCH(exc)); |
| 2842 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | static stmt_ty |
| 2846 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 2847 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2848 | const int nch = NCH(n); |
| 2849 | int n_except = (nch - 3)/3; |
| 2850 | asdl_seq *body, *orelse = NULL, *finally = NULL; |
| 2851 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2852 | REQ(n, try_stmt); |
| 2853 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2854 | body = ast_for_suite(c, CHILD(n, 2)); |
| 2855 | if (body == NULL) |
| 2856 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2857 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2858 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 2859 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 2860 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 2861 | /* we can assume it's an "else", |
| 2862 | because nch >= 9 for try-else-finally and |
| 2863 | it would otherwise have a type of except_clause */ |
| 2864 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 2865 | if (orelse == NULL) |
| 2866 | return NULL; |
| 2867 | n_except--; |
| 2868 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2870 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 2871 | if (finally == NULL) |
| 2872 | return NULL; |
| 2873 | n_except--; |
| 2874 | } |
| 2875 | else { |
| 2876 | /* we can assume it's an "else", |
| 2877 | otherwise it would have a type of except_clause */ |
| 2878 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 2879 | if (orelse == NULL) |
| 2880 | return NULL; |
| 2881 | n_except--; |
| 2882 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2884 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 2885 | ast_error(n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2886 | return NULL; |
| 2887 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2888 | |
| 2889 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2890 | int i; |
| 2891 | stmt_ty except_st; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2892 | /* process except statements to create a try ... except */ |
| 2893 | asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena); |
| 2894 | if (handlers == NULL) |
| 2895 | return NULL; |
| 2896 | |
| 2897 | for (i = 0; i < n_except; i++) { |
| 2898 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 2899 | CHILD(n, 5 + i * 3)); |
| 2900 | if (!e) |
| 2901 | return NULL; |
| 2902 | asdl_seq_SET(handlers, i, e); |
| 2903 | } |
| 2904 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2905 | except_st = TryExcept(body, handlers, orelse, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2906 | n->n_col_offset, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2907 | if (!finally) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2908 | return except_st; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2909 | |
| 2910 | /* if a 'finally' is present too, we nest the TryExcept within a |
| 2911 | TryFinally to emulate try ... except ... finally */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2912 | body = asdl_seq_new(1, c->c_arena); |
| 2913 | if (body == NULL) |
| 2914 | return NULL; |
| 2915 | asdl_seq_SET(body, 0, except_st); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | /* must be a try ... finally (except clauses are in body, if any exist) */ |
| 2919 | assert(finally != NULL); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2920 | 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] | 2921 | } |
| 2922 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2923 | static expr_ty |
| 2924 | ast_for_with_var(struct compiling *c, const node *n) |
| 2925 | { |
| 2926 | REQ(n, with_var); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2927 | return ast_for_expr(c, CHILD(n, 1)); |
| 2928 | } |
| 2929 | |
| 2930 | /* with_stmt: 'with' test [ with_var ] ':' suite */ |
| 2931 | static stmt_ty |
| 2932 | ast_for_with_stmt(struct compiling *c, const node *n) |
| 2933 | { |
| 2934 | expr_ty context_expr, optional_vars = NULL; |
| 2935 | int suite_index = 3; /* skip 'with', test, and ':' */ |
| 2936 | asdl_seq *suite_seq; |
| 2937 | |
| 2938 | assert(TYPE(n) == with_stmt); |
| 2939 | context_expr = ast_for_expr(c, CHILD(n, 1)); |
| 2940 | if (TYPE(CHILD(n, 2)) == with_var) { |
| 2941 | optional_vars = ast_for_with_var(c, CHILD(n, 2)); |
| 2942 | |
| 2943 | if (!optional_vars) { |
| 2944 | return NULL; |
| 2945 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2946 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2947 | return NULL; |
| 2948 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2949 | suite_index = 4; |
| 2950 | } |
| 2951 | |
| 2952 | suite_seq = ast_for_suite(c, CHILD(n, suite_index)); |
| 2953 | if (!suite_seq) { |
| 2954 | return NULL; |
| 2955 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2956 | return With(context_expr, optional_vars, suite_seq, LINENO(n), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2957 | n->n_col_offset, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2960 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2961 | 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] | 2962 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2963 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
| 2964 | asdl_seq *s; |
| 2965 | expr_ty call, dummy; |
| 2966 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2967 | REQ(n, classdef); |
| 2968 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2969 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2970 | s = ast_for_suite(c, CHILD(n, 3)); |
| 2971 | if (!s) |
| 2972 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2973 | 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] | 2974 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2975 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2976 | |
| 2977 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2978 | s = ast_for_suite(c, CHILD(n,5)); |
| 2979 | if (!s) |
| 2980 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2981 | 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] | 2982 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2985 | /* class NAME '(' arglist ')' ':' suite */ |
| 2986 | /* build up a fake Call node so we can extract its pieces */ |
| 2987 | dummy = Name(NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2988 | call = ast_for_call(c, CHILD(n, 3), dummy); |
| 2989 | if (!call) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2990 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2991 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2992 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2993 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2994 | |
| 2995 | return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), |
| 2996 | call->v.Call.args, call->v.Call.keywords, |
| 2997 | call->v.Call.starargs, call->v.Call.kwargs, s, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2998 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | static stmt_ty |
| 3002 | ast_for_stmt(struct compiling *c, const node *n) |
| 3003 | { |
| 3004 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3005 | assert(NCH(n) == 1); |
| 3006 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3007 | } |
| 3008 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3009 | assert(num_stmts(n) == 1); |
| 3010 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3011 | } |
| 3012 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3013 | REQ(n, small_stmt); |
| 3014 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3015 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3016 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3017 | */ |
| 3018 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3019 | case expr_stmt: |
| 3020 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3021 | case del_stmt: |
| 3022 | return ast_for_del_stmt(c, n); |
| 3023 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3024 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3025 | case flow_stmt: |
| 3026 | return ast_for_flow_stmt(c, n); |
| 3027 | case import_stmt: |
| 3028 | return ast_for_import_stmt(c, n); |
| 3029 | case global_stmt: |
| 3030 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3031 | case nonlocal_stmt: |
| 3032 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3033 | case assert_stmt: |
| 3034 | return ast_for_assert_stmt(c, n); |
| 3035 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3036 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3037 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3038 | TYPE(n), NCH(n)); |
| 3039 | return NULL; |
| 3040 | } |
| 3041 | } |
| 3042 | else { |
| 3043 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3044 | | funcdef | classdef | decorated |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3045 | */ |
| 3046 | node *ch = CHILD(n, 0); |
| 3047 | REQ(n, compound_stmt); |
| 3048 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3049 | case if_stmt: |
| 3050 | return ast_for_if_stmt(c, ch); |
| 3051 | case while_stmt: |
| 3052 | return ast_for_while_stmt(c, ch); |
| 3053 | case for_stmt: |
| 3054 | return ast_for_for_stmt(c, ch); |
| 3055 | case try_stmt: |
| 3056 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3057 | case with_stmt: |
| 3058 | return ast_for_with_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3059 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3060 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3061 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3062 | return ast_for_classdef(c, ch, NULL); |
| 3063 | case decorated: |
| 3064 | return ast_for_decorated(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3065 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3066 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3067 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3068 | TYPE(n), NCH(n)); |
| 3069 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3070 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3075 | parsenumber(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3076 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3077 | const char *end; |
| 3078 | long x; |
| 3079 | double dx; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3080 | #ifndef WITHOUT_COMPLEX |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3081 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3082 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3083 | #endif |
| 3084 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3085 | errno = 0; |
| 3086 | end = s + strlen(s) - 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3087 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3088 | imflag = *end == 'j' || *end == 'J'; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3089 | #endif |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3090 | if (s[0] == '0') { |
| 3091 | x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); |
| 3092 | if (x < 0 && errno == 0) { |
| 3093 | return PyLong_FromString((char *)s, |
| 3094 | (char **)0, |
| 3095 | 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3096 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3097 | } |
| 3098 | else |
| 3099 | x = PyOS_strtol((char *)s, (char **)&end, 0); |
| 3100 | if (*end == '\0') { |
| 3101 | if (errno != 0) |
| 3102 | return PyLong_FromString((char *)s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3103 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3104 | } |
| 3105 | /* XXX Huge floats may silently fail */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3106 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3107 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3108 | compl.real = 0.; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3109 | PyFPE_START_PROTECT("atof", return 0) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3110 | compl.imag = PyOS_ascii_atof(s); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3111 | PyFPE_END_PROTECT(c) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3112 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3113 | } |
| 3114 | else |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3115 | #endif |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3116 | { |
| 3117 | PyFPE_START_PROTECT("atof", return 0) |
| 3118 | dx = PyOS_ascii_atof(s); |
| 3119 | PyFPE_END_PROTECT(dx) |
| 3120 | return PyFloat_FromDouble(dx); |
| 3121 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3125 | 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] | 3126 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3127 | PyObject *u, *v; |
| 3128 | char *s, *t; |
| 3129 | t = s = (char *)*sPtr; |
| 3130 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 3131 | while (s < end && (*s & 0x80)) s++; |
| 3132 | *sPtr = s; |
| 3133 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 3134 | if (u == NULL) |
| 3135 | return NULL; |
| 3136 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 3137 | Py_DECREF(u); |
| 3138 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3142 | 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] | 3143 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3144 | PyObject *v, *u; |
| 3145 | char *buf; |
| 3146 | char *p; |
| 3147 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3148 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3149 | if (encoding == NULL) { |
| 3150 | buf = (char *)s; |
| 3151 | u = NULL; |
| 3152 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 3153 | buf = (char *)s; |
| 3154 | u = NULL; |
| 3155 | } else { |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 3156 | /* check for integer overflow */ |
| 3157 | if (len > PY_SIZE_MAX / 4) |
| 3158 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3159 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3160 | u = PyBytes_FromStringAndSize((char *)NULL, len * 4); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3161 | if (u == NULL) |
| 3162 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3163 | p = buf = PyBytes_AsString(u); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3164 | end = s + len; |
| 3165 | while (s < end) { |
| 3166 | if (*s == '\\') { |
| 3167 | *p++ = *s++; |
| 3168 | if (*s & 0x80) { |
| 3169 | strcpy(p, "u005c"); |
| 3170 | p += 5; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3171 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3172 | } |
| 3173 | if (*s & 0x80) { /* XXX inefficient */ |
| 3174 | PyObject *w; |
| 3175 | char *r; |
| 3176 | Py_ssize_t rn, i; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3177 | w = decode_utf8(c, &s, end, "utf-16-be"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3178 | if (w == NULL) { |
| 3179 | Py_DECREF(u); |
| 3180 | return NULL; |
| 3181 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3182 | r = PyBytes_AS_STRING(w); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3183 | rn = Py_SIZE(w); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3184 | assert(rn % 2 == 0); |
| 3185 | for (i = 0; i < rn; i += 2) { |
| 3186 | sprintf(p, "\\u%02x%02x", |
| 3187 | r[i + 0] & 0xFF, |
| 3188 | r[i + 1] & 0xFF); |
| 3189 | p += 6; |
| 3190 | } |
| 3191 | Py_DECREF(w); |
| 3192 | } else { |
| 3193 | *p++ = *s++; |
| 3194 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3195 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3196 | len = p - buf; |
| 3197 | s = buf; |
| 3198 | } |
| 3199 | if (rawmode) |
| 3200 | v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); |
| 3201 | else |
| 3202 | v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
| 3203 | Py_XDECREF(u); |
| 3204 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | /* s is a Python string literal, including the bracketing quote characters, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3208 | * 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] | 3209 | * parsestr parses it, and returns the decoded Python string object. |
| 3210 | */ |
| 3211 | static PyObject * |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3212 | parsestr(struct compiling *c, const node *n, int *bytesmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3213 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3214 | size_t len; |
| 3215 | const char *s = STR(n); |
| 3216 | int quote = Py_CHARMASK(*s); |
| 3217 | int rawmode = 0; |
| 3218 | int need_encoding; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3219 | if (isalpha(quote)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3220 | if (quote == 'b' || quote == 'B') { |
| 3221 | quote = *++s; |
| 3222 | *bytesmode = 1; |
| 3223 | } |
| 3224 | if (quote == 'r' || quote == 'R') { |
| 3225 | quote = *++s; |
| 3226 | rawmode = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3227 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3228 | } |
| 3229 | if (quote != '\'' && quote != '\"') { |
| 3230 | PyErr_BadInternalCall(); |
| 3231 | return NULL; |
| 3232 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3233 | s++; |
| 3234 | len = strlen(s); |
| 3235 | if (len > INT_MAX) { |
| 3236 | PyErr_SetString(PyExc_OverflowError, |
| 3237 | "string to parse is too long"); |
| 3238 | return NULL; |
| 3239 | } |
| 3240 | if (s[--len] != quote) { |
| 3241 | PyErr_BadInternalCall(); |
| 3242 | return NULL; |
| 3243 | } |
| 3244 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 3245 | s += 2; |
| 3246 | len -= 2; |
| 3247 | if (s[--len] != quote || s[--len] != quote) { |
| 3248 | PyErr_BadInternalCall(); |
| 3249 | return NULL; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 3250 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3251 | } |
Benjamin Peterson | 8dbca06 | 2008-04-05 14:49:54 +0000 | [diff] [blame] | 3252 | if (!*bytesmode && !rawmode) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3253 | return decode_unicode(c, s, len, rawmode, c->c_encoding); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3254 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3255 | if (*bytesmode) { |
| 3256 | /* Disallow non-ascii characters (but not escapes) */ |
| 3257 | const char *c; |
| 3258 | for (c = s; *c; c++) { |
| 3259 | if (Py_CHARMASK(*c) >= 0x80) { |
| 3260 | ast_error(n, "bytes can only contain ASCII " |
| 3261 | "literal characters."); |
| 3262 | return NULL; |
| 3263 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 3264 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3265 | } |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3266 | need_encoding = (!*bytesmode && c->c_encoding != NULL && |
| 3267 | strcmp(c->c_encoding, "utf-8") != 0 && |
| 3268 | strcmp(c->c_encoding, "iso-8859-1") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3269 | if (rawmode || strchr(s, '\\') == NULL) { |
| 3270 | if (need_encoding) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3271 | PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL); |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3272 | if (u == NULL || !*bytesmode) |
| 3273 | return u; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3274 | v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3275 | Py_DECREF(u); |
| 3276 | return v; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3277 | } else if (*bytesmode) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3278 | return PyBytes_FromStringAndSize(s, len); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3279 | } else if (strcmp(c->c_encoding, "utf-8") == 0) { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3280 | return PyUnicode_FromStringAndSize(s, len); |
| 3281 | } else { |
| 3282 | return PyUnicode_DecodeLatin1(s, len, NULL); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3283 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3284 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3285 | return PyBytes_DecodeEscape(s, len, NULL, 1, |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3286 | need_encoding ? c->c_encoding : NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3289 | /* 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] | 3290 | * compile-time literal catenation, calling parsestr() on each piece, and |
| 3291 | * pasting the intermediate results together. |
| 3292 | */ |
| 3293 | static PyObject * |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 3294 | parsestrplus(struct compiling *c, const node *n, int *bytesmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3295 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3296 | PyObject *v; |
| 3297 | int i; |
| 3298 | REQ(CHILD(n, 0), STRING); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3299 | v = parsestr(c, CHILD(n, 0), bytesmode); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3300 | if (v != NULL) { |
| 3301 | /* String literal concatenation */ |
| 3302 | for (i = 1; i < NCH(n); i++) { |
| 3303 | PyObject *s; |
| 3304 | int subbm = 0; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3305 | s = parsestr(c, CHILD(n, i), &subbm); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3306 | if (s == NULL) |
| 3307 | goto onError; |
| 3308 | if (*bytesmode != subbm) { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3309 | ast_error(n, "cannot mix bytes and nonbytes literals"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3310 | goto onError; |
| 3311 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3312 | if (PyBytes_Check(v) && PyBytes_Check(s)) { |
| 3313 | PyBytes_ConcatAndDel(&v, s); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3314 | if (v == NULL) |
| 3315 | goto onError; |
| 3316 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3317 | else { |
| 3318 | PyObject *temp = PyUnicode_Concat(v, s); |
| 3319 | Py_DECREF(s); |
| 3320 | Py_DECREF(v); |
| 3321 | v = temp; |
| 3322 | if (v == NULL) |
| 3323 | goto onError; |
| 3324 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3325 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3326 | } |
| 3327 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3328 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3329 | onError: |
| 3330 | Py_XDECREF(v); |
| 3331 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | } |