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