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