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"; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 97 | case NamedStore: |
| 98 | return "NamedStore"; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 99 | case Del: |
| 100 | return "Del"; |
| 101 | case AugLoad: |
| 102 | return "AugLoad"; |
| 103 | case AugStore: |
| 104 | return "AugStore"; |
| 105 | case Param: |
| 106 | return "Param"; |
| 107 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 108 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | validate_arguments(arguments_ty args) |
| 114 | { |
| 115 | if (!validate_args(args->args)) |
| 116 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 117 | if (args->vararg && args->vararg->annotation |
| 118 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | if (!validate_args(args->kwonlyargs)) |
| 122 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 123 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 124 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 128 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 129 | return 0; |
| 130 | } |
| 131 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 132 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 133 | "kw_defaults on arguments"); |
| 134 | return 0; |
| 135 | } |
| 136 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 137 | } |
| 138 | |
| 139 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 140 | validate_constant(PyObject *value) |
| 141 | { |
| 142 | if (value == Py_None || value == Py_Ellipsis) |
| 143 | return 1; |
| 144 | |
| 145 | if (PyLong_CheckExact(value) |
| 146 | || PyFloat_CheckExact(value) |
| 147 | || PyComplex_CheckExact(value) |
| 148 | || PyBool_Check(value) |
| 149 | || PyUnicode_CheckExact(value) |
| 150 | || PyBytes_CheckExact(value)) |
| 151 | return 1; |
| 152 | |
| 153 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 154 | PyObject *it; |
| 155 | |
| 156 | it = PyObject_GetIter(value); |
| 157 | if (it == NULL) |
| 158 | return 0; |
| 159 | |
| 160 | while (1) { |
| 161 | PyObject *item = PyIter_Next(it); |
| 162 | if (item == NULL) { |
| 163 | if (PyErr_Occurred()) { |
| 164 | Py_DECREF(it); |
| 165 | return 0; |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | if (!validate_constant(item)) { |
| 171 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 172 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 173 | return 0; |
| 174 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 175 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | Py_DECREF(it); |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 186 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 187 | { |
| 188 | int check_ctx = 1; |
| 189 | expr_context_ty actual_ctx; |
| 190 | |
| 191 | /* First check expression context. */ |
| 192 | switch (exp->kind) { |
| 193 | case Attribute_kind: |
| 194 | actual_ctx = exp->v.Attribute.ctx; |
| 195 | break; |
| 196 | case Subscript_kind: |
| 197 | actual_ctx = exp->v.Subscript.ctx; |
| 198 | break; |
| 199 | case Starred_kind: |
| 200 | actual_ctx = exp->v.Starred.ctx; |
| 201 | break; |
| 202 | case Name_kind: |
| 203 | actual_ctx = exp->v.Name.ctx; |
| 204 | break; |
| 205 | case List_kind: |
| 206 | actual_ctx = exp->v.List.ctx; |
| 207 | break; |
| 208 | case Tuple_kind: |
| 209 | actual_ctx = exp->v.Tuple.ctx; |
| 210 | break; |
| 211 | default: |
| 212 | if (ctx != Load) { |
| 213 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 214 | "assigned to in %s context", expr_context_name(ctx)); |
| 215 | return 0; |
| 216 | } |
| 217 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 218 | /* set actual_ctx to prevent gcc warning */ |
| 219 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 220 | } |
| 221 | if (check_ctx && actual_ctx != ctx) { |
| 222 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 223 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* Now validate expression. */ |
| 228 | switch (exp->kind) { |
| 229 | case BoolOp_kind: |
| 230 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 231 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 232 | return 0; |
| 233 | } |
| 234 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 235 | case BinOp_kind: |
| 236 | return validate_expr(exp->v.BinOp.left, Load) && |
| 237 | validate_expr(exp->v.BinOp.right, Load); |
| 238 | case UnaryOp_kind: |
| 239 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 240 | case Lambda_kind: |
| 241 | return validate_arguments(exp->v.Lambda.args) && |
| 242 | validate_expr(exp->v.Lambda.body, Load); |
| 243 | case IfExp_kind: |
| 244 | return validate_expr(exp->v.IfExp.test, Load) && |
| 245 | validate_expr(exp->v.IfExp.body, Load) && |
| 246 | validate_expr(exp->v.IfExp.orelse, Load); |
| 247 | case Dict_kind: |
| 248 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 249 | PyErr_SetString(PyExc_ValueError, |
| 250 | "Dict doesn't have the same number of keys as values"); |
| 251 | return 0; |
| 252 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 253 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 254 | dict literals, i.e. ``{**{a:b}}`` */ |
| 255 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 256 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 257 | case Set_kind: |
| 258 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 259 | #define COMP(NAME) \ |
| 260 | case NAME ## _kind: \ |
| 261 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 262 | validate_expr(exp->v.NAME.elt, Load); |
| 263 | COMP(ListComp) |
| 264 | COMP(SetComp) |
| 265 | COMP(GeneratorExp) |
| 266 | #undef COMP |
| 267 | case DictComp_kind: |
| 268 | return validate_comprehension(exp->v.DictComp.generators) && |
| 269 | validate_expr(exp->v.DictComp.key, Load) && |
| 270 | validate_expr(exp->v.DictComp.value, Load); |
| 271 | case Yield_kind: |
| 272 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 273 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 274 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 275 | case Await_kind: |
| 276 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 277 | case Compare_kind: |
| 278 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 279 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 280 | return 0; |
| 281 | } |
| 282 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 283 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 284 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 285 | "of comparators and operands"); |
| 286 | return 0; |
| 287 | } |
| 288 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 289 | validate_expr(exp->v.Compare.left, Load); |
| 290 | case Call_kind: |
| 291 | return validate_expr(exp->v.Call.func, Load) && |
| 292 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 293 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 294 | case Constant_kind: |
| 295 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 296 | PyErr_Format(PyExc_TypeError, |
| 297 | "got an invalid type in Constant: %s", |
| 298 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 302 | case JoinedStr_kind: |
| 303 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 304 | case FormattedValue_kind: |
| 305 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 306 | return 0; |
| 307 | if (exp->v.FormattedValue.format_spec) |
| 308 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 309 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 310 | case Attribute_kind: |
| 311 | return validate_expr(exp->v.Attribute.value, Load); |
| 312 | case Subscript_kind: |
| 313 | return validate_slice(exp->v.Subscript.slice) && |
| 314 | validate_expr(exp->v.Subscript.value, Load); |
| 315 | case Starred_kind: |
| 316 | return validate_expr(exp->v.Starred.value, ctx); |
| 317 | case List_kind: |
| 318 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 319 | case Tuple_kind: |
| 320 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
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; |
| 324 | default: |
| 325 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 326 | return 0; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | static int |
| 331 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 332 | { |
| 333 | if (asdl_seq_LEN(seq)) |
| 334 | return 1; |
| 335 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int |
| 340 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 341 | { |
| 342 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 343 | validate_exprs(targets, ctx, 0); |
| 344 | } |
| 345 | |
| 346 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 347 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 348 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 349 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int |
| 353 | validate_stmt(stmt_ty stmt) |
| 354 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 355 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 356 | switch (stmt->kind) { |
| 357 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 358 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 359 | validate_arguments(stmt->v.FunctionDef.args) && |
| 360 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 361 | (!stmt->v.FunctionDef.returns || |
| 362 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 363 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 364 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 365 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 366 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 367 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 368 | case Return_kind: |
| 369 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 370 | case Delete_kind: |
| 371 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 372 | case Assign_kind: |
| 373 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 374 | validate_expr(stmt->v.Assign.value, Load); |
| 375 | case AugAssign_kind: |
| 376 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 377 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 378 | case AnnAssign_kind: |
| 379 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 380 | stmt->v.AnnAssign.simple) { |
| 381 | PyErr_SetString(PyExc_TypeError, |
| 382 | "AnnAssign with simple non-Name target"); |
| 383 | return 0; |
| 384 | } |
| 385 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 386 | (!stmt->v.AnnAssign.value || |
| 387 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 388 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 389 | case For_kind: |
| 390 | return validate_expr(stmt->v.For.target, Store) && |
| 391 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 392 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 393 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 394 | case AsyncFor_kind: |
| 395 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 396 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 397 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 398 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 399 | case While_kind: |
| 400 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 401 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 402 | validate_stmts(stmt->v.While.orelse); |
| 403 | case If_kind: |
| 404 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 405 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 406 | validate_stmts(stmt->v.If.orelse); |
| 407 | case With_kind: |
| 408 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 409 | return 0; |
| 410 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 411 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 412 | if (!validate_expr(item->context_expr, Load) || |
| 413 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 414 | return 0; |
| 415 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 416 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 417 | case AsyncWith_kind: |
| 418 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 419 | return 0; |
| 420 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 421 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 422 | if (!validate_expr(item->context_expr, Load) || |
| 423 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 424 | return 0; |
| 425 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 426 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 427 | case Raise_kind: |
| 428 | if (stmt->v.Raise.exc) { |
| 429 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 430 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 431 | } |
| 432 | if (stmt->v.Raise.cause) { |
| 433 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 434 | return 0; |
| 435 | } |
| 436 | return 1; |
| 437 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 438 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 439 | return 0; |
| 440 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 441 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 442 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 443 | return 0; |
| 444 | } |
| 445 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 446 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 447 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 448 | return 0; |
| 449 | } |
| 450 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 451 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 452 | if ((handler->v.ExceptHandler.type && |
| 453 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 454 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 458 | validate_stmts(stmt->v.Try.finalbody)) && |
| 459 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 460 | validate_stmts(stmt->v.Try.orelse)); |
| 461 | case Assert_kind: |
| 462 | return validate_expr(stmt->v.Assert.test, Load) && |
| 463 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 464 | case Import_kind: |
| 465 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 466 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 467 | if (stmt->v.ImportFrom.level < 0) { |
| 468 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 472 | case Global_kind: |
| 473 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 474 | case Nonlocal_kind: |
| 475 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 476 | case Expr_kind: |
| 477 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 478 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 479 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 480 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 481 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 482 | (!stmt->v.AsyncFunctionDef.returns || |
| 483 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 484 | case Pass_kind: |
| 485 | case Break_kind: |
| 486 | case Continue_kind: |
| 487 | return 1; |
| 488 | default: |
| 489 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 490 | return 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | static int |
| 495 | validate_stmts(asdl_seq *seq) |
| 496 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 497 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 498 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 499 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 500 | if (stmt) { |
| 501 | if (!validate_stmt(stmt)) |
| 502 | return 0; |
| 503 | } |
| 504 | else { |
| 505 | PyErr_SetString(PyExc_ValueError, |
| 506 | "None disallowed in statement list"); |
| 507 | return 0; |
| 508 | } |
| 509 | } |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | static int |
| 514 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 515 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 516 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 517 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 518 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 519 | if (expr) { |
| 520 | if (!validate_expr(expr, ctx)) |
| 521 | return 0; |
| 522 | } |
| 523 | else if (!null_ok) { |
| 524 | PyErr_SetString(PyExc_ValueError, |
| 525 | "None disallowed in expression list"); |
| 526 | return 0; |
| 527 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 528 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 529 | } |
| 530 | return 1; |
| 531 | } |
| 532 | |
| 533 | int |
| 534 | PyAST_Validate(mod_ty mod) |
| 535 | { |
| 536 | int res = 0; |
| 537 | |
| 538 | switch (mod->kind) { |
| 539 | case Module_kind: |
| 540 | res = validate_stmts(mod->v.Module.body); |
| 541 | break; |
| 542 | case Interactive_kind: |
| 543 | res = validate_stmts(mod->v.Interactive.body); |
| 544 | break; |
| 545 | case Expression_kind: |
| 546 | res = validate_expr(mod->v.Expression.body, Load); |
| 547 | break; |
| 548 | case Suite_kind: |
| 549 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 550 | break; |
| 551 | default: |
| 552 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 553 | res = 0; |
| 554 | break; |
| 555 | } |
| 556 | return res; |
| 557 | } |
| 558 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 559 | /* 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] | 560 | #include "grammar.h" |
| 561 | #include "parsetok.h" |
| 562 | #include "graminit.h" |
| 563 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 564 | /* Data structure used internally */ |
| 565 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 566 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 567 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 568 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
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; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 788 | |
| 789 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 792 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 | switch (TYPE(n)) { |
| 794 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 795 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 797 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 | for (i = 0; i < NCH(n) - 1; i++) { |
| 799 | ch = CHILD(n, i); |
| 800 | if (TYPE(ch) == NEWLINE) |
| 801 | continue; |
| 802 | REQ(ch, stmt); |
| 803 | num = num_stmts(ch); |
| 804 | if (num == 1) { |
| 805 | s = ast_for_stmt(&c, ch); |
| 806 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 807 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 808 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 809 | } |
| 810 | else { |
| 811 | ch = CHILD(ch, 0); |
| 812 | REQ(ch, simple_stmt); |
| 813 | for (j = 0; j < num; j++) { |
| 814 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 815 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 816 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 817 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 821 | |
| 822 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 823 | ch = CHILD(n, NCH(n) - 1); |
| 824 | REQ(ch, ENDMARKER); |
| 825 | num = NCH(ch); |
| 826 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 827 | if (!type_ignores) |
| 828 | goto out; |
| 829 | |
| 830 | for (i = 0; i < num; i++) { |
| 831 | type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena); |
| 832 | if (!ti) |
| 833 | goto out; |
| 834 | asdl_seq_SET(type_ignores, i, ti); |
| 835 | } |
| 836 | |
| 837 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 838 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 | case eval_input: { |
| 840 | expr_ty testlist_ast; |
| 841 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 842 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 843 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 845 | goto out; |
| 846 | res = Expression(testlist_ast, arena); |
| 847 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 848 | } |
| 849 | case single_input: |
| 850 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 851 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 852 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 853 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 854 | 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] | 855 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 856 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 857 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 858 | goto out; |
| 859 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 860 | } |
| 861 | else { |
| 862 | n = CHILD(n, 0); |
| 863 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 864 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 866 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 867 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 868 | s = ast_for_stmt(&c, n); |
| 869 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 870 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 871 | asdl_seq_SET(stmts, 0, s); |
| 872 | } |
| 873 | else { |
| 874 | /* Only a simple_stmt can contain multiple statements. */ |
| 875 | REQ(n, simple_stmt); |
| 876 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 877 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 878 | break; |
| 879 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 880 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 881 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 882 | asdl_seq_SET(stmts, i / 2, s); |
| 883 | } |
| 884 | } |
| 885 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 886 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 887 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 888 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 889 | case func_type_input: |
| 890 | n = CHILD(n, 0); |
| 891 | REQ(n, func_type); |
| 892 | |
| 893 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 894 | ch = CHILD(n, 1); |
| 895 | /* this is overly permissive -- we don't pay any attention to |
| 896 | * stars on the args -- just parse them into an ordered list */ |
| 897 | num = 0; |
| 898 | for (i = 0; i < NCH(ch); i++) { |
| 899 | if (TYPE(CHILD(ch, i)) == test) { |
| 900 | num++; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | argtypes = _Py_asdl_seq_new(num, arena); |
| 905 | if (!argtypes) |
| 906 | goto out; |
| 907 | |
| 908 | j = 0; |
| 909 | for (i = 0; i < NCH(ch); i++) { |
| 910 | if (TYPE(CHILD(ch, i)) == test) { |
| 911 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 912 | if (!arg) |
| 913 | goto out; |
| 914 | asdl_seq_SET(argtypes, j++, arg); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | else { |
| 919 | argtypes = _Py_asdl_seq_new(0, arena); |
| 920 | if (!argtypes) |
| 921 | goto out; |
| 922 | } |
| 923 | |
| 924 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 925 | if (!ret) |
| 926 | goto out; |
| 927 | res = FunctionType(argtypes, ret, arena); |
| 928 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 930 | PyErr_Format(PyExc_SystemError, |
| 931 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 932 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 934 | out: |
| 935 | if (c.c_normalize) { |
| 936 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 937 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 938 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 941 | mod_ty |
| 942 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 943 | PyArena *arena) |
| 944 | { |
| 945 | mod_ty mod; |
| 946 | PyObject *filename; |
| 947 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 948 | if (filename == NULL) |
| 949 | return NULL; |
| 950 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 951 | Py_DECREF(filename); |
| 952 | return mod; |
| 953 | |
| 954 | } |
| 955 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 956 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 957 | */ |
| 958 | |
| 959 | static operator_ty |
| 960 | get_operator(const node *n) |
| 961 | { |
| 962 | switch (TYPE(n)) { |
| 963 | case VBAR: |
| 964 | return BitOr; |
| 965 | case CIRCUMFLEX: |
| 966 | return BitXor; |
| 967 | case AMPER: |
| 968 | return BitAnd; |
| 969 | case LEFTSHIFT: |
| 970 | return LShift; |
| 971 | case RIGHTSHIFT: |
| 972 | return RShift; |
| 973 | case PLUS: |
| 974 | return Add; |
| 975 | case MINUS: |
| 976 | return Sub; |
| 977 | case STAR: |
| 978 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 979 | case AT: |
| 980 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 981 | case SLASH: |
| 982 | return Div; |
| 983 | case DOUBLESLASH: |
| 984 | return FloorDiv; |
| 985 | case PERCENT: |
| 986 | return Mod; |
| 987 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 988 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 992 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 993 | "None", |
| 994 | "True", |
| 995 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 996 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 997 | NULL, |
| 998 | }; |
| 999 | |
| 1000 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1001 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1002 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1003 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1004 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1005 | const char * const *p = FORBIDDEN; |
| 1006 | if (!full_checks) { |
| 1007 | /* In most cases, the parser will protect True, False, and None |
| 1008 | from being assign to. */ |
| 1009 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1010 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1011 | for (; *p; p++) { |
| 1012 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1013 | ast_error(c, n, "cannot assign to %U", name); |
| 1014 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1020 | static expr_ty |
| 1021 | copy_location(expr_ty e, const node *n) |
| 1022 | { |
| 1023 | if (e) { |
| 1024 | e->lineno = LINENO(n); |
| 1025 | e->col_offset = n->n_col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1026 | e->end_lineno = n->n_end_lineno; |
| 1027 | e->end_col_offset = n->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1028 | } |
| 1029 | return e; |
| 1030 | } |
| 1031 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1032 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1033 | |
| 1034 | Only sets context for expr kinds that "can appear in assignment context" |
| 1035 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1036 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1037 | */ |
| 1038 | |
| 1039 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1040 | 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] | 1041 | { |
| 1042 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1043 | /* If a particular expression type can't be used for assign / delete, |
| 1044 | set expr_name to its name and an error message will be generated. |
| 1045 | */ |
| 1046 | const char* expr_name = NULL; |
| 1047 | |
| 1048 | /* The ast defines augmented store and load contexts, but the |
| 1049 | implementation here doesn't actually use them. The code may be |
| 1050 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1051 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1052 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1053 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1054 | */ |
| 1055 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1056 | |
| 1057 | switch (e->kind) { |
| 1058 | case Attribute_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1059 | if (ctx == NamedStore) { |
| 1060 | expr_name = "attribute"; |
| 1061 | break; |
| 1062 | } |
| 1063 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1064 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1065 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1066 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1067 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1068 | case Subscript_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1069 | if (ctx == NamedStore) { |
| 1070 | expr_name = "subscript"; |
| 1071 | break; |
| 1072 | } |
| 1073 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1074 | e->v.Subscript.ctx = ctx; |
| 1075 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1076 | case Starred_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1077 | if (ctx == NamedStore) { |
| 1078 | expr_name = "starred"; |
| 1079 | break; |
| 1080 | } |
| 1081 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1082 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1083 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1084 | return 0; |
| 1085 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1086 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1087 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1088 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1089 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1090 | } |
| 1091 | e->v.Name.ctx = ctx; |
| 1092 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1093 | case List_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1094 | if (ctx == NamedStore) { |
| 1095 | expr_name = "list"; |
| 1096 | break; |
| 1097 | } |
| 1098 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1099 | e->v.List.ctx = ctx; |
| 1100 | s = e->v.List.elts; |
| 1101 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1102 | case Tuple_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1103 | if (ctx == NamedStore) { |
| 1104 | expr_name = "tuple"; |
| 1105 | break; |
| 1106 | } |
| 1107 | |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1108 | e->v.Tuple.ctx = ctx; |
| 1109 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1110 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1111 | case Lambda_kind: |
| 1112 | expr_name = "lambda"; |
| 1113 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1114 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1115 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1116 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1117 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1118 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1119 | case UnaryOp_kind: |
| 1120 | expr_name = "operator"; |
| 1121 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1122 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1123 | expr_name = "generator expression"; |
| 1124 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1125 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1126 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1127 | expr_name = "yield expression"; |
| 1128 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1129 | case Await_kind: |
| 1130 | expr_name = "await expression"; |
| 1131 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1132 | case ListComp_kind: |
| 1133 | expr_name = "list comprehension"; |
| 1134 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1135 | case SetComp_kind: |
| 1136 | expr_name = "set comprehension"; |
| 1137 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1138 | case DictComp_kind: |
| 1139 | expr_name = "dict comprehension"; |
| 1140 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1141 | case Dict_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1142 | expr_name = "dict display"; |
| 1143 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1144 | case Set_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1145 | expr_name = "set display"; |
| 1146 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1147 | case JoinedStr_kind: |
| 1148 | case FormattedValue_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1149 | expr_name = "f-string expression"; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1150 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1151 | case Constant_kind: { |
| 1152 | PyObject *value = e->v.Constant.value; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1153 | if (value == Py_None || value == Py_False || value == Py_True |
| 1154 | || value == Py_Ellipsis) |
| 1155 | { |
| 1156 | return ast_error(c, n, "cannot %s %R", |
| 1157 | ctx == Store ? "assign to" : "delete", |
| 1158 | value); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1159 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1160 | expr_name = "literal"; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1161 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1162 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1163 | case Compare_kind: |
| 1164 | expr_name = "comparison"; |
| 1165 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1166 | case IfExp_kind: |
| 1167 | expr_name = "conditional expression"; |
| 1168 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1169 | case NamedExpr_kind: |
| 1170 | expr_name = "named expression"; |
| 1171 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1172 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | PyErr_Format(PyExc_SystemError, |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1174 | "unexpected expression in %sassignment %d (line %d)", |
| 1175 | ctx == NamedStore ? "named ": "", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1176 | e->kind, e->lineno); |
| 1177 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1179 | /* Check for error string set by switch */ |
| 1180 | if (expr_name) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1181 | if (ctx == NamedStore) { |
| 1182 | return ast_error(c, n, "cannot use named assignment with %s", |
| 1183 | expr_name); |
| 1184 | } |
| 1185 | else { |
| 1186 | return ast_error(c, n, "cannot %s %s", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1187 | ctx == Store ? "assign to" : "delete", |
| 1188 | expr_name); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1189 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1192 | /* 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] | 1193 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1194 | */ |
| 1195 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1196 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1197 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1198 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1199 | 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] | 1200 | return 0; |
| 1201 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1202 | } |
| 1203 | return 1; |
| 1204 | } |
| 1205 | |
| 1206 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1207 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1208 | { |
| 1209 | REQ(n, augassign); |
| 1210 | n = CHILD(n, 0); |
| 1211 | switch (STR(n)[0]) { |
| 1212 | case '+': |
| 1213 | return Add; |
| 1214 | case '-': |
| 1215 | return Sub; |
| 1216 | case '/': |
| 1217 | if (STR(n)[1] == '/') |
| 1218 | return FloorDiv; |
| 1219 | else |
| 1220 | return Div; |
| 1221 | case '%': |
| 1222 | return Mod; |
| 1223 | case '<': |
| 1224 | return LShift; |
| 1225 | case '>': |
| 1226 | return RShift; |
| 1227 | case '&': |
| 1228 | return BitAnd; |
| 1229 | case '^': |
| 1230 | return BitXor; |
| 1231 | case '|': |
| 1232 | return BitOr; |
| 1233 | case '*': |
| 1234 | if (STR(n)[1] == '*') |
| 1235 | return Pow; |
| 1236 | else |
| 1237 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1238 | case '@': |
| 1239 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1240 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1241 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1242 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1247 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1249 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1250 | |'is' 'not' |
| 1251 | */ |
| 1252 | REQ(n, comp_op); |
| 1253 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1254 | n = CHILD(n, 0); |
| 1255 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1256 | case LESS: |
| 1257 | return Lt; |
| 1258 | case GREATER: |
| 1259 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1260 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1261 | return Eq; |
| 1262 | case LESSEQUAL: |
| 1263 | return LtE; |
| 1264 | case GREATEREQUAL: |
| 1265 | return GtE; |
| 1266 | case NOTEQUAL: |
| 1267 | return NotEq; |
| 1268 | case NAME: |
| 1269 | if (strcmp(STR(n), "in") == 0) |
| 1270 | return In; |
| 1271 | if (strcmp(STR(n), "is") == 0) |
| 1272 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1273 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1274 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1275 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1276 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1277 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1278 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1279 | } |
| 1280 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1281 | /* handle "not in" and "is not" */ |
| 1282 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1283 | case NAME: |
| 1284 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1285 | return NotIn; |
| 1286 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1287 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1288 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1289 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1290 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1291 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1292 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1293 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1294 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1295 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1296 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1297 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | static asdl_seq * |
| 1301 | seq_for_testlist(struct compiling *c, const node *n) |
| 1302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1304 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1305 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1306 | asdl_seq *seq; |
| 1307 | expr_ty expression; |
| 1308 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1309 | 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] | 1310 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1311 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1312 | if (!seq) |
| 1313 | return NULL; |
| 1314 | |
| 1315 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1317 | 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] | 1318 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1319 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1320 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1321 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1322 | |
| 1323 | assert(i / 2 < seq->size); |
| 1324 | asdl_seq_SET(seq, i / 2, expression); |
| 1325 | } |
| 1326 | return seq; |
| 1327 | } |
| 1328 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1329 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1330 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1331 | { |
| 1332 | identifier name; |
| 1333 | expr_ty annotation = NULL; |
| 1334 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1335 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1336 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1337 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1338 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1339 | name = NEW_IDENTIFIER(ch); |
| 1340 | if (!name) |
| 1341 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1342 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1343 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1344 | |
| 1345 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1346 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1347 | if (!annotation) |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1351 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1352 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1353 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1354 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1355 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1358 | /* returns -1 if failed to handle keyword only arguments |
| 1359 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1360 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1361 | ^^^ |
| 1362 | start pointing here |
| 1363 | */ |
| 1364 | static int |
| 1365 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1366 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1367 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1368 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1369 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1370 | expr_ty expression, annotation; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1371 | arg_ty arg = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1372 | int i = start; |
| 1373 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1374 | |
| 1375 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1376 | 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] | 1377 | return -1; |
| 1378 | } |
| 1379 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1380 | while (i < NCH(n)) { |
| 1381 | ch = CHILD(n, i); |
| 1382 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1383 | case vfpdef: |
| 1384 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1385 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1386 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1387 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1388 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1389 | asdl_seq_SET(kwdefaults, j, expression); |
| 1390 | i += 2; /* '=' and test */ |
| 1391 | } |
| 1392 | else { /* setting NULL if no default value exists */ |
| 1393 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1394 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1395 | if (NCH(ch) == 3) { |
| 1396 | /* ch is NAME ':' test */ |
| 1397 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1398 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1399 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1400 | } |
| 1401 | else { |
| 1402 | annotation = NULL; |
| 1403 | } |
| 1404 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1405 | argname = NEW_IDENTIFIER(ch); |
| 1406 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1407 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1408 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1409 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1410 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1411 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1412 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1413 | if (!arg) |
| 1414 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1415 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1416 | i += 1; /* the name */ |
| 1417 | if (TYPE(CHILD(n, i)) == COMMA) |
| 1418 | i += 1; /* the comma, if present */ |
| 1419 | break; |
| 1420 | case TYPE_COMMENT: |
| 1421 | /* arg will be equal to the last argument processed */ |
| 1422 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1423 | if (!arg->type_comment) |
| 1424 | goto error; |
| 1425 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1426 | break; |
| 1427 | case DOUBLESTAR: |
| 1428 | return i; |
| 1429 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1430 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1431 | goto error; |
| 1432 | } |
| 1433 | } |
| 1434 | return i; |
| 1435 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1437 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1438 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1439 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | |
| 1441 | static arguments_ty |
| 1442 | ast_for_arguments(struct compiling *c, const node *n) |
| 1443 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1444 | /* This function handles both typedargslist (function definition) |
| 1445 | and varargslist (lambda definition). |
| 1446 | |
| 1447 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1448 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1449 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1450 | | '**' tfpdef [',']]] |
| 1451 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1452 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1453 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1454 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1455 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1456 | | '**' vfpdef [',']]] |
| 1457 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1458 | | '**' vfpdef [','] |
| 1459 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1460 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1461 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1462 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1463 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1464 | int nposdefaults = 0, found_default = 0; |
| 1465 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1466 | arg_ty vararg = NULL, kwarg = NULL; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1467 | arg_ty arg = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1468 | node *ch; |
| 1469 | |
| 1470 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1471 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1472 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1473 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1475 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1476 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1477 | /* First count the number of positional args & defaults. The |
| 1478 | variable i is the loop index for this for loop and the next. |
| 1479 | The next loop picks up where the first leaves off. |
| 1480 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1481 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1482 | ch = CHILD(n, i); |
| 1483 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1484 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1485 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1486 | if (i < NCH(n) && /* skip argument following star */ |
| 1487 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1488 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1489 | i++; |
| 1490 | } |
| 1491 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1492 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1493 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1494 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1495 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1496 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1498 | defaults for keyword only args */ |
| 1499 | for ( ; i < NCH(n); ++i) { |
| 1500 | ch = CHILD(n, i); |
| 1501 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1502 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1503 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1504 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1505 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1506 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1507 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1508 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1509 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1510 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1512 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1513 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1514 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1516 | since we set NULL as default for keyword only argument w/o default |
| 1517 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1518 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1519 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1520 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1521 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1522 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1523 | /* tfpdef: NAME [':' test] |
| 1524 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | */ |
| 1526 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1527 | j = 0; /* index for defaults */ |
| 1528 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1530 | ch = CHILD(n, i); |
| 1531 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1532 | case tfpdef: |
| 1533 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1534 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1535 | anything other than EQUAL or a comma? */ |
| 1536 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1537 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1538 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1539 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1540 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1541 | assert(posdefaults != NULL); |
| 1542 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1544 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1545 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1546 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1547 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1548 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1549 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1550 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1551 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1552 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1553 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1554 | asdl_seq_SET(posargs, k++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1555 | i += 1; /* the name */ |
| 1556 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1557 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1560 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1561 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1562 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1563 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1564 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1565 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1566 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1567 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1568 | if (TYPE(ch) == COMMA) { |
| 1569 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1570 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1571 | |
| 1572 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1573 | ast_error(c, CHILD(n, i), |
| 1574 | "bare * has associated type comment"); |
| 1575 | return NULL; |
| 1576 | } |
| 1577 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1578 | res = handle_keywordonly_args(c, n, i, |
| 1579 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1580 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1581 | i = res; /* res has new position to process */ |
| 1582 | } |
| 1583 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1584 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1585 | if (!vararg) |
| 1586 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1587 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1588 | i += 2; /* the star and the name */ |
| 1589 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1590 | i += 1; /* the comma, if present */ |
| 1591 | |
| 1592 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1593 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1594 | if (!vararg->type_comment) |
| 1595 | return NULL; |
| 1596 | i += 1; |
| 1597 | } |
| 1598 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1599 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1600 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1601 | int res = 0; |
| 1602 | res = handle_keywordonly_args(c, n, i, |
| 1603 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1604 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1605 | i = res; /* res has new position to process */ |
| 1606 | } |
| 1607 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1608 | break; |
| 1609 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1610 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1611 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1612 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1613 | if (!kwarg) |
| 1614 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1615 | i += 2; /* the double star and the name */ |
| 1616 | if (TYPE(CHILD(n, i)) == COMMA) |
| 1617 | i += 1; /* the comma, if present */ |
| 1618 | break; |
| 1619 | case TYPE_COMMENT: |
| 1620 | assert(i); |
| 1621 | |
| 1622 | if (kwarg) |
| 1623 | arg = kwarg; |
| 1624 | |
| 1625 | /* arg will be equal to the last argument processed */ |
| 1626 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1627 | if (!arg->type_comment) |
| 1628 | return NULL; |
| 1629 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1632 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1633 | "unexpected node in varargslist: %d @ %d", |
| 1634 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1635 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1636 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1637 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1638 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static expr_ty |
| 1642 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1643 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1644 | expr_ty e; |
| 1645 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1646 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1647 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1648 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1649 | |
| 1650 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1651 | |
| 1652 | lineno = LINENO(n); |
| 1653 | col_offset = n->n_col_offset; |
| 1654 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1655 | ch = CHILD(n, 0); |
| 1656 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1657 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1658 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1659 | e = Name(id, Load, lineno, col_offset, |
| 1660 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1661 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1662 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1663 | |
| 1664 | for (i = 2; i < NCH(n); i+=2) { |
| 1665 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1666 | if (!id) |
| 1667 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1668 | e = Attribute(e, id, Load, lineno, col_offset, |
| 1669 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1670 | if (!e) |
| 1671 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | static expr_ty |
| 1678 | ast_for_decorator(struct compiling *c, const node *n) |
| 1679 | { |
| 1680 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1681 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1682 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1684 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1685 | REQ(CHILD(n, 0), AT); |
| 1686 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1688 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1689 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1690 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1691 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1692 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1693 | d = name_expr; |
| 1694 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1695 | } |
| 1696 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1697 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1698 | 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] | 1699 | if (!d) |
| 1700 | return NULL; |
| 1701 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1702 | } |
| 1703 | else { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1704 | 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] | 1705 | if (!d) |
| 1706 | return NULL; |
| 1707 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static asdl_seq* |
| 1714 | ast_for_decorators(struct compiling *c, const node *n) |
| 1715 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1716 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1717 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1718 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1720 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1721 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1722 | if (!decorator_seq) |
| 1723 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1725 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1726 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1727 | if (!d) |
| 1728 | return NULL; |
| 1729 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1730 | } |
| 1731 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1735 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1736 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1737 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1738 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1739 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1740 | identifier name; |
| 1741 | arguments_ty args; |
| 1742 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1743 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1744 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1745 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1746 | node *tc; |
| 1747 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1748 | |
| 1749 | REQ(n, funcdef); |
| 1750 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1751 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1752 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1753 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1754 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1755 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1756 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1757 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1758 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1759 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1760 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1761 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1762 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1763 | name_i += 2; |
| 1764 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1765 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1766 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1767 | if (!type_comment) |
| 1768 | return NULL; |
| 1769 | name_i += 1; |
| 1770 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1771 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1772 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1773 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1774 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1776 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1777 | /* Check if the suite has a type comment in it. */ |
| 1778 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1779 | |
| 1780 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1781 | if (type_comment != NULL) { |
| 1782 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1783 | return NULL; |
| 1784 | } |
| 1785 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1786 | if (!type_comment) |
| 1787 | return NULL; |
| 1788 | } |
| 1789 | } |
| 1790 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1791 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1792 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1793 | 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] | 1794 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1795 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1796 | 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] | 1797 | } |
| 1798 | |
| 1799 | static stmt_ty |
| 1800 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1801 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1802 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1803 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1804 | REQ(CHILD(n, 0), NAME); |
| 1805 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1806 | REQ(CHILD(n, 1), funcdef); |
| 1807 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1808 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1809 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | static stmt_ty |
| 1813 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1814 | { |
| 1815 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1816 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1817 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | static stmt_ty |
| 1822 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1823 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1824 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1825 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1826 | REQ(CHILD(n, 0), NAME); |
| 1827 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1828 | |
| 1829 | switch (TYPE(CHILD(n, 1))) { |
| 1830 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1831 | return ast_for_funcdef_impl(c, n, NULL, |
| 1832 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1833 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1834 | return ast_for_with_stmt(c, n, |
| 1835 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1836 | |
| 1837 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1838 | return ast_for_for_stmt(c, n, |
| 1839 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1840 | |
| 1841 | default: |
| 1842 | PyErr_Format(PyExc_SystemError, |
| 1843 | "invalid async stament: %s", |
| 1844 | STR(CHILD(n, 1))); |
| 1845 | return NULL; |
| 1846 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1849 | static stmt_ty |
| 1850 | ast_for_decorated(struct compiling *c, const node *n) |
| 1851 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1852 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1853 | stmt_ty thing = NULL; |
| 1854 | asdl_seq *decorator_seq = NULL; |
| 1855 | |
| 1856 | REQ(n, decorated); |
| 1857 | |
| 1858 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1859 | if (!decorator_seq) |
| 1860 | return NULL; |
| 1861 | |
| 1862 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1863 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1865 | |
| 1866 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1867 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1868 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1869 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1870 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1871 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1872 | } |
| 1873 | return thing; |
| 1874 | } |
| 1875 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1876 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1877 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1878 | { |
| 1879 | /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* |
| 1880 | ['else' ':' suite] |
| 1881 | namedexpr_test: test [':=' test] |
| 1882 | argument: ( test [comp_for] | |
| 1883 | test ':=' test | |
| 1884 | test '=' test | |
| 1885 | '**' test | |
| 1886 | '*' test ) |
| 1887 | */ |
| 1888 | expr_ty target, value; |
| 1889 | |
| 1890 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1891 | if (!target) |
| 1892 | return NULL; |
| 1893 | |
| 1894 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1895 | if (!value) |
| 1896 | return NULL; |
| 1897 | |
| 1898 | if (!set_context(c, target, NamedStore, n)) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1902 | n->n_end_col_offset, c->c_arena); |
| 1903 | } |
| 1904 | |
| 1905 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1907 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1908 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1909 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1910 | arguments_ty args; |
| 1911 | expr_ty expression; |
| 1912 | |
| 1913 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1914 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1915 | if (!args) |
| 1916 | return NULL; |
| 1917 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1918 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1919 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1920 | } |
| 1921 | else { |
| 1922 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1923 | if (!args) |
| 1924 | return NULL; |
| 1925 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1926 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1927 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1930 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1931 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1934 | static expr_ty |
| 1935 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1938 | expr_ty expression, body, orelse; |
| 1939 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1940 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1941 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1942 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1943 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1944 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1945 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1946 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1947 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1948 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1949 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1950 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1951 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1952 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
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 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1957 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1958 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1959 | */ |
| 1960 | |
| 1961 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1962 | count_comp_fors(struct compiling *c, const node *n) |
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 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1965 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1966 | count_comp_for: |
| 1967 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1968 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1969 | if (NCH(n) == 2) { |
| 1970 | REQ(CHILD(n, 0), NAME); |
| 1971 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1972 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1973 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1974 | else if (NCH(n) == 1) { |
| 1975 | n = CHILD(n, 0); |
| 1976 | } |
| 1977 | else { |
| 1978 | goto error; |
| 1979 | } |
| 1980 | if (NCH(n) == (5)) { |
| 1981 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1982 | } |
| 1983 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1984 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1985 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1986 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1987 | REQ(n, comp_iter); |
| 1988 | n = CHILD(n, 0); |
| 1989 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1990 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1991 | else if (TYPE(n) == comp_if) { |
| 1992 | if (NCH(n) == 3) { |
| 1993 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1994 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1995 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1996 | else |
| 1997 | return n_fors; |
| 1998 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1999 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2000 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2001 | /* Should never be reached */ |
| 2002 | PyErr_SetString(PyExc_SystemError, |
| 2003 | "logic error in count_comp_fors"); |
| 2004 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2007 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2008 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2009 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2010 | */ |
| 2011 | |
| 2012 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2013 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2014 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2015 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2017 | while (1) { |
| 2018 | REQ(n, comp_iter); |
| 2019 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2020 | return n_ifs; |
| 2021 | n = CHILD(n, 0); |
| 2022 | REQ(n, comp_if); |
| 2023 | n_ifs++; |
| 2024 | if (NCH(n) == 2) |
| 2025 | return n_ifs; |
| 2026 | n = CHILD(n, 2); |
| 2027 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2030 | static asdl_seq * |
| 2031 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2032 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2033 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2034 | asdl_seq *comps; |
| 2035 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2036 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2037 | if (n_fors == -1) |
| 2038 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2039 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2040 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2041 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2042 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2043 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2044 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2045 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2046 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2047 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2048 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2049 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2050 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2052 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2054 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2055 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2056 | REQ(CHILD(n, 0), NAME); |
| 2057 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 2058 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2059 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2060 | else { |
| 2061 | sync_n = CHILD(n, 0); |
| 2062 | } |
| 2063 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2064 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2065 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2066 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2067 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2068 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2069 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2070 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2071 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2072 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2073 | /* Check the # of children rather than the length of t, since |
| 2074 | (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] | 2075 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2076 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2077 | comp = comprehension(first, expression, NULL, |
| 2078 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2079 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2080 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2081 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2082 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2083 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2084 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2085 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2086 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2087 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2088 | int j, n_ifs; |
| 2089 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2091 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2092 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2093 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2094 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2095 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2096 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2097 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2098 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2099 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2100 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2101 | REQ(n, comp_iter); |
| 2102 | n = CHILD(n, 0); |
| 2103 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2105 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2106 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2107 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2108 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2109 | if (NCH(n) == 3) |
| 2110 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2111 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2112 | /* on exit, must guarantee that n is a comp_for */ |
| 2113 | if (TYPE(n) == comp_iter) |
| 2114 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2115 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2116 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2117 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2118 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2119 | return comps; |
| 2120 | } |
| 2121 | |
| 2122 | static expr_ty |
| 2123 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2124 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2125 | /* testlist_comp: (test|star_expr) |
| 2126 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2127 | expr_ty elt; |
| 2128 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2129 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2131 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2133 | ch = CHILD(n, 0); |
| 2134 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2135 | if (!elt) |
| 2136 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2137 | if (elt->kind == Starred_kind) { |
| 2138 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2139 | return NULL; |
| 2140 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2142 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2143 | if (!comps) |
| 2144 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2145 | |
| 2146 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2147 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2148 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2149 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2150 | return ListComp(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_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2153 | return SetComp(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 |
| 2156 | /* Should never happen */ |
| 2157 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2160 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2161 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2162 | * elements. Iff successful, nonzero is returned. |
| 2163 | */ |
| 2164 | static int |
| 2165 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2166 | expr_ty *key, expr_ty *value) |
| 2167 | { |
| 2168 | expr_ty expression; |
| 2169 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2170 | assert(NCH(n) - *i >= 2); |
| 2171 | |
| 2172 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2173 | if (!expression) |
| 2174 | return 0; |
| 2175 | *key = NULL; |
| 2176 | *value = expression; |
| 2177 | |
| 2178 | *i += 2; |
| 2179 | } |
| 2180 | else { |
| 2181 | assert(NCH(n) - *i >= 3); |
| 2182 | |
| 2183 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2184 | if (!expression) |
| 2185 | return 0; |
| 2186 | *key = expression; |
| 2187 | |
| 2188 | REQ(CHILD(n, *i + 1), COLON); |
| 2189 | |
| 2190 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2191 | if (!expression) |
| 2192 | return 0; |
| 2193 | *value = expression; |
| 2194 | |
| 2195 | *i += 3; |
| 2196 | } |
| 2197 | return 1; |
| 2198 | } |
| 2199 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2200 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2201 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2202 | { |
| 2203 | expr_ty key, value; |
| 2204 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2205 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2207 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2208 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2209 | assert(key); |
| 2210 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2212 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2213 | if (!comps) |
| 2214 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2216 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2217 | 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] | 2218 | } |
| 2219 | |
| 2220 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2221 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2222 | { |
| 2223 | int i; |
| 2224 | int j; |
| 2225 | int size; |
| 2226 | asdl_seq *keys, *values; |
| 2227 | |
| 2228 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2229 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2230 | if (!keys) |
| 2231 | return NULL; |
| 2232 | |
| 2233 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2234 | if (!values) |
| 2235 | return NULL; |
| 2236 | |
| 2237 | j = 0; |
| 2238 | for (i = 0; i < NCH(n); i++) { |
| 2239 | expr_ty key, value; |
| 2240 | |
| 2241 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2242 | return NULL; |
| 2243 | asdl_seq_SET(keys, j, key); |
| 2244 | asdl_seq_SET(values, j, value); |
| 2245 | |
| 2246 | j++; |
| 2247 | } |
| 2248 | keys->size = j; |
| 2249 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2250 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2251 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2255 | ast_for_genexp(struct compiling *c, const node *n) |
| 2256 | { |
| 2257 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2258 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | static expr_ty |
| 2262 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2263 | { |
| 2264 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2265 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | static expr_ty |
| 2269 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2270 | { |
| 2271 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2272 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2275 | static expr_ty |
| 2276 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2277 | { |
| 2278 | int i; |
| 2279 | int size; |
| 2280 | asdl_seq *elts; |
| 2281 | |
| 2282 | assert(TYPE(n) == (dictorsetmaker)); |
| 2283 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2284 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2285 | if (!elts) |
| 2286 | return NULL; |
| 2287 | for (i = 0; i < NCH(n); i += 2) { |
| 2288 | expr_ty expression; |
| 2289 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2290 | if (!expression) |
| 2291 | return NULL; |
| 2292 | asdl_seq_SET(elts, i / 2, expression); |
| 2293 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2294 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2295 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2296 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2297 | |
| 2298 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 | ast_for_atom(struct compiling *c, const node *n) |
| 2300 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2301 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2302 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2303 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2304 | */ |
| 2305 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2307 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2308 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2309 | PyObject *name; |
| 2310 | const char *s = STR(ch); |
| 2311 | size_t len = strlen(s); |
| 2312 | if (len >= 4 && len <= 5) { |
| 2313 | if (!strcmp(s, "None")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2314 | return Constant(Py_None, LINENO(n), n->n_col_offset, |
| 2315 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2316 | if (!strcmp(s, "True")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2317 | return Constant(Py_True, LINENO(n), n->n_col_offset, |
| 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, "False")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2320 | return Constant(Py_False, LINENO(n), n->n_col_offset, |
| 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 | } |
| 2323 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2324 | if (!name) |
| 2325 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2326 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2327 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2328 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2329 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2330 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2331 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2332 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2333 | const char *errtype = NULL; |
| 2334 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2335 | errtype = "unicode error"; |
| 2336 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2337 | errtype = "value error"; |
| 2338 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2339 | PyObject *type, *value, *tback, *errstr; |
| 2340 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2341 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2342 | if (errstr) { |
| 2343 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2344 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2345 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2346 | else { |
| 2347 | PyErr_Clear(); |
| 2348 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2349 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2350 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2351 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2352 | Py_XDECREF(tback); |
| 2353 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2354 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2355 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2356 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2357 | } |
| 2358 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2359 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2360 | if (!pynum) |
| 2361 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2362 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2363 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2364 | Py_DECREF(pynum); |
| 2365 | return NULL; |
| 2366 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2367 | return Constant(pynum, LINENO(n), n->n_col_offset, |
| 2368 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2369 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2370 | case ELLIPSIS: /* Ellipsis */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2371 | return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, |
| 2372 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2373 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2374 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2376 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2377 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2378 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2380 | if (TYPE(ch) == yield_expr) |
| 2381 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2383 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2384 | if (NCH(ch) == 1) { |
| 2385 | return ast_for_testlist(c, ch); |
| 2386 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2387 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2388 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2389 | return copy_location(ast_for_genexp(c, ch), n); |
| 2390 | } |
| 2391 | else { |
| 2392 | return copy_location(ast_for_testlist(c, ch), n); |
| 2393 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2394 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2395 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2397 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2398 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2399 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2401 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2402 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2403 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2404 | if (!elts) |
| 2405 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2406 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2407 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2408 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2409 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2410 | else { |
| 2411 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2412 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2413 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2414 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2415 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2416 | * ((test | '*' test) |
| 2417 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2418 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2419 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2420 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2421 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2422 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2423 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2424 | } |
| 2425 | else { |
| 2426 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2427 | if (NCH(ch) == 1 || |
| 2428 | (NCH(ch) > 1 && |
| 2429 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2430 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2431 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2432 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2433 | else if (NCH(ch) > 1 && |
| 2434 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2435 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2436 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2437 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2438 | else if (NCH(ch) > 3 - is_dict && |
| 2439 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2440 | /* It's a dictionary comprehension. */ |
| 2441 | if (is_dict) { |
| 2442 | ast_error(c, n, "dict unpacking cannot be used in " |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 2443 | "dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2444 | return NULL; |
| 2445 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2446 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2447 | } |
| 2448 | else { |
| 2449 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2450 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2451 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2452 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2453 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2454 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2455 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2456 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2457 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | static slice_ty |
| 2462 | ast_for_slice(struct compiling *c, const node *n) |
| 2463 | { |
| 2464 | node *ch; |
| 2465 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2466 | |
| 2467 | REQ(n, subscript); |
| 2468 | |
| 2469 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2470 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2471 | sliceop: ':' [test] |
| 2472 | */ |
| 2473 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2474 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2475 | /* 'step' variable hold no significance in terms of being used over |
| 2476 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2478 | if (!step) |
| 2479 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2481 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2485 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2486 | if (!lower) |
| 2487 | return NULL; |
| 2488 | } |
| 2489 | |
| 2490 | /* If there's an upper bound it's in the second or third position. */ |
| 2491 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2492 | if (NCH(n) > 1) { |
| 2493 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2494 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2495 | if (TYPE(n2) == test) { |
| 2496 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2497 | if (!upper) |
| 2498 | return NULL; |
| 2499 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2500 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2501 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2502 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2503 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2504 | if (TYPE(n2) == test) { |
| 2505 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2506 | if (!upper) |
| 2507 | return NULL; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | ch = CHILD(n, NCH(n) - 1); |
| 2512 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2513 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2514 | ch = CHILD(ch, 1); |
| 2515 | if (TYPE(ch) == test) { |
| 2516 | step = ast_for_expr(c, ch); |
| 2517 | if (!step) |
| 2518 | return NULL; |
| 2519 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2523 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | static expr_ty |
| 2527 | ast_for_binop(struct compiling *c, const node *n) |
| 2528 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2529 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2530 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2531 | BinOp(BinOp(A, op, B), op, C). |
| 2532 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2533 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2534 | int i, nops; |
| 2535 | expr_ty expr1, expr2, result; |
| 2536 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2537 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2538 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2539 | if (!expr1) |
| 2540 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2542 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2543 | if (!expr2) |
| 2544 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2545 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2546 | newoperator = get_operator(CHILD(n, 1)); |
| 2547 | if (!newoperator) |
| 2548 | return NULL; |
| 2549 | |
| 2550 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2551 | 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] | 2552 | c->c_arena); |
| 2553 | if (!result) |
| 2554 | return NULL; |
| 2555 | |
| 2556 | nops = (NCH(n) - 1) / 2; |
| 2557 | for (i = 1; i < nops; i++) { |
| 2558 | expr_ty tmp_result, tmp; |
| 2559 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2560 | |
| 2561 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2562 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2563 | return NULL; |
| 2564 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2565 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2566 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2567 | return NULL; |
| 2568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2570 | LINENO(next_oper), next_oper->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2571 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2572 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2573 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2575 | return NULL; |
| 2576 | result = tmp_result; |
| 2577 | } |
| 2578 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2581 | static expr_ty |
| 2582 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2585 | subscriptlist: subscript (',' subscript)* [','] |
| 2586 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2587 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2588 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2589 | REQ(n, trailer); |
| 2590 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2591 | if (NCH(n) == 2) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2592 | return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, |
| 2593 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2594 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2595 | 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] | 2596 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2597 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2598 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2599 | if (!attr_id) |
| 2600 | return NULL; |
| 2601 | return Attribute(left_expr, attr_id, Load, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2602 | LINENO(n), n->n_col_offset, |
| 2603 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2604 | } |
| 2605 | else { |
| 2606 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2607 | REQ(CHILD(n, 2), RSQB); |
| 2608 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2609 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2610 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2611 | if (!slc) |
| 2612 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2613 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2614 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2615 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2616 | } |
| 2617 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2619 | by treating the sequence as a tuple literal if there are |
| 2620 | no slice features. |
| 2621 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2622 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2623 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2624 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2625 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2626 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2627 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2628 | if (!slices) |
| 2629 | return NULL; |
| 2630 | for (j = 0; j < NCH(n); j += 2) { |
| 2631 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2632 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2633 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2634 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2635 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2636 | asdl_seq_SET(slices, j / 2, slc); |
| 2637 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2638 | if (!simple) { |
| 2639 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2640 | Load, LINENO(n), n->n_col_offset, |
| 2641 | 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] | 2642 | } |
| 2643 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2644 | 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] | 2645 | if (!elts) |
| 2646 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2647 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2648 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2649 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2650 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2651 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2652 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2653 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2654 | if (!e) |
| 2655 | return NULL; |
| 2656 | return Subscript(left_expr, Index(e, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2657 | Load, LINENO(n), n->n_col_offset, |
| 2658 | 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] | 2659 | } |
| 2660 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2664 | ast_for_factor(struct compiling *c, const node *n) |
| 2665 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2666 | expr_ty expression; |
| 2667 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2668 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2669 | if (!expression) |
| 2670 | return NULL; |
| 2671 | |
| 2672 | switch (TYPE(CHILD(n, 0))) { |
| 2673 | case PLUS: |
| 2674 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2675 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2676 | c->c_arena); |
| 2677 | case MINUS: |
| 2678 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2679 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2680 | c->c_arena); |
| 2681 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2682 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2683 | n->n_end_lineno, n->n_end_col_offset, |
| 2684 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2685 | } |
| 2686 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2687 | TYPE(CHILD(n, 0))); |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | |
| 2691 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2692 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2693 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2694 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2695 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2696 | |
| 2697 | REQ(n, atom_expr); |
| 2698 | nch = NCH(n); |
| 2699 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2700 | if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2701 | start = 1; |
| 2702 | assert(nch > 1); |
| 2703 | } |
| 2704 | |
| 2705 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2706 | if (!e) |
| 2707 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2708 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2709 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2710 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2711 | return Await(e, LINENO(n), n->n_col_offset, |
| 2712 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2716 | node *ch = CHILD(n, i); |
| 2717 | if (TYPE(ch) != trailer) |
| 2718 | break; |
| 2719 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2720 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2721 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2722 | tmp->lineno = e->lineno; |
| 2723 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2724 | e = tmp; |
| 2725 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2726 | |
| 2727 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2728 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2729 | return Await(e, LINENO(n), n->n_col_offset, |
| 2730 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2731 | } |
| 2732 | else { |
| 2733 | return e; |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | static expr_ty |
| 2738 | ast_for_power(struct compiling *c, const node *n) |
| 2739 | { |
| 2740 | /* power: atom trailer* ('**' factor)* |
| 2741 | */ |
| 2742 | expr_ty e; |
| 2743 | REQ(n, power); |
| 2744 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2745 | if (!e) |
| 2746 | return NULL; |
| 2747 | if (NCH(n) == 1) |
| 2748 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2749 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2750 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2751 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2752 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2753 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2754 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2755 | } |
| 2756 | return e; |
| 2757 | } |
| 2758 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2759 | static expr_ty |
| 2760 | ast_for_starred(struct compiling *c, const node *n) |
| 2761 | { |
| 2762 | expr_ty tmp; |
| 2763 | REQ(n, star_expr); |
| 2764 | |
| 2765 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2766 | if (!tmp) |
| 2767 | return NULL; |
| 2768 | |
| 2769 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2770 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2771 | 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] | 2772 | } |
| 2773 | |
| 2774 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2775 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2776 | */ |
| 2777 | |
| 2778 | static expr_ty |
| 2779 | ast_for_expr(struct compiling *c, const node *n) |
| 2780 | { |
| 2781 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2782 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2783 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2784 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2786 | and_test: not_test ('and' not_test)* |
| 2787 | not_test: 'not' not_test | comparison |
| 2788 | comparison: expr (comp_op expr)* |
| 2789 | expr: xor_expr ('|' xor_expr)* |
| 2790 | xor_expr: and_expr ('^' and_expr)* |
| 2791 | and_expr: shift_expr ('&' shift_expr)* |
| 2792 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2793 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2794 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2795 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2796 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2797 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2798 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2799 | */ |
| 2800 | |
| 2801 | asdl_seq *seq; |
| 2802 | int i; |
| 2803 | |
| 2804 | loop: |
| 2805 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2806 | case namedexpr_test: |
| 2807 | if (NCH(n) == 3) |
| 2808 | return ast_for_namedexpr(c, n); |
| 2809 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2810 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2811 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2812 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2813 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2814 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2815 | else if (NCH(n) > 1) |
| 2816 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2817 | /* Fallthrough */ |
| 2818 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2819 | case and_test: |
| 2820 | if (NCH(n) == 1) { |
| 2821 | n = CHILD(n, 0); |
| 2822 | goto loop; |
| 2823 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2824 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | if (!seq) |
| 2826 | return NULL; |
| 2827 | for (i = 0; i < NCH(n); i += 2) { |
| 2828 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2829 | if (!e) |
| 2830 | return NULL; |
| 2831 | asdl_seq_SET(seq, i / 2, e); |
| 2832 | } |
| 2833 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2834 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2835 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2836 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2837 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2838 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2839 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2840 | case not_test: |
| 2841 | if (NCH(n) == 1) { |
| 2842 | n = CHILD(n, 0); |
| 2843 | goto loop; |
| 2844 | } |
| 2845 | else { |
| 2846 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2847 | if (!expression) |
| 2848 | return NULL; |
| 2849 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2850 | return UnaryOp(Not, expression, 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); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2853 | } |
| 2854 | case comparison: |
| 2855 | if (NCH(n) == 1) { |
| 2856 | n = CHILD(n, 0); |
| 2857 | goto loop; |
| 2858 | } |
| 2859 | else { |
| 2860 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2861 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2862 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2863 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | if (!ops) |
| 2865 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2866 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2867 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2868 | return NULL; |
| 2869 | } |
| 2870 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2871 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2873 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2874 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2876 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2877 | |
| 2878 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2879 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2881 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2883 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2884 | asdl_seq_SET(cmps, i / 2, expression); |
| 2885 | } |
| 2886 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2887 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2889 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2891 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2892 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2893 | } |
| 2894 | break; |
| 2895 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2896 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2898 | /* The next five cases all handle BinOps. The main body of code |
| 2899 | is the same in each case, but the switch turned inside out to |
| 2900 | reuse the code for each type of operator. |
| 2901 | */ |
| 2902 | case expr: |
| 2903 | case xor_expr: |
| 2904 | case and_expr: |
| 2905 | case shift_expr: |
| 2906 | case arith_expr: |
| 2907 | case term: |
| 2908 | if (NCH(n) == 1) { |
| 2909 | n = CHILD(n, 0); |
| 2910 | goto loop; |
| 2911 | } |
| 2912 | return ast_for_binop(c, n); |
| 2913 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2914 | node *an = NULL; |
| 2915 | node *en = NULL; |
| 2916 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2917 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2918 | if (NCH(n) > 1) |
| 2919 | an = CHILD(n, 1); /* yield_arg */ |
| 2920 | if (an) { |
| 2921 | en = CHILD(an, NCH(an) - 1); |
| 2922 | if (NCH(an) == 2) { |
| 2923 | is_from = 1; |
| 2924 | exp = ast_for_expr(c, en); |
| 2925 | } |
| 2926 | else |
| 2927 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2928 | if (!exp) |
| 2929 | return NULL; |
| 2930 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2931 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2932 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2933 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2934 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2935 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2936 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2937 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | if (NCH(n) == 1) { |
| 2939 | n = CHILD(n, 0); |
| 2940 | goto loop; |
| 2941 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2942 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2943 | case power: |
| 2944 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2945 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2946 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | return NULL; |
| 2948 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2949 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2950 | return NULL; |
| 2951 | } |
| 2952 | |
| 2953 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2954 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2955 | const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2956 | { |
| 2957 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2958 | arglist: argument (',' argument)* [','] |
| 2959 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2960 | */ |
| 2961 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2962 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2963 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2964 | asdl_seq *args; |
| 2965 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2966 | |
| 2967 | REQ(n, arglist); |
| 2968 | |
| 2969 | nargs = 0; |
| 2970 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2971 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2972 | node *ch = CHILD(n, i); |
| 2973 | if (TYPE(ch) == argument) { |
| 2974 | if (NCH(ch) == 1) |
| 2975 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2976 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2977 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2978 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2979 | ast_error(c, ch, "invalid syntax"); |
| 2980 | return NULL; |
| 2981 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2982 | if (NCH(n) > 1) { |
| 2983 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2984 | return NULL; |
| 2985 | } |
| 2986 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2987 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2988 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2989 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 2990 | nargs++; |
| 2991 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2992 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2993 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2994 | nkeywords++; |
| 2995 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2996 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2997 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2998 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3000 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3001 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3002 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3003 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3004 | |
| 3005 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 3006 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 3007 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3008 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3009 | node *ch = CHILD(n, i); |
| 3010 | if (TYPE(ch) == argument) { |
| 3011 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3012 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3013 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3014 | /* a positional argument */ |
| 3015 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3016 | if (ndoublestars) { |
| 3017 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3018 | "positional argument follows " |
| 3019 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3020 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3021 | else { |
| 3022 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3023 | "positional argument follows " |
| 3024 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3025 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3026 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3027 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3028 | e = ast_for_expr(c, chch); |
| 3029 | if (!e) |
| 3030 | return NULL; |
| 3031 | asdl_seq_SET(args, nargs++, e); |
| 3032 | } |
| 3033 | else if (TYPE(chch) == STAR) { |
| 3034 | /* an iterable argument unpacking */ |
| 3035 | expr_ty starred; |
| 3036 | if (ndoublestars) { |
| 3037 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3038 | "iterable argument unpacking follows " |
| 3039 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3040 | return NULL; |
| 3041 | } |
| 3042 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3043 | if (!e) |
| 3044 | return NULL; |
| 3045 | starred = Starred(e, Load, LINENO(chch), |
| 3046 | chch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3047 | chch->n_end_lineno, chch->n_end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3048 | c->c_arena); |
| 3049 | if (!starred) |
| 3050 | return NULL; |
| 3051 | asdl_seq_SET(args, nargs++, starred); |
| 3052 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3053 | } |
| 3054 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3055 | /* a keyword argument unpacking */ |
| 3056 | keyword_ty kw; |
| 3057 | i++; |
| 3058 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3059 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3060 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3061 | kw = keyword(NULL, e, c->c_arena); |
| 3062 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3063 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3065 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3066 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3067 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3068 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3069 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3070 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3071 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3072 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3073 | /* treat colon equal as positional argument */ |
| 3074 | if (nkeywords) { |
| 3075 | if (ndoublestars) { |
| 3076 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3077 | "positional argument follows " |
| 3078 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3079 | } |
| 3080 | else { |
| 3081 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3082 | "positional argument follows " |
| 3083 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3084 | } |
| 3085 | return NULL; |
| 3086 | } |
| 3087 | e = ast_for_namedexpr(c, ch); |
| 3088 | if (!e) |
| 3089 | return NULL; |
| 3090 | asdl_seq_SET(args, nargs++, e); |
| 3091 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3092 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3093 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3094 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3095 | identifier key, tmp; |
| 3096 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3097 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3098 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3099 | // expression) in the keyword slot of a call site. So, we need |
| 3100 | // to manually enforce that the keyword is a NAME here. |
| 3101 | static const int name_tree[] = { |
| 3102 | test, |
| 3103 | or_test, |
| 3104 | and_test, |
| 3105 | not_test, |
| 3106 | comparison, |
| 3107 | expr, |
| 3108 | xor_expr, |
| 3109 | and_expr, |
| 3110 | shift_expr, |
| 3111 | arith_expr, |
| 3112 | term, |
| 3113 | factor, |
| 3114 | power, |
| 3115 | atom_expr, |
| 3116 | atom, |
| 3117 | 0, |
| 3118 | }; |
| 3119 | node *expr_node = chch; |
| 3120 | for (int i = 0; name_tree[i]; i++) { |
| 3121 | if (TYPE(expr_node) != name_tree[i]) |
| 3122 | break; |
| 3123 | if (NCH(expr_node) != 1) |
| 3124 | break; |
| 3125 | expr_node = CHILD(expr_node, 0); |
| 3126 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3127 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3128 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3129 | "expression cannot contain assignment, " |
| 3130 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3131 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3132 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3133 | key = new_identifier(STR(expr_node), c); |
| 3134 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3135 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3137 | if (forbidden_name(c, key, chch, 1)) { |
| 3138 | return NULL; |
| 3139 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3140 | for (k = 0; k < nkeywords; k++) { |
| 3141 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3142 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 3143 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3144 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3145 | return NULL; |
| 3146 | } |
| 3147 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3148 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3149 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3150 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3151 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3153 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3154 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3155 | } |
| 3156 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3159 | return Call(func, args, keywords, func->lineno, func->col_offset, |
| 3160 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3163 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3164 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3165 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3166 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3167 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3168 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3169 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3170 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3171 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3172 | } |
| 3173 | else { |
| 3174 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3175 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3176 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3177 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3178 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3179 | else { |
| 3180 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3181 | if (!tmp) |
| 3182 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3183 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3184 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3188 | static stmt_ty |
| 3189 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3190 | { |
| 3191 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3192 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3193 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3194 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3195 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3196 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3197 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3198 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3199 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3200 | int num = NCH(n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3202 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3203 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3204 | if (!e) |
| 3205 | return NULL; |
| 3206 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3207 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3208 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3209 | } |
| 3210 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3211 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3212 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3213 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3214 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3215 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3216 | if (!expr1) |
| 3217 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3218 | if(!set_context(c, expr1, Store, ch)) |
| 3219 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3220 | /* set_context checks that most expressions are not the left side. |
| 3221 | Augmented assignments can only have a name, a subscript, or an |
| 3222 | attribute on the left, though, so we have to explicitly check for |
| 3223 | those. */ |
| 3224 | switch (expr1->kind) { |
| 3225 | case Name_kind: |
| 3226 | case Attribute_kind: |
| 3227 | case Subscript_kind: |
| 3228 | break; |
| 3229 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3230 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3231 | return NULL; |
| 3232 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3233 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3234 | ch = CHILD(n, 2); |
| 3235 | if (TYPE(ch) == testlist) |
| 3236 | expr2 = ast_for_testlist(c, ch); |
| 3237 | else |
| 3238 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3239 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | return NULL; |
| 3241 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3242 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3243 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3244 | return NULL; |
| 3245 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3246 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3247 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3248 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3249 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3250 | expr_ty expr1, expr2, expr3; |
| 3251 | node *ch = CHILD(n, 0); |
| 3252 | node *deep, *ann = CHILD(n, 1); |
| 3253 | int simple = 1; |
| 3254 | |
| 3255 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3256 | deep = ch; |
| 3257 | while (NCH(deep) == 1) { |
| 3258 | deep = CHILD(deep, 0); |
| 3259 | } |
| 3260 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3261 | simple = 0; |
| 3262 | } |
| 3263 | expr1 = ast_for_testlist(c, ch); |
| 3264 | if (!expr1) { |
| 3265 | return NULL; |
| 3266 | } |
| 3267 | switch (expr1->kind) { |
| 3268 | case Name_kind: |
| 3269 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3270 | return NULL; |
| 3271 | } |
| 3272 | expr1->v.Name.ctx = Store; |
| 3273 | break; |
| 3274 | case Attribute_kind: |
| 3275 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3276 | return NULL; |
| 3277 | } |
| 3278 | expr1->v.Attribute.ctx = Store; |
| 3279 | break; |
| 3280 | case Subscript_kind: |
| 3281 | expr1->v.Subscript.ctx = Store; |
| 3282 | break; |
| 3283 | case List_kind: |
| 3284 | ast_error(c, ch, |
| 3285 | "only single target (not list) can be annotated"); |
| 3286 | return NULL; |
| 3287 | case Tuple_kind: |
| 3288 | ast_error(c, ch, |
| 3289 | "only single target (not tuple) can be annotated"); |
| 3290 | return NULL; |
| 3291 | default: |
| 3292 | ast_error(c, ch, |
| 3293 | "illegal target for annotation"); |
| 3294 | return NULL; |
| 3295 | } |
| 3296 | |
| 3297 | if (expr1->kind != Name_kind) { |
| 3298 | simple = 0; |
| 3299 | } |
| 3300 | ch = CHILD(ann, 1); |
| 3301 | expr2 = ast_for_expr(c, ch); |
| 3302 | if (!expr2) { |
| 3303 | return NULL; |
| 3304 | } |
| 3305 | if (NCH(ann) == 2) { |
| 3306 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3307 | LINENO(n), n->n_col_offset, |
| 3308 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3309 | } |
| 3310 | else { |
| 3311 | ch = CHILD(ann, 3); |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3312 | if (TYPE(ch) == testlist) { |
| 3313 | expr3 = ast_for_testlist(c, ch); |
| 3314 | } |
| 3315 | else { |
| 3316 | expr3 = ast_for_expr(c, ch); |
| 3317 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3318 | if (!expr3) { |
| 3319 | return NULL; |
| 3320 | } |
| 3321 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3322 | LINENO(n), n->n_col_offset, |
| 3323 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3324 | } |
| 3325 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3326 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3327 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3328 | asdl_seq *targets; |
| 3329 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3330 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3331 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3333 | /* a normal assignment */ |
| 3334 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3335 | |
| 3336 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3337 | nch_minus_type = num - has_type_comment; |
| 3338 | |
| 3339 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3340 | if (!targets) |
| 3341 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3342 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3343 | expr_ty e; |
| 3344 | node *ch = CHILD(n, i); |
| 3345 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3346 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3347 | return NULL; |
| 3348 | } |
| 3349 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3351 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3352 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3353 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3354 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3355 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3357 | asdl_seq_SET(targets, i / 2, e); |
| 3358 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3359 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3360 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3361 | expression = ast_for_testlist(c, value); |
| 3362 | else |
| 3363 | expression = ast_for_expr(c, value); |
| 3364 | if (!expression) |
| 3365 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3366 | if (has_type_comment) { |
| 3367 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3368 | if (!type_comment) |
| 3369 | return NULL; |
| 3370 | } |
| 3371 | else |
| 3372 | type_comment = NULL; |
| 3373 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3374 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3375 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3378 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3379 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3380 | 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] | 3381 | { |
| 3382 | asdl_seq *seq; |
| 3383 | int i; |
| 3384 | expr_ty e; |
| 3385 | |
| 3386 | REQ(n, exprlist); |
| 3387 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3388 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3389 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3390 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3391 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3392 | e = ast_for_expr(c, CHILD(n, i)); |
| 3393 | if (!e) |
| 3394 | return NULL; |
| 3395 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3396 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3397 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3398 | } |
| 3399 | return seq; |
| 3400 | } |
| 3401 | |
| 3402 | static stmt_ty |
| 3403 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3404 | { |
| 3405 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3407 | /* del_stmt: 'del' exprlist */ |
| 3408 | REQ(n, del_stmt); |
| 3409 | |
| 3410 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3411 | if (!expr_list) |
| 3412 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3413 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3414 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | static stmt_ty |
| 3418 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3419 | { |
| 3420 | /* |
| 3421 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3422 | | yield_stmt |
| 3423 | break_stmt: 'break' |
| 3424 | continue_stmt: 'continue' |
| 3425 | return_stmt: 'return' [testlist] |
| 3426 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3427 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3428 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3429 | */ |
| 3430 | node *ch; |
| 3431 | |
| 3432 | REQ(n, flow_stmt); |
| 3433 | ch = CHILD(n, 0); |
| 3434 | switch (TYPE(ch)) { |
| 3435 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3436 | return Break(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 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3439 | return Continue(LINENO(n), n->n_col_offset, |
| 3440 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3441 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3442 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3443 | if (!exp) |
| 3444 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3445 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3446 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3447 | } |
| 3448 | case return_stmt: |
| 3449 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3450 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3451 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3452 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3453 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3454 | if (!expression) |
| 3455 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3456 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3457 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | } |
| 3459 | case raise_stmt: |
| 3460 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3461 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3462 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3463 | else if (NCH(ch) >= 2) { |
| 3464 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3465 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3466 | if (!expression) |
| 3467 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3468 | if (NCH(ch) == 4) { |
| 3469 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3470 | if (!cause) |
| 3471 | return NULL; |
| 3472 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3473 | return Raise(expression, cause, 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 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3476 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3478 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3479 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3480 | return NULL; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3485 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3486 | { |
| 3487 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3488 | import_as_name: NAME ['as' NAME] |
| 3489 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3490 | dotted_name: NAME ('.' NAME)* |
| 3491 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3492 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3493 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3494 | loop: |
| 3495 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3496 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3497 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3498 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3499 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3500 | if (!name) |
| 3501 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3502 | if (NCH(n) == 3) { |
| 3503 | node *str_node = CHILD(n, 2); |
| 3504 | str = NEW_IDENTIFIER(str_node); |
| 3505 | if (!str) |
| 3506 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3507 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3508 | return NULL; |
| 3509 | } |
| 3510 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3511 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3512 | return NULL; |
| 3513 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3514 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3515 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3516 | case dotted_as_name: |
| 3517 | if (NCH(n) == 1) { |
| 3518 | n = CHILD(n, 0); |
| 3519 | goto loop; |
| 3520 | } |
| 3521 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3522 | node *asname_node = CHILD(n, 2); |
| 3523 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3524 | if (!a) |
| 3525 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3526 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3527 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3528 | if (!a->asname) |
| 3529 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3530 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3531 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3532 | return a; |
| 3533 | } |
| 3534 | break; |
| 3535 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3536 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3537 | node *name_node = CHILD(n, 0); |
| 3538 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3539 | if (!name) |
| 3540 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3541 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3542 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3543 | return alias(name, NULL, c->c_arena); |
| 3544 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | else { |
| 3546 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3547 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3548 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3550 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3551 | |
| 3552 | len = 0; |
| 3553 | for (i = 0; i < NCH(n); i += 2) |
| 3554 | /* length of string plus one for the dot */ |
| 3555 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3556 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3557 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3558 | if (!str) |
| 3559 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3560 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3561 | if (!s) |
| 3562 | return NULL; |
| 3563 | for (i = 0; i < NCH(n); i += 2) { |
| 3564 | char *sch = STR(CHILD(n, i)); |
| 3565 | strcpy(s, STR(CHILD(n, i))); |
| 3566 | s += strlen(sch); |
| 3567 | *s++ = '.'; |
| 3568 | } |
| 3569 | --s; |
| 3570 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3572 | PyBytes_GET_SIZE(str), |
| 3573 | NULL); |
| 3574 | Py_DECREF(str); |
| 3575 | if (!uni) |
| 3576 | return NULL; |
| 3577 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3578 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3579 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3580 | Py_DECREF(str); |
| 3581 | return NULL; |
| 3582 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3583 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | } |
| 3585 | break; |
| 3586 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3587 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3588 | if (!str) |
| 3589 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3590 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3591 | Py_DECREF(str); |
| 3592 | return NULL; |
| 3593 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3594 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3595 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3596 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | "unexpected import name: %d", TYPE(n)); |
| 3598 | return NULL; |
| 3599 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3600 | |
| 3601 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | return NULL; |
| 3603 | } |
| 3604 | |
| 3605 | static stmt_ty |
| 3606 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3607 | { |
| 3608 | /* |
| 3609 | import_stmt: import_name | import_from |
| 3610 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3611 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3612 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3613 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3614 | int lineno; |
| 3615 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3616 | int i; |
| 3617 | asdl_seq *aliases; |
| 3618 | |
| 3619 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3620 | lineno = LINENO(n); |
| 3621 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3622 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3623 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3624 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3625 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3626 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3627 | if (!aliases) |
| 3628 | return NULL; |
| 3629 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3630 | 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] | 3631 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3632 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3633 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3634 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3635 | // Even though n is modified above, the end position is not changed |
| 3636 | return Import(aliases, lineno, col_offset, |
| 3637 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3638 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3639 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3640 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3641 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3642 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3643 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3644 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3646 | /* Count the number of dots (for relative imports) and check for the |
| 3647 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3648 | for (idx = 1; idx < NCH(n); idx++) { |
| 3649 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3650 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3651 | if (!mod) |
| 3652 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3653 | idx++; |
| 3654 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3655 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3656 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3657 | ndots += 3; |
| 3658 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3659 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3660 | break; |
| 3661 | } |
| 3662 | ndots++; |
| 3663 | } |
| 3664 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3665 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3666 | case STAR: |
| 3667 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3668 | n = CHILD(n, idx); |
| 3669 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | case LPAR: |
| 3672 | /* from ... import (x, y, z) */ |
| 3673 | n = CHILD(n, idx + 1); |
| 3674 | n_children = NCH(n); |
| 3675 | break; |
| 3676 | case import_as_names: |
| 3677 | /* from ... import x, y, z */ |
| 3678 | n = CHILD(n, idx); |
| 3679 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3680 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3681 | ast_error(c, n, |
| 3682 | "trailing comma not allowed without" |
| 3683 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3684 | return NULL; |
| 3685 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3686 | break; |
| 3687 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3688 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3689 | return NULL; |
| 3690 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3691 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3692 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3693 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3694 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3695 | |
| 3696 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3697 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3698 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3699 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3701 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3702 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3703 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3704 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3705 | 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] | 3706 | if (!import_alias) |
| 3707 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3708 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3709 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3710 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3711 | if (mod != NULL) |
| 3712 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3713 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3714 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3715 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3717 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3718 | "unknown import statement: starts with command '%s'", |
| 3719 | STR(CHILD(n, 0))); |
| 3720 | return NULL; |
| 3721 | } |
| 3722 | |
| 3723 | static stmt_ty |
| 3724 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3725 | { |
| 3726 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3727 | identifier name; |
| 3728 | asdl_seq *s; |
| 3729 | int i; |
| 3730 | |
| 3731 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3732 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3733 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3734 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3735 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3736 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3737 | if (!name) |
| 3738 | return NULL; |
| 3739 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3740 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3741 | return Global(s, LINENO(n), n->n_col_offset, |
| 3742 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3746 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3747 | { |
| 3748 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3749 | identifier name; |
| 3750 | asdl_seq *s; |
| 3751 | int i; |
| 3752 | |
| 3753 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3754 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3755 | if (!s) |
| 3756 | return NULL; |
| 3757 | for (i = 1; i < NCH(n); i += 2) { |
| 3758 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3759 | if (!name) |
| 3760 | return NULL; |
| 3761 | asdl_seq_SET(s, i / 2, name); |
| 3762 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3763 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3764 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3768 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3769 | { |
| 3770 | /* assert_stmt: 'assert' test [',' test] */ |
| 3771 | REQ(n, assert_stmt); |
| 3772 | if (NCH(n) == 2) { |
| 3773 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3774 | if (!expression) |
| 3775 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3776 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3777 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3778 | } |
| 3779 | else if (NCH(n) == 4) { |
| 3780 | expr_ty expr1, expr2; |
| 3781 | |
| 3782 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3783 | if (!expr1) |
| 3784 | return NULL; |
| 3785 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3786 | if (!expr2) |
| 3787 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3789 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3790 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3791 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3792 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3793 | "improper number of parts to 'assert' statement: %d", |
| 3794 | NCH(n)); |
| 3795 | return NULL; |
| 3796 | } |
| 3797 | |
| 3798 | static asdl_seq * |
| 3799 | ast_for_suite(struct compiling *c, const node *n) |
| 3800 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3801 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3802 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3803 | stmt_ty s; |
| 3804 | int i, total, num, end, pos = 0; |
| 3805 | node *ch; |
| 3806 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3807 | if (TYPE(n) != func_body_suite) { |
| 3808 | REQ(n, suite); |
| 3809 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3810 | |
| 3811 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3812 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3813 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3814 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3815 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3816 | n = CHILD(n, 0); |
| 3817 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3819 | */ |
| 3820 | end = NCH(n) - 1; |
| 3821 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3822 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3823 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3824 | for (i = 0; i < end; i += 2) { |
| 3825 | ch = CHILD(n, i); |
| 3826 | s = ast_for_stmt(c, ch); |
| 3827 | if (!s) |
| 3828 | return NULL; |
| 3829 | asdl_seq_SET(seq, pos++, s); |
| 3830 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3831 | } |
| 3832 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3833 | i = 2; |
| 3834 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3835 | i += 2; |
| 3836 | REQ(CHILD(n, 2), NEWLINE); |
| 3837 | } |
| 3838 | |
| 3839 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3840 | ch = CHILD(n, i); |
| 3841 | REQ(ch, stmt); |
| 3842 | num = num_stmts(ch); |
| 3843 | if (num == 1) { |
| 3844 | /* small_stmt or compound_stmt with only one child */ |
| 3845 | s = ast_for_stmt(c, ch); |
| 3846 | if (!s) |
| 3847 | return NULL; |
| 3848 | asdl_seq_SET(seq, pos++, s); |
| 3849 | } |
| 3850 | else { |
| 3851 | int j; |
| 3852 | ch = CHILD(ch, 0); |
| 3853 | REQ(ch, simple_stmt); |
| 3854 | for (j = 0; j < NCH(ch); j += 2) { |
| 3855 | /* statement terminates with a semi-colon ';' */ |
| 3856 | if (NCH(CHILD(ch, j)) == 0) { |
| 3857 | assert((j + 1) == NCH(ch)); |
| 3858 | break; |
| 3859 | } |
| 3860 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3861 | if (!s) |
| 3862 | return NULL; |
| 3863 | asdl_seq_SET(seq, pos++, s); |
| 3864 | } |
| 3865 | } |
| 3866 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3867 | } |
| 3868 | assert(pos == seq->size); |
| 3869 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3872 | static void |
| 3873 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3874 | { |
Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame^] | 3875 | Py_ssize_t tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3876 | // There must be no empty suites. |
| 3877 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3878 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3879 | *end_lineno = last->end_lineno; |
| 3880 | *end_col_offset = last->end_col_offset; |
| 3881 | } |
| 3882 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3883 | static stmt_ty |
| 3884 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3885 | { |
| 3886 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3887 | ['else' ':' suite] |
| 3888 | */ |
| 3889 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3890 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3891 | |
| 3892 | REQ(n, if_stmt); |
| 3893 | |
| 3894 | if (NCH(n) == 4) { |
| 3895 | expr_ty expression; |
| 3896 | asdl_seq *suite_seq; |
| 3897 | |
| 3898 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3899 | if (!expression) |
| 3900 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3901 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3902 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3903 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3904 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3905 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3906 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3907 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3908 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3909 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3910 | s = STR(CHILD(n, 4)); |
| 3911 | /* s[2], the third character in the string, will be |
| 3912 | 's' for el_s_e, or |
| 3913 | 'i' for el_i_f |
| 3914 | */ |
| 3915 | if (s[2] == 's') { |
| 3916 | expr_ty expression; |
| 3917 | asdl_seq *seq1, *seq2; |
| 3918 | |
| 3919 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3920 | if (!expression) |
| 3921 | return NULL; |
| 3922 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3923 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3924 | return NULL; |
| 3925 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3926 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3927 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3928 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3929 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3930 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3931 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3932 | } |
| 3933 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3934 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3935 | expr_ty expression; |
| 3936 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3937 | asdl_seq *orelse = NULL; |
| 3938 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3939 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3940 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3941 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3942 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3943 | has_else = 1; |
| 3944 | n_elif -= 3; |
| 3945 | } |
| 3946 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3947 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3948 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3949 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3950 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3951 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3952 | if (!orelse) |
| 3953 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3954 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3955 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3956 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3957 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3958 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3959 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3960 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3961 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3962 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3963 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3965 | asdl_seq_SET(orelse, 0, |
| 3966 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3967 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3968 | CHILD(n, NCH(n) - 6)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3969 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3970 | /* the just-created orelse handled the last elif */ |
| 3971 | n_elif--; |
| 3972 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3973 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3974 | for (i = 0; i < n_elif; i++) { |
| 3975 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3976 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3977 | if (!newobj) |
| 3978 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3979 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3980 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3981 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3982 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3983 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3984 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3985 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3986 | if (orelse != NULL) { |
| 3987 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3988 | } else { |
| 3989 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3990 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3991 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3992 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3993 | LINENO(CHILD(n, off)), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3994 | CHILD(n, off)->n_col_offset, |
| 3995 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3996 | orelse = newobj; |
| 3997 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3998 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3999 | if (!expression) |
| 4000 | return NULL; |
| 4001 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 4002 | if (!suite_seq) |
| 4003 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4004 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4005 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4006 | LINENO(n), n->n_col_offset, |
| 4007 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4008 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4009 | |
| 4010 | PyErr_Format(PyExc_SystemError, |
| 4011 | "unexpected token in 'if' statement: %s", s); |
| 4012 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | static stmt_ty |
| 4016 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 4017 | { |
| 4018 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 4019 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4020 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4021 | |
| 4022 | if (NCH(n) == 4) { |
| 4023 | expr_ty expression; |
| 4024 | asdl_seq *suite_seq; |
| 4025 | |
| 4026 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4027 | if (!expression) |
| 4028 | return NULL; |
| 4029 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4030 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4031 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4032 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4033 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4034 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4035 | } |
| 4036 | else if (NCH(n) == 7) { |
| 4037 | expr_ty expression; |
| 4038 | asdl_seq *seq1, *seq2; |
| 4039 | |
| 4040 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4041 | if (!expression) |
| 4042 | return NULL; |
| 4043 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4044 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4045 | return NULL; |
| 4046 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4047 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4048 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4049 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4050 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4051 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4052 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4053 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4054 | |
| 4055 | PyErr_Format(PyExc_SystemError, |
| 4056 | "wrong number of tokens for 'while' statement: %d", |
| 4057 | NCH(n)); |
| 4058 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4062 | 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] | 4063 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4064 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4065 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4066 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4067 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4068 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4069 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4070 | int has_type_comment; |
| 4071 | string type_comment; |
| 4072 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4073 | REQ(n, for_stmt); |
| 4074 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4075 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4076 | |
| 4077 | if (NCH(n) == 9 + has_type_comment) { |
| 4078 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4079 | if (!seq) |
| 4080 | return NULL; |
| 4081 | } |
| 4082 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4083 | node_target = CHILD(n, 1); |
| 4084 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4085 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4086 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4087 | /* Check the # of children rather than the length of _target, since |
| 4088 | 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] | 4089 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4090 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4091 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4092 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4093 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4094 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4095 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4096 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4097 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4098 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4099 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4100 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4101 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4102 | return NULL; |
| 4103 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4104 | if (seq != NULL) { |
| 4105 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4106 | } else { |
| 4107 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4108 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4109 | |
| 4110 | if (has_type_comment) { |
| 4111 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4112 | if (!type_comment) |
| 4113 | return NULL; |
| 4114 | } |
| 4115 | else |
| 4116 | type_comment = NULL; |
| 4117 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4118 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4119 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4120 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4121 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4122 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4123 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4124 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4125 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | static excepthandler_ty |
| 4129 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4130 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4131 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4132 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4133 | REQ(exc, except_clause); |
| 4134 | REQ(body, suite); |
| 4135 | |
| 4136 | if (NCH(exc) == 1) { |
| 4137 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4138 | if (!suite_seq) |
| 4139 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4140 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4141 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4142 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4143 | exc->n_col_offset, |
| 4144 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4145 | } |
| 4146 | else if (NCH(exc) == 2) { |
| 4147 | expr_ty expression; |
| 4148 | asdl_seq *suite_seq; |
| 4149 | |
| 4150 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4151 | if (!expression) |
| 4152 | return NULL; |
| 4153 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4154 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4155 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4156 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4157 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4158 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4159 | exc->n_col_offset, |
| 4160 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4161 | } |
| 4162 | else if (NCH(exc) == 4) { |
| 4163 | asdl_seq *suite_seq; |
| 4164 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4165 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4166 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4167 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4168 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4169 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4170 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4171 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4172 | return NULL; |
| 4173 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4174 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4175 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4176 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4177 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4178 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4179 | exc->n_col_offset, |
| 4180 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4181 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4182 | |
| 4183 | PyErr_Format(PyExc_SystemError, |
| 4184 | "wrong number of children for 'except' clause: %d", |
| 4185 | NCH(exc)); |
| 4186 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | static stmt_ty |
| 4190 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4191 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4192 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4193 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4194 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4195 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4196 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4197 | REQ(n, try_stmt); |
| 4198 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4199 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4200 | if (body == NULL) |
| 4201 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4202 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4203 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4204 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4205 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4206 | /* we can assume it's an "else", |
| 4207 | because nch >= 9 for try-else-finally and |
| 4208 | it would otherwise have a type of except_clause */ |
| 4209 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4210 | if (orelse == NULL) |
| 4211 | return NULL; |
| 4212 | n_except--; |
| 4213 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4214 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4215 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4216 | if (finally == NULL) |
| 4217 | return NULL; |
| 4218 | n_except--; |
| 4219 | } |
| 4220 | else { |
| 4221 | /* we can assume it's an "else", |
| 4222 | otherwise it would have a type of except_clause */ |
| 4223 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4224 | if (orelse == NULL) |
| 4225 | return NULL; |
| 4226 | n_except--; |
| 4227 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4228 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4229 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4230 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4231 | return NULL; |
| 4232 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4234 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4235 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4236 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4237 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4238 | if (handlers == NULL) |
| 4239 | return NULL; |
| 4240 | |
| 4241 | for (i = 0; i < n_except; i++) { |
| 4242 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4243 | CHILD(n, 5 + i * 3)); |
| 4244 | if (!e) |
| 4245 | return NULL; |
| 4246 | asdl_seq_SET(handlers, i, e); |
| 4247 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4250 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4251 | if (finally != NULL) { |
| 4252 | // finally is always last |
| 4253 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4254 | } else if (orelse != NULL) { |
| 4255 | // otherwise else is last |
| 4256 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4257 | } else { |
| 4258 | // inline the get_last_end_pos logic due to layout mismatch |
| 4259 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4260 | end_lineno = last_handler->end_lineno; |
| 4261 | end_col_offset = last_handler->end_col_offset; |
| 4262 | } |
| 4263 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4264 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4267 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4268 | static withitem_ty |
| 4269 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4270 | { |
| 4271 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4272 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4273 | REQ(n, with_item); |
| 4274 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4275 | if (!context_expr) |
| 4276 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4277 | if (NCH(n) == 3) { |
| 4278 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4279 | |
| 4280 | if (!optional_vars) { |
| 4281 | return NULL; |
| 4282 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4283 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4284 | return NULL; |
| 4285 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4288 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4291 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4292 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4293 | 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] | 4294 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4295 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4296 | 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] | 4297 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4298 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4299 | |
| 4300 | REQ(n, with_stmt); |
| 4301 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4302 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4303 | nch_minus_type = NCH(n) - has_type_comment; |
| 4304 | |
| 4305 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4306 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4307 | if (!items) |
| 4308 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4309 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4310 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4311 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4312 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4313 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4316 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4317 | if (!body) |
| 4318 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4319 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4320 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4321 | if (has_type_comment) { |
| 4322 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4323 | if (!type_comment) |
| 4324 | return NULL; |
| 4325 | } |
| 4326 | else |
| 4327 | type_comment = NULL; |
| 4328 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4329 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4330 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4331 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4332 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4333 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4334 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4337 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4338 | 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] | 4339 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4340 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4341 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4342 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4343 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4344 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4345 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4346 | REQ(n, classdef); |
| 4347 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4348 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4349 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4350 | if (!s) |
| 4351 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4352 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4353 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4354 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4355 | if (!classname) |
| 4356 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4357 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4358 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4359 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4360 | LINENO(n), n->n_col_offset, |
| 4361 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4362 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4363 | |
| 4364 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4365 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4366 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4367 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4368 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4369 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4370 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4371 | if (!classname) |
| 4372 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4373 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4374 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4375 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4376 | LINENO(n), n->n_col_offset, |
| 4377 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4380 | /* class NAME '(' arglist ')' ':' suite */ |
| 4381 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4382 | { |
| 4383 | PyObject *dummy_name; |
| 4384 | expr_ty dummy; |
| 4385 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4386 | if (!dummy_name) |
| 4387 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4388 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4389 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4390 | c->c_arena); |
| 4391 | 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] | 4392 | if (!call) |
| 4393 | return NULL; |
| 4394 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4395 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4396 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4397 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4398 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4399 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4400 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4401 | if (!classname) |
| 4402 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4403 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4404 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4405 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4406 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4407 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4408 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | static stmt_ty |
| 4412 | ast_for_stmt(struct compiling *c, const node *n) |
| 4413 | { |
| 4414 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4415 | assert(NCH(n) == 1); |
| 4416 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4417 | } |
| 4418 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4419 | assert(num_stmts(n) == 1); |
| 4420 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4421 | } |
| 4422 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4423 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4424 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4425 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4426 | */ |
| 4427 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4428 | case expr_stmt: |
| 4429 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4430 | case del_stmt: |
| 4431 | return ast_for_del_stmt(c, n); |
| 4432 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4433 | return Pass(LINENO(n), n->n_col_offset, |
| 4434 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4435 | case flow_stmt: |
| 4436 | return ast_for_flow_stmt(c, n); |
| 4437 | case import_stmt: |
| 4438 | return ast_for_import_stmt(c, n); |
| 4439 | case global_stmt: |
| 4440 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4441 | case nonlocal_stmt: |
| 4442 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4443 | case assert_stmt: |
| 4444 | return ast_for_assert_stmt(c, n); |
| 4445 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4446 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4447 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4448 | TYPE(n), NCH(n)); |
| 4449 | return NULL; |
| 4450 | } |
| 4451 | } |
| 4452 | else { |
| 4453 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4454 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4455 | */ |
| 4456 | node *ch = CHILD(n, 0); |
| 4457 | REQ(n, compound_stmt); |
| 4458 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4459 | case if_stmt: |
| 4460 | return ast_for_if_stmt(c, ch); |
| 4461 | case while_stmt: |
| 4462 | return ast_for_while_stmt(c, ch); |
| 4463 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4464 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4465 | case try_stmt: |
| 4466 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4467 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4468 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4469 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4470 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4471 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4472 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | case decorated: |
| 4474 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4475 | case async_stmt: |
| 4476 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4477 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4478 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4479 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4480 | TYPE(n), NCH(n)); |
| 4481 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4482 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4483 | } |
| 4484 | } |
| 4485 | |
| 4486 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4487 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4488 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4489 | const char *end; |
| 4490 | long x; |
| 4491 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4492 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4493 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4494 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4495 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4496 | errno = 0; |
| 4497 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4498 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4499 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4500 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4501 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4502 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4503 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4504 | } |
| 4505 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4506 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4507 | if (*end == '\0') { |
| 4508 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4509 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4510 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4511 | } |
| 4512 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4513 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4514 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4515 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4516 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4517 | return NULL; |
| 4518 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4519 | } |
| 4520 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4521 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4522 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4523 | if (dx == -1.0 && PyErr_Occurred()) |
| 4524 | return NULL; |
| 4525 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4526 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4530 | parsenumber(struct compiling *c, const char *s) |
| 4531 | { |
| 4532 | char *dup, *end; |
| 4533 | PyObject *res = NULL; |
| 4534 | |
| 4535 | assert(s != NULL); |
| 4536 | |
| 4537 | if (strchr(s, '_') == NULL) { |
| 4538 | return parsenumber_raw(c, s); |
| 4539 | } |
| 4540 | /* Create a duplicate without underscores. */ |
| 4541 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4542 | if (dup == NULL) { |
| 4543 | return PyErr_NoMemory(); |
| 4544 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4545 | end = dup; |
| 4546 | for (; *s; s++) { |
| 4547 | if (*s != '_') { |
| 4548 | *end++ = *s; |
| 4549 | } |
| 4550 | } |
| 4551 | *end = '\0'; |
| 4552 | res = parsenumber_raw(c, dup); |
| 4553 | PyMem_Free(dup); |
| 4554 | return res; |
| 4555 | } |
| 4556 | |
| 4557 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4558 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4559 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4560 | const char *s, *t; |
| 4561 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4562 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4563 | while (s < end && (*s & 0x80)) s++; |
| 4564 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4565 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4568 | static int |
| 4569 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4570 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4571 | { |
| 4572 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4573 | first_invalid_escape_char); |
| 4574 | if (msg == NULL) { |
| 4575 | return -1; |
| 4576 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4577 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4578 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4579 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4580 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4581 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4582 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4583 | to get a more accurate error report */ |
| 4584 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4585 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4586 | } |
| 4587 | Py_DECREF(msg); |
| 4588 | return -1; |
| 4589 | } |
| 4590 | Py_DECREF(msg); |
| 4591 | return 0; |
| 4592 | } |
| 4593 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4594 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4595 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4596 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4597 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4598 | PyObject *v, *u; |
| 4599 | char *buf; |
| 4600 | char *p; |
| 4601 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4602 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4603 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4604 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4605 | return NULL; |
| 4606 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4607 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4608 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4609 | if (u == NULL) |
| 4610 | return NULL; |
| 4611 | p = buf = PyBytes_AsString(u); |
| 4612 | end = s + len; |
| 4613 | while (s < end) { |
| 4614 | if (*s == '\\') { |
| 4615 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4616 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4617 | strcpy(p, "u005c"); |
| 4618 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4619 | if (s >= end) |
| 4620 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4621 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4622 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4623 | if (*s & 0x80) { /* XXX inefficient */ |
| 4624 | PyObject *w; |
| 4625 | int kind; |
| 4626 | void *data; |
| 4627 | Py_ssize_t len, i; |
| 4628 | w = decode_utf8(c, &s, end); |
| 4629 | if (w == NULL) { |
| 4630 | Py_DECREF(u); |
| 4631 | return NULL; |
| 4632 | } |
| 4633 | kind = PyUnicode_KIND(w); |
| 4634 | data = PyUnicode_DATA(w); |
| 4635 | len = PyUnicode_GET_LENGTH(w); |
| 4636 | for (i = 0; i < len; i++) { |
| 4637 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4638 | sprintf(p, "\\U%08x", chr); |
| 4639 | p += 10; |
| 4640 | } |
| 4641 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4642 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4643 | Py_DECREF(w); |
| 4644 | } else { |
| 4645 | *p++ = *s++; |
| 4646 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4647 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4648 | len = p - buf; |
| 4649 | s = buf; |
| 4650 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4651 | const char *first_invalid_escape; |
| 4652 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4653 | |
| 4654 | if (v != NULL && first_invalid_escape != NULL) { |
| 4655 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4656 | /* We have not decref u before because first_invalid_escape points |
| 4657 | inside u. */ |
| 4658 | Py_XDECREF(u); |
| 4659 | Py_DECREF(v); |
| 4660 | return NULL; |
| 4661 | } |
| 4662 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4663 | Py_XDECREF(u); |
| 4664 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4667 | static PyObject * |
| 4668 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4669 | size_t len) |
| 4670 | { |
| 4671 | const char *first_invalid_escape; |
| 4672 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4673 | &first_invalid_escape); |
| 4674 | if (result == NULL) |
| 4675 | return NULL; |
| 4676 | |
| 4677 | if (first_invalid_escape != NULL) { |
| 4678 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4679 | Py_DECREF(result); |
| 4680 | return NULL; |
| 4681 | } |
| 4682 | } |
| 4683 | return result; |
| 4684 | } |
| 4685 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4686 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4687 | and `col_offset` to existing locations. */ |
| 4688 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4689 | { |
| 4690 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4691 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4692 | for (int i = 0; i < NCH(n); ++i) { |
| 4693 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4694 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4695 | col_offset = 0; |
| 4696 | } |
| 4697 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4698 | } |
| 4699 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4700 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | /* Fix locations for the given node and its children. |
| 4704 | |
| 4705 | `parent` is the enclosing node. |
| 4706 | `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] | 4707 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4708 | */ |
| 4709 | static void |
| 4710 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4711 | { |
| 4712 | char *substr = NULL; |
| 4713 | char *start; |
| 4714 | int lines = LINENO(parent) - 1; |
| 4715 | int cols = parent->n_col_offset; |
| 4716 | /* Find the full fstring to fix location information in `n`. */ |
| 4717 | while (parent && parent->n_type != STRING) |
| 4718 | parent = parent->n_child; |
| 4719 | if (parent && parent->n_str) { |
| 4720 | substr = strstr(parent->n_str, expr_str); |
| 4721 | if (substr) { |
| 4722 | start = substr; |
| 4723 | while (start > parent->n_str) { |
| 4724 | if (start[0] == '\n') |
| 4725 | break; |
| 4726 | start--; |
| 4727 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4728 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4729 | /* adjust the start based on the number of newlines encountered |
| 4730 | before the f-string expression */ |
| 4731 | for (char* p = parent->n_str; p < substr; p++) { |
| 4732 | if (*p == '\n') { |
| 4733 | lines++; |
| 4734 | } |
| 4735 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4736 | } |
| 4737 | } |
| 4738 | fstring_shift_node_locations(n, lines, cols); |
| 4739 | } |
| 4740 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4741 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4742 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4743 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4744 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4745 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4746 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4747 | { |
| 4748 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4749 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4750 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4751 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4752 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4753 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4754 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4755 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4756 | assert(*(expr_start-1) == '{'); |
| 4757 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4758 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4759 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4760 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4761 | because turning the expression '' in to '()' would go from being invalid |
| 4762 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4763 | for (s = expr_start; s != expr_end; s++) { |
| 4764 | char c = *s; |
| 4765 | /* The Python parser ignores only the following whitespace |
| 4766 | characters (\r already is converted to \n). */ |
| 4767 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4768 | break; |
| 4769 | } |
| 4770 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4771 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4772 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4773 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4774 | } |
| 4775 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4776 | len = expr_end - expr_start; |
| 4777 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4778 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4779 | if (str == NULL) { |
| 4780 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4781 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4782 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4783 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4784 | str[0] = '('; |
| 4785 | memcpy(str+1, expr_start, len); |
| 4786 | str[len+1] = ')'; |
| 4787 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4788 | |
| 4789 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4790 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4791 | Py_eval_input, 0); |
| 4792 | if (!mod_n) { |
| 4793 | PyMem_RawFree(str); |
| 4794 | return NULL; |
| 4795 | } |
| 4796 | /* Reuse str to find the correct column offset. */ |
| 4797 | str[0] = '{'; |
| 4798 | str[len+1] = '}'; |
| 4799 | fstring_fix_node_location(n, mod_n, str); |
| 4800 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4801 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4802 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4803 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4804 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4805 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | /* Return -1 on error. |
| 4809 | |
| 4810 | Return 0 if we reached the end of the literal. |
| 4811 | |
| 4812 | Return 1 if we haven't reached the end of the literal, but we want |
| 4813 | the caller to process the literal up to this point. Used for |
| 4814 | doubled braces. |
| 4815 | */ |
| 4816 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4817 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4818 | PyObject **literal, int recurse_lvl, |
| 4819 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4820 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4821 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4822 | brace (which isn't part of a unicode name escape such as |
| 4823 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4824 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4825 | const char *s = *str; |
| 4826 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4827 | int result = 0; |
| 4828 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4829 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4830 | while (s < end) { |
| 4831 | char ch = *s++; |
| 4832 | if (!raw && ch == '\\' && s < end) { |
| 4833 | ch = *s++; |
| 4834 | if (ch == 'N') { |
| 4835 | if (s < end && *s++ == '{') { |
| 4836 | while (s < end && *s++ != '}') { |
| 4837 | } |
| 4838 | continue; |
| 4839 | } |
| 4840 | break; |
| 4841 | } |
| 4842 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4843 | return -1; |
| 4844 | } |
| 4845 | } |
| 4846 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4847 | /* Check for doubled braces, but only at the top level. If |
| 4848 | we checked at every level, then f'{0:{3}}' would fail |
| 4849 | with the two closing braces. */ |
| 4850 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4851 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4852 | /* We're going to tell the caller that the literal ends |
| 4853 | here, but that they should continue scanning. But also |
| 4854 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4855 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4856 | result = 1; |
| 4857 | goto done; |
| 4858 | } |
| 4859 | |
| 4860 | /* Where a single '{' is the start of a new expression, a |
| 4861 | single '}' is not allowed. */ |
| 4862 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4863 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4864 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4865 | return -1; |
| 4866 | } |
| 4867 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4868 | /* We're either at a '{', which means we're starting another |
| 4869 | expression; or a '}', which means we're at the end of this |
| 4870 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4871 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4872 | break; |
| 4873 | } |
| 4874 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4875 | *str = s; |
| 4876 | assert(s <= end); |
| 4877 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4878 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4879 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4880 | if (raw) |
| 4881 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4882 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4883 | NULL, NULL); |
| 4884 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4885 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4886 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4887 | if (!*literal) |
| 4888 | return -1; |
| 4889 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4890 | return result; |
| 4891 | } |
| 4892 | |
| 4893 | /* Forward declaration because parsing is recursive. */ |
| 4894 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4895 | 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] | 4896 | struct compiling *c, const node *n); |
| 4897 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4898 | /* 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] | 4899 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4900 | which includes the expression, conversion character, and |
| 4901 | format_spec expression. |
| 4902 | |
| 4903 | Note that I don't do a perfect job here: I don't make sure that a |
| 4904 | closing brace doesn't match an opening paren, for example. It |
| 4905 | doesn't need to error on all invalid expressions, just correctly |
| 4906 | find the end of all valid ones. Any errors inside the expression |
| 4907 | will be caught when we parse it later. */ |
| 4908 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4909 | 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] | 4910 | expr_ty *expression, struct compiling *c, const node *n) |
| 4911 | { |
| 4912 | /* Return -1 on error, else 0. */ |
| 4913 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4914 | const char *expr_start; |
| 4915 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4916 | expr_ty simple_expression; |
| 4917 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4918 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4919 | |
| 4920 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4921 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4922 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4923 | |
| 4924 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4925 | int string_type = 0; |
| 4926 | |
| 4927 | /* Keep track of nesting level for braces/parens/brackets in |
| 4928 | expressions. */ |
| 4929 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4930 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4931 | |
| 4932 | /* Can only nest one level deep. */ |
| 4933 | if (recurse_lvl >= 2) { |
| 4934 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4935 | return -1; |
| 4936 | } |
| 4937 | |
| 4938 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4939 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4940 | assert(**str == '{'); |
| 4941 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4942 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4943 | expr_start = *str; |
| 4944 | for (; *str < end; (*str)++) { |
| 4945 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4946 | |
| 4947 | /* Loop invariants. */ |
| 4948 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4949 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4950 | if (quote_char) |
| 4951 | assert(string_type == 1 || string_type == 3); |
| 4952 | else |
| 4953 | assert(string_type == 0); |
| 4954 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4955 | ch = **str; |
| 4956 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4957 | if (ch == '\\') { |
| 4958 | /* Error: can't include a backslash character, inside |
| 4959 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4960 | ast_error(c, n, |
| 4961 | "f-string expression part " |
| 4962 | "cannot include a backslash"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4963 | return -1; |
| 4964 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4965 | if (quote_char) { |
| 4966 | /* We're inside a string. See if we're at the end. */ |
| 4967 | /* This code needs to implement the same non-error logic |
| 4968 | as tok_get from tokenizer.c, at the letter_quote |
| 4969 | label. To actually share that code would be a |
| 4970 | nightmare. But, it's unlikely to change and is small, |
| 4971 | so duplicate it here. Note we don't need to catch all |
| 4972 | of the errors, since they'll be caught when parsing the |
| 4973 | expression. We just need to match the non-error |
| 4974 | cases. Thus we can ignore \n in single-quoted strings, |
| 4975 | for example. Or non-terminated strings. */ |
| 4976 | if (ch == quote_char) { |
| 4977 | /* Does this match the string_type (single or triple |
| 4978 | quoted)? */ |
| 4979 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4980 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4981 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4982 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4983 | string_type = 0; |
| 4984 | quote_char = 0; |
| 4985 | continue; |
| 4986 | } |
| 4987 | } else { |
| 4988 | /* We're at the end of a normal string. */ |
| 4989 | quote_char = 0; |
| 4990 | string_type = 0; |
| 4991 | continue; |
| 4992 | } |
| 4993 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4994 | } else if (ch == '\'' || ch == '"') { |
| 4995 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4996 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4997 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4998 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4999 | } else { |
| 5000 | /* Start of a normal string. */ |
| 5001 | string_type = 1; |
| 5002 | } |
| 5003 | /* Start looking for the end of the string. */ |
| 5004 | quote_char = ch; |
| 5005 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5006 | if (nested_depth >= MAXLEVEL) { |
| 5007 | ast_error(c, n, "f-string: too many nested parenthesis"); |
| 5008 | return -1; |
| 5009 | } |
| 5010 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5011 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5012 | } else if (ch == '#') { |
| 5013 | /* Error: can't include a comment character, inside parens |
| 5014 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5015 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5016 | return -1; |
| 5017 | } else if (nested_depth == 0 && |
| 5018 | (ch == '!' || ch == ':' || ch == '}')) { |
| 5019 | /* First, test for the special case of "!=". Since '=' is |
| 5020 | not an allowed conversion character, nothing is lost in |
| 5021 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5022 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5023 | /* This isn't a conversion character, just continue. */ |
| 5024 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5025 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5026 | /* Normal way out of this loop. */ |
| 5027 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5028 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5029 | if (!nested_depth) { |
| 5030 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
| 5031 | return -1; |
| 5032 | } |
| 5033 | nested_depth--; |
| 5034 | int opening = parenstack[nested_depth]; |
| 5035 | if (!((opening == '(' && ch == ')') || |
| 5036 | (opening == '[' && ch == ']') || |
| 5037 | (opening == '{' && ch == '}'))) |
| 5038 | { |
| 5039 | ast_error(c, n, |
| 5040 | "f-string: closing parenthesis '%c' " |
| 5041 | "does not match opening parenthesis '%c'", |
| 5042 | ch, opening); |
| 5043 | return -1; |
| 5044 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5045 | } else { |
| 5046 | /* Just consume this char and loop around. */ |
| 5047 | } |
| 5048 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5049 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5050 | /* If we leave this loop in a string or with mismatched parens, we |
| 5051 | don't care. We'll get a syntax error when compiling the |
| 5052 | expression. But, we can produce a better error message, so |
| 5053 | let's just do that.*/ |
| 5054 | if (quote_char) { |
| 5055 | ast_error(c, n, "f-string: unterminated string"); |
| 5056 | return -1; |
| 5057 | } |
| 5058 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5059 | int opening = parenstack[nested_depth - 1]; |
| 5060 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5061 | return -1; |
| 5062 | } |
| 5063 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5064 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5065 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5066 | |
| 5067 | /* Compile the expression as soon as possible, so we show errors |
| 5068 | related to the expression before errors related to the |
| 5069 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5070 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5071 | if (!simple_expression) |
| 5072 | return -1; |
| 5073 | |
| 5074 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5075 | if (**str == '!') { |
| 5076 | *str += 1; |
| 5077 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5078 | goto unexpected_end_of_string; |
| 5079 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5080 | conversion = **str; |
| 5081 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5082 | |
| 5083 | /* Validate the conversion. */ |
| 5084 | if (!(conversion == 's' || conversion == 'r' |
| 5085 | || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5086 | ast_error(c, n, |
| 5087 | "f-string: invalid conversion character: " |
| 5088 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5089 | return -1; |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5094 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5095 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5096 | if (**str == ':') { |
| 5097 | *str += 1; |
| 5098 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5099 | goto unexpected_end_of_string; |
| 5100 | |
| 5101 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5102 | 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] | 5103 | if (!format_spec) |
| 5104 | return -1; |
| 5105 | } |
| 5106 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5107 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5108 | goto unexpected_end_of_string; |
| 5109 | |
| 5110 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5111 | assert(*str < end); |
| 5112 | assert(**str == '}'); |
| 5113 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5114 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5115 | /* And now create the FormattedValue node that represents this |
| 5116 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5117 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5118 | format_spec, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5119 | n->n_end_lineno, n->n_end_col_offset, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5120 | c->c_arena); |
| 5121 | if (!*expression) |
| 5122 | return -1; |
| 5123 | |
| 5124 | return 0; |
| 5125 | |
| 5126 | unexpected_end_of_string: |
| 5127 | ast_error(c, n, "f-string: expecting '}'"); |
| 5128 | return -1; |
| 5129 | } |
| 5130 | |
| 5131 | /* Return -1 on error. |
| 5132 | |
| 5133 | Return 0 if we have a literal (possible zero length) and an |
| 5134 | expression (zero length if at the end of the string. |
| 5135 | |
| 5136 | Return 1 if we have a literal, but no expression, and we want the |
| 5137 | caller to call us again. This is used to deal with doubled |
| 5138 | braces. |
| 5139 | |
| 5140 | When called multiple times on the string 'a{{b{0}c', this function |
| 5141 | will return: |
| 5142 | |
| 5143 | 1. the literal 'a{' with no expression, and a return value |
| 5144 | of 1. Despite the fact that there's no expression, the return |
| 5145 | value of 1 means we're not finished yet. |
| 5146 | |
| 5147 | 2. the literal 'b' and the expression '0', with a return value of |
| 5148 | 0. The fact that there's an expression means we're not finished. |
| 5149 | |
| 5150 | 3. literal 'c' with no expression and a return value of 0. The |
| 5151 | combination of the return value of 0 with no expression means |
| 5152 | we're finished. |
| 5153 | */ |
| 5154 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5155 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5156 | int recurse_lvl, PyObject **literal, |
| 5157 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5158 | struct compiling *c, const node *n) |
| 5159 | { |
| 5160 | int result; |
| 5161 | |
| 5162 | assert(*literal == NULL && *expression == NULL); |
| 5163 | |
| 5164 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5165 | 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] | 5166 | if (result < 0) |
| 5167 | goto error; |
| 5168 | |
| 5169 | assert(result == 0 || result == 1); |
| 5170 | |
| 5171 | if (result == 1) |
| 5172 | /* We have a literal, but don't look at the expression. */ |
| 5173 | return 1; |
| 5174 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5175 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5176 | /* We're at the end of the string or the end of a nested |
| 5177 | f-string: no expression. The top-level error case where we |
| 5178 | expect to be at the end of the string but we're at a '}' is |
| 5179 | handled later. */ |
| 5180 | return 0; |
| 5181 | |
| 5182 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5184 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5185 | 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] | 5186 | goto error; |
| 5187 | |
| 5188 | return 0; |
| 5189 | |
| 5190 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5191 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5192 | return -1; |
| 5193 | } |
| 5194 | |
| 5195 | #define EXPRLIST_N_CACHED 64 |
| 5196 | |
| 5197 | typedef struct { |
| 5198 | /* Incrementally build an array of expr_ty, so be used in an |
| 5199 | asdl_seq. Cache some small but reasonably sized number of |
| 5200 | expr_ty's, and then after that start dynamically allocating, |
| 5201 | doubling the number allocated each time. Note that the f-string |
| 5202 | 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] | 5203 | 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] | 5204 | fast as you add exressions in an f-string. */ |
| 5205 | |
| 5206 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5207 | Py_ssize_t size; /* Number we've used. */ |
| 5208 | expr_ty *p; /* Pointer to the memory we're actually |
| 5209 | using. Will point to 'data' until we |
| 5210 | start dynamically allocating. */ |
| 5211 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5212 | } ExprList; |
| 5213 | |
| 5214 | #ifdef NDEBUG |
| 5215 | #define ExprList_check_invariants(l) |
| 5216 | #else |
| 5217 | static void |
| 5218 | ExprList_check_invariants(ExprList *l) |
| 5219 | { |
| 5220 | /* Check our invariants. Make sure this object is "live", and |
| 5221 | hasn't been deallocated. */ |
| 5222 | assert(l->size >= 0); |
| 5223 | assert(l->p != NULL); |
| 5224 | if (l->size <= EXPRLIST_N_CACHED) |
| 5225 | assert(l->data == l->p); |
| 5226 | } |
| 5227 | #endif |
| 5228 | |
| 5229 | static void |
| 5230 | ExprList_Init(ExprList *l) |
| 5231 | { |
| 5232 | l->allocated = EXPRLIST_N_CACHED; |
| 5233 | l->size = 0; |
| 5234 | |
| 5235 | /* Until we start allocating dynamically, p points to data. */ |
| 5236 | l->p = l->data; |
| 5237 | |
| 5238 | ExprList_check_invariants(l); |
| 5239 | } |
| 5240 | |
| 5241 | static int |
| 5242 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5243 | { |
| 5244 | ExprList_check_invariants(l); |
| 5245 | if (l->size >= l->allocated) { |
| 5246 | /* We need to alloc (or realloc) the memory. */ |
| 5247 | Py_ssize_t new_size = l->allocated * 2; |
| 5248 | |
| 5249 | /* See if we've ever allocated anything dynamically. */ |
| 5250 | if (l->p == l->data) { |
| 5251 | Py_ssize_t i; |
| 5252 | /* We're still using the cached data. Switch to |
| 5253 | alloc-ing. */ |
| 5254 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5255 | if (!l->p) |
| 5256 | return -1; |
| 5257 | /* Copy the cached data into the new buffer. */ |
| 5258 | for (i = 0; i < l->size; i++) |
| 5259 | l->p[i] = l->data[i]; |
| 5260 | } else { |
| 5261 | /* Just realloc. */ |
| 5262 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5263 | if (!tmp) { |
| 5264 | PyMem_RawFree(l->p); |
| 5265 | l->p = NULL; |
| 5266 | return -1; |
| 5267 | } |
| 5268 | l->p = tmp; |
| 5269 | } |
| 5270 | |
| 5271 | l->allocated = new_size; |
| 5272 | assert(l->allocated == 2 * l->size); |
| 5273 | } |
| 5274 | |
| 5275 | l->p[l->size++] = exp; |
| 5276 | |
| 5277 | ExprList_check_invariants(l); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
| 5281 | static void |
| 5282 | ExprList_Dealloc(ExprList *l) |
| 5283 | { |
| 5284 | ExprList_check_invariants(l); |
| 5285 | |
| 5286 | /* If there's been an error, or we've never dynamically allocated, |
| 5287 | do nothing. */ |
| 5288 | if (!l->p || l->p == l->data) { |
| 5289 | /* Do nothing. */ |
| 5290 | } else { |
| 5291 | /* We have dynamically allocated. Free the memory. */ |
| 5292 | PyMem_RawFree(l->p); |
| 5293 | } |
| 5294 | l->p = NULL; |
| 5295 | l->size = -1; |
| 5296 | } |
| 5297 | |
| 5298 | static asdl_seq * |
| 5299 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5300 | { |
| 5301 | asdl_seq *seq; |
| 5302 | |
| 5303 | ExprList_check_invariants(l); |
| 5304 | |
| 5305 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5306 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5307 | if (seq) { |
| 5308 | Py_ssize_t i; |
| 5309 | for (i = 0; i < l->size; i++) |
| 5310 | asdl_seq_SET(seq, i, l->p[i]); |
| 5311 | } |
| 5312 | ExprList_Dealloc(l); |
| 5313 | return seq; |
| 5314 | } |
| 5315 | |
| 5316 | /* The FstringParser is designed to add a mix of strings and |
| 5317 | f-strings, and concat them together as needed. Ultimately, it |
| 5318 | generates an expr_ty. */ |
| 5319 | typedef struct { |
| 5320 | PyObject *last_str; |
| 5321 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5322 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5323 | } FstringParser; |
| 5324 | |
| 5325 | #ifdef NDEBUG |
| 5326 | #define FstringParser_check_invariants(state) |
| 5327 | #else |
| 5328 | static void |
| 5329 | FstringParser_check_invariants(FstringParser *state) |
| 5330 | { |
| 5331 | if (state->last_str) |
| 5332 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5333 | ExprList_check_invariants(&state->expr_list); |
| 5334 | } |
| 5335 | #endif |
| 5336 | |
| 5337 | static void |
| 5338 | FstringParser_Init(FstringParser *state) |
| 5339 | { |
| 5340 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5341 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5342 | ExprList_Init(&state->expr_list); |
| 5343 | FstringParser_check_invariants(state); |
| 5344 | } |
| 5345 | |
| 5346 | static void |
| 5347 | FstringParser_Dealloc(FstringParser *state) |
| 5348 | { |
| 5349 | FstringParser_check_invariants(state); |
| 5350 | |
| 5351 | Py_XDECREF(state->last_str); |
| 5352 | ExprList_Dealloc(&state->expr_list); |
| 5353 | } |
| 5354 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5355 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5356 | static expr_ty |
| 5357 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5358 | { |
| 5359 | PyObject *s = *str; |
| 5360 | *str = NULL; |
| 5361 | assert(PyUnicode_CheckExact(s)); |
| 5362 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5363 | Py_DECREF(s); |
| 5364 | return NULL; |
| 5365 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5366 | return Constant(s, LINENO(n), n->n_col_offset, |
| 5367 | 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] | 5368 | } |
| 5369 | |
| 5370 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5371 | decref'd. */ |
| 5372 | static int |
| 5373 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5374 | { |
| 5375 | FstringParser_check_invariants(state); |
| 5376 | |
| 5377 | assert(PyUnicode_CheckExact(str)); |
| 5378 | |
| 5379 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5380 | Py_DECREF(str); |
| 5381 | return 0; |
| 5382 | } |
| 5383 | |
| 5384 | if (!state->last_str) { |
| 5385 | /* We didn't have a string before, so just remember this one. */ |
| 5386 | state->last_str = str; |
| 5387 | } else { |
| 5388 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5389 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5390 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5391 | return -1; |
| 5392 | } |
| 5393 | FstringParser_check_invariants(state); |
| 5394 | return 0; |
| 5395 | } |
| 5396 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5397 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5398 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5399 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5400 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5401 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5402 | struct compiling *c, const node *n) |
| 5403 | { |
| 5404 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5405 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5406 | |
| 5407 | /* Parse the f-string. */ |
| 5408 | while (1) { |
| 5409 | PyObject *literal = NULL; |
| 5410 | expr_ty expression = NULL; |
| 5411 | |
| 5412 | /* If there's a zero length literal in front of the |
| 5413 | expression, literal will be NULL. If we're at the end of |
| 5414 | the f-string, expression will be NULL (unless result == 1, |
| 5415 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5416 | 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] | 5417 | &literal, &expression, |
| 5418 | c, n); |
| 5419 | if (result < 0) |
| 5420 | return -1; |
| 5421 | |
| 5422 | /* Add the literal, if any. */ |
| 5423 | if (!literal) { |
| 5424 | /* Do nothing. Just leave last_str alone (and possibly |
| 5425 | NULL). */ |
| 5426 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 5427 | /* Note that the literal can be zero length, if the |
| 5428 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5429 | state->last_str = literal; |
| 5430 | literal = NULL; |
| 5431 | } else { |
| 5432 | /* We have a literal, concatenate it. */ |
| 5433 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 5434 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 5435 | return -1; |
| 5436 | literal = NULL; |
| 5437 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5438 | |
| 5439 | /* We've dealt with the literal now. It can't be leaked on further |
| 5440 | errors. */ |
| 5441 | assert(literal == NULL); |
| 5442 | |
| 5443 | /* See if we should just loop around to get the next literal |
| 5444 | and expression, while ignoring the expression this |
| 5445 | time. This is used for un-doubling braces, as an |
| 5446 | optimization. */ |
| 5447 | if (result == 1) |
| 5448 | continue; |
| 5449 | |
| 5450 | if (!expression) |
| 5451 | /* We're done with this f-string. */ |
| 5452 | break; |
| 5453 | |
| 5454 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5455 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5456 | if (!state->last_str) { |
| 5457 | /* Do nothing. No previous literal. */ |
| 5458 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5459 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5460 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5461 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5462 | return -1; |
| 5463 | } |
| 5464 | |
| 5465 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5466 | return -1; |
| 5467 | } |
| 5468 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5469 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5470 | string. Otherwise, we must be at a right brace. */ |
| 5471 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5472 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5473 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5474 | return -1; |
| 5475 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5476 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5477 | ast_error(c, n, "f-string: expecting '}'"); |
| 5478 | return -1; |
| 5479 | } |
| 5480 | |
| 5481 | FstringParser_check_invariants(state); |
| 5482 | return 0; |
| 5483 | } |
| 5484 | |
| 5485 | /* 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] | 5486 | 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] | 5487 | static expr_ty |
| 5488 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5489 | const node *n) |
| 5490 | { |
| 5491 | asdl_seq *seq; |
| 5492 | |
| 5493 | FstringParser_check_invariants(state); |
| 5494 | |
| 5495 | /* If we're just a constant string with no expressions, return |
| 5496 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5497 | if (!state->fmode) { |
| 5498 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5499 | if (!state->last_str) { |
| 5500 | /* Create a zero length string. */ |
| 5501 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5502 | if (!state->last_str) |
| 5503 | goto error; |
| 5504 | } |
| 5505 | return make_str_node_and_del(&state->last_str, c, n); |
| 5506 | } |
| 5507 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5508 | /* 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] | 5509 | last node in our expression list. */ |
| 5510 | if (state->last_str) { |
| 5511 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5512 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5513 | goto error; |
| 5514 | } |
| 5515 | /* This has already been freed. */ |
| 5516 | assert(state->last_str == NULL); |
| 5517 | |
| 5518 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5519 | if (!seq) |
| 5520 | goto error; |
| 5521 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5522 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5523 | 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] | 5524 | |
| 5525 | error: |
| 5526 | FstringParser_Dealloc(state); |
| 5527 | return NULL; |
| 5528 | } |
| 5529 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5530 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5531 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5532 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5533 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5534 | 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] | 5535 | struct compiling *c, const node *n) |
| 5536 | { |
| 5537 | FstringParser state; |
| 5538 | |
| 5539 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5540 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5541 | c, n) < 0) { |
| 5542 | FstringParser_Dealloc(&state); |
| 5543 | return NULL; |
| 5544 | } |
| 5545 | |
| 5546 | return FstringParser_Finish(&state, c, n); |
| 5547 | } |
| 5548 | |
| 5549 | /* n is a Python string literal, including the bracketing quote |
| 5550 | 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] | 5551 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5552 | 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] | 5553 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5554 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5555 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5556 | static int |
| 5557 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5558 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5559 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5560 | size_t len; |
| 5561 | const char *s = STR(n); |
| 5562 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5563 | int fmode = 0; |
| 5564 | *bytesmode = 0; |
| 5565 | *rawmode = 0; |
| 5566 | *result = NULL; |
| 5567 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5568 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5569 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5570 | if (quote == 'b' || quote == 'B') { |
| 5571 | quote = *++s; |
| 5572 | *bytesmode = 1; |
| 5573 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5574 | else if (quote == 'u' || quote == 'U') { |
| 5575 | quote = *++s; |
| 5576 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5577 | else if (quote == 'r' || quote == 'R') { |
| 5578 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5579 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5580 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5581 | else if (quote == 'f' || quote == 'F') { |
| 5582 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5583 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5584 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5585 | else { |
| 5586 | break; |
| 5587 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5588 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5589 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5590 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5591 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5592 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5593 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5594 | if (quote != '\'' && quote != '\"') { |
| 5595 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5596 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5597 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5598 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5599 | s++; |
| 5600 | len = strlen(s); |
| 5601 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5602 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5603 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5604 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5605 | } |
| 5606 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5607 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5608 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5609 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5610 | } |
| 5611 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5612 | /* A triple quoted string. We've already skipped one quote at |
| 5613 | the start and one at the end of the string. Now skip the |
| 5614 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5615 | s += 2; |
| 5616 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5617 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5618 | if (s[--len] != quote || s[--len] != quote) { |
| 5619 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5620 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5621 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5622 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5623 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5624 | if (fmode) { |
| 5625 | /* Just return the bytes. The caller will parse the resulting |
| 5626 | string. */ |
| 5627 | *fstr = s; |
| 5628 | *fstrlen = len; |
| 5629 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5630 | } |
| 5631 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5632 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5633 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5634 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5635 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5636 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5637 | const char *ch; |
| 5638 | for (ch = s; *ch; ch++) { |
| 5639 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5640 | ast_error(c, n, |
| 5641 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5642 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5643 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5644 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5645 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5646 | if (*rawmode) |
| 5647 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5648 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5649 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5650 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5651 | if (*rawmode) |
| 5652 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5653 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5654 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5655 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5656 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5659 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5660 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5661 | 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] | 5662 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5663 | 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] | 5664 | node if there's just an f-string (with no leading or trailing |
| 5665 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5666 | any literals involved. */ |
| 5667 | static expr_ty |
| 5668 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5669 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5670 | int bytesmode = 0; |
| 5671 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5672 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5673 | |
| 5674 | FstringParser state; |
| 5675 | FstringParser_Init(&state); |
| 5676 | |
| 5677 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5678 | int this_bytesmode; |
| 5679 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5680 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5681 | const char *fstr; |
| 5682 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5683 | |
| 5684 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5685 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5686 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5687 | goto error; |
| 5688 | |
| 5689 | /* Check that we're not mixing bytes with unicode. */ |
| 5690 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5691 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5692 | /* s is NULL if the current string part is an f-string. */ |
| 5693 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5694 | goto error; |
| 5695 | } |
| 5696 | bytesmode = this_bytesmode; |
| 5697 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5698 | if (fstr != NULL) { |
| 5699 | int result; |
| 5700 | assert(s == NULL && !bytesmode); |
| 5701 | /* This is an f-string. Parse and concatenate it. */ |
| 5702 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5703 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5704 | if (result < 0) |
| 5705 | goto error; |
| 5706 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5707 | /* A string or byte string. */ |
| 5708 | assert(s != NULL && fstr == NULL); |
| 5709 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5710 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5711 | PyUnicode_CheckExact(s)); |
| 5712 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5713 | if (bytesmode) { |
| 5714 | /* For bytes, concat as we go. */ |
| 5715 | if (i == 0) { |
| 5716 | /* First time, just remember this value. */ |
| 5717 | bytes_str = s; |
| 5718 | } else { |
| 5719 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5720 | if (!bytes_str) |
| 5721 | goto error; |
| 5722 | } |
| 5723 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5724 | /* This is a regular string. Concatenate it. */ |
| 5725 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5726 | goto error; |
| 5727 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5728 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5729 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5730 | if (bytesmode) { |
| 5731 | /* Just return the bytes object and we're done. */ |
| 5732 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5733 | goto error; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5734 | return Constant(bytes_str, LINENO(n), n->n_col_offset, |
| 5735 | 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] | 5736 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5737 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5738 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5739 | assert(bytes_str == NULL); |
| 5740 | |
| 5741 | return FstringParser_Finish(&state, c, n); |
| 5742 | |
| 5743 | error: |
| 5744 | Py_XDECREF(bytes_str); |
| 5745 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5746 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5747 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5748 | |
| 5749 | PyObject * |
| 5750 | _PyAST_GetDocString(asdl_seq *body) |
| 5751 | { |
| 5752 | if (!asdl_seq_LEN(body)) { |
| 5753 | return NULL; |
| 5754 | } |
| 5755 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5756 | if (st->kind != Expr_kind) { |
| 5757 | return NULL; |
| 5758 | } |
| 5759 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5760 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5761 | return e->v.Constant.value; |
| 5762 | } |
| 5763 | return NULL; |
| 5764 | } |