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 | |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 16 | #define MAXLEVEL 200 /* Max parentheses level */ |
| 17 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 18 | static int validate_stmts(asdl_seq *); |
| 19 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 20 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 21 | static int validate_stmt(stmt_ty); |
| 22 | static int validate_expr(expr_ty, expr_context_ty); |
| 23 | |
| 24 | static int |
| 25 | validate_comprehension(asdl_seq *gens) |
| 26 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 27 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 28 | if (!asdl_seq_LEN(gens)) { |
| 29 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 30 | return 0; |
| 31 | } |
| 32 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 33 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 34 | if (!validate_expr(comp->target, Store) || |
| 35 | !validate_expr(comp->iter, Load) || |
| 36 | !validate_exprs(comp->ifs, Load, 0)) |
| 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | validate_slice(slice_ty slice) |
| 44 | { |
| 45 | switch (slice->kind) { |
| 46 | case Slice_kind: |
| 47 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 48 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 49 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 50 | case ExtSlice_kind: { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 51 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 52 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 53 | return 0; |
| 54 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 55 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 56 | return 0; |
| 57 | return 1; |
| 58 | } |
| 59 | case Index_kind: |
| 60 | return validate_expr(slice->v.Index.value, Load); |
| 61 | default: |
| 62 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | validate_keywords(asdl_seq *keywords) |
| 69 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 70 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 71 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 72 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 73 | return 0; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | validate_args(asdl_seq *args) |
| 79 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 80 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 81 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 82 | arg_ty arg = asdl_seq_GET(args, i); |
| 83 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 84 | return 0; |
| 85 | } |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static const char * |
| 90 | expr_context_name(expr_context_ty ctx) |
| 91 | { |
| 92 | switch (ctx) { |
| 93 | case Load: |
| 94 | return "Load"; |
| 95 | case Store: |
| 96 | return "Store"; |
| 97 | case Del: |
| 98 | return "Del"; |
| 99 | case AugLoad: |
| 100 | return "AugLoad"; |
| 101 | case AugStore: |
| 102 | return "AugStore"; |
| 103 | case Param: |
| 104 | return "Param"; |
| 105 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 106 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | validate_arguments(arguments_ty args) |
| 112 | { |
| 113 | if (!validate_args(args->args)) |
| 114 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 115 | if (args->vararg && args->vararg->annotation |
| 116 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | if (!validate_args(args->kwonlyargs)) |
| 120 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 121 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 122 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 126 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 127 | return 0; |
| 128 | } |
| 129 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 130 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 131 | "kw_defaults on arguments"); |
| 132 | return 0; |
| 133 | } |
| 134 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 135 | } |
| 136 | |
| 137 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 138 | validate_constant(PyObject *value) |
| 139 | { |
| 140 | if (value == Py_None || value == Py_Ellipsis) |
| 141 | return 1; |
| 142 | |
| 143 | if (PyLong_CheckExact(value) |
| 144 | || PyFloat_CheckExact(value) |
| 145 | || PyComplex_CheckExact(value) |
| 146 | || PyBool_Check(value) |
| 147 | || PyUnicode_CheckExact(value) |
| 148 | || PyBytes_CheckExact(value)) |
| 149 | return 1; |
| 150 | |
| 151 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 152 | PyObject *it; |
| 153 | |
| 154 | it = PyObject_GetIter(value); |
| 155 | if (it == NULL) |
| 156 | return 0; |
| 157 | |
| 158 | while (1) { |
| 159 | PyObject *item = PyIter_Next(it); |
| 160 | if (item == NULL) { |
| 161 | if (PyErr_Occurred()) { |
| 162 | Py_DECREF(it); |
| 163 | return 0; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (!validate_constant(item)) { |
| 169 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 170 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 173 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | Py_DECREF(it); |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 184 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 185 | { |
| 186 | int check_ctx = 1; |
| 187 | expr_context_ty actual_ctx; |
| 188 | |
| 189 | /* First check expression context. */ |
| 190 | switch (exp->kind) { |
| 191 | case Attribute_kind: |
| 192 | actual_ctx = exp->v.Attribute.ctx; |
| 193 | break; |
| 194 | case Subscript_kind: |
| 195 | actual_ctx = exp->v.Subscript.ctx; |
| 196 | break; |
| 197 | case Starred_kind: |
| 198 | actual_ctx = exp->v.Starred.ctx; |
| 199 | break; |
| 200 | case Name_kind: |
| 201 | actual_ctx = exp->v.Name.ctx; |
| 202 | break; |
| 203 | case List_kind: |
| 204 | actual_ctx = exp->v.List.ctx; |
| 205 | break; |
| 206 | case Tuple_kind: |
| 207 | actual_ctx = exp->v.Tuple.ctx; |
| 208 | break; |
| 209 | default: |
| 210 | if (ctx != Load) { |
| 211 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 212 | "assigned to in %s context", expr_context_name(ctx)); |
| 213 | return 0; |
| 214 | } |
| 215 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 216 | /* set actual_ctx to prevent gcc warning */ |
| 217 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 218 | } |
| 219 | if (check_ctx && actual_ctx != ctx) { |
| 220 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 221 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* Now validate expression. */ |
| 226 | switch (exp->kind) { |
| 227 | case BoolOp_kind: |
| 228 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 229 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 230 | return 0; |
| 231 | } |
| 232 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 233 | case BinOp_kind: |
| 234 | return validate_expr(exp->v.BinOp.left, Load) && |
| 235 | validate_expr(exp->v.BinOp.right, Load); |
| 236 | case UnaryOp_kind: |
| 237 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 238 | case Lambda_kind: |
| 239 | return validate_arguments(exp->v.Lambda.args) && |
| 240 | validate_expr(exp->v.Lambda.body, Load); |
| 241 | case IfExp_kind: |
| 242 | return validate_expr(exp->v.IfExp.test, Load) && |
| 243 | validate_expr(exp->v.IfExp.body, Load) && |
| 244 | validate_expr(exp->v.IfExp.orelse, Load); |
| 245 | case Dict_kind: |
| 246 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 247 | PyErr_SetString(PyExc_ValueError, |
| 248 | "Dict doesn't have the same number of keys as values"); |
| 249 | return 0; |
| 250 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 251 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 252 | dict literals, i.e. ``{**{a:b}}`` */ |
| 253 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 254 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 255 | case Set_kind: |
| 256 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 257 | #define COMP(NAME) \ |
| 258 | case NAME ## _kind: \ |
| 259 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 260 | validate_expr(exp->v.NAME.elt, Load); |
| 261 | COMP(ListComp) |
| 262 | COMP(SetComp) |
| 263 | COMP(GeneratorExp) |
| 264 | #undef COMP |
| 265 | case DictComp_kind: |
| 266 | return validate_comprehension(exp->v.DictComp.generators) && |
| 267 | validate_expr(exp->v.DictComp.key, Load) && |
| 268 | validate_expr(exp->v.DictComp.value, Load); |
| 269 | case Yield_kind: |
| 270 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 271 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 272 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 273 | case Await_kind: |
| 274 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 275 | case Compare_kind: |
| 276 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 277 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 278 | return 0; |
| 279 | } |
| 280 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 281 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 282 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 283 | "of comparators and operands"); |
| 284 | return 0; |
| 285 | } |
| 286 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 287 | validate_expr(exp->v.Compare.left, Load); |
| 288 | case Call_kind: |
| 289 | return validate_expr(exp->v.Call.func, Load) && |
| 290 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 291 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 292 | case Constant_kind: |
| 293 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 294 | PyErr_Format(PyExc_TypeError, |
| 295 | "got an invalid type in Constant: %s", |
| 296 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 300 | case JoinedStr_kind: |
| 301 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 302 | case FormattedValue_kind: |
| 303 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 304 | return 0; |
| 305 | if (exp->v.FormattedValue.format_spec) |
| 306 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 307 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 308 | case Attribute_kind: |
| 309 | return validate_expr(exp->v.Attribute.value, Load); |
| 310 | case Subscript_kind: |
| 311 | return validate_slice(exp->v.Subscript.slice) && |
| 312 | validate_expr(exp->v.Subscript.value, Load); |
| 313 | case Starred_kind: |
| 314 | return validate_expr(exp->v.Starred.value, ctx); |
| 315 | case List_kind: |
| 316 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 317 | case Tuple_kind: |
| 318 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 319 | case NamedExpr_kind: |
| 320 | return validate_expr(exp->v.NamedExpr.value, Load); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 321 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 322 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 323 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 324 | } |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 325 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 326 | return 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static int |
| 330 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 331 | { |
| 332 | if (asdl_seq_LEN(seq)) |
| 333 | return 1; |
| 334 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int |
| 339 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 340 | { |
| 341 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 342 | validate_exprs(targets, ctx, 0); |
| 343 | } |
| 344 | |
| 345 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 346 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 347 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 348 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int |
| 352 | validate_stmt(stmt_ty stmt) |
| 353 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 354 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 355 | switch (stmt->kind) { |
| 356 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 357 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 358 | validate_arguments(stmt->v.FunctionDef.args) && |
| 359 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 360 | (!stmt->v.FunctionDef.returns || |
| 361 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 362 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 363 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 364 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 365 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 366 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 367 | case Return_kind: |
| 368 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 369 | case Delete_kind: |
| 370 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 371 | case Assign_kind: |
| 372 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 373 | validate_expr(stmt->v.Assign.value, Load); |
| 374 | case AugAssign_kind: |
| 375 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 376 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 377 | case AnnAssign_kind: |
| 378 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 379 | stmt->v.AnnAssign.simple) { |
| 380 | PyErr_SetString(PyExc_TypeError, |
| 381 | "AnnAssign with simple non-Name target"); |
| 382 | return 0; |
| 383 | } |
| 384 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 385 | (!stmt->v.AnnAssign.value || |
| 386 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 387 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 388 | case For_kind: |
| 389 | return validate_expr(stmt->v.For.target, Store) && |
| 390 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 391 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 392 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 393 | case AsyncFor_kind: |
| 394 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 395 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 396 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 397 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 398 | case While_kind: |
| 399 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 400 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 401 | validate_stmts(stmt->v.While.orelse); |
| 402 | case If_kind: |
| 403 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 404 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 405 | validate_stmts(stmt->v.If.orelse); |
| 406 | case With_kind: |
| 407 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 408 | return 0; |
| 409 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 410 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 411 | if (!validate_expr(item->context_expr, Load) || |
| 412 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 413 | return 0; |
| 414 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 415 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 416 | case AsyncWith_kind: |
| 417 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 418 | return 0; |
| 419 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 420 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 421 | if (!validate_expr(item->context_expr, Load) || |
| 422 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 423 | return 0; |
| 424 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 425 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 426 | case Raise_kind: |
| 427 | if (stmt->v.Raise.exc) { |
| 428 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 429 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 430 | } |
| 431 | if (stmt->v.Raise.cause) { |
| 432 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 433 | return 0; |
| 434 | } |
| 435 | return 1; |
| 436 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 437 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 438 | return 0; |
| 439 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 440 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 441 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 442 | return 0; |
| 443 | } |
| 444 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 445 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 446 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 447 | return 0; |
| 448 | } |
| 449 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 450 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 451 | if ((handler->v.ExceptHandler.type && |
| 452 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 453 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 457 | validate_stmts(stmt->v.Try.finalbody)) && |
| 458 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 459 | validate_stmts(stmt->v.Try.orelse)); |
| 460 | case Assert_kind: |
| 461 | return validate_expr(stmt->v.Assert.test, Load) && |
| 462 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 463 | case Import_kind: |
| 464 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 465 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 466 | if (stmt->v.ImportFrom.level < 0) { |
| 467 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 471 | case Global_kind: |
| 472 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 473 | case Nonlocal_kind: |
| 474 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 475 | case Expr_kind: |
| 476 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 477 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 478 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 479 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 480 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 481 | (!stmt->v.AsyncFunctionDef.returns || |
| 482 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 483 | case Pass_kind: |
| 484 | case Break_kind: |
| 485 | case Continue_kind: |
| 486 | return 1; |
| 487 | default: |
| 488 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 489 | return 0; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static int |
| 494 | validate_stmts(asdl_seq *seq) |
| 495 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 496 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 497 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 498 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 499 | if (stmt) { |
| 500 | if (!validate_stmt(stmt)) |
| 501 | return 0; |
| 502 | } |
| 503 | else { |
| 504 | PyErr_SetString(PyExc_ValueError, |
| 505 | "None disallowed in statement list"); |
| 506 | return 0; |
| 507 | } |
| 508 | } |
| 509 | return 1; |
| 510 | } |
| 511 | |
| 512 | static int |
| 513 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 514 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 515 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 516 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 517 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 518 | if (expr) { |
| 519 | if (!validate_expr(expr, ctx)) |
| 520 | return 0; |
| 521 | } |
| 522 | else if (!null_ok) { |
| 523 | PyErr_SetString(PyExc_ValueError, |
| 524 | "None disallowed in expression list"); |
| 525 | return 0; |
| 526 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 527 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 528 | } |
| 529 | return 1; |
| 530 | } |
| 531 | |
| 532 | int |
| 533 | PyAST_Validate(mod_ty mod) |
| 534 | { |
| 535 | int res = 0; |
| 536 | |
| 537 | switch (mod->kind) { |
| 538 | case Module_kind: |
| 539 | res = validate_stmts(mod->v.Module.body); |
| 540 | break; |
| 541 | case Interactive_kind: |
| 542 | res = validate_stmts(mod->v.Interactive.body); |
| 543 | break; |
| 544 | case Expression_kind: |
| 545 | res = validate_expr(mod->v.Expression.body, Load); |
| 546 | break; |
| 547 | case Suite_kind: |
| 548 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 549 | break; |
| 550 | default: |
| 551 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 552 | res = 0; |
| 553 | break; |
| 554 | } |
| 555 | return res; |
| 556 | } |
| 557 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 558 | /* 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] | 559 | #include "grammar.h" |
| 560 | #include "parsetok.h" |
| 561 | #include "graminit.h" |
| 562 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | /* Data structure used internally */ |
| 564 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 565 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 566 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 567 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 568 | int c_feature_version; /* Latest minor version of Python for allowed features */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 572 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 573 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 574 | 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] | 575 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 576 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 577 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 578 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 579 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 580 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 581 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 582 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 583 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 584 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 585 | const node *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 586 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 587 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 588 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 589 | static void get_last_end_pos(asdl_seq *, int *, int *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 590 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 591 | #define COMP_GENEXP 0 |
| 592 | #define COMP_LISTCOMP 1 |
| 593 | #define COMP_SETCOMP 2 |
| 594 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 595 | static int |
| 596 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 597 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 598 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 599 | if (!m) |
| 600 | return 0; |
| 601 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 602 | Py_DECREF(m); |
| 603 | if (!c->c_normalize) |
| 604 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 609 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 610 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 611 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 612 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 613 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 614 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 615 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 616 | /* Check whether there are non-ASCII characters in the |
| 617 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 618 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 619 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 620 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 621 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 622 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 623 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 624 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 625 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 626 | if (form == NULL) { |
| 627 | Py_DECREF(id); |
| 628 | return NULL; |
| 629 | } |
| 630 | PyObject *args[2] = {form, id}; |
| 631 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 632 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 633 | if (!id2) |
| 634 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 635 | if (!PyUnicode_Check(id2)) { |
| 636 | PyErr_Format(PyExc_TypeError, |
| 637 | "unicodedata.normalize() must return a string, not " |
| 638 | "%.200s", |
| 639 | Py_TYPE(id2)->tp_name); |
| 640 | Py_DECREF(id2); |
| 641 | return NULL; |
| 642 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 643 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 644 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 645 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 646 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 647 | Py_DECREF(id); |
| 648 | return NULL; |
| 649 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 650 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 653 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 655 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 656 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 657 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 658 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 659 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 661 | va_start(va, errmsg); |
| 662 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 663 | va_end(va); |
| 664 | if (!errstr) { |
| 665 | return 0; |
| 666 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 667 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 669 | Py_INCREF(Py_None); |
| 670 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 671 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 672 | tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 673 | if (!tmp) { |
| 674 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 675 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 676 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 677 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 678 | Py_DECREF(errstr); |
| 679 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 680 | if (value) { |
| 681 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 682 | Py_DECREF(value); |
| 683 | } |
| 684 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* num_stmts() returns number of contained statements. |
| 688 | |
| 689 | Use this routine to determine how big a sequence is needed for |
| 690 | the statements in a parse tree. Its raison d'etre is this bit of |
| 691 | grammar: |
| 692 | |
| 693 | stmt: simple_stmt | compound_stmt |
| 694 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 695 | |
| 696 | A simple_stmt can contain multiple small_stmt elements joined |
| 697 | by semicolons. If the arg is a simple_stmt, the number of |
| 698 | small_stmt elements is returned. |
| 699 | */ |
| 700 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 701 | static string |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 702 | new_type_comment(const char *s, struct compiling *c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 703 | { |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 704 | PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); |
Guido van Rossum | 4b250fc | 2019-02-11 08:10:42 -0800 | [diff] [blame] | 705 | if (res == NULL) |
| 706 | return NULL; |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 707 | if (PyArena_AddPyObject(c->c_arena, res) < 0) { |
| 708 | Py_DECREF(res); |
| 709 | return NULL; |
| 710 | } |
| 711 | return res; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 712 | } |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 713 | #define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 714 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 715 | static int |
| 716 | num_stmts(const node *n) |
| 717 | { |
| 718 | int i, l; |
| 719 | node *ch; |
| 720 | |
| 721 | switch (TYPE(n)) { |
| 722 | case single_input: |
| 723 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 724 | return 0; |
| 725 | else |
| 726 | return num_stmts(CHILD(n, 0)); |
| 727 | case file_input: |
| 728 | l = 0; |
| 729 | for (i = 0; i < NCH(n); i++) { |
| 730 | ch = CHILD(n, i); |
| 731 | if (TYPE(ch) == stmt) |
| 732 | l += num_stmts(ch); |
| 733 | } |
| 734 | return l; |
| 735 | case stmt: |
| 736 | return num_stmts(CHILD(n, 0)); |
| 737 | case compound_stmt: |
| 738 | return 1; |
| 739 | case simple_stmt: |
| 740 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 741 | case suite: |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 742 | case func_body_suite: |
| 743 | /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
| 744 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 745 | if (NCH(n) == 1) |
| 746 | return num_stmts(CHILD(n, 0)); |
| 747 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 748 | i = 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 749 | l = 0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 750 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) |
| 751 | i += 2; |
| 752 | for (; i < (NCH(n) - 1); i++) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 753 | l += num_stmts(CHILD(n, i)); |
| 754 | return l; |
| 755 | } |
| 756 | default: { |
| 757 | char buf[128]; |
| 758 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 759 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 760 | TYPE(n), NCH(n)); |
| 761 | Py_FatalError(buf); |
| 762 | } |
| 763 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 764 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* Transform the CST rooted at node * to the appropriate AST |
| 768 | */ |
| 769 | |
| 770 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 771 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 772 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 774 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 775 | asdl_seq *stmts = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 776 | asdl_seq *type_ignores = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 777 | stmt_ty s; |
| 778 | node *ch; |
| 779 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 780 | mod_ty res = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 781 | asdl_seq *argtypes = NULL; |
| 782 | expr_ty ret, arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 784 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 785 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 786 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 787 | c.c_normalize = NULL; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 788 | c.c_feature_version = flags->cf_feature_version; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 789 | |
| 790 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 793 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | switch (TYPE(n)) { |
| 795 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 796 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 797 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 798 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | for (i = 0; i < NCH(n) - 1; i++) { |
| 800 | ch = CHILD(n, i); |
| 801 | if (TYPE(ch) == NEWLINE) |
| 802 | continue; |
| 803 | REQ(ch, stmt); |
| 804 | num = num_stmts(ch); |
| 805 | if (num == 1) { |
| 806 | s = ast_for_stmt(&c, ch); |
| 807 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 808 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 809 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 810 | } |
| 811 | else { |
| 812 | ch = CHILD(ch, 0); |
| 813 | REQ(ch, simple_stmt); |
| 814 | for (j = 0; j < num; j++) { |
| 815 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 816 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 817 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 818 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 822 | |
| 823 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 824 | ch = CHILD(n, NCH(n) - 1); |
| 825 | REQ(ch, ENDMARKER); |
| 826 | num = NCH(ch); |
| 827 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 828 | if (!type_ignores) |
| 829 | goto out; |
| 830 | |
| 831 | for (i = 0; i < num; i++) { |
| 832 | type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena); |
| 833 | if (!ti) |
| 834 | goto out; |
| 835 | asdl_seq_SET(type_ignores, i, ti); |
| 836 | } |
| 837 | |
| 838 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 839 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 840 | case eval_input: { |
| 841 | expr_ty testlist_ast; |
| 842 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 843 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 844 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 845 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 846 | goto out; |
| 847 | res = Expression(testlist_ast, arena); |
| 848 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | } |
| 850 | case single_input: |
| 851 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 852 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 853 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 854 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 855 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 856 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 857 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 858 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 859 | goto out; |
| 860 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | } |
| 862 | else { |
| 863 | n = CHILD(n, 0); |
| 864 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 865 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 867 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 869 | s = ast_for_stmt(&c, n); |
| 870 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 871 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 872 | asdl_seq_SET(stmts, 0, s); |
| 873 | } |
| 874 | else { |
| 875 | /* Only a simple_stmt can contain multiple statements. */ |
| 876 | REQ(n, simple_stmt); |
| 877 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 878 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 879 | break; |
| 880 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 881 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 882 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 883 | asdl_seq_SET(stmts, i / 2, s); |
| 884 | } |
| 885 | } |
| 886 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 887 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 888 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 889 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 890 | case func_type_input: |
| 891 | n = CHILD(n, 0); |
| 892 | REQ(n, func_type); |
| 893 | |
| 894 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 895 | ch = CHILD(n, 1); |
| 896 | /* this is overly permissive -- we don't pay any attention to |
| 897 | * stars on the args -- just parse them into an ordered list */ |
| 898 | num = 0; |
| 899 | for (i = 0; i < NCH(ch); i++) { |
| 900 | if (TYPE(CHILD(ch, i)) == test) { |
| 901 | num++; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | argtypes = _Py_asdl_seq_new(num, arena); |
| 906 | if (!argtypes) |
| 907 | goto out; |
| 908 | |
| 909 | j = 0; |
| 910 | for (i = 0; i < NCH(ch); i++) { |
| 911 | if (TYPE(CHILD(ch, i)) == test) { |
| 912 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 913 | if (!arg) |
| 914 | goto out; |
| 915 | asdl_seq_SET(argtypes, j++, arg); |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | else { |
| 920 | argtypes = _Py_asdl_seq_new(0, arena); |
| 921 | if (!argtypes) |
| 922 | goto out; |
| 923 | } |
| 924 | |
| 925 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 926 | if (!ret) |
| 927 | goto out; |
| 928 | res = FunctionType(argtypes, ret, arena); |
| 929 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 930 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 931 | PyErr_Format(PyExc_SystemError, |
| 932 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 933 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 934 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 935 | out: |
| 936 | if (c.c_normalize) { |
| 937 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 938 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 939 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 942 | mod_ty |
| 943 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 944 | PyArena *arena) |
| 945 | { |
| 946 | mod_ty mod; |
| 947 | PyObject *filename; |
| 948 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 949 | if (filename == NULL) |
| 950 | return NULL; |
| 951 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 952 | Py_DECREF(filename); |
| 953 | return mod; |
| 954 | |
| 955 | } |
| 956 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 957 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 958 | */ |
| 959 | |
| 960 | static operator_ty |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 961 | get_operator(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 962 | { |
| 963 | switch (TYPE(n)) { |
| 964 | case VBAR: |
| 965 | return BitOr; |
| 966 | case CIRCUMFLEX: |
| 967 | return BitXor; |
| 968 | case AMPER: |
| 969 | return BitAnd; |
| 970 | case LEFTSHIFT: |
| 971 | return LShift; |
| 972 | case RIGHTSHIFT: |
| 973 | return RShift; |
| 974 | case PLUS: |
| 975 | return Add; |
| 976 | case MINUS: |
| 977 | return Sub; |
| 978 | case STAR: |
| 979 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 980 | case AT: |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 981 | if (c->c_feature_version < 5) { |
| 982 | ast_error(c, n, |
| 983 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 984 | return (operator_ty)0; |
| 985 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 986 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 987 | case SLASH: |
| 988 | return Div; |
| 989 | case DOUBLESLASH: |
| 990 | return FloorDiv; |
| 991 | case PERCENT: |
| 992 | return Mod; |
| 993 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 994 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 998 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 999 | "None", |
| 1000 | "True", |
| 1001 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1002 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1003 | NULL, |
| 1004 | }; |
| 1005 | |
| 1006 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1007 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1008 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1009 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1010 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1011 | const char * const *p = FORBIDDEN; |
| 1012 | if (!full_checks) { |
| 1013 | /* In most cases, the parser will protect True, False, and None |
| 1014 | from being assign to. */ |
| 1015 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1016 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1017 | for (; *p; p++) { |
| 1018 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1019 | ast_error(c, n, "cannot assign to %U", name); |
| 1020 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1026 | static expr_ty |
| 1027 | copy_location(expr_ty e, const node *n) |
| 1028 | { |
| 1029 | if (e) { |
| 1030 | e->lineno = LINENO(n); |
| 1031 | e->col_offset = n->n_col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1032 | e->end_lineno = n->n_end_lineno; |
| 1033 | e->end_col_offset = n->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1034 | } |
| 1035 | return e; |
| 1036 | } |
| 1037 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1038 | static const char * |
| 1039 | get_expr_name(expr_ty e) |
| 1040 | { |
| 1041 | switch (e->kind) { |
| 1042 | case Attribute_kind: |
| 1043 | return "attribute"; |
| 1044 | case Subscript_kind: |
| 1045 | return "subscript"; |
| 1046 | case Starred_kind: |
| 1047 | return "starred"; |
| 1048 | case Name_kind: |
| 1049 | return "name"; |
| 1050 | case List_kind: |
| 1051 | return "list"; |
| 1052 | case Tuple_kind: |
| 1053 | return "tuple"; |
| 1054 | case Lambda_kind: |
| 1055 | return "lambda"; |
| 1056 | case Call_kind: |
| 1057 | return "function call"; |
| 1058 | case BoolOp_kind: |
| 1059 | case BinOp_kind: |
| 1060 | case UnaryOp_kind: |
| 1061 | return "operator"; |
| 1062 | case GeneratorExp_kind: |
| 1063 | return "generator expression"; |
| 1064 | case Yield_kind: |
| 1065 | case YieldFrom_kind: |
| 1066 | return "yield expression"; |
| 1067 | case Await_kind: |
| 1068 | return "await expression"; |
| 1069 | case ListComp_kind: |
| 1070 | return "list comprehension"; |
| 1071 | case SetComp_kind: |
| 1072 | return "set comprehension"; |
| 1073 | case DictComp_kind: |
| 1074 | return "dict comprehension"; |
| 1075 | case Dict_kind: |
| 1076 | return "dict display"; |
| 1077 | case Set_kind: |
| 1078 | return "set display"; |
| 1079 | case JoinedStr_kind: |
| 1080 | case FormattedValue_kind: |
| 1081 | return "f-string expression"; |
| 1082 | case Constant_kind: { |
| 1083 | PyObject *value = e->v.Constant.value; |
| 1084 | if (value == Py_None) { |
| 1085 | return "None"; |
| 1086 | } |
| 1087 | if (value == Py_False) { |
| 1088 | return "False"; |
| 1089 | } |
| 1090 | if (value == Py_True) { |
| 1091 | return "True"; |
| 1092 | } |
| 1093 | if (value == Py_Ellipsis) { |
| 1094 | return "Ellipsis"; |
| 1095 | } |
| 1096 | return "literal"; |
| 1097 | } |
| 1098 | case Compare_kind: |
| 1099 | return "comparison"; |
| 1100 | case IfExp_kind: |
| 1101 | return "conditional expression"; |
| 1102 | case NamedExpr_kind: |
| 1103 | return "named expression"; |
| 1104 | default: |
| 1105 | PyErr_Format(PyExc_SystemError, |
| 1106 | "unexpected expression in assignment %d (line %d)", |
| 1107 | e->kind, e->lineno); |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1112 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1113 | |
| 1114 | Only sets context for expr kinds that "can appear in assignment context" |
| 1115 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1116 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1117 | */ |
| 1118 | |
| 1119 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1120 | 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] | 1121 | { |
| 1122 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1123 | |
| 1124 | /* The ast defines augmented store and load contexts, but the |
| 1125 | implementation here doesn't actually use them. The code may be |
| 1126 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1127 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1128 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1129 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1130 | */ |
| 1131 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1132 | |
| 1133 | switch (e->kind) { |
| 1134 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1135 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1136 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1137 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1138 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1139 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1140 | e->v.Subscript.ctx = ctx; |
| 1141 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1142 | case Starred_kind: |
| 1143 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1144 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1145 | return 0; |
| 1146 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1147 | case Name_kind: |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1148 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1149 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1150 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1151 | } |
| 1152 | e->v.Name.ctx = ctx; |
| 1153 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1154 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1155 | e->v.List.ctx = ctx; |
| 1156 | s = e->v.List.elts; |
| 1157 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1158 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1159 | e->v.Tuple.ctx = ctx; |
| 1160 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1161 | break; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1162 | default: { |
| 1163 | const char *expr_name = get_expr_name(e); |
| 1164 | if (expr_name != NULL) { |
| 1165 | ast_error(c, n, "cannot %s %s", |
| 1166 | ctx == Store ? "assign to" : "delete", |
| 1167 | expr_name); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1168 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1169 | return 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1170 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 | /* 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] | 1174 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1175 | */ |
| 1176 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1177 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1179 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1180 | 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] | 1181 | return 0; |
| 1182 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1183 | } |
| 1184 | return 1; |
| 1185 | } |
| 1186 | |
| 1187 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1188 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1189 | { |
| 1190 | REQ(n, augassign); |
| 1191 | n = CHILD(n, 0); |
| 1192 | switch (STR(n)[0]) { |
| 1193 | case '+': |
| 1194 | return Add; |
| 1195 | case '-': |
| 1196 | return Sub; |
| 1197 | case '/': |
| 1198 | if (STR(n)[1] == '/') |
| 1199 | return FloorDiv; |
| 1200 | else |
| 1201 | return Div; |
| 1202 | case '%': |
| 1203 | return Mod; |
| 1204 | case '<': |
| 1205 | return LShift; |
| 1206 | case '>': |
| 1207 | return RShift; |
| 1208 | case '&': |
| 1209 | return BitAnd; |
| 1210 | case '^': |
| 1211 | return BitXor; |
| 1212 | case '|': |
| 1213 | return BitOr; |
| 1214 | case '*': |
| 1215 | if (STR(n)[1] == '*') |
| 1216 | return Pow; |
| 1217 | else |
| 1218 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1219 | case '@': |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1220 | if (c->c_feature_version < 5) { |
| 1221 | ast_error(c, n, |
| 1222 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 1223 | return (operator_ty)0; |
| 1224 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1225 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1226 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1227 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1228 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1233 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1234 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1235 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1236 | |'is' 'not' |
| 1237 | */ |
| 1238 | REQ(n, comp_op); |
| 1239 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1240 | n = CHILD(n, 0); |
| 1241 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1242 | case LESS: |
| 1243 | return Lt; |
| 1244 | case GREATER: |
| 1245 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1246 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1247 | return Eq; |
| 1248 | case LESSEQUAL: |
| 1249 | return LtE; |
| 1250 | case GREATEREQUAL: |
| 1251 | return GtE; |
| 1252 | case NOTEQUAL: |
| 1253 | return NotEq; |
| 1254 | case NAME: |
| 1255 | if (strcmp(STR(n), "in") == 0) |
| 1256 | return In; |
| 1257 | if (strcmp(STR(n), "is") == 0) |
| 1258 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1259 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1260 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1261 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1262 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1263 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1264 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1265 | } |
| 1266 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1267 | /* handle "not in" and "is not" */ |
| 1268 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1269 | case NAME: |
| 1270 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1271 | return NotIn; |
| 1272 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1273 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1274 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1275 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1276 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1277 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1278 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1279 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1280 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1281 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1282 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1283 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | static asdl_seq * |
| 1287 | seq_for_testlist(struct compiling *c, const node *n) |
| 1288 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1290 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1291 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1292 | asdl_seq *seq; |
| 1293 | expr_ty expression; |
| 1294 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1295 | 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] | 1296 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1297 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1298 | if (!seq) |
| 1299 | return NULL; |
| 1300 | |
| 1301 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1303 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1304 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1305 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1306 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1307 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1308 | |
| 1309 | assert(i / 2 < seq->size); |
| 1310 | asdl_seq_SET(seq, i / 2, expression); |
| 1311 | } |
| 1312 | return seq; |
| 1313 | } |
| 1314 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1315 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1316 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1317 | { |
| 1318 | identifier name; |
| 1319 | expr_ty annotation = NULL; |
| 1320 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1321 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1322 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1323 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1324 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1325 | name = NEW_IDENTIFIER(ch); |
| 1326 | if (!name) |
| 1327 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1328 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1329 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1330 | |
| 1331 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1332 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1333 | if (!annotation) |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1337 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1338 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1339 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1340 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1341 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1344 | /* returns -1 if failed to handle keyword only arguments |
| 1345 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1346 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1347 | ^^^ |
| 1348 | start pointing here |
| 1349 | */ |
| 1350 | static int |
| 1351 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1352 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1353 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1354 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1355 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1356 | expr_ty expression, annotation; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1357 | arg_ty arg = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1358 | int i = start; |
| 1359 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1362 | 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] | 1363 | return -1; |
| 1364 | } |
| 1365 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1366 | while (i < NCH(n)) { |
| 1367 | ch = CHILD(n, i); |
| 1368 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1369 | case vfpdef: |
| 1370 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1371 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1372 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1373 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1374 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1375 | asdl_seq_SET(kwdefaults, j, expression); |
| 1376 | i += 2; /* '=' and test */ |
| 1377 | } |
| 1378 | else { /* setting NULL if no default value exists */ |
| 1379 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1380 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1381 | if (NCH(ch) == 3) { |
| 1382 | /* ch is NAME ':' test */ |
| 1383 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1384 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1385 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1386 | } |
| 1387 | else { |
| 1388 | annotation = NULL; |
| 1389 | } |
| 1390 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1391 | argname = NEW_IDENTIFIER(ch); |
| 1392 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1393 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1394 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1395 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1396 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1397 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1398 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1399 | if (!arg) |
| 1400 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1401 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1402 | i += 1; /* the name */ |
| 1403 | if (TYPE(CHILD(n, i)) == COMMA) |
| 1404 | i += 1; /* the comma, if present */ |
| 1405 | break; |
| 1406 | case TYPE_COMMENT: |
| 1407 | /* arg will be equal to the last argument processed */ |
| 1408 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1409 | if (!arg->type_comment) |
| 1410 | goto error; |
| 1411 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | case DOUBLESTAR: |
| 1414 | return i; |
| 1415 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1416 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1417 | goto error; |
| 1418 | } |
| 1419 | } |
| 1420 | return i; |
| 1421 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1423 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1424 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1425 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1426 | |
| 1427 | static arguments_ty |
| 1428 | ast_for_arguments(struct compiling *c, const node *n) |
| 1429 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1430 | /* This function handles both typedargslist (function definition) |
| 1431 | and varargslist (lambda definition). |
| 1432 | |
| 1433 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1434 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1435 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1436 | | '**' tfpdef [',']]] |
| 1437 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1438 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1439 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1440 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1441 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1442 | | '**' vfpdef [',']]] |
| 1443 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1444 | | '**' vfpdef [','] |
| 1445 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1446 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1447 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1448 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1449 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1450 | int nposdefaults = 0, found_default = 0; |
| 1451 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1452 | arg_ty vararg = NULL, kwarg = NULL; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1453 | arg_ty arg = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1454 | node *ch; |
| 1455 | |
| 1456 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1457 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1458 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1459 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1460 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1461 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1462 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1463 | /* First count the number of positional args & defaults. The |
| 1464 | variable i is the loop index for this for loop and the next. |
| 1465 | The next loop picks up where the first leaves off. |
| 1466 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1467 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1468 | ch = CHILD(n, i); |
| 1469 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1470 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1471 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1472 | if (i < NCH(n) && /* skip argument following star */ |
| 1473 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1474 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1475 | i++; |
| 1476 | } |
| 1477 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1478 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1479 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1480 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1481 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1482 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1484 | defaults for keyword only args */ |
| 1485 | for ( ; i < NCH(n); ++i) { |
| 1486 | ch = CHILD(n, i); |
| 1487 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1488 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1489 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1490 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1491 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1492 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1493 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1494 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1495 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1496 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1498 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1499 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1500 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1502 | since we set NULL as default for keyword only argument w/o default |
| 1503 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1504 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1505 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1506 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1507 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1508 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1509 | /* tfpdef: NAME [':' test] |
| 1510 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1511 | */ |
| 1512 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1513 | j = 0; /* index for defaults */ |
| 1514 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1515 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1516 | ch = CHILD(n, i); |
| 1517 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1518 | case tfpdef: |
| 1519 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1521 | anything other than EQUAL or a comma? */ |
| 1522 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1523 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1524 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1525 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1526 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1527 | assert(posdefaults != NULL); |
| 1528 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1530 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1531 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1532 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1533 | ast_error(c, n, |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1534 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1535 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1536 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1537 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1538 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1539 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1540 | asdl_seq_SET(posargs, k++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1541 | i += 1; /* the name */ |
| 1542 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1543 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1544 | break; |
| 1545 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1546 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1547 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1548 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1549 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1550 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1551 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1552 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1553 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1554 | if (TYPE(ch) == COMMA) { |
| 1555 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1556 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1557 | |
| 1558 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1559 | ast_error(c, CHILD(n, i), |
| 1560 | "bare * has associated type comment"); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1564 | res = handle_keywordonly_args(c, n, i, |
| 1565 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1566 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1567 | i = res; /* res has new position to process */ |
| 1568 | } |
| 1569 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1570 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1571 | if (!vararg) |
| 1572 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1573 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1574 | i += 2; /* the star and the name */ |
| 1575 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1576 | i += 1; /* the comma, if present */ |
| 1577 | |
| 1578 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1579 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1580 | if (!vararg->type_comment) |
| 1581 | return NULL; |
| 1582 | i += 1; |
| 1583 | } |
| 1584 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1585 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1586 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1587 | int res = 0; |
| 1588 | res = handle_keywordonly_args(c, n, i, |
| 1589 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1590 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1591 | i = res; /* res has new position to process */ |
| 1592 | } |
| 1593 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1596 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1597 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1598 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1599 | if (!kwarg) |
| 1600 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1601 | i += 2; /* the double star and the name */ |
| 1602 | if (TYPE(CHILD(n, i)) == COMMA) |
| 1603 | i += 1; /* the comma, if present */ |
| 1604 | break; |
| 1605 | case TYPE_COMMENT: |
| 1606 | assert(i); |
| 1607 | |
| 1608 | if (kwarg) |
| 1609 | arg = kwarg; |
| 1610 | |
| 1611 | /* arg will be equal to the last argument processed */ |
| 1612 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1613 | if (!arg->type_comment) |
| 1614 | return NULL; |
| 1615 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1616 | break; |
| 1617 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1618 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1619 | "unexpected node in varargslist: %d @ %d", |
| 1620 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1621 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1622 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1623 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1624 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | static expr_ty |
| 1628 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1629 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1630 | expr_ty e; |
| 1631 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1632 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1633 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1634 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1635 | |
| 1636 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1637 | |
| 1638 | lineno = LINENO(n); |
| 1639 | col_offset = n->n_col_offset; |
| 1640 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1641 | ch = CHILD(n, 0); |
| 1642 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1643 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1644 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1645 | e = Name(id, Load, lineno, col_offset, |
| 1646 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1647 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1648 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1649 | |
| 1650 | for (i = 2; i < NCH(n); i+=2) { |
| 1651 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1652 | if (!id) |
| 1653 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1654 | e = Attribute(e, id, Load, lineno, col_offset, |
| 1655 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1656 | if (!e) |
| 1657 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | static expr_ty |
| 1664 | ast_for_decorator(struct compiling *c, const node *n) |
| 1665 | { |
| 1666 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1667 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1668 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1670 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1671 | REQ(CHILD(n, 0), AT); |
| 1672 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1674 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1675 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1676 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1678 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1679 | d = name_expr; |
| 1680 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1681 | } |
| 1682 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1683 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1684 | n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1685 | if (!d) |
| 1686 | return NULL; |
| 1687 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1688 | } |
| 1689 | else { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1690 | d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1691 | if (!d) |
| 1692 | return NULL; |
| 1693 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | static asdl_seq* |
| 1700 | ast_for_decorators(struct compiling *c, const node *n) |
| 1701 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1702 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1703 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1704 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1706 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1707 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | if (!decorator_seq) |
| 1709 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1711 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1712 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1713 | if (!d) |
| 1714 | return NULL; |
| 1715 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | } |
| 1717 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1721 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1722 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1723 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1724 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1725 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1726 | identifier name; |
| 1727 | arguments_ty args; |
| 1728 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1729 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1730 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1731 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1732 | node *tc; |
| 1733 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1734 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1735 | if (is_async && c->c_feature_version < 5) { |
| 1736 | ast_error(c, n, |
| 1737 | "Async functions are only supported in Python 3.5 and greater"); |
| 1738 | return NULL; |
| 1739 | } |
| 1740 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1741 | REQ(n, funcdef); |
| 1742 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1743 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1744 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1745 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1746 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1747 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1748 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1749 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1750 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1751 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1752 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1753 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1754 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1755 | name_i += 2; |
| 1756 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1757 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1758 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1759 | if (!type_comment) |
| 1760 | return NULL; |
| 1761 | name_i += 1; |
| 1762 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1763 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1764 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1765 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1766 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1767 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1768 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1769 | /* Check if the suite has a type comment in it. */ |
| 1770 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1771 | |
| 1772 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1773 | if (type_comment != NULL) { |
| 1774 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1778 | if (!type_comment) |
| 1779 | return NULL; |
| 1780 | } |
| 1781 | } |
| 1782 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1783 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1784 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1785 | LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1786 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1787 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1788 | LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | static stmt_ty |
| 1792 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1793 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1794 | /* async_funcdef: ASYNC funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1795 | REQ(n, async_funcdef); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1796 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1797 | REQ(CHILD(n, 1), funcdef); |
| 1798 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1799 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1800 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | static stmt_ty |
| 1804 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1805 | { |
| 1806 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1807 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1808 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | static stmt_ty |
| 1813 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1814 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1815 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1816 | REQ(n, async_stmt); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1817 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1818 | |
| 1819 | switch (TYPE(CHILD(n, 1))) { |
| 1820 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1821 | return ast_for_funcdef_impl(c, n, NULL, |
| 1822 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1823 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1824 | return ast_for_with_stmt(c, n, |
| 1825 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1826 | |
| 1827 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1828 | return ast_for_for_stmt(c, n, |
| 1829 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1830 | |
| 1831 | default: |
| 1832 | PyErr_Format(PyExc_SystemError, |
| 1833 | "invalid async stament: %s", |
| 1834 | STR(CHILD(n, 1))); |
| 1835 | return NULL; |
| 1836 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1839 | static stmt_ty |
| 1840 | ast_for_decorated(struct compiling *c, const node *n) |
| 1841 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1842 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1843 | stmt_ty thing = NULL; |
| 1844 | asdl_seq *decorator_seq = NULL; |
| 1845 | |
| 1846 | REQ(n, decorated); |
| 1847 | |
| 1848 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1849 | if (!decorator_seq) |
| 1850 | return NULL; |
| 1851 | |
| 1852 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1853 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1855 | |
| 1856 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1857 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1858 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1859 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1860 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1861 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1862 | } |
| 1863 | return thing; |
| 1864 | } |
| 1865 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1866 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1867 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1868 | { |
| 1869 | /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* |
| 1870 | ['else' ':' suite] |
| 1871 | namedexpr_test: test [':=' test] |
| 1872 | argument: ( test [comp_for] | |
| 1873 | test ':=' test | |
| 1874 | test '=' test | |
| 1875 | '**' test | |
| 1876 | '*' test ) |
| 1877 | */ |
| 1878 | expr_ty target, value; |
| 1879 | |
| 1880 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1881 | if (!target) |
| 1882 | return NULL; |
| 1883 | |
| 1884 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1885 | if (!value) |
| 1886 | return NULL; |
| 1887 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1888 | if (target->kind != Name_kind) { |
| 1889 | const char *expr_name = get_expr_name(target); |
| 1890 | if (expr_name != NULL) { |
| 1891 | ast_error(c, n, "cannot use named assignment with %s", expr_name); |
| 1892 | } |
| 1893 | return NULL; |
| 1894 | } |
| 1895 | |
| 1896 | if (!set_context(c, target, Store, n)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1897 | return NULL; |
| 1898 | |
| 1899 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1900 | n->n_end_col_offset, c->c_arena); |
| 1901 | } |
| 1902 | |
| 1903 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1904 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1905 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1906 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1907 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1908 | arguments_ty args; |
| 1909 | expr_ty expression; |
| 1910 | |
| 1911 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1912 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1913 | if (!args) |
| 1914 | return NULL; |
| 1915 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1916 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1917 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1918 | } |
| 1919 | else { |
| 1920 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1921 | if (!args) |
| 1922 | return NULL; |
| 1923 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1924 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1928 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1929 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1932 | static expr_ty |
| 1933 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1936 | expr_ty expression, body, orelse; |
| 1937 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1938 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1939 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1940 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1941 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1942 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1943 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1944 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1945 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1946 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1947 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1948 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1949 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1950 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1953 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1954 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1955 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1956 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1957 | */ |
| 1958 | |
| 1959 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1960 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1961 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1962 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1963 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1964 | count_comp_for: |
| 1965 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1966 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1967 | if (NCH(n) == 2) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1968 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1969 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1970 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1971 | else if (NCH(n) == 1) { |
| 1972 | n = CHILD(n, 0); |
| 1973 | } |
| 1974 | else { |
| 1975 | goto error; |
| 1976 | } |
| 1977 | if (NCH(n) == (5)) { |
| 1978 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1979 | } |
| 1980 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1981 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1982 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1983 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1984 | REQ(n, comp_iter); |
| 1985 | n = CHILD(n, 0); |
| 1986 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1987 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1988 | else if (TYPE(n) == comp_if) { |
| 1989 | if (NCH(n) == 3) { |
| 1990 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1991 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1992 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1993 | else |
| 1994 | return n_fors; |
| 1995 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1996 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1997 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1998 | /* Should never be reached */ |
| 1999 | PyErr_SetString(PyExc_SystemError, |
| 2000 | "logic error in count_comp_fors"); |
| 2001 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2004 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2005 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2006 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2007 | */ |
| 2008 | |
| 2009 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2010 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2011 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2012 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2013 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2014 | while (1) { |
| 2015 | REQ(n, comp_iter); |
| 2016 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2017 | return n_ifs; |
| 2018 | n = CHILD(n, 0); |
| 2019 | REQ(n, comp_if); |
| 2020 | n_ifs++; |
| 2021 | if (NCH(n) == 2) |
| 2022 | return n_ifs; |
| 2023 | n = CHILD(n, 2); |
| 2024 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2027 | static asdl_seq * |
| 2028 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2029 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2030 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2031 | asdl_seq *comps; |
| 2032 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2033 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2034 | if (n_fors == -1) |
| 2035 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2037 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2038 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2039 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2040 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2041 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2042 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2043 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2044 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2045 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2046 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2047 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2049 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2051 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2052 | is_async = 1; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2053 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2054 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2055 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2056 | else { |
| 2057 | sync_n = CHILD(n, 0); |
| 2058 | } |
| 2059 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2060 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2061 | /* Async comprehensions only allowed in Python 3.6 and greater */ |
| 2062 | if (is_async && c->c_feature_version < 6) { |
| 2063 | ast_error(c, n, |
| 2064 | "Async comprehensions are only supported in Python 3.6 and greater"); |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2068 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2069 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2070 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2071 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2072 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2073 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2074 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2075 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2076 | /* Check the # of children rather than the length of t, since |
| 2077 | (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] | 2078 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2079 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2080 | comp = comprehension(first, expression, NULL, |
| 2081 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2082 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2083 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2084 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2085 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2086 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2087 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2088 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2089 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2090 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2091 | int j, n_ifs; |
| 2092 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2094 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2095 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2096 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2097 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2099 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2100 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2101 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2102 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2103 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2104 | REQ(n, comp_iter); |
| 2105 | n = CHILD(n, 0); |
| 2106 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2108 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2109 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2110 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2111 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2112 | if (NCH(n) == 3) |
| 2113 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2114 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2115 | /* on exit, must guarantee that n is a comp_for */ |
| 2116 | if (TYPE(n) == comp_iter) |
| 2117 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2118 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2119 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2120 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2121 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2122 | return comps; |
| 2123 | } |
| 2124 | |
| 2125 | static expr_ty |
| 2126 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2127 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2128 | /* testlist_comp: (test|star_expr) |
| 2129 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2130 | expr_ty elt; |
| 2131 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2132 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2134 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2136 | ch = CHILD(n, 0); |
| 2137 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2138 | if (!elt) |
| 2139 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2140 | if (elt->kind == Starred_kind) { |
| 2141 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2142 | return NULL; |
| 2143 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2145 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2146 | if (!comps) |
| 2147 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2148 | |
| 2149 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2150 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2151 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2152 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2153 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2154 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2155 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2156 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2157 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2158 | else |
| 2159 | /* Should never happen */ |
| 2160 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2163 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2164 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2165 | * elements. Iff successful, nonzero is returned. |
| 2166 | */ |
| 2167 | static int |
| 2168 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2169 | expr_ty *key, expr_ty *value) |
| 2170 | { |
| 2171 | expr_ty expression; |
| 2172 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2173 | assert(NCH(n) - *i >= 2); |
| 2174 | |
| 2175 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2176 | if (!expression) |
| 2177 | return 0; |
| 2178 | *key = NULL; |
| 2179 | *value = expression; |
| 2180 | |
| 2181 | *i += 2; |
| 2182 | } |
| 2183 | else { |
| 2184 | assert(NCH(n) - *i >= 3); |
| 2185 | |
| 2186 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2187 | if (!expression) |
| 2188 | return 0; |
| 2189 | *key = expression; |
| 2190 | |
| 2191 | REQ(CHILD(n, *i + 1), COLON); |
| 2192 | |
| 2193 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2194 | if (!expression) |
| 2195 | return 0; |
| 2196 | *value = expression; |
| 2197 | |
| 2198 | *i += 3; |
| 2199 | } |
| 2200 | return 1; |
| 2201 | } |
| 2202 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2203 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2204 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2205 | { |
| 2206 | expr_ty key, value; |
| 2207 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2208 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2210 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2211 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2212 | assert(key); |
| 2213 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2215 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2216 | if (!comps) |
| 2217 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2219 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2220 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2224 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2225 | { |
| 2226 | int i; |
| 2227 | int j; |
| 2228 | int size; |
| 2229 | asdl_seq *keys, *values; |
| 2230 | |
| 2231 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2232 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2233 | if (!keys) |
| 2234 | return NULL; |
| 2235 | |
| 2236 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2237 | if (!values) |
| 2238 | return NULL; |
| 2239 | |
| 2240 | j = 0; |
| 2241 | for (i = 0; i < NCH(n); i++) { |
| 2242 | expr_ty key, value; |
| 2243 | |
| 2244 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2245 | return NULL; |
| 2246 | asdl_seq_SET(keys, j, key); |
| 2247 | asdl_seq_SET(values, j, value); |
| 2248 | |
| 2249 | j++; |
| 2250 | } |
| 2251 | keys->size = j; |
| 2252 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2253 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2254 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2258 | ast_for_genexp(struct compiling *c, const node *n) |
| 2259 | { |
| 2260 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2261 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | static expr_ty |
| 2265 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2266 | { |
| 2267 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2268 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | static expr_ty |
| 2272 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2273 | { |
| 2274 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2275 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2278 | static expr_ty |
| 2279 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2280 | { |
| 2281 | int i; |
| 2282 | int size; |
| 2283 | asdl_seq *elts; |
| 2284 | |
| 2285 | assert(TYPE(n) == (dictorsetmaker)); |
| 2286 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2287 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2288 | if (!elts) |
| 2289 | return NULL; |
| 2290 | for (i = 0; i < NCH(n); i += 2) { |
| 2291 | expr_ty expression; |
| 2292 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2293 | if (!expression) |
| 2294 | return NULL; |
| 2295 | asdl_seq_SET(elts, i / 2, expression); |
| 2296 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2297 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2298 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2299 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2300 | |
| 2301 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2302 | ast_for_atom(struct compiling *c, const node *n) |
| 2303 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2304 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2305 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2306 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2307 | */ |
| 2308 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2310 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2311 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2312 | PyObject *name; |
| 2313 | const char *s = STR(ch); |
| 2314 | size_t len = strlen(s); |
| 2315 | if (len >= 4 && len <= 5) { |
| 2316 | if (!strcmp(s, "None")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2317 | return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2318 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2319 | if (!strcmp(s, "True")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2320 | return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2321 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2322 | if (!strcmp(s, "False")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2323 | return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2324 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2325 | } |
| 2326 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2327 | if (!name) |
| 2328 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2329 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2330 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2331 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2332 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2333 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2334 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2335 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2336 | const char *errtype = NULL; |
| 2337 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2338 | errtype = "unicode error"; |
| 2339 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2340 | errtype = "value error"; |
| 2341 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2342 | PyObject *type, *value, *tback, *errstr; |
| 2343 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2344 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2345 | if (errstr) { |
| 2346 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2347 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2348 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2349 | else { |
| 2350 | PyErr_Clear(); |
| 2351 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2352 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2353 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2354 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2355 | Py_XDECREF(tback); |
| 2356 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2357 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2358 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2359 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2360 | } |
| 2361 | case NUMBER: { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2362 | PyObject *pynum; |
| 2363 | /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ |
| 2364 | /* Check for underscores here rather than in parse_number so we can report a line number on error */ |
| 2365 | if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { |
| 2366 | ast_error(c, ch, |
| 2367 | "Underscores in numeric literals are only supported in Python 3.6 and greater"); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2371 | if (!pynum) |
| 2372 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2373 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2374 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2375 | Py_DECREF(pynum); |
| 2376 | return NULL; |
| 2377 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2378 | return Constant(pynum, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2379 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2380 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2381 | case ELLIPSIS: /* Ellipsis */ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2382 | return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2383 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2384 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2385 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2387 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2388 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2389 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2391 | if (TYPE(ch) == yield_expr) |
| 2392 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2395 | if (NCH(ch) == 1) { |
| 2396 | return ast_for_testlist(c, ch); |
| 2397 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2398 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2399 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2400 | return copy_location(ast_for_genexp(c, ch), n); |
| 2401 | } |
| 2402 | else { |
| 2403 | return copy_location(ast_for_testlist(c, ch), n); |
| 2404 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2405 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2406 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2408 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2409 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2410 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2412 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2413 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2414 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2415 | if (!elts) |
| 2416 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2417 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2418 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2419 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2420 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2421 | else { |
| 2422 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2423 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2424 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2425 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2426 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2427 | * ((test | '*' test) |
| 2428 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2429 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2430 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2431 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2432 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2433 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2434 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2435 | } |
| 2436 | else { |
| 2437 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2438 | if (NCH(ch) == 1 || |
| 2439 | (NCH(ch) > 1 && |
| 2440 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2441 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2442 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2443 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2444 | else if (NCH(ch) > 1 && |
| 2445 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2446 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2447 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2448 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2449 | else if (NCH(ch) > 3 - is_dict && |
| 2450 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2451 | /* It's a dictionary comprehension. */ |
| 2452 | if (is_dict) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2453 | ast_error(c, n, |
| 2454 | "dict unpacking cannot be used in dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2455 | return NULL; |
| 2456 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2457 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2458 | } |
| 2459 | else { |
| 2460 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2461 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2462 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2463 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2464 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2465 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2466 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2467 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2468 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | static slice_ty |
| 2473 | ast_for_slice(struct compiling *c, const node *n) |
| 2474 | { |
| 2475 | node *ch; |
| 2476 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2477 | |
| 2478 | REQ(n, subscript); |
| 2479 | |
| 2480 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2481 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2482 | sliceop: ':' [test] |
| 2483 | */ |
| 2484 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2485 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2486 | /* 'step' variable hold no significance in terms of being used over |
| 2487 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2489 | if (!step) |
| 2490 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2492 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2496 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2497 | if (!lower) |
| 2498 | return NULL; |
| 2499 | } |
| 2500 | |
| 2501 | /* If there's an upper bound it's in the second or third position. */ |
| 2502 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2503 | if (NCH(n) > 1) { |
| 2504 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2505 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2506 | if (TYPE(n2) == test) { |
| 2507 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2508 | if (!upper) |
| 2509 | return NULL; |
| 2510 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2511 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2512 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2513 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2514 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2515 | if (TYPE(n2) == test) { |
| 2516 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2517 | if (!upper) |
| 2518 | return NULL; |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | ch = CHILD(n, NCH(n) - 1); |
| 2523 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2524 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2525 | ch = CHILD(ch, 1); |
| 2526 | if (TYPE(ch) == test) { |
| 2527 | step = ast_for_expr(c, ch); |
| 2528 | if (!step) |
| 2529 | return NULL; |
| 2530 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2531 | } |
| 2532 | } |
| 2533 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2534 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | static expr_ty |
| 2538 | ast_for_binop(struct compiling *c, const node *n) |
| 2539 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2540 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2542 | BinOp(BinOp(A, op, B), op, C). |
| 2543 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2544 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2545 | int i, nops; |
| 2546 | expr_ty expr1, expr2, result; |
| 2547 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2548 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2549 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2550 | if (!expr1) |
| 2551 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2552 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2553 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2554 | if (!expr2) |
| 2555 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2556 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2557 | newoperator = get_operator(c, CHILD(n, 1)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2558 | if (!newoperator) |
| 2559 | return NULL; |
| 2560 | |
| 2561 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2562 | CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2563 | c->c_arena); |
| 2564 | if (!result) |
| 2565 | return NULL; |
| 2566 | |
| 2567 | nops = (NCH(n) - 1) / 2; |
| 2568 | for (i = 1; i < nops; i++) { |
| 2569 | expr_ty tmp_result, tmp; |
| 2570 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2571 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2572 | newoperator = get_operator(c, next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2573 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2574 | return NULL; |
| 2575 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2576 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2577 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2578 | return NULL; |
| 2579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2581 | LINENO(next_oper), next_oper->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2582 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2583 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2584 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2585 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2586 | return NULL; |
| 2587 | result = tmp_result; |
| 2588 | } |
| 2589 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2592 | static expr_ty |
| 2593 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2594 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2596 | subscriptlist: subscript (',' subscript)* [','] |
| 2597 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2598 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2599 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2600 | REQ(n, trailer); |
| 2601 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2602 | if (NCH(n) == 2) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2603 | return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, |
| 2604 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2605 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2606 | return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2607 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2608 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2609 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2610 | if (!attr_id) |
| 2611 | return NULL; |
| 2612 | return Attribute(left_expr, attr_id, Load, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2613 | LINENO(n), n->n_col_offset, |
| 2614 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2615 | } |
| 2616 | else { |
| 2617 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2618 | REQ(CHILD(n, 2), RSQB); |
| 2619 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2620 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2621 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2622 | if (!slc) |
| 2623 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2624 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2625 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2626 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2627 | } |
| 2628 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2630 | by treating the sequence as a tuple literal if there are |
| 2631 | no slice features. |
| 2632 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2633 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2634 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2635 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2636 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2637 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2638 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2639 | if (!slices) |
| 2640 | return NULL; |
| 2641 | for (j = 0; j < NCH(n); j += 2) { |
| 2642 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2643 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2644 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2645 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2646 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2647 | asdl_seq_SET(slices, j / 2, slc); |
| 2648 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2649 | if (!simple) { |
| 2650 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2651 | Load, LINENO(n), n->n_col_offset, |
| 2652 | n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2653 | } |
| 2654 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2655 | 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] | 2656 | if (!elts) |
| 2657 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2658 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2659 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2660 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2661 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2662 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2663 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2664 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2665 | if (!e) |
| 2666 | return NULL; |
| 2667 | return Subscript(left_expr, Index(e, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2668 | Load, LINENO(n), n->n_col_offset, |
| 2669 | n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2670 | } |
| 2671 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2675 | ast_for_factor(struct compiling *c, const node *n) |
| 2676 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2677 | expr_ty expression; |
| 2678 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2679 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2680 | if (!expression) |
| 2681 | return NULL; |
| 2682 | |
| 2683 | switch (TYPE(CHILD(n, 0))) { |
| 2684 | case PLUS: |
| 2685 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2686 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2687 | c->c_arena); |
| 2688 | case MINUS: |
| 2689 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2690 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2691 | c->c_arena); |
| 2692 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2693 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2694 | n->n_end_lineno, n->n_end_col_offset, |
| 2695 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2696 | } |
| 2697 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2698 | TYPE(CHILD(n, 0))); |
| 2699 | return NULL; |
| 2700 | } |
| 2701 | |
| 2702 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2703 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2704 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2705 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2706 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2707 | |
| 2708 | REQ(n, atom_expr); |
| 2709 | nch = NCH(n); |
| 2710 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2711 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2712 | if (c->c_feature_version < 5) { |
| 2713 | ast_error(c, n, |
| 2714 | "Await expressions are only supported in Python 3.5 and greater"); |
| 2715 | return NULL; |
| 2716 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2717 | start = 1; |
| 2718 | assert(nch > 1); |
| 2719 | } |
| 2720 | |
| 2721 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2722 | if (!e) |
| 2723 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2724 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2725 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2726 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2727 | return Await(e, LINENO(n), n->n_col_offset, |
| 2728 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2732 | node *ch = CHILD(n, i); |
| 2733 | if (TYPE(ch) != trailer) |
| 2734 | break; |
| 2735 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2736 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2737 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2738 | tmp->lineno = e->lineno; |
| 2739 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2740 | e = tmp; |
| 2741 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2742 | |
| 2743 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2744 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2745 | return Await(e, LINENO(n), n->n_col_offset, |
| 2746 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2747 | } |
| 2748 | else { |
| 2749 | return e; |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | static expr_ty |
| 2754 | ast_for_power(struct compiling *c, const node *n) |
| 2755 | { |
| 2756 | /* power: atom trailer* ('**' factor)* |
| 2757 | */ |
| 2758 | expr_ty e; |
| 2759 | REQ(n, power); |
| 2760 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2761 | if (!e) |
| 2762 | return NULL; |
| 2763 | if (NCH(n) == 1) |
| 2764 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2765 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2766 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2767 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2768 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2769 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2770 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2771 | } |
| 2772 | return e; |
| 2773 | } |
| 2774 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2775 | static expr_ty |
| 2776 | ast_for_starred(struct compiling *c, const node *n) |
| 2777 | { |
| 2778 | expr_ty tmp; |
| 2779 | REQ(n, star_expr); |
| 2780 | |
| 2781 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2782 | if (!tmp) |
| 2783 | return NULL; |
| 2784 | |
| 2785 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2786 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2787 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2791 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2792 | */ |
| 2793 | |
| 2794 | static expr_ty |
| 2795 | ast_for_expr(struct compiling *c, const node *n) |
| 2796 | { |
| 2797 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2798 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2799 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2800 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2802 | and_test: not_test ('and' not_test)* |
| 2803 | not_test: 'not' not_test | comparison |
| 2804 | comparison: expr (comp_op expr)* |
| 2805 | expr: xor_expr ('|' xor_expr)* |
| 2806 | xor_expr: and_expr ('^' and_expr)* |
| 2807 | and_expr: shift_expr ('&' shift_expr)* |
| 2808 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2809 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2810 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2811 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2812 | power: atom_expr ['**' factor] |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2813 | atom_expr: [AWAIT] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2814 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2815 | */ |
| 2816 | |
| 2817 | asdl_seq *seq; |
| 2818 | int i; |
| 2819 | |
| 2820 | loop: |
| 2821 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2822 | case namedexpr_test: |
| 2823 | if (NCH(n) == 3) |
| 2824 | return ast_for_namedexpr(c, n); |
| 2825 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2826 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2827 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2828 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2829 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2830 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2831 | else if (NCH(n) > 1) |
| 2832 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2833 | /* Fallthrough */ |
| 2834 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2835 | case and_test: |
| 2836 | if (NCH(n) == 1) { |
| 2837 | n = CHILD(n, 0); |
| 2838 | goto loop; |
| 2839 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2840 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2841 | if (!seq) |
| 2842 | return NULL; |
| 2843 | for (i = 0; i < NCH(n); i += 2) { |
| 2844 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2845 | if (!e) |
| 2846 | return NULL; |
| 2847 | asdl_seq_SET(seq, i / 2, e); |
| 2848 | } |
| 2849 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2850 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2851 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2852 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2853 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2854 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2855 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | case not_test: |
| 2857 | if (NCH(n) == 1) { |
| 2858 | n = CHILD(n, 0); |
| 2859 | goto loop; |
| 2860 | } |
| 2861 | else { |
| 2862 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2863 | if (!expression) |
| 2864 | return NULL; |
| 2865 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2866 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2867 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2868 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | } |
| 2870 | case comparison: |
| 2871 | if (NCH(n) == 1) { |
| 2872 | n = CHILD(n, 0); |
| 2873 | goto loop; |
| 2874 | } |
| 2875 | else { |
| 2876 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2877 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2878 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2879 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | if (!ops) |
| 2881 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2882 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2884 | return NULL; |
| 2885 | } |
| 2886 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2887 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2889 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2890 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2892 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2893 | |
| 2894 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2895 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2896 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2897 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2899 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2900 | asdl_seq_SET(cmps, i / 2, expression); |
| 2901 | } |
| 2902 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2903 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2905 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2907 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2908 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2909 | } |
| 2910 | break; |
| 2911 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2912 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2914 | /* The next five cases all handle BinOps. The main body of code |
| 2915 | is the same in each case, but the switch turned inside out to |
| 2916 | reuse the code for each type of operator. |
| 2917 | */ |
| 2918 | case expr: |
| 2919 | case xor_expr: |
| 2920 | case and_expr: |
| 2921 | case shift_expr: |
| 2922 | case arith_expr: |
| 2923 | case term: |
| 2924 | if (NCH(n) == 1) { |
| 2925 | n = CHILD(n, 0); |
| 2926 | goto loop; |
| 2927 | } |
| 2928 | return ast_for_binop(c, n); |
| 2929 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2930 | node *an = NULL; |
| 2931 | node *en = NULL; |
| 2932 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2933 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2934 | if (NCH(n) > 1) |
| 2935 | an = CHILD(n, 1); /* yield_arg */ |
| 2936 | if (an) { |
| 2937 | en = CHILD(an, NCH(an) - 1); |
| 2938 | if (NCH(an) == 2) { |
| 2939 | is_from = 1; |
| 2940 | exp = ast_for_expr(c, en); |
| 2941 | } |
| 2942 | else |
| 2943 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2944 | if (!exp) |
| 2945 | return NULL; |
| 2946 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2947 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2948 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2949 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2950 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2951 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2952 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2953 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2954 | if (NCH(n) == 1) { |
| 2955 | n = CHILD(n, 0); |
| 2956 | goto loop; |
| 2957 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2958 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2959 | case power: |
| 2960 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2961 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2962 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2963 | return NULL; |
| 2964 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2965 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2966 | return NULL; |
| 2967 | } |
| 2968 | |
| 2969 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2970 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2971 | const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | { |
| 2973 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2974 | arglist: argument (',' argument)* [','] |
| 2975 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2976 | */ |
| 2977 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2978 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2979 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2980 | asdl_seq *args; |
| 2981 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | |
| 2983 | REQ(n, arglist); |
| 2984 | |
| 2985 | nargs = 0; |
| 2986 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2987 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2988 | node *ch = CHILD(n, i); |
| 2989 | if (TYPE(ch) == argument) { |
| 2990 | if (NCH(ch) == 1) |
| 2991 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2992 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2993 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2994 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2995 | ast_error(c, ch, "invalid syntax"); |
| 2996 | return NULL; |
| 2997 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2998 | if (NCH(n) > 1) { |
| 2999 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 3000 | return NULL; |
| 3001 | } |
| 3002 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3003 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 3004 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3005 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3006 | nargs++; |
| 3007 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3008 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3009 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3010 | nkeywords++; |
| 3011 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3012 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 3014 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3015 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3016 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3017 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3018 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3019 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3020 | |
| 3021 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 3022 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 3023 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3024 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3025 | node *ch = CHILD(n, i); |
| 3026 | if (TYPE(ch) == argument) { |
| 3027 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3028 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3029 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3030 | /* a positional argument */ |
| 3031 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3032 | if (ndoublestars) { |
| 3033 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3034 | "positional argument follows " |
| 3035 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3036 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3037 | else { |
| 3038 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3039 | "positional argument follows " |
| 3040 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3041 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3042 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3043 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3044 | e = ast_for_expr(c, chch); |
| 3045 | if (!e) |
| 3046 | return NULL; |
| 3047 | asdl_seq_SET(args, nargs++, e); |
| 3048 | } |
| 3049 | else if (TYPE(chch) == STAR) { |
| 3050 | /* an iterable argument unpacking */ |
| 3051 | expr_ty starred; |
| 3052 | if (ndoublestars) { |
| 3053 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3054 | "iterable argument unpacking follows " |
| 3055 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3056 | return NULL; |
| 3057 | } |
| 3058 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3059 | if (!e) |
| 3060 | return NULL; |
| 3061 | starred = Starred(e, Load, LINENO(chch), |
| 3062 | chch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3063 | chch->n_end_lineno, chch->n_end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3064 | c->c_arena); |
| 3065 | if (!starred) |
| 3066 | return NULL; |
| 3067 | asdl_seq_SET(args, nargs++, starred); |
| 3068 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3069 | } |
| 3070 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3071 | /* a keyword argument unpacking */ |
| 3072 | keyword_ty kw; |
| 3073 | i++; |
| 3074 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3075 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3076 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3077 | kw = keyword(NULL, e, c->c_arena); |
| 3078 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3079 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3081 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3082 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3083 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3084 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3085 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3086 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3087 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3088 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3089 | /* treat colon equal as positional argument */ |
| 3090 | if (nkeywords) { |
| 3091 | if (ndoublestars) { |
| 3092 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3093 | "positional argument follows " |
| 3094 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3095 | } |
| 3096 | else { |
| 3097 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3098 | "positional argument follows " |
| 3099 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3100 | } |
| 3101 | return NULL; |
| 3102 | } |
| 3103 | e = ast_for_namedexpr(c, ch); |
| 3104 | if (!e) |
| 3105 | return NULL; |
| 3106 | asdl_seq_SET(args, nargs++, e); |
| 3107 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3108 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3109 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3110 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3111 | identifier key, tmp; |
| 3112 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3113 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3114 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3115 | // expression) in the keyword slot of a call site. So, we need |
| 3116 | // to manually enforce that the keyword is a NAME here. |
| 3117 | static const int name_tree[] = { |
| 3118 | test, |
| 3119 | or_test, |
| 3120 | and_test, |
| 3121 | not_test, |
| 3122 | comparison, |
| 3123 | expr, |
| 3124 | xor_expr, |
| 3125 | and_expr, |
| 3126 | shift_expr, |
| 3127 | arith_expr, |
| 3128 | term, |
| 3129 | factor, |
| 3130 | power, |
| 3131 | atom_expr, |
| 3132 | atom, |
| 3133 | 0, |
| 3134 | }; |
| 3135 | node *expr_node = chch; |
| 3136 | for (int i = 0; name_tree[i]; i++) { |
| 3137 | if (TYPE(expr_node) != name_tree[i]) |
| 3138 | break; |
| 3139 | if (NCH(expr_node) != 1) |
| 3140 | break; |
| 3141 | expr_node = CHILD(expr_node, 0); |
| 3142 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3143 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3144 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3145 | "expression cannot contain assignment, " |
| 3146 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3147 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3148 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3149 | key = new_identifier(STR(expr_node), c); |
| 3150 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3151 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3153 | if (forbidden_name(c, key, chch, 1)) { |
| 3154 | return NULL; |
| 3155 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3156 | for (k = 0; k < nkeywords; k++) { |
| 3157 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3158 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 3159 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3160 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3161 | return NULL; |
| 3162 | } |
| 3163 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3164 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3165 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3166 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3167 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3168 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3169 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3170 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3171 | } |
| 3172 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3175 | return Call(func, args, keywords, func->lineno, func->col_offset, |
| 3176 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3179 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3180 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3182 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3183 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3184 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3185 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3186 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3187 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3188 | } |
| 3189 | else { |
| 3190 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3191 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3192 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3193 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3194 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3195 | else { |
| 3196 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3197 | if (!tmp) |
| 3198 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3199 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3200 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3204 | static stmt_ty |
| 3205 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3206 | { |
| 3207 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3208 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3209 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3210 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3211 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3212 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3213 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3214 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3215 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3216 | int num = NCH(n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3217 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3218 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3219 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3220 | if (!e) |
| 3221 | return NULL; |
| 3222 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3223 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3224 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3225 | } |
| 3226 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3227 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3228 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3229 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3230 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3231 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3232 | if (!expr1) |
| 3233 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3234 | if(!set_context(c, expr1, Store, ch)) |
| 3235 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3236 | /* set_context checks that most expressions are not the left side. |
| 3237 | Augmented assignments can only have a name, a subscript, or an |
| 3238 | attribute on the left, though, so we have to explicitly check for |
| 3239 | those. */ |
| 3240 | switch (expr1->kind) { |
| 3241 | case Name_kind: |
| 3242 | case Attribute_kind: |
| 3243 | case Subscript_kind: |
| 3244 | break; |
| 3245 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3246 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3247 | return NULL; |
| 3248 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3249 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3250 | ch = CHILD(n, 2); |
| 3251 | if (TYPE(ch) == testlist) |
| 3252 | expr2 = ast_for_testlist(c, ch); |
| 3253 | else |
| 3254 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3255 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3256 | return NULL; |
| 3257 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3258 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3259 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3260 | return NULL; |
| 3261 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3262 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3263 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3264 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3265 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3266 | expr_ty expr1, expr2, expr3; |
| 3267 | node *ch = CHILD(n, 0); |
| 3268 | node *deep, *ann = CHILD(n, 1); |
| 3269 | int simple = 1; |
| 3270 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 3271 | /* AnnAssigns are only allowed in Python 3.6 or greater */ |
| 3272 | if (c->c_feature_version < 6) { |
| 3273 | ast_error(c, ch, |
| 3274 | "Variable annotation syntax is only supported in Python 3.6 and greater"); |
| 3275 | return NULL; |
| 3276 | } |
| 3277 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3278 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3279 | deep = ch; |
| 3280 | while (NCH(deep) == 1) { |
| 3281 | deep = CHILD(deep, 0); |
| 3282 | } |
| 3283 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3284 | simple = 0; |
| 3285 | } |
| 3286 | expr1 = ast_for_testlist(c, ch); |
| 3287 | if (!expr1) { |
| 3288 | return NULL; |
| 3289 | } |
| 3290 | switch (expr1->kind) { |
| 3291 | case Name_kind: |
| 3292 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3293 | return NULL; |
| 3294 | } |
| 3295 | expr1->v.Name.ctx = Store; |
| 3296 | break; |
| 3297 | case Attribute_kind: |
| 3298 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3299 | return NULL; |
| 3300 | } |
| 3301 | expr1->v.Attribute.ctx = Store; |
| 3302 | break; |
| 3303 | case Subscript_kind: |
| 3304 | expr1->v.Subscript.ctx = Store; |
| 3305 | break; |
| 3306 | case List_kind: |
| 3307 | ast_error(c, ch, |
| 3308 | "only single target (not list) can be annotated"); |
| 3309 | return NULL; |
| 3310 | case Tuple_kind: |
| 3311 | ast_error(c, ch, |
| 3312 | "only single target (not tuple) can be annotated"); |
| 3313 | return NULL; |
| 3314 | default: |
| 3315 | ast_error(c, ch, |
| 3316 | "illegal target for annotation"); |
| 3317 | return NULL; |
| 3318 | } |
| 3319 | |
| 3320 | if (expr1->kind != Name_kind) { |
| 3321 | simple = 0; |
| 3322 | } |
| 3323 | ch = CHILD(ann, 1); |
| 3324 | expr2 = ast_for_expr(c, ch); |
| 3325 | if (!expr2) { |
| 3326 | return NULL; |
| 3327 | } |
| 3328 | if (NCH(ann) == 2) { |
| 3329 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3330 | LINENO(n), n->n_col_offset, |
| 3331 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3332 | } |
| 3333 | else { |
| 3334 | ch = CHILD(ann, 3); |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3335 | if (TYPE(ch) == testlist) { |
| 3336 | expr3 = ast_for_testlist(c, ch); |
| 3337 | } |
| 3338 | else { |
| 3339 | expr3 = ast_for_expr(c, ch); |
| 3340 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3341 | if (!expr3) { |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3345 | LINENO(n), n->n_col_offset, |
| 3346 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3347 | } |
| 3348 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3349 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3350 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3351 | asdl_seq *targets; |
| 3352 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3353 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3354 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3355 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3356 | /* a normal assignment */ |
| 3357 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3358 | |
| 3359 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3360 | nch_minus_type = num - has_type_comment; |
| 3361 | |
| 3362 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3363 | if (!targets) |
| 3364 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3365 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3366 | expr_ty e; |
| 3367 | node *ch = CHILD(n, i); |
| 3368 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3369 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3370 | return NULL; |
| 3371 | } |
| 3372 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3374 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3375 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3376 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3377 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3378 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3379 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3380 | asdl_seq_SET(targets, i / 2, e); |
| 3381 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3382 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3383 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3384 | expression = ast_for_testlist(c, value); |
| 3385 | else |
| 3386 | expression = ast_for_expr(c, value); |
| 3387 | if (!expression) |
| 3388 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3389 | if (has_type_comment) { |
| 3390 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3391 | if (!type_comment) |
| 3392 | return NULL; |
| 3393 | } |
| 3394 | else |
| 3395 | type_comment = NULL; |
| 3396 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3397 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3398 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3401 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3402 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3403 | 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] | 3404 | { |
| 3405 | asdl_seq *seq; |
| 3406 | int i; |
| 3407 | expr_ty e; |
| 3408 | |
| 3409 | REQ(n, exprlist); |
| 3410 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3411 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3412 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3413 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3414 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3415 | e = ast_for_expr(c, CHILD(n, i)); |
| 3416 | if (!e) |
| 3417 | return NULL; |
| 3418 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3419 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3420 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3421 | } |
| 3422 | return seq; |
| 3423 | } |
| 3424 | |
| 3425 | static stmt_ty |
| 3426 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3427 | { |
| 3428 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3430 | /* del_stmt: 'del' exprlist */ |
| 3431 | REQ(n, del_stmt); |
| 3432 | |
| 3433 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3434 | if (!expr_list) |
| 3435 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3436 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3437 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | static stmt_ty |
| 3441 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3442 | { |
| 3443 | /* |
| 3444 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3445 | | yield_stmt |
| 3446 | break_stmt: 'break' |
| 3447 | continue_stmt: 'continue' |
| 3448 | return_stmt: 'return' [testlist] |
| 3449 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3450 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3451 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3452 | */ |
| 3453 | node *ch; |
| 3454 | |
| 3455 | REQ(n, flow_stmt); |
| 3456 | ch = CHILD(n, 0); |
| 3457 | switch (TYPE(ch)) { |
| 3458 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3459 | return Break(LINENO(n), n->n_col_offset, |
| 3460 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3461 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3462 | return Continue(LINENO(n), n->n_col_offset, |
| 3463 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3464 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3465 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3466 | if (!exp) |
| 3467 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3468 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3469 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3470 | } |
| 3471 | case return_stmt: |
| 3472 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3473 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3474 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3476 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | if (!expression) |
| 3478 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3479 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3480 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3481 | } |
| 3482 | case raise_stmt: |
| 3483 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3484 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3485 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3486 | else if (NCH(ch) >= 2) { |
| 3487 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3488 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3489 | if (!expression) |
| 3490 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3491 | if (NCH(ch) == 4) { |
| 3492 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3493 | if (!cause) |
| 3494 | return NULL; |
| 3495 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3496 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3497 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3498 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3499 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3500 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3501 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3502 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3503 | return NULL; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3508 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3509 | { |
| 3510 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3511 | import_as_name: NAME ['as' NAME] |
| 3512 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3513 | dotted_name: NAME ('.' NAME)* |
| 3514 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3515 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3516 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3517 | loop: |
| 3518 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3519 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3520 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3521 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3522 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3523 | if (!name) |
| 3524 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3525 | if (NCH(n) == 3) { |
| 3526 | node *str_node = CHILD(n, 2); |
| 3527 | str = NEW_IDENTIFIER(str_node); |
| 3528 | if (!str) |
| 3529 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3530 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3531 | return NULL; |
| 3532 | } |
| 3533 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3534 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3535 | return NULL; |
| 3536 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3537 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3538 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | case dotted_as_name: |
| 3540 | if (NCH(n) == 1) { |
| 3541 | n = CHILD(n, 0); |
| 3542 | goto loop; |
| 3543 | } |
| 3544 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3545 | node *asname_node = CHILD(n, 2); |
| 3546 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3547 | if (!a) |
| 3548 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3550 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3551 | if (!a->asname) |
| 3552 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3553 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3554 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3555 | return a; |
| 3556 | } |
| 3557 | break; |
| 3558 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3559 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3560 | node *name_node = CHILD(n, 0); |
| 3561 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3562 | if (!name) |
| 3563 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3564 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3565 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3566 | return alias(name, NULL, c->c_arena); |
| 3567 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3568 | else { |
| 3569 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3570 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3571 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3572 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3574 | |
| 3575 | len = 0; |
| 3576 | for (i = 0; i < NCH(n); i += 2) |
| 3577 | /* length of string plus one for the dot */ |
| 3578 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3579 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3580 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3581 | if (!str) |
| 3582 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3583 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | if (!s) |
| 3585 | return NULL; |
| 3586 | for (i = 0; i < NCH(n); i += 2) { |
| 3587 | char *sch = STR(CHILD(n, i)); |
| 3588 | strcpy(s, STR(CHILD(n, i))); |
| 3589 | s += strlen(sch); |
| 3590 | *s++ = '.'; |
| 3591 | } |
| 3592 | --s; |
| 3593 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3595 | PyBytes_GET_SIZE(str), |
| 3596 | NULL); |
| 3597 | Py_DECREF(str); |
| 3598 | if (!uni) |
| 3599 | return NULL; |
| 3600 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3601 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3602 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3603 | Py_DECREF(str); |
| 3604 | return NULL; |
| 3605 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3606 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3607 | } |
| 3608 | break; |
| 3609 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3610 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3611 | if (!str) |
| 3612 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3613 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3614 | Py_DECREF(str); |
| 3615 | return NULL; |
| 3616 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3617 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3618 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3619 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3620 | "unexpected import name: %d", TYPE(n)); |
| 3621 | return NULL; |
| 3622 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3623 | |
| 3624 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3625 | return NULL; |
| 3626 | } |
| 3627 | |
| 3628 | static stmt_ty |
| 3629 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3630 | { |
| 3631 | /* |
| 3632 | import_stmt: import_name | import_from |
| 3633 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3634 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3635 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3636 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3637 | int lineno; |
| 3638 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | int i; |
| 3640 | asdl_seq *aliases; |
| 3641 | |
| 3642 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3643 | lineno = LINENO(n); |
| 3644 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3646 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3647 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3648 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3649 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3650 | if (!aliases) |
| 3651 | return NULL; |
| 3652 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3653 | 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] | 3654 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3655 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3656 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3657 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3658 | // Even though n is modified above, the end position is not changed |
| 3659 | return Import(aliases, lineno, col_offset, |
| 3660 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3661 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3662 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3663 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3664 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3665 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3666 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3667 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3669 | /* Count the number of dots (for relative imports) and check for the |
| 3670 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3671 | for (idx = 1; idx < NCH(n); idx++) { |
| 3672 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3673 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3674 | if (!mod) |
| 3675 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3676 | idx++; |
| 3677 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3678 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3679 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3680 | ndots += 3; |
| 3681 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3682 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3683 | break; |
| 3684 | } |
| 3685 | ndots++; |
| 3686 | } |
| 3687 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3688 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3689 | case STAR: |
| 3690 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3691 | n = CHILD(n, idx); |
| 3692 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3693 | break; |
| 3694 | case LPAR: |
| 3695 | /* from ... import (x, y, z) */ |
| 3696 | n = CHILD(n, idx + 1); |
| 3697 | n_children = NCH(n); |
| 3698 | break; |
| 3699 | case import_as_names: |
| 3700 | /* from ... import x, y, z */ |
| 3701 | n = CHILD(n, idx); |
| 3702 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3703 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3704 | ast_error(c, n, |
| 3705 | "trailing comma not allowed without" |
| 3706 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3707 | return NULL; |
| 3708 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3711 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3712 | return NULL; |
| 3713 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3714 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3715 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3716 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3717 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3718 | |
| 3719 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3720 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3721 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3722 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3723 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3724 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3726 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3727 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3728 | 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] | 3729 | if (!import_alias) |
| 3730 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3731 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3732 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3733 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3734 | if (mod != NULL) |
| 3735 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3736 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3737 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3738 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3739 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3740 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3741 | "unknown import statement: starts with command '%s'", |
| 3742 | STR(CHILD(n, 0))); |
| 3743 | return NULL; |
| 3744 | } |
| 3745 | |
| 3746 | static stmt_ty |
| 3747 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3748 | { |
| 3749 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3750 | identifier name; |
| 3751 | asdl_seq *s; |
| 3752 | int i; |
| 3753 | |
| 3754 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3755 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3756 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3757 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3758 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3759 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3760 | if (!name) |
| 3761 | return NULL; |
| 3762 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3763 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3764 | return Global(s, LINENO(n), n->n_col_offset, |
| 3765 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3769 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3770 | { |
| 3771 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3772 | identifier name; |
| 3773 | asdl_seq *s; |
| 3774 | int i; |
| 3775 | |
| 3776 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3777 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3778 | if (!s) |
| 3779 | return NULL; |
| 3780 | for (i = 1; i < NCH(n); i += 2) { |
| 3781 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3782 | if (!name) |
| 3783 | return NULL; |
| 3784 | asdl_seq_SET(s, i / 2, name); |
| 3785 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3786 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3787 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3791 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3792 | { |
| 3793 | /* assert_stmt: 'assert' test [',' test] */ |
| 3794 | REQ(n, assert_stmt); |
| 3795 | if (NCH(n) == 2) { |
| 3796 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3797 | if (!expression) |
| 3798 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3799 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3800 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3801 | } |
| 3802 | else if (NCH(n) == 4) { |
| 3803 | expr_ty expr1, expr2; |
| 3804 | |
| 3805 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3806 | if (!expr1) |
| 3807 | return NULL; |
| 3808 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3809 | if (!expr2) |
| 3810 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3811 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3812 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3813 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3814 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3815 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3816 | "improper number of parts to 'assert' statement: %d", |
| 3817 | NCH(n)); |
| 3818 | return NULL; |
| 3819 | } |
| 3820 | |
| 3821 | static asdl_seq * |
| 3822 | ast_for_suite(struct compiling *c, const node *n) |
| 3823 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3824 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3825 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3826 | stmt_ty s; |
| 3827 | int i, total, num, end, pos = 0; |
| 3828 | node *ch; |
| 3829 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3830 | if (TYPE(n) != func_body_suite) { |
| 3831 | REQ(n, suite); |
| 3832 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3833 | |
| 3834 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3835 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3836 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3837 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3838 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3839 | n = CHILD(n, 0); |
| 3840 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3841 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3842 | */ |
| 3843 | end = NCH(n) - 1; |
| 3844 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3845 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3846 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3847 | for (i = 0; i < end; i += 2) { |
| 3848 | ch = CHILD(n, i); |
| 3849 | s = ast_for_stmt(c, ch); |
| 3850 | if (!s) |
| 3851 | return NULL; |
| 3852 | asdl_seq_SET(seq, pos++, s); |
| 3853 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3854 | } |
| 3855 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3856 | i = 2; |
| 3857 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3858 | i += 2; |
| 3859 | REQ(CHILD(n, 2), NEWLINE); |
| 3860 | } |
| 3861 | |
| 3862 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3863 | ch = CHILD(n, i); |
| 3864 | REQ(ch, stmt); |
| 3865 | num = num_stmts(ch); |
| 3866 | if (num == 1) { |
| 3867 | /* small_stmt or compound_stmt with only one child */ |
| 3868 | s = ast_for_stmt(c, ch); |
| 3869 | if (!s) |
| 3870 | return NULL; |
| 3871 | asdl_seq_SET(seq, pos++, s); |
| 3872 | } |
| 3873 | else { |
| 3874 | int j; |
| 3875 | ch = CHILD(ch, 0); |
| 3876 | REQ(ch, simple_stmt); |
| 3877 | for (j = 0; j < NCH(ch); j += 2) { |
| 3878 | /* statement terminates with a semi-colon ';' */ |
| 3879 | if (NCH(CHILD(ch, j)) == 0) { |
| 3880 | assert((j + 1) == NCH(ch)); |
| 3881 | break; |
| 3882 | } |
| 3883 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3884 | if (!s) |
| 3885 | return NULL; |
| 3886 | asdl_seq_SET(seq, pos++, s); |
| 3887 | } |
| 3888 | } |
| 3889 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3890 | } |
| 3891 | assert(pos == seq->size); |
| 3892 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3895 | static void |
| 3896 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3897 | { |
Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame] | 3898 | Py_ssize_t tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3899 | // There must be no empty suites. |
| 3900 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3901 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3902 | *end_lineno = last->end_lineno; |
| 3903 | *end_col_offset = last->end_col_offset; |
| 3904 | } |
| 3905 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3906 | static stmt_ty |
| 3907 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3908 | { |
| 3909 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3910 | ['else' ':' suite] |
| 3911 | */ |
| 3912 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3913 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3914 | |
| 3915 | REQ(n, if_stmt); |
| 3916 | |
| 3917 | if (NCH(n) == 4) { |
| 3918 | expr_ty expression; |
| 3919 | asdl_seq *suite_seq; |
| 3920 | |
| 3921 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3922 | if (!expression) |
| 3923 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3924 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3925 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3926 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3927 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3928 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3929 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3930 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3931 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3932 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3933 | s = STR(CHILD(n, 4)); |
| 3934 | /* s[2], the third character in the string, will be |
| 3935 | 's' for el_s_e, or |
| 3936 | 'i' for el_i_f |
| 3937 | */ |
| 3938 | if (s[2] == 's') { |
| 3939 | expr_ty expression; |
| 3940 | asdl_seq *seq1, *seq2; |
| 3941 | |
| 3942 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3943 | if (!expression) |
| 3944 | return NULL; |
| 3945 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3946 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3947 | return NULL; |
| 3948 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3949 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3950 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3951 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3952 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3953 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3954 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3955 | } |
| 3956 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3957 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3958 | expr_ty expression; |
| 3959 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3960 | asdl_seq *orelse = NULL; |
| 3961 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3962 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3963 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3964 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3965 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3966 | has_else = 1; |
| 3967 | n_elif -= 3; |
| 3968 | } |
| 3969 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3970 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3971 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3972 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3973 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3974 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3975 | if (!orelse) |
| 3976 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3977 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3978 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3979 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3980 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3981 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3982 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3983 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3984 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3985 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3986 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | asdl_seq_SET(orelse, 0, |
| 3989 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3990 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3991 | CHILD(n, NCH(n) - 6)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3992 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3993 | /* the just-created orelse handled the last elif */ |
| 3994 | n_elif--; |
| 3995 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3996 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3997 | for (i = 0; i < n_elif; i++) { |
| 3998 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3999 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4000 | if (!newobj) |
| 4001 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4002 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4003 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4004 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4005 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4006 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4007 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4008 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4009 | if (orelse != NULL) { |
| 4010 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4011 | } else { |
| 4012 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4013 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4014 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4015 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4016 | LINENO(CHILD(n, off)), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4017 | CHILD(n, off)->n_col_offset, |
| 4018 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4019 | orelse = newobj; |
| 4020 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4021 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4022 | if (!expression) |
| 4023 | return NULL; |
| 4024 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 4025 | if (!suite_seq) |
| 4026 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4027 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4028 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4029 | LINENO(n), n->n_col_offset, |
| 4030 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4031 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4032 | |
| 4033 | PyErr_Format(PyExc_SystemError, |
| 4034 | "unexpected token in 'if' statement: %s", s); |
| 4035 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | static stmt_ty |
| 4039 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 4040 | { |
| 4041 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 4042 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4043 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4044 | |
| 4045 | if (NCH(n) == 4) { |
| 4046 | expr_ty expression; |
| 4047 | asdl_seq *suite_seq; |
| 4048 | |
| 4049 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4050 | if (!expression) |
| 4051 | return NULL; |
| 4052 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4053 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4054 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4055 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4056 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4057 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4058 | } |
| 4059 | else if (NCH(n) == 7) { |
| 4060 | expr_ty expression; |
| 4061 | asdl_seq *seq1, *seq2; |
| 4062 | |
| 4063 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4064 | if (!expression) |
| 4065 | return NULL; |
| 4066 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4067 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4068 | return NULL; |
| 4069 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4070 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4071 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4072 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4073 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4074 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4075 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4076 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4077 | |
| 4078 | PyErr_Format(PyExc_SystemError, |
| 4079 | "wrong number of tokens for 'while' statement: %d", |
| 4080 | NCH(n)); |
| 4081 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4085 | 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] | 4086 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4087 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4088 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4089 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4090 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4091 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4092 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4093 | int has_type_comment; |
| 4094 | string type_comment; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4095 | |
| 4096 | if (is_async && c->c_feature_version < 5) { |
| 4097 | ast_error(c, n, |
| 4098 | "Async for loops are only supported in Python 3.5 and greater"); |
| 4099 | return NULL; |
| 4100 | } |
| 4101 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4102 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4103 | REQ(n, for_stmt); |
| 4104 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4105 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4106 | |
| 4107 | if (NCH(n) == 9 + has_type_comment) { |
| 4108 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4109 | if (!seq) |
| 4110 | return NULL; |
| 4111 | } |
| 4112 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4113 | node_target = CHILD(n, 1); |
| 4114 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4115 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4116 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4117 | /* Check the # of children rather than the length of _target, since |
| 4118 | 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] | 4119 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4120 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4121 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4122 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4123 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4124 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4125 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4126 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4127 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4128 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4129 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4130 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4131 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4132 | return NULL; |
| 4133 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4134 | if (seq != NULL) { |
| 4135 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4136 | } else { |
| 4137 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4138 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4139 | |
| 4140 | if (has_type_comment) { |
| 4141 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4142 | if (!type_comment) |
| 4143 | return NULL; |
| 4144 | } |
| 4145 | else |
| 4146 | type_comment = NULL; |
| 4147 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4148 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4149 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4150 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4151 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4152 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4153 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4154 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4155 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | static excepthandler_ty |
| 4159 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4160 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4161 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4162 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4163 | REQ(exc, except_clause); |
| 4164 | REQ(body, suite); |
| 4165 | |
| 4166 | if (NCH(exc) == 1) { |
| 4167 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4168 | if (!suite_seq) |
| 4169 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4170 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4171 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4172 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4173 | exc->n_col_offset, |
| 4174 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4175 | } |
| 4176 | else if (NCH(exc) == 2) { |
| 4177 | expr_ty expression; |
| 4178 | asdl_seq *suite_seq; |
| 4179 | |
| 4180 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4181 | if (!expression) |
| 4182 | return NULL; |
| 4183 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4184 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4185 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4186 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4187 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4188 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4189 | exc->n_col_offset, |
| 4190 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4191 | } |
| 4192 | else if (NCH(exc) == 4) { |
| 4193 | asdl_seq *suite_seq; |
| 4194 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4195 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4196 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4197 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4198 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4199 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4200 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4201 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4202 | return NULL; |
| 4203 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4204 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4205 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4206 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4207 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4208 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4209 | exc->n_col_offset, |
| 4210 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4211 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4212 | |
| 4213 | PyErr_Format(PyExc_SystemError, |
| 4214 | "wrong number of children for 'except' clause: %d", |
| 4215 | NCH(exc)); |
| 4216 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | static stmt_ty |
| 4220 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4221 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4222 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4223 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4224 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4225 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4226 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4227 | REQ(n, try_stmt); |
| 4228 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4229 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4230 | if (body == NULL) |
| 4231 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4232 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4233 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4234 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4235 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4236 | /* we can assume it's an "else", |
| 4237 | because nch >= 9 for try-else-finally and |
| 4238 | it would otherwise have a type of except_clause */ |
| 4239 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4240 | if (orelse == NULL) |
| 4241 | return NULL; |
| 4242 | n_except--; |
| 4243 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4244 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4245 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4246 | if (finally == NULL) |
| 4247 | return NULL; |
| 4248 | n_except--; |
| 4249 | } |
| 4250 | else { |
| 4251 | /* we can assume it's an "else", |
| 4252 | otherwise it would have a type of except_clause */ |
| 4253 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4254 | if (orelse == NULL) |
| 4255 | return NULL; |
| 4256 | n_except--; |
| 4257 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4258 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4259 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4260 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4261 | return NULL; |
| 4262 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4264 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4265 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4266 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4267 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4268 | if (handlers == NULL) |
| 4269 | return NULL; |
| 4270 | |
| 4271 | for (i = 0; i < n_except; i++) { |
| 4272 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4273 | CHILD(n, 5 + i * 3)); |
| 4274 | if (!e) |
| 4275 | return NULL; |
| 4276 | asdl_seq_SET(handlers, i, e); |
| 4277 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4280 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4281 | if (finally != NULL) { |
| 4282 | // finally is always last |
| 4283 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4284 | } else if (orelse != NULL) { |
| 4285 | // otherwise else is last |
| 4286 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4287 | } else { |
| 4288 | // inline the get_last_end_pos logic due to layout mismatch |
| 4289 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4290 | end_lineno = last_handler->end_lineno; |
| 4291 | end_col_offset = last_handler->end_col_offset; |
| 4292 | } |
| 4293 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4294 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4297 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4298 | static withitem_ty |
| 4299 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4300 | { |
| 4301 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4302 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4303 | REQ(n, with_item); |
| 4304 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4305 | if (!context_expr) |
| 4306 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4307 | if (NCH(n) == 3) { |
| 4308 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4309 | |
| 4310 | if (!optional_vars) { |
| 4311 | return NULL; |
| 4312 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4313 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4314 | return NULL; |
| 4315 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4318 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4321 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4322 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4323 | 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] | 4324 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4325 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4326 | int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4327 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4328 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4329 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4330 | if (is_async && c->c_feature_version < 5) { |
| 4331 | ast_error(c, n, |
| 4332 | "Async with statements are only supported in Python 3.5 and greater"); |
| 4333 | return NULL; |
| 4334 | } |
| 4335 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4336 | REQ(n, with_stmt); |
| 4337 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4338 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4339 | nch_minus_type = NCH(n) - has_type_comment; |
| 4340 | |
| 4341 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4342 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4343 | if (!items) |
| 4344 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4345 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4346 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4347 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4348 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4349 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4352 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4353 | if (!body) |
| 4354 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4355 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4356 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4357 | if (has_type_comment) { |
| 4358 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4359 | if (!type_comment) |
| 4360 | return NULL; |
| 4361 | } |
| 4362 | else |
| 4363 | type_comment = NULL; |
| 4364 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4365 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4366 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4367 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4368 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4369 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4370 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4373 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4374 | 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] | 4375 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4376 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4377 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4378 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4379 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4380 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4381 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4382 | REQ(n, classdef); |
| 4383 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4384 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4385 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4386 | if (!s) |
| 4387 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4388 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4389 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4390 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4391 | if (!classname) |
| 4392 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4393 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4394 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4395 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4396 | LINENO(n), n->n_col_offset, |
| 4397 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4398 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4399 | |
| 4400 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4401 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4402 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4403 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4404 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4405 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4406 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4407 | if (!classname) |
| 4408 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4409 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4410 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4411 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4412 | LINENO(n), n->n_col_offset, |
| 4413 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4416 | /* class NAME '(' arglist ')' ':' suite */ |
| 4417 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4418 | { |
| 4419 | PyObject *dummy_name; |
| 4420 | expr_ty dummy; |
| 4421 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4422 | if (!dummy_name) |
| 4423 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4424 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4425 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4426 | c->c_arena); |
| 4427 | call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4)); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4428 | if (!call) |
| 4429 | return NULL; |
| 4430 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4431 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4432 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4433 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4434 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4435 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4436 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4437 | if (!classname) |
| 4438 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4439 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4440 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4441 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4442 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4443 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4444 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | static stmt_ty |
| 4448 | ast_for_stmt(struct compiling *c, const node *n) |
| 4449 | { |
| 4450 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4451 | assert(NCH(n) == 1); |
| 4452 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4453 | } |
| 4454 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4455 | assert(num_stmts(n) == 1); |
| 4456 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4457 | } |
| 4458 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4459 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4460 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4461 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4462 | */ |
| 4463 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4464 | case expr_stmt: |
| 4465 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4466 | case del_stmt: |
| 4467 | return ast_for_del_stmt(c, n); |
| 4468 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4469 | return Pass(LINENO(n), n->n_col_offset, |
| 4470 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4471 | case flow_stmt: |
| 4472 | return ast_for_flow_stmt(c, n); |
| 4473 | case import_stmt: |
| 4474 | return ast_for_import_stmt(c, n); |
| 4475 | case global_stmt: |
| 4476 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4477 | case nonlocal_stmt: |
| 4478 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4479 | case assert_stmt: |
| 4480 | return ast_for_assert_stmt(c, n); |
| 4481 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4482 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4483 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4484 | TYPE(n), NCH(n)); |
| 4485 | return NULL; |
| 4486 | } |
| 4487 | } |
| 4488 | else { |
| 4489 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4490 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4491 | */ |
| 4492 | node *ch = CHILD(n, 0); |
| 4493 | REQ(n, compound_stmt); |
| 4494 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4495 | case if_stmt: |
| 4496 | return ast_for_if_stmt(c, ch); |
| 4497 | case while_stmt: |
| 4498 | return ast_for_while_stmt(c, ch); |
| 4499 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4500 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4501 | case try_stmt: |
| 4502 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4503 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4504 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4505 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4506 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4507 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4508 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | case decorated: |
| 4510 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4511 | case async_stmt: |
| 4512 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4513 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4514 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4515 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4516 | TYPE(n), NCH(n)); |
| 4517 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4518 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4523 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4524 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4525 | const char *end; |
| 4526 | long x; |
| 4527 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4528 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4529 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4530 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4531 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4532 | errno = 0; |
| 4533 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4534 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4535 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4536 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4537 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4538 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4539 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4540 | } |
| 4541 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4542 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4543 | if (*end == '\0') { |
| 4544 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4545 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4546 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4547 | } |
| 4548 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4549 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4550 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4551 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4552 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4553 | return NULL; |
| 4554 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4555 | } |
| 4556 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4557 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4558 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4559 | if (dx == -1.0 && PyErr_Occurred()) |
| 4560 | return NULL; |
| 4561 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4562 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4566 | parsenumber(struct compiling *c, const char *s) |
| 4567 | { |
| 4568 | char *dup, *end; |
| 4569 | PyObject *res = NULL; |
| 4570 | |
| 4571 | assert(s != NULL); |
| 4572 | |
| 4573 | if (strchr(s, '_') == NULL) { |
| 4574 | return parsenumber_raw(c, s); |
| 4575 | } |
| 4576 | /* Create a duplicate without underscores. */ |
| 4577 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4578 | if (dup == NULL) { |
| 4579 | return PyErr_NoMemory(); |
| 4580 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4581 | end = dup; |
| 4582 | for (; *s; s++) { |
| 4583 | if (*s != '_') { |
| 4584 | *end++ = *s; |
| 4585 | } |
| 4586 | } |
| 4587 | *end = '\0'; |
| 4588 | res = parsenumber_raw(c, dup); |
| 4589 | PyMem_Free(dup); |
| 4590 | return res; |
| 4591 | } |
| 4592 | |
| 4593 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4594 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4595 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4596 | const char *s, *t; |
| 4597 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4598 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4599 | while (s < end && (*s & 0x80)) s++; |
| 4600 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4601 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4604 | static int |
| 4605 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4606 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4607 | { |
| 4608 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4609 | first_invalid_escape_char); |
| 4610 | if (msg == NULL) { |
| 4611 | return -1; |
| 4612 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4613 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4614 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4615 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4616 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4617 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4618 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4619 | to get a more accurate error report */ |
| 4620 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4621 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4622 | } |
| 4623 | Py_DECREF(msg); |
| 4624 | return -1; |
| 4625 | } |
| 4626 | Py_DECREF(msg); |
| 4627 | return 0; |
| 4628 | } |
| 4629 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4630 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4631 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4632 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4633 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4634 | PyObject *v, *u; |
| 4635 | char *buf; |
| 4636 | char *p; |
| 4637 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4638 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4639 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4640 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4641 | return NULL; |
| 4642 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4643 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4644 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4645 | if (u == NULL) |
| 4646 | return NULL; |
| 4647 | p = buf = PyBytes_AsString(u); |
| 4648 | end = s + len; |
| 4649 | while (s < end) { |
| 4650 | if (*s == '\\') { |
| 4651 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4652 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4653 | strcpy(p, "u005c"); |
| 4654 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4655 | if (s >= end) |
| 4656 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4657 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4658 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4659 | if (*s & 0x80) { /* XXX inefficient */ |
| 4660 | PyObject *w; |
| 4661 | int kind; |
| 4662 | void *data; |
| 4663 | Py_ssize_t len, i; |
| 4664 | w = decode_utf8(c, &s, end); |
| 4665 | if (w == NULL) { |
| 4666 | Py_DECREF(u); |
| 4667 | return NULL; |
| 4668 | } |
| 4669 | kind = PyUnicode_KIND(w); |
| 4670 | data = PyUnicode_DATA(w); |
| 4671 | len = PyUnicode_GET_LENGTH(w); |
| 4672 | for (i = 0; i < len; i++) { |
| 4673 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4674 | sprintf(p, "\\U%08x", chr); |
| 4675 | p += 10; |
| 4676 | } |
| 4677 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4678 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4679 | Py_DECREF(w); |
| 4680 | } else { |
| 4681 | *p++ = *s++; |
| 4682 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4683 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4684 | len = p - buf; |
| 4685 | s = buf; |
| 4686 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4687 | const char *first_invalid_escape; |
| 4688 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4689 | |
| 4690 | if (v != NULL && first_invalid_escape != NULL) { |
| 4691 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4692 | /* We have not decref u before because first_invalid_escape points |
| 4693 | inside u. */ |
| 4694 | Py_XDECREF(u); |
| 4695 | Py_DECREF(v); |
| 4696 | return NULL; |
| 4697 | } |
| 4698 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4699 | Py_XDECREF(u); |
| 4700 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4703 | static PyObject * |
| 4704 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4705 | size_t len) |
| 4706 | { |
| 4707 | const char *first_invalid_escape; |
| 4708 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4709 | &first_invalid_escape); |
| 4710 | if (result == NULL) |
| 4711 | return NULL; |
| 4712 | |
| 4713 | if (first_invalid_escape != NULL) { |
| 4714 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4715 | Py_DECREF(result); |
| 4716 | return NULL; |
| 4717 | } |
| 4718 | } |
| 4719 | return result; |
| 4720 | } |
| 4721 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4722 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4723 | and `col_offset` to existing locations. */ |
| 4724 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4725 | { |
| 4726 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4727 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4728 | for (int i = 0; i < NCH(n); ++i) { |
| 4729 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4730 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4731 | col_offset = 0; |
| 4732 | } |
| 4733 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4734 | } |
| 4735 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4736 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | /* Fix locations for the given node and its children. |
| 4740 | |
| 4741 | `parent` is the enclosing node. |
| 4742 | `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] | 4743 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4744 | */ |
| 4745 | static void |
| 4746 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4747 | { |
| 4748 | char *substr = NULL; |
| 4749 | char *start; |
| 4750 | int lines = LINENO(parent) - 1; |
| 4751 | int cols = parent->n_col_offset; |
| 4752 | /* Find the full fstring to fix location information in `n`. */ |
| 4753 | while (parent && parent->n_type != STRING) |
| 4754 | parent = parent->n_child; |
| 4755 | if (parent && parent->n_str) { |
| 4756 | substr = strstr(parent->n_str, expr_str); |
| 4757 | if (substr) { |
| 4758 | start = substr; |
| 4759 | while (start > parent->n_str) { |
| 4760 | if (start[0] == '\n') |
| 4761 | break; |
| 4762 | start--; |
| 4763 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4764 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4765 | /* adjust the start based on the number of newlines encountered |
| 4766 | before the f-string expression */ |
| 4767 | for (char* p = parent->n_str; p < substr; p++) { |
| 4768 | if (*p == '\n') { |
| 4769 | lines++; |
| 4770 | } |
| 4771 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4772 | } |
| 4773 | } |
| 4774 | fstring_shift_node_locations(n, lines, cols); |
| 4775 | } |
| 4776 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4777 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4778 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4779 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4780 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4781 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4782 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4783 | { |
| 4784 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4785 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4786 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4787 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4788 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4789 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4790 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4791 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4792 | assert(*(expr_start-1) == '{'); |
| 4793 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4794 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4795 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4796 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4797 | because turning the expression '' in to '()' would go from being invalid |
| 4798 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4799 | for (s = expr_start; s != expr_end; s++) { |
| 4800 | char c = *s; |
| 4801 | /* The Python parser ignores only the following whitespace |
| 4802 | characters (\r already is converted to \n). */ |
| 4803 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4804 | break; |
| 4805 | } |
| 4806 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4807 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4808 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4809 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4810 | } |
| 4811 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4812 | len = expr_end - expr_start; |
| 4813 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4814 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4815 | if (str == NULL) { |
| 4816 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4817 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4818 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4819 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4820 | str[0] = '('; |
| 4821 | memcpy(str+1, expr_start, len); |
| 4822 | str[len+1] = ')'; |
| 4823 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4824 | |
| 4825 | cf.cf_flags = PyCF_ONLY_AST; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4826 | cf.cf_feature_version = PY_MINOR_VERSION; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4827 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4828 | Py_eval_input, 0); |
| 4829 | if (!mod_n) { |
| 4830 | PyMem_RawFree(str); |
| 4831 | return NULL; |
| 4832 | } |
| 4833 | /* Reuse str to find the correct column offset. */ |
| 4834 | str[0] = '{'; |
| 4835 | str[len+1] = '}'; |
| 4836 | fstring_fix_node_location(n, mod_n, str); |
| 4837 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4838 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4839 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4840 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4841 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4842 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | /* Return -1 on error. |
| 4846 | |
| 4847 | Return 0 if we reached the end of the literal. |
| 4848 | |
| 4849 | Return 1 if we haven't reached the end of the literal, but we want |
| 4850 | the caller to process the literal up to this point. Used for |
| 4851 | doubled braces. |
| 4852 | */ |
| 4853 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4854 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4855 | PyObject **literal, int recurse_lvl, |
| 4856 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4857 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4858 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4859 | brace (which isn't part of a unicode name escape such as |
| 4860 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4861 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4862 | const char *s = *str; |
| 4863 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4864 | int result = 0; |
| 4865 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4866 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4867 | while (s < end) { |
| 4868 | char ch = *s++; |
| 4869 | if (!raw && ch == '\\' && s < end) { |
| 4870 | ch = *s++; |
| 4871 | if (ch == 'N') { |
| 4872 | if (s < end && *s++ == '{') { |
| 4873 | while (s < end && *s++ != '}') { |
| 4874 | } |
| 4875 | continue; |
| 4876 | } |
| 4877 | break; |
| 4878 | } |
| 4879 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4880 | return -1; |
| 4881 | } |
| 4882 | } |
| 4883 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4884 | /* Check for doubled braces, but only at the top level. If |
| 4885 | we checked at every level, then f'{0:{3}}' would fail |
| 4886 | with the two closing braces. */ |
| 4887 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4888 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4889 | /* We're going to tell the caller that the literal ends |
| 4890 | here, but that they should continue scanning. But also |
| 4891 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4892 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4893 | result = 1; |
| 4894 | goto done; |
| 4895 | } |
| 4896 | |
| 4897 | /* Where a single '{' is the start of a new expression, a |
| 4898 | single '}' is not allowed. */ |
| 4899 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4900 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4901 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4902 | return -1; |
| 4903 | } |
| 4904 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4905 | /* We're either at a '{', which means we're starting another |
| 4906 | expression; or a '}', which means we're at the end of this |
| 4907 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4908 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4909 | break; |
| 4910 | } |
| 4911 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4912 | *str = s; |
| 4913 | assert(s <= end); |
| 4914 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4915 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4916 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4917 | if (raw) |
| 4918 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4919 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4920 | NULL, NULL); |
| 4921 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4922 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4923 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4924 | if (!*literal) |
| 4925 | return -1; |
| 4926 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4927 | return result; |
| 4928 | } |
| 4929 | |
| 4930 | /* Forward declaration because parsing is recursive. */ |
| 4931 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4932 | 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] | 4933 | struct compiling *c, const node *n); |
| 4934 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4935 | /* 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] | 4936 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4937 | which includes the expression, conversion character, and |
| 4938 | format_spec expression. |
| 4939 | |
| 4940 | Note that I don't do a perfect job here: I don't make sure that a |
| 4941 | closing brace doesn't match an opening paren, for example. It |
| 4942 | doesn't need to error on all invalid expressions, just correctly |
| 4943 | find the end of all valid ones. Any errors inside the expression |
| 4944 | will be caught when we parse it later. */ |
| 4945 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4946 | 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] | 4947 | expr_ty *expression, struct compiling *c, const node *n) |
| 4948 | { |
| 4949 | /* Return -1 on error, else 0. */ |
| 4950 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4951 | const char *expr_start; |
| 4952 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4953 | expr_ty simple_expression; |
| 4954 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4955 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4956 | |
| 4957 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4958 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4959 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4960 | |
| 4961 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4962 | int string_type = 0; |
| 4963 | |
| 4964 | /* Keep track of nesting level for braces/parens/brackets in |
| 4965 | expressions. */ |
| 4966 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4967 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4968 | |
| 4969 | /* Can only nest one level deep. */ |
| 4970 | if (recurse_lvl >= 2) { |
| 4971 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4972 | return -1; |
| 4973 | } |
| 4974 | |
| 4975 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4976 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4977 | assert(**str == '{'); |
| 4978 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4979 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4980 | expr_start = *str; |
| 4981 | for (; *str < end; (*str)++) { |
| 4982 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4983 | |
| 4984 | /* Loop invariants. */ |
| 4985 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4986 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4987 | if (quote_char) |
| 4988 | assert(string_type == 1 || string_type == 3); |
| 4989 | else |
| 4990 | assert(string_type == 0); |
| 4991 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4992 | ch = **str; |
| 4993 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4994 | if (ch == '\\') { |
| 4995 | /* Error: can't include a backslash character, inside |
| 4996 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4997 | ast_error(c, n, |
| 4998 | "f-string expression part " |
| 4999 | "cannot include a backslash"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5000 | return -1; |
| 5001 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5002 | if (quote_char) { |
| 5003 | /* We're inside a string. See if we're at the end. */ |
| 5004 | /* This code needs to implement the same non-error logic |
| 5005 | as tok_get from tokenizer.c, at the letter_quote |
| 5006 | label. To actually share that code would be a |
| 5007 | nightmare. But, it's unlikely to change and is small, |
| 5008 | so duplicate it here. Note we don't need to catch all |
| 5009 | of the errors, since they'll be caught when parsing the |
| 5010 | expression. We just need to match the non-error |
| 5011 | cases. Thus we can ignore \n in single-quoted strings, |
| 5012 | for example. Or non-terminated strings. */ |
| 5013 | if (ch == quote_char) { |
| 5014 | /* Does this match the string_type (single or triple |
| 5015 | quoted)? */ |
| 5016 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5017 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5018 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5019 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5020 | string_type = 0; |
| 5021 | quote_char = 0; |
| 5022 | continue; |
| 5023 | } |
| 5024 | } else { |
| 5025 | /* We're at the end of a normal string. */ |
| 5026 | quote_char = 0; |
| 5027 | string_type = 0; |
| 5028 | continue; |
| 5029 | } |
| 5030 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5031 | } else if (ch == '\'' || ch == '"') { |
| 5032 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5033 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5034 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5035 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5036 | } else { |
| 5037 | /* Start of a normal string. */ |
| 5038 | string_type = 1; |
| 5039 | } |
| 5040 | /* Start looking for the end of the string. */ |
| 5041 | quote_char = ch; |
| 5042 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5043 | if (nested_depth >= MAXLEVEL) { |
| 5044 | ast_error(c, n, "f-string: too many nested parenthesis"); |
| 5045 | return -1; |
| 5046 | } |
| 5047 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5048 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5049 | } else if (ch == '#') { |
| 5050 | /* Error: can't include a comment character, inside parens |
| 5051 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5052 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5053 | return -1; |
| 5054 | } else if (nested_depth == 0 && |
| 5055 | (ch == '!' || ch == ':' || ch == '}')) { |
| 5056 | /* First, test for the special case of "!=". Since '=' is |
| 5057 | not an allowed conversion character, nothing is lost in |
| 5058 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5059 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5060 | /* This isn't a conversion character, just continue. */ |
| 5061 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5062 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5063 | /* Normal way out of this loop. */ |
| 5064 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5065 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5066 | if (!nested_depth) { |
| 5067 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
| 5068 | return -1; |
| 5069 | } |
| 5070 | nested_depth--; |
| 5071 | int opening = parenstack[nested_depth]; |
| 5072 | if (!((opening == '(' && ch == ')') || |
| 5073 | (opening == '[' && ch == ']') || |
| 5074 | (opening == '{' && ch == '}'))) |
| 5075 | { |
| 5076 | ast_error(c, n, |
| 5077 | "f-string: closing parenthesis '%c' " |
| 5078 | "does not match opening parenthesis '%c'", |
| 5079 | ch, opening); |
| 5080 | return -1; |
| 5081 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5082 | } else { |
| 5083 | /* Just consume this char and loop around. */ |
| 5084 | } |
| 5085 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5086 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5087 | /* If we leave this loop in a string or with mismatched parens, we |
| 5088 | don't care. We'll get a syntax error when compiling the |
| 5089 | expression. But, we can produce a better error message, so |
| 5090 | let's just do that.*/ |
| 5091 | if (quote_char) { |
| 5092 | ast_error(c, n, "f-string: unterminated string"); |
| 5093 | return -1; |
| 5094 | } |
| 5095 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5096 | int opening = parenstack[nested_depth - 1]; |
| 5097 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5098 | return -1; |
| 5099 | } |
| 5100 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5101 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5102 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5103 | |
| 5104 | /* Compile the expression as soon as possible, so we show errors |
| 5105 | related to the expression before errors related to the |
| 5106 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5107 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5108 | if (!simple_expression) |
| 5109 | return -1; |
| 5110 | |
| 5111 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5112 | if (**str == '!') { |
| 5113 | *str += 1; |
| 5114 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5115 | goto unexpected_end_of_string; |
| 5116 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5117 | conversion = **str; |
| 5118 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5119 | |
| 5120 | /* Validate the conversion. */ |
| 5121 | if (!(conversion == 's' || conversion == 'r' |
| 5122 | || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5123 | ast_error(c, n, |
| 5124 | "f-string: invalid conversion character: " |
| 5125 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5126 | return -1; |
| 5127 | } |
| 5128 | } |
| 5129 | |
| 5130 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5131 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5132 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5133 | if (**str == ':') { |
| 5134 | *str += 1; |
| 5135 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5136 | goto unexpected_end_of_string; |
| 5137 | |
| 5138 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5139 | 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] | 5140 | if (!format_spec) |
| 5141 | return -1; |
| 5142 | } |
| 5143 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5144 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5145 | goto unexpected_end_of_string; |
| 5146 | |
| 5147 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5148 | assert(*str < end); |
| 5149 | assert(**str == '}'); |
| 5150 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5151 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5152 | /* And now create the FormattedValue node that represents this |
| 5153 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5154 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5155 | format_spec, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5156 | n->n_end_lineno, n->n_end_col_offset, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5157 | c->c_arena); |
| 5158 | if (!*expression) |
| 5159 | return -1; |
| 5160 | |
| 5161 | return 0; |
| 5162 | |
| 5163 | unexpected_end_of_string: |
| 5164 | ast_error(c, n, "f-string: expecting '}'"); |
| 5165 | return -1; |
| 5166 | } |
| 5167 | |
| 5168 | /* Return -1 on error. |
| 5169 | |
| 5170 | Return 0 if we have a literal (possible zero length) and an |
| 5171 | expression (zero length if at the end of the string. |
| 5172 | |
| 5173 | Return 1 if we have a literal, but no expression, and we want the |
| 5174 | caller to call us again. This is used to deal with doubled |
| 5175 | braces. |
| 5176 | |
| 5177 | When called multiple times on the string 'a{{b{0}c', this function |
| 5178 | will return: |
| 5179 | |
| 5180 | 1. the literal 'a{' with no expression, and a return value |
| 5181 | of 1. Despite the fact that there's no expression, the return |
| 5182 | value of 1 means we're not finished yet. |
| 5183 | |
| 5184 | 2. the literal 'b' and the expression '0', with a return value of |
| 5185 | 0. The fact that there's an expression means we're not finished. |
| 5186 | |
| 5187 | 3. literal 'c' with no expression and a return value of 0. The |
| 5188 | combination of the return value of 0 with no expression means |
| 5189 | we're finished. |
| 5190 | */ |
| 5191 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5192 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5193 | int recurse_lvl, PyObject **literal, |
| 5194 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5195 | struct compiling *c, const node *n) |
| 5196 | { |
| 5197 | int result; |
| 5198 | |
| 5199 | assert(*literal == NULL && *expression == NULL); |
| 5200 | |
| 5201 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5202 | 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] | 5203 | if (result < 0) |
| 5204 | goto error; |
| 5205 | |
| 5206 | assert(result == 0 || result == 1); |
| 5207 | |
| 5208 | if (result == 1) |
| 5209 | /* We have a literal, but don't look at the expression. */ |
| 5210 | return 1; |
| 5211 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5212 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5213 | /* We're at the end of the string or the end of a nested |
| 5214 | f-string: no expression. The top-level error case where we |
| 5215 | expect to be at the end of the string but we're at a '}' is |
| 5216 | handled later. */ |
| 5217 | return 0; |
| 5218 | |
| 5219 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5220 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5221 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5222 | 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] | 5223 | goto error; |
| 5224 | |
| 5225 | return 0; |
| 5226 | |
| 5227 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5228 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5229 | return -1; |
| 5230 | } |
| 5231 | |
| 5232 | #define EXPRLIST_N_CACHED 64 |
| 5233 | |
| 5234 | typedef struct { |
| 5235 | /* Incrementally build an array of expr_ty, so be used in an |
| 5236 | asdl_seq. Cache some small but reasonably sized number of |
| 5237 | expr_ty's, and then after that start dynamically allocating, |
| 5238 | doubling the number allocated each time. Note that the f-string |
| 5239 | 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] | 5240 | 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] | 5241 | fast as you add exressions in an f-string. */ |
| 5242 | |
| 5243 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5244 | Py_ssize_t size; /* Number we've used. */ |
| 5245 | expr_ty *p; /* Pointer to the memory we're actually |
| 5246 | using. Will point to 'data' until we |
| 5247 | start dynamically allocating. */ |
| 5248 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5249 | } ExprList; |
| 5250 | |
| 5251 | #ifdef NDEBUG |
| 5252 | #define ExprList_check_invariants(l) |
| 5253 | #else |
| 5254 | static void |
| 5255 | ExprList_check_invariants(ExprList *l) |
| 5256 | { |
| 5257 | /* Check our invariants. Make sure this object is "live", and |
| 5258 | hasn't been deallocated. */ |
| 5259 | assert(l->size >= 0); |
| 5260 | assert(l->p != NULL); |
| 5261 | if (l->size <= EXPRLIST_N_CACHED) |
| 5262 | assert(l->data == l->p); |
| 5263 | } |
| 5264 | #endif |
| 5265 | |
| 5266 | static void |
| 5267 | ExprList_Init(ExprList *l) |
| 5268 | { |
| 5269 | l->allocated = EXPRLIST_N_CACHED; |
| 5270 | l->size = 0; |
| 5271 | |
| 5272 | /* Until we start allocating dynamically, p points to data. */ |
| 5273 | l->p = l->data; |
| 5274 | |
| 5275 | ExprList_check_invariants(l); |
| 5276 | } |
| 5277 | |
| 5278 | static int |
| 5279 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5280 | { |
| 5281 | ExprList_check_invariants(l); |
| 5282 | if (l->size >= l->allocated) { |
| 5283 | /* We need to alloc (or realloc) the memory. */ |
| 5284 | Py_ssize_t new_size = l->allocated * 2; |
| 5285 | |
| 5286 | /* See if we've ever allocated anything dynamically. */ |
| 5287 | if (l->p == l->data) { |
| 5288 | Py_ssize_t i; |
| 5289 | /* We're still using the cached data. Switch to |
| 5290 | alloc-ing. */ |
| 5291 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5292 | if (!l->p) |
| 5293 | return -1; |
| 5294 | /* Copy the cached data into the new buffer. */ |
| 5295 | for (i = 0; i < l->size; i++) |
| 5296 | l->p[i] = l->data[i]; |
| 5297 | } else { |
| 5298 | /* Just realloc. */ |
| 5299 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5300 | if (!tmp) { |
| 5301 | PyMem_RawFree(l->p); |
| 5302 | l->p = NULL; |
| 5303 | return -1; |
| 5304 | } |
| 5305 | l->p = tmp; |
| 5306 | } |
| 5307 | |
| 5308 | l->allocated = new_size; |
| 5309 | assert(l->allocated == 2 * l->size); |
| 5310 | } |
| 5311 | |
| 5312 | l->p[l->size++] = exp; |
| 5313 | |
| 5314 | ExprList_check_invariants(l); |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | static void |
| 5319 | ExprList_Dealloc(ExprList *l) |
| 5320 | { |
| 5321 | ExprList_check_invariants(l); |
| 5322 | |
| 5323 | /* If there's been an error, or we've never dynamically allocated, |
| 5324 | do nothing. */ |
| 5325 | if (!l->p || l->p == l->data) { |
| 5326 | /* Do nothing. */ |
| 5327 | } else { |
| 5328 | /* We have dynamically allocated. Free the memory. */ |
| 5329 | PyMem_RawFree(l->p); |
| 5330 | } |
| 5331 | l->p = NULL; |
| 5332 | l->size = -1; |
| 5333 | } |
| 5334 | |
| 5335 | static asdl_seq * |
| 5336 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5337 | { |
| 5338 | asdl_seq *seq; |
| 5339 | |
| 5340 | ExprList_check_invariants(l); |
| 5341 | |
| 5342 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5343 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5344 | if (seq) { |
| 5345 | Py_ssize_t i; |
| 5346 | for (i = 0; i < l->size; i++) |
| 5347 | asdl_seq_SET(seq, i, l->p[i]); |
| 5348 | } |
| 5349 | ExprList_Dealloc(l); |
| 5350 | return seq; |
| 5351 | } |
| 5352 | |
| 5353 | /* The FstringParser is designed to add a mix of strings and |
| 5354 | f-strings, and concat them together as needed. Ultimately, it |
| 5355 | generates an expr_ty. */ |
| 5356 | typedef struct { |
| 5357 | PyObject *last_str; |
| 5358 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5359 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5360 | } FstringParser; |
| 5361 | |
| 5362 | #ifdef NDEBUG |
| 5363 | #define FstringParser_check_invariants(state) |
| 5364 | #else |
| 5365 | static void |
| 5366 | FstringParser_check_invariants(FstringParser *state) |
| 5367 | { |
| 5368 | if (state->last_str) |
| 5369 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5370 | ExprList_check_invariants(&state->expr_list); |
| 5371 | } |
| 5372 | #endif |
| 5373 | |
| 5374 | static void |
| 5375 | FstringParser_Init(FstringParser *state) |
| 5376 | { |
| 5377 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5378 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5379 | ExprList_Init(&state->expr_list); |
| 5380 | FstringParser_check_invariants(state); |
| 5381 | } |
| 5382 | |
| 5383 | static void |
| 5384 | FstringParser_Dealloc(FstringParser *state) |
| 5385 | { |
| 5386 | FstringParser_check_invariants(state); |
| 5387 | |
| 5388 | Py_XDECREF(state->last_str); |
| 5389 | ExprList_Dealloc(&state->expr_list); |
| 5390 | } |
| 5391 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5392 | /* Constants for the following */ |
| 5393 | static PyObject *u_kind; |
| 5394 | |
| 5395 | /* Compute 'kind' field for string Constant (either 'u' or None) */ |
| 5396 | static PyObject * |
| 5397 | make_kind(struct compiling *c, const node *n) |
| 5398 | { |
| 5399 | char *s = NULL; |
| 5400 | PyObject *kind = NULL; |
| 5401 | |
| 5402 | /* Find the first string literal, if any */ |
| 5403 | while (TYPE(n) != STRING) { |
| 5404 | if (NCH(n) == 0) |
| 5405 | return NULL; |
| 5406 | n = CHILD(n, 0); |
| 5407 | } |
| 5408 | REQ(n, STRING); |
| 5409 | |
| 5410 | /* If it starts with 'u', return a PyUnicode "u" string */ |
| 5411 | s = STR(n); |
| 5412 | if (s && *s == 'u') { |
| 5413 | if (!u_kind) { |
| 5414 | u_kind = PyUnicode_InternFromString("u"); |
| 5415 | if (!u_kind) |
| 5416 | return NULL; |
| 5417 | } |
| 5418 | kind = u_kind; |
| 5419 | if (PyArena_AddPyObject(c->c_arena, kind) < 0) { |
| 5420 | return NULL; |
| 5421 | } |
| 5422 | Py_INCREF(kind); |
| 5423 | } |
| 5424 | return kind; |
| 5425 | } |
| 5426 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5427 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5428 | static expr_ty |
| 5429 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5430 | { |
| 5431 | PyObject *s = *str; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5432 | PyObject *kind = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5433 | *str = NULL; |
| 5434 | assert(PyUnicode_CheckExact(s)); |
| 5435 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5436 | Py_DECREF(s); |
| 5437 | return NULL; |
| 5438 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5439 | kind = make_kind(c, n); |
| 5440 | if (kind == NULL && PyErr_Occurred()) |
| 5441 | return NULL; |
| 5442 | return Constant(s, kind, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5443 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5444 | } |
| 5445 | |
| 5446 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5447 | decref'd. */ |
| 5448 | static int |
| 5449 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5450 | { |
| 5451 | FstringParser_check_invariants(state); |
| 5452 | |
| 5453 | assert(PyUnicode_CheckExact(str)); |
| 5454 | |
| 5455 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5456 | Py_DECREF(str); |
| 5457 | return 0; |
| 5458 | } |
| 5459 | |
| 5460 | if (!state->last_str) { |
| 5461 | /* We didn't have a string before, so just remember this one. */ |
| 5462 | state->last_str = str; |
| 5463 | } else { |
| 5464 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5465 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5466 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5467 | return -1; |
| 5468 | } |
| 5469 | FstringParser_check_invariants(state); |
| 5470 | return 0; |
| 5471 | } |
| 5472 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5473 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5474 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5475 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5476 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5477 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5478 | struct compiling *c, const node *n) |
| 5479 | { |
| 5480 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5481 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5482 | |
| 5483 | /* Parse the f-string. */ |
| 5484 | while (1) { |
| 5485 | PyObject *literal = NULL; |
| 5486 | expr_ty expression = NULL; |
| 5487 | |
| 5488 | /* If there's a zero length literal in front of the |
| 5489 | expression, literal will be NULL. If we're at the end of |
| 5490 | the f-string, expression will be NULL (unless result == 1, |
| 5491 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5492 | 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] | 5493 | &literal, &expression, |
| 5494 | c, n); |
| 5495 | if (result < 0) |
| 5496 | return -1; |
| 5497 | |
| 5498 | /* Add the literal, if any. */ |
| 5499 | if (!literal) { |
| 5500 | /* Do nothing. Just leave last_str alone (and possibly |
| 5501 | NULL). */ |
| 5502 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 5503 | /* Note that the literal can be zero length, if the |
| 5504 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5505 | state->last_str = literal; |
| 5506 | literal = NULL; |
| 5507 | } else { |
| 5508 | /* We have a literal, concatenate it. */ |
| 5509 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 5510 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 5511 | return -1; |
| 5512 | literal = NULL; |
| 5513 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5514 | |
| 5515 | /* We've dealt with the literal now. It can't be leaked on further |
| 5516 | errors. */ |
| 5517 | assert(literal == NULL); |
| 5518 | |
| 5519 | /* See if we should just loop around to get the next literal |
| 5520 | and expression, while ignoring the expression this |
| 5521 | time. This is used for un-doubling braces, as an |
| 5522 | optimization. */ |
| 5523 | if (result == 1) |
| 5524 | continue; |
| 5525 | |
| 5526 | if (!expression) |
| 5527 | /* We're done with this f-string. */ |
| 5528 | break; |
| 5529 | |
| 5530 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5531 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5532 | if (!state->last_str) { |
| 5533 | /* Do nothing. No previous literal. */ |
| 5534 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5535 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5536 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5537 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5538 | return -1; |
| 5539 | } |
| 5540 | |
| 5541 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5542 | return -1; |
| 5543 | } |
| 5544 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5545 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5546 | string. Otherwise, we must be at a right brace. */ |
| 5547 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5548 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5549 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5550 | return -1; |
| 5551 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5552 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5553 | ast_error(c, n, "f-string: expecting '}'"); |
| 5554 | return -1; |
| 5555 | } |
| 5556 | |
| 5557 | FstringParser_check_invariants(state); |
| 5558 | return 0; |
| 5559 | } |
| 5560 | |
| 5561 | /* 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] | 5562 | 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] | 5563 | static expr_ty |
| 5564 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5565 | const node *n) |
| 5566 | { |
| 5567 | asdl_seq *seq; |
| 5568 | |
| 5569 | FstringParser_check_invariants(state); |
| 5570 | |
| 5571 | /* If we're just a constant string with no expressions, return |
| 5572 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5573 | if (!state->fmode) { |
| 5574 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5575 | if (!state->last_str) { |
| 5576 | /* Create a zero length string. */ |
| 5577 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5578 | if (!state->last_str) |
| 5579 | goto error; |
| 5580 | } |
| 5581 | return make_str_node_and_del(&state->last_str, c, n); |
| 5582 | } |
| 5583 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5584 | /* 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] | 5585 | last node in our expression list. */ |
| 5586 | if (state->last_str) { |
| 5587 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5588 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5589 | goto error; |
| 5590 | } |
| 5591 | /* This has already been freed. */ |
| 5592 | assert(state->last_str == NULL); |
| 5593 | |
| 5594 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5595 | if (!seq) |
| 5596 | goto error; |
| 5597 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5598 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5599 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5600 | |
| 5601 | error: |
| 5602 | FstringParser_Dealloc(state); |
| 5603 | return NULL; |
| 5604 | } |
| 5605 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5606 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5607 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5608 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5609 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5610 | 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] | 5611 | struct compiling *c, const node *n) |
| 5612 | { |
| 5613 | FstringParser state; |
| 5614 | |
| 5615 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5616 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5617 | c, n) < 0) { |
| 5618 | FstringParser_Dealloc(&state); |
| 5619 | return NULL; |
| 5620 | } |
| 5621 | |
| 5622 | return FstringParser_Finish(&state, c, n); |
| 5623 | } |
| 5624 | |
| 5625 | /* n is a Python string literal, including the bracketing quote |
| 5626 | 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] | 5627 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5628 | 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] | 5629 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5630 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5631 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5632 | static int |
| 5633 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5634 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5635 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5636 | size_t len; |
| 5637 | const char *s = STR(n); |
| 5638 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5639 | int fmode = 0; |
| 5640 | *bytesmode = 0; |
| 5641 | *rawmode = 0; |
| 5642 | *result = NULL; |
| 5643 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5644 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5645 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5646 | if (quote == 'b' || quote == 'B') { |
| 5647 | quote = *++s; |
| 5648 | *bytesmode = 1; |
| 5649 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5650 | else if (quote == 'u' || quote == 'U') { |
| 5651 | quote = *++s; |
| 5652 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5653 | else if (quote == 'r' || quote == 'R') { |
| 5654 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5655 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5656 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5657 | else if (quote == 'f' || quote == 'F') { |
| 5658 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5659 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5660 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5661 | else { |
| 5662 | break; |
| 5663 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5664 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5665 | } |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 5666 | |
| 5667 | /* fstrings are only allowed in Python 3.6 and greater */ |
| 5668 | if (fmode && c->c_feature_version < 6) { |
| 5669 | ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); |
| 5670 | return -1; |
| 5671 | } |
| 5672 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5673 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5674 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5675 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5676 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5677 | if (quote != '\'' && quote != '\"') { |
| 5678 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5679 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5680 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5681 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5682 | s++; |
| 5683 | len = strlen(s); |
| 5684 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5685 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5686 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5687 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5688 | } |
| 5689 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5690 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5691 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5692 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5693 | } |
| 5694 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5695 | /* A triple quoted string. We've already skipped one quote at |
| 5696 | the start and one at the end of the string. Now skip the |
| 5697 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5698 | s += 2; |
| 5699 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5700 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5701 | if (s[--len] != quote || s[--len] != quote) { |
| 5702 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5703 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5704 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5705 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5706 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5707 | if (fmode) { |
| 5708 | /* Just return the bytes. The caller will parse the resulting |
| 5709 | string. */ |
| 5710 | *fstr = s; |
| 5711 | *fstrlen = len; |
| 5712 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5713 | } |
| 5714 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5715 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5716 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5717 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5718 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5719 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5720 | const char *ch; |
| 5721 | for (ch = s; *ch; ch++) { |
| 5722 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5723 | ast_error(c, n, |
| 5724 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5725 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5726 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5727 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5728 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5729 | if (*rawmode) |
| 5730 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5731 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5732 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5733 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5734 | if (*rawmode) |
| 5735 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5736 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5737 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5738 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5739 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5742 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5743 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5744 | 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] | 5745 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5746 | 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] | 5747 | node if there's just an f-string (with no leading or trailing |
| 5748 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5749 | any literals involved. */ |
| 5750 | static expr_ty |
| 5751 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5752 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5753 | int bytesmode = 0; |
| 5754 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5755 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5756 | |
| 5757 | FstringParser state; |
| 5758 | FstringParser_Init(&state); |
| 5759 | |
| 5760 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5761 | int this_bytesmode; |
| 5762 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5763 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5764 | const char *fstr; |
| 5765 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5766 | |
| 5767 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5768 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5769 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5770 | goto error; |
| 5771 | |
| 5772 | /* Check that we're not mixing bytes with unicode. */ |
| 5773 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5774 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5775 | /* s is NULL if the current string part is an f-string. */ |
| 5776 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5777 | goto error; |
| 5778 | } |
| 5779 | bytesmode = this_bytesmode; |
| 5780 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5781 | if (fstr != NULL) { |
| 5782 | int result; |
| 5783 | assert(s == NULL && !bytesmode); |
| 5784 | /* This is an f-string. Parse and concatenate it. */ |
| 5785 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5786 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5787 | if (result < 0) |
| 5788 | goto error; |
| 5789 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5790 | /* A string or byte string. */ |
| 5791 | assert(s != NULL && fstr == NULL); |
| 5792 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5793 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5794 | PyUnicode_CheckExact(s)); |
| 5795 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5796 | if (bytesmode) { |
| 5797 | /* For bytes, concat as we go. */ |
| 5798 | if (i == 0) { |
| 5799 | /* First time, just remember this value. */ |
| 5800 | bytes_str = s; |
| 5801 | } else { |
| 5802 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5803 | if (!bytes_str) |
| 5804 | goto error; |
| 5805 | } |
| 5806 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5807 | /* This is a regular string. Concatenate it. */ |
| 5808 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5809 | goto error; |
| 5810 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5811 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5812 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5813 | if (bytesmode) { |
| 5814 | /* Just return the bytes object and we're done. */ |
| 5815 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5816 | goto error; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5817 | return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5818 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5819 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5820 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5821 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5822 | assert(bytes_str == NULL); |
| 5823 | |
| 5824 | return FstringParser_Finish(&state, c, n); |
| 5825 | |
| 5826 | error: |
| 5827 | Py_XDECREF(bytes_str); |
| 5828 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5829 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5830 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5831 | |
| 5832 | PyObject * |
| 5833 | _PyAST_GetDocString(asdl_seq *body) |
| 5834 | { |
| 5835 | if (!asdl_seq_LEN(body)) { |
| 5836 | return NULL; |
| 5837 | } |
| 5838 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5839 | if (st->kind != Expr_kind) { |
| 5840 | return NULL; |
| 5841 | } |
| 5842 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5843 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5844 | return e->v.Constant.value; |
| 5845 | } |
| 5846 | return NULL; |
| 5847 | } |