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; |
| 1971 | identifier key; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1972 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1973 | /* CHILD(ch, 0) is test, but must be an identifier? */ |
| 1974 | e = ast_for_expr(c, CHILD(ch, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1975 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1976 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1977 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1978 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1979 | * SF bug 132313 points out that complaining about a keyword |
| 1980 | * then is very confusing. |
| 1981 | */ |
| 1982 | if (e->kind == Lambda_kind) { |
| 1983 | ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1984 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1985 | } else if (e->kind != Name_kind) { |
| 1986 | ast_error(CHILD(ch, 0), "keyword can't be an expression"); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1987 | return NULL; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1988 | } else if (forbidden_name(e, ch)) { |
| 1989 | return NULL; |
| 1990 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1991 | key = e->v.Name.id; |
| 1992 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1993 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1994 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1995 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1996 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1997 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1998 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 1999 | } |
| 2000 | } |
| 2001 | else if (TYPE(ch) == STAR) { |
| 2002 | vararg = ast_for_expr(c, CHILD(n, i+1)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2003 | if (!vararg) |
| 2004 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2005 | i++; |
| 2006 | } |
| 2007 | else if (TYPE(ch) == DOUBLESTAR) { |
| 2008 | kwarg = ast_for_expr(c, CHILD(n, i+1)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2009 | if (!kwarg) |
| 2010 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2011 | i++; |
| 2012 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2015 | 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] | 2016 | } |
| 2017 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2018 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2019 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2020 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2021 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2022 | /* testlist: test (',' test)* [','] */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2023 | /* testlist1: test (',' test)* */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2024 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2025 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2026 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2027 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2028 | } |
| 2029 | else { |
| 2030 | assert(TYPE(n) == testlist || |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2031 | TYPE(n) == testlist1); |
| 2032 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2033 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2034 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2035 | else { |
| 2036 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2037 | if (!tmp) |
| 2038 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2039 | 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] | 2040 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2043 | static stmt_ty |
| 2044 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2045 | { |
| 2046 | REQ(n, expr_stmt); |
| 2047 | /* expr_stmt: testlist (augassign (yield_expr|testlist) |
| 2048 | | ('=' (yield_expr|testlist))*) |
| 2049 | testlist: test (',' test)* [','] |
| 2050 | augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2051 | | '<<=' | '>>=' | '**=' | '//=' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2052 | test: ... here starts the operator precendence dance |
| 2053 | */ |
| 2054 | |
| 2055 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2056 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2057 | if (!e) |
| 2058 | return NULL; |
| 2059 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2060 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2061 | } |
| 2062 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2063 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2064 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2065 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2066 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2067 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2068 | if (!expr1) |
| 2069 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2070 | /* TODO(nas): Remove duplicated error checks (set_context does it) */ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2071 | switch (expr1->kind) { |
| 2072 | case GeneratorExp_kind: |
| 2073 | ast_error(ch, "augmented assignment to generator " |
| 2074 | "expression not possible"); |
| 2075 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2076 | case Yield_kind: |
| 2077 | ast_error(ch, "augmented assignment to yield " |
| 2078 | "expression not possible"); |
| 2079 | return NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2080 | case Name_kind: { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2081 | if (forbidden_name(expr1, ch)) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2082 | return NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 2083 | break; |
| 2084 | } |
| 2085 | case Attribute_kind: |
| 2086 | case Subscript_kind: |
| 2087 | break; |
| 2088 | default: |
| 2089 | ast_error(ch, "illegal expression for augmented " |
| 2090 | "assignment"); |
| 2091 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2092 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2093 | if(!set_context(c, expr1, Store, ch)) |
| 2094 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2095 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2096 | ch = CHILD(n, 2); |
| 2097 | if (TYPE(ch) == testlist) |
| 2098 | expr2 = ast_for_testlist(c, ch); |
| 2099 | else |
| 2100 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2101 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2102 | return NULL; |
| 2103 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2104 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2105 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2106 | return NULL; |
| 2107 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2108 | 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] | 2109 | } |
| 2110 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2111 | int i; |
| 2112 | asdl_seq *targets; |
| 2113 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2114 | expr_ty expression; |
| 2115 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2116 | /* a normal assignment */ |
| 2117 | REQ(CHILD(n, 1), EQUAL); |
| 2118 | targets = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 2119 | if (!targets) |
| 2120 | return NULL; |
| 2121 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 2122 | expr_ty e; |
| 2123 | node *ch = CHILD(n, i); |
| 2124 | if (TYPE(ch) == yield_expr) { |
| 2125 | ast_error(ch, "assignment to yield expression not possible"); |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | e = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2129 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2130 | /* set context to assign */ |
| 2131 | if (!e) |
| 2132 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2133 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2134 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2135 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2136 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2137 | asdl_seq_SET(targets, i / 2, e); |
| 2138 | } |
| 2139 | value = CHILD(n, NCH(n) - 1); |
| 2140 | if (TYPE(value) == testlist) |
| 2141 | expression = ast_for_testlist(c, value); |
| 2142 | else |
| 2143 | expression = ast_for_expr(c, value); |
| 2144 | if (!expression) |
| 2145 | return NULL; |
| 2146 | 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] | 2147 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2150 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2151 | 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] | 2152 | { |
| 2153 | asdl_seq *seq; |
| 2154 | int i; |
| 2155 | expr_ty e; |
| 2156 | |
| 2157 | REQ(n, exprlist); |
| 2158 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2159 | seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2160 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2161 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2162 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2163 | e = ast_for_expr(c, CHILD(n, i)); |
| 2164 | if (!e) |
| 2165 | return NULL; |
| 2166 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2167 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2168 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2169 | } |
| 2170 | return seq; |
| 2171 | } |
| 2172 | |
| 2173 | static stmt_ty |
| 2174 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 2175 | { |
| 2176 | asdl_seq *expr_list; |
| 2177 | |
| 2178 | /* del_stmt: 'del' exprlist */ |
| 2179 | REQ(n, del_stmt); |
| 2180 | |
| 2181 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 2182 | if (!expr_list) |
| 2183 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2184 | 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] | 2185 | } |
| 2186 | |
| 2187 | static stmt_ty |
| 2188 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 2189 | { |
| 2190 | /* |
| 2191 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 2192 | | yield_stmt |
| 2193 | break_stmt: 'break' |
| 2194 | continue_stmt: 'continue' |
| 2195 | return_stmt: 'return' [testlist] |
| 2196 | yield_stmt: yield_expr |
| 2197 | yield_expr: 'yield' testlist |
| 2198 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 2199 | */ |
| 2200 | node *ch; |
| 2201 | |
| 2202 | REQ(n, flow_stmt); |
| 2203 | ch = CHILD(n, 0); |
| 2204 | switch (TYPE(ch)) { |
| 2205 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2206 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2207 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2208 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2209 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2210 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 2211 | if (!exp) |
| 2212 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2213 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2214 | } |
| 2215 | case return_stmt: |
| 2216 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2217 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2218 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2219 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2220 | if (!expression) |
| 2221 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2222 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2223 | } |
| 2224 | case raise_stmt: |
| 2225 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2226 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 2227 | else if (NCH(ch) >= 2) { |
| 2228 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2229 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 2230 | if (!expression) |
| 2231 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2232 | if (NCH(ch) == 4) { |
| 2233 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 2234 | if (!cause) |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | 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] | 2238 | } |
| 2239 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2240 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2241 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 2242 | return NULL; |
| 2243 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2244 | |
| 2245 | PyErr_SetString(PyExc_SystemError, "unhandled flow statement"); |
| 2246 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | static alias_ty |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2250 | alias_for_import_name(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2251 | { |
| 2252 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 2253 | import_as_name: NAME ['as' NAME] |
| 2254 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2255 | dotted_name: NAME ('.' NAME)* |
| 2256 | */ |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2257 | PyObject *str; |
| 2258 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2259 | loop: |
| 2260 | switch (TYPE(n)) { |
| 2261 | case import_as_name: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2262 | str = NULL; |
| 2263 | if (NCH(n) == 3) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2264 | str = NEW_IDENTIFIER(CHILD(n, 2)); |
| 2265 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2266 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2267 | case dotted_as_name: |
| 2268 | if (NCH(n) == 1) { |
| 2269 | n = CHILD(n, 0); |
| 2270 | goto loop; |
| 2271 | } |
| 2272 | else { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2273 | alias_ty a = alias_for_import_name(c, CHILD(n, 0)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2274 | if (!a) |
| 2275 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2276 | assert(!a->asname); |
| 2277 | a->asname = NEW_IDENTIFIER(CHILD(n, 2)); |
| 2278 | return a; |
| 2279 | } |
| 2280 | break; |
| 2281 | case dotted_name: |
| 2282 | if (NCH(n) == 1) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2283 | return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2284 | else { |
| 2285 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 2286 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 2287 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2288 | char *s; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2289 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2290 | |
| 2291 | len = 0; |
| 2292 | for (i = 0; i < NCH(n); i += 2) |
| 2293 | /* length of string plus one for the dot */ |
| 2294 | len += strlen(STR(CHILD(n, i))) + 1; |
| 2295 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2296 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2297 | if (!str) |
| 2298 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2299 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | if (!s) |
| 2301 | return NULL; |
| 2302 | for (i = 0; i < NCH(n); i += 2) { |
| 2303 | char *sch = STR(CHILD(n, i)); |
| 2304 | strcpy(s, STR(CHILD(n, i))); |
| 2305 | s += strlen(sch); |
| 2306 | *s++ = '.'; |
| 2307 | } |
| 2308 | --s; |
| 2309 | *s = '\0'; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2310 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 2311 | PyBytes_GET_SIZE(str), |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2312 | NULL); |
| 2313 | Py_DECREF(str); |
| 2314 | if (!uni) |
| 2315 | return NULL; |
| 2316 | str = uni; |
| 2317 | PyUnicode_InternInPlace(&str); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2318 | PyArena_AddPyObject(c->c_arena, str); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2319 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2320 | } |
| 2321 | break; |
| 2322 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2323 | str = PyUnicode_InternFromString("*"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2324 | PyArena_AddPyObject(c->c_arena, str); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2325 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2326 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2327 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2328 | "unexpected import name: %d", TYPE(n)); |
| 2329 | return NULL; |
| 2330 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2331 | |
| 2332 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2333 | return NULL; |
| 2334 | } |
| 2335 | |
| 2336 | static stmt_ty |
| 2337 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 2338 | { |
| 2339 | /* |
| 2340 | import_stmt: import_name | import_from |
| 2341 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 2342 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 2343 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2344 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2345 | int lineno; |
| 2346 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2347 | int i; |
| 2348 | asdl_seq *aliases; |
| 2349 | |
| 2350 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2351 | lineno = LINENO(n); |
| 2352 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2353 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 2354 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2355 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2356 | REQ(n, dotted_as_names); |
| 2357 | aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 2358 | if (!aliases) |
| 2359 | return NULL; |
| 2360 | for (i = 0; i < NCH(n); i += 2) { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2361 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2362 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2363 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2364 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2365 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2366 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2367 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 2368 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2369 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2370 | int idx, ndots = 0; |
| 2371 | alias_ty mod = NULL; |
| 2372 | identifier modname; |
| 2373 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2374 | /* Count the number of dots (for relative imports) and check for the |
| 2375 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2376 | for (idx = 1; idx < NCH(n); idx++) { |
| 2377 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
| 2378 | mod = alias_for_import_name(c, CHILD(n, idx)); |
| 2379 | idx++; |
| 2380 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 2381 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
| 2382 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
| 2383 | ndots += 3; |
| 2384 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2385 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 2386 | break; |
| 2387 | } |
| 2388 | ndots++; |
| 2389 | } |
| 2390 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2391 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 2392 | case STAR: |
| 2393 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2394 | n = CHILD(n, idx); |
| 2395 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2396 | break; |
| 2397 | case LPAR: |
| 2398 | /* from ... import (x, y, z) */ |
| 2399 | n = CHILD(n, idx + 1); |
| 2400 | n_children = NCH(n); |
| 2401 | break; |
| 2402 | case import_as_names: |
| 2403 | /* from ... import x, y, z */ |
| 2404 | n = CHILD(n, idx); |
| 2405 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 2406 | if (n_children % 2 == 0) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2407 | ast_error(n, "trailing comma not allowed without" |
| 2408 | " surrounding parentheses"); |
| 2409 | return NULL; |
| 2410 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | default: |
| 2413 | ast_error(n, "Unexpected node-type in from-import"); |
| 2414 | return NULL; |
| 2415 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2416 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2417 | aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena); |
| 2418 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2419 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2420 | |
| 2421 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 2422 | if (TYPE(n) == STAR) { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2423 | alias_ty import_alias = alias_for_import_name(c, n); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2424 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2425 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2426 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2427 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2428 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2429 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2430 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); |
| 2431 | if (!import_alias) |
| 2432 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2433 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2434 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2435 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2436 | if (mod != NULL) |
| 2437 | modname = mod->name; |
| 2438 | else |
| 2439 | modname = new_identifier("", c->c_arena); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2440 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2441 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2442 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2443 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2444 | "unknown import statement: starts with command '%s'", |
| 2445 | STR(CHILD(n, 0))); |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | |
| 2449 | static stmt_ty |
| 2450 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 2451 | { |
| 2452 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 2453 | identifier name; |
| 2454 | asdl_seq *s; |
| 2455 | int i; |
| 2456 | |
| 2457 | REQ(n, global_stmt); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2458 | s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2459 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2460 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2461 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2462 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 2463 | if (!name) |
| 2464 | return NULL; |
| 2465 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2466 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2467 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 2471 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 2472 | { |
| 2473 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 2474 | identifier name; |
| 2475 | asdl_seq *s; |
| 2476 | int i; |
| 2477 | |
| 2478 | REQ(n, nonlocal_stmt); |
| 2479 | s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 2480 | if (!s) |
| 2481 | return NULL; |
| 2482 | for (i = 1; i < NCH(n); i += 2) { |
| 2483 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 2484 | if (!name) |
| 2485 | return NULL; |
| 2486 | asdl_seq_SET(s, i / 2, name); |
| 2487 | } |
| 2488 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 2489 | } |
| 2490 | |
| 2491 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2492 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 2493 | { |
| 2494 | /* assert_stmt: 'assert' test [',' test] */ |
| 2495 | REQ(n, assert_stmt); |
| 2496 | if (NCH(n) == 2) { |
| 2497 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2498 | if (!expression) |
| 2499 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2500 | 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] | 2501 | } |
| 2502 | else if (NCH(n) == 4) { |
| 2503 | expr_ty expr1, expr2; |
| 2504 | |
| 2505 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 2506 | if (!expr1) |
| 2507 | return NULL; |
| 2508 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 2509 | if (!expr2) |
| 2510 | return NULL; |
| 2511 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2512 | 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] | 2513 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2514 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2515 | "improper number of parts to 'assert' statement: %d", |
| 2516 | NCH(n)); |
| 2517 | return NULL; |
| 2518 | } |
| 2519 | |
| 2520 | static asdl_seq * |
| 2521 | ast_for_suite(struct compiling *c, const node *n) |
| 2522 | { |
| 2523 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2524 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2525 | stmt_ty s; |
| 2526 | int i, total, num, end, pos = 0; |
| 2527 | node *ch; |
| 2528 | |
| 2529 | REQ(n, suite); |
| 2530 | |
| 2531 | total = num_stmts(n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2532 | seq = asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2533 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2534 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2535 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2536 | n = CHILD(n, 0); |
| 2537 | /* simple_stmt always ends with a NEWLINE, |
| 2538 | and may have a trailing SEMI |
| 2539 | */ |
| 2540 | end = NCH(n) - 1; |
| 2541 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 2542 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2543 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2544 | for (i = 0; i < end; i += 2) { |
| 2545 | ch = CHILD(n, i); |
| 2546 | s = ast_for_stmt(c, ch); |
| 2547 | if (!s) |
| 2548 | return NULL; |
| 2549 | asdl_seq_SET(seq, pos++, s); |
| 2550 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2551 | } |
| 2552 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2553 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 2554 | ch = CHILD(n, i); |
| 2555 | REQ(ch, stmt); |
| 2556 | num = num_stmts(ch); |
| 2557 | if (num == 1) { |
| 2558 | /* small_stmt or compound_stmt with only one child */ |
| 2559 | s = ast_for_stmt(c, ch); |
| 2560 | if (!s) |
| 2561 | return NULL; |
| 2562 | asdl_seq_SET(seq, pos++, s); |
| 2563 | } |
| 2564 | else { |
| 2565 | int j; |
| 2566 | ch = CHILD(ch, 0); |
| 2567 | REQ(ch, simple_stmt); |
| 2568 | for (j = 0; j < NCH(ch); j += 2) { |
| 2569 | /* statement terminates with a semi-colon ';' */ |
| 2570 | if (NCH(CHILD(ch, j)) == 0) { |
| 2571 | assert((j + 1) == NCH(ch)); |
| 2572 | break; |
| 2573 | } |
| 2574 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 2575 | if (!s) |
| 2576 | return NULL; |
| 2577 | asdl_seq_SET(seq, pos++, s); |
| 2578 | } |
| 2579 | } |
| 2580 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2581 | } |
| 2582 | assert(pos == seq->size); |
| 2583 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | static stmt_ty |
| 2587 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 2588 | { |
| 2589 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 2590 | ['else' ':' suite] |
| 2591 | */ |
| 2592 | char *s; |
| 2593 | |
| 2594 | REQ(n, if_stmt); |
| 2595 | |
| 2596 | if (NCH(n) == 4) { |
| 2597 | expr_ty expression; |
| 2598 | asdl_seq *suite_seq; |
| 2599 | |
| 2600 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2601 | if (!expression) |
| 2602 | return NULL; |
| 2603 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2604 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2605 | return NULL; |
| 2606 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2607 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 2608 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2609 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2610 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2611 | s = STR(CHILD(n, 4)); |
| 2612 | /* s[2], the third character in the string, will be |
| 2613 | 's' for el_s_e, or |
| 2614 | 'i' for el_i_f |
| 2615 | */ |
| 2616 | if (s[2] == 's') { |
| 2617 | expr_ty expression; |
| 2618 | asdl_seq *seq1, *seq2; |
| 2619 | |
| 2620 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2621 | if (!expression) |
| 2622 | return NULL; |
| 2623 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2624 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2625 | return NULL; |
| 2626 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2627 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2628 | return NULL; |
| 2629 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2630 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 2631 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2632 | } |
| 2633 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2634 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2635 | expr_ty expression; |
| 2636 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2637 | asdl_seq *orelse = NULL; |
| 2638 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2639 | /* must reference the child n_elif+1 since 'else' token is third, |
| 2640 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2641 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 2642 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 2643 | has_else = 1; |
| 2644 | n_elif -= 3; |
| 2645 | } |
| 2646 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2648 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2649 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2650 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2651 | orelse = asdl_seq_new(1, c->c_arena); |
| 2652 | if (!orelse) |
| 2653 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2655 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2656 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2657 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 2658 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2659 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2660 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 2661 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2662 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2663 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2664 | asdl_seq_SET(orelse, 0, |
| 2665 | If(expression, suite_seq, suite_seq2, |
| 2666 | LINENO(CHILD(n, NCH(n) - 6)), |
| 2667 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 2668 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2669 | /* the just-created orelse handled the last elif */ |
| 2670 | n_elif--; |
| 2671 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2672 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2673 | for (i = 0; i < n_elif; i++) { |
| 2674 | int off = 5 + (n_elif - i - 1) * 4; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2675 | asdl_seq *newobj = asdl_seq_new(1, c->c_arena); |
| 2676 | if (!newobj) |
| 2677 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2678 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2679 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2680 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2681 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2682 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2683 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2684 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2685 | asdl_seq_SET(newobj, 0, |
| 2686 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2687 | LINENO(CHILD(n, off)), |
| 2688 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2689 | orelse = newobj; |
| 2690 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2691 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2692 | if (!expression) |
| 2693 | return NULL; |
| 2694 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 2695 | if (!suite_seq) |
| 2696 | return NULL; |
| 2697 | return If(expression, suite_seq, orelse, |
| 2698 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2700 | |
| 2701 | PyErr_Format(PyExc_SystemError, |
| 2702 | "unexpected token in 'if' statement: %s", s); |
| 2703 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | static stmt_ty |
| 2707 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 2708 | { |
| 2709 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 2710 | REQ(n, while_stmt); |
| 2711 | |
| 2712 | if (NCH(n) == 4) { |
| 2713 | expr_ty expression; |
| 2714 | asdl_seq *suite_seq; |
| 2715 | |
| 2716 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2717 | if (!expression) |
| 2718 | return NULL; |
| 2719 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2720 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2721 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2722 | 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] | 2723 | } |
| 2724 | else if (NCH(n) == 7) { |
| 2725 | expr_ty expression; |
| 2726 | asdl_seq *seq1, *seq2; |
| 2727 | |
| 2728 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2729 | if (!expression) |
| 2730 | return NULL; |
| 2731 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2732 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2733 | return NULL; |
| 2734 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2735 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2736 | return NULL; |
| 2737 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2738 | 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] | 2739 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2740 | |
| 2741 | PyErr_Format(PyExc_SystemError, |
| 2742 | "wrong number of tokens for 'while' statement: %d", |
| 2743 | NCH(n)); |
| 2744 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | static stmt_ty |
| 2748 | ast_for_for_stmt(struct compiling *c, const node *n) |
| 2749 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2750 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2751 | expr_ty expression; |
| 2752 | expr_ty target; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2753 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2754 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 2755 | REQ(n, for_stmt); |
| 2756 | |
| 2757 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2758 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2759 | if (!seq) |
| 2760 | return NULL; |
| 2761 | } |
| 2762 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2763 | node_target = CHILD(n, 1); |
| 2764 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2765 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2766 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2767 | /* Check the # of children rather than the length of _target, since |
| 2768 | for x, in ... has 1 element in _target, but still requires a Tuple. */ |
| 2769 | if (NCH(node_target) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2770 | target = (expr_ty)asdl_seq_GET(_target, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2771 | else |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2772 | 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] | 2773 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2774 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2775 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2776 | return NULL; |
| 2777 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2778 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2779 | return NULL; |
| 2780 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2781 | return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, |
| 2782 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | static excepthandler_ty |
| 2786 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 2787 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2788 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2789 | REQ(exc, except_clause); |
| 2790 | REQ(body, suite); |
| 2791 | |
| 2792 | if (NCH(exc) == 1) { |
| 2793 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 2794 | if (!suite_seq) |
| 2795 | return NULL; |
| 2796 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 2797 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2798 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2799 | } |
| 2800 | else if (NCH(exc) == 2) { |
| 2801 | expr_ty expression; |
| 2802 | asdl_seq *suite_seq; |
| 2803 | |
| 2804 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 2805 | if (!expression) |
| 2806 | return NULL; |
| 2807 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2808 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2809 | return NULL; |
| 2810 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 2811 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2812 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2813 | } |
| 2814 | else if (NCH(exc) == 4) { |
| 2815 | asdl_seq *suite_seq; |
| 2816 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 2817 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2818 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2819 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2820 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2821 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2822 | return NULL; |
| 2823 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2824 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | return NULL; |
| 2826 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 2827 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2828 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2829 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2830 | |
| 2831 | PyErr_Format(PyExc_SystemError, |
| 2832 | "wrong number of children for 'except' clause: %d", |
| 2833 | NCH(exc)); |
| 2834 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | static stmt_ty |
| 2838 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 2839 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2840 | const int nch = NCH(n); |
| 2841 | int n_except = (nch - 3)/3; |
| 2842 | asdl_seq *body, *orelse = NULL, *finally = NULL; |
| 2843 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2844 | REQ(n, try_stmt); |
| 2845 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2846 | body = ast_for_suite(c, CHILD(n, 2)); |
| 2847 | if (body == NULL) |
| 2848 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2849 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2850 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 2851 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 2852 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 2853 | /* we can assume it's an "else", |
| 2854 | because nch >= 9 for try-else-finally and |
| 2855 | it would otherwise have a type of except_clause */ |
| 2856 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 2857 | if (orelse == NULL) |
| 2858 | return NULL; |
| 2859 | n_except--; |
| 2860 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2861 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2862 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 2863 | if (finally == NULL) |
| 2864 | return NULL; |
| 2865 | n_except--; |
| 2866 | } |
| 2867 | else { |
| 2868 | /* we can assume it's an "else", |
| 2869 | otherwise it would have a type of except_clause */ |
| 2870 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 2871 | if (orelse == NULL) |
| 2872 | return NULL; |
| 2873 | n_except--; |
| 2874 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2876 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Neal Norwitz | 7b3d5e1 | 2005-11-13 21:17:28 +0000 | [diff] [blame] | 2877 | ast_error(n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2878 | return NULL; |
| 2879 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2880 | |
| 2881 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2882 | int i; |
| 2883 | stmt_ty except_st; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2884 | /* process except statements to create a try ... except */ |
| 2885 | asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena); |
| 2886 | if (handlers == NULL) |
| 2887 | return NULL; |
| 2888 | |
| 2889 | for (i = 0; i < n_except; i++) { |
| 2890 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 2891 | CHILD(n, 5 + i * 3)); |
| 2892 | if (!e) |
| 2893 | return NULL; |
| 2894 | asdl_seq_SET(handlers, i, e); |
| 2895 | } |
| 2896 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2897 | except_st = TryExcept(body, handlers, orelse, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2898 | n->n_col_offset, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2899 | if (!finally) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2900 | return except_st; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2901 | |
| 2902 | /* if a 'finally' is present too, we nest the TryExcept within a |
| 2903 | TryFinally to emulate try ... except ... finally */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2904 | body = asdl_seq_new(1, c->c_arena); |
| 2905 | if (body == NULL) |
| 2906 | return NULL; |
| 2907 | asdl_seq_SET(body, 0, except_st); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | /* must be a try ... finally (except clauses are in body, if any exist) */ |
| 2911 | assert(finally != NULL); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2912 | 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] | 2913 | } |
| 2914 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2915 | static expr_ty |
| 2916 | ast_for_with_var(struct compiling *c, const node *n) |
| 2917 | { |
| 2918 | REQ(n, with_var); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2919 | return ast_for_expr(c, CHILD(n, 1)); |
| 2920 | } |
| 2921 | |
| 2922 | /* with_stmt: 'with' test [ with_var ] ':' suite */ |
| 2923 | static stmt_ty |
| 2924 | ast_for_with_stmt(struct compiling *c, const node *n) |
| 2925 | { |
| 2926 | expr_ty context_expr, optional_vars = NULL; |
| 2927 | int suite_index = 3; /* skip 'with', test, and ':' */ |
| 2928 | asdl_seq *suite_seq; |
| 2929 | |
| 2930 | assert(TYPE(n) == with_stmt); |
| 2931 | context_expr = ast_for_expr(c, CHILD(n, 1)); |
| 2932 | if (TYPE(CHILD(n, 2)) == with_var) { |
| 2933 | optional_vars = ast_for_with_var(c, CHILD(n, 2)); |
| 2934 | |
| 2935 | if (!optional_vars) { |
| 2936 | return NULL; |
| 2937 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2938 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2939 | return NULL; |
| 2940 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2941 | suite_index = 4; |
| 2942 | } |
| 2943 | |
| 2944 | suite_seq = ast_for_suite(c, CHILD(n, suite_index)); |
| 2945 | if (!suite_seq) { |
| 2946 | return NULL; |
| 2947 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2948 | return With(context_expr, optional_vars, suite_seq, LINENO(n), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2949 | n->n_col_offset, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2952 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2953 | 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] | 2954 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2955 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
| 2956 | asdl_seq *s; |
| 2957 | expr_ty call, dummy; |
| 2958 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2959 | REQ(n, classdef); |
| 2960 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2961 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2962 | s = ast_for_suite(c, CHILD(n, 3)); |
| 2963 | if (!s) |
| 2964 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2965 | 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] | 2966 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2967 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2968 | |
| 2969 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2970 | s = ast_for_suite(c, CHILD(n,5)); |
| 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 | } |
| 2976 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2977 | /* class NAME '(' arglist ')' ':' suite */ |
| 2978 | /* build up a fake Call node so we can extract its pieces */ |
| 2979 | dummy = Name(NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2980 | call = ast_for_call(c, CHILD(n, 3), dummy); |
| 2981 | if (!call) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2984 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2986 | |
| 2987 | return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), |
| 2988 | call->v.Call.args, call->v.Call.keywords, |
| 2989 | call->v.Call.starargs, call->v.Call.kwargs, s, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2990 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | static stmt_ty |
| 2994 | ast_for_stmt(struct compiling *c, const node *n) |
| 2995 | { |
| 2996 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2997 | assert(NCH(n) == 1); |
| 2998 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | } |
| 3000 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3001 | assert(num_stmts(n) == 1); |
| 3002 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3003 | } |
| 3004 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3005 | REQ(n, small_stmt); |
| 3006 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3007 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3008 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3009 | */ |
| 3010 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3011 | case expr_stmt: |
| 3012 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | case del_stmt: |
| 3014 | return ast_for_del_stmt(c, n); |
| 3015 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3016 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3017 | case flow_stmt: |
| 3018 | return ast_for_flow_stmt(c, n); |
| 3019 | case import_stmt: |
| 3020 | return ast_for_import_stmt(c, n); |
| 3021 | case global_stmt: |
| 3022 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3023 | case nonlocal_stmt: |
| 3024 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3025 | case assert_stmt: |
| 3026 | return ast_for_assert_stmt(c, n); |
| 3027 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3028 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3029 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3030 | TYPE(n), NCH(n)); |
| 3031 | return NULL; |
| 3032 | } |
| 3033 | } |
| 3034 | else { |
| 3035 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3036 | | funcdef | classdef | decorated |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3037 | */ |
| 3038 | node *ch = CHILD(n, 0); |
| 3039 | REQ(n, compound_stmt); |
| 3040 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3041 | case if_stmt: |
| 3042 | return ast_for_if_stmt(c, ch); |
| 3043 | case while_stmt: |
| 3044 | return ast_for_while_stmt(c, ch); |
| 3045 | case for_stmt: |
| 3046 | return ast_for_for_stmt(c, ch); |
| 3047 | case try_stmt: |
| 3048 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3049 | case with_stmt: |
| 3050 | return ast_for_with_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3051 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3052 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3053 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3054 | return ast_for_classdef(c, ch, NULL); |
| 3055 | case decorated: |
| 3056 | return ast_for_decorated(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3058 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3059 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3060 | TYPE(n), NCH(n)); |
| 3061 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3062 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3067 | parsenumber(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3068 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3069 | const char *end; |
| 3070 | long x; |
| 3071 | double dx; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3072 | #ifndef WITHOUT_COMPLEX |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3073 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3074 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3075 | #endif |
| 3076 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3077 | errno = 0; |
| 3078 | end = s + strlen(s) - 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3079 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3080 | imflag = *end == 'j' || *end == 'J'; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3081 | #endif |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3082 | if (s[0] == '0') { |
| 3083 | x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); |
| 3084 | if (x < 0 && errno == 0) { |
| 3085 | return PyLong_FromString((char *)s, |
| 3086 | (char **)0, |
| 3087 | 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3088 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3089 | } |
| 3090 | else |
| 3091 | x = PyOS_strtol((char *)s, (char **)&end, 0); |
| 3092 | if (*end == '\0') { |
| 3093 | if (errno != 0) |
| 3094 | return PyLong_FromString((char *)s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3095 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3096 | } |
| 3097 | /* XXX Huge floats may silently fail */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3098 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3099 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3100 | compl.real = 0.; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3101 | PyFPE_START_PROTECT("atof", return 0) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3102 | compl.imag = PyOS_ascii_atof(s); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3103 | PyFPE_END_PROTECT(c) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3104 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3105 | } |
| 3106 | else |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3107 | #endif |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3108 | { |
| 3109 | PyFPE_START_PROTECT("atof", return 0) |
| 3110 | dx = PyOS_ascii_atof(s); |
| 3111 | PyFPE_END_PROTECT(dx) |
| 3112 | return PyFloat_FromDouble(dx); |
| 3113 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3117 | 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] | 3118 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3119 | PyObject *u, *v; |
| 3120 | char *s, *t; |
| 3121 | t = s = (char *)*sPtr; |
| 3122 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 3123 | while (s < end && (*s & 0x80)) s++; |
| 3124 | *sPtr = s; |
| 3125 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 3126 | if (u == NULL) |
| 3127 | return NULL; |
| 3128 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 3129 | Py_DECREF(u); |
| 3130 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | static PyObject * |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3134 | 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] | 3135 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3136 | PyObject *v, *u; |
| 3137 | char *buf; |
| 3138 | char *p; |
| 3139 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3140 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3141 | if (encoding == NULL) { |
| 3142 | buf = (char *)s; |
| 3143 | u = NULL; |
| 3144 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 3145 | buf = (char *)s; |
| 3146 | u = NULL; |
| 3147 | } else { |
| 3148 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3149 | u = PyBytes_FromStringAndSize((char *)NULL, len * 4); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3150 | if (u == NULL) |
| 3151 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3152 | p = buf = PyBytes_AsString(u); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3153 | end = s + len; |
| 3154 | while (s < end) { |
| 3155 | if (*s == '\\') { |
| 3156 | *p++ = *s++; |
| 3157 | if (*s & 0x80) { |
| 3158 | strcpy(p, "u005c"); |
| 3159 | p += 5; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3160 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3161 | } |
| 3162 | if (*s & 0x80) { /* XXX inefficient */ |
| 3163 | PyObject *w; |
| 3164 | char *r; |
| 3165 | Py_ssize_t rn, i; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3166 | w = decode_utf8(c, &s, end, "utf-16-be"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3167 | if (w == NULL) { |
| 3168 | Py_DECREF(u); |
| 3169 | return NULL; |
| 3170 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3171 | r = PyBytes_AS_STRING(w); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3172 | rn = Py_SIZE(w); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3173 | assert(rn % 2 == 0); |
| 3174 | for (i = 0; i < rn; i += 2) { |
| 3175 | sprintf(p, "\\u%02x%02x", |
| 3176 | r[i + 0] & 0xFF, |
| 3177 | r[i + 1] & 0xFF); |
| 3178 | p += 6; |
| 3179 | } |
| 3180 | Py_DECREF(w); |
| 3181 | } else { |
| 3182 | *p++ = *s++; |
| 3183 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3184 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3185 | len = p - buf; |
| 3186 | s = buf; |
| 3187 | } |
| 3188 | if (rawmode) |
| 3189 | v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); |
| 3190 | else |
| 3191 | v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
| 3192 | Py_XDECREF(u); |
| 3193 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* s is a Python string literal, including the bracketing quote characters, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3197 | * 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] | 3198 | * parsestr parses it, and returns the decoded Python string object. |
| 3199 | */ |
| 3200 | static PyObject * |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3201 | parsestr(struct compiling *c, const node *n, int *bytesmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3202 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3203 | size_t len; |
| 3204 | const char *s = STR(n); |
| 3205 | int quote = Py_CHARMASK(*s); |
| 3206 | int rawmode = 0; |
| 3207 | int need_encoding; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3208 | if (isalpha(quote)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3209 | if (quote == 'b' || quote == 'B') { |
| 3210 | quote = *++s; |
| 3211 | *bytesmode = 1; |
| 3212 | } |
| 3213 | if (quote == 'r' || quote == 'R') { |
| 3214 | quote = *++s; |
| 3215 | rawmode = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3216 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3217 | } |
| 3218 | if (quote != '\'' && quote != '\"') { |
| 3219 | PyErr_BadInternalCall(); |
| 3220 | return NULL; |
| 3221 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3222 | s++; |
| 3223 | len = strlen(s); |
| 3224 | if (len > INT_MAX) { |
| 3225 | PyErr_SetString(PyExc_OverflowError, |
| 3226 | "string to parse is too long"); |
| 3227 | return NULL; |
| 3228 | } |
| 3229 | if (s[--len] != quote) { |
| 3230 | PyErr_BadInternalCall(); |
| 3231 | return NULL; |
| 3232 | } |
| 3233 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 3234 | s += 2; |
| 3235 | len -= 2; |
| 3236 | if (s[--len] != quote || s[--len] != quote) { |
| 3237 | PyErr_BadInternalCall(); |
| 3238 | return NULL; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 3239 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3240 | } |
Benjamin Peterson | 8dbca06 | 2008-04-05 14:49:54 +0000 | [diff] [blame] | 3241 | if (!*bytesmode && !rawmode) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3242 | return decode_unicode(c, s, len, rawmode, c->c_encoding); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3243 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3244 | if (*bytesmode) { |
| 3245 | /* Disallow non-ascii characters (but not escapes) */ |
| 3246 | const char *c; |
| 3247 | for (c = s; *c; c++) { |
| 3248 | if (Py_CHARMASK(*c) >= 0x80) { |
| 3249 | ast_error(n, "bytes can only contain ASCII " |
| 3250 | "literal characters."); |
| 3251 | return NULL; |
| 3252 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 3253 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3254 | } |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3255 | need_encoding = (!*bytesmode && c->c_encoding != NULL && |
| 3256 | strcmp(c->c_encoding, "utf-8") != 0 && |
| 3257 | strcmp(c->c_encoding, "iso-8859-1") != 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3258 | if (rawmode || strchr(s, '\\') == NULL) { |
| 3259 | if (need_encoding) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3260 | PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL); |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3261 | if (u == NULL || !*bytesmode) |
| 3262 | return u; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3263 | v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3264 | Py_DECREF(u); |
| 3265 | return v; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3266 | } else if (*bytesmode) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3267 | return PyBytes_FromStringAndSize(s, len); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3268 | } else if (strcmp(c->c_encoding, "utf-8") == 0) { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3269 | return PyUnicode_FromStringAndSize(s, len); |
| 3270 | } else { |
| 3271 | return PyUnicode_DecodeLatin1(s, len, NULL); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3272 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3273 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3274 | return PyBytes_DecodeEscape(s, len, NULL, 1, |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3275 | need_encoding ? c->c_encoding : NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3278 | /* 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] | 3279 | * compile-time literal catenation, calling parsestr() on each piece, and |
| 3280 | * pasting the intermediate results together. |
| 3281 | */ |
| 3282 | static PyObject * |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 3283 | parsestrplus(struct compiling *c, const node *n, int *bytesmode) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3284 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3285 | PyObject *v; |
| 3286 | int i; |
| 3287 | REQ(CHILD(n, 0), STRING); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3288 | v = parsestr(c, CHILD(n, 0), bytesmode); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3289 | if (v != NULL) { |
| 3290 | /* String literal concatenation */ |
| 3291 | for (i = 1; i < NCH(n); i++) { |
| 3292 | PyObject *s; |
| 3293 | int subbm = 0; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 3294 | s = parsestr(c, CHILD(n, i), &subbm); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3295 | if (s == NULL) |
| 3296 | goto onError; |
| 3297 | if (*bytesmode != subbm) { |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 3298 | ast_error(n, "cannot mix bytes and nonbytes literals"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3299 | goto onError; |
| 3300 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3301 | if (PyBytes_Check(v) && PyBytes_Check(s)) { |
| 3302 | PyBytes_ConcatAndDel(&v, s); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3303 | if (v == NULL) |
| 3304 | goto onError; |
| 3305 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3306 | else { |
| 3307 | PyObject *temp = PyUnicode_Concat(v, s); |
| 3308 | Py_DECREF(s); |
| 3309 | Py_DECREF(v); |
| 3310 | v = temp; |
| 3311 | if (v == NULL) |
| 3312 | goto onError; |
| 3313 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3314 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3315 | } |
| 3316 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3317 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3318 | onError: |
| 3319 | Py_XDECREF(v); |
| 3320 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3321 | } |