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