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 |
Miss Islington (bot) | 90ee51f | 2020-06-06 10:04:38 -0700 | [diff] [blame] | 25 | validate_name(PyObject *name) |
| 26 | { |
| 27 | assert(PyUnicode_Check(name)); |
| 28 | static const char * const forbidden[] = { |
| 29 | "None", |
| 30 | "True", |
| 31 | "False", |
| 32 | NULL |
| 33 | }; |
| 34 | for (int i = 0; forbidden[i] != NULL; i++) { |
| 35 | if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) { |
| 36 | PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]); |
| 37 | return 0; |
| 38 | } |
| 39 | } |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 44 | validate_comprehension(asdl_seq *gens) |
| 45 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 46 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 47 | if (!asdl_seq_LEN(gens)) { |
| 48 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 49 | return 0; |
| 50 | } |
| 51 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 52 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 53 | if (!validate_expr(comp->target, Store) || |
| 54 | !validate_expr(comp->iter, Load) || |
| 55 | !validate_exprs(comp->ifs, Load, 0)) |
| 56 | return 0; |
| 57 | } |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 62 | validate_keywords(asdl_seq *keywords) |
| 63 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 64 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 65 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 66 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 67 | return 0; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | validate_args(asdl_seq *args) |
| 73 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 74 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 75 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 76 | arg_ty arg = asdl_seq_GET(args, i); |
| 77 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 78 | return 0; |
| 79 | } |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | static const char * |
| 84 | expr_context_name(expr_context_ty ctx) |
| 85 | { |
| 86 | switch (ctx) { |
| 87 | case Load: |
| 88 | return "Load"; |
| 89 | case Store: |
| 90 | return "Store"; |
| 91 | case Del: |
| 92 | return "Del"; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 93 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 94 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | static int |
| 99 | validate_arguments(arguments_ty args) |
| 100 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 101 | if (!validate_args(args->posonlyargs) || !validate_args(args->args)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 102 | return 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 103 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 104 | if (args->vararg && args->vararg->annotation |
| 105 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | if (!validate_args(args->kwonlyargs)) |
| 109 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 110 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 111 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 112 | return 0; |
| 113 | } |
Pablo Galindo | 2f58a84 | 2019-05-31 14:09:49 +0100 | [diff] [blame] | 114 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 115 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 116 | return 0; |
| 117 | } |
| 118 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 119 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 120 | "kw_defaults on arguments"); |
| 121 | return 0; |
| 122 | } |
| 123 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 124 | } |
| 125 | |
| 126 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 127 | validate_constant(PyObject *value) |
| 128 | { |
| 129 | if (value == Py_None || value == Py_Ellipsis) |
| 130 | return 1; |
| 131 | |
| 132 | if (PyLong_CheckExact(value) |
| 133 | || PyFloat_CheckExact(value) |
| 134 | || PyComplex_CheckExact(value) |
| 135 | || PyBool_Check(value) |
| 136 | || PyUnicode_CheckExact(value) |
| 137 | || PyBytes_CheckExact(value)) |
| 138 | return 1; |
| 139 | |
| 140 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 141 | PyObject *it; |
| 142 | |
| 143 | it = PyObject_GetIter(value); |
| 144 | if (it == NULL) |
| 145 | return 0; |
| 146 | |
| 147 | while (1) { |
| 148 | PyObject *item = PyIter_Next(it); |
| 149 | if (item == NULL) { |
| 150 | if (PyErr_Occurred()) { |
| 151 | Py_DECREF(it); |
| 152 | return 0; |
| 153 | } |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | if (!validate_constant(item)) { |
| 158 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 159 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 162 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | Py_DECREF(it); |
| 166 | return 1; |
| 167 | } |
| 168 | |
Batuhan TaĆkaya | 0ac59f9 | 2020-03-19 14:32:28 +0300 | [diff] [blame] | 169 | if (!PyErr_Occurred()) { |
| 170 | PyErr_Format(PyExc_TypeError, |
| 171 | "got an invalid type in Constant: %s", |
| 172 | _PyType_Name(Py_TYPE(value))); |
| 173 | } |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 178 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 179 | { |
| 180 | int check_ctx = 1; |
| 181 | expr_context_ty actual_ctx; |
| 182 | |
| 183 | /* First check expression context. */ |
| 184 | switch (exp->kind) { |
| 185 | case Attribute_kind: |
| 186 | actual_ctx = exp->v.Attribute.ctx; |
| 187 | break; |
| 188 | case Subscript_kind: |
| 189 | actual_ctx = exp->v.Subscript.ctx; |
| 190 | break; |
| 191 | case Starred_kind: |
| 192 | actual_ctx = exp->v.Starred.ctx; |
| 193 | break; |
| 194 | case Name_kind: |
Miss Islington (bot) | 90ee51f | 2020-06-06 10:04:38 -0700 | [diff] [blame] | 195 | if (!validate_name(exp->v.Name.id)) { |
| 196 | return 0; |
| 197 | } |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 198 | actual_ctx = exp->v.Name.ctx; |
| 199 | break; |
| 200 | case List_kind: |
| 201 | actual_ctx = exp->v.List.ctx; |
| 202 | break; |
| 203 | case Tuple_kind: |
| 204 | actual_ctx = exp->v.Tuple.ctx; |
| 205 | break; |
| 206 | default: |
| 207 | if (ctx != Load) { |
| 208 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 209 | "assigned to in %s context", expr_context_name(ctx)); |
| 210 | return 0; |
| 211 | } |
| 212 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 213 | /* set actual_ctx to prevent gcc warning */ |
| 214 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 215 | } |
| 216 | if (check_ctx && actual_ctx != ctx) { |
| 217 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 218 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Now validate expression. */ |
| 223 | switch (exp->kind) { |
| 224 | case BoolOp_kind: |
| 225 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 226 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 227 | return 0; |
| 228 | } |
| 229 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 230 | case BinOp_kind: |
| 231 | return validate_expr(exp->v.BinOp.left, Load) && |
| 232 | validate_expr(exp->v.BinOp.right, Load); |
| 233 | case UnaryOp_kind: |
| 234 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 235 | case Lambda_kind: |
| 236 | return validate_arguments(exp->v.Lambda.args) && |
| 237 | validate_expr(exp->v.Lambda.body, Load); |
| 238 | case IfExp_kind: |
| 239 | return validate_expr(exp->v.IfExp.test, Load) && |
| 240 | validate_expr(exp->v.IfExp.body, Load) && |
| 241 | validate_expr(exp->v.IfExp.orelse, Load); |
| 242 | case Dict_kind: |
| 243 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 244 | PyErr_SetString(PyExc_ValueError, |
| 245 | "Dict doesn't have the same number of keys as values"); |
| 246 | return 0; |
| 247 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 248 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 249 | dict literals, i.e. ``{**{a:b}}`` */ |
| 250 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 251 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 252 | case Set_kind: |
| 253 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 254 | #define COMP(NAME) \ |
| 255 | case NAME ## _kind: \ |
| 256 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 257 | validate_expr(exp->v.NAME.elt, Load); |
| 258 | COMP(ListComp) |
| 259 | COMP(SetComp) |
| 260 | COMP(GeneratorExp) |
| 261 | #undef COMP |
| 262 | case DictComp_kind: |
| 263 | return validate_comprehension(exp->v.DictComp.generators) && |
| 264 | validate_expr(exp->v.DictComp.key, Load) && |
| 265 | validate_expr(exp->v.DictComp.value, Load); |
| 266 | case Yield_kind: |
| 267 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 268 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 269 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 270 | case Await_kind: |
| 271 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 272 | case Compare_kind: |
| 273 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 274 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 275 | return 0; |
| 276 | } |
| 277 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 278 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 279 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 280 | "of comparators and operands"); |
| 281 | return 0; |
| 282 | } |
| 283 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 284 | validate_expr(exp->v.Compare.left, Load); |
| 285 | case Call_kind: |
| 286 | return validate_expr(exp->v.Call.func, Load) && |
| 287 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 288 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 289 | case Constant_kind: |
| 290 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 294 | case JoinedStr_kind: |
| 295 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 296 | case FormattedValue_kind: |
| 297 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 298 | return 0; |
| 299 | if (exp->v.FormattedValue.format_spec) |
| 300 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 301 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 302 | case Attribute_kind: |
| 303 | return validate_expr(exp->v.Attribute.value, Load); |
| 304 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 305 | return validate_expr(exp->v.Subscript.slice, Load) && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 306 | validate_expr(exp->v.Subscript.value, Load); |
| 307 | case Starred_kind: |
| 308 | return validate_expr(exp->v.Starred.value, ctx); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 309 | case Slice_kind: |
| 310 | return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) && |
| 311 | (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) && |
| 312 | (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 313 | case List_kind: |
| 314 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 315 | case Tuple_kind: |
| 316 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 317 | case NamedExpr_kind: |
| 318 | return validate_expr(exp->v.NamedExpr.value, Load); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 319 | /* This last case doesn't have any checking. */ |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 320 | case Name_kind: |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 321 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 322 | } |
Pablo Galindo | 0c9258a | 2019-03-18 13:51:53 +0000 | [diff] [blame] | 323 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 324 | return 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static int |
| 328 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 329 | { |
| 330 | if (asdl_seq_LEN(seq)) |
| 331 | return 1; |
| 332 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int |
| 337 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 338 | { |
| 339 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 340 | validate_exprs(targets, ctx, 0); |
| 341 | } |
| 342 | |
| 343 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 344 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 345 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 346 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static int |
| 350 | validate_stmt(stmt_ty stmt) |
| 351 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 352 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 353 | switch (stmt->kind) { |
| 354 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 355 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 356 | validate_arguments(stmt->v.FunctionDef.args) && |
| 357 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 358 | (!stmt->v.FunctionDef.returns || |
| 359 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 360 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 361 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 362 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 363 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 364 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 365 | case Return_kind: |
| 366 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 367 | case Delete_kind: |
| 368 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 369 | case Assign_kind: |
| 370 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 371 | validate_expr(stmt->v.Assign.value, Load); |
| 372 | case AugAssign_kind: |
| 373 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 374 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 375 | case AnnAssign_kind: |
| 376 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 377 | stmt->v.AnnAssign.simple) { |
| 378 | PyErr_SetString(PyExc_TypeError, |
| 379 | "AnnAssign with simple non-Name target"); |
| 380 | return 0; |
| 381 | } |
| 382 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 383 | (!stmt->v.AnnAssign.value || |
| 384 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 385 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 386 | case For_kind: |
| 387 | return validate_expr(stmt->v.For.target, Store) && |
| 388 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 389 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 390 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 391 | case AsyncFor_kind: |
| 392 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 393 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 394 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 395 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 396 | case While_kind: |
| 397 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 398 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 399 | validate_stmts(stmt->v.While.orelse); |
| 400 | case If_kind: |
| 401 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 402 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 403 | validate_stmts(stmt->v.If.orelse); |
| 404 | case With_kind: |
| 405 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 406 | return 0; |
| 407 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 408 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 409 | if (!validate_expr(item->context_expr, Load) || |
| 410 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 411 | return 0; |
| 412 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 413 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 414 | case AsyncWith_kind: |
| 415 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 416 | return 0; |
| 417 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 418 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 419 | if (!validate_expr(item->context_expr, Load) || |
| 420 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 421 | return 0; |
| 422 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 423 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 424 | case Raise_kind: |
| 425 | if (stmt->v.Raise.exc) { |
| 426 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 427 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 428 | } |
| 429 | if (stmt->v.Raise.cause) { |
| 430 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 431 | return 0; |
| 432 | } |
| 433 | return 1; |
| 434 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 435 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 436 | return 0; |
| 437 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 438 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 439 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 440 | return 0; |
| 441 | } |
| 442 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 443 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 444 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 445 | return 0; |
| 446 | } |
| 447 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 448 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 449 | if ((handler->v.ExceptHandler.type && |
| 450 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 451 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 455 | validate_stmts(stmt->v.Try.finalbody)) && |
| 456 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 457 | validate_stmts(stmt->v.Try.orelse)); |
| 458 | case Assert_kind: |
| 459 | return validate_expr(stmt->v.Assert.test, Load) && |
| 460 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 461 | case Import_kind: |
| 462 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 463 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 464 | if (stmt->v.ImportFrom.level < 0) { |
| 465 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 469 | case Global_kind: |
| 470 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 471 | case Nonlocal_kind: |
| 472 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 473 | case Expr_kind: |
| 474 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 475 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 476 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 477 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 478 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 479 | (!stmt->v.AsyncFunctionDef.returns || |
| 480 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 481 | case Pass_kind: |
| 482 | case Break_kind: |
| 483 | case Continue_kind: |
| 484 | return 1; |
| 485 | default: |
| 486 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 487 | return 0; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static int |
| 492 | validate_stmts(asdl_seq *seq) |
| 493 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 494 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 495 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 496 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 497 | if (stmt) { |
| 498 | if (!validate_stmt(stmt)) |
| 499 | return 0; |
| 500 | } |
| 501 | else { |
| 502 | PyErr_SetString(PyExc_ValueError, |
| 503 | "None disallowed in statement list"); |
| 504 | return 0; |
| 505 | } |
| 506 | } |
| 507 | return 1; |
| 508 | } |
| 509 | |
| 510 | static int |
| 511 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 512 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 513 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 514 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 515 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 516 | if (expr) { |
| 517 | if (!validate_expr(expr, ctx)) |
| 518 | return 0; |
| 519 | } |
| 520 | else if (!null_ok) { |
| 521 | PyErr_SetString(PyExc_ValueError, |
| 522 | "None disallowed in expression list"); |
| 523 | return 0; |
| 524 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 525 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 526 | } |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | int |
| 531 | PyAST_Validate(mod_ty mod) |
| 532 | { |
| 533 | int res = 0; |
| 534 | |
| 535 | switch (mod->kind) { |
| 536 | case Module_kind: |
| 537 | res = validate_stmts(mod->v.Module.body); |
| 538 | break; |
| 539 | case Interactive_kind: |
| 540 | res = validate_stmts(mod->v.Interactive.body); |
| 541 | break; |
| 542 | case Expression_kind: |
| 543 | res = validate_expr(mod->v.Expression.body, Load); |
| 544 | break; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 545 | default: |
| 546 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 547 | res = 0; |
| 548 | break; |
| 549 | } |
| 550 | return res; |
| 551 | } |
| 552 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 553 | /* 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] | 554 | #include "grammar.h" |
| 555 | #include "parsetok.h" |
| 556 | #include "graminit.h" |
| 557 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 558 | /* Data structure used internally */ |
| 559 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 560 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 561 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 562 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 563 | int c_feature_version; /* Latest minor version of Python for allowed features */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 567 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 568 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 569 | 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] | 570 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 571 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 572 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 573 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 574 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 575 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 576 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 577 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 578 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 579 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 580 | const node *, const node *, const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 581 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 582 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 583 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 584 | static void get_last_end_pos(asdl_seq *, int *, int *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 585 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 586 | #define COMP_GENEXP 0 |
| 587 | #define COMP_LISTCOMP 1 |
| 588 | #define COMP_SETCOMP 2 |
| 589 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 590 | static int |
| 591 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 592 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 593 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 594 | if (!m) |
| 595 | return 0; |
| 596 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 597 | Py_DECREF(m); |
| 598 | if (!c->c_normalize) |
| 599 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 604 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 605 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 606 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 607 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 608 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 609 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 610 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 611 | /* Check whether there are non-ASCII characters in the |
| 612 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 613 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 614 | PyObject *id2; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 615 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 616 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 617 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 618 | } |
Dino Viehland | 5b172c2 | 2019-09-11 08:47:17 -0700 | [diff] [blame] | 619 | PyObject *form = PyUnicode_InternFromString("NFKC"); |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 620 | if (form == NULL) { |
| 621 | Py_DECREF(id); |
| 622 | return NULL; |
| 623 | } |
| 624 | PyObject *args[2] = {form, id}; |
| 625 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 626 | Py_DECREF(id); |
Dino Viehland | 8d88e8c | 2019-09-12 15:38:13 +0100 | [diff] [blame] | 627 | Py_DECREF(form); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 628 | if (!id2) |
| 629 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 630 | if (!PyUnicode_Check(id2)) { |
| 631 | PyErr_Format(PyExc_TypeError, |
| 632 | "unicodedata.normalize() must return a string, not " |
| 633 | "%.200s", |
Dino Viehland | 5b172c2 | 2019-09-11 08:47:17 -0700 | [diff] [blame] | 634 | _PyType_Name(Py_TYPE(id2))); |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 635 | Py_DECREF(id2); |
| 636 | return NULL; |
| 637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 638 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 639 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 640 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 641 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 642 | Py_DECREF(id); |
| 643 | return NULL; |
| 644 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 645 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 648 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 649 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 650 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 651 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 653 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 654 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 655 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 656 | va_start(va, errmsg); |
| 657 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 658 | va_end(va); |
| 659 | if (!errstr) { |
| 660 | return 0; |
| 661 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 662 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 663 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 664 | Py_INCREF(Py_None); |
| 665 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 666 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 667 | 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] | 668 | if (!tmp) { |
| 669 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 670 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 671 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 672 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 673 | Py_DECREF(errstr); |
| 674 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 675 | if (value) { |
| 676 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 677 | Py_DECREF(value); |
| 678 | } |
| 679 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* num_stmts() returns number of contained statements. |
| 683 | |
| 684 | Use this routine to determine how big a sequence is needed for |
| 685 | the statements in a parse tree. Its raison d'etre is this bit of |
| 686 | grammar: |
| 687 | |
| 688 | stmt: simple_stmt | compound_stmt |
| 689 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 690 | |
| 691 | A simple_stmt can contain multiple small_stmt elements joined |
| 692 | by semicolons. If the arg is a simple_stmt, the number of |
| 693 | small_stmt elements is returned. |
| 694 | */ |
| 695 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 696 | static string |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 697 | new_type_comment(const char *s, struct compiling *c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 698 | { |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 699 | PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); |
Guido van Rossum | 4b250fc | 2019-02-11 08:10:42 -0800 | [diff] [blame] | 700 | if (res == NULL) |
| 701 | return NULL; |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 702 | if (PyArena_AddPyObject(c->c_arena, res) < 0) { |
| 703 | Py_DECREF(res); |
| 704 | return NULL; |
| 705 | } |
| 706 | return res; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 707 | } |
Guido van Rossum | d2b4c19 | 2019-02-01 15:28:13 -0800 | [diff] [blame] | 708 | #define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 709 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 710 | static int |
| 711 | num_stmts(const node *n) |
| 712 | { |
| 713 | int i, l; |
| 714 | node *ch; |
| 715 | |
| 716 | switch (TYPE(n)) { |
| 717 | case single_input: |
| 718 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 719 | return 0; |
| 720 | else |
| 721 | return num_stmts(CHILD(n, 0)); |
| 722 | case file_input: |
| 723 | l = 0; |
| 724 | for (i = 0; i < NCH(n); i++) { |
| 725 | ch = CHILD(n, i); |
| 726 | if (TYPE(ch) == stmt) |
| 727 | l += num_stmts(ch); |
| 728 | } |
| 729 | return l; |
| 730 | case stmt: |
| 731 | return num_stmts(CHILD(n, 0)); |
| 732 | case compound_stmt: |
| 733 | return 1; |
| 734 | case simple_stmt: |
| 735 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 736 | case suite: |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 737 | case func_body_suite: |
| 738 | /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
| 739 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 740 | if (NCH(n) == 1) |
| 741 | return num_stmts(CHILD(n, 0)); |
| 742 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 743 | i = 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 744 | l = 0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 745 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) |
| 746 | i += 2; |
| 747 | for (; i < (NCH(n) - 1); i++) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 748 | l += num_stmts(CHILD(n, i)); |
| 749 | return l; |
| 750 | } |
| 751 | default: { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 752 | _Py_FatalErrorFormat(__func__, "Non-statement found: %d %d", |
| 753 | TYPE(n), NCH(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 756 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* Transform the CST rooted at node * to the appropriate AST |
| 760 | */ |
| 761 | |
| 762 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 763 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 764 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 766 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | asdl_seq *stmts = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 768 | asdl_seq *type_ignores = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 769 | stmt_ty s; |
| 770 | node *ch; |
| 771 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 772 | mod_ty res = NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 773 | asdl_seq *argtypes = NULL; |
| 774 | expr_ty ret, arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 775 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 776 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 777 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 778 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 779 | c.c_normalize = NULL; |
Guido van Rossum | 2a1ee1d | 2020-06-27 17:34:30 -0700 | [diff] [blame^] | 780 | c.c_feature_version = flags && (flags->cf_flags & PyCF_ONLY_AST) ? |
| 781 | flags->cf_feature_version : PY_MINOR_VERSION; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 782 | |
| 783 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 785 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 786 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 787 | switch (TYPE(n)) { |
| 788 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 789 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 791 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | for (i = 0; i < NCH(n) - 1; i++) { |
| 793 | ch = CHILD(n, i); |
| 794 | if (TYPE(ch) == NEWLINE) |
| 795 | continue; |
| 796 | REQ(ch, stmt); |
| 797 | num = num_stmts(ch); |
| 798 | if (num == 1) { |
| 799 | s = ast_for_stmt(&c, ch); |
| 800 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 801 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 802 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | } |
| 804 | else { |
| 805 | ch = CHILD(ch, 0); |
| 806 | REQ(ch, simple_stmt); |
| 807 | for (j = 0; j < num; j++) { |
| 808 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 809 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 810 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 811 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 815 | |
| 816 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 817 | ch = CHILD(n, NCH(n) - 1); |
| 818 | REQ(ch, ENDMARKER); |
| 819 | num = NCH(ch); |
| 820 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 821 | if (!type_ignores) |
| 822 | goto out; |
| 823 | |
| 824 | for (i = 0; i < num; i++) { |
Michael J. Sullivan | 933e150 | 2019-05-22 07:54:20 -0700 | [diff] [blame] | 825 | string type_comment = new_type_comment(STR(CHILD(ch, i)), &c); |
| 826 | if (!type_comment) |
| 827 | goto out; |
| 828 | type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 829 | if (!ti) |
| 830 | goto out; |
| 831 | asdl_seq_SET(type_ignores, i, ti); |
| 832 | } |
| 833 | |
| 834 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 835 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | case eval_input: { |
| 837 | expr_ty testlist_ast; |
| 838 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 839 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 840 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 841 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 842 | goto out; |
| 843 | res = Expression(testlist_ast, arena); |
| 844 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 845 | } |
| 846 | case single_input: |
| 847 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 848 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 850 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 851 | 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] | 852 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 853 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 854 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 855 | goto out; |
| 856 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 857 | } |
| 858 | else { |
| 859 | n = CHILD(n, 0); |
| 860 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 861 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 862 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 863 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 864 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 865 | s = ast_for_stmt(&c, n); |
| 866 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 867 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | asdl_seq_SET(stmts, 0, s); |
| 869 | } |
| 870 | else { |
| 871 | /* Only a simple_stmt can contain multiple statements. */ |
| 872 | REQ(n, simple_stmt); |
| 873 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 874 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 875 | break; |
| 876 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 877 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 878 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 879 | asdl_seq_SET(stmts, i / 2, s); |
| 880 | } |
| 881 | } |
| 882 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 883 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 884 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 885 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 886 | case func_type_input: |
| 887 | n = CHILD(n, 0); |
| 888 | REQ(n, func_type); |
| 889 | |
| 890 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 891 | ch = CHILD(n, 1); |
| 892 | /* this is overly permissive -- we don't pay any attention to |
| 893 | * stars on the args -- just parse them into an ordered list */ |
| 894 | num = 0; |
| 895 | for (i = 0; i < NCH(ch); i++) { |
| 896 | if (TYPE(CHILD(ch, i)) == test) { |
| 897 | num++; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | argtypes = _Py_asdl_seq_new(num, arena); |
| 902 | if (!argtypes) |
| 903 | goto out; |
| 904 | |
| 905 | j = 0; |
| 906 | for (i = 0; i < NCH(ch); i++) { |
| 907 | if (TYPE(CHILD(ch, i)) == test) { |
| 908 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 909 | if (!arg) |
| 910 | goto out; |
| 911 | asdl_seq_SET(argtypes, j++, arg); |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | else { |
| 916 | argtypes = _Py_asdl_seq_new(0, arena); |
| 917 | if (!argtypes) |
| 918 | goto out; |
| 919 | } |
| 920 | |
| 921 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 922 | if (!ret) |
| 923 | goto out; |
| 924 | res = FunctionType(argtypes, ret, arena); |
| 925 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 926 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 927 | PyErr_Format(PyExc_SystemError, |
| 928 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 929 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 930 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 931 | out: |
| 932 | if (c.c_normalize) { |
| 933 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 934 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 935 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 938 | mod_ty |
| 939 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 940 | PyArena *arena) |
| 941 | { |
| 942 | mod_ty mod; |
| 943 | PyObject *filename; |
| 944 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 945 | if (filename == NULL) |
| 946 | return NULL; |
| 947 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 948 | Py_DECREF(filename); |
| 949 | return mod; |
| 950 | |
| 951 | } |
| 952 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 953 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 954 | */ |
| 955 | |
| 956 | static operator_ty |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 957 | get_operator(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 958 | { |
| 959 | switch (TYPE(n)) { |
| 960 | case VBAR: |
| 961 | return BitOr; |
| 962 | case CIRCUMFLEX: |
| 963 | return BitXor; |
| 964 | case AMPER: |
| 965 | return BitAnd; |
| 966 | case LEFTSHIFT: |
| 967 | return LShift; |
| 968 | case RIGHTSHIFT: |
| 969 | return RShift; |
| 970 | case PLUS: |
| 971 | return Add; |
| 972 | case MINUS: |
| 973 | return Sub; |
| 974 | case STAR: |
| 975 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 976 | case AT: |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 977 | if (c->c_feature_version < 5) { |
| 978 | ast_error(c, n, |
| 979 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 980 | return (operator_ty)0; |
| 981 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 982 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 983 | case SLASH: |
| 984 | return Div; |
| 985 | case DOUBLESLASH: |
| 986 | return FloorDiv; |
| 987 | case PERCENT: |
| 988 | return Mod; |
| 989 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 990 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 994 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 995 | "None", |
| 996 | "True", |
| 997 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 998 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 999 | NULL, |
| 1000 | }; |
| 1001 | |
| 1002 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1003 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1004 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1005 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1006 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1007 | const char * const *p = FORBIDDEN; |
| 1008 | if (!full_checks) { |
| 1009 | /* In most cases, the parser will protect True, False, and None |
| 1010 | from being assign to. */ |
| 1011 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1012 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1013 | for (; *p; p++) { |
| 1014 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1015 | ast_error(c, n, "cannot assign to %U", name); |
| 1016 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1022 | static expr_ty |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 1023 | copy_location(expr_ty e, const node *n, const node *end) |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1024 | { |
| 1025 | if (e) { |
| 1026 | e->lineno = LINENO(n); |
| 1027 | e->col_offset = n->n_col_offset; |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 1028 | e->end_lineno = end->n_end_lineno; |
| 1029 | e->end_col_offset = end->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1030 | } |
| 1031 | return e; |
| 1032 | } |
| 1033 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1034 | static const char * |
| 1035 | get_expr_name(expr_ty e) |
| 1036 | { |
| 1037 | switch (e->kind) { |
| 1038 | case Attribute_kind: |
| 1039 | return "attribute"; |
| 1040 | case Subscript_kind: |
| 1041 | return "subscript"; |
| 1042 | case Starred_kind: |
| 1043 | return "starred"; |
| 1044 | case Name_kind: |
| 1045 | return "name"; |
| 1046 | case List_kind: |
| 1047 | return "list"; |
| 1048 | case Tuple_kind: |
| 1049 | return "tuple"; |
| 1050 | case Lambda_kind: |
| 1051 | return "lambda"; |
| 1052 | case Call_kind: |
| 1053 | return "function call"; |
| 1054 | case BoolOp_kind: |
| 1055 | case BinOp_kind: |
| 1056 | case UnaryOp_kind: |
| 1057 | return "operator"; |
| 1058 | case GeneratorExp_kind: |
| 1059 | return "generator expression"; |
| 1060 | case Yield_kind: |
| 1061 | case YieldFrom_kind: |
| 1062 | return "yield expression"; |
| 1063 | case Await_kind: |
| 1064 | return "await expression"; |
| 1065 | case ListComp_kind: |
| 1066 | return "list comprehension"; |
| 1067 | case SetComp_kind: |
| 1068 | return "set comprehension"; |
| 1069 | case DictComp_kind: |
| 1070 | return "dict comprehension"; |
| 1071 | case Dict_kind: |
| 1072 | return "dict display"; |
| 1073 | case Set_kind: |
| 1074 | return "set display"; |
| 1075 | case JoinedStr_kind: |
| 1076 | case FormattedValue_kind: |
| 1077 | return "f-string expression"; |
| 1078 | case Constant_kind: { |
| 1079 | PyObject *value = e->v.Constant.value; |
| 1080 | if (value == Py_None) { |
| 1081 | return "None"; |
| 1082 | } |
| 1083 | if (value == Py_False) { |
| 1084 | return "False"; |
| 1085 | } |
| 1086 | if (value == Py_True) { |
| 1087 | return "True"; |
| 1088 | } |
| 1089 | if (value == Py_Ellipsis) { |
| 1090 | return "Ellipsis"; |
| 1091 | } |
| 1092 | return "literal"; |
| 1093 | } |
| 1094 | case Compare_kind: |
| 1095 | return "comparison"; |
| 1096 | case IfExp_kind: |
| 1097 | return "conditional expression"; |
| 1098 | case NamedExpr_kind: |
| 1099 | return "named expression"; |
| 1100 | default: |
| 1101 | PyErr_Format(PyExc_SystemError, |
| 1102 | "unexpected expression in assignment %d (line %d)", |
| 1103 | e->kind, e->lineno); |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | } |
| 1107 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1108 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1109 | |
| 1110 | Only sets context for expr kinds that "can appear in assignment context" |
| 1111 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1112 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1113 | */ |
| 1114 | |
| 1115 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1116 | 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] | 1117 | { |
| 1118 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1119 | |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 1120 | /* Expressions in an augmented assignment have a Store context. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1121 | |
| 1122 | switch (e->kind) { |
| 1123 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1124 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1125 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1126 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1127 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1128 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1129 | e->v.Subscript.ctx = ctx; |
| 1130 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1131 | case Starred_kind: |
| 1132 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1133 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1134 | return 0; |
| 1135 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1136 | case Name_kind: |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1137 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1138 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1139 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1140 | } |
| 1141 | e->v.Name.ctx = ctx; |
| 1142 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1144 | e->v.List.ctx = ctx; |
| 1145 | s = e->v.List.elts; |
| 1146 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1147 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1148 | e->v.Tuple.ctx = ctx; |
| 1149 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1150 | break; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1151 | default: { |
| 1152 | const char *expr_name = get_expr_name(e); |
| 1153 | if (expr_name != NULL) { |
| 1154 | ast_error(c, n, "cannot %s %s", |
| 1155 | ctx == Store ? "assign to" : "delete", |
| 1156 | expr_name); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1157 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1158 | return 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1159 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | /* 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] | 1163 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | */ |
| 1165 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1166 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1167 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1168 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1169 | 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] | 1170 | return 0; |
| 1171 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1172 | } |
| 1173 | return 1; |
| 1174 | } |
| 1175 | |
| 1176 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1177 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | { |
| 1179 | REQ(n, augassign); |
| 1180 | n = CHILD(n, 0); |
| 1181 | switch (STR(n)[0]) { |
| 1182 | case '+': |
| 1183 | return Add; |
| 1184 | case '-': |
| 1185 | return Sub; |
| 1186 | case '/': |
| 1187 | if (STR(n)[1] == '/') |
| 1188 | return FloorDiv; |
| 1189 | else |
| 1190 | return Div; |
| 1191 | case '%': |
| 1192 | return Mod; |
| 1193 | case '<': |
| 1194 | return LShift; |
| 1195 | case '>': |
| 1196 | return RShift; |
| 1197 | case '&': |
| 1198 | return BitAnd; |
| 1199 | case '^': |
| 1200 | return BitXor; |
| 1201 | case '|': |
| 1202 | return BitOr; |
| 1203 | case '*': |
| 1204 | if (STR(n)[1] == '*') |
| 1205 | return Pow; |
| 1206 | else |
| 1207 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1208 | case '@': |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1209 | if (c->c_feature_version < 5) { |
| 1210 | ast_error(c, n, |
| 1211 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 1212 | return (operator_ty)0; |
| 1213 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1214 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1216 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1217 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1222 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1223 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1224 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1225 | |'is' 'not' |
| 1226 | */ |
| 1227 | REQ(n, comp_op); |
| 1228 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1229 | n = CHILD(n, 0); |
| 1230 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1231 | case LESS: |
| 1232 | return Lt; |
| 1233 | case GREATER: |
| 1234 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1235 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1236 | return Eq; |
| 1237 | case LESSEQUAL: |
| 1238 | return LtE; |
| 1239 | case GREATEREQUAL: |
| 1240 | return GtE; |
| 1241 | case NOTEQUAL: |
| 1242 | return NotEq; |
| 1243 | case NAME: |
| 1244 | if (strcmp(STR(n), "in") == 0) |
| 1245 | return In; |
| 1246 | if (strcmp(STR(n), "is") == 0) |
| 1247 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1248 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1249 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1250 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1251 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1252 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1253 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1254 | } |
| 1255 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1256 | /* handle "not in" and "is not" */ |
| 1257 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1258 | case NAME: |
| 1259 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1260 | return NotIn; |
| 1261 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1262 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1263 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1264 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1265 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1266 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1267 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1268 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1269 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1270 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1271 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1272 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | static asdl_seq * |
| 1276 | seq_for_testlist(struct compiling *c, const node *n) |
| 1277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1279 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1280 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1281 | asdl_seq *seq; |
| 1282 | expr_ty expression; |
| 1283 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1284 | 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] | 1285 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1286 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1287 | if (!seq) |
| 1288 | return NULL; |
| 1289 | |
| 1290 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1292 | 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] | 1293 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1294 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1295 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1296 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1297 | |
| 1298 | assert(i / 2 < seq->size); |
| 1299 | asdl_seq_SET(seq, i / 2, expression); |
| 1300 | } |
| 1301 | return seq; |
| 1302 | } |
| 1303 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1304 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1305 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1306 | { |
| 1307 | identifier name; |
| 1308 | expr_ty annotation = NULL; |
| 1309 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1310 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1311 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1312 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1313 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1314 | name = NEW_IDENTIFIER(ch); |
| 1315 | if (!name) |
| 1316 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1317 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1318 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1321 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1322 | if (!annotation) |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1326 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1327 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1328 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1329 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1330 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1333 | /* returns -1 if failed to handle keyword only arguments |
| 1334 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1335 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1336 | ^^^ |
| 1337 | start pointing here |
| 1338 | */ |
| 1339 | static int |
| 1340 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1341 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1342 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1343 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1344 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1345 | expr_ty expression, annotation; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1346 | arg_ty arg = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1347 | int i = start; |
| 1348 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1351 | 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] | 1352 | return -1; |
| 1353 | } |
| 1354 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1355 | while (i < NCH(n)) { |
| 1356 | ch = CHILD(n, i); |
| 1357 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1358 | case vfpdef: |
| 1359 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1360 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1361 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1362 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1363 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1364 | asdl_seq_SET(kwdefaults, j, expression); |
| 1365 | i += 2; /* '=' and test */ |
| 1366 | } |
| 1367 | else { /* setting NULL if no default value exists */ |
| 1368 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1369 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1370 | if (NCH(ch) == 3) { |
| 1371 | /* ch is NAME ':' test */ |
| 1372 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1373 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1374 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1375 | } |
| 1376 | else { |
| 1377 | annotation = NULL; |
| 1378 | } |
| 1379 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1380 | argname = NEW_IDENTIFIER(ch); |
| 1381 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1382 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1383 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1384 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1385 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1386 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1387 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1388 | if (!arg) |
| 1389 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1390 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1391 | i += 1; /* the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1392 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1393 | i += 1; /* the comma, if present */ |
| 1394 | break; |
| 1395 | case TYPE_COMMENT: |
| 1396 | /* arg will be equal to the last argument processed */ |
| 1397 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1398 | if (!arg->type_comment) |
| 1399 | goto error; |
| 1400 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1401 | break; |
| 1402 | case DOUBLESTAR: |
| 1403 | return i; |
| 1404 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1405 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1406 | goto error; |
| 1407 | } |
| 1408 | } |
| 1409 | return i; |
| 1410 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1412 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1413 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1414 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1415 | |
| 1416 | static arguments_ty |
| 1417 | ast_for_arguments(struct compiling *c, const node *n) |
| 1418 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1419 | /* This function handles both typedargslist (function definition) |
| 1420 | and varargslist (lambda definition). |
| 1421 | |
| 1422 | parameters: '(' [typedargslist] ')' |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1423 | |
| 1424 | The following definition for typedarglist is equivalent to this set of rules: |
| 1425 | |
| 1426 | arguments = argument (',' [TYPE_COMMENT] argument)* |
| 1427 | argument = tfpdef ['=' test] |
| 1428 | kwargs = '**' tfpdef [','] [TYPE_COMMENT] |
| 1429 | args = '*' [tfpdef] |
| 1430 | kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' |
| 1431 | [TYPE_COMMENT] [kwargs]]) |
| 1432 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1433 | poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' |
| 1434 | [TYPE_COMMENT] [args_kwonly_kwargs]]) |
| 1435 | typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1436 | typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] |
| 1437 | typedargslist_no_posonly]])|(typedargslist_no_posonly)" |
| 1438 | |
| 1439 | typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1440 | ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ',' |
| 1441 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1442 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1443 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1444 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1445 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1446 | '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (',' |
| 1447 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1448 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1449 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1450 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1451 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1452 | '**' tfpdef [','] [TYPE_COMMENT])) |
| 1453 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1454 | tfpdef: NAME [':' test] |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1455 | |
| 1456 | The following definition for varargslist is equivalent to this set of rules: |
| 1457 | |
| 1458 | arguments = argument (',' argument )* |
| 1459 | argument = vfpdef ['=' test] |
| 1460 | kwargs = '**' vfpdef [','] |
| 1461 | args = '*' [vfpdef] |
| 1462 | kwonly_kwargs = (',' argument )* [',' [kwargs]] |
| 1463 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1464 | poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 1465 | vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1466 | varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | |
| 1467 | (vararglist_no_posonly) |
| 1468 | |
| 1469 | varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' |
| 1470 | test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' |
| 1471 | ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* |
| 1472 | [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef |
| 1473 | ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1474 | | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef |
| 1475 | [',']]] | '**' vfpdef [',']) |
| 1476 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1477 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1478 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1479 | */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1480 | int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1481 | int nposdefaults = 0, found_default = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1482 | asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1483 | arg_ty vararg = NULL, kwarg = NULL; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1484 | arg_ty arg = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1485 | node *ch; |
| 1486 | |
| 1487 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1488 | if (NCH(n) == 2) /* () as argument list */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1489 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1490 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1491 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1492 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1494 | /* First count the number of positional args & defaults. The |
| 1495 | variable i is the loop index for this for loop and the next. |
| 1496 | The next loop picks up where the first leaves off. |
| 1497 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1499 | ch = CHILD(n, i); |
| 1500 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1501 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1502 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1503 | if (i < NCH(n) && /* skip argument following star */ |
| 1504 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1505 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1506 | i++; |
| 1507 | } |
| 1508 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1509 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1510 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1511 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1512 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1513 | if (TYPE(ch) == SLASH ) { |
| 1514 | nposonlyargs = nposargs; |
| 1515 | nposargs = 0; |
| 1516 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1517 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1519 | defaults for keyword only args */ |
| 1520 | for ( ; i < NCH(n); ++i) { |
| 1521 | ch = CHILD(n, i); |
| 1522 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1523 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1524 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1525 | posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL); |
| 1526 | if (!posonlyargs && nposonlyargs) { |
| 1527 | return NULL; |
| 1528 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1529 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1530 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1531 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1532 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1533 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1534 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1535 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1537 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1538 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1539 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1541 | since we set NULL as default for keyword only argument w/o default |
| 1542 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1543 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1544 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1545 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1546 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1547 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1548 | /* tfpdef: NAME [':' test] |
| 1549 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1550 | */ |
| 1551 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1552 | j = 0; /* index for defaults */ |
| 1553 | k = 0; /* index for args */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1554 | l = 0; /* index for posonlyargs */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1555 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1556 | ch = CHILD(n, i); |
| 1557 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1558 | case tfpdef: |
| 1559 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1560 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1561 | anything other than EQUAL or a comma? */ |
| 1562 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1563 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1564 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1565 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1566 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1567 | assert(posdefaults != NULL); |
| 1568 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1569 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1570 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1571 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1572 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1573 | ast_error(c, n, |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1574 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1575 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1576 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1577 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1578 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1579 | return NULL; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1580 | if (l < nposonlyargs) { |
| 1581 | asdl_seq_SET(posonlyargs, l++, arg); |
| 1582 | } else { |
| 1583 | asdl_seq_SET(posargs, k++, arg); |
| 1584 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1585 | i += 1; /* the name */ |
| 1586 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1587 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1588 | break; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1589 | case SLASH: |
| 1590 | /* Advance the slash and the comma. If there are more names |
| 1591 | * after the slash there will be a comma so we are advancing |
| 1592 | * the correct number of nodes. If the slash is the last item, |
| 1593 | * we will be advancing an extra token but then * i > NCH(n) |
| 1594 | * and the enclosing while will finish correctly. */ |
| 1595 | i += 2; |
| 1596 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1597 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1598 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1599 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1600 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1601 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1602 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1603 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1604 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1605 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1606 | if (TYPE(ch) == COMMA) { |
| 1607 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1608 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1609 | |
| 1610 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1611 | ast_error(c, CHILD(n, i), |
| 1612 | "bare * has associated type comment"); |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1616 | res = handle_keywordonly_args(c, n, i, |
| 1617 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1618 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1619 | i = res; /* res has new position to process */ |
| 1620 | } |
| 1621 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1622 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1623 | if (!vararg) |
| 1624 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1625 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1626 | i += 2; /* the star and the name */ |
| 1627 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1628 | i += 1; /* the comma, if present */ |
| 1629 | |
| 1630 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1631 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1632 | if (!vararg->type_comment) |
| 1633 | return NULL; |
| 1634 | i += 1; |
| 1635 | } |
| 1636 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1637 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1638 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1639 | int res = 0; |
| 1640 | res = handle_keywordonly_args(c, n, i, |
| 1641 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1642 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1643 | i = res; /* res has new position to process */ |
| 1644 | } |
| 1645 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1646 | break; |
| 1647 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1648 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1649 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1650 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1651 | if (!kwarg) |
| 1652 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1653 | i += 2; /* the double star and the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1654 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1655 | i += 1; /* the comma, if present */ |
| 1656 | break; |
| 1657 | case TYPE_COMMENT: |
| 1658 | assert(i); |
| 1659 | |
| 1660 | if (kwarg) |
| 1661 | arg = kwarg; |
| 1662 | |
| 1663 | /* arg will be equal to the last argument processed */ |
| 1664 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1665 | if (!arg->type_comment) |
| 1666 | return NULL; |
| 1667 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1668 | break; |
| 1669 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1670 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1671 | "unexpected node in varargslist: %d @ %d", |
| 1672 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1673 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1674 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1675 | } |
Pablo Galindo | cd6e83b | 2019-07-15 01:32:18 +0200 | [diff] [blame] | 1676 | return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1680 | ast_for_decorator(struct compiling *c, const node *n) |
| 1681 | { |
Brandt Bucher | be501ca | 2020-03-03 14:25:44 -0800 | [diff] [blame] | 1682 | /* decorator: '@' namedexpr_test NEWLINE */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1684 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1685 | REQ(CHILD(n, 0), AT); |
Brandt Bucher | be501ca | 2020-03-03 14:25:44 -0800 | [diff] [blame] | 1686 | REQ(CHILD(n, 2), NEWLINE); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1687 | |
Brandt Bucher | be501ca | 2020-03-03 14:25:44 -0800 | [diff] [blame] | 1688 | return ast_for_expr(c, CHILD(n, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | static asdl_seq* |
| 1692 | ast_for_decorators(struct compiling *c, const node *n) |
| 1693 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1694 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1695 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1696 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1698 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1699 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1700 | if (!decorator_seq) |
| 1701 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1703 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1704 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1705 | if (!d) |
| 1706 | return NULL; |
| 1707 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | } |
| 1709 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1713 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1714 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1715 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1716 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1717 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1718 | identifier name; |
| 1719 | arguments_ty args; |
| 1720 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1721 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1722 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1723 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1724 | node *tc; |
| 1725 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1726 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1727 | if (is_async && c->c_feature_version < 5) { |
| 1728 | ast_error(c, n, |
| 1729 | "Async functions are only supported in Python 3.5 and greater"); |
| 1730 | return NULL; |
| 1731 | } |
| 1732 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1733 | REQ(n, funcdef); |
| 1734 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1735 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1736 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1737 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1738 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1739 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1740 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1741 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1742 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1743 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1744 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1745 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1746 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1747 | name_i += 2; |
| 1748 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1749 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1750 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1751 | if (!type_comment) |
| 1752 | return NULL; |
| 1753 | name_i += 1; |
| 1754 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1755 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1756 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1757 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1758 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1759 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1760 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1761 | /* Check if the suite has a type comment in it. */ |
| 1762 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1763 | |
| 1764 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1765 | if (type_comment != NULL) { |
| 1766 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1767 | return NULL; |
| 1768 | } |
| 1769 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1770 | if (!type_comment) |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | } |
| 1774 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1775 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1776 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1777 | 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] | 1778 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1779 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1780 | 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] | 1781 | } |
| 1782 | |
| 1783 | static stmt_ty |
| 1784 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1785 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1786 | /* async_funcdef: ASYNC funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1787 | REQ(n, async_funcdef); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1788 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1789 | REQ(CHILD(n, 1), funcdef); |
| 1790 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1791 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1792 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | static stmt_ty |
| 1796 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1797 | { |
| 1798 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1799 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1800 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | |
| 1804 | static stmt_ty |
| 1805 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1806 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1807 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1808 | REQ(n, async_stmt); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1809 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1810 | |
| 1811 | switch (TYPE(CHILD(n, 1))) { |
| 1812 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1813 | return ast_for_funcdef_impl(c, n, NULL, |
| 1814 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1815 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1816 | return ast_for_with_stmt(c, n, |
| 1817 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1818 | |
| 1819 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1820 | return ast_for_for_stmt(c, n, |
| 1821 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1822 | |
| 1823 | default: |
| 1824 | PyErr_Format(PyExc_SystemError, |
| 1825 | "invalid async stament: %s", |
| 1826 | STR(CHILD(n, 1))); |
| 1827 | return NULL; |
| 1828 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1831 | static stmt_ty |
| 1832 | ast_for_decorated(struct compiling *c, const node *n) |
| 1833 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1834 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1835 | stmt_ty thing = NULL; |
| 1836 | asdl_seq *decorator_seq = NULL; |
| 1837 | |
| 1838 | REQ(n, decorated); |
| 1839 | |
| 1840 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1841 | if (!decorator_seq) |
| 1842 | return NULL; |
| 1843 | |
| 1844 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1845 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1847 | |
| 1848 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1849 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1850 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1851 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1852 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1853 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1854 | } |
| 1855 | return thing; |
| 1856 | } |
| 1857 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1858 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1859 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1860 | { |
Guido van Rossum | b08d3f7 | 2019-12-15 10:00:33 -0800 | [diff] [blame] | 1861 | /* namedexpr_test: test [':=' test] |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1862 | argument: ( test [comp_for] | |
| 1863 | test ':=' test | |
| 1864 | test '=' test | |
| 1865 | '**' test | |
| 1866 | '*' test ) |
| 1867 | */ |
| 1868 | expr_ty target, value; |
| 1869 | |
| 1870 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1871 | if (!target) |
| 1872 | return NULL; |
| 1873 | |
| 1874 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1875 | if (!value) |
| 1876 | return NULL; |
| 1877 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1878 | if (target->kind != Name_kind) { |
| 1879 | const char *expr_name = get_expr_name(target); |
| 1880 | if (expr_name != NULL) { |
Ned Batchelder | 37143a8 | 2019-12-31 21:40:58 -0500 | [diff] [blame] | 1881 | ast_error(c, n, "cannot use assignment expressions with %s", expr_name); |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1882 | } |
| 1883 | return NULL; |
| 1884 | } |
| 1885 | |
| 1886 | if (!set_context(c, target, Store, n)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1887 | return NULL; |
| 1888 | |
| 1889 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1890 | n->n_end_col_offset, c->c_arena); |
| 1891 | } |
| 1892 | |
| 1893 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1894 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1895 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1896 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1897 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1898 | arguments_ty args; |
| 1899 | expr_ty expression; |
| 1900 | |
| 1901 | if (NCH(n) == 3) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1902 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1903 | if (!args) |
| 1904 | return NULL; |
| 1905 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1906 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1907 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1908 | } |
| 1909 | else { |
| 1910 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1911 | if (!args) |
| 1912 | return NULL; |
| 1913 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1914 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1915 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1918 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1919 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1922 | static expr_ty |
| 1923 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1926 | expr_ty expression, body, orelse; |
| 1927 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1928 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1929 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1930 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1931 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1932 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1933 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1934 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1935 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1936 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1937 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1938 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1939 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1940 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1943 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1944 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1945 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1946 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1947 | */ |
| 1948 | |
| 1949 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1950 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1951 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1952 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1953 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1954 | count_comp_for: |
| 1955 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1956 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1957 | if (NCH(n) == 2) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1958 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1959 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1960 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1961 | else if (NCH(n) == 1) { |
| 1962 | n = CHILD(n, 0); |
| 1963 | } |
| 1964 | else { |
| 1965 | goto error; |
| 1966 | } |
| 1967 | if (NCH(n) == (5)) { |
| 1968 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1969 | } |
| 1970 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1971 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1972 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1973 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1974 | REQ(n, comp_iter); |
| 1975 | n = CHILD(n, 0); |
| 1976 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1977 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1978 | else if (TYPE(n) == comp_if) { |
| 1979 | if (NCH(n) == 3) { |
| 1980 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1981 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1982 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1983 | else |
| 1984 | return n_fors; |
| 1985 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1986 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1987 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1988 | /* Should never be reached */ |
| 1989 | PyErr_SetString(PyExc_SystemError, |
| 1990 | "logic error in count_comp_fors"); |
| 1991 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1994 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1995 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1996 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1997 | */ |
| 1998 | |
| 1999 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2000 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2001 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2002 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2003 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2004 | while (1) { |
| 2005 | REQ(n, comp_iter); |
| 2006 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2007 | return n_ifs; |
| 2008 | n = CHILD(n, 0); |
| 2009 | REQ(n, comp_if); |
| 2010 | n_ifs++; |
| 2011 | if (NCH(n) == 2) |
| 2012 | return n_ifs; |
| 2013 | n = CHILD(n, 2); |
| 2014 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2017 | static asdl_seq * |
| 2018 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2019 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2020 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2021 | asdl_seq *comps; |
| 2022 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2023 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2024 | if (n_fors == -1) |
| 2025 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2026 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2027 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2028 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2029 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2030 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2031 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2032 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2033 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2034 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2035 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2036 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2037 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2039 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2041 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2042 | is_async = 1; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2043 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2044 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2045 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2046 | else { |
| 2047 | sync_n = CHILD(n, 0); |
| 2048 | } |
| 2049 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2050 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2051 | /* Async comprehensions only allowed in Python 3.6 and greater */ |
| 2052 | if (is_async && c->c_feature_version < 6) { |
| 2053 | ast_error(c, n, |
| 2054 | "Async comprehensions are only supported in Python 3.6 and greater"); |
| 2055 | return NULL; |
| 2056 | } |
| 2057 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2058 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2059 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2060 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2061 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2062 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2063 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2064 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2065 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2066 | /* Check the # of children rather than the length of t, since |
| 2067 | (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] | 2068 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2069 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2070 | comp = comprehension(first, expression, NULL, |
| 2071 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2072 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2073 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2074 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2075 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2076 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2077 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2078 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2079 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2080 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2081 | int j, n_ifs; |
| 2082 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2084 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2085 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2086 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2087 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2088 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2089 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2090 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2091 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2092 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2093 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2094 | REQ(n, comp_iter); |
| 2095 | n = CHILD(n, 0); |
| 2096 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2098 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2099 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2100 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2101 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2102 | if (NCH(n) == 3) |
| 2103 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2104 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2105 | /* on exit, must guarantee that n is a comp_for */ |
| 2106 | if (TYPE(n) == comp_iter) |
| 2107 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2108 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2109 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2110 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2111 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2112 | return comps; |
| 2113 | } |
| 2114 | |
| 2115 | static expr_ty |
| 2116 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2117 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2118 | /* testlist_comp: (test|star_expr) |
| 2119 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2120 | expr_ty elt; |
| 2121 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2122 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2124 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2126 | ch = CHILD(n, 0); |
| 2127 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2128 | if (!elt) |
| 2129 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2130 | if (elt->kind == Starred_kind) { |
| 2131 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2132 | return NULL; |
| 2133 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2135 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2136 | if (!comps) |
| 2137 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2138 | |
| 2139 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2140 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2141 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2142 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2143 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2144 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2145 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2146 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2147 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2148 | else |
| 2149 | /* Should never happen */ |
| 2150 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2153 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2154 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2155 | * elements. Iff successful, nonzero is returned. |
| 2156 | */ |
| 2157 | static int |
| 2158 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2159 | expr_ty *key, expr_ty *value) |
| 2160 | { |
| 2161 | expr_ty expression; |
| 2162 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2163 | assert(NCH(n) - *i >= 2); |
| 2164 | |
| 2165 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2166 | if (!expression) |
| 2167 | return 0; |
| 2168 | *key = NULL; |
| 2169 | *value = expression; |
| 2170 | |
| 2171 | *i += 2; |
| 2172 | } |
| 2173 | else { |
| 2174 | assert(NCH(n) - *i >= 3); |
| 2175 | |
| 2176 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2177 | if (!expression) |
| 2178 | return 0; |
| 2179 | *key = expression; |
| 2180 | |
| 2181 | REQ(CHILD(n, *i + 1), COLON); |
| 2182 | |
| 2183 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2184 | if (!expression) |
| 2185 | return 0; |
| 2186 | *value = expression; |
| 2187 | |
| 2188 | *i += 3; |
| 2189 | } |
| 2190 | return 1; |
| 2191 | } |
| 2192 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2193 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2194 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2195 | { |
| 2196 | expr_ty key, value; |
| 2197 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2198 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2200 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2201 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2202 | assert(key); |
| 2203 | assert(NCH(n) - i >= 1); |
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 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2206 | if (!comps) |
| 2207 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2209 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2210 | 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] | 2211 | } |
| 2212 | |
| 2213 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2214 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2215 | { |
| 2216 | int i; |
| 2217 | int j; |
| 2218 | int size; |
| 2219 | asdl_seq *keys, *values; |
| 2220 | |
| 2221 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2222 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2223 | if (!keys) |
| 2224 | return NULL; |
| 2225 | |
| 2226 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2227 | if (!values) |
| 2228 | return NULL; |
| 2229 | |
| 2230 | j = 0; |
| 2231 | for (i = 0; i < NCH(n); i++) { |
| 2232 | expr_ty key, value; |
| 2233 | |
| 2234 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2235 | return NULL; |
| 2236 | asdl_seq_SET(keys, j, key); |
| 2237 | asdl_seq_SET(values, j, value); |
| 2238 | |
| 2239 | j++; |
| 2240 | } |
| 2241 | keys->size = j; |
| 2242 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2243 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2244 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2248 | ast_for_genexp(struct compiling *c, const node *n) |
| 2249 | { |
| 2250 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2251 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | static expr_ty |
| 2255 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2256 | { |
| 2257 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2258 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | static expr_ty |
| 2262 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2263 | { |
| 2264 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2265 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2268 | static expr_ty |
| 2269 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2270 | { |
| 2271 | int i; |
| 2272 | int size; |
| 2273 | asdl_seq *elts; |
| 2274 | |
| 2275 | assert(TYPE(n) == (dictorsetmaker)); |
| 2276 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2277 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2278 | if (!elts) |
| 2279 | return NULL; |
| 2280 | for (i = 0; i < NCH(n); i += 2) { |
| 2281 | expr_ty expression; |
| 2282 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2283 | if (!expression) |
| 2284 | return NULL; |
| 2285 | asdl_seq_SET(elts, i / 2, expression); |
| 2286 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2287 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2288 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2289 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2290 | |
| 2291 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2292 | ast_for_atom(struct compiling *c, const node *n) |
| 2293 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2294 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2295 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2296 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2297 | */ |
| 2298 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2301 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2302 | PyObject *name; |
| 2303 | const char *s = STR(ch); |
| 2304 | size_t len = strlen(s); |
| 2305 | if (len >= 4 && len <= 5) { |
| 2306 | if (!strcmp(s, "None")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2307 | return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2308 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2309 | if (!strcmp(s, "True")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2310 | return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2311 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2312 | if (!strcmp(s, "False")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2313 | return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2314 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2315 | } |
| 2316 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2317 | if (!name) |
| 2318 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2319 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2320 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2321 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2322 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2323 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2324 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2325 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2326 | const char *errtype = NULL; |
| 2327 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2328 | errtype = "unicode error"; |
| 2329 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2330 | errtype = "value error"; |
| 2331 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2332 | PyObject *type, *value, *tback, *errstr; |
| 2333 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2334 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2335 | if (errstr) { |
| 2336 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2337 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2338 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2339 | else { |
| 2340 | PyErr_Clear(); |
| 2341 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2342 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2343 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2344 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2345 | Py_XDECREF(tback); |
| 2346 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2347 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2348 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2349 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2350 | } |
| 2351 | case NUMBER: { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2352 | PyObject *pynum; |
| 2353 | /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ |
| 2354 | /* Check for underscores here rather than in parse_number so we can report a line number on error */ |
| 2355 | if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { |
| 2356 | ast_error(c, ch, |
| 2357 | "Underscores in numeric literals are only supported in Python 3.6 and greater"); |
| 2358 | return NULL; |
| 2359 | } |
| 2360 | pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2361 | if (!pynum) |
| 2362 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2363 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2364 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2365 | Py_DECREF(pynum); |
| 2366 | return NULL; |
| 2367 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2368 | return Constant(pynum, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2369 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2370 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2371 | case ELLIPSIS: /* Ellipsis */ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2372 | return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2373 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2374 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2375 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2377 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2378 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2379 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2381 | if (TYPE(ch) == yield_expr) |
| 2382 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2385 | if (NCH(ch) == 1) { |
| 2386 | return ast_for_testlist(c, ch); |
| 2387 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2388 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2389 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2390 | return copy_location(ast_for_genexp(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2391 | } |
| 2392 | else { |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2393 | return copy_location(ast_for_testlist(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2394 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2395 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2396 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2398 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2399 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2400 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2402 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2403 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2404 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2405 | if (!elts) |
| 2406 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2407 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2408 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2409 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2410 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2411 | else { |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2412 | return copy_location(ast_for_listcomp(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2413 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2414 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2415 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2416 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2417 | * ((test | '*' test) |
| 2418 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2419 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2420 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2421 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2422 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2423 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2424 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2425 | } |
| 2426 | else { |
| 2427 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2428 | if (NCH(ch) == 1 || |
| 2429 | (NCH(ch) > 1 && |
| 2430 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2431 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2432 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2433 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2434 | else if (NCH(ch) > 1 && |
| 2435 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2436 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2437 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2438 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2439 | else if (NCH(ch) > 3 - is_dict && |
| 2440 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2441 | /* It's a dictionary comprehension. */ |
| 2442 | if (is_dict) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2443 | ast_error(c, n, |
| 2444 | "dict unpacking cannot be used in dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2445 | return NULL; |
| 2446 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2447 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2448 | } |
| 2449 | else { |
| 2450 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2451 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2452 | } |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2453 | return copy_location(res, n, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2454 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2455 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2456 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2457 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2458 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2462 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2463 | ast_for_slice(struct compiling *c, const node *n) |
| 2464 | { |
| 2465 | node *ch; |
| 2466 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2467 | |
| 2468 | REQ(n, subscript); |
| 2469 | |
| 2470 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2471 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2472 | sliceop: ':' [test] |
| 2473 | */ |
| 2474 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2475 | if (NCH(n) == 1 && TYPE(ch) == test) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2476 | return ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2480 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2481 | if (!lower) |
| 2482 | return NULL; |
| 2483 | } |
| 2484 | |
| 2485 | /* If there's an upper bound it's in the second or third position. */ |
| 2486 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2487 | if (NCH(n) > 1) { |
| 2488 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2489 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2490 | if (TYPE(n2) == test) { |
| 2491 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2492 | if (!upper) |
| 2493 | return NULL; |
| 2494 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2495 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2496 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2497 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2498 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2499 | if (TYPE(n2) == test) { |
| 2500 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2501 | if (!upper) |
| 2502 | return NULL; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | ch = CHILD(n, NCH(n) - 1); |
| 2507 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2508 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2509 | ch = CHILD(ch, 1); |
| 2510 | if (TYPE(ch) == test) { |
| 2511 | step = ast_for_expr(c, ch); |
| 2512 | if (!step) |
| 2513 | return NULL; |
| 2514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2518 | return Slice(lower, upper, step, LINENO(n), n->n_col_offset, |
| 2519 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | static expr_ty |
| 2523 | ast_for_binop(struct compiling *c, const node *n) |
| 2524 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2525 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2527 | BinOp(BinOp(A, op, B), op, C). |
| 2528 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2529 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2530 | int i, nops; |
| 2531 | expr_ty expr1, expr2, result; |
| 2532 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2533 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2534 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2535 | if (!expr1) |
| 2536 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2537 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2538 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2539 | if (!expr2) |
| 2540 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2542 | newoperator = get_operator(c, CHILD(n, 1)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2543 | if (!newoperator) |
| 2544 | return NULL; |
| 2545 | |
| 2546 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2547 | 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] | 2548 | c->c_arena); |
| 2549 | if (!result) |
| 2550 | return NULL; |
| 2551 | |
| 2552 | nops = (NCH(n) - 1) / 2; |
| 2553 | for (i = 1; i < nops; i++) { |
| 2554 | expr_ty tmp_result, tmp; |
| 2555 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2556 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2557 | newoperator = get_operator(c, next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2558 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2559 | return NULL; |
| 2560 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2561 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2562 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2563 | return NULL; |
| 2564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 | tmp_result = BinOp(result, newoperator, tmp, |
Carl Friedrich Bolz-Tereick | 110a47c | 2019-07-08 23:17:56 +0200 | [diff] [blame] | 2566 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2567 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2568 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2569 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2571 | return NULL; |
| 2572 | result = tmp_result; |
| 2573 | } |
| 2574 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2577 | static expr_ty |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2578 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2581 | subscriptlist: subscript (',' subscript)* [','] |
| 2582 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2583 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2584 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2585 | REQ(n, trailer); |
| 2586 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2587 | if (NCH(n) == 2) |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2588 | return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2589 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2590 | else |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2591 | return ast_for_call(c, CHILD(n, 1), left_expr, |
| 2592 | start, CHILD(n, 0), CHILD(n, 2)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2593 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2594 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2595 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2596 | if (!attr_id) |
| 2597 | return NULL; |
| 2598 | return Attribute(left_expr, attr_id, Load, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2599 | LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2600 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2601 | } |
| 2602 | else { |
| 2603 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2604 | REQ(CHILD(n, 2), RSQB); |
| 2605 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2606 | if (NCH(n) == 1) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2607 | expr_ty slc = ast_for_slice(c, CHILD(n, 0)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2608 | if (!slc) |
| 2609 | return NULL; |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2610 | return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2611 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2612 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2613 | } |
| 2614 | else { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2615 | int j; |
| 2616 | expr_ty slc, e; |
| 2617 | asdl_seq *elts; |
| 2618 | elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 2619 | if (!elts) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2620 | return NULL; |
| 2621 | for (j = 0; j < NCH(n); j += 2) { |
| 2622 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2623 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2624 | return NULL; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2625 | asdl_seq_SET(elts, j / 2, slc); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2626 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2627 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2628 | n->n_end_lineno, n->n_end_col_offset, |
| 2629 | c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2630 | if (!e) |
| 2631 | return NULL; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2632 | return Subscript(left_expr, e, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2633 | Load, LINENO(start), start->n_col_offset, |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2634 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
| 2635 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2641 | ast_for_factor(struct compiling *c, const node *n) |
| 2642 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2643 | expr_ty expression; |
| 2644 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2645 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2646 | if (!expression) |
| 2647 | return NULL; |
| 2648 | |
| 2649 | switch (TYPE(CHILD(n, 0))) { |
| 2650 | case PLUS: |
| 2651 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2652 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2653 | c->c_arena); |
| 2654 | case MINUS: |
| 2655 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2656 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2657 | c->c_arena); |
| 2658 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2659 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2660 | n->n_end_lineno, n->n_end_col_offset, |
| 2661 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2662 | } |
| 2663 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2664 | TYPE(CHILD(n, 0))); |
| 2665 | return NULL; |
| 2666 | } |
| 2667 | |
| 2668 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2669 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2670 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2671 | int i, nch, start = 0; |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2672 | expr_ty e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2673 | |
| 2674 | REQ(n, atom_expr); |
| 2675 | nch = NCH(n); |
| 2676 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2677 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2678 | if (c->c_feature_version < 5) { |
| 2679 | ast_error(c, n, |
| 2680 | "Await expressions are only supported in Python 3.5 and greater"); |
| 2681 | return NULL; |
| 2682 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2683 | start = 1; |
| 2684 | assert(nch > 1); |
| 2685 | } |
| 2686 | |
| 2687 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2688 | if (!e) |
| 2689 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2690 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2691 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2692 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2693 | return Await(e, LINENO(n), n->n_col_offset, |
| 2694 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2698 | node *ch = CHILD(n, i); |
| 2699 | if (TYPE(ch) != trailer) |
| 2700 | break; |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2701 | e = ast_for_trailer(c, ch, e, CHILD(n, start)); |
| 2702 | if (!e) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2703 | return NULL; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2704 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2705 | |
| 2706 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2707 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2708 | return Await(e, LINENO(n), n->n_col_offset, |
| 2709 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2710 | } |
| 2711 | else { |
| 2712 | return e; |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | static expr_ty |
| 2717 | ast_for_power(struct compiling *c, const node *n) |
| 2718 | { |
| 2719 | /* power: atom trailer* ('**' factor)* |
| 2720 | */ |
| 2721 | expr_ty e; |
| 2722 | REQ(n, power); |
| 2723 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2724 | if (!e) |
| 2725 | return NULL; |
| 2726 | if (NCH(n) == 1) |
| 2727 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2728 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2729 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2730 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2731 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2732 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2733 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2734 | } |
| 2735 | return e; |
| 2736 | } |
| 2737 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2738 | static expr_ty |
| 2739 | ast_for_starred(struct compiling *c, const node *n) |
| 2740 | { |
| 2741 | expr_ty tmp; |
| 2742 | REQ(n, star_expr); |
| 2743 | |
| 2744 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2745 | if (!tmp) |
| 2746 | return NULL; |
| 2747 | |
| 2748 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2749 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2750 | 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] | 2751 | } |
| 2752 | |
| 2753 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2754 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2755 | */ |
| 2756 | |
| 2757 | static expr_ty |
| 2758 | ast_for_expr(struct compiling *c, const node *n) |
| 2759 | { |
| 2760 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2761 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2762 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2763 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2765 | and_test: not_test ('and' not_test)* |
| 2766 | not_test: 'not' not_test | comparison |
| 2767 | comparison: expr (comp_op expr)* |
| 2768 | expr: xor_expr ('|' xor_expr)* |
| 2769 | xor_expr: and_expr ('^' and_expr)* |
| 2770 | and_expr: shift_expr ('&' shift_expr)* |
| 2771 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2772 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2773 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2774 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2775 | power: atom_expr ['**' factor] |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2776 | atom_expr: [AWAIT] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2777 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2778 | */ |
| 2779 | |
| 2780 | asdl_seq *seq; |
| 2781 | int i; |
| 2782 | |
| 2783 | loop: |
| 2784 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2785 | case namedexpr_test: |
| 2786 | if (NCH(n) == 3) |
| 2787 | return ast_for_namedexpr(c, n); |
| 2788 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2789 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2790 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2791 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2792 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2793 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2794 | else if (NCH(n) > 1) |
| 2795 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2796 | /* Fallthrough */ |
| 2797 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2798 | case and_test: |
| 2799 | if (NCH(n) == 1) { |
| 2800 | n = CHILD(n, 0); |
| 2801 | goto loop; |
| 2802 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2803 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2804 | if (!seq) |
| 2805 | return NULL; |
| 2806 | for (i = 0; i < NCH(n); i += 2) { |
| 2807 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2808 | if (!e) |
| 2809 | return NULL; |
| 2810 | asdl_seq_SET(seq, i / 2, e); |
| 2811 | } |
| 2812 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2813 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2814 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2815 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2816 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2817 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2818 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2819 | case not_test: |
| 2820 | if (NCH(n) == 1) { |
| 2821 | n = CHILD(n, 0); |
| 2822 | goto loop; |
| 2823 | } |
| 2824 | else { |
| 2825 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2826 | if (!expression) |
| 2827 | return NULL; |
| 2828 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2829 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2830 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2831 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2832 | } |
| 2833 | case comparison: |
| 2834 | if (NCH(n) == 1) { |
| 2835 | n = CHILD(n, 0); |
| 2836 | goto loop; |
| 2837 | } |
| 2838 | else { |
| 2839 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2840 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2841 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2842 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2843 | if (!ops) |
| 2844 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2845 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2846 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2847 | return NULL; |
| 2848 | } |
| 2849 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2850 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2851 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2852 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2853 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2854 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2855 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | |
| 2857 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2858 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2859 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2860 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2862 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2863 | asdl_seq_SET(cmps, i / 2, expression); |
| 2864 | } |
| 2865 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2866 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2867 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2868 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2870 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2871 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2873 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2874 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2876 | /* The next five cases all handle BinOps. The main body of code |
| 2877 | is the same in each case, but the switch turned inside out to |
| 2878 | reuse the code for each type of operator. |
| 2879 | */ |
| 2880 | case expr: |
| 2881 | case xor_expr: |
| 2882 | case and_expr: |
| 2883 | case shift_expr: |
| 2884 | case arith_expr: |
| 2885 | case term: |
| 2886 | if (NCH(n) == 1) { |
| 2887 | n = CHILD(n, 0); |
| 2888 | goto loop; |
| 2889 | } |
| 2890 | return ast_for_binop(c, n); |
| 2891 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2892 | node *an = NULL; |
| 2893 | node *en = NULL; |
| 2894 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2895 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2896 | if (NCH(n) > 1) |
| 2897 | an = CHILD(n, 1); /* yield_arg */ |
| 2898 | if (an) { |
| 2899 | en = CHILD(an, NCH(an) - 1); |
| 2900 | if (NCH(an) == 2) { |
| 2901 | is_from = 1; |
| 2902 | exp = ast_for_expr(c, en); |
| 2903 | } |
| 2904 | else |
| 2905 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2906 | if (!exp) |
| 2907 | return NULL; |
| 2908 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2909 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2910 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2911 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2912 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2913 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2914 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2915 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2916 | if (NCH(n) == 1) { |
| 2917 | n = CHILD(n, 0); |
| 2918 | goto loop; |
| 2919 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2920 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2921 | case power: |
| 2922 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2923 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2924 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2925 | return NULL; |
| 2926 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2927 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2928 | return NULL; |
| 2929 | } |
| 2930 | |
| 2931 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2932 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2933 | const node *start, const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2934 | { |
| 2935 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2936 | arglist: argument (',' argument)* [','] |
| 2937 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | */ |
| 2939 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2940 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2941 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2942 | asdl_seq *args; |
| 2943 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2944 | |
| 2945 | REQ(n, arglist); |
| 2946 | |
| 2947 | nargs = 0; |
| 2948 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2949 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2950 | node *ch = CHILD(n, i); |
| 2951 | if (TYPE(ch) == argument) { |
| 2952 | if (NCH(ch) == 1) |
| 2953 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2954 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2955 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2956 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2957 | ast_error(c, ch, "invalid syntax"); |
| 2958 | return NULL; |
| 2959 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2960 | if (NCH(n) > 1) { |
| 2961 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2962 | return NULL; |
| 2963 | } |
| 2964 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2965 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2966 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2967 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 2968 | nargs++; |
| 2969 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2970 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2971 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2972 | nkeywords++; |
| 2973 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2975 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2976 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2977 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2978 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2979 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2980 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2981 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2982 | |
| 2983 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2984 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2985 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2986 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2987 | node *ch = CHILD(n, i); |
| 2988 | if (TYPE(ch) == argument) { |
| 2989 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2990 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2991 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2992 | /* a positional argument */ |
| 2993 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2994 | if (ndoublestars) { |
| 2995 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 2996 | "positional argument follows " |
| 2997 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2998 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2999 | else { |
| 3000 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3001 | "positional argument follows " |
| 3002 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3003 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3004 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3005 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3006 | e = ast_for_expr(c, chch); |
| 3007 | if (!e) |
| 3008 | return NULL; |
| 3009 | asdl_seq_SET(args, nargs++, e); |
| 3010 | } |
| 3011 | else if (TYPE(chch) == STAR) { |
| 3012 | /* an iterable argument unpacking */ |
| 3013 | expr_ty starred; |
| 3014 | if (ndoublestars) { |
| 3015 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3016 | "iterable argument unpacking follows " |
| 3017 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3018 | return NULL; |
| 3019 | } |
| 3020 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3021 | if (!e) |
| 3022 | return NULL; |
| 3023 | starred = Starred(e, Load, LINENO(chch), |
| 3024 | chch->n_col_offset, |
Lysandros Nikolaou | 50d4f12 | 2019-12-18 01:20:55 +0100 | [diff] [blame] | 3025 | e->end_lineno, e->end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3026 | c->c_arena); |
| 3027 | if (!starred) |
| 3028 | return NULL; |
| 3029 | asdl_seq_SET(args, nargs++, starred); |
| 3030 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3031 | } |
| 3032 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3033 | /* a keyword argument unpacking */ |
| 3034 | keyword_ty kw; |
| 3035 | i++; |
| 3036 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3037 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3038 | return NULL; |
Pablo Galindo | 168660b | 2020-04-02 00:47:39 +0100 | [diff] [blame] | 3039 | kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset, |
| 3040 | e->end_lineno, e->end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3041 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3042 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3044 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3045 | /* the lone generator expression */ |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 3046 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3047 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3048 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3049 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3051 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3052 | /* treat colon equal as positional argument */ |
| 3053 | if (nkeywords) { |
| 3054 | if (ndoublestars) { |
| 3055 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3056 | "positional argument follows " |
| 3057 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3058 | } |
| 3059 | else { |
| 3060 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3061 | "positional argument follows " |
| 3062 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3063 | } |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | e = ast_for_namedexpr(c, ch); |
| 3067 | if (!e) |
| 3068 | return NULL; |
| 3069 | asdl_seq_SET(args, nargs++, e); |
| 3070 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3071 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3072 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3073 | keyword_ty kw; |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 3074 | identifier key; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3075 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3076 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3077 | // expression) in the keyword slot of a call site. So, we need |
| 3078 | // to manually enforce that the keyword is a NAME here. |
| 3079 | static const int name_tree[] = { |
| 3080 | test, |
| 3081 | or_test, |
| 3082 | and_test, |
| 3083 | not_test, |
| 3084 | comparison, |
| 3085 | expr, |
| 3086 | xor_expr, |
| 3087 | and_expr, |
| 3088 | shift_expr, |
| 3089 | arith_expr, |
| 3090 | term, |
| 3091 | factor, |
| 3092 | power, |
| 3093 | atom_expr, |
| 3094 | atom, |
| 3095 | 0, |
| 3096 | }; |
| 3097 | node *expr_node = chch; |
| 3098 | for (int i = 0; name_tree[i]; i++) { |
| 3099 | if (TYPE(expr_node) != name_tree[i]) |
| 3100 | break; |
| 3101 | if (NCH(expr_node) != 1) |
| 3102 | break; |
| 3103 | expr_node = CHILD(expr_node, 0); |
| 3104 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3105 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3106 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3107 | "expression cannot contain assignment, " |
| 3108 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3109 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3110 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3111 | key = new_identifier(STR(expr_node), c); |
| 3112 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3113 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3115 | if (forbidden_name(c, key, chch, 1)) { |
| 3116 | return NULL; |
| 3117 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3118 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3120 | return NULL; |
Pablo Galindo | 168660b | 2020-04-02 00:47:39 +0100 | [diff] [blame] | 3121 | kw = keyword(key, e, chch->n_lineno, chch->n_col_offset, |
Pablo Galindo | 40cf35c | 2020-04-03 21:02:26 +0100 | [diff] [blame] | 3122 | e->end_lineno, e->end_col_offset, c->c_arena); |
Pablo Galindo | 168660b | 2020-04-02 00:47:39 +0100 | [diff] [blame] | 3123 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3124 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3125 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3126 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3127 | } |
| 3128 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 3131 | return Call(func, args, keywords, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3132 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3135 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3136 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3137 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3138 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3139 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3140 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3141 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3142 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3143 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3144 | } |
| 3145 | else { |
| 3146 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3147 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3148 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3149 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3150 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3151 | else { |
| 3152 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3153 | if (!tmp) |
| 3154 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3155 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3156 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3157 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3160 | static stmt_ty |
| 3161 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3162 | { |
| 3163 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3164 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3165 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3166 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3167 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3168 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3169 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3170 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3171 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3172 | int num = NCH(n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3173 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3174 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3175 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3176 | if (!e) |
| 3177 | return NULL; |
| 3178 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3179 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3180 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | } |
| 3182 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3183 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3184 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3185 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3186 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3187 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3188 | if (!expr1) |
| 3189 | return NULL; |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 3190 | /* Augmented assignments can only have a name, a subscript, or an |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3191 | attribute on the left, though, so we have to explicitly check for |
| 3192 | those. */ |
| 3193 | switch (expr1->kind) { |
| 3194 | case Name_kind: |
| 3195 | case Attribute_kind: |
| 3196 | case Subscript_kind: |
| 3197 | break; |
| 3198 | default: |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 3199 | ast_error(c, ch, "'%s' is an illegal expression for augmented assignment", |
| 3200 | get_expr_name(expr1)); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3201 | return NULL; |
| 3202 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3203 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 3204 | /* set_context checks that most expressions are not the left side. */ |
| 3205 | if(!set_context(c, expr1, Store, ch)) { |
| 3206 | return NULL; |
| 3207 | } |
| 3208 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3209 | ch = CHILD(n, 2); |
| 3210 | if (TYPE(ch) == testlist) |
| 3211 | expr2 = ast_for_testlist(c, ch); |
| 3212 | else |
| 3213 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3214 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3215 | return NULL; |
| 3216 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3217 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3218 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3219 | return NULL; |
| 3220 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3221 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3222 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3223 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3224 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3225 | expr_ty expr1, expr2, expr3; |
| 3226 | node *ch = CHILD(n, 0); |
| 3227 | node *deep, *ann = CHILD(n, 1); |
| 3228 | int simple = 1; |
| 3229 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 3230 | /* AnnAssigns are only allowed in Python 3.6 or greater */ |
| 3231 | if (c->c_feature_version < 6) { |
| 3232 | ast_error(c, ch, |
| 3233 | "Variable annotation syntax is only supported in Python 3.6 and greater"); |
| 3234 | return NULL; |
| 3235 | } |
| 3236 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3237 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3238 | deep = ch; |
| 3239 | while (NCH(deep) == 1) { |
| 3240 | deep = CHILD(deep, 0); |
| 3241 | } |
| 3242 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3243 | simple = 0; |
| 3244 | } |
| 3245 | expr1 = ast_for_testlist(c, ch); |
| 3246 | if (!expr1) { |
| 3247 | return NULL; |
| 3248 | } |
| 3249 | switch (expr1->kind) { |
| 3250 | case Name_kind: |
| 3251 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | expr1->v.Name.ctx = Store; |
| 3255 | break; |
| 3256 | case Attribute_kind: |
| 3257 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3258 | return NULL; |
| 3259 | } |
| 3260 | expr1->v.Attribute.ctx = Store; |
| 3261 | break; |
| 3262 | case Subscript_kind: |
| 3263 | expr1->v.Subscript.ctx = Store; |
| 3264 | break; |
| 3265 | case List_kind: |
| 3266 | ast_error(c, ch, |
| 3267 | "only single target (not list) can be annotated"); |
| 3268 | return NULL; |
| 3269 | case Tuple_kind: |
| 3270 | ast_error(c, ch, |
| 3271 | "only single target (not tuple) can be annotated"); |
| 3272 | return NULL; |
| 3273 | default: |
| 3274 | ast_error(c, ch, |
| 3275 | "illegal target for annotation"); |
| 3276 | return NULL; |
| 3277 | } |
| 3278 | |
| 3279 | if (expr1->kind != Name_kind) { |
| 3280 | simple = 0; |
| 3281 | } |
| 3282 | ch = CHILD(ann, 1); |
| 3283 | expr2 = ast_for_expr(c, ch); |
| 3284 | if (!expr2) { |
| 3285 | return NULL; |
| 3286 | } |
| 3287 | if (NCH(ann) == 2) { |
| 3288 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3289 | LINENO(n), n->n_col_offset, |
| 3290 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3291 | } |
| 3292 | else { |
| 3293 | ch = CHILD(ann, 3); |
Pablo Galindo | 8565f6b | 2019-06-03 08:34:20 +0100 | [diff] [blame] | 3294 | if (TYPE(ch) == testlist_star_expr) { |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3295 | expr3 = ast_for_testlist(c, ch); |
| 3296 | } |
| 3297 | else { |
| 3298 | expr3 = ast_for_expr(c, ch); |
| 3299 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3300 | if (!expr3) { |
| 3301 | return NULL; |
| 3302 | } |
| 3303 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3304 | LINENO(n), n->n_col_offset, |
| 3305 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3306 | } |
| 3307 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3308 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3309 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3310 | asdl_seq *targets; |
| 3311 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3312 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3313 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3314 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3315 | /* a normal assignment */ |
| 3316 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3317 | |
| 3318 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3319 | nch_minus_type = num - has_type_comment; |
| 3320 | |
| 3321 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3322 | if (!targets) |
| 3323 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3324 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3325 | expr_ty e; |
| 3326 | node *ch = CHILD(n, i); |
| 3327 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3328 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3329 | return NULL; |
| 3330 | } |
| 3331 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3333 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3334 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3335 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3336 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3337 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3338 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3339 | asdl_seq_SET(targets, i / 2, e); |
| 3340 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3341 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3342 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3343 | expression = ast_for_testlist(c, value); |
| 3344 | else |
| 3345 | expression = ast_for_expr(c, value); |
| 3346 | if (!expression) |
| 3347 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3348 | if (has_type_comment) { |
| 3349 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3350 | if (!type_comment) |
| 3351 | return NULL; |
| 3352 | } |
| 3353 | else |
| 3354 | type_comment = NULL; |
| 3355 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3356 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3357 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3360 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3361 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3362 | 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] | 3363 | { |
| 3364 | asdl_seq *seq; |
| 3365 | int i; |
| 3366 | expr_ty e; |
| 3367 | |
| 3368 | REQ(n, exprlist); |
| 3369 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3370 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3371 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3372 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3373 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3374 | e = ast_for_expr(c, CHILD(n, i)); |
| 3375 | if (!e) |
| 3376 | return NULL; |
| 3377 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3378 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3379 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3380 | } |
| 3381 | return seq; |
| 3382 | } |
| 3383 | |
| 3384 | static stmt_ty |
| 3385 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3386 | { |
| 3387 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3389 | /* del_stmt: 'del' exprlist */ |
| 3390 | REQ(n, del_stmt); |
| 3391 | |
| 3392 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3393 | if (!expr_list) |
| 3394 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3395 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3396 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | static stmt_ty |
| 3400 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3401 | { |
| 3402 | /* |
| 3403 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3404 | | yield_stmt |
| 3405 | break_stmt: 'break' |
| 3406 | continue_stmt: 'continue' |
| 3407 | return_stmt: 'return' [testlist] |
| 3408 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3409 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3410 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3411 | */ |
| 3412 | node *ch; |
| 3413 | |
| 3414 | REQ(n, flow_stmt); |
| 3415 | ch = CHILD(n, 0); |
| 3416 | switch (TYPE(ch)) { |
| 3417 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3418 | return Break(LINENO(n), n->n_col_offset, |
| 3419 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3420 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3421 | return Continue(LINENO(n), n->n_col_offset, |
| 3422 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3423 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3424 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3425 | if (!exp) |
| 3426 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3427 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3428 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3429 | } |
| 3430 | case return_stmt: |
| 3431 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3432 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3433 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3434 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3435 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | if (!expression) |
| 3437 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3438 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3439 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3440 | } |
| 3441 | case raise_stmt: |
| 3442 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3443 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3444 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3445 | else if (NCH(ch) >= 2) { |
| 3446 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3447 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3448 | if (!expression) |
| 3449 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3450 | if (NCH(ch) == 4) { |
| 3451 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3452 | if (!cause) |
| 3453 | return NULL; |
| 3454 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3455 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3456 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3457 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3458 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3459 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3460 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3461 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3462 | return NULL; |
| 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3467 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3468 | { |
| 3469 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3470 | import_as_name: NAME ['as' NAME] |
| 3471 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3472 | dotted_name: NAME ('.' NAME)* |
| 3473 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3474 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3475 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3476 | loop: |
| 3477 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3478 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3479 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3480 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3481 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3482 | if (!name) |
| 3483 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3484 | if (NCH(n) == 3) { |
| 3485 | node *str_node = CHILD(n, 2); |
| 3486 | str = NEW_IDENTIFIER(str_node); |
| 3487 | if (!str) |
| 3488 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3489 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3490 | return NULL; |
| 3491 | } |
| 3492 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3493 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3494 | return NULL; |
| 3495 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3496 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3497 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3498 | case dotted_as_name: |
| 3499 | if (NCH(n) == 1) { |
| 3500 | n = CHILD(n, 0); |
| 3501 | goto loop; |
| 3502 | } |
| 3503 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3504 | node *asname_node = CHILD(n, 2); |
| 3505 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3506 | if (!a) |
| 3507 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3508 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3509 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3510 | if (!a->asname) |
| 3511 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3512 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3513 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3514 | return a; |
| 3515 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3516 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3517 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3518 | node *name_node = CHILD(n, 0); |
| 3519 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3520 | if (!name) |
| 3521 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3522 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3523 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3524 | return alias(name, NULL, c->c_arena); |
| 3525 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3526 | else { |
| 3527 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3528 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3529 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3530 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3532 | |
| 3533 | len = 0; |
| 3534 | for (i = 0; i < NCH(n); i += 2) |
| 3535 | /* length of string plus one for the dot */ |
| 3536 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3537 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3538 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | if (!str) |
| 3540 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3541 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3542 | if (!s) |
| 3543 | return NULL; |
| 3544 | for (i = 0; i < NCH(n); i += 2) { |
| 3545 | char *sch = STR(CHILD(n, i)); |
| 3546 | strcpy(s, STR(CHILD(n, i))); |
| 3547 | s += strlen(sch); |
| 3548 | *s++ = '.'; |
| 3549 | } |
| 3550 | --s; |
| 3551 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3553 | PyBytes_GET_SIZE(str), |
| 3554 | NULL); |
| 3555 | Py_DECREF(str); |
| 3556 | if (!uni) |
| 3557 | return NULL; |
| 3558 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3559 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3560 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3561 | Py_DECREF(str); |
| 3562 | return NULL; |
| 3563 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3564 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3565 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3566 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3567 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3568 | if (!str) |
| 3569 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3570 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3571 | Py_DECREF(str); |
| 3572 | return NULL; |
| 3573 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3574 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3575 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3576 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3577 | "unexpected import name: %d", TYPE(n)); |
| 3578 | return NULL; |
| 3579 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | static stmt_ty |
| 3583 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3584 | { |
| 3585 | /* |
| 3586 | import_stmt: import_name | import_from |
| 3587 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3588 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3589 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3590 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3591 | int lineno; |
| 3592 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3593 | int i; |
| 3594 | asdl_seq *aliases; |
| 3595 | |
| 3596 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3597 | lineno = LINENO(n); |
| 3598 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3599 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3600 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3601 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3602 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3603 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3604 | if (!aliases) |
| 3605 | return NULL; |
| 3606 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3607 | 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] | 3608 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3609 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3610 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3611 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3612 | // Even though n is modified above, the end position is not changed |
| 3613 | return Import(aliases, lineno, col_offset, |
| 3614 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3615 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3616 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3618 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3619 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3620 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3621 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3623 | /* Count the number of dots (for relative imports) and check for the |
| 3624 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3625 | for (idx = 1; idx < NCH(n); idx++) { |
| 3626 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3627 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3628 | if (!mod) |
| 3629 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3630 | idx++; |
| 3631 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3632 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3633 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3634 | ndots += 3; |
| 3635 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3636 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3637 | break; |
| 3638 | } |
| 3639 | ndots++; |
| 3640 | } |
| 3641 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3642 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3643 | case STAR: |
| 3644 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3645 | n = CHILD(n, idx); |
| 3646 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3647 | break; |
| 3648 | case LPAR: |
| 3649 | /* from ... import (x, y, z) */ |
| 3650 | n = CHILD(n, idx + 1); |
| 3651 | n_children = NCH(n); |
| 3652 | break; |
| 3653 | case import_as_names: |
| 3654 | /* from ... import x, y, z */ |
| 3655 | n = CHILD(n, idx); |
| 3656 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3657 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3658 | ast_error(c, n, |
| 3659 | "trailing comma not allowed without" |
| 3660 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3661 | return NULL; |
| 3662 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3663 | break; |
| 3664 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3665 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3666 | return NULL; |
| 3667 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3669 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3670 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3671 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3672 | |
| 3673 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3674 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3675 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3676 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3677 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3678 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3679 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3680 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3681 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3682 | 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] | 3683 | if (!import_alias) |
| 3684 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3685 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3686 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3688 | if (mod != NULL) |
| 3689 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3690 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3691 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3692 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3693 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3694 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3695 | "unknown import statement: starts with command '%s'", |
| 3696 | STR(CHILD(n, 0))); |
| 3697 | return NULL; |
| 3698 | } |
| 3699 | |
| 3700 | static stmt_ty |
| 3701 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3702 | { |
| 3703 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3704 | identifier name; |
| 3705 | asdl_seq *s; |
| 3706 | int i; |
| 3707 | |
| 3708 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3709 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3710 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3711 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3712 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3713 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3714 | if (!name) |
| 3715 | return NULL; |
| 3716 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3717 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3718 | return Global(s, LINENO(n), n->n_col_offset, |
| 3719 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3723 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3724 | { |
| 3725 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3726 | identifier name; |
| 3727 | asdl_seq *s; |
| 3728 | int i; |
| 3729 | |
| 3730 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3731 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3732 | if (!s) |
| 3733 | return NULL; |
| 3734 | for (i = 1; i < NCH(n); i += 2) { |
| 3735 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3736 | if (!name) |
| 3737 | return NULL; |
| 3738 | asdl_seq_SET(s, i / 2, name); |
| 3739 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3740 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3741 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3745 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3746 | { |
| 3747 | /* assert_stmt: 'assert' test [',' test] */ |
| 3748 | REQ(n, assert_stmt); |
| 3749 | if (NCH(n) == 2) { |
| 3750 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3751 | if (!expression) |
| 3752 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3753 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3754 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3755 | } |
| 3756 | else if (NCH(n) == 4) { |
| 3757 | expr_ty expr1, expr2; |
| 3758 | |
| 3759 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3760 | if (!expr1) |
| 3761 | return NULL; |
| 3762 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3763 | if (!expr2) |
| 3764 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3766 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3767 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3768 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3769 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3770 | "improper number of parts to 'assert' statement: %d", |
| 3771 | NCH(n)); |
| 3772 | return NULL; |
| 3773 | } |
| 3774 | |
| 3775 | static asdl_seq * |
| 3776 | ast_for_suite(struct compiling *c, const node *n) |
| 3777 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3778 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3779 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3780 | stmt_ty s; |
| 3781 | int i, total, num, end, pos = 0; |
| 3782 | node *ch; |
| 3783 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3784 | if (TYPE(n) != func_body_suite) { |
| 3785 | REQ(n, suite); |
| 3786 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3787 | |
| 3788 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3789 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3790 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3791 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3792 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3793 | n = CHILD(n, 0); |
| 3794 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3795 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3796 | */ |
| 3797 | end = NCH(n) - 1; |
| 3798 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3799 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3800 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3801 | for (i = 0; i < end; i += 2) { |
| 3802 | ch = CHILD(n, i); |
| 3803 | s = ast_for_stmt(c, ch); |
| 3804 | if (!s) |
| 3805 | return NULL; |
| 3806 | asdl_seq_SET(seq, pos++, s); |
| 3807 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3808 | } |
| 3809 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3810 | i = 2; |
| 3811 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3812 | i += 2; |
| 3813 | REQ(CHILD(n, 2), NEWLINE); |
| 3814 | } |
| 3815 | |
| 3816 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3817 | ch = CHILD(n, i); |
| 3818 | REQ(ch, stmt); |
| 3819 | num = num_stmts(ch); |
| 3820 | if (num == 1) { |
| 3821 | /* small_stmt or compound_stmt with only one child */ |
| 3822 | s = ast_for_stmt(c, ch); |
| 3823 | if (!s) |
| 3824 | return NULL; |
| 3825 | asdl_seq_SET(seq, pos++, s); |
| 3826 | } |
| 3827 | else { |
| 3828 | int j; |
| 3829 | ch = CHILD(ch, 0); |
| 3830 | REQ(ch, simple_stmt); |
| 3831 | for (j = 0; j < NCH(ch); j += 2) { |
| 3832 | /* statement terminates with a semi-colon ';' */ |
| 3833 | if (NCH(CHILD(ch, j)) == 0) { |
| 3834 | assert((j + 1) == NCH(ch)); |
| 3835 | break; |
| 3836 | } |
| 3837 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3838 | if (!s) |
| 3839 | return NULL; |
| 3840 | asdl_seq_SET(seq, pos++, s); |
| 3841 | } |
| 3842 | } |
| 3843 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3844 | } |
| 3845 | assert(pos == seq->size); |
| 3846 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3849 | static void |
| 3850 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3851 | { |
Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame] | 3852 | Py_ssize_t tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3853 | // There must be no empty suites. |
| 3854 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3855 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3856 | *end_lineno = last->end_lineno; |
| 3857 | *end_col_offset = last->end_col_offset; |
| 3858 | } |
| 3859 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3860 | static stmt_ty |
| 3861 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3862 | { |
| 3863 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3864 | ['else' ':' suite] |
| 3865 | */ |
| 3866 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3867 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3868 | |
| 3869 | REQ(n, if_stmt); |
| 3870 | |
| 3871 | if (NCH(n) == 4) { |
| 3872 | expr_ty expression; |
| 3873 | asdl_seq *suite_seq; |
| 3874 | |
| 3875 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3876 | if (!expression) |
| 3877 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3879 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3880 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3881 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3883 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3884 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3885 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3886 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3887 | s = STR(CHILD(n, 4)); |
| 3888 | /* s[2], the third character in the string, will be |
| 3889 | 's' for el_s_e, or |
| 3890 | 'i' for el_i_f |
| 3891 | */ |
| 3892 | if (s[2] == 's') { |
| 3893 | expr_ty expression; |
| 3894 | asdl_seq *seq1, *seq2; |
| 3895 | |
| 3896 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3897 | if (!expression) |
| 3898 | return NULL; |
| 3899 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3900 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3901 | return NULL; |
| 3902 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3903 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3904 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3905 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3906 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3907 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3908 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3909 | } |
| 3910 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3911 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3912 | expr_ty expression; |
| 3913 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3914 | asdl_seq *orelse = NULL; |
| 3915 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3916 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3917 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3918 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3919 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3920 | has_else = 1; |
| 3921 | n_elif -= 3; |
| 3922 | } |
| 3923 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3924 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3925 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3926 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3927 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3928 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3929 | if (!orelse) |
| 3930 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3931 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3932 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3933 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3934 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3935 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3936 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3937 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3938 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3939 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3940 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3942 | asdl_seq_SET(orelse, 0, |
| 3943 | If(expression, suite_seq, suite_seq2, |
Lysandros Nikolaou | 5936a4c | 2019-12-14 11:24:57 +0100 | [diff] [blame] | 3944 | LINENO(CHILD(n, NCH(n) - 7)), |
| 3945 | CHILD(n, NCH(n) - 7)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3946 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3947 | /* the just-created orelse handled the last elif */ |
| 3948 | n_elif--; |
| 3949 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3950 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3951 | for (i = 0; i < n_elif; i++) { |
| 3952 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3953 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3954 | if (!newobj) |
| 3955 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3956 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3957 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3958 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3959 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3960 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3962 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3963 | if (orelse != NULL) { |
| 3964 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3965 | } else { |
| 3966 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3967 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3968 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | If(expression, suite_seq, orelse, |
Lysandros Nikolaou | 025a602 | 2019-12-12 22:40:21 +0100 | [diff] [blame] | 3970 | LINENO(CHILD(n, off - 1)), |
| 3971 | CHILD(n, off - 1)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3972 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3973 | orelse = newobj; |
| 3974 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3975 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3976 | if (!expression) |
| 3977 | return NULL; |
| 3978 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3979 | if (!suite_seq) |
| 3980 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3981 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3982 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3983 | LINENO(n), n->n_col_offset, |
| 3984 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3985 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3986 | |
| 3987 | PyErr_Format(PyExc_SystemError, |
| 3988 | "unexpected token in 'if' statement: %s", s); |
| 3989 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | static stmt_ty |
| 3993 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3994 | { |
| 3995 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3996 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3997 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3998 | |
| 3999 | if (NCH(n) == 4) { |
| 4000 | expr_ty expression; |
| 4001 | asdl_seq *suite_seq; |
| 4002 | |
| 4003 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4004 | if (!expression) |
| 4005 | return NULL; |
| 4006 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4007 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4008 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4009 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4010 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4011 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4012 | } |
| 4013 | else if (NCH(n) == 7) { |
| 4014 | expr_ty expression; |
| 4015 | asdl_seq *seq1, *seq2; |
| 4016 | |
| 4017 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4018 | if (!expression) |
| 4019 | return NULL; |
| 4020 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4021 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4022 | return NULL; |
| 4023 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4024 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4025 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4026 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4027 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4028 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4029 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4030 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4031 | |
| 4032 | PyErr_Format(PyExc_SystemError, |
| 4033 | "wrong number of tokens for 'while' statement: %d", |
| 4034 | NCH(n)); |
| 4035 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4039 | 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] | 4040 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4041 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4042 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4043 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4044 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4045 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4046 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4047 | int has_type_comment; |
| 4048 | string type_comment; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4049 | |
| 4050 | if (is_async && c->c_feature_version < 5) { |
| 4051 | ast_error(c, n, |
| 4052 | "Async for loops are only supported in Python 3.5 and greater"); |
| 4053 | return NULL; |
| 4054 | } |
| 4055 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4056 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4057 | REQ(n, for_stmt); |
| 4058 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4059 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4060 | |
| 4061 | if (NCH(n) == 9 + has_type_comment) { |
| 4062 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4063 | if (!seq) |
| 4064 | return NULL; |
| 4065 | } |
| 4066 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4067 | node_target = CHILD(n, 1); |
| 4068 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4069 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4070 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4071 | /* Check the # of children rather than the length of _target, since |
| 4072 | 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] | 4073 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4074 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4075 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4076 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4077 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4078 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4079 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4080 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4081 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4082 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4083 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4084 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4085 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4086 | return NULL; |
| 4087 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4088 | if (seq != NULL) { |
| 4089 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4090 | } else { |
| 4091 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4092 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4093 | |
| 4094 | if (has_type_comment) { |
| 4095 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4096 | if (!type_comment) |
| 4097 | return NULL; |
| 4098 | } |
| 4099 | else |
| 4100 | type_comment = NULL; |
| 4101 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4102 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4103 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4104 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4105 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4106 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4107 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4108 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4109 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | static excepthandler_ty |
| 4113 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4114 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4115 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4116 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4117 | REQ(exc, except_clause); |
| 4118 | REQ(body, suite); |
| 4119 | |
| 4120 | if (NCH(exc) == 1) { |
| 4121 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4122 | if (!suite_seq) |
| 4123 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4124 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4125 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4126 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4127 | exc->n_col_offset, |
| 4128 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4129 | } |
| 4130 | else if (NCH(exc) == 2) { |
| 4131 | expr_ty expression; |
| 4132 | asdl_seq *suite_seq; |
| 4133 | |
| 4134 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4135 | if (!expression) |
| 4136 | return NULL; |
| 4137 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4138 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4139 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4140 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4141 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4142 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4143 | exc->n_col_offset, |
| 4144 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4145 | } |
| 4146 | else if (NCH(exc) == 4) { |
| 4147 | asdl_seq *suite_seq; |
| 4148 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4149 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4150 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4151 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4152 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4153 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4154 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4155 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4156 | return NULL; |
| 4157 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4158 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4159 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4160 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4161 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4162 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4163 | exc->n_col_offset, |
| 4164 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4165 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4166 | |
| 4167 | PyErr_Format(PyExc_SystemError, |
| 4168 | "wrong number of children for 'except' clause: %d", |
| 4169 | NCH(exc)); |
| 4170 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | static stmt_ty |
| 4174 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4175 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4176 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4177 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4178 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4179 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4180 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4181 | REQ(n, try_stmt); |
| 4182 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4183 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4184 | if (body == NULL) |
| 4185 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4186 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4187 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4188 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4189 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4190 | /* we can assume it's an "else", |
| 4191 | because nch >= 9 for try-else-finally and |
| 4192 | it would otherwise have a type of except_clause */ |
| 4193 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4194 | if (orelse == NULL) |
| 4195 | return NULL; |
| 4196 | n_except--; |
| 4197 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4198 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4199 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4200 | if (finally == NULL) |
| 4201 | return NULL; |
| 4202 | n_except--; |
| 4203 | } |
| 4204 | else { |
| 4205 | /* we can assume it's an "else", |
| 4206 | otherwise it would have a type of except_clause */ |
| 4207 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 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 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4214 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4215 | return NULL; |
| 4216 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4217 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4218 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4219 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4220 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4221 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4222 | if (handlers == NULL) |
| 4223 | return NULL; |
| 4224 | |
| 4225 | for (i = 0; i < n_except; i++) { |
| 4226 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4227 | CHILD(n, 5 + i * 3)); |
| 4228 | if (!e) |
| 4229 | return NULL; |
| 4230 | asdl_seq_SET(handlers, i, e); |
| 4231 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4234 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4235 | if (finally != NULL) { |
| 4236 | // finally is always last |
| 4237 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4238 | } else if (orelse != NULL) { |
| 4239 | // otherwise else is last |
| 4240 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4241 | } else { |
| 4242 | // inline the get_last_end_pos logic due to layout mismatch |
| 4243 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4244 | end_lineno = last_handler->end_lineno; |
| 4245 | end_col_offset = last_handler->end_col_offset; |
| 4246 | } |
| 4247 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4248 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4251 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4252 | static withitem_ty |
| 4253 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4254 | { |
| 4255 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4256 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4257 | REQ(n, with_item); |
| 4258 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4259 | if (!context_expr) |
| 4260 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4261 | if (NCH(n) == 3) { |
| 4262 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4263 | |
| 4264 | if (!optional_vars) { |
| 4265 | return NULL; |
| 4266 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4267 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4268 | return NULL; |
| 4269 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4272 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4275 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4276 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4277 | 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] | 4278 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4279 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4280 | 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] | 4281 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4282 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4283 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4284 | if (is_async && c->c_feature_version < 5) { |
| 4285 | ast_error(c, n, |
| 4286 | "Async with statements are only supported in Python 3.5 and greater"); |
| 4287 | return NULL; |
| 4288 | } |
| 4289 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4290 | REQ(n, with_stmt); |
| 4291 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4292 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4293 | nch_minus_type = NCH(n) - has_type_comment; |
| 4294 | |
| 4295 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4296 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4297 | if (!items) |
| 4298 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4299 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4300 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4301 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4302 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4303 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4306 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4307 | if (!body) |
| 4308 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4309 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4310 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4311 | if (has_type_comment) { |
| 4312 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4313 | if (!type_comment) |
| 4314 | return NULL; |
| 4315 | } |
| 4316 | else |
| 4317 | type_comment = NULL; |
| 4318 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4319 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4320 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4321 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4322 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4323 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4324 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4327 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4328 | 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] | 4329 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4330 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4331 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4332 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4333 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4334 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4335 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4336 | REQ(n, classdef); |
| 4337 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4338 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4339 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4340 | if (!s) |
| 4341 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4342 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4343 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4344 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4345 | if (!classname) |
| 4346 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4347 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4348 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4349 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4350 | LINENO(n), n->n_col_offset, |
| 4351 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4352 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4353 | |
| 4354 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4355 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4356 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4357 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4358 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4359 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4360 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4361 | if (!classname) |
| 4362 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4363 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4364 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4365 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4366 | LINENO(n), n->n_col_offset, |
| 4367 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4370 | /* class NAME '(' arglist ')' ':' suite */ |
| 4371 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4372 | { |
| 4373 | PyObject *dummy_name; |
| 4374 | expr_ty dummy; |
| 4375 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4376 | if (!dummy_name) |
| 4377 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4378 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4379 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4380 | c->c_arena); |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 4381 | call = ast_for_call(c, CHILD(n, 3), dummy, |
| 4382 | CHILD(n, 1), NULL, CHILD(n, 4)); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4383 | if (!call) |
| 4384 | return NULL; |
| 4385 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4386 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4387 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4388 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4389 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4390 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4391 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4392 | if (!classname) |
| 4393 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4394 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4395 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4396 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4397 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4398 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4399 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | static stmt_ty |
| 4403 | ast_for_stmt(struct compiling *c, const node *n) |
| 4404 | { |
| 4405 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4406 | assert(NCH(n) == 1); |
| 4407 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4408 | } |
| 4409 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4410 | assert(num_stmts(n) == 1); |
| 4411 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4412 | } |
| 4413 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4414 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4415 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4416 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4417 | */ |
| 4418 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4419 | case expr_stmt: |
| 4420 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4421 | case del_stmt: |
| 4422 | return ast_for_del_stmt(c, n); |
| 4423 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4424 | return Pass(LINENO(n), n->n_col_offset, |
| 4425 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4426 | case flow_stmt: |
| 4427 | return ast_for_flow_stmt(c, n); |
| 4428 | case import_stmt: |
| 4429 | return ast_for_import_stmt(c, n); |
| 4430 | case global_stmt: |
| 4431 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4432 | case nonlocal_stmt: |
| 4433 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4434 | case assert_stmt: |
| 4435 | return ast_for_assert_stmt(c, n); |
| 4436 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4437 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4438 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4439 | TYPE(n), NCH(n)); |
| 4440 | return NULL; |
| 4441 | } |
| 4442 | } |
| 4443 | else { |
| 4444 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4445 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4446 | */ |
| 4447 | node *ch = CHILD(n, 0); |
| 4448 | REQ(n, compound_stmt); |
| 4449 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4450 | case if_stmt: |
| 4451 | return ast_for_if_stmt(c, ch); |
| 4452 | case while_stmt: |
| 4453 | return ast_for_while_stmt(c, ch); |
| 4454 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4455 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4456 | case try_stmt: |
| 4457 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4458 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4459 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4460 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4461 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4462 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4463 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | case decorated: |
| 4465 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4466 | case async_stmt: |
| 4467 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4468 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4469 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4470 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4471 | TYPE(n), NCH(n)); |
| 4472 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4473 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4478 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4479 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4480 | const char *end; |
| 4481 | long x; |
| 4482 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4483 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4484 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4485 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4486 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4487 | errno = 0; |
| 4488 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4489 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4490 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4491 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4492 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4493 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4494 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4495 | } |
| 4496 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4497 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4498 | if (*end == '\0') { |
| 4499 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4500 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4501 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4502 | } |
| 4503 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4504 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4505 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4506 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4507 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4508 | return NULL; |
| 4509 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4510 | } |
| 4511 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4512 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4513 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4514 | if (dx == -1.0 && PyErr_Occurred()) |
| 4515 | return NULL; |
| 4516 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4517 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4521 | parsenumber(struct compiling *c, const char *s) |
| 4522 | { |
| 4523 | char *dup, *end; |
| 4524 | PyObject *res = NULL; |
| 4525 | |
| 4526 | assert(s != NULL); |
| 4527 | |
| 4528 | if (strchr(s, '_') == NULL) { |
| 4529 | return parsenumber_raw(c, s); |
| 4530 | } |
| 4531 | /* Create a duplicate without underscores. */ |
| 4532 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4533 | if (dup == NULL) { |
| 4534 | return PyErr_NoMemory(); |
| 4535 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4536 | end = dup; |
| 4537 | for (; *s; s++) { |
| 4538 | if (*s != '_') { |
| 4539 | *end++ = *s; |
| 4540 | } |
| 4541 | } |
| 4542 | *end = '\0'; |
| 4543 | res = parsenumber_raw(c, dup); |
| 4544 | PyMem_Free(dup); |
| 4545 | return res; |
| 4546 | } |
| 4547 | |
| 4548 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4549 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4550 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4551 | const char *s, *t; |
| 4552 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4553 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4554 | while (s < end && (*s & 0x80)) s++; |
| 4555 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4556 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4559 | static int |
| 4560 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4561 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4562 | { |
| 4563 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4564 | first_invalid_escape_char); |
| 4565 | if (msg == NULL) { |
| 4566 | return -1; |
| 4567 | } |
Gregory P. Smith | b4be87a | 2019-08-10 00:19:07 -0700 | [diff] [blame] | 4568 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4569 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4570 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4571 | { |
Gregory P. Smith | b4be87a | 2019-08-10 00:19:07 -0700 | [diff] [blame] | 4572 | if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) { |
| 4573 | /* Replace the DeprecationWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4574 | to get a more accurate error report */ |
| 4575 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4576 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4577 | } |
| 4578 | Py_DECREF(msg); |
| 4579 | return -1; |
| 4580 | } |
| 4581 | Py_DECREF(msg); |
| 4582 | return 0; |
| 4583 | } |
| 4584 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4585 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4586 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4587 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4588 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4589 | PyObject *v, *u; |
| 4590 | char *buf; |
| 4591 | char *p; |
| 4592 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4593 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4594 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4595 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4596 | return NULL; |
| 4597 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4598 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4599 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4600 | if (u == NULL) |
| 4601 | return NULL; |
| 4602 | p = buf = PyBytes_AsString(u); |
| 4603 | end = s + len; |
| 4604 | while (s < end) { |
| 4605 | if (*s == '\\') { |
| 4606 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4607 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4608 | strcpy(p, "u005c"); |
| 4609 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4610 | if (s >= end) |
| 4611 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4612 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4613 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4614 | if (*s & 0x80) { /* XXX inefficient */ |
| 4615 | PyObject *w; |
| 4616 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 4617 | const void *data; |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4618 | Py_ssize_t len, i; |
| 4619 | w = decode_utf8(c, &s, end); |
| 4620 | if (w == NULL) { |
| 4621 | Py_DECREF(u); |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | kind = PyUnicode_KIND(w); |
| 4625 | data = PyUnicode_DATA(w); |
| 4626 | len = PyUnicode_GET_LENGTH(w); |
| 4627 | for (i = 0; i < len; i++) { |
| 4628 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4629 | sprintf(p, "\\U%08x", chr); |
| 4630 | p += 10; |
| 4631 | } |
| 4632 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4633 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4634 | Py_DECREF(w); |
| 4635 | } else { |
| 4636 | *p++ = *s++; |
| 4637 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4638 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4639 | len = p - buf; |
| 4640 | s = buf; |
| 4641 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4642 | const char *first_invalid_escape; |
| 4643 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4644 | |
| 4645 | if (v != NULL && first_invalid_escape != NULL) { |
| 4646 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4647 | /* We have not decref u before because first_invalid_escape points |
| 4648 | inside u. */ |
| 4649 | Py_XDECREF(u); |
| 4650 | Py_DECREF(v); |
| 4651 | return NULL; |
| 4652 | } |
| 4653 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4654 | Py_XDECREF(u); |
| 4655 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4658 | static PyObject * |
| 4659 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4660 | size_t len) |
| 4661 | { |
| 4662 | const char *first_invalid_escape; |
Greg Price | 3a4f667 | 2019-09-12 11:12:22 -0700 | [diff] [blame] | 4663 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4664 | &first_invalid_escape); |
| 4665 | if (result == NULL) |
| 4666 | return NULL; |
| 4667 | |
| 4668 | if (first_invalid_escape != NULL) { |
| 4669 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4670 | Py_DECREF(result); |
| 4671 | return NULL; |
| 4672 | } |
| 4673 | } |
| 4674 | return result; |
| 4675 | } |
| 4676 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4677 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4678 | and `col_offset` to existing locations. */ |
| 4679 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4680 | { |
| 4681 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4682 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4683 | for (int i = 0; i < NCH(n); ++i) { |
| 4684 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4685 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4686 | col_offset = 0; |
| 4687 | } |
| 4688 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4689 | } |
| 4690 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4691 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | /* Fix locations for the given node and its children. |
| 4695 | |
| 4696 | `parent` is the enclosing node. |
| 4697 | `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] | 4698 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4699 | */ |
| 4700 | static void |
| 4701 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4702 | { |
| 4703 | char *substr = NULL; |
| 4704 | char *start; |
| 4705 | int lines = LINENO(parent) - 1; |
| 4706 | int cols = parent->n_col_offset; |
| 4707 | /* Find the full fstring to fix location information in `n`. */ |
| 4708 | while (parent && parent->n_type != STRING) |
| 4709 | parent = parent->n_child; |
| 4710 | if (parent && parent->n_str) { |
| 4711 | substr = strstr(parent->n_str, expr_str); |
| 4712 | if (substr) { |
| 4713 | start = substr; |
| 4714 | while (start > parent->n_str) { |
| 4715 | if (start[0] == '\n') |
| 4716 | break; |
| 4717 | start--; |
| 4718 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4719 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4720 | /* adjust the start based on the number of newlines encountered |
| 4721 | before the f-string expression */ |
| 4722 | for (char* p = parent->n_str; p < substr; p++) { |
| 4723 | if (*p == '\n') { |
| 4724 | lines++; |
| 4725 | } |
| 4726 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4727 | } |
| 4728 | } |
| 4729 | fstring_shift_node_locations(n, lines, cols); |
| 4730 | } |
| 4731 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4732 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4733 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4734 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4735 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4736 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4737 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4738 | { |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4739 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4740 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4741 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4742 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4743 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4744 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4745 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4746 | assert(*(expr_start-1) == '{'); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4747 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' || |
| 4748 | *expr_end == '='); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4749 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4750 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4751 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4752 | because turning the expression '' in to '()' would go from being invalid |
| 4753 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4754 | for (s = expr_start; s != expr_end; s++) { |
| 4755 | char c = *s; |
| 4756 | /* The Python parser ignores only the following whitespace |
| 4757 | characters (\r already is converted to \n). */ |
| 4758 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4759 | break; |
| 4760 | } |
| 4761 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4762 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4763 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4764 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4765 | } |
| 4766 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4767 | len = expr_end - expr_start; |
| 4768 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 4769 | str = PyMem_Malloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4770 | if (str == NULL) { |
| 4771 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4772 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4773 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4774 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4775 | str[0] = '('; |
| 4776 | memcpy(str+1, expr_start, len); |
| 4777 | str[len+1] = ')'; |
| 4778 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4779 | |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 4780 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4781 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4782 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4783 | Py_eval_input, 0); |
| 4784 | if (!mod_n) { |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 4785 | PyMem_Free(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4786 | return NULL; |
| 4787 | } |
| 4788 | /* Reuse str to find the correct column offset. */ |
| 4789 | str[0] = '{'; |
| 4790 | str[len+1] = '}'; |
| 4791 | fstring_fix_node_location(n, mod_n, str); |
| 4792 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 4793 | PyMem_Free(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4794 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4795 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4796 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4797 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | /* Return -1 on error. |
| 4801 | |
| 4802 | Return 0 if we reached the end of the literal. |
| 4803 | |
| 4804 | Return 1 if we haven't reached the end of the literal, but we want |
| 4805 | the caller to process the literal up to this point. Used for |
| 4806 | doubled braces. |
| 4807 | */ |
| 4808 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4809 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4810 | PyObject **literal, int recurse_lvl, |
| 4811 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4812 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4813 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4814 | brace (which isn't part of a unicode name escape such as |
| 4815 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4816 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4817 | const char *s = *str; |
| 4818 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4819 | int result = 0; |
| 4820 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4821 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4822 | while (s < end) { |
| 4823 | char ch = *s++; |
| 4824 | if (!raw && ch == '\\' && s < end) { |
| 4825 | ch = *s++; |
| 4826 | if (ch == 'N') { |
| 4827 | if (s < end && *s++ == '{') { |
| 4828 | while (s < end && *s++ != '}') { |
| 4829 | } |
| 4830 | continue; |
| 4831 | } |
| 4832 | break; |
| 4833 | } |
| 4834 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4835 | return -1; |
| 4836 | } |
| 4837 | } |
| 4838 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4839 | /* Check for doubled braces, but only at the top level. If |
| 4840 | we checked at every level, then f'{0:{3}}' would fail |
| 4841 | with the two closing braces. */ |
| 4842 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4843 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4844 | /* We're going to tell the caller that the literal ends |
| 4845 | here, but that they should continue scanning. But also |
| 4846 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4847 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4848 | result = 1; |
| 4849 | goto done; |
| 4850 | } |
| 4851 | |
| 4852 | /* Where a single '{' is the start of a new expression, a |
| 4853 | single '}' is not allowed. */ |
| 4854 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4855 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4856 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4857 | return -1; |
| 4858 | } |
| 4859 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4860 | /* We're either at a '{', which means we're starting another |
| 4861 | expression; or a '}', which means we're at the end of this |
| 4862 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4863 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4864 | break; |
| 4865 | } |
| 4866 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4867 | *str = s; |
| 4868 | assert(s <= end); |
| 4869 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4870 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4871 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4872 | if (raw) |
| 4873 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4874 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4875 | NULL, NULL); |
| 4876 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4877 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4878 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4879 | if (!*literal) |
| 4880 | return -1; |
| 4881 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4882 | return result; |
| 4883 | } |
| 4884 | |
| 4885 | /* Forward declaration because parsing is recursive. */ |
| 4886 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4887 | 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] | 4888 | struct compiling *c, const node *n); |
| 4889 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4890 | /* Parse the f-string at *str, ending at end. We know *str starts an |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4891 | expression (so it must be a '{'). Returns the FormattedValue node, which |
| 4892 | includes the expression, conversion character, format_spec expression, and |
| 4893 | optionally the text of the expression (if = is used). |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4894 | |
| 4895 | Note that I don't do a perfect job here: I don't make sure that a |
| 4896 | closing brace doesn't match an opening paren, for example. It |
| 4897 | doesn't need to error on all invalid expressions, just correctly |
| 4898 | find the end of all valid ones. Any errors inside the expression |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 4899 | will be caught when we parse it later. |
| 4900 | |
| 4901 | *expression is set to the expression. For an '=' "debug" expression, |
| 4902 | *expr_text is set to the debug text (the original text of the expression, |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 4903 | including the '=' and any whitespace around it, as a string object). If |
| 4904 | not a debug expression, *expr_text set to NULL. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4905 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4906 | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 4907 | PyObject **expr_text, expr_ty *expression, |
| 4908 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 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. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4916 | int conversion = -1; /* The conversion char. Use default if not |
| 4917 | specified, or !r if using = and no format |
| 4918 | spec. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4919 | |
| 4920 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4921 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4922 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4923 | |
| 4924 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4925 | int string_type = 0; |
| 4926 | |
| 4927 | /* Keep track of nesting level for braces/parens/brackets in |
| 4928 | expressions. */ |
| 4929 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4930 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4931 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 4932 | *expr_text = NULL; |
| 4933 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4934 | /* Can only nest one level deep. */ |
| 4935 | if (recurse_lvl >= 2) { |
| 4936 | ast_error(c, n, "f-string: expressions nested too deeply"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4937 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4941 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4942 | assert(**str == '{'); |
| 4943 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4944 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4945 | expr_start = *str; |
| 4946 | for (; *str < end; (*str)++) { |
| 4947 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4948 | |
| 4949 | /* Loop invariants. */ |
| 4950 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4951 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4952 | if (quote_char) |
| 4953 | assert(string_type == 1 || string_type == 3); |
| 4954 | else |
| 4955 | assert(string_type == 0); |
| 4956 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4957 | ch = **str; |
| 4958 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4959 | if (ch == '\\') { |
| 4960 | /* Error: can't include a backslash character, inside |
| 4961 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4962 | ast_error(c, n, |
| 4963 | "f-string expression part " |
| 4964 | "cannot include a backslash"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4965 | goto error; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4966 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4967 | if (quote_char) { |
| 4968 | /* We're inside a string. See if we're at the end. */ |
| 4969 | /* This code needs to implement the same non-error logic |
| 4970 | as tok_get from tokenizer.c, at the letter_quote |
| 4971 | label. To actually share that code would be a |
| 4972 | nightmare. But, it's unlikely to change and is small, |
| 4973 | so duplicate it here. Note we don't need to catch all |
| 4974 | of the errors, since they'll be caught when parsing the |
| 4975 | expression. We just need to match the non-error |
| 4976 | cases. Thus we can ignore \n in single-quoted strings, |
| 4977 | for example. Or non-terminated strings. */ |
| 4978 | if (ch == quote_char) { |
| 4979 | /* Does this match the string_type (single or triple |
| 4980 | quoted)? */ |
| 4981 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4982 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4983 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4984 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4985 | string_type = 0; |
| 4986 | quote_char = 0; |
| 4987 | continue; |
| 4988 | } |
| 4989 | } else { |
| 4990 | /* We're at the end of a normal string. */ |
| 4991 | quote_char = 0; |
| 4992 | string_type = 0; |
| 4993 | continue; |
| 4994 | } |
| 4995 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4996 | } else if (ch == '\'' || ch == '"') { |
| 4997 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4998 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4999 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5000 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5001 | } else { |
| 5002 | /* Start of a normal string. */ |
| 5003 | string_type = 1; |
| 5004 | } |
| 5005 | /* Start looking for the end of the string. */ |
| 5006 | quote_char = ch; |
| 5007 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5008 | if (nested_depth >= MAXLEVEL) { |
| 5009 | ast_error(c, n, "f-string: too many nested parenthesis"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5010 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5011 | } |
| 5012 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5013 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5014 | } else if (ch == '#') { |
| 5015 | /* Error: can't include a comment character, inside parens |
| 5016 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5017 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5018 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5019 | } else if (nested_depth == 0 && |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5020 | (ch == '!' || ch == ':' || ch == '}' || |
| 5021 | ch == '=' || ch == '>' || ch == '<')) { |
| 5022 | /* See if there's a next character. */ |
| 5023 | if (*str+1 < end) { |
| 5024 | char next = *(*str+1); |
| 5025 | |
| 5026 | /* For "!=". since '=' is not an allowed conversion character, |
| 5027 | nothing is lost in this test. */ |
| 5028 | if ((ch == '!' && next == '=') || /* != */ |
| 5029 | (ch == '=' && next == '=') || /* == */ |
| 5030 | (ch == '<' && next == '=') || /* <= */ |
| 5031 | (ch == '>' && next == '=') /* >= */ |
| 5032 | ) { |
| 5033 | *str += 1; |
| 5034 | continue; |
| 5035 | } |
| 5036 | /* Don't get out of the loop for these, if they're single |
| 5037 | chars (not part of 2-char tokens). If by themselves, they |
| 5038 | don't end an expression (unlike say '!'). */ |
| 5039 | if (ch == '>' || ch == '<') { |
| 5040 | continue; |
| 5041 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5042 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5043 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5044 | /* Normal way out of this loop. */ |
| 5045 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5046 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5047 | if (!nested_depth) { |
| 5048 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5049 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5050 | } |
| 5051 | nested_depth--; |
| 5052 | int opening = parenstack[nested_depth]; |
| 5053 | if (!((opening == '(' && ch == ')') || |
| 5054 | (opening == '[' && ch == ']') || |
| 5055 | (opening == '{' && ch == '}'))) |
| 5056 | { |
| 5057 | ast_error(c, n, |
| 5058 | "f-string: closing parenthesis '%c' " |
| 5059 | "does not match opening parenthesis '%c'", |
| 5060 | ch, opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5061 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5062 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5063 | } else { |
| 5064 | /* Just consume this char and loop around. */ |
| 5065 | } |
| 5066 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5067 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5068 | /* If we leave this loop in a string or with mismatched parens, we |
| 5069 | don't care. We'll get a syntax error when compiling the |
| 5070 | expression. But, we can produce a better error message, so |
| 5071 | let's just do that.*/ |
| 5072 | if (quote_char) { |
| 5073 | ast_error(c, n, "f-string: unterminated string"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5074 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5075 | } |
| 5076 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5077 | int opening = parenstack[nested_depth - 1]; |
| 5078 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5079 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5080 | } |
| 5081 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5082 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5083 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5084 | |
| 5085 | /* Compile the expression as soon as possible, so we show errors |
| 5086 | related to the expression before errors related to the |
| 5087 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5088 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5089 | if (!simple_expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5090 | goto error; |
| 5091 | |
| 5092 | /* Check for =, which puts the text value of the expression in |
| 5093 | expr_text. */ |
| 5094 | if (**str == '=') { |
Pablo Galindo | 9b83829 | 2020-05-27 22:01:11 +0100 | [diff] [blame] | 5095 | if (c->c_feature_version < 8) { |
| 5096 | ast_error(c, n, |
| 5097 | "f-string: self documenting expressions are " |
| 5098 | "only supported in Python 3.8 and greater"); |
| 5099 | goto error; |
| 5100 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5101 | *str += 1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5102 | |
| 5103 | /* Skip over ASCII whitespace. No need to test for end of string |
| 5104 | here, since we know there's at least a trailing quote somewhere |
| 5105 | ahead. */ |
| 5106 | while (Py_ISSPACE(**str)) { |
| 5107 | *str += 1; |
| 5108 | } |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5109 | |
| 5110 | /* Set *expr_text to the text of the expression. */ |
| 5111 | *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); |
| 5112 | if (!*expr_text) { |
| 5113 | goto error; |
| 5114 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5115 | } |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5116 | |
| 5117 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5118 | if (**str == '!') { |
| 5119 | *str += 1; |
| 5120 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5121 | goto unexpected_end_of_string; |
| 5122 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5123 | conversion = **str; |
| 5124 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5125 | |
| 5126 | /* Validate the conversion. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5127 | if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5128 | ast_error(c, n, |
| 5129 | "f-string: invalid conversion character: " |
| 5130 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5131 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5132 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5133 | |
| 5134 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5135 | |
| 5136 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5137 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5138 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5139 | if (**str == ':') { |
| 5140 | *str += 1; |
| 5141 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5142 | goto unexpected_end_of_string; |
| 5143 | |
| 5144 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5145 | 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] | 5146 | if (!format_spec) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5147 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5148 | } |
| 5149 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5150 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5151 | goto unexpected_end_of_string; |
| 5152 | |
| 5153 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5154 | assert(*str < end); |
| 5155 | assert(**str == '}'); |
| 5156 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5157 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5158 | /* If we're in = mode (detected by non-NULL expr_text), and have no format |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 5159 | spec and no explicit conversion, set the conversion to 'r'. */ |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5160 | if (*expr_text && format_spec == NULL && conversion == -1) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5161 | conversion = 'r'; |
| 5162 | } |
| 5163 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5164 | /* And now create the FormattedValue node that represents this |
| 5165 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5166 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5167 | format_spec, LINENO(n), |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5168 | n->n_col_offset, n->n_end_lineno, |
| 5169 | n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5170 | if (!*expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5171 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5172 | |
| 5173 | return 0; |
| 5174 | |
| 5175 | unexpected_end_of_string: |
| 5176 | ast_error(c, n, "f-string: expecting '}'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5177 | /* Falls through to error. */ |
| 5178 | |
| 5179 | error: |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5180 | Py_XDECREF(*expr_text); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5181 | return -1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5182 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /* Return -1 on error. |
| 5186 | |
| 5187 | Return 0 if we have a literal (possible zero length) and an |
| 5188 | expression (zero length if at the end of the string. |
| 5189 | |
| 5190 | Return 1 if we have a literal, but no expression, and we want the |
| 5191 | caller to call us again. This is used to deal with doubled |
| 5192 | braces. |
| 5193 | |
| 5194 | When called multiple times on the string 'a{{b{0}c', this function |
| 5195 | will return: |
| 5196 | |
| 5197 | 1. the literal 'a{' with no expression, and a return value |
| 5198 | of 1. Despite the fact that there's no expression, the return |
| 5199 | value of 1 means we're not finished yet. |
| 5200 | |
| 5201 | 2. the literal 'b' and the expression '0', with a return value of |
| 5202 | 0. The fact that there's an expression means we're not finished. |
| 5203 | |
| 5204 | 3. literal 'c' with no expression and a return value of 0. The |
| 5205 | combination of the return value of 0 with no expression means |
| 5206 | we're finished. |
| 5207 | */ |
| 5208 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5209 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5210 | int recurse_lvl, PyObject **literal, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5211 | PyObject **expr_text, expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5212 | struct compiling *c, const node *n) |
| 5213 | { |
| 5214 | int result; |
| 5215 | |
| 5216 | assert(*literal == NULL && *expression == NULL); |
| 5217 | |
| 5218 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5219 | 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] | 5220 | if (result < 0) |
| 5221 | goto error; |
| 5222 | |
| 5223 | assert(result == 0 || result == 1); |
| 5224 | |
| 5225 | if (result == 1) |
| 5226 | /* We have a literal, but don't look at the expression. */ |
| 5227 | return 1; |
| 5228 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5230 | /* We're at the end of the string or the end of a nested |
| 5231 | f-string: no expression. The top-level error case where we |
| 5232 | expect to be at the end of the string but we're at a '}' is |
| 5233 | handled later. */ |
| 5234 | return 0; |
| 5235 | |
| 5236 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5237 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5238 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5239 | if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text, |
| 5240 | expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5241 | goto error; |
| 5242 | |
| 5243 | return 0; |
| 5244 | |
| 5245 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5246 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5247 | return -1; |
| 5248 | } |
| 5249 | |
| 5250 | #define EXPRLIST_N_CACHED 64 |
| 5251 | |
| 5252 | typedef struct { |
| 5253 | /* Incrementally build an array of expr_ty, so be used in an |
| 5254 | asdl_seq. Cache some small but reasonably sized number of |
| 5255 | expr_ty's, and then after that start dynamically allocating, |
| 5256 | doubling the number allocated each time. Note that the f-string |
| 5257 | 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] | 5258 | Constant for the literal 'a'. So you add expr_ty's about twice as |
Min ho Kim | 39d87b5 | 2019-08-31 06:21:19 +1000 | [diff] [blame] | 5259 | fast as you add expressions in an f-string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5260 | |
| 5261 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5262 | Py_ssize_t size; /* Number we've used. */ |
| 5263 | expr_ty *p; /* Pointer to the memory we're actually |
| 5264 | using. Will point to 'data' until we |
| 5265 | start dynamically allocating. */ |
| 5266 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5267 | } ExprList; |
| 5268 | |
| 5269 | #ifdef NDEBUG |
| 5270 | #define ExprList_check_invariants(l) |
| 5271 | #else |
| 5272 | static void |
| 5273 | ExprList_check_invariants(ExprList *l) |
| 5274 | { |
| 5275 | /* Check our invariants. Make sure this object is "live", and |
| 5276 | hasn't been deallocated. */ |
| 5277 | assert(l->size >= 0); |
| 5278 | assert(l->p != NULL); |
| 5279 | if (l->size <= EXPRLIST_N_CACHED) |
| 5280 | assert(l->data == l->p); |
| 5281 | } |
| 5282 | #endif |
| 5283 | |
| 5284 | static void |
| 5285 | ExprList_Init(ExprList *l) |
| 5286 | { |
| 5287 | l->allocated = EXPRLIST_N_CACHED; |
| 5288 | l->size = 0; |
| 5289 | |
| 5290 | /* Until we start allocating dynamically, p points to data. */ |
| 5291 | l->p = l->data; |
| 5292 | |
| 5293 | ExprList_check_invariants(l); |
| 5294 | } |
| 5295 | |
| 5296 | static int |
| 5297 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5298 | { |
| 5299 | ExprList_check_invariants(l); |
| 5300 | if (l->size >= l->allocated) { |
| 5301 | /* We need to alloc (or realloc) the memory. */ |
| 5302 | Py_ssize_t new_size = l->allocated * 2; |
| 5303 | |
| 5304 | /* See if we've ever allocated anything dynamically. */ |
| 5305 | if (l->p == l->data) { |
| 5306 | Py_ssize_t i; |
| 5307 | /* We're still using the cached data. Switch to |
| 5308 | alloc-ing. */ |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 5309 | l->p = PyMem_Malloc(sizeof(expr_ty) * new_size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5310 | if (!l->p) |
| 5311 | return -1; |
| 5312 | /* Copy the cached data into the new buffer. */ |
| 5313 | for (i = 0; i < l->size; i++) |
| 5314 | l->p[i] = l->data[i]; |
| 5315 | } else { |
| 5316 | /* Just realloc. */ |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 5317 | expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5318 | if (!tmp) { |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 5319 | PyMem_Free(l->p); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5320 | l->p = NULL; |
| 5321 | return -1; |
| 5322 | } |
| 5323 | l->p = tmp; |
| 5324 | } |
| 5325 | |
| 5326 | l->allocated = new_size; |
| 5327 | assert(l->allocated == 2 * l->size); |
| 5328 | } |
| 5329 | |
| 5330 | l->p[l->size++] = exp; |
| 5331 | |
| 5332 | ExprList_check_invariants(l); |
| 5333 | return 0; |
| 5334 | } |
| 5335 | |
| 5336 | static void |
| 5337 | ExprList_Dealloc(ExprList *l) |
| 5338 | { |
| 5339 | ExprList_check_invariants(l); |
| 5340 | |
| 5341 | /* If there's been an error, or we've never dynamically allocated, |
| 5342 | do nothing. */ |
| 5343 | if (!l->p || l->p == l->data) { |
| 5344 | /* Do nothing. */ |
| 5345 | } else { |
| 5346 | /* We have dynamically allocated. Free the memory. */ |
Lysandros Nikolaou | 5193d0a | 2020-06-27 21:35:18 +0300 | [diff] [blame] | 5347 | PyMem_Free(l->p); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5348 | } |
| 5349 | l->p = NULL; |
| 5350 | l->size = -1; |
| 5351 | } |
| 5352 | |
| 5353 | static asdl_seq * |
| 5354 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5355 | { |
| 5356 | asdl_seq *seq; |
| 5357 | |
| 5358 | ExprList_check_invariants(l); |
| 5359 | |
| 5360 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5361 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5362 | if (seq) { |
| 5363 | Py_ssize_t i; |
| 5364 | for (i = 0; i < l->size; i++) |
| 5365 | asdl_seq_SET(seq, i, l->p[i]); |
| 5366 | } |
| 5367 | ExprList_Dealloc(l); |
| 5368 | return seq; |
| 5369 | } |
| 5370 | |
| 5371 | /* The FstringParser is designed to add a mix of strings and |
| 5372 | f-strings, and concat them together as needed. Ultimately, it |
| 5373 | generates an expr_ty. */ |
| 5374 | typedef struct { |
| 5375 | PyObject *last_str; |
| 5376 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5377 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5378 | } FstringParser; |
| 5379 | |
| 5380 | #ifdef NDEBUG |
| 5381 | #define FstringParser_check_invariants(state) |
| 5382 | #else |
| 5383 | static void |
| 5384 | FstringParser_check_invariants(FstringParser *state) |
| 5385 | { |
| 5386 | if (state->last_str) |
| 5387 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5388 | ExprList_check_invariants(&state->expr_list); |
| 5389 | } |
| 5390 | #endif |
| 5391 | |
| 5392 | static void |
| 5393 | FstringParser_Init(FstringParser *state) |
| 5394 | { |
| 5395 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5396 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5397 | ExprList_Init(&state->expr_list); |
| 5398 | FstringParser_check_invariants(state); |
| 5399 | } |
| 5400 | |
| 5401 | static void |
| 5402 | FstringParser_Dealloc(FstringParser *state) |
| 5403 | { |
| 5404 | FstringParser_check_invariants(state); |
| 5405 | |
| 5406 | Py_XDECREF(state->last_str); |
| 5407 | ExprList_Dealloc(&state->expr_list); |
| 5408 | } |
| 5409 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5410 | /* Constants for the following */ |
| 5411 | static PyObject *u_kind; |
| 5412 | |
| 5413 | /* Compute 'kind' field for string Constant (either 'u' or None) */ |
| 5414 | static PyObject * |
| 5415 | make_kind(struct compiling *c, const node *n) |
| 5416 | { |
| 5417 | char *s = NULL; |
| 5418 | PyObject *kind = NULL; |
| 5419 | |
| 5420 | /* Find the first string literal, if any */ |
| 5421 | while (TYPE(n) != STRING) { |
| 5422 | if (NCH(n) == 0) |
| 5423 | return NULL; |
| 5424 | n = CHILD(n, 0); |
| 5425 | } |
| 5426 | REQ(n, STRING); |
| 5427 | |
| 5428 | /* If it starts with 'u', return a PyUnicode "u" string */ |
| 5429 | s = STR(n); |
| 5430 | if (s && *s == 'u') { |
| 5431 | if (!u_kind) { |
| 5432 | u_kind = PyUnicode_InternFromString("u"); |
| 5433 | if (!u_kind) |
| 5434 | return NULL; |
| 5435 | } |
| 5436 | kind = u_kind; |
| 5437 | if (PyArena_AddPyObject(c->c_arena, kind) < 0) { |
| 5438 | return NULL; |
| 5439 | } |
| 5440 | Py_INCREF(kind); |
| 5441 | } |
| 5442 | return kind; |
| 5443 | } |
| 5444 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5445 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5446 | static expr_ty |
| 5447 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5448 | { |
| 5449 | PyObject *s = *str; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5450 | PyObject *kind = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5451 | *str = NULL; |
| 5452 | assert(PyUnicode_CheckExact(s)); |
| 5453 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5454 | Py_DECREF(s); |
| 5455 | return NULL; |
| 5456 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5457 | kind = make_kind(c, n); |
| 5458 | if (kind == NULL && PyErr_Occurred()) |
| 5459 | return NULL; |
| 5460 | return Constant(s, kind, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5461 | 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] | 5462 | } |
| 5463 | |
| 5464 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5465 | decref'd. */ |
| 5466 | static int |
| 5467 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5468 | { |
| 5469 | FstringParser_check_invariants(state); |
| 5470 | |
| 5471 | assert(PyUnicode_CheckExact(str)); |
| 5472 | |
| 5473 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5474 | Py_DECREF(str); |
| 5475 | return 0; |
| 5476 | } |
| 5477 | |
| 5478 | if (!state->last_str) { |
| 5479 | /* We didn't have a string before, so just remember this one. */ |
| 5480 | state->last_str = str; |
| 5481 | } else { |
| 5482 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5483 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5484 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5485 | return -1; |
| 5486 | } |
| 5487 | FstringParser_check_invariants(state); |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5491 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5492 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5493 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5494 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5495 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5496 | struct compiling *c, const node *n) |
| 5497 | { |
| 5498 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5499 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5500 | |
| 5501 | /* Parse the f-string. */ |
| 5502 | while (1) { |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5503 | PyObject *literal = NULL; |
| 5504 | PyObject *expr_text = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5505 | expr_ty expression = NULL; |
| 5506 | |
| 5507 | /* If there's a zero length literal in front of the |
| 5508 | expression, literal will be NULL. If we're at the end of |
| 5509 | the f-string, expression will be NULL (unless result == 1, |
| 5510 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5511 | int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5512 | &literal, &expr_text, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5513 | &expression, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5514 | if (result < 0) |
| 5515 | return -1; |
| 5516 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5517 | /* Add the literal, if any. */ |
| 5518 | if (literal && FstringParser_ConcatAndDel(state, literal) < 0) { |
| 5519 | Py_XDECREF(expr_text); |
| 5520 | return -1; |
| 5521 | } |
| 5522 | /* Add the expr_text, if any. */ |
| 5523 | if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) { |
| 5524 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5525 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5526 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5527 | /* We've dealt with the literal and expr_text, their ownership has |
| 5528 | been transferred to the state object. Don't look at them again. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5529 | |
| 5530 | /* See if we should just loop around to get the next literal |
| 5531 | and expression, while ignoring the expression this |
| 5532 | time. This is used for un-doubling braces, as an |
| 5533 | optimization. */ |
| 5534 | if (result == 1) |
| 5535 | continue; |
| 5536 | |
| 5537 | if (!expression) |
| 5538 | /* We're done with this f-string. */ |
| 5539 | break; |
| 5540 | |
| 5541 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5542 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5543 | if (!state->last_str) { |
| 5544 | /* Do nothing. No previous literal. */ |
| 5545 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5546 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5547 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5548 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5549 | return -1; |
| 5550 | } |
| 5551 | |
| 5552 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5553 | return -1; |
| 5554 | } |
| 5555 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5556 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5557 | string. Otherwise, we must be at a right brace. */ |
| 5558 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5559 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5560 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5561 | return -1; |
| 5562 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5563 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5564 | ast_error(c, n, "f-string: expecting '}'"); |
| 5565 | return -1; |
| 5566 | } |
| 5567 | |
| 5568 | FstringParser_check_invariants(state); |
| 5569 | return 0; |
| 5570 | } |
| 5571 | |
| 5572 | /* 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] | 5573 | 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] | 5574 | static expr_ty |
| 5575 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5576 | const node *n) |
| 5577 | { |
| 5578 | asdl_seq *seq; |
| 5579 | |
| 5580 | FstringParser_check_invariants(state); |
| 5581 | |
| 5582 | /* If we're just a constant string with no expressions, return |
| 5583 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5584 | if (!state->fmode) { |
| 5585 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5586 | if (!state->last_str) { |
| 5587 | /* Create a zero length string. */ |
| 5588 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5589 | if (!state->last_str) |
| 5590 | goto error; |
| 5591 | } |
| 5592 | return make_str_node_and_del(&state->last_str, c, n); |
| 5593 | } |
| 5594 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5595 | /* 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] | 5596 | last node in our expression list. */ |
| 5597 | if (state->last_str) { |
| 5598 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5599 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5600 | goto error; |
| 5601 | } |
| 5602 | /* This has already been freed. */ |
| 5603 | assert(state->last_str == NULL); |
| 5604 | |
| 5605 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5606 | if (!seq) |
| 5607 | goto error; |
| 5608 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5609 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5610 | 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] | 5611 | |
| 5612 | error: |
| 5613 | FstringParser_Dealloc(state); |
| 5614 | return NULL; |
| 5615 | } |
| 5616 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5617 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5618 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5619 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5620 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5621 | 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] | 5622 | struct compiling *c, const node *n) |
| 5623 | { |
| 5624 | FstringParser state; |
| 5625 | |
| 5626 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5627 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5628 | c, n) < 0) { |
| 5629 | FstringParser_Dealloc(&state); |
| 5630 | return NULL; |
| 5631 | } |
| 5632 | |
| 5633 | return FstringParser_Finish(&state, c, n); |
| 5634 | } |
| 5635 | |
| 5636 | /* n is a Python string literal, including the bracketing quote |
| 5637 | 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] | 5638 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5639 | 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] | 5640 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5641 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5642 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5643 | static int |
| 5644 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5645 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5646 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5647 | size_t len; |
| 5648 | const char *s = STR(n); |
| 5649 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5650 | int fmode = 0; |
| 5651 | *bytesmode = 0; |
| 5652 | *rawmode = 0; |
| 5653 | *result = NULL; |
| 5654 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5655 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5656 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5657 | if (quote == 'b' || quote == 'B') { |
| 5658 | quote = *++s; |
| 5659 | *bytesmode = 1; |
| 5660 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5661 | else if (quote == 'u' || quote == 'U') { |
| 5662 | quote = *++s; |
| 5663 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5664 | else if (quote == 'r' || quote == 'R') { |
| 5665 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5666 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5667 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5668 | else if (quote == 'f' || quote == 'F') { |
| 5669 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5670 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5671 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5672 | else { |
| 5673 | break; |
| 5674 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5675 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5676 | } |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 5677 | |
| 5678 | /* fstrings are only allowed in Python 3.6 and greater */ |
| 5679 | if (fmode && c->c_feature_version < 6) { |
| 5680 | ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); |
| 5681 | return -1; |
| 5682 | } |
| 5683 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5684 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5685 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5686 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5687 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5688 | if (quote != '\'' && quote != '\"') { |
| 5689 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5690 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5691 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5692 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5693 | s++; |
| 5694 | len = strlen(s); |
| 5695 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5697 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5698 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5699 | } |
| 5700 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5701 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5702 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5703 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5704 | } |
| 5705 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5706 | /* A triple quoted string. We've already skipped one quote at |
| 5707 | the start and one at the end of the string. Now skip the |
| 5708 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5709 | s += 2; |
| 5710 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5711 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5712 | if (s[--len] != quote || s[--len] != quote) { |
| 5713 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5714 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5715 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5716 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5717 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5718 | if (fmode) { |
| 5719 | /* Just return the bytes. The caller will parse the resulting |
| 5720 | string. */ |
| 5721 | *fstr = s; |
| 5722 | *fstrlen = len; |
| 5723 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5724 | } |
| 5725 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5726 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5727 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5728 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5729 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5730 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5731 | const char *ch; |
| 5732 | for (ch = s; *ch; ch++) { |
| 5733 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5734 | ast_error(c, n, |
| 5735 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5736 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5737 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5738 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5739 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5740 | if (*rawmode) |
| 5741 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5742 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5743 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5744 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5745 | if (*rawmode) |
| 5746 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5747 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5748 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5749 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5750 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5753 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5754 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5755 | 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] | 5756 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5757 | 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] | 5758 | node if there's just an f-string (with no leading or trailing |
| 5759 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5760 | any literals involved. */ |
| 5761 | static expr_ty |
| 5762 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5763 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5764 | int bytesmode = 0; |
| 5765 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5766 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5767 | |
| 5768 | FstringParser state; |
| 5769 | FstringParser_Init(&state); |
| 5770 | |
| 5771 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5772 | int this_bytesmode; |
| 5773 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5774 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5775 | const char *fstr; |
| 5776 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5777 | |
| 5778 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5779 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5780 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5781 | goto error; |
| 5782 | |
| 5783 | /* Check that we're not mixing bytes with unicode. */ |
| 5784 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5785 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5786 | /* s is NULL if the current string part is an f-string. */ |
| 5787 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5788 | goto error; |
| 5789 | } |
| 5790 | bytesmode = this_bytesmode; |
| 5791 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5792 | if (fstr != NULL) { |
| 5793 | int result; |
| 5794 | assert(s == NULL && !bytesmode); |
| 5795 | /* This is an f-string. Parse and concatenate it. */ |
| 5796 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5797 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5798 | if (result < 0) |
| 5799 | goto error; |
| 5800 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5801 | /* A string or byte string. */ |
| 5802 | assert(s != NULL && fstr == NULL); |
| 5803 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5804 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5805 | PyUnicode_CheckExact(s)); |
| 5806 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5807 | if (bytesmode) { |
| 5808 | /* For bytes, concat as we go. */ |
| 5809 | if (i == 0) { |
| 5810 | /* First time, just remember this value. */ |
| 5811 | bytes_str = s; |
| 5812 | } else { |
| 5813 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5814 | if (!bytes_str) |
| 5815 | goto error; |
| 5816 | } |
| 5817 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5818 | /* This is a regular string. Concatenate it. */ |
| 5819 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5820 | goto error; |
| 5821 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5822 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5823 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5824 | if (bytesmode) { |
| 5825 | /* Just return the bytes object and we're done. */ |
| 5826 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5827 | goto error; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5828 | return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5829 | 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] | 5830 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5831 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5832 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5833 | assert(bytes_str == NULL); |
| 5834 | |
| 5835 | return FstringParser_Finish(&state, c, n); |
| 5836 | |
| 5837 | error: |
| 5838 | Py_XDECREF(bytes_str); |
| 5839 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5840 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5841 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5842 | |
| 5843 | PyObject * |
| 5844 | _PyAST_GetDocString(asdl_seq *body) |
| 5845 | { |
| 5846 | if (!asdl_seq_LEN(body)) { |
| 5847 | return NULL; |
| 5848 | } |
| 5849 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5850 | if (st->kind != Expr_kind) { |
| 5851 | return NULL; |
| 5852 | } |
| 5853 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5854 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5855 | return e->v.Constant.value; |
| 5856 | } |
| 5857 | return NULL; |
| 5858 | } |