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