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