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