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 |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [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; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 653 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 654 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 655 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 656 | Py_INCREF(Py_None); |
| 657 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 659 | tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 660 | if (!tmp) |
| 661 | return 0; |
| 662 | errstr = PyUnicode_FromString(errmsg); |
| 663 | if (!errstr) { |
| 664 | Py_DECREF(tmp); |
| 665 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 666 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 667 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | Py_DECREF(errstr); |
| 669 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 670 | if (value) { |
| 671 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 672 | Py_DECREF(value); |
| 673 | } |
| 674 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* num_stmts() returns number of contained statements. |
| 678 | |
| 679 | Use this routine to determine how big a sequence is needed for |
| 680 | the statements in a parse tree. Its raison d'etre is this bit of |
| 681 | grammar: |
| 682 | |
| 683 | stmt: simple_stmt | compound_stmt |
| 684 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 685 | |
| 686 | A simple_stmt can contain multiple small_stmt elements joined |
| 687 | by semicolons. If the arg is a simple_stmt, the number of |
| 688 | small_stmt elements is returned. |
| 689 | */ |
| 690 | |
| 691 | static int |
| 692 | num_stmts(const node *n) |
| 693 | { |
| 694 | int i, l; |
| 695 | node *ch; |
| 696 | |
| 697 | switch (TYPE(n)) { |
| 698 | case single_input: |
| 699 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 700 | return 0; |
| 701 | else |
| 702 | return num_stmts(CHILD(n, 0)); |
| 703 | case file_input: |
| 704 | l = 0; |
| 705 | for (i = 0; i < NCH(n); i++) { |
| 706 | ch = CHILD(n, i); |
| 707 | if (TYPE(ch) == stmt) |
| 708 | l += num_stmts(ch); |
| 709 | } |
| 710 | return l; |
| 711 | case stmt: |
| 712 | return num_stmts(CHILD(n, 0)); |
| 713 | case compound_stmt: |
| 714 | return 1; |
| 715 | case simple_stmt: |
| 716 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 717 | case suite: |
| 718 | if (NCH(n) == 1) |
| 719 | return num_stmts(CHILD(n, 0)); |
| 720 | else { |
| 721 | l = 0; |
| 722 | for (i = 2; i < (NCH(n) - 1); i++) |
| 723 | l += num_stmts(CHILD(n, i)); |
| 724 | return l; |
| 725 | } |
| 726 | default: { |
| 727 | char buf[128]; |
| 728 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 729 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 730 | TYPE(n), NCH(n)); |
| 731 | Py_FatalError(buf); |
| 732 | } |
| 733 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 734 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* Transform the CST rooted at node * to the appropriate AST |
| 738 | */ |
| 739 | |
| 740 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 741 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 742 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 743 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 744 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 745 | asdl_seq *stmts = NULL; |
| 746 | stmt_ty s; |
| 747 | node *ch; |
| 748 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 749 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 750 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 751 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 752 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 753 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 754 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 755 | |
| 756 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 757 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 758 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 759 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 760 | switch (TYPE(n)) { |
| 761 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 762 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 763 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 764 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | for (i = 0; i < NCH(n) - 1; i++) { |
| 766 | ch = CHILD(n, i); |
| 767 | if (TYPE(ch) == NEWLINE) |
| 768 | continue; |
| 769 | REQ(ch, stmt); |
| 770 | num = num_stmts(ch); |
| 771 | if (num == 1) { |
| 772 | s = ast_for_stmt(&c, ch); |
| 773 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 774 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 775 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 | } |
| 777 | else { |
| 778 | ch = CHILD(ch, 0); |
| 779 | REQ(ch, simple_stmt); |
| 780 | for (j = 0; j < num; j++) { |
| 781 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 782 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 783 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 784 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 788 | res = Module(stmts, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 789 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | case eval_input: { |
| 791 | expr_ty testlist_ast; |
| 792 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 793 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 794 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 796 | goto out; |
| 797 | res = Expression(testlist_ast, arena); |
| 798 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | } |
| 800 | case single_input: |
| 801 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 802 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 804 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 805 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 806 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 807 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 808 | goto out; |
| 809 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 810 | } |
| 811 | else { |
| 812 | n = CHILD(n, 0); |
| 813 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 814 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 815 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 816 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 817 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 818 | s = ast_for_stmt(&c, n); |
| 819 | if (!s) |
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 | asdl_seq_SET(stmts, 0, s); |
| 822 | } |
| 823 | else { |
| 824 | /* Only a simple_stmt can contain multiple statements. */ |
| 825 | REQ(n, simple_stmt); |
| 826 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 827 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 828 | break; |
| 829 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 830 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 831 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 832 | asdl_seq_SET(stmts, i / 2, s); |
| 833 | } |
| 834 | } |
| 835 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 836 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 838 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 840 | PyErr_Format(PyExc_SystemError, |
| 841 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 842 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 843 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 844 | out: |
| 845 | if (c.c_normalize) { |
| 846 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 847 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 848 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 851 | mod_ty |
| 852 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 853 | PyArena *arena) |
| 854 | { |
| 855 | mod_ty mod; |
| 856 | PyObject *filename; |
| 857 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 858 | if (filename == NULL) |
| 859 | return NULL; |
| 860 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 861 | Py_DECREF(filename); |
| 862 | return mod; |
| 863 | |
| 864 | } |
| 865 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 867 | */ |
| 868 | |
| 869 | static operator_ty |
| 870 | get_operator(const node *n) |
| 871 | { |
| 872 | switch (TYPE(n)) { |
| 873 | case VBAR: |
| 874 | return BitOr; |
| 875 | case CIRCUMFLEX: |
| 876 | return BitXor; |
| 877 | case AMPER: |
| 878 | return BitAnd; |
| 879 | case LEFTSHIFT: |
| 880 | return LShift; |
| 881 | case RIGHTSHIFT: |
| 882 | return RShift; |
| 883 | case PLUS: |
| 884 | return Add; |
| 885 | case MINUS: |
| 886 | return Sub; |
| 887 | case STAR: |
| 888 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 889 | case AT: |
| 890 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 891 | case SLASH: |
| 892 | return Div; |
| 893 | case DOUBLESLASH: |
| 894 | return FloorDiv; |
| 895 | case PERCENT: |
| 896 | return Mod; |
| 897 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 898 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 902 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 903 | "None", |
| 904 | "True", |
| 905 | "False", |
| 906 | NULL, |
| 907 | }; |
| 908 | |
| 909 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 910 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 911 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 912 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 913 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 914 | if (_PyUnicode_EqualToASCIIString(name, "__debug__")) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 915 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 916 | return 1; |
| 917 | } |
| 918 | if (full_checks) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 919 | const char * const *p; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 920 | for (p = FORBIDDEN; *p; p++) { |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 921 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 922 | ast_error(c, n, "assignment to keyword"); |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 923 | return 1; |
| 924 | } |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | return 0; |
| 928 | } |
| 929 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 930 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 931 | |
| 932 | Only sets context for expr kinds that "can appear in assignment context" |
| 933 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 934 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 935 | */ |
| 936 | |
| 937 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 938 | 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] | 939 | { |
| 940 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 941 | /* If a particular expression type can't be used for assign / delete, |
| 942 | set expr_name to its name and an error message will be generated. |
| 943 | */ |
| 944 | const char* expr_name = NULL; |
| 945 | |
| 946 | /* The ast defines augmented store and load contexts, but the |
| 947 | implementation here doesn't actually use them. The code may be |
| 948 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 949 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 950 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 951 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 952 | */ |
| 953 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 954 | |
| 955 | switch (e->kind) { |
| 956 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 957 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 958 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 959 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 960 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 961 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 962 | e->v.Subscript.ctx = ctx; |
| 963 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 964 | case Starred_kind: |
| 965 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 966 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 969 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 970 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 971 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 972 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 973 | } |
| 974 | e->v.Name.ctx = ctx; |
| 975 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 976 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 977 | e->v.List.ctx = ctx; |
| 978 | s = e->v.List.elts; |
| 979 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 980 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 981 | e->v.Tuple.ctx = ctx; |
| 982 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 983 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 984 | case Lambda_kind: |
| 985 | expr_name = "lambda"; |
| 986 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 987 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 988 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 989 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 990 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 991 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 992 | case UnaryOp_kind: |
| 993 | expr_name = "operator"; |
| 994 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 995 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 996 | expr_name = "generator expression"; |
| 997 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 998 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 999 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1000 | expr_name = "yield expression"; |
| 1001 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1002 | case Await_kind: |
| 1003 | expr_name = "await expression"; |
| 1004 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1005 | case ListComp_kind: |
| 1006 | expr_name = "list comprehension"; |
| 1007 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1008 | case SetComp_kind: |
| 1009 | expr_name = "set comprehension"; |
| 1010 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1011 | case DictComp_kind: |
| 1012 | expr_name = "dict comprehension"; |
| 1013 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1014 | case Dict_kind: |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1015 | case Set_kind: |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1016 | case JoinedStr_kind: |
| 1017 | case FormattedValue_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1018 | expr_name = "literal"; |
| 1019 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1020 | case Constant_kind: { |
| 1021 | PyObject *value = e->v.Constant.value; |
| 1022 | if (value == Py_None || value == Py_False || value == Py_True) { |
| 1023 | expr_name = "keyword"; |
| 1024 | } |
| 1025 | else if (value == Py_Ellipsis) { |
| 1026 | expr_name = "Ellipsis"; |
| 1027 | } |
| 1028 | else { |
| 1029 | expr_name = "literal"; |
| 1030 | } |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1031 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1032 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1033 | case Compare_kind: |
| 1034 | expr_name = "comparison"; |
| 1035 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1036 | case IfExp_kind: |
| 1037 | expr_name = "conditional expression"; |
| 1038 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1039 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | PyErr_Format(PyExc_SystemError, |
| 1041 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1042 | e->kind, e->lineno); |
| 1043 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1044 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1045 | /* Check for error string set by switch */ |
| 1046 | if (expr_name) { |
| 1047 | char buf[300]; |
| 1048 | PyOS_snprintf(buf, sizeof(buf), |
| 1049 | "can't %s %s", |
| 1050 | ctx == Store ? "assign to" : "delete", |
| 1051 | expr_name); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1052 | return ast_error(c, n, buf); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1055 | /* 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] | 1056 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1057 | */ |
| 1058 | if (s) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1059 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1060 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1061 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1062 | 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] | 1063 | return 0; |
| 1064 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1065 | } |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1070 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1071 | { |
| 1072 | REQ(n, augassign); |
| 1073 | n = CHILD(n, 0); |
| 1074 | switch (STR(n)[0]) { |
| 1075 | case '+': |
| 1076 | return Add; |
| 1077 | case '-': |
| 1078 | return Sub; |
| 1079 | case '/': |
| 1080 | if (STR(n)[1] == '/') |
| 1081 | return FloorDiv; |
| 1082 | else |
| 1083 | return Div; |
| 1084 | case '%': |
| 1085 | return Mod; |
| 1086 | case '<': |
| 1087 | return LShift; |
| 1088 | case '>': |
| 1089 | return RShift; |
| 1090 | case '&': |
| 1091 | return BitAnd; |
| 1092 | case '^': |
| 1093 | return BitXor; |
| 1094 | case '|': |
| 1095 | return BitOr; |
| 1096 | case '*': |
| 1097 | if (STR(n)[1] == '*') |
| 1098 | return Pow; |
| 1099 | else |
| 1100 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1101 | case '@': |
| 1102 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1103 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1104 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1105 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1110 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1111 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1112 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1113 | |'is' 'not' |
| 1114 | */ |
| 1115 | REQ(n, comp_op); |
| 1116 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1117 | n = CHILD(n, 0); |
| 1118 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1119 | case LESS: |
| 1120 | return Lt; |
| 1121 | case GREATER: |
| 1122 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1123 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1124 | return Eq; |
| 1125 | case LESSEQUAL: |
| 1126 | return LtE; |
| 1127 | case GREATEREQUAL: |
| 1128 | return GtE; |
| 1129 | case NOTEQUAL: |
| 1130 | return NotEq; |
| 1131 | case NAME: |
| 1132 | if (strcmp(STR(n), "in") == 0) |
| 1133 | return In; |
| 1134 | if (strcmp(STR(n), "is") == 0) |
| 1135 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1136 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1137 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1138 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1139 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1140 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1141 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1142 | } |
| 1143 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1144 | /* handle "not in" and "is not" */ |
| 1145 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | case NAME: |
| 1147 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1148 | return NotIn; |
| 1149 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1150 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1151 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1152 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1153 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1154 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1155 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1156 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1157 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1158 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1159 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1160 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static asdl_seq * |
| 1164 | seq_for_testlist(struct compiling *c, const node *n) |
| 1165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1167 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1168 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1169 | asdl_seq *seq; |
| 1170 | expr_ty expression; |
| 1171 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1172 | 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] | 1173 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1174 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1175 | if (!seq) |
| 1176 | return NULL; |
| 1177 | |
| 1178 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1180 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1181 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1182 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1183 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1185 | |
| 1186 | assert(i / 2 < seq->size); |
| 1187 | asdl_seq_SET(seq, i / 2, expression); |
| 1188 | } |
| 1189 | return seq; |
| 1190 | } |
| 1191 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1192 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1193 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1194 | { |
| 1195 | identifier name; |
| 1196 | expr_ty annotation = NULL; |
| 1197 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1198 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1199 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1200 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1201 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1202 | name = NEW_IDENTIFIER(ch); |
| 1203 | if (!name) |
| 1204 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1205 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1206 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1209 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1210 | if (!annotation) |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1214 | 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] | 1215 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1216 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1217 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1220 | /* returns -1 if failed to handle keyword only arguments |
| 1221 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1222 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1223 | ^^^ |
| 1224 | start pointing here |
| 1225 | */ |
| 1226 | static int |
| 1227 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1228 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1229 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1230 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1231 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1232 | expr_ty expression, annotation; |
| 1233 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1234 | int i = start; |
| 1235 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1238 | 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] | 1239 | return -1; |
| 1240 | } |
| 1241 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1242 | while (i < NCH(n)) { |
| 1243 | ch = CHILD(n, i); |
| 1244 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1245 | case vfpdef: |
| 1246 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1247 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1248 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1249 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1250 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1251 | asdl_seq_SET(kwdefaults, j, expression); |
| 1252 | i += 2; /* '=' and test */ |
| 1253 | } |
| 1254 | else { /* setting NULL if no default value exists */ |
| 1255 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1256 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1257 | if (NCH(ch) == 3) { |
| 1258 | /* ch is NAME ':' test */ |
| 1259 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1260 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1261 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1262 | } |
| 1263 | else { |
| 1264 | annotation = NULL; |
| 1265 | } |
| 1266 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1267 | argname = NEW_IDENTIFIER(ch); |
| 1268 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1269 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1270 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1271 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1272 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 1273 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1274 | if (!arg) |
| 1275 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1276 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1277 | i += 2; /* the name and the comma */ |
| 1278 | break; |
| 1279 | case DOUBLESTAR: |
| 1280 | return i; |
| 1281 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1282 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1283 | goto error; |
| 1284 | } |
| 1285 | } |
| 1286 | return i; |
| 1287 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1289 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1290 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1291 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1292 | |
| 1293 | static arguments_ty |
| 1294 | ast_for_arguments(struct compiling *c, const node *n) |
| 1295 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1296 | /* This function handles both typedargslist (function definition) |
| 1297 | and varargslist (lambda definition). |
| 1298 | |
| 1299 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1300 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1301 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1302 | | '**' tfpdef [',']]] |
| 1303 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1304 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1305 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1306 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1307 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1308 | | '**' vfpdef [',']]] |
| 1309 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1310 | | '**' vfpdef [','] |
| 1311 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1312 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1313 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1314 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1315 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1316 | int nposdefaults = 0, found_default = 0; |
| 1317 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1318 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1319 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1320 | node *ch; |
| 1321 | |
| 1322 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1323 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1324 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1325 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1326 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1327 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1328 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1329 | /* First count the number of positional args & defaults. The |
| 1330 | variable i is the loop index for this for loop and the next. |
| 1331 | The next loop picks up where the first leaves off. |
| 1332 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1333 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1334 | ch = CHILD(n, i); |
| 1335 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1336 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1337 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1338 | if (i < NCH(n) && /* skip argument following star */ |
| 1339 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1340 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1341 | i++; |
| 1342 | } |
| 1343 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1344 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1345 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1346 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1347 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1348 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1350 | defaults for keyword only args */ |
| 1351 | for ( ; i < NCH(n); ++i) { |
| 1352 | ch = CHILD(n, i); |
| 1353 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1354 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1355 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1356 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1357 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1358 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1359 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1360 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1361 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1362 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1364 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1365 | if (!posdefaults && nposdefaults) |
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 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1368 | since we set NULL as default for keyword only argument w/o default |
| 1369 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1370 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1371 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1372 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1373 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1374 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1375 | /* tfpdef: NAME [':' test] |
| 1376 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1377 | */ |
| 1378 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1379 | j = 0; /* index for defaults */ |
| 1380 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1381 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1382 | ch = CHILD(n, i); |
| 1383 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1384 | case tfpdef: |
| 1385 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1386 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1387 | anything other than EQUAL or a comma? */ |
| 1388 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1389 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1390 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1391 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1392 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1393 | assert(posdefaults != NULL); |
| 1394 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1395 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1396 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1397 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1398 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1399 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1400 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1401 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1402 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1403 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1404 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1405 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1406 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1407 | i += 2; /* the name and the comma */ |
| 1408 | break; |
| 1409 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1410 | if (i+1 >= NCH(n) || |
| 1411 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1412 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1413 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1414 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1415 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1416 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1417 | if (TYPE(ch) == COMMA) { |
| 1418 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | i += 2; /* now follows keyword only arguments */ |
| 1420 | res = handle_keywordonly_args(c, n, i, |
| 1421 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1422 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1423 | i = res; /* res has new position to process */ |
| 1424 | } |
| 1425 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1426 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1427 | if (!vararg) |
| 1428 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1429 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1430 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1431 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1432 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1433 | int res = 0; |
| 1434 | res = handle_keywordonly_args(c, n, i, |
| 1435 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1436 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1437 | i = res; /* res has new position to process */ |
| 1438 | } |
| 1439 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | break; |
| 1441 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1442 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1443 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1444 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1445 | if (!kwarg) |
| 1446 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1447 | i += 3; |
| 1448 | break; |
| 1449 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1450 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1451 | "unexpected node in varargslist: %d @ %d", |
| 1452 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1453 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1454 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1455 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1456 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static expr_ty |
| 1460 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1461 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1462 | expr_ty e; |
| 1463 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1464 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1465 | int i; |
| 1466 | |
| 1467 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1468 | |
| 1469 | lineno = LINENO(n); |
| 1470 | col_offset = n->n_col_offset; |
| 1471 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1472 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1473 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1474 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1475 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1476 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1477 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1478 | |
| 1479 | for (i = 2; i < NCH(n); i+=2) { |
| 1480 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1481 | if (!id) |
| 1482 | return NULL; |
| 1483 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1484 | if (!e) |
| 1485 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | static expr_ty |
| 1492 | ast_for_decorator(struct compiling *c, const node *n) |
| 1493 | { |
| 1494 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1495 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1496 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1499 | REQ(CHILD(n, 0), AT); |
| 1500 | REQ(RCHILD(n, -1), NEWLINE); |
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 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1503 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1504 | return NULL; |
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 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1507 | d = name_expr; |
| 1508 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1509 | } |
| 1510 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1511 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1512 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1513 | if (!d) |
| 1514 | return NULL; |
| 1515 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | } |
| 1517 | else { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 1518 | d = ast_for_call(c, CHILD(n, 3), name_expr, true); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1519 | if (!d) |
| 1520 | return NULL; |
| 1521 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | static asdl_seq* |
| 1528 | ast_for_decorators(struct compiling *c, const node *n) |
| 1529 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1530 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1531 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1534 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1535 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1536 | if (!decorator_seq) |
| 1537 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1540 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1541 | if (!d) |
| 1542 | return NULL; |
| 1543 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1544 | } |
| 1545 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1549 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1550 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1551 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1552 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1553 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1554 | identifier name; |
| 1555 | arguments_ty args; |
| 1556 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1557 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1558 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1559 | |
| 1560 | REQ(n, funcdef); |
| 1561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1563 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1564 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1565 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1566 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1567 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1568 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1569 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1570 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1571 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1572 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1573 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1574 | name_i += 2; |
| 1575 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1576 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1577 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1578 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1579 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1580 | if (is_async) |
| 1581 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 1582 | LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1583 | else |
| 1584 | return FunctionDef(name, args, body, decorator_seq, returns, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1585 | LINENO(n), n->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | static stmt_ty |
| 1589 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1590 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1591 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1592 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1593 | REQ(CHILD(n, 0), NAME); |
| 1594 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1595 | REQ(CHILD(n, 1), funcdef); |
| 1596 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1597 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1598 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | static stmt_ty |
| 1602 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1603 | { |
| 1604 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1605 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1606 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | |
| 1610 | static stmt_ty |
| 1611 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1612 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1613 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1614 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1615 | REQ(CHILD(n, 0), NAME); |
| 1616 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1617 | |
| 1618 | switch (TYPE(CHILD(n, 1))) { |
| 1619 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1620 | return ast_for_funcdef_impl(c, n, NULL, |
| 1621 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1622 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1623 | return ast_for_with_stmt(c, n, |
| 1624 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1625 | |
| 1626 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1627 | return ast_for_for_stmt(c, n, |
| 1628 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1629 | |
| 1630 | default: |
| 1631 | PyErr_Format(PyExc_SystemError, |
| 1632 | "invalid async stament: %s", |
| 1633 | STR(CHILD(n, 1))); |
| 1634 | return NULL; |
| 1635 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1638 | static stmt_ty |
| 1639 | ast_for_decorated(struct compiling *c, const node *n) |
| 1640 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1641 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1642 | stmt_ty thing = NULL; |
| 1643 | asdl_seq *decorator_seq = NULL; |
| 1644 | |
| 1645 | REQ(n, decorated); |
| 1646 | |
| 1647 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1648 | if (!decorator_seq) |
| 1649 | return NULL; |
| 1650 | |
| 1651 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1652 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1654 | |
| 1655 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1656 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1657 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1658 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1659 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1660 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1661 | } |
| 1662 | return thing; |
| 1663 | } |
| 1664 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1665 | static expr_ty |
| 1666 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1667 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1668 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1669 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1670 | arguments_ty args; |
| 1671 | expr_ty expression; |
| 1672 | |
| 1673 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1674 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1675 | if (!args) |
| 1676 | return NULL; |
| 1677 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1678 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1679 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1680 | } |
| 1681 | else { |
| 1682 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1683 | if (!args) |
| 1684 | return NULL; |
| 1685 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1686 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1687 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1690 | 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] | 1691 | } |
| 1692 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1693 | static expr_ty |
| 1694 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1697 | expr_ty expression, body, orelse; |
| 1698 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1699 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1700 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1701 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1702 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1703 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1704 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1705 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1706 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1707 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1708 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1709 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1710 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1713 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1714 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1715 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1716 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1717 | */ |
| 1718 | |
| 1719 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1720 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1721 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1722 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1723 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1724 | count_comp_for: |
| 1725 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1726 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1727 | if (NCH(n) == 2) { |
| 1728 | REQ(CHILD(n, 0), NAME); |
| 1729 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1730 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1731 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1732 | else if (NCH(n) == 1) { |
| 1733 | n = CHILD(n, 0); |
| 1734 | } |
| 1735 | else { |
| 1736 | goto error; |
| 1737 | } |
| 1738 | if (NCH(n) == (5)) { |
| 1739 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1740 | } |
| 1741 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1742 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1743 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1744 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1745 | REQ(n, comp_iter); |
| 1746 | n = CHILD(n, 0); |
| 1747 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1748 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1749 | else if (TYPE(n) == comp_if) { |
| 1750 | if (NCH(n) == 3) { |
| 1751 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1752 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1753 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1754 | else |
| 1755 | return n_fors; |
| 1756 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1757 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1758 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1759 | /* Should never be reached */ |
| 1760 | PyErr_SetString(PyExc_SystemError, |
| 1761 | "logic error in count_comp_fors"); |
| 1762 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1765 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1766 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1767 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1768 | */ |
| 1769 | |
| 1770 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1771 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1772 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1773 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1774 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1775 | while (1) { |
| 1776 | REQ(n, comp_iter); |
| 1777 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1778 | return n_ifs; |
| 1779 | n = CHILD(n, 0); |
| 1780 | REQ(n, comp_if); |
| 1781 | n_ifs++; |
| 1782 | if (NCH(n) == 2) |
| 1783 | return n_ifs; |
| 1784 | n = CHILD(n, 2); |
| 1785 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1788 | static asdl_seq * |
| 1789 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1790 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1791 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1792 | asdl_seq *comps; |
| 1793 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1794 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1795 | if (n_fors == -1) |
| 1796 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1798 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1799 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1800 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1801 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1802 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1803 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1804 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1805 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1806 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1807 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1808 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1810 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1812 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1813 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1814 | REQ(CHILD(n, 0), NAME); |
| 1815 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1816 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1817 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1818 | else { |
| 1819 | sync_n = CHILD(n, 0); |
| 1820 | } |
| 1821 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1822 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1823 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1824 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1825 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1826 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1827 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1828 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1829 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1830 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1831 | /* Check the # of children rather than the length of t, since |
| 1832 | (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] | 1833 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1834 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1835 | comp = comprehension(first, expression, NULL, |
| 1836 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1837 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1838 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1839 | first->col_offset, c->c_arena), |
| 1840 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1841 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1842 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1843 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1844 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1845 | int j, n_ifs; |
| 1846 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1848 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1849 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1850 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1851 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1852 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1853 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1854 | if (!ifs) |
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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1857 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1858 | REQ(n, comp_iter); |
| 1859 | n = CHILD(n, 0); |
| 1860 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1862 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1863 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1864 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1865 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1866 | if (NCH(n) == 3) |
| 1867 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1868 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1869 | /* on exit, must guarantee that n is a comp_for */ |
| 1870 | if (TYPE(n) == comp_iter) |
| 1871 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1872 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1873 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1874 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1875 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1876 | return comps; |
| 1877 | } |
| 1878 | |
| 1879 | static expr_ty |
| 1880 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1881 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1882 | /* testlist_comp: (test|star_expr) |
| 1883 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1884 | expr_ty elt; |
| 1885 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1886 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1888 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1890 | ch = CHILD(n, 0); |
| 1891 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1892 | if (!elt) |
| 1893 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1894 | if (elt->kind == Starred_kind) { |
| 1895 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1896 | return NULL; |
| 1897 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1899 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1900 | if (!comps) |
| 1901 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1902 | |
| 1903 | if (type == COMP_GENEXP) |
| 1904 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1905 | else if (type == COMP_LISTCOMP) |
| 1906 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1907 | else if (type == COMP_SETCOMP) |
| 1908 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1909 | else |
| 1910 | /* Should never happen */ |
| 1911 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1914 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1915 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1916 | * elements. Iff successful, nonzero is returned. |
| 1917 | */ |
| 1918 | static int |
| 1919 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1920 | expr_ty *key, expr_ty *value) |
| 1921 | { |
| 1922 | expr_ty expression; |
| 1923 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1924 | assert(NCH(n) - *i >= 2); |
| 1925 | |
| 1926 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1927 | if (!expression) |
| 1928 | return 0; |
| 1929 | *key = NULL; |
| 1930 | *value = expression; |
| 1931 | |
| 1932 | *i += 2; |
| 1933 | } |
| 1934 | else { |
| 1935 | assert(NCH(n) - *i >= 3); |
| 1936 | |
| 1937 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1938 | if (!expression) |
| 1939 | return 0; |
| 1940 | *key = expression; |
| 1941 | |
| 1942 | REQ(CHILD(n, *i + 1), COLON); |
| 1943 | |
| 1944 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1945 | if (!expression) |
| 1946 | return 0; |
| 1947 | *value = expression; |
| 1948 | |
| 1949 | *i += 3; |
| 1950 | } |
| 1951 | return 1; |
| 1952 | } |
| 1953 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1954 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1955 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1956 | { |
| 1957 | expr_ty key, value; |
| 1958 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1959 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1961 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1962 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1963 | assert(key); |
| 1964 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1966 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1967 | if (!comps) |
| 1968 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1970 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1971 | } |
| 1972 | |
| 1973 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1974 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 1975 | { |
| 1976 | int i; |
| 1977 | int j; |
| 1978 | int size; |
| 1979 | asdl_seq *keys, *values; |
| 1980 | |
| 1981 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 1982 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 1983 | if (!keys) |
| 1984 | return NULL; |
| 1985 | |
| 1986 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 1987 | if (!values) |
| 1988 | return NULL; |
| 1989 | |
| 1990 | j = 0; |
| 1991 | for (i = 0; i < NCH(n); i++) { |
| 1992 | expr_ty key, value; |
| 1993 | |
| 1994 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 1995 | return NULL; |
| 1996 | asdl_seq_SET(keys, j, key); |
| 1997 | asdl_seq_SET(values, j, value); |
| 1998 | |
| 1999 | j++; |
| 2000 | } |
| 2001 | keys->size = j; |
| 2002 | values->size = j; |
| 2003 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2004 | } |
| 2005 | |
| 2006 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2007 | ast_for_genexp(struct compiling *c, const node *n) |
| 2008 | { |
| 2009 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2010 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | static expr_ty |
| 2014 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2015 | { |
| 2016 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2017 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | static expr_ty |
| 2021 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2022 | { |
| 2023 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2024 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2027 | static expr_ty |
| 2028 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2029 | { |
| 2030 | int i; |
| 2031 | int size; |
| 2032 | asdl_seq *elts; |
| 2033 | |
| 2034 | assert(TYPE(n) == (dictorsetmaker)); |
| 2035 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2036 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2037 | if (!elts) |
| 2038 | return NULL; |
| 2039 | for (i = 0; i < NCH(n); i += 2) { |
| 2040 | expr_ty expression; |
| 2041 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2042 | if (!expression) |
| 2043 | return NULL; |
| 2044 | asdl_seq_SET(elts, i / 2, expression); |
| 2045 | } |
| 2046 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2047 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2048 | |
| 2049 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2050 | ast_for_atom(struct compiling *c, const node *n) |
| 2051 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2052 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2053 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2054 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2055 | */ |
| 2056 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2058 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2059 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2060 | PyObject *name; |
| 2061 | const char *s = STR(ch); |
| 2062 | size_t len = strlen(s); |
| 2063 | if (len >= 4 && len <= 5) { |
| 2064 | if (!strcmp(s, "None")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2065 | 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] | 2066 | if (!strcmp(s, "True")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2067 | 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] | 2068 | if (!strcmp(s, "False")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2069 | 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] | 2070 | } |
| 2071 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2072 | if (!name) |
| 2073 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2074 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2075 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2076 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2077 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2078 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2079 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2080 | const char *errtype = NULL; |
| 2081 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2082 | errtype = "unicode error"; |
| 2083 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2084 | errtype = "value error"; |
| 2085 | if (errtype) { |
| 2086 | char buf[128]; |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2087 | const char *s = NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2088 | PyObject *type, *value, *tback, *errstr; |
| 2089 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2090 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2091 | if (errstr) |
| 2092 | s = PyUnicode_AsUTF8(errstr); |
| 2093 | if (s) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2094 | PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2095 | } else { |
Victor Stinner | 00723e0 | 2015-09-03 12:57:11 +0200 | [diff] [blame] | 2096 | PyErr_Clear(); |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2097 | PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2098 | } |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 2099 | Py_XDECREF(errstr); |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 2100 | ast_error(c, n, buf); |
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 | } |
| 2818 | if (TYPE(expr_node) == lambdef) { |
| 2819 | // f(lambda x: x[0] = 3) ends up getting parsed with LHS |
| 2820 | // test = lambda x: x[0], and RHS test = 3. Issue #132313 |
| 2821 | // points out that complaining about a keyword then is very |
| 2822 | // confusing. |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2823 | ast_error(c, chch, |
| 2824 | "lambda cannot contain assignment"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2825 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2826 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2827 | else if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2828 | ast_error(c, chch, |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2829 | "keyword can't be an expression"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2830 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2831 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2832 | key = new_identifier(STR(expr_node), c); |
| 2833 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2834 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2836 | if (forbidden_name(c, key, chch, 1)) { |
| 2837 | return NULL; |
| 2838 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2839 | for (k = 0; k < nkeywords; k++) { |
| 2840 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2841 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2842 | ast_error(c, chch, |
| 2843 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2844 | return NULL; |
| 2845 | } |
| 2846 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2847 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2848 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2849 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2850 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2851 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2852 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2853 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2854 | } |
| 2855 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2858 | 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] | 2859 | } |
| 2860 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2861 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2862 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2863 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2864 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2865 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2866 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2867 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2868 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2869 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2870 | } |
| 2871 | else { |
| 2872 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2873 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2874 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2876 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2877 | else { |
| 2878 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2879 | if (!tmp) |
| 2880 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2881 | 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] | 2882 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2885 | static stmt_ty |
| 2886 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2887 | { |
| 2888 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2889 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2890 | ('=' (yield_expr|testlist_star_expr))*) |
| 2891 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2892 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2893 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2894 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2895 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2896 | */ |
| 2897 | |
| 2898 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2899 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2900 | if (!e) |
| 2901 | return NULL; |
| 2902 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2903 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | } |
| 2905 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2906 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2907 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2908 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2909 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2910 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2911 | if (!expr1) |
| 2912 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2913 | if(!set_context(c, expr1, Store, ch)) |
| 2914 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2915 | /* set_context checks that most expressions are not the left side. |
| 2916 | Augmented assignments can only have a name, a subscript, or an |
| 2917 | attribute on the left, though, so we have to explicitly check for |
| 2918 | those. */ |
| 2919 | switch (expr1->kind) { |
| 2920 | case Name_kind: |
| 2921 | case Attribute_kind: |
| 2922 | case Subscript_kind: |
| 2923 | break; |
| 2924 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2925 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2926 | return NULL; |
| 2927 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2928 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2929 | ch = CHILD(n, 2); |
| 2930 | if (TYPE(ch) == testlist) |
| 2931 | expr2 = ast_for_testlist(c, ch); |
| 2932 | else |
| 2933 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2934 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2935 | return NULL; |
| 2936 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2937 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2938 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2939 | return NULL; |
| 2940 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2941 | 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] | 2942 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2943 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2944 | expr_ty expr1, expr2, expr3; |
| 2945 | node *ch = CHILD(n, 0); |
| 2946 | node *deep, *ann = CHILD(n, 1); |
| 2947 | int simple = 1; |
| 2948 | |
| 2949 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2950 | deep = ch; |
| 2951 | while (NCH(deep) == 1) { |
| 2952 | deep = CHILD(deep, 0); |
| 2953 | } |
| 2954 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2955 | simple = 0; |
| 2956 | } |
| 2957 | expr1 = ast_for_testlist(c, ch); |
| 2958 | if (!expr1) { |
| 2959 | return NULL; |
| 2960 | } |
| 2961 | switch (expr1->kind) { |
| 2962 | case Name_kind: |
| 2963 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2964 | return NULL; |
| 2965 | } |
| 2966 | expr1->v.Name.ctx = Store; |
| 2967 | break; |
| 2968 | case Attribute_kind: |
| 2969 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2970 | return NULL; |
| 2971 | } |
| 2972 | expr1->v.Attribute.ctx = Store; |
| 2973 | break; |
| 2974 | case Subscript_kind: |
| 2975 | expr1->v.Subscript.ctx = Store; |
| 2976 | break; |
| 2977 | case List_kind: |
| 2978 | ast_error(c, ch, |
| 2979 | "only single target (not list) can be annotated"); |
| 2980 | return NULL; |
| 2981 | case Tuple_kind: |
| 2982 | ast_error(c, ch, |
| 2983 | "only single target (not tuple) can be annotated"); |
| 2984 | return NULL; |
| 2985 | default: |
| 2986 | ast_error(c, ch, |
| 2987 | "illegal target for annotation"); |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | |
| 2991 | if (expr1->kind != Name_kind) { |
| 2992 | simple = 0; |
| 2993 | } |
| 2994 | ch = CHILD(ann, 1); |
| 2995 | expr2 = ast_for_expr(c, ch); |
| 2996 | if (!expr2) { |
| 2997 | return NULL; |
| 2998 | } |
| 2999 | if (NCH(ann) == 2) { |
| 3000 | return AnnAssign(expr1, expr2, NULL, simple, |
| 3001 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3002 | } |
| 3003 | else { |
| 3004 | ch = CHILD(ann, 3); |
| 3005 | expr3 = ast_for_expr(c, ch); |
| 3006 | if (!expr3) { |
| 3007 | return NULL; |
| 3008 | } |
| 3009 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3010 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3011 | } |
| 3012 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3014 | int i; |
| 3015 | asdl_seq *targets; |
| 3016 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3017 | expr_ty expression; |
| 3018 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3019 | /* a normal assignment */ |
| 3020 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3021 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3022 | if (!targets) |
| 3023 | return NULL; |
| 3024 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3025 | expr_ty e; |
| 3026 | node *ch = CHILD(n, i); |
| 3027 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3028 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3029 | return NULL; |
| 3030 | } |
| 3031 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3033 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3034 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3035 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3036 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3037 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3038 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3039 | asdl_seq_SET(targets, i / 2, e); |
| 3040 | } |
| 3041 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3042 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3043 | expression = ast_for_testlist(c, value); |
| 3044 | else |
| 3045 | expression = ast_for_expr(c, value); |
| 3046 | if (!expression) |
| 3047 | return NULL; |
| 3048 | 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] | 3049 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3052 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3053 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3054 | 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] | 3055 | { |
| 3056 | asdl_seq *seq; |
| 3057 | int i; |
| 3058 | expr_ty e; |
| 3059 | |
| 3060 | REQ(n, exprlist); |
| 3061 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3062 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3063 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3064 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3065 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3066 | e = ast_for_expr(c, CHILD(n, i)); |
| 3067 | if (!e) |
| 3068 | return NULL; |
| 3069 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3070 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3071 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3072 | } |
| 3073 | return seq; |
| 3074 | } |
| 3075 | |
| 3076 | static stmt_ty |
| 3077 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3078 | { |
| 3079 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3081 | /* del_stmt: 'del' exprlist */ |
| 3082 | REQ(n, del_stmt); |
| 3083 | |
| 3084 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3085 | if (!expr_list) |
| 3086 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3087 | 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] | 3088 | } |
| 3089 | |
| 3090 | static stmt_ty |
| 3091 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3092 | { |
| 3093 | /* |
| 3094 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3095 | | yield_stmt |
| 3096 | break_stmt: 'break' |
| 3097 | continue_stmt: 'continue' |
| 3098 | return_stmt: 'return' [testlist] |
| 3099 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3100 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3101 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3102 | */ |
| 3103 | node *ch; |
| 3104 | |
| 3105 | REQ(n, flow_stmt); |
| 3106 | ch = CHILD(n, 0); |
| 3107 | switch (TYPE(ch)) { |
| 3108 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3109 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3110 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3111 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3113 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3114 | if (!exp) |
| 3115 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3116 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3117 | } |
| 3118 | case return_stmt: |
| 3119 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3120 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3121 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3122 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3123 | if (!expression) |
| 3124 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3125 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3126 | } |
| 3127 | case raise_stmt: |
| 3128 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3129 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3130 | else if (NCH(ch) >= 2) { |
| 3131 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3132 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3133 | if (!expression) |
| 3134 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3135 | if (NCH(ch) == 4) { |
| 3136 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3137 | if (!cause) |
| 3138 | return NULL; |
| 3139 | } |
| 3140 | 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] | 3141 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3142 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3143 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3144 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3145 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3151 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | { |
| 3153 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3154 | import_as_name: NAME ['as' NAME] |
| 3155 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3156 | dotted_name: NAME ('.' NAME)* |
| 3157 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3158 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3159 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3160 | loop: |
| 3161 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3162 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3163 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3164 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3165 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3166 | if (!name) |
| 3167 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3168 | if (NCH(n) == 3) { |
| 3169 | node *str_node = CHILD(n, 2); |
| 3170 | str = NEW_IDENTIFIER(str_node); |
| 3171 | if (!str) |
| 3172 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3173 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3174 | return NULL; |
| 3175 | } |
| 3176 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3177 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3178 | return NULL; |
| 3179 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3180 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3181 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3182 | case dotted_as_name: |
| 3183 | if (NCH(n) == 1) { |
| 3184 | n = CHILD(n, 0); |
| 3185 | goto loop; |
| 3186 | } |
| 3187 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3188 | node *asname_node = CHILD(n, 2); |
| 3189 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3190 | if (!a) |
| 3191 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3192 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3193 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3194 | if (!a->asname) |
| 3195 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3196 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3197 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3198 | return a; |
| 3199 | } |
| 3200 | break; |
| 3201 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3202 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3203 | node *name_node = CHILD(n, 0); |
| 3204 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3205 | if (!name) |
| 3206 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3207 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3208 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3209 | return alias(name, NULL, c->c_arena); |
| 3210 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3211 | else { |
| 3212 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3213 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3214 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3215 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3217 | |
| 3218 | len = 0; |
| 3219 | for (i = 0; i < NCH(n); i += 2) |
| 3220 | /* length of string plus one for the dot */ |
| 3221 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3222 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3223 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3224 | if (!str) |
| 3225 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3226 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3227 | if (!s) |
| 3228 | return NULL; |
| 3229 | for (i = 0; i < NCH(n); i += 2) { |
| 3230 | char *sch = STR(CHILD(n, i)); |
| 3231 | strcpy(s, STR(CHILD(n, i))); |
| 3232 | s += strlen(sch); |
| 3233 | *s++ = '.'; |
| 3234 | } |
| 3235 | --s; |
| 3236 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3238 | PyBytes_GET_SIZE(str), |
| 3239 | NULL); |
| 3240 | Py_DECREF(str); |
| 3241 | if (!uni) |
| 3242 | return NULL; |
| 3243 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3244 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3245 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3246 | Py_DECREF(str); |
| 3247 | return NULL; |
| 3248 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3249 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3250 | } |
| 3251 | break; |
| 3252 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3253 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3254 | if (!str) |
| 3255 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3256 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3257 | Py_DECREF(str); |
| 3258 | return NULL; |
| 3259 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3260 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3261 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3262 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3263 | "unexpected import name: %d", TYPE(n)); |
| 3264 | return NULL; |
| 3265 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3266 | |
| 3267 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3268 | return NULL; |
| 3269 | } |
| 3270 | |
| 3271 | static stmt_ty |
| 3272 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3273 | { |
| 3274 | /* |
| 3275 | import_stmt: import_name | import_from |
| 3276 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3277 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3278 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3279 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3280 | int lineno; |
| 3281 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3282 | int i; |
| 3283 | asdl_seq *aliases; |
| 3284 | |
| 3285 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3286 | lineno = LINENO(n); |
| 3287 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3289 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3290 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3291 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3292 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3293 | if (!aliases) |
| 3294 | return NULL; |
| 3295 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3296 | 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] | 3297 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3298 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3299 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3300 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3301 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3302 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3303 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3304 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3305 | int idx, ndots = 0; |
| 3306 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3307 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3308 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3309 | /* Count the number of dots (for relative imports) and check for the |
| 3310 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3311 | for (idx = 1; idx < NCH(n); idx++) { |
| 3312 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3313 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3314 | if (!mod) |
| 3315 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3316 | idx++; |
| 3317 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3318 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3320 | ndots += 3; |
| 3321 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3322 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3323 | break; |
| 3324 | } |
| 3325 | ndots++; |
| 3326 | } |
| 3327 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3328 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3329 | case STAR: |
| 3330 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3331 | n = CHILD(n, idx); |
| 3332 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3333 | break; |
| 3334 | case LPAR: |
| 3335 | /* from ... import (x, y, z) */ |
| 3336 | n = CHILD(n, idx + 1); |
| 3337 | n_children = NCH(n); |
| 3338 | break; |
| 3339 | case import_as_names: |
| 3340 | /* from ... import x, y, z */ |
| 3341 | n = CHILD(n, idx); |
| 3342 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3343 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3344 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3345 | " surrounding parentheses"); |
| 3346 | return NULL; |
| 3347 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3348 | break; |
| 3349 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3350 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3351 | return NULL; |
| 3352 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3353 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3354 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3355 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3357 | |
| 3358 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3359 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3360 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3361 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3362 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3363 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3364 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3365 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3366 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3367 | 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] | 3368 | if (!import_alias) |
| 3369 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3370 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3371 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3372 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3373 | if (mod != NULL) |
| 3374 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3375 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3376 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3377 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3378 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3379 | "unknown import statement: starts with command '%s'", |
| 3380 | STR(CHILD(n, 0))); |
| 3381 | return NULL; |
| 3382 | } |
| 3383 | |
| 3384 | static stmt_ty |
| 3385 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3386 | { |
| 3387 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3388 | identifier name; |
| 3389 | asdl_seq *s; |
| 3390 | int i; |
| 3391 | |
| 3392 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3393 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3394 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3395 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3396 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3397 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3398 | if (!name) |
| 3399 | return NULL; |
| 3400 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3401 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3402 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3406 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3407 | { |
| 3408 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3409 | identifier name; |
| 3410 | asdl_seq *s; |
| 3411 | int i; |
| 3412 | |
| 3413 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3414 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3415 | if (!s) |
| 3416 | return NULL; |
| 3417 | for (i = 1; i < NCH(n); i += 2) { |
| 3418 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3419 | if (!name) |
| 3420 | return NULL; |
| 3421 | asdl_seq_SET(s, i / 2, name); |
| 3422 | } |
| 3423 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3424 | } |
| 3425 | |
| 3426 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3427 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3428 | { |
| 3429 | /* assert_stmt: 'assert' test [',' test] */ |
| 3430 | REQ(n, assert_stmt); |
| 3431 | if (NCH(n) == 2) { |
| 3432 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3433 | if (!expression) |
| 3434 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3435 | 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] | 3436 | } |
| 3437 | else if (NCH(n) == 4) { |
| 3438 | expr_ty expr1, expr2; |
| 3439 | |
| 3440 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3441 | if (!expr1) |
| 3442 | return NULL; |
| 3443 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3444 | if (!expr2) |
| 3445 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3447 | 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] | 3448 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3449 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3450 | "improper number of parts to 'assert' statement: %d", |
| 3451 | NCH(n)); |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | |
| 3455 | static asdl_seq * |
| 3456 | ast_for_suite(struct compiling *c, const node *n) |
| 3457 | { |
| 3458 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3459 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3460 | stmt_ty s; |
| 3461 | int i, total, num, end, pos = 0; |
| 3462 | node *ch; |
| 3463 | |
| 3464 | REQ(n, suite); |
| 3465 | |
| 3466 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3467 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3468 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3469 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3470 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3471 | n = CHILD(n, 0); |
| 3472 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3474 | */ |
| 3475 | end = NCH(n) - 1; |
| 3476 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3477 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3478 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3479 | for (i = 0; i < end; i += 2) { |
| 3480 | ch = CHILD(n, i); |
| 3481 | s = ast_for_stmt(c, ch); |
| 3482 | if (!s) |
| 3483 | return NULL; |
| 3484 | asdl_seq_SET(seq, pos++, s); |
| 3485 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3486 | } |
| 3487 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3488 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3489 | ch = CHILD(n, i); |
| 3490 | REQ(ch, stmt); |
| 3491 | num = num_stmts(ch); |
| 3492 | if (num == 1) { |
| 3493 | /* small_stmt or compound_stmt with only one child */ |
| 3494 | s = ast_for_stmt(c, ch); |
| 3495 | if (!s) |
| 3496 | return NULL; |
| 3497 | asdl_seq_SET(seq, pos++, s); |
| 3498 | } |
| 3499 | else { |
| 3500 | int j; |
| 3501 | ch = CHILD(ch, 0); |
| 3502 | REQ(ch, simple_stmt); |
| 3503 | for (j = 0; j < NCH(ch); j += 2) { |
| 3504 | /* statement terminates with a semi-colon ';' */ |
| 3505 | if (NCH(CHILD(ch, j)) == 0) { |
| 3506 | assert((j + 1) == NCH(ch)); |
| 3507 | break; |
| 3508 | } |
| 3509 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3510 | if (!s) |
| 3511 | return NULL; |
| 3512 | asdl_seq_SET(seq, pos++, s); |
| 3513 | } |
| 3514 | } |
| 3515 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3516 | } |
| 3517 | assert(pos == seq->size); |
| 3518 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | static stmt_ty |
| 3522 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3523 | { |
| 3524 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3525 | ['else' ':' suite] |
| 3526 | */ |
| 3527 | char *s; |
| 3528 | |
| 3529 | REQ(n, if_stmt); |
| 3530 | |
| 3531 | if (NCH(n) == 4) { |
| 3532 | expr_ty expression; |
| 3533 | asdl_seq *suite_seq; |
| 3534 | |
| 3535 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3536 | if (!expression) |
| 3537 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3539 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3540 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3542 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3543 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3544 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3545 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3546 | s = STR(CHILD(n, 4)); |
| 3547 | /* s[2], the third character in the string, will be |
| 3548 | 's' for el_s_e, or |
| 3549 | 'i' for el_i_f |
| 3550 | */ |
| 3551 | if (s[2] == 's') { |
| 3552 | expr_ty expression; |
| 3553 | asdl_seq *seq1, *seq2; |
| 3554 | |
| 3555 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3556 | if (!expression) |
| 3557 | return NULL; |
| 3558 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3559 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3560 | return NULL; |
| 3561 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3562 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3563 | return NULL; |
| 3564 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3565 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3566 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3567 | } |
| 3568 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3569 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3570 | expr_ty expression; |
| 3571 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3572 | asdl_seq *orelse = NULL; |
| 3573 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3574 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3575 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3576 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3577 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3578 | has_else = 1; |
| 3579 | n_elif -= 3; |
| 3580 | } |
| 3581 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3582 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3583 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3584 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3585 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3586 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3587 | if (!orelse) |
| 3588 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3589 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3590 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3591 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3592 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3593 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3594 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3595 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3596 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | asdl_seq_SET(orelse, 0, |
| 3600 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3601 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3602 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3603 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3604 | /* the just-created orelse handled the last elif */ |
| 3605 | n_elif--; |
| 3606 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3607 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3608 | for (i = 0; i < n_elif; i++) { |
| 3609 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3610 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3611 | if (!newobj) |
| 3612 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3613 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3614 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3615 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3616 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3617 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3618 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3619 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3620 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3622 | LINENO(CHILD(n, off)), |
| 3623 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3624 | orelse = newobj; |
| 3625 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3626 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3627 | if (!expression) |
| 3628 | return NULL; |
| 3629 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3630 | if (!suite_seq) |
| 3631 | return NULL; |
| 3632 | return If(expression, suite_seq, orelse, |
| 3633 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3634 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3635 | |
| 3636 | PyErr_Format(PyExc_SystemError, |
| 3637 | "unexpected token in 'if' statement: %s", s); |
| 3638 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | static stmt_ty |
| 3642 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3643 | { |
| 3644 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3645 | REQ(n, while_stmt); |
| 3646 | |
| 3647 | if (NCH(n) == 4) { |
| 3648 | expr_ty expression; |
| 3649 | asdl_seq *suite_seq; |
| 3650 | |
| 3651 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3652 | if (!expression) |
| 3653 | return NULL; |
| 3654 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3655 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3656 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3657 | 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] | 3658 | } |
| 3659 | else if (NCH(n) == 7) { |
| 3660 | expr_ty expression; |
| 3661 | asdl_seq *seq1, *seq2; |
| 3662 | |
| 3663 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3664 | if (!expression) |
| 3665 | return NULL; |
| 3666 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3667 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | return NULL; |
| 3669 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3670 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3671 | return NULL; |
| 3672 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3673 | 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] | 3674 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3675 | |
| 3676 | PyErr_Format(PyExc_SystemError, |
| 3677 | "wrong number of tokens for 'while' statement: %d", |
| 3678 | NCH(n)); |
| 3679 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3683 | 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] | 3684 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3685 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3686 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3688 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3689 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3690 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3691 | REQ(n, for_stmt); |
| 3692 | |
| 3693 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3694 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3695 | if (!seq) |
| 3696 | return NULL; |
| 3697 | } |
| 3698 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3699 | node_target = CHILD(n, 1); |
| 3700 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3701 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3702 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3703 | /* Check the # of children rather than the length of _target, since |
| 3704 | 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] | 3705 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3706 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3707 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3708 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3709 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3710 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3711 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3712 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3713 | return NULL; |
| 3714 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3715 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | return NULL; |
| 3717 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3718 | if (is_async) |
| 3719 | return AsyncFor(target, expression, suite_seq, seq, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3720 | LINENO(n0), n0->n_col_offset, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3721 | c->c_arena); |
| 3722 | else |
| 3723 | return For(target, expression, suite_seq, seq, |
| 3724 | LINENO(n), n->n_col_offset, |
| 3725 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | static excepthandler_ty |
| 3729 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3730 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3731 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3732 | REQ(exc, except_clause); |
| 3733 | REQ(body, suite); |
| 3734 | |
| 3735 | if (NCH(exc) == 1) { |
| 3736 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3737 | if (!suite_seq) |
| 3738 | return NULL; |
| 3739 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3740 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3741 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3742 | } |
| 3743 | else if (NCH(exc) == 2) { |
| 3744 | expr_ty expression; |
| 3745 | asdl_seq *suite_seq; |
| 3746 | |
| 3747 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3748 | if (!expression) |
| 3749 | return NULL; |
| 3750 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3751 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3752 | return NULL; |
| 3753 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3754 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3755 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3756 | } |
| 3757 | else if (NCH(exc) == 4) { |
| 3758 | asdl_seq *suite_seq; |
| 3759 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3760 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3761 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3762 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3763 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3764 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3765 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3766 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3767 | return NULL; |
| 3768 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3769 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3770 | return NULL; |
| 3771 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3772 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3773 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3774 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3775 | |
| 3776 | PyErr_Format(PyExc_SystemError, |
| 3777 | "wrong number of children for 'except' clause: %d", |
| 3778 | NCH(exc)); |
| 3779 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | static stmt_ty |
| 3783 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3784 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3785 | const int nch = NCH(n); |
| 3786 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3787 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3788 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | REQ(n, try_stmt); |
| 3790 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3791 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3792 | if (body == NULL) |
| 3793 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3794 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3795 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3796 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3797 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3798 | /* we can assume it's an "else", |
| 3799 | because nch >= 9 for try-else-finally and |
| 3800 | it would otherwise have a type of except_clause */ |
| 3801 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3802 | if (orelse == NULL) |
| 3803 | return NULL; |
| 3804 | n_except--; |
| 3805 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3806 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3807 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3808 | if (finally == NULL) |
| 3809 | return NULL; |
| 3810 | n_except--; |
| 3811 | } |
| 3812 | else { |
| 3813 | /* we can assume it's an "else", |
| 3814 | otherwise it would have a type of except_clause */ |
| 3815 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3816 | if (orelse == NULL) |
| 3817 | return NULL; |
| 3818 | n_except--; |
| 3819 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3820 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3821 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3822 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3823 | return NULL; |
| 3824 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3826 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3827 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3828 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3829 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3830 | if (handlers == NULL) |
| 3831 | return NULL; |
| 3832 | |
| 3833 | for (i = 0; i < n_except; i++) { |
| 3834 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3835 | CHILD(n, 5 + i * 3)); |
| 3836 | if (!e) |
| 3837 | return NULL; |
| 3838 | asdl_seq_SET(handlers, i, e); |
| 3839 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3842 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3843 | 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] | 3844 | } |
| 3845 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3846 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3847 | static withitem_ty |
| 3848 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3849 | { |
| 3850 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3851 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3852 | REQ(n, with_item); |
| 3853 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3854 | if (!context_expr) |
| 3855 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3856 | if (NCH(n) == 3) { |
| 3857 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3858 | |
| 3859 | if (!optional_vars) { |
| 3860 | return NULL; |
| 3861 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3862 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3863 | return NULL; |
| 3864 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3867 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3870 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3871 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3872 | 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] | 3873 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3874 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3875 | int i, n_items; |
| 3876 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3877 | |
| 3878 | REQ(n, with_stmt); |
| 3879 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3880 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3881 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3882 | if (!items) |
| 3883 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3884 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3885 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3886 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3887 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3888 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3891 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3892 | if (!body) |
| 3893 | return NULL; |
| 3894 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3895 | if (is_async) |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3896 | 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] | 3897 | else |
| 3898 | 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] | 3899 | } |
| 3900 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3901 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3902 | 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] | 3903 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3904 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3905 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3906 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3907 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3908 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3909 | REQ(n, classdef); |
| 3910 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3911 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3912 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3913 | if (!s) |
| 3914 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3915 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3916 | if (!classname) |
| 3917 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3918 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3919 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3920 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3921 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3922 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3923 | |
| 3924 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3925 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3926 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3927 | return NULL; |
| 3928 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3929 | if (!classname) |
| 3930 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3931 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3932 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3933 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3934 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3937 | /* class NAME '(' arglist ')' ':' suite */ |
| 3938 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3939 | { |
| 3940 | PyObject *dummy_name; |
| 3941 | expr_ty dummy; |
| 3942 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3943 | if (!dummy_name) |
| 3944 | return NULL; |
| 3945 | 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] | 3946 | call = ast_for_call(c, CHILD(n, 3), dummy, false); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3947 | if (!call) |
| 3948 | return NULL; |
| 3949 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3950 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3951 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3952 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3953 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3954 | if (!classname) |
| 3955 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3956 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3957 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3958 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3959 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3960 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | static stmt_ty |
| 3964 | ast_for_stmt(struct compiling *c, const node *n) |
| 3965 | { |
| 3966 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3967 | assert(NCH(n) == 1); |
| 3968 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3969 | } |
| 3970 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3971 | assert(num_stmts(n) == 1); |
| 3972 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3973 | } |
| 3974 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3975 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3976 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3977 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3978 | */ |
| 3979 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3980 | case expr_stmt: |
| 3981 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3982 | case del_stmt: |
| 3983 | return ast_for_del_stmt(c, n); |
| 3984 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3985 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3986 | case flow_stmt: |
| 3987 | return ast_for_flow_stmt(c, n); |
| 3988 | case import_stmt: |
| 3989 | return ast_for_import_stmt(c, n); |
| 3990 | case global_stmt: |
| 3991 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3992 | case nonlocal_stmt: |
| 3993 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3994 | case assert_stmt: |
| 3995 | return ast_for_assert_stmt(c, n); |
| 3996 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3997 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3998 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3999 | TYPE(n), NCH(n)); |
| 4000 | return NULL; |
| 4001 | } |
| 4002 | } |
| 4003 | else { |
| 4004 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4005 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4006 | */ |
| 4007 | node *ch = CHILD(n, 0); |
| 4008 | REQ(n, compound_stmt); |
| 4009 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4010 | case if_stmt: |
| 4011 | return ast_for_if_stmt(c, ch); |
| 4012 | case while_stmt: |
| 4013 | return ast_for_while_stmt(c, ch); |
| 4014 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4015 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4016 | case try_stmt: |
| 4017 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4018 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4019 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4020 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4021 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4022 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4023 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4024 | case decorated: |
| 4025 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4026 | case async_stmt: |
| 4027 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4028 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4029 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4030 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4031 | TYPE(n), NCH(n)); |
| 4032 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4033 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4038 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4039 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4040 | const char *end; |
| 4041 | long x; |
| 4042 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4043 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4044 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4045 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4046 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4047 | errno = 0; |
| 4048 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4049 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4050 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4051 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4052 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4053 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4054 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4055 | } |
| 4056 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4057 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4058 | if (*end == '\0') { |
| 4059 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4060 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4061 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4062 | } |
| 4063 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4064 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4065 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4066 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4067 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4068 | return NULL; |
| 4069 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4070 | } |
| 4071 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4072 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4073 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4074 | if (dx == -1.0 && PyErr_Occurred()) |
| 4075 | return NULL; |
| 4076 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4077 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4081 | parsenumber(struct compiling *c, const char *s) |
| 4082 | { |
| 4083 | char *dup, *end; |
| 4084 | PyObject *res = NULL; |
| 4085 | |
| 4086 | assert(s != NULL); |
| 4087 | |
| 4088 | if (strchr(s, '_') == NULL) { |
| 4089 | return parsenumber_raw(c, s); |
| 4090 | } |
| 4091 | /* Create a duplicate without underscores. */ |
| 4092 | dup = PyMem_Malloc(strlen(s) + 1); |
| 4093 | end = dup; |
| 4094 | for (; *s; s++) { |
| 4095 | if (*s != '_') { |
| 4096 | *end++ = *s; |
| 4097 | } |
| 4098 | } |
| 4099 | *end = '\0'; |
| 4100 | res = parsenumber_raw(c, dup); |
| 4101 | PyMem_Free(dup); |
| 4102 | return res; |
| 4103 | } |
| 4104 | |
| 4105 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4106 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4107 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4108 | const char *s, *t; |
| 4109 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4110 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4111 | while (s < end && (*s & 0x80)) s++; |
| 4112 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4113 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4116 | static int |
| 4117 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4118 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4119 | { |
| 4120 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4121 | first_invalid_escape_char); |
| 4122 | if (msg == NULL) { |
| 4123 | return -1; |
| 4124 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4125 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4126 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4127 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4128 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4129 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4130 | const char *s; |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4131 | |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4132 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4133 | to get a more accurate error report */ |
| 4134 | PyErr_Clear(); |
Victor Stinner | f9cca36 | 2016-11-15 09:12:10 +0100 | [diff] [blame] | 4135 | |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4136 | s = PyUnicode_AsUTF8(msg); |
| 4137 | if (s != NULL) { |
| 4138 | ast_error(c, n, s); |
| 4139 | } |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4140 | } |
| 4141 | Py_DECREF(msg); |
| 4142 | return -1; |
| 4143 | } |
| 4144 | Py_DECREF(msg); |
| 4145 | return 0; |
| 4146 | } |
| 4147 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4148 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4149 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4150 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4151 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4152 | PyObject *v, *u; |
| 4153 | char *buf; |
| 4154 | char *p; |
| 4155 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4156 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4157 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4158 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4159 | return NULL; |
| 4160 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4161 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4162 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4163 | if (u == NULL) |
| 4164 | return NULL; |
| 4165 | p = buf = PyBytes_AsString(u); |
| 4166 | end = s + len; |
| 4167 | while (s < end) { |
| 4168 | if (*s == '\\') { |
| 4169 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4170 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4171 | strcpy(p, "u005c"); |
| 4172 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4173 | if (s >= end) |
| 4174 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4175 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4176 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4177 | if (*s & 0x80) { /* XXX inefficient */ |
| 4178 | PyObject *w; |
| 4179 | int kind; |
| 4180 | void *data; |
| 4181 | Py_ssize_t len, i; |
| 4182 | w = decode_utf8(c, &s, end); |
| 4183 | if (w == NULL) { |
| 4184 | Py_DECREF(u); |
| 4185 | return NULL; |
| 4186 | } |
| 4187 | kind = PyUnicode_KIND(w); |
| 4188 | data = PyUnicode_DATA(w); |
| 4189 | len = PyUnicode_GET_LENGTH(w); |
| 4190 | for (i = 0; i < len; i++) { |
| 4191 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4192 | sprintf(p, "\\U%08x", chr); |
| 4193 | p += 10; |
| 4194 | } |
| 4195 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4196 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4197 | Py_DECREF(w); |
| 4198 | } else { |
| 4199 | *p++ = *s++; |
| 4200 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4201 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4202 | len = p - buf; |
| 4203 | s = buf; |
| 4204 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4205 | const char *first_invalid_escape; |
| 4206 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4207 | |
| 4208 | if (v != NULL && first_invalid_escape != NULL) { |
| 4209 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4210 | /* We have not decref u before because first_invalid_escape points |
| 4211 | inside u. */ |
| 4212 | Py_XDECREF(u); |
| 4213 | Py_DECREF(v); |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4217 | Py_XDECREF(u); |
| 4218 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4221 | static PyObject * |
| 4222 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4223 | size_t len) |
| 4224 | { |
| 4225 | const char *first_invalid_escape; |
| 4226 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4227 | &first_invalid_escape); |
| 4228 | if (result == NULL) |
| 4229 | return NULL; |
| 4230 | |
| 4231 | if (first_invalid_escape != NULL) { |
| 4232 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4233 | Py_DECREF(result); |
| 4234 | return NULL; |
| 4235 | } |
| 4236 | } |
| 4237 | return result; |
| 4238 | } |
| 4239 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4240 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4241 | and `col_offset` to existing locations. */ |
| 4242 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4243 | { |
| 4244 | n->n_col_offset = n->n_col_offset + col_offset; |
| 4245 | for (int i = 0; i < NCH(n); ++i) { |
| 4246 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4247 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4248 | col_offset = 0; |
| 4249 | } |
| 4250 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4251 | } |
| 4252 | n->n_lineno = n->n_lineno + lineno; |
| 4253 | } |
| 4254 | |
| 4255 | /* Fix locations for the given node and its children. |
| 4256 | |
| 4257 | `parent` is the enclosing node. |
| 4258 | `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] | 4259 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4260 | */ |
| 4261 | static void |
| 4262 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4263 | { |
| 4264 | char *substr = NULL; |
| 4265 | char *start; |
| 4266 | int lines = LINENO(parent) - 1; |
| 4267 | int cols = parent->n_col_offset; |
| 4268 | /* Find the full fstring to fix location information in `n`. */ |
| 4269 | while (parent && parent->n_type != STRING) |
| 4270 | parent = parent->n_child; |
| 4271 | if (parent && parent->n_str) { |
| 4272 | substr = strstr(parent->n_str, expr_str); |
| 4273 | if (substr) { |
| 4274 | start = substr; |
| 4275 | while (start > parent->n_str) { |
| 4276 | if (start[0] == '\n') |
| 4277 | break; |
| 4278 | start--; |
| 4279 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4280 | cols += (int)(substr - start); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4281 | /* Fix lineno in mulitline strings. */ |
| 4282 | while ((substr = strchr(substr + 1, '\n'))) |
| 4283 | lines--; |
| 4284 | } |
| 4285 | } |
| 4286 | fstring_shift_node_locations(n, lines, cols); |
| 4287 | } |
| 4288 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4289 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4290 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4291 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4292 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4293 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4294 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4295 | { |
| 4296 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4297 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4298 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4299 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4300 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4301 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4302 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4303 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4304 | assert(*(expr_start-1) == '{'); |
| 4305 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4306 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4307 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4308 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4309 | because turning the expression '' in to '()' would go from being invalid |
| 4310 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4311 | for (s = expr_start; s != expr_end; s++) { |
| 4312 | char c = *s; |
| 4313 | /* The Python parser ignores only the following whitespace |
| 4314 | characters (\r already is converted to \n). */ |
| 4315 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4316 | break; |
| 4317 | } |
| 4318 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4319 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4320 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4321 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4322 | } |
| 4323 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4324 | len = expr_end - expr_start; |
| 4325 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4326 | str = PyMem_RawMalloc(len + 3); |
| 4327 | if (str == NULL) |
| 4328 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4329 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4330 | str[0] = '('; |
| 4331 | memcpy(str+1, expr_start, len); |
| 4332 | str[len+1] = ')'; |
| 4333 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4334 | |
| 4335 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4336 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4337 | Py_eval_input, 0); |
| 4338 | if (!mod_n) { |
| 4339 | PyMem_RawFree(str); |
| 4340 | return NULL; |
| 4341 | } |
| 4342 | /* Reuse str to find the correct column offset. */ |
| 4343 | str[0] = '{'; |
| 4344 | str[len+1] = '}'; |
| 4345 | fstring_fix_node_location(n, mod_n, str); |
| 4346 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4347 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4348 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4349 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4350 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4351 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | /* Return -1 on error. |
| 4355 | |
| 4356 | Return 0 if we reached the end of the literal. |
| 4357 | |
| 4358 | Return 1 if we haven't reached the end of the literal, but we want |
| 4359 | the caller to process the literal up to this point. Used for |
| 4360 | doubled braces. |
| 4361 | */ |
| 4362 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4363 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4364 | PyObject **literal, int recurse_lvl, |
| 4365 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4366 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4367 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4368 | brace (which isn't part of a unicode name escape such as |
| 4369 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4370 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4371 | const char *s = *str; |
| 4372 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4373 | int result = 0; |
| 4374 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4375 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4376 | while (s < end) { |
| 4377 | char ch = *s++; |
| 4378 | if (!raw && ch == '\\' && s < end) { |
| 4379 | ch = *s++; |
| 4380 | if (ch == 'N') { |
| 4381 | if (s < end && *s++ == '{') { |
| 4382 | while (s < end && *s++ != '}') { |
| 4383 | } |
| 4384 | continue; |
| 4385 | } |
| 4386 | break; |
| 4387 | } |
| 4388 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4389 | return -1; |
| 4390 | } |
| 4391 | } |
| 4392 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4393 | /* Check for doubled braces, but only at the top level. If |
| 4394 | we checked at every level, then f'{0:{3}}' would fail |
| 4395 | with the two closing braces. */ |
| 4396 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4397 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4398 | /* We're going to tell the caller that the literal ends |
| 4399 | here, but that they should continue scanning. But also |
| 4400 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4401 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4402 | result = 1; |
| 4403 | goto done; |
| 4404 | } |
| 4405 | |
| 4406 | /* Where a single '{' is the start of a new expression, a |
| 4407 | single '}' is not allowed. */ |
| 4408 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4409 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4410 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4411 | return -1; |
| 4412 | } |
| 4413 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4414 | /* We're either at a '{', which means we're starting another |
| 4415 | expression; or a '}', which means we're at the end of this |
| 4416 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4417 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4418 | break; |
| 4419 | } |
| 4420 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4421 | *str = s; |
| 4422 | assert(s <= end); |
| 4423 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4424 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4425 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4426 | if (raw) |
| 4427 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4428 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4429 | NULL, NULL); |
| 4430 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4431 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4432 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4433 | if (!*literal) |
| 4434 | return -1; |
| 4435 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4436 | return result; |
| 4437 | } |
| 4438 | |
| 4439 | /* Forward declaration because parsing is recursive. */ |
| 4440 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4441 | 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] | 4442 | struct compiling *c, const node *n); |
| 4443 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4444 | /* 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] | 4445 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4446 | which includes the expression, conversion character, and |
| 4447 | format_spec expression. |
| 4448 | |
| 4449 | Note that I don't do a perfect job here: I don't make sure that a |
| 4450 | closing brace doesn't match an opening paren, for example. It |
| 4451 | doesn't need to error on all invalid expressions, just correctly |
| 4452 | find the end of all valid ones. Any errors inside the expression |
| 4453 | will be caught when we parse it later. */ |
| 4454 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4455 | 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] | 4456 | expr_ty *expression, struct compiling *c, const node *n) |
| 4457 | { |
| 4458 | /* Return -1 on error, else 0. */ |
| 4459 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4460 | const char *expr_start; |
| 4461 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4462 | expr_ty simple_expression; |
| 4463 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4464 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4465 | |
| 4466 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4467 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4468 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4469 | |
| 4470 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4471 | int string_type = 0; |
| 4472 | |
| 4473 | /* Keep track of nesting level for braces/parens/brackets in |
| 4474 | expressions. */ |
| 4475 | Py_ssize_t nested_depth = 0; |
| 4476 | |
| 4477 | /* Can only nest one level deep. */ |
| 4478 | if (recurse_lvl >= 2) { |
| 4479 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4480 | return -1; |
| 4481 | } |
| 4482 | |
| 4483 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4484 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4485 | assert(**str == '{'); |
| 4486 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4487 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4488 | expr_start = *str; |
| 4489 | for (; *str < end; (*str)++) { |
| 4490 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4491 | |
| 4492 | /* Loop invariants. */ |
| 4493 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4494 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4495 | if (quote_char) |
| 4496 | assert(string_type == 1 || string_type == 3); |
| 4497 | else |
| 4498 | assert(string_type == 0); |
| 4499 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4500 | ch = **str; |
| 4501 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4502 | if (ch == '\\') { |
| 4503 | /* Error: can't include a backslash character, inside |
| 4504 | parens or strings or not. */ |
| 4505 | ast_error(c, n, "f-string expression part " |
| 4506 | "cannot include a backslash"); |
| 4507 | return -1; |
| 4508 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4509 | if (quote_char) { |
| 4510 | /* We're inside a string. See if we're at the end. */ |
| 4511 | /* This code needs to implement the same non-error logic |
| 4512 | as tok_get from tokenizer.c, at the letter_quote |
| 4513 | label. To actually share that code would be a |
| 4514 | nightmare. But, it's unlikely to change and is small, |
| 4515 | so duplicate it here. Note we don't need to catch all |
| 4516 | of the errors, since they'll be caught when parsing the |
| 4517 | expression. We just need to match the non-error |
| 4518 | cases. Thus we can ignore \n in single-quoted strings, |
| 4519 | for example. Or non-terminated strings. */ |
| 4520 | if (ch == quote_char) { |
| 4521 | /* Does this match the string_type (single or triple |
| 4522 | quoted)? */ |
| 4523 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4524 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4525 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4526 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4527 | string_type = 0; |
| 4528 | quote_char = 0; |
| 4529 | continue; |
| 4530 | } |
| 4531 | } else { |
| 4532 | /* We're at the end of a normal string. */ |
| 4533 | quote_char = 0; |
| 4534 | string_type = 0; |
| 4535 | continue; |
| 4536 | } |
| 4537 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4538 | } else if (ch == '\'' || ch == '"') { |
| 4539 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4540 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4541 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4542 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4543 | } else { |
| 4544 | /* Start of a normal string. */ |
| 4545 | string_type = 1; |
| 4546 | } |
| 4547 | /* Start looking for the end of the string. */ |
| 4548 | quote_char = ch; |
| 4549 | } else if (ch == '[' || ch == '{' || ch == '(') { |
| 4550 | nested_depth++; |
| 4551 | } else if (nested_depth != 0 && |
| 4552 | (ch == ']' || ch == '}' || ch == ')')) { |
| 4553 | nested_depth--; |
| 4554 | } else if (ch == '#') { |
| 4555 | /* Error: can't include a comment character, inside parens |
| 4556 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4557 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4558 | return -1; |
| 4559 | } else if (nested_depth == 0 && |
| 4560 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4561 | /* First, test for the special case of "!=". Since '=' is |
| 4562 | not an allowed conversion character, nothing is lost in |
| 4563 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4564 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4565 | /* This isn't a conversion character, just continue. */ |
| 4566 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4567 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4568 | /* Normal way out of this loop. */ |
| 4569 | break; |
| 4570 | } else { |
| 4571 | /* Just consume this char and loop around. */ |
| 4572 | } |
| 4573 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4574 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4575 | /* If we leave this loop in a string or with mismatched parens, we |
| 4576 | don't care. We'll get a syntax error when compiling the |
| 4577 | expression. But, we can produce a better error message, so |
| 4578 | let's just do that.*/ |
| 4579 | if (quote_char) { |
| 4580 | ast_error(c, n, "f-string: unterminated string"); |
| 4581 | return -1; |
| 4582 | } |
| 4583 | if (nested_depth) { |
| 4584 | ast_error(c, n, "f-string: mismatched '(', '{', or '['"); |
| 4585 | return -1; |
| 4586 | } |
| 4587 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4588 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4589 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4590 | |
| 4591 | /* Compile the expression as soon as possible, so we show errors |
| 4592 | related to the expression before errors related to the |
| 4593 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4594 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4595 | if (!simple_expression) |
| 4596 | return -1; |
| 4597 | |
| 4598 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4599 | if (**str == '!') { |
| 4600 | *str += 1; |
| 4601 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4602 | goto unexpected_end_of_string; |
| 4603 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4604 | conversion = **str; |
| 4605 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4606 | |
| 4607 | /* Validate the conversion. */ |
| 4608 | if (!(conversion == 's' || conversion == 'r' |
| 4609 | || conversion == 'a')) { |
| 4610 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4611 | "expected 's', 'r', or 'a'"); |
| 4612 | return -1; |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4617 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4618 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4619 | if (**str == ':') { |
| 4620 | *str += 1; |
| 4621 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4622 | goto unexpected_end_of_string; |
| 4623 | |
| 4624 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4625 | 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] | 4626 | if (!format_spec) |
| 4627 | return -1; |
| 4628 | } |
| 4629 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4630 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4631 | goto unexpected_end_of_string; |
| 4632 | |
| 4633 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4634 | assert(*str < end); |
| 4635 | assert(**str == '}'); |
| 4636 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4637 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4638 | /* And now create the FormattedValue node that represents this |
| 4639 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4640 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4641 | format_spec, LINENO(n), n->n_col_offset, |
| 4642 | c->c_arena); |
| 4643 | if (!*expression) |
| 4644 | return -1; |
| 4645 | |
| 4646 | return 0; |
| 4647 | |
| 4648 | unexpected_end_of_string: |
| 4649 | ast_error(c, n, "f-string: expecting '}'"); |
| 4650 | return -1; |
| 4651 | } |
| 4652 | |
| 4653 | /* Return -1 on error. |
| 4654 | |
| 4655 | Return 0 if we have a literal (possible zero length) and an |
| 4656 | expression (zero length if at the end of the string. |
| 4657 | |
| 4658 | Return 1 if we have a literal, but no expression, and we want the |
| 4659 | caller to call us again. This is used to deal with doubled |
| 4660 | braces. |
| 4661 | |
| 4662 | When called multiple times on the string 'a{{b{0}c', this function |
| 4663 | will return: |
| 4664 | |
| 4665 | 1. the literal 'a{' with no expression, and a return value |
| 4666 | of 1. Despite the fact that there's no expression, the return |
| 4667 | value of 1 means we're not finished yet. |
| 4668 | |
| 4669 | 2. the literal 'b' and the expression '0', with a return value of |
| 4670 | 0. The fact that there's an expression means we're not finished. |
| 4671 | |
| 4672 | 3. literal 'c' with no expression and a return value of 0. The |
| 4673 | combination of the return value of 0 with no expression means |
| 4674 | we're finished. |
| 4675 | */ |
| 4676 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4677 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4678 | int recurse_lvl, PyObject **literal, |
| 4679 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4680 | struct compiling *c, const node *n) |
| 4681 | { |
| 4682 | int result; |
| 4683 | |
| 4684 | assert(*literal == NULL && *expression == NULL); |
| 4685 | |
| 4686 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4687 | 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] | 4688 | if (result < 0) |
| 4689 | goto error; |
| 4690 | |
| 4691 | assert(result == 0 || result == 1); |
| 4692 | |
| 4693 | if (result == 1) |
| 4694 | /* We have a literal, but don't look at the expression. */ |
| 4695 | return 1; |
| 4696 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4697 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4698 | /* We're at the end of the string or the end of a nested |
| 4699 | f-string: no expression. The top-level error case where we |
| 4700 | expect to be at the end of the string but we're at a '}' is |
| 4701 | handled later. */ |
| 4702 | return 0; |
| 4703 | |
| 4704 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4705 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4706 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4707 | 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] | 4708 | goto error; |
| 4709 | |
| 4710 | return 0; |
| 4711 | |
| 4712 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4713 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4714 | return -1; |
| 4715 | } |
| 4716 | |
| 4717 | #define EXPRLIST_N_CACHED 64 |
| 4718 | |
| 4719 | typedef struct { |
| 4720 | /* Incrementally build an array of expr_ty, so be used in an |
| 4721 | asdl_seq. Cache some small but reasonably sized number of |
| 4722 | expr_ty's, and then after that start dynamically allocating, |
| 4723 | doubling the number allocated each time. Note that the f-string |
| 4724 | 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] | 4725 | 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] | 4726 | fast as you add exressions in an f-string. */ |
| 4727 | |
| 4728 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4729 | Py_ssize_t size; /* Number we've used. */ |
| 4730 | expr_ty *p; /* Pointer to the memory we're actually |
| 4731 | using. Will point to 'data' until we |
| 4732 | start dynamically allocating. */ |
| 4733 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4734 | } ExprList; |
| 4735 | |
| 4736 | #ifdef NDEBUG |
| 4737 | #define ExprList_check_invariants(l) |
| 4738 | #else |
| 4739 | static void |
| 4740 | ExprList_check_invariants(ExprList *l) |
| 4741 | { |
| 4742 | /* Check our invariants. Make sure this object is "live", and |
| 4743 | hasn't been deallocated. */ |
| 4744 | assert(l->size >= 0); |
| 4745 | assert(l->p != NULL); |
| 4746 | if (l->size <= EXPRLIST_N_CACHED) |
| 4747 | assert(l->data == l->p); |
| 4748 | } |
| 4749 | #endif |
| 4750 | |
| 4751 | static void |
| 4752 | ExprList_Init(ExprList *l) |
| 4753 | { |
| 4754 | l->allocated = EXPRLIST_N_CACHED; |
| 4755 | l->size = 0; |
| 4756 | |
| 4757 | /* Until we start allocating dynamically, p points to data. */ |
| 4758 | l->p = l->data; |
| 4759 | |
| 4760 | ExprList_check_invariants(l); |
| 4761 | } |
| 4762 | |
| 4763 | static int |
| 4764 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4765 | { |
| 4766 | ExprList_check_invariants(l); |
| 4767 | if (l->size >= l->allocated) { |
| 4768 | /* We need to alloc (or realloc) the memory. */ |
| 4769 | Py_ssize_t new_size = l->allocated * 2; |
| 4770 | |
| 4771 | /* See if we've ever allocated anything dynamically. */ |
| 4772 | if (l->p == l->data) { |
| 4773 | Py_ssize_t i; |
| 4774 | /* We're still using the cached data. Switch to |
| 4775 | alloc-ing. */ |
| 4776 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4777 | if (!l->p) |
| 4778 | return -1; |
| 4779 | /* Copy the cached data into the new buffer. */ |
| 4780 | for (i = 0; i < l->size; i++) |
| 4781 | l->p[i] = l->data[i]; |
| 4782 | } else { |
| 4783 | /* Just realloc. */ |
| 4784 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4785 | if (!tmp) { |
| 4786 | PyMem_RawFree(l->p); |
| 4787 | l->p = NULL; |
| 4788 | return -1; |
| 4789 | } |
| 4790 | l->p = tmp; |
| 4791 | } |
| 4792 | |
| 4793 | l->allocated = new_size; |
| 4794 | assert(l->allocated == 2 * l->size); |
| 4795 | } |
| 4796 | |
| 4797 | l->p[l->size++] = exp; |
| 4798 | |
| 4799 | ExprList_check_invariants(l); |
| 4800 | return 0; |
| 4801 | } |
| 4802 | |
| 4803 | static void |
| 4804 | ExprList_Dealloc(ExprList *l) |
| 4805 | { |
| 4806 | ExprList_check_invariants(l); |
| 4807 | |
| 4808 | /* If there's been an error, or we've never dynamically allocated, |
| 4809 | do nothing. */ |
| 4810 | if (!l->p || l->p == l->data) { |
| 4811 | /* Do nothing. */ |
| 4812 | } else { |
| 4813 | /* We have dynamically allocated. Free the memory. */ |
| 4814 | PyMem_RawFree(l->p); |
| 4815 | } |
| 4816 | l->p = NULL; |
| 4817 | l->size = -1; |
| 4818 | } |
| 4819 | |
| 4820 | static asdl_seq * |
| 4821 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4822 | { |
| 4823 | asdl_seq *seq; |
| 4824 | |
| 4825 | ExprList_check_invariants(l); |
| 4826 | |
| 4827 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4828 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4829 | if (seq) { |
| 4830 | Py_ssize_t i; |
| 4831 | for (i = 0; i < l->size; i++) |
| 4832 | asdl_seq_SET(seq, i, l->p[i]); |
| 4833 | } |
| 4834 | ExprList_Dealloc(l); |
| 4835 | return seq; |
| 4836 | } |
| 4837 | |
| 4838 | /* The FstringParser is designed to add a mix of strings and |
| 4839 | f-strings, and concat them together as needed. Ultimately, it |
| 4840 | generates an expr_ty. */ |
| 4841 | typedef struct { |
| 4842 | PyObject *last_str; |
| 4843 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4844 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4845 | } FstringParser; |
| 4846 | |
| 4847 | #ifdef NDEBUG |
| 4848 | #define FstringParser_check_invariants(state) |
| 4849 | #else |
| 4850 | static void |
| 4851 | FstringParser_check_invariants(FstringParser *state) |
| 4852 | { |
| 4853 | if (state->last_str) |
| 4854 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4855 | ExprList_check_invariants(&state->expr_list); |
| 4856 | } |
| 4857 | #endif |
| 4858 | |
| 4859 | static void |
| 4860 | FstringParser_Init(FstringParser *state) |
| 4861 | { |
| 4862 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4863 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4864 | ExprList_Init(&state->expr_list); |
| 4865 | FstringParser_check_invariants(state); |
| 4866 | } |
| 4867 | |
| 4868 | static void |
| 4869 | FstringParser_Dealloc(FstringParser *state) |
| 4870 | { |
| 4871 | FstringParser_check_invariants(state); |
| 4872 | |
| 4873 | Py_XDECREF(state->last_str); |
| 4874 | ExprList_Dealloc(&state->expr_list); |
| 4875 | } |
| 4876 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4877 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4878 | static expr_ty |
| 4879 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4880 | { |
| 4881 | PyObject *s = *str; |
| 4882 | *str = NULL; |
| 4883 | assert(PyUnicode_CheckExact(s)); |
| 4884 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4885 | Py_DECREF(s); |
| 4886 | return NULL; |
| 4887 | } |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4888 | 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] | 4889 | } |
| 4890 | |
| 4891 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4892 | decref'd. */ |
| 4893 | static int |
| 4894 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4895 | { |
| 4896 | FstringParser_check_invariants(state); |
| 4897 | |
| 4898 | assert(PyUnicode_CheckExact(str)); |
| 4899 | |
| 4900 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4901 | Py_DECREF(str); |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | if (!state->last_str) { |
| 4906 | /* We didn't have a string before, so just remember this one. */ |
| 4907 | state->last_str = str; |
| 4908 | } else { |
| 4909 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4910 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4911 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4912 | return -1; |
| 4913 | } |
| 4914 | FstringParser_check_invariants(state); |
| 4915 | return 0; |
| 4916 | } |
| 4917 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4918 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4919 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4920 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4921 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4922 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4923 | struct compiling *c, const node *n) |
| 4924 | { |
| 4925 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4926 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4927 | |
| 4928 | /* Parse the f-string. */ |
| 4929 | while (1) { |
| 4930 | PyObject *literal = NULL; |
| 4931 | expr_ty expression = NULL; |
| 4932 | |
| 4933 | /* If there's a zero length literal in front of the |
| 4934 | expression, literal will be NULL. If we're at the end of |
| 4935 | the f-string, expression will be NULL (unless result == 1, |
| 4936 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4937 | 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] | 4938 | &literal, &expression, |
| 4939 | c, n); |
| 4940 | if (result < 0) |
| 4941 | return -1; |
| 4942 | |
| 4943 | /* Add the literal, if any. */ |
| 4944 | if (!literal) { |
| 4945 | /* Do nothing. Just leave last_str alone (and possibly |
| 4946 | NULL). */ |
| 4947 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 4948 | /* Note that the literal can be zero length, if the |
| 4949 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4950 | state->last_str = literal; |
| 4951 | literal = NULL; |
| 4952 | } else { |
| 4953 | /* We have a literal, concatenate it. */ |
| 4954 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4955 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4956 | return -1; |
| 4957 | literal = NULL; |
| 4958 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4959 | |
| 4960 | /* We've dealt with the literal now. It can't be leaked on further |
| 4961 | errors. */ |
| 4962 | assert(literal == NULL); |
| 4963 | |
| 4964 | /* See if we should just loop around to get the next literal |
| 4965 | and expression, while ignoring the expression this |
| 4966 | time. This is used for un-doubling braces, as an |
| 4967 | optimization. */ |
| 4968 | if (result == 1) |
| 4969 | continue; |
| 4970 | |
| 4971 | if (!expression) |
| 4972 | /* We're done with this f-string. */ |
| 4973 | break; |
| 4974 | |
| 4975 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4976 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4977 | if (!state->last_str) { |
| 4978 | /* Do nothing. No previous literal. */ |
| 4979 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4980 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4981 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 4982 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 4983 | return -1; |
| 4984 | } |
| 4985 | |
| 4986 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 4987 | return -1; |
| 4988 | } |
| 4989 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4990 | /* If recurse_lvl is zero, then we must be at the end of the |
| 4991 | string. Otherwise, we must be at a right brace. */ |
| 4992 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4993 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4994 | ast_error(c, n, "f-string: unexpected end of string"); |
| 4995 | return -1; |
| 4996 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4997 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4998 | ast_error(c, n, "f-string: expecting '}'"); |
| 4999 | return -1; |
| 5000 | } |
| 5001 | |
| 5002 | FstringParser_check_invariants(state); |
| 5003 | return 0; |
| 5004 | } |
| 5005 | |
| 5006 | /* 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] | 5007 | 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] | 5008 | static expr_ty |
| 5009 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5010 | const node *n) |
| 5011 | { |
| 5012 | asdl_seq *seq; |
| 5013 | |
| 5014 | FstringParser_check_invariants(state); |
| 5015 | |
| 5016 | /* If we're just a constant string with no expressions, return |
| 5017 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5018 | if (!state->fmode) { |
| 5019 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5020 | if (!state->last_str) { |
| 5021 | /* Create a zero length string. */ |
| 5022 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5023 | if (!state->last_str) |
| 5024 | goto error; |
| 5025 | } |
| 5026 | return make_str_node_and_del(&state->last_str, c, n); |
| 5027 | } |
| 5028 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5029 | /* 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] | 5030 | last node in our expression list. */ |
| 5031 | if (state->last_str) { |
| 5032 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5033 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5034 | goto error; |
| 5035 | } |
| 5036 | /* This has already been freed. */ |
| 5037 | assert(state->last_str == NULL); |
| 5038 | |
| 5039 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5040 | if (!seq) |
| 5041 | goto error; |
| 5042 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5043 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5044 | |
| 5045 | error: |
| 5046 | FstringParser_Dealloc(state); |
| 5047 | return NULL; |
| 5048 | } |
| 5049 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5050 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5051 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5052 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5053 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5054 | 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] | 5055 | struct compiling *c, const node *n) |
| 5056 | { |
| 5057 | FstringParser state; |
| 5058 | |
| 5059 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5060 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5061 | c, n) < 0) { |
| 5062 | FstringParser_Dealloc(&state); |
| 5063 | return NULL; |
| 5064 | } |
| 5065 | |
| 5066 | return FstringParser_Finish(&state, c, n); |
| 5067 | } |
| 5068 | |
| 5069 | /* n is a Python string literal, including the bracketing quote |
| 5070 | 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] | 5071 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5072 | 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] | 5073 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5074 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5075 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5076 | static int |
| 5077 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5078 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5079 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5080 | size_t len; |
| 5081 | const char *s = STR(n); |
| 5082 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5083 | int fmode = 0; |
| 5084 | *bytesmode = 0; |
| 5085 | *rawmode = 0; |
| 5086 | *result = NULL; |
| 5087 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5088 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5089 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5090 | if (quote == 'b' || quote == 'B') { |
| 5091 | quote = *++s; |
| 5092 | *bytesmode = 1; |
| 5093 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5094 | else if (quote == 'u' || quote == 'U') { |
| 5095 | quote = *++s; |
| 5096 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5097 | else if (quote == 'r' || quote == 'R') { |
| 5098 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5099 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5100 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5101 | else if (quote == 'f' || quote == 'F') { |
| 5102 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5103 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5104 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5105 | else { |
| 5106 | break; |
| 5107 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5108 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5109 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5110 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5111 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5112 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5113 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5114 | if (quote != '\'' && quote != '\"') { |
| 5115 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5116 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5117 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5118 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5119 | s++; |
| 5120 | len = strlen(s); |
| 5121 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5123 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5124 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5125 | } |
| 5126 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5127 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5128 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5129 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5130 | } |
| 5131 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5132 | /* A triple quoted string. We've already skipped one quote at |
| 5133 | the start and one at the end of the string. Now skip the |
| 5134 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5135 | s += 2; |
| 5136 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5137 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5138 | if (s[--len] != quote || s[--len] != quote) { |
| 5139 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5140 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5141 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5142 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5143 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5144 | if (fmode) { |
| 5145 | /* Just return the bytes. The caller will parse the resulting |
| 5146 | string. */ |
| 5147 | *fstr = s; |
| 5148 | *fstrlen = len; |
| 5149 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5150 | } |
| 5151 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5152 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5153 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5154 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5155 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5156 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5157 | const char *ch; |
| 5158 | for (ch = s; *ch; ch++) { |
| 5159 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5160 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5161 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5162 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5163 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5164 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5165 | if (*rawmode) |
| 5166 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5167 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5168 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5169 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5170 | if (*rawmode) |
| 5171 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5172 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5173 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5174 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5175 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5178 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5179 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5180 | 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] | 5181 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5182 | 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] | 5183 | node if there's just an f-string (with no leading or trailing |
| 5184 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5185 | any literals involved. */ |
| 5186 | static expr_ty |
| 5187 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5188 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5189 | int bytesmode = 0; |
| 5190 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5191 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5192 | |
| 5193 | FstringParser state; |
| 5194 | FstringParser_Init(&state); |
| 5195 | |
| 5196 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5197 | int this_bytesmode; |
| 5198 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5199 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5200 | const char *fstr; |
| 5201 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5202 | |
| 5203 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5204 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5205 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5206 | goto error; |
| 5207 | |
| 5208 | /* Check that we're not mixing bytes with unicode. */ |
| 5209 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5210 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5211 | /* s is NULL if the current string part is an f-string. */ |
| 5212 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5213 | goto error; |
| 5214 | } |
| 5215 | bytesmode = this_bytesmode; |
| 5216 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5217 | if (fstr != NULL) { |
| 5218 | int result; |
| 5219 | assert(s == NULL && !bytesmode); |
| 5220 | /* This is an f-string. Parse and concatenate it. */ |
| 5221 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5222 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5223 | if (result < 0) |
| 5224 | goto error; |
| 5225 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5226 | /* A string or byte string. */ |
| 5227 | assert(s != NULL && fstr == NULL); |
| 5228 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5230 | PyUnicode_CheckExact(s)); |
| 5231 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5232 | if (bytesmode) { |
| 5233 | /* For bytes, concat as we go. */ |
| 5234 | if (i == 0) { |
| 5235 | /* First time, just remember this value. */ |
| 5236 | bytes_str = s; |
| 5237 | } else { |
| 5238 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5239 | if (!bytes_str) |
| 5240 | goto error; |
| 5241 | } |
| 5242 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5243 | /* This is a regular string. Concatenate it. */ |
| 5244 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5245 | goto error; |
| 5246 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5247 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5248 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5249 | if (bytesmode) { |
| 5250 | /* Just return the bytes object and we're done. */ |
| 5251 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5252 | goto error; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5253 | 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] | 5254 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5255 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5256 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5257 | assert(bytes_str == NULL); |
| 5258 | |
| 5259 | return FstringParser_Finish(&state, c, n); |
| 5260 | |
| 5261 | error: |
| 5262 | Py_XDECREF(bytes_str); |
| 5263 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5264 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5265 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5266 | |
| 5267 | PyObject * |
| 5268 | _PyAST_GetDocString(asdl_seq *body) |
| 5269 | { |
| 5270 | if (!asdl_seq_LEN(body)) { |
| 5271 | return NULL; |
| 5272 | } |
| 5273 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5274 | if (st->kind != Expr_kind) { |
| 5275 | return NULL; |
| 5276 | } |
| 5277 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5278 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5279 | return e->v.Constant.value; |
| 5280 | } |
| 5281 | return NULL; |
| 5282 | } |