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