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> |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 14 | #include <stdbool.h> |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 16 | static int validate_stmts(asdl_seq *); |
| 17 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 18 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 19 | static int validate_stmt(stmt_ty); |
| 20 | static int validate_expr(expr_ty, expr_context_ty); |
| 21 | |
| 22 | static int |
| 23 | validate_comprehension(asdl_seq *gens) |
| 24 | { |
| 25 | int i; |
| 26 | if (!asdl_seq_LEN(gens)) { |
| 27 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 28 | return 0; |
| 29 | } |
| 30 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 31 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 32 | if (!validate_expr(comp->target, Store) || |
| 33 | !validate_expr(comp->iter, Load) || |
| 34 | !validate_exprs(comp->ifs, Load, 0)) |
| 35 | return 0; |
| 36 | } |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | static int |
| 41 | validate_slice(slice_ty slice) |
| 42 | { |
| 43 | switch (slice->kind) { |
| 44 | case Slice_kind: |
| 45 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 46 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 47 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 48 | case ExtSlice_kind: { |
| 49 | int i; |
| 50 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 51 | return 0; |
| 52 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 53 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 54 | return 0; |
| 55 | return 1; |
| 56 | } |
| 57 | case Index_kind: |
| 58 | return validate_expr(slice->v.Index.value, Load); |
| 59 | default: |
| 60 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | validate_keywords(asdl_seq *keywords) |
| 67 | { |
| 68 | int i; |
| 69 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 70 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 71 | return 0; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static int |
| 76 | validate_args(asdl_seq *args) |
| 77 | { |
| 78 | int i; |
| 79 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 80 | arg_ty arg = asdl_seq_GET(args, i); |
| 81 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 82 | return 0; |
| 83 | } |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | static const char * |
| 88 | expr_context_name(expr_context_ty ctx) |
| 89 | { |
| 90 | switch (ctx) { |
| 91 | case Load: |
| 92 | return "Load"; |
| 93 | case Store: |
| 94 | return "Store"; |
| 95 | case Del: |
| 96 | return "Del"; |
| 97 | case AugLoad: |
| 98 | return "AugLoad"; |
| 99 | case AugStore: |
| 100 | return "AugStore"; |
| 101 | case Param: |
| 102 | return "Param"; |
| 103 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 104 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | validate_arguments(arguments_ty args) |
| 110 | { |
| 111 | if (!validate_args(args->args)) |
| 112 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 113 | if (args->vararg && args->vararg->annotation |
| 114 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | if (!validate_args(args->kwonlyargs)) |
| 118 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 119 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 120 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 124 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 125 | return 0; |
| 126 | } |
| 127 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 128 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 129 | "kw_defaults on arguments"); |
| 130 | return 0; |
| 131 | } |
| 132 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 133 | } |
| 134 | |
| 135 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 136 | validate_constant(PyObject *value) |
| 137 | { |
| 138 | if (value == Py_None || value == Py_Ellipsis) |
| 139 | return 1; |
| 140 | |
| 141 | if (PyLong_CheckExact(value) |
| 142 | || PyFloat_CheckExact(value) |
| 143 | || PyComplex_CheckExact(value) |
| 144 | || PyBool_Check(value) |
| 145 | || PyUnicode_CheckExact(value) |
| 146 | || PyBytes_CheckExact(value)) |
| 147 | return 1; |
| 148 | |
| 149 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 150 | PyObject *it; |
| 151 | |
| 152 | it = PyObject_GetIter(value); |
| 153 | if (it == NULL) |
| 154 | return 0; |
| 155 | |
| 156 | while (1) { |
| 157 | PyObject *item = PyIter_Next(it); |
| 158 | if (item == NULL) { |
| 159 | if (PyErr_Occurred()) { |
| 160 | Py_DECREF(it); |
| 161 | return 0; |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | if (!validate_constant(item)) { |
| 167 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 168 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 171 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Py_DECREF(it); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 182 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 183 | { |
| 184 | int check_ctx = 1; |
| 185 | expr_context_ty actual_ctx; |
| 186 | |
| 187 | /* First check expression context. */ |
| 188 | switch (exp->kind) { |
| 189 | case Attribute_kind: |
| 190 | actual_ctx = exp->v.Attribute.ctx; |
| 191 | break; |
| 192 | case Subscript_kind: |
| 193 | actual_ctx = exp->v.Subscript.ctx; |
| 194 | break; |
| 195 | case Starred_kind: |
| 196 | actual_ctx = exp->v.Starred.ctx; |
| 197 | break; |
| 198 | case Name_kind: |
| 199 | actual_ctx = exp->v.Name.ctx; |
| 200 | break; |
| 201 | case List_kind: |
| 202 | actual_ctx = exp->v.List.ctx; |
| 203 | break; |
| 204 | case Tuple_kind: |
| 205 | actual_ctx = exp->v.Tuple.ctx; |
| 206 | break; |
| 207 | default: |
| 208 | if (ctx != Load) { |
| 209 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 210 | "assigned to in %s context", expr_context_name(ctx)); |
| 211 | return 0; |
| 212 | } |
| 213 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 214 | /* set actual_ctx to prevent gcc warning */ |
| 215 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 216 | } |
| 217 | if (check_ctx && actual_ctx != ctx) { |
| 218 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 219 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* Now validate expression. */ |
| 224 | switch (exp->kind) { |
| 225 | case BoolOp_kind: |
| 226 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 227 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 228 | return 0; |
| 229 | } |
| 230 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 231 | case BinOp_kind: |
| 232 | return validate_expr(exp->v.BinOp.left, Load) && |
| 233 | validate_expr(exp->v.BinOp.right, Load); |
| 234 | case UnaryOp_kind: |
| 235 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 236 | case Lambda_kind: |
| 237 | return validate_arguments(exp->v.Lambda.args) && |
| 238 | validate_expr(exp->v.Lambda.body, Load); |
| 239 | case IfExp_kind: |
| 240 | return validate_expr(exp->v.IfExp.test, Load) && |
| 241 | validate_expr(exp->v.IfExp.body, Load) && |
| 242 | validate_expr(exp->v.IfExp.orelse, Load); |
| 243 | case Dict_kind: |
| 244 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 245 | PyErr_SetString(PyExc_ValueError, |
| 246 | "Dict doesn't have the same number of keys as values"); |
| 247 | return 0; |
| 248 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 249 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 250 | dict literals, i.e. ``{**{a:b}}`` */ |
| 251 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 252 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 253 | case Set_kind: |
| 254 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 255 | #define COMP(NAME) \ |
| 256 | case NAME ## _kind: \ |
| 257 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 258 | validate_expr(exp->v.NAME.elt, Load); |
| 259 | COMP(ListComp) |
| 260 | COMP(SetComp) |
| 261 | COMP(GeneratorExp) |
| 262 | #undef COMP |
| 263 | case DictComp_kind: |
| 264 | return validate_comprehension(exp->v.DictComp.generators) && |
| 265 | validate_expr(exp->v.DictComp.key, Load) && |
| 266 | validate_expr(exp->v.DictComp.value, Load); |
| 267 | case Yield_kind: |
| 268 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 269 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 270 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 271 | case Await_kind: |
| 272 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 273 | case Compare_kind: |
| 274 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 275 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 276 | return 0; |
| 277 | } |
| 278 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 279 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 280 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 281 | "of comparators and operands"); |
| 282 | return 0; |
| 283 | } |
| 284 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 285 | validate_expr(exp->v.Compare.left, Load); |
| 286 | case Call_kind: |
| 287 | return validate_expr(exp->v.Call.func, Load) && |
| 288 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 289 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 290 | case Constant_kind: |
| 291 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 292 | PyErr_Format(PyExc_TypeError, |
| 293 | "got an invalid type in Constant: %s", |
| 294 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 298 | case JoinedStr_kind: |
| 299 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 300 | case FormattedValue_kind: |
| 301 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 302 | return 0; |
| 303 | if (exp->v.FormattedValue.format_spec) |
| 304 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 305 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 306 | case Attribute_kind: |
| 307 | return validate_expr(exp->v.Attribute.value, Load); |
| 308 | case Subscript_kind: |
| 309 | return validate_slice(exp->v.Subscript.slice) && |
| 310 | validate_expr(exp->v.Subscript.value, Load); |
| 311 | case Starred_kind: |
| 312 | return validate_expr(exp->v.Starred.value, ctx); |
| 313 | case List_kind: |
| 314 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 315 | case Tuple_kind: |
| 316 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 317 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 318 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 319 | return 1; |
| 320 | default: |
| 321 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 322 | return 0; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static int |
| 327 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 328 | { |
| 329 | if (asdl_seq_LEN(seq)) |
| 330 | return 1; |
| 331 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int |
| 336 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 337 | { |
| 338 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 339 | validate_exprs(targets, ctx, 0); |
| 340 | } |
| 341 | |
| 342 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 343 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 344 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 345 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static int |
| 349 | validate_stmt(stmt_ty stmt) |
| 350 | { |
| 351 | int i; |
| 352 | switch (stmt->kind) { |
| 353 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 354 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 355 | validate_arguments(stmt->v.FunctionDef.args) && |
| 356 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 357 | (!stmt->v.FunctionDef.returns || |
| 358 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 359 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 360 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 361 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 362 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 363 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 364 | case Return_kind: |
| 365 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 366 | case Delete_kind: |
| 367 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 368 | case Assign_kind: |
| 369 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 370 | validate_expr(stmt->v.Assign.value, Load); |
| 371 | case AugAssign_kind: |
| 372 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 373 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 374 | case AnnAssign_kind: |
| 375 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 376 | stmt->v.AnnAssign.simple) { |
| 377 | PyErr_SetString(PyExc_TypeError, |
| 378 | "AnnAssign with simple non-Name target"); |
| 379 | return 0; |
| 380 | } |
| 381 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 382 | (!stmt->v.AnnAssign.value || |
| 383 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 384 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 385 | case For_kind: |
| 386 | return validate_expr(stmt->v.For.target, Store) && |
| 387 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 388 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 389 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 390 | case AsyncFor_kind: |
| 391 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 392 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 393 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 394 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 395 | case While_kind: |
| 396 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 397 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 398 | validate_stmts(stmt->v.While.orelse); |
| 399 | case If_kind: |
| 400 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 401 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 402 | validate_stmts(stmt->v.If.orelse); |
| 403 | case With_kind: |
| 404 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 405 | return 0; |
| 406 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 407 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 408 | if (!validate_expr(item->context_expr, Load) || |
| 409 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 410 | return 0; |
| 411 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 412 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 413 | case AsyncWith_kind: |
| 414 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 415 | return 0; |
| 416 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 417 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 418 | if (!validate_expr(item->context_expr, Load) || |
| 419 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 420 | return 0; |
| 421 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 422 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 423 | case Raise_kind: |
| 424 | if (stmt->v.Raise.exc) { |
| 425 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 426 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 427 | } |
| 428 | if (stmt->v.Raise.cause) { |
| 429 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 430 | return 0; |
| 431 | } |
| 432 | return 1; |
| 433 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 434 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 435 | return 0; |
| 436 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 437 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 438 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 439 | return 0; |
| 440 | } |
| 441 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 442 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 443 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 444 | return 0; |
| 445 | } |
| 446 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 447 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 448 | if ((handler->v.ExceptHandler.type && |
| 449 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 450 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 454 | validate_stmts(stmt->v.Try.finalbody)) && |
| 455 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 456 | validate_stmts(stmt->v.Try.orelse)); |
| 457 | case Assert_kind: |
| 458 | return validate_expr(stmt->v.Assert.test, Load) && |
| 459 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 460 | case Import_kind: |
| 461 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 462 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 463 | if (stmt->v.ImportFrom.level < 0) { |
| 464 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 468 | case Global_kind: |
| 469 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 470 | case Nonlocal_kind: |
| 471 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 472 | case Expr_kind: |
| 473 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 474 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 475 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 476 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 477 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 478 | (!stmt->v.AsyncFunctionDef.returns || |
| 479 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 480 | case Pass_kind: |
| 481 | case Break_kind: |
| 482 | case Continue_kind: |
| 483 | return 1; |
| 484 | default: |
| 485 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 486 | return 0; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static int |
| 491 | validate_stmts(asdl_seq *seq) |
| 492 | { |
| 493 | int i; |
| 494 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 495 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 496 | if (stmt) { |
| 497 | if (!validate_stmt(stmt)) |
| 498 | return 0; |
| 499 | } |
| 500 | else { |
| 501 | PyErr_SetString(PyExc_ValueError, |
| 502 | "None disallowed in statement list"); |
| 503 | return 0; |
| 504 | } |
| 505 | } |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | static int |
| 510 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 511 | { |
| 512 | int i; |
| 513 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 514 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 515 | if (expr) { |
| 516 | if (!validate_expr(expr, ctx)) |
| 517 | return 0; |
| 518 | } |
| 519 | else if (!null_ok) { |
| 520 | PyErr_SetString(PyExc_ValueError, |
| 521 | "None disallowed in expression list"); |
| 522 | return 0; |
| 523 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 524 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 525 | } |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | int |
| 530 | PyAST_Validate(mod_ty mod) |
| 531 | { |
| 532 | int res = 0; |
| 533 | |
| 534 | switch (mod->kind) { |
| 535 | case Module_kind: |
| 536 | res = validate_stmts(mod->v.Module.body); |
| 537 | break; |
| 538 | case Interactive_kind: |
| 539 | res = validate_stmts(mod->v.Interactive.body); |
| 540 | break; |
| 541 | case Expression_kind: |
| 542 | res = validate_expr(mod->v.Expression.body, Load); |
| 543 | break; |
| 544 | case Suite_kind: |
| 545 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 546 | break; |
| 547 | default: |
| 548 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 549 | res = 0; |
| 550 | break; |
| 551 | } |
| 552 | return res; |
| 553 | } |
| 554 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 555 | /* 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] | 556 | #include "grammar.h" |
| 557 | #include "parsetok.h" |
| 558 | #include "graminit.h" |
| 559 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 560 | /* Data structure used internally */ |
| 561 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 562 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 563 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 564 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 568 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 569 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 570 | static asdl_seq *ast_for_suite(struct compiling *c, const node *n); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 571 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 572 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 573 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 574 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 575 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 576 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 577 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 578 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 579 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 580 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 581 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 582 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 583 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 584 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 585 | #define COMP_GENEXP 0 |
| 586 | #define COMP_LISTCOMP 1 |
| 587 | #define COMP_SETCOMP 2 |
| 588 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 589 | static int |
| 590 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 591 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 592 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 593 | if (!m) |
| 594 | return 0; |
| 595 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 596 | Py_DECREF(m); |
| 597 | if (!c->c_normalize) |
| 598 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 599 | return 1; |
| 600 | } |
| 601 | |
| 602 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 603 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 604 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 605 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 606 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 607 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 608 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 609 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 610 | /* Check whether there are non-ASCII characters in the |
| 611 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 612 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 613 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 614 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 615 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 616 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 617 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 618 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 619 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 620 | if (form == NULL) { |
| 621 | Py_DECREF(id); |
| 622 | return NULL; |
| 623 | } |
| 624 | PyObject *args[2] = {form, id}; |
| 625 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 626 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 627 | if (!id2) |
| 628 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 629 | if (!PyUnicode_Check(id2)) { |
| 630 | PyErr_Format(PyExc_TypeError, |
| 631 | "unicodedata.normalize() must return a string, not " |
| 632 | "%.200s", |
| 633 | Py_TYPE(id2)->tp_name); |
| 634 | Py_DECREF(id2); |
| 635 | return NULL; |
| 636 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 637 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 638 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 639 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 640 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 641 | Py_DECREF(id); |
| 642 | return NULL; |
| 643 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 644 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 647 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 648 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 649 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 650 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 651 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 652 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 653 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 655 | va_start(va, errmsg); |
| 656 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 657 | va_end(va); |
| 658 | if (!errstr) { |
| 659 | return 0; |
| 660 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 661 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 662 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 663 | Py_INCREF(Py_None); |
| 664 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 665 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 666 | tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 667 | if (!tmp) { |
| 668 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 669 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 670 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 671 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 672 | Py_DECREF(errstr); |
| 673 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 674 | if (value) { |
| 675 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 676 | Py_DECREF(value); |
| 677 | } |
| 678 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* num_stmts() returns number of contained statements. |
| 682 | |
| 683 | Use this routine to determine how big a sequence is needed for |
| 684 | the statements in a parse tree. Its raison d'etre is this bit of |
| 685 | grammar: |
| 686 | |
| 687 | stmt: simple_stmt | compound_stmt |
| 688 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 689 | |
| 690 | A simple_stmt can contain multiple small_stmt elements joined |
| 691 | by semicolons. If the arg is a simple_stmt, the number of |
| 692 | small_stmt elements is returned. |
| 693 | */ |
| 694 | |
| 695 | static int |
| 696 | num_stmts(const node *n) |
| 697 | { |
| 698 | int i, l; |
| 699 | node *ch; |
| 700 | |
| 701 | switch (TYPE(n)) { |
| 702 | case single_input: |
| 703 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 704 | return 0; |
| 705 | else |
| 706 | return num_stmts(CHILD(n, 0)); |
| 707 | case file_input: |
| 708 | l = 0; |
| 709 | for (i = 0; i < NCH(n); i++) { |
| 710 | ch = CHILD(n, i); |
| 711 | if (TYPE(ch) == stmt) |
| 712 | l += num_stmts(ch); |
| 713 | } |
| 714 | return l; |
| 715 | case stmt: |
| 716 | return num_stmts(CHILD(n, 0)); |
| 717 | case compound_stmt: |
| 718 | return 1; |
| 719 | case simple_stmt: |
| 720 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 721 | case suite: |
| 722 | if (NCH(n) == 1) |
| 723 | return num_stmts(CHILD(n, 0)); |
| 724 | else { |
| 725 | l = 0; |
| 726 | for (i = 2; i < (NCH(n) - 1); i++) |
| 727 | l += num_stmts(CHILD(n, i)); |
| 728 | return l; |
| 729 | } |
| 730 | default: { |
| 731 | char buf[128]; |
| 732 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 733 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 734 | TYPE(n), NCH(n)); |
| 735 | Py_FatalError(buf); |
| 736 | } |
| 737 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 738 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* Transform the CST rooted at node * to the appropriate AST |
| 742 | */ |
| 743 | |
| 744 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 745 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 746 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 747 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 748 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 749 | asdl_seq *stmts = NULL; |
| 750 | stmt_ty s; |
| 751 | node *ch; |
| 752 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 753 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 754 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 755 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 756 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 757 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 758 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 759 | |
| 760 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 761 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 762 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 763 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 764 | switch (TYPE(n)) { |
| 765 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 766 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 768 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 769 | for (i = 0; i < NCH(n) - 1; i++) { |
| 770 | ch = CHILD(n, i); |
| 771 | if (TYPE(ch) == NEWLINE) |
| 772 | continue; |
| 773 | REQ(ch, stmt); |
| 774 | num = num_stmts(ch); |
| 775 | if (num == 1) { |
| 776 | s = ast_for_stmt(&c, ch); |
| 777 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 778 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 779 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 780 | } |
| 781 | else { |
| 782 | ch = CHILD(ch, 0); |
| 783 | REQ(ch, simple_stmt); |
| 784 | for (j = 0; j < num; j++) { |
| 785 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 786 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 787 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 788 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 792 | res = Module(stmts, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 793 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | case eval_input: { |
| 795 | expr_ty testlist_ast; |
| 796 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 797 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 798 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 800 | goto out; |
| 801 | res = Expression(testlist_ast, arena); |
| 802 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | } |
| 804 | case single_input: |
| 805 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 806 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 807 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 808 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 809 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 810 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 811 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 812 | goto out; |
| 813 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 814 | } |
| 815 | else { |
| 816 | n = CHILD(n, 0); |
| 817 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 818 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 820 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 821 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 822 | s = ast_for_stmt(&c, n); |
| 823 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 824 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 825 | asdl_seq_SET(stmts, 0, s); |
| 826 | } |
| 827 | else { |
| 828 | /* Only a simple_stmt can contain multiple statements. */ |
| 829 | REQ(n, simple_stmt); |
| 830 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 831 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 832 | break; |
| 833 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 834 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 835 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | asdl_seq_SET(stmts, i / 2, s); |
| 837 | } |
| 838 | } |
| 839 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 840 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 841 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 842 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 843 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 844 | PyErr_Format(PyExc_SystemError, |
| 845 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 846 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 847 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 848 | out: |
| 849 | if (c.c_normalize) { |
| 850 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 851 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 852 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 855 | mod_ty |
| 856 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 857 | PyArena *arena) |
| 858 | { |
| 859 | mod_ty mod; |
| 860 | PyObject *filename; |
| 861 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 862 | if (filename == NULL) |
| 863 | return NULL; |
| 864 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 865 | Py_DECREF(filename); |
| 866 | return mod; |
| 867 | |
| 868 | } |
| 869 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 870 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 871 | */ |
| 872 | |
| 873 | static operator_ty |
| 874 | get_operator(const node *n) |
| 875 | { |
| 876 | switch (TYPE(n)) { |
| 877 | case VBAR: |
| 878 | return BitOr; |
| 879 | case CIRCUMFLEX: |
| 880 | return BitXor; |
| 881 | case AMPER: |
| 882 | return BitAnd; |
| 883 | case LEFTSHIFT: |
| 884 | return LShift; |
| 885 | case RIGHTSHIFT: |
| 886 | return RShift; |
| 887 | case PLUS: |
| 888 | return Add; |
| 889 | case MINUS: |
| 890 | return Sub; |
| 891 | case STAR: |
| 892 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 893 | case AT: |
| 894 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 895 | case SLASH: |
| 896 | return Div; |
| 897 | case DOUBLESLASH: |
| 898 | return FloorDiv; |
| 899 | case PERCENT: |
| 900 | return Mod; |
| 901 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 902 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 906 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 907 | "None", |
| 908 | "True", |
| 909 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 910 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 911 | NULL, |
| 912 | }; |
| 913 | |
| 914 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 915 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 916 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 917 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 918 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 919 | const char * const *p = FORBIDDEN; |
| 920 | if (!full_checks) { |
| 921 | /* In most cases, the parser will protect True, False, and None |
| 922 | from being assign to. */ |
| 923 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 924 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 925 | for (; *p; p++) { |
| 926 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 927 | ast_error(c, n, "cannot assign to %U", name); |
| 928 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | return 0; |
| 932 | } |
| 933 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 934 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 935 | |
| 936 | Only sets context for expr kinds that "can appear in assignment context" |
| 937 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 938 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 939 | */ |
| 940 | |
| 941 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 942 | 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] | 943 | { |
| 944 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 945 | /* If a particular expression type can't be used for assign / delete, |
| 946 | set expr_name to its name and an error message will be generated. |
| 947 | */ |
| 948 | const char* expr_name = NULL; |
| 949 | |
| 950 | /* The ast defines augmented store and load contexts, but the |
| 951 | implementation here doesn't actually use them. The code may be |
| 952 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 953 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 954 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 955 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 956 | */ |
| 957 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 958 | |
| 959 | switch (e->kind) { |
| 960 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 961 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 962 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 963 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 964 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 965 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 966 | e->v.Subscript.ctx = ctx; |
| 967 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 968 | case Starred_kind: |
| 969 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 970 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 971 | return 0; |
| 972 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 973 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 974 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 975 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 976 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 977 | } |
| 978 | e->v.Name.ctx = ctx; |
| 979 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 980 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 981 | e->v.List.ctx = ctx; |
| 982 | s = e->v.List.elts; |
| 983 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 984 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 985 | e->v.Tuple.ctx = ctx; |
| 986 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 987 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 988 | case Lambda_kind: |
| 989 | expr_name = "lambda"; |
| 990 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 991 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 992 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 993 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 994 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 995 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 996 | case UnaryOp_kind: |
| 997 | expr_name = "operator"; |
| 998 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 999 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1000 | expr_name = "generator expression"; |
| 1001 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1002 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1003 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1004 | expr_name = "yield expression"; |
| 1005 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1006 | case Await_kind: |
| 1007 | expr_name = "await expression"; |
| 1008 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1009 | case ListComp_kind: |
| 1010 | expr_name = "list comprehension"; |
| 1011 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1012 | case SetComp_kind: |
| 1013 | expr_name = "set comprehension"; |
| 1014 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1015 | case DictComp_kind: |
| 1016 | expr_name = "dict comprehension"; |
| 1017 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1018 | case Dict_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 1019 | expr_name = "dict display"; |
| 1020 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1021 | case Set_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 1022 | expr_name = "set display"; |
| 1023 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1024 | case JoinedStr_kind: |
| 1025 | case FormattedValue_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 1026 | expr_name = "f-string expression"; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1027 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1028 | case Constant_kind: { |
| 1029 | PyObject *value = e->v.Constant.value; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 1030 | if (value == Py_None || value == Py_False || value == Py_True |
| 1031 | || value == Py_Ellipsis) |
| 1032 | { |
| 1033 | return ast_error(c, n, "cannot %s %R", |
| 1034 | ctx == Store ? "assign to" : "delete", |
| 1035 | value); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1036 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 1037 | expr_name = "literal"; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1038 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1039 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1040 | case Compare_kind: |
| 1041 | expr_name = "comparison"; |
| 1042 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1043 | case IfExp_kind: |
| 1044 | expr_name = "conditional expression"; |
| 1045 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1046 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | PyErr_Format(PyExc_SystemError, |
| 1048 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1049 | e->kind, e->lineno); |
| 1050 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1051 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1052 | /* Check for error string set by switch */ |
| 1053 | if (expr_name) { |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 1054 | return ast_error(c, n, "cannot %s %s", |
| 1055 | ctx == Store ? "assign to" : "delete", |
| 1056 | expr_name); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1059 | /* 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] | 1060 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1061 | */ |
| 1062 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1063 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1064 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1065 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1066 | 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] | 1067 | return 0; |
| 1068 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1069 | } |
| 1070 | return 1; |
| 1071 | } |
| 1072 | |
| 1073 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1074 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1075 | { |
| 1076 | REQ(n, augassign); |
| 1077 | n = CHILD(n, 0); |
| 1078 | switch (STR(n)[0]) { |
| 1079 | case '+': |
| 1080 | return Add; |
| 1081 | case '-': |
| 1082 | return Sub; |
| 1083 | case '/': |
| 1084 | if (STR(n)[1] == '/') |
| 1085 | return FloorDiv; |
| 1086 | else |
| 1087 | return Div; |
| 1088 | case '%': |
| 1089 | return Mod; |
| 1090 | case '<': |
| 1091 | return LShift; |
| 1092 | case '>': |
| 1093 | return RShift; |
| 1094 | case '&': |
| 1095 | return BitAnd; |
| 1096 | case '^': |
| 1097 | return BitXor; |
| 1098 | case '|': |
| 1099 | return BitOr; |
| 1100 | case '*': |
| 1101 | if (STR(n)[1] == '*') |
| 1102 | return Pow; |
| 1103 | else |
| 1104 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1105 | case '@': |
| 1106 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1107 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1108 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1109 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1114 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1115 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1116 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1117 | |'is' 'not' |
| 1118 | */ |
| 1119 | REQ(n, comp_op); |
| 1120 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1121 | n = CHILD(n, 0); |
| 1122 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | case LESS: |
| 1124 | return Lt; |
| 1125 | case GREATER: |
| 1126 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1127 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1128 | return Eq; |
| 1129 | case LESSEQUAL: |
| 1130 | return LtE; |
| 1131 | case GREATEREQUAL: |
| 1132 | return GtE; |
| 1133 | case NOTEQUAL: |
| 1134 | return NotEq; |
| 1135 | case NAME: |
| 1136 | if (strcmp(STR(n), "in") == 0) |
| 1137 | return In; |
| 1138 | if (strcmp(STR(n), "is") == 0) |
| 1139 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1140 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1141 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1142 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1144 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1145 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | } |
| 1147 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1148 | /* handle "not in" and "is not" */ |
| 1149 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1150 | case NAME: |
| 1151 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1152 | return NotIn; |
| 1153 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1154 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1155 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1156 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1157 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1158 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1159 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1160 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1161 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1162 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1163 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1164 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | static asdl_seq * |
| 1168 | seq_for_testlist(struct compiling *c, const node *n) |
| 1169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1171 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1172 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1173 | asdl_seq *seq; |
| 1174 | expr_ty expression; |
| 1175 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1176 | 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] | 1177 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1178 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1179 | if (!seq) |
| 1180 | return NULL; |
| 1181 | |
| 1182 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1184 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1185 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1186 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1187 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1188 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1189 | |
| 1190 | assert(i / 2 < seq->size); |
| 1191 | asdl_seq_SET(seq, i / 2, expression); |
| 1192 | } |
| 1193 | return seq; |
| 1194 | } |
| 1195 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1196 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1197 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1198 | { |
| 1199 | identifier name; |
| 1200 | expr_ty annotation = NULL; |
| 1201 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1202 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1203 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1204 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1205 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1206 | name = NEW_IDENTIFIER(ch); |
| 1207 | if (!name) |
| 1208 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1209 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1210 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1211 | |
| 1212 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1213 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1214 | if (!annotation) |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1218 | 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] | 1219 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1220 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1221 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1224 | /* returns -1 if failed to handle keyword only arguments |
| 1225 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1226 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1227 | ^^^ |
| 1228 | start pointing here |
| 1229 | */ |
| 1230 | static int |
| 1231 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1232 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1233 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1234 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1235 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1236 | expr_ty expression, annotation; |
| 1237 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1238 | int i = start; |
| 1239 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1242 | 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] | 1243 | return -1; |
| 1244 | } |
| 1245 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1246 | while (i < NCH(n)) { |
| 1247 | ch = CHILD(n, i); |
| 1248 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1249 | case vfpdef: |
| 1250 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1251 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1252 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1253 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1254 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1255 | asdl_seq_SET(kwdefaults, j, expression); |
| 1256 | i += 2; /* '=' and test */ |
| 1257 | } |
| 1258 | else { /* setting NULL if no default value exists */ |
| 1259 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1260 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1261 | if (NCH(ch) == 3) { |
| 1262 | /* ch is NAME ':' test */ |
| 1263 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1264 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1265 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1266 | } |
| 1267 | else { |
| 1268 | annotation = NULL; |
| 1269 | } |
| 1270 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1271 | argname = NEW_IDENTIFIER(ch); |
| 1272 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1273 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1274 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1275 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1276 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 1277 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1278 | if (!arg) |
| 1279 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1280 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1281 | i += 2; /* the name and the comma */ |
| 1282 | break; |
| 1283 | case DOUBLESTAR: |
| 1284 | return i; |
| 1285 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1286 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1287 | goto error; |
| 1288 | } |
| 1289 | } |
| 1290 | return i; |
| 1291 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1293 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1294 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1295 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1296 | |
| 1297 | static arguments_ty |
| 1298 | ast_for_arguments(struct compiling *c, const node *n) |
| 1299 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1300 | /* This function handles both typedargslist (function definition) |
| 1301 | and varargslist (lambda definition). |
| 1302 | |
| 1303 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1304 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1305 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1306 | | '**' tfpdef [',']]] |
| 1307 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1308 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1309 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1310 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1311 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1312 | | '**' vfpdef [',']]] |
| 1313 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1314 | | '**' vfpdef [','] |
| 1315 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1316 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1317 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1318 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1319 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1320 | int nposdefaults = 0, found_default = 0; |
| 1321 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1322 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1323 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1324 | node *ch; |
| 1325 | |
| 1326 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1327 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1328 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1329 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1330 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1331 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1332 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1333 | /* First count the number of positional args & defaults. The |
| 1334 | variable i is the loop index for this for loop and the next. |
| 1335 | The next loop picks up where the first leaves off. |
| 1336 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1337 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1338 | ch = CHILD(n, i); |
| 1339 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1340 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1341 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1342 | if (i < NCH(n) && /* skip argument following star */ |
| 1343 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1344 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1345 | i++; |
| 1346 | } |
| 1347 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1348 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1349 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1350 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1351 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1352 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1354 | defaults for keyword only args */ |
| 1355 | for ( ; i < NCH(n); ++i) { |
| 1356 | ch = CHILD(n, i); |
| 1357 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1358 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1359 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1360 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1361 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1362 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1363 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1364 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1365 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1366 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1368 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1369 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1370 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1372 | since we set NULL as default for keyword only argument w/o default |
| 1373 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1374 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1375 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1376 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1377 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1378 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1379 | /* tfpdef: NAME [':' test] |
| 1380 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1381 | */ |
| 1382 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1383 | j = 0; /* index for defaults */ |
| 1384 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1385 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1386 | ch = CHILD(n, i); |
| 1387 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1388 | case tfpdef: |
| 1389 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1390 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1391 | anything other than EQUAL or a comma? */ |
| 1392 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1393 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1394 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1395 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1396 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1397 | assert(posdefaults != NULL); |
| 1398 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1399 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1400 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1401 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1402 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1403 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1404 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1405 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1406 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1407 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1408 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1409 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1410 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1411 | i += 2; /* the name and the comma */ |
| 1412 | break; |
| 1413 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1414 | if (i+1 >= NCH(n) || |
| 1415 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1416 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1417 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1418 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1420 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1421 | if (TYPE(ch) == COMMA) { |
| 1422 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1423 | i += 2; /* now follows keyword only arguments */ |
| 1424 | res = handle_keywordonly_args(c, n, i, |
| 1425 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1426 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1427 | i = res; /* res has new position to process */ |
| 1428 | } |
| 1429 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1430 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1431 | if (!vararg) |
| 1432 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1433 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1434 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1435 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1436 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1437 | int res = 0; |
| 1438 | res = handle_keywordonly_args(c, n, i, |
| 1439 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1440 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1441 | i = res; /* res has new position to process */ |
| 1442 | } |
| 1443 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1446 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1447 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1448 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1449 | if (!kwarg) |
| 1450 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1451 | i += 3; |
| 1452 | break; |
| 1453 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1454 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1455 | "unexpected node in varargslist: %d @ %d", |
| 1456 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1457 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1458 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1459 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1460 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static expr_ty |
| 1464 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1465 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1466 | expr_ty e; |
| 1467 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1468 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1469 | int i; |
| 1470 | |
| 1471 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1472 | |
| 1473 | lineno = LINENO(n); |
| 1474 | col_offset = n->n_col_offset; |
| 1475 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1476 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1477 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1478 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1479 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1480 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1481 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1482 | |
| 1483 | for (i = 2; i < NCH(n); i+=2) { |
| 1484 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1485 | if (!id) |
| 1486 | return NULL; |
| 1487 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1488 | if (!e) |
| 1489 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static expr_ty |
| 1496 | ast_for_decorator(struct compiling *c, const node *n) |
| 1497 | { |
| 1498 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1499 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1500 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1502 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1503 | REQ(CHILD(n, 0), AT); |
| 1504 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1507 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1508 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1510 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1511 | d = name_expr; |
| 1512 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1513 | } |
| 1514 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1515 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1516 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1517 | if (!d) |
| 1518 | return NULL; |
| 1519 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | } |
| 1521 | else { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 1522 | d = ast_for_call(c, CHILD(n, 3), name_expr, true); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1523 | if (!d) |
| 1524 | return NULL; |
| 1525 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static asdl_seq* |
| 1532 | ast_for_decorators(struct compiling *c, const node *n) |
| 1533 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1534 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1535 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1536 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1538 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1539 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1540 | if (!decorator_seq) |
| 1541 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1544 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1545 | if (!d) |
| 1546 | return NULL; |
| 1547 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1548 | } |
| 1549 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1553 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1554 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1555 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1556 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1557 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1558 | identifier name; |
| 1559 | arguments_ty args; |
| 1560 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1561 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1562 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1563 | |
| 1564 | REQ(n, funcdef); |
| 1565 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1566 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1567 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1568 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1569 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1570 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1571 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1572 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1573 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1574 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1575 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1576 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1577 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1578 | name_i += 2; |
| 1579 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1580 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1581 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1582 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1583 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1584 | if (is_async) |
| 1585 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 1586 | LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1587 | else |
| 1588 | return FunctionDef(name, args, body, decorator_seq, returns, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1589 | LINENO(n), n->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | static stmt_ty |
| 1593 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1594 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1595 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1596 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1597 | REQ(CHILD(n, 0), NAME); |
| 1598 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1599 | REQ(CHILD(n, 1), funcdef); |
| 1600 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1601 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1602 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static stmt_ty |
| 1606 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1607 | { |
| 1608 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1609 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1610 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | static stmt_ty |
| 1615 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1616 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1617 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1618 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1619 | REQ(CHILD(n, 0), NAME); |
| 1620 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1621 | |
| 1622 | switch (TYPE(CHILD(n, 1))) { |
| 1623 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1624 | return ast_for_funcdef_impl(c, n, NULL, |
| 1625 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1626 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1627 | return ast_for_with_stmt(c, n, |
| 1628 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1629 | |
| 1630 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1631 | return ast_for_for_stmt(c, n, |
| 1632 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1633 | |
| 1634 | default: |
| 1635 | PyErr_Format(PyExc_SystemError, |
| 1636 | "invalid async stament: %s", |
| 1637 | STR(CHILD(n, 1))); |
| 1638 | return NULL; |
| 1639 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1642 | static stmt_ty |
| 1643 | ast_for_decorated(struct compiling *c, const node *n) |
| 1644 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1645 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1646 | stmt_ty thing = NULL; |
| 1647 | asdl_seq *decorator_seq = NULL; |
| 1648 | |
| 1649 | REQ(n, decorated); |
| 1650 | |
| 1651 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1652 | if (!decorator_seq) |
| 1653 | return NULL; |
| 1654 | |
| 1655 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1656 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1660 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1661 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1662 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1663 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1664 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1665 | } |
| 1666 | return thing; |
| 1667 | } |
| 1668 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1669 | static expr_ty |
| 1670 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1671 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1672 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1673 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1674 | arguments_ty args; |
| 1675 | expr_ty expression; |
| 1676 | |
| 1677 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1678 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1679 | if (!args) |
| 1680 | return NULL; |
| 1681 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1682 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1684 | } |
| 1685 | else { |
| 1686 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1687 | if (!args) |
| 1688 | return NULL; |
| 1689 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1690 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1691 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1694 | 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] | 1695 | } |
| 1696 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1697 | static expr_ty |
| 1698 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1701 | expr_ty expression, body, orelse; |
| 1702 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1703 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1704 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1705 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1706 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1707 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1708 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1709 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1710 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1711 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1712 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1713 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1714 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1717 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1718 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1719 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1720 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1721 | */ |
| 1722 | |
| 1723 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1724 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1725 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1726 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1727 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1728 | count_comp_for: |
| 1729 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1730 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1731 | if (NCH(n) == 2) { |
| 1732 | REQ(CHILD(n, 0), NAME); |
| 1733 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1734 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1735 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1736 | else if (NCH(n) == 1) { |
| 1737 | n = CHILD(n, 0); |
| 1738 | } |
| 1739 | else { |
| 1740 | goto error; |
| 1741 | } |
| 1742 | if (NCH(n) == (5)) { |
| 1743 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1744 | } |
| 1745 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1746 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1747 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1748 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1749 | REQ(n, comp_iter); |
| 1750 | n = CHILD(n, 0); |
| 1751 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1752 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1753 | else if (TYPE(n) == comp_if) { |
| 1754 | if (NCH(n) == 3) { |
| 1755 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1756 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1757 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1758 | else |
| 1759 | return n_fors; |
| 1760 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1761 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1762 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1763 | /* Should never be reached */ |
| 1764 | PyErr_SetString(PyExc_SystemError, |
| 1765 | "logic error in count_comp_fors"); |
| 1766 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1769 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1770 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1771 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1772 | */ |
| 1773 | |
| 1774 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1775 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1776 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1777 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1778 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1779 | while (1) { |
| 1780 | REQ(n, comp_iter); |
| 1781 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1782 | return n_ifs; |
| 1783 | n = CHILD(n, 0); |
| 1784 | REQ(n, comp_if); |
| 1785 | n_ifs++; |
| 1786 | if (NCH(n) == 2) |
| 1787 | return n_ifs; |
| 1788 | n = CHILD(n, 2); |
| 1789 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1792 | static asdl_seq * |
| 1793 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1794 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1795 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1796 | asdl_seq *comps; |
| 1797 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1798 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1799 | if (n_fors == -1) |
| 1800 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1801 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1802 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1803 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1804 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1805 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1806 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1807 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1808 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1809 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1810 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1811 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1812 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1814 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1816 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1817 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1818 | REQ(CHILD(n, 0), NAME); |
| 1819 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1820 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1821 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1822 | else { |
| 1823 | sync_n = CHILD(n, 0); |
| 1824 | } |
| 1825 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1826 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1827 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1828 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1829 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1830 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1831 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1832 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1834 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1835 | /* Check the # of children rather than the length of t, since |
| 1836 | (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] | 1837 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1838 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1839 | comp = comprehension(first, expression, NULL, |
| 1840 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1841 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1842 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1843 | first->col_offset, c->c_arena), |
| 1844 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1845 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1846 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1847 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1848 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1849 | int j, n_ifs; |
| 1850 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1852 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1853 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1854 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1855 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1857 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1858 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1859 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1860 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1861 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1862 | REQ(n, comp_iter); |
| 1863 | n = CHILD(n, 0); |
| 1864 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1866 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1867 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1868 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1869 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1870 | if (NCH(n) == 3) |
| 1871 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1872 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1873 | /* on exit, must guarantee that n is a comp_for */ |
| 1874 | if (TYPE(n) == comp_iter) |
| 1875 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1876 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1877 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1878 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1879 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1880 | return comps; |
| 1881 | } |
| 1882 | |
| 1883 | static expr_ty |
| 1884 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1885 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1886 | /* testlist_comp: (test|star_expr) |
| 1887 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1888 | expr_ty elt; |
| 1889 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1890 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1892 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1894 | ch = CHILD(n, 0); |
| 1895 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1896 | if (!elt) |
| 1897 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1898 | if (elt->kind == Starred_kind) { |
| 1899 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1900 | return NULL; |
| 1901 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1903 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1904 | if (!comps) |
| 1905 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1906 | |
| 1907 | if (type == COMP_GENEXP) |
| 1908 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1909 | else if (type == COMP_LISTCOMP) |
| 1910 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1911 | else if (type == COMP_SETCOMP) |
| 1912 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1913 | else |
| 1914 | /* Should never happen */ |
| 1915 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1918 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1919 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1920 | * elements. Iff successful, nonzero is returned. |
| 1921 | */ |
| 1922 | static int |
| 1923 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1924 | expr_ty *key, expr_ty *value) |
| 1925 | { |
| 1926 | expr_ty expression; |
| 1927 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1928 | assert(NCH(n) - *i >= 2); |
| 1929 | |
| 1930 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1931 | if (!expression) |
| 1932 | return 0; |
| 1933 | *key = NULL; |
| 1934 | *value = expression; |
| 1935 | |
| 1936 | *i += 2; |
| 1937 | } |
| 1938 | else { |
| 1939 | assert(NCH(n) - *i >= 3); |
| 1940 | |
| 1941 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1942 | if (!expression) |
| 1943 | return 0; |
| 1944 | *key = expression; |
| 1945 | |
| 1946 | REQ(CHILD(n, *i + 1), COLON); |
| 1947 | |
| 1948 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1949 | if (!expression) |
| 1950 | return 0; |
| 1951 | *value = expression; |
| 1952 | |
| 1953 | *i += 3; |
| 1954 | } |
| 1955 | return 1; |
| 1956 | } |
| 1957 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1958 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1959 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1960 | { |
| 1961 | expr_ty key, value; |
| 1962 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1963 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1965 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1966 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1967 | assert(key); |
| 1968 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1970 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1971 | if (!comps) |
| 1972 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1974 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1975 | } |
| 1976 | |
| 1977 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1978 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 1979 | { |
| 1980 | int i; |
| 1981 | int j; |
| 1982 | int size; |
| 1983 | asdl_seq *keys, *values; |
| 1984 | |
| 1985 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 1986 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 1987 | if (!keys) |
| 1988 | return NULL; |
| 1989 | |
| 1990 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 1991 | if (!values) |
| 1992 | return NULL; |
| 1993 | |
| 1994 | j = 0; |
| 1995 | for (i = 0; i < NCH(n); i++) { |
| 1996 | expr_ty key, value; |
| 1997 | |
| 1998 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 1999 | return NULL; |
| 2000 | asdl_seq_SET(keys, j, key); |
| 2001 | asdl_seq_SET(values, j, value); |
| 2002 | |
| 2003 | j++; |
| 2004 | } |
| 2005 | keys->size = j; |
| 2006 | values->size = j; |
| 2007 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2008 | } |
| 2009 | |
| 2010 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2011 | ast_for_genexp(struct compiling *c, const node *n) |
| 2012 | { |
| 2013 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2014 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | static expr_ty |
| 2018 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2019 | { |
| 2020 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2021 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | static expr_ty |
| 2025 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2026 | { |
| 2027 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2028 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2031 | static expr_ty |
| 2032 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2033 | { |
| 2034 | int i; |
| 2035 | int size; |
| 2036 | asdl_seq *elts; |
| 2037 | |
| 2038 | assert(TYPE(n) == (dictorsetmaker)); |
| 2039 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2040 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2041 | if (!elts) |
| 2042 | return NULL; |
| 2043 | for (i = 0; i < NCH(n); i += 2) { |
| 2044 | expr_ty expression; |
| 2045 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2046 | if (!expression) |
| 2047 | return NULL; |
| 2048 | asdl_seq_SET(elts, i / 2, expression); |
| 2049 | } |
| 2050 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2051 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2052 | |
| 2053 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2054 | ast_for_atom(struct compiling *c, const node *n) |
| 2055 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2056 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2057 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2058 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2059 | */ |
| 2060 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2062 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2063 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2064 | PyObject *name; |
| 2065 | const char *s = STR(ch); |
| 2066 | size_t len = strlen(s); |
| 2067 | if (len >= 4 && len <= 5) { |
| 2068 | if (!strcmp(s, "None")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2069 | return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2070 | if (!strcmp(s, "True")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2071 | return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2072 | if (!strcmp(s, "False")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2073 | return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2074 | } |
| 2075 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2076 | if (!name) |
| 2077 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2078 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2079 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2080 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2081 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2082 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2083 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2084 | const char *errtype = NULL; |
| 2085 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2086 | errtype = "unicode error"; |
| 2087 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2088 | errtype = "value error"; |
| 2089 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2090 | PyObject *type, *value, *tback, *errstr; |
| 2091 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2092 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 2093 | if (errstr) { |
| 2094 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2095 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2096 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 2097 | else { |
| 2098 | PyErr_Clear(); |
| 2099 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2100 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2101 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2102 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2103 | Py_XDECREF(tback); |
| 2104 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2105 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2106 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2107 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2108 | } |
| 2109 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2110 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2111 | if (!pynum) |
| 2112 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2113 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2114 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2115 | Py_DECREF(pynum); |
| 2116 | return NULL; |
| 2117 | } |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2118 | return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2119 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2120 | case ELLIPSIS: /* Ellipsis */ |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2121 | return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2122 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2123 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2125 | if (TYPE(ch) == RPAR) |
| 2126 | 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] | 2127 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2128 | if (TYPE(ch) == yield_expr) |
| 2129 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2132 | if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2133 | return ast_for_genexp(c, ch); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2134 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2135 | return ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2136 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2137 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2138 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2139 | if (TYPE(ch) == RSQB) |
| 2140 | 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] | 2141 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2142 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2143 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2144 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2145 | if (!elts) |
| 2146 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2147 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2148 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2149 | } |
| 2150 | else |
| 2151 | return ast_for_listcomp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2152 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2153 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2154 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2155 | * ((test | '*' test) |
| 2156 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2157 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2158 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2159 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2160 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2161 | 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] | 2162 | } |
| 2163 | else { |
| 2164 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2165 | if (NCH(ch) == 1 || |
| 2166 | (NCH(ch) > 1 && |
| 2167 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2168 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2169 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2170 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2171 | else if (NCH(ch) > 1 && |
| 2172 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2173 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2174 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2175 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2176 | else if (NCH(ch) > 3 - is_dict && |
| 2177 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2178 | /* It's a dictionary comprehension. */ |
| 2179 | if (is_dict) { |
| 2180 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2181 | "dict comprehension"); |
| 2182 | return NULL; |
| 2183 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2184 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2185 | } |
| 2186 | else { |
| 2187 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2188 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2189 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2190 | if (res) { |
| 2191 | res->lineno = LINENO(n); |
| 2192 | res->col_offset = n->n_col_offset; |
| 2193 | } |
| 2194 | return res; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2195 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2196 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2197 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2198 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2199 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | static slice_ty |
| 2204 | ast_for_slice(struct compiling *c, const node *n) |
| 2205 | { |
| 2206 | node *ch; |
| 2207 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2208 | |
| 2209 | REQ(n, subscript); |
| 2210 | |
| 2211 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2212 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2213 | sliceop: ':' [test] |
| 2214 | */ |
| 2215 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2216 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2217 | /* 'step' variable hold no significance in terms of being used over |
| 2218 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2220 | if (!step) |
| 2221 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2223 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2227 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2228 | if (!lower) |
| 2229 | return NULL; |
| 2230 | } |
| 2231 | |
| 2232 | /* If there's an upper bound it's in the second or third position. */ |
| 2233 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2234 | if (NCH(n) > 1) { |
| 2235 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2236 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2237 | if (TYPE(n2) == test) { |
| 2238 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2239 | if (!upper) |
| 2240 | return NULL; |
| 2241 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2242 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2243 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2244 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2245 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2246 | if (TYPE(n2) == test) { |
| 2247 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2248 | if (!upper) |
| 2249 | return NULL; |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | ch = CHILD(n, NCH(n) - 1); |
| 2254 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2255 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2256 | ch = CHILD(ch, 1); |
| 2257 | if (TYPE(ch) == test) { |
| 2258 | step = ast_for_expr(c, ch); |
| 2259 | if (!step) |
| 2260 | return NULL; |
| 2261 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2262 | } |
| 2263 | } |
| 2264 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2265 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | static expr_ty |
| 2269 | ast_for_binop(struct compiling *c, const node *n) |
| 2270 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2271 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2273 | BinOp(BinOp(A, op, B), op, C). |
| 2274 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2275 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2276 | int i, nops; |
| 2277 | expr_ty expr1, expr2, result; |
| 2278 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2279 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2280 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2281 | if (!expr1) |
| 2282 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2283 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2284 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2285 | if (!expr2) |
| 2286 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2287 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2288 | newoperator = get_operator(CHILD(n, 1)); |
| 2289 | if (!newoperator) |
| 2290 | return NULL; |
| 2291 | |
| 2292 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2293 | c->c_arena); |
| 2294 | if (!result) |
| 2295 | return NULL; |
| 2296 | |
| 2297 | nops = (NCH(n) - 1) / 2; |
| 2298 | for (i = 1; i < nops; i++) { |
| 2299 | expr_ty tmp_result, tmp; |
| 2300 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2301 | |
| 2302 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2303 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2304 | return NULL; |
| 2305 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2306 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2307 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2308 | return NULL; |
| 2309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2311 | LINENO(next_oper), next_oper->n_col_offset, |
| 2312 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2314 | return NULL; |
| 2315 | result = tmp_result; |
| 2316 | } |
| 2317 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2320 | static expr_ty |
| 2321 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2323 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2324 | subscriptlist: subscript (',' subscript)* [','] |
| 2325 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2326 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2327 | REQ(n, trailer); |
| 2328 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2329 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2330 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2331 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2332 | else |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2333 | return ast_for_call(c, CHILD(n, 1), left_expr, true); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2334 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2335 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2336 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2337 | if (!attr_id) |
| 2338 | return NULL; |
| 2339 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2340 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2341 | } |
| 2342 | else { |
| 2343 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2344 | REQ(CHILD(n, 2), RSQB); |
| 2345 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2346 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2347 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2348 | if (!slc) |
| 2349 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2350 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2351 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2352 | } |
| 2353 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2355 | by treating the sequence as a tuple literal if there are |
| 2356 | no slice features. |
| 2357 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2358 | int j; |
| 2359 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2360 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2361 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2362 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2363 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2364 | if (!slices) |
| 2365 | return NULL; |
| 2366 | for (j = 0; j < NCH(n); j += 2) { |
| 2367 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2368 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2369 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2370 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2371 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2372 | asdl_seq_SET(slices, j / 2, slc); |
| 2373 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2374 | if (!simple) { |
| 2375 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2376 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2377 | } |
| 2378 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2379 | 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] | 2380 | if (!elts) |
| 2381 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2382 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2383 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2384 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2385 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2386 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2387 | 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] | 2388 | if (!e) |
| 2389 | return NULL; |
| 2390 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2391 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2392 | } |
| 2393 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2397 | ast_for_factor(struct compiling *c, const node *n) |
| 2398 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2399 | expr_ty expression; |
| 2400 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2401 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2402 | if (!expression) |
| 2403 | return NULL; |
| 2404 | |
| 2405 | switch (TYPE(CHILD(n, 0))) { |
| 2406 | case PLUS: |
| 2407 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2408 | c->c_arena); |
| 2409 | case MINUS: |
| 2410 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2411 | c->c_arena); |
| 2412 | case TILDE: |
| 2413 | return UnaryOp(Invert, expression, LINENO(n), |
| 2414 | n->n_col_offset, c->c_arena); |
| 2415 | } |
| 2416 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2417 | TYPE(CHILD(n, 0))); |
| 2418 | return NULL; |
| 2419 | } |
| 2420 | |
| 2421 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2422 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2423 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2424 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2425 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2426 | |
| 2427 | REQ(n, atom_expr); |
| 2428 | nch = NCH(n); |
| 2429 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2430 | 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] | 2431 | start = 1; |
| 2432 | assert(nch > 1); |
| 2433 | } |
| 2434 | |
| 2435 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2436 | if (!e) |
| 2437 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2438 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2439 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2440 | if (start && nch == 2) { |
| 2441 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2442 | } |
| 2443 | |
| 2444 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2445 | node *ch = CHILD(n, i); |
| 2446 | if (TYPE(ch) != trailer) |
| 2447 | break; |
| 2448 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2449 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2450 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2451 | tmp->lineno = e->lineno; |
| 2452 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2453 | e = tmp; |
| 2454 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2455 | |
| 2456 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2457 | /* there was an 'await' */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2458 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2459 | } |
| 2460 | else { |
| 2461 | return e; |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | static expr_ty |
| 2466 | ast_for_power(struct compiling *c, const node *n) |
| 2467 | { |
| 2468 | /* power: atom trailer* ('**' factor)* |
| 2469 | */ |
| 2470 | expr_ty e; |
| 2471 | REQ(n, power); |
| 2472 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2473 | if (!e) |
| 2474 | return NULL; |
| 2475 | if (NCH(n) == 1) |
| 2476 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2477 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2478 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2479 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2480 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2481 | 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] | 2482 | } |
| 2483 | return e; |
| 2484 | } |
| 2485 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2486 | static expr_ty |
| 2487 | ast_for_starred(struct compiling *c, const node *n) |
| 2488 | { |
| 2489 | expr_ty tmp; |
| 2490 | REQ(n, star_expr); |
| 2491 | |
| 2492 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2493 | if (!tmp) |
| 2494 | return NULL; |
| 2495 | |
| 2496 | /* The Load context is changed later. */ |
| 2497 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2498 | } |
| 2499 | |
| 2500 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2501 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2502 | */ |
| 2503 | |
| 2504 | static expr_ty |
| 2505 | ast_for_expr(struct compiling *c, const node *n) |
| 2506 | { |
| 2507 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2508 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2509 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2511 | and_test: not_test ('and' not_test)* |
| 2512 | not_test: 'not' not_test | comparison |
| 2513 | comparison: expr (comp_op expr)* |
| 2514 | expr: xor_expr ('|' xor_expr)* |
| 2515 | xor_expr: and_expr ('^' and_expr)* |
| 2516 | and_expr: shift_expr ('&' shift_expr)* |
| 2517 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2518 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2519 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2520 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2521 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2522 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2523 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2524 | */ |
| 2525 | |
| 2526 | asdl_seq *seq; |
| 2527 | int i; |
| 2528 | |
| 2529 | loop: |
| 2530 | switch (TYPE(n)) { |
| 2531 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2532 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2533 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2534 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2535 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2536 | else if (NCH(n) > 1) |
| 2537 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2538 | /* Fallthrough */ |
| 2539 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2540 | case and_test: |
| 2541 | if (NCH(n) == 1) { |
| 2542 | n = CHILD(n, 0); |
| 2543 | goto loop; |
| 2544 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2545 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2546 | if (!seq) |
| 2547 | return NULL; |
| 2548 | for (i = 0; i < NCH(n); i += 2) { |
| 2549 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2550 | if (!e) |
| 2551 | return NULL; |
| 2552 | asdl_seq_SET(seq, i / 2, e); |
| 2553 | } |
| 2554 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2555 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2556 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2557 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2558 | 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] | 2559 | case not_test: |
| 2560 | if (NCH(n) == 1) { |
| 2561 | n = CHILD(n, 0); |
| 2562 | goto loop; |
| 2563 | } |
| 2564 | else { |
| 2565 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2566 | if (!expression) |
| 2567 | return NULL; |
| 2568 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2569 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2570 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2571 | } |
| 2572 | case comparison: |
| 2573 | if (NCH(n) == 1) { |
| 2574 | n = CHILD(n, 0); |
| 2575 | goto loop; |
| 2576 | } |
| 2577 | else { |
| 2578 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2579 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2580 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2581 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2582 | if (!ops) |
| 2583 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2584 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2585 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2586 | return NULL; |
| 2587 | } |
| 2588 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2589 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2590 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2591 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2592 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2593 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2594 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2595 | |
| 2596 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2597 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2598 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2599 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2601 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2602 | asdl_seq_SET(cmps, i / 2, expression); |
| 2603 | } |
| 2604 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2605 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2606 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2607 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2609 | return Compare(expression, ops, cmps, LINENO(n), |
| 2610 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2611 | } |
| 2612 | break; |
| 2613 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2614 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2615 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2616 | /* The next five cases all handle BinOps. The main body of code |
| 2617 | is the same in each case, but the switch turned inside out to |
| 2618 | reuse the code for each type of operator. |
| 2619 | */ |
| 2620 | case expr: |
| 2621 | case xor_expr: |
| 2622 | case and_expr: |
| 2623 | case shift_expr: |
| 2624 | case arith_expr: |
| 2625 | case term: |
| 2626 | if (NCH(n) == 1) { |
| 2627 | n = CHILD(n, 0); |
| 2628 | goto loop; |
| 2629 | } |
| 2630 | return ast_for_binop(c, n); |
| 2631 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2632 | node *an = NULL; |
| 2633 | node *en = NULL; |
| 2634 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2635 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2636 | if (NCH(n) > 1) |
| 2637 | an = CHILD(n, 1); /* yield_arg */ |
| 2638 | if (an) { |
| 2639 | en = CHILD(an, NCH(an) - 1); |
| 2640 | if (NCH(an) == 2) { |
| 2641 | is_from = 1; |
| 2642 | exp = ast_for_expr(c, en); |
| 2643 | } |
| 2644 | else |
| 2645 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2646 | if (!exp) |
| 2647 | return NULL; |
| 2648 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2649 | if (is_from) |
| 2650 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2651 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2652 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2653 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | if (NCH(n) == 1) { |
| 2655 | n = CHILD(n, 0); |
| 2656 | goto loop; |
| 2657 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2658 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2659 | case power: |
| 2660 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2661 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2662 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2663 | return NULL; |
| 2664 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2665 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2666 | return NULL; |
| 2667 | } |
| 2668 | |
| 2669 | static expr_ty |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2670 | ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2671 | { |
| 2672 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2673 | arglist: argument (',' argument)* [','] |
| 2674 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2675 | */ |
| 2676 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2677 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2678 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2679 | asdl_seq *args; |
| 2680 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2681 | |
| 2682 | REQ(n, arglist); |
| 2683 | |
| 2684 | nargs = 0; |
| 2685 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2686 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2687 | node *ch = CHILD(n, i); |
| 2688 | if (TYPE(ch) == argument) { |
| 2689 | if (NCH(ch) == 1) |
| 2690 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2691 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2692 | nargs++; |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2693 | if (!allowgen) { |
| 2694 | ast_error(c, ch, "invalid syntax"); |
| 2695 | return NULL; |
| 2696 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2697 | if (NCH(n) > 1) { |
| 2698 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2699 | return NULL; |
| 2700 | } |
| 2701 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2702 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2703 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2705 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2706 | nkeywords++; |
| 2707 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2708 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2709 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2710 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2711 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2712 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2713 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2714 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2715 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2716 | |
| 2717 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2718 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2719 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2720 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2721 | node *ch = CHILD(n, i); |
| 2722 | if (TYPE(ch) == argument) { |
| 2723 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2724 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2725 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2726 | /* a positional argument */ |
| 2727 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2728 | if (ndoublestars) { |
| 2729 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2730 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2731 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2732 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2733 | else { |
| 2734 | ast_error(c, chch, |
| 2735 | "positional argument follows " |
| 2736 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2737 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2738 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2739 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2740 | e = ast_for_expr(c, chch); |
| 2741 | if (!e) |
| 2742 | return NULL; |
| 2743 | asdl_seq_SET(args, nargs++, e); |
| 2744 | } |
| 2745 | else if (TYPE(chch) == STAR) { |
| 2746 | /* an iterable argument unpacking */ |
| 2747 | expr_ty starred; |
| 2748 | if (ndoublestars) { |
| 2749 | ast_error(c, chch, |
| 2750 | "iterable argument unpacking follows " |
| 2751 | "keyword argument unpacking"); |
| 2752 | return NULL; |
| 2753 | } |
| 2754 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2755 | if (!e) |
| 2756 | return NULL; |
| 2757 | starred = Starred(e, Load, LINENO(chch), |
| 2758 | chch->n_col_offset, |
| 2759 | c->c_arena); |
| 2760 | if (!starred) |
| 2761 | return NULL; |
| 2762 | asdl_seq_SET(args, nargs++, starred); |
| 2763 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2764 | } |
| 2765 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2766 | /* a keyword argument unpacking */ |
| 2767 | keyword_ty kw; |
| 2768 | i++; |
| 2769 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2770 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2771 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2772 | kw = keyword(NULL, e, c->c_arena); |
| 2773 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2774 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2776 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2777 | /* the lone generator expression */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2778 | e = ast_for_genexp(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2779 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2780 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2781 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2782 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2783 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2784 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2785 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2786 | identifier key, tmp; |
| 2787 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2788 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2789 | // To remain LL(1), the grammar accepts any test (basically, any |
| 2790 | // expression) in the keyword slot of a call site. So, we need |
| 2791 | // to manually enforce that the keyword is a NAME here. |
| 2792 | static const int name_tree[] = { |
| 2793 | test, |
| 2794 | or_test, |
| 2795 | and_test, |
| 2796 | not_test, |
| 2797 | comparison, |
| 2798 | expr, |
| 2799 | xor_expr, |
| 2800 | and_expr, |
| 2801 | shift_expr, |
| 2802 | arith_expr, |
| 2803 | term, |
| 2804 | factor, |
| 2805 | power, |
| 2806 | atom_expr, |
| 2807 | atom, |
| 2808 | 0, |
| 2809 | }; |
| 2810 | node *expr_node = chch; |
| 2811 | for (int i = 0; name_tree[i]; i++) { |
| 2812 | if (TYPE(expr_node) != name_tree[i]) |
| 2813 | break; |
| 2814 | if (NCH(expr_node) != 1) |
| 2815 | break; |
| 2816 | expr_node = CHILD(expr_node, 0); |
| 2817 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 2818 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2819 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 2820 | "expression cannot contain assignment, " |
| 2821 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2822 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2823 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2824 | key = new_identifier(STR(expr_node), c); |
| 2825 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2826 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2828 | if (forbidden_name(c, key, chch, 1)) { |
| 2829 | return NULL; |
| 2830 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2831 | for (k = 0; k < nkeywords; k++) { |
| 2832 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2833 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2834 | ast_error(c, chch, |
| 2835 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2836 | return NULL; |
| 2837 | } |
| 2838 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2839 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2840 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2841 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2842 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2843 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2844 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2845 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2846 | } |
| 2847 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2850 | 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] | 2851 | } |
| 2852 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2853 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2854 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2855 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2856 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2857 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2858 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2859 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2860 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2861 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2862 | } |
| 2863 | else { |
| 2864 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2865 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2866 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2867 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2868 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | else { |
| 2870 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2871 | if (!tmp) |
| 2872 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2873 | 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] | 2874 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2877 | static stmt_ty |
| 2878 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2879 | { |
| 2880 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2881 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2882 | ('=' (yield_expr|testlist_star_expr))*) |
| 2883 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2884 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2885 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2886 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2887 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | */ |
| 2889 | |
| 2890 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2891 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2892 | if (!e) |
| 2893 | return NULL; |
| 2894 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2895 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2896 | } |
| 2897 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2898 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2899 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2900 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2901 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2902 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2903 | if (!expr1) |
| 2904 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2905 | if(!set_context(c, expr1, Store, ch)) |
| 2906 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2907 | /* set_context checks that most expressions are not the left side. |
| 2908 | Augmented assignments can only have a name, a subscript, or an |
| 2909 | attribute on the left, though, so we have to explicitly check for |
| 2910 | those. */ |
| 2911 | switch (expr1->kind) { |
| 2912 | case Name_kind: |
| 2913 | case Attribute_kind: |
| 2914 | case Subscript_kind: |
| 2915 | break; |
| 2916 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2917 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2918 | return NULL; |
| 2919 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2920 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2921 | ch = CHILD(n, 2); |
| 2922 | if (TYPE(ch) == testlist) |
| 2923 | expr2 = ast_for_testlist(c, ch); |
| 2924 | else |
| 2925 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2926 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2927 | return NULL; |
| 2928 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2929 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2930 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2931 | return NULL; |
| 2932 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2933 | 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] | 2934 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2935 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2936 | expr_ty expr1, expr2, expr3; |
| 2937 | node *ch = CHILD(n, 0); |
| 2938 | node *deep, *ann = CHILD(n, 1); |
| 2939 | int simple = 1; |
| 2940 | |
| 2941 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2942 | deep = ch; |
| 2943 | while (NCH(deep) == 1) { |
| 2944 | deep = CHILD(deep, 0); |
| 2945 | } |
| 2946 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2947 | simple = 0; |
| 2948 | } |
| 2949 | expr1 = ast_for_testlist(c, ch); |
| 2950 | if (!expr1) { |
| 2951 | return NULL; |
| 2952 | } |
| 2953 | switch (expr1->kind) { |
| 2954 | case Name_kind: |
| 2955 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2956 | return NULL; |
| 2957 | } |
| 2958 | expr1->v.Name.ctx = Store; |
| 2959 | break; |
| 2960 | case Attribute_kind: |
| 2961 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2962 | return NULL; |
| 2963 | } |
| 2964 | expr1->v.Attribute.ctx = Store; |
| 2965 | break; |
| 2966 | case Subscript_kind: |
| 2967 | expr1->v.Subscript.ctx = Store; |
| 2968 | break; |
| 2969 | case List_kind: |
| 2970 | ast_error(c, ch, |
| 2971 | "only single target (not list) can be annotated"); |
| 2972 | return NULL; |
| 2973 | case Tuple_kind: |
| 2974 | ast_error(c, ch, |
| 2975 | "only single target (not tuple) can be annotated"); |
| 2976 | return NULL; |
| 2977 | default: |
| 2978 | ast_error(c, ch, |
| 2979 | "illegal target for annotation"); |
| 2980 | return NULL; |
| 2981 | } |
| 2982 | |
| 2983 | if (expr1->kind != Name_kind) { |
| 2984 | simple = 0; |
| 2985 | } |
| 2986 | ch = CHILD(ann, 1); |
| 2987 | expr2 = ast_for_expr(c, ch); |
| 2988 | if (!expr2) { |
| 2989 | return NULL; |
| 2990 | } |
| 2991 | if (NCH(ann) == 2) { |
| 2992 | return AnnAssign(expr1, expr2, NULL, simple, |
| 2993 | LINENO(n), n->n_col_offset, c->c_arena); |
| 2994 | } |
| 2995 | else { |
| 2996 | ch = CHILD(ann, 3); |
| 2997 | expr3 = ast_for_expr(c, ch); |
| 2998 | if (!expr3) { |
| 2999 | return NULL; |
| 3000 | } |
| 3001 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3002 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3003 | } |
| 3004 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3005 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3006 | int i; |
| 3007 | asdl_seq *targets; |
| 3008 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3009 | expr_ty expression; |
| 3010 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3011 | /* a normal assignment */ |
| 3012 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3013 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3014 | if (!targets) |
| 3015 | return NULL; |
| 3016 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3017 | expr_ty e; |
| 3018 | node *ch = CHILD(n, i); |
| 3019 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3020 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3021 | return NULL; |
| 3022 | } |
| 3023 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3025 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3026 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3027 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3028 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3029 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3030 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3031 | asdl_seq_SET(targets, i / 2, e); |
| 3032 | } |
| 3033 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3034 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3035 | expression = ast_for_testlist(c, value); |
| 3036 | else |
| 3037 | expression = ast_for_expr(c, value); |
| 3038 | if (!expression) |
| 3039 | return NULL; |
| 3040 | 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] | 3041 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3044 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3045 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3046 | 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] | 3047 | { |
| 3048 | asdl_seq *seq; |
| 3049 | int i; |
| 3050 | expr_ty e; |
| 3051 | |
| 3052 | REQ(n, exprlist); |
| 3053 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3054 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3055 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3056 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3058 | e = ast_for_expr(c, CHILD(n, i)); |
| 3059 | if (!e) |
| 3060 | return NULL; |
| 3061 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3062 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3063 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3064 | } |
| 3065 | return seq; |
| 3066 | } |
| 3067 | |
| 3068 | static stmt_ty |
| 3069 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3070 | { |
| 3071 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3073 | /* del_stmt: 'del' exprlist */ |
| 3074 | REQ(n, del_stmt); |
| 3075 | |
| 3076 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3077 | if (!expr_list) |
| 3078 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3079 | 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] | 3080 | } |
| 3081 | |
| 3082 | static stmt_ty |
| 3083 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3084 | { |
| 3085 | /* |
| 3086 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3087 | | yield_stmt |
| 3088 | break_stmt: 'break' |
| 3089 | continue_stmt: 'continue' |
| 3090 | return_stmt: 'return' [testlist] |
| 3091 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3092 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3093 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3094 | */ |
| 3095 | node *ch; |
| 3096 | |
| 3097 | REQ(n, flow_stmt); |
| 3098 | ch = CHILD(n, 0); |
| 3099 | switch (TYPE(ch)) { |
| 3100 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3101 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3102 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3103 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3104 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3105 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3106 | if (!exp) |
| 3107 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3108 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3109 | } |
| 3110 | case return_stmt: |
| 3111 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3112 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3113 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3114 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3115 | if (!expression) |
| 3116 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3117 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3118 | } |
| 3119 | case raise_stmt: |
| 3120 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3121 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3122 | else if (NCH(ch) >= 2) { |
| 3123 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3124 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3125 | if (!expression) |
| 3126 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3127 | if (NCH(ch) == 4) { |
| 3128 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3129 | if (!cause) |
| 3130 | return NULL; |
| 3131 | } |
| 3132 | 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] | 3133 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3134 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3135 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3136 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3137 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3138 | return NULL; |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3143 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3144 | { |
| 3145 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3146 | import_as_name: NAME ['as' NAME] |
| 3147 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3148 | dotted_name: NAME ('.' NAME)* |
| 3149 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3150 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3151 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | loop: |
| 3153 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3154 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3155 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3156 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3157 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3158 | if (!name) |
| 3159 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3160 | if (NCH(n) == 3) { |
| 3161 | node *str_node = CHILD(n, 2); |
| 3162 | str = NEW_IDENTIFIER(str_node); |
| 3163 | if (!str) |
| 3164 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3165 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3166 | return NULL; |
| 3167 | } |
| 3168 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3169 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3170 | return NULL; |
| 3171 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3172 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3173 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3174 | case dotted_as_name: |
| 3175 | if (NCH(n) == 1) { |
| 3176 | n = CHILD(n, 0); |
| 3177 | goto loop; |
| 3178 | } |
| 3179 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3180 | node *asname_node = CHILD(n, 2); |
| 3181 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3182 | if (!a) |
| 3183 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3184 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3185 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3186 | if (!a->asname) |
| 3187 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3188 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3189 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3190 | return a; |
| 3191 | } |
| 3192 | break; |
| 3193 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3194 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3195 | node *name_node = CHILD(n, 0); |
| 3196 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3197 | if (!name) |
| 3198 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3199 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3200 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3201 | return alias(name, NULL, c->c_arena); |
| 3202 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3203 | else { |
| 3204 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3205 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3206 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3209 | |
| 3210 | len = 0; |
| 3211 | for (i = 0; i < NCH(n); i += 2) |
| 3212 | /* length of string plus one for the dot */ |
| 3213 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3214 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3215 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3216 | if (!str) |
| 3217 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3218 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3219 | if (!s) |
| 3220 | return NULL; |
| 3221 | for (i = 0; i < NCH(n); i += 2) { |
| 3222 | char *sch = STR(CHILD(n, i)); |
| 3223 | strcpy(s, STR(CHILD(n, i))); |
| 3224 | s += strlen(sch); |
| 3225 | *s++ = '.'; |
| 3226 | } |
| 3227 | --s; |
| 3228 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3230 | PyBytes_GET_SIZE(str), |
| 3231 | NULL); |
| 3232 | Py_DECREF(str); |
| 3233 | if (!uni) |
| 3234 | return NULL; |
| 3235 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3236 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3237 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3238 | Py_DECREF(str); |
| 3239 | return NULL; |
| 3240 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3241 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3242 | } |
| 3243 | break; |
| 3244 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3245 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3246 | if (!str) |
| 3247 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3248 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3249 | Py_DECREF(str); |
| 3250 | return NULL; |
| 3251 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3252 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3253 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3254 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3255 | "unexpected import name: %d", TYPE(n)); |
| 3256 | return NULL; |
| 3257 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3258 | |
| 3259 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3260 | return NULL; |
| 3261 | } |
| 3262 | |
| 3263 | static stmt_ty |
| 3264 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3265 | { |
| 3266 | /* |
| 3267 | import_stmt: import_name | import_from |
| 3268 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3269 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3270 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3271 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3272 | int lineno; |
| 3273 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3274 | int i; |
| 3275 | asdl_seq *aliases; |
| 3276 | |
| 3277 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3278 | lineno = LINENO(n); |
| 3279 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3280 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3281 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3282 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3283 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3284 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3285 | if (!aliases) |
| 3286 | return NULL; |
| 3287 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3288 | 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] | 3289 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3290 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3291 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3292 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3293 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3294 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3295 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3296 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3297 | int idx, ndots = 0; |
| 3298 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3299 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3301 | /* Count the number of dots (for relative imports) and check for the |
| 3302 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3303 | for (idx = 1; idx < NCH(n); idx++) { |
| 3304 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3305 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3306 | if (!mod) |
| 3307 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3308 | idx++; |
| 3309 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3310 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3312 | ndots += 3; |
| 3313 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3314 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3315 | break; |
| 3316 | } |
| 3317 | ndots++; |
| 3318 | } |
| 3319 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3320 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3321 | case STAR: |
| 3322 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3323 | n = CHILD(n, idx); |
| 3324 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3325 | break; |
| 3326 | case LPAR: |
| 3327 | /* from ... import (x, y, z) */ |
| 3328 | n = CHILD(n, idx + 1); |
| 3329 | n_children = NCH(n); |
| 3330 | break; |
| 3331 | case import_as_names: |
| 3332 | /* from ... import x, y, z */ |
| 3333 | n = CHILD(n, idx); |
| 3334 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3335 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3336 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3337 | " surrounding parentheses"); |
| 3338 | return NULL; |
| 3339 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3340 | break; |
| 3341 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3342 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3343 | return NULL; |
| 3344 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3345 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3346 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3347 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3348 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3349 | |
| 3350 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3351 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3352 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3353 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3354 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3355 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3357 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3358 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3359 | 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] | 3360 | if (!import_alias) |
| 3361 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3362 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3363 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3364 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3365 | if (mod != NULL) |
| 3366 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3367 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3368 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3369 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3370 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3371 | "unknown import statement: starts with command '%s'", |
| 3372 | STR(CHILD(n, 0))); |
| 3373 | return NULL; |
| 3374 | } |
| 3375 | |
| 3376 | static stmt_ty |
| 3377 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3378 | { |
| 3379 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3380 | identifier name; |
| 3381 | asdl_seq *s; |
| 3382 | int i; |
| 3383 | |
| 3384 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3385 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3386 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3387 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3388 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3389 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3390 | if (!name) |
| 3391 | return NULL; |
| 3392 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3393 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3394 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3398 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3399 | { |
| 3400 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3401 | identifier name; |
| 3402 | asdl_seq *s; |
| 3403 | int i; |
| 3404 | |
| 3405 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3406 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3407 | if (!s) |
| 3408 | return NULL; |
| 3409 | for (i = 1; i < NCH(n); i += 2) { |
| 3410 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3411 | if (!name) |
| 3412 | return NULL; |
| 3413 | asdl_seq_SET(s, i / 2, name); |
| 3414 | } |
| 3415 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3416 | } |
| 3417 | |
| 3418 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3419 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3420 | { |
| 3421 | /* assert_stmt: 'assert' test [',' test] */ |
| 3422 | REQ(n, assert_stmt); |
| 3423 | if (NCH(n) == 2) { |
| 3424 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3425 | if (!expression) |
| 3426 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3427 | 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] | 3428 | } |
| 3429 | else if (NCH(n) == 4) { |
| 3430 | expr_ty expr1, expr2; |
| 3431 | |
| 3432 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3433 | if (!expr1) |
| 3434 | return NULL; |
| 3435 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3436 | if (!expr2) |
| 3437 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3438 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3439 | 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] | 3440 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3441 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3442 | "improper number of parts to 'assert' statement: %d", |
| 3443 | NCH(n)); |
| 3444 | return NULL; |
| 3445 | } |
| 3446 | |
| 3447 | static asdl_seq * |
| 3448 | ast_for_suite(struct compiling *c, const node *n) |
| 3449 | { |
| 3450 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3451 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3452 | stmt_ty s; |
| 3453 | int i, total, num, end, pos = 0; |
| 3454 | node *ch; |
| 3455 | |
| 3456 | REQ(n, suite); |
| 3457 | |
| 3458 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3459 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3460 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3461 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3462 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3463 | n = CHILD(n, 0); |
| 3464 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3466 | */ |
| 3467 | end = NCH(n) - 1; |
| 3468 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3469 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3470 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3471 | for (i = 0; i < end; i += 2) { |
| 3472 | ch = CHILD(n, i); |
| 3473 | s = ast_for_stmt(c, ch); |
| 3474 | if (!s) |
| 3475 | return NULL; |
| 3476 | asdl_seq_SET(seq, pos++, s); |
| 3477 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3478 | } |
| 3479 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3480 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3481 | ch = CHILD(n, i); |
| 3482 | REQ(ch, stmt); |
| 3483 | num = num_stmts(ch); |
| 3484 | if (num == 1) { |
| 3485 | /* small_stmt or compound_stmt with only one child */ |
| 3486 | s = ast_for_stmt(c, ch); |
| 3487 | if (!s) |
| 3488 | return NULL; |
| 3489 | asdl_seq_SET(seq, pos++, s); |
| 3490 | } |
| 3491 | else { |
| 3492 | int j; |
| 3493 | ch = CHILD(ch, 0); |
| 3494 | REQ(ch, simple_stmt); |
| 3495 | for (j = 0; j < NCH(ch); j += 2) { |
| 3496 | /* statement terminates with a semi-colon ';' */ |
| 3497 | if (NCH(CHILD(ch, j)) == 0) { |
| 3498 | assert((j + 1) == NCH(ch)); |
| 3499 | break; |
| 3500 | } |
| 3501 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3502 | if (!s) |
| 3503 | return NULL; |
| 3504 | asdl_seq_SET(seq, pos++, s); |
| 3505 | } |
| 3506 | } |
| 3507 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3508 | } |
| 3509 | assert(pos == seq->size); |
| 3510 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | static stmt_ty |
| 3514 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3515 | { |
| 3516 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3517 | ['else' ':' suite] |
| 3518 | */ |
| 3519 | char *s; |
| 3520 | |
| 3521 | REQ(n, if_stmt); |
| 3522 | |
| 3523 | if (NCH(n) == 4) { |
| 3524 | expr_ty expression; |
| 3525 | asdl_seq *suite_seq; |
| 3526 | |
| 3527 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3528 | if (!expression) |
| 3529 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3531 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3532 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3534 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3535 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3536 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3537 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3538 | s = STR(CHILD(n, 4)); |
| 3539 | /* s[2], the third character in the string, will be |
| 3540 | 's' for el_s_e, or |
| 3541 | 'i' for el_i_f |
| 3542 | */ |
| 3543 | if (s[2] == 's') { |
| 3544 | expr_ty expression; |
| 3545 | asdl_seq *seq1, *seq2; |
| 3546 | |
| 3547 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3548 | if (!expression) |
| 3549 | return NULL; |
| 3550 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3551 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3552 | return NULL; |
| 3553 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3554 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3555 | return NULL; |
| 3556 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3557 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3558 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3559 | } |
| 3560 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3561 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3562 | expr_ty expression; |
| 3563 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3564 | asdl_seq *orelse = NULL; |
| 3565 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3566 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3567 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3568 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3569 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3570 | has_else = 1; |
| 3571 | n_elif -= 3; |
| 3572 | } |
| 3573 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3574 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3575 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3576 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3578 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3579 | if (!orelse) |
| 3580 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3581 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3582 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3583 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3584 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3585 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3586 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3587 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3588 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3589 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | asdl_seq_SET(orelse, 0, |
| 3592 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3593 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3594 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3595 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3596 | /* the just-created orelse handled the last elif */ |
| 3597 | n_elif--; |
| 3598 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3599 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3600 | for (i = 0; i < n_elif; i++) { |
| 3601 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3602 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3603 | if (!newobj) |
| 3604 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3605 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3606 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3607 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3608 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3609 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3610 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3611 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3612 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3614 | LINENO(CHILD(n, off)), |
| 3615 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3616 | orelse = newobj; |
| 3617 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3618 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3619 | if (!expression) |
| 3620 | return NULL; |
| 3621 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3622 | if (!suite_seq) |
| 3623 | return NULL; |
| 3624 | return If(expression, suite_seq, orelse, |
| 3625 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3626 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3627 | |
| 3628 | PyErr_Format(PyExc_SystemError, |
| 3629 | "unexpected token in 'if' statement: %s", s); |
| 3630 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | static stmt_ty |
| 3634 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3635 | { |
| 3636 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3637 | REQ(n, while_stmt); |
| 3638 | |
| 3639 | if (NCH(n) == 4) { |
| 3640 | expr_ty expression; |
| 3641 | asdl_seq *suite_seq; |
| 3642 | |
| 3643 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3644 | if (!expression) |
| 3645 | return NULL; |
| 3646 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3647 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3648 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3649 | 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] | 3650 | } |
| 3651 | else if (NCH(n) == 7) { |
| 3652 | expr_ty expression; |
| 3653 | asdl_seq *seq1, *seq2; |
| 3654 | |
| 3655 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3656 | if (!expression) |
| 3657 | return NULL; |
| 3658 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3659 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3660 | return NULL; |
| 3661 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3662 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3663 | return NULL; |
| 3664 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3665 | 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] | 3666 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3667 | |
| 3668 | PyErr_Format(PyExc_SystemError, |
| 3669 | "wrong number of tokens for 'while' statement: %d", |
| 3670 | NCH(n)); |
| 3671 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3675 | ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3676 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3677 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3678 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3679 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3680 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3681 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3682 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3683 | REQ(n, for_stmt); |
| 3684 | |
| 3685 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3686 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | if (!seq) |
| 3688 | return NULL; |
| 3689 | } |
| 3690 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3691 | node_target = CHILD(n, 1); |
| 3692 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3693 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3694 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3695 | /* Check the # of children rather than the length of _target, since |
| 3696 | 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] | 3697 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3698 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3699 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3701 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3702 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3703 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3704 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3705 | return NULL; |
| 3706 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3707 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3708 | return NULL; |
| 3709 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3710 | if (is_async) |
| 3711 | return AsyncFor(target, expression, suite_seq, seq, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3712 | LINENO(n0), n0->n_col_offset, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3713 | c->c_arena); |
| 3714 | else |
| 3715 | return For(target, expression, suite_seq, seq, |
| 3716 | LINENO(n), n->n_col_offset, |
| 3717 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | static excepthandler_ty |
| 3721 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3722 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3723 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3724 | REQ(exc, except_clause); |
| 3725 | REQ(body, suite); |
| 3726 | |
| 3727 | if (NCH(exc) == 1) { |
| 3728 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3729 | if (!suite_seq) |
| 3730 | return NULL; |
| 3731 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3732 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3733 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3734 | } |
| 3735 | else if (NCH(exc) == 2) { |
| 3736 | expr_ty expression; |
| 3737 | asdl_seq *suite_seq; |
| 3738 | |
| 3739 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3740 | if (!expression) |
| 3741 | return NULL; |
| 3742 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3743 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3744 | return NULL; |
| 3745 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3746 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3747 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3748 | } |
| 3749 | else if (NCH(exc) == 4) { |
| 3750 | asdl_seq *suite_seq; |
| 3751 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3752 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3753 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3754 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3755 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3756 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3757 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3758 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3759 | return NULL; |
| 3760 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3761 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3762 | return NULL; |
| 3763 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3764 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3765 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3766 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3767 | |
| 3768 | PyErr_Format(PyExc_SystemError, |
| 3769 | "wrong number of children for 'except' clause: %d", |
| 3770 | NCH(exc)); |
| 3771 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | static stmt_ty |
| 3775 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3776 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3777 | const int nch = NCH(n); |
| 3778 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3779 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3780 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3781 | REQ(n, try_stmt); |
| 3782 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3783 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3784 | if (body == NULL) |
| 3785 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3786 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3787 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3788 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3789 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3790 | /* we can assume it's an "else", |
| 3791 | because nch >= 9 for try-else-finally and |
| 3792 | it would otherwise have a type of except_clause */ |
| 3793 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3794 | if (orelse == NULL) |
| 3795 | return NULL; |
| 3796 | n_except--; |
| 3797 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3798 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3799 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3800 | if (finally == NULL) |
| 3801 | return NULL; |
| 3802 | n_except--; |
| 3803 | } |
| 3804 | else { |
| 3805 | /* we can assume it's an "else", |
| 3806 | otherwise it would have a type of except_clause */ |
| 3807 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3808 | if (orelse == NULL) |
| 3809 | return NULL; |
| 3810 | n_except--; |
| 3811 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3812 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3813 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3814 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3815 | return NULL; |
| 3816 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3818 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3819 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3820 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3821 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3822 | if (handlers == NULL) |
| 3823 | return NULL; |
| 3824 | |
| 3825 | for (i = 0; i < n_except; i++) { |
| 3826 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3827 | CHILD(n, 5 + i * 3)); |
| 3828 | if (!e) |
| 3829 | return NULL; |
| 3830 | asdl_seq_SET(handlers, i, e); |
| 3831 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3834 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3835 | 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] | 3836 | } |
| 3837 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3838 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3839 | static withitem_ty |
| 3840 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3841 | { |
| 3842 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3843 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3844 | REQ(n, with_item); |
| 3845 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3846 | if (!context_expr) |
| 3847 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3848 | if (NCH(n) == 3) { |
| 3849 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3850 | |
| 3851 | if (!optional_vars) { |
| 3852 | return NULL; |
| 3853 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3854 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3855 | return NULL; |
| 3856 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3859 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3862 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3863 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3864 | ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3865 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3866 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3867 | int i, n_items; |
| 3868 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3869 | |
| 3870 | REQ(n, with_stmt); |
| 3871 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3872 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3873 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3874 | if (!items) |
| 3875 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3876 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3877 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3878 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3879 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3880 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3883 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3884 | if (!body) |
| 3885 | return NULL; |
| 3886 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3887 | if (is_async) |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3888 | return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3889 | else |
| 3890 | 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] | 3891 | } |
| 3892 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3893 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3894 | 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] | 3895 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3896 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3897 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3898 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3899 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3900 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3901 | REQ(n, classdef); |
| 3902 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3903 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3904 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3905 | if (!s) |
| 3906 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3907 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3908 | if (!classname) |
| 3909 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3910 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3911 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3912 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3913 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3914 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3915 | |
| 3916 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3917 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3918 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3919 | return NULL; |
| 3920 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3921 | if (!classname) |
| 3922 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3923 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3924 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3925 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3926 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3929 | /* class NAME '(' arglist ')' ':' suite */ |
| 3930 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3931 | { |
| 3932 | PyObject *dummy_name; |
| 3933 | expr_ty dummy; |
| 3934 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3935 | if (!dummy_name) |
| 3936 | return NULL; |
| 3937 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 3938 | call = ast_for_call(c, CHILD(n, 3), dummy, false); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3939 | if (!call) |
| 3940 | return NULL; |
| 3941 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3942 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3943 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3944 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3945 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3946 | if (!classname) |
| 3947 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3948 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3949 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3950 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3951 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3952 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | static stmt_ty |
| 3956 | ast_for_stmt(struct compiling *c, const node *n) |
| 3957 | { |
| 3958 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3959 | assert(NCH(n) == 1); |
| 3960 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | } |
| 3962 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3963 | assert(num_stmts(n) == 1); |
| 3964 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3965 | } |
| 3966 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3967 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3968 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3969 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3970 | */ |
| 3971 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3972 | case expr_stmt: |
| 3973 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3974 | case del_stmt: |
| 3975 | return ast_for_del_stmt(c, n); |
| 3976 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3977 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3978 | case flow_stmt: |
| 3979 | return ast_for_flow_stmt(c, n); |
| 3980 | case import_stmt: |
| 3981 | return ast_for_import_stmt(c, n); |
| 3982 | case global_stmt: |
| 3983 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3984 | case nonlocal_stmt: |
| 3985 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3986 | case assert_stmt: |
| 3987 | return ast_for_assert_stmt(c, n); |
| 3988 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3989 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3990 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3991 | TYPE(n), NCH(n)); |
| 3992 | return NULL; |
| 3993 | } |
| 3994 | } |
| 3995 | else { |
| 3996 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3997 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3998 | */ |
| 3999 | node *ch = CHILD(n, 0); |
| 4000 | REQ(n, compound_stmt); |
| 4001 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4002 | case if_stmt: |
| 4003 | return ast_for_if_stmt(c, ch); |
| 4004 | case while_stmt: |
| 4005 | return ast_for_while_stmt(c, ch); |
| 4006 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4007 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4008 | case try_stmt: |
| 4009 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4010 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4011 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4012 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4013 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4014 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4015 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4016 | case decorated: |
| 4017 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4018 | case async_stmt: |
| 4019 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4020 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4021 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4022 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4023 | TYPE(n), NCH(n)); |
| 4024 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4025 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4030 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4031 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4032 | const char *end; |
| 4033 | long x; |
| 4034 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4035 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4036 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4037 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4038 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4039 | errno = 0; |
| 4040 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4041 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4042 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4043 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4044 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4045 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4046 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4047 | } |
| 4048 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4049 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4050 | if (*end == '\0') { |
| 4051 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4052 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4053 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4054 | } |
| 4055 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4056 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4057 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4058 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4059 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4060 | return NULL; |
| 4061 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4062 | } |
| 4063 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4064 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4065 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4066 | if (dx == -1.0 && PyErr_Occurred()) |
| 4067 | return NULL; |
| 4068 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4069 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4073 | parsenumber(struct compiling *c, const char *s) |
| 4074 | { |
| 4075 | char *dup, *end; |
| 4076 | PyObject *res = NULL; |
| 4077 | |
| 4078 | assert(s != NULL); |
| 4079 | |
| 4080 | if (strchr(s, '_') == NULL) { |
| 4081 | return parsenumber_raw(c, s); |
| 4082 | } |
| 4083 | /* Create a duplicate without underscores. */ |
| 4084 | dup = PyMem_Malloc(strlen(s) + 1); |
| 4085 | end = dup; |
| 4086 | for (; *s; s++) { |
| 4087 | if (*s != '_') { |
| 4088 | *end++ = *s; |
| 4089 | } |
| 4090 | } |
| 4091 | *end = '\0'; |
| 4092 | res = parsenumber_raw(c, dup); |
| 4093 | PyMem_Free(dup); |
| 4094 | return res; |
| 4095 | } |
| 4096 | |
| 4097 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4098 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4099 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4100 | const char *s, *t; |
| 4101 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4102 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4103 | while (s < end && (*s & 0x80)) s++; |
| 4104 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4105 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4108 | static int |
| 4109 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4110 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4111 | { |
| 4112 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4113 | first_invalid_escape_char); |
| 4114 | if (msg == NULL) { |
| 4115 | return -1; |
| 4116 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4117 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4118 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4119 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4120 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4121 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4122 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4123 | to get a more accurate error report */ |
| 4124 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame^] | 4125 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4126 | } |
| 4127 | Py_DECREF(msg); |
| 4128 | return -1; |
| 4129 | } |
| 4130 | Py_DECREF(msg); |
| 4131 | return 0; |
| 4132 | } |
| 4133 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4134 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4135 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4136 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4137 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4138 | PyObject *v, *u; |
| 4139 | char *buf; |
| 4140 | char *p; |
| 4141 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4142 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4143 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4144 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4145 | return NULL; |
| 4146 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4147 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4148 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4149 | if (u == NULL) |
| 4150 | return NULL; |
| 4151 | p = buf = PyBytes_AsString(u); |
| 4152 | end = s + len; |
| 4153 | while (s < end) { |
| 4154 | if (*s == '\\') { |
| 4155 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4156 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4157 | strcpy(p, "u005c"); |
| 4158 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4159 | if (s >= end) |
| 4160 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4161 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4162 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4163 | if (*s & 0x80) { /* XXX inefficient */ |
| 4164 | PyObject *w; |
| 4165 | int kind; |
| 4166 | void *data; |
| 4167 | Py_ssize_t len, i; |
| 4168 | w = decode_utf8(c, &s, end); |
| 4169 | if (w == NULL) { |
| 4170 | Py_DECREF(u); |
| 4171 | return NULL; |
| 4172 | } |
| 4173 | kind = PyUnicode_KIND(w); |
| 4174 | data = PyUnicode_DATA(w); |
| 4175 | len = PyUnicode_GET_LENGTH(w); |
| 4176 | for (i = 0; i < len; i++) { |
| 4177 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4178 | sprintf(p, "\\U%08x", chr); |
| 4179 | p += 10; |
| 4180 | } |
| 4181 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4182 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4183 | Py_DECREF(w); |
| 4184 | } else { |
| 4185 | *p++ = *s++; |
| 4186 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4187 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4188 | len = p - buf; |
| 4189 | s = buf; |
| 4190 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4191 | const char *first_invalid_escape; |
| 4192 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4193 | |
| 4194 | if (v != NULL && first_invalid_escape != NULL) { |
| 4195 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4196 | /* We have not decref u before because first_invalid_escape points |
| 4197 | inside u. */ |
| 4198 | Py_XDECREF(u); |
| 4199 | Py_DECREF(v); |
| 4200 | return NULL; |
| 4201 | } |
| 4202 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4203 | Py_XDECREF(u); |
| 4204 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4207 | static PyObject * |
| 4208 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4209 | size_t len) |
| 4210 | { |
| 4211 | const char *first_invalid_escape; |
| 4212 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4213 | &first_invalid_escape); |
| 4214 | if (result == NULL) |
| 4215 | return NULL; |
| 4216 | |
| 4217 | if (first_invalid_escape != NULL) { |
| 4218 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4219 | Py_DECREF(result); |
| 4220 | return NULL; |
| 4221 | } |
| 4222 | } |
| 4223 | return result; |
| 4224 | } |
| 4225 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4226 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4227 | and `col_offset` to existing locations. */ |
| 4228 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4229 | { |
| 4230 | n->n_col_offset = n->n_col_offset + col_offset; |
| 4231 | for (int i = 0; i < NCH(n); ++i) { |
| 4232 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4233 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4234 | col_offset = 0; |
| 4235 | } |
| 4236 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4237 | } |
| 4238 | n->n_lineno = n->n_lineno + lineno; |
| 4239 | } |
| 4240 | |
| 4241 | /* Fix locations for the given node and its children. |
| 4242 | |
| 4243 | `parent` is the enclosing node. |
| 4244 | `n` is the node which locations are going to be fixed relative to parent. |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 4245 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4246 | */ |
| 4247 | static void |
| 4248 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4249 | { |
| 4250 | char *substr = NULL; |
| 4251 | char *start; |
| 4252 | int lines = LINENO(parent) - 1; |
| 4253 | int cols = parent->n_col_offset; |
| 4254 | /* Find the full fstring to fix location information in `n`. */ |
| 4255 | while (parent && parent->n_type != STRING) |
| 4256 | parent = parent->n_child; |
| 4257 | if (parent && parent->n_str) { |
| 4258 | substr = strstr(parent->n_str, expr_str); |
| 4259 | if (substr) { |
| 4260 | start = substr; |
| 4261 | while (start > parent->n_str) { |
| 4262 | if (start[0] == '\n') |
| 4263 | break; |
| 4264 | start--; |
| 4265 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4266 | cols += (int)(substr - start); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4267 | /* Fix lineno in mulitline strings. */ |
| 4268 | while ((substr = strchr(substr + 1, '\n'))) |
| 4269 | lines--; |
| 4270 | } |
| 4271 | } |
| 4272 | fstring_shift_node_locations(n, lines, cols); |
| 4273 | } |
| 4274 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4275 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4276 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4277 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4278 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4279 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4280 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4281 | { |
| 4282 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4283 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4284 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4285 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4286 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4287 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4288 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4289 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4290 | assert(*(expr_start-1) == '{'); |
| 4291 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4292 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4293 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4294 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4295 | because turning the expression '' in to '()' would go from being invalid |
| 4296 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4297 | for (s = expr_start; s != expr_end; s++) { |
| 4298 | char c = *s; |
| 4299 | /* The Python parser ignores only the following whitespace |
| 4300 | characters (\r already is converted to \n). */ |
| 4301 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4302 | break; |
| 4303 | } |
| 4304 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4305 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4306 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4307 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4308 | } |
| 4309 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4310 | len = expr_end - expr_start; |
| 4311 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4312 | str = PyMem_RawMalloc(len + 3); |
| 4313 | if (str == NULL) |
| 4314 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4315 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4316 | str[0] = '('; |
| 4317 | memcpy(str+1, expr_start, len); |
| 4318 | str[len+1] = ')'; |
| 4319 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4320 | |
| 4321 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4322 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4323 | Py_eval_input, 0); |
| 4324 | if (!mod_n) { |
| 4325 | PyMem_RawFree(str); |
| 4326 | return NULL; |
| 4327 | } |
| 4328 | /* Reuse str to find the correct column offset. */ |
| 4329 | str[0] = '{'; |
| 4330 | str[len+1] = '}'; |
| 4331 | fstring_fix_node_location(n, mod_n, str); |
| 4332 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4333 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4334 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4335 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4336 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4337 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | /* Return -1 on error. |
| 4341 | |
| 4342 | Return 0 if we reached the end of the literal. |
| 4343 | |
| 4344 | Return 1 if we haven't reached the end of the literal, but we want |
| 4345 | the caller to process the literal up to this point. Used for |
| 4346 | doubled braces. |
| 4347 | */ |
| 4348 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4349 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4350 | PyObject **literal, int recurse_lvl, |
| 4351 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4352 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4353 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4354 | brace (which isn't part of a unicode name escape such as |
| 4355 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4356 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4357 | const char *s = *str; |
| 4358 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4359 | int result = 0; |
| 4360 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4361 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4362 | while (s < end) { |
| 4363 | char ch = *s++; |
| 4364 | if (!raw && ch == '\\' && s < end) { |
| 4365 | ch = *s++; |
| 4366 | if (ch == 'N') { |
| 4367 | if (s < end && *s++ == '{') { |
| 4368 | while (s < end && *s++ != '}') { |
| 4369 | } |
| 4370 | continue; |
| 4371 | } |
| 4372 | break; |
| 4373 | } |
| 4374 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4375 | return -1; |
| 4376 | } |
| 4377 | } |
| 4378 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4379 | /* Check for doubled braces, but only at the top level. If |
| 4380 | we checked at every level, then f'{0:{3}}' would fail |
| 4381 | with the two closing braces. */ |
| 4382 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4383 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4384 | /* We're going to tell the caller that the literal ends |
| 4385 | here, but that they should continue scanning. But also |
| 4386 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4387 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4388 | result = 1; |
| 4389 | goto done; |
| 4390 | } |
| 4391 | |
| 4392 | /* Where a single '{' is the start of a new expression, a |
| 4393 | single '}' is not allowed. */ |
| 4394 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4395 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4396 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4397 | return -1; |
| 4398 | } |
| 4399 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4400 | /* We're either at a '{', which means we're starting another |
| 4401 | expression; or a '}', which means we're at the end of this |
| 4402 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4403 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4407 | *str = s; |
| 4408 | assert(s <= end); |
| 4409 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4410 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4411 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4412 | if (raw) |
| 4413 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4414 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4415 | NULL, NULL); |
| 4416 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4417 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4418 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4419 | if (!*literal) |
| 4420 | return -1; |
| 4421 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4422 | return result; |
| 4423 | } |
| 4424 | |
| 4425 | /* Forward declaration because parsing is recursive. */ |
| 4426 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4427 | 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] | 4428 | struct compiling *c, const node *n); |
| 4429 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4430 | /* 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] | 4431 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4432 | which includes the expression, conversion character, and |
| 4433 | format_spec expression. |
| 4434 | |
| 4435 | Note that I don't do a perfect job here: I don't make sure that a |
| 4436 | closing brace doesn't match an opening paren, for example. It |
| 4437 | doesn't need to error on all invalid expressions, just correctly |
| 4438 | find the end of all valid ones. Any errors inside the expression |
| 4439 | will be caught when we parse it later. */ |
| 4440 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4441 | 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] | 4442 | expr_ty *expression, struct compiling *c, const node *n) |
| 4443 | { |
| 4444 | /* Return -1 on error, else 0. */ |
| 4445 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4446 | const char *expr_start; |
| 4447 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4448 | expr_ty simple_expression; |
| 4449 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4450 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4451 | |
| 4452 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4453 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4454 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4455 | |
| 4456 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4457 | int string_type = 0; |
| 4458 | |
| 4459 | /* Keep track of nesting level for braces/parens/brackets in |
| 4460 | expressions. */ |
| 4461 | Py_ssize_t nested_depth = 0; |
| 4462 | |
| 4463 | /* Can only nest one level deep. */ |
| 4464 | if (recurse_lvl >= 2) { |
| 4465 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4466 | return -1; |
| 4467 | } |
| 4468 | |
| 4469 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4470 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4471 | assert(**str == '{'); |
| 4472 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4473 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4474 | expr_start = *str; |
| 4475 | for (; *str < end; (*str)++) { |
| 4476 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4477 | |
| 4478 | /* Loop invariants. */ |
| 4479 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4480 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4481 | if (quote_char) |
| 4482 | assert(string_type == 1 || string_type == 3); |
| 4483 | else |
| 4484 | assert(string_type == 0); |
| 4485 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4486 | ch = **str; |
| 4487 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4488 | if (ch == '\\') { |
| 4489 | /* Error: can't include a backslash character, inside |
| 4490 | parens or strings or not. */ |
| 4491 | ast_error(c, n, "f-string expression part " |
| 4492 | "cannot include a backslash"); |
| 4493 | return -1; |
| 4494 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4495 | if (quote_char) { |
| 4496 | /* We're inside a string. See if we're at the end. */ |
| 4497 | /* This code needs to implement the same non-error logic |
| 4498 | as tok_get from tokenizer.c, at the letter_quote |
| 4499 | label. To actually share that code would be a |
| 4500 | nightmare. But, it's unlikely to change and is small, |
| 4501 | so duplicate it here. Note we don't need to catch all |
| 4502 | of the errors, since they'll be caught when parsing the |
| 4503 | expression. We just need to match the non-error |
| 4504 | cases. Thus we can ignore \n in single-quoted strings, |
| 4505 | for example. Or non-terminated strings. */ |
| 4506 | if (ch == quote_char) { |
| 4507 | /* Does this match the string_type (single or triple |
| 4508 | quoted)? */ |
| 4509 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4510 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4511 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4512 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4513 | string_type = 0; |
| 4514 | quote_char = 0; |
| 4515 | continue; |
| 4516 | } |
| 4517 | } else { |
| 4518 | /* We're at the end of a normal string. */ |
| 4519 | quote_char = 0; |
| 4520 | string_type = 0; |
| 4521 | continue; |
| 4522 | } |
| 4523 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4524 | } else if (ch == '\'' || ch == '"') { |
| 4525 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4526 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4527 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4528 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4529 | } else { |
| 4530 | /* Start of a normal string. */ |
| 4531 | string_type = 1; |
| 4532 | } |
| 4533 | /* Start looking for the end of the string. */ |
| 4534 | quote_char = ch; |
| 4535 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4536 | nested_depth++; |
| 4537 | } else if (nested_depth != 0 && |
| 4538 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4539 | nested_depth--; |
| 4540 | } else if (ch == '#') { |
| 4541 | /* Error: can't include a comment character, inside parens |
| 4542 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4543 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4544 | return -1; |
| 4545 | } else if (nested_depth == 0 && |
| 4546 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4547 | /* First, test for the special case of "!=". Since '=' is |
| 4548 | not an allowed conversion character, nothing is lost in |
| 4549 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4550 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4551 | /* This isn't a conversion character, just continue. */ |
| 4552 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4553 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4554 | /* Normal way out of this loop. */ |
| 4555 | break; |
| 4556 | } else { |
| 4557 | /* Just consume this char and loop around. */ |
| 4558 | } |
| 4559 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4560 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4561 | /* If we leave this loop in a string or with mismatched parens, we |
| 4562 | don't care. We'll get a syntax error when compiling the |
| 4563 | expression. But, we can produce a better error message, so |
| 4564 | let's just do that.*/ |
| 4565 | if (quote_char) { |
| 4566 | ast_error(c, n, "f-string: unterminated string"); |
| 4567 | return -1; |
| 4568 | } |
| 4569 | if (nested_depth) { |
| 4570 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4571 | return -1; |
| 4572 | } |
| 4573 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4574 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4575 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4576 | |
| 4577 | /* Compile the expression as soon as possible, so we show errors |
| 4578 | related to the expression before errors related to the |
| 4579 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4580 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4581 | if (!simple_expression) |
| 4582 | return -1; |
| 4583 | |
| 4584 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4585 | if (**str == '!') { |
| 4586 | *str += 1; |
| 4587 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4588 | goto unexpected_end_of_string; |
| 4589 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4590 | conversion = **str; |
| 4591 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4592 | |
| 4593 | /* Validate the conversion. */ |
| 4594 | if (!(conversion == 's' || conversion == 'r' |
| 4595 | || conversion == 'a')) { |
| 4596 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4597 | "expected 's', 'r', or 'a'"); |
| 4598 | return -1; |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4603 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4604 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4605 | if (**str == ':') { |
| 4606 | *str += 1; |
| 4607 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4608 | goto unexpected_end_of_string; |
| 4609 | |
| 4610 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4611 | 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] | 4612 | if (!format_spec) |
| 4613 | return -1; |
| 4614 | } |
| 4615 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4616 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4617 | goto unexpected_end_of_string; |
| 4618 | |
| 4619 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4620 | assert(*str < end); |
| 4621 | assert(**str == '}'); |
| 4622 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4623 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4624 | /* And now create the FormattedValue node that represents this |
| 4625 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4626 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4627 | format_spec, LINENO(n), n->n_col_offset, |
| 4628 | c->c_arena); |
| 4629 | if (!*expression) |
| 4630 | return -1; |
| 4631 | |
| 4632 | return 0; |
| 4633 | |
| 4634 | unexpected_end_of_string: |
| 4635 | ast_error(c, n, "f-string: expecting '}'"); |
| 4636 | return -1; |
| 4637 | } |
| 4638 | |
| 4639 | /* Return -1 on error. |
| 4640 | |
| 4641 | Return 0 if we have a literal (possible zero length) and an |
| 4642 | expression (zero length if at the end of the string. |
| 4643 | |
| 4644 | Return 1 if we have a literal, but no expression, and we want the |
| 4645 | caller to call us again. This is used to deal with doubled |
| 4646 | braces. |
| 4647 | |
| 4648 | When called multiple times on the string 'a{{b{0}c', this function |
| 4649 | will return: |
| 4650 | |
| 4651 | 1. the literal 'a{' with no expression, and a return value |
| 4652 | of 1. Despite the fact that there's no expression, the return |
| 4653 | value of 1 means we're not finished yet. |
| 4654 | |
| 4655 | 2. the literal 'b' and the expression '0', with a return value of |
| 4656 | 0. The fact that there's an expression means we're not finished. |
| 4657 | |
| 4658 | 3. literal 'c' with no expression and a return value of 0. The |
| 4659 | combination of the return value of 0 with no expression means |
| 4660 | we're finished. |
| 4661 | */ |
| 4662 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4663 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4664 | int recurse_lvl, PyObject **literal, |
| 4665 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4666 | struct compiling *c, const node *n) |
| 4667 | { |
| 4668 | int result; |
| 4669 | |
| 4670 | assert(*literal == NULL && *expression == NULL); |
| 4671 | |
| 4672 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4673 | 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] | 4674 | if (result < 0) |
| 4675 | goto error; |
| 4676 | |
| 4677 | assert(result == 0 || result == 1); |
| 4678 | |
| 4679 | if (result == 1) |
| 4680 | /* We have a literal, but don't look at the expression. */ |
| 4681 | return 1; |
| 4682 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4683 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4684 | /* We're at the end of the string or the end of a nested |
| 4685 | f-string: no expression. The top-level error case where we |
| 4686 | expect to be at the end of the string but we're at a '}' is |
| 4687 | handled later. */ |
| 4688 | return 0; |
| 4689 | |
| 4690 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4691 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4692 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4693 | 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] | 4694 | goto error; |
| 4695 | |
| 4696 | return 0; |
| 4697 | |
| 4698 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4699 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4700 | return -1; |
| 4701 | } |
| 4702 | |
| 4703 | #define EXPRLIST_N_CACHED 64 |
| 4704 | |
| 4705 | typedef struct { |
| 4706 | /* Incrementally build an array of expr_ty, so be used in an |
| 4707 | asdl_seq. Cache some small but reasonably sized number of |
| 4708 | expr_ty's, and then after that start dynamically allocating, |
| 4709 | doubling the number allocated each time. Note that the f-string |
| 4710 | f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4711 | Constant for the literal 'a'. So you add expr_ty's about twice as |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4712 | fast as you add exressions in an f-string. */ |
| 4713 | |
| 4714 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4715 | Py_ssize_t size; /* Number we've used. */ |
| 4716 | expr_ty *p; /* Pointer to the memory we're actually |
| 4717 | using. Will point to 'data' until we |
| 4718 | start dynamically allocating. */ |
| 4719 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4720 | } ExprList; |
| 4721 | |
| 4722 | #ifdef NDEBUG |
| 4723 | #define ExprList_check_invariants(l) |
| 4724 | #else |
| 4725 | static void |
| 4726 | ExprList_check_invariants(ExprList *l) |
| 4727 | { |
| 4728 | /* Check our invariants. Make sure this object is "live", and |
| 4729 | hasn't been deallocated. */ |
| 4730 | assert(l->size >= 0); |
| 4731 | assert(l->p != NULL); |
| 4732 | if (l->size <= EXPRLIST_N_CACHED) |
| 4733 | assert(l->data == l->p); |
| 4734 | } |
| 4735 | #endif |
| 4736 | |
| 4737 | static void |
| 4738 | ExprList_Init(ExprList *l) |
| 4739 | { |
| 4740 | l->allocated = EXPRLIST_N_CACHED; |
| 4741 | l->size = 0; |
| 4742 | |
| 4743 | /* Until we start allocating dynamically, p points to data. */ |
| 4744 | l->p = l->data; |
| 4745 | |
| 4746 | ExprList_check_invariants(l); |
| 4747 | } |
| 4748 | |
| 4749 | static int |
| 4750 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4751 | { |
| 4752 | ExprList_check_invariants(l); |
| 4753 | if (l->size >= l->allocated) { |
| 4754 | /* We need to alloc (or realloc) the memory. */ |
| 4755 | Py_ssize_t new_size = l->allocated * 2; |
| 4756 | |
| 4757 | /* See if we've ever allocated anything dynamically. */ |
| 4758 | if (l->p == l->data) { |
| 4759 | Py_ssize_t i; |
| 4760 | /* We're still using the cached data. Switch to |
| 4761 | alloc-ing. */ |
| 4762 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4763 | if (!l->p) |
| 4764 | return -1; |
| 4765 | /* Copy the cached data into the new buffer. */ |
| 4766 | for (i = 0; i < l->size; i++) |
| 4767 | l->p[i] = l->data[i]; |
| 4768 | } else { |
| 4769 | /* Just realloc. */ |
| 4770 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4771 | if (!tmp) { |
| 4772 | PyMem_RawFree(l->p); |
| 4773 | l->p = NULL; |
| 4774 | return -1; |
| 4775 | } |
| 4776 | l->p = tmp; |
| 4777 | } |
| 4778 | |
| 4779 | l->allocated = new_size; |
| 4780 | assert(l->allocated == 2 * l->size); |
| 4781 | } |
| 4782 | |
| 4783 | l->p[l->size++] = exp; |
| 4784 | |
| 4785 | ExprList_check_invariants(l); |
| 4786 | return 0; |
| 4787 | } |
| 4788 | |
| 4789 | static void |
| 4790 | ExprList_Dealloc(ExprList *l) |
| 4791 | { |
| 4792 | ExprList_check_invariants(l); |
| 4793 | |
| 4794 | /* If there's been an error, or we've never dynamically allocated, |
| 4795 | do nothing. */ |
| 4796 | if (!l->p || l->p == l->data) { |
| 4797 | /* Do nothing. */ |
| 4798 | } else { |
| 4799 | /* We have dynamically allocated. Free the memory. */ |
| 4800 | PyMem_RawFree(l->p); |
| 4801 | } |
| 4802 | l->p = NULL; |
| 4803 | l->size = -1; |
| 4804 | } |
| 4805 | |
| 4806 | static asdl_seq * |
| 4807 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4808 | { |
| 4809 | asdl_seq *seq; |
| 4810 | |
| 4811 | ExprList_check_invariants(l); |
| 4812 | |
| 4813 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4814 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4815 | if (seq) { |
| 4816 | Py_ssize_t i; |
| 4817 | for (i = 0; i < l->size; i++) |
| 4818 | asdl_seq_SET(seq, i, l->p[i]); |
| 4819 | } |
| 4820 | ExprList_Dealloc(l); |
| 4821 | return seq; |
| 4822 | } |
| 4823 | |
| 4824 | /* The FstringParser is designed to add a mix of strings and |
| 4825 | f-strings, and concat them together as needed. Ultimately, it |
| 4826 | generates an expr_ty. */ |
| 4827 | typedef struct { |
| 4828 | PyObject *last_str; |
| 4829 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4830 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4831 | } FstringParser; |
| 4832 | |
| 4833 | #ifdef NDEBUG |
| 4834 | #define FstringParser_check_invariants(state) |
| 4835 | #else |
| 4836 | static void |
| 4837 | FstringParser_check_invariants(FstringParser *state) |
| 4838 | { |
| 4839 | if (state->last_str) |
| 4840 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4841 | ExprList_check_invariants(&state->expr_list); |
| 4842 | } |
| 4843 | #endif |
| 4844 | |
| 4845 | static void |
| 4846 | FstringParser_Init(FstringParser *state) |
| 4847 | { |
| 4848 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4849 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4850 | ExprList_Init(&state->expr_list); |
| 4851 | FstringParser_check_invariants(state); |
| 4852 | } |
| 4853 | |
| 4854 | static void |
| 4855 | FstringParser_Dealloc(FstringParser *state) |
| 4856 | { |
| 4857 | FstringParser_check_invariants(state); |
| 4858 | |
| 4859 | Py_XDECREF(state->last_str); |
| 4860 | ExprList_Dealloc(&state->expr_list); |
| 4861 | } |
| 4862 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4863 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4864 | static expr_ty |
| 4865 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4866 | { |
| 4867 | PyObject *s = *str; |
| 4868 | *str = NULL; |
| 4869 | assert(PyUnicode_CheckExact(s)); |
| 4870 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4871 | Py_DECREF(s); |
| 4872 | return NULL; |
| 4873 | } |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4874 | return Constant(s, LINENO(n), n->n_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4878 | decref'd. */ |
| 4879 | static int |
| 4880 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4881 | { |
| 4882 | FstringParser_check_invariants(state); |
| 4883 | |
| 4884 | assert(PyUnicode_CheckExact(str)); |
| 4885 | |
| 4886 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4887 | Py_DECREF(str); |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | if (!state->last_str) { |
| 4892 | /* We didn't have a string before, so just remember this one. */ |
| 4893 | state->last_str = str; |
| 4894 | } else { |
| 4895 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4896 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4897 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4898 | return -1; |
| 4899 | } |
| 4900 | FstringParser_check_invariants(state); |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4904 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4905 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4906 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4907 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4908 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4909 | struct compiling *c, const node *n) |
| 4910 | { |
| 4911 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4912 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4913 | |
| 4914 | /* Parse the f-string. */ |
| 4915 | while (1) { |
| 4916 | PyObject *literal = NULL; |
| 4917 | expr_ty expression = NULL; |
| 4918 | |
| 4919 | /* If there's a zero length literal in front of the |
| 4920 | expression, literal will be NULL. If we're at the end of |
| 4921 | the f-string, expression will be NULL (unless result == 1, |
| 4922 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4923 | 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] | 4924 | &literal, &expression, |
| 4925 | c, n); |
| 4926 | if (result < 0) |
| 4927 | return -1; |
| 4928 | |
| 4929 | /* Add the literal, if any. */ |
| 4930 | if (!literal) { |
| 4931 | /* Do nothing. Just leave last_str alone (and possibly |
| 4932 | NULL). */ |
| 4933 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 4934 | /* Note that the literal can be zero length, if the |
| 4935 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4936 | state->last_str = literal; |
| 4937 | literal = NULL; |
| 4938 | } else { |
| 4939 | /* We have a literal, concatenate it. */ |
| 4940 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4941 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4942 | return -1; |
| 4943 | literal = NULL; |
| 4944 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4945 | |
| 4946 | /* We've dealt with the literal now. It can't be leaked on further |
| 4947 | errors. */ |
| 4948 | assert(literal == NULL); |
| 4949 | |
| 4950 | /* See if we should just loop around to get the next literal |
| 4951 | and expression, while ignoring the expression this |
| 4952 | time. This is used for un-doubling braces, as an |
| 4953 | optimization. */ |
| 4954 | if (result == 1) |
| 4955 | continue; |
| 4956 | |
| 4957 | if (!expression) |
| 4958 | /* We're done with this f-string. */ |
| 4959 | break; |
| 4960 | |
| 4961 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4962 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4963 | if (!state->last_str) { |
| 4964 | /* Do nothing. No previous literal. */ |
| 4965 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4966 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4967 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 4968 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 4969 | return -1; |
| 4970 | } |
| 4971 | |
| 4972 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 4973 | return -1; |
| 4974 | } |
| 4975 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4976 | /* If recurse_lvl is zero, then we must be at the end of the |
| 4977 | string. Otherwise, we must be at a right brace. */ |
| 4978 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4979 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4980 | ast_error(c, n, "f-string: unexpected end of string"); |
| 4981 | return -1; |
| 4982 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4983 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4984 | ast_error(c, n, "f-string: expecting '}'"); |
| 4985 | return -1; |
| 4986 | } |
| 4987 | |
| 4988 | FstringParser_check_invariants(state); |
| 4989 | return 0; |
| 4990 | } |
| 4991 | |
| 4992 | /* Convert the partial state reflected in last_str and expr_list to an |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4993 | expr_ty. The expr_ty can be a Constant, or a JoinedStr. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4994 | static expr_ty |
| 4995 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 4996 | const node *n) |
| 4997 | { |
| 4998 | asdl_seq *seq; |
| 4999 | |
| 5000 | FstringParser_check_invariants(state); |
| 5001 | |
| 5002 | /* If we're just a constant string with no expressions, return |
| 5003 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5004 | if (!state->fmode) { |
| 5005 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5006 | if (!state->last_str) { |
| 5007 | /* Create a zero length string. */ |
| 5008 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5009 | if (!state->last_str) |
| 5010 | goto error; |
| 5011 | } |
| 5012 | return make_str_node_and_del(&state->last_str, c, n); |
| 5013 | } |
| 5014 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5015 | /* Create a Constant node out of last_str, if needed. It will be the |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5016 | last node in our expression list. */ |
| 5017 | if (state->last_str) { |
| 5018 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5019 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5020 | goto error; |
| 5021 | } |
| 5022 | /* This has already been freed. */ |
| 5023 | assert(state->last_str == NULL); |
| 5024 | |
| 5025 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5026 | if (!seq) |
| 5027 | goto error; |
| 5028 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5029 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5030 | |
| 5031 | error: |
| 5032 | FstringParser_Dealloc(state); |
| 5033 | return NULL; |
| 5034 | } |
| 5035 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5036 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5037 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5038 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5039 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5040 | 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] | 5041 | struct compiling *c, const node *n) |
| 5042 | { |
| 5043 | FstringParser state; |
| 5044 | |
| 5045 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5046 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5047 | c, n) < 0) { |
| 5048 | FstringParser_Dealloc(&state); |
| 5049 | return NULL; |
| 5050 | } |
| 5051 | |
| 5052 | return FstringParser_Finish(&state, c, n); |
| 5053 | } |
| 5054 | |
| 5055 | /* n is a Python string literal, including the bracketing quote |
| 5056 | 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] | 5057 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5058 | 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] | 5059 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5060 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5061 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5062 | static int |
| 5063 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5064 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5065 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5066 | size_t len; |
| 5067 | const char *s = STR(n); |
| 5068 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5069 | int fmode = 0; |
| 5070 | *bytesmode = 0; |
| 5071 | *rawmode = 0; |
| 5072 | *result = NULL; |
| 5073 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5074 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5075 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5076 | if (quote == 'b' || quote == 'B') { |
| 5077 | quote = *++s; |
| 5078 | *bytesmode = 1; |
| 5079 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5080 | else if (quote == 'u' || quote == 'U') { |
| 5081 | quote = *++s; |
| 5082 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5083 | else if (quote == 'r' || quote == 'R') { |
| 5084 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5085 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5086 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5087 | else if (quote == 'f' || quote == 'F') { |
| 5088 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5089 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5090 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5091 | else { |
| 5092 | break; |
| 5093 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5094 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5095 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5096 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5097 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5098 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5099 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5100 | if (quote != '\'' && quote != '\"') { |
| 5101 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5102 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5103 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5104 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5105 | s++; |
| 5106 | len = strlen(s); |
| 5107 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5109 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5110 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5111 | } |
| 5112 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5113 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5114 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5115 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5116 | } |
| 5117 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5118 | /* A triple quoted string. We've already skipped one quote at |
| 5119 | the start and one at the end of the string. Now skip the |
| 5120 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5121 | s += 2; |
| 5122 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5123 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5124 | if (s[--len] != quote || s[--len] != quote) { |
| 5125 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5126 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5127 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5128 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5129 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5130 | if (fmode) { |
| 5131 | /* Just return the bytes. The caller will parse the resulting |
| 5132 | string. */ |
| 5133 | *fstr = s; |
| 5134 | *fstrlen = len; |
| 5135 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5136 | } |
| 5137 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5138 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5139 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5140 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5141 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5142 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5143 | const char *ch; |
| 5144 | for (ch = s; *ch; ch++) { |
| 5145 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5146 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5147 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5148 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5149 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5150 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5151 | if (*rawmode) |
| 5152 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5153 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5154 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5155 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5156 | if (*rawmode) |
| 5157 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5158 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5159 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5160 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5161 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5164 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5165 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5166 | concatenate them together, and the result will be a Constant node. For |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5167 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5168 | will be a Constant node if there were no f-strings; a FormattedValue |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5169 | node if there's just an f-string (with no leading or trailing |
| 5170 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5171 | any literals involved. */ |
| 5172 | static expr_ty |
| 5173 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5174 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5175 | int bytesmode = 0; |
| 5176 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5177 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5178 | |
| 5179 | FstringParser state; |
| 5180 | FstringParser_Init(&state); |
| 5181 | |
| 5182 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | int this_bytesmode; |
| 5184 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5185 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5186 | const char *fstr; |
| 5187 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5188 | |
| 5189 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5190 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5191 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5192 | goto error; |
| 5193 | |
| 5194 | /* Check that we're not mixing bytes with unicode. */ |
| 5195 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5196 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5197 | /* s is NULL if the current string part is an f-string. */ |
| 5198 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5199 | goto error; |
| 5200 | } |
| 5201 | bytesmode = this_bytesmode; |
| 5202 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5203 | if (fstr != NULL) { |
| 5204 | int result; |
| 5205 | assert(s == NULL && !bytesmode); |
| 5206 | /* This is an f-string. Parse and concatenate it. */ |
| 5207 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5208 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5209 | if (result < 0) |
| 5210 | goto error; |
| 5211 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5212 | /* A string or byte string. */ |
| 5213 | assert(s != NULL && fstr == NULL); |
| 5214 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5215 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5216 | PyUnicode_CheckExact(s)); |
| 5217 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5218 | if (bytesmode) { |
| 5219 | /* For bytes, concat as we go. */ |
| 5220 | if (i == 0) { |
| 5221 | /* First time, just remember this value. */ |
| 5222 | bytes_str = s; |
| 5223 | } else { |
| 5224 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5225 | if (!bytes_str) |
| 5226 | goto error; |
| 5227 | } |
| 5228 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | /* This is a regular string. Concatenate it. */ |
| 5230 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5231 | goto error; |
| 5232 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5233 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5234 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5235 | if (bytesmode) { |
| 5236 | /* Just return the bytes object and we're done. */ |
| 5237 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5238 | goto error; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5239 | return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5240 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5241 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5242 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5243 | assert(bytes_str == NULL); |
| 5244 | |
| 5245 | return FstringParser_Finish(&state, c, n); |
| 5246 | |
| 5247 | error: |
| 5248 | Py_XDECREF(bytes_str); |
| 5249 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5250 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5251 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5252 | |
| 5253 | PyObject * |
| 5254 | _PyAST_GetDocString(asdl_seq *body) |
| 5255 | { |
| 5256 | if (!asdl_seq_LEN(body)) { |
| 5257 | return NULL; |
| 5258 | } |
| 5259 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5260 | if (st->kind != Expr_kind) { |
| 5261 | return NULL; |
| 5262 | } |
| 5263 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5264 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5265 | return e->v.Constant.value; |
| 5266 | } |
| 5267 | return NULL; |
| 5268 | } |