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