Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file includes functions to transform a concrete syntax tree (CST) to |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 | * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4 | * |
| 5 | */ |
| 6 | #include "Python.h" |
| 7 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 8 | #include "node.h" |
| 9 | #include "ast.h" |
| 10 | #include "token.h" |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 11 | #include "pythonrun.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 15 | static int validate_stmts(asdl_seq *); |
| 16 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 17 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 18 | static int validate_stmt(stmt_ty); |
| 19 | static int validate_expr(expr_ty, expr_context_ty); |
| 20 | |
| 21 | static int |
| 22 | validate_comprehension(asdl_seq *gens) |
| 23 | { |
| 24 | int i; |
| 25 | if (!asdl_seq_LEN(gens)) { |
| 26 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 27 | return 0; |
| 28 | } |
| 29 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 30 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 31 | if (!validate_expr(comp->target, Store) || |
| 32 | !validate_expr(comp->iter, Load) || |
| 33 | !validate_exprs(comp->ifs, Load, 0)) |
| 34 | return 0; |
| 35 | } |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | validate_slice(slice_ty slice) |
| 41 | { |
| 42 | switch (slice->kind) { |
| 43 | case Slice_kind: |
| 44 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 45 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 46 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 47 | case ExtSlice_kind: { |
| 48 | int i; |
| 49 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 50 | return 0; |
| 51 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 52 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 53 | return 0; |
| 54 | return 1; |
| 55 | } |
| 56 | case Index_kind: |
| 57 | return validate_expr(slice->v.Index.value, Load); |
| 58 | default: |
| 59 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 60 | return 0; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static int |
| 65 | validate_keywords(asdl_seq *keywords) |
| 66 | { |
| 67 | int i; |
| 68 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 69 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 70 | return 0; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | static int |
| 75 | validate_args(asdl_seq *args) |
| 76 | { |
| 77 | int i; |
| 78 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 79 | arg_ty arg = asdl_seq_GET(args, i); |
| 80 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 81 | return 0; |
| 82 | } |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | static const char * |
| 87 | expr_context_name(expr_context_ty ctx) |
| 88 | { |
| 89 | switch (ctx) { |
| 90 | case Load: |
| 91 | return "Load"; |
| 92 | case Store: |
| 93 | return "Store"; |
| 94 | case Del: |
| 95 | return "Del"; |
| 96 | case AugLoad: |
| 97 | return "AugLoad"; |
| 98 | case AugStore: |
| 99 | return "AugStore"; |
| 100 | case Param: |
| 101 | return "Param"; |
| 102 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 103 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | validate_arguments(arguments_ty args) |
| 109 | { |
| 110 | if (!validate_args(args->args)) |
| 111 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 112 | if (args->vararg && args->vararg->annotation |
| 113 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | if (!validate_args(args->kwonlyargs)) |
| 117 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 118 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 119 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 123 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 124 | return 0; |
| 125 | } |
| 126 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 127 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 128 | "kw_defaults on arguments"); |
| 129 | return 0; |
| 130 | } |
| 131 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 132 | } |
| 133 | |
| 134 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 135 | validate_constant(PyObject *value) |
| 136 | { |
| 137 | if (value == Py_None || value == Py_Ellipsis) |
| 138 | return 1; |
| 139 | |
| 140 | if (PyLong_CheckExact(value) |
| 141 | || PyFloat_CheckExact(value) |
| 142 | || PyComplex_CheckExact(value) |
| 143 | || PyBool_Check(value) |
| 144 | || PyUnicode_CheckExact(value) |
| 145 | || PyBytes_CheckExact(value)) |
| 146 | return 1; |
| 147 | |
| 148 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 149 | PyObject *it; |
| 150 | |
| 151 | it = PyObject_GetIter(value); |
| 152 | if (it == NULL) |
| 153 | return 0; |
| 154 | |
| 155 | while (1) { |
| 156 | PyObject *item = PyIter_Next(it); |
| 157 | if (item == NULL) { |
| 158 | if (PyErr_Occurred()) { |
| 159 | Py_DECREF(it); |
| 160 | return 0; |
| 161 | } |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (!validate_constant(item)) { |
| 166 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 167 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 168 | return 0; |
| 169 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 170 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | Py_DECREF(it); |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 181 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 182 | { |
| 183 | int check_ctx = 1; |
| 184 | expr_context_ty actual_ctx; |
| 185 | |
| 186 | /* First check expression context. */ |
| 187 | switch (exp->kind) { |
| 188 | case Attribute_kind: |
| 189 | actual_ctx = exp->v.Attribute.ctx; |
| 190 | break; |
| 191 | case Subscript_kind: |
| 192 | actual_ctx = exp->v.Subscript.ctx; |
| 193 | break; |
| 194 | case Starred_kind: |
| 195 | actual_ctx = exp->v.Starred.ctx; |
| 196 | break; |
| 197 | case Name_kind: |
| 198 | actual_ctx = exp->v.Name.ctx; |
| 199 | break; |
| 200 | case List_kind: |
| 201 | actual_ctx = exp->v.List.ctx; |
| 202 | break; |
| 203 | case Tuple_kind: |
| 204 | actual_ctx = exp->v.Tuple.ctx; |
| 205 | break; |
| 206 | default: |
| 207 | if (ctx != Load) { |
| 208 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 209 | "assigned to in %s context", expr_context_name(ctx)); |
| 210 | return 0; |
| 211 | } |
| 212 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 213 | /* set actual_ctx to prevent gcc warning */ |
| 214 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 215 | } |
| 216 | if (check_ctx && actual_ctx != ctx) { |
| 217 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 218 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Now validate expression. */ |
| 223 | switch (exp->kind) { |
| 224 | case BoolOp_kind: |
| 225 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 226 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 227 | return 0; |
| 228 | } |
| 229 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 230 | case BinOp_kind: |
| 231 | return validate_expr(exp->v.BinOp.left, Load) && |
| 232 | validate_expr(exp->v.BinOp.right, Load); |
| 233 | case UnaryOp_kind: |
| 234 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 235 | case Lambda_kind: |
| 236 | return validate_arguments(exp->v.Lambda.args) && |
| 237 | validate_expr(exp->v.Lambda.body, Load); |
| 238 | case IfExp_kind: |
| 239 | return validate_expr(exp->v.IfExp.test, Load) && |
| 240 | validate_expr(exp->v.IfExp.body, Load) && |
| 241 | validate_expr(exp->v.IfExp.orelse, Load); |
| 242 | case Dict_kind: |
| 243 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 244 | PyErr_SetString(PyExc_ValueError, |
| 245 | "Dict doesn't have the same number of keys as values"); |
| 246 | return 0; |
| 247 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 248 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 249 | dict literals, i.e. ``{**{a:b}}`` */ |
| 250 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 251 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 252 | case Set_kind: |
| 253 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 254 | #define COMP(NAME) \ |
| 255 | case NAME ## _kind: \ |
| 256 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 257 | validate_expr(exp->v.NAME.elt, Load); |
| 258 | COMP(ListComp) |
| 259 | COMP(SetComp) |
| 260 | COMP(GeneratorExp) |
| 261 | #undef COMP |
| 262 | case DictComp_kind: |
| 263 | return validate_comprehension(exp->v.DictComp.generators) && |
| 264 | validate_expr(exp->v.DictComp.key, Load) && |
| 265 | validate_expr(exp->v.DictComp.value, Load); |
| 266 | case Yield_kind: |
| 267 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 268 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 269 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 270 | case Await_kind: |
| 271 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 272 | case Compare_kind: |
| 273 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 274 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 275 | return 0; |
| 276 | } |
| 277 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 278 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 279 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 280 | "of comparators and operands"); |
| 281 | return 0; |
| 282 | } |
| 283 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 284 | validate_expr(exp->v.Compare.left, Load); |
| 285 | case Call_kind: |
| 286 | return validate_expr(exp->v.Call.func, Load) && |
| 287 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 288 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 289 | case Constant_kind: |
| 290 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 291 | PyErr_Format(PyExc_TypeError, |
| 292 | "got an invalid type in Constant: %s", |
| 293 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 297 | case Num_kind: { |
| 298 | PyObject *n = exp->v.Num.n; |
| 299 | if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) && |
| 300 | !PyComplex_CheckExact(n)) { |
| 301 | PyErr_SetString(PyExc_TypeError, "non-numeric type in Num"); |
| 302 | return 0; |
| 303 | } |
| 304 | return 1; |
| 305 | } |
| 306 | case Str_kind: { |
| 307 | PyObject *s = exp->v.Str.s; |
| 308 | if (!PyUnicode_CheckExact(s)) { |
| 309 | PyErr_SetString(PyExc_TypeError, "non-string type in Str"); |
| 310 | return 0; |
| 311 | } |
| 312 | return 1; |
| 313 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 314 | case JoinedStr_kind: |
| 315 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 316 | case FormattedValue_kind: |
| 317 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 318 | return 0; |
| 319 | if (exp->v.FormattedValue.format_spec) |
| 320 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 321 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 322 | case Bytes_kind: { |
| 323 | PyObject *b = exp->v.Bytes.s; |
| 324 | if (!PyBytes_CheckExact(b)) { |
| 325 | PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes"); |
| 326 | return 0; |
| 327 | } |
| 328 | return 1; |
| 329 | } |
| 330 | case Attribute_kind: |
| 331 | return validate_expr(exp->v.Attribute.value, Load); |
| 332 | case Subscript_kind: |
| 333 | return validate_slice(exp->v.Subscript.slice) && |
| 334 | validate_expr(exp->v.Subscript.value, Load); |
| 335 | case Starred_kind: |
| 336 | return validate_expr(exp->v.Starred.value, ctx); |
| 337 | case List_kind: |
| 338 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 339 | case Tuple_kind: |
| 340 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
| 341 | /* These last cases don't have any checking. */ |
| 342 | case Name_kind: |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 343 | case NameConstant_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 344 | case Ellipsis_kind: |
| 345 | return 1; |
| 346 | default: |
| 347 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 348 | return 0; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static int |
| 353 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 354 | { |
| 355 | if (asdl_seq_LEN(seq)) |
| 356 | return 1; |
| 357 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int |
| 362 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 363 | { |
| 364 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 365 | validate_exprs(targets, ctx, 0); |
| 366 | } |
| 367 | |
| 368 | static int |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 369 | validate_body(asdl_seq *body, const char *owner, int allowempty) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 370 | { |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 371 | if (!allowempty && !validate_nonempty_seq(body, "body", owner)) { |
| 372 | return 0; |
| 373 | } |
| 374 | return validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int |
| 378 | validate_stmt(stmt_ty stmt) |
| 379 | { |
| 380 | int i; |
| 381 | switch (stmt->kind) { |
| 382 | case FunctionDef_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 383 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef", |
| 384 | stmt->v.FunctionDef.docstring != NULL) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 385 | validate_arguments(stmt->v.FunctionDef.args) && |
| 386 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 387 | (!stmt->v.FunctionDef.returns || |
| 388 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 389 | case ClassDef_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 390 | return validate_body(stmt->v.ClassDef.body, "ClassDef", |
| 391 | stmt->v.ClassDef.docstring != NULL) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 392 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 393 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 394 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 395 | case Return_kind: |
| 396 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 397 | case Delete_kind: |
| 398 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 399 | case Assign_kind: |
| 400 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 401 | validate_expr(stmt->v.Assign.value, Load); |
| 402 | case AugAssign_kind: |
| 403 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 404 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 405 | case AnnAssign_kind: |
| 406 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 407 | stmt->v.AnnAssign.simple) { |
| 408 | PyErr_SetString(PyExc_TypeError, |
| 409 | "AnnAssign with simple non-Name target"); |
| 410 | return 0; |
| 411 | } |
| 412 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 413 | (!stmt->v.AnnAssign.value || |
| 414 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 415 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 416 | case For_kind: |
| 417 | return validate_expr(stmt->v.For.target, Store) && |
| 418 | validate_expr(stmt->v.For.iter, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 419 | validate_body(stmt->v.For.body, "For", 0) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 420 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 421 | case AsyncFor_kind: |
| 422 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 423 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 424 | validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 425 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 426 | case While_kind: |
| 427 | return validate_expr(stmt->v.While.test, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 428 | validate_body(stmt->v.While.body, "While", 0) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 429 | validate_stmts(stmt->v.While.orelse); |
| 430 | case If_kind: |
| 431 | return validate_expr(stmt->v.If.test, Load) && |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 432 | validate_body(stmt->v.If.body, "If", 0) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 433 | validate_stmts(stmt->v.If.orelse); |
| 434 | case With_kind: |
| 435 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 436 | return 0; |
| 437 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 438 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 439 | if (!validate_expr(item->context_expr, Load) || |
| 440 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 441 | return 0; |
| 442 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 443 | return validate_body(stmt->v.With.body, "With", 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 444 | case AsyncWith_kind: |
| 445 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 446 | return 0; |
| 447 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 448 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 449 | if (!validate_expr(item->context_expr, Load) || |
| 450 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 451 | return 0; |
| 452 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 453 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 454 | case Raise_kind: |
| 455 | if (stmt->v.Raise.exc) { |
| 456 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 457 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 458 | } |
| 459 | if (stmt->v.Raise.cause) { |
| 460 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 461 | return 0; |
| 462 | } |
| 463 | return 1; |
| 464 | case Try_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 465 | if (!validate_body(stmt->v.Try.body, "Try", 0)) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 466 | return 0; |
| 467 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 468 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 469 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 470 | return 0; |
| 471 | } |
| 472 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 473 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 474 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 475 | return 0; |
| 476 | } |
| 477 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 478 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 479 | if ((handler->v.ExceptHandler.type && |
| 480 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 481 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0)) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 485 | validate_stmts(stmt->v.Try.finalbody)) && |
| 486 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 487 | validate_stmts(stmt->v.Try.orelse)); |
| 488 | case Assert_kind: |
| 489 | return validate_expr(stmt->v.Assert.test, Load) && |
| 490 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 491 | case Import_kind: |
| 492 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 493 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 494 | if (stmt->v.ImportFrom.level < 0) { |
| 495 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 499 | case Global_kind: |
| 500 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 501 | case Nonlocal_kind: |
| 502 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 503 | case Expr_kind: |
| 504 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 505 | case AsyncFunctionDef_kind: |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 506 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef", |
| 507 | stmt->v.AsyncFunctionDef.docstring != NULL) && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 508 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 509 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 510 | (!stmt->v.AsyncFunctionDef.returns || |
| 511 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 512 | case Pass_kind: |
| 513 | case Break_kind: |
| 514 | case Continue_kind: |
| 515 | return 1; |
| 516 | default: |
| 517 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 518 | return 0; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static int |
| 523 | validate_stmts(asdl_seq *seq) |
| 524 | { |
| 525 | int i; |
| 526 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 527 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 528 | if (stmt) { |
| 529 | if (!validate_stmt(stmt)) |
| 530 | return 0; |
| 531 | } |
| 532 | else { |
| 533 | PyErr_SetString(PyExc_ValueError, |
| 534 | "None disallowed in statement list"); |
| 535 | return 0; |
| 536 | } |
| 537 | } |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | static int |
| 542 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 543 | { |
| 544 | int i; |
| 545 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 546 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 547 | if (expr) { |
| 548 | if (!validate_expr(expr, ctx)) |
| 549 | return 0; |
| 550 | } |
| 551 | else if (!null_ok) { |
| 552 | PyErr_SetString(PyExc_ValueError, |
| 553 | "None disallowed in expression list"); |
| 554 | return 0; |
| 555 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 556 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 557 | } |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | int |
| 562 | PyAST_Validate(mod_ty mod) |
| 563 | { |
| 564 | int res = 0; |
| 565 | |
| 566 | switch (mod->kind) { |
| 567 | case Module_kind: |
| 568 | res = validate_stmts(mod->v.Module.body); |
| 569 | break; |
| 570 | case Interactive_kind: |
| 571 | res = validate_stmts(mod->v.Interactive.body); |
| 572 | break; |
| 573 | case Expression_kind: |
| 574 | res = validate_expr(mod->v.Expression.body, Load); |
| 575 | break; |
| 576 | case Suite_kind: |
| 577 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 578 | break; |
| 579 | default: |
| 580 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 581 | res = 0; |
| 582 | break; |
| 583 | } |
| 584 | return res; |
| 585 | } |
| 586 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 587 | /* This is done here, so defines like "test" don't interfere with AST use above. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 588 | #include "grammar.h" |
| 589 | #include "parsetok.h" |
| 590 | #include "graminit.h" |
| 591 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 592 | /* Data structure used internally */ |
| 593 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 594 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 595 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 596 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 600 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 601 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 602 | static asdl_seq *ast_for_body(struct compiling *c, const node *n, |
| 603 | string *docstring); |
| 604 | static string docstring_from_stmts(asdl_seq *stmts); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 605 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 606 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 607 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 608 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 609 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 610 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); |
| 611 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); |
| 612 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 613 | /* Note different signature for ast_for_call */ |
| 614 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); |
| 615 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 616 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 617 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 618 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 619 | #define COMP_GENEXP 0 |
| 620 | #define COMP_LISTCOMP 1 |
| 621 | #define COMP_SETCOMP 2 |
| 622 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 623 | static int |
| 624 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 625 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 626 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 627 | if (!m) |
| 628 | return 0; |
| 629 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 630 | Py_DECREF(m); |
| 631 | if (!c->c_normalize) |
| 632 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 633 | return 1; |
| 634 | } |
| 635 | |
| 636 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 637 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 638 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 639 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 640 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 641 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 642 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 643 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 644 | /* Check whether there are non-ASCII characters in the |
| 645 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 646 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 647 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 648 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 649 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 650 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 651 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 652 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 653 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 654 | if (form == NULL) { |
| 655 | Py_DECREF(id); |
| 656 | return NULL; |
| 657 | } |
| 658 | PyObject *args[2] = {form, id}; |
| 659 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 660 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 661 | if (!id2) |
| 662 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 663 | if (!PyUnicode_Check(id2)) { |
| 664 | PyErr_Format(PyExc_TypeError, |
| 665 | "unicodedata.normalize() must return a string, not " |
| 666 | "%.200s", |
| 667 | Py_TYPE(id2)->tp_name); |
| 668 | Py_DECREF(id2); |
| 669 | return NULL; |
| 670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 671 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 672 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 673 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 674 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 675 | Py_DECREF(id); |
| 676 | return NULL; |
| 677 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 678 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 681 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 683 | static int |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 684 | ast_error(struct compiling *c, const node *n, const char *errmsg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 685 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 686 | PyObject *value, *errstr, *loc, *tmp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 687 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 688 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 689 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 690 | Py_INCREF(Py_None); |
| 691 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 692 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 693 | tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 694 | if (!tmp) |
| 695 | return 0; |
| 696 | errstr = PyUnicode_FromString(errmsg); |
| 697 | if (!errstr) { |
| 698 | Py_DECREF(tmp); |
| 699 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 700 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 701 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 702 | Py_DECREF(errstr); |
| 703 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 704 | if (value) { |
| 705 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 706 | Py_DECREF(value); |
| 707 | } |
| 708 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* num_stmts() returns number of contained statements. |
| 712 | |
| 713 | Use this routine to determine how big a sequence is needed for |
| 714 | the statements in a parse tree. Its raison d'etre is this bit of |
| 715 | grammar: |
| 716 | |
| 717 | stmt: simple_stmt | compound_stmt |
| 718 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 719 | |
| 720 | A simple_stmt can contain multiple small_stmt elements joined |
| 721 | by semicolons. If the arg is a simple_stmt, the number of |
| 722 | small_stmt elements is returned. |
| 723 | */ |
| 724 | |
| 725 | static int |
| 726 | num_stmts(const node *n) |
| 727 | { |
| 728 | int i, l; |
| 729 | node *ch; |
| 730 | |
| 731 | switch (TYPE(n)) { |
| 732 | case single_input: |
| 733 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 734 | return 0; |
| 735 | else |
| 736 | return num_stmts(CHILD(n, 0)); |
| 737 | case file_input: |
| 738 | l = 0; |
| 739 | for (i = 0; i < NCH(n); i++) { |
| 740 | ch = CHILD(n, i); |
| 741 | if (TYPE(ch) == stmt) |
| 742 | l += num_stmts(ch); |
| 743 | } |
| 744 | return l; |
| 745 | case stmt: |
| 746 | return num_stmts(CHILD(n, 0)); |
| 747 | case compound_stmt: |
| 748 | return 1; |
| 749 | case simple_stmt: |
| 750 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 751 | case suite: |
| 752 | if (NCH(n) == 1) |
| 753 | return num_stmts(CHILD(n, 0)); |
| 754 | else { |
| 755 | l = 0; |
| 756 | for (i = 2; i < (NCH(n) - 1); i++) |
| 757 | l += num_stmts(CHILD(n, i)); |
| 758 | return l; |
| 759 | } |
| 760 | default: { |
| 761 | char buf[128]; |
| 762 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 763 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 764 | TYPE(n), NCH(n)); |
| 765 | Py_FatalError(buf); |
| 766 | } |
| 767 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 768 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* Transform the CST rooted at node * to the appropriate AST |
| 772 | */ |
| 773 | |
| 774 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 775 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 776 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 777 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 778 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 779 | asdl_seq *stmts = NULL; |
| 780 | stmt_ty s; |
| 781 | node *ch; |
| 782 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 783 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 785 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 786 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 787 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 788 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 789 | |
| 790 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 793 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | switch (TYPE(n)) { |
| 795 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 796 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 797 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 798 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | for (i = 0; i < NCH(n) - 1; i++) { |
| 800 | ch = CHILD(n, i); |
| 801 | if (TYPE(ch) == NEWLINE) |
| 802 | continue; |
| 803 | REQ(ch, stmt); |
| 804 | num = num_stmts(ch); |
| 805 | if (num == 1) { |
| 806 | s = ast_for_stmt(&c, ch); |
| 807 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 808 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 809 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 810 | } |
| 811 | else { |
| 812 | ch = CHILD(ch, 0); |
| 813 | REQ(ch, simple_stmt); |
| 814 | for (j = 0; j < num; j++) { |
| 815 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 816 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 817 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 818 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 822 | res = Module(stmts, docstring_from_stmts(stmts), arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 823 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 824 | case eval_input: { |
| 825 | expr_ty testlist_ast; |
| 826 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 827 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 828 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 829 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 830 | goto out; |
| 831 | res = Expression(testlist_ast, arena); |
| 832 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 833 | } |
| 834 | case single_input: |
| 835 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 836 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 838 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 839 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 840 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 841 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 842 | goto out; |
| 843 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | } |
| 845 | else { |
| 846 | n = CHILD(n, 0); |
| 847 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 848 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 850 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 851 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 852 | s = ast_for_stmt(&c, n); |
| 853 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 854 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 855 | asdl_seq_SET(stmts, 0, s); |
| 856 | } |
| 857 | else { |
| 858 | /* Only a simple_stmt can contain multiple statements. */ |
| 859 | REQ(n, simple_stmt); |
| 860 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 862 | break; |
| 863 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 864 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 865 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | asdl_seq_SET(stmts, i / 2, s); |
| 867 | } |
| 868 | } |
| 869 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 870 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 871 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 872 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 874 | PyErr_Format(PyExc_SystemError, |
| 875 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 876 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 877 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 878 | out: |
| 879 | if (c.c_normalize) { |
| 880 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 881 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 882 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 885 | mod_ty |
| 886 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 887 | PyArena *arena) |
| 888 | { |
| 889 | mod_ty mod; |
| 890 | PyObject *filename; |
| 891 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 892 | if (filename == NULL) |
| 893 | return NULL; |
| 894 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 895 | Py_DECREF(filename); |
| 896 | return mod; |
| 897 | |
| 898 | } |
| 899 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 900 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 901 | */ |
| 902 | |
| 903 | static operator_ty |
| 904 | get_operator(const node *n) |
| 905 | { |
| 906 | switch (TYPE(n)) { |
| 907 | case VBAR: |
| 908 | return BitOr; |
| 909 | case CIRCUMFLEX: |
| 910 | return BitXor; |
| 911 | case AMPER: |
| 912 | return BitAnd; |
| 913 | case LEFTSHIFT: |
| 914 | return LShift; |
| 915 | case RIGHTSHIFT: |
| 916 | return RShift; |
| 917 | case PLUS: |
| 918 | return Add; |
| 919 | case MINUS: |
| 920 | return Sub; |
| 921 | case STAR: |
| 922 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 923 | case AT: |
| 924 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 925 | case SLASH: |
| 926 | return Div; |
| 927 | case DOUBLESLASH: |
| 928 | return FloorDiv; |
| 929 | case PERCENT: |
| 930 | return Mod; |
| 931 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 932 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 936 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 937 | "None", |
| 938 | "True", |
| 939 | "False", |
| 940 | NULL, |
| 941 | }; |
| 942 | |
| 943 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 944 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 945 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 946 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 947 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 948 | if (_PyUnicode_EqualToASCIIString(name, "__debug__")) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 949 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 950 | return 1; |
| 951 | } |
| 952 | if (full_checks) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 953 | const char * const *p; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 954 | for (p = FORBIDDEN; *p; p++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 955 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 956 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 957 | return 1; |
| 958 | } |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | return 0; |
| 962 | } |
| 963 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 964 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 965 | |
| 966 | Only sets context for expr kinds that "can appear in assignment context" |
| 967 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 968 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 969 | */ |
| 970 | |
| 971 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 972 | set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 973 | { |
| 974 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 975 | /* If a particular expression type can't be used for assign / delete, |
| 976 | set expr_name to its name and an error message will be generated. |
| 977 | */ |
| 978 | const char* expr_name = NULL; |
| 979 | |
| 980 | /* The ast defines augmented store and load contexts, but the |
| 981 | implementation here doesn't actually use them. The code may be |
| 982 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 983 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 984 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 985 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 986 | */ |
| 987 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 988 | |
| 989 | switch (e->kind) { |
| 990 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 991 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 992 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 993 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 994 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 995 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 996 | e->v.Subscript.ctx = ctx; |
| 997 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 998 | case Starred_kind: |
| 999 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1000 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1001 | return 0; |
| 1002 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1003 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1004 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1005 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1006 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1007 | } |
| 1008 | e->v.Name.ctx = ctx; |
| 1009 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1010 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1011 | e->v.List.ctx = ctx; |
| 1012 | s = e->v.List.elts; |
| 1013 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1014 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1015 | e->v.Tuple.ctx = ctx; |
| 1016 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1017 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1018 | case Lambda_kind: |
| 1019 | expr_name = "lambda"; |
| 1020 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1021 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1022 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1023 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1024 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1025 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1026 | case UnaryOp_kind: |
| 1027 | expr_name = "operator"; |
| 1028 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1029 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1030 | expr_name = "generator expression"; |
| 1031 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1032 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1033 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1034 | expr_name = "yield expression"; |
| 1035 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1036 | case Await_kind: |
| 1037 | expr_name = "await expression"; |
| 1038 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1039 | case ListComp_kind: |
| 1040 | expr_name = "list comprehension"; |
| 1041 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1042 | case SetComp_kind: |
| 1043 | expr_name = "set comprehension"; |
| 1044 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1045 | case DictComp_kind: |
| 1046 | expr_name = "dict comprehension"; |
| 1047 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1048 | case Dict_kind: |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1049 | case Set_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1050 | case Num_kind: |
| 1051 | case Str_kind: |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 1052 | case Bytes_kind: |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1053 | case JoinedStr_kind: |
| 1054 | case FormattedValue_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1055 | expr_name = "literal"; |
| 1056 | break; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1057 | case NameConstant_kind: |
| 1058 | expr_name = "keyword"; |
| 1059 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1060 | case Ellipsis_kind: |
| 1061 | expr_name = "Ellipsis"; |
| 1062 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1063 | case Compare_kind: |
| 1064 | expr_name = "comparison"; |
| 1065 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1066 | case IfExp_kind: |
| 1067 | expr_name = "conditional expression"; |
| 1068 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1069 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | PyErr_Format(PyExc_SystemError, |
| 1071 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1072 | e->kind, e->lineno); |
| 1073 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1074 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1075 | /* Check for error string set by switch */ |
| 1076 | if (expr_name) { |
| 1077 | char buf[300]; |
| 1078 | PyOS_snprintf(buf, sizeof(buf), |
| 1079 | "can't %s %s", |
| 1080 | ctx == Store ? "assign to" : "delete", |
| 1081 | expr_name); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1082 | return ast_error(c, n, buf); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1085 | /* If the LHS is a list or tuple, we need to set the assignment |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1087 | */ |
| 1088 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1089 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1090 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1091 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1092 | if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1095 | } |
| 1096 | return 1; |
| 1097 | } |
| 1098 | |
| 1099 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1100 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1101 | { |
| 1102 | REQ(n, augassign); |
| 1103 | n = CHILD(n, 0); |
| 1104 | switch (STR(n)[0]) { |
| 1105 | case '+': |
| 1106 | return Add; |
| 1107 | case '-': |
| 1108 | return Sub; |
| 1109 | case '/': |
| 1110 | if (STR(n)[1] == '/') |
| 1111 | return FloorDiv; |
| 1112 | else |
| 1113 | return Div; |
| 1114 | case '%': |
| 1115 | return Mod; |
| 1116 | case '<': |
| 1117 | return LShift; |
| 1118 | case '>': |
| 1119 | return RShift; |
| 1120 | case '&': |
| 1121 | return BitAnd; |
| 1122 | case '^': |
| 1123 | return BitXor; |
| 1124 | case '|': |
| 1125 | return BitOr; |
| 1126 | case '*': |
| 1127 | if (STR(n)[1] == '*') |
| 1128 | return Pow; |
| 1129 | else |
| 1130 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1131 | case '@': |
| 1132 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1133 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1134 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1135 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1140 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1141 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1142 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | |'is' 'not' |
| 1144 | */ |
| 1145 | REQ(n, comp_op); |
| 1146 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1147 | n = CHILD(n, 0); |
| 1148 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1149 | case LESS: |
| 1150 | return Lt; |
| 1151 | case GREATER: |
| 1152 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1153 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1154 | return Eq; |
| 1155 | case LESSEQUAL: |
| 1156 | return LtE; |
| 1157 | case GREATEREQUAL: |
| 1158 | return GtE; |
| 1159 | case NOTEQUAL: |
| 1160 | return NotEq; |
| 1161 | case NAME: |
| 1162 | if (strcmp(STR(n), "in") == 0) |
| 1163 | return In; |
| 1164 | if (strcmp(STR(n), "is") == 0) |
| 1165 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1166 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1167 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1168 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1169 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1170 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1171 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1172 | } |
| 1173 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1174 | /* handle "not in" and "is not" */ |
| 1175 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1176 | case NAME: |
| 1177 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1178 | return NotIn; |
| 1179 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1180 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1181 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1183 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1185 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1186 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1187 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1188 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1189 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1190 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static asdl_seq * |
| 1194 | seq_for_testlist(struct compiling *c, const node *n) |
| 1195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1197 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1198 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1199 | asdl_seq *seq; |
| 1200 | expr_ty expression; |
| 1201 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1202 | assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1203 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1204 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1205 | if (!seq) |
| 1206 | return NULL; |
| 1207 | |
| 1208 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1210 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1211 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1212 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1213 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1214 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | |
| 1216 | assert(i / 2 < seq->size); |
| 1217 | asdl_seq_SET(seq, i / 2, expression); |
| 1218 | } |
| 1219 | return seq; |
| 1220 | } |
| 1221 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1222 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1223 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1224 | { |
| 1225 | identifier name; |
| 1226 | expr_ty annotation = NULL; |
| 1227 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1228 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1229 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1230 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1231 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1232 | name = NEW_IDENTIFIER(ch); |
| 1233 | if (!name) |
| 1234 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1235 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1236 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1239 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1240 | if (!annotation) |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1244 | ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1245 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1246 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1247 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1250 | /* returns -1 if failed to handle keyword only arguments |
| 1251 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1252 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1253 | ^^^ |
| 1254 | start pointing here |
| 1255 | */ |
| 1256 | static int |
| 1257 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1258 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1259 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1260 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1261 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1262 | expr_ty expression, annotation; |
| 1263 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1264 | int i = start; |
| 1265 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1266 | |
| 1267 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1268 | ast_error(c, CHILD(n, start), "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1269 | return -1; |
| 1270 | } |
| 1271 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1272 | while (i < NCH(n)) { |
| 1273 | ch = CHILD(n, i); |
| 1274 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1275 | case vfpdef: |
| 1276 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1277 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1278 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1279 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1280 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1281 | asdl_seq_SET(kwdefaults, j, expression); |
| 1282 | i += 2; /* '=' and test */ |
| 1283 | } |
| 1284 | else { /* setting NULL if no default value exists */ |
| 1285 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1286 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1287 | if (NCH(ch) == 3) { |
| 1288 | /* ch is NAME ':' test */ |
| 1289 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1290 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1291 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1292 | } |
| 1293 | else { |
| 1294 | annotation = NULL; |
| 1295 | } |
| 1296 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1297 | argname = NEW_IDENTIFIER(ch); |
| 1298 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1299 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1300 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1301 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1302 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 1303 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1304 | if (!arg) |
| 1305 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1306 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1307 | i += 2; /* the name and the comma */ |
| 1308 | break; |
| 1309 | case DOUBLESTAR: |
| 1310 | return i; |
| 1311 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1312 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1313 | goto error; |
| 1314 | } |
| 1315 | } |
| 1316 | return i; |
| 1317 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1319 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1320 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1321 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1322 | |
| 1323 | static arguments_ty |
| 1324 | ast_for_arguments(struct compiling *c, const node *n) |
| 1325 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1326 | /* This function handles both typedargslist (function definition) |
| 1327 | and varargslist (lambda definition). |
| 1328 | |
| 1329 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1330 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1331 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1332 | | '**' tfpdef [',']]] |
| 1333 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1334 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1335 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1336 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1337 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1338 | | '**' vfpdef [',']]] |
| 1339 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1340 | | '**' vfpdef [','] |
| 1341 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1342 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1343 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1344 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1345 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1346 | int nposdefaults = 0, found_default = 0; |
| 1347 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1348 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1349 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1350 | node *ch; |
| 1351 | |
| 1352 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1353 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1354 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1355 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1357 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1358 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1359 | /* First count the number of positional args & defaults. The |
| 1360 | variable i is the loop index for this for loop and the next. |
| 1361 | The next loop picks up where the first leaves off. |
| 1362 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1363 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1364 | ch = CHILD(n, i); |
| 1365 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1366 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1367 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1368 | if (i < NCH(n) && /* skip argument following star */ |
| 1369 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1370 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1371 | i++; |
| 1372 | } |
| 1373 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1374 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1375 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1376 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1377 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1378 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1380 | defaults for keyword only args */ |
| 1381 | for ( ; i < NCH(n); ++i) { |
| 1382 | ch = CHILD(n, i); |
| 1383 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1384 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1385 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1386 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1387 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1388 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1389 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1390 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1391 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1392 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1394 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1395 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1396 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1398 | since we set NULL as default for keyword only argument w/o default |
| 1399 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1400 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1401 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1402 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1403 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1404 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1405 | /* tfpdef: NAME [':' test] |
| 1406 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1407 | */ |
| 1408 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1409 | j = 0; /* index for defaults */ |
| 1410 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1411 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1412 | ch = CHILD(n, i); |
| 1413 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1414 | case tfpdef: |
| 1415 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1416 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1417 | anything other than EQUAL or a comma? */ |
| 1418 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1419 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1420 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1421 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1422 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1423 | assert(posdefaults != NULL); |
| 1424 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1425 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1426 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1427 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1428 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1429 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1430 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1431 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1432 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1433 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1434 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1435 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1436 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | i += 2; /* the name and the comma */ |
| 1438 | break; |
| 1439 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1440 | if (i+1 >= NCH(n) || |
| 1441 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1442 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1443 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1444 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1445 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1446 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1447 | if (TYPE(ch) == COMMA) { |
| 1448 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1449 | i += 2; /* now follows keyword only arguments */ |
| 1450 | res = handle_keywordonly_args(c, n, i, |
| 1451 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1452 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1453 | i = res; /* res has new position to process */ |
| 1454 | } |
| 1455 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1456 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1457 | if (!vararg) |
| 1458 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1459 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1460 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1461 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1462 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1463 | int res = 0; |
| 1464 | res = handle_keywordonly_args(c, n, i, |
| 1465 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1466 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1467 | i = res; /* res has new position to process */ |
| 1468 | } |
| 1469 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1472 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1473 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1474 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1475 | if (!kwarg) |
| 1476 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1477 | i += 3; |
| 1478 | break; |
| 1479 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1480 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1481 | "unexpected node in varargslist: %d @ %d", |
| 1482 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1483 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1484 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1485 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1486 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | static expr_ty |
| 1490 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1491 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1492 | expr_ty e; |
| 1493 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1494 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1495 | int i; |
| 1496 | |
| 1497 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1498 | |
| 1499 | lineno = LINENO(n); |
| 1500 | col_offset = n->n_col_offset; |
| 1501 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1502 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1503 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1504 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1505 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1507 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | |
| 1509 | for (i = 2; i < NCH(n); i+=2) { |
| 1510 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1511 | if (!id) |
| 1512 | return NULL; |
| 1513 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1514 | if (!e) |
| 1515 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static expr_ty |
| 1522 | ast_for_decorator(struct compiling *c, const node *n) |
| 1523 | { |
| 1524 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1525 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1526 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1528 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1529 | REQ(CHILD(n, 0), AT); |
| 1530 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1533 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1534 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1536 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1537 | d = name_expr; |
| 1538 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1541 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1542 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1543 | if (!d) |
| 1544 | return NULL; |
| 1545 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1546 | } |
| 1547 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1548 | d = ast_for_call(c, CHILD(n, 3), name_expr); |
| 1549 | if (!d) |
| 1550 | return NULL; |
| 1551 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static asdl_seq* |
| 1558 | ast_for_decorators(struct compiling *c, const node *n) |
| 1559 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1560 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1561 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1564 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1565 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1566 | if (!decorator_seq) |
| 1567 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1569 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1570 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1571 | if (!d) |
| 1572 | return NULL; |
| 1573 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1574 | } |
| 1575 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1579 | ast_for_funcdef_impl(struct compiling *c, const node *n, |
| 1580 | asdl_seq *decorator_seq, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1581 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1582 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1583 | identifier name; |
| 1584 | arguments_ty args; |
| 1585 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1586 | expr_ty returns = NULL; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1587 | string docstring; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1588 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1589 | |
| 1590 | REQ(n, funcdef); |
| 1591 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1592 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1593 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1594 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1595 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1596 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1597 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1598 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1599 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1600 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1601 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1602 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1603 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1604 | name_i += 2; |
| 1605 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1606 | body = ast_for_body(c, CHILD(n, name_i + 3), &docstring); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1607 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1608 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1609 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1610 | if (is_async) |
| 1611 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1612 | docstring, LINENO(n), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1613 | n->n_col_offset, c->c_arena); |
| 1614 | else |
| 1615 | return FunctionDef(name, args, body, decorator_seq, returns, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1616 | docstring, LINENO(n), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1617 | n->n_col_offset, c->c_arena); |
| 1618 | } |
| 1619 | |
| 1620 | static stmt_ty |
| 1621 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1622 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1623 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1624 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1625 | REQ(CHILD(n, 0), NAME); |
| 1626 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1627 | REQ(CHILD(n, 1), funcdef); |
| 1628 | |
| 1629 | return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, |
| 1630 | 1 /* is_async */); |
| 1631 | } |
| 1632 | |
| 1633 | static stmt_ty |
| 1634 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1635 | { |
| 1636 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1637 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1638 | 0 /* is_async */); |
| 1639 | } |
| 1640 | |
| 1641 | |
| 1642 | static stmt_ty |
| 1643 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1644 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1645 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1646 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1647 | REQ(CHILD(n, 0), NAME); |
| 1648 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1649 | |
| 1650 | switch (TYPE(CHILD(n, 1))) { |
| 1651 | case funcdef: |
| 1652 | return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, |
| 1653 | 1 /* is_async */); |
| 1654 | case with_stmt: |
| 1655 | return ast_for_with_stmt(c, CHILD(n, 1), |
| 1656 | 1 /* is_async */); |
| 1657 | |
| 1658 | case for_stmt: |
| 1659 | return ast_for_for_stmt(c, CHILD(n, 1), |
| 1660 | 1 /* is_async */); |
| 1661 | |
| 1662 | default: |
| 1663 | PyErr_Format(PyExc_SystemError, |
| 1664 | "invalid async stament: %s", |
| 1665 | STR(CHILD(n, 1))); |
| 1666 | return NULL; |
| 1667 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1670 | static stmt_ty |
| 1671 | ast_for_decorated(struct compiling *c, const node *n) |
| 1672 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1673 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1674 | stmt_ty thing = NULL; |
| 1675 | asdl_seq *decorator_seq = NULL; |
| 1676 | |
| 1677 | REQ(n, decorated); |
| 1678 | |
| 1679 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1680 | if (!decorator_seq) |
| 1681 | return NULL; |
| 1682 | |
| 1683 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1684 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1686 | |
| 1687 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1688 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1689 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1690 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1691 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1692 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1693 | } |
Christian Heimes | 09aaa88 | 2008-02-23 15:01:06 +0000 | [diff] [blame] | 1694 | /* we count the decorators in when talking about the class' or |
| 1695 | * function's line number */ |
| 1696 | if (thing) { |
| 1697 | thing->lineno = LINENO(n); |
| 1698 | thing->col_offset = n->n_col_offset; |
| 1699 | } |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1700 | return thing; |
| 1701 | } |
| 1702 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1703 | static expr_ty |
| 1704 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1705 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1706 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1707 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | arguments_ty args; |
| 1709 | expr_ty expression; |
| 1710 | |
| 1711 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1712 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1713 | if (!args) |
| 1714 | return NULL; |
| 1715 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1716 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1717 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1718 | } |
| 1719 | else { |
| 1720 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1721 | if (!args) |
| 1722 | return NULL; |
| 1723 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1724 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1725 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1728 | return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1731 | static expr_ty |
| 1732 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1733 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1735 | expr_ty expression, body, orelse; |
| 1736 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1737 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1738 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1739 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1740 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1741 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1742 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1743 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1744 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1745 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1746 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1747 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1748 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1751 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1752 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1753 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1754 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1755 | */ |
| 1756 | |
| 1757 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1758 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1759 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1760 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1761 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1762 | count_comp_for: |
| 1763 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1764 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1765 | if (NCH(n) == 2) { |
| 1766 | REQ(CHILD(n, 0), NAME); |
| 1767 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1768 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1769 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1770 | else if (NCH(n) == 1) { |
| 1771 | n = CHILD(n, 0); |
| 1772 | } |
| 1773 | else { |
| 1774 | goto error; |
| 1775 | } |
| 1776 | if (NCH(n) == (5)) { |
| 1777 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1778 | } |
| 1779 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1780 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1781 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1782 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1783 | REQ(n, comp_iter); |
| 1784 | n = CHILD(n, 0); |
| 1785 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1786 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1787 | else if (TYPE(n) == comp_if) { |
| 1788 | if (NCH(n) == 3) { |
| 1789 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1790 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1791 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1792 | else |
| 1793 | return n_fors; |
| 1794 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1795 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1796 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1797 | /* Should never be reached */ |
| 1798 | PyErr_SetString(PyExc_SystemError, |
| 1799 | "logic error in count_comp_fors"); |
| 1800 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1803 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1804 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1805 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1806 | */ |
| 1807 | |
| 1808 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1809 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1810 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1811 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1812 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1813 | while (1) { |
| 1814 | REQ(n, comp_iter); |
| 1815 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1816 | return n_ifs; |
| 1817 | n = CHILD(n, 0); |
| 1818 | REQ(n, comp_if); |
| 1819 | n_ifs++; |
| 1820 | if (NCH(n) == 2) |
| 1821 | return n_ifs; |
| 1822 | n = CHILD(n, 2); |
| 1823 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1826 | static asdl_seq * |
| 1827 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1828 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1829 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1830 | asdl_seq *comps; |
| 1831 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1832 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | if (n_fors == -1) |
| 1834 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1835 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1836 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1837 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1838 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1839 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1840 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1841 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1842 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1843 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1844 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1845 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1846 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1848 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1850 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1851 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1852 | REQ(CHILD(n, 0), NAME); |
| 1853 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1854 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1855 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1856 | else { |
| 1857 | sync_n = CHILD(n, 0); |
| 1858 | } |
| 1859 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1860 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1861 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1862 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1863 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1864 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1865 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1866 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1867 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1868 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1869 | /* Check the # of children rather than the length of t, since |
| 1870 | (x for x, in ...) has 1 element in t, but still requires a Tuple. */ |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1871 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1872 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1873 | comp = comprehension(first, expression, NULL, |
| 1874 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1875 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1876 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1877 | first->col_offset, c->c_arena), |
| 1878 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1879 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1880 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1881 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1882 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1883 | int j, n_ifs; |
| 1884 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 1886 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1887 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1888 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1889 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1891 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1892 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1893 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1894 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1895 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1896 | REQ(n, comp_iter); |
| 1897 | n = CHILD(n, 0); |
| 1898 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1900 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1901 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1902 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1903 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1904 | if (NCH(n) == 3) |
| 1905 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1907 | /* on exit, must guarantee that n is a comp_for */ |
| 1908 | if (TYPE(n) == comp_iter) |
| 1909 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1910 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1911 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1912 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1913 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1914 | return comps; |
| 1915 | } |
| 1916 | |
| 1917 | static expr_ty |
| 1918 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1919 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1920 | /* testlist_comp: (test|star_expr) |
| 1921 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1922 | expr_ty elt; |
| 1923 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1924 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1926 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1928 | ch = CHILD(n, 0); |
| 1929 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1930 | if (!elt) |
| 1931 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1932 | if (elt->kind == Starred_kind) { |
| 1933 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1934 | return NULL; |
| 1935 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1937 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1938 | if (!comps) |
| 1939 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1940 | |
| 1941 | if (type == COMP_GENEXP) |
| 1942 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1943 | else if (type == COMP_LISTCOMP) |
| 1944 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1945 | else if (type == COMP_SETCOMP) |
| 1946 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1947 | else |
| 1948 | /* Should never happen */ |
| 1949 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1952 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1953 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1954 | * elements. Iff successful, nonzero is returned. |
| 1955 | */ |
| 1956 | static int |
| 1957 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1958 | expr_ty *key, expr_ty *value) |
| 1959 | { |
| 1960 | expr_ty expression; |
| 1961 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1962 | assert(NCH(n) - *i >= 2); |
| 1963 | |
| 1964 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1965 | if (!expression) |
| 1966 | return 0; |
| 1967 | *key = NULL; |
| 1968 | *value = expression; |
| 1969 | |
| 1970 | *i += 2; |
| 1971 | } |
| 1972 | else { |
| 1973 | assert(NCH(n) - *i >= 3); |
| 1974 | |
| 1975 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1976 | if (!expression) |
| 1977 | return 0; |
| 1978 | *key = expression; |
| 1979 | |
| 1980 | REQ(CHILD(n, *i + 1), COLON); |
| 1981 | |
| 1982 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1983 | if (!expression) |
| 1984 | return 0; |
| 1985 | *value = expression; |
| 1986 | |
| 1987 | *i += 3; |
| 1988 | } |
| 1989 | return 1; |
| 1990 | } |
| 1991 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1992 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1993 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1994 | { |
| 1995 | expr_ty key, value; |
| 1996 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1997 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1999 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2000 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2001 | assert(key); |
| 2002 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2004 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2005 | if (!comps) |
| 2006 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2008 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 2009 | } |
| 2010 | |
| 2011 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2012 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2013 | { |
| 2014 | int i; |
| 2015 | int j; |
| 2016 | int size; |
| 2017 | asdl_seq *keys, *values; |
| 2018 | |
| 2019 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2020 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2021 | if (!keys) |
| 2022 | return NULL; |
| 2023 | |
| 2024 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2025 | if (!values) |
| 2026 | return NULL; |
| 2027 | |
| 2028 | j = 0; |
| 2029 | for (i = 0; i < NCH(n); i++) { |
| 2030 | expr_ty key, value; |
| 2031 | |
| 2032 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2033 | return NULL; |
| 2034 | asdl_seq_SET(keys, j, key); |
| 2035 | asdl_seq_SET(values, j, value); |
| 2036 | |
| 2037 | j++; |
| 2038 | } |
| 2039 | keys->size = j; |
| 2040 | values->size = j; |
| 2041 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2042 | } |
| 2043 | |
| 2044 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2045 | ast_for_genexp(struct compiling *c, const node *n) |
| 2046 | { |
| 2047 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2048 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | static expr_ty |
| 2052 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2053 | { |
| 2054 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2055 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | static expr_ty |
| 2059 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2060 | { |
| 2061 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2062 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2065 | static expr_ty |
| 2066 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2067 | { |
| 2068 | int i; |
| 2069 | int size; |
| 2070 | asdl_seq *elts; |
| 2071 | |
| 2072 | assert(TYPE(n) == (dictorsetmaker)); |
| 2073 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2074 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2075 | if (!elts) |
| 2076 | return NULL; |
| 2077 | for (i = 0; i < NCH(n); i += 2) { |
| 2078 | expr_ty expression; |
| 2079 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2080 | if (!expression) |
| 2081 | return NULL; |
| 2082 | asdl_seq_SET(elts, i / 2, expression); |
| 2083 | } |
| 2084 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2085 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2086 | |
| 2087 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2088 | ast_for_atom(struct compiling *c, const node *n) |
| 2089 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2090 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2091 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2092 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2093 | */ |
| 2094 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2096 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2097 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2098 | PyObject *name; |
| 2099 | const char *s = STR(ch); |
| 2100 | size_t len = strlen(s); |
| 2101 | if (len >= 4 && len <= 5) { |
| 2102 | if (!strcmp(s, "None")) |
| 2103 | return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
| 2104 | if (!strcmp(s, "True")) |
| 2105 | return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
| 2106 | if (!strcmp(s, "False")) |
| 2107 | return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
| 2108 | } |
| 2109 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2110 | if (!name) |
| 2111 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2112 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2113 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2114 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2115 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2116 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2117 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2118 | const char *errtype = NULL; |
| 2119 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2120 | errtype = "unicode error"; |
| 2121 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2122 | errtype = "value error"; |
| 2123 | if (errtype) { |
| 2124 | char buf[128]; |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2125 | const char *s = NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2126 | PyObject *type, *value, *tback, *errstr; |
| 2127 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2128 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2129 | if (errstr) |
| 2130 | s = PyUnicode_AsUTF8(errstr); |
| 2131 | if (s) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2132 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2133 | } else { |
Victor Stinner | 00723e0 | 2015-09-03 12:57:11 +0200 | [diff] [blame] | 2134 | PyErr_Clear(); |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2135 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2136 | } |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2137 | Py_XDECREF(errstr); |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2138 | ast_error(c, n, buf); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2139 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2140 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2141 | Py_XDECREF(tback); |
| 2142 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2143 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2144 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2145 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2146 | } |
| 2147 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2148 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2149 | if (!pynum) |
| 2150 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2151 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2152 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2153 | Py_DECREF(pynum); |
| 2154 | return NULL; |
| 2155 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2156 | return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2157 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2158 | case ELLIPSIS: /* Ellipsis */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2159 | return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2160 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2161 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2163 | if (TYPE(ch) == RPAR) |
| 2164 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2166 | if (TYPE(ch) == yield_expr) |
| 2167 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2170 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2171 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2172 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2173 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2174 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2175 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2177 | if (TYPE(ch) == RSQB) |
| 2178 | return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2180 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2181 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2182 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2183 | if (!elts) |
| 2184 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2185 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2186 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2187 | } |
| 2188 | else |
| 2189 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2190 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2191 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2192 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2193 | * ((test | '*' test) |
| 2194 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2195 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2196 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2197 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2198 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2199 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2200 | } |
| 2201 | else { |
| 2202 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2203 | if (NCH(ch) == 1 || |
| 2204 | (NCH(ch) > 1 && |
| 2205 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2206 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2207 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2208 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2209 | else if (NCH(ch) > 1 && |
| 2210 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2211 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2212 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2213 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2214 | else if (NCH(ch) > 3 - is_dict && |
| 2215 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2216 | /* It's a dictionary comprehension. */ |
| 2217 | if (is_dict) { |
| 2218 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2219 | "dict comprehension"); |
| 2220 | return NULL; |
| 2221 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2222 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2223 | } |
| 2224 | else { |
| 2225 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2226 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2227 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2228 | if (res) { |
| 2229 | res->lineno = LINENO(n); |
| 2230 | res->col_offset = n->n_col_offset; |
| 2231 | } |
| 2232 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2233 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2234 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2235 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2236 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2237 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | static slice_ty |
| 2242 | ast_for_slice(struct compiling *c, const node *n) |
| 2243 | { |
| 2244 | node *ch; |
| 2245 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2246 | |
| 2247 | REQ(n, subscript); |
| 2248 | |
| 2249 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2250 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2251 | sliceop: ':' [test] |
| 2252 | */ |
| 2253 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2254 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2255 | /* 'step' variable hold no significance in terms of being used over |
| 2256 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2258 | if (!step) |
| 2259 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2261 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2265 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2266 | if (!lower) |
| 2267 | return NULL; |
| 2268 | } |
| 2269 | |
| 2270 | /* If there's an upper bound it's in the second or third position. */ |
| 2271 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2272 | if (NCH(n) > 1) { |
| 2273 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2274 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2275 | if (TYPE(n2) == test) { |
| 2276 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2277 | if (!upper) |
| 2278 | return NULL; |
| 2279 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2280 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2281 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2282 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2283 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2284 | if (TYPE(n2) == test) { |
| 2285 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2286 | if (!upper) |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | ch = CHILD(n, NCH(n) - 1); |
| 2292 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2293 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2294 | ch = CHILD(ch, 1); |
| 2295 | if (TYPE(ch) == test) { |
| 2296 | step = ast_for_expr(c, ch); |
| 2297 | if (!step) |
| 2298 | return NULL; |
| 2299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2303 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | static expr_ty |
| 2307 | ast_for_binop(struct compiling *c, const node *n) |
| 2308 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2309 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2311 | BinOp(BinOp(A, op, B), op, C). |
| 2312 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2313 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2314 | int i, nops; |
| 2315 | expr_ty expr1, expr2, result; |
| 2316 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2317 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2318 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2319 | if (!expr1) |
| 2320 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2321 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2322 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2323 | if (!expr2) |
| 2324 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2325 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2326 | newoperator = get_operator(CHILD(n, 1)); |
| 2327 | if (!newoperator) |
| 2328 | return NULL; |
| 2329 | |
| 2330 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2331 | c->c_arena); |
| 2332 | if (!result) |
| 2333 | return NULL; |
| 2334 | |
| 2335 | nops = (NCH(n) - 1) / 2; |
| 2336 | for (i = 1; i < nops; i++) { |
| 2337 | expr_ty tmp_result, tmp; |
| 2338 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2339 | |
| 2340 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2341 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2342 | return NULL; |
| 2343 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2344 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2345 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2346 | return NULL; |
| 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2349 | LINENO(next_oper), next_oper->n_col_offset, |
| 2350 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2352 | return NULL; |
| 2353 | result = tmp_result; |
| 2354 | } |
| 2355 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2358 | static expr_ty |
| 2359 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2362 | subscriptlist: subscript (',' subscript)* [','] |
| 2363 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2364 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2365 | REQ(n, trailer); |
| 2366 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2367 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2368 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2369 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2370 | else |
| 2371 | return ast_for_call(c, CHILD(n, 1), left_expr); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2372 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2373 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2374 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2375 | if (!attr_id) |
| 2376 | return NULL; |
| 2377 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2378 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2379 | } |
| 2380 | else { |
| 2381 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2382 | REQ(CHILD(n, 2), RSQB); |
| 2383 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2384 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2385 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2386 | if (!slc) |
| 2387 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2388 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2389 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2390 | } |
| 2391 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2393 | by treating the sequence as a tuple literal if there are |
| 2394 | no slice features. |
| 2395 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2396 | int j; |
| 2397 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2398 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2399 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2400 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2401 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2402 | if (!slices) |
| 2403 | return NULL; |
| 2404 | for (j = 0; j < NCH(n); j += 2) { |
| 2405 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2406 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2407 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2408 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2409 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2410 | asdl_seq_SET(slices, j / 2, slc); |
| 2411 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2412 | if (!simple) { |
| 2413 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2414 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2415 | } |
| 2416 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2417 | elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2418 | if (!elts) |
| 2419 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2420 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2421 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2422 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2423 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2424 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2425 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2426 | if (!e) |
| 2427 | return NULL; |
| 2428 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2429 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2430 | } |
| 2431 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2435 | ast_for_factor(struct compiling *c, const node *n) |
| 2436 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2437 | expr_ty expression; |
| 2438 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2439 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2440 | if (!expression) |
| 2441 | return NULL; |
| 2442 | |
| 2443 | switch (TYPE(CHILD(n, 0))) { |
| 2444 | case PLUS: |
| 2445 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2446 | c->c_arena); |
| 2447 | case MINUS: |
| 2448 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2449 | c->c_arena); |
| 2450 | case TILDE: |
| 2451 | return UnaryOp(Invert, expression, LINENO(n), |
| 2452 | n->n_col_offset, c->c_arena); |
| 2453 | } |
| 2454 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2455 | TYPE(CHILD(n, 0))); |
| 2456 | return NULL; |
| 2457 | } |
| 2458 | |
| 2459 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2460 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2461 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2462 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2463 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2464 | |
| 2465 | REQ(n, atom_expr); |
| 2466 | nch = NCH(n); |
| 2467 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 2468 | if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2469 | start = 1; |
| 2470 | assert(nch > 1); |
| 2471 | } |
| 2472 | |
| 2473 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2474 | if (!e) |
| 2475 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2476 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2477 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2478 | if (start && nch == 2) { |
| 2479 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2480 | } |
| 2481 | |
| 2482 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2483 | node *ch = CHILD(n, i); |
| 2484 | if (TYPE(ch) != trailer) |
| 2485 | break; |
| 2486 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2487 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2488 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2489 | tmp->lineno = e->lineno; |
| 2490 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2491 | e = tmp; |
| 2492 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2493 | |
| 2494 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 2495 | /* there was an 'await' */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2496 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2497 | } |
| 2498 | else { |
| 2499 | return e; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | static expr_ty |
| 2504 | ast_for_power(struct compiling *c, const node *n) |
| 2505 | { |
| 2506 | /* power: atom trailer* ('**' factor)* |
| 2507 | */ |
| 2508 | expr_ty e; |
| 2509 | REQ(n, power); |
| 2510 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2511 | if (!e) |
| 2512 | return NULL; |
| 2513 | if (NCH(n) == 1) |
| 2514 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2515 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2516 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2517 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2518 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2519 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2520 | } |
| 2521 | return e; |
| 2522 | } |
| 2523 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2524 | static expr_ty |
| 2525 | ast_for_starred(struct compiling *c, const node *n) |
| 2526 | { |
| 2527 | expr_ty tmp; |
| 2528 | REQ(n, star_expr); |
| 2529 | |
| 2530 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2531 | if (!tmp) |
| 2532 | return NULL; |
| 2533 | |
| 2534 | /* The Load context is changed later. */ |
| 2535 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2536 | } |
| 2537 | |
| 2538 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2539 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2540 | */ |
| 2541 | |
| 2542 | static expr_ty |
| 2543 | ast_for_expr(struct compiling *c, const node *n) |
| 2544 | { |
| 2545 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2546 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2547 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2549 | and_test: not_test ('and' not_test)* |
| 2550 | not_test: 'not' not_test | comparison |
| 2551 | comparison: expr (comp_op expr)* |
| 2552 | expr: xor_expr ('|' xor_expr)* |
| 2553 | xor_expr: and_expr ('^' and_expr)* |
| 2554 | and_expr: shift_expr ('&' shift_expr)* |
| 2555 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2556 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2557 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2558 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2559 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame^] | 2560 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2561 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2562 | */ |
| 2563 | |
| 2564 | asdl_seq *seq; |
| 2565 | int i; |
| 2566 | |
| 2567 | loop: |
| 2568 | switch (TYPE(n)) { |
| 2569 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2570 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2571 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2572 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2573 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2574 | else if (NCH(n) > 1) |
| 2575 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2576 | /* Fallthrough */ |
| 2577 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2578 | case and_test: |
| 2579 | if (NCH(n) == 1) { |
| 2580 | n = CHILD(n, 0); |
| 2581 | goto loop; |
| 2582 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2583 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2584 | if (!seq) |
| 2585 | return NULL; |
| 2586 | for (i = 0; i < NCH(n); i += 2) { |
| 2587 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2588 | if (!e) |
| 2589 | return NULL; |
| 2590 | asdl_seq_SET(seq, i / 2, e); |
| 2591 | } |
| 2592 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2593 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2594 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2595 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2596 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2597 | case not_test: |
| 2598 | if (NCH(n) == 1) { |
| 2599 | n = CHILD(n, 0); |
| 2600 | goto loop; |
| 2601 | } |
| 2602 | else { |
| 2603 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2604 | if (!expression) |
| 2605 | return NULL; |
| 2606 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2607 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2608 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2609 | } |
| 2610 | case comparison: |
| 2611 | if (NCH(n) == 1) { |
| 2612 | n = CHILD(n, 0); |
| 2613 | goto loop; |
| 2614 | } |
| 2615 | else { |
| 2616 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2617 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2618 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2619 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2620 | if (!ops) |
| 2621 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2622 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2623 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2624 | return NULL; |
| 2625 | } |
| 2626 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2627 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2628 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2629 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2630 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2631 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2632 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2633 | |
| 2634 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2635 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2636 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2637 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2639 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 | asdl_seq_SET(cmps, i / 2, expression); |
| 2641 | } |
| 2642 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2643 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2644 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2645 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2646 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2647 | return Compare(expression, ops, cmps, LINENO(n), |
| 2648 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2649 | } |
| 2650 | break; |
| 2651 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2652 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | /* The next five cases all handle BinOps. The main body of code |
| 2655 | is the same in each case, but the switch turned inside out to |
| 2656 | reuse the code for each type of operator. |
| 2657 | */ |
| 2658 | case expr: |
| 2659 | case xor_expr: |
| 2660 | case and_expr: |
| 2661 | case shift_expr: |
| 2662 | case arith_expr: |
| 2663 | case term: |
| 2664 | if (NCH(n) == 1) { |
| 2665 | n = CHILD(n, 0); |
| 2666 | goto loop; |
| 2667 | } |
| 2668 | return ast_for_binop(c, n); |
| 2669 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2670 | node *an = NULL; |
| 2671 | node *en = NULL; |
| 2672 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2673 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2674 | if (NCH(n) > 1) |
| 2675 | an = CHILD(n, 1); /* yield_arg */ |
| 2676 | if (an) { |
| 2677 | en = CHILD(an, NCH(an) - 1); |
| 2678 | if (NCH(an) == 2) { |
| 2679 | is_from = 1; |
| 2680 | exp = ast_for_expr(c, en); |
| 2681 | } |
| 2682 | else |
| 2683 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2684 | if (!exp) |
| 2685 | return NULL; |
| 2686 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2687 | if (is_from) |
| 2688 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2689 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2690 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2691 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | if (NCH(n) == 1) { |
| 2693 | n = CHILD(n, 0); |
| 2694 | goto loop; |
| 2695 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2696 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2697 | case power: |
| 2698 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2700 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2701 | return NULL; |
| 2702 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2703 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | return NULL; |
| 2705 | } |
| 2706 | |
| 2707 | static expr_ty |
| 2708 | ast_for_call(struct compiling *c, const node *n, expr_ty func) |
| 2709 | { |
| 2710 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2711 | arglist: argument (',' argument)* [','] |
| 2712 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2713 | */ |
| 2714 | |
| 2715 | int i, nargs, nkeywords, ngens; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2716 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2717 | asdl_seq *args; |
| 2718 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2719 | |
| 2720 | REQ(n, arglist); |
| 2721 | |
| 2722 | nargs = 0; |
| 2723 | nkeywords = 0; |
| 2724 | ngens = 0; |
| 2725 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2726 | node *ch = CHILD(n, i); |
| 2727 | if (TYPE(ch) == argument) { |
| 2728 | if (NCH(ch) == 1) |
| 2729 | nargs++; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2730 | else if (TYPE(CHILD(ch, 1)) == comp_for) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2731 | ngens++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2732 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2733 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2734 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2735 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2736 | nkeywords++; |
| 2737 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 | } |
| 2739 | if (ngens > 1 || (ngens && (nargs || nkeywords))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2740 | ast_error(c, n, "Generator expression must be parenthesized " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2741 | "if not sole argument"); |
| 2742 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2745 | args = _Py_asdl_seq_new(nargs + ngens, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2746 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2747 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2748 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2749 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2750 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2751 | |
| 2752 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2753 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2754 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2755 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2756 | node *ch = CHILD(n, i); |
| 2757 | if (TYPE(ch) == argument) { |
| 2758 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2759 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2760 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2761 | /* a positional argument */ |
| 2762 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2763 | if (ndoublestars) { |
| 2764 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2765 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2766 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2767 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2768 | else { |
| 2769 | ast_error(c, chch, |
| 2770 | "positional argument follows " |
| 2771 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2772 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2773 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2774 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2775 | e = ast_for_expr(c, chch); |
| 2776 | if (!e) |
| 2777 | return NULL; |
| 2778 | asdl_seq_SET(args, nargs++, e); |
| 2779 | } |
| 2780 | else if (TYPE(chch) == STAR) { |
| 2781 | /* an iterable argument unpacking */ |
| 2782 | expr_ty starred; |
| 2783 | if (ndoublestars) { |
| 2784 | ast_error(c, chch, |
| 2785 | "iterable argument unpacking follows " |
| 2786 | "keyword argument unpacking"); |
| 2787 | return NULL; |
| 2788 | } |
| 2789 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2790 | if (!e) |
| 2791 | return NULL; |
| 2792 | starred = Starred(e, Load, LINENO(chch), |
| 2793 | chch->n_col_offset, |
| 2794 | c->c_arena); |
| 2795 | if (!starred) |
| 2796 | return NULL; |
| 2797 | asdl_seq_SET(args, nargs++, starred); |
| 2798 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2799 | } |
| 2800 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2801 | /* a keyword argument unpacking */ |
| 2802 | keyword_ty kw; |
| 2803 | i++; |
| 2804 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2805 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2806 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2807 | kw = keyword(NULL, e, c->c_arena); |
| 2808 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2809 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2811 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2812 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2813 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2814 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2815 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2816 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2818 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2819 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2820 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2821 | identifier key, tmp; |
| 2822 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2823 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2824 | /* chch is test, but must be an identifier? */ |
| 2825 | e = ast_for_expr(c, chch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2826 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2827 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2828 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2829 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 2830 | * SF bug 132313 points out that complaining about a keyword |
| 2831 | * then is very confusing. |
| 2832 | */ |
| 2833 | if (e->kind == Lambda_kind) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2834 | ast_error(c, chch, |
| 2835 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2836 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2837 | } |
| 2838 | else if (e->kind != Name_kind) { |
| 2839 | ast_error(c, chch, |
| 2840 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2841 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2842 | } |
| 2843 | else if (forbidden_name(c, e->v.Name.id, ch, 1)) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2844 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2846 | key = e->v.Name.id; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2847 | for (k = 0; k < nkeywords; k++) { |
| 2848 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2849 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2850 | ast_error(c, chch, |
| 2851 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2852 | return NULL; |
| 2853 | } |
| 2854 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2855 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2857 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2858 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2859 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2860 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2861 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2862 | } |
| 2863 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2866 | return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2870 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2871 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2872 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2873 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2874 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2875 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2876 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2877 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2878 | } |
| 2879 | else { |
| 2880 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2881 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2882 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2884 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2885 | else { |
| 2886 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2887 | if (!tmp) |
| 2888 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2889 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2893 | static stmt_ty |
| 2894 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2895 | { |
| 2896 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2897 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2898 | ('=' (yield_expr|testlist_star_expr))*) |
| 2899 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2900 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2901 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2902 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2903 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | */ |
| 2905 | |
| 2906 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2907 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2908 | if (!e) |
| 2909 | return NULL; |
| 2910 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2911 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2912 | } |
| 2913 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2914 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2915 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2916 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2917 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2918 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2919 | if (!expr1) |
| 2920 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2921 | if(!set_context(c, expr1, Store, ch)) |
| 2922 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2923 | /* set_context checks that most expressions are not the left side. |
| 2924 | Augmented assignments can only have a name, a subscript, or an |
| 2925 | attribute on the left, though, so we have to explicitly check for |
| 2926 | those. */ |
| 2927 | switch (expr1->kind) { |
| 2928 | case Name_kind: |
| 2929 | case Attribute_kind: |
| 2930 | case Subscript_kind: |
| 2931 | break; |
| 2932 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2933 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2934 | return NULL; |
| 2935 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2936 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2937 | ch = CHILD(n, 2); |
| 2938 | if (TYPE(ch) == testlist) |
| 2939 | expr2 = ast_for_testlist(c, ch); |
| 2940 | else |
| 2941 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2942 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | return NULL; |
| 2944 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2945 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2946 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | return NULL; |
| 2948 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2949 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2950 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2951 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2952 | expr_ty expr1, expr2, expr3; |
| 2953 | node *ch = CHILD(n, 0); |
| 2954 | node *deep, *ann = CHILD(n, 1); |
| 2955 | int simple = 1; |
| 2956 | |
| 2957 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2958 | deep = ch; |
| 2959 | while (NCH(deep) == 1) { |
| 2960 | deep = CHILD(deep, 0); |
| 2961 | } |
| 2962 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2963 | simple = 0; |
| 2964 | } |
| 2965 | expr1 = ast_for_testlist(c, ch); |
| 2966 | if (!expr1) { |
| 2967 | return NULL; |
| 2968 | } |
| 2969 | switch (expr1->kind) { |
| 2970 | case Name_kind: |
| 2971 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2972 | return NULL; |
| 2973 | } |
| 2974 | expr1->v.Name.ctx = Store; |
| 2975 | break; |
| 2976 | case Attribute_kind: |
| 2977 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | expr1->v.Attribute.ctx = Store; |
| 2981 | break; |
| 2982 | case Subscript_kind: |
| 2983 | expr1->v.Subscript.ctx = Store; |
| 2984 | break; |
| 2985 | case List_kind: |
| 2986 | ast_error(c, ch, |
| 2987 | "only single target (not list) can be annotated"); |
| 2988 | return NULL; |
| 2989 | case Tuple_kind: |
| 2990 | ast_error(c, ch, |
| 2991 | "only single target (not tuple) can be annotated"); |
| 2992 | return NULL; |
| 2993 | default: |
| 2994 | ast_error(c, ch, |
| 2995 | "illegal target for annotation"); |
| 2996 | return NULL; |
| 2997 | } |
| 2998 | |
| 2999 | if (expr1->kind != Name_kind) { |
| 3000 | simple = 0; |
| 3001 | } |
| 3002 | ch = CHILD(ann, 1); |
| 3003 | expr2 = ast_for_expr(c, ch); |
| 3004 | if (!expr2) { |
| 3005 | return NULL; |
| 3006 | } |
| 3007 | if (NCH(ann) == 2) { |
| 3008 | return AnnAssign(expr1, expr2, NULL, simple, |
| 3009 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3010 | } |
| 3011 | else { |
| 3012 | ch = CHILD(ann, 3); |
| 3013 | expr3 = ast_for_expr(c, ch); |
| 3014 | if (!expr3) { |
| 3015 | return NULL; |
| 3016 | } |
| 3017 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3018 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3019 | } |
| 3020 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3021 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3022 | int i; |
| 3023 | asdl_seq *targets; |
| 3024 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3025 | expr_ty expression; |
| 3026 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3027 | /* a normal assignment */ |
| 3028 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3029 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3030 | if (!targets) |
| 3031 | return NULL; |
| 3032 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3033 | expr_ty e; |
| 3034 | node *ch = CHILD(n, i); |
| 3035 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3036 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3037 | return NULL; |
| 3038 | } |
| 3039 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3041 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3042 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3043 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3044 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3045 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3046 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3047 | asdl_seq_SET(targets, i / 2, e); |
| 3048 | } |
| 3049 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3050 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3051 | expression = ast_for_testlist(c, value); |
| 3052 | else |
| 3053 | expression = ast_for_expr(c, value); |
| 3054 | if (!expression) |
| 3055 | return NULL; |
| 3056 | return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3060 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3061 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3062 | ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3063 | { |
| 3064 | asdl_seq *seq; |
| 3065 | int i; |
| 3066 | expr_ty e; |
| 3067 | |
| 3068 | REQ(n, exprlist); |
| 3069 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3070 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3071 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3072 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3073 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3074 | e = ast_for_expr(c, CHILD(n, i)); |
| 3075 | if (!e) |
| 3076 | return NULL; |
| 3077 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3078 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3079 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3080 | } |
| 3081 | return seq; |
| 3082 | } |
| 3083 | |
| 3084 | static stmt_ty |
| 3085 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3086 | { |
| 3087 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3089 | /* del_stmt: 'del' exprlist */ |
| 3090 | REQ(n, del_stmt); |
| 3091 | |
| 3092 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3093 | if (!expr_list) |
| 3094 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3095 | return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | static stmt_ty |
| 3099 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3100 | { |
| 3101 | /* |
| 3102 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3103 | | yield_stmt |
| 3104 | break_stmt: 'break' |
| 3105 | continue_stmt: 'continue' |
| 3106 | return_stmt: 'return' [testlist] |
| 3107 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3108 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3109 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3110 | */ |
| 3111 | node *ch; |
| 3112 | |
| 3113 | REQ(n, flow_stmt); |
| 3114 | ch = CHILD(n, 0); |
| 3115 | switch (TYPE(ch)) { |
| 3116 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3117 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3118 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3119 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3120 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3121 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3122 | if (!exp) |
| 3123 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3124 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3125 | } |
| 3126 | case return_stmt: |
| 3127 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3128 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3129 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3130 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3131 | if (!expression) |
| 3132 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3133 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3134 | } |
| 3135 | case raise_stmt: |
| 3136 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3137 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3138 | else if (NCH(ch) >= 2) { |
| 3139 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3140 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3141 | if (!expression) |
| 3142 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3143 | if (NCH(ch) == 4) { |
| 3144 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3145 | if (!cause) |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3149 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3150 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3151 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3152 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3153 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3154 | return NULL; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3159 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3160 | { |
| 3161 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3162 | import_as_name: NAME ['as' NAME] |
| 3163 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3164 | dotted_name: NAME ('.' NAME)* |
| 3165 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3166 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3167 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3168 | loop: |
| 3169 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3170 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3171 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3172 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3173 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3174 | if (!name) |
| 3175 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3176 | if (NCH(n) == 3) { |
| 3177 | node *str_node = CHILD(n, 2); |
| 3178 | str = NEW_IDENTIFIER(str_node); |
| 3179 | if (!str) |
| 3180 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3181 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3182 | return NULL; |
| 3183 | } |
| 3184 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3185 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3186 | return NULL; |
| 3187 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3188 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3189 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3190 | case dotted_as_name: |
| 3191 | if (NCH(n) == 1) { |
| 3192 | n = CHILD(n, 0); |
| 3193 | goto loop; |
| 3194 | } |
| 3195 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3196 | node *asname_node = CHILD(n, 2); |
| 3197 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3198 | if (!a) |
| 3199 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3200 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3201 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3202 | if (!a->asname) |
| 3203 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3204 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3205 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3206 | return a; |
| 3207 | } |
| 3208 | break; |
| 3209 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3210 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3211 | node *name_node = CHILD(n, 0); |
| 3212 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3213 | if (!name) |
| 3214 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3215 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3216 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3217 | return alias(name, NULL, c->c_arena); |
| 3218 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3219 | else { |
| 3220 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3221 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3222 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3223 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3225 | |
| 3226 | len = 0; |
| 3227 | for (i = 0; i < NCH(n); i += 2) |
| 3228 | /* length of string plus one for the dot */ |
| 3229 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3230 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3231 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3232 | if (!str) |
| 3233 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3234 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3235 | if (!s) |
| 3236 | return NULL; |
| 3237 | for (i = 0; i < NCH(n); i += 2) { |
| 3238 | char *sch = STR(CHILD(n, i)); |
| 3239 | strcpy(s, STR(CHILD(n, i))); |
| 3240 | s += strlen(sch); |
| 3241 | *s++ = '.'; |
| 3242 | } |
| 3243 | --s; |
| 3244 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3246 | PyBytes_GET_SIZE(str), |
| 3247 | NULL); |
| 3248 | Py_DECREF(str); |
| 3249 | if (!uni) |
| 3250 | return NULL; |
| 3251 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3252 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3253 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3254 | Py_DECREF(str); |
| 3255 | return NULL; |
| 3256 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3257 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3258 | } |
| 3259 | break; |
| 3260 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3261 | str = PyUnicode_InternFromString("*"); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3262 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3263 | Py_DECREF(str); |
| 3264 | return NULL; |
| 3265 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3266 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3267 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3268 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3269 | "unexpected import name: %d", TYPE(n)); |
| 3270 | return NULL; |
| 3271 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3272 | |
| 3273 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3274 | return NULL; |
| 3275 | } |
| 3276 | |
| 3277 | static stmt_ty |
| 3278 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3279 | { |
| 3280 | /* |
| 3281 | import_stmt: import_name | import_from |
| 3282 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3283 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3284 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3285 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3286 | int lineno; |
| 3287 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | int i; |
| 3289 | asdl_seq *aliases; |
| 3290 | |
| 3291 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3292 | lineno = LINENO(n); |
| 3293 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3294 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3295 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3296 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3297 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3298 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3299 | if (!aliases) |
| 3300 | return NULL; |
| 3301 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3302 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3303 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3304 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3305 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3306 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3307 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3308 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3309 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3310 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3311 | int idx, ndots = 0; |
| 3312 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3313 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3315 | /* Count the number of dots (for relative imports) and check for the |
| 3316 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3317 | for (idx = 1; idx < NCH(n); idx++) { |
| 3318 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3319 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3320 | if (!mod) |
| 3321 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3322 | idx++; |
| 3323 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3324 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3326 | ndots += 3; |
| 3327 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3328 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3329 | break; |
| 3330 | } |
| 3331 | ndots++; |
| 3332 | } |
| 3333 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3334 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3335 | case STAR: |
| 3336 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3337 | n = CHILD(n, idx); |
| 3338 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3339 | break; |
| 3340 | case LPAR: |
| 3341 | /* from ... import (x, y, z) */ |
| 3342 | n = CHILD(n, idx + 1); |
| 3343 | n_children = NCH(n); |
| 3344 | break; |
| 3345 | case import_as_names: |
| 3346 | /* from ... import x, y, z */ |
| 3347 | n = CHILD(n, idx); |
| 3348 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3349 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3350 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3351 | " surrounding parentheses"); |
| 3352 | return NULL; |
| 3353 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3354 | break; |
| 3355 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3356 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3357 | return NULL; |
| 3358 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3359 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3360 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3361 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3362 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3363 | |
| 3364 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3365 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3366 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3367 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3368 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3369 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3370 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3371 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3372 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3373 | alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3374 | if (!import_alias) |
| 3375 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3376 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3377 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3378 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3379 | if (mod != NULL) |
| 3380 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3381 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3382 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3383 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3384 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3385 | "unknown import statement: starts with command '%s'", |
| 3386 | STR(CHILD(n, 0))); |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | |
| 3390 | static stmt_ty |
| 3391 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3392 | { |
| 3393 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3394 | identifier name; |
| 3395 | asdl_seq *s; |
| 3396 | int i; |
| 3397 | |
| 3398 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3399 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3400 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3401 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3402 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3403 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3404 | if (!name) |
| 3405 | return NULL; |
| 3406 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3407 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3408 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3412 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3413 | { |
| 3414 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3415 | identifier name; |
| 3416 | asdl_seq *s; |
| 3417 | int i; |
| 3418 | |
| 3419 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3420 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3421 | if (!s) |
| 3422 | return NULL; |
| 3423 | for (i = 1; i < NCH(n); i += 2) { |
| 3424 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3425 | if (!name) |
| 3426 | return NULL; |
| 3427 | asdl_seq_SET(s, i / 2, name); |
| 3428 | } |
| 3429 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3430 | } |
| 3431 | |
| 3432 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3433 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3434 | { |
| 3435 | /* assert_stmt: 'assert' test [',' test] */ |
| 3436 | REQ(n, assert_stmt); |
| 3437 | if (NCH(n) == 2) { |
| 3438 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3439 | if (!expression) |
| 3440 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3441 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3442 | } |
| 3443 | else if (NCH(n) == 4) { |
| 3444 | expr_ty expr1, expr2; |
| 3445 | |
| 3446 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3447 | if (!expr1) |
| 3448 | return NULL; |
| 3449 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3450 | if (!expr2) |
| 3451 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3453 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3454 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3455 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3456 | "improper number of parts to 'assert' statement: %d", |
| 3457 | NCH(n)); |
| 3458 | return NULL; |
| 3459 | } |
| 3460 | |
| 3461 | static asdl_seq * |
| 3462 | ast_for_suite(struct compiling *c, const node *n) |
| 3463 | { |
| 3464 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3465 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3466 | stmt_ty s; |
| 3467 | int i, total, num, end, pos = 0; |
| 3468 | node *ch; |
| 3469 | |
| 3470 | REQ(n, suite); |
| 3471 | |
| 3472 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3473 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3474 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3475 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3476 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3477 | n = CHILD(n, 0); |
| 3478 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3479 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3480 | */ |
| 3481 | end = NCH(n) - 1; |
| 3482 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3483 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3484 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3485 | for (i = 0; i < end; i += 2) { |
| 3486 | ch = CHILD(n, i); |
| 3487 | s = ast_for_stmt(c, ch); |
| 3488 | if (!s) |
| 3489 | return NULL; |
| 3490 | asdl_seq_SET(seq, pos++, s); |
| 3491 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3492 | } |
| 3493 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3494 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3495 | ch = CHILD(n, i); |
| 3496 | REQ(ch, stmt); |
| 3497 | num = num_stmts(ch); |
| 3498 | if (num == 1) { |
| 3499 | /* small_stmt or compound_stmt with only one child */ |
| 3500 | s = ast_for_stmt(c, ch); |
| 3501 | if (!s) |
| 3502 | return NULL; |
| 3503 | asdl_seq_SET(seq, pos++, s); |
| 3504 | } |
| 3505 | else { |
| 3506 | int j; |
| 3507 | ch = CHILD(ch, 0); |
| 3508 | REQ(ch, simple_stmt); |
| 3509 | for (j = 0; j < NCH(ch); j += 2) { |
| 3510 | /* statement terminates with a semi-colon ';' */ |
| 3511 | if (NCH(CHILD(ch, j)) == 0) { |
| 3512 | assert((j + 1) == NCH(ch)); |
| 3513 | break; |
| 3514 | } |
| 3515 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3516 | if (!s) |
| 3517 | return NULL; |
| 3518 | asdl_seq_SET(seq, pos++, s); |
| 3519 | } |
| 3520 | } |
| 3521 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3522 | } |
| 3523 | assert(pos == seq->size); |
| 3524 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3527 | static string |
| 3528 | docstring_from_stmts(asdl_seq *stmts) |
| 3529 | { |
| 3530 | if (stmts && stmts->size) { |
| 3531 | stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 3532 | /* If first statement is a literal string, it's the doc string. */ |
| 3533 | if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) { |
| 3534 | string doc = s->v.Expr.value->v.Str.s; |
| 3535 | /* not very efficient, but simple */ |
| 3536 | memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1), |
| 3537 | (stmts->size - 1) * sizeof(void*)); |
| 3538 | stmts->size--; |
| 3539 | return doc; |
| 3540 | } |
| 3541 | } |
| 3542 | return NULL; |
| 3543 | } |
| 3544 | |
| 3545 | static asdl_seq * |
| 3546 | ast_for_body(struct compiling *c, const node *n, string *docstring) |
| 3547 | { |
| 3548 | asdl_seq *stmts = ast_for_suite(c, n); |
| 3549 | *docstring = docstring_from_stmts(stmts); |
| 3550 | return stmts; |
| 3551 | } |
| 3552 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3553 | static stmt_ty |
| 3554 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3555 | { |
| 3556 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3557 | ['else' ':' suite] |
| 3558 | */ |
| 3559 | char *s; |
| 3560 | |
| 3561 | REQ(n, if_stmt); |
| 3562 | |
| 3563 | if (NCH(n) == 4) { |
| 3564 | expr_ty expression; |
| 3565 | asdl_seq *suite_seq; |
| 3566 | |
| 3567 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3568 | if (!expression) |
| 3569 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3571 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3572 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3574 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3575 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3577 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3578 | s = STR(CHILD(n, 4)); |
| 3579 | /* s[2], the third character in the string, will be |
| 3580 | 's' for el_s_e, or |
| 3581 | 'i' for el_i_f |
| 3582 | */ |
| 3583 | if (s[2] == 's') { |
| 3584 | expr_ty expression; |
| 3585 | asdl_seq *seq1, *seq2; |
| 3586 | |
| 3587 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3588 | if (!expression) |
| 3589 | return NULL; |
| 3590 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3591 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3592 | return NULL; |
| 3593 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3594 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3595 | return NULL; |
| 3596 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3597 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3598 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3599 | } |
| 3600 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3601 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3602 | expr_ty expression; |
| 3603 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3604 | asdl_seq *orelse = NULL; |
| 3605 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3607 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3608 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3609 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3610 | has_else = 1; |
| 3611 | n_elif -= 3; |
| 3612 | } |
| 3613 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3614 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3615 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3616 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3618 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3619 | if (!orelse) |
| 3620 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3621 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3622 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3623 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3624 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3625 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3626 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3627 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3628 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3629 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | asdl_seq_SET(orelse, 0, |
| 3632 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3633 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3634 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3635 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3636 | /* the just-created orelse handled the last elif */ |
| 3637 | n_elif--; |
| 3638 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3640 | for (i = 0; i < n_elif; i++) { |
| 3641 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3642 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3643 | if (!newobj) |
| 3644 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3646 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3647 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3648 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3649 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3650 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3651 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3652 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3654 | LINENO(CHILD(n, off)), |
| 3655 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3656 | orelse = newobj; |
| 3657 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3658 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3659 | if (!expression) |
| 3660 | return NULL; |
| 3661 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3662 | if (!suite_seq) |
| 3663 | return NULL; |
| 3664 | return If(expression, suite_seq, orelse, |
| 3665 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3666 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3667 | |
| 3668 | PyErr_Format(PyExc_SystemError, |
| 3669 | "unexpected token in 'if' statement: %s", s); |
| 3670 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | static stmt_ty |
| 3674 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3675 | { |
| 3676 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3677 | REQ(n, while_stmt); |
| 3678 | |
| 3679 | if (NCH(n) == 4) { |
| 3680 | expr_ty expression; |
| 3681 | asdl_seq *suite_seq; |
| 3682 | |
| 3683 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3684 | if (!expression) |
| 3685 | return NULL; |
| 3686 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3687 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3688 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3689 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3690 | } |
| 3691 | else if (NCH(n) == 7) { |
| 3692 | expr_ty expression; |
| 3693 | asdl_seq *seq1, *seq2; |
| 3694 | |
| 3695 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3696 | if (!expression) |
| 3697 | return NULL; |
| 3698 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3699 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | return NULL; |
| 3701 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3702 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3703 | return NULL; |
| 3704 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3705 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3706 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3707 | |
| 3708 | PyErr_Format(PyExc_SystemError, |
| 3709 | "wrong number of tokens for 'while' statement: %d", |
| 3710 | NCH(n)); |
| 3711 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3715 | ast_for_for_stmt(struct compiling *c, const node *n, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3717 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3718 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3719 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3720 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3721 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3722 | REQ(n, for_stmt); |
| 3723 | |
| 3724 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3725 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3726 | if (!seq) |
| 3727 | return NULL; |
| 3728 | } |
| 3729 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3730 | node_target = CHILD(n, 1); |
| 3731 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3732 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3733 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3734 | /* Check the # of children rather than the length of _target, since |
| 3735 | for x, in ... has 1 element in _target, but still requires a Tuple. */ |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3736 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3737 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3738 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3739 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3740 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3741 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3742 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3743 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3744 | return NULL; |
| 3745 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3746 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3747 | return NULL; |
| 3748 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3749 | if (is_async) |
| 3750 | return AsyncFor(target, expression, suite_seq, seq, |
| 3751 | LINENO(n), n->n_col_offset, |
| 3752 | c->c_arena); |
| 3753 | else |
| 3754 | return For(target, expression, suite_seq, seq, |
| 3755 | LINENO(n), n->n_col_offset, |
| 3756 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | static excepthandler_ty |
| 3760 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3761 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3762 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3763 | REQ(exc, except_clause); |
| 3764 | REQ(body, suite); |
| 3765 | |
| 3766 | if (NCH(exc) == 1) { |
| 3767 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3768 | if (!suite_seq) |
| 3769 | return NULL; |
| 3770 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3771 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3772 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3773 | } |
| 3774 | else if (NCH(exc) == 2) { |
| 3775 | expr_ty expression; |
| 3776 | asdl_seq *suite_seq; |
| 3777 | |
| 3778 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3779 | if (!expression) |
| 3780 | return NULL; |
| 3781 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3782 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3783 | return NULL; |
| 3784 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3785 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3786 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3787 | } |
| 3788 | else if (NCH(exc) == 4) { |
| 3789 | asdl_seq *suite_seq; |
| 3790 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3791 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3792 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3793 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3794 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3795 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3796 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3797 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3798 | return NULL; |
| 3799 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3800 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3801 | return NULL; |
| 3802 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3803 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3804 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3805 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3806 | |
| 3807 | PyErr_Format(PyExc_SystemError, |
| 3808 | "wrong number of children for 'except' clause: %d", |
| 3809 | NCH(exc)); |
| 3810 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | static stmt_ty |
| 3814 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3815 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3816 | const int nch = NCH(n); |
| 3817 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3818 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3819 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3820 | REQ(n, try_stmt); |
| 3821 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3822 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3823 | if (body == NULL) |
| 3824 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3825 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3826 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3827 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3828 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3829 | /* we can assume it's an "else", |
| 3830 | because nch >= 9 for try-else-finally and |
| 3831 | it would otherwise have a type of except_clause */ |
| 3832 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3833 | if (orelse == NULL) |
| 3834 | return NULL; |
| 3835 | n_except--; |
| 3836 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3837 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3838 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3839 | if (finally == NULL) |
| 3840 | return NULL; |
| 3841 | n_except--; |
| 3842 | } |
| 3843 | else { |
| 3844 | /* we can assume it's an "else", |
| 3845 | otherwise it would have a type of except_clause */ |
| 3846 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3847 | if (orelse == NULL) |
| 3848 | return NULL; |
| 3849 | n_except--; |
| 3850 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3851 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3852 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3853 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3854 | return NULL; |
| 3855 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3856 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3857 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3858 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3859 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3860 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3861 | if (handlers == NULL) |
| 3862 | return NULL; |
| 3863 | |
| 3864 | for (i = 0; i < n_except; i++) { |
| 3865 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3866 | CHILD(n, 5 + i * 3)); |
| 3867 | if (!e) |
| 3868 | return NULL; |
| 3869 | asdl_seq_SET(handlers, i, e); |
| 3870 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3873 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3874 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3877 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3878 | static withitem_ty |
| 3879 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3880 | { |
| 3881 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3882 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3883 | REQ(n, with_item); |
| 3884 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3885 | if (!context_expr) |
| 3886 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3887 | if (NCH(n) == 3) { |
| 3888 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3889 | |
| 3890 | if (!optional_vars) { |
| 3891 | return NULL; |
| 3892 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3893 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3894 | return NULL; |
| 3895 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3898 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3901 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3902 | static stmt_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3903 | ast_for_with_stmt(struct compiling *c, const node *n, int is_async) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3904 | { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3905 | int i, n_items; |
| 3906 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3907 | |
| 3908 | REQ(n, with_stmt); |
| 3909 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3910 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3911 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3912 | if (!items) |
| 3913 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3914 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3915 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3916 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3917 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3918 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3921 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3922 | if (!body) |
| 3923 | return NULL; |
| 3924 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3925 | if (is_async) |
| 3926 | return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
| 3927 | else |
| 3928 | return With(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3931 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3932 | ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3933 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3934 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3935 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3936 | asdl_seq *s; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3937 | string docstring; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3938 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3939 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3940 | REQ(n, classdef); |
| 3941 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3942 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3943 | s = ast_for_body(c, CHILD(n, 3), &docstring); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3944 | if (!s) |
| 3945 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3946 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3947 | if (!classname) |
| 3948 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3949 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3950 | return NULL; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3951 | return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring, |
| 3952 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3953 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3954 | |
| 3955 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3956 | s = ast_for_body(c, CHILD(n, 5), &docstring); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3957 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3958 | return NULL; |
| 3959 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3960 | if (!classname) |
| 3961 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3962 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3963 | return NULL; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3964 | return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring, |
| 3965 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3968 | /* class NAME '(' arglist ')' ':' suite */ |
| 3969 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3970 | { |
| 3971 | PyObject *dummy_name; |
| 3972 | expr_ty dummy; |
| 3973 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3974 | if (!dummy_name) |
| 3975 | return NULL; |
| 3976 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 3977 | call = ast_for_call(c, CHILD(n, 3), dummy); |
| 3978 | if (!call) |
| 3979 | return NULL; |
| 3980 | } |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3981 | s = ast_for_body(c, CHILD(n, 6), &docstring); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3982 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3983 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3984 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3985 | if (!classname) |
| 3986 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3987 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3988 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3989 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3990 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3991 | decorator_seq, docstring, LINENO(n), n->n_col_offset, |
| 3992 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | static stmt_ty |
| 3996 | ast_for_stmt(struct compiling *c, const node *n) |
| 3997 | { |
| 3998 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3999 | assert(NCH(n) == 1); |
| 4000 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4001 | } |
| 4002 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4003 | assert(num_stmts(n) == 1); |
| 4004 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4005 | } |
| 4006 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4007 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4008 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4009 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4010 | */ |
| 4011 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4012 | case expr_stmt: |
| 4013 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4014 | case del_stmt: |
| 4015 | return ast_for_del_stmt(c, n); |
| 4016 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 4017 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4018 | case flow_stmt: |
| 4019 | return ast_for_flow_stmt(c, n); |
| 4020 | case import_stmt: |
| 4021 | return ast_for_import_stmt(c, n); |
| 4022 | case global_stmt: |
| 4023 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4024 | case nonlocal_stmt: |
| 4025 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4026 | case assert_stmt: |
| 4027 | return ast_for_assert_stmt(c, n); |
| 4028 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4029 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4030 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4031 | TYPE(n), NCH(n)); |
| 4032 | return NULL; |
| 4033 | } |
| 4034 | } |
| 4035 | else { |
| 4036 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4037 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4038 | */ |
| 4039 | node *ch = CHILD(n, 0); |
| 4040 | REQ(n, compound_stmt); |
| 4041 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4042 | case if_stmt: |
| 4043 | return ast_for_if_stmt(c, ch); |
| 4044 | case while_stmt: |
| 4045 | return ast_for_while_stmt(c, ch); |
| 4046 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4047 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4048 | case try_stmt: |
| 4049 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4050 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4051 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4052 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4053 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4054 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4055 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | case decorated: |
| 4057 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4058 | case async_stmt: |
| 4059 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4060 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4061 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4062 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4063 | TYPE(n), NCH(n)); |
| 4064 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4065 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4070 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4071 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4072 | const char *end; |
| 4073 | long x; |
| 4074 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4075 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4076 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4077 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4078 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4079 | errno = 0; |
| 4080 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4081 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4082 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4083 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4084 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4085 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4086 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4087 | } |
| 4088 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4089 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4090 | if (*end == '\0') { |
| 4091 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4092 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4093 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4094 | } |
| 4095 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4096 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4097 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4098 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4099 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4100 | return NULL; |
| 4101 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4102 | } |
| 4103 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4104 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4105 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4106 | if (dx == -1.0 && PyErr_Occurred()) |
| 4107 | return NULL; |
| 4108 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4109 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4113 | parsenumber(struct compiling *c, const char *s) |
| 4114 | { |
| 4115 | char *dup, *end; |
| 4116 | PyObject *res = NULL; |
| 4117 | |
| 4118 | assert(s != NULL); |
| 4119 | |
| 4120 | if (strchr(s, '_') == NULL) { |
| 4121 | return parsenumber_raw(c, s); |
| 4122 | } |
| 4123 | /* Create a duplicate without underscores. */ |
| 4124 | dup = PyMem_Malloc(strlen(s) + 1); |
| 4125 | end = dup; |
| 4126 | for (; *s; s++) { |
| 4127 | if (*s != '_') { |
| 4128 | *end++ = *s; |
| 4129 | } |
| 4130 | } |
| 4131 | *end = '\0'; |
| 4132 | res = parsenumber_raw(c, dup); |
| 4133 | PyMem_Free(dup); |
| 4134 | return res; |
| 4135 | } |
| 4136 | |
| 4137 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4138 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4139 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4140 | const char *s, *t; |
| 4141 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4142 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4143 | while (s < end && (*s & 0x80)) s++; |
| 4144 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4145 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4148 | static int |
| 4149 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
| 4150 | char first_invalid_escape_char) |
| 4151 | { |
| 4152 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4153 | first_invalid_escape_char); |
| 4154 | if (msg == NULL) { |
| 4155 | return -1; |
| 4156 | } |
| 4157 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
| 4158 | c->c_filename, LINENO(n), |
| 4159 | NULL, NULL) < 0 && |
| 4160 | PyErr_ExceptionMatches(PyExc_DeprecationWarning)) |
| 4161 | { |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4162 | const char *s; |
| 4163 | |
| 4164 | /* Replace the DeprecationWarning exception with a SyntaxError |
| 4165 | to get a more accurate error report */ |
| 4166 | PyErr_Clear(); |
| 4167 | |
| 4168 | s = PyUnicode_AsUTF8(msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4169 | if (s != NULL) { |
| 4170 | ast_error(c, n, s); |
| 4171 | } |
| 4172 | Py_DECREF(msg); |
| 4173 | return -1; |
| 4174 | } |
| 4175 | Py_DECREF(msg); |
| 4176 | return 0; |
| 4177 | } |
| 4178 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4179 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4180 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4181 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4182 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4183 | PyObject *v, *u; |
| 4184 | char *buf; |
| 4185 | char *p; |
| 4186 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4187 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4188 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4189 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4190 | return NULL; |
| 4191 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4192 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4193 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4194 | if (u == NULL) |
| 4195 | return NULL; |
| 4196 | p = buf = PyBytes_AsString(u); |
| 4197 | end = s + len; |
| 4198 | while (s < end) { |
| 4199 | if (*s == '\\') { |
| 4200 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4201 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4202 | strcpy(p, "u005c"); |
| 4203 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4204 | if (s >= end) |
| 4205 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4206 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4207 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4208 | if (*s & 0x80) { /* XXX inefficient */ |
| 4209 | PyObject *w; |
| 4210 | int kind; |
| 4211 | void *data; |
| 4212 | Py_ssize_t len, i; |
| 4213 | w = decode_utf8(c, &s, end); |
| 4214 | if (w == NULL) { |
| 4215 | Py_DECREF(u); |
| 4216 | return NULL; |
| 4217 | } |
| 4218 | kind = PyUnicode_KIND(w); |
| 4219 | data = PyUnicode_DATA(w); |
| 4220 | len = PyUnicode_GET_LENGTH(w); |
| 4221 | for (i = 0; i < len; i++) { |
| 4222 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4223 | sprintf(p, "\\U%08x", chr); |
| 4224 | p += 10; |
| 4225 | } |
| 4226 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4227 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4228 | Py_DECREF(w); |
| 4229 | } else { |
| 4230 | *p++ = *s++; |
| 4231 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4232 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4233 | len = p - buf; |
| 4234 | s = buf; |
| 4235 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4236 | const char *first_invalid_escape; |
| 4237 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4238 | |
| 4239 | if (v != NULL && first_invalid_escape != NULL) { |
| 4240 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4241 | /* We have not decref u before because first_invalid_escape points |
| 4242 | inside u. */ |
| 4243 | Py_XDECREF(u); |
| 4244 | Py_DECREF(v); |
| 4245 | return NULL; |
| 4246 | } |
| 4247 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4248 | Py_XDECREF(u); |
| 4249 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4252 | static PyObject * |
| 4253 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4254 | size_t len) |
| 4255 | { |
| 4256 | const char *first_invalid_escape; |
| 4257 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4258 | &first_invalid_escape); |
| 4259 | if (result == NULL) |
| 4260 | return NULL; |
| 4261 | |
| 4262 | if (first_invalid_escape != NULL) { |
| 4263 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4264 | Py_DECREF(result); |
| 4265 | return NULL; |
| 4266 | } |
| 4267 | } |
| 4268 | return result; |
| 4269 | } |
| 4270 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4271 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4272 | and `col_offset` to existing locations. */ |
| 4273 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4274 | { |
| 4275 | n->n_col_offset = n->n_col_offset + col_offset; |
| 4276 | for (int i = 0; i < NCH(n); ++i) { |
| 4277 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4278 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4279 | col_offset = 0; |
| 4280 | } |
| 4281 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4282 | } |
| 4283 | n->n_lineno = n->n_lineno + lineno; |
| 4284 | } |
| 4285 | |
| 4286 | /* Fix locations for the given node and its children. |
| 4287 | |
| 4288 | `parent` is the enclosing node. |
| 4289 | `n` is the node which locations are going to be fixed relative to parent. |
| 4290 | `expr_str` is the child node's string representation, incuding braces. |
| 4291 | */ |
| 4292 | static void |
| 4293 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4294 | { |
| 4295 | char *substr = NULL; |
| 4296 | char *start; |
| 4297 | int lines = LINENO(parent) - 1; |
| 4298 | int cols = parent->n_col_offset; |
| 4299 | /* Find the full fstring to fix location information in `n`. */ |
| 4300 | while (parent && parent->n_type != STRING) |
| 4301 | parent = parent->n_child; |
| 4302 | if (parent && parent->n_str) { |
| 4303 | substr = strstr(parent->n_str, expr_str); |
| 4304 | if (substr) { |
| 4305 | start = substr; |
| 4306 | while (start > parent->n_str) { |
| 4307 | if (start[0] == '\n') |
| 4308 | break; |
| 4309 | start--; |
| 4310 | } |
| 4311 | cols += substr - start; |
| 4312 | /* Fix lineno in mulitline strings. */ |
| 4313 | while ((substr = strchr(substr + 1, '\n'))) |
| 4314 | lines--; |
| 4315 | } |
| 4316 | } |
| 4317 | fstring_shift_node_locations(n, lines, cols); |
| 4318 | } |
| 4319 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4320 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4321 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4322 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4323 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4324 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4325 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4326 | { |
| 4327 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4328 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4329 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4330 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4331 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4332 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4333 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4334 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4335 | assert(*(expr_start-1) == '{'); |
| 4336 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4337 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4338 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4339 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4340 | because turning the expression '' in to '()' would go from being invalid |
| 4341 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4342 | for (s = expr_start; s != expr_end; s++) { |
| 4343 | char c = *s; |
| 4344 | /* The Python parser ignores only the following whitespace |
| 4345 | characters (\r already is converted to \n). */ |
| 4346 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4347 | break; |
| 4348 | } |
| 4349 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4350 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4351 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4352 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4353 | } |
| 4354 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4355 | len = expr_end - expr_start; |
| 4356 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4357 | str = PyMem_RawMalloc(len + 3); |
| 4358 | if (str == NULL) |
| 4359 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4360 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4361 | str[0] = '('; |
| 4362 | memcpy(str+1, expr_start, len); |
| 4363 | str[len+1] = ')'; |
| 4364 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4365 | |
| 4366 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4367 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4368 | Py_eval_input, 0); |
| 4369 | if (!mod_n) { |
| 4370 | PyMem_RawFree(str); |
| 4371 | return NULL; |
| 4372 | } |
| 4373 | /* Reuse str to find the correct column offset. */ |
| 4374 | str[0] = '{'; |
| 4375 | str[len+1] = '}'; |
| 4376 | fstring_fix_node_location(n, mod_n, str); |
| 4377 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4378 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4379 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4380 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4381 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4382 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | /* Return -1 on error. |
| 4386 | |
| 4387 | Return 0 if we reached the end of the literal. |
| 4388 | |
| 4389 | Return 1 if we haven't reached the end of the literal, but we want |
| 4390 | the caller to process the literal up to this point. Used for |
| 4391 | doubled braces. |
| 4392 | */ |
| 4393 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4394 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4395 | PyObject **literal, int recurse_lvl, |
| 4396 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4397 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4398 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4399 | brace (which isn't part of a unicode name escape such as |
| 4400 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4401 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4402 | const char *s = *str; |
| 4403 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4404 | int result = 0; |
| 4405 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4406 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4407 | while (s < end) { |
| 4408 | char ch = *s++; |
| 4409 | if (!raw && ch == '\\' && s < end) { |
| 4410 | ch = *s++; |
| 4411 | if (ch == 'N') { |
| 4412 | if (s < end && *s++ == '{') { |
| 4413 | while (s < end && *s++ != '}') { |
| 4414 | } |
| 4415 | continue; |
| 4416 | } |
| 4417 | break; |
| 4418 | } |
| 4419 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4420 | return -1; |
| 4421 | } |
| 4422 | } |
| 4423 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4424 | /* Check for doubled braces, but only at the top level. If |
| 4425 | we checked at every level, then f'{0:{3}}' would fail |
| 4426 | with the two closing braces. */ |
| 4427 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4428 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4429 | /* We're going to tell the caller that the literal ends |
| 4430 | here, but that they should continue scanning. But also |
| 4431 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4432 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4433 | result = 1; |
| 4434 | goto done; |
| 4435 | } |
| 4436 | |
| 4437 | /* Where a single '{' is the start of a new expression, a |
| 4438 | single '}' is not allowed. */ |
| 4439 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4440 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4441 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4442 | return -1; |
| 4443 | } |
| 4444 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4445 | /* We're either at a '{', which means we're starting another |
| 4446 | expression; or a '}', which means we're at the end of this |
| 4447 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4448 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4449 | break; |
| 4450 | } |
| 4451 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4452 | *str = s; |
| 4453 | assert(s <= end); |
| 4454 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4455 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4456 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4457 | if (raw) |
| 4458 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4459 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4460 | NULL, NULL); |
| 4461 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4462 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4463 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4464 | if (!*literal) |
| 4465 | return -1; |
| 4466 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4467 | return result; |
| 4468 | } |
| 4469 | |
| 4470 | /* Forward declaration because parsing is recursive. */ |
| 4471 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4472 | fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4473 | struct compiling *c, const node *n); |
| 4474 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4475 | /* Parse the f-string at *str, ending at end. We know *str starts an |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4476 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4477 | which includes the expression, conversion character, and |
| 4478 | format_spec expression. |
| 4479 | |
| 4480 | Note that I don't do a perfect job here: I don't make sure that a |
| 4481 | closing brace doesn't match an opening paren, for example. It |
| 4482 | doesn't need to error on all invalid expressions, just correctly |
| 4483 | find the end of all valid ones. Any errors inside the expression |
| 4484 | will be caught when we parse it later. */ |
| 4485 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4486 | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4487 | expr_ty *expression, struct compiling *c, const node *n) |
| 4488 | { |
| 4489 | /* Return -1 on error, else 0. */ |
| 4490 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4491 | const char *expr_start; |
| 4492 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4493 | expr_ty simple_expression; |
| 4494 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4495 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4496 | |
| 4497 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4498 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4499 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4500 | |
| 4501 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4502 | int string_type = 0; |
| 4503 | |
| 4504 | /* Keep track of nesting level for braces/parens/brackets in |
| 4505 | expressions. */ |
| 4506 | Py_ssize_t nested_depth = 0; |
| 4507 | |
| 4508 | /* Can only nest one level deep. */ |
| 4509 | if (recurse_lvl >= 2) { |
| 4510 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4511 | return -1; |
| 4512 | } |
| 4513 | |
| 4514 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4515 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4516 | assert(**str == '{'); |
| 4517 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4518 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4519 | expr_start = *str; |
| 4520 | for (; *str < end; (*str)++) { |
| 4521 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4522 | |
| 4523 | /* Loop invariants. */ |
| 4524 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4525 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4526 | if (quote_char) |
| 4527 | assert(string_type == 1 || string_type == 3); |
| 4528 | else |
| 4529 | assert(string_type == 0); |
| 4530 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4531 | ch = **str; |
| 4532 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4533 | if (ch == '\\') { |
| 4534 | /* Error: can't include a backslash character, inside |
| 4535 | parens or strings or not. */ |
| 4536 | ast_error(c, n, "f-string expression part " |
| 4537 | "cannot include a backslash"); |
| 4538 | return -1; |
| 4539 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4540 | if (quote_char) { |
| 4541 | /* We're inside a string. See if we're at the end. */ |
| 4542 | /* This code needs to implement the same non-error logic |
| 4543 | as tok_get from tokenizer.c, at the letter_quote |
| 4544 | label. To actually share that code would be a |
| 4545 | nightmare. But, it's unlikely to change and is small, |
| 4546 | so duplicate it here. Note we don't need to catch all |
| 4547 | of the errors, since they'll be caught when parsing the |
| 4548 | expression. We just need to match the non-error |
| 4549 | cases. Thus we can ignore \n in single-quoted strings, |
| 4550 | for example. Or non-terminated strings. */ |
| 4551 | if (ch == quote_char) { |
| 4552 | /* Does this match the string_type (single or triple |
| 4553 | quoted)? */ |
| 4554 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4555 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4556 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4557 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4558 | string_type = 0; |
| 4559 | quote_char = 0; |
| 4560 | continue; |
| 4561 | } |
| 4562 | } else { |
| 4563 | /* We're at the end of a normal string. */ |
| 4564 | quote_char = 0; |
| 4565 | string_type = 0; |
| 4566 | continue; |
| 4567 | } |
| 4568 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4569 | } else if (ch == '\'' || ch == '"') { |
| 4570 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4571 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4572 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4573 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4574 | } else { |
| 4575 | /* Start of a normal string. */ |
| 4576 | string_type = 1; |
| 4577 | } |
| 4578 | /* Start looking for the end of the string. */ |
| 4579 | quote_char = ch; |
| 4580 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4581 | nested_depth++; |
| 4582 | } else if (nested_depth != 0 && |
| 4583 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4584 | nested_depth--; |
| 4585 | } else if (ch == '#') { |
| 4586 | /* Error: can't include a comment character, inside parens |
| 4587 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4588 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4589 | return -1; |
| 4590 | } else if (nested_depth == 0 && |
| 4591 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4592 | /* First, test for the special case of "!=". Since '=' is |
| 4593 | not an allowed conversion character, nothing is lost in |
| 4594 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4595 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4596 | /* This isn't a conversion character, just continue. */ |
| 4597 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4598 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4599 | /* Normal way out of this loop. */ |
| 4600 | break; |
| 4601 | } else { |
| 4602 | /* Just consume this char and loop around. */ |
| 4603 | } |
| 4604 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4605 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4606 | /* If we leave this loop in a string or with mismatched parens, we |
| 4607 | don't care. We'll get a syntax error when compiling the |
| 4608 | expression. But, we can produce a better error message, so |
| 4609 | let's just do that.*/ |
| 4610 | if (quote_char) { |
| 4611 | ast_error(c, n, "f-string: unterminated string"); |
| 4612 | return -1; |
| 4613 | } |
| 4614 | if (nested_depth) { |
| 4615 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4616 | return -1; |
| 4617 | } |
| 4618 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4619 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4620 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4621 | |
| 4622 | /* Compile the expression as soon as possible, so we show errors |
| 4623 | related to the expression before errors related to the |
| 4624 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4625 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4626 | if (!simple_expression) |
| 4627 | return -1; |
| 4628 | |
| 4629 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4630 | if (**str == '!') { |
| 4631 | *str += 1; |
| 4632 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4633 | goto unexpected_end_of_string; |
| 4634 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4635 | conversion = **str; |
| 4636 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4637 | |
| 4638 | /* Validate the conversion. */ |
| 4639 | if (!(conversion == 's' || conversion == 'r' |
| 4640 | || conversion == 'a')) { |
| 4641 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4642 | "expected 's', 'r', or 'a'"); |
| 4643 | return -1; |
| 4644 | } |
| 4645 | } |
| 4646 | |
| 4647 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4648 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4649 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4650 | if (**str == ':') { |
| 4651 | *str += 1; |
| 4652 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4653 | goto unexpected_end_of_string; |
| 4654 | |
| 4655 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4656 | format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4657 | if (!format_spec) |
| 4658 | return -1; |
| 4659 | } |
| 4660 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4661 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4662 | goto unexpected_end_of_string; |
| 4663 | |
| 4664 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4665 | assert(*str < end); |
| 4666 | assert(**str == '}'); |
| 4667 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4668 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4669 | /* And now create the FormattedValue node that represents this |
| 4670 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4671 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4672 | format_spec, LINENO(n), n->n_col_offset, |
| 4673 | c->c_arena); |
| 4674 | if (!*expression) |
| 4675 | return -1; |
| 4676 | |
| 4677 | return 0; |
| 4678 | |
| 4679 | unexpected_end_of_string: |
| 4680 | ast_error(c, n, "f-string: expecting '}'"); |
| 4681 | return -1; |
| 4682 | } |
| 4683 | |
| 4684 | /* Return -1 on error. |
| 4685 | |
| 4686 | Return 0 if we have a literal (possible zero length) and an |
| 4687 | expression (zero length if at the end of the string. |
| 4688 | |
| 4689 | Return 1 if we have a literal, but no expression, and we want the |
| 4690 | caller to call us again. This is used to deal with doubled |
| 4691 | braces. |
| 4692 | |
| 4693 | When called multiple times on the string 'a{{b{0}c', this function |
| 4694 | will return: |
| 4695 | |
| 4696 | 1. the literal 'a{' with no expression, and a return value |
| 4697 | of 1. Despite the fact that there's no expression, the return |
| 4698 | value of 1 means we're not finished yet. |
| 4699 | |
| 4700 | 2. the literal 'b' and the expression '0', with a return value of |
| 4701 | 0. The fact that there's an expression means we're not finished. |
| 4702 | |
| 4703 | 3. literal 'c' with no expression and a return value of 0. The |
| 4704 | combination of the return value of 0 with no expression means |
| 4705 | we're finished. |
| 4706 | */ |
| 4707 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4708 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4709 | int recurse_lvl, PyObject **literal, |
| 4710 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4711 | struct compiling *c, const node *n) |
| 4712 | { |
| 4713 | int result; |
| 4714 | |
| 4715 | assert(*literal == NULL && *expression == NULL); |
| 4716 | |
| 4717 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4718 | result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4719 | if (result < 0) |
| 4720 | goto error; |
| 4721 | |
| 4722 | assert(result == 0 || result == 1); |
| 4723 | |
| 4724 | if (result == 1) |
| 4725 | /* We have a literal, but don't look at the expression. */ |
| 4726 | return 1; |
| 4727 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4728 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4729 | /* We're at the end of the string or the end of a nested |
| 4730 | f-string: no expression. The top-level error case where we |
| 4731 | expect to be at the end of the string but we're at a '}' is |
| 4732 | handled later. */ |
| 4733 | return 0; |
| 4734 | |
| 4735 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4736 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4737 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4738 | if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4739 | goto error; |
| 4740 | |
| 4741 | return 0; |
| 4742 | |
| 4743 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4744 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4745 | return -1; |
| 4746 | } |
| 4747 | |
| 4748 | #define EXPRLIST_N_CACHED 64 |
| 4749 | |
| 4750 | typedef struct { |
| 4751 | /* Incrementally build an array of expr_ty, so be used in an |
| 4752 | asdl_seq. Cache some small but reasonably sized number of |
| 4753 | expr_ty's, and then after that start dynamically allocating, |
| 4754 | doubling the number allocated each time. Note that the f-string |
| 4755 | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
| 4756 | Str for the literal 'a'. So you add expr_ty's about twice as |
| 4757 | fast as you add exressions in an f-string. */ |
| 4758 | |
| 4759 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4760 | Py_ssize_t size; /* Number we've used. */ |
| 4761 | expr_ty *p; /* Pointer to the memory we're actually |
| 4762 | using. Will point to 'data' until we |
| 4763 | start dynamically allocating. */ |
| 4764 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4765 | } ExprList; |
| 4766 | |
| 4767 | #ifdef NDEBUG |
| 4768 | #define ExprList_check_invariants(l) |
| 4769 | #else |
| 4770 | static void |
| 4771 | ExprList_check_invariants(ExprList *l) |
| 4772 | { |
| 4773 | /* Check our invariants. Make sure this object is "live", and |
| 4774 | hasn't been deallocated. */ |
| 4775 | assert(l->size >= 0); |
| 4776 | assert(l->p != NULL); |
| 4777 | if (l->size <= EXPRLIST_N_CACHED) |
| 4778 | assert(l->data == l->p); |
| 4779 | } |
| 4780 | #endif |
| 4781 | |
| 4782 | static void |
| 4783 | ExprList_Init(ExprList *l) |
| 4784 | { |
| 4785 | l->allocated = EXPRLIST_N_CACHED; |
| 4786 | l->size = 0; |
| 4787 | |
| 4788 | /* Until we start allocating dynamically, p points to data. */ |
| 4789 | l->p = l->data; |
| 4790 | |
| 4791 | ExprList_check_invariants(l); |
| 4792 | } |
| 4793 | |
| 4794 | static int |
| 4795 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4796 | { |
| 4797 | ExprList_check_invariants(l); |
| 4798 | if (l->size >= l->allocated) { |
| 4799 | /* We need to alloc (or realloc) the memory. */ |
| 4800 | Py_ssize_t new_size = l->allocated * 2; |
| 4801 | |
| 4802 | /* See if we've ever allocated anything dynamically. */ |
| 4803 | if (l->p == l->data) { |
| 4804 | Py_ssize_t i; |
| 4805 | /* We're still using the cached data. Switch to |
| 4806 | alloc-ing. */ |
| 4807 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4808 | if (!l->p) |
| 4809 | return -1; |
| 4810 | /* Copy the cached data into the new buffer. */ |
| 4811 | for (i = 0; i < l->size; i++) |
| 4812 | l->p[i] = l->data[i]; |
| 4813 | } else { |
| 4814 | /* Just realloc. */ |
| 4815 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4816 | if (!tmp) { |
| 4817 | PyMem_RawFree(l->p); |
| 4818 | l->p = NULL; |
| 4819 | return -1; |
| 4820 | } |
| 4821 | l->p = tmp; |
| 4822 | } |
| 4823 | |
| 4824 | l->allocated = new_size; |
| 4825 | assert(l->allocated == 2 * l->size); |
| 4826 | } |
| 4827 | |
| 4828 | l->p[l->size++] = exp; |
| 4829 | |
| 4830 | ExprList_check_invariants(l); |
| 4831 | return 0; |
| 4832 | } |
| 4833 | |
| 4834 | static void |
| 4835 | ExprList_Dealloc(ExprList *l) |
| 4836 | { |
| 4837 | ExprList_check_invariants(l); |
| 4838 | |
| 4839 | /* If there's been an error, or we've never dynamically allocated, |
| 4840 | do nothing. */ |
| 4841 | if (!l->p || l->p == l->data) { |
| 4842 | /* Do nothing. */ |
| 4843 | } else { |
| 4844 | /* We have dynamically allocated. Free the memory. */ |
| 4845 | PyMem_RawFree(l->p); |
| 4846 | } |
| 4847 | l->p = NULL; |
| 4848 | l->size = -1; |
| 4849 | } |
| 4850 | |
| 4851 | static asdl_seq * |
| 4852 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4853 | { |
| 4854 | asdl_seq *seq; |
| 4855 | |
| 4856 | ExprList_check_invariants(l); |
| 4857 | |
| 4858 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4859 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4860 | if (seq) { |
| 4861 | Py_ssize_t i; |
| 4862 | for (i = 0; i < l->size; i++) |
| 4863 | asdl_seq_SET(seq, i, l->p[i]); |
| 4864 | } |
| 4865 | ExprList_Dealloc(l); |
| 4866 | return seq; |
| 4867 | } |
| 4868 | |
| 4869 | /* The FstringParser is designed to add a mix of strings and |
| 4870 | f-strings, and concat them together as needed. Ultimately, it |
| 4871 | generates an expr_ty. */ |
| 4872 | typedef struct { |
| 4873 | PyObject *last_str; |
| 4874 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4875 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4876 | } FstringParser; |
| 4877 | |
| 4878 | #ifdef NDEBUG |
| 4879 | #define FstringParser_check_invariants(state) |
| 4880 | #else |
| 4881 | static void |
| 4882 | FstringParser_check_invariants(FstringParser *state) |
| 4883 | { |
| 4884 | if (state->last_str) |
| 4885 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4886 | ExprList_check_invariants(&state->expr_list); |
| 4887 | } |
| 4888 | #endif |
| 4889 | |
| 4890 | static void |
| 4891 | FstringParser_Init(FstringParser *state) |
| 4892 | { |
| 4893 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4894 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4895 | ExprList_Init(&state->expr_list); |
| 4896 | FstringParser_check_invariants(state); |
| 4897 | } |
| 4898 | |
| 4899 | static void |
| 4900 | FstringParser_Dealloc(FstringParser *state) |
| 4901 | { |
| 4902 | FstringParser_check_invariants(state); |
| 4903 | |
| 4904 | Py_XDECREF(state->last_str); |
| 4905 | ExprList_Dealloc(&state->expr_list); |
| 4906 | } |
| 4907 | |
| 4908 | /* Make a Str node, but decref the PyUnicode object being added. */ |
| 4909 | static expr_ty |
| 4910 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4911 | { |
| 4912 | PyObject *s = *str; |
| 4913 | *str = NULL; |
| 4914 | assert(PyUnicode_CheckExact(s)); |
| 4915 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4916 | Py_DECREF(s); |
| 4917 | return NULL; |
| 4918 | } |
| 4919 | return Str(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 4920 | } |
| 4921 | |
| 4922 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4923 | decref'd. */ |
| 4924 | static int |
| 4925 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4926 | { |
| 4927 | FstringParser_check_invariants(state); |
| 4928 | |
| 4929 | assert(PyUnicode_CheckExact(str)); |
| 4930 | |
| 4931 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4932 | Py_DECREF(str); |
| 4933 | return 0; |
| 4934 | } |
| 4935 | |
| 4936 | if (!state->last_str) { |
| 4937 | /* We didn't have a string before, so just remember this one. */ |
| 4938 | state->last_str = str; |
| 4939 | } else { |
| 4940 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4941 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4942 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4943 | return -1; |
| 4944 | } |
| 4945 | FstringParser_check_invariants(state); |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4949 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4950 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4951 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4952 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4953 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4954 | struct compiling *c, const node *n) |
| 4955 | { |
| 4956 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4957 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4958 | |
| 4959 | /* Parse the f-string. */ |
| 4960 | while (1) { |
| 4961 | PyObject *literal = NULL; |
| 4962 | expr_ty expression = NULL; |
| 4963 | |
| 4964 | /* If there's a zero length literal in front of the |
| 4965 | expression, literal will be NULL. If we're at the end of |
| 4966 | the f-string, expression will be NULL (unless result == 1, |
| 4967 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4968 | int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4969 | &literal, &expression, |
| 4970 | c, n); |
| 4971 | if (result < 0) |
| 4972 | return -1; |
| 4973 | |
| 4974 | /* Add the literal, if any. */ |
| 4975 | if (!literal) { |
| 4976 | /* Do nothing. Just leave last_str alone (and possibly |
| 4977 | NULL). */ |
| 4978 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 4979 | /* Note that the literal can be zero length, if the |
| 4980 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4981 | state->last_str = literal; |
| 4982 | literal = NULL; |
| 4983 | } else { |
| 4984 | /* We have a literal, concatenate it. */ |
| 4985 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4986 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4987 | return -1; |
| 4988 | literal = NULL; |
| 4989 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4990 | |
| 4991 | /* We've dealt with the literal now. It can't be leaked on further |
| 4992 | errors. */ |
| 4993 | assert(literal == NULL); |
| 4994 | |
| 4995 | /* See if we should just loop around to get the next literal |
| 4996 | and expression, while ignoring the expression this |
| 4997 | time. This is used for un-doubling braces, as an |
| 4998 | optimization. */ |
| 4999 | if (result == 1) |
| 5000 | continue; |
| 5001 | |
| 5002 | if (!expression) |
| 5003 | /* We're done with this f-string. */ |
| 5004 | break; |
| 5005 | |
| 5006 | /* We know we have an expression. Convert any existing string |
| 5007 | to a Str node. */ |
| 5008 | if (!state->last_str) { |
| 5009 | /* Do nothing. No previous literal. */ |
| 5010 | } else { |
| 5011 | /* Convert the existing last_str literal to a Str node. */ |
| 5012 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5013 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5014 | return -1; |
| 5015 | } |
| 5016 | |
| 5017 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5018 | return -1; |
| 5019 | } |
| 5020 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5021 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5022 | string. Otherwise, we must be at a right brace. */ |
| 5023 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5024 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5025 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5026 | return -1; |
| 5027 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5028 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5029 | ast_error(c, n, "f-string: expecting '}'"); |
| 5030 | return -1; |
| 5031 | } |
| 5032 | |
| 5033 | FstringParser_check_invariants(state); |
| 5034 | return 0; |
| 5035 | } |
| 5036 | |
| 5037 | /* Convert the partial state reflected in last_str and expr_list to an |
| 5038 | expr_ty. The expr_ty can be a Str, or a JoinedStr. */ |
| 5039 | static expr_ty |
| 5040 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5041 | const node *n) |
| 5042 | { |
| 5043 | asdl_seq *seq; |
| 5044 | |
| 5045 | FstringParser_check_invariants(state); |
| 5046 | |
| 5047 | /* If we're just a constant string with no expressions, return |
| 5048 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5049 | if (!state->fmode) { |
| 5050 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5051 | if (!state->last_str) { |
| 5052 | /* Create a zero length string. */ |
| 5053 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5054 | if (!state->last_str) |
| 5055 | goto error; |
| 5056 | } |
| 5057 | return make_str_node_and_del(&state->last_str, c, n); |
| 5058 | } |
| 5059 | |
| 5060 | /* Create a Str node out of last_str, if needed. It will be the |
| 5061 | last node in our expression list. */ |
| 5062 | if (state->last_str) { |
| 5063 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5064 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5065 | goto error; |
| 5066 | } |
| 5067 | /* This has already been freed. */ |
| 5068 | assert(state->last_str == NULL); |
| 5069 | |
| 5070 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5071 | if (!seq) |
| 5072 | goto error; |
| 5073 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5074 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5075 | |
| 5076 | error: |
| 5077 | FstringParser_Dealloc(state); |
| 5078 | return NULL; |
| 5079 | } |
| 5080 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5081 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5082 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5083 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5084 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5085 | fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5086 | struct compiling *c, const node *n) |
| 5087 | { |
| 5088 | FstringParser state; |
| 5089 | |
| 5090 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5091 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5092 | c, n) < 0) { |
| 5093 | FstringParser_Dealloc(&state); |
| 5094 | return NULL; |
| 5095 | } |
| 5096 | |
| 5097 | return FstringParser_Finish(&state, c, n); |
| 5098 | } |
| 5099 | |
| 5100 | /* n is a Python string literal, including the bracketing quote |
| 5101 | characters, and r, b, u, &/or f prefixes (if any), and embedded |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5102 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5103 | decoded Python string object. If the string is an f-string, set |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5104 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5105 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5106 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5107 | static int |
| 5108 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5109 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5110 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5111 | size_t len; |
| 5112 | const char *s = STR(n); |
| 5113 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5114 | int fmode = 0; |
| 5115 | *bytesmode = 0; |
| 5116 | *rawmode = 0; |
| 5117 | *result = NULL; |
| 5118 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5119 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5120 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5121 | if (quote == 'b' || quote == 'B') { |
| 5122 | quote = *++s; |
| 5123 | *bytesmode = 1; |
| 5124 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5125 | else if (quote == 'u' || quote == 'U') { |
| 5126 | quote = *++s; |
| 5127 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5128 | else if (quote == 'r' || quote == 'R') { |
| 5129 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5130 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5131 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5132 | else if (quote == 'f' || quote == 'F') { |
| 5133 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5134 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5135 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5136 | else { |
| 5137 | break; |
| 5138 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5139 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5140 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5141 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5142 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5143 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5144 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5145 | if (quote != '\'' && quote != '\"') { |
| 5146 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5147 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5148 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5149 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5150 | s++; |
| 5151 | len = strlen(s); |
| 5152 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5153 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5154 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5155 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5156 | } |
| 5157 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5158 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5159 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5160 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5161 | } |
| 5162 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5163 | /* A triple quoted string. We've already skipped one quote at |
| 5164 | the start and one at the end of the string. Now skip the |
| 5165 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5166 | s += 2; |
| 5167 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5168 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5169 | if (s[--len] != quote || s[--len] != quote) { |
| 5170 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5171 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5172 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5173 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5174 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5175 | if (fmode) { |
| 5176 | /* Just return the bytes. The caller will parse the resulting |
| 5177 | string. */ |
| 5178 | *fstr = s; |
| 5179 | *fstrlen = len; |
| 5180 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5181 | } |
| 5182 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5184 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5185 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5186 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5187 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5188 | const char *ch; |
| 5189 | for (ch = s; *ch; ch++) { |
| 5190 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5191 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5192 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5193 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5194 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5195 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5196 | if (*rawmode) |
| 5197 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5198 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5199 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5200 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5201 | if (*rawmode) |
| 5202 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5203 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5204 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5205 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5206 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5209 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5210 | each STRING atom, and process it as needed. For bytes, just |
| 5211 | concatenate them together, and the result will be a Bytes node. For |
| 5212 | normal strings and f-strings, concatenate them together. The result |
| 5213 | will be a Str node if there were no f-strings; a FormattedValue |
| 5214 | node if there's just an f-string (with no leading or trailing |
| 5215 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5216 | any literals involved. */ |
| 5217 | static expr_ty |
| 5218 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5219 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5220 | int bytesmode = 0; |
| 5221 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5222 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5223 | |
| 5224 | FstringParser state; |
| 5225 | FstringParser_Init(&state); |
| 5226 | |
| 5227 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5228 | int this_bytesmode; |
| 5229 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5230 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5231 | const char *fstr; |
| 5232 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5233 | |
| 5234 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5235 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5236 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5237 | goto error; |
| 5238 | |
| 5239 | /* Check that we're not mixing bytes with unicode. */ |
| 5240 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5241 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5242 | /* s is NULL if the current string part is an f-string. */ |
| 5243 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5244 | goto error; |
| 5245 | } |
| 5246 | bytesmode = this_bytesmode; |
| 5247 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5248 | if (fstr != NULL) { |
| 5249 | int result; |
| 5250 | assert(s == NULL && !bytesmode); |
| 5251 | /* This is an f-string. Parse and concatenate it. */ |
| 5252 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5253 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5254 | if (result < 0) |
| 5255 | goto error; |
| 5256 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5257 | /* A string or byte string. */ |
| 5258 | assert(s != NULL && fstr == NULL); |
| 5259 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5260 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5261 | PyUnicode_CheckExact(s)); |
| 5262 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5263 | if (bytesmode) { |
| 5264 | /* For bytes, concat as we go. */ |
| 5265 | if (i == 0) { |
| 5266 | /* First time, just remember this value. */ |
| 5267 | bytes_str = s; |
| 5268 | } else { |
| 5269 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5270 | if (!bytes_str) |
| 5271 | goto error; |
| 5272 | } |
| 5273 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5274 | /* This is a regular string. Concatenate it. */ |
| 5275 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5276 | goto error; |
| 5277 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5278 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5279 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5280 | if (bytesmode) { |
| 5281 | /* Just return the bytes object and we're done. */ |
| 5282 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5283 | goto error; |
| 5284 | return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
| 5285 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5286 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5287 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5288 | assert(bytes_str == NULL); |
| 5289 | |
| 5290 | return FstringParser_Finish(&state, c, n); |
| 5291 | |
| 5292 | error: |
| 5293 | Py_XDECREF(bytes_str); |
| 5294 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5295 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5296 | } |