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