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