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