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