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