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