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); |
| 705 | if (PyArena_AddPyObject(c->c_arena, res) < 0) { |
| 706 | Py_DECREF(res); |
| 707 | return NULL; |
| 708 | } |
| 709 | return res; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 710 | } |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 711 | #define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 712 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 713 | static int |
| 714 | num_stmts(const node *n) |
| 715 | { |
| 716 | int i, l; |
| 717 | node *ch; |
| 718 | |
| 719 | switch (TYPE(n)) { |
| 720 | case single_input: |
| 721 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 722 | return 0; |
| 723 | else |
| 724 | return num_stmts(CHILD(n, 0)); |
| 725 | case file_input: |
| 726 | l = 0; |
| 727 | for (i = 0; i < NCH(n); i++) { |
| 728 | ch = CHILD(n, i); |
| 729 | if (TYPE(ch) == stmt) |
| 730 | l += num_stmts(ch); |
| 731 | } |
| 732 | return l; |
| 733 | case stmt: |
| 734 | return num_stmts(CHILD(n, 0)); |
| 735 | case compound_stmt: |
| 736 | return 1; |
| 737 | case simple_stmt: |
| 738 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 739 | case suite: |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 740 | case func_body_suite: |
| 741 | /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
| 742 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 743 | if (NCH(n) == 1) |
| 744 | return num_stmts(CHILD(n, 0)); |
| 745 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 746 | i = 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 747 | l = 0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 748 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) |
| 749 | i += 2; |
| 750 | for (; i < (NCH(n) - 1); i++) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 751 | l += num_stmts(CHILD(n, i)); |
| 752 | return l; |
| 753 | } |
| 754 | default: { |
| 755 | char buf[128]; |
| 756 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 757 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 758 | TYPE(n), NCH(n)); |
| 759 | Py_FatalError(buf); |
| 760 | } |
| 761 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 762 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* Transform the CST rooted at node * to the appropriate AST |
| 766 | */ |
| 767 | |
| 768 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 769 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 770 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 771 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 772 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | asdl_seq *stmts = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 774 | asdl_seq *type_ignores = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 775 | stmt_ty s; |
| 776 | node *ch; |
| 777 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 778 | mod_ty res = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 779 | asdl_seq *argtypes = NULL; |
| 780 | expr_ty ret, arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 781 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 782 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 783 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 784 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 785 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 786 | |
| 787 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 788 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 790 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | switch (TYPE(n)) { |
| 792 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 793 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 795 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | for (i = 0; i < NCH(n) - 1; i++) { |
| 797 | ch = CHILD(n, i); |
| 798 | if (TYPE(ch) == NEWLINE) |
| 799 | continue; |
| 800 | REQ(ch, stmt); |
| 801 | num = num_stmts(ch); |
| 802 | if (num == 1) { |
| 803 | s = ast_for_stmt(&c, ch); |
| 804 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 805 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 806 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 807 | } |
| 808 | else { |
| 809 | ch = CHILD(ch, 0); |
| 810 | REQ(ch, simple_stmt); |
| 811 | for (j = 0; j < num; j++) { |
| 812 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 813 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 814 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 815 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 819 | |
| 820 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 821 | ch = CHILD(n, NCH(n) - 1); |
| 822 | REQ(ch, ENDMARKER); |
| 823 | num = NCH(ch); |
| 824 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 825 | if (!type_ignores) |
| 826 | goto out; |
| 827 | |
| 828 | for (i = 0; i < num; i++) { |
| 829 | type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena); |
| 830 | if (!ti) |
| 831 | goto out; |
| 832 | asdl_seq_SET(type_ignores, i, ti); |
| 833 | } |
| 834 | |
| 835 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 836 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | case eval_input: { |
| 838 | expr_ty testlist_ast; |
| 839 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 840 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 841 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 842 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 843 | goto out; |
| 844 | res = Expression(testlist_ast, arena); |
| 845 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 846 | } |
| 847 | case single_input: |
| 848 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 849 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 850 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 851 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 852 | 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] | 853 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 854 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 855 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 856 | goto out; |
| 857 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 858 | } |
| 859 | else { |
| 860 | n = CHILD(n, 0); |
| 861 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 862 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 863 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 864 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 866 | s = ast_for_stmt(&c, n); |
| 867 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 868 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 869 | asdl_seq_SET(stmts, 0, s); |
| 870 | } |
| 871 | else { |
| 872 | /* Only a simple_stmt can contain multiple statements. */ |
| 873 | REQ(n, simple_stmt); |
| 874 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 875 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 876 | break; |
| 877 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 878 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 879 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 880 | asdl_seq_SET(stmts, i / 2, s); |
| 881 | } |
| 882 | } |
| 883 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 884 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 885 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 886 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 887 | case func_type_input: |
| 888 | n = CHILD(n, 0); |
| 889 | REQ(n, func_type); |
| 890 | |
| 891 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 892 | ch = CHILD(n, 1); |
| 893 | /* this is overly permissive -- we don't pay any attention to |
| 894 | * stars on the args -- just parse them into an ordered list */ |
| 895 | num = 0; |
| 896 | for (i = 0; i < NCH(ch); i++) { |
| 897 | if (TYPE(CHILD(ch, i)) == test) { |
| 898 | num++; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | argtypes = _Py_asdl_seq_new(num, arena); |
| 903 | if (!argtypes) |
| 904 | goto out; |
| 905 | |
| 906 | j = 0; |
| 907 | for (i = 0; i < NCH(ch); i++) { |
| 908 | if (TYPE(CHILD(ch, i)) == test) { |
| 909 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 910 | if (!arg) |
| 911 | goto out; |
| 912 | asdl_seq_SET(argtypes, j++, arg); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | else { |
| 917 | argtypes = _Py_asdl_seq_new(0, arena); |
| 918 | if (!argtypes) |
| 919 | goto out; |
| 920 | } |
| 921 | |
| 922 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 923 | if (!ret) |
| 924 | goto out; |
| 925 | res = FunctionType(argtypes, ret, arena); |
| 926 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 928 | PyErr_Format(PyExc_SystemError, |
| 929 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 930 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 931 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 932 | out: |
| 933 | if (c.c_normalize) { |
| 934 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 935 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 936 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 939 | mod_ty |
| 940 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 941 | PyArena *arena) |
| 942 | { |
| 943 | mod_ty mod; |
| 944 | PyObject *filename; |
| 945 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 946 | if (filename == NULL) |
| 947 | return NULL; |
| 948 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 949 | Py_DECREF(filename); |
| 950 | return mod; |
| 951 | |
| 952 | } |
| 953 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 954 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 955 | */ |
| 956 | |
| 957 | static operator_ty |
| 958 | get_operator(const node *n) |
| 959 | { |
| 960 | switch (TYPE(n)) { |
| 961 | case VBAR: |
| 962 | return BitOr; |
| 963 | case CIRCUMFLEX: |
| 964 | return BitXor; |
| 965 | case AMPER: |
| 966 | return BitAnd; |
| 967 | case LEFTSHIFT: |
| 968 | return LShift; |
| 969 | case RIGHTSHIFT: |
| 970 | return RShift; |
| 971 | case PLUS: |
| 972 | return Add; |
| 973 | case MINUS: |
| 974 | return Sub; |
| 975 | case STAR: |
| 976 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 977 | case AT: |
| 978 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 979 | case SLASH: |
| 980 | return Div; |
| 981 | case DOUBLESLASH: |
| 982 | return FloorDiv; |
| 983 | case PERCENT: |
| 984 | return Mod; |
| 985 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 986 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 990 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 991 | "None", |
| 992 | "True", |
| 993 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 994 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 995 | NULL, |
| 996 | }; |
| 997 | |
| 998 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 999 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1000 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1001 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1002 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1003 | const char * const *p = FORBIDDEN; |
| 1004 | if (!full_checks) { |
| 1005 | /* In most cases, the parser will protect True, False, and None |
| 1006 | from being assign to. */ |
| 1007 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1008 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1009 | for (; *p; p++) { |
| 1010 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1011 | ast_error(c, n, "cannot assign to %U", name); |
| 1012 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1018 | static expr_ty |
| 1019 | copy_location(expr_ty e, const node *n) |
| 1020 | { |
| 1021 | if (e) { |
| 1022 | e->lineno = LINENO(n); |
| 1023 | e->col_offset = n->n_col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1024 | e->end_lineno = n->n_end_lineno; |
| 1025 | e->end_col_offset = n->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1026 | } |
| 1027 | return e; |
| 1028 | } |
| 1029 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1030 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1031 | |
| 1032 | Only sets context for expr kinds that "can appear in assignment context" |
| 1033 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1034 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1035 | */ |
| 1036 | |
| 1037 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1038 | 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] | 1039 | { |
| 1040 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1041 | /* If a particular expression type can't be used for assign / delete, |
| 1042 | set expr_name to its name and an error message will be generated. |
| 1043 | */ |
| 1044 | const char* expr_name = NULL; |
| 1045 | |
| 1046 | /* The ast defines augmented store and load contexts, but the |
| 1047 | implementation here doesn't actually use them. The code may be |
| 1048 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1049 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1050 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1051 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1052 | */ |
| 1053 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1054 | |
| 1055 | switch (e->kind) { |
| 1056 | case Attribute_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1057 | if (ctx == NamedStore) { |
| 1058 | expr_name = "attribute"; |
| 1059 | break; |
| 1060 | } |
| 1061 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1062 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1063 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1064 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1065 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1066 | case Subscript_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1067 | if (ctx == NamedStore) { |
| 1068 | expr_name = "subscript"; |
| 1069 | break; |
| 1070 | } |
| 1071 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1072 | e->v.Subscript.ctx = ctx; |
| 1073 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1074 | case Starred_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1075 | if (ctx == NamedStore) { |
| 1076 | expr_name = "starred"; |
| 1077 | break; |
| 1078 | } |
| 1079 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1080 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1081 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1082 | return 0; |
| 1083 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1084 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1085 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1086 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1087 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1088 | } |
| 1089 | e->v.Name.ctx = ctx; |
| 1090 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1091 | case List_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1092 | if (ctx == NamedStore) { |
| 1093 | expr_name = "list"; |
| 1094 | break; |
| 1095 | } |
| 1096 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1097 | e->v.List.ctx = ctx; |
| 1098 | s = e->v.List.elts; |
| 1099 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1100 | case Tuple_kind: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1101 | if (ctx == NamedStore) { |
| 1102 | expr_name = "tuple"; |
| 1103 | break; |
| 1104 | } |
| 1105 | |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1106 | e->v.Tuple.ctx = ctx; |
| 1107 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1108 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1109 | case Lambda_kind: |
| 1110 | expr_name = "lambda"; |
| 1111 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1112 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1113 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1114 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1115 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1116 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1117 | case UnaryOp_kind: |
| 1118 | expr_name = "operator"; |
| 1119 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1120 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1121 | expr_name = "generator expression"; |
| 1122 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1123 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1124 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1125 | expr_name = "yield expression"; |
| 1126 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1127 | case Await_kind: |
| 1128 | expr_name = "await expression"; |
| 1129 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1130 | case ListComp_kind: |
| 1131 | expr_name = "list comprehension"; |
| 1132 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1133 | case SetComp_kind: |
| 1134 | expr_name = "set comprehension"; |
| 1135 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1136 | case DictComp_kind: |
| 1137 | expr_name = "dict comprehension"; |
| 1138 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1139 | case Dict_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1140 | expr_name = "dict display"; |
| 1141 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1142 | case Set_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1143 | expr_name = "set display"; |
| 1144 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1145 | case JoinedStr_kind: |
| 1146 | case FormattedValue_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1147 | expr_name = "f-string expression"; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1148 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1149 | case Constant_kind: { |
| 1150 | PyObject *value = e->v.Constant.value; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1151 | if (value == Py_None || value == Py_False || value == Py_True |
| 1152 | || value == Py_Ellipsis) |
| 1153 | { |
| 1154 | return ast_error(c, n, "cannot %s %R", |
| 1155 | ctx == Store ? "assign to" : "delete", |
| 1156 | value); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1157 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1158 | expr_name = "literal"; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1159 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1160 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1161 | case Compare_kind: |
| 1162 | expr_name = "comparison"; |
| 1163 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1164 | case IfExp_kind: |
| 1165 | expr_name = "conditional expression"; |
| 1166 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1167 | case NamedExpr_kind: |
| 1168 | expr_name = "named expression"; |
| 1169 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1170 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | PyErr_Format(PyExc_SystemError, |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1172 | "unexpected expression in %sassignment %d (line %d)", |
| 1173 | ctx == NamedStore ? "named ": "", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1174 | e->kind, e->lineno); |
| 1175 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1176 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1177 | /* Check for error string set by switch */ |
| 1178 | if (expr_name) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1179 | if (ctx == NamedStore) { |
| 1180 | return ast_error(c, n, "cannot use named assignment with %s", |
| 1181 | expr_name); |
| 1182 | } |
| 1183 | else { |
| 1184 | return ast_error(c, n, "cannot %s %s", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1185 | ctx == Store ? "assign to" : "delete", |
| 1186 | expr_name); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1187 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1190 | /* 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] | 1191 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1192 | */ |
| 1193 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1194 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1195 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1196 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1197 | 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] | 1198 | return 0; |
| 1199 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1200 | } |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
| 1204 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1205 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1206 | { |
| 1207 | REQ(n, augassign); |
| 1208 | n = CHILD(n, 0); |
| 1209 | switch (STR(n)[0]) { |
| 1210 | case '+': |
| 1211 | return Add; |
| 1212 | case '-': |
| 1213 | return Sub; |
| 1214 | case '/': |
| 1215 | if (STR(n)[1] == '/') |
| 1216 | return FloorDiv; |
| 1217 | else |
| 1218 | return Div; |
| 1219 | case '%': |
| 1220 | return Mod; |
| 1221 | case '<': |
| 1222 | return LShift; |
| 1223 | case '>': |
| 1224 | return RShift; |
| 1225 | case '&': |
| 1226 | return BitAnd; |
| 1227 | case '^': |
| 1228 | return BitXor; |
| 1229 | case '|': |
| 1230 | return BitOr; |
| 1231 | case '*': |
| 1232 | if (STR(n)[1] == '*') |
| 1233 | return Pow; |
| 1234 | else |
| 1235 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1236 | case '@': |
| 1237 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1238 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1239 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1240 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1245 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1246 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1247 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | |'is' 'not' |
| 1249 | */ |
| 1250 | REQ(n, comp_op); |
| 1251 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1252 | n = CHILD(n, 0); |
| 1253 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1254 | case LESS: |
| 1255 | return Lt; |
| 1256 | case GREATER: |
| 1257 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1258 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1259 | return Eq; |
| 1260 | case LESSEQUAL: |
| 1261 | return LtE; |
| 1262 | case GREATEREQUAL: |
| 1263 | return GtE; |
| 1264 | case NOTEQUAL: |
| 1265 | return NotEq; |
| 1266 | case NAME: |
| 1267 | if (strcmp(STR(n), "in") == 0) |
| 1268 | return In; |
| 1269 | if (strcmp(STR(n), "is") == 0) |
| 1270 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1271 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1272 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1273 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1274 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1275 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1276 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1277 | } |
| 1278 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1279 | /* handle "not in" and "is not" */ |
| 1280 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1281 | case NAME: |
| 1282 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1283 | return NotIn; |
| 1284 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1285 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1286 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1287 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1288 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1289 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1290 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1291 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1292 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1293 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1294 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1295 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static asdl_seq * |
| 1299 | seq_for_testlist(struct compiling *c, const node *n) |
| 1300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1302 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1303 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1304 | asdl_seq *seq; |
| 1305 | expr_ty expression; |
| 1306 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1307 | 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] | 1308 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1309 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1310 | if (!seq) |
| 1311 | return NULL; |
| 1312 | |
| 1313 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1315 | 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] | 1316 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1317 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1318 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1319 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1320 | |
| 1321 | assert(i / 2 < seq->size); |
| 1322 | asdl_seq_SET(seq, i / 2, expression); |
| 1323 | } |
| 1324 | return seq; |
| 1325 | } |
| 1326 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1327 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1328 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1329 | { |
| 1330 | identifier name; |
| 1331 | expr_ty annotation = NULL; |
| 1332 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1333 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1334 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1335 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1336 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1337 | name = NEW_IDENTIFIER(ch); |
| 1338 | if (!name) |
| 1339 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1340 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1341 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1342 | |
| 1343 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1344 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1345 | if (!annotation) |
| 1346 | return NULL; |
| 1347 | } |
| 1348 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1349 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1350 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1351 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1352 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1353 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1356 | /* returns -1 if failed to handle keyword only arguments |
| 1357 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1358 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1359 | ^^^ |
| 1360 | start pointing here |
| 1361 | */ |
| 1362 | static int |
| 1363 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1364 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1365 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1366 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1367 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1368 | expr_ty expression, annotation; |
| 1369 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1370 | int i = start; |
| 1371 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1372 | |
| 1373 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1374 | 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] | 1375 | return -1; |
| 1376 | } |
| 1377 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1378 | while (i < NCH(n)) { |
| 1379 | ch = CHILD(n, i); |
| 1380 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1381 | case vfpdef: |
| 1382 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1383 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1384 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1385 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1386 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1387 | asdl_seq_SET(kwdefaults, j, expression); |
| 1388 | i += 2; /* '=' and test */ |
| 1389 | } |
| 1390 | else { /* setting NULL if no default value exists */ |
| 1391 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1392 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1393 | if (NCH(ch) == 3) { |
| 1394 | /* ch is NAME ':' test */ |
| 1395 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1396 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1397 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1398 | } |
| 1399 | else { |
| 1400 | annotation = NULL; |
| 1401 | } |
| 1402 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1403 | argname = NEW_IDENTIFIER(ch); |
| 1404 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1405 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1406 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1407 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1408 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1409 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1410 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1411 | if (!arg) |
| 1412 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1413 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1414 | i += 1; /* the name */ |
| 1415 | if (TYPE(CHILD(n, i)) == COMMA) |
| 1416 | i += 1; /* the comma, if present */ |
| 1417 | break; |
| 1418 | case TYPE_COMMENT: |
| 1419 | /* arg will be equal to the last argument processed */ |
| 1420 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1421 | if (!arg->type_comment) |
| 1422 | goto error; |
| 1423 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | case DOUBLESTAR: |
| 1426 | return i; |
| 1427 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1428 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1429 | goto error; |
| 1430 | } |
| 1431 | } |
| 1432 | return i; |
| 1433 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1435 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1436 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1437 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1438 | |
| 1439 | static arguments_ty |
| 1440 | ast_for_arguments(struct compiling *c, const node *n) |
| 1441 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1442 | /* This function handles both typedargslist (function definition) |
| 1443 | and varargslist (lambda definition). |
| 1444 | |
| 1445 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1446 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1447 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1448 | | '**' tfpdef [',']]] |
| 1449 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1450 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1451 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1452 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1453 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1454 | | '**' vfpdef [',']]] |
| 1455 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1456 | | '**' vfpdef [','] |
| 1457 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1458 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1459 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1460 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1461 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1462 | int nposdefaults = 0, found_default = 0; |
| 1463 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1464 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1465 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 | node *ch; |
| 1467 | |
| 1468 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1469 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1470 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1471 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1472 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1473 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1475 | /* First count the number of positional args & defaults. The |
| 1476 | variable i is the loop index for this for loop and the next. |
| 1477 | The next loop picks up where the first leaves off. |
| 1478 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1479 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1480 | ch = CHILD(n, i); |
| 1481 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1482 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1483 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1484 | if (i < NCH(n) && /* skip argument following star */ |
| 1485 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1486 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1487 | i++; |
| 1488 | } |
| 1489 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1490 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1491 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1492 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1493 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1494 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1496 | defaults for keyword only args */ |
| 1497 | for ( ; i < NCH(n); ++i) { |
| 1498 | ch = CHILD(n, i); |
| 1499 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1500 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1501 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1502 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1503 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1504 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1505 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1506 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1507 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1508 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1510 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1511 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1512 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1514 | since we set NULL as default for keyword only argument w/o default |
| 1515 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1516 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1517 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1518 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1519 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1520 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1521 | /* tfpdef: NAME [':' test] |
| 1522 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1523 | */ |
| 1524 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1525 | j = 0; /* index for defaults */ |
| 1526 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1527 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1528 | ch = CHILD(n, i); |
| 1529 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1530 | case tfpdef: |
| 1531 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1533 | anything other than EQUAL or a comma? */ |
| 1534 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1535 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1536 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1537 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1538 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1539 | assert(posdefaults != NULL); |
| 1540 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1541 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1542 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1543 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1544 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1545 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1546 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1547 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1548 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1549 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1550 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1551 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1552 | asdl_seq_SET(posargs, k++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1553 | i += 1; /* the name */ |
| 1554 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1555 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1556 | break; |
| 1557 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1558 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1559 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1560 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1561 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1562 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1563 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1564 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1565 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1566 | if (TYPE(ch) == COMMA) { |
| 1567 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1568 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1569 | |
| 1570 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1571 | ast_error(c, CHILD(n, i), |
| 1572 | "bare * has associated type comment"); |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1576 | res = handle_keywordonly_args(c, n, i, |
| 1577 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1578 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1579 | i = res; /* res has new position to process */ |
| 1580 | } |
| 1581 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1582 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1583 | if (!vararg) |
| 1584 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1585 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1586 | i += 2; /* the star and the name */ |
| 1587 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1588 | i += 1; /* the comma, if present */ |
| 1589 | |
| 1590 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1591 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1592 | if (!vararg->type_comment) |
| 1593 | return NULL; |
| 1594 | i += 1; |
| 1595 | } |
| 1596 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1597 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1598 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1599 | int res = 0; |
| 1600 | res = handle_keywordonly_args(c, n, i, |
| 1601 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1602 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1603 | i = res; /* res has new position to process */ |
| 1604 | } |
| 1605 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1606 | break; |
| 1607 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1608 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1609 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1610 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1611 | if (!kwarg) |
| 1612 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1613 | i += 2; /* the double star and the name */ |
| 1614 | if (TYPE(CHILD(n, i)) == COMMA) |
| 1615 | i += 1; /* the comma, if present */ |
| 1616 | break; |
| 1617 | case TYPE_COMMENT: |
| 1618 | assert(i); |
| 1619 | |
| 1620 | if (kwarg) |
| 1621 | arg = kwarg; |
| 1622 | |
| 1623 | /* arg will be equal to the last argument processed */ |
| 1624 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1625 | if (!arg->type_comment) |
| 1626 | return NULL; |
| 1627 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1630 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1631 | "unexpected node in varargslist: %d @ %d", |
| 1632 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1633 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1634 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1635 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1636 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | static expr_ty |
| 1640 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1641 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1642 | expr_ty e; |
| 1643 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1644 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1645 | int i; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1646 | node *ch; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1647 | |
| 1648 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1649 | |
| 1650 | lineno = LINENO(n); |
| 1651 | col_offset = n->n_col_offset; |
| 1652 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1653 | ch = CHILD(n, 0); |
| 1654 | id = NEW_IDENTIFIER(ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1655 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1656 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1657 | e = Name(id, Load, lineno, col_offset, |
| 1658 | ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1659 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1660 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1661 | |
| 1662 | for (i = 2; i < NCH(n); i+=2) { |
| 1663 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1664 | if (!id) |
| 1665 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1666 | e = Attribute(e, id, Load, lineno, col_offset, |
| 1667 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1668 | if (!e) |
| 1669 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | static expr_ty |
| 1676 | ast_for_decorator(struct compiling *c, const node *n) |
| 1677 | { |
| 1678 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1679 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1680 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1682 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1683 | REQ(CHILD(n, 0), AT); |
| 1684 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1686 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1687 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1688 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1690 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1691 | d = name_expr; |
| 1692 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1693 | } |
| 1694 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1695 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1696 | 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] | 1697 | if (!d) |
| 1698 | return NULL; |
| 1699 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1700 | } |
| 1701 | else { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1702 | 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] | 1703 | if (!d) |
| 1704 | return NULL; |
| 1705 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static asdl_seq* |
| 1712 | ast_for_decorators(struct compiling *c, const node *n) |
| 1713 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1714 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1715 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1718 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1719 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1720 | if (!decorator_seq) |
| 1721 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1723 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1724 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1725 | if (!d) |
| 1726 | return NULL; |
| 1727 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1728 | } |
| 1729 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1733 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1734 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1735 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1736 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1737 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1738 | identifier name; |
| 1739 | arguments_ty args; |
| 1740 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1741 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1742 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1743 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1744 | node *tc; |
| 1745 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1746 | |
| 1747 | REQ(n, funcdef); |
| 1748 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1749 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1750 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1751 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1752 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1753 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1754 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1755 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1756 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1757 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1758 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1759 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1760 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1761 | name_i += 2; |
| 1762 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1763 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1764 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1765 | if (!type_comment) |
| 1766 | return NULL; |
| 1767 | name_i += 1; |
| 1768 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1769 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1770 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1771 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1772 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1773 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1774 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1775 | /* Check if the suite has a type comment in it. */ |
| 1776 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1777 | |
| 1778 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1779 | if (type_comment != NULL) { |
| 1780 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1784 | if (!type_comment) |
| 1785 | return NULL; |
| 1786 | } |
| 1787 | } |
| 1788 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1789 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1790 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1791 | 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] | 1792 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1793 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1794 | 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] | 1795 | } |
| 1796 | |
| 1797 | static stmt_ty |
| 1798 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1799 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1800 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1801 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1802 | REQ(CHILD(n, 0), NAME); |
| 1803 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1804 | REQ(CHILD(n, 1), funcdef); |
| 1805 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1806 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1807 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | static stmt_ty |
| 1811 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1812 | { |
| 1813 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1814 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1815 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | |
| 1819 | static stmt_ty |
| 1820 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1821 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1822 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1823 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1824 | REQ(CHILD(n, 0), NAME); |
| 1825 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1826 | |
| 1827 | switch (TYPE(CHILD(n, 1))) { |
| 1828 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1829 | return ast_for_funcdef_impl(c, n, NULL, |
| 1830 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1831 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1832 | return ast_for_with_stmt(c, n, |
| 1833 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1834 | |
| 1835 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1836 | return ast_for_for_stmt(c, n, |
| 1837 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1838 | |
| 1839 | default: |
| 1840 | PyErr_Format(PyExc_SystemError, |
| 1841 | "invalid async stament: %s", |
| 1842 | STR(CHILD(n, 1))); |
| 1843 | return NULL; |
| 1844 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1847 | static stmt_ty |
| 1848 | ast_for_decorated(struct compiling *c, const node *n) |
| 1849 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1850 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1851 | stmt_ty thing = NULL; |
| 1852 | asdl_seq *decorator_seq = NULL; |
| 1853 | |
| 1854 | REQ(n, decorated); |
| 1855 | |
| 1856 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1857 | if (!decorator_seq) |
| 1858 | return NULL; |
| 1859 | |
| 1860 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1861 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1863 | |
| 1864 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1865 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1866 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1867 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1868 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1869 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1870 | } |
| 1871 | return thing; |
| 1872 | } |
| 1873 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1874 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1875 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1876 | { |
| 1877 | /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* |
| 1878 | ['else' ':' suite] |
| 1879 | namedexpr_test: test [':=' test] |
| 1880 | argument: ( test [comp_for] | |
| 1881 | test ':=' test | |
| 1882 | test '=' test | |
| 1883 | '**' test | |
| 1884 | '*' test ) |
| 1885 | */ |
| 1886 | expr_ty target, value; |
| 1887 | |
| 1888 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1889 | if (!target) |
| 1890 | return NULL; |
| 1891 | |
| 1892 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1893 | if (!value) |
| 1894 | return NULL; |
| 1895 | |
| 1896 | if (!set_context(c, target, NamedStore, n)) |
| 1897 | return NULL; |
| 1898 | |
| 1899 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1900 | n->n_end_col_offset, c->c_arena); |
| 1901 | } |
| 1902 | |
| 1903 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1904 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1905 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1906 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1907 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1908 | arguments_ty args; |
| 1909 | expr_ty expression; |
| 1910 | |
| 1911 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1912 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1913 | if (!args) |
| 1914 | return NULL; |
| 1915 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1916 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1917 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1918 | } |
| 1919 | else { |
| 1920 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1921 | if (!args) |
| 1922 | return NULL; |
| 1923 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1924 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1928 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1929 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1932 | static expr_ty |
| 1933 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1936 | expr_ty expression, body, orelse; |
| 1937 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1938 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1939 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1940 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1941 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1942 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1943 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1944 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1945 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1946 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1947 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1948 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1949 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1950 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1953 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1954 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1955 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1956 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1957 | */ |
| 1958 | |
| 1959 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1960 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1961 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1962 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1963 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1964 | count_comp_for: |
| 1965 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1966 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1967 | if (NCH(n) == 2) { |
| 1968 | REQ(CHILD(n, 0), NAME); |
| 1969 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1970 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1971 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1972 | else if (NCH(n) == 1) { |
| 1973 | n = CHILD(n, 0); |
| 1974 | } |
| 1975 | else { |
| 1976 | goto error; |
| 1977 | } |
| 1978 | if (NCH(n) == (5)) { |
| 1979 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1980 | } |
| 1981 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1982 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1983 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1984 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1985 | REQ(n, comp_iter); |
| 1986 | n = CHILD(n, 0); |
| 1987 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1988 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1989 | else if (TYPE(n) == comp_if) { |
| 1990 | if (NCH(n) == 3) { |
| 1991 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1992 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1993 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1994 | else |
| 1995 | return n_fors; |
| 1996 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1997 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1998 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1999 | /* Should never be reached */ |
| 2000 | PyErr_SetString(PyExc_SystemError, |
| 2001 | "logic error in count_comp_fors"); |
| 2002 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2005 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2006 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2007 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2008 | */ |
| 2009 | |
| 2010 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2011 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2012 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2013 | int n_ifs = 0; |
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 | while (1) { |
| 2016 | REQ(n, comp_iter); |
| 2017 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2018 | return n_ifs; |
| 2019 | n = CHILD(n, 0); |
| 2020 | REQ(n, comp_if); |
| 2021 | n_ifs++; |
| 2022 | if (NCH(n) == 2) |
| 2023 | return n_ifs; |
| 2024 | n = CHILD(n, 2); |
| 2025 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2028 | static asdl_seq * |
| 2029 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2030 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2031 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2032 | asdl_seq *comps; |
| 2033 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2034 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2035 | if (n_fors == -1) |
| 2036 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2037 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2038 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2039 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2040 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2041 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2042 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2043 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2044 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2045 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2046 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2047 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2048 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2050 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2052 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2053 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2054 | REQ(CHILD(n, 0), NAME); |
| 2055 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 2056 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2057 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2058 | else { |
| 2059 | sync_n = CHILD(n, 0); |
| 2060 | } |
| 2061 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2062 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2063 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2064 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2065 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2066 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2067 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2068 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2069 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2070 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2071 | /* Check the # of children rather than the length of t, since |
| 2072 | (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] | 2073 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2074 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2075 | comp = comprehension(first, expression, NULL, |
| 2076 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2077 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2078 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2079 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2080 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2081 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2082 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2083 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2084 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2085 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2086 | int j, n_ifs; |
| 2087 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2089 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2090 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2091 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2092 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2093 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2094 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2095 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2096 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2097 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2098 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2099 | REQ(n, comp_iter); |
| 2100 | n = CHILD(n, 0); |
| 2101 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2103 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2104 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2105 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2106 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2107 | if (NCH(n) == 3) |
| 2108 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2109 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2110 | /* on exit, must guarantee that n is a comp_for */ |
| 2111 | if (TYPE(n) == comp_iter) |
| 2112 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2113 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2114 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2115 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2116 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2117 | return comps; |
| 2118 | } |
| 2119 | |
| 2120 | static expr_ty |
| 2121 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2122 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2123 | /* testlist_comp: (test|star_expr) |
| 2124 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2125 | expr_ty elt; |
| 2126 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2127 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2129 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2131 | ch = CHILD(n, 0); |
| 2132 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2133 | if (!elt) |
| 2134 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2135 | if (elt->kind == Starred_kind) { |
| 2136 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2137 | return NULL; |
| 2138 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2140 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2141 | if (!comps) |
| 2142 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2143 | |
| 2144 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2145 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2146 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2147 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2148 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2149 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2150 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2151 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2152 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2153 | else |
| 2154 | /* Should never happen */ |
| 2155 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2158 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2159 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2160 | * elements. Iff successful, nonzero is returned. |
| 2161 | */ |
| 2162 | static int |
| 2163 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2164 | expr_ty *key, expr_ty *value) |
| 2165 | { |
| 2166 | expr_ty expression; |
| 2167 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2168 | assert(NCH(n) - *i >= 2); |
| 2169 | |
| 2170 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2171 | if (!expression) |
| 2172 | return 0; |
| 2173 | *key = NULL; |
| 2174 | *value = expression; |
| 2175 | |
| 2176 | *i += 2; |
| 2177 | } |
| 2178 | else { |
| 2179 | assert(NCH(n) - *i >= 3); |
| 2180 | |
| 2181 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2182 | if (!expression) |
| 2183 | return 0; |
| 2184 | *key = expression; |
| 2185 | |
| 2186 | REQ(CHILD(n, *i + 1), COLON); |
| 2187 | |
| 2188 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2189 | if (!expression) |
| 2190 | return 0; |
| 2191 | *value = expression; |
| 2192 | |
| 2193 | *i += 3; |
| 2194 | } |
| 2195 | return 1; |
| 2196 | } |
| 2197 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2198 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2199 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2200 | { |
| 2201 | expr_ty key, value; |
| 2202 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2203 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2205 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2206 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2207 | assert(key); |
| 2208 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2210 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2211 | if (!comps) |
| 2212 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2214 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2215 | 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] | 2216 | } |
| 2217 | |
| 2218 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2219 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2220 | { |
| 2221 | int i; |
| 2222 | int j; |
| 2223 | int size; |
| 2224 | asdl_seq *keys, *values; |
| 2225 | |
| 2226 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2227 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2228 | if (!keys) |
| 2229 | return NULL; |
| 2230 | |
| 2231 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2232 | if (!values) |
| 2233 | return NULL; |
| 2234 | |
| 2235 | j = 0; |
| 2236 | for (i = 0; i < NCH(n); i++) { |
| 2237 | expr_ty key, value; |
| 2238 | |
| 2239 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2240 | return NULL; |
| 2241 | asdl_seq_SET(keys, j, key); |
| 2242 | asdl_seq_SET(values, j, value); |
| 2243 | |
| 2244 | j++; |
| 2245 | } |
| 2246 | keys->size = j; |
| 2247 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2248 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2249 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2253 | ast_for_genexp(struct compiling *c, const node *n) |
| 2254 | { |
| 2255 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2256 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | static expr_ty |
| 2260 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2261 | { |
| 2262 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2263 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static expr_ty |
| 2267 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2268 | { |
| 2269 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2270 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2273 | static expr_ty |
| 2274 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2275 | { |
| 2276 | int i; |
| 2277 | int size; |
| 2278 | asdl_seq *elts; |
| 2279 | |
| 2280 | assert(TYPE(n) == (dictorsetmaker)); |
| 2281 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2282 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2283 | if (!elts) |
| 2284 | return NULL; |
| 2285 | for (i = 0; i < NCH(n); i += 2) { |
| 2286 | expr_ty expression; |
| 2287 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2288 | if (!expression) |
| 2289 | return NULL; |
| 2290 | asdl_seq_SET(elts, i / 2, expression); |
| 2291 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2292 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2293 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2294 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2295 | |
| 2296 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2297 | ast_for_atom(struct compiling *c, const node *n) |
| 2298 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2299 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2300 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2301 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2302 | */ |
| 2303 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2305 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2306 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2307 | PyObject *name; |
| 2308 | const char *s = STR(ch); |
| 2309 | size_t len = strlen(s); |
| 2310 | if (len >= 4 && len <= 5) { |
| 2311 | if (!strcmp(s, "None")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2312 | return Constant(Py_None, LINENO(n), n->n_col_offset, |
| 2313 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2314 | if (!strcmp(s, "True")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2315 | return Constant(Py_True, LINENO(n), n->n_col_offset, |
| 2316 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2317 | if (!strcmp(s, "False")) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2318 | return Constant(Py_False, LINENO(n), n->n_col_offset, |
| 2319 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2320 | } |
| 2321 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2322 | if (!name) |
| 2323 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2324 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2325 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2326 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2327 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2328 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2329 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2330 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2331 | const char *errtype = NULL; |
| 2332 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2333 | errtype = "unicode error"; |
| 2334 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2335 | errtype = "value error"; |
| 2336 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2337 | PyObject *type, *value, *tback, *errstr; |
| 2338 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2339 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2340 | if (errstr) { |
| 2341 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2342 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2343 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2344 | else { |
| 2345 | PyErr_Clear(); |
| 2346 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2347 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2348 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2349 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2350 | Py_XDECREF(tback); |
| 2351 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2352 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2353 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2354 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2355 | } |
| 2356 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2357 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2358 | if (!pynum) |
| 2359 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2360 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2361 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2362 | Py_DECREF(pynum); |
| 2363 | return NULL; |
| 2364 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2365 | return Constant(pynum, LINENO(n), n->n_col_offset, |
| 2366 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2367 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2368 | case ELLIPSIS: /* Ellipsis */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2369 | return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, |
| 2370 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2371 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2372 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2374 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2375 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2376 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2378 | if (TYPE(ch) == yield_expr) |
| 2379 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2382 | if (NCH(ch) == 1) { |
| 2383 | return ast_for_testlist(c, ch); |
| 2384 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2385 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2386 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2387 | return copy_location(ast_for_genexp(c, ch), n); |
| 2388 | } |
| 2389 | else { |
| 2390 | return copy_location(ast_for_testlist(c, ch), n); |
| 2391 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2392 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2393 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2395 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2396 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2397 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2399 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2400 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2401 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2402 | if (!elts) |
| 2403 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2404 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2405 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2406 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2407 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2408 | else { |
| 2409 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2410 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2411 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2412 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2413 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2414 | * ((test | '*' test) |
| 2415 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2416 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2417 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2418 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2419 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2420 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2421 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2422 | } |
| 2423 | else { |
| 2424 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2425 | if (NCH(ch) == 1 || |
| 2426 | (NCH(ch) > 1 && |
| 2427 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2428 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2429 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2430 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2431 | else if (NCH(ch) > 1 && |
| 2432 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2433 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2434 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2435 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2436 | else if (NCH(ch) > 3 - is_dict && |
| 2437 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2438 | /* It's a dictionary comprehension. */ |
| 2439 | if (is_dict) { |
| 2440 | ast_error(c, n, "dict unpacking cannot be used in " |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 2441 | "dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2442 | return NULL; |
| 2443 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2444 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2445 | } |
| 2446 | else { |
| 2447 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2448 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2449 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2450 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2451 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2452 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2453 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2454 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2455 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | static slice_ty |
| 2460 | ast_for_slice(struct compiling *c, const node *n) |
| 2461 | { |
| 2462 | node *ch; |
| 2463 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2464 | |
| 2465 | REQ(n, subscript); |
| 2466 | |
| 2467 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2468 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2469 | sliceop: ':' [test] |
| 2470 | */ |
| 2471 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2472 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2473 | /* 'step' variable hold no significance in terms of being used over |
| 2474 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2476 | if (!step) |
| 2477 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2479 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2483 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2484 | if (!lower) |
| 2485 | return NULL; |
| 2486 | } |
| 2487 | |
| 2488 | /* If there's an upper bound it's in the second or third position. */ |
| 2489 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2490 | if (NCH(n) > 1) { |
| 2491 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2492 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2493 | if (TYPE(n2) == test) { |
| 2494 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2495 | if (!upper) |
| 2496 | return NULL; |
| 2497 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2498 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2499 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2500 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2501 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2502 | if (TYPE(n2) == test) { |
| 2503 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2504 | if (!upper) |
| 2505 | return NULL; |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | ch = CHILD(n, NCH(n) - 1); |
| 2510 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2511 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2512 | ch = CHILD(ch, 1); |
| 2513 | if (TYPE(ch) == test) { |
| 2514 | step = ast_for_expr(c, ch); |
| 2515 | if (!step) |
| 2516 | return NULL; |
| 2517 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2521 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static expr_ty |
| 2525 | ast_for_binop(struct compiling *c, const node *n) |
| 2526 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2527 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2529 | BinOp(BinOp(A, op, B), op, C). |
| 2530 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2531 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2532 | int i, nops; |
| 2533 | expr_ty expr1, expr2, result; |
| 2534 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2535 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2536 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2537 | if (!expr1) |
| 2538 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2539 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2540 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2541 | if (!expr2) |
| 2542 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2543 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2544 | newoperator = get_operator(CHILD(n, 1)); |
| 2545 | if (!newoperator) |
| 2546 | return NULL; |
| 2547 | |
| 2548 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2549 | 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] | 2550 | c->c_arena); |
| 2551 | if (!result) |
| 2552 | return NULL; |
| 2553 | |
| 2554 | nops = (NCH(n) - 1) / 2; |
| 2555 | for (i = 1; i < nops; i++) { |
| 2556 | expr_ty tmp_result, tmp; |
| 2557 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2558 | |
| 2559 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2560 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2561 | return NULL; |
| 2562 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2563 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2564 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2565 | return NULL; |
| 2566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2568 | LINENO(next_oper), next_oper->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2569 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2570 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2571 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2572 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2573 | return NULL; |
| 2574 | result = tmp_result; |
| 2575 | } |
| 2576 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2579 | static expr_ty |
| 2580 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2583 | subscriptlist: subscript (',' subscript)* [','] |
| 2584 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2585 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2586 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2587 | REQ(n, trailer); |
| 2588 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2589 | if (NCH(n) == 2) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2590 | return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset, |
| 2591 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2592 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2593 | 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] | 2594 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2595 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2596 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2597 | if (!attr_id) |
| 2598 | return NULL; |
| 2599 | return Attribute(left_expr, attr_id, Load, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2600 | LINENO(n), n->n_col_offset, |
| 2601 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2602 | } |
| 2603 | else { |
| 2604 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2605 | REQ(CHILD(n, 2), RSQB); |
| 2606 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2607 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2608 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2609 | if (!slc) |
| 2610 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2611 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2612 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2613 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2614 | } |
| 2615 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2617 | by treating the sequence as a tuple literal if there are |
| 2618 | no slice features. |
| 2619 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2620 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2621 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2622 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2623 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2624 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2625 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2626 | if (!slices) |
| 2627 | return NULL; |
| 2628 | for (j = 0; j < NCH(n); j += 2) { |
| 2629 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2630 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2631 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2632 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2633 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2634 | asdl_seq_SET(slices, j / 2, slc); |
| 2635 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2636 | if (!simple) { |
| 2637 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2638 | Load, LINENO(n), n->n_col_offset, |
| 2639 | 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] | 2640 | } |
| 2641 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2642 | 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] | 2643 | if (!elts) |
| 2644 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2645 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2646 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2647 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2648 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2649 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2650 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
| 2651 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2652 | if (!e) |
| 2653 | return NULL; |
| 2654 | return Subscript(left_expr, Index(e, c->c_arena), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2655 | Load, LINENO(n), n->n_col_offset, |
| 2656 | 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] | 2657 | } |
| 2658 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2662 | ast_for_factor(struct compiling *c, const node *n) |
| 2663 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2664 | expr_ty expression; |
| 2665 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2666 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2667 | if (!expression) |
| 2668 | return NULL; |
| 2669 | |
| 2670 | switch (TYPE(CHILD(n, 0))) { |
| 2671 | case PLUS: |
| 2672 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2673 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2674 | c->c_arena); |
| 2675 | case MINUS: |
| 2676 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2677 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2678 | c->c_arena); |
| 2679 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2680 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2681 | n->n_end_lineno, n->n_end_col_offset, |
| 2682 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2683 | } |
| 2684 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2685 | TYPE(CHILD(n, 0))); |
| 2686 | return NULL; |
| 2687 | } |
| 2688 | |
| 2689 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2690 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2691 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2692 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2693 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2694 | |
| 2695 | REQ(n, atom_expr); |
| 2696 | nch = NCH(n); |
| 2697 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2698 | 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] | 2699 | start = 1; |
| 2700 | assert(nch > 1); |
| 2701 | } |
| 2702 | |
| 2703 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2704 | if (!e) |
| 2705 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2706 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2707 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2708 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2709 | return Await(e, LINENO(n), n->n_col_offset, |
| 2710 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2714 | node *ch = CHILD(n, i); |
| 2715 | if (TYPE(ch) != trailer) |
| 2716 | break; |
| 2717 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2718 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2719 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2720 | tmp->lineno = e->lineno; |
| 2721 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2722 | e = tmp; |
| 2723 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2724 | |
| 2725 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2726 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2727 | return Await(e, LINENO(n), n->n_col_offset, |
| 2728 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2729 | } |
| 2730 | else { |
| 2731 | return e; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | static expr_ty |
| 2736 | ast_for_power(struct compiling *c, const node *n) |
| 2737 | { |
| 2738 | /* power: atom trailer* ('**' factor)* |
| 2739 | */ |
| 2740 | expr_ty e; |
| 2741 | REQ(n, power); |
| 2742 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2743 | if (!e) |
| 2744 | return NULL; |
| 2745 | if (NCH(n) == 1) |
| 2746 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2747 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2748 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2749 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2750 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2751 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2752 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2753 | } |
| 2754 | return e; |
| 2755 | } |
| 2756 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2757 | static expr_ty |
| 2758 | ast_for_starred(struct compiling *c, const node *n) |
| 2759 | { |
| 2760 | expr_ty tmp; |
| 2761 | REQ(n, star_expr); |
| 2762 | |
| 2763 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2764 | if (!tmp) |
| 2765 | return NULL; |
| 2766 | |
| 2767 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2768 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2769 | 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] | 2770 | } |
| 2771 | |
| 2772 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2773 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2774 | */ |
| 2775 | |
| 2776 | static expr_ty |
| 2777 | ast_for_expr(struct compiling *c, const node *n) |
| 2778 | { |
| 2779 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2780 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2781 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2782 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2784 | and_test: not_test ('and' not_test)* |
| 2785 | not_test: 'not' not_test | comparison |
| 2786 | comparison: expr (comp_op expr)* |
| 2787 | expr: xor_expr ('|' xor_expr)* |
| 2788 | xor_expr: and_expr ('^' and_expr)* |
| 2789 | and_expr: shift_expr ('&' shift_expr)* |
| 2790 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2791 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2792 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2793 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2794 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2795 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2796 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2797 | */ |
| 2798 | |
| 2799 | asdl_seq *seq; |
| 2800 | int i; |
| 2801 | |
| 2802 | loop: |
| 2803 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2804 | case namedexpr_test: |
| 2805 | if (NCH(n) == 3) |
| 2806 | return ast_for_namedexpr(c, n); |
| 2807 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2808 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2809 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2810 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2811 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2812 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2813 | else if (NCH(n) > 1) |
| 2814 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2815 | /* Fallthrough */ |
| 2816 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | case and_test: |
| 2818 | if (NCH(n) == 1) { |
| 2819 | n = CHILD(n, 0); |
| 2820 | goto loop; |
| 2821 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2822 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2823 | if (!seq) |
| 2824 | return NULL; |
| 2825 | for (i = 0; i < NCH(n); i += 2) { |
| 2826 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2827 | if (!e) |
| 2828 | return NULL; |
| 2829 | asdl_seq_SET(seq, i / 2, e); |
| 2830 | } |
| 2831 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2832 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2833 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2834 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2835 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2836 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2837 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2838 | case not_test: |
| 2839 | if (NCH(n) == 1) { |
| 2840 | n = CHILD(n, 0); |
| 2841 | goto loop; |
| 2842 | } |
| 2843 | else { |
| 2844 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2845 | if (!expression) |
| 2846 | return NULL; |
| 2847 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2848 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2849 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2850 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2851 | } |
| 2852 | case comparison: |
| 2853 | if (NCH(n) == 1) { |
| 2854 | n = CHILD(n, 0); |
| 2855 | goto loop; |
| 2856 | } |
| 2857 | else { |
| 2858 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2859 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2860 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2861 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 | if (!ops) |
| 2863 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2864 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2865 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2866 | return NULL; |
| 2867 | } |
| 2868 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2869 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2870 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2871 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2872 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2873 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2874 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | |
| 2876 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2877 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2878 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2879 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2881 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2882 | asdl_seq_SET(cmps, i / 2, expression); |
| 2883 | } |
| 2884 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2885 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2886 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2887 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2889 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2890 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 | } |
| 2892 | break; |
| 2893 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2894 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2896 | /* The next five cases all handle BinOps. The main body of code |
| 2897 | is the same in each case, but the switch turned inside out to |
| 2898 | reuse the code for each type of operator. |
| 2899 | */ |
| 2900 | case expr: |
| 2901 | case xor_expr: |
| 2902 | case and_expr: |
| 2903 | case shift_expr: |
| 2904 | case arith_expr: |
| 2905 | case term: |
| 2906 | if (NCH(n) == 1) { |
| 2907 | n = CHILD(n, 0); |
| 2908 | goto loop; |
| 2909 | } |
| 2910 | return ast_for_binop(c, n); |
| 2911 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2912 | node *an = NULL; |
| 2913 | node *en = NULL; |
| 2914 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2915 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2916 | if (NCH(n) > 1) |
| 2917 | an = CHILD(n, 1); /* yield_arg */ |
| 2918 | if (an) { |
| 2919 | en = CHILD(an, NCH(an) - 1); |
| 2920 | if (NCH(an) == 2) { |
| 2921 | is_from = 1; |
| 2922 | exp = ast_for_expr(c, en); |
| 2923 | } |
| 2924 | else |
| 2925 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2926 | if (!exp) |
| 2927 | return NULL; |
| 2928 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2929 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2930 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2931 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2932 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2933 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2934 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2935 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2936 | if (NCH(n) == 1) { |
| 2937 | n = CHILD(n, 0); |
| 2938 | goto loop; |
| 2939 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2940 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2941 | case power: |
| 2942 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2944 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2945 | return NULL; |
| 2946 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2947 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2948 | return NULL; |
| 2949 | } |
| 2950 | |
| 2951 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2952 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2953 | const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2954 | { |
| 2955 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2956 | arglist: argument (',' argument)* [','] |
| 2957 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2958 | */ |
| 2959 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2960 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2961 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2962 | asdl_seq *args; |
| 2963 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2964 | |
| 2965 | REQ(n, arglist); |
| 2966 | |
| 2967 | nargs = 0; |
| 2968 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2969 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2970 | node *ch = CHILD(n, i); |
| 2971 | if (TYPE(ch) == argument) { |
| 2972 | if (NCH(ch) == 1) |
| 2973 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2974 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2975 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2976 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2977 | ast_error(c, ch, "invalid syntax"); |
| 2978 | return NULL; |
| 2979 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2980 | if (NCH(n) > 1) { |
| 2981 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2982 | return NULL; |
| 2983 | } |
| 2984 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2985 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2986 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2987 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 2988 | nargs++; |
| 2989 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2990 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2991 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2992 | nkeywords++; |
| 2993 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2994 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2995 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2996 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2997 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2998 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2999 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3001 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3002 | |
| 3003 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 3004 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 3005 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3006 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3007 | node *ch = CHILD(n, i); |
| 3008 | if (TYPE(ch) == argument) { |
| 3009 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3010 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3011 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3012 | /* a positional argument */ |
| 3013 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3014 | if (ndoublestars) { |
| 3015 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3016 | "positional argument follows " |
| 3017 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3018 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3019 | else { |
| 3020 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3021 | "positional argument follows " |
| 3022 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3023 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3024 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3025 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3026 | e = ast_for_expr(c, chch); |
| 3027 | if (!e) |
| 3028 | return NULL; |
| 3029 | asdl_seq_SET(args, nargs++, e); |
| 3030 | } |
| 3031 | else if (TYPE(chch) == STAR) { |
| 3032 | /* an iterable argument unpacking */ |
| 3033 | expr_ty starred; |
| 3034 | if (ndoublestars) { |
| 3035 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3036 | "iterable argument unpacking follows " |
| 3037 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3038 | return NULL; |
| 3039 | } |
| 3040 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3041 | if (!e) |
| 3042 | return NULL; |
| 3043 | starred = Starred(e, Load, LINENO(chch), |
| 3044 | chch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3045 | chch->n_end_lineno, chch->n_end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3046 | c->c_arena); |
| 3047 | if (!starred) |
| 3048 | return NULL; |
| 3049 | asdl_seq_SET(args, nargs++, starred); |
| 3050 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3051 | } |
| 3052 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3053 | /* a keyword argument unpacking */ |
| 3054 | keyword_ty kw; |
| 3055 | i++; |
| 3056 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3058 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3059 | kw = keyword(NULL, e, c->c_arena); |
| 3060 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3061 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3063 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3064 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3065 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3066 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3067 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3068 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3069 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3070 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3071 | /* treat colon equal as positional argument */ |
| 3072 | if (nkeywords) { |
| 3073 | if (ndoublestars) { |
| 3074 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3075 | "positional argument follows " |
| 3076 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3077 | } |
| 3078 | else { |
| 3079 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3080 | "positional argument follows " |
| 3081 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3082 | } |
| 3083 | return NULL; |
| 3084 | } |
| 3085 | e = ast_for_namedexpr(c, ch); |
| 3086 | if (!e) |
| 3087 | return NULL; |
| 3088 | asdl_seq_SET(args, nargs++, e); |
| 3089 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3090 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3091 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3092 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3093 | identifier key, tmp; |
| 3094 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3095 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3096 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3097 | // expression) in the keyword slot of a call site. So, we need |
| 3098 | // to manually enforce that the keyword is a NAME here. |
| 3099 | static const int name_tree[] = { |
| 3100 | test, |
| 3101 | or_test, |
| 3102 | and_test, |
| 3103 | not_test, |
| 3104 | comparison, |
| 3105 | expr, |
| 3106 | xor_expr, |
| 3107 | and_expr, |
| 3108 | shift_expr, |
| 3109 | arith_expr, |
| 3110 | term, |
| 3111 | factor, |
| 3112 | power, |
| 3113 | atom_expr, |
| 3114 | atom, |
| 3115 | 0, |
| 3116 | }; |
| 3117 | node *expr_node = chch; |
| 3118 | for (int i = 0; name_tree[i]; i++) { |
| 3119 | if (TYPE(expr_node) != name_tree[i]) |
| 3120 | break; |
| 3121 | if (NCH(expr_node) != 1) |
| 3122 | break; |
| 3123 | expr_node = CHILD(expr_node, 0); |
| 3124 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3125 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3126 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3127 | "expression cannot contain assignment, " |
| 3128 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3129 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3130 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3131 | key = new_identifier(STR(expr_node), c); |
| 3132 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3133 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3135 | if (forbidden_name(c, key, chch, 1)) { |
| 3136 | return NULL; |
| 3137 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3138 | for (k = 0; k < nkeywords; k++) { |
| 3139 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3140 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 3141 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3142 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 3143 | return NULL; |
| 3144 | } |
| 3145 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3146 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3147 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3148 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3149 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3150 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3151 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3152 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3153 | } |
| 3154 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3157 | return Call(func, args, keywords, func->lineno, func->col_offset, |
| 3158 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3161 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3162 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3163 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3164 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3165 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3166 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3167 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3168 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3169 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3170 | } |
| 3171 | else { |
| 3172 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3173 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3174 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3175 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3176 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3177 | else { |
| 3178 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3179 | if (!tmp) |
| 3180 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3181 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3182 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3183 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3186 | static stmt_ty |
| 3187 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3188 | { |
| 3189 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3190 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3191 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3192 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3193 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3194 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3195 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3196 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3197 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3198 | int num = NCH(n); |
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 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3201 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3202 | if (!e) |
| 3203 | return NULL; |
| 3204 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3205 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3206 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | } |
| 3208 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3209 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3210 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3211 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3212 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3213 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3214 | if (!expr1) |
| 3215 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3216 | if(!set_context(c, expr1, Store, ch)) |
| 3217 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3218 | /* set_context checks that most expressions are not the left side. |
| 3219 | Augmented assignments can only have a name, a subscript, or an |
| 3220 | attribute on the left, though, so we have to explicitly check for |
| 3221 | those. */ |
| 3222 | switch (expr1->kind) { |
| 3223 | case Name_kind: |
| 3224 | case Attribute_kind: |
| 3225 | case Subscript_kind: |
| 3226 | break; |
| 3227 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3228 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3229 | return NULL; |
| 3230 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3231 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3232 | ch = CHILD(n, 2); |
| 3233 | if (TYPE(ch) == testlist) |
| 3234 | expr2 = ast_for_testlist(c, ch); |
| 3235 | else |
| 3236 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3237 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3238 | return NULL; |
| 3239 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3240 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3241 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3242 | return NULL; |
| 3243 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3244 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3245 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3246 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3247 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3248 | expr_ty expr1, expr2, expr3; |
| 3249 | node *ch = CHILD(n, 0); |
| 3250 | node *deep, *ann = CHILD(n, 1); |
| 3251 | int simple = 1; |
| 3252 | |
| 3253 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3254 | deep = ch; |
| 3255 | while (NCH(deep) == 1) { |
| 3256 | deep = CHILD(deep, 0); |
| 3257 | } |
| 3258 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3259 | simple = 0; |
| 3260 | } |
| 3261 | expr1 = ast_for_testlist(c, ch); |
| 3262 | if (!expr1) { |
| 3263 | return NULL; |
| 3264 | } |
| 3265 | switch (expr1->kind) { |
| 3266 | case Name_kind: |
| 3267 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3268 | return NULL; |
| 3269 | } |
| 3270 | expr1->v.Name.ctx = Store; |
| 3271 | break; |
| 3272 | case Attribute_kind: |
| 3273 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3274 | return NULL; |
| 3275 | } |
| 3276 | expr1->v.Attribute.ctx = Store; |
| 3277 | break; |
| 3278 | case Subscript_kind: |
| 3279 | expr1->v.Subscript.ctx = Store; |
| 3280 | break; |
| 3281 | case List_kind: |
| 3282 | ast_error(c, ch, |
| 3283 | "only single target (not list) can be annotated"); |
| 3284 | return NULL; |
| 3285 | case Tuple_kind: |
| 3286 | ast_error(c, ch, |
| 3287 | "only single target (not tuple) can be annotated"); |
| 3288 | return NULL; |
| 3289 | default: |
| 3290 | ast_error(c, ch, |
| 3291 | "illegal target for annotation"); |
| 3292 | return NULL; |
| 3293 | } |
| 3294 | |
| 3295 | if (expr1->kind != Name_kind) { |
| 3296 | simple = 0; |
| 3297 | } |
| 3298 | ch = CHILD(ann, 1); |
| 3299 | expr2 = ast_for_expr(c, ch); |
| 3300 | if (!expr2) { |
| 3301 | return NULL; |
| 3302 | } |
| 3303 | if (NCH(ann) == 2) { |
| 3304 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3305 | LINENO(n), n->n_col_offset, |
| 3306 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3307 | } |
| 3308 | else { |
| 3309 | ch = CHILD(ann, 3); |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3310 | if (TYPE(ch) == testlist) { |
| 3311 | expr3 = ast_for_testlist(c, ch); |
| 3312 | } |
| 3313 | else { |
| 3314 | expr3 = ast_for_expr(c, ch); |
| 3315 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3316 | if (!expr3) { |
| 3317 | return NULL; |
| 3318 | } |
| 3319 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3320 | LINENO(n), n->n_col_offset, |
| 3321 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3322 | } |
| 3323 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3324 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3325 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3326 | asdl_seq *targets; |
| 3327 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3328 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3329 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3330 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3331 | /* a normal assignment */ |
| 3332 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3333 | |
| 3334 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3335 | nch_minus_type = num - has_type_comment; |
| 3336 | |
| 3337 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3338 | if (!targets) |
| 3339 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3340 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3341 | expr_ty e; |
| 3342 | node *ch = CHILD(n, i); |
| 3343 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3344 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3345 | return NULL; |
| 3346 | } |
| 3347 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3349 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3350 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3351 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3352 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3353 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3354 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3355 | asdl_seq_SET(targets, i / 2, e); |
| 3356 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3357 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3358 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3359 | expression = ast_for_testlist(c, value); |
| 3360 | else |
| 3361 | expression = ast_for_expr(c, value); |
| 3362 | if (!expression) |
| 3363 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3364 | if (has_type_comment) { |
| 3365 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3366 | if (!type_comment) |
| 3367 | return NULL; |
| 3368 | } |
| 3369 | else |
| 3370 | type_comment = NULL; |
| 3371 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3372 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3373 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3376 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3377 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3378 | 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] | 3379 | { |
| 3380 | asdl_seq *seq; |
| 3381 | int i; |
| 3382 | expr_ty e; |
| 3383 | |
| 3384 | REQ(n, exprlist); |
| 3385 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3386 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3387 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3388 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3389 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3390 | e = ast_for_expr(c, CHILD(n, i)); |
| 3391 | if (!e) |
| 3392 | return NULL; |
| 3393 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3394 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3395 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3396 | } |
| 3397 | return seq; |
| 3398 | } |
| 3399 | |
| 3400 | static stmt_ty |
| 3401 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3402 | { |
| 3403 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3405 | /* del_stmt: 'del' exprlist */ |
| 3406 | REQ(n, del_stmt); |
| 3407 | |
| 3408 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3409 | if (!expr_list) |
| 3410 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3411 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3412 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | static stmt_ty |
| 3416 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3417 | { |
| 3418 | /* |
| 3419 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3420 | | yield_stmt |
| 3421 | break_stmt: 'break' |
| 3422 | continue_stmt: 'continue' |
| 3423 | return_stmt: 'return' [testlist] |
| 3424 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3425 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3426 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3427 | */ |
| 3428 | node *ch; |
| 3429 | |
| 3430 | REQ(n, flow_stmt); |
| 3431 | ch = CHILD(n, 0); |
| 3432 | switch (TYPE(ch)) { |
| 3433 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3434 | return Break(LINENO(n), n->n_col_offset, |
| 3435 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3437 | return Continue(LINENO(n), n->n_col_offset, |
| 3438 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3439 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3440 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3441 | if (!exp) |
| 3442 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3443 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3444 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3445 | } |
| 3446 | case return_stmt: |
| 3447 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3448 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3449 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3450 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3451 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3452 | if (!expression) |
| 3453 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3454 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3455 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3456 | } |
| 3457 | case raise_stmt: |
| 3458 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3459 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3460 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3461 | else if (NCH(ch) >= 2) { |
| 3462 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3463 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3464 | if (!expression) |
| 3465 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3466 | if (NCH(ch) == 4) { |
| 3467 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3468 | if (!cause) |
| 3469 | return NULL; |
| 3470 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3471 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3472 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3473 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3474 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3476 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3478 | return NULL; |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3483 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3484 | { |
| 3485 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3486 | import_as_name: NAME ['as' NAME] |
| 3487 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3488 | dotted_name: NAME ('.' NAME)* |
| 3489 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3490 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3491 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3492 | loop: |
| 3493 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3494 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3495 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3496 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3497 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3498 | if (!name) |
| 3499 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3500 | if (NCH(n) == 3) { |
| 3501 | node *str_node = CHILD(n, 2); |
| 3502 | str = NEW_IDENTIFIER(str_node); |
| 3503 | if (!str) |
| 3504 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3505 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3506 | return NULL; |
| 3507 | } |
| 3508 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3509 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3510 | return NULL; |
| 3511 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3512 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3513 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3514 | case dotted_as_name: |
| 3515 | if (NCH(n) == 1) { |
| 3516 | n = CHILD(n, 0); |
| 3517 | goto loop; |
| 3518 | } |
| 3519 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3520 | node *asname_node = CHILD(n, 2); |
| 3521 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3522 | if (!a) |
| 3523 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3524 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3525 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3526 | if (!a->asname) |
| 3527 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3528 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3529 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3530 | return a; |
| 3531 | } |
| 3532 | break; |
| 3533 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3534 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3535 | node *name_node = CHILD(n, 0); |
| 3536 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3537 | if (!name) |
| 3538 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3539 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3540 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3541 | return alias(name, NULL, c->c_arena); |
| 3542 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3543 | else { |
| 3544 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3545 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3546 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3547 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | |
| 3550 | len = 0; |
| 3551 | for (i = 0; i < NCH(n); i += 2) |
| 3552 | /* length of string plus one for the dot */ |
| 3553 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3554 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3555 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3556 | if (!str) |
| 3557 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3558 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3559 | if (!s) |
| 3560 | return NULL; |
| 3561 | for (i = 0; i < NCH(n); i += 2) { |
| 3562 | char *sch = STR(CHILD(n, i)); |
| 3563 | strcpy(s, STR(CHILD(n, i))); |
| 3564 | s += strlen(sch); |
| 3565 | *s++ = '.'; |
| 3566 | } |
| 3567 | --s; |
| 3568 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3570 | PyBytes_GET_SIZE(str), |
| 3571 | NULL); |
| 3572 | Py_DECREF(str); |
| 3573 | if (!uni) |
| 3574 | return NULL; |
| 3575 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3576 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3577 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3578 | Py_DECREF(str); |
| 3579 | return NULL; |
| 3580 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3581 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3582 | } |
| 3583 | break; |
| 3584 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3585 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3586 | if (!str) |
| 3587 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3588 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3589 | Py_DECREF(str); |
| 3590 | return NULL; |
| 3591 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3592 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3593 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3594 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3595 | "unexpected import name: %d", TYPE(n)); |
| 3596 | return NULL; |
| 3597 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3598 | |
| 3599 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3600 | return NULL; |
| 3601 | } |
| 3602 | |
| 3603 | static stmt_ty |
| 3604 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3605 | { |
| 3606 | /* |
| 3607 | import_stmt: import_name | import_from |
| 3608 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3609 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3610 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3611 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3612 | int lineno; |
| 3613 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3614 | int i; |
| 3615 | asdl_seq *aliases; |
| 3616 | |
| 3617 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3618 | lineno = LINENO(n); |
| 3619 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3620 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3621 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3622 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3623 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3624 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3625 | if (!aliases) |
| 3626 | return NULL; |
| 3627 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3628 | 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] | 3629 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3630 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3631 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3632 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3633 | // Even though n is modified above, the end position is not changed |
| 3634 | return Import(aliases, lineno, col_offset, |
| 3635 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3636 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3637 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3638 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3639 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3640 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3641 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3642 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3644 | /* Count the number of dots (for relative imports) and check for the |
| 3645 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3646 | for (idx = 1; idx < NCH(n); idx++) { |
| 3647 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3648 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3649 | if (!mod) |
| 3650 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3651 | idx++; |
| 3652 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3653 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3655 | ndots += 3; |
| 3656 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3657 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3658 | break; |
| 3659 | } |
| 3660 | ndots++; |
| 3661 | } |
| 3662 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3663 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3664 | case STAR: |
| 3665 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3666 | n = CHILD(n, idx); |
| 3667 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3668 | break; |
| 3669 | case LPAR: |
| 3670 | /* from ... import (x, y, z) */ |
| 3671 | n = CHILD(n, idx + 1); |
| 3672 | n_children = NCH(n); |
| 3673 | break; |
| 3674 | case import_as_names: |
| 3675 | /* from ... import x, y, z */ |
| 3676 | n = CHILD(n, idx); |
| 3677 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3678 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3679 | ast_error(c, n, |
| 3680 | "trailing comma not allowed without" |
| 3681 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3682 | return NULL; |
| 3683 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3684 | break; |
| 3685 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3686 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3687 | return NULL; |
| 3688 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3689 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3690 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3691 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3692 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3693 | |
| 3694 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3695 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3696 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3697 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3698 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3699 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3700 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3701 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3702 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3703 | 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] | 3704 | if (!import_alias) |
| 3705 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3706 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3707 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3708 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3709 | if (mod != NULL) |
| 3710 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3711 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3712 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3713 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3714 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3715 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | "unknown import statement: starts with command '%s'", |
| 3717 | STR(CHILD(n, 0))); |
| 3718 | return NULL; |
| 3719 | } |
| 3720 | |
| 3721 | static stmt_ty |
| 3722 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3723 | { |
| 3724 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3725 | identifier name; |
| 3726 | asdl_seq *s; |
| 3727 | int i; |
| 3728 | |
| 3729 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3730 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3731 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3732 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3733 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3734 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3735 | if (!name) |
| 3736 | return NULL; |
| 3737 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3738 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3739 | return Global(s, LINENO(n), n->n_col_offset, |
| 3740 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3744 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3745 | { |
| 3746 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3747 | identifier name; |
| 3748 | asdl_seq *s; |
| 3749 | int i; |
| 3750 | |
| 3751 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3752 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3753 | if (!s) |
| 3754 | return NULL; |
| 3755 | for (i = 1; i < NCH(n); i += 2) { |
| 3756 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3757 | if (!name) |
| 3758 | return NULL; |
| 3759 | asdl_seq_SET(s, i / 2, name); |
| 3760 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3761 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3762 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3766 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3767 | { |
| 3768 | /* assert_stmt: 'assert' test [',' test] */ |
| 3769 | REQ(n, assert_stmt); |
| 3770 | if (NCH(n) == 2) { |
| 3771 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3772 | if (!expression) |
| 3773 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3774 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3775 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3776 | } |
| 3777 | else if (NCH(n) == 4) { |
| 3778 | expr_ty expr1, expr2; |
| 3779 | |
| 3780 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3781 | if (!expr1) |
| 3782 | return NULL; |
| 3783 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3784 | if (!expr2) |
| 3785 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3786 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3787 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3788 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3790 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3791 | "improper number of parts to 'assert' statement: %d", |
| 3792 | NCH(n)); |
| 3793 | return NULL; |
| 3794 | } |
| 3795 | |
| 3796 | static asdl_seq * |
| 3797 | ast_for_suite(struct compiling *c, const node *n) |
| 3798 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3799 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3800 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3801 | stmt_ty s; |
| 3802 | int i, total, num, end, pos = 0; |
| 3803 | node *ch; |
| 3804 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3805 | if (TYPE(n) != func_body_suite) { |
| 3806 | REQ(n, suite); |
| 3807 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3808 | |
| 3809 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3810 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3811 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3812 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3813 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3814 | n = CHILD(n, 0); |
| 3815 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3817 | */ |
| 3818 | end = NCH(n) - 1; |
| 3819 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3820 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3821 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3822 | for (i = 0; i < end; i += 2) { |
| 3823 | ch = CHILD(n, i); |
| 3824 | s = ast_for_stmt(c, ch); |
| 3825 | if (!s) |
| 3826 | return NULL; |
| 3827 | asdl_seq_SET(seq, pos++, s); |
| 3828 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3829 | } |
| 3830 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3831 | i = 2; |
| 3832 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3833 | i += 2; |
| 3834 | REQ(CHILD(n, 2), NEWLINE); |
| 3835 | } |
| 3836 | |
| 3837 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3838 | ch = CHILD(n, i); |
| 3839 | REQ(ch, stmt); |
| 3840 | num = num_stmts(ch); |
| 3841 | if (num == 1) { |
| 3842 | /* small_stmt or compound_stmt with only one child */ |
| 3843 | s = ast_for_stmt(c, ch); |
| 3844 | if (!s) |
| 3845 | return NULL; |
| 3846 | asdl_seq_SET(seq, pos++, s); |
| 3847 | } |
| 3848 | else { |
| 3849 | int j; |
| 3850 | ch = CHILD(ch, 0); |
| 3851 | REQ(ch, simple_stmt); |
| 3852 | for (j = 0; j < NCH(ch); j += 2) { |
| 3853 | /* statement terminates with a semi-colon ';' */ |
| 3854 | if (NCH(CHILD(ch, j)) == 0) { |
| 3855 | assert((j + 1) == NCH(ch)); |
| 3856 | break; |
| 3857 | } |
| 3858 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3859 | if (!s) |
| 3860 | return NULL; |
| 3861 | asdl_seq_SET(seq, pos++, s); |
| 3862 | } |
| 3863 | } |
| 3864 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3865 | } |
| 3866 | assert(pos == seq->size); |
| 3867 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3870 | static void |
| 3871 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3872 | { |
| 3873 | int tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame^] | 3874 | // There must be no empty suites. |
| 3875 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3876 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3877 | *end_lineno = last->end_lineno; |
| 3878 | *end_col_offset = last->end_col_offset; |
| 3879 | } |
| 3880 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3881 | static stmt_ty |
| 3882 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3883 | { |
| 3884 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3885 | ['else' ':' suite] |
| 3886 | */ |
| 3887 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3888 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3889 | |
| 3890 | REQ(n, if_stmt); |
| 3891 | |
| 3892 | if (NCH(n) == 4) { |
| 3893 | expr_ty expression; |
| 3894 | asdl_seq *suite_seq; |
| 3895 | |
| 3896 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3897 | if (!expression) |
| 3898 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3900 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3901 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3902 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3904 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3905 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3906 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3907 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3908 | s = STR(CHILD(n, 4)); |
| 3909 | /* s[2], the third character in the string, will be |
| 3910 | 's' for el_s_e, or |
| 3911 | 'i' for el_i_f |
| 3912 | */ |
| 3913 | if (s[2] == 's') { |
| 3914 | expr_ty expression; |
| 3915 | asdl_seq *seq1, *seq2; |
| 3916 | |
| 3917 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3918 | if (!expression) |
| 3919 | return NULL; |
| 3920 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3921 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3922 | return NULL; |
| 3923 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3924 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3925 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3926 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3927 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3928 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3929 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3930 | } |
| 3931 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3932 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3933 | expr_ty expression; |
| 3934 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3935 | asdl_seq *orelse = NULL; |
| 3936 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3937 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3938 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3939 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3940 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3941 | has_else = 1; |
| 3942 | n_elif -= 3; |
| 3943 | } |
| 3944 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3945 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3946 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3947 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3948 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3949 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3950 | if (!orelse) |
| 3951 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3952 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3953 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3954 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3955 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3956 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3957 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3958 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3959 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3960 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3961 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3963 | asdl_seq_SET(orelse, 0, |
| 3964 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3965 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3966 | CHILD(n, NCH(n) - 6)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3967 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3968 | /* the just-created orelse handled the last elif */ |
| 3969 | n_elif--; |
| 3970 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3971 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3972 | for (i = 0; i < n_elif; i++) { |
| 3973 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3974 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3975 | if (!newobj) |
| 3976 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3977 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3978 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3979 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3980 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3981 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3982 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3983 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3984 | if (orelse != NULL) { |
| 3985 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3986 | } else { |
| 3987 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3988 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3989 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3990 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3991 | LINENO(CHILD(n, off)), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3992 | CHILD(n, off)->n_col_offset, |
| 3993 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3994 | orelse = newobj; |
| 3995 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3996 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3997 | if (!expression) |
| 3998 | return NULL; |
| 3999 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 4000 | if (!suite_seq) |
| 4001 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4002 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4003 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4004 | LINENO(n), n->n_col_offset, |
| 4005 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4006 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4007 | |
| 4008 | PyErr_Format(PyExc_SystemError, |
| 4009 | "unexpected token in 'if' statement: %s", s); |
| 4010 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | static stmt_ty |
| 4014 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 4015 | { |
| 4016 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 4017 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4018 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4019 | |
| 4020 | if (NCH(n) == 4) { |
| 4021 | expr_ty expression; |
| 4022 | asdl_seq *suite_seq; |
| 4023 | |
| 4024 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4025 | if (!expression) |
| 4026 | return NULL; |
| 4027 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4028 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4029 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4030 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4031 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4032 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4033 | } |
| 4034 | else if (NCH(n) == 7) { |
| 4035 | expr_ty expression; |
| 4036 | asdl_seq *seq1, *seq2; |
| 4037 | |
| 4038 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4039 | if (!expression) |
| 4040 | return NULL; |
| 4041 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4042 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4043 | return NULL; |
| 4044 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4045 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4046 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4047 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4048 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4049 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4050 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4051 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4052 | |
| 4053 | PyErr_Format(PyExc_SystemError, |
| 4054 | "wrong number of tokens for 'while' statement: %d", |
| 4055 | NCH(n)); |
| 4056 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4060 | 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] | 4061 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4062 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4063 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4064 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4065 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4066 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4067 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4068 | int has_type_comment; |
| 4069 | string type_comment; |
| 4070 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4071 | REQ(n, for_stmt); |
| 4072 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4073 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4074 | |
| 4075 | if (NCH(n) == 9 + has_type_comment) { |
| 4076 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4077 | if (!seq) |
| 4078 | return NULL; |
| 4079 | } |
| 4080 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4081 | node_target = CHILD(n, 1); |
| 4082 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4083 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4084 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4085 | /* Check the # of children rather than the length of _target, since |
| 4086 | 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] | 4087 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4088 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4089 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4090 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4091 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4092 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4093 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4094 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4095 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4096 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4097 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4098 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4099 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4100 | return NULL; |
| 4101 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4102 | if (seq != NULL) { |
| 4103 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4104 | } else { |
| 4105 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4106 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4107 | |
| 4108 | if (has_type_comment) { |
| 4109 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4110 | if (!type_comment) |
| 4111 | return NULL; |
| 4112 | } |
| 4113 | else |
| 4114 | type_comment = NULL; |
| 4115 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4116 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4117 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4118 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4119 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4120 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4121 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4122 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4123 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | static excepthandler_ty |
| 4127 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4128 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4129 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4130 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4131 | REQ(exc, except_clause); |
| 4132 | REQ(body, suite); |
| 4133 | |
| 4134 | if (NCH(exc) == 1) { |
| 4135 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4136 | if (!suite_seq) |
| 4137 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4138 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4139 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4140 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4141 | exc->n_col_offset, |
| 4142 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4143 | } |
| 4144 | else if (NCH(exc) == 2) { |
| 4145 | expr_ty expression; |
| 4146 | asdl_seq *suite_seq; |
| 4147 | |
| 4148 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4149 | if (!expression) |
| 4150 | return NULL; |
| 4151 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4152 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4153 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4154 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4155 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4156 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4157 | exc->n_col_offset, |
| 4158 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4159 | } |
| 4160 | else if (NCH(exc) == 4) { |
| 4161 | asdl_seq *suite_seq; |
| 4162 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4163 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4164 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4165 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4166 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4167 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4168 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4169 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4170 | return NULL; |
| 4171 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4172 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4173 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4174 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4175 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4176 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4177 | exc->n_col_offset, |
| 4178 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4179 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4180 | |
| 4181 | PyErr_Format(PyExc_SystemError, |
| 4182 | "wrong number of children for 'except' clause: %d", |
| 4183 | NCH(exc)); |
| 4184 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | static stmt_ty |
| 4188 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4189 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4190 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4191 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4192 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4193 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4194 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | REQ(n, try_stmt); |
| 4196 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4197 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4198 | if (body == NULL) |
| 4199 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4200 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4201 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4202 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4203 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4204 | /* we can assume it's an "else", |
| 4205 | because nch >= 9 for try-else-finally and |
| 4206 | it would otherwise have a type of except_clause */ |
| 4207 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4208 | if (orelse == NULL) |
| 4209 | return NULL; |
| 4210 | n_except--; |
| 4211 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4212 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4213 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4214 | if (finally == NULL) |
| 4215 | return NULL; |
| 4216 | n_except--; |
| 4217 | } |
| 4218 | else { |
| 4219 | /* we can assume it's an "else", |
| 4220 | otherwise it would have a type of except_clause */ |
| 4221 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4222 | if (orelse == NULL) |
| 4223 | return NULL; |
| 4224 | n_except--; |
| 4225 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4226 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4227 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4228 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4229 | return NULL; |
| 4230 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4231 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4232 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4233 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4234 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4235 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4236 | if (handlers == NULL) |
| 4237 | return NULL; |
| 4238 | |
| 4239 | for (i = 0; i < n_except; i++) { |
| 4240 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4241 | CHILD(n, 5 + i * 3)); |
| 4242 | if (!e) |
| 4243 | return NULL; |
| 4244 | asdl_seq_SET(handlers, i, e); |
| 4245 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4248 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4249 | if (finally != NULL) { |
| 4250 | // finally is always last |
| 4251 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4252 | } else if (orelse != NULL) { |
| 4253 | // otherwise else is last |
| 4254 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4255 | } else { |
| 4256 | // inline the get_last_end_pos logic due to layout mismatch |
| 4257 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4258 | end_lineno = last_handler->end_lineno; |
| 4259 | end_col_offset = last_handler->end_col_offset; |
| 4260 | } |
| 4261 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4262 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4265 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4266 | static withitem_ty |
| 4267 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4268 | { |
| 4269 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4270 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4271 | REQ(n, with_item); |
| 4272 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4273 | if (!context_expr) |
| 4274 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4275 | if (NCH(n) == 3) { |
| 4276 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4277 | |
| 4278 | if (!optional_vars) { |
| 4279 | return NULL; |
| 4280 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4281 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4282 | return NULL; |
| 4283 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4286 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4289 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4290 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4291 | 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] | 4292 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4293 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4294 | 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] | 4295 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4296 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4297 | |
| 4298 | REQ(n, with_stmt); |
| 4299 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4300 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4301 | nch_minus_type = NCH(n) - has_type_comment; |
| 4302 | |
| 4303 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4304 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4305 | if (!items) |
| 4306 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4307 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4308 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4309 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4310 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4311 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4314 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4315 | if (!body) |
| 4316 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4317 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4318 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4319 | if (has_type_comment) { |
| 4320 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4321 | if (!type_comment) |
| 4322 | return NULL; |
| 4323 | } |
| 4324 | else |
| 4325 | type_comment = NULL; |
| 4326 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4327 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4328 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4329 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4330 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4331 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4332 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4335 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4336 | 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] | 4337 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4338 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4339 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4340 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4341 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4342 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4343 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4344 | REQ(n, classdef); |
| 4345 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4346 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4347 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4348 | if (!s) |
| 4349 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4350 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4351 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4352 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4353 | if (!classname) |
| 4354 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4355 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4356 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4357 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4358 | LINENO(n), n->n_col_offset, |
| 4359 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4360 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4361 | |
| 4362 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4363 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4364 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4365 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4366 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4367 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4368 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4369 | if (!classname) |
| 4370 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4371 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4372 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4373 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4374 | LINENO(n), n->n_col_offset, |
| 4375 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4378 | /* class NAME '(' arglist ')' ':' suite */ |
| 4379 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4380 | { |
| 4381 | PyObject *dummy_name; |
| 4382 | expr_ty dummy; |
| 4383 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4384 | if (!dummy_name) |
| 4385 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4386 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4387 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4388 | c->c_arena); |
| 4389 | 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] | 4390 | if (!call) |
| 4391 | return NULL; |
| 4392 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4393 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4394 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4395 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4396 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4397 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4398 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4399 | if (!classname) |
| 4400 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4401 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4402 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4403 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4404 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4405 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4406 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | static stmt_ty |
| 4410 | ast_for_stmt(struct compiling *c, const node *n) |
| 4411 | { |
| 4412 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4413 | assert(NCH(n) == 1); |
| 4414 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4415 | } |
| 4416 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4417 | assert(num_stmts(n) == 1); |
| 4418 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4419 | } |
| 4420 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4421 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4422 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4423 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4424 | */ |
| 4425 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4426 | case expr_stmt: |
| 4427 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4428 | case del_stmt: |
| 4429 | return ast_for_del_stmt(c, n); |
| 4430 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4431 | return Pass(LINENO(n), n->n_col_offset, |
| 4432 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4433 | case flow_stmt: |
| 4434 | return ast_for_flow_stmt(c, n); |
| 4435 | case import_stmt: |
| 4436 | return ast_for_import_stmt(c, n); |
| 4437 | case global_stmt: |
| 4438 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4439 | case nonlocal_stmt: |
| 4440 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4441 | case assert_stmt: |
| 4442 | return ast_for_assert_stmt(c, n); |
| 4443 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4444 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4445 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4446 | TYPE(n), NCH(n)); |
| 4447 | return NULL; |
| 4448 | } |
| 4449 | } |
| 4450 | else { |
| 4451 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4452 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4453 | */ |
| 4454 | node *ch = CHILD(n, 0); |
| 4455 | REQ(n, compound_stmt); |
| 4456 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4457 | case if_stmt: |
| 4458 | return ast_for_if_stmt(c, ch); |
| 4459 | case while_stmt: |
| 4460 | return ast_for_while_stmt(c, ch); |
| 4461 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4462 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4463 | case try_stmt: |
| 4464 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4465 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4466 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4467 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4468 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4469 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4470 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | case decorated: |
| 4472 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4473 | case async_stmt: |
| 4474 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4475 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4476 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4477 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4478 | TYPE(n), NCH(n)); |
| 4479 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4480 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4485 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4486 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4487 | const char *end; |
| 4488 | long x; |
| 4489 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4490 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4491 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4492 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4493 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4494 | errno = 0; |
| 4495 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4496 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4497 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4498 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4499 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4500 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4501 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4502 | } |
| 4503 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4504 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4505 | if (*end == '\0') { |
| 4506 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4507 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4508 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4509 | } |
| 4510 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4511 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4512 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4513 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4514 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4515 | return NULL; |
| 4516 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4517 | } |
| 4518 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4519 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4520 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4521 | if (dx == -1.0 && PyErr_Occurred()) |
| 4522 | return NULL; |
| 4523 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4524 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4528 | parsenumber(struct compiling *c, const char *s) |
| 4529 | { |
| 4530 | char *dup, *end; |
| 4531 | PyObject *res = NULL; |
| 4532 | |
| 4533 | assert(s != NULL); |
| 4534 | |
| 4535 | if (strchr(s, '_') == NULL) { |
| 4536 | return parsenumber_raw(c, s); |
| 4537 | } |
| 4538 | /* Create a duplicate without underscores. */ |
| 4539 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4540 | if (dup == NULL) { |
| 4541 | return PyErr_NoMemory(); |
| 4542 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4543 | end = dup; |
| 4544 | for (; *s; s++) { |
| 4545 | if (*s != '_') { |
| 4546 | *end++ = *s; |
| 4547 | } |
| 4548 | } |
| 4549 | *end = '\0'; |
| 4550 | res = parsenumber_raw(c, dup); |
| 4551 | PyMem_Free(dup); |
| 4552 | return res; |
| 4553 | } |
| 4554 | |
| 4555 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4556 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4557 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4558 | const char *s, *t; |
| 4559 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4560 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4561 | while (s < end && (*s & 0x80)) s++; |
| 4562 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4563 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4566 | static int |
| 4567 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4568 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4569 | { |
| 4570 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4571 | first_invalid_escape_char); |
| 4572 | if (msg == NULL) { |
| 4573 | return -1; |
| 4574 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4575 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4576 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4577 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4578 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4579 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4580 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4581 | to get a more accurate error report */ |
| 4582 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4583 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4584 | } |
| 4585 | Py_DECREF(msg); |
| 4586 | return -1; |
| 4587 | } |
| 4588 | Py_DECREF(msg); |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4592 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4593 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4594 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4595 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4596 | PyObject *v, *u; |
| 4597 | char *buf; |
| 4598 | char *p; |
| 4599 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4600 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4601 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4602 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4603 | return NULL; |
| 4604 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4605 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4606 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4607 | if (u == NULL) |
| 4608 | return NULL; |
| 4609 | p = buf = PyBytes_AsString(u); |
| 4610 | end = s + len; |
| 4611 | while (s < end) { |
| 4612 | if (*s == '\\') { |
| 4613 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4614 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4615 | strcpy(p, "u005c"); |
| 4616 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4617 | if (s >= end) |
| 4618 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4619 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4620 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4621 | if (*s & 0x80) { /* XXX inefficient */ |
| 4622 | PyObject *w; |
| 4623 | int kind; |
| 4624 | void *data; |
| 4625 | Py_ssize_t len, i; |
| 4626 | w = decode_utf8(c, &s, end); |
| 4627 | if (w == NULL) { |
| 4628 | Py_DECREF(u); |
| 4629 | return NULL; |
| 4630 | } |
| 4631 | kind = PyUnicode_KIND(w); |
| 4632 | data = PyUnicode_DATA(w); |
| 4633 | len = PyUnicode_GET_LENGTH(w); |
| 4634 | for (i = 0; i < len; i++) { |
| 4635 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4636 | sprintf(p, "\\U%08x", chr); |
| 4637 | p += 10; |
| 4638 | } |
| 4639 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4640 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4641 | Py_DECREF(w); |
| 4642 | } else { |
| 4643 | *p++ = *s++; |
| 4644 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4645 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4646 | len = p - buf; |
| 4647 | s = buf; |
| 4648 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4649 | const char *first_invalid_escape; |
| 4650 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4651 | |
| 4652 | if (v != NULL && first_invalid_escape != NULL) { |
| 4653 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4654 | /* We have not decref u before because first_invalid_escape points |
| 4655 | inside u. */ |
| 4656 | Py_XDECREF(u); |
| 4657 | Py_DECREF(v); |
| 4658 | return NULL; |
| 4659 | } |
| 4660 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4661 | Py_XDECREF(u); |
| 4662 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4665 | static PyObject * |
| 4666 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4667 | size_t len) |
| 4668 | { |
| 4669 | const char *first_invalid_escape; |
| 4670 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4671 | &first_invalid_escape); |
| 4672 | if (result == NULL) |
| 4673 | return NULL; |
| 4674 | |
| 4675 | if (first_invalid_escape != NULL) { |
| 4676 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4677 | Py_DECREF(result); |
| 4678 | return NULL; |
| 4679 | } |
| 4680 | } |
| 4681 | return result; |
| 4682 | } |
| 4683 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4684 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4685 | and `col_offset` to existing locations. */ |
| 4686 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4687 | { |
| 4688 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4689 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4690 | for (int i = 0; i < NCH(n); ++i) { |
| 4691 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4692 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4693 | col_offset = 0; |
| 4694 | } |
| 4695 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4696 | } |
| 4697 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4698 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | /* Fix locations for the given node and its children. |
| 4702 | |
| 4703 | `parent` is the enclosing node. |
| 4704 | `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] | 4705 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4706 | */ |
| 4707 | static void |
| 4708 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4709 | { |
| 4710 | char *substr = NULL; |
| 4711 | char *start; |
| 4712 | int lines = LINENO(parent) - 1; |
| 4713 | int cols = parent->n_col_offset; |
| 4714 | /* Find the full fstring to fix location information in `n`. */ |
| 4715 | while (parent && parent->n_type != STRING) |
| 4716 | parent = parent->n_child; |
| 4717 | if (parent && parent->n_str) { |
| 4718 | substr = strstr(parent->n_str, expr_str); |
| 4719 | if (substr) { |
| 4720 | start = substr; |
| 4721 | while (start > parent->n_str) { |
| 4722 | if (start[0] == '\n') |
| 4723 | break; |
| 4724 | start--; |
| 4725 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4726 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4727 | /* adjust the start based on the number of newlines encountered |
| 4728 | before the f-string expression */ |
| 4729 | for (char* p = parent->n_str; p < substr; p++) { |
| 4730 | if (*p == '\n') { |
| 4731 | lines++; |
| 4732 | } |
| 4733 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4734 | } |
| 4735 | } |
| 4736 | fstring_shift_node_locations(n, lines, cols); |
| 4737 | } |
| 4738 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4739 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4740 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4741 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4742 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4743 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4744 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4745 | { |
| 4746 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4747 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4748 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4749 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4750 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4751 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4752 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4753 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4754 | assert(*(expr_start-1) == '{'); |
| 4755 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4756 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4757 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4758 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4759 | because turning the expression '' in to '()' would go from being invalid |
| 4760 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4761 | for (s = expr_start; s != expr_end; s++) { |
| 4762 | char c = *s; |
| 4763 | /* The Python parser ignores only the following whitespace |
| 4764 | characters (\r already is converted to \n). */ |
| 4765 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4766 | break; |
| 4767 | } |
| 4768 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4769 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4770 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4771 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4772 | } |
| 4773 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4774 | len = expr_end - expr_start; |
| 4775 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4776 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4777 | if (str == NULL) { |
| 4778 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4779 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4780 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4781 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4782 | str[0] = '('; |
| 4783 | memcpy(str+1, expr_start, len); |
| 4784 | str[len+1] = ')'; |
| 4785 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4786 | |
| 4787 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4788 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4789 | Py_eval_input, 0); |
| 4790 | if (!mod_n) { |
| 4791 | PyMem_RawFree(str); |
| 4792 | return NULL; |
| 4793 | } |
| 4794 | /* Reuse str to find the correct column offset. */ |
| 4795 | str[0] = '{'; |
| 4796 | str[len+1] = '}'; |
| 4797 | fstring_fix_node_location(n, mod_n, str); |
| 4798 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4799 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4800 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4801 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4802 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4803 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | /* Return -1 on error. |
| 4807 | |
| 4808 | Return 0 if we reached the end of the literal. |
| 4809 | |
| 4810 | Return 1 if we haven't reached the end of the literal, but we want |
| 4811 | the caller to process the literal up to this point. Used for |
| 4812 | doubled braces. |
| 4813 | */ |
| 4814 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4815 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4816 | PyObject **literal, int recurse_lvl, |
| 4817 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4818 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4819 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4820 | brace (which isn't part of a unicode name escape such as |
| 4821 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4822 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4823 | const char *s = *str; |
| 4824 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4825 | int result = 0; |
| 4826 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4827 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4828 | while (s < end) { |
| 4829 | char ch = *s++; |
| 4830 | if (!raw && ch == '\\' && s < end) { |
| 4831 | ch = *s++; |
| 4832 | if (ch == 'N') { |
| 4833 | if (s < end && *s++ == '{') { |
| 4834 | while (s < end && *s++ != '}') { |
| 4835 | } |
| 4836 | continue; |
| 4837 | } |
| 4838 | break; |
| 4839 | } |
| 4840 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4841 | return -1; |
| 4842 | } |
| 4843 | } |
| 4844 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4845 | /* Check for doubled braces, but only at the top level. If |
| 4846 | we checked at every level, then f'{0:{3}}' would fail |
| 4847 | with the two closing braces. */ |
| 4848 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4849 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4850 | /* We're going to tell the caller that the literal ends |
| 4851 | here, but that they should continue scanning. But also |
| 4852 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4853 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4854 | result = 1; |
| 4855 | goto done; |
| 4856 | } |
| 4857 | |
| 4858 | /* Where a single '{' is the start of a new expression, a |
| 4859 | single '}' is not allowed. */ |
| 4860 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4861 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4862 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4863 | return -1; |
| 4864 | } |
| 4865 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4866 | /* We're either at a '{', which means we're starting another |
| 4867 | expression; or a '}', which means we're at the end of this |
| 4868 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4869 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4870 | break; |
| 4871 | } |
| 4872 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4873 | *str = s; |
| 4874 | assert(s <= end); |
| 4875 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4876 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4877 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4878 | if (raw) |
| 4879 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4880 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4881 | NULL, NULL); |
| 4882 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4883 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4884 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4885 | if (!*literal) |
| 4886 | return -1; |
| 4887 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4888 | return result; |
| 4889 | } |
| 4890 | |
| 4891 | /* Forward declaration because parsing is recursive. */ |
| 4892 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4893 | 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] | 4894 | struct compiling *c, const node *n); |
| 4895 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4896 | /* 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] | 4897 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4898 | which includes the expression, conversion character, and |
| 4899 | format_spec expression. |
| 4900 | |
| 4901 | Note that I don't do a perfect job here: I don't make sure that a |
| 4902 | closing brace doesn't match an opening paren, for example. It |
| 4903 | doesn't need to error on all invalid expressions, just correctly |
| 4904 | find the end of all valid ones. Any errors inside the expression |
| 4905 | will be caught when we parse it later. */ |
| 4906 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4907 | 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] | 4908 | expr_ty *expression, struct compiling *c, const node *n) |
| 4909 | { |
| 4910 | /* Return -1 on error, else 0. */ |
| 4911 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4912 | const char *expr_start; |
| 4913 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4914 | expr_ty simple_expression; |
| 4915 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4916 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4917 | |
| 4918 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4919 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4920 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4921 | |
| 4922 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4923 | int string_type = 0; |
| 4924 | |
| 4925 | /* Keep track of nesting level for braces/parens/brackets in |
| 4926 | expressions. */ |
| 4927 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4928 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4929 | |
| 4930 | /* Can only nest one level deep. */ |
| 4931 | if (recurse_lvl >= 2) { |
| 4932 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4933 | return -1; |
| 4934 | } |
| 4935 | |
| 4936 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4937 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4938 | assert(**str == '{'); |
| 4939 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4940 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4941 | expr_start = *str; |
| 4942 | for (; *str < end; (*str)++) { |
| 4943 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4944 | |
| 4945 | /* Loop invariants. */ |
| 4946 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4947 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4948 | if (quote_char) |
| 4949 | assert(string_type == 1 || string_type == 3); |
| 4950 | else |
| 4951 | assert(string_type == 0); |
| 4952 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4953 | ch = **str; |
| 4954 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4955 | if (ch == '\\') { |
| 4956 | /* Error: can't include a backslash character, inside |
| 4957 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4958 | ast_error(c, n, |
| 4959 | "f-string expression part " |
| 4960 | "cannot include a backslash"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4961 | return -1; |
| 4962 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4963 | if (quote_char) { |
| 4964 | /* We're inside a string. See if we're at the end. */ |
| 4965 | /* This code needs to implement the same non-error logic |
| 4966 | as tok_get from tokenizer.c, at the letter_quote |
| 4967 | label. To actually share that code would be a |
| 4968 | nightmare. But, it's unlikely to change and is small, |
| 4969 | so duplicate it here. Note we don't need to catch all |
| 4970 | of the errors, since they'll be caught when parsing the |
| 4971 | expression. We just need to match the non-error |
| 4972 | cases. Thus we can ignore \n in single-quoted strings, |
| 4973 | for example. Or non-terminated strings. */ |
| 4974 | if (ch == quote_char) { |
| 4975 | /* Does this match the string_type (single or triple |
| 4976 | quoted)? */ |
| 4977 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4978 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4979 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4980 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4981 | string_type = 0; |
| 4982 | quote_char = 0; |
| 4983 | continue; |
| 4984 | } |
| 4985 | } else { |
| 4986 | /* We're at the end of a normal string. */ |
| 4987 | quote_char = 0; |
| 4988 | string_type = 0; |
| 4989 | continue; |
| 4990 | } |
| 4991 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4992 | } else if (ch == '\'' || ch == '"') { |
| 4993 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4994 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4995 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4996 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4997 | } else { |
| 4998 | /* Start of a normal string. */ |
| 4999 | string_type = 1; |
| 5000 | } |
| 5001 | /* Start looking for the end of the string. */ |
| 5002 | quote_char = ch; |
| 5003 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5004 | if (nested_depth >= MAXLEVEL) { |
| 5005 | ast_error(c, n, "f-string: too many nested parenthesis"); |
| 5006 | return -1; |
| 5007 | } |
| 5008 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5009 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5010 | } else if (ch == '#') { |
| 5011 | /* Error: can't include a comment character, inside parens |
| 5012 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5013 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5014 | return -1; |
| 5015 | } else if (nested_depth == 0 && |
| 5016 | (ch == '!' || ch == ':' || ch == '}')) { |
| 5017 | /* First, test for the special case of "!=". Since '=' is |
| 5018 | not an allowed conversion character, nothing is lost in |
| 5019 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5020 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5021 | /* This isn't a conversion character, just continue. */ |
| 5022 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5023 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5024 | /* Normal way out of this loop. */ |
| 5025 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5026 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5027 | if (!nested_depth) { |
| 5028 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
| 5029 | return -1; |
| 5030 | } |
| 5031 | nested_depth--; |
| 5032 | int opening = parenstack[nested_depth]; |
| 5033 | if (!((opening == '(' && ch == ')') || |
| 5034 | (opening == '[' && ch == ']') || |
| 5035 | (opening == '{' && ch == '}'))) |
| 5036 | { |
| 5037 | ast_error(c, n, |
| 5038 | "f-string: closing parenthesis '%c' " |
| 5039 | "does not match opening parenthesis '%c'", |
| 5040 | ch, opening); |
| 5041 | return -1; |
| 5042 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5043 | } else { |
| 5044 | /* Just consume this char and loop around. */ |
| 5045 | } |
| 5046 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5047 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5048 | /* If we leave this loop in a string or with mismatched parens, we |
| 5049 | don't care. We'll get a syntax error when compiling the |
| 5050 | expression. But, we can produce a better error message, so |
| 5051 | let's just do that.*/ |
| 5052 | if (quote_char) { |
| 5053 | ast_error(c, n, "f-string: unterminated string"); |
| 5054 | return -1; |
| 5055 | } |
| 5056 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5057 | int opening = parenstack[nested_depth - 1]; |
| 5058 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5059 | return -1; |
| 5060 | } |
| 5061 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5062 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5063 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5064 | |
| 5065 | /* Compile the expression as soon as possible, so we show errors |
| 5066 | related to the expression before errors related to the |
| 5067 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5068 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5069 | if (!simple_expression) |
| 5070 | return -1; |
| 5071 | |
| 5072 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5073 | if (**str == '!') { |
| 5074 | *str += 1; |
| 5075 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5076 | goto unexpected_end_of_string; |
| 5077 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5078 | conversion = **str; |
| 5079 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5080 | |
| 5081 | /* Validate the conversion. */ |
| 5082 | if (!(conversion == 's' || conversion == 'r' |
| 5083 | || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5084 | ast_error(c, n, |
| 5085 | "f-string: invalid conversion character: " |
| 5086 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5087 | return -1; |
| 5088 | } |
| 5089 | } |
| 5090 | |
| 5091 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5092 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5093 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5094 | if (**str == ':') { |
| 5095 | *str += 1; |
| 5096 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5097 | goto unexpected_end_of_string; |
| 5098 | |
| 5099 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5100 | 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] | 5101 | if (!format_spec) |
| 5102 | return -1; |
| 5103 | } |
| 5104 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5105 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5106 | goto unexpected_end_of_string; |
| 5107 | |
| 5108 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5109 | assert(*str < end); |
| 5110 | assert(**str == '}'); |
| 5111 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5112 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5113 | /* And now create the FormattedValue node that represents this |
| 5114 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5115 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5116 | format_spec, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5117 | n->n_end_lineno, n->n_end_col_offset, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5118 | c->c_arena); |
| 5119 | if (!*expression) |
| 5120 | return -1; |
| 5121 | |
| 5122 | return 0; |
| 5123 | |
| 5124 | unexpected_end_of_string: |
| 5125 | ast_error(c, n, "f-string: expecting '}'"); |
| 5126 | return -1; |
| 5127 | } |
| 5128 | |
| 5129 | /* Return -1 on error. |
| 5130 | |
| 5131 | Return 0 if we have a literal (possible zero length) and an |
| 5132 | expression (zero length if at the end of the string. |
| 5133 | |
| 5134 | Return 1 if we have a literal, but no expression, and we want the |
| 5135 | caller to call us again. This is used to deal with doubled |
| 5136 | braces. |
| 5137 | |
| 5138 | When called multiple times on the string 'a{{b{0}c', this function |
| 5139 | will return: |
| 5140 | |
| 5141 | 1. the literal 'a{' with no expression, and a return value |
| 5142 | of 1. Despite the fact that there's no expression, the return |
| 5143 | value of 1 means we're not finished yet. |
| 5144 | |
| 5145 | 2. the literal 'b' and the expression '0', with a return value of |
| 5146 | 0. The fact that there's an expression means we're not finished. |
| 5147 | |
| 5148 | 3. literal 'c' with no expression and a return value of 0. The |
| 5149 | combination of the return value of 0 with no expression means |
| 5150 | we're finished. |
| 5151 | */ |
| 5152 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5153 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5154 | int recurse_lvl, PyObject **literal, |
| 5155 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5156 | struct compiling *c, const node *n) |
| 5157 | { |
| 5158 | int result; |
| 5159 | |
| 5160 | assert(*literal == NULL && *expression == NULL); |
| 5161 | |
| 5162 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5163 | 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] | 5164 | if (result < 0) |
| 5165 | goto error; |
| 5166 | |
| 5167 | assert(result == 0 || result == 1); |
| 5168 | |
| 5169 | if (result == 1) |
| 5170 | /* We have a literal, but don't look at the expression. */ |
| 5171 | return 1; |
| 5172 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5173 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5174 | /* We're at the end of the string or the end of a nested |
| 5175 | f-string: no expression. The top-level error case where we |
| 5176 | expect to be at the end of the string but we're at a '}' is |
| 5177 | handled later. */ |
| 5178 | return 0; |
| 5179 | |
| 5180 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5181 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5182 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | 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] | 5184 | goto error; |
| 5185 | |
| 5186 | return 0; |
| 5187 | |
| 5188 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5189 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5190 | return -1; |
| 5191 | } |
| 5192 | |
| 5193 | #define EXPRLIST_N_CACHED 64 |
| 5194 | |
| 5195 | typedef struct { |
| 5196 | /* Incrementally build an array of expr_ty, so be used in an |
| 5197 | asdl_seq. Cache some small but reasonably sized number of |
| 5198 | expr_ty's, and then after that start dynamically allocating, |
| 5199 | doubling the number allocated each time. Note that the f-string |
| 5200 | 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] | 5201 | 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] | 5202 | fast as you add exressions in an f-string. */ |
| 5203 | |
| 5204 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5205 | Py_ssize_t size; /* Number we've used. */ |
| 5206 | expr_ty *p; /* Pointer to the memory we're actually |
| 5207 | using. Will point to 'data' until we |
| 5208 | start dynamically allocating. */ |
| 5209 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5210 | } ExprList; |
| 5211 | |
| 5212 | #ifdef NDEBUG |
| 5213 | #define ExprList_check_invariants(l) |
| 5214 | #else |
| 5215 | static void |
| 5216 | ExprList_check_invariants(ExprList *l) |
| 5217 | { |
| 5218 | /* Check our invariants. Make sure this object is "live", and |
| 5219 | hasn't been deallocated. */ |
| 5220 | assert(l->size >= 0); |
| 5221 | assert(l->p != NULL); |
| 5222 | if (l->size <= EXPRLIST_N_CACHED) |
| 5223 | assert(l->data == l->p); |
| 5224 | } |
| 5225 | #endif |
| 5226 | |
| 5227 | static void |
| 5228 | ExprList_Init(ExprList *l) |
| 5229 | { |
| 5230 | l->allocated = EXPRLIST_N_CACHED; |
| 5231 | l->size = 0; |
| 5232 | |
| 5233 | /* Until we start allocating dynamically, p points to data. */ |
| 5234 | l->p = l->data; |
| 5235 | |
| 5236 | ExprList_check_invariants(l); |
| 5237 | } |
| 5238 | |
| 5239 | static int |
| 5240 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5241 | { |
| 5242 | ExprList_check_invariants(l); |
| 5243 | if (l->size >= l->allocated) { |
| 5244 | /* We need to alloc (or realloc) the memory. */ |
| 5245 | Py_ssize_t new_size = l->allocated * 2; |
| 5246 | |
| 5247 | /* See if we've ever allocated anything dynamically. */ |
| 5248 | if (l->p == l->data) { |
| 5249 | Py_ssize_t i; |
| 5250 | /* We're still using the cached data. Switch to |
| 5251 | alloc-ing. */ |
| 5252 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5253 | if (!l->p) |
| 5254 | return -1; |
| 5255 | /* Copy the cached data into the new buffer. */ |
| 5256 | for (i = 0; i < l->size; i++) |
| 5257 | l->p[i] = l->data[i]; |
| 5258 | } else { |
| 5259 | /* Just realloc. */ |
| 5260 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5261 | if (!tmp) { |
| 5262 | PyMem_RawFree(l->p); |
| 5263 | l->p = NULL; |
| 5264 | return -1; |
| 5265 | } |
| 5266 | l->p = tmp; |
| 5267 | } |
| 5268 | |
| 5269 | l->allocated = new_size; |
| 5270 | assert(l->allocated == 2 * l->size); |
| 5271 | } |
| 5272 | |
| 5273 | l->p[l->size++] = exp; |
| 5274 | |
| 5275 | ExprList_check_invariants(l); |
| 5276 | return 0; |
| 5277 | } |
| 5278 | |
| 5279 | static void |
| 5280 | ExprList_Dealloc(ExprList *l) |
| 5281 | { |
| 5282 | ExprList_check_invariants(l); |
| 5283 | |
| 5284 | /* If there's been an error, or we've never dynamically allocated, |
| 5285 | do nothing. */ |
| 5286 | if (!l->p || l->p == l->data) { |
| 5287 | /* Do nothing. */ |
| 5288 | } else { |
| 5289 | /* We have dynamically allocated. Free the memory. */ |
| 5290 | PyMem_RawFree(l->p); |
| 5291 | } |
| 5292 | l->p = NULL; |
| 5293 | l->size = -1; |
| 5294 | } |
| 5295 | |
| 5296 | static asdl_seq * |
| 5297 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5298 | { |
| 5299 | asdl_seq *seq; |
| 5300 | |
| 5301 | ExprList_check_invariants(l); |
| 5302 | |
| 5303 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5304 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5305 | if (seq) { |
| 5306 | Py_ssize_t i; |
| 5307 | for (i = 0; i < l->size; i++) |
| 5308 | asdl_seq_SET(seq, i, l->p[i]); |
| 5309 | } |
| 5310 | ExprList_Dealloc(l); |
| 5311 | return seq; |
| 5312 | } |
| 5313 | |
| 5314 | /* The FstringParser is designed to add a mix of strings and |
| 5315 | f-strings, and concat them together as needed. Ultimately, it |
| 5316 | generates an expr_ty. */ |
| 5317 | typedef struct { |
| 5318 | PyObject *last_str; |
| 5319 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5320 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5321 | } FstringParser; |
| 5322 | |
| 5323 | #ifdef NDEBUG |
| 5324 | #define FstringParser_check_invariants(state) |
| 5325 | #else |
| 5326 | static void |
| 5327 | FstringParser_check_invariants(FstringParser *state) |
| 5328 | { |
| 5329 | if (state->last_str) |
| 5330 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5331 | ExprList_check_invariants(&state->expr_list); |
| 5332 | } |
| 5333 | #endif |
| 5334 | |
| 5335 | static void |
| 5336 | FstringParser_Init(FstringParser *state) |
| 5337 | { |
| 5338 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5339 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5340 | ExprList_Init(&state->expr_list); |
| 5341 | FstringParser_check_invariants(state); |
| 5342 | } |
| 5343 | |
| 5344 | static void |
| 5345 | FstringParser_Dealloc(FstringParser *state) |
| 5346 | { |
| 5347 | FstringParser_check_invariants(state); |
| 5348 | |
| 5349 | Py_XDECREF(state->last_str); |
| 5350 | ExprList_Dealloc(&state->expr_list); |
| 5351 | } |
| 5352 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5353 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5354 | static expr_ty |
| 5355 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5356 | { |
| 5357 | PyObject *s = *str; |
| 5358 | *str = NULL; |
| 5359 | assert(PyUnicode_CheckExact(s)); |
| 5360 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5361 | Py_DECREF(s); |
| 5362 | return NULL; |
| 5363 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5364 | return Constant(s, LINENO(n), n->n_col_offset, |
| 5365 | 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] | 5366 | } |
| 5367 | |
| 5368 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5369 | decref'd. */ |
| 5370 | static int |
| 5371 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5372 | { |
| 5373 | FstringParser_check_invariants(state); |
| 5374 | |
| 5375 | assert(PyUnicode_CheckExact(str)); |
| 5376 | |
| 5377 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5378 | Py_DECREF(str); |
| 5379 | return 0; |
| 5380 | } |
| 5381 | |
| 5382 | if (!state->last_str) { |
| 5383 | /* We didn't have a string before, so just remember this one. */ |
| 5384 | state->last_str = str; |
| 5385 | } else { |
| 5386 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5387 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5388 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5389 | return -1; |
| 5390 | } |
| 5391 | FstringParser_check_invariants(state); |
| 5392 | return 0; |
| 5393 | } |
| 5394 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5395 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5396 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5397 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5398 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5399 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5400 | struct compiling *c, const node *n) |
| 5401 | { |
| 5402 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5403 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5404 | |
| 5405 | /* Parse the f-string. */ |
| 5406 | while (1) { |
| 5407 | PyObject *literal = NULL; |
| 5408 | expr_ty expression = NULL; |
| 5409 | |
| 5410 | /* If there's a zero length literal in front of the |
| 5411 | expression, literal will be NULL. If we're at the end of |
| 5412 | the f-string, expression will be NULL (unless result == 1, |
| 5413 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5414 | 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] | 5415 | &literal, &expression, |
| 5416 | c, n); |
| 5417 | if (result < 0) |
| 5418 | return -1; |
| 5419 | |
| 5420 | /* Add the literal, if any. */ |
| 5421 | if (!literal) { |
| 5422 | /* Do nothing. Just leave last_str alone (and possibly |
| 5423 | NULL). */ |
| 5424 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 5425 | /* Note that the literal can be zero length, if the |
| 5426 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5427 | state->last_str = literal; |
| 5428 | literal = NULL; |
| 5429 | } else { |
| 5430 | /* We have a literal, concatenate it. */ |
| 5431 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 5432 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 5433 | return -1; |
| 5434 | literal = NULL; |
| 5435 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5436 | |
| 5437 | /* We've dealt with the literal now. It can't be leaked on further |
| 5438 | errors. */ |
| 5439 | assert(literal == NULL); |
| 5440 | |
| 5441 | /* See if we should just loop around to get the next literal |
| 5442 | and expression, while ignoring the expression this |
| 5443 | time. This is used for un-doubling braces, as an |
| 5444 | optimization. */ |
| 5445 | if (result == 1) |
| 5446 | continue; |
| 5447 | |
| 5448 | if (!expression) |
| 5449 | /* We're done with this f-string. */ |
| 5450 | break; |
| 5451 | |
| 5452 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5453 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5454 | if (!state->last_str) { |
| 5455 | /* Do nothing. No previous literal. */ |
| 5456 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5457 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5458 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5459 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5460 | return -1; |
| 5461 | } |
| 5462 | |
| 5463 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5464 | return -1; |
| 5465 | } |
| 5466 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5467 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5468 | string. Otherwise, we must be at a right brace. */ |
| 5469 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5470 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5471 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5472 | return -1; |
| 5473 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5474 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5475 | ast_error(c, n, "f-string: expecting '}'"); |
| 5476 | return -1; |
| 5477 | } |
| 5478 | |
| 5479 | FstringParser_check_invariants(state); |
| 5480 | return 0; |
| 5481 | } |
| 5482 | |
| 5483 | /* 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] | 5484 | 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] | 5485 | static expr_ty |
| 5486 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5487 | const node *n) |
| 5488 | { |
| 5489 | asdl_seq *seq; |
| 5490 | |
| 5491 | FstringParser_check_invariants(state); |
| 5492 | |
| 5493 | /* If we're just a constant string with no expressions, return |
| 5494 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5495 | if (!state->fmode) { |
| 5496 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5497 | if (!state->last_str) { |
| 5498 | /* Create a zero length string. */ |
| 5499 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5500 | if (!state->last_str) |
| 5501 | goto error; |
| 5502 | } |
| 5503 | return make_str_node_and_del(&state->last_str, c, n); |
| 5504 | } |
| 5505 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5506 | /* 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] | 5507 | last node in our expression list. */ |
| 5508 | if (state->last_str) { |
| 5509 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5510 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5511 | goto error; |
| 5512 | } |
| 5513 | /* This has already been freed. */ |
| 5514 | assert(state->last_str == NULL); |
| 5515 | |
| 5516 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5517 | if (!seq) |
| 5518 | goto error; |
| 5519 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5520 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5521 | 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] | 5522 | |
| 5523 | error: |
| 5524 | FstringParser_Dealloc(state); |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5528 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5529 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5530 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5531 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5532 | 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] | 5533 | struct compiling *c, const node *n) |
| 5534 | { |
| 5535 | FstringParser state; |
| 5536 | |
| 5537 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5538 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5539 | c, n) < 0) { |
| 5540 | FstringParser_Dealloc(&state); |
| 5541 | return NULL; |
| 5542 | } |
| 5543 | |
| 5544 | return FstringParser_Finish(&state, c, n); |
| 5545 | } |
| 5546 | |
| 5547 | /* n is a Python string literal, including the bracketing quote |
| 5548 | 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] | 5549 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5550 | 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] | 5551 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5552 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5553 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5554 | static int |
| 5555 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5556 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5557 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5558 | size_t len; |
| 5559 | const char *s = STR(n); |
| 5560 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5561 | int fmode = 0; |
| 5562 | *bytesmode = 0; |
| 5563 | *rawmode = 0; |
| 5564 | *result = NULL; |
| 5565 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5566 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5567 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5568 | if (quote == 'b' || quote == 'B') { |
| 5569 | quote = *++s; |
| 5570 | *bytesmode = 1; |
| 5571 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5572 | else if (quote == 'u' || quote == 'U') { |
| 5573 | quote = *++s; |
| 5574 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5575 | else if (quote == 'r' || quote == 'R') { |
| 5576 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5577 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5578 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5579 | else if (quote == 'f' || quote == 'F') { |
| 5580 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5581 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5582 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5583 | else { |
| 5584 | break; |
| 5585 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5586 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5587 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5588 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5589 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5590 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5591 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5592 | if (quote != '\'' && quote != '\"') { |
| 5593 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5594 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5595 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5596 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5597 | s++; |
| 5598 | len = strlen(s); |
| 5599 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5600 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5601 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5602 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5603 | } |
| 5604 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5605 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5606 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5607 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5608 | } |
| 5609 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5610 | /* A triple quoted string. We've already skipped one quote at |
| 5611 | the start and one at the end of the string. Now skip the |
| 5612 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5613 | s += 2; |
| 5614 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5615 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5616 | if (s[--len] != quote || s[--len] != quote) { |
| 5617 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5618 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5619 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5620 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5621 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5622 | if (fmode) { |
| 5623 | /* Just return the bytes. The caller will parse the resulting |
| 5624 | string. */ |
| 5625 | *fstr = s; |
| 5626 | *fstrlen = len; |
| 5627 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5628 | } |
| 5629 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5630 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5631 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5632 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5633 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5634 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5635 | const char *ch; |
| 5636 | for (ch = s; *ch; ch++) { |
| 5637 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5638 | ast_error(c, n, |
| 5639 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5640 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5641 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5642 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5643 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5644 | if (*rawmode) |
| 5645 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5646 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5647 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5648 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5649 | if (*rawmode) |
| 5650 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5651 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5652 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5653 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5654 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5657 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5658 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5659 | 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] | 5660 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5661 | 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] | 5662 | node if there's just an f-string (with no leading or trailing |
| 5663 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5664 | any literals involved. */ |
| 5665 | static expr_ty |
| 5666 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5667 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5668 | int bytesmode = 0; |
| 5669 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5670 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5671 | |
| 5672 | FstringParser state; |
| 5673 | FstringParser_Init(&state); |
| 5674 | |
| 5675 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5676 | int this_bytesmode; |
| 5677 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5678 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5679 | const char *fstr; |
| 5680 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5681 | |
| 5682 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5683 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5684 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5685 | goto error; |
| 5686 | |
| 5687 | /* Check that we're not mixing bytes with unicode. */ |
| 5688 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5689 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5690 | /* s is NULL if the current string part is an f-string. */ |
| 5691 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5692 | goto error; |
| 5693 | } |
| 5694 | bytesmode = this_bytesmode; |
| 5695 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5696 | if (fstr != NULL) { |
| 5697 | int result; |
| 5698 | assert(s == NULL && !bytesmode); |
| 5699 | /* This is an f-string. Parse and concatenate it. */ |
| 5700 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5701 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5702 | if (result < 0) |
| 5703 | goto error; |
| 5704 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5705 | /* A string or byte string. */ |
| 5706 | assert(s != NULL && fstr == NULL); |
| 5707 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5708 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5709 | PyUnicode_CheckExact(s)); |
| 5710 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5711 | if (bytesmode) { |
| 5712 | /* For bytes, concat as we go. */ |
| 5713 | if (i == 0) { |
| 5714 | /* First time, just remember this value. */ |
| 5715 | bytes_str = s; |
| 5716 | } else { |
| 5717 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5718 | if (!bytes_str) |
| 5719 | goto error; |
| 5720 | } |
| 5721 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5722 | /* This is a regular string. Concatenate it. */ |
| 5723 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5724 | goto error; |
| 5725 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5726 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5727 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5728 | if (bytesmode) { |
| 5729 | /* Just return the bytes object and we're done. */ |
| 5730 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5731 | goto error; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5732 | return Constant(bytes_str, LINENO(n), n->n_col_offset, |
| 5733 | 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] | 5734 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5735 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5736 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5737 | assert(bytes_str == NULL); |
| 5738 | |
| 5739 | return FstringParser_Finish(&state, c, n); |
| 5740 | |
| 5741 | error: |
| 5742 | Py_XDECREF(bytes_str); |
| 5743 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5744 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5745 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5746 | |
| 5747 | PyObject * |
| 5748 | _PyAST_GetDocString(asdl_seq *body) |
| 5749 | { |
| 5750 | if (!asdl_seq_LEN(body)) { |
| 5751 | return NULL; |
| 5752 | } |
| 5753 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5754 | if (st->kind != Expr_kind) { |
| 5755 | return NULL; |
| 5756 | } |
| 5757 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5758 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5759 | return e->v.Constant.value; |
| 5760 | } |
| 5761 | return NULL; |
| 5762 | } |