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