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 | 77f0ed7 | 2019-05-28 16:44:58 -0700 | [diff] [blame] | 780 | c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 781 | |
| 782 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 785 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 786 | switch (TYPE(n)) { |
| 787 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 788 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 790 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | for (i = 0; i < NCH(n) - 1; i++) { |
| 792 | ch = CHILD(n, i); |
| 793 | if (TYPE(ch) == NEWLINE) |
| 794 | continue; |
| 795 | REQ(ch, stmt); |
| 796 | num = num_stmts(ch); |
| 797 | if (num == 1) { |
| 798 | s = ast_for_stmt(&c, ch); |
| 799 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 800 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 801 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 802 | } |
| 803 | else { |
| 804 | ch = CHILD(ch, 0); |
| 805 | REQ(ch, simple_stmt); |
| 806 | for (j = 0; j < num; j++) { |
| 807 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 808 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 809 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 810 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 814 | |
| 815 | /* Type ignores are stored under the ENDMARKER in file_input. */ |
| 816 | ch = CHILD(n, NCH(n) - 1); |
| 817 | REQ(ch, ENDMARKER); |
| 818 | num = NCH(ch); |
| 819 | type_ignores = _Py_asdl_seq_new(num, arena); |
| 820 | if (!type_ignores) |
| 821 | goto out; |
| 822 | |
| 823 | for (i = 0; i < num; i++) { |
Michael J. Sullivan | 933e150 | 2019-05-22 07:54:20 -0700 | [diff] [blame] | 824 | string type_comment = new_type_comment(STR(CHILD(ch, i)), &c); |
| 825 | if (!type_comment) |
| 826 | goto out; |
| 827 | 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] | 828 | if (!ti) |
| 829 | goto out; |
| 830 | asdl_seq_SET(type_ignores, i, ti); |
| 831 | } |
| 832 | |
| 833 | res = Module(stmts, type_ignores, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 834 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 835 | case eval_input: { |
| 836 | expr_ty testlist_ast; |
| 837 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 838 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 839 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 840 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 841 | goto out; |
| 842 | res = Expression(testlist_ast, arena); |
| 843 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | } |
| 845 | case single_input: |
| 846 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 847 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 848 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 849 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 850 | 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] | 851 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 852 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 853 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 854 | goto out; |
| 855 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | } |
| 857 | else { |
| 858 | n = CHILD(n, 0); |
| 859 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 860 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 862 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 863 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 864 | s = ast_for_stmt(&c, n); |
| 865 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 866 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 867 | asdl_seq_SET(stmts, 0, s); |
| 868 | } |
| 869 | else { |
| 870 | /* Only a simple_stmt can contain multiple statements. */ |
| 871 | REQ(n, simple_stmt); |
| 872 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 874 | break; |
| 875 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 876 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 877 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 878 | asdl_seq_SET(stmts, i / 2, s); |
| 879 | } |
| 880 | } |
| 881 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 882 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 883 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 884 | break; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 885 | case func_type_input: |
| 886 | n = CHILD(n, 0); |
| 887 | REQ(n, func_type); |
| 888 | |
| 889 | if (TYPE(CHILD(n, 1)) == typelist) { |
| 890 | ch = CHILD(n, 1); |
| 891 | /* this is overly permissive -- we don't pay any attention to |
| 892 | * stars on the args -- just parse them into an ordered list */ |
| 893 | num = 0; |
| 894 | for (i = 0; i < NCH(ch); i++) { |
| 895 | if (TYPE(CHILD(ch, i)) == test) { |
| 896 | num++; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | argtypes = _Py_asdl_seq_new(num, arena); |
| 901 | if (!argtypes) |
| 902 | goto out; |
| 903 | |
| 904 | j = 0; |
| 905 | for (i = 0; i < NCH(ch); i++) { |
| 906 | if (TYPE(CHILD(ch, i)) == test) { |
| 907 | arg = ast_for_expr(&c, CHILD(ch, i)); |
| 908 | if (!arg) |
| 909 | goto out; |
| 910 | asdl_seq_SET(argtypes, j++, arg); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | else { |
| 915 | argtypes = _Py_asdl_seq_new(0, arena); |
| 916 | if (!argtypes) |
| 917 | goto out; |
| 918 | } |
| 919 | |
| 920 | ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); |
| 921 | if (!ret) |
| 922 | goto out; |
| 923 | res = FunctionType(argtypes, ret, arena); |
| 924 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 925 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 926 | PyErr_Format(PyExc_SystemError, |
| 927 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 928 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 930 | out: |
| 931 | if (c.c_normalize) { |
| 932 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 933 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 934 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 937 | mod_ty |
| 938 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 939 | PyArena *arena) |
| 940 | { |
| 941 | mod_ty mod; |
| 942 | PyObject *filename; |
| 943 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 944 | if (filename == NULL) |
| 945 | return NULL; |
| 946 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 947 | Py_DECREF(filename); |
| 948 | return mod; |
| 949 | |
| 950 | } |
| 951 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 952 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 953 | */ |
| 954 | |
| 955 | static operator_ty |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 956 | get_operator(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 957 | { |
| 958 | switch (TYPE(n)) { |
| 959 | case VBAR: |
| 960 | return BitOr; |
| 961 | case CIRCUMFLEX: |
| 962 | return BitXor; |
| 963 | case AMPER: |
| 964 | return BitAnd; |
| 965 | case LEFTSHIFT: |
| 966 | return LShift; |
| 967 | case RIGHTSHIFT: |
| 968 | return RShift; |
| 969 | case PLUS: |
| 970 | return Add; |
| 971 | case MINUS: |
| 972 | return Sub; |
| 973 | case STAR: |
| 974 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 975 | case AT: |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 976 | if (c->c_feature_version < 5) { |
| 977 | ast_error(c, n, |
| 978 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 979 | return (operator_ty)0; |
| 980 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 981 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 982 | case SLASH: |
| 983 | return Div; |
| 984 | case DOUBLESLASH: |
| 985 | return FloorDiv; |
| 986 | case PERCENT: |
| 987 | return Mod; |
| 988 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 989 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 993 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 994 | "None", |
| 995 | "True", |
| 996 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 997 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 998 | NULL, |
| 999 | }; |
| 1000 | |
| 1001 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1002 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 1003 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1004 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 1005 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1006 | const char * const *p = FORBIDDEN; |
| 1007 | if (!full_checks) { |
| 1008 | /* In most cases, the parser will protect True, False, and None |
| 1009 | from being assign to. */ |
| 1010 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1011 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1012 | for (; *p; p++) { |
| 1013 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 1014 | ast_error(c, n, "cannot assign to %U", name); |
| 1015 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1021 | static expr_ty |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 1022 | copy_location(expr_ty e, const node *n, const node *end) |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1023 | { |
| 1024 | if (e) { |
| 1025 | e->lineno = LINENO(n); |
| 1026 | e->col_offset = n->n_col_offset; |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 1027 | e->end_lineno = end->n_end_lineno; |
| 1028 | e->end_col_offset = end->n_end_col_offset; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1029 | } |
| 1030 | return e; |
| 1031 | } |
| 1032 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1033 | static const char * |
| 1034 | get_expr_name(expr_ty e) |
| 1035 | { |
| 1036 | switch (e->kind) { |
| 1037 | case Attribute_kind: |
| 1038 | return "attribute"; |
| 1039 | case Subscript_kind: |
| 1040 | return "subscript"; |
| 1041 | case Starred_kind: |
| 1042 | return "starred"; |
| 1043 | case Name_kind: |
| 1044 | return "name"; |
| 1045 | case List_kind: |
| 1046 | return "list"; |
| 1047 | case Tuple_kind: |
| 1048 | return "tuple"; |
| 1049 | case Lambda_kind: |
| 1050 | return "lambda"; |
| 1051 | case Call_kind: |
| 1052 | return "function call"; |
| 1053 | case BoolOp_kind: |
| 1054 | case BinOp_kind: |
| 1055 | case UnaryOp_kind: |
| 1056 | return "operator"; |
| 1057 | case GeneratorExp_kind: |
| 1058 | return "generator expression"; |
| 1059 | case Yield_kind: |
| 1060 | case YieldFrom_kind: |
| 1061 | return "yield expression"; |
| 1062 | case Await_kind: |
| 1063 | return "await expression"; |
| 1064 | case ListComp_kind: |
| 1065 | return "list comprehension"; |
| 1066 | case SetComp_kind: |
| 1067 | return "set comprehension"; |
| 1068 | case DictComp_kind: |
| 1069 | return "dict comprehension"; |
| 1070 | case Dict_kind: |
| 1071 | return "dict display"; |
| 1072 | case Set_kind: |
| 1073 | return "set display"; |
| 1074 | case JoinedStr_kind: |
| 1075 | case FormattedValue_kind: |
| 1076 | return "f-string expression"; |
| 1077 | case Constant_kind: { |
| 1078 | PyObject *value = e->v.Constant.value; |
| 1079 | if (value == Py_None) { |
| 1080 | return "None"; |
| 1081 | } |
| 1082 | if (value == Py_False) { |
| 1083 | return "False"; |
| 1084 | } |
| 1085 | if (value == Py_True) { |
| 1086 | return "True"; |
| 1087 | } |
| 1088 | if (value == Py_Ellipsis) { |
| 1089 | return "Ellipsis"; |
| 1090 | } |
| 1091 | return "literal"; |
| 1092 | } |
| 1093 | case Compare_kind: |
| 1094 | return "comparison"; |
| 1095 | case IfExp_kind: |
| 1096 | return "conditional expression"; |
| 1097 | case NamedExpr_kind: |
| 1098 | return "named expression"; |
| 1099 | default: |
| 1100 | PyErr_Format(PyExc_SystemError, |
| 1101 | "unexpected expression in assignment %d (line %d)", |
| 1102 | e->kind, e->lineno); |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | } |
| 1106 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1107 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1108 | |
| 1109 | Only sets context for expr kinds that "can appear in assignment context" |
| 1110 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 1111 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1112 | */ |
| 1113 | |
| 1114 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1115 | 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] | 1116 | { |
| 1117 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1118 | |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 1119 | /* Expressions in an augmented assignment have a Store context. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1120 | |
| 1121 | switch (e->kind) { |
| 1122 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1123 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1124 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1125 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1126 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1127 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1128 | e->v.Subscript.ctx = ctx; |
| 1129 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1130 | case Starred_kind: |
| 1131 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1132 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1133 | return 0; |
| 1134 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1135 | case Name_kind: |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1136 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1137 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1138 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1139 | } |
| 1140 | e->v.Name.ctx = ctx; |
| 1141 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1142 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1143 | e->v.List.ctx = ctx; |
| 1144 | s = e->v.List.elts; |
| 1145 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 1147 | e->v.Tuple.ctx = ctx; |
| 1148 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1149 | break; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1150 | default: { |
| 1151 | const char *expr_name = get_expr_name(e); |
| 1152 | if (expr_name != NULL) { |
| 1153 | ast_error(c, n, "cannot %s %s", |
| 1154 | ctx == Store ? "assign to" : "delete", |
| 1155 | expr_name); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1156 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1157 | return 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1158 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1161 | /* 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] | 1162 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1163 | */ |
| 1164 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1165 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1166 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1167 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1168 | 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] | 1169 | return 0; |
| 1170 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | } |
| 1172 | return 1; |
| 1173 | } |
| 1174 | |
| 1175 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1176 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 | { |
| 1178 | REQ(n, augassign); |
| 1179 | n = CHILD(n, 0); |
| 1180 | switch (STR(n)[0]) { |
| 1181 | case '+': |
| 1182 | return Add; |
| 1183 | case '-': |
| 1184 | return Sub; |
| 1185 | case '/': |
| 1186 | if (STR(n)[1] == '/') |
| 1187 | return FloorDiv; |
| 1188 | else |
| 1189 | return Div; |
| 1190 | case '%': |
| 1191 | return Mod; |
| 1192 | case '<': |
| 1193 | return LShift; |
| 1194 | case '>': |
| 1195 | return RShift; |
| 1196 | case '&': |
| 1197 | return BitAnd; |
| 1198 | case '^': |
| 1199 | return BitXor; |
| 1200 | case '|': |
| 1201 | return BitOr; |
| 1202 | case '*': |
| 1203 | if (STR(n)[1] == '*') |
| 1204 | return Pow; |
| 1205 | else |
| 1206 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1207 | case '@': |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1208 | if (c->c_feature_version < 5) { |
| 1209 | ast_error(c, n, |
| 1210 | "The '@' operator is only supported in Python 3.5 and greater"); |
| 1211 | return (operator_ty)0; |
| 1212 | } |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1213 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1214 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1215 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1216 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1221 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1222 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1223 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1224 | |'is' 'not' |
| 1225 | */ |
| 1226 | REQ(n, comp_op); |
| 1227 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1228 | n = CHILD(n, 0); |
| 1229 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1230 | case LESS: |
| 1231 | return Lt; |
| 1232 | case GREATER: |
| 1233 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1234 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1235 | return Eq; |
| 1236 | case LESSEQUAL: |
| 1237 | return LtE; |
| 1238 | case GREATEREQUAL: |
| 1239 | return GtE; |
| 1240 | case NOTEQUAL: |
| 1241 | return NotEq; |
| 1242 | case NAME: |
| 1243 | if (strcmp(STR(n), "in") == 0) |
| 1244 | return In; |
| 1245 | if (strcmp(STR(n), "is") == 0) |
| 1246 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1247 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1249 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1250 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1251 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1252 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1253 | } |
| 1254 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1255 | /* handle "not in" and "is not" */ |
| 1256 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1257 | case NAME: |
| 1258 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1259 | return NotIn; |
| 1260 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1261 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1262 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1263 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1264 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1265 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1266 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1267 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1268 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1269 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1270 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1271 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | static asdl_seq * |
| 1275 | seq_for_testlist(struct compiling *c, const node *n) |
| 1276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1278 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1279 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1280 | asdl_seq *seq; |
| 1281 | expr_ty expression; |
| 1282 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1283 | 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] | 1284 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1285 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1286 | if (!seq) |
| 1287 | return NULL; |
| 1288 | |
| 1289 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | const node *ch = CHILD(n, i); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1291 | 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] | 1292 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1293 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1294 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1295 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1296 | |
| 1297 | assert(i / 2 < seq->size); |
| 1298 | asdl_seq_SET(seq, i / 2, expression); |
| 1299 | } |
| 1300 | return seq; |
| 1301 | } |
| 1302 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1303 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1304 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1305 | { |
| 1306 | identifier name; |
| 1307 | expr_ty annotation = NULL; |
| 1308 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1309 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1310 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1311 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1312 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1313 | name = NEW_IDENTIFIER(ch); |
| 1314 | if (!name) |
| 1315 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1316 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1317 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1318 | |
| 1319 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1320 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1321 | if (!annotation) |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1325 | ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1326 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1327 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1328 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1329 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1332 | /* returns -1 if failed to handle keyword only arguments |
| 1333 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1334 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1335 | ^^^ |
| 1336 | start pointing here |
| 1337 | */ |
| 1338 | static int |
| 1339 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1340 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1341 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1342 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1343 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1344 | expr_ty expression, annotation; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1345 | arg_ty arg = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1346 | int i = start; |
| 1347 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1348 | |
| 1349 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1350 | 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] | 1351 | return -1; |
| 1352 | } |
| 1353 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1354 | while (i < NCH(n)) { |
| 1355 | ch = CHILD(n, i); |
| 1356 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1357 | case vfpdef: |
| 1358 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1359 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1360 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1361 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1362 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1363 | asdl_seq_SET(kwdefaults, j, expression); |
| 1364 | i += 2; /* '=' and test */ |
| 1365 | } |
| 1366 | else { /* setting NULL if no default value exists */ |
| 1367 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1368 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1369 | if (NCH(ch) == 3) { |
| 1370 | /* ch is NAME ':' test */ |
| 1371 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1372 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1373 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1374 | } |
| 1375 | else { |
| 1376 | annotation = NULL; |
| 1377 | } |
| 1378 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1379 | argname = NEW_IDENTIFIER(ch); |
| 1380 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1381 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1382 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1383 | goto error; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1384 | arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1385 | ch->n_end_lineno, ch->n_end_col_offset, |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1386 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1387 | if (!arg) |
| 1388 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1389 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1390 | i += 1; /* the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1391 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1392 | i += 1; /* the comma, if present */ |
| 1393 | break; |
| 1394 | case TYPE_COMMENT: |
| 1395 | /* arg will be equal to the last argument processed */ |
| 1396 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1397 | if (!arg->type_comment) |
| 1398 | goto error; |
| 1399 | i += 1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | case DOUBLESTAR: |
| 1402 | return i; |
| 1403 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1404 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1405 | goto error; |
| 1406 | } |
| 1407 | } |
| 1408 | return i; |
| 1409 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1411 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1412 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1413 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1414 | |
| 1415 | static arguments_ty |
| 1416 | ast_for_arguments(struct compiling *c, const node *n) |
| 1417 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1418 | /* This function handles both typedargslist (function definition) |
| 1419 | and varargslist (lambda definition). |
| 1420 | |
| 1421 | parameters: '(' [typedargslist] ')' |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1422 | |
| 1423 | The following definition for typedarglist is equivalent to this set of rules: |
| 1424 | |
| 1425 | arguments = argument (',' [TYPE_COMMENT] argument)* |
| 1426 | argument = tfpdef ['=' test] |
| 1427 | kwargs = '**' tfpdef [','] [TYPE_COMMENT] |
| 1428 | args = '*' [tfpdef] |
| 1429 | kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' |
| 1430 | [TYPE_COMMENT] [kwargs]]) |
| 1431 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1432 | poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' |
| 1433 | [TYPE_COMMENT] [args_kwonly_kwargs]]) |
| 1434 | typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1435 | typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] |
| 1436 | typedargslist_no_posonly]])|(typedargslist_no_posonly)" |
| 1437 | |
| 1438 | typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1439 | ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ',' |
| 1440 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1441 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1442 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1443 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1444 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1445 | '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (',' |
| 1446 | [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' |
| 1447 | [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' |
| 1448 | [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] |
| 1449 | [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* |
| 1450 | (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | |
| 1451 | '**' tfpdef [','] [TYPE_COMMENT])) |
| 1452 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1453 | tfpdef: NAME [':' test] |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1454 | |
| 1455 | The following definition for varargslist is equivalent to this set of rules: |
| 1456 | |
| 1457 | arguments = argument (',' argument )* |
| 1458 | argument = vfpdef ['=' test] |
| 1459 | kwargs = '**' vfpdef [','] |
| 1460 | args = '*' [vfpdef] |
| 1461 | kwonly_kwargs = (',' argument )* [',' [kwargs]] |
| 1462 | args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 1463 | poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 1464 | vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 1465 | varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | |
| 1466 | (vararglist_no_posonly) |
| 1467 | |
| 1468 | varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' |
| 1469 | test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' |
| 1470 | ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* |
| 1471 | [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef |
| 1472 | ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1473 | | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef |
| 1474 | [',']]] | '**' vfpdef [',']) |
| 1475 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1476 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1477 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1478 | */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1479 | int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1480 | int nposdefaults = 0, found_default = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1481 | asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1482 | arg_ty vararg = NULL, kwarg = NULL; |
Pablo Galindo | 164686f | 2019-02-10 20:37:07 +0000 | [diff] [blame] | 1483 | arg_ty arg = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1484 | node *ch; |
| 1485 | |
| 1486 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1487 | if (NCH(n) == 2) /* () as argument list */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1488 | 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] | 1489 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1491 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1492 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1493 | /* First count the number of positional args & defaults. The |
| 1494 | variable i is the loop index for this for loop and the next. |
| 1495 | The next loop picks up where the first leaves off. |
| 1496 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1497 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1498 | ch = CHILD(n, i); |
| 1499 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1500 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1501 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1502 | if (i < NCH(n) && /* skip argument following star */ |
| 1503 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1504 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1505 | i++; |
| 1506 | } |
| 1507 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1508 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1509 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1510 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1511 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1512 | if (TYPE(ch) == SLASH ) { |
| 1513 | nposonlyargs = nposargs; |
| 1514 | nposargs = 0; |
| 1515 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1518 | defaults for keyword only args */ |
| 1519 | for ( ; i < NCH(n); ++i) { |
| 1520 | ch = CHILD(n, i); |
| 1521 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1522 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1523 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1524 | posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL); |
| 1525 | if (!posonlyargs && nposonlyargs) { |
| 1526 | return NULL; |
| 1527 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1528 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1529 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1530 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1531 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1532 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1533 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1534 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1536 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1537 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1538 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1540 | since we set NULL as default for keyword only argument w/o default |
| 1541 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1542 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1543 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1544 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1545 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1546 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1547 | /* tfpdef: NAME [':' test] |
| 1548 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1549 | */ |
| 1550 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1551 | j = 0; /* index for defaults */ |
| 1552 | k = 0; /* index for args */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1553 | l = 0; /* index for posonlyargs */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1554 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1555 | ch = CHILD(n, i); |
| 1556 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1557 | case tfpdef: |
| 1558 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1559 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1560 | anything other than EQUAL or a comma? */ |
| 1561 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1562 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1563 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1564 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1565 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1566 | assert(posdefaults != NULL); |
| 1567 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1568 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1569 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1570 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1571 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1572 | ast_error(c, n, |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1573 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1574 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1575 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1576 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1577 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1578 | return NULL; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1579 | if (l < nposonlyargs) { |
| 1580 | asdl_seq_SET(posonlyargs, l++, arg); |
| 1581 | } else { |
| 1582 | asdl_seq_SET(posargs, k++, arg); |
| 1583 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1584 | i += 1; /* the name */ |
| 1585 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1586 | i += 1; /* the comma, if present */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1587 | break; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1588 | case SLASH: |
| 1589 | /* Advance the slash and the comma. If there are more names |
| 1590 | * after the slash there will be a comma so we are advancing |
| 1591 | * the correct number of nodes. If the slash is the last item, |
| 1592 | * we will be advancing an extra token but then * i > NCH(n) |
| 1593 | * and the enclosing while will finish correctly. */ |
| 1594 | i += 2; |
| 1595 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1596 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1597 | if (i+1 >= NCH(n) || |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1598 | (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA |
| 1599 | || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1600 | ast_error(c, CHILD(n, i), |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1601 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1602 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1603 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1604 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1605 | if (TYPE(ch) == COMMA) { |
| 1606 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1607 | i += 2; /* now follows keyword only arguments */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1608 | |
| 1609 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1610 | ast_error(c, CHILD(n, i), |
| 1611 | "bare * has associated type comment"); |
| 1612 | return NULL; |
| 1613 | } |
| 1614 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1615 | res = handle_keywordonly_args(c, n, i, |
| 1616 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1617 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1618 | i = res; /* res has new position to process */ |
| 1619 | } |
| 1620 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1621 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1622 | if (!vararg) |
| 1623 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1624 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1625 | i += 2; /* the star and the name */ |
| 1626 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
| 1627 | i += 1; /* the comma, if present */ |
| 1628 | |
| 1629 | if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { |
| 1630 | vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); |
| 1631 | if (!vararg->type_comment) |
| 1632 | return NULL; |
| 1633 | i += 1; |
| 1634 | } |
| 1635 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1636 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1637 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1638 | int res = 0; |
| 1639 | res = handle_keywordonly_args(c, n, i, |
| 1640 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1641 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1642 | i = res; /* res has new position to process */ |
| 1643 | } |
| 1644 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1645 | break; |
| 1646 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1647 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1648 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1649 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1650 | if (!kwarg) |
| 1651 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1652 | i += 2; /* the double star and the name */ |
Brad Larsen | a4d7836 | 2019-04-01 10:36:05 -0400 | [diff] [blame] | 1653 | if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1654 | i += 1; /* the comma, if present */ |
| 1655 | break; |
| 1656 | case TYPE_COMMENT: |
| 1657 | assert(i); |
| 1658 | |
| 1659 | if (kwarg) |
| 1660 | arg = kwarg; |
| 1661 | |
| 1662 | /* arg will be equal to the last argument processed */ |
| 1663 | arg->type_comment = NEW_TYPE_COMMENT(ch); |
| 1664 | if (!arg->type_comment) |
| 1665 | return NULL; |
| 1666 | i += 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1667 | break; |
| 1668 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1669 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1670 | "unexpected node in varargslist: %d @ %d", |
| 1671 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1672 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1673 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1674 | } |
Pablo Galindo | cd6e83b | 2019-07-15 01:32:18 +0200 | [diff] [blame] | 1675 | return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1679 | ast_for_decorator(struct compiling *c, const node *n) |
| 1680 | { |
Brandt Bucher | be501ca | 2020-03-03 14:25:44 -0800 | [diff] [blame] | 1681 | /* decorator: '@' namedexpr_test NEWLINE */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1684 | REQ(CHILD(n, 0), AT); |
Brandt Bucher | be501ca | 2020-03-03 14:25:44 -0800 | [diff] [blame] | 1685 | REQ(CHILD(n, 2), NEWLINE); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1686 | |
Brandt Bucher | be501ca | 2020-03-03 14:25:44 -0800 | [diff] [blame] | 1687 | return ast_for_expr(c, CHILD(n, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | static asdl_seq* |
| 1691 | ast_for_decorators(struct compiling *c, const node *n) |
| 1692 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1693 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1694 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1695 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1697 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1698 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1699 | if (!decorator_seq) |
| 1700 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1702 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1703 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1704 | if (!d) |
| 1705 | return NULL; |
| 1706 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1707 | } |
| 1708 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1712 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1713 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1714 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1715 | /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1716 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1717 | identifier name; |
| 1718 | arguments_ty args; |
| 1719 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1720 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1721 | int name_i = 1; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1722 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1723 | node *tc; |
| 1724 | string type_comment = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1725 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1726 | if (is_async && c->c_feature_version < 5) { |
| 1727 | ast_error(c, n, |
| 1728 | "Async functions are only supported in Python 3.5 and greater"); |
| 1729 | return NULL; |
| 1730 | } |
| 1731 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | REQ(n, funcdef); |
| 1733 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1734 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1735 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1736 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1737 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1738 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1739 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1740 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1741 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1742 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1743 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1744 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1745 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1746 | name_i += 2; |
| 1747 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1748 | if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { |
| 1749 | type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); |
| 1750 | if (!type_comment) |
| 1751 | return NULL; |
| 1752 | name_i += 1; |
| 1753 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1754 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1755 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1756 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1757 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1758 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1759 | if (NCH(CHILD(n, name_i + 3)) > 1) { |
| 1760 | /* Check if the suite has a type comment in it. */ |
| 1761 | tc = CHILD(CHILD(n, name_i + 3), 1); |
| 1762 | |
| 1763 | if (TYPE(tc) == TYPE_COMMENT) { |
| 1764 | if (type_comment != NULL) { |
| 1765 | ast_error(c, n, "Cannot have two type comments on def"); |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | type_comment = NEW_TYPE_COMMENT(tc); |
| 1769 | if (!type_comment) |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | } |
| 1773 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1774 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1775 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1776 | 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] | 1777 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1778 | return FunctionDef(name, args, body, decorator_seq, returns, type_comment, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1779 | 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] | 1780 | } |
| 1781 | |
| 1782 | static stmt_ty |
| 1783 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1784 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1785 | /* async_funcdef: ASYNC funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1786 | REQ(n, async_funcdef); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1787 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1788 | REQ(CHILD(n, 1), funcdef); |
| 1789 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1790 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1791 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | static stmt_ty |
| 1795 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1796 | { |
| 1797 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1798 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1799 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | |
| 1803 | static stmt_ty |
| 1804 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1805 | { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1806 | /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1807 | REQ(n, async_stmt); |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1808 | REQ(CHILD(n, 0), ASYNC); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1809 | |
| 1810 | switch (TYPE(CHILD(n, 1))) { |
| 1811 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1812 | return ast_for_funcdef_impl(c, n, NULL, |
| 1813 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1814 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1815 | return ast_for_with_stmt(c, n, |
| 1816 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1817 | |
| 1818 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1819 | return ast_for_for_stmt(c, n, |
| 1820 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1821 | |
| 1822 | default: |
| 1823 | PyErr_Format(PyExc_SystemError, |
| 1824 | "invalid async stament: %s", |
| 1825 | STR(CHILD(n, 1))); |
| 1826 | return NULL; |
| 1827 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1830 | static stmt_ty |
| 1831 | ast_for_decorated(struct compiling *c, const node *n) |
| 1832 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1833 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1834 | stmt_ty thing = NULL; |
| 1835 | asdl_seq *decorator_seq = NULL; |
| 1836 | |
| 1837 | REQ(n, decorated); |
| 1838 | |
| 1839 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1840 | if (!decorator_seq) |
| 1841 | return NULL; |
| 1842 | |
| 1843 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1844 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1846 | |
| 1847 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1848 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1849 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1850 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1851 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1852 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1853 | } |
| 1854 | return thing; |
| 1855 | } |
| 1856 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1857 | static expr_ty |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1858 | ast_for_namedexpr(struct compiling *c, const node *n) |
| 1859 | { |
Guido van Rossum | b08d3f7 | 2019-12-15 10:00:33 -0800 | [diff] [blame] | 1860 | /* namedexpr_test: test [':=' test] |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1861 | argument: ( test [comp_for] | |
| 1862 | test ':=' test | |
| 1863 | test '=' test | |
| 1864 | '**' test | |
| 1865 | '*' test ) |
| 1866 | */ |
| 1867 | expr_ty target, value; |
| 1868 | |
| 1869 | target = ast_for_expr(c, CHILD(n, 0)); |
| 1870 | if (!target) |
| 1871 | return NULL; |
| 1872 | |
| 1873 | value = ast_for_expr(c, CHILD(n, 2)); |
| 1874 | if (!value) |
| 1875 | return NULL; |
| 1876 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1877 | if (target->kind != Name_kind) { |
| 1878 | const char *expr_name = get_expr_name(target); |
| 1879 | if (expr_name != NULL) { |
Ned Batchelder | 37143a8 | 2019-12-31 21:40:58 -0500 | [diff] [blame] | 1880 | ast_error(c, n, "cannot use assignment expressions with %s", expr_name); |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 1881 | } |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | |
| 1885 | if (!set_context(c, target, Store, n)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1886 | return NULL; |
| 1887 | |
| 1888 | return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, |
| 1889 | n->n_end_col_offset, c->c_arena); |
| 1890 | } |
| 1891 | |
| 1892 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1893 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1894 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1895 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1896 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1897 | arguments_ty args; |
| 1898 | expr_ty expression; |
| 1899 | |
| 1900 | if (NCH(n) == 3) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1901 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1902 | if (!args) |
| 1903 | return NULL; |
| 1904 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1905 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1907 | } |
| 1908 | else { |
| 1909 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1910 | if (!args) |
| 1911 | return NULL; |
| 1912 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1913 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1914 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1917 | return Lambda(args, expression, LINENO(n), n->n_col_offset, |
| 1918 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1921 | static expr_ty |
| 1922 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1925 | expr_ty expression, body, orelse; |
| 1926 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1927 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1928 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1929 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1930 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1931 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1932 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1933 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1934 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1935 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1936 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1937 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1938 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1939 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1942 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1943 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1944 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1945 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1946 | */ |
| 1947 | |
| 1948 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1949 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1950 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1951 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1953 | count_comp_for: |
| 1954 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1955 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1956 | if (NCH(n) == 2) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1957 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1958 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1959 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1960 | else if (NCH(n) == 1) { |
| 1961 | n = CHILD(n, 0); |
| 1962 | } |
| 1963 | else { |
| 1964 | goto error; |
| 1965 | } |
| 1966 | if (NCH(n) == (5)) { |
| 1967 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1968 | } |
| 1969 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1970 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1971 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1972 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1973 | REQ(n, comp_iter); |
| 1974 | n = CHILD(n, 0); |
| 1975 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1976 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1977 | else if (TYPE(n) == comp_if) { |
| 1978 | if (NCH(n) == 3) { |
| 1979 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1980 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1981 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1982 | else |
| 1983 | return n_fors; |
| 1984 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1985 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1986 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1987 | /* Should never be reached */ |
| 1988 | PyErr_SetString(PyExc_SystemError, |
| 1989 | "logic error in count_comp_fors"); |
| 1990 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1993 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1994 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1995 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1996 | */ |
| 1997 | |
| 1998 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1999 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2000 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2001 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2003 | while (1) { |
| 2004 | REQ(n, comp_iter); |
| 2005 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 2006 | return n_ifs; |
| 2007 | n = CHILD(n, 0); |
| 2008 | REQ(n, comp_if); |
| 2009 | n_ifs++; |
| 2010 | if (NCH(n) == 2) |
| 2011 | return n_ifs; |
| 2012 | n = CHILD(n, 2); |
| 2013 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2016 | static asdl_seq * |
| 2017 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2018 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2019 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2020 | asdl_seq *comps; |
| 2021 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2022 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2023 | if (n_fors == -1) |
| 2024 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2026 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2027 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2028 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2029 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2030 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2031 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2032 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 2033 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2034 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2035 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2036 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2038 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2040 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2041 | is_async = 1; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2042 | REQ(CHILD(n, 0), ASYNC); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2043 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2044 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2045 | else { |
| 2046 | sync_n = CHILD(n, 0); |
| 2047 | } |
| 2048 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2049 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2050 | /* Async comprehensions only allowed in Python 3.6 and greater */ |
| 2051 | if (is_async && c->c_feature_version < 6) { |
| 2052 | ast_error(c, n, |
| 2053 | "Async comprehensions are only supported in Python 3.6 and greater"); |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2057 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2058 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2059 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2060 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2061 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2062 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2063 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2064 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2065 | /* Check the # of children rather than the length of t, since |
| 2066 | (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] | 2067 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2068 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2069 | comp = comprehension(first, expression, NULL, |
| 2070 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2071 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2072 | comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
| 2073 | for_ch->n_end_lineno, for_ch->n_end_col_offset, |
| 2074 | c->c_arena), |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 2075 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2076 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2077 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2078 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2079 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2080 | int j, n_ifs; |
| 2081 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2083 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2084 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2085 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2086 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2087 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2088 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2089 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2090 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2091 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2092 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2093 | REQ(n, comp_iter); |
| 2094 | n = CHILD(n, 0); |
| 2095 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2097 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2098 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2099 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 2100 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2101 | if (NCH(n) == 3) |
| 2102 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2103 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2104 | /* on exit, must guarantee that n is a comp_for */ |
| 2105 | if (TYPE(n) == comp_iter) |
| 2106 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2107 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2108 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2109 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2110 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2111 | return comps; |
| 2112 | } |
| 2113 | |
| 2114 | static expr_ty |
| 2115 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 2116 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2117 | /* testlist_comp: (test|star_expr) |
| 2118 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2119 | expr_ty elt; |
| 2120 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2121 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2123 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2125 | ch = CHILD(n, 0); |
| 2126 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2127 | if (!elt) |
| 2128 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2129 | if (elt->kind == Starred_kind) { |
| 2130 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 2131 | return NULL; |
| 2132 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2134 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 2135 | if (!comps) |
| 2136 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2137 | |
| 2138 | if (type == COMP_GENEXP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2139 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, |
| 2140 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2141 | else if (type == COMP_LISTCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2142 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2143 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2144 | else if (type == COMP_SETCOMP) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2145 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, |
| 2146 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2147 | else |
| 2148 | /* Should never happen */ |
| 2149 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2152 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 2153 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 2154 | * elements. Iff successful, nonzero is returned. |
| 2155 | */ |
| 2156 | static int |
| 2157 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 2158 | expr_ty *key, expr_ty *value) |
| 2159 | { |
| 2160 | expr_ty expression; |
| 2161 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 2162 | assert(NCH(n) - *i >= 2); |
| 2163 | |
| 2164 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 2165 | if (!expression) |
| 2166 | return 0; |
| 2167 | *key = NULL; |
| 2168 | *value = expression; |
| 2169 | |
| 2170 | *i += 2; |
| 2171 | } |
| 2172 | else { |
| 2173 | assert(NCH(n) - *i >= 3); |
| 2174 | |
| 2175 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 2176 | if (!expression) |
| 2177 | return 0; |
| 2178 | *key = expression; |
| 2179 | |
| 2180 | REQ(CHILD(n, *i + 1), COLON); |
| 2181 | |
| 2182 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 2183 | if (!expression) |
| 2184 | return 0; |
| 2185 | *value = expression; |
| 2186 | |
| 2187 | *i += 3; |
| 2188 | } |
| 2189 | return 1; |
| 2190 | } |
| 2191 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2192 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2193 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 2194 | { |
| 2195 | expr_ty key, value; |
| 2196 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2197 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2199 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2200 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2201 | assert(key); |
| 2202 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2204 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2205 | if (!comps) |
| 2206 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2208 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, |
| 2209 | 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] | 2210 | } |
| 2211 | |
| 2212 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2213 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 2214 | { |
| 2215 | int i; |
| 2216 | int j; |
| 2217 | int size; |
| 2218 | asdl_seq *keys, *values; |
| 2219 | |
| 2220 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 2221 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2222 | if (!keys) |
| 2223 | return NULL; |
| 2224 | |
| 2225 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2226 | if (!values) |
| 2227 | return NULL; |
| 2228 | |
| 2229 | j = 0; |
| 2230 | for (i = 0; i < NCH(n); i++) { |
| 2231 | expr_ty key, value; |
| 2232 | |
| 2233 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2234 | return NULL; |
| 2235 | asdl_seq_SET(keys, j, key); |
| 2236 | asdl_seq_SET(values, j, value); |
| 2237 | |
| 2238 | j++; |
| 2239 | } |
| 2240 | keys->size = j; |
| 2241 | values->size = j; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2242 | return Dict(keys, values, LINENO(n), n->n_col_offset, |
| 2243 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2247 | ast_for_genexp(struct compiling *c, const node *n) |
| 2248 | { |
| 2249 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2250 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | static expr_ty |
| 2254 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2255 | { |
| 2256 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2257 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | static expr_ty |
| 2261 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2262 | { |
| 2263 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2264 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2267 | static expr_ty |
| 2268 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2269 | { |
| 2270 | int i; |
| 2271 | int size; |
| 2272 | asdl_seq *elts; |
| 2273 | |
| 2274 | assert(TYPE(n) == (dictorsetmaker)); |
| 2275 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2276 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2277 | if (!elts) |
| 2278 | return NULL; |
| 2279 | for (i = 0; i < NCH(n); i += 2) { |
| 2280 | expr_ty expression; |
| 2281 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2282 | if (!expression) |
| 2283 | return NULL; |
| 2284 | asdl_seq_SET(elts, i / 2, expression); |
| 2285 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2286 | return Set(elts, LINENO(n), n->n_col_offset, |
| 2287 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2288 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2289 | |
| 2290 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2291 | ast_for_atom(struct compiling *c, const node *n) |
| 2292 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2293 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2294 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2295 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2296 | */ |
| 2297 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2300 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2301 | PyObject *name; |
| 2302 | const char *s = STR(ch); |
| 2303 | size_t len = strlen(s); |
| 2304 | if (len >= 4 && len <= 5) { |
| 2305 | if (!strcmp(s, "None")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2306 | return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2307 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2308 | if (!strcmp(s, "True")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2309 | return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2310 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2311 | if (!strcmp(s, "False")) |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2312 | return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2313 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2314 | } |
| 2315 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2316 | if (!name) |
| 2317 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2318 | /* All names start in Load context, but may later be changed. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2319 | return Name(name, Load, LINENO(n), n->n_col_offset, |
| 2320 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2321 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2322 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2323 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2324 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2325 | const char *errtype = NULL; |
| 2326 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2327 | errtype = "unicode error"; |
| 2328 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2329 | errtype = "value error"; |
| 2330 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2331 | PyObject *type, *value, *tback, *errstr; |
| 2332 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2333 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2334 | if (errstr) { |
| 2335 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2336 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2337 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2338 | else { |
| 2339 | PyErr_Clear(); |
| 2340 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2341 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2342 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2343 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2344 | Py_XDECREF(tback); |
| 2345 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2346 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2347 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2348 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2349 | } |
| 2350 | case NUMBER: { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2351 | PyObject *pynum; |
| 2352 | /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ |
| 2353 | /* Check for underscores here rather than in parse_number so we can report a line number on error */ |
| 2354 | if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { |
| 2355 | ast_error(c, ch, |
| 2356 | "Underscores in numeric literals are only supported in Python 3.6 and greater"); |
| 2357 | return NULL; |
| 2358 | } |
| 2359 | pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2360 | if (!pynum) |
| 2361 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2362 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2363 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2364 | Py_DECREF(pynum); |
| 2365 | return NULL; |
| 2366 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2367 | return Constant(pynum, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2368 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2369 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2370 | case ELLIPSIS: /* Ellipsis */ |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 2371 | return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2372 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2373 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2374 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2376 | if (TYPE(ch) == RPAR) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2377 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, |
| 2378 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2380 | if (TYPE(ch) == yield_expr) |
| 2381 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2383 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2384 | if (NCH(ch) == 1) { |
| 2385 | return ast_for_testlist(c, ch); |
| 2386 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2387 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2388 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2389 | return copy_location(ast_for_genexp(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2390 | } |
| 2391 | else { |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2392 | return copy_location(ast_for_testlist(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2393 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2394 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2395 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2397 | if (TYPE(ch) == RSQB) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2398 | return List(NULL, Load, LINENO(n), n->n_col_offset, |
| 2399 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2401 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2402 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2403 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2404 | if (!elts) |
| 2405 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2406 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2407 | return List(elts, Load, LINENO(n), n->n_col_offset, |
| 2408 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2409 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2410 | else { |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2411 | return copy_location(ast_for_listcomp(c, ch), n, n); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2412 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2413 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2414 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2415 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2416 | * ((test | '*' test) |
| 2417 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2418 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2419 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2420 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2421 | /* It's an empty dict. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2422 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, |
| 2423 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2424 | } |
| 2425 | else { |
| 2426 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2427 | if (NCH(ch) == 1 || |
| 2428 | (NCH(ch) > 1 && |
| 2429 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2430 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2431 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2432 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2433 | else if (NCH(ch) > 1 && |
| 2434 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2435 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2436 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2437 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2438 | else if (NCH(ch) > 3 - is_dict && |
| 2439 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2440 | /* It's a dictionary comprehension. */ |
| 2441 | if (is_dict) { |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2442 | ast_error(c, n, |
| 2443 | "dict unpacking cannot be used in dict comprehension"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2444 | return NULL; |
| 2445 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2446 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2447 | } |
| 2448 | else { |
| 2449 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2450 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2451 | } |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 2452 | return copy_location(res, n, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2453 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2454 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2455 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2456 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2457 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2461 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2462 | ast_for_slice(struct compiling *c, const node *n) |
| 2463 | { |
| 2464 | node *ch; |
| 2465 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2466 | |
| 2467 | REQ(n, subscript); |
| 2468 | |
| 2469 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2470 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2471 | sliceop: ':' [test] |
| 2472 | */ |
| 2473 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2474 | if (NCH(n) == 1 && TYPE(ch) == test) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2475 | return ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2479 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2480 | if (!lower) |
| 2481 | return NULL; |
| 2482 | } |
| 2483 | |
| 2484 | /* If there's an upper bound it's in the second or third position. */ |
| 2485 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2486 | if (NCH(n) > 1) { |
| 2487 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2488 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2489 | if (TYPE(n2) == test) { |
| 2490 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2491 | if (!upper) |
| 2492 | return NULL; |
| 2493 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2494 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2495 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2496 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2497 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2498 | if (TYPE(n2) == test) { |
| 2499 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2500 | if (!upper) |
| 2501 | return NULL; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | ch = CHILD(n, NCH(n) - 1); |
| 2506 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2507 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2508 | ch = CHILD(ch, 1); |
| 2509 | if (TYPE(ch) == test) { |
| 2510 | step = ast_for_expr(c, ch); |
| 2511 | if (!step) |
| 2512 | return NULL; |
| 2513 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2517 | return Slice(lower, upper, step, LINENO(n), n->n_col_offset, |
| 2518 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | static expr_ty |
| 2522 | ast_for_binop(struct compiling *c, const node *n) |
| 2523 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2524 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2526 | BinOp(BinOp(A, op, B), op, C). |
| 2527 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2528 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2529 | int i, nops; |
| 2530 | expr_ty expr1, expr2, result; |
| 2531 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2532 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2533 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2534 | if (!expr1) |
| 2535 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2536 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2537 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2538 | if (!expr2) |
| 2539 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2540 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2541 | newoperator = get_operator(c, CHILD(n, 1)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2542 | if (!newoperator) |
| 2543 | return NULL; |
| 2544 | |
| 2545 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2546 | 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] | 2547 | c->c_arena); |
| 2548 | if (!result) |
| 2549 | return NULL; |
| 2550 | |
| 2551 | nops = (NCH(n) - 1) / 2; |
| 2552 | for (i = 1; i < nops; i++) { |
| 2553 | expr_ty tmp_result, tmp; |
| 2554 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2555 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2556 | newoperator = get_operator(c, next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2557 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2558 | return NULL; |
| 2559 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2560 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2561 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2562 | return NULL; |
| 2563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | tmp_result = BinOp(result, newoperator, tmp, |
Carl Friedrich Bolz-Tereick | 110a47c | 2019-07-08 23:17:56 +0200 | [diff] [blame] | 2565 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2566 | CHILD(n, i * 2 + 2)->n_end_lineno, |
| 2567 | CHILD(n, i * 2 + 2)->n_end_col_offset, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2568 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2570 | return NULL; |
| 2571 | result = tmp_result; |
| 2572 | } |
| 2573 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2576 | static expr_ty |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2577 | 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] | 2578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2580 | subscriptlist: subscript (',' subscript)* [','] |
| 2581 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2582 | */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2583 | const node *n_copy = n; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2584 | REQ(n, trailer); |
| 2585 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2586 | if (NCH(n) == 2) |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2587 | return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2588 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2589 | else |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2590 | return ast_for_call(c, CHILD(n, 1), left_expr, |
| 2591 | start, CHILD(n, 0), CHILD(n, 2)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2592 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2593 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2594 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2595 | if (!attr_id) |
| 2596 | return NULL; |
| 2597 | return Attribute(left_expr, attr_id, Load, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2598 | LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2599 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2600 | } |
| 2601 | else { |
| 2602 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2603 | REQ(CHILD(n, 2), RSQB); |
| 2604 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2605 | if (NCH(n) == 1) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2606 | expr_ty slc = ast_for_slice(c, CHILD(n, 0)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2607 | if (!slc) |
| 2608 | return NULL; |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2609 | return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2610 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2611 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2612 | } |
| 2613 | else { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2614 | int j; |
| 2615 | expr_ty slc, e; |
| 2616 | asdl_seq *elts; |
| 2617 | elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 2618 | if (!elts) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2619 | return NULL; |
| 2620 | for (j = 0; j < NCH(n); j += 2) { |
| 2621 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2622 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2623 | return NULL; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2624 | asdl_seq_SET(elts, j / 2, slc); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2625 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2626 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2627 | n->n_end_lineno, n->n_end_col_offset, |
| 2628 | c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2629 | if (!e) |
| 2630 | return NULL; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2631 | return Subscript(left_expr, e, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2632 | Load, LINENO(start), start->n_col_offset, |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 2633 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
| 2634 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2640 | ast_for_factor(struct compiling *c, const node *n) |
| 2641 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2642 | expr_ty expression; |
| 2643 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2644 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2645 | if (!expression) |
| 2646 | return NULL; |
| 2647 | |
| 2648 | switch (TYPE(CHILD(n, 0))) { |
| 2649 | case PLUS: |
| 2650 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2651 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2652 | c->c_arena); |
| 2653 | case MINUS: |
| 2654 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2655 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2656 | c->c_arena); |
| 2657 | case TILDE: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2658 | return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, |
| 2659 | n->n_end_lineno, n->n_end_col_offset, |
| 2660 | c->c_arena); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2661 | } |
| 2662 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2663 | TYPE(CHILD(n, 0))); |
| 2664 | return NULL; |
| 2665 | } |
| 2666 | |
| 2667 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2668 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2669 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2670 | int i, nch, start = 0; |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2671 | expr_ty e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2672 | |
| 2673 | REQ(n, atom_expr); |
| 2674 | nch = NCH(n); |
| 2675 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2676 | if (TYPE(CHILD(n, 0)) == AWAIT) { |
| 2677 | if (c->c_feature_version < 5) { |
| 2678 | ast_error(c, n, |
| 2679 | "Await expressions are only supported in Python 3.5 and greater"); |
| 2680 | return NULL; |
| 2681 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2682 | start = 1; |
| 2683 | assert(nch > 1); |
| 2684 | } |
| 2685 | |
| 2686 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2687 | if (!e) |
| 2688 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2689 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2690 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2691 | if (start && nch == 2) { |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2692 | return Await(e, LINENO(n), n->n_col_offset, |
| 2693 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2697 | node *ch = CHILD(n, i); |
| 2698 | if (TYPE(ch) != trailer) |
| 2699 | break; |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2700 | e = ast_for_trailer(c, ch, e, CHILD(n, start)); |
| 2701 | if (!e) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2702 | return NULL; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2703 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2704 | |
| 2705 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2706 | /* there was an 'await' */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2707 | return Await(e, LINENO(n), n->n_col_offset, |
| 2708 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2709 | } |
| 2710 | else { |
| 2711 | return e; |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | static expr_ty |
| 2716 | ast_for_power(struct compiling *c, const node *n) |
| 2717 | { |
| 2718 | /* power: atom trailer* ('**' factor)* |
| 2719 | */ |
| 2720 | expr_ty e; |
| 2721 | REQ(n, power); |
| 2722 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2723 | if (!e) |
| 2724 | return NULL; |
| 2725 | if (NCH(n) == 1) |
| 2726 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2727 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2728 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2729 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2730 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2731 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, |
| 2732 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2733 | } |
| 2734 | return e; |
| 2735 | } |
| 2736 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2737 | static expr_ty |
| 2738 | ast_for_starred(struct compiling *c, const node *n) |
| 2739 | { |
| 2740 | expr_ty tmp; |
| 2741 | REQ(n, star_expr); |
| 2742 | |
| 2743 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2744 | if (!tmp) |
| 2745 | return NULL; |
| 2746 | |
| 2747 | /* The Load context is changed later. */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2748 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, |
| 2749 | 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] | 2750 | } |
| 2751 | |
| 2752 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2753 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2754 | */ |
| 2755 | |
| 2756 | static expr_ty |
| 2757 | ast_for_expr(struct compiling *c, const node *n) |
| 2758 | { |
| 2759 | /* handle the full range of simple expressions |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2760 | namedexpr_test: test [':=' test] |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2761 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2762 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2764 | and_test: not_test ('and' not_test)* |
| 2765 | not_test: 'not' not_test | comparison |
| 2766 | comparison: expr (comp_op expr)* |
| 2767 | expr: xor_expr ('|' xor_expr)* |
| 2768 | xor_expr: and_expr ('^' and_expr)* |
| 2769 | and_expr: shift_expr ('&' shift_expr)* |
| 2770 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2771 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2772 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2773 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2774 | power: atom_expr ['**' factor] |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 2775 | atom_expr: [AWAIT] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2776 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2777 | */ |
| 2778 | |
| 2779 | asdl_seq *seq; |
| 2780 | int i; |
| 2781 | |
| 2782 | loop: |
| 2783 | switch (TYPE(n)) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2784 | case namedexpr_test: |
| 2785 | if (NCH(n) == 3) |
| 2786 | return ast_for_namedexpr(c, n); |
| 2787 | /* Fallthrough */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2788 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2789 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2790 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2791 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2792 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2793 | else if (NCH(n) > 1) |
| 2794 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2795 | /* Fallthrough */ |
| 2796 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2797 | case and_test: |
| 2798 | if (NCH(n) == 1) { |
| 2799 | n = CHILD(n, 0); |
| 2800 | goto loop; |
| 2801 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2802 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2803 | if (!seq) |
| 2804 | return NULL; |
| 2805 | for (i = 0; i < NCH(n); i += 2) { |
| 2806 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2807 | if (!e) |
| 2808 | return NULL; |
| 2809 | asdl_seq_SET(seq, i / 2, e); |
| 2810 | } |
| 2811 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2812 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2813 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2814 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2815 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2816 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, |
| 2817 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2818 | case not_test: |
| 2819 | if (NCH(n) == 1) { |
| 2820 | n = CHILD(n, 0); |
| 2821 | goto loop; |
| 2822 | } |
| 2823 | else { |
| 2824 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2825 | if (!expression) |
| 2826 | return NULL; |
| 2827 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2828 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2829 | n->n_end_lineno, n->n_end_col_offset, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2830 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2831 | } |
| 2832 | case comparison: |
| 2833 | if (NCH(n) == 1) { |
| 2834 | n = CHILD(n, 0); |
| 2835 | goto loop; |
| 2836 | } |
| 2837 | else { |
| 2838 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2839 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2840 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2841 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2842 | if (!ops) |
| 2843 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2844 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2845 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2846 | return NULL; |
| 2847 | } |
| 2848 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2849 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2850 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2851 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2852 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2853 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2854 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2855 | |
| 2856 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2857 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2858 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2859 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2861 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 | asdl_seq_SET(cmps, i / 2, expression); |
| 2863 | } |
| 2864 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2865 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2866 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2867 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2869 | return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, |
| 2870 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2871 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2873 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | /* The next five cases all handle BinOps. The main body of code |
| 2876 | is the same in each case, but the switch turned inside out to |
| 2877 | reuse the code for each type of operator. |
| 2878 | */ |
| 2879 | case expr: |
| 2880 | case xor_expr: |
| 2881 | case and_expr: |
| 2882 | case shift_expr: |
| 2883 | case arith_expr: |
| 2884 | case term: |
| 2885 | if (NCH(n) == 1) { |
| 2886 | n = CHILD(n, 0); |
| 2887 | goto loop; |
| 2888 | } |
| 2889 | return ast_for_binop(c, n); |
| 2890 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2891 | node *an = NULL; |
| 2892 | node *en = NULL; |
| 2893 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2894 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2895 | if (NCH(n) > 1) |
| 2896 | an = CHILD(n, 1); /* yield_arg */ |
| 2897 | if (an) { |
| 2898 | en = CHILD(an, NCH(an) - 1); |
| 2899 | if (NCH(an) == 2) { |
| 2900 | is_from = 1; |
| 2901 | exp = ast_for_expr(c, en); |
| 2902 | } |
| 2903 | else |
| 2904 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2905 | if (!exp) |
| 2906 | return NULL; |
| 2907 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2908 | if (is_from) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 2909 | return YieldFrom(exp, LINENO(n), n->n_col_offset, |
| 2910 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
| 2911 | return Yield(exp, LINENO(n), n->n_col_offset, |
| 2912 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2913 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2914 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2915 | if (NCH(n) == 1) { |
| 2916 | n = CHILD(n, 0); |
| 2917 | goto loop; |
| 2918 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2919 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2920 | case power: |
| 2921 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2923 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2924 | return NULL; |
| 2925 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2926 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2927 | return NULL; |
| 2928 | } |
| 2929 | |
| 2930 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2931 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 2932 | const node *start, const node *maybegenbeg, const node *closepar) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2933 | { |
| 2934 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2935 | arglist: argument (',' argument)* [','] |
| 2936 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2937 | */ |
| 2938 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2939 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2940 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2941 | asdl_seq *args; |
| 2942 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | |
| 2944 | REQ(n, arglist); |
| 2945 | |
| 2946 | nargs = 0; |
| 2947 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2948 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2949 | node *ch = CHILD(n, i); |
| 2950 | if (TYPE(ch) == argument) { |
| 2951 | if (NCH(ch) == 1) |
| 2952 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2953 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2954 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2955 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2956 | ast_error(c, ch, "invalid syntax"); |
| 2957 | return NULL; |
| 2958 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2959 | if (NCH(n) > 1) { |
| 2960 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2961 | return NULL; |
| 2962 | } |
| 2963 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2964 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2965 | nargs++; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 2966 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 2967 | nargs++; |
| 2968 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2969 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2970 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2971 | nkeywords++; |
| 2972 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2973 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2975 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2976 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2977 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2978 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2979 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2980 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2981 | |
| 2982 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2983 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2984 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2986 | node *ch = CHILD(n, i); |
| 2987 | if (TYPE(ch) == argument) { |
| 2988 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2989 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2990 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2991 | /* a positional argument */ |
| 2992 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2993 | if (ndoublestars) { |
| 2994 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 2995 | "positional argument follows " |
| 2996 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2997 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2998 | else { |
| 2999 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3000 | "positional argument follows " |
| 3001 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3002 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3003 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 3004 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3005 | e = ast_for_expr(c, chch); |
| 3006 | if (!e) |
| 3007 | return NULL; |
| 3008 | asdl_seq_SET(args, nargs++, e); |
| 3009 | } |
| 3010 | else if (TYPE(chch) == STAR) { |
| 3011 | /* an iterable argument unpacking */ |
| 3012 | expr_ty starred; |
| 3013 | if (ndoublestars) { |
| 3014 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3015 | "iterable argument unpacking follows " |
| 3016 | "keyword argument unpacking"); |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3017 | return NULL; |
| 3018 | } |
| 3019 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 3020 | if (!e) |
| 3021 | return NULL; |
| 3022 | starred = Starred(e, Load, LINENO(chch), |
| 3023 | chch->n_col_offset, |
Lysandros Nikolaou | 50d4f12 | 2019-12-18 01:20:55 +0100 | [diff] [blame] | 3024 | e->end_lineno, e->end_col_offset, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 3025 | c->c_arena); |
| 3026 | if (!starred) |
| 3027 | return NULL; |
| 3028 | asdl_seq_SET(args, nargs++, starred); |
| 3029 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3030 | } |
| 3031 | else if (TYPE(chch) == DOUBLESTAR) { |
| 3032 | /* a keyword argument unpacking */ |
| 3033 | keyword_ty kw; |
| 3034 | i++; |
| 3035 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3036 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3037 | return NULL; |
Pablo Galindo | 168660b | 2020-04-02 00:47:39 +0100 | [diff] [blame] | 3038 | kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset, |
| 3039 | e->end_lineno, e->end_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3040 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3041 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3043 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3044 | /* the lone generator expression */ |
Guido van Rossum | a796d8e | 2020-01-09 11:18:47 -0800 | [diff] [blame] | 3045 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3046 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3047 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3048 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3049 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3050 | else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { |
| 3051 | /* treat colon equal as positional argument */ |
| 3052 | if (nkeywords) { |
| 3053 | if (ndoublestars) { |
| 3054 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3055 | "positional argument follows " |
| 3056 | "keyword argument unpacking"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3057 | } |
| 3058 | else { |
| 3059 | ast_error(c, chch, |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3060 | "positional argument follows " |
| 3061 | "keyword argument"); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3062 | } |
| 3063 | return NULL; |
| 3064 | } |
| 3065 | e = ast_for_namedexpr(c, ch); |
| 3066 | if (!e) |
| 3067 | return NULL; |
| 3068 | asdl_seq_SET(args, nargs++, e); |
| 3069 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3070 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3071 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3072 | keyword_ty kw; |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 3073 | identifier key; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3074 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3075 | // To remain LL(1), the grammar accepts any test (basically, any |
| 3076 | // expression) in the keyword slot of a call site. So, we need |
| 3077 | // to manually enforce that the keyword is a NAME here. |
| 3078 | static const int name_tree[] = { |
| 3079 | test, |
| 3080 | or_test, |
| 3081 | and_test, |
| 3082 | not_test, |
| 3083 | comparison, |
| 3084 | expr, |
| 3085 | xor_expr, |
| 3086 | and_expr, |
| 3087 | shift_expr, |
| 3088 | arith_expr, |
| 3089 | term, |
| 3090 | factor, |
| 3091 | power, |
| 3092 | atom_expr, |
| 3093 | atom, |
| 3094 | 0, |
| 3095 | }; |
| 3096 | node *expr_node = chch; |
| 3097 | for (int i = 0; name_tree[i]; i++) { |
| 3098 | if (TYPE(expr_node) != name_tree[i]) |
| 3099 | break; |
| 3100 | if (NCH(expr_node) != 1) |
| 3101 | break; |
| 3102 | expr_node = CHILD(expr_node, 0); |
| 3103 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3104 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3105 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 3106 | "expression cannot contain assignment, " |
| 3107 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3108 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3109 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3110 | key = new_identifier(STR(expr_node), c); |
| 3111 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 3112 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 3114 | if (forbidden_name(c, key, chch, 1)) { |
| 3115 | return NULL; |
| 3116 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3117 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3118 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3119 | return NULL; |
Pablo Galindo | 168660b | 2020-04-02 00:47:39 +0100 | [diff] [blame] | 3120 | kw = keyword(key, e, chch->n_lineno, chch->n_col_offset, |
Pablo Galindo | 40cf35c | 2020-04-03 21:02:26 +0100 | [diff] [blame] | 3121 | e->end_lineno, e->end_col_offset, c->c_arena); |
Pablo Galindo | 168660b | 2020-04-02 00:47:39 +0100 | [diff] [blame] | 3122 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3123 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3124 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3125 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 3126 | } |
| 3127 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 3130 | return Call(func, args, keywords, LINENO(start), start->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3131 | closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3134 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3135 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3136 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3137 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3138 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3139 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3140 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3141 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3142 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3143 | } |
| 3144 | else { |
| 3145 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3146 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3147 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3148 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3149 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3150 | else { |
| 3151 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 3152 | if (!tmp) |
| 3153 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3154 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, |
| 3155 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3156 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3159 | static stmt_ty |
| 3160 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 3161 | { |
| 3162 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3163 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3164 | [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) |
| 3165 | annassign: ':' test ['=' (yield_expr|testlist)] |
| 3166 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 3167 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 3168 | '<<=' | '>>=' | '**=' | '//=') |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 3169 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3170 | */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3171 | int num = NCH(n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3172 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3173 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3174 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3175 | if (!e) |
| 3176 | return NULL; |
| 3177 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3178 | return Expr(e, LINENO(n), n->n_col_offset, |
| 3179 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3180 | } |
| 3181 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 3182 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3183 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3184 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3186 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3187 | if (!expr1) |
| 3188 | return NULL; |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 3189 | /* Augmented assignments can only have a name, a subscript, or an |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3190 | attribute on the left, though, so we have to explicitly check for |
| 3191 | those. */ |
| 3192 | switch (expr1->kind) { |
| 3193 | case Name_kind: |
| 3194 | case Attribute_kind: |
| 3195 | case Subscript_kind: |
| 3196 | break; |
| 3197 | default: |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 3198 | ast_error(c, ch, "'%s' is an illegal expression for augmented assignment", |
| 3199 | get_expr_name(expr1)); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 3200 | return NULL; |
| 3201 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3202 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 3203 | /* set_context checks that most expressions are not the left side. */ |
| 3204 | if(!set_context(c, expr1, Store, ch)) { |
| 3205 | return NULL; |
| 3206 | } |
| 3207 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3208 | ch = CHILD(n, 2); |
| 3209 | if (TYPE(ch) == testlist) |
| 3210 | expr2 = ast_for_testlist(c, ch); |
| 3211 | else |
| 3212 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3213 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3214 | return NULL; |
| 3215 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3216 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3217 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3218 | return NULL; |
| 3219 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3220 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 3221 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3222 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3223 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 3224 | expr_ty expr1, expr2, expr3; |
| 3225 | node *ch = CHILD(n, 0); |
| 3226 | node *deep, *ann = CHILD(n, 1); |
| 3227 | int simple = 1; |
| 3228 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 3229 | /* AnnAssigns are only allowed in Python 3.6 or greater */ |
| 3230 | if (c->c_feature_version < 6) { |
| 3231 | ast_error(c, ch, |
| 3232 | "Variable annotation syntax is only supported in Python 3.6 and greater"); |
| 3233 | return NULL; |
| 3234 | } |
| 3235 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3236 | /* we keep track of parens to qualify (x) as expression not name */ |
| 3237 | deep = ch; |
| 3238 | while (NCH(deep) == 1) { |
| 3239 | deep = CHILD(deep, 0); |
| 3240 | } |
| 3241 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 3242 | simple = 0; |
| 3243 | } |
| 3244 | expr1 = ast_for_testlist(c, ch); |
| 3245 | if (!expr1) { |
| 3246 | return NULL; |
| 3247 | } |
| 3248 | switch (expr1->kind) { |
| 3249 | case Name_kind: |
| 3250 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 3251 | return NULL; |
| 3252 | } |
| 3253 | expr1->v.Name.ctx = Store; |
| 3254 | break; |
| 3255 | case Attribute_kind: |
| 3256 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 3257 | return NULL; |
| 3258 | } |
| 3259 | expr1->v.Attribute.ctx = Store; |
| 3260 | break; |
| 3261 | case Subscript_kind: |
| 3262 | expr1->v.Subscript.ctx = Store; |
| 3263 | break; |
| 3264 | case List_kind: |
| 3265 | ast_error(c, ch, |
| 3266 | "only single target (not list) can be annotated"); |
| 3267 | return NULL; |
| 3268 | case Tuple_kind: |
| 3269 | ast_error(c, ch, |
| 3270 | "only single target (not tuple) can be annotated"); |
| 3271 | return NULL; |
| 3272 | default: |
| 3273 | ast_error(c, ch, |
| 3274 | "illegal target for annotation"); |
| 3275 | return NULL; |
| 3276 | } |
| 3277 | |
| 3278 | if (expr1->kind != Name_kind) { |
| 3279 | simple = 0; |
| 3280 | } |
| 3281 | ch = CHILD(ann, 1); |
| 3282 | expr2 = ast_for_expr(c, ch); |
| 3283 | if (!expr2) { |
| 3284 | return NULL; |
| 3285 | } |
| 3286 | if (NCH(ann) == 2) { |
| 3287 | return AnnAssign(expr1, expr2, NULL, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3288 | LINENO(n), n->n_col_offset, |
| 3289 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3290 | } |
| 3291 | else { |
| 3292 | ch = CHILD(ann, 3); |
Pablo Galindo | 8565f6b | 2019-06-03 08:34:20 +0100 | [diff] [blame] | 3293 | if (TYPE(ch) == testlist_star_expr) { |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 3294 | expr3 = ast_for_testlist(c, ch); |
| 3295 | } |
| 3296 | else { |
| 3297 | expr3 = ast_for_expr(c, ch); |
| 3298 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3299 | if (!expr3) { |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | return AnnAssign(expr1, expr2, expr3, simple, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3303 | LINENO(n), n->n_col_offset, |
| 3304 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3305 | } |
| 3306 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3308 | int i, nch_minus_type, has_type_comment; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3309 | asdl_seq *targets; |
| 3310 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3311 | expr_ty expression; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3312 | string type_comment; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3313 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3314 | /* a normal assignment */ |
| 3315 | REQ(CHILD(n, 1), EQUAL); |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3316 | |
| 3317 | has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; |
| 3318 | nch_minus_type = num - has_type_comment; |
| 3319 | |
| 3320 | targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3321 | if (!targets) |
| 3322 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3323 | for (i = 0; i < nch_minus_type - 2; i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3324 | expr_ty e; |
| 3325 | node *ch = CHILD(n, i); |
| 3326 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3327 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3328 | return NULL; |
| 3329 | } |
| 3330 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3332 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3333 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3334 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3335 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3336 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3337 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3338 | asdl_seq_SET(targets, i / 2, e); |
| 3339 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3340 | value = CHILD(n, nch_minus_type - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3341 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3342 | expression = ast_for_testlist(c, value); |
| 3343 | else |
| 3344 | expression = ast_for_expr(c, value); |
| 3345 | if (!expression) |
| 3346 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3347 | if (has_type_comment) { |
| 3348 | type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); |
| 3349 | if (!type_comment) |
| 3350 | return NULL; |
| 3351 | } |
| 3352 | else |
| 3353 | type_comment = NULL; |
| 3354 | return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3355 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3359 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3360 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3361 | 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] | 3362 | { |
| 3363 | asdl_seq *seq; |
| 3364 | int i; |
| 3365 | expr_ty e; |
| 3366 | |
| 3367 | REQ(n, exprlist); |
| 3368 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3369 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3370 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3371 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3372 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3373 | e = ast_for_expr(c, CHILD(n, i)); |
| 3374 | if (!e) |
| 3375 | return NULL; |
| 3376 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3377 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3378 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3379 | } |
| 3380 | return seq; |
| 3381 | } |
| 3382 | |
| 3383 | static stmt_ty |
| 3384 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3385 | { |
| 3386 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3388 | /* del_stmt: 'del' exprlist */ |
| 3389 | REQ(n, del_stmt); |
| 3390 | |
| 3391 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3392 | if (!expr_list) |
| 3393 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3394 | return Delete(expr_list, LINENO(n), n->n_col_offset, |
| 3395 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | static stmt_ty |
| 3399 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3400 | { |
| 3401 | /* |
| 3402 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3403 | | yield_stmt |
| 3404 | break_stmt: 'break' |
| 3405 | continue_stmt: 'continue' |
| 3406 | return_stmt: 'return' [testlist] |
| 3407 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3408 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3409 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3410 | */ |
| 3411 | node *ch; |
| 3412 | |
| 3413 | REQ(n, flow_stmt); |
| 3414 | ch = CHILD(n, 0); |
| 3415 | switch (TYPE(ch)) { |
| 3416 | case break_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3417 | return Break(LINENO(n), n->n_col_offset, |
| 3418 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3419 | case continue_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3420 | return Continue(LINENO(n), n->n_col_offset, |
| 3421 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3422 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3423 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3424 | if (!exp) |
| 3425 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3426 | return Expr(exp, LINENO(n), n->n_col_offset, |
| 3427 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3428 | } |
| 3429 | case return_stmt: |
| 3430 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3431 | return Return(NULL, LINENO(n), n->n_col_offset, |
| 3432 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3433 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3434 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3435 | if (!expression) |
| 3436 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3437 | return Return(expression, LINENO(n), n->n_col_offset, |
| 3438 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3439 | } |
| 3440 | case raise_stmt: |
| 3441 | if (NCH(ch) == 1) |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3442 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, |
| 3443 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3444 | else if (NCH(ch) >= 2) { |
| 3445 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3446 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3447 | if (!expression) |
| 3448 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3449 | if (NCH(ch) == 4) { |
| 3450 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3451 | if (!cause) |
| 3452 | return NULL; |
| 3453 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3454 | return Raise(expression, cause, LINENO(n), n->n_col_offset, |
| 3455 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3456 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3457 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3459 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3460 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3461 | return NULL; |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3466 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | { |
| 3468 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3469 | import_as_name: NAME ['as' NAME] |
| 3470 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3471 | dotted_name: NAME ('.' NAME)* |
| 3472 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3473 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3474 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | loop: |
| 3476 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3477 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3478 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3479 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3480 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3481 | if (!name) |
| 3482 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3483 | if (NCH(n) == 3) { |
| 3484 | node *str_node = CHILD(n, 2); |
| 3485 | str = NEW_IDENTIFIER(str_node); |
| 3486 | if (!str) |
| 3487 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3488 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3489 | return NULL; |
| 3490 | } |
| 3491 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3492 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3493 | return NULL; |
| 3494 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3495 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3496 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3497 | case dotted_as_name: |
| 3498 | if (NCH(n) == 1) { |
| 3499 | n = CHILD(n, 0); |
| 3500 | goto loop; |
| 3501 | } |
| 3502 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3503 | node *asname_node = CHILD(n, 2); |
| 3504 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3505 | if (!a) |
| 3506 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3507 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3508 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3509 | if (!a->asname) |
| 3510 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3511 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3512 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3513 | return a; |
| 3514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3515 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3516 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3517 | node *name_node = CHILD(n, 0); |
| 3518 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3519 | if (!name) |
| 3520 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3521 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3522 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3523 | return alias(name, NULL, c->c_arena); |
| 3524 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3525 | else { |
| 3526 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3527 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3528 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3529 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3531 | |
| 3532 | len = 0; |
| 3533 | for (i = 0; i < NCH(n); i += 2) |
| 3534 | /* length of string plus one for the dot */ |
| 3535 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3536 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3537 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3538 | if (!str) |
| 3539 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3540 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3541 | if (!s) |
| 3542 | return NULL; |
| 3543 | for (i = 0; i < NCH(n); i += 2) { |
| 3544 | char *sch = STR(CHILD(n, i)); |
| 3545 | strcpy(s, STR(CHILD(n, i))); |
| 3546 | s += strlen(sch); |
| 3547 | *s++ = '.'; |
| 3548 | } |
| 3549 | --s; |
| 3550 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3552 | PyBytes_GET_SIZE(str), |
| 3553 | NULL); |
| 3554 | Py_DECREF(str); |
| 3555 | if (!uni) |
| 3556 | return NULL; |
| 3557 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3558 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3559 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3560 | Py_DECREF(str); |
| 3561 | return NULL; |
| 3562 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3563 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3564 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3565 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3566 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3567 | if (!str) |
| 3568 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3569 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3570 | Py_DECREF(str); |
| 3571 | return NULL; |
| 3572 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3573 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3574 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3575 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | "unexpected import name: %d", TYPE(n)); |
| 3577 | return NULL; |
| 3578 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | static stmt_ty |
| 3582 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3583 | { |
| 3584 | /* |
| 3585 | import_stmt: import_name | import_from |
| 3586 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3587 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3588 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3589 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3590 | int lineno; |
| 3591 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3592 | int i; |
| 3593 | asdl_seq *aliases; |
| 3594 | |
| 3595 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3596 | lineno = LINENO(n); |
| 3597 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3599 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3600 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3601 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3602 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3603 | if (!aliases) |
| 3604 | return NULL; |
| 3605 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3606 | 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] | 3607 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3608 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3609 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3610 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3611 | // Even though n is modified above, the end position is not changed |
| 3612 | return Import(aliases, lineno, col_offset, |
| 3613 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3614 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3615 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3616 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3617 | int idx, ndots = 0; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3618 | const node *n_copy = n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3619 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3620 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3622 | /* Count the number of dots (for relative imports) and check for the |
| 3623 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3624 | for (idx = 1; idx < NCH(n); idx++) { |
| 3625 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3626 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3627 | if (!mod) |
| 3628 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3629 | idx++; |
| 3630 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3631 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3633 | ndots += 3; |
| 3634 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3635 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3636 | break; |
| 3637 | } |
| 3638 | ndots++; |
| 3639 | } |
| 3640 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3641 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3642 | case STAR: |
| 3643 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3644 | n = CHILD(n, idx); |
| 3645 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3646 | break; |
| 3647 | case LPAR: |
| 3648 | /* from ... import (x, y, z) */ |
| 3649 | n = CHILD(n, idx + 1); |
| 3650 | n_children = NCH(n); |
| 3651 | break; |
| 3652 | case import_as_names: |
| 3653 | /* from ... import x, y, z */ |
| 3654 | n = CHILD(n, idx); |
| 3655 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3656 | if (n_children % 2 == 0) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3657 | ast_error(c, n, |
| 3658 | "trailing comma not allowed without" |
| 3659 | " surrounding parentheses"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3660 | return NULL; |
| 3661 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3662 | break; |
| 3663 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3664 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3665 | return NULL; |
| 3666 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3667 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3668 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3669 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3670 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3671 | |
| 3672 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3673 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3674 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3675 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3676 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3677 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3678 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3679 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3680 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3681 | 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] | 3682 | if (!import_alias) |
| 3683 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3684 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3685 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3686 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3687 | if (mod != NULL) |
| 3688 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3689 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3690 | n_copy->n_end_lineno, n_copy->n_end_col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3691 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3692 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3693 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3694 | "unknown import statement: starts with command '%s'", |
| 3695 | STR(CHILD(n, 0))); |
| 3696 | return NULL; |
| 3697 | } |
| 3698 | |
| 3699 | static stmt_ty |
| 3700 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3701 | { |
| 3702 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3703 | identifier name; |
| 3704 | asdl_seq *s; |
| 3705 | int i; |
| 3706 | |
| 3707 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3708 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3709 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3710 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3712 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3713 | if (!name) |
| 3714 | return NULL; |
| 3715 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3716 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3717 | return Global(s, LINENO(n), n->n_col_offset, |
| 3718 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3722 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3723 | { |
| 3724 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3725 | identifier name; |
| 3726 | asdl_seq *s; |
| 3727 | int i; |
| 3728 | |
| 3729 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3730 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3731 | if (!s) |
| 3732 | return NULL; |
| 3733 | for (i = 1; i < NCH(n); i += 2) { |
| 3734 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3735 | if (!name) |
| 3736 | return NULL; |
| 3737 | asdl_seq_SET(s, i / 2, name); |
| 3738 | } |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3739 | return Nonlocal(s, LINENO(n), n->n_col_offset, |
| 3740 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3744 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3745 | { |
| 3746 | /* assert_stmt: 'assert' test [',' test] */ |
| 3747 | REQ(n, assert_stmt); |
| 3748 | if (NCH(n) == 2) { |
| 3749 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3750 | if (!expression) |
| 3751 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3752 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, |
| 3753 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3754 | } |
| 3755 | else if (NCH(n) == 4) { |
| 3756 | expr_ty expr1, expr2; |
| 3757 | |
| 3758 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3759 | if (!expr1) |
| 3760 | return NULL; |
| 3761 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3762 | if (!expr2) |
| 3763 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3764 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3765 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, |
| 3766 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3767 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3768 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3769 | "improper number of parts to 'assert' statement: %d", |
| 3770 | NCH(n)); |
| 3771 | return NULL; |
| 3772 | } |
| 3773 | |
| 3774 | static asdl_seq * |
| 3775 | ast_for_suite(struct compiling *c, const node *n) |
| 3776 | { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3777 | /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3778 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3779 | stmt_ty s; |
| 3780 | int i, total, num, end, pos = 0; |
| 3781 | node *ch; |
| 3782 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3783 | if (TYPE(n) != func_body_suite) { |
| 3784 | REQ(n, suite); |
| 3785 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3786 | |
| 3787 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3788 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3790 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3791 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3792 | n = CHILD(n, 0); |
| 3793 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3795 | */ |
| 3796 | end = NCH(n) - 1; |
| 3797 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3798 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3799 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3800 | for (i = 0; i < end; i += 2) { |
| 3801 | ch = CHILD(n, i); |
| 3802 | s = ast_for_stmt(c, ch); |
| 3803 | if (!s) |
| 3804 | return NULL; |
| 3805 | asdl_seq_SET(seq, pos++, s); |
| 3806 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3807 | } |
| 3808 | else { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 3809 | i = 2; |
| 3810 | if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { |
| 3811 | i += 2; |
| 3812 | REQ(CHILD(n, 2), NEWLINE); |
| 3813 | } |
| 3814 | |
| 3815 | for (; i < (NCH(n) - 1); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3816 | ch = CHILD(n, i); |
| 3817 | REQ(ch, stmt); |
| 3818 | num = num_stmts(ch); |
| 3819 | if (num == 1) { |
| 3820 | /* small_stmt or compound_stmt with only one child */ |
| 3821 | s = ast_for_stmt(c, ch); |
| 3822 | if (!s) |
| 3823 | return NULL; |
| 3824 | asdl_seq_SET(seq, pos++, s); |
| 3825 | } |
| 3826 | else { |
| 3827 | int j; |
| 3828 | ch = CHILD(ch, 0); |
| 3829 | REQ(ch, simple_stmt); |
| 3830 | for (j = 0; j < NCH(ch); j += 2) { |
| 3831 | /* statement terminates with a semi-colon ';' */ |
| 3832 | if (NCH(CHILD(ch, j)) == 0) { |
| 3833 | assert((j + 1) == NCH(ch)); |
| 3834 | break; |
| 3835 | } |
| 3836 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3837 | if (!s) |
| 3838 | return NULL; |
| 3839 | asdl_seq_SET(seq, pos++, s); |
| 3840 | } |
| 3841 | } |
| 3842 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3843 | } |
| 3844 | assert(pos == seq->size); |
| 3845 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3848 | static void |
| 3849 | get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) |
| 3850 | { |
Pablo Galindo | 46a9792 | 2019-02-19 22:51:53 +0000 | [diff] [blame] | 3851 | Py_ssize_t tot = asdl_seq_LEN(s); |
Ivan Levkivskyi | 181835d | 2019-02-10 15:39:49 +0000 | [diff] [blame] | 3852 | // There must be no empty suites. |
| 3853 | assert(tot > 0); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3854 | stmt_ty last = asdl_seq_GET(s, tot - 1); |
| 3855 | *end_lineno = last->end_lineno; |
| 3856 | *end_col_offset = last->end_col_offset; |
| 3857 | } |
| 3858 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3859 | static stmt_ty |
| 3860 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3861 | { |
| 3862 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3863 | ['else' ':' suite] |
| 3864 | */ |
| 3865 | char *s; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3866 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3867 | |
| 3868 | REQ(n, if_stmt); |
| 3869 | |
| 3870 | if (NCH(n) == 4) { |
| 3871 | expr_ty expression; |
| 3872 | asdl_seq *suite_seq; |
| 3873 | |
| 3874 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3875 | if (!expression) |
| 3876 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3878 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3879 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3880 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3881 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3882 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3883 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3884 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3885 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3886 | s = STR(CHILD(n, 4)); |
| 3887 | /* s[2], the third character in the string, will be |
| 3888 | 's' for el_s_e, or |
| 3889 | 'i' for el_i_f |
| 3890 | */ |
| 3891 | if (s[2] == 's') { |
| 3892 | expr_ty expression; |
| 3893 | asdl_seq *seq1, *seq2; |
| 3894 | |
| 3895 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3896 | if (!expression) |
| 3897 | return NULL; |
| 3898 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3899 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3900 | return NULL; |
| 3901 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3902 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3903 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3904 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3905 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3906 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3907 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3908 | } |
| 3909 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3910 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3911 | expr_ty expression; |
| 3912 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3913 | asdl_seq *orelse = NULL; |
| 3914 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3915 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3916 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3917 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3918 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3919 | has_else = 1; |
| 3920 | n_elif -= 3; |
| 3921 | } |
| 3922 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3923 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3924 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3925 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3926 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3927 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3928 | if (!orelse) |
| 3929 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3930 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3931 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3932 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3933 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3934 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3935 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3936 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3937 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3938 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3939 | get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3941 | asdl_seq_SET(orelse, 0, |
| 3942 | If(expression, suite_seq, suite_seq2, |
Lysandros Nikolaou | 5936a4c | 2019-12-14 11:24:57 +0100 | [diff] [blame] | 3943 | LINENO(CHILD(n, NCH(n) - 7)), |
| 3944 | CHILD(n, NCH(n) - 7)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3945 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3946 | /* the just-created orelse handled the last elif */ |
| 3947 | n_elif--; |
| 3948 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3949 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3950 | for (i = 0; i < n_elif; i++) { |
| 3951 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3952 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3953 | if (!newobj) |
| 3954 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3955 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3956 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3957 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3958 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3959 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3960 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3962 | if (orelse != NULL) { |
| 3963 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 3964 | } else { |
| 3965 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 3966 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3967 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | If(expression, suite_seq, orelse, |
Lysandros Nikolaou | 025a602 | 2019-12-12 22:40:21 +0100 | [diff] [blame] | 3969 | LINENO(CHILD(n, off - 1)), |
| 3970 | CHILD(n, off - 1)->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3971 | end_lineno, end_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3972 | orelse = newobj; |
| 3973 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3974 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3975 | if (!expression) |
| 3976 | return NULL; |
| 3977 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3978 | if (!suite_seq) |
| 3979 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3980 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3981 | return If(expression, suite_seq, orelse, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3982 | LINENO(n), n->n_col_offset, |
| 3983 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3984 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3985 | |
| 3986 | PyErr_Format(PyExc_SystemError, |
| 3987 | "unexpected token in 'if' statement: %s", s); |
| 3988 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | static stmt_ty |
| 3992 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3993 | { |
| 3994 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3995 | REQ(n, while_stmt); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 3996 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3997 | |
| 3998 | if (NCH(n) == 4) { |
| 3999 | expr_ty expression; |
| 4000 | asdl_seq *suite_seq; |
| 4001 | |
| 4002 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4003 | if (!expression) |
| 4004 | return NULL; |
| 4005 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4006 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4007 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4008 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4009 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 4010 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4011 | } |
| 4012 | else if (NCH(n) == 7) { |
| 4013 | expr_ty expression; |
| 4014 | asdl_seq *seq1, *seq2; |
| 4015 | |
| 4016 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 4017 | if (!expression) |
| 4018 | return NULL; |
| 4019 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4020 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4021 | return NULL; |
| 4022 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4023 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4024 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4025 | get_last_end_pos(seq2, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4026 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4027 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 4028 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4029 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4030 | |
| 4031 | PyErr_Format(PyExc_SystemError, |
| 4032 | "wrong number of tokens for 'while' statement: %d", |
| 4033 | NCH(n)); |
| 4034 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4038 | 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] | 4039 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4040 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4041 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4042 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4043 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4044 | const node *node_target; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4045 | int end_lineno, end_col_offset; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4046 | int has_type_comment; |
| 4047 | string type_comment; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4048 | |
| 4049 | if (is_async && c->c_feature_version < 5) { |
| 4050 | ast_error(c, n, |
| 4051 | "Async for loops are only supported in Python 3.5 and greater"); |
| 4052 | return NULL; |
| 4053 | } |
| 4054 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4055 | /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4056 | REQ(n, for_stmt); |
| 4057 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4058 | has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; |
| 4059 | |
| 4060 | if (NCH(n) == 9 + has_type_comment) { |
| 4061 | seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4062 | if (!seq) |
| 4063 | return NULL; |
| 4064 | } |
| 4065 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4066 | node_target = CHILD(n, 1); |
| 4067 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4068 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4069 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4070 | /* Check the # of children rather than the length of _target, since |
| 4071 | 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] | 4072 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4073 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 4074 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4075 | else |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4076 | target = Tuple(_target, Store, first->lineno, first->col_offset, |
| 4077 | node_target->n_end_lineno, node_target->n_end_col_offset, |
| 4078 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4079 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 4080 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4081 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4082 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4083 | suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4084 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4085 | return NULL; |
| 4086 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4087 | if (seq != NULL) { |
| 4088 | get_last_end_pos(seq, &end_lineno, &end_col_offset); |
| 4089 | } else { |
| 4090 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
| 4091 | } |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4092 | |
| 4093 | if (has_type_comment) { |
| 4094 | type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); |
| 4095 | if (!type_comment) |
| 4096 | return NULL; |
| 4097 | } |
| 4098 | else |
| 4099 | type_comment = NULL; |
| 4100 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4101 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4102 | return AsyncFor(target, expression, suite_seq, seq, type_comment, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 4103 | LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4104 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4105 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4106 | return For(target, expression, suite_seq, seq, type_comment, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4107 | LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4108 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | static excepthandler_ty |
| 4112 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 4113 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4114 | /* except_clause: 'except' [test ['as' test]] */ |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4115 | int end_lineno, end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4116 | REQ(exc, except_clause); |
| 4117 | REQ(body, suite); |
| 4118 | |
| 4119 | if (NCH(exc) == 1) { |
| 4120 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 4121 | if (!suite_seq) |
| 4122 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4123 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4124 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4125 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4126 | exc->n_col_offset, |
| 4127 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4128 | } |
| 4129 | else if (NCH(exc) == 2) { |
| 4130 | expr_ty expression; |
| 4131 | asdl_seq *suite_seq; |
| 4132 | |
| 4133 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 4134 | if (!expression) |
| 4135 | return NULL; |
| 4136 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4137 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4138 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4139 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4140 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4141 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4142 | exc->n_col_offset, |
| 4143 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4144 | } |
| 4145 | else if (NCH(exc) == 4) { |
| 4146 | asdl_seq *suite_seq; |
| 4147 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 4148 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4149 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4150 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4151 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4152 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4153 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4154 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4155 | return NULL; |
| 4156 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4157 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4158 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4159 | get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4160 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 4161 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4162 | exc->n_col_offset, |
| 4163 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4164 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4165 | |
| 4166 | PyErr_Format(PyExc_SystemError, |
| 4167 | "wrong number of children for 'except' clause: %d", |
| 4168 | NCH(exc)); |
| 4169 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | static stmt_ty |
| 4173 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 4174 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4175 | const int nch = NCH(n); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4176 | int end_lineno, end_col_offset, n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4177 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4178 | excepthandler_ty last_handler; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4179 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4180 | REQ(n, try_stmt); |
| 4181 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4182 | body = ast_for_suite(c, CHILD(n, 2)); |
| 4183 | if (body == NULL) |
| 4184 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4185 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4186 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 4187 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 4188 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 4189 | /* we can assume it's an "else", |
| 4190 | because nch >= 9 for try-else-finally and |
| 4191 | it would otherwise have a type of except_clause */ |
| 4192 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 4193 | if (orelse == NULL) |
| 4194 | return NULL; |
| 4195 | n_except--; |
| 4196 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4197 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4198 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4199 | if (finally == NULL) |
| 4200 | return NULL; |
| 4201 | n_except--; |
| 4202 | } |
| 4203 | else { |
| 4204 | /* we can assume it's an "else", |
| 4205 | otherwise it would have a type of except_clause */ |
| 4206 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 4207 | if (orelse == NULL) |
| 4208 | return NULL; |
| 4209 | n_except--; |
| 4210 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4211 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4212 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4213 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4214 | return NULL; |
| 4215 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4217 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4218 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4219 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4220 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4221 | if (handlers == NULL) |
| 4222 | return NULL; |
| 4223 | |
| 4224 | for (i = 0; i < n_except; i++) { |
| 4225 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 4226 | CHILD(n, 5 + i * 3)); |
| 4227 | if (!e) |
| 4228 | return NULL; |
| 4229 | asdl_seq_SET(handlers, i, e); |
| 4230 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 4233 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4234 | if (finally != NULL) { |
| 4235 | // finally is always last |
| 4236 | get_last_end_pos(finally, &end_lineno, &end_col_offset); |
| 4237 | } else if (orelse != NULL) { |
| 4238 | // otherwise else is last |
| 4239 | get_last_end_pos(orelse, &end_lineno, &end_col_offset); |
| 4240 | } else { |
| 4241 | // inline the get_last_end_pos logic due to layout mismatch |
| 4242 | last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); |
| 4243 | end_lineno = last_handler->end_lineno; |
| 4244 | end_col_offset = last_handler->end_col_offset; |
| 4245 | } |
| 4246 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, |
| 4247 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4250 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4251 | static withitem_ty |
| 4252 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4253 | { |
| 4254 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4255 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4256 | REQ(n, with_item); |
| 4257 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 4258 | if (!context_expr) |
| 4259 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4260 | if (NCH(n) == 3) { |
| 4261 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4262 | |
| 4263 | if (!optional_vars) { |
| 4264 | return NULL; |
| 4265 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4266 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4267 | return NULL; |
| 4268 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4271 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4274 | /* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4275 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4276 | 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] | 4277 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 4278 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4279 | 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] | 4280 | asdl_seq *items, *body; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4281 | string type_comment; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4282 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 4283 | if (is_async && c->c_feature_version < 5) { |
| 4284 | ast_error(c, n, |
| 4285 | "Async with statements are only supported in Python 3.5 and greater"); |
| 4286 | return NULL; |
| 4287 | } |
| 4288 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4289 | REQ(n, with_stmt); |
| 4290 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4291 | has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; |
| 4292 | nch_minus_type = NCH(n) - has_type_comment; |
| 4293 | |
| 4294 | n_items = (nch_minus_type - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 4295 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 4296 | if (!items) |
| 4297 | return NULL; |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4298 | for (i = 1; i < nch_minus_type - 2; i += 2) { |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4299 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 4300 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4301 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4302 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4305 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 4306 | if (!body) |
| 4307 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4308 | get_last_end_pos(body, &end_lineno, &end_col_offset); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4309 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4310 | if (has_type_comment) { |
| 4311 | type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); |
| 4312 | if (!type_comment) |
| 4313 | return NULL; |
| 4314 | } |
| 4315 | else |
| 4316 | type_comment = NULL; |
| 4317 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4318 | if (is_async) |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4319 | return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4320 | end_lineno, end_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4321 | else |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4322 | return With(items, body, type_comment, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4323 | end_lineno, end_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4326 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4327 | 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] | 4328 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4329 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4330 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4331 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4332 | expr_ty call; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4333 | int end_lineno, end_col_offset; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4334 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4335 | REQ(n, classdef); |
| 4336 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4337 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4338 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4339 | if (!s) |
| 4340 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4341 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4342 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4343 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4344 | if (!classname) |
| 4345 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4346 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4347 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4348 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4349 | LINENO(n), n->n_col_offset, |
| 4350 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4351 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4352 | |
| 4353 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4354 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4355 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4356 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4357 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4358 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4359 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4360 | if (!classname) |
| 4361 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4362 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4363 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4364 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4365 | LINENO(n), n->n_col_offset, |
| 4366 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4369 | /* class NAME '(' arglist ')' ':' suite */ |
| 4370 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4371 | { |
| 4372 | PyObject *dummy_name; |
| 4373 | expr_ty dummy; |
| 4374 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4375 | if (!dummy_name) |
| 4376 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4377 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, |
| 4378 | CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, |
| 4379 | c->c_arena); |
Serhiy Storchaka | 6e619c4 | 2020-02-12 22:37:49 +0200 | [diff] [blame] | 4380 | call = ast_for_call(c, CHILD(n, 3), dummy, |
| 4381 | CHILD(n, 1), NULL, CHILD(n, 4)); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 4382 | if (!call) |
| 4383 | return NULL; |
| 4384 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 4385 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 4386 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4387 | return NULL; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4388 | get_last_end_pos(s, &end_lineno, &end_col_offset); |
| 4389 | |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 4390 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 4391 | if (!classname) |
| 4392 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 4393 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 4394 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4395 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4396 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4397 | decorator_seq, LINENO(n), n->n_col_offset, |
| 4398 | end_lineno, end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | static stmt_ty |
| 4402 | ast_for_stmt(struct compiling *c, const node *n) |
| 4403 | { |
| 4404 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4405 | assert(NCH(n) == 1); |
| 4406 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4407 | } |
| 4408 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4409 | assert(num_stmts(n) == 1); |
| 4410 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4411 | } |
| 4412 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4413 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4414 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 4415 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4416 | */ |
| 4417 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4418 | case expr_stmt: |
| 4419 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4420 | case del_stmt: |
| 4421 | return ast_for_del_stmt(c, n); |
| 4422 | case pass_stmt: |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4423 | return Pass(LINENO(n), n->n_col_offset, |
| 4424 | n->n_end_lineno, n->n_end_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4425 | case flow_stmt: |
| 4426 | return ast_for_flow_stmt(c, n); |
| 4427 | case import_stmt: |
| 4428 | return ast_for_import_stmt(c, n); |
| 4429 | case global_stmt: |
| 4430 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4431 | case nonlocal_stmt: |
| 4432 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4433 | case assert_stmt: |
| 4434 | return ast_for_assert_stmt(c, n); |
| 4435 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4436 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4437 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4438 | TYPE(n), NCH(n)); |
| 4439 | return NULL; |
| 4440 | } |
| 4441 | } |
| 4442 | else { |
| 4443 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4444 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4445 | */ |
| 4446 | node *ch = CHILD(n, 0); |
| 4447 | REQ(n, compound_stmt); |
| 4448 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4449 | case if_stmt: |
| 4450 | return ast_for_if_stmt(c, ch); |
| 4451 | case while_stmt: |
| 4452 | return ast_for_while_stmt(c, ch); |
| 4453 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4454 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4455 | case try_stmt: |
| 4456 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4457 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4458 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4459 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4460 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4461 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4462 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | case decorated: |
| 4464 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4465 | case async_stmt: |
| 4466 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4467 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4468 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4469 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4470 | TYPE(n), NCH(n)); |
| 4471 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4472 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4477 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4478 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4479 | const char *end; |
| 4480 | long x; |
| 4481 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4482 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4483 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4484 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4485 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4486 | errno = 0; |
| 4487 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4488 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4489 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4490 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4491 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4492 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4493 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4494 | } |
| 4495 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4496 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4497 | if (*end == '\0') { |
| 4498 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4499 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4500 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4501 | } |
| 4502 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4503 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4504 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4505 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4506 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4507 | return NULL; |
| 4508 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4509 | } |
| 4510 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4511 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4512 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4513 | if (dx == -1.0 && PyErr_Occurred()) |
| 4514 | return NULL; |
| 4515 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4516 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4520 | parsenumber(struct compiling *c, const char *s) |
| 4521 | { |
| 4522 | char *dup, *end; |
| 4523 | PyObject *res = NULL; |
| 4524 | |
| 4525 | assert(s != NULL); |
| 4526 | |
| 4527 | if (strchr(s, '_') == NULL) { |
| 4528 | return parsenumber_raw(c, s); |
| 4529 | } |
| 4530 | /* Create a duplicate without underscores. */ |
| 4531 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4532 | if (dup == NULL) { |
| 4533 | return PyErr_NoMemory(); |
| 4534 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4535 | end = dup; |
| 4536 | for (; *s; s++) { |
| 4537 | if (*s != '_') { |
| 4538 | *end++ = *s; |
| 4539 | } |
| 4540 | } |
| 4541 | *end = '\0'; |
| 4542 | res = parsenumber_raw(c, dup); |
| 4543 | PyMem_Free(dup); |
| 4544 | return res; |
| 4545 | } |
| 4546 | |
| 4547 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4548 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4549 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4550 | const char *s, *t; |
| 4551 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4552 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4553 | while (s < end && (*s & 0x80)) s++; |
| 4554 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4555 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4558 | static int |
| 4559 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4560 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4561 | { |
| 4562 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4563 | first_invalid_escape_char); |
| 4564 | if (msg == NULL) { |
| 4565 | return -1; |
| 4566 | } |
Gregory P. Smith | b4be87a | 2019-08-10 00:19:07 -0700 | [diff] [blame] | 4567 | if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4568 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4569 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4570 | { |
Gregory P. Smith | b4be87a | 2019-08-10 00:19:07 -0700 | [diff] [blame] | 4571 | if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) { |
| 4572 | /* Replace the DeprecationWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4573 | to get a more accurate error report */ |
| 4574 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4575 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4576 | } |
| 4577 | Py_DECREF(msg); |
| 4578 | return -1; |
| 4579 | } |
| 4580 | Py_DECREF(msg); |
| 4581 | return 0; |
| 4582 | } |
| 4583 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4584 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4585 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4586 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4587 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4588 | PyObject *v, *u; |
| 4589 | char *buf; |
| 4590 | char *p; |
| 4591 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4592 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4593 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4594 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4595 | return NULL; |
| 4596 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4597 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4598 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4599 | if (u == NULL) |
| 4600 | return NULL; |
| 4601 | p = buf = PyBytes_AsString(u); |
| 4602 | end = s + len; |
| 4603 | while (s < end) { |
| 4604 | if (*s == '\\') { |
| 4605 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4606 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4607 | strcpy(p, "u005c"); |
| 4608 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4609 | if (s >= end) |
| 4610 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4611 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4612 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4613 | if (*s & 0x80) { /* XXX inefficient */ |
| 4614 | PyObject *w; |
| 4615 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 4616 | const void *data; |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4617 | Py_ssize_t len, i; |
| 4618 | w = decode_utf8(c, &s, end); |
| 4619 | if (w == NULL) { |
| 4620 | Py_DECREF(u); |
| 4621 | return NULL; |
| 4622 | } |
| 4623 | kind = PyUnicode_KIND(w); |
| 4624 | data = PyUnicode_DATA(w); |
| 4625 | len = PyUnicode_GET_LENGTH(w); |
| 4626 | for (i = 0; i < len; i++) { |
| 4627 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4628 | sprintf(p, "\\U%08x", chr); |
| 4629 | p += 10; |
| 4630 | } |
| 4631 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4632 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4633 | Py_DECREF(w); |
| 4634 | } else { |
| 4635 | *p++ = *s++; |
| 4636 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4637 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4638 | len = p - buf; |
| 4639 | s = buf; |
| 4640 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4641 | const char *first_invalid_escape; |
| 4642 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4643 | |
| 4644 | if (v != NULL && first_invalid_escape != NULL) { |
| 4645 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4646 | /* We have not decref u before because first_invalid_escape points |
| 4647 | inside u. */ |
| 4648 | Py_XDECREF(u); |
| 4649 | Py_DECREF(v); |
| 4650 | return NULL; |
| 4651 | } |
| 4652 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4653 | Py_XDECREF(u); |
| 4654 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4657 | static PyObject * |
| 4658 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4659 | size_t len) |
| 4660 | { |
| 4661 | const char *first_invalid_escape; |
Greg Price | 3a4f667 | 2019-09-12 11:12:22 -0700 | [diff] [blame] | 4662 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4663 | &first_invalid_escape); |
| 4664 | if (result == NULL) |
| 4665 | return NULL; |
| 4666 | |
| 4667 | if (first_invalid_escape != NULL) { |
| 4668 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4669 | Py_DECREF(result); |
| 4670 | return NULL; |
| 4671 | } |
| 4672 | } |
| 4673 | return result; |
| 4674 | } |
| 4675 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4676 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4677 | and `col_offset` to existing locations. */ |
| 4678 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4679 | { |
| 4680 | n->n_col_offset = n->n_col_offset + col_offset; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4681 | n->n_end_col_offset = n->n_end_col_offset + col_offset; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4682 | for (int i = 0; i < NCH(n); ++i) { |
| 4683 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4684 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4685 | col_offset = 0; |
| 4686 | } |
| 4687 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4688 | } |
| 4689 | n->n_lineno = n->n_lineno + lineno; |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4690 | n->n_end_lineno = n->n_end_lineno + lineno; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | /* Fix locations for the given node and its children. |
| 4694 | |
| 4695 | `parent` is the enclosing node. |
| 4696 | `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] | 4697 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4698 | */ |
| 4699 | static void |
| 4700 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4701 | { |
| 4702 | char *substr = NULL; |
| 4703 | char *start; |
| 4704 | int lines = LINENO(parent) - 1; |
| 4705 | int cols = parent->n_col_offset; |
| 4706 | /* Find the full fstring to fix location information in `n`. */ |
| 4707 | while (parent && parent->n_type != STRING) |
| 4708 | parent = parent->n_child; |
| 4709 | if (parent && parent->n_str) { |
| 4710 | substr = strstr(parent->n_str, expr_str); |
| 4711 | if (substr) { |
| 4712 | start = substr; |
| 4713 | while (start > parent->n_str) { |
| 4714 | if (start[0] == '\n') |
| 4715 | break; |
| 4716 | start--; |
| 4717 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4718 | cols += (int)(substr - start); |
Anthony Sottile | 995d9b9 | 2019-01-12 20:05:13 -0800 | [diff] [blame] | 4719 | /* adjust the start based on the number of newlines encountered |
| 4720 | before the f-string expression */ |
| 4721 | for (char* p = parent->n_str; p < substr; p++) { |
| 4722 | if (*p == '\n') { |
| 4723 | lines++; |
| 4724 | } |
| 4725 | } |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4726 | } |
| 4727 | } |
| 4728 | fstring_shift_node_locations(n, lines, cols); |
| 4729 | } |
| 4730 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4731 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4732 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4733 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4734 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4735 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4736 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4737 | { |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4738 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4739 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4740 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4741 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4742 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4743 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4744 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4745 | assert(*(expr_start-1) == '{'); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4746 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' || |
| 4747 | *expr_end == '='); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4748 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4749 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4750 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4751 | because turning the expression '' in to '()' would go from being invalid |
| 4752 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4753 | for (s = expr_start; s != expr_end; s++) { |
| 4754 | char c = *s; |
| 4755 | /* The Python parser ignores only the following whitespace |
| 4756 | characters (\r already is converted to \n). */ |
| 4757 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4758 | break; |
| 4759 | } |
| 4760 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4761 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4762 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4763 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4764 | } |
| 4765 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4766 | len = expr_end - expr_start; |
| 4767 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4768 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4769 | if (str == NULL) { |
| 4770 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4771 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4772 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4773 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4774 | str[0] = '('; |
| 4775 | memcpy(str+1, expr_start, len); |
| 4776 | str[len+1] = ')'; |
| 4777 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4778 | |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 4779 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4780 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4781 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4782 | Py_eval_input, 0); |
| 4783 | if (!mod_n) { |
| 4784 | PyMem_RawFree(str); |
| 4785 | return NULL; |
| 4786 | } |
| 4787 | /* Reuse str to find the correct column offset. */ |
| 4788 | str[0] = '{'; |
| 4789 | str[len+1] = '}'; |
| 4790 | fstring_fix_node_location(n, mod_n, str); |
| 4791 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4792 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4793 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4794 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4795 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4796 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /* Return -1 on error. |
| 4800 | |
| 4801 | Return 0 if we reached the end of the literal. |
| 4802 | |
| 4803 | Return 1 if we haven't reached the end of the literal, but we want |
| 4804 | the caller to process the literal up to this point. Used for |
| 4805 | doubled braces. |
| 4806 | */ |
| 4807 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4808 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4809 | PyObject **literal, int recurse_lvl, |
| 4810 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4811 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4812 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4813 | brace (which isn't part of a unicode name escape such as |
| 4814 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4815 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4816 | const char *s = *str; |
| 4817 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4818 | int result = 0; |
| 4819 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4820 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4821 | while (s < end) { |
| 4822 | char ch = *s++; |
| 4823 | if (!raw && ch == '\\' && s < end) { |
| 4824 | ch = *s++; |
| 4825 | if (ch == 'N') { |
| 4826 | if (s < end && *s++ == '{') { |
| 4827 | while (s < end && *s++ != '}') { |
| 4828 | } |
| 4829 | continue; |
| 4830 | } |
| 4831 | break; |
| 4832 | } |
| 4833 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4834 | return -1; |
| 4835 | } |
| 4836 | } |
| 4837 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4838 | /* Check for doubled braces, but only at the top level. If |
| 4839 | we checked at every level, then f'{0:{3}}' would fail |
| 4840 | with the two closing braces. */ |
| 4841 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4842 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4843 | /* We're going to tell the caller that the literal ends |
| 4844 | here, but that they should continue scanning. But also |
| 4845 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4846 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4847 | result = 1; |
| 4848 | goto done; |
| 4849 | } |
| 4850 | |
| 4851 | /* Where a single '{' is the start of a new expression, a |
| 4852 | single '}' is not allowed. */ |
| 4853 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4854 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4855 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4856 | return -1; |
| 4857 | } |
| 4858 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4859 | /* We're either at a '{', which means we're starting another |
| 4860 | expression; or a '}', which means we're at the end of this |
| 4861 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4862 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4863 | break; |
| 4864 | } |
| 4865 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4866 | *str = s; |
| 4867 | assert(s <= end); |
| 4868 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4869 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4870 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4871 | if (raw) |
| 4872 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4873 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4874 | NULL, NULL); |
| 4875 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4876 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4877 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4878 | if (!*literal) |
| 4879 | return -1; |
| 4880 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4881 | return result; |
| 4882 | } |
| 4883 | |
| 4884 | /* Forward declaration because parsing is recursive. */ |
| 4885 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4886 | 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] | 4887 | struct compiling *c, const node *n); |
| 4888 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4889 | /* 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] | 4890 | expression (so it must be a '{'). Returns the FormattedValue node, which |
| 4891 | includes the expression, conversion character, format_spec expression, and |
| 4892 | optionally the text of the expression (if = is used). |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4893 | |
| 4894 | Note that I don't do a perfect job here: I don't make sure that a |
| 4895 | closing brace doesn't match an opening paren, for example. It |
| 4896 | doesn't need to error on all invalid expressions, just correctly |
| 4897 | 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] | 4898 | will be caught when we parse it later. |
| 4899 | |
| 4900 | *expression is set to the expression. For an '=' "debug" expression, |
| 4901 | *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] | 4902 | including the '=' and any whitespace around it, as a string object). If |
| 4903 | not a debug expression, *expr_text set to NULL. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4904 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4905 | 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] | 4906 | PyObject **expr_text, expr_ty *expression, |
| 4907 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4908 | { |
| 4909 | /* Return -1 on error, else 0. */ |
| 4910 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4911 | const char *expr_start; |
| 4912 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4913 | expr_ty simple_expression; |
| 4914 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4915 | int conversion = -1; /* The conversion char. Use default if not |
| 4916 | specified, or !r if using = and no format |
| 4917 | spec. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4918 | |
| 4919 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4920 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4921 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4922 | |
| 4923 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4924 | int string_type = 0; |
| 4925 | |
| 4926 | /* Keep track of nesting level for braces/parens/brackets in |
| 4927 | expressions. */ |
| 4928 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 4929 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4930 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 4931 | *expr_text = NULL; |
| 4932 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4933 | /* Can only nest one level deep. */ |
| 4934 | if (recurse_lvl >= 2) { |
| 4935 | ast_error(c, n, "f-string: expressions nested too deeply"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4936 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4940 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4941 | assert(**str == '{'); |
| 4942 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4943 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4944 | expr_start = *str; |
| 4945 | for (; *str < end; (*str)++) { |
| 4946 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4947 | |
| 4948 | /* Loop invariants. */ |
| 4949 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4950 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4951 | if (quote_char) |
| 4952 | assert(string_type == 1 || string_type == 3); |
| 4953 | else |
| 4954 | assert(string_type == 0); |
| 4955 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4956 | ch = **str; |
| 4957 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4958 | if (ch == '\\') { |
| 4959 | /* Error: can't include a backslash character, inside |
| 4960 | parens or strings or not. */ |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 4961 | ast_error(c, n, |
| 4962 | "f-string expression part " |
| 4963 | "cannot include a backslash"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4964 | goto error; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4965 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4966 | if (quote_char) { |
| 4967 | /* We're inside a string. See if we're at the end. */ |
| 4968 | /* This code needs to implement the same non-error logic |
| 4969 | as tok_get from tokenizer.c, at the letter_quote |
| 4970 | label. To actually share that code would be a |
| 4971 | nightmare. But, it's unlikely to change and is small, |
| 4972 | so duplicate it here. Note we don't need to catch all |
| 4973 | of the errors, since they'll be caught when parsing the |
| 4974 | expression. We just need to match the non-error |
| 4975 | cases. Thus we can ignore \n in single-quoted strings, |
| 4976 | for example. Or non-terminated strings. */ |
| 4977 | if (ch == quote_char) { |
| 4978 | /* Does this match the string_type (single or triple |
| 4979 | quoted)? */ |
| 4980 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4981 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4982 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4983 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4984 | string_type = 0; |
| 4985 | quote_char = 0; |
| 4986 | continue; |
| 4987 | } |
| 4988 | } else { |
| 4989 | /* We're at the end of a normal string. */ |
| 4990 | quote_char = 0; |
| 4991 | string_type = 0; |
| 4992 | continue; |
| 4993 | } |
| 4994 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4995 | } else if (ch == '\'' || ch == '"') { |
| 4996 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4997 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4998 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4999 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5000 | } else { |
| 5001 | /* Start of a normal string. */ |
| 5002 | string_type = 1; |
| 5003 | } |
| 5004 | /* Start looking for the end of the string. */ |
| 5005 | quote_char = ch; |
| 5006 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5007 | if (nested_depth >= MAXLEVEL) { |
| 5008 | ast_error(c, n, "f-string: too many nested parenthesis"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5009 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5010 | } |
| 5011 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5012 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5013 | } else if (ch == '#') { |
| 5014 | /* Error: can't include a comment character, inside parens |
| 5015 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 5016 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5017 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5018 | } else if (nested_depth == 0 && |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5019 | (ch == '!' || ch == ':' || ch == '}' || |
| 5020 | ch == '=' || ch == '>' || ch == '<')) { |
| 5021 | /* See if there's a next character. */ |
| 5022 | if (*str+1 < end) { |
| 5023 | char next = *(*str+1); |
| 5024 | |
| 5025 | /* For "!=". since '=' is not an allowed conversion character, |
| 5026 | nothing is lost in this test. */ |
| 5027 | if ((ch == '!' && next == '=') || /* != */ |
| 5028 | (ch == '=' && next == '=') || /* == */ |
| 5029 | (ch == '<' && next == '=') || /* <= */ |
| 5030 | (ch == '>' && next == '=') /* >= */ |
| 5031 | ) { |
| 5032 | *str += 1; |
| 5033 | continue; |
| 5034 | } |
| 5035 | /* Don't get out of the loop for these, if they're single |
| 5036 | chars (not part of 2-char tokens). If by themselves, they |
| 5037 | don't end an expression (unlike say '!'). */ |
| 5038 | if (ch == '>' || ch == '<') { |
| 5039 | continue; |
| 5040 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5041 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5042 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5043 | /* Normal way out of this loop. */ |
| 5044 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5045 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 5046 | if (!nested_depth) { |
| 5047 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5048 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5049 | } |
| 5050 | nested_depth--; |
| 5051 | int opening = parenstack[nested_depth]; |
| 5052 | if (!((opening == '(' && ch == ')') || |
| 5053 | (opening == '[' && ch == ']') || |
| 5054 | (opening == '{' && ch == '}'))) |
| 5055 | { |
| 5056 | ast_error(c, n, |
| 5057 | "f-string: closing parenthesis '%c' " |
| 5058 | "does not match opening parenthesis '%c'", |
| 5059 | ch, opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5060 | goto error; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5061 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5062 | } else { |
| 5063 | /* Just consume this char and loop around. */ |
| 5064 | } |
| 5065 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5066 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5067 | /* If we leave this loop in a string or with mismatched parens, we |
| 5068 | don't care. We'll get a syntax error when compiling the |
| 5069 | expression. But, we can produce a better error message, so |
| 5070 | let's just do that.*/ |
| 5071 | if (quote_char) { |
| 5072 | ast_error(c, n, "f-string: unterminated string"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5073 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5074 | } |
| 5075 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame] | 5076 | int opening = parenstack[nested_depth - 1]; |
| 5077 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5078 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5079 | } |
| 5080 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5081 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5082 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5083 | |
| 5084 | /* Compile the expression as soon as possible, so we show errors |
| 5085 | related to the expression before errors related to the |
| 5086 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5087 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5088 | if (!simple_expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5089 | goto error; |
| 5090 | |
| 5091 | /* Check for =, which puts the text value of the expression in |
| 5092 | expr_text. */ |
| 5093 | if (**str == '=') { |
Pablo Galindo | 9b83829 | 2020-05-27 22:01:11 +0100 | [diff] [blame] | 5094 | if (c->c_feature_version < 8) { |
| 5095 | ast_error(c, n, |
| 5096 | "f-string: self documenting expressions are " |
| 5097 | "only supported in Python 3.8 and greater"); |
| 5098 | goto error; |
| 5099 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5100 | *str += 1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5101 | |
| 5102 | /* Skip over ASCII whitespace. No need to test for end of string |
| 5103 | here, since we know there's at least a trailing quote somewhere |
| 5104 | ahead. */ |
| 5105 | while (Py_ISSPACE(**str)) { |
| 5106 | *str += 1; |
| 5107 | } |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5108 | |
| 5109 | /* Set *expr_text to the text of the expression. */ |
| 5110 | *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); |
| 5111 | if (!*expr_text) { |
| 5112 | goto error; |
| 5113 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5114 | } |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 5115 | |
| 5116 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5117 | if (**str == '!') { |
| 5118 | *str += 1; |
| 5119 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5120 | goto unexpected_end_of_string; |
| 5121 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5122 | conversion = **str; |
| 5123 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5124 | |
| 5125 | /* Validate the conversion. */ |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5126 | if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5127 | ast_error(c, n, |
| 5128 | "f-string: invalid conversion character: " |
| 5129 | "expected 's', 'r', or 'a'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5130 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5131 | } |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5132 | |
| 5133 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5134 | |
| 5135 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5136 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5137 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5138 | if (**str == ':') { |
| 5139 | *str += 1; |
| 5140 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5141 | goto unexpected_end_of_string; |
| 5142 | |
| 5143 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5144 | 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] | 5145 | if (!format_spec) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5146 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5147 | } |
| 5148 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5149 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5150 | goto unexpected_end_of_string; |
| 5151 | |
| 5152 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5153 | assert(*str < end); |
| 5154 | assert(**str == '}'); |
| 5155 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5156 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5157 | /* 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] | 5158 | spec and no explicit conversion, set the conversion to 'r'. */ |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5159 | if (*expr_text && format_spec == NULL && conversion == -1) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5160 | conversion = 'r'; |
| 5161 | } |
| 5162 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5163 | /* And now create the FormattedValue node that represents this |
| 5164 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 5165 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5166 | format_spec, LINENO(n), |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5167 | n->n_col_offset, n->n_end_lineno, |
| 5168 | n->n_end_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5169 | if (!*expression) |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5170 | goto error; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5171 | |
| 5172 | return 0; |
| 5173 | |
| 5174 | unexpected_end_of_string: |
| 5175 | ast_error(c, n, "f-string: expecting '}'"); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5176 | /* Falls through to error. */ |
| 5177 | |
| 5178 | error: |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5179 | Py_XDECREF(*expr_text); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5180 | return -1; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 5181 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | /* Return -1 on error. |
| 5185 | |
| 5186 | Return 0 if we have a literal (possible zero length) and an |
| 5187 | expression (zero length if at the end of the string. |
| 5188 | |
| 5189 | Return 1 if we have a literal, but no expression, and we want the |
| 5190 | caller to call us again. This is used to deal with doubled |
| 5191 | braces. |
| 5192 | |
| 5193 | When called multiple times on the string 'a{{b{0}c', this function |
| 5194 | will return: |
| 5195 | |
| 5196 | 1. the literal 'a{' with no expression, and a return value |
| 5197 | of 1. Despite the fact that there's no expression, the return |
| 5198 | value of 1 means we're not finished yet. |
| 5199 | |
| 5200 | 2. the literal 'b' and the expression '0', with a return value of |
| 5201 | 0. The fact that there's an expression means we're not finished. |
| 5202 | |
| 5203 | 3. literal 'c' with no expression and a return value of 0. The |
| 5204 | combination of the return value of 0 with no expression means |
| 5205 | we're finished. |
| 5206 | */ |
| 5207 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5208 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 5209 | int recurse_lvl, PyObject **literal, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5210 | PyObject **expr_text, expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5211 | struct compiling *c, const node *n) |
| 5212 | { |
| 5213 | int result; |
| 5214 | |
| 5215 | assert(*literal == NULL && *expression == NULL); |
| 5216 | |
| 5217 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5218 | 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] | 5219 | if (result < 0) |
| 5220 | goto error; |
| 5221 | |
| 5222 | assert(result == 0 || result == 1); |
| 5223 | |
| 5224 | if (result == 1) |
| 5225 | /* We have a literal, but don't look at the expression. */ |
| 5226 | return 1; |
| 5227 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5228 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5229 | /* We're at the end of the string or the end of a nested |
| 5230 | f-string: no expression. The top-level error case where we |
| 5231 | expect to be at the end of the string but we're at a '}' is |
| 5232 | handled later. */ |
| 5233 | return 0; |
| 5234 | |
| 5235 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5236 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5237 | |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5238 | if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text, |
| 5239 | expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5240 | goto error; |
| 5241 | |
| 5242 | return 0; |
| 5243 | |
| 5244 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5245 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5246 | return -1; |
| 5247 | } |
| 5248 | |
| 5249 | #define EXPRLIST_N_CACHED 64 |
| 5250 | |
| 5251 | typedef struct { |
| 5252 | /* Incrementally build an array of expr_ty, so be used in an |
| 5253 | asdl_seq. Cache some small but reasonably sized number of |
| 5254 | expr_ty's, and then after that start dynamically allocating, |
| 5255 | doubling the number allocated each time. Note that the f-string |
| 5256 | 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] | 5257 | 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] | 5258 | fast as you add expressions in an f-string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5259 | |
| 5260 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 5261 | Py_ssize_t size; /* Number we've used. */ |
| 5262 | expr_ty *p; /* Pointer to the memory we're actually |
| 5263 | using. Will point to 'data' until we |
| 5264 | start dynamically allocating. */ |
| 5265 | expr_ty data[EXPRLIST_N_CACHED]; |
| 5266 | } ExprList; |
| 5267 | |
| 5268 | #ifdef NDEBUG |
| 5269 | #define ExprList_check_invariants(l) |
| 5270 | #else |
| 5271 | static void |
| 5272 | ExprList_check_invariants(ExprList *l) |
| 5273 | { |
| 5274 | /* Check our invariants. Make sure this object is "live", and |
| 5275 | hasn't been deallocated. */ |
| 5276 | assert(l->size >= 0); |
| 5277 | assert(l->p != NULL); |
| 5278 | if (l->size <= EXPRLIST_N_CACHED) |
| 5279 | assert(l->data == l->p); |
| 5280 | } |
| 5281 | #endif |
| 5282 | |
| 5283 | static void |
| 5284 | ExprList_Init(ExprList *l) |
| 5285 | { |
| 5286 | l->allocated = EXPRLIST_N_CACHED; |
| 5287 | l->size = 0; |
| 5288 | |
| 5289 | /* Until we start allocating dynamically, p points to data. */ |
| 5290 | l->p = l->data; |
| 5291 | |
| 5292 | ExprList_check_invariants(l); |
| 5293 | } |
| 5294 | |
| 5295 | static int |
| 5296 | ExprList_Append(ExprList *l, expr_ty exp) |
| 5297 | { |
| 5298 | ExprList_check_invariants(l); |
| 5299 | if (l->size >= l->allocated) { |
| 5300 | /* We need to alloc (or realloc) the memory. */ |
| 5301 | Py_ssize_t new_size = l->allocated * 2; |
| 5302 | |
| 5303 | /* See if we've ever allocated anything dynamically. */ |
| 5304 | if (l->p == l->data) { |
| 5305 | Py_ssize_t i; |
| 5306 | /* We're still using the cached data. Switch to |
| 5307 | alloc-ing. */ |
| 5308 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 5309 | if (!l->p) |
| 5310 | return -1; |
| 5311 | /* Copy the cached data into the new buffer. */ |
| 5312 | for (i = 0; i < l->size; i++) |
| 5313 | l->p[i] = l->data[i]; |
| 5314 | } else { |
| 5315 | /* Just realloc. */ |
| 5316 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 5317 | if (!tmp) { |
| 5318 | PyMem_RawFree(l->p); |
| 5319 | l->p = NULL; |
| 5320 | return -1; |
| 5321 | } |
| 5322 | l->p = tmp; |
| 5323 | } |
| 5324 | |
| 5325 | l->allocated = new_size; |
| 5326 | assert(l->allocated == 2 * l->size); |
| 5327 | } |
| 5328 | |
| 5329 | l->p[l->size++] = exp; |
| 5330 | |
| 5331 | ExprList_check_invariants(l); |
| 5332 | return 0; |
| 5333 | } |
| 5334 | |
| 5335 | static void |
| 5336 | ExprList_Dealloc(ExprList *l) |
| 5337 | { |
| 5338 | ExprList_check_invariants(l); |
| 5339 | |
| 5340 | /* If there's been an error, or we've never dynamically allocated, |
| 5341 | do nothing. */ |
| 5342 | if (!l->p || l->p == l->data) { |
| 5343 | /* Do nothing. */ |
| 5344 | } else { |
| 5345 | /* We have dynamically allocated. Free the memory. */ |
| 5346 | PyMem_RawFree(l->p); |
| 5347 | } |
| 5348 | l->p = NULL; |
| 5349 | l->size = -1; |
| 5350 | } |
| 5351 | |
| 5352 | static asdl_seq * |
| 5353 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 5354 | { |
| 5355 | asdl_seq *seq; |
| 5356 | |
| 5357 | ExprList_check_invariants(l); |
| 5358 | |
| 5359 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 5360 | seq = _Py_asdl_seq_new(l->size, arena); |
| 5361 | if (seq) { |
| 5362 | Py_ssize_t i; |
| 5363 | for (i = 0; i < l->size; i++) |
| 5364 | asdl_seq_SET(seq, i, l->p[i]); |
| 5365 | } |
| 5366 | ExprList_Dealloc(l); |
| 5367 | return seq; |
| 5368 | } |
| 5369 | |
| 5370 | /* The FstringParser is designed to add a mix of strings and |
| 5371 | f-strings, and concat them together as needed. Ultimately, it |
| 5372 | generates an expr_ty. */ |
| 5373 | typedef struct { |
| 5374 | PyObject *last_str; |
| 5375 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5376 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5377 | } FstringParser; |
| 5378 | |
| 5379 | #ifdef NDEBUG |
| 5380 | #define FstringParser_check_invariants(state) |
| 5381 | #else |
| 5382 | static void |
| 5383 | FstringParser_check_invariants(FstringParser *state) |
| 5384 | { |
| 5385 | if (state->last_str) |
| 5386 | assert(PyUnicode_CheckExact(state->last_str)); |
| 5387 | ExprList_check_invariants(&state->expr_list); |
| 5388 | } |
| 5389 | #endif |
| 5390 | |
| 5391 | static void |
| 5392 | FstringParser_Init(FstringParser *state) |
| 5393 | { |
| 5394 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5395 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5396 | ExprList_Init(&state->expr_list); |
| 5397 | FstringParser_check_invariants(state); |
| 5398 | } |
| 5399 | |
| 5400 | static void |
| 5401 | FstringParser_Dealloc(FstringParser *state) |
| 5402 | { |
| 5403 | FstringParser_check_invariants(state); |
| 5404 | |
| 5405 | Py_XDECREF(state->last_str); |
| 5406 | ExprList_Dealloc(&state->expr_list); |
| 5407 | } |
| 5408 | |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5409 | /* Constants for the following */ |
| 5410 | static PyObject *u_kind; |
| 5411 | |
| 5412 | /* Compute 'kind' field for string Constant (either 'u' or None) */ |
| 5413 | static PyObject * |
| 5414 | make_kind(struct compiling *c, const node *n) |
| 5415 | { |
| 5416 | char *s = NULL; |
| 5417 | PyObject *kind = NULL; |
| 5418 | |
| 5419 | /* Find the first string literal, if any */ |
| 5420 | while (TYPE(n) != STRING) { |
| 5421 | if (NCH(n) == 0) |
| 5422 | return NULL; |
| 5423 | n = CHILD(n, 0); |
| 5424 | } |
| 5425 | REQ(n, STRING); |
| 5426 | |
| 5427 | /* If it starts with 'u', return a PyUnicode "u" string */ |
| 5428 | s = STR(n); |
| 5429 | if (s && *s == 'u') { |
| 5430 | if (!u_kind) { |
| 5431 | u_kind = PyUnicode_InternFromString("u"); |
| 5432 | if (!u_kind) |
| 5433 | return NULL; |
| 5434 | } |
| 5435 | kind = u_kind; |
| 5436 | if (PyArena_AddPyObject(c->c_arena, kind) < 0) { |
| 5437 | return NULL; |
| 5438 | } |
| 5439 | Py_INCREF(kind); |
| 5440 | } |
| 5441 | return kind; |
| 5442 | } |
| 5443 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5444 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5445 | static expr_ty |
| 5446 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 5447 | { |
| 5448 | PyObject *s = *str; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5449 | PyObject *kind = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5450 | *str = NULL; |
| 5451 | assert(PyUnicode_CheckExact(s)); |
| 5452 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 5453 | Py_DECREF(s); |
| 5454 | return NULL; |
| 5455 | } |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5456 | kind = make_kind(c, n); |
| 5457 | if (kind == NULL && PyErr_Occurred()) |
| 5458 | return NULL; |
| 5459 | return Constant(s, kind, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5460 | 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] | 5461 | } |
| 5462 | |
| 5463 | /* Add a non-f-string (that is, a regular literal string). str is |
| 5464 | decref'd. */ |
| 5465 | static int |
| 5466 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 5467 | { |
| 5468 | FstringParser_check_invariants(state); |
| 5469 | |
| 5470 | assert(PyUnicode_CheckExact(str)); |
| 5471 | |
| 5472 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 5473 | Py_DECREF(str); |
| 5474 | return 0; |
| 5475 | } |
| 5476 | |
| 5477 | if (!state->last_str) { |
| 5478 | /* We didn't have a string before, so just remember this one. */ |
| 5479 | state->last_str = str; |
| 5480 | } else { |
| 5481 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 5482 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 5483 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5484 | return -1; |
| 5485 | } |
| 5486 | FstringParser_check_invariants(state); |
| 5487 | return 0; |
| 5488 | } |
| 5489 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5490 | /* Parse an f-string. The f-string is in *str to end, with no |
| 5491 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5492 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5493 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 5494 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5495 | struct compiling *c, const node *n) |
| 5496 | { |
| 5497 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5498 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5499 | |
| 5500 | /* Parse the f-string. */ |
| 5501 | while (1) { |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5502 | PyObject *literal = NULL; |
| 5503 | PyObject *expr_text = NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5504 | expr_ty expression = NULL; |
| 5505 | |
| 5506 | /* If there's a zero length literal in front of the |
| 5507 | expression, literal will be NULL. If we're at the end of |
| 5508 | the f-string, expression will be NULL (unless result == 1, |
| 5509 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5510 | 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] | 5511 | &literal, &expr_text, |
Eric V. Smith | 6f6ff8a | 2019-05-27 15:31:52 -0400 | [diff] [blame] | 5512 | &expression, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5513 | if (result < 0) |
| 5514 | return -1; |
| 5515 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5516 | /* Add the literal, if any. */ |
| 5517 | if (literal && FstringParser_ConcatAndDel(state, literal) < 0) { |
| 5518 | Py_XDECREF(expr_text); |
| 5519 | return -1; |
| 5520 | } |
| 5521 | /* Add the expr_text, if any. */ |
| 5522 | if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) { |
| 5523 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5524 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5525 | |
Eric V. Smith | f83d1db | 2019-05-29 03:55:44 -0400 | [diff] [blame] | 5526 | /* We've dealt with the literal and expr_text, their ownership has |
| 5527 | 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] | 5528 | |
| 5529 | /* See if we should just loop around to get the next literal |
| 5530 | and expression, while ignoring the expression this |
| 5531 | time. This is used for un-doubling braces, as an |
| 5532 | optimization. */ |
| 5533 | if (result == 1) |
| 5534 | continue; |
| 5535 | |
| 5536 | if (!expression) |
| 5537 | /* We're done with this f-string. */ |
| 5538 | break; |
| 5539 | |
| 5540 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5541 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5542 | if (!state->last_str) { |
| 5543 | /* Do nothing. No previous literal. */ |
| 5544 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5545 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5546 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5547 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5548 | return -1; |
| 5549 | } |
| 5550 | |
| 5551 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5552 | return -1; |
| 5553 | } |
| 5554 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5555 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5556 | string. Otherwise, we must be at a right brace. */ |
| 5557 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5558 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5559 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5560 | return -1; |
| 5561 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5562 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5563 | ast_error(c, n, "f-string: expecting '}'"); |
| 5564 | return -1; |
| 5565 | } |
| 5566 | |
| 5567 | FstringParser_check_invariants(state); |
| 5568 | return 0; |
| 5569 | } |
| 5570 | |
| 5571 | /* 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] | 5572 | 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] | 5573 | static expr_ty |
| 5574 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5575 | const node *n) |
| 5576 | { |
| 5577 | asdl_seq *seq; |
| 5578 | |
| 5579 | FstringParser_check_invariants(state); |
| 5580 | |
| 5581 | /* If we're just a constant string with no expressions, return |
| 5582 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5583 | if (!state->fmode) { |
| 5584 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5585 | if (!state->last_str) { |
| 5586 | /* Create a zero length string. */ |
| 5587 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5588 | if (!state->last_str) |
| 5589 | goto error; |
| 5590 | } |
| 5591 | return make_str_node_and_del(&state->last_str, c, n); |
| 5592 | } |
| 5593 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5594 | /* 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] | 5595 | last node in our expression list. */ |
| 5596 | if (state->last_str) { |
| 5597 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5598 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5599 | goto error; |
| 5600 | } |
| 5601 | /* This has already been freed. */ |
| 5602 | assert(state->last_str == NULL); |
| 5603 | |
| 5604 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5605 | if (!seq) |
| 5606 | goto error; |
| 5607 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5608 | return JoinedStr(seq, LINENO(n), n->n_col_offset, |
| 5609 | 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] | 5610 | |
| 5611 | error: |
| 5612 | FstringParser_Dealloc(state); |
| 5613 | return NULL; |
| 5614 | } |
| 5615 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5616 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5617 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5618 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5619 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5620 | 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] | 5621 | struct compiling *c, const node *n) |
| 5622 | { |
| 5623 | FstringParser state; |
| 5624 | |
| 5625 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5626 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5627 | c, n) < 0) { |
| 5628 | FstringParser_Dealloc(&state); |
| 5629 | return NULL; |
| 5630 | } |
| 5631 | |
| 5632 | return FstringParser_Finish(&state, c, n); |
| 5633 | } |
| 5634 | |
| 5635 | /* n is a Python string literal, including the bracketing quote |
| 5636 | 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] | 5637 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5638 | 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] | 5639 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5640 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5641 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5642 | static int |
| 5643 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5644 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5645 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5646 | size_t len; |
| 5647 | const char *s = STR(n); |
| 5648 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5649 | int fmode = 0; |
| 5650 | *bytesmode = 0; |
| 5651 | *rawmode = 0; |
| 5652 | *result = NULL; |
| 5653 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5654 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5655 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5656 | if (quote == 'b' || quote == 'B') { |
| 5657 | quote = *++s; |
| 5658 | *bytesmode = 1; |
| 5659 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5660 | else if (quote == 'u' || quote == 'U') { |
| 5661 | quote = *++s; |
| 5662 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5663 | else if (quote == 'r' || quote == 'R') { |
| 5664 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5665 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5666 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5667 | else if (quote == 'f' || quote == 'F') { |
| 5668 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5669 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5670 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5671 | else { |
| 5672 | break; |
| 5673 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5674 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5675 | } |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 5676 | |
| 5677 | /* fstrings are only allowed in Python 3.6 and greater */ |
| 5678 | if (fmode && c->c_feature_version < 6) { |
| 5679 | ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); |
| 5680 | return -1; |
| 5681 | } |
| 5682 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5683 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5684 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5685 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5686 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5687 | if (quote != '\'' && quote != '\"') { |
| 5688 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5689 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5690 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5691 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5692 | s++; |
| 5693 | len = strlen(s); |
| 5694 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5695 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5696 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5697 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5698 | } |
| 5699 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5700 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5701 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5702 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5703 | } |
| 5704 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5705 | /* A triple quoted string. We've already skipped one quote at |
| 5706 | the start and one at the end of the string. Now skip the |
| 5707 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5708 | s += 2; |
| 5709 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5710 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5711 | if (s[--len] != quote || s[--len] != quote) { |
| 5712 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5713 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5714 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5715 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5716 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5717 | if (fmode) { |
| 5718 | /* Just return the bytes. The caller will parse the resulting |
| 5719 | string. */ |
| 5720 | *fstr = s; |
| 5721 | *fstrlen = len; |
| 5722 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5723 | } |
| 5724 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5725 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5726 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5727 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5728 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5729 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5730 | const char *ch; |
| 5731 | for (ch = s; *ch; ch++) { |
| 5732 | if (Py_CHARMASK(*ch) >= 0x80) { |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 5733 | ast_error(c, n, |
| 5734 | "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5735 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5736 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5737 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5738 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5739 | if (*rawmode) |
| 5740 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5741 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5742 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5743 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5744 | if (*rawmode) |
| 5745 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5746 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5747 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5748 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5749 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5752 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5753 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5754 | 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] | 5755 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5756 | 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] | 5757 | node if there's just an f-string (with no leading or trailing |
| 5758 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5759 | any literals involved. */ |
| 5760 | static expr_ty |
| 5761 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5762 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5763 | int bytesmode = 0; |
| 5764 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5765 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5766 | |
| 5767 | FstringParser state; |
| 5768 | FstringParser_Init(&state); |
| 5769 | |
| 5770 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5771 | int this_bytesmode; |
| 5772 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5773 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5774 | const char *fstr; |
| 5775 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5776 | |
| 5777 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5778 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5779 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5780 | goto error; |
| 5781 | |
| 5782 | /* Check that we're not mixing bytes with unicode. */ |
| 5783 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5784 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5785 | /* s is NULL if the current string part is an f-string. */ |
| 5786 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5787 | goto error; |
| 5788 | } |
| 5789 | bytesmode = this_bytesmode; |
| 5790 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5791 | if (fstr != NULL) { |
| 5792 | int result; |
| 5793 | assert(s == NULL && !bytesmode); |
| 5794 | /* This is an f-string. Parse and concatenate it. */ |
| 5795 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5796 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5797 | if (result < 0) |
| 5798 | goto error; |
| 5799 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5800 | /* A string or byte string. */ |
| 5801 | assert(s != NULL && fstr == NULL); |
| 5802 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5803 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5804 | PyUnicode_CheckExact(s)); |
| 5805 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5806 | if (bytesmode) { |
| 5807 | /* For bytes, concat as we go. */ |
| 5808 | if (i == 0) { |
| 5809 | /* First time, just remember this value. */ |
| 5810 | bytes_str = s; |
| 5811 | } else { |
| 5812 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5813 | if (!bytes_str) |
| 5814 | goto error; |
| 5815 | } |
| 5816 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5817 | /* This is a regular string. Concatenate it. */ |
| 5818 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5819 | goto error; |
| 5820 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5821 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5822 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5823 | if (bytesmode) { |
| 5824 | /* Just return the bytes object and we're done. */ |
| 5825 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5826 | goto error; |
Guido van Rossum | 10f8ce6 | 2019-03-13 13:00:46 -0700 | [diff] [blame] | 5827 | return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5828 | 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] | 5829 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5830 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5831 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5832 | assert(bytes_str == NULL); |
| 5833 | |
| 5834 | return FstringParser_Finish(&state, c, n); |
| 5835 | |
| 5836 | error: |
| 5837 | Py_XDECREF(bytes_str); |
| 5838 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5839 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5840 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5841 | |
| 5842 | PyObject * |
| 5843 | _PyAST_GetDocString(asdl_seq *body) |
| 5844 | { |
| 5845 | if (!asdl_seq_LEN(body)) { |
| 5846 | return NULL; |
| 5847 | } |
| 5848 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5849 | if (st->kind != Expr_kind) { |
| 5850 | return NULL; |
| 5851 | } |
| 5852 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5853 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5854 | return e->v.Constant.value; |
| 5855 | } |
| 5856 | return NULL; |
| 5857 | } |