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