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