Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file includes functions to transform a concrete syntax tree (CST) to |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 3 | * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4 | * |
| 5 | */ |
| 6 | #include "Python.h" |
| 7 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 8 | #include "node.h" |
| 9 | #include "ast.h" |
| 10 | #include "token.h" |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 11 | #include "pythonrun.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 14 | #include <stdbool.h> |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 | |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame^] | 16 | #define MAXLEVEL 200 /* Max parentheses level */ |
| 17 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 18 | static int validate_stmts(asdl_seq *); |
| 19 | static int validate_exprs(asdl_seq *, expr_context_ty, int); |
| 20 | static int validate_nonempty_seq(asdl_seq *, const char *, const char *); |
| 21 | static int validate_stmt(stmt_ty); |
| 22 | static int validate_expr(expr_ty, expr_context_ty); |
| 23 | |
| 24 | static int |
| 25 | validate_comprehension(asdl_seq *gens) |
| 26 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 27 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 28 | if (!asdl_seq_LEN(gens)) { |
| 29 | PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); |
| 30 | return 0; |
| 31 | } |
| 32 | for (i = 0; i < asdl_seq_LEN(gens); i++) { |
| 33 | comprehension_ty comp = asdl_seq_GET(gens, i); |
| 34 | if (!validate_expr(comp->target, Store) || |
| 35 | !validate_expr(comp->iter, Load) || |
| 36 | !validate_exprs(comp->ifs, Load, 0)) |
| 37 | return 0; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | validate_slice(slice_ty slice) |
| 44 | { |
| 45 | switch (slice->kind) { |
| 46 | case Slice_kind: |
| 47 | return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) && |
| 48 | (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) && |
| 49 | (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load)); |
| 50 | case ExtSlice_kind: { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 51 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 52 | if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice")) |
| 53 | return 0; |
| 54 | for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++) |
| 55 | if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i))) |
| 56 | return 0; |
| 57 | return 1; |
| 58 | } |
| 59 | case Index_kind: |
| 60 | return validate_expr(slice->v.Index.value, Load); |
| 61 | default: |
| 62 | PyErr_SetString(PyExc_SystemError, "unknown slice node"); |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | validate_keywords(asdl_seq *keywords) |
| 69 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 70 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 71 | for (i = 0; i < asdl_seq_LEN(keywords); i++) |
| 72 | if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) |
| 73 | return 0; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | validate_args(asdl_seq *args) |
| 79 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 80 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 81 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 82 | arg_ty arg = asdl_seq_GET(args, i); |
| 83 | if (arg->annotation && !validate_expr(arg->annotation, Load)) |
| 84 | return 0; |
| 85 | } |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static const char * |
| 90 | expr_context_name(expr_context_ty ctx) |
| 91 | { |
| 92 | switch (ctx) { |
| 93 | case Load: |
| 94 | return "Load"; |
| 95 | case Store: |
| 96 | return "Store"; |
| 97 | case Del: |
| 98 | return "Del"; |
| 99 | case AugLoad: |
| 100 | return "AugLoad"; |
| 101 | case AugStore: |
| 102 | return "AugStore"; |
| 103 | case Param: |
| 104 | return "Param"; |
| 105 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 106 | Py_UNREACHABLE(); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | validate_arguments(arguments_ty args) |
| 112 | { |
| 113 | if (!validate_args(args->args)) |
| 114 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 115 | if (args->vararg && args->vararg->annotation |
| 116 | && !validate_expr(args->vararg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | if (!validate_args(args->kwonlyargs)) |
| 120 | return 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 121 | if (args->kwarg && args->kwarg->annotation |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 122 | && !validate_expr(args->kwarg->annotation, Load)) { |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { |
| 126 | PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); |
| 127 | return 0; |
| 128 | } |
| 129 | if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) { |
| 130 | PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as " |
| 131 | "kw_defaults on arguments"); |
| 132 | return 0; |
| 133 | } |
| 134 | return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1); |
| 135 | } |
| 136 | |
| 137 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 138 | validate_constant(PyObject *value) |
| 139 | { |
| 140 | if (value == Py_None || value == Py_Ellipsis) |
| 141 | return 1; |
| 142 | |
| 143 | if (PyLong_CheckExact(value) |
| 144 | || PyFloat_CheckExact(value) |
| 145 | || PyComplex_CheckExact(value) |
| 146 | || PyBool_Check(value) |
| 147 | || PyUnicode_CheckExact(value) |
| 148 | || PyBytes_CheckExact(value)) |
| 149 | return 1; |
| 150 | |
| 151 | if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) { |
| 152 | PyObject *it; |
| 153 | |
| 154 | it = PyObject_GetIter(value); |
| 155 | if (it == NULL) |
| 156 | return 0; |
| 157 | |
| 158 | while (1) { |
| 159 | PyObject *item = PyIter_Next(it); |
| 160 | if (item == NULL) { |
| 161 | if (PyErr_Occurred()) { |
| 162 | Py_DECREF(it); |
| 163 | return 0; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (!validate_constant(item)) { |
| 169 | Py_DECREF(it); |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 170 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
Victor Stinner | 726f690 | 2016-01-27 00:11:47 +0100 | [diff] [blame] | 173 | Py_DECREF(item); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | Py_DECREF(it); |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 184 | validate_expr(expr_ty exp, expr_context_ty ctx) |
| 185 | { |
| 186 | int check_ctx = 1; |
| 187 | expr_context_ty actual_ctx; |
| 188 | |
| 189 | /* First check expression context. */ |
| 190 | switch (exp->kind) { |
| 191 | case Attribute_kind: |
| 192 | actual_ctx = exp->v.Attribute.ctx; |
| 193 | break; |
| 194 | case Subscript_kind: |
| 195 | actual_ctx = exp->v.Subscript.ctx; |
| 196 | break; |
| 197 | case Starred_kind: |
| 198 | actual_ctx = exp->v.Starred.ctx; |
| 199 | break; |
| 200 | case Name_kind: |
| 201 | actual_ctx = exp->v.Name.ctx; |
| 202 | break; |
| 203 | case List_kind: |
| 204 | actual_ctx = exp->v.List.ctx; |
| 205 | break; |
| 206 | case Tuple_kind: |
| 207 | actual_ctx = exp->v.Tuple.ctx; |
| 208 | break; |
| 209 | default: |
| 210 | if (ctx != Load) { |
| 211 | PyErr_Format(PyExc_ValueError, "expression which can't be " |
| 212 | "assigned to in %s context", expr_context_name(ctx)); |
| 213 | return 0; |
| 214 | } |
| 215 | check_ctx = 0; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 216 | /* set actual_ctx to prevent gcc warning */ |
| 217 | actual_ctx = 0; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 218 | } |
| 219 | if (check_ctx && actual_ctx != ctx) { |
| 220 | PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead", |
| 221 | expr_context_name(ctx), expr_context_name(actual_ctx)); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* Now validate expression. */ |
| 226 | switch (exp->kind) { |
| 227 | case BoolOp_kind: |
| 228 | if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) { |
| 229 | PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values"); |
| 230 | return 0; |
| 231 | } |
| 232 | return validate_exprs(exp->v.BoolOp.values, Load, 0); |
| 233 | case BinOp_kind: |
| 234 | return validate_expr(exp->v.BinOp.left, Load) && |
| 235 | validate_expr(exp->v.BinOp.right, Load); |
| 236 | case UnaryOp_kind: |
| 237 | return validate_expr(exp->v.UnaryOp.operand, Load); |
| 238 | case Lambda_kind: |
| 239 | return validate_arguments(exp->v.Lambda.args) && |
| 240 | validate_expr(exp->v.Lambda.body, Load); |
| 241 | case IfExp_kind: |
| 242 | return validate_expr(exp->v.IfExp.test, Load) && |
| 243 | validate_expr(exp->v.IfExp.body, Load) && |
| 244 | validate_expr(exp->v.IfExp.orelse, Load); |
| 245 | case Dict_kind: |
| 246 | if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) { |
| 247 | PyErr_SetString(PyExc_ValueError, |
| 248 | "Dict doesn't have the same number of keys as values"); |
| 249 | return 0; |
| 250 | } |
Yury Selivanov | b3d5313 | 2015-09-01 16:10:49 -0400 | [diff] [blame] | 251 | /* null_ok=1 for keys expressions to allow dict unpacking to work in |
| 252 | dict literals, i.e. ``{**{a:b}}`` */ |
| 253 | return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) && |
| 254 | validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 255 | case Set_kind: |
| 256 | return validate_exprs(exp->v.Set.elts, Load, 0); |
| 257 | #define COMP(NAME) \ |
| 258 | case NAME ## _kind: \ |
| 259 | return validate_comprehension(exp->v.NAME.generators) && \ |
| 260 | validate_expr(exp->v.NAME.elt, Load); |
| 261 | COMP(ListComp) |
| 262 | COMP(SetComp) |
| 263 | COMP(GeneratorExp) |
| 264 | #undef COMP |
| 265 | case DictComp_kind: |
| 266 | return validate_comprehension(exp->v.DictComp.generators) && |
| 267 | validate_expr(exp->v.DictComp.key, Load) && |
| 268 | validate_expr(exp->v.DictComp.value, Load); |
| 269 | case Yield_kind: |
| 270 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 271 | case YieldFrom_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 272 | return validate_expr(exp->v.YieldFrom.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 273 | case Await_kind: |
| 274 | return validate_expr(exp->v.Await.value, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 275 | case Compare_kind: |
| 276 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { |
| 277 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); |
| 278 | return 0; |
| 279 | } |
| 280 | if (asdl_seq_LEN(exp->v.Compare.comparators) != |
| 281 | asdl_seq_LEN(exp->v.Compare.ops)) { |
| 282 | PyErr_SetString(PyExc_ValueError, "Compare has a different number " |
| 283 | "of comparators and operands"); |
| 284 | return 0; |
| 285 | } |
| 286 | return validate_exprs(exp->v.Compare.comparators, Load, 0) && |
| 287 | validate_expr(exp->v.Compare.left, Load); |
| 288 | case Call_kind: |
| 289 | return validate_expr(exp->v.Call.func, Load) && |
| 290 | validate_exprs(exp->v.Call.args, Load, 0) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 291 | validate_keywords(exp->v.Call.keywords); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 292 | case Constant_kind: |
| 293 | if (!validate_constant(exp->v.Constant.value)) { |
Victor Stinner | be59d14 | 2016-01-27 00:39:12 +0100 | [diff] [blame] | 294 | PyErr_Format(PyExc_TypeError, |
| 295 | "got an invalid type in Constant: %s", |
| 296 | Py_TYPE(exp->v.Constant.value)->tp_name); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | return 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 300 | case JoinedStr_kind: |
| 301 | return validate_exprs(exp->v.JoinedStr.values, Load, 0); |
| 302 | case FormattedValue_kind: |
| 303 | if (validate_expr(exp->v.FormattedValue.value, Load) == 0) |
| 304 | return 0; |
| 305 | if (exp->v.FormattedValue.format_spec) |
| 306 | return validate_expr(exp->v.FormattedValue.format_spec, Load); |
| 307 | return 1; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 308 | case Attribute_kind: |
| 309 | return validate_expr(exp->v.Attribute.value, Load); |
| 310 | case Subscript_kind: |
| 311 | return validate_slice(exp->v.Subscript.slice) && |
| 312 | validate_expr(exp->v.Subscript.value, Load); |
| 313 | case Starred_kind: |
| 314 | return validate_expr(exp->v.Starred.value, ctx); |
| 315 | case List_kind: |
| 316 | return validate_exprs(exp->v.List.elts, ctx, 0); |
| 317 | case Tuple_kind: |
| 318 | return validate_exprs(exp->v.Tuple.elts, ctx, 0); |
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; |
| 322 | default: |
| 323 | PyErr_SetString(PyExc_SystemError, "unexpected expression"); |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static int |
| 329 | validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner) |
| 330 | { |
| 331 | if (asdl_seq_LEN(seq)) |
| 332 | return 1; |
| 333 | PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int |
| 338 | validate_assignlist(asdl_seq *targets, expr_context_ty ctx) |
| 339 | { |
| 340 | return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") && |
| 341 | validate_exprs(targets, ctx, 0); |
| 342 | } |
| 343 | |
| 344 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 345 | validate_body(asdl_seq *body, const char *owner) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 346 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 347 | return validate_nonempty_seq(body, "body", owner) && validate_stmts(body); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static int |
| 351 | validate_stmt(stmt_ty stmt) |
| 352 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 353 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 354 | switch (stmt->kind) { |
| 355 | case FunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 356 | return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 357 | validate_arguments(stmt->v.FunctionDef.args) && |
| 358 | validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) && |
| 359 | (!stmt->v.FunctionDef.returns || |
| 360 | validate_expr(stmt->v.FunctionDef.returns, Load)); |
| 361 | case ClassDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 362 | return validate_body(stmt->v.ClassDef.body, "ClassDef") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 363 | validate_exprs(stmt->v.ClassDef.bases, Load, 0) && |
| 364 | validate_keywords(stmt->v.ClassDef.keywords) && |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 365 | validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 366 | case Return_kind: |
| 367 | return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load); |
| 368 | case Delete_kind: |
| 369 | return validate_assignlist(stmt->v.Delete.targets, Del); |
| 370 | case Assign_kind: |
| 371 | return validate_assignlist(stmt->v.Assign.targets, Store) && |
| 372 | validate_expr(stmt->v.Assign.value, Load); |
| 373 | case AugAssign_kind: |
| 374 | return validate_expr(stmt->v.AugAssign.target, Store) && |
| 375 | validate_expr(stmt->v.AugAssign.value, Load); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 376 | case AnnAssign_kind: |
| 377 | if (stmt->v.AnnAssign.target->kind != Name_kind && |
| 378 | stmt->v.AnnAssign.simple) { |
| 379 | PyErr_SetString(PyExc_TypeError, |
| 380 | "AnnAssign with simple non-Name target"); |
| 381 | return 0; |
| 382 | } |
| 383 | return validate_expr(stmt->v.AnnAssign.target, Store) && |
| 384 | (!stmt->v.AnnAssign.value || |
| 385 | validate_expr(stmt->v.AnnAssign.value, Load)) && |
| 386 | validate_expr(stmt->v.AnnAssign.annotation, Load); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 387 | case For_kind: |
| 388 | return validate_expr(stmt->v.For.target, Store) && |
| 389 | validate_expr(stmt->v.For.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 390 | validate_body(stmt->v.For.body, "For") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 391 | validate_stmts(stmt->v.For.orelse); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 392 | case AsyncFor_kind: |
| 393 | return validate_expr(stmt->v.AsyncFor.target, Store) && |
| 394 | validate_expr(stmt->v.AsyncFor.iter, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 395 | validate_body(stmt->v.AsyncFor.body, "AsyncFor") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 396 | validate_stmts(stmt->v.AsyncFor.orelse); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 397 | case While_kind: |
| 398 | return validate_expr(stmt->v.While.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 399 | validate_body(stmt->v.While.body, "While") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 400 | validate_stmts(stmt->v.While.orelse); |
| 401 | case If_kind: |
| 402 | return validate_expr(stmt->v.If.test, Load) && |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 403 | validate_body(stmt->v.If.body, "If") && |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 404 | validate_stmts(stmt->v.If.orelse); |
| 405 | case With_kind: |
| 406 | if (!validate_nonempty_seq(stmt->v.With.items, "items", "With")) |
| 407 | return 0; |
| 408 | for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) { |
| 409 | withitem_ty item = asdl_seq_GET(stmt->v.With.items, i); |
| 410 | if (!validate_expr(item->context_expr, Load) || |
| 411 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 412 | return 0; |
| 413 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 414 | return validate_body(stmt->v.With.body, "With"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 415 | case AsyncWith_kind: |
| 416 | if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith")) |
| 417 | return 0; |
| 418 | for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) { |
| 419 | withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i); |
| 420 | if (!validate_expr(item->context_expr, Load) || |
| 421 | (item->optional_vars && !validate_expr(item->optional_vars, Store))) |
| 422 | return 0; |
| 423 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 424 | return validate_body(stmt->v.AsyncWith.body, "AsyncWith"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 425 | case Raise_kind: |
| 426 | if (stmt->v.Raise.exc) { |
| 427 | return validate_expr(stmt->v.Raise.exc, Load) && |
| 428 | (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load)); |
| 429 | } |
| 430 | if (stmt->v.Raise.cause) { |
| 431 | PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception"); |
| 432 | return 0; |
| 433 | } |
| 434 | return 1; |
| 435 | case Try_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 436 | if (!validate_body(stmt->v.Try.body, "Try")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 437 | return 0; |
| 438 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 439 | !asdl_seq_LEN(stmt->v.Try.finalbody)) { |
| 440 | PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody"); |
| 441 | return 0; |
| 442 | } |
| 443 | if (!asdl_seq_LEN(stmt->v.Try.handlers) && |
| 444 | asdl_seq_LEN(stmt->v.Try.orelse)) { |
| 445 | PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers"); |
| 446 | return 0; |
| 447 | } |
| 448 | for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) { |
| 449 | excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i); |
| 450 | if ((handler->v.ExceptHandler.type && |
| 451 | !validate_expr(handler->v.ExceptHandler.type, Load)) || |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 452 | !validate_body(handler->v.ExceptHandler.body, "ExceptHandler")) |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | return (!asdl_seq_LEN(stmt->v.Try.finalbody) || |
| 456 | validate_stmts(stmt->v.Try.finalbody)) && |
| 457 | (!asdl_seq_LEN(stmt->v.Try.orelse) || |
| 458 | validate_stmts(stmt->v.Try.orelse)); |
| 459 | case Assert_kind: |
| 460 | return validate_expr(stmt->v.Assert.test, Load) && |
| 461 | (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load)); |
| 462 | case Import_kind: |
| 463 | return validate_nonempty_seq(stmt->v.Import.names, "names", "Import"); |
| 464 | case ImportFrom_kind: |
Serhiy Storchaka | fbd1523 | 2016-06-27 21:39:12 +0300 | [diff] [blame] | 465 | if (stmt->v.ImportFrom.level < 0) { |
| 466 | PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level"); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom"); |
| 470 | case Global_kind: |
| 471 | return validate_nonempty_seq(stmt->v.Global.names, "names", "Global"); |
| 472 | case Nonlocal_kind: |
| 473 | return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal"); |
| 474 | case Expr_kind: |
| 475 | return validate_expr(stmt->v.Expr.value, Load); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 476 | case AsyncFunctionDef_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 477 | return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") && |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 478 | validate_arguments(stmt->v.AsyncFunctionDef.args) && |
| 479 | validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) && |
| 480 | (!stmt->v.AsyncFunctionDef.returns || |
| 481 | validate_expr(stmt->v.AsyncFunctionDef.returns, Load)); |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 482 | case Pass_kind: |
| 483 | case Break_kind: |
| 484 | case Continue_kind: |
| 485 | return 1; |
| 486 | default: |
| 487 | PyErr_SetString(PyExc_SystemError, "unexpected statement"); |
| 488 | return 0; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static int |
| 493 | validate_stmts(asdl_seq *seq) |
| 494 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 495 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 496 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 497 | stmt_ty stmt = asdl_seq_GET(seq, i); |
| 498 | if (stmt) { |
| 499 | if (!validate_stmt(stmt)) |
| 500 | return 0; |
| 501 | } |
| 502 | else { |
| 503 | PyErr_SetString(PyExc_ValueError, |
| 504 | "None disallowed in statement list"); |
| 505 | return 0; |
| 506 | } |
| 507 | } |
| 508 | return 1; |
| 509 | } |
| 510 | |
| 511 | static int |
| 512 | validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) |
| 513 | { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 514 | Py_ssize_t i; |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 515 | for (i = 0; i < asdl_seq_LEN(exprs); i++) { |
| 516 | expr_ty expr = asdl_seq_GET(exprs, i); |
| 517 | if (expr) { |
| 518 | if (!validate_expr(expr, ctx)) |
| 519 | return 0; |
| 520 | } |
| 521 | else if (!null_ok) { |
| 522 | PyErr_SetString(PyExc_ValueError, |
| 523 | "None disallowed in expression list"); |
| 524 | return 0; |
| 525 | } |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 526 | |
Benjamin Peterson | 832bfe2 | 2011-08-09 16:15:04 -0500 | [diff] [blame] | 527 | } |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | int |
| 532 | PyAST_Validate(mod_ty mod) |
| 533 | { |
| 534 | int res = 0; |
| 535 | |
| 536 | switch (mod->kind) { |
| 537 | case Module_kind: |
| 538 | res = validate_stmts(mod->v.Module.body); |
| 539 | break; |
| 540 | case Interactive_kind: |
| 541 | res = validate_stmts(mod->v.Interactive.body); |
| 542 | break; |
| 543 | case Expression_kind: |
| 544 | res = validate_expr(mod->v.Expression.body, Load); |
| 545 | break; |
| 546 | case Suite_kind: |
| 547 | PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler"); |
| 548 | break; |
| 549 | default: |
| 550 | PyErr_SetString(PyExc_SystemError, "impossible module node"); |
| 551 | res = 0; |
| 552 | break; |
| 553 | } |
| 554 | return res; |
| 555 | } |
| 556 | |
Benjamin Peterson | d3af6e3 | 2012-01-16 09:56:35 -0500 | [diff] [blame] | 557 | /* 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] | 558 | #include "grammar.h" |
| 559 | #include "parsetok.h" |
| 560 | #include "graminit.h" |
| 561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | /* Data structure used internally */ |
| 563 | struct compiling { |
Eric V. Smith | 163b5c6 | 2015-08-21 09:40:38 -0400 | [diff] [blame] | 564 | PyArena *c_arena; /* Arena for allocating memory. */ |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 565 | PyObject *c_filename; /* filename */ |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 566 | PyObject *c_normalize; /* Normalization function from unicodedata. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | static asdl_seq *seq_for_testlist(struct compiling *, const node *); |
| 570 | static expr_ty ast_for_expr(struct compiling *, const node *); |
| 571 | static stmt_ty ast_for_stmt(struct compiling *, const node *); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 572 | 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] | 573 | static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
| 574 | expr_context_ty); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 575 | static expr_ty ast_for_testlist(struct compiling *, const node *); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 576 | static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 577 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 578 | static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); |
| 579 | static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 580 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 581 | /* Note different signature for ast_for_call */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 582 | static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, |
| 583 | const node *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 584 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 585 | static PyObject *parsenumber(struct compiling *, const char *); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 586 | static expr_ty parsestrplus(struct compiling *, const node *n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 587 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 588 | #define COMP_GENEXP 0 |
| 589 | #define COMP_LISTCOMP 1 |
| 590 | #define COMP_SETCOMP 2 |
| 591 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 592 | static int |
| 593 | init_normalization(struct compiling *c) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 594 | { |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 595 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 596 | if (!m) |
| 597 | return 0; |
| 598 | c->c_normalize = PyObject_GetAttrString(m, "normalize"); |
| 599 | Py_DECREF(m); |
| 600 | if (!c->c_normalize) |
| 601 | return 0; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 602 | return 1; |
| 603 | } |
| 604 | |
| 605 | static identifier |
Benjamin Peterson | d40528f | 2012-09-02 16:37:09 -0400 | [diff] [blame] | 606 | new_identifier(const char *n, struct compiling *c) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 607 | { |
Benjamin Peterson | c7dedb0 | 2012-09-02 16:36:01 -0400 | [diff] [blame] | 608 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 609 | if (!id) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 610 | return NULL; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 611 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
Benjamin Peterson | 5eda913 | 2012-01-16 09:47:42 -0500 | [diff] [blame] | 612 | assert(PyUnicode_IS_READY(id)); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 613 | /* Check whether there are non-ASCII characters in the |
| 614 | identifier; if so, normalize to NFKC. */ |
Benjamin Peterson | de5827d | 2012-01-16 09:55:53 -0500 | [diff] [blame] | 615 | if (!PyUnicode_IS_ASCII(id)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 616 | PyObject *id2; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 617 | _Py_IDENTIFIER(NFKC); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 618 | if (!c->c_normalize && !init_normalization(c)) { |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 619 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 620 | return NULL; |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 621 | } |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 622 | PyObject *form = _PyUnicode_FromId(&PyId_NFKC); |
| 623 | if (form == NULL) { |
| 624 | Py_DECREF(id); |
| 625 | return NULL; |
| 626 | } |
| 627 | PyObject *args[2] = {form, id}; |
| 628 | id2 = _PyObject_FastCall(c->c_normalize, args, 2); |
Benjamin Peterson | 0fa35ea | 2012-01-16 09:50:48 -0500 | [diff] [blame] | 629 | Py_DECREF(id); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 630 | if (!id2) |
| 631 | return NULL; |
Oren Milman | 7dc46d8 | 2017-09-30 20:16:24 +0300 | [diff] [blame] | 632 | if (!PyUnicode_Check(id2)) { |
| 633 | PyErr_Format(PyExc_TypeError, |
| 634 | "unicodedata.normalize() must return a string, not " |
| 635 | "%.200s", |
| 636 | Py_TYPE(id2)->tp_name); |
| 637 | Py_DECREF(id2); |
| 638 | return NULL; |
| 639 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | id = id2; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 641 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 642 | PyUnicode_InternInPlace(&id); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 643 | if (PyArena_AddPyObject(c->c_arena, id) < 0) { |
| 644 | Py_DECREF(id); |
| 645 | return NULL; |
| 646 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 647 | return id; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 650 | #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 651 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | static int |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 653 | ast_error(struct compiling *c, const node *n, const char *errmsg, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 655 | PyObject *value, *errstr, *loc, *tmp; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 656 | va_list va; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 657 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 658 | va_start(va, errmsg); |
| 659 | errstr = PyUnicode_FromFormatV(errmsg, va); |
| 660 | va_end(va); |
| 661 | if (!errstr) { |
| 662 | return 0; |
| 663 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 664 | loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 665 | if (!loc) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 666 | Py_INCREF(Py_None); |
| 667 | loc = Py_None; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | } |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 669 | 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] | 670 | if (!tmp) { |
| 671 | Py_DECREF(errstr); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 672 | return 0; |
Neal Norwitz | 8ad64aa | 2005-12-11 20:08:33 +0000 | [diff] [blame] | 673 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 674 | value = PyTuple_Pack(2, errstr, tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 675 | Py_DECREF(errstr); |
| 676 | Py_DECREF(tmp); |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 677 | if (value) { |
| 678 | PyErr_SetObject(PyExc_SyntaxError, value); |
| 679 | Py_DECREF(value); |
| 680 | } |
| 681 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* num_stmts() returns number of contained statements. |
| 685 | |
| 686 | Use this routine to determine how big a sequence is needed for |
| 687 | the statements in a parse tree. Its raison d'etre is this bit of |
| 688 | grammar: |
| 689 | |
| 690 | stmt: simple_stmt | compound_stmt |
| 691 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 692 | |
| 693 | A simple_stmt can contain multiple small_stmt elements joined |
| 694 | by semicolons. If the arg is a simple_stmt, the number of |
| 695 | small_stmt elements is returned. |
| 696 | */ |
| 697 | |
| 698 | static int |
| 699 | num_stmts(const node *n) |
| 700 | { |
| 701 | int i, l; |
| 702 | node *ch; |
| 703 | |
| 704 | switch (TYPE(n)) { |
| 705 | case single_input: |
| 706 | if (TYPE(CHILD(n, 0)) == NEWLINE) |
| 707 | return 0; |
| 708 | else |
| 709 | return num_stmts(CHILD(n, 0)); |
| 710 | case file_input: |
| 711 | l = 0; |
| 712 | for (i = 0; i < NCH(n); i++) { |
| 713 | ch = CHILD(n, i); |
| 714 | if (TYPE(ch) == stmt) |
| 715 | l += num_stmts(ch); |
| 716 | } |
| 717 | return l; |
| 718 | case stmt: |
| 719 | return num_stmts(CHILD(n, 0)); |
| 720 | case compound_stmt: |
| 721 | return 1; |
| 722 | case simple_stmt: |
| 723 | return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ |
| 724 | case suite: |
| 725 | if (NCH(n) == 1) |
| 726 | return num_stmts(CHILD(n, 0)); |
| 727 | else { |
| 728 | l = 0; |
| 729 | for (i = 2; i < (NCH(n) - 1); i++) |
| 730 | l += num_stmts(CHILD(n, i)); |
| 731 | return l; |
| 732 | } |
| 733 | default: { |
| 734 | char buf[128]; |
| 735 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 736 | sprintf(buf, "Non-statement found: %d %d", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 737 | TYPE(n), NCH(n)); |
| 738 | Py_FatalError(buf); |
| 739 | } |
| 740 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 741 | Py_UNREACHABLE(); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* Transform the CST rooted at node * to the appropriate AST |
| 745 | */ |
| 746 | |
| 747 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 748 | PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, |
| 749 | PyObject *filename, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 750 | { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 751 | int i, j, k, num; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 752 | asdl_seq *stmts = NULL; |
| 753 | stmt_ty s; |
| 754 | node *ch; |
| 755 | struct compiling c; |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 756 | mod_ty res = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 757 | |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 758 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 759 | /* borrowed reference */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 760 | c.c_filename = filename; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 761 | c.c_normalize = NULL; |
Benjamin Peterson | 9d66d4a | 2016-02-25 23:25:14 -0800 | [diff] [blame] | 762 | |
| 763 | if (TYPE(n) == encoding_decl) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 764 | n = CHILD(n, 0); |
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 | k = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | switch (TYPE(n)) { |
| 768 | case file_input: |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 769 | stmts = _Py_asdl_seq_new(num_stmts(n), arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 770 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 771 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 772 | for (i = 0; i < NCH(n) - 1; i++) { |
| 773 | ch = CHILD(n, i); |
| 774 | if (TYPE(ch) == NEWLINE) |
| 775 | continue; |
| 776 | REQ(ch, stmt); |
| 777 | num = num_stmts(ch); |
| 778 | if (num == 1) { |
| 779 | s = ast_for_stmt(&c, ch); |
| 780 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 781 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 782 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | } |
| 784 | else { |
| 785 | ch = CHILD(ch, 0); |
| 786 | REQ(ch, simple_stmt); |
| 787 | for (j = 0; j < num; j++) { |
| 788 | s = ast_for_stmt(&c, CHILD(ch, j * 2)); |
| 789 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 790 | goto out; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 791 | asdl_seq_SET(stmts, k++, s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 795 | res = Module(stmts, arena); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 796 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 797 | case eval_input: { |
| 798 | expr_ty testlist_ast; |
| 799 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 800 | /* XXX Why not comp_for here? */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 801 | testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 802 | if (!testlist_ast) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 803 | goto out; |
| 804 | res = Expression(testlist_ast, arena); |
| 805 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 806 | } |
| 807 | case single_input: |
| 808 | if (TYPE(CHILD(n, 0)) == NEWLINE) { |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 809 | stmts = _Py_asdl_seq_new(1, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 810 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 811 | goto out; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 812 | asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
| 813 | arena)); |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 814 | if (!asdl_seq_GET(stmts, 0)) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 815 | goto out; |
| 816 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 817 | } |
| 818 | else { |
| 819 | n = CHILD(n, 0); |
| 820 | num = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 821 | stmts = _Py_asdl_seq_new(num, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 | if (!stmts) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 823 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 824 | if (num == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 825 | s = ast_for_stmt(&c, n); |
| 826 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 827 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 828 | asdl_seq_SET(stmts, 0, s); |
| 829 | } |
| 830 | else { |
| 831 | /* Only a simple_stmt can contain multiple statements. */ |
| 832 | REQ(n, simple_stmt); |
| 833 | for (i = 0; i < NCH(n); i += 2) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 834 | if (TYPE(CHILD(n, i)) == NEWLINE) |
| 835 | break; |
| 836 | s = ast_for_stmt(&c, CHILD(n, i)); |
| 837 | if (!s) |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 838 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 | asdl_seq_SET(stmts, i / 2, s); |
| 840 | } |
| 841 | } |
| 842 | |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 843 | res = Interactive(stmts, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | } |
Benjamin Peterson | c8909dd | 2012-01-16 17:44:12 -0500 | [diff] [blame] | 845 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 846 | default: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 847 | PyErr_Format(PyExc_SystemError, |
| 848 | "invalid node %d for PyAST_FromNode", TYPE(n)); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 849 | goto out; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 850 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 851 | out: |
| 852 | if (c.c_normalize) { |
| 853 | Py_DECREF(c.c_normalize); |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 854 | } |
Benjamin Peterson | 55e0043 | 2012-01-16 17:22:31 -0500 | [diff] [blame] | 855 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 858 | mod_ty |
| 859 | PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, |
| 860 | PyArena *arena) |
| 861 | { |
| 862 | mod_ty mod; |
| 863 | PyObject *filename; |
| 864 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 865 | if (filename == NULL) |
| 866 | return NULL; |
| 867 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
| 868 | Py_DECREF(filename); |
| 869 | return mod; |
| 870 | |
| 871 | } |
| 872 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) |
| 874 | */ |
| 875 | |
| 876 | static operator_ty |
| 877 | get_operator(const node *n) |
| 878 | { |
| 879 | switch (TYPE(n)) { |
| 880 | case VBAR: |
| 881 | return BitOr; |
| 882 | case CIRCUMFLEX: |
| 883 | return BitXor; |
| 884 | case AMPER: |
| 885 | return BitAnd; |
| 886 | case LEFTSHIFT: |
| 887 | return LShift; |
| 888 | case RIGHTSHIFT: |
| 889 | return RShift; |
| 890 | case PLUS: |
| 891 | return Add; |
| 892 | case MINUS: |
| 893 | return Sub; |
| 894 | case STAR: |
| 895 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 896 | case AT: |
| 897 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 898 | case SLASH: |
| 899 | return Div; |
| 900 | case DOUBLESLASH: |
| 901 | return FloorDiv; |
| 902 | case PERCENT: |
| 903 | return Mod; |
| 904 | default: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 905 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 909 | static const char * const FORBIDDEN[] = { |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 910 | "None", |
| 911 | "True", |
| 912 | "False", |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 913 | "__debug__", |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 914 | NULL, |
| 915 | }; |
| 916 | |
| 917 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 918 | forbidden_name(struct compiling *c, identifier name, const node *n, |
| 919 | int full_checks) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 920 | { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 921 | assert(PyUnicode_Check(name)); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 922 | const char * const *p = FORBIDDEN; |
| 923 | if (!full_checks) { |
| 924 | /* In most cases, the parser will protect True, False, and None |
| 925 | from being assign to. */ |
| 926 | p += 3; |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 927 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 928 | for (; *p; p++) { |
| 929 | if (_PyUnicode_EqualToASCIIString(name, *p)) { |
| 930 | ast_error(c, n, "cannot assign to %U", name); |
| 931 | return 1; |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | return 0; |
| 935 | } |
| 936 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 937 | static expr_ty |
| 938 | copy_location(expr_ty e, const node *n) |
| 939 | { |
| 940 | if (e) { |
| 941 | e->lineno = LINENO(n); |
| 942 | e->col_offset = n->n_col_offset; |
| 943 | } |
| 944 | return e; |
| 945 | } |
| 946 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 947 | /* Set the context ctx for expr_ty e, recursively traversing e. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 948 | |
| 949 | Only sets context for expr kinds that "can appear in assignment context" |
| 950 | (according to ../Parser/Python.asdl). For other expr kinds, it sets |
| 951 | an appropriate syntax error and returns false. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 952 | */ |
| 953 | |
| 954 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 955 | 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] | 956 | { |
| 957 | asdl_seq *s = NULL; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 958 | /* If a particular expression type can't be used for assign / delete, |
| 959 | set expr_name to its name and an error message will be generated. |
| 960 | */ |
| 961 | const char* expr_name = NULL; |
| 962 | |
| 963 | /* The ast defines augmented store and load contexts, but the |
| 964 | implementation here doesn't actually use them. The code may be |
| 965 | a little more complex than necessary as a result. It also means |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 966 | that expressions in an augmented assignment have a Store context. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 967 | Consider restructuring so that augmented assignment uses |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 968 | set_context(), too. |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 969 | */ |
| 970 | assert(ctx != AugStore && ctx != AugLoad); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 971 | |
| 972 | switch (e->kind) { |
| 973 | case Attribute_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 974 | e->v.Attribute.ctx = ctx; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 975 | if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 976 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 977 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 978 | case Subscript_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 979 | e->v.Subscript.ctx = ctx; |
| 980 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 981 | case Starred_kind: |
| 982 | e->v.Starred.ctx = ctx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 983 | if (!set_context(c, e->v.Starred.value, ctx, n)) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 984 | return 0; |
| 985 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 986 | case Name_kind: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 987 | if (ctx == Store) { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 988 | if (forbidden_name(c, e->v.Name.id, n, 0)) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 989 | return 0; /* forbidden_name() calls ast_error() */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 990 | } |
| 991 | e->v.Name.ctx = ctx; |
| 992 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 993 | case List_kind: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 994 | e->v.List.ctx = ctx; |
| 995 | s = e->v.List.elts; |
| 996 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 997 | case Tuple_kind: |
Berker Peksag | 094c9c9 | 2016-05-18 08:44:29 +0300 | [diff] [blame] | 998 | e->v.Tuple.ctx = ctx; |
| 999 | s = e->v.Tuple.elts; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1000 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1001 | case Lambda_kind: |
| 1002 | expr_name = "lambda"; |
| 1003 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1004 | case Call_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1005 | expr_name = "function call"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1006 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1007 | case BoolOp_kind: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1008 | case BinOp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1009 | case UnaryOp_kind: |
| 1010 | expr_name = "operator"; |
| 1011 | break; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1012 | case GeneratorExp_kind: |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1013 | expr_name = "generator expression"; |
| 1014 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1015 | case Yield_kind: |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 1016 | case YieldFrom_kind: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1017 | expr_name = "yield expression"; |
| 1018 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1019 | case Await_kind: |
| 1020 | expr_name = "await expression"; |
| 1021 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1022 | case ListComp_kind: |
| 1023 | expr_name = "list comprehension"; |
| 1024 | break; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1025 | case SetComp_kind: |
| 1026 | expr_name = "set comprehension"; |
| 1027 | break; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1028 | case DictComp_kind: |
| 1029 | expr_name = "dict comprehension"; |
| 1030 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1031 | case Dict_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1032 | expr_name = "dict display"; |
| 1033 | break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1034 | case Set_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1035 | expr_name = "set display"; |
| 1036 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1037 | case JoinedStr_kind: |
| 1038 | case FormattedValue_kind: |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1039 | expr_name = "f-string expression"; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1040 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1041 | case Constant_kind: { |
| 1042 | PyObject *value = e->v.Constant.value; |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1043 | if (value == Py_None || value == Py_False || value == Py_True |
| 1044 | || value == Py_Ellipsis) |
| 1045 | { |
| 1046 | return ast_error(c, n, "cannot %s %R", |
| 1047 | ctx == Store ? "assign to" : "delete", |
| 1048 | value); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1049 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1050 | expr_name = "literal"; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 1051 | break; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1052 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1053 | case Compare_kind: |
| 1054 | expr_name = "comparison"; |
| 1055 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1056 | case IfExp_kind: |
| 1057 | expr_name = "conditional expression"; |
| 1058 | break; |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1059 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | PyErr_Format(PyExc_SystemError, |
| 1061 | "unexpected expression in assignment %d (line %d)", |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1062 | e->kind, e->lineno); |
| 1063 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1064 | } |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1065 | /* Check for error string set by switch */ |
| 1066 | if (expr_name) { |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1067 | return ast_error(c, n, "cannot %s %s", |
| 1068 | ctx == Store ? "assign to" : "delete", |
| 1069 | expr_name); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1072 | /* 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] | 1073 | context for all the contained elements. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1074 | */ |
| 1075 | if (s) { |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 1076 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1077 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1078 | for (i = 0; i < asdl_seq_LEN(s); i++) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1079 | 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] | 1080 | return 0; |
| 1081 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1082 | } |
| 1083 | return 1; |
| 1084 | } |
| 1085 | |
| 1086 | static operator_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1087 | ast_for_augassign(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1088 | { |
| 1089 | REQ(n, augassign); |
| 1090 | n = CHILD(n, 0); |
| 1091 | switch (STR(n)[0]) { |
| 1092 | case '+': |
| 1093 | return Add; |
| 1094 | case '-': |
| 1095 | return Sub; |
| 1096 | case '/': |
| 1097 | if (STR(n)[1] == '/') |
| 1098 | return FloorDiv; |
| 1099 | else |
| 1100 | return Div; |
| 1101 | case '%': |
| 1102 | return Mod; |
| 1103 | case '<': |
| 1104 | return LShift; |
| 1105 | case '>': |
| 1106 | return RShift; |
| 1107 | case '&': |
| 1108 | return BitAnd; |
| 1109 | case '^': |
| 1110 | return BitXor; |
| 1111 | case '|': |
| 1112 | return BitOr; |
| 1113 | case '*': |
| 1114 | if (STR(n)[1] == '*') |
| 1115 | return Pow; |
| 1116 | else |
| 1117 | return Mult; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1118 | case '@': |
| 1119 | return MatMult; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1120 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1121 | PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1122 | return (operator_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | static cmpop_ty |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1127 | ast_for_comp_op(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1128 | { |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 1129 | /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1130 | |'is' 'not' |
| 1131 | */ |
| 1132 | REQ(n, comp_op); |
| 1133 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1134 | n = CHILD(n, 0); |
| 1135 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1136 | case LESS: |
| 1137 | return Lt; |
| 1138 | case GREATER: |
| 1139 | return Gt; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1140 | case EQEQUAL: /* == */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1141 | return Eq; |
| 1142 | case LESSEQUAL: |
| 1143 | return LtE; |
| 1144 | case GREATEREQUAL: |
| 1145 | return GtE; |
| 1146 | case NOTEQUAL: |
| 1147 | return NotEq; |
| 1148 | case NAME: |
| 1149 | if (strcmp(STR(n), "in") == 0) |
| 1150 | return In; |
| 1151 | if (strcmp(STR(n), "is") == 0) |
| 1152 | return Is; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1153 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1154 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1155 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1156 | STR(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1157 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1158 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1159 | } |
| 1160 | else if (NCH(n) == 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1161 | /* handle "not in" and "is not" */ |
| 1162 | switch (TYPE(CHILD(n, 0))) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1163 | case NAME: |
| 1164 | if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1165 | return NotIn; |
| 1166 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1167 | return IsNot; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1168 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1169 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1170 | PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1172 | return (cmpop_ty)0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1173 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1174 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1175 | PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1176 | NCH(n)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1177 | return (cmpop_ty)0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | static asdl_seq * |
| 1181 | seq_for_testlist(struct compiling *c, const node *n) |
| 1182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | /* testlist: test (',' test)* [','] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1184 | testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
| 1185 | */ |
Armin Rigo | 3144130 | 2005-10-21 12:57:31 +0000 | [diff] [blame] | 1186 | asdl_seq *seq; |
| 1187 | expr_ty expression; |
| 1188 | int i; |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1189 | 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] | 1190 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1191 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1192 | if (!seq) |
| 1193 | return NULL; |
| 1194 | |
| 1195 | for (i = 0; i < NCH(n); i += 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | const node *ch = CHILD(n, i); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1197 | assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1198 | |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 1199 | expression = ast_for_expr(c, ch); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1200 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1201 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1202 | |
| 1203 | assert(i / 2 < seq->size); |
| 1204 | asdl_seq_SET(seq, i / 2, expression); |
| 1205 | } |
| 1206 | return seq; |
| 1207 | } |
| 1208 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1209 | static arg_ty |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1210 | ast_for_arg(struct compiling *c, const node *n) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1211 | { |
| 1212 | identifier name; |
| 1213 | expr_ty annotation = NULL; |
| 1214 | node *ch; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1215 | arg_ty ret; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1216 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1217 | assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1218 | ch = CHILD(n, 0); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1219 | name = NEW_IDENTIFIER(ch); |
| 1220 | if (!name) |
| 1221 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1222 | if (forbidden_name(c, name, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1223 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1224 | |
| 1225 | if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
| 1226 | annotation = ast_for_expr(c, CHILD(n, 2)); |
| 1227 | if (!annotation) |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1231 | ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1232 | if (!ret) |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1233 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 1234 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1237 | /* returns -1 if failed to handle keyword only arguments |
| 1238 | returns new position to keep processing if successful |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1239 | (',' tfpdef ['=' test])* |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1240 | ^^^ |
| 1241 | start pointing here |
| 1242 | */ |
| 1243 | static int |
| 1244 | handle_keywordonly_args(struct compiling *c, const node *n, int start, |
| 1245 | asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
| 1246 | { |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1247 | PyObject *argname; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1248 | node *ch; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1249 | expr_ty expression, annotation; |
| 1250 | arg_ty arg; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1251 | int i = start; |
| 1252 | int j = 0; /* index for kwdefaults and kwonlyargs */ |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1253 | |
| 1254 | if (kwonlyargs == NULL) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1255 | 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] | 1256 | return -1; |
| 1257 | } |
| 1258 | assert(kwdefaults != NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1259 | while (i < NCH(n)) { |
| 1260 | ch = CHILD(n, i); |
| 1261 | switch (TYPE(ch)) { |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1262 | case vfpdef: |
| 1263 | case tfpdef: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1264 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1265 | expression = ast_for_expr(c, CHILD(n, i + 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1266 | if (!expression) |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1267 | goto error; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1268 | asdl_seq_SET(kwdefaults, j, expression); |
| 1269 | i += 2; /* '=' and test */ |
| 1270 | } |
| 1271 | else { /* setting NULL if no default value exists */ |
| 1272 | asdl_seq_SET(kwdefaults, j, NULL); |
| 1273 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1274 | if (NCH(ch) == 3) { |
| 1275 | /* ch is NAME ':' test */ |
| 1276 | annotation = ast_for_expr(c, CHILD(ch, 2)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1277 | if (!annotation) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1278 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1279 | } |
| 1280 | else { |
| 1281 | annotation = NULL; |
| 1282 | } |
| 1283 | ch = CHILD(ch, 0); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1284 | argname = NEW_IDENTIFIER(ch); |
| 1285 | if (!argname) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1286 | goto error; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1287 | if (forbidden_name(c, argname, ch, 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1288 | goto error; |
Victor Stinner | c106c68 | 2015-11-06 17:01:48 +0100 | [diff] [blame] | 1289 | arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset, |
| 1290 | c->c_arena); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 1291 | if (!arg) |
| 1292 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1293 | asdl_seq_SET(kwonlyargs, j++, arg); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1294 | i += 2; /* the name and the comma */ |
| 1295 | break; |
| 1296 | case DOUBLESTAR: |
| 1297 | return i; |
| 1298 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1299 | ast_error(c, ch, "unexpected node"); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1300 | goto error; |
| 1301 | } |
| 1302 | } |
| 1303 | return i; |
| 1304 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | return -1; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1306 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1307 | |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1308 | /* Create AST for argument list. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1309 | |
| 1310 | static arguments_ty |
| 1311 | ast_for_arguments(struct compiling *c, const node *n) |
| 1312 | { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1313 | /* This function handles both typedargslist (function definition) |
| 1314 | and varargslist (lambda definition). |
| 1315 | |
| 1316 | parameters: '(' [typedargslist] ')' |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1317 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 1318 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1319 | | '**' tfpdef [',']]] |
| 1320 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 1321 | | '**' tfpdef [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1322 | tfpdef: NAME [':' test] |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1323 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 1324 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1325 | | '**' vfpdef [',']]] |
| 1326 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 1327 | | '**' vfpdef [','] |
| 1328 | ) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1329 | vfpdef: NAME |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1330 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1331 | */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1332 | int i, j, k, nposargs = 0, nkwonlyargs = 0; |
| 1333 | int nposdefaults = 0, found_default = 0; |
| 1334 | asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1335 | arg_ty vararg = NULL, kwarg = NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1336 | arg_ty arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1337 | node *ch; |
| 1338 | |
| 1339 | if (TYPE(n) == parameters) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1340 | if (NCH(n) == 2) /* () as argument list */ |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1341 | return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1342 | n = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1343 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1344 | assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1345 | |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1346 | /* First count the number of positional args & defaults. The |
| 1347 | variable i is the loop index for this for loop and the next. |
| 1348 | The next loop picks up where the first leaves off. |
| 1349 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1350 | for (i = 0; i < NCH(n); i++) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1351 | ch = CHILD(n, i); |
| 1352 | if (TYPE(ch) == STAR) { |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1353 | /* skip star */ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1354 | i++; |
Jeremy Hylton | e921e02 | 2008-07-17 16:37:17 +0000 | [diff] [blame] | 1355 | if (i < NCH(n) && /* skip argument following star */ |
| 1356 | (TYPE(CHILD(n, i)) == tfpdef || |
| 1357 | TYPE(CHILD(n, i)) == vfpdef)) { |
| 1358 | i++; |
| 1359 | } |
| 1360 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1361 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1362 | if (TYPE(ch) == DOUBLESTAR) break; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1363 | if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1364 | if (TYPE(ch) == EQUAL) nposdefaults++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1365 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | /* count the number of keyword only args & |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1367 | defaults for keyword only args */ |
| 1368 | for ( ; i < NCH(n); ++i) { |
| 1369 | ch = CHILD(n, i); |
| 1370 | if (TYPE(ch) == DOUBLESTAR) break; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1371 | if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1372 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1373 | posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1374 | if (!posargs && nposargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1375 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1376 | kwonlyargs = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1377 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1378 | if (!kwonlyargs && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1379 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | posdefaults = (nposdefaults ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1381 | _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1382 | if (!posdefaults && nposdefaults) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1383 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | /* The length of kwonlyargs and kwdefaults are same |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1385 | since we set NULL as default for keyword only argument w/o default |
| 1386 | - we have sequence data structure, but no dictionary */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1387 | kwdefaults = (nkwonlyargs ? |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1388 | _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1389 | if (!kwdefaults && nkwonlyargs) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1390 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1391 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1392 | /* tfpdef: NAME [':' test] |
| 1393 | vfpdef: NAME |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1394 | */ |
| 1395 | i = 0; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1396 | j = 0; /* index for defaults */ |
| 1397 | k = 0; /* index for args */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1398 | while (i < NCH(n)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1399 | ch = CHILD(n, i); |
| 1400 | switch (TYPE(ch)) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1401 | case tfpdef: |
| 1402 | case vfpdef: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1403 | /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 1404 | anything other than EQUAL or a comma? */ |
| 1405 | /* XXX Should NCH(n) check be made a separate check? */ |
| 1406 | if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1407 | expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
| 1408 | if (!expression) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1409 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1410 | assert(posdefaults != NULL); |
| 1411 | asdl_seq_SET(posdefaults, j++, expression); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1412 | i += 2; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1413 | found_default = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1414 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1415 | else if (found_default) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1416 | ast_error(c, n, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1417 | "non-default argument follows default argument"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1418 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1419 | } |
Benjamin Peterson | bc4665e | 2012-03-12 11:00:41 -0700 | [diff] [blame] | 1420 | arg = ast_for_arg(c, ch); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1421 | if (!arg) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1422 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1423 | asdl_seq_SET(posargs, k++, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1424 | i += 2; /* the name and the comma */ |
| 1425 | break; |
| 1426 | case STAR: |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 1427 | if (i+1 >= NCH(n) || |
| 1428 | (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1429 | ast_error(c, CHILD(n, i), |
Amaury Forgeot d'Arc | 40d3a67 | 2007-12-09 21:49:48 +0000 | [diff] [blame] | 1430 | "named arguments must follow bare *"); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1431 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1432 | } |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1433 | ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1434 | if (TYPE(ch) == COMMA) { |
| 1435 | int res = 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1436 | i += 2; /* now follows keyword only arguments */ |
| 1437 | res = handle_keywordonly_args(c, n, i, |
| 1438 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1439 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1440 | i = res; /* res has new position to process */ |
| 1441 | } |
| 1442 | else { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1443 | vararg = ast_for_arg(c, ch); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 1444 | if (!vararg) |
| 1445 | return NULL; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1446 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1447 | i += 3; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1448 | if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
| 1449 | || TYPE(CHILD(n, i)) == vfpdef)) { |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1450 | int res = 0; |
| 1451 | res = handle_keywordonly_args(c, n, i, |
| 1452 | kwonlyargs, kwdefaults); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1453 | if (res == -1) return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1454 | i = res; /* res has new position to process */ |
| 1455 | } |
| 1456 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1457 | break; |
| 1458 | case DOUBLESTAR: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1459 | ch = CHILD(n, i+1); /* tfpdef */ |
| 1460 | assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1461 | kwarg = ast_for_arg(c, ch); |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1462 | if (!kwarg) |
| 1463 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1464 | i += 3; |
| 1465 | break; |
| 1466 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 1467 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1468 | "unexpected node in varargslist: %d @ %d", |
| 1469 | TYPE(ch), i); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1470 | return NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1471 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1472 | } |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1473 | return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | static expr_ty |
| 1477 | ast_for_dotted_name(struct compiling *c, const node *n) |
| 1478 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1479 | expr_ty e; |
| 1480 | identifier id; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1481 | int lineno, col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1482 | int i; |
| 1483 | |
| 1484 | REQ(n, dotted_name); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1485 | |
| 1486 | lineno = LINENO(n); |
| 1487 | col_offset = n->n_col_offset; |
| 1488 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1489 | id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 1490 | if (!id) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1491 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1492 | e = Name(id, Load, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1494 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1495 | |
| 1496 | for (i = 2; i < NCH(n); i+=2) { |
| 1497 | id = NEW_IDENTIFIER(CHILD(n, i)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1498 | if (!id) |
| 1499 | return NULL; |
| 1500 | e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
| 1501 | if (!e) |
| 1502 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | return e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static expr_ty |
| 1509 | ast_for_decorator(struct compiling *c, const node *n) |
| 1510 | { |
| 1511 | /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ |
| 1512 | expr_ty d = NULL; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1513 | expr_ty name_expr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1515 | REQ(n, decorator); |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1516 | REQ(CHILD(n, 0), AT); |
| 1517 | REQ(RCHILD(n, -1), NEWLINE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1519 | name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 1520 | if (!name_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1521 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1523 | if (NCH(n) == 3) { /* No arguments */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1524 | d = name_expr; |
| 1525 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1526 | } |
| 1527 | else if (NCH(n) == 5) { /* Call with no arguments */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1528 | d = Call(name_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1529 | n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1530 | if (!d) |
| 1531 | return NULL; |
| 1532 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1533 | } |
| 1534 | else { |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 1535 | d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1536 | if (!d) |
| 1537 | return NULL; |
| 1538 | name_expr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | return d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static asdl_seq* |
| 1545 | ast_for_decorators(struct compiling *c, const node *n) |
| 1546 | { |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1547 | asdl_seq* decorator_seq; |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 1548 | expr_ty d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1549 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1551 | REQ(n, decorators); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1552 | decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1553 | if (!decorator_seq) |
| 1554 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1556 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1557 | d = ast_for_decorator(c, CHILD(n, i)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1558 | if (!d) |
| 1559 | return NULL; |
| 1560 | asdl_seq_SET(decorator_seq, i, d); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1561 | } |
| 1562 | return decorator_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1566 | ast_for_funcdef_impl(struct compiling *c, const node *n0, |
| 1567 | asdl_seq *decorator_seq, bool is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1568 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1569 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1570 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1571 | identifier name; |
| 1572 | arguments_ty args; |
| 1573 | asdl_seq *body; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1574 | expr_ty returns = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1575 | int name_i = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1576 | |
| 1577 | REQ(n, funcdef); |
| 1578 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1579 | name = NEW_IDENTIFIER(CHILD(n, name_i)); |
| 1580 | if (!name) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1581 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 1582 | if (forbidden_name(c, name, CHILD(n, name_i), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 1583 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1584 | args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1585 | if (!args) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1586 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1587 | if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
| 1588 | returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
| 1589 | if (!returns) |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 1590 | return NULL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1591 | name_i += 2; |
| 1592 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1593 | body = ast_for_suite(c, CHILD(n, name_i + 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1594 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1595 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1596 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1597 | if (is_async) |
| 1598 | return AsyncFunctionDef(name, args, body, decorator_seq, returns, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 1599 | LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1600 | else |
| 1601 | return FunctionDef(name, args, body, decorator_seq, returns, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1602 | LINENO(n), n->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static stmt_ty |
| 1606 | ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1607 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1608 | /* async_funcdef: 'async' funcdef */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1609 | REQ(n, async_funcdef); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1610 | REQ(CHILD(n, 0), NAME); |
| 1611 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1612 | REQ(CHILD(n, 1), funcdef); |
| 1613 | |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1614 | return ast_for_funcdef_impl(c, n, decorator_seq, |
| 1615 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | static stmt_ty |
| 1619 | ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 1620 | { |
| 1621 | /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
| 1622 | return ast_for_funcdef_impl(c, n, decorator_seq, |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1623 | false /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | static stmt_ty |
| 1628 | ast_for_async_stmt(struct compiling *c, const node *n) |
| 1629 | { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1630 | /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1631 | REQ(n, async_stmt); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1632 | REQ(CHILD(n, 0), NAME); |
| 1633 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1634 | |
| 1635 | switch (TYPE(CHILD(n, 1))) { |
| 1636 | case funcdef: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1637 | return ast_for_funcdef_impl(c, n, NULL, |
| 1638 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1639 | case with_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1640 | return ast_for_with_stmt(c, n, |
| 1641 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1642 | |
| 1643 | case for_stmt: |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 1644 | return ast_for_for_stmt(c, n, |
| 1645 | true /* is_async */); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1646 | |
| 1647 | default: |
| 1648 | PyErr_Format(PyExc_SystemError, |
| 1649 | "invalid async stament: %s", |
| 1650 | STR(CHILD(n, 1))); |
| 1651 | return NULL; |
| 1652 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1655 | static stmt_ty |
| 1656 | ast_for_decorated(struct compiling *c, const node *n) |
| 1657 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1658 | /* decorated: decorators (classdef | funcdef | async_funcdef) */ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1659 | stmt_ty thing = NULL; |
| 1660 | asdl_seq *decorator_seq = NULL; |
| 1661 | |
| 1662 | REQ(n, decorated); |
| 1663 | |
| 1664 | decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
| 1665 | if (!decorator_seq) |
| 1666 | return NULL; |
| 1667 | |
| 1668 | assert(TYPE(CHILD(n, 1)) == funcdef || |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1669 | TYPE(CHILD(n, 1)) == async_funcdef || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | TYPE(CHILD(n, 1)) == classdef); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1671 | |
| 1672 | if (TYPE(CHILD(n, 1)) == funcdef) { |
| 1673 | thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
| 1674 | } else if (TYPE(CHILD(n, 1)) == classdef) { |
| 1675 | thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1676 | } else if (TYPE(CHILD(n, 1)) == async_funcdef) { |
| 1677 | thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq); |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1678 | } |
| 1679 | return thing; |
| 1680 | } |
| 1681 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1682 | static expr_ty |
| 1683 | ast_for_lambdef(struct compiling *c, const node *n) |
| 1684 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1685 | /* lambdef: 'lambda' [varargslist] ':' test |
| 1686 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1687 | arguments_ty args; |
| 1688 | expr_ty expression; |
| 1689 | |
| 1690 | if (NCH(n) == 3) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1691 | args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1692 | if (!args) |
| 1693 | return NULL; |
| 1694 | expression = ast_for_expr(c, CHILD(n, 2)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1695 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1696 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1697 | } |
| 1698 | else { |
| 1699 | args = ast_for_arguments(c, CHILD(n, 1)); |
| 1700 | if (!args) |
| 1701 | return NULL; |
| 1702 | expression = ast_for_expr(c, CHILD(n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1703 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1704 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 1707 | return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1710 | static expr_ty |
| 1711 | ast_for_ifexpr(struct compiling *c, const node *n) |
| 1712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | /* test: or_test 'if' or_test 'else' test */ |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1714 | expr_ty expression, body, orelse; |
| 1715 | |
Thomas Wouters | aa8b6c5 | 2006-02-27 16:46:22 +0000 | [diff] [blame] | 1716 | assert(NCH(n) == 5); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1717 | body = ast_for_expr(c, CHILD(n, 0)); |
| 1718 | if (!body) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1719 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1720 | expression = ast_for_expr(c, CHILD(n, 2)); |
| 1721 | if (!expression) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1722 | return NULL; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1723 | orelse = ast_for_expr(c, CHILD(n, 4)); |
| 1724 | if (!orelse) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1725 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1726 | return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
| 1727 | c->c_arena); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1730 | /* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1731 | Count the number of 'for' loops in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1733 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1734 | */ |
| 1735 | |
| 1736 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1737 | count_comp_fors(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1738 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1739 | int n_fors = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1740 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1741 | count_comp_for: |
| 1742 | n_fors++; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1743 | REQ(n, comp_for); |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1744 | if (NCH(n) == 2) { |
| 1745 | REQ(CHILD(n, 0), NAME); |
| 1746 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1747 | n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1748 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1749 | else if (NCH(n) == 1) { |
| 1750 | n = CHILD(n, 0); |
| 1751 | } |
| 1752 | else { |
| 1753 | goto error; |
| 1754 | } |
| 1755 | if (NCH(n) == (5)) { |
| 1756 | n = CHILD(n, 4); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1757 | } |
| 1758 | else { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1759 | return n_fors; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1760 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1761 | count_comp_iter: |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1762 | REQ(n, comp_iter); |
| 1763 | n = CHILD(n, 0); |
| 1764 | if (TYPE(n) == comp_for) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1765 | goto count_comp_for; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1766 | else if (TYPE(n) == comp_if) { |
| 1767 | if (NCH(n) == 3) { |
| 1768 | n = CHILD(n, 2); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1769 | goto count_comp_iter; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1770 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1771 | else |
| 1772 | return n_fors; |
| 1773 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 1774 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1775 | error: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1776 | /* Should never be reached */ |
| 1777 | PyErr_SetString(PyExc_SystemError, |
| 1778 | "logic error in count_comp_fors"); |
| 1779 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1782 | /* Count the number of 'if' statements in a comprehension. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1783 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1784 | Helper for ast_for_comprehension(). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1785 | */ |
| 1786 | |
| 1787 | static int |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1788 | count_comp_ifs(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1789 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1790 | int n_ifs = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1791 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1792 | while (1) { |
| 1793 | REQ(n, comp_iter); |
| 1794 | if (TYPE(CHILD(n, 0)) == comp_for) |
| 1795 | return n_ifs; |
| 1796 | n = CHILD(n, 0); |
| 1797 | REQ(n, comp_if); |
| 1798 | n_ifs++; |
| 1799 | if (NCH(n) == 2) |
| 1800 | return n_ifs; |
| 1801 | n = CHILD(n, 2); |
| 1802 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1805 | static asdl_seq * |
| 1806 | ast_for_comprehension(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1807 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1808 | int i, n_fors; |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1809 | asdl_seq *comps; |
| 1810 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1811 | n_fors = count_comp_fors(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1812 | if (n_fors == -1) |
| 1813 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1814 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1815 | comps = _Py_asdl_seq_new(n_fors, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1816 | if (!comps) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1817 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1818 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | for (i = 0; i < n_fors; i++) { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1820 | comprehension_ty comp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1821 | asdl_seq *t; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 1822 | expr_ty expression, first; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1823 | node *for_ch; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1824 | node *sync_n; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1825 | int is_async = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1827 | REQ(n, comp_for); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1829 | if (NCH(n) == 2) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1830 | is_async = 1; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1831 | REQ(CHILD(n, 0), NAME); |
| 1832 | assert(strcmp(STR(CHILD(n, 0)), "async") == 0); |
| 1833 | sync_n = CHILD(n, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1834 | } |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1835 | else { |
| 1836 | sync_n = CHILD(n, 0); |
| 1837 | } |
| 1838 | REQ(sync_n, sync_comp_for); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1839 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1840 | for_ch = CHILD(sync_n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1841 | t = ast_for_exprlist(c, for_ch, Store); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1842 | if (!t) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1843 | return NULL; |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1844 | expression = ast_for_expr(c, CHILD(sync_n, 3)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1845 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1846 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1847 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1848 | /* Check the # of children rather than the length of t, since |
| 1849 | (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] | 1850 | first = (expr_ty)asdl_seq_GET(t, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1851 | if (NCH(for_ch) == 1) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1852 | comp = comprehension(first, expression, NULL, |
| 1853 | is_async, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1854 | else |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1855 | comp = comprehension(Tuple(t, Store, first->lineno, |
| 1856 | first->col_offset, c->c_arena), |
| 1857 | expression, NULL, is_async, c->c_arena); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1858 | if (!comp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1859 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1860 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1861 | if (NCH(sync_n) == 5) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1862 | int j, n_ifs; |
| 1863 | asdl_seq *ifs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 1865 | n = CHILD(sync_n, 4); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1866 | n_ifs = count_comp_ifs(c, n); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1867 | if (n_ifs == -1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1868 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 1870 | ifs = _Py_asdl_seq_new(n_ifs, c->c_arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1871 | if (!ifs) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1872 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1873 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1874 | for (j = 0; j < n_ifs; j++) { |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1875 | REQ(n, comp_iter); |
| 1876 | n = CHILD(n, 0); |
| 1877 | REQ(n, comp_if); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1879 | expression = ast_for_expr(c, CHILD(n, 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1880 | if (!expression) |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 1881 | return NULL; |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 1882 | asdl_seq_SET(ifs, j, expression); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1883 | if (NCH(n) == 3) |
| 1884 | n = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1885 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1886 | /* on exit, must guarantee that n is a comp_for */ |
| 1887 | if (TYPE(n) == comp_iter) |
| 1888 | n = CHILD(n, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1889 | comp->ifs = ifs; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1890 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1891 | asdl_seq_SET(comps, i, comp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1892 | } |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1893 | return comps; |
| 1894 | } |
| 1895 | |
| 1896 | static expr_ty |
| 1897 | ast_for_itercomp(struct compiling *c, const node *n, int type) |
| 1898 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1899 | /* testlist_comp: (test|star_expr) |
| 1900 | * ( comp_for | (',' (test|star_expr))* [','] ) */ |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1901 | expr_ty elt; |
| 1902 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1903 | node *ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1904 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1905 | assert(NCH(n) > 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1907 | ch = CHILD(n, 0); |
| 1908 | elt = ast_for_expr(c, ch); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1909 | if (!elt) |
| 1910 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1911 | if (elt->kind == Starred_kind) { |
| 1912 | ast_error(c, ch, "iterable unpacking cannot be used in comprehension"); |
| 1913 | return NULL; |
| 1914 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1916 | comps = ast_for_comprehension(c, CHILD(n, 1)); |
| 1917 | if (!comps) |
| 1918 | return NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1919 | |
| 1920 | if (type == COMP_GENEXP) |
| 1921 | return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1922 | else if (type == COMP_LISTCOMP) |
| 1923 | return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1924 | else if (type == COMP_SETCOMP) |
| 1925 | return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1926 | else |
| 1927 | /* Should never happen */ |
| 1928 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1931 | /* Fills in the key, value pair corresponding to the dict element. In case |
| 1932 | * of an unpacking, key is NULL. *i is advanced by the number of ast |
| 1933 | * elements. Iff successful, nonzero is returned. |
| 1934 | */ |
| 1935 | static int |
| 1936 | ast_for_dictelement(struct compiling *c, const node *n, int *i, |
| 1937 | expr_ty *key, expr_ty *value) |
| 1938 | { |
| 1939 | expr_ty expression; |
| 1940 | if (TYPE(CHILD(n, *i)) == DOUBLESTAR) { |
| 1941 | assert(NCH(n) - *i >= 2); |
| 1942 | |
| 1943 | expression = ast_for_expr(c, CHILD(n, *i + 1)); |
| 1944 | if (!expression) |
| 1945 | return 0; |
| 1946 | *key = NULL; |
| 1947 | *value = expression; |
| 1948 | |
| 1949 | *i += 2; |
| 1950 | } |
| 1951 | else { |
| 1952 | assert(NCH(n) - *i >= 3); |
| 1953 | |
| 1954 | expression = ast_for_expr(c, CHILD(n, *i)); |
| 1955 | if (!expression) |
| 1956 | return 0; |
| 1957 | *key = expression; |
| 1958 | |
| 1959 | REQ(CHILD(n, *i + 1), COLON); |
| 1960 | |
| 1961 | expression = ast_for_expr(c, CHILD(n, *i + 2)); |
| 1962 | if (!expression) |
| 1963 | return 0; |
| 1964 | *value = expression; |
| 1965 | |
| 1966 | *i += 3; |
| 1967 | } |
| 1968 | return 1; |
| 1969 | } |
| 1970 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1971 | static expr_ty |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1972 | ast_for_dictcomp(struct compiling *c, const node *n) |
| 1973 | { |
| 1974 | expr_ty key, value; |
| 1975 | asdl_seq *comps; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1976 | int i = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1978 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1979 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1980 | assert(key); |
| 1981 | assert(NCH(n) - i >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1983 | comps = ast_for_comprehension(c, CHILD(n, i)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1984 | if (!comps) |
| 1985 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1987 | return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
| 1988 | } |
| 1989 | |
| 1990 | static expr_ty |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1991 | ast_for_dictdisplay(struct compiling *c, const node *n) |
| 1992 | { |
| 1993 | int i; |
| 1994 | int j; |
| 1995 | int size; |
| 1996 | asdl_seq *keys, *values; |
| 1997 | |
| 1998 | size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */ |
| 1999 | keys = _Py_asdl_seq_new(size, c->c_arena); |
| 2000 | if (!keys) |
| 2001 | return NULL; |
| 2002 | |
| 2003 | values = _Py_asdl_seq_new(size, c->c_arena); |
| 2004 | if (!values) |
| 2005 | return NULL; |
| 2006 | |
| 2007 | j = 0; |
| 2008 | for (i = 0; i < NCH(n); i++) { |
| 2009 | expr_ty key, value; |
| 2010 | |
| 2011 | if (!ast_for_dictelement(c, n, &i, &key, &value)) |
| 2012 | return NULL; |
| 2013 | asdl_seq_SET(keys, j, key); |
| 2014 | asdl_seq_SET(values, j, value); |
| 2015 | |
| 2016 | j++; |
| 2017 | } |
| 2018 | keys->size = j; |
| 2019 | values->size = j; |
| 2020 | return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
| 2021 | } |
| 2022 | |
| 2023 | static expr_ty |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2024 | ast_for_genexp(struct compiling *c, const node *n) |
| 2025 | { |
| 2026 | assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2027 | return ast_for_itercomp(c, n, COMP_GENEXP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | static expr_ty |
| 2031 | ast_for_listcomp(struct compiling *c, const node *n) |
| 2032 | { |
| 2033 | assert(TYPE(n) == (testlist_comp)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2034 | return ast_for_itercomp(c, n, COMP_LISTCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | static expr_ty |
| 2038 | ast_for_setcomp(struct compiling *c, const node *n) |
| 2039 | { |
| 2040 | assert(TYPE(n) == (dictorsetmaker)); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 2041 | return ast_for_itercomp(c, n, COMP_SETCOMP); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2044 | static expr_ty |
| 2045 | ast_for_setdisplay(struct compiling *c, const node *n) |
| 2046 | { |
| 2047 | int i; |
| 2048 | int size; |
| 2049 | asdl_seq *elts; |
| 2050 | |
| 2051 | assert(TYPE(n) == (dictorsetmaker)); |
| 2052 | size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */ |
| 2053 | elts = _Py_asdl_seq_new(size, c->c_arena); |
| 2054 | if (!elts) |
| 2055 | return NULL; |
| 2056 | for (i = 0; i < NCH(n); i += 2) { |
| 2057 | expr_ty expression; |
| 2058 | expression = ast_for_expr(c, CHILD(n, i)); |
| 2059 | if (!expression) |
| 2060 | return NULL; |
| 2061 | asdl_seq_SET(elts, i / 2, expression); |
| 2062 | } |
| 2063 | return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
| 2064 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2065 | |
| 2066 | static expr_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2067 | ast_for_atom(struct compiling *c, const node *n) |
| 2068 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2069 | /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
| 2070 | | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2071 | | '...' | 'None' | 'True' | 'False' |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2072 | */ |
| 2073 | node *ch = CHILD(n, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2075 | switch (TYPE(ch)) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2076 | case NAME: { |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2077 | PyObject *name; |
| 2078 | const char *s = STR(ch); |
| 2079 | size_t len = strlen(s); |
| 2080 | if (len >= 4 && len <= 5) { |
| 2081 | if (!strcmp(s, "None")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2082 | return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2083 | if (!strcmp(s, "True")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2084 | return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2085 | if (!strcmp(s, "False")) |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2086 | return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2087 | } |
| 2088 | name = new_identifier(s, c); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2089 | if (!name) |
| 2090 | return NULL; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 2091 | /* All names start in Load context, but may later be changed. */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2092 | return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2093 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2094 | case STRING: { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2095 | expr_ty str = parsestrplus(c, n); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2096 | if (!str) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 2097 | const char *errtype = NULL; |
| 2098 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) |
| 2099 | errtype = "unicode error"; |
| 2100 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 2101 | errtype = "value error"; |
| 2102 | if (errtype) { |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2103 | PyObject *type, *value, *tback, *errstr; |
| 2104 | PyErr_Fetch(&type, &value, &tback); |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 2105 | errstr = PyObject_Str(value); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2106 | if (errstr) { |
| 2107 | ast_error(c, n, "(%s) %U", errtype, errstr); |
| 2108 | Py_DECREF(errstr); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2109 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2110 | else { |
| 2111 | PyErr_Clear(); |
| 2112 | ast_error(c, n, "(%s) unknown error", errtype); |
| 2113 | } |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2114 | Py_DECREF(type); |
Victor Stinner | 0fae8f9 | 2013-07-17 21:51:42 +0200 | [diff] [blame] | 2115 | Py_XDECREF(value); |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2116 | Py_XDECREF(tback); |
| 2117 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2118 | return NULL; |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 2119 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 2120 | return str; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2121 | } |
| 2122 | case NUMBER: { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2123 | PyObject *pynum = parsenumber(c, STR(ch)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2124 | if (!pynum) |
| 2125 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2126 | |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 2127 | if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { |
| 2128 | Py_DECREF(pynum); |
| 2129 | return NULL; |
| 2130 | } |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2131 | return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2132 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 2133 | case ELLIPSIS: /* Ellipsis */ |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2134 | return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2135 | case LPAR: /* some parenthesized expressions */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2136 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2138 | if (TYPE(ch) == RPAR) |
| 2139 | return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2141 | if (TYPE(ch) == yield_expr) |
| 2142 | return ast_for_expr(c, ch); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2145 | if (NCH(ch) == 1) { |
| 2146 | return ast_for_testlist(c, ch); |
| 2147 | } |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2148 | |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2149 | if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2150 | return copy_location(ast_for_genexp(c, ch), n); |
| 2151 | } |
| 2152 | else { |
| 2153 | return copy_location(ast_for_testlist(c, ch), n); |
| 2154 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2155 | case LSQB: /* list (or list comprehension) */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2156 | ch = CHILD(n, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2158 | if (TYPE(ch) == RSQB) |
| 2159 | return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2161 | REQ(ch, testlist_comp); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2162 | if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
| 2163 | asdl_seq *elts = seq_for_testlist(c, ch); |
| 2164 | if (!elts) |
| 2165 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2166 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2167 | return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2168 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2169 | else { |
| 2170 | return copy_location(ast_for_listcomp(c, ch), n); |
| 2171 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2172 | case LBRACE: { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2173 | /* dictorsetmaker: ( ((test ':' test | '**' test) |
| 2174 | * (comp_for | (',' (test ':' test | '**' test))* [','])) | |
| 2175 | * ((test | '*' test) |
| 2176 | * (comp_for | (',' (test | '*' test))* [','])) ) */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2177 | expr_ty res; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2178 | ch = CHILD(n, 1); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2179 | if (TYPE(ch) == RBRACE) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2180 | /* It's an empty dict. */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2181 | return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2182 | } |
| 2183 | else { |
| 2184 | int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); |
| 2185 | if (NCH(ch) == 1 || |
| 2186 | (NCH(ch) > 1 && |
| 2187 | TYPE(CHILD(ch, 1)) == COMMA)) { |
| 2188 | /* It's a set display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2189 | res = ast_for_setdisplay(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2190 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2191 | else if (NCH(ch) > 1 && |
| 2192 | TYPE(CHILD(ch, 1)) == comp_for) { |
| 2193 | /* It's a set comprehension. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2194 | res = ast_for_setcomp(c, ch); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2195 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2196 | else if (NCH(ch) > 3 - is_dict && |
| 2197 | TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { |
| 2198 | /* It's a dictionary comprehension. */ |
| 2199 | if (is_dict) { |
| 2200 | ast_error(c, n, "dict unpacking cannot be used in " |
| 2201 | "dict comprehension"); |
| 2202 | return NULL; |
| 2203 | } |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2204 | res = ast_for_dictcomp(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2205 | } |
| 2206 | else { |
| 2207 | /* It's a dictionary display. */ |
Benjamin Peterson | 58b5395 | 2015-09-25 22:44:43 -0700 | [diff] [blame] | 2208 | res = ast_for_dictdisplay(c, ch); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2209 | } |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2210 | return copy_location(res, n); |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2211 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2212 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2213 | default: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2214 | PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 2215 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | static slice_ty |
| 2220 | ast_for_slice(struct compiling *c, const node *n) |
| 2221 | { |
| 2222 | node *ch; |
| 2223 | expr_ty lower = NULL, upper = NULL, step = NULL; |
| 2224 | |
| 2225 | REQ(n, subscript); |
| 2226 | |
| 2227 | /* |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 2228 | subscript: test | [test] ':' [test] [sliceop] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2229 | sliceop: ':' [test] |
| 2230 | */ |
| 2231 | ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2232 | if (NCH(n) == 1 && TYPE(ch) == test) { |
| 2233 | /* 'step' variable hold no significance in terms of being used over |
| 2234 | other vars */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | step = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2236 | if (!step) |
| 2237 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2239 | return Index(step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | if (TYPE(ch) == test) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2243 | lower = ast_for_expr(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2244 | if (!lower) |
| 2245 | return NULL; |
| 2246 | } |
| 2247 | |
| 2248 | /* If there's an upper bound it's in the second or third position. */ |
| 2249 | if (TYPE(ch) == COLON) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2250 | if (NCH(n) > 1) { |
| 2251 | node *n2 = CHILD(n, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2252 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2253 | if (TYPE(n2) == test) { |
| 2254 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2255 | if (!upper) |
| 2256 | return NULL; |
| 2257 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2258 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2259 | } else if (NCH(n) > 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2260 | node *n2 = CHILD(n, 2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2261 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2262 | if (TYPE(n2) == test) { |
| 2263 | upper = ast_for_expr(c, n2); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2264 | if (!upper) |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | ch = CHILD(n, NCH(n) - 1); |
| 2270 | if (TYPE(ch) == sliceop) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2271 | if (NCH(ch) != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2272 | ch = CHILD(ch, 1); |
| 2273 | if (TYPE(ch) == test) { |
| 2274 | step = ast_for_expr(c, ch); |
| 2275 | if (!step) |
| 2276 | return NULL; |
| 2277 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
| 2280 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2281 | return Slice(lower, upper, step, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | static expr_ty |
| 2285 | ast_for_binop(struct compiling *c, const node *n) |
| 2286 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2287 | /* Must account for a sequence of expressions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | How should A op B op C by represented? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2289 | BinOp(BinOp(A, op, B), op, C). |
| 2290 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2291 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2292 | int i, nops; |
| 2293 | expr_ty expr1, expr2, result; |
| 2294 | operator_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2295 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2296 | expr1 = ast_for_expr(c, CHILD(n, 0)); |
| 2297 | if (!expr1) |
| 2298 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2300 | expr2 = ast_for_expr(c, CHILD(n, 2)); |
| 2301 | if (!expr2) |
| 2302 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2304 | newoperator = get_operator(CHILD(n, 1)); |
| 2305 | if (!newoperator) |
| 2306 | return NULL; |
| 2307 | |
| 2308 | result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
| 2309 | c->c_arena); |
| 2310 | if (!result) |
| 2311 | return NULL; |
| 2312 | |
| 2313 | nops = (NCH(n) - 1) / 2; |
| 2314 | for (i = 1; i < nops; i++) { |
| 2315 | expr_ty tmp_result, tmp; |
| 2316 | const node* next_oper = CHILD(n, i * 2 + 1); |
| 2317 | |
| 2318 | newoperator = get_operator(next_oper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2319 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2320 | return NULL; |
| 2321 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2322 | tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
| 2323 | if (!tmp) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2324 | return NULL; |
| 2325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | tmp_result = BinOp(result, newoperator, tmp, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2327 | LINENO(next_oper), next_oper->n_col_offset, |
| 2328 | c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2329 | if (!tmp_result) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2330 | return NULL; |
| 2331 | result = tmp_result; |
| 2332 | } |
| 2333 | return result; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2336 | static expr_ty |
| 2337 | ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
| 2338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2340 | subscriptlist: subscript (',' subscript)* [','] |
| 2341 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
| 2342 | */ |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2343 | REQ(n, trailer); |
| 2344 | if (TYPE(CHILD(n, 0)) == LPAR) { |
| 2345 | if (NCH(n) == 2) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2346 | return Call(left_expr, NULL, NULL, LINENO(n), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2347 | n->n_col_offset, c->c_arena); |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2348 | else |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2349 | return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2350 | } |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2351 | else if (TYPE(CHILD(n, 0)) == DOT) { |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 2352 | PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
| 2353 | if (!attr_id) |
| 2354 | return NULL; |
| 2355 | return Attribute(left_expr, attr_id, Load, |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2356 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 9ebfbf0 | 2006-02-27 16:50:35 +0000 | [diff] [blame] | 2357 | } |
| 2358 | else { |
| 2359 | REQ(CHILD(n, 0), LSQB); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2360 | REQ(CHILD(n, 2), RSQB); |
| 2361 | n = CHILD(n, 1); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2362 | if (NCH(n) == 1) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2363 | slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
| 2364 | if (!slc) |
| 2365 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2366 | return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
| 2367 | c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2368 | } |
| 2369 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | /* The grammar is ambiguous here. The ambiguity is resolved |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2371 | by treating the sequence as a tuple literal if there are |
| 2372 | no slice features. |
| 2373 | */ |
Victor Stinner | 4d73ae7 | 2018-11-22 14:45:16 +0100 | [diff] [blame] | 2374 | Py_ssize_t j; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2375 | slice_ty slc; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2376 | expr_ty e; |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2377 | int simple = 1; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2378 | asdl_seq *slices, *elts; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2379 | slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2380 | if (!slices) |
| 2381 | return NULL; |
| 2382 | for (j = 0; j < NCH(n); j += 2) { |
| 2383 | slc = ast_for_slice(c, CHILD(n, j)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2384 | if (!slc) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2385 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2386 | if (slc->kind != Index_kind) |
Neal Norwitz | 6baa4c4 | 2007-02-26 19:14:12 +0000 | [diff] [blame] | 2387 | simple = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2388 | asdl_seq_SET(slices, j / 2, slc); |
| 2389 | } |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2390 | if (!simple) { |
| 2391 | return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2392 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2393 | } |
| 2394 | /* extract Index values and put them in a Tuple */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2395 | elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2396 | if (!elts) |
| 2397 | return NULL; |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2398 | for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
| 2399 | slc = (slice_ty)asdl_seq_GET(slices, j); |
| 2400 | assert(slc->kind == Index_kind && slc->v.Index.value); |
| 2401 | asdl_seq_SET(elts, j, slc->v.Index.value); |
| 2402 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2403 | e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | c7d3726 | 2006-02-27 17:29:29 +0000 | [diff] [blame] | 2404 | if (!e) |
| 2405 | return NULL; |
| 2406 | return Subscript(left_expr, Index(e, c->c_arena), |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2407 | Load, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2408 | } |
| 2409 | } |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | static expr_ty |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2413 | ast_for_factor(struct compiling *c, const node *n) |
| 2414 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2415 | expr_ty expression; |
| 2416 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2417 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 2418 | if (!expression) |
| 2419 | return NULL; |
| 2420 | |
| 2421 | switch (TYPE(CHILD(n, 0))) { |
| 2422 | case PLUS: |
| 2423 | return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
| 2424 | c->c_arena); |
| 2425 | case MINUS: |
| 2426 | return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
| 2427 | c->c_arena); |
| 2428 | case TILDE: |
| 2429 | return UnaryOp(Invert, expression, LINENO(n), |
| 2430 | n->n_col_offset, c->c_arena); |
| 2431 | } |
| 2432 | PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
| 2433 | TYPE(CHILD(n, 0))); |
| 2434 | return NULL; |
| 2435 | } |
| 2436 | |
| 2437 | static expr_ty |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2438 | ast_for_atom_expr(struct compiling *c, const node *n) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2439 | { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2440 | int i, nch, start = 0; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2441 | expr_ty e, tmp; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2442 | |
| 2443 | REQ(n, atom_expr); |
| 2444 | nch = NCH(n); |
| 2445 | |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2446 | if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2447 | start = 1; |
| 2448 | assert(nch > 1); |
| 2449 | } |
| 2450 | |
| 2451 | e = ast_for_atom(c, CHILD(n, start)); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2452 | if (!e) |
| 2453 | return NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2454 | if (nch == 1) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2455 | return e; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2456 | if (start && nch == 2) { |
| 2457 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2458 | } |
| 2459 | |
| 2460 | for (i = start + 1; i < nch; i++) { |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2461 | node *ch = CHILD(n, i); |
| 2462 | if (TYPE(ch) != trailer) |
| 2463 | break; |
| 2464 | tmp = ast_for_trailer(c, ch, e); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2465 | if (!tmp) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2466 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2467 | tmp->lineno = e->lineno; |
| 2468 | tmp->col_offset = e->col_offset; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2469 | e = tmp; |
| 2470 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2471 | |
| 2472 | if (start) { |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2473 | /* there was an 'await' */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2474 | return Await(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 2475 | } |
| 2476 | else { |
| 2477 | return e; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | static expr_ty |
| 2482 | ast_for_power(struct compiling *c, const node *n) |
| 2483 | { |
| 2484 | /* power: atom trailer* ('**' factor)* |
| 2485 | */ |
| 2486 | expr_ty e; |
| 2487 | REQ(n, power); |
| 2488 | e = ast_for_atom_expr(c, CHILD(n, 0)); |
| 2489 | if (!e) |
| 2490 | return NULL; |
| 2491 | if (NCH(n) == 1) |
| 2492 | return e; |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2493 | if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
| 2494 | expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2495 | if (!f) |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2496 | return NULL; |
Benjamin Peterson | 7a66fc2 | 2015-02-02 10:51:20 -0500 | [diff] [blame] | 2497 | e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2498 | } |
| 2499 | return e; |
| 2500 | } |
| 2501 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2502 | static expr_ty |
| 2503 | ast_for_starred(struct compiling *c, const node *n) |
| 2504 | { |
| 2505 | expr_ty tmp; |
| 2506 | REQ(n, star_expr); |
| 2507 | |
| 2508 | tmp = ast_for_expr(c, CHILD(n, 1)); |
| 2509 | if (!tmp) |
| 2510 | return NULL; |
| 2511 | |
| 2512 | /* The Load context is changed later. */ |
| 2513 | return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 2514 | } |
| 2515 | |
| 2516 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2517 | /* Do not name a variable 'expr'! Will cause a compile error. |
| 2518 | */ |
| 2519 | |
| 2520 | static expr_ty |
| 2521 | ast_for_expr(struct compiling *c, const node *n) |
| 2522 | { |
| 2523 | /* handle the full range of simple expressions |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2524 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2525 | test_nocond: or_test | lambdef_nocond |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | or_test: and_test ('or' and_test)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2527 | and_test: not_test ('and' not_test)* |
| 2528 | not_test: 'not' not_test | comparison |
| 2529 | comparison: expr (comp_op expr)* |
| 2530 | expr: xor_expr ('|' xor_expr)* |
| 2531 | xor_expr: and_expr ('^' and_expr)* |
| 2532 | and_expr: shift_expr ('&' shift_expr)* |
| 2533 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 2534 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2535 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2536 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2537 | power: atom_expr ['**' factor] |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 2538 | atom_expr: ['await'] atom trailer* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2539 | yield_expr: 'yield' [yield_arg] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2540 | */ |
| 2541 | |
| 2542 | asdl_seq *seq; |
| 2543 | int i; |
| 2544 | |
| 2545 | loop: |
| 2546 | switch (TYPE(n)) { |
| 2547 | case test: |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2548 | case test_nocond: |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2549 | if (TYPE(CHILD(n, 0)) == lambdef || |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2550 | TYPE(CHILD(n, 0)) == lambdef_nocond) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2551 | return ast_for_lambdef(c, CHILD(n, 0)); |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2552 | else if (NCH(n) > 1) |
| 2553 | return ast_for_ifexpr(c, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2554 | /* Fallthrough */ |
| 2555 | case or_test: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2556 | case and_test: |
| 2557 | if (NCH(n) == 1) { |
| 2558 | n = CHILD(n, 0); |
| 2559 | goto loop; |
| 2560 | } |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2561 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2562 | if (!seq) |
| 2563 | return NULL; |
| 2564 | for (i = 0; i < NCH(n); i += 2) { |
| 2565 | expr_ty e = ast_for_expr(c, CHILD(n, i)); |
| 2566 | if (!e) |
| 2567 | return NULL; |
| 2568 | asdl_seq_SET(seq, i / 2, e); |
| 2569 | } |
| 2570 | if (!strcmp(STR(CHILD(n, 1)), "and")) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2571 | return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
| 2572 | c->c_arena); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2573 | assert(!strcmp(STR(CHILD(n, 1)), "or")); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 2574 | return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2575 | case not_test: |
| 2576 | if (NCH(n) == 1) { |
| 2577 | n = CHILD(n, 0); |
| 2578 | goto loop; |
| 2579 | } |
| 2580 | else { |
| 2581 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 2582 | if (!expression) |
| 2583 | return NULL; |
| 2584 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2585 | return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
| 2586 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2587 | } |
| 2588 | case comparison: |
| 2589 | if (NCH(n) == 1) { |
| 2590 | n = CHILD(n, 0); |
| 2591 | goto loop; |
| 2592 | } |
| 2593 | else { |
| 2594 | expr_ty expression; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2595 | asdl_int_seq *ops; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2596 | asdl_seq *cmps; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2597 | ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2598 | if (!ops) |
| 2599 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2600 | cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2601 | if (!cmps) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2602 | return NULL; |
| 2603 | } |
| 2604 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2605 | cmpop_ty newoperator; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2606 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2607 | newoperator = ast_for_comp_op(c, CHILD(n, i)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2608 | if (!newoperator) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2609 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2610 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2611 | |
| 2612 | expression = ast_for_expr(c, CHILD(n, i + 1)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2613 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2614 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2615 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2617 | asdl_seq_SET(ops, i / 2, newoperator); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2618 | asdl_seq_SET(cmps, i / 2, expression); |
| 2619 | } |
| 2620 | expression = ast_for_expr(c, CHILD(n, 0)); |
Neal Norwitz | e76adcd | 2005-11-15 05:04:31 +0000 | [diff] [blame] | 2621 | if (!expression) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2622 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2623 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2625 | return Compare(expression, ops, cmps, LINENO(n), |
| 2626 | n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2627 | } |
| 2628 | break; |
| 2629 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2630 | case star_expr: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | return ast_for_starred(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2632 | /* The next five cases all handle BinOps. The main body of code |
| 2633 | is the same in each case, but the switch turned inside out to |
| 2634 | reuse the code for each type of operator. |
| 2635 | */ |
| 2636 | case expr: |
| 2637 | case xor_expr: |
| 2638 | case and_expr: |
| 2639 | case shift_expr: |
| 2640 | case arith_expr: |
| 2641 | case term: |
| 2642 | if (NCH(n) == 1) { |
| 2643 | n = CHILD(n, 0); |
| 2644 | goto loop; |
| 2645 | } |
| 2646 | return ast_for_binop(c, n); |
| 2647 | case yield_expr: { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2648 | node *an = NULL; |
| 2649 | node *en = NULL; |
| 2650 | int is_from = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2651 | expr_ty exp = NULL; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2652 | if (NCH(n) > 1) |
| 2653 | an = CHILD(n, 1); /* yield_arg */ |
| 2654 | if (an) { |
| 2655 | en = CHILD(an, NCH(an) - 1); |
| 2656 | if (NCH(an) == 2) { |
| 2657 | is_from = 1; |
| 2658 | exp = ast_for_expr(c, en); |
| 2659 | } |
| 2660 | else |
| 2661 | exp = ast_for_testlist(c, en); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2662 | if (!exp) |
| 2663 | return NULL; |
| 2664 | } |
Benjamin Peterson | 527c622 | 2012-01-14 08:58:23 -0500 | [diff] [blame] | 2665 | if (is_from) |
| 2666 | return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 2667 | return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2668 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2669 | case factor: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2670 | if (NCH(n) == 1) { |
| 2671 | n = CHILD(n, 0); |
| 2672 | goto loop; |
| 2673 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2674 | return ast_for_factor(c, n); |
Neil Schemenauer | 982e8d6 | 2005-10-25 09:16:05 +0000 | [diff] [blame] | 2675 | case power: |
| 2676 | return ast_for_power(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2677 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 2678 | PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2679 | return NULL; |
| 2680 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2681 | /* should never get here unless if error is set */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2682 | return NULL; |
| 2683 | } |
| 2684 | |
| 2685 | static expr_ty |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2686 | ast_for_call(struct compiling *c, const node *n, expr_ty func, |
| 2687 | const node *maybegenbeg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2688 | { |
| 2689 | /* |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2690 | arglist: argument (',' argument)* [','] |
| 2691 | argument: ( test [comp_for] | '*' test | test '=' test | '**' test ) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | */ |
| 2693 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2694 | int i, nargs, nkeywords; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2695 | int ndoublestars; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2696 | asdl_seq *args; |
| 2697 | asdl_seq *keywords; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2698 | |
| 2699 | REQ(n, arglist); |
| 2700 | |
| 2701 | nargs = 0; |
| 2702 | nkeywords = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2703 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2704 | node *ch = CHILD(n, i); |
| 2705 | if (TYPE(ch) == argument) { |
| 2706 | if (NCH(ch) == 1) |
| 2707 | nargs++; |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2708 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
| 2709 | nargs++; |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2710 | if (!maybegenbeg) { |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 2711 | ast_error(c, ch, "invalid syntax"); |
| 2712 | return NULL; |
| 2713 | } |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2714 | if (NCH(n) > 1) { |
| 2715 | ast_error(c, ch, "Generator expression must be parenthesized"); |
| 2716 | return NULL; |
| 2717 | } |
| 2718 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2719 | else if (TYPE(CHILD(ch, 0)) == STAR) |
| 2720 | nargs++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2721 | else |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2722 | /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2723 | nkeywords++; |
| 2724 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2725 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2726 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 2727 | args = _Py_asdl_seq_new(nargs, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2728 | if (!args) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2729 | return NULL; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 2730 | keywords = _Py_asdl_seq_new(nkeywords, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2731 | if (!keywords) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2732 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2733 | |
| 2734 | nargs = 0; /* positional arguments + iterable argument unpackings */ |
| 2735 | nkeywords = 0; /* keyword arguments + keyword argument unpackings */ |
| 2736 | ndoublestars = 0; /* just keyword argument unpackings */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2737 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2738 | node *ch = CHILD(n, i); |
| 2739 | if (TYPE(ch) == argument) { |
| 2740 | expr_ty e; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2741 | node *chch = CHILD(ch, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2742 | if (NCH(ch) == 1) { |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2743 | /* a positional argument */ |
| 2744 | if (nkeywords) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2745 | if (ndoublestars) { |
| 2746 | ast_error(c, chch, |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2747 | "positional argument follows " |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2748 | "keyword argument unpacking"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2749 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2750 | else { |
| 2751 | ast_error(c, chch, |
| 2752 | "positional argument follows " |
| 2753 | "keyword argument"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2754 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2755 | return NULL; |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 2756 | } |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 2757 | e = ast_for_expr(c, chch); |
| 2758 | if (!e) |
| 2759 | return NULL; |
| 2760 | asdl_seq_SET(args, nargs++, e); |
| 2761 | } |
| 2762 | else if (TYPE(chch) == STAR) { |
| 2763 | /* an iterable argument unpacking */ |
| 2764 | expr_ty starred; |
| 2765 | if (ndoublestars) { |
| 2766 | ast_error(c, chch, |
| 2767 | "iterable argument unpacking follows " |
| 2768 | "keyword argument unpacking"); |
| 2769 | return NULL; |
| 2770 | } |
| 2771 | e = ast_for_expr(c, CHILD(ch, 1)); |
| 2772 | if (!e) |
| 2773 | return NULL; |
| 2774 | starred = Starred(e, Load, LINENO(chch), |
| 2775 | chch->n_col_offset, |
| 2776 | c->c_arena); |
| 2777 | if (!starred) |
| 2778 | return NULL; |
| 2779 | asdl_seq_SET(args, nargs++, starred); |
| 2780 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2781 | } |
| 2782 | else if (TYPE(chch) == DOUBLESTAR) { |
| 2783 | /* a keyword argument unpacking */ |
| 2784 | keyword_ty kw; |
| 2785 | i++; |
| 2786 | e = ast_for_expr(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2787 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2788 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2789 | kw = keyword(NULL, e, c->c_arena); |
| 2790 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2791 | ndoublestars++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2793 | else if (TYPE(CHILD(ch, 1)) == comp_for) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2794 | /* the lone generator expression */ |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 2795 | e = copy_location(ast_for_genexp(c, ch), maybegenbeg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2796 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2797 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2798 | asdl_seq_SET(args, nargs++, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2799 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2800 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2801 | /* a keyword argument */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2802 | keyword_ty kw; |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2803 | identifier key, tmp; |
| 2804 | int k; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2805 | |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2806 | // To remain LL(1), the grammar accepts any test (basically, any |
| 2807 | // expression) in the keyword slot of a call site. So, we need |
| 2808 | // to manually enforce that the keyword is a NAME here. |
| 2809 | static const int name_tree[] = { |
| 2810 | test, |
| 2811 | or_test, |
| 2812 | and_test, |
| 2813 | not_test, |
| 2814 | comparison, |
| 2815 | expr, |
| 2816 | xor_expr, |
| 2817 | and_expr, |
| 2818 | shift_expr, |
| 2819 | arith_expr, |
| 2820 | term, |
| 2821 | factor, |
| 2822 | power, |
| 2823 | atom_expr, |
| 2824 | atom, |
| 2825 | 0, |
| 2826 | }; |
| 2827 | node *expr_node = chch; |
| 2828 | for (int i = 0; name_tree[i]; i++) { |
| 2829 | if (TYPE(expr_node) != name_tree[i]) |
| 2830 | break; |
| 2831 | if (NCH(expr_node) != 1) |
| 2832 | break; |
| 2833 | expr_node = CHILD(expr_node, 0); |
| 2834 | } |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2835 | if (TYPE(expr_node) != NAME) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2836 | ast_error(c, chch, |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 2837 | "expression cannot contain assignment, " |
| 2838 | "perhaps you meant \"==\"?"); |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2839 | return NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2840 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2841 | key = new_identifier(STR(expr_node), c); |
| 2842 | if (key == NULL) { |
Benjamin Peterson | c64ae92 | 2012-01-16 18:02:21 -0500 | [diff] [blame] | 2843 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | } |
Benjamin Peterson | c9a71dd | 2018-09-12 17:14:39 -0700 | [diff] [blame] | 2845 | if (forbidden_name(c, key, chch, 1)) { |
| 2846 | return NULL; |
| 2847 | } |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2848 | for (k = 0; k < nkeywords; k++) { |
| 2849 | tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2850 | if (tmp && !PyUnicode_Compare(tmp, key)) { |
| 2851 | ast_error(c, chch, |
| 2852 | "keyword argument repeated"); |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 2853 | return NULL; |
| 2854 | } |
| 2855 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2856 | e = ast_for_expr(c, CHILD(ch, 2)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2857 | if (!e) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2858 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2859 | kw = keyword(key, e, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2860 | if (!kw) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 2861 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2862 | asdl_seq_SET(keywords, nkeywords++, kw); |
| 2863 | } |
| 2864 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2867 | return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2870 | static expr_ty |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2871 | ast_for_testlist(struct compiling *c, const node* n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 | { |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2873 | /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2874 | /* testlist: test (',' test)* [','] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | assert(NCH(n) > 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2876 | if (TYPE(n) == testlist_comp) { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2877 | if (NCH(n) > 1) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2878 | assert(TYPE(CHILD(n, 1)) != comp_for); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2879 | } |
| 2880 | else { |
| 2881 | assert(TYPE(n) == testlist || |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2882 | TYPE(n) == testlist_star_expr); |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2883 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2884 | if (NCH(n) == 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2885 | return ast_for_expr(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2886 | else { |
| 2887 | asdl_seq *tmp = seq_for_testlist(c, n); |
| 2888 | if (!tmp) |
| 2889 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2890 | return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 | } |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2894 | static stmt_ty |
| 2895 | ast_for_expr_stmt(struct compiling *c, const node *n) |
| 2896 | { |
| 2897 | REQ(n, expr_stmt); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2898 | /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 2899 | ('=' (yield_expr|testlist_star_expr))*) |
| 2900 | annassign: ':' test ['=' test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 2901 | testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2902 | augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2903 | | '<<=' | '>>=' | '**=' | '//=' |
Martin Panter | 69332c1 | 2016-08-04 13:07:31 +0000 | [diff] [blame] | 2904 | test: ... here starts the operator precedence dance |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2905 | */ |
| 2906 | |
| 2907 | if (NCH(n) == 1) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2908 | expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2909 | if (!e) |
| 2910 | return NULL; |
| 2911 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2912 | return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2913 | } |
| 2914 | else if (TYPE(CHILD(n, 1)) == augassign) { |
| 2915 | expr_ty expr1, expr2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2916 | operator_ty newoperator; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2917 | node *ch = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2918 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2919 | expr1 = ast_for_testlist(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2920 | if (!expr1) |
| 2921 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2922 | if(!set_context(c, expr1, Store, ch)) |
| 2923 | return NULL; |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2924 | /* set_context checks that most expressions are not the left side. |
| 2925 | Augmented assignments can only have a name, a subscript, or an |
| 2926 | attribute on the left, though, so we have to explicitly check for |
| 2927 | those. */ |
| 2928 | switch (expr1->kind) { |
| 2929 | case Name_kind: |
| 2930 | case Attribute_kind: |
| 2931 | case Subscript_kind: |
| 2932 | break; |
| 2933 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 2934 | ast_error(c, ch, "illegal expression for augmented assignment"); |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 2935 | return NULL; |
| 2936 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2937 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2938 | ch = CHILD(n, 2); |
| 2939 | if (TYPE(ch) == testlist) |
| 2940 | expr2 = ast_for_testlist(c, ch); |
| 2941 | else |
| 2942 | expr2 = ast_for_expr(c, ch); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 2943 | if (!expr2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2944 | return NULL; |
| 2945 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2946 | newoperator = ast_for_augassign(c, CHILD(n, 1)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2947 | if (!newoperator) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2948 | return NULL; |
| 2949 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2950 | return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2951 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2952 | else if (TYPE(CHILD(n, 1)) == annassign) { |
| 2953 | expr_ty expr1, expr2, expr3; |
| 2954 | node *ch = CHILD(n, 0); |
| 2955 | node *deep, *ann = CHILD(n, 1); |
| 2956 | int simple = 1; |
| 2957 | |
| 2958 | /* we keep track of parens to qualify (x) as expression not name */ |
| 2959 | deep = ch; |
| 2960 | while (NCH(deep) == 1) { |
| 2961 | deep = CHILD(deep, 0); |
| 2962 | } |
| 2963 | if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) { |
| 2964 | simple = 0; |
| 2965 | } |
| 2966 | expr1 = ast_for_testlist(c, ch); |
| 2967 | if (!expr1) { |
| 2968 | return NULL; |
| 2969 | } |
| 2970 | switch (expr1->kind) { |
| 2971 | case Name_kind: |
| 2972 | if (forbidden_name(c, expr1->v.Name.id, n, 0)) { |
| 2973 | return NULL; |
| 2974 | } |
| 2975 | expr1->v.Name.ctx = Store; |
| 2976 | break; |
| 2977 | case Attribute_kind: |
| 2978 | if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) { |
| 2979 | return NULL; |
| 2980 | } |
| 2981 | expr1->v.Attribute.ctx = Store; |
| 2982 | break; |
| 2983 | case Subscript_kind: |
| 2984 | expr1->v.Subscript.ctx = Store; |
| 2985 | break; |
| 2986 | case List_kind: |
| 2987 | ast_error(c, ch, |
| 2988 | "only single target (not list) can be annotated"); |
| 2989 | return NULL; |
| 2990 | case Tuple_kind: |
| 2991 | ast_error(c, ch, |
| 2992 | "only single target (not tuple) can be annotated"); |
| 2993 | return NULL; |
| 2994 | default: |
| 2995 | ast_error(c, ch, |
| 2996 | "illegal target for annotation"); |
| 2997 | return NULL; |
| 2998 | } |
| 2999 | |
| 3000 | if (expr1->kind != Name_kind) { |
| 3001 | simple = 0; |
| 3002 | } |
| 3003 | ch = CHILD(ann, 1); |
| 3004 | expr2 = ast_for_expr(c, ch); |
| 3005 | if (!expr2) { |
| 3006 | return NULL; |
| 3007 | } |
| 3008 | if (NCH(ann) == 2) { |
| 3009 | return AnnAssign(expr1, expr2, NULL, simple, |
| 3010 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3011 | } |
| 3012 | else { |
| 3013 | ch = CHILD(ann, 3); |
| 3014 | expr3 = ast_for_expr(c, ch); |
| 3015 | if (!expr3) { |
| 3016 | return NULL; |
| 3017 | } |
| 3018 | return AnnAssign(expr1, expr2, expr3, simple, |
| 3019 | LINENO(n), n->n_col_offset, c->c_arena); |
| 3020 | } |
| 3021 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3022 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3023 | int i; |
| 3024 | asdl_seq *targets; |
| 3025 | node *value; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3026 | expr_ty expression; |
| 3027 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3028 | /* a normal assignment */ |
| 3029 | REQ(CHILD(n, 1), EQUAL); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3030 | targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3031 | if (!targets) |
| 3032 | return NULL; |
| 3033 | for (i = 0; i < NCH(n) - 2; i += 2) { |
| 3034 | expr_ty e; |
| 3035 | node *ch = CHILD(n, i); |
| 3036 | if (TYPE(ch) == yield_expr) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3037 | ast_error(c, ch, "assignment to yield expression not possible"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3038 | return NULL; |
| 3039 | } |
| 3040 | e = ast_for_testlist(c, ch); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3041 | if (!e) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3042 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3043 | |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3044 | /* set context to assign */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3045 | if (!set_context(c, e, Store, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3046 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3047 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3048 | asdl_seq_SET(targets, i / 2, e); |
| 3049 | } |
| 3050 | value = CHILD(n, NCH(n) - 1); |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 3051 | if (TYPE(value) == testlist_star_expr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3052 | expression = ast_for_testlist(c, value); |
| 3053 | else |
| 3054 | expression = ast_for_expr(c, value); |
| 3055 | if (!expression) |
| 3056 | return NULL; |
| 3057 | return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3058 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3061 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3062 | static asdl_seq * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3063 | 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] | 3064 | { |
| 3065 | asdl_seq *seq; |
| 3066 | int i; |
| 3067 | expr_ty e; |
| 3068 | |
| 3069 | REQ(n, exprlist); |
| 3070 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3071 | seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3072 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3073 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3074 | for (i = 0; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3075 | e = ast_for_expr(c, CHILD(n, i)); |
| 3076 | if (!e) |
| 3077 | return NULL; |
| 3078 | asdl_seq_SET(seq, i / 2, e); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3079 | if (context && !set_context(c, e, context, CHILD(n, i))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3080 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3081 | } |
| 3082 | return seq; |
| 3083 | } |
| 3084 | |
| 3085 | static stmt_ty |
| 3086 | ast_for_del_stmt(struct compiling *c, const node *n) |
| 3087 | { |
| 3088 | asdl_seq *expr_list; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3090 | /* del_stmt: 'del' exprlist */ |
| 3091 | REQ(n, del_stmt); |
| 3092 | |
| 3093 | expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); |
| 3094 | if (!expr_list) |
| 3095 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3096 | return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | static stmt_ty |
| 3100 | ast_for_flow_stmt(struct compiling *c, const node *n) |
| 3101 | { |
| 3102 | /* |
| 3103 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 3104 | | yield_stmt |
| 3105 | break_stmt: 'break' |
| 3106 | continue_stmt: 'continue' |
| 3107 | return_stmt: 'return' [testlist] |
| 3108 | yield_stmt: yield_expr |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 3109 | yield_expr: 'yield' testlist | 'yield' 'from' test |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3110 | raise_stmt: 'raise' [test [',' test [',' test]]] |
| 3111 | */ |
| 3112 | node *ch; |
| 3113 | |
| 3114 | REQ(n, flow_stmt); |
| 3115 | ch = CHILD(n, 0); |
| 3116 | switch (TYPE(ch)) { |
| 3117 | case break_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3118 | return Break(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | case continue_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3120 | return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3121 | case yield_stmt: { /* will reduce to yield_expr */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3122 | expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
| 3123 | if (!exp) |
| 3124 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3125 | return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3126 | } |
| 3127 | case return_stmt: |
| 3128 | if (NCH(ch) == 1) |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3129 | return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3130 | else { |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3131 | expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3132 | if (!expression) |
| 3133 | return NULL; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3134 | return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3135 | } |
| 3136 | case raise_stmt: |
| 3137 | if (NCH(ch) == 1) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3138 | return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 3139 | else if (NCH(ch) >= 2) { |
| 3140 | expr_ty cause = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3141 | expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 3142 | if (!expression) |
| 3143 | return NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3144 | if (NCH(ch) == 4) { |
| 3145 | cause = ast_for_expr(c, CHILD(ch, 3)); |
| 3146 | if (!cause) |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3150 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 3151 | /* fall through */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3153 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3154 | "unexpected flow_stmt: %d", TYPE(ch)); |
| 3155 | return NULL; |
| 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | static alias_ty |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3160 | alias_for_import_name(struct compiling *c, const node *n, int store) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3161 | { |
| 3162 | /* |
Thomas Wouters | 8ae1295 | 2006-02-28 22:42:15 +0000 | [diff] [blame] | 3163 | import_as_name: NAME ['as' NAME] |
| 3164 | dotted_as_name: dotted_name ['as' NAME] |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3165 | dotted_name: NAME ('.' NAME)* |
| 3166 | */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3167 | identifier str, name; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3168 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3169 | loop: |
| 3170 | switch (TYPE(n)) { |
Benjamin Peterson | f63d615 | 2011-06-20 21:40:19 -0500 | [diff] [blame] | 3171 | case import_as_name: { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3172 | node *name_node = CHILD(n, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3173 | str = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3174 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3175 | if (!name) |
| 3176 | return NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3177 | if (NCH(n) == 3) { |
| 3178 | node *str_node = CHILD(n, 2); |
| 3179 | str = NEW_IDENTIFIER(str_node); |
| 3180 | if (!str) |
| 3181 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3182 | if (store && forbidden_name(c, str, str_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3183 | return NULL; |
| 3184 | } |
| 3185 | else { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3186 | if (forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3187 | return NULL; |
| 3188 | } |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3189 | return alias(name, str, c->c_arena); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3190 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3191 | case dotted_as_name: |
| 3192 | if (NCH(n) == 1) { |
| 3193 | n = CHILD(n, 0); |
| 3194 | goto loop; |
| 3195 | } |
| 3196 | else { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3197 | node *asname_node = CHILD(n, 2); |
| 3198 | alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3199 | if (!a) |
| 3200 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | assert(!a->asname); |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3202 | a->asname = NEW_IDENTIFIER(asname_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3203 | if (!a->asname) |
| 3204 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3205 | if (forbidden_name(c, a->asname, asname_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3206 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | return a; |
| 3208 | } |
| 3209 | break; |
| 3210 | case dotted_name: |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3211 | if (NCH(n) == 1) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3212 | node *name_node = CHILD(n, 0); |
| 3213 | name = NEW_IDENTIFIER(name_node); |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3214 | if (!name) |
| 3215 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3216 | if (store && forbidden_name(c, name, name_node, 0)) |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3217 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3218 | return alias(name, NULL, c->c_arena); |
| 3219 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3220 | else { |
| 3221 | /* Create a string of the form "a.b.c" */ |
Tim Peters | e93e64f | 2006-01-08 02:28:41 +0000 | [diff] [blame] | 3222 | int i; |
Tim Peters | 5db42c4 | 2006-01-08 02:25:34 +0000 | [diff] [blame] | 3223 | size_t len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3224 | char *s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | PyObject *uni; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3226 | |
| 3227 | len = 0; |
| 3228 | for (i = 0; i < NCH(n); i += 2) |
| 3229 | /* length of string plus one for the dot */ |
| 3230 | len += strlen(STR(CHILD(n, i))) + 1; |
| 3231 | len--; /* the last name doesn't have a dot */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3232 | str = PyBytes_FromStringAndSize(NULL, len); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3233 | if (!str) |
| 3234 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3235 | s = PyBytes_AS_STRING(str); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3236 | if (!s) |
| 3237 | return NULL; |
| 3238 | for (i = 0; i < NCH(n); i += 2) { |
| 3239 | char *sch = STR(CHILD(n, i)); |
| 3240 | strcpy(s, STR(CHILD(n, i))); |
| 3241 | s += strlen(sch); |
| 3242 | *s++ = '.'; |
| 3243 | } |
| 3244 | --s; |
| 3245 | *s = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
| 3247 | PyBytes_GET_SIZE(str), |
| 3248 | NULL); |
| 3249 | Py_DECREF(str); |
| 3250 | if (!uni) |
| 3251 | return NULL; |
| 3252 | str = uni; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3253 | PyUnicode_InternInPlace(&str); |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3254 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3255 | Py_DECREF(str); |
| 3256 | return NULL; |
| 3257 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3258 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3259 | } |
| 3260 | break; |
| 3261 | case STAR: |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3262 | str = PyUnicode_InternFromString("*"); |
Alexey Izbyshev | 28853a2 | 2018-08-22 07:55:16 +0300 | [diff] [blame] | 3263 | if (!str) |
| 3264 | return NULL; |
Victor Stinner | 43d8195 | 2013-07-17 00:57:58 +0200 | [diff] [blame] | 3265 | if (PyArena_AddPyObject(c->c_arena, str) < 0) { |
| 3266 | Py_DECREF(str); |
| 3267 | return NULL; |
| 3268 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3269 | return alias(str, NULL, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3270 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3271 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3272 | "unexpected import name: %d", TYPE(n)); |
| 3273 | return NULL; |
| 3274 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3275 | |
| 3276 | PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3277 | return NULL; |
| 3278 | } |
| 3279 | |
| 3280 | static stmt_ty |
| 3281 | ast_for_import_stmt(struct compiling *c, const node *n) |
| 3282 | { |
| 3283 | /* |
| 3284 | import_stmt: import_name | import_from |
| 3285 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3286 | import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 3287 | 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | */ |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3289 | int lineno; |
| 3290 | int col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3291 | int i; |
| 3292 | asdl_seq *aliases; |
| 3293 | |
| 3294 | REQ(n, import_stmt); |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3295 | lineno = LINENO(n); |
| 3296 | col_offset = n->n_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3297 | n = CHILD(n, 0); |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3298 | if (TYPE(n) == import_name) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3299 | n = CHILD(n, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3300 | REQ(n, dotted_as_names); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3301 | aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3302 | if (!aliases) |
| 3303 | return NULL; |
| 3304 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3305 | 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] | 3306 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3308 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3309 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3310 | return Import(aliases, lineno, col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3311 | } |
Thomas Wouters | 8622e93 | 2006-02-27 17:14:45 +0000 | [diff] [blame] | 3312 | else if (TYPE(n) == import_from) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3313 | int n_children; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3314 | int idx, ndots = 0; |
| 3315 | alias_ty mod = NULL; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3316 | identifier modname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3318 | /* Count the number of dots (for relative imports) and check for the |
| 3319 | optional module name */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3320 | for (idx = 1; idx < NCH(n); idx++) { |
| 3321 | if (TYPE(CHILD(n, idx)) == dotted_name) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3322 | mod = alias_for_import_name(c, CHILD(n, idx), 0); |
| 3323 | if (!mod) |
| 3324 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3325 | idx++; |
| 3326 | break; |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3327 | } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | /* three consecutive dots are tokenized as one ELLIPSIS */ |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 3329 | ndots += 3; |
| 3330 | continue; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3331 | } else if (TYPE(CHILD(n, idx)) != DOT) { |
| 3332 | break; |
| 3333 | } |
| 3334 | ndots++; |
| 3335 | } |
| 3336 | idx++; /* skip over the 'import' keyword */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3337 | switch (TYPE(CHILD(n, idx))) { |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3338 | case STAR: |
| 3339 | /* from ... import * */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3340 | n = CHILD(n, idx); |
| 3341 | n_children = 1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | case LPAR: |
| 3344 | /* from ... import (x, y, z) */ |
| 3345 | n = CHILD(n, idx + 1); |
| 3346 | n_children = NCH(n); |
| 3347 | break; |
| 3348 | case import_as_names: |
| 3349 | /* from ... import x, y, z */ |
| 3350 | n = CHILD(n, idx); |
| 3351 | n_children = NCH(n); |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3352 | if (n_children % 2 == 0) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3353 | ast_error(c, n, "trailing comma not allowed without" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3354 | " surrounding parentheses"); |
| 3355 | return NULL; |
| 3356 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3357 | break; |
| 3358 | default: |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3359 | ast_error(c, n, "Unexpected node-type in from-import"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3360 | return NULL; |
| 3361 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3363 | aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3364 | if (!aliases) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3365 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3366 | |
| 3367 | /* handle "from ... import *" special b/c there's no children */ |
Thomas Wouters | 106203c | 2006-02-27 17:05:19 +0000 | [diff] [blame] | 3368 | if (TYPE(n) == STAR) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3369 | alias_ty import_alias = alias_for_import_name(c, n, 1); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3370 | if (!import_alias) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3371 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3372 | asdl_seq_SET(aliases, 0, import_alias); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3373 | } |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3374 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3375 | for (i = 0; i < NCH(n); i += 2) { |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3376 | 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] | 3377 | if (!import_alias) |
| 3378 | return NULL; |
Georg Brandl | 5c60ea3 | 2016-01-18 07:53:59 +0100 | [diff] [blame] | 3379 | asdl_seq_SET(aliases, i / 2, import_alias); |
Jeremy Hylton | a829313 | 2006-02-28 17:58:27 +0000 | [diff] [blame] | 3380 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3381 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3382 | if (mod != NULL) |
| 3383 | modname = mod->name; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3384 | return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3385 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3386 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3387 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3388 | "unknown import statement: starts with command '%s'", |
| 3389 | STR(CHILD(n, 0))); |
| 3390 | return NULL; |
| 3391 | } |
| 3392 | |
| 3393 | static stmt_ty |
| 3394 | ast_for_global_stmt(struct compiling *c, const node *n) |
| 3395 | { |
| 3396 | /* global_stmt: 'global' NAME (',' NAME)* */ |
| 3397 | identifier name; |
| 3398 | asdl_seq *s; |
| 3399 | int i; |
| 3400 | |
| 3401 | REQ(n, global_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3402 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3403 | if (!s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3404 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3405 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3406 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3407 | if (!name) |
| 3408 | return NULL; |
| 3409 | asdl_seq_SET(s, i / 2, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3410 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3411 | return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | static stmt_ty |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3415 | ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
| 3416 | { |
| 3417 | /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
| 3418 | identifier name; |
| 3419 | asdl_seq *s; |
| 3420 | int i; |
| 3421 | |
| 3422 | REQ(n, nonlocal_stmt); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3423 | s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3424 | if (!s) |
| 3425 | return NULL; |
| 3426 | for (i = 1; i < NCH(n); i += 2) { |
| 3427 | name = NEW_IDENTIFIER(CHILD(n, i)); |
| 3428 | if (!name) |
| 3429 | return NULL; |
| 3430 | asdl_seq_SET(s, i / 2, name); |
| 3431 | } |
| 3432 | return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 3433 | } |
| 3434 | |
| 3435 | static stmt_ty |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | ast_for_assert_stmt(struct compiling *c, const node *n) |
| 3437 | { |
| 3438 | /* assert_stmt: 'assert' test [',' test] */ |
| 3439 | REQ(n, assert_stmt); |
| 3440 | if (NCH(n) == 2) { |
| 3441 | expr_ty expression = ast_for_expr(c, CHILD(n, 1)); |
| 3442 | if (!expression) |
| 3443 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3444 | return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3445 | } |
| 3446 | else if (NCH(n) == 4) { |
| 3447 | expr_ty expr1, expr2; |
| 3448 | |
| 3449 | expr1 = ast_for_expr(c, CHILD(n, 1)); |
| 3450 | if (!expr1) |
| 3451 | return NULL; |
| 3452 | expr2 = ast_for_expr(c, CHILD(n, 3)); |
| 3453 | if (!expr2) |
| 3454 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3456 | return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3457 | } |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 3458 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3459 | "improper number of parts to 'assert' statement: %d", |
| 3460 | NCH(n)); |
| 3461 | return NULL; |
| 3462 | } |
| 3463 | |
| 3464 | static asdl_seq * |
| 3465 | ast_for_suite(struct compiling *c, const node *n) |
| 3466 | { |
| 3467 | /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3468 | asdl_seq *seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3469 | stmt_ty s; |
| 3470 | int i, total, num, end, pos = 0; |
| 3471 | node *ch; |
| 3472 | |
| 3473 | REQ(n, suite); |
| 3474 | |
| 3475 | total = num_stmts(n); |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3476 | seq = _Py_asdl_seq_new(total, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | if (!seq) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3478 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3479 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3480 | n = CHILD(n, 0); |
| 3481 | /* simple_stmt always ends with a NEWLINE, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | and may have a trailing SEMI |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3483 | */ |
| 3484 | end = NCH(n) - 1; |
| 3485 | if (TYPE(CHILD(n, end - 1)) == SEMI) |
| 3486 | end--; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3487 | /* loop by 2 to skip semi-colons */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3488 | for (i = 0; i < end; i += 2) { |
| 3489 | ch = CHILD(n, i); |
| 3490 | s = ast_for_stmt(c, ch); |
| 3491 | if (!s) |
| 3492 | return NULL; |
| 3493 | asdl_seq_SET(seq, pos++, s); |
| 3494 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3495 | } |
| 3496 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3497 | for (i = 2; i < (NCH(n) - 1); i++) { |
| 3498 | ch = CHILD(n, i); |
| 3499 | REQ(ch, stmt); |
| 3500 | num = num_stmts(ch); |
| 3501 | if (num == 1) { |
| 3502 | /* small_stmt or compound_stmt with only one child */ |
| 3503 | s = ast_for_stmt(c, ch); |
| 3504 | if (!s) |
| 3505 | return NULL; |
| 3506 | asdl_seq_SET(seq, pos++, s); |
| 3507 | } |
| 3508 | else { |
| 3509 | int j; |
| 3510 | ch = CHILD(ch, 0); |
| 3511 | REQ(ch, simple_stmt); |
| 3512 | for (j = 0; j < NCH(ch); j += 2) { |
| 3513 | /* statement terminates with a semi-colon ';' */ |
| 3514 | if (NCH(CHILD(ch, j)) == 0) { |
| 3515 | assert((j + 1) == NCH(ch)); |
| 3516 | break; |
| 3517 | } |
| 3518 | s = ast_for_stmt(c, CHILD(ch, j)); |
| 3519 | if (!s) |
| 3520 | return NULL; |
| 3521 | asdl_seq_SET(seq, pos++, s); |
| 3522 | } |
| 3523 | } |
| 3524 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3525 | } |
| 3526 | assert(pos == seq->size); |
| 3527 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | static stmt_ty |
| 3531 | ast_for_if_stmt(struct compiling *c, const node *n) |
| 3532 | { |
| 3533 | /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* |
| 3534 | ['else' ':' suite] |
| 3535 | */ |
| 3536 | char *s; |
| 3537 | |
| 3538 | REQ(n, if_stmt); |
| 3539 | |
| 3540 | if (NCH(n) == 4) { |
| 3541 | expr_ty expression; |
| 3542 | asdl_seq *suite_seq; |
| 3543 | |
| 3544 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3545 | if (!expression) |
| 3546 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3548 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3550 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3551 | return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
| 3552 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3553 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3554 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3555 | s = STR(CHILD(n, 4)); |
| 3556 | /* s[2], the third character in the string, will be |
| 3557 | 's' for el_s_e, or |
| 3558 | 'i' for el_i_f |
| 3559 | */ |
| 3560 | if (s[2] == 's') { |
| 3561 | expr_ty expression; |
| 3562 | asdl_seq *seq1, *seq2; |
| 3563 | |
| 3564 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3565 | if (!expression) |
| 3566 | return NULL; |
| 3567 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3568 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3569 | return NULL; |
| 3570 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3571 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3572 | return NULL; |
| 3573 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3574 | return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
| 3575 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | } |
| 3577 | else if (s[2] == 'i') { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3578 | int i, n_elif, has_else = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3579 | expr_ty expression; |
| 3580 | asdl_seq *suite_seq; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3581 | asdl_seq *orelse = NULL; |
| 3582 | n_elif = NCH(n) - 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3583 | /* must reference the child n_elif+1 since 'else' token is third, |
| 3584 | not fourth, child from the end. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3585 | if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
| 3586 | && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
| 3587 | has_else = 1; |
| 3588 | n_elif -= 3; |
| 3589 | } |
| 3590 | n_elif /= 4; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3591 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3592 | if (has_else) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3593 | asdl_seq *suite_seq2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3594 | |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3595 | orelse = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3596 | if (!orelse) |
| 3597 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3599 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3600 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3601 | suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
| 3602 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3603 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3604 | suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3605 | if (!suite_seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | asdl_seq_SET(orelse, 0, |
| 3609 | If(expression, suite_seq, suite_seq2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3610 | LINENO(CHILD(n, NCH(n) - 6)), |
| 3611 | CHILD(n, NCH(n) - 6)->n_col_offset, |
| 3612 | c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3613 | /* the just-created orelse handled the last elif */ |
| 3614 | n_elif--; |
| 3615 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3616 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3617 | for (i = 0; i < n_elif; i++) { |
| 3618 | int off = 5 + (n_elif - i - 1) * 4; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3619 | asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3620 | if (!newobj) |
| 3621 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3622 | expression = ast_for_expr(c, CHILD(n, off)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3623 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3624 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3625 | suite_seq = ast_for_suite(c, CHILD(n, off + 2)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3626 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3627 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3628 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3629 | asdl_seq_SET(newobj, 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | If(expression, suite_seq, orelse, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3631 | LINENO(CHILD(n, off)), |
| 3632 | CHILD(n, off)->n_col_offset, c->c_arena)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3633 | orelse = newobj; |
| 3634 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3635 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3636 | if (!expression) |
| 3637 | return NULL; |
| 3638 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
| 3639 | if (!suite_seq) |
| 3640 | return NULL; |
| 3641 | return If(expression, suite_seq, orelse, |
| 3642 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3643 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3644 | |
| 3645 | PyErr_Format(PyExc_SystemError, |
| 3646 | "unexpected token in 'if' statement: %s", s); |
| 3647 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | static stmt_ty |
| 3651 | ast_for_while_stmt(struct compiling *c, const node *n) |
| 3652 | { |
| 3653 | /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ |
| 3654 | REQ(n, while_stmt); |
| 3655 | |
| 3656 | if (NCH(n) == 4) { |
| 3657 | expr_ty expression; |
| 3658 | asdl_seq *suite_seq; |
| 3659 | |
| 3660 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3661 | if (!expression) |
| 3662 | return NULL; |
| 3663 | suite_seq = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3664 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3665 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3666 | return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3667 | } |
| 3668 | else if (NCH(n) == 7) { |
| 3669 | expr_ty expression; |
| 3670 | asdl_seq *seq1, *seq2; |
| 3671 | |
| 3672 | expression = ast_for_expr(c, CHILD(n, 1)); |
| 3673 | if (!expression) |
| 3674 | return NULL; |
| 3675 | seq1 = ast_for_suite(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3676 | if (!seq1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3677 | return NULL; |
| 3678 | seq2 = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3679 | if (!seq2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3680 | return NULL; |
| 3681 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3682 | return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3683 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3684 | |
| 3685 | PyErr_Format(PyExc_SystemError, |
| 3686 | "wrong number of tokens for 'while' statement: %d", |
| 3687 | NCH(n)); |
| 3688 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3692 | 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] | 3693 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3694 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3695 | asdl_seq *_target, *seq = NULL, *suite_seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3696 | expr_ty expression; |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3697 | expr_ty target, first; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3698 | const node *node_target; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3699 | /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 3700 | REQ(n, for_stmt); |
| 3701 | |
| 3702 | if (NCH(n) == 9) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3703 | seq = ast_for_suite(c, CHILD(n, 8)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3704 | if (!seq) |
| 3705 | return NULL; |
| 3706 | } |
| 3707 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3708 | node_target = CHILD(n, 1); |
| 3709 | _target = ast_for_exprlist(c, node_target, Store); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3710 | if (!_target) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3712 | /* Check the # of children rather than the length of _target, since |
| 3713 | 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] | 3714 | first = (expr_ty)asdl_seq_GET(_target, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3715 | if (NCH(node_target) == 1) |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3716 | target = first; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3717 | else |
Benjamin Peterson | 2e4b0e1 | 2009-09-11 22:36:20 +0000 | [diff] [blame] | 3718 | target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | |
Neil Schemenauer | c5dd10a | 2005-10-25 07:54:54 +0000 | [diff] [blame] | 3720 | expression = ast_for_testlist(c, CHILD(n, 3)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3721 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3722 | return NULL; |
| 3723 | suite_seq = ast_for_suite(c, CHILD(n, 5)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3724 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | return NULL; |
| 3726 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3727 | if (is_async) |
| 3728 | return AsyncFor(target, expression, suite_seq, seq, |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3729 | LINENO(n0), n0->n_col_offset, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3730 | c->c_arena); |
| 3731 | else |
| 3732 | return For(target, expression, suite_seq, seq, |
| 3733 | LINENO(n), n->n_col_offset, |
| 3734 | c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | static excepthandler_ty |
| 3738 | ast_for_except_clause(struct compiling *c, const node *exc, node *body) |
| 3739 | { |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3740 | /* except_clause: 'except' [test ['as' test]] */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3741 | REQ(exc, except_clause); |
| 3742 | REQ(body, suite); |
| 3743 | |
| 3744 | if (NCH(exc) == 1) { |
| 3745 | asdl_seq *suite_seq = ast_for_suite(c, body); |
| 3746 | if (!suite_seq) |
| 3747 | return NULL; |
| 3748 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3749 | return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3750 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3751 | } |
| 3752 | else if (NCH(exc) == 2) { |
| 3753 | expr_ty expression; |
| 3754 | asdl_seq *suite_seq; |
| 3755 | |
| 3756 | expression = ast_for_expr(c, CHILD(exc, 1)); |
| 3757 | if (!expression) |
| 3758 | return NULL; |
| 3759 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3760 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3761 | return NULL; |
| 3762 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3763 | return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3764 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3765 | } |
| 3766 | else if (NCH(exc) == 4) { |
| 3767 | asdl_seq *suite_seq; |
| 3768 | expr_ty expression; |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 3769 | identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3770 | if (!e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3771 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3772 | if (forbidden_name(c, e, CHILD(exc, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3773 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3774 | expression = ast_for_expr(c, CHILD(exc, 1)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3775 | if (!expression) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3776 | return NULL; |
| 3777 | suite_seq = ast_for_suite(c, body); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3778 | if (!suite_seq) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3779 | return NULL; |
| 3780 | |
Neal Norwitz | ad74aa8 | 2008-03-31 05:14:30 +0000 | [diff] [blame] | 3781 | return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3782 | exc->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3783 | } |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3784 | |
| 3785 | PyErr_Format(PyExc_SystemError, |
| 3786 | "wrong number of children for 'except' clause: %d", |
| 3787 | NCH(exc)); |
| 3788 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | static stmt_ty |
| 3792 | ast_for_try_stmt(struct compiling *c, const node *n) |
| 3793 | { |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3794 | const int nch = NCH(n); |
| 3795 | int n_except = (nch - 3)/3; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3796 | asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3797 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3798 | REQ(n, try_stmt); |
| 3799 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3800 | body = ast_for_suite(c, CHILD(n, 2)); |
| 3801 | if (body == NULL) |
| 3802 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3803 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3804 | if (TYPE(CHILD(n, nch - 3)) == NAME) { |
| 3805 | if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
| 3806 | if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
| 3807 | /* we can assume it's an "else", |
| 3808 | because nch >= 9 for try-else-finally and |
| 3809 | it would otherwise have a type of except_clause */ |
| 3810 | orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
| 3811 | if (orelse == NULL) |
| 3812 | return NULL; |
| 3813 | n_except--; |
| 3814 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3815 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3816 | finally = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3817 | if (finally == NULL) |
| 3818 | return NULL; |
| 3819 | n_except--; |
| 3820 | } |
| 3821 | else { |
| 3822 | /* we can assume it's an "else", |
| 3823 | otherwise it would have a type of except_clause */ |
| 3824 | orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
| 3825 | if (orelse == NULL) |
| 3826 | return NULL; |
| 3827 | n_except--; |
| 3828 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3829 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3830 | else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3831 | ast_error(c, n, "malformed 'try' statement"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3832 | return NULL; |
| 3833 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3834 | |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3835 | if (n_except > 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3836 | int i; |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3837 | /* process except statements to create a try ... except */ |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3838 | handlers = _Py_asdl_seq_new(n_except, c->c_arena); |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3839 | if (handlers == NULL) |
| 3840 | return NULL; |
| 3841 | |
| 3842 | for (i = 0; i < n_except; i++) { |
| 3843 | excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
| 3844 | CHILD(n, 5 + i * 3)); |
| 3845 | if (!e) |
| 3846 | return NULL; |
| 3847 | asdl_seq_SET(handlers, i, e); |
| 3848 | } |
Neal Norwitz | f599f42 | 2005-12-17 21:33:47 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3851 | assert(finally != NULL || asdl_seq_LEN(handlers)); |
| 3852 | return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3855 | /* with_item: test ['as' expr] */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3856 | static withitem_ty |
| 3857 | ast_for_with_item(struct compiling *c, const node *n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3858 | { |
| 3859 | expr_ty context_expr, optional_vars = NULL; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3860 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3861 | REQ(n, with_item); |
| 3862 | context_expr = ast_for_expr(c, CHILD(n, 0)); |
Amaury Forgeot d'Arc | 92dc80a | 2010-08-19 17:43:15 +0000 | [diff] [blame] | 3863 | if (!context_expr) |
| 3864 | return NULL; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3865 | if (NCH(n) == 3) { |
| 3866 | optional_vars = ast_for_expr(c, CHILD(n, 2)); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3867 | |
| 3868 | if (!optional_vars) { |
| 3869 | return NULL; |
| 3870 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3871 | if (!set_context(c, optional_vars, Store, n)) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3872 | return NULL; |
| 3873 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3876 | return withitem(context_expr, optional_vars, c->c_arena); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3879 | /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
| 3880 | static stmt_ty |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3881 | 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] | 3882 | { |
guoci | 90fc898 | 2018-09-11 17:45:45 -0400 | [diff] [blame] | 3883 | const node * const n = is_async ? CHILD(n0, 1) : n0; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3884 | int i, n_items; |
| 3885 | asdl_seq *items, *body; |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3886 | |
| 3887 | REQ(n, with_stmt); |
| 3888 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3889 | n_items = (NCH(n) - 2) / 2; |
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 3890 | items = _Py_asdl_seq_new(n_items, c->c_arena); |
Stefan Krah | 28a2ad5 | 2012-08-20 16:07:38 +0200 | [diff] [blame] | 3891 | if (!items) |
| 3892 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3893 | for (i = 1; i < NCH(n) - 2; i += 2) { |
| 3894 | withitem_ty item = ast_for_with_item(c, CHILD(n, i)); |
| 3895 | if (!item) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3896 | return NULL; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3897 | asdl_seq_SET(items, (i - 1) / 2, item); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3900 | body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
| 3901 | if (!body) |
| 3902 | return NULL; |
| 3903 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3904 | if (is_async) |
Benjamin Peterson | d13e59c | 2018-09-11 15:29:57 -0700 | [diff] [blame] | 3905 | return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3906 | else |
| 3907 | return With(items, body, LINENO(n), n->n_col_offset, c->c_arena); |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3910 | static stmt_ty |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3911 | 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] | 3912 | { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3913 | /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3914 | PyObject *classname; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3915 | asdl_seq *s; |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3916 | expr_ty call; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3917 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3918 | REQ(n, classdef); |
| 3919 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3920 | if (NCH(n) == 4) { /* class NAME ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3921 | s = ast_for_suite(c, CHILD(n, 3)); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3922 | if (!s) |
| 3923 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3924 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3925 | if (!classname) |
| 3926 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3927 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3928 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3929 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3930 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3931 | } |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3932 | |
| 3933 | if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3934 | s = ast_for_suite(c, CHILD(n, 5)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3935 | if (!s) |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3936 | return NULL; |
| 3937 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3938 | if (!classname) |
| 3939 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3940 | if (forbidden_name(c, classname, CHILD(n, 3), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3941 | return NULL; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3942 | return ClassDef(classname, NULL, NULL, s, decorator_seq, |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 3943 | LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3946 | /* class NAME '(' arglist ')' ':' suite */ |
| 3947 | /* build up a fake Call node so we can extract its pieces */ |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3948 | { |
| 3949 | PyObject *dummy_name; |
| 3950 | expr_ty dummy; |
| 3951 | dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3952 | if (!dummy_name) |
| 3953 | return NULL; |
| 3954 | dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
Serhiy Storchaka | b619b09 | 2018-11-27 09:40:29 +0200 | [diff] [blame] | 3955 | call = ast_for_call(c, CHILD(n, 3), dummy, NULL); |
Benjamin Peterson | d951e7b | 2008-11-25 22:19:53 +0000 | [diff] [blame] | 3956 | if (!call) |
| 3957 | return NULL; |
| 3958 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3959 | s = ast_for_suite(c, CHILD(n, 6)); |
Neal Norwitz | 84456bd | 2005-12-18 03:16:20 +0000 | [diff] [blame] | 3960 | if (!s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | return NULL; |
Benjamin Peterson | 3076006 | 2008-11-25 04:02:28 +0000 | [diff] [blame] | 3962 | classname = NEW_IDENTIFIER(CHILD(n, 1)); |
| 3963 | if (!classname) |
| 3964 | return NULL; |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 3965 | if (forbidden_name(c, classname, CHILD(n, 1), 0)) |
Benjamin Peterson | 70f5276 | 2009-06-28 23:32:44 +0000 | [diff] [blame] | 3966 | return NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3967 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3968 | return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 3969 | decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | static stmt_ty |
| 3973 | ast_for_stmt(struct compiling *c, const node *n) |
| 3974 | { |
| 3975 | if (TYPE(n) == stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3976 | assert(NCH(n) == 1); |
| 3977 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3978 | } |
| 3979 | if (TYPE(n) == simple_stmt) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3980 | assert(num_stmts(n) == 1); |
| 3981 | n = CHILD(n, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3982 | } |
| 3983 | if (TYPE(n) == small_stmt) { |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3984 | n = CHILD(n, 0); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 3985 | /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
| 3986 | | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3987 | */ |
| 3988 | switch (TYPE(n)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3989 | case expr_stmt: |
| 3990 | return ast_for_expr_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3991 | case del_stmt: |
| 3992 | return ast_for_del_stmt(c, n); |
| 3993 | case pass_stmt: |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 3994 | return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3995 | case flow_stmt: |
| 3996 | return ast_for_flow_stmt(c, n); |
| 3997 | case import_stmt: |
| 3998 | return ast_for_import_stmt(c, n); |
| 3999 | case global_stmt: |
| 4000 | return ast_for_global_stmt(c, n); |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 4001 | case nonlocal_stmt: |
| 4002 | return ast_for_nonlocal_stmt(c, n); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4003 | case assert_stmt: |
| 4004 | return ast_for_assert_stmt(c, n); |
| 4005 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4006 | PyErr_Format(PyExc_SystemError, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4007 | "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 4008 | TYPE(n), NCH(n)); |
| 4009 | return NULL; |
| 4010 | } |
| 4011 | } |
| 4012 | else { |
| 4013 | /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4014 | | funcdef | classdef | decorated | async_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4015 | */ |
| 4016 | node *ch = CHILD(n, 0); |
| 4017 | REQ(n, compound_stmt); |
| 4018 | switch (TYPE(ch)) { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4019 | case if_stmt: |
| 4020 | return ast_for_if_stmt(c, ch); |
| 4021 | case while_stmt: |
| 4022 | return ast_for_while_stmt(c, ch); |
| 4023 | case for_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4024 | return ast_for_for_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4025 | case try_stmt: |
| 4026 | return ast_for_try_stmt(c, ch); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4027 | case with_stmt: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4028 | return ast_for_with_stmt(c, ch, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4029 | case funcdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4030 | return ast_for_funcdef(c, ch, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4031 | case classdef: |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4032 | return ast_for_classdef(c, ch, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4033 | case decorated: |
| 4034 | return ast_for_decorated(c, ch); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4035 | case async_stmt: |
| 4036 | return ast_for_async_stmt(c, ch); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4037 | default: |
Neal Norwitz | 7979265 | 2005-11-14 04:25:03 +0000 | [diff] [blame] | 4038 | PyErr_Format(PyExc_SystemError, |
Jelle Zijlstra | 898ff92 | 2018-05-13 17:04:53 -0400 | [diff] [blame] | 4039 | "unhandled compound_stmt: TYPE=%d NCH=%d\n", |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4040 | TYPE(n), NCH(n)); |
| 4041 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4042 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4047 | parsenumber_raw(struct compiling *c, const char *s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4048 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4049 | const char *end; |
| 4050 | long x; |
| 4051 | double dx; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4052 | Py_complex compl; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4053 | int imflag; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4054 | |
Mark Dickinson | d3c827b | 2008-12-05 18:10:46 +0000 | [diff] [blame] | 4055 | assert(s != NULL); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4056 | errno = 0; |
| 4057 | end = s + strlen(s) - 1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4058 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4059 | if (s[0] == '0') { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4060 | x = (long) PyOS_strtoul(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4061 | if (x < 0 && errno == 0) { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4062 | return PyLong_FromString(s, (char **)0, 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4063 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4064 | } |
| 4065 | else |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4066 | x = PyOS_strtol(s, (char **)&end, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4067 | if (*end == '\0') { |
| 4068 | if (errno != 0) |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4069 | return PyLong_FromString(s, (char **)0, 0); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4070 | return PyLong_FromLong(x); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4071 | } |
| 4072 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4073 | if (imflag) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4074 | compl.real = 0.; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4075 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
| 4076 | if (compl.imag == -1.0 && PyErr_Occurred()) |
| 4077 | return NULL; |
| 4078 | return PyComplex_FromCComplex(compl); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4079 | } |
| 4080 | else |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4081 | { |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4082 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 4083 | if (dx == -1.0 && PyErr_Occurred()) |
| 4084 | return NULL; |
| 4085 | return PyFloat_FromDouble(dx); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4086 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | static PyObject * |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4090 | parsenumber(struct compiling *c, const char *s) |
| 4091 | { |
| 4092 | char *dup, *end; |
| 4093 | PyObject *res = NULL; |
| 4094 | |
| 4095 | assert(s != NULL); |
| 4096 | |
| 4097 | if (strchr(s, '_') == NULL) { |
| 4098 | return parsenumber_raw(c, s); |
| 4099 | } |
| 4100 | /* Create a duplicate without underscores. */ |
| 4101 | dup = PyMem_Malloc(strlen(s) + 1); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4102 | if (dup == NULL) { |
| 4103 | return PyErr_NoMemory(); |
| 4104 | } |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 4105 | end = dup; |
| 4106 | for (; *s; s++) { |
| 4107 | if (*s != '_') { |
| 4108 | *end++ = *s; |
| 4109 | } |
| 4110 | } |
| 4111 | *end = '\0'; |
| 4112 | res = parsenumber_raw(c, dup); |
| 4113 | PyMem_Free(dup); |
| 4114 | return res; |
| 4115 | } |
| 4116 | |
| 4117 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4118 | decode_utf8(struct compiling *c, const char **sPtr, const char *end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4119 | { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 4120 | const char *s, *t; |
| 4121 | t = s = *sPtr; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4122 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 4123 | while (s < end && (*s & 0x80)) s++; |
| 4124 | *sPtr = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4125 | return PyUnicode_DecodeUTF8(t, s - t, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4128 | static int |
| 4129 | warn_invalid_escape_sequence(struct compiling *c, const node *n, |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 4130 | unsigned char first_invalid_escape_char) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4131 | { |
| 4132 | PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c", |
| 4133 | first_invalid_escape_char); |
| 4134 | if (msg == NULL) { |
| 4135 | return -1; |
| 4136 | } |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4137 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4138 | c->c_filename, LINENO(n), |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4139 | NULL, NULL) < 0) |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4140 | { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4141 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 6543912 | 2018-10-19 17:42:06 +0300 | [diff] [blame] | 4142 | /* Replace the SyntaxWarning exception with a SyntaxError |
Serhiy Storchaka | a561862 | 2017-12-01 08:40:23 +0200 | [diff] [blame] | 4143 | to get a more accurate error report */ |
| 4144 | PyErr_Clear(); |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 4145 | ast_error(c, n, "%U", msg); |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4146 | } |
| 4147 | Py_DECREF(msg); |
| 4148 | return -1; |
| 4149 | } |
| 4150 | Py_DECREF(msg); |
| 4151 | return 0; |
| 4152 | } |
| 4153 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4154 | static PyObject * |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4155 | decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4156 | size_t len) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4157 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4158 | PyObject *v, *u; |
| 4159 | char *buf; |
| 4160 | char *p; |
| 4161 | const char *end; |
Guido van Rossum | 29fd712 | 2007-11-12 01:13:56 +0000 | [diff] [blame] | 4162 | |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4163 | /* check for integer overflow */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 4164 | if (len > SIZE_MAX / 6) |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4165 | return NULL; |
| 4166 | /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
| 4167 | "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
| 4168 | u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
| 4169 | if (u == NULL) |
| 4170 | return NULL; |
| 4171 | p = buf = PyBytes_AsString(u); |
| 4172 | end = s + len; |
| 4173 | while (s < end) { |
| 4174 | if (*s == '\\') { |
| 4175 | *p++ = *s++; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4176 | if (s >= end || *s & 0x80) { |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4177 | strcpy(p, "u005c"); |
| 4178 | p += 5; |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4179 | if (s >= end) |
| 4180 | break; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4181 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4182 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4183 | if (*s & 0x80) { /* XXX inefficient */ |
| 4184 | PyObject *w; |
| 4185 | int kind; |
| 4186 | void *data; |
| 4187 | Py_ssize_t len, i; |
| 4188 | w = decode_utf8(c, &s, end); |
| 4189 | if (w == NULL) { |
| 4190 | Py_DECREF(u); |
| 4191 | return NULL; |
| 4192 | } |
| 4193 | kind = PyUnicode_KIND(w); |
| 4194 | data = PyUnicode_DATA(w); |
| 4195 | len = PyUnicode_GET_LENGTH(w); |
| 4196 | for (i = 0; i < len; i++) { |
| 4197 | Py_UCS4 chr = PyUnicode_READ(kind, data, i); |
| 4198 | sprintf(p, "\\U%08x", chr); |
| 4199 | p += 10; |
| 4200 | } |
| 4201 | /* Should be impossible to overflow */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4202 | assert(p - buf <= PyBytes_GET_SIZE(u)); |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4203 | Py_DECREF(w); |
| 4204 | } else { |
| 4205 | *p++ = *s++; |
| 4206 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4207 | } |
Benjamin Peterson | 202803a | 2016-02-25 22:34:45 -0800 | [diff] [blame] | 4208 | len = p - buf; |
| 4209 | s = buf; |
| 4210 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4211 | const char *first_invalid_escape; |
| 4212 | v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape); |
| 4213 | |
| 4214 | if (v != NULL && first_invalid_escape != NULL) { |
| 4215 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4216 | /* We have not decref u before because first_invalid_escape points |
| 4217 | inside u. */ |
| 4218 | Py_XDECREF(u); |
| 4219 | Py_DECREF(v); |
| 4220 | return NULL; |
| 4221 | } |
| 4222 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4223 | Py_XDECREF(u); |
| 4224 | return v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4227 | static PyObject * |
| 4228 | decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, |
| 4229 | size_t len) |
| 4230 | { |
| 4231 | const char *first_invalid_escape; |
| 4232 | PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL, |
| 4233 | &first_invalid_escape); |
| 4234 | if (result == NULL) |
| 4235 | return NULL; |
| 4236 | |
| 4237 | if (first_invalid_escape != NULL) { |
| 4238 | if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { |
| 4239 | Py_DECREF(result); |
| 4240 | return NULL; |
| 4241 | } |
| 4242 | } |
| 4243 | return result; |
| 4244 | } |
| 4245 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4246 | /* Shift locations for the given node and all its children by adding `lineno` |
| 4247 | and `col_offset` to existing locations. */ |
| 4248 | static void fstring_shift_node_locations(node *n, int lineno, int col_offset) |
| 4249 | { |
| 4250 | n->n_col_offset = n->n_col_offset + col_offset; |
| 4251 | for (int i = 0; i < NCH(n); ++i) { |
| 4252 | if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { |
| 4253 | /* Shifting column offsets unnecessary if there's been newlines. */ |
| 4254 | col_offset = 0; |
| 4255 | } |
| 4256 | fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); |
| 4257 | } |
| 4258 | n->n_lineno = n->n_lineno + lineno; |
| 4259 | } |
| 4260 | |
| 4261 | /* Fix locations for the given node and its children. |
| 4262 | |
| 4263 | `parent` is the enclosing node. |
| 4264 | `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] | 4265 | `expr_str` is the child node's string representation, including braces. |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4266 | */ |
| 4267 | static void |
| 4268 | fstring_fix_node_location(const node *parent, node *n, char *expr_str) |
| 4269 | { |
| 4270 | char *substr = NULL; |
| 4271 | char *start; |
| 4272 | int lines = LINENO(parent) - 1; |
| 4273 | int cols = parent->n_col_offset; |
| 4274 | /* Find the full fstring to fix location information in `n`. */ |
| 4275 | while (parent && parent->n_type != STRING) |
| 4276 | parent = parent->n_child; |
| 4277 | if (parent && parent->n_str) { |
| 4278 | substr = strstr(parent->n_str, expr_str); |
| 4279 | if (substr) { |
| 4280 | start = substr; |
| 4281 | while (start > parent->n_str) { |
| 4282 | if (start[0] == '\n') |
| 4283 | break; |
| 4284 | start--; |
| 4285 | } |
Victor Stinner | fb7e799 | 2018-04-30 23:51:02 +0200 | [diff] [blame] | 4286 | cols += (int)(substr - start); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4287 | /* Fix lineno in mulitline strings. */ |
| 4288 | while ((substr = strchr(substr + 1, '\n'))) |
| 4289 | lines--; |
| 4290 | } |
| 4291 | } |
| 4292 | fstring_shift_node_locations(n, lines, cols); |
| 4293 | } |
| 4294 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4295 | /* Compile this expression in to an expr_ty. Add parens around the |
| 4296 | expression, in order to allow leading spaces in the expression. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4297 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4298 | fstring_compile_expr(const char *expr_start, const char *expr_end, |
| 4299 | struct compiling *c, const node *n) |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4300 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4301 | { |
| 4302 | PyCompilerFlags cf; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4303 | node *mod_n; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4304 | mod_ty mod; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4305 | char *str; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4306 | Py_ssize_t len; |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4307 | const char *s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4308 | |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4309 | assert(expr_end >= expr_start); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4310 | assert(*(expr_start-1) == '{'); |
| 4311 | assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':'); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4312 | |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4313 | /* If the substring is all whitespace, it's an error. We need to catch this |
| 4314 | here, and not when we call PyParser_SimpleParseStringFlagsFilename, |
| 4315 | because turning the expression '' in to '()' would go from being invalid |
| 4316 | to valid. */ |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4317 | for (s = expr_start; s != expr_end; s++) { |
| 4318 | char c = *s; |
| 4319 | /* The Python parser ignores only the following whitespace |
| 4320 | characters (\r already is converted to \n). */ |
| 4321 | if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4322 | break; |
| 4323 | } |
| 4324 | } |
Serhiy Storchaka | 2e9cd58 | 2017-06-08 23:43:54 +0300 | [diff] [blame] | 4325 | if (s == expr_end) { |
Eric V. Smith | 1e5fcc3 | 2015-09-24 08:52:04 -0400 | [diff] [blame] | 4326 | ast_error(c, n, "f-string: empty expression not allowed"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4327 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4328 | } |
| 4329 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4330 | len = expr_end - expr_start; |
| 4331 | /* Allocate 3 extra bytes: open paren, close paren, null byte. */ |
| 4332 | str = PyMem_RawMalloc(len + 3); |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4333 | if (str == NULL) { |
| 4334 | PyErr_NoMemory(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4335 | return NULL; |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 4336 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4337 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4338 | str[0] = '('; |
| 4339 | memcpy(str+1, expr_start, len); |
| 4340 | str[len+1] = ')'; |
| 4341 | str[len+2] = 0; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4342 | |
| 4343 | cf.cf_flags = PyCF_ONLY_AST; |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4344 | mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", |
| 4345 | Py_eval_input, 0); |
| 4346 | if (!mod_n) { |
| 4347 | PyMem_RawFree(str); |
| 4348 | return NULL; |
| 4349 | } |
| 4350 | /* Reuse str to find the correct column offset. */ |
| 4351 | str[0] = '{'; |
| 4352 | str[len+1] = '}'; |
| 4353 | fstring_fix_node_location(n, mod_n, str); |
| 4354 | mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4355 | PyMem_RawFree(str); |
Ćukasz Langa | e7c566c | 2017-09-06 17:27:58 -0700 | [diff] [blame] | 4356 | PyNode_Free(mod_n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4357 | if (!mod) |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4358 | return NULL; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4359 | return mod->v.Expression.body; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4360 | } |
| 4361 | |
| 4362 | /* Return -1 on error. |
| 4363 | |
| 4364 | Return 0 if we reached the end of the literal. |
| 4365 | |
| 4366 | Return 1 if we haven't reached the end of the literal, but we want |
| 4367 | the caller to process the literal up to this point. Used for |
| 4368 | doubled braces. |
| 4369 | */ |
| 4370 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4371 | fstring_find_literal(const char **str, const char *end, int raw, |
| 4372 | PyObject **literal, int recurse_lvl, |
| 4373 | struct compiling *c, const node *n) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4374 | { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4375 | /* Get any literal string. It ends when we hit an un-doubled left |
| 4376 | brace (which isn't part of a unicode name escape such as |
| 4377 | "\N{EULER CONSTANT}"), or the end of the string. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4378 | |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4379 | const char *s = *str; |
| 4380 | const char *literal_start = s; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4381 | int result = 0; |
| 4382 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4383 | assert(*literal == NULL); |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4384 | while (s < end) { |
| 4385 | char ch = *s++; |
| 4386 | if (!raw && ch == '\\' && s < end) { |
| 4387 | ch = *s++; |
| 4388 | if (ch == 'N') { |
| 4389 | if (s < end && *s++ == '{') { |
| 4390 | while (s < end && *s++ != '}') { |
| 4391 | } |
| 4392 | continue; |
| 4393 | } |
| 4394 | break; |
| 4395 | } |
| 4396 | if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) { |
| 4397 | return -1; |
| 4398 | } |
| 4399 | } |
| 4400 | if (ch == '{' || ch == '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4401 | /* Check for doubled braces, but only at the top level. If |
| 4402 | we checked at every level, then f'{0:{3}}' would fail |
| 4403 | with the two closing braces. */ |
| 4404 | if (recurse_lvl == 0) { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4405 | if (s < end && *s == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4406 | /* We're going to tell the caller that the literal ends |
| 4407 | here, but that they should continue scanning. But also |
| 4408 | skip over the second brace when we resume scanning. */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4409 | *str = s + 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4410 | result = 1; |
| 4411 | goto done; |
| 4412 | } |
| 4413 | |
| 4414 | /* Where a single '{' is the start of a new expression, a |
| 4415 | single '}' is not allowed. */ |
| 4416 | if (ch == '}') { |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4417 | *str = s - 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4418 | ast_error(c, n, "f-string: single '}' is not allowed"); |
| 4419 | return -1; |
| 4420 | } |
| 4421 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4422 | /* We're either at a '{', which means we're starting another |
| 4423 | expression; or a '}', which means we're at the end of this |
| 4424 | f-string (for a nested format_spec). */ |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4425 | s--; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4426 | break; |
| 4427 | } |
| 4428 | } |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4429 | *str = s; |
| 4430 | assert(s <= end); |
| 4431 | assert(s == end || *s == '{' || *s == '}'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4432 | done: |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4433 | if (literal_start != s) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4434 | if (raw) |
| 4435 | *literal = PyUnicode_DecodeUTF8Stateful(literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4436 | s - literal_start, |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4437 | NULL, NULL); |
| 4438 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 4439 | *literal = decode_unicode_with_escapes(c, n, literal_start, |
Serhiy Storchaka | 0cd7a3f | 2017-05-25 13:33:55 +0300 | [diff] [blame] | 4440 | s - literal_start); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4441 | if (!*literal) |
| 4442 | return -1; |
| 4443 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4444 | return result; |
| 4445 | } |
| 4446 | |
| 4447 | /* Forward declaration because parsing is recursive. */ |
| 4448 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4449 | 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] | 4450 | struct compiling *c, const node *n); |
| 4451 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4452 | /* Parse the f-string at *str, ending at end. We know *str starts an |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4453 | expression (so it must be a '{'). Returns the FormattedValue node, |
| 4454 | which includes the expression, conversion character, and |
| 4455 | format_spec expression. |
| 4456 | |
| 4457 | Note that I don't do a perfect job here: I don't make sure that a |
| 4458 | closing brace doesn't match an opening paren, for example. It |
| 4459 | doesn't need to error on all invalid expressions, just correctly |
| 4460 | find the end of all valid ones. Any errors inside the expression |
| 4461 | will be caught when we parse it later. */ |
| 4462 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4463 | fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4464 | expr_ty *expression, struct compiling *c, const node *n) |
| 4465 | { |
| 4466 | /* Return -1 on error, else 0. */ |
| 4467 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4468 | const char *expr_start; |
| 4469 | const char *expr_end; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4470 | expr_ty simple_expression; |
| 4471 | expr_ty format_spec = NULL; /* Optional format specifier. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4472 | int conversion = -1; /* The conversion char. -1 if not specified. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4473 | |
| 4474 | /* 0 if we're not in a string, else the quote char we're trying to |
| 4475 | match (single or double quote). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4476 | char quote_char = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4477 | |
| 4478 | /* If we're inside a string, 1=normal, 3=triple-quoted. */ |
| 4479 | int string_type = 0; |
| 4480 | |
| 4481 | /* Keep track of nesting level for braces/parens/brackets in |
| 4482 | expressions. */ |
| 4483 | Py_ssize_t nested_depth = 0; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame^] | 4484 | char parenstack[MAXLEVEL]; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4485 | |
| 4486 | /* Can only nest one level deep. */ |
| 4487 | if (recurse_lvl >= 2) { |
| 4488 | ast_error(c, n, "f-string: expressions nested too deeply"); |
| 4489 | return -1; |
| 4490 | } |
| 4491 | |
| 4492 | /* The first char must be a left brace, or we wouldn't have gotten |
| 4493 | here. Skip over it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4494 | assert(**str == '{'); |
| 4495 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4496 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4497 | expr_start = *str; |
| 4498 | for (; *str < end; (*str)++) { |
| 4499 | char ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4500 | |
| 4501 | /* Loop invariants. */ |
| 4502 | assert(nested_depth >= 0); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4503 | assert(*str >= expr_start && *str < end); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4504 | if (quote_char) |
| 4505 | assert(string_type == 1 || string_type == 3); |
| 4506 | else |
| 4507 | assert(string_type == 0); |
| 4508 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4509 | ch = **str; |
| 4510 | /* Nowhere inside an expression is a backslash allowed. */ |
| 4511 | if (ch == '\\') { |
| 4512 | /* Error: can't include a backslash character, inside |
| 4513 | parens or strings or not. */ |
| 4514 | ast_error(c, n, "f-string expression part " |
| 4515 | "cannot include a backslash"); |
| 4516 | return -1; |
| 4517 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4518 | if (quote_char) { |
| 4519 | /* We're inside a string. See if we're at the end. */ |
| 4520 | /* This code needs to implement the same non-error logic |
| 4521 | as tok_get from tokenizer.c, at the letter_quote |
| 4522 | label. To actually share that code would be a |
| 4523 | nightmare. But, it's unlikely to change and is small, |
| 4524 | so duplicate it here. Note we don't need to catch all |
| 4525 | of the errors, since they'll be caught when parsing the |
| 4526 | expression. We just need to match the non-error |
| 4527 | cases. Thus we can ignore \n in single-quoted strings, |
| 4528 | for example. Or non-terminated strings. */ |
| 4529 | if (ch == quote_char) { |
| 4530 | /* Does this match the string_type (single or triple |
| 4531 | quoted)? */ |
| 4532 | if (string_type == 3) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4533 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4534 | /* We're at the end of a triple quoted string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4535 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4536 | string_type = 0; |
| 4537 | quote_char = 0; |
| 4538 | continue; |
| 4539 | } |
| 4540 | } else { |
| 4541 | /* We're at the end of a normal string. */ |
| 4542 | quote_char = 0; |
| 4543 | string_type = 0; |
| 4544 | continue; |
| 4545 | } |
| 4546 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4547 | } else if (ch == '\'' || ch == '"') { |
| 4548 | /* Is this a triple quoted string? */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4549 | if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4550 | string_type = 3; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4551 | *str += 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4552 | } else { |
| 4553 | /* Start of a normal string. */ |
| 4554 | string_type = 1; |
| 4555 | } |
| 4556 | /* Start looking for the end of the string. */ |
| 4557 | quote_char = ch; |
| 4558 | } else if (ch == '[' || ch == '{' || ch == '(') { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame^] | 4559 | if (nested_depth >= MAXLEVEL) { |
| 4560 | ast_error(c, n, "f-string: too many nested parenthesis"); |
| 4561 | return -1; |
| 4562 | } |
| 4563 | parenstack[nested_depth] = ch; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4564 | nested_depth++; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4565 | } else if (ch == '#') { |
| 4566 | /* Error: can't include a comment character, inside parens |
| 4567 | or not. */ |
Eric V. Smith | 09835dc | 2016-09-11 18:58:20 -0400 | [diff] [blame] | 4568 | ast_error(c, n, "f-string expression part cannot include '#'"); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4569 | return -1; |
| 4570 | } else if (nested_depth == 0 && |
| 4571 | (ch == '!' || ch == ':' || ch == '}')) { |
| 4572 | /* First, test for the special case of "!=". Since '=' is |
| 4573 | not an allowed conversion character, nothing is lost in |
| 4574 | this test. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4575 | if (ch == '!' && *str+1 < end && *(*str+1) == '=') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4576 | /* This isn't a conversion character, just continue. */ |
| 4577 | continue; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4578 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4579 | /* Normal way out of this loop. */ |
| 4580 | break; |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame^] | 4581 | } else if (ch == ']' || ch == '}' || ch == ')') { |
| 4582 | if (!nested_depth) { |
| 4583 | ast_error(c, n, "f-string: unmatched '%c'", ch); |
| 4584 | return -1; |
| 4585 | } |
| 4586 | nested_depth--; |
| 4587 | int opening = parenstack[nested_depth]; |
| 4588 | if (!((opening == '(' && ch == ')') || |
| 4589 | (opening == '[' && ch == ']') || |
| 4590 | (opening == '{' && ch == '}'))) |
| 4591 | { |
| 4592 | ast_error(c, n, |
| 4593 | "f-string: closing parenthesis '%c' " |
| 4594 | "does not match opening parenthesis '%c'", |
| 4595 | ch, opening); |
| 4596 | return -1; |
| 4597 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4598 | } else { |
| 4599 | /* Just consume this char and loop around. */ |
| 4600 | } |
| 4601 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4602 | expr_end = *str; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4603 | /* If we leave this loop in a string or with mismatched parens, we |
| 4604 | don't care. We'll get a syntax error when compiling the |
| 4605 | expression. But, we can produce a better error message, so |
| 4606 | let's just do that.*/ |
| 4607 | if (quote_char) { |
| 4608 | ast_error(c, n, "f-string: unterminated string"); |
| 4609 | return -1; |
| 4610 | } |
| 4611 | if (nested_depth) { |
Serhiy Storchaka | 58159ef | 2019-01-12 09:46:50 +0200 | [diff] [blame^] | 4612 | int opening = parenstack[nested_depth - 1]; |
| 4613 | ast_error(c, n, "f-string: unmatched '%c'", opening); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4614 | return -1; |
| 4615 | } |
| 4616 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4617 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4618 | goto unexpected_end_of_string; |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4619 | |
| 4620 | /* Compile the expression as soon as possible, so we show errors |
| 4621 | related to the expression before errors related to the |
| 4622 | conversion or format_spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4623 | simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); |
Eric V. Smith | 1d44c41 | 2015-09-23 07:49:00 -0400 | [diff] [blame] | 4624 | if (!simple_expression) |
| 4625 | return -1; |
| 4626 | |
| 4627 | /* Check for a conversion char, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4628 | if (**str == '!') { |
| 4629 | *str += 1; |
| 4630 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4631 | goto unexpected_end_of_string; |
| 4632 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4633 | conversion = **str; |
| 4634 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4635 | |
| 4636 | /* Validate the conversion. */ |
| 4637 | if (!(conversion == 's' || conversion == 'r' |
| 4638 | || conversion == 'a')) { |
| 4639 | ast_error(c, n, "f-string: invalid conversion character: " |
| 4640 | "expected 's', 'r', or 'a'"); |
| 4641 | return -1; |
| 4642 | } |
| 4643 | } |
| 4644 | |
| 4645 | /* Check for the format spec, if present. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4646 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4647 | goto unexpected_end_of_string; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4648 | if (**str == ':') { |
| 4649 | *str += 1; |
| 4650 | if (*str >= end) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4651 | goto unexpected_end_of_string; |
| 4652 | |
| 4653 | /* Parse the format spec. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4654 | 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] | 4655 | if (!format_spec) |
| 4656 | return -1; |
| 4657 | } |
| 4658 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4659 | if (*str >= end || **str != '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4660 | goto unexpected_end_of_string; |
| 4661 | |
| 4662 | /* We're at a right brace. Consume it. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4663 | assert(*str < end); |
| 4664 | assert(**str == '}'); |
| 4665 | *str += 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4666 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4667 | /* And now create the FormattedValue node that represents this |
| 4668 | entire expression with the conversion and format spec. */ |
Benjamin Peterson | 4ba5c88 | 2016-09-09 19:31:12 -0700 | [diff] [blame] | 4669 | *expression = FormattedValue(simple_expression, conversion, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4670 | format_spec, LINENO(n), n->n_col_offset, |
| 4671 | c->c_arena); |
| 4672 | if (!*expression) |
| 4673 | return -1; |
| 4674 | |
| 4675 | return 0; |
| 4676 | |
| 4677 | unexpected_end_of_string: |
| 4678 | ast_error(c, n, "f-string: expecting '}'"); |
| 4679 | return -1; |
| 4680 | } |
| 4681 | |
| 4682 | /* Return -1 on error. |
| 4683 | |
| 4684 | Return 0 if we have a literal (possible zero length) and an |
| 4685 | expression (zero length if at the end of the string. |
| 4686 | |
| 4687 | Return 1 if we have a literal, but no expression, and we want the |
| 4688 | caller to call us again. This is used to deal with doubled |
| 4689 | braces. |
| 4690 | |
| 4691 | When called multiple times on the string 'a{{b{0}c', this function |
| 4692 | will return: |
| 4693 | |
| 4694 | 1. the literal 'a{' with no expression, and a return value |
| 4695 | of 1. Despite the fact that there's no expression, the return |
| 4696 | value of 1 means we're not finished yet. |
| 4697 | |
| 4698 | 2. the literal 'b' and the expression '0', with a return value of |
| 4699 | 0. The fact that there's an expression means we're not finished. |
| 4700 | |
| 4701 | 3. literal 'c' with no expression and a return value of 0. The |
| 4702 | combination of the return value of 0 with no expression means |
| 4703 | we're finished. |
| 4704 | */ |
| 4705 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4706 | fstring_find_literal_and_expr(const char **str, const char *end, int raw, |
| 4707 | int recurse_lvl, PyObject **literal, |
| 4708 | expr_ty *expression, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4709 | struct compiling *c, const node *n) |
| 4710 | { |
| 4711 | int result; |
| 4712 | |
| 4713 | assert(*literal == NULL && *expression == NULL); |
| 4714 | |
| 4715 | /* Get any literal string. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4716 | 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] | 4717 | if (result < 0) |
| 4718 | goto error; |
| 4719 | |
| 4720 | assert(result == 0 || result == 1); |
| 4721 | |
| 4722 | if (result == 1) |
| 4723 | /* We have a literal, but don't look at the expression. */ |
| 4724 | return 1; |
| 4725 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4726 | if (*str >= end || **str == '}') |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4727 | /* We're at the end of the string or the end of a nested |
| 4728 | f-string: no expression. The top-level error case where we |
| 4729 | expect to be at the end of the string but we're at a '}' is |
| 4730 | handled later. */ |
| 4731 | return 0; |
| 4732 | |
| 4733 | /* We must now be the start of an expression, on a '{'. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4734 | assert(**str == '{'); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4735 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4736 | if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4737 | goto error; |
| 4738 | |
| 4739 | return 0; |
| 4740 | |
| 4741 | error: |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4742 | Py_CLEAR(*literal); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4743 | return -1; |
| 4744 | } |
| 4745 | |
| 4746 | #define EXPRLIST_N_CACHED 64 |
| 4747 | |
| 4748 | typedef struct { |
| 4749 | /* Incrementally build an array of expr_ty, so be used in an |
| 4750 | asdl_seq. Cache some small but reasonably sized number of |
| 4751 | expr_ty's, and then after that start dynamically allocating, |
| 4752 | doubling the number allocated each time. Note that the f-string |
| 4753 | 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] | 4754 | Constant for the literal 'a'. So you add expr_ty's about twice as |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4755 | fast as you add exressions in an f-string. */ |
| 4756 | |
| 4757 | Py_ssize_t allocated; /* Number we've allocated. */ |
| 4758 | Py_ssize_t size; /* Number we've used. */ |
| 4759 | expr_ty *p; /* Pointer to the memory we're actually |
| 4760 | using. Will point to 'data' until we |
| 4761 | start dynamically allocating. */ |
| 4762 | expr_ty data[EXPRLIST_N_CACHED]; |
| 4763 | } ExprList; |
| 4764 | |
| 4765 | #ifdef NDEBUG |
| 4766 | #define ExprList_check_invariants(l) |
| 4767 | #else |
| 4768 | static void |
| 4769 | ExprList_check_invariants(ExprList *l) |
| 4770 | { |
| 4771 | /* Check our invariants. Make sure this object is "live", and |
| 4772 | hasn't been deallocated. */ |
| 4773 | assert(l->size >= 0); |
| 4774 | assert(l->p != NULL); |
| 4775 | if (l->size <= EXPRLIST_N_CACHED) |
| 4776 | assert(l->data == l->p); |
| 4777 | } |
| 4778 | #endif |
| 4779 | |
| 4780 | static void |
| 4781 | ExprList_Init(ExprList *l) |
| 4782 | { |
| 4783 | l->allocated = EXPRLIST_N_CACHED; |
| 4784 | l->size = 0; |
| 4785 | |
| 4786 | /* Until we start allocating dynamically, p points to data. */ |
| 4787 | l->p = l->data; |
| 4788 | |
| 4789 | ExprList_check_invariants(l); |
| 4790 | } |
| 4791 | |
| 4792 | static int |
| 4793 | ExprList_Append(ExprList *l, expr_ty exp) |
| 4794 | { |
| 4795 | ExprList_check_invariants(l); |
| 4796 | if (l->size >= l->allocated) { |
| 4797 | /* We need to alloc (or realloc) the memory. */ |
| 4798 | Py_ssize_t new_size = l->allocated * 2; |
| 4799 | |
| 4800 | /* See if we've ever allocated anything dynamically. */ |
| 4801 | if (l->p == l->data) { |
| 4802 | Py_ssize_t i; |
| 4803 | /* We're still using the cached data. Switch to |
| 4804 | alloc-ing. */ |
| 4805 | l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size); |
| 4806 | if (!l->p) |
| 4807 | return -1; |
| 4808 | /* Copy the cached data into the new buffer. */ |
| 4809 | for (i = 0; i < l->size; i++) |
| 4810 | l->p[i] = l->data[i]; |
| 4811 | } else { |
| 4812 | /* Just realloc. */ |
| 4813 | expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size); |
| 4814 | if (!tmp) { |
| 4815 | PyMem_RawFree(l->p); |
| 4816 | l->p = NULL; |
| 4817 | return -1; |
| 4818 | } |
| 4819 | l->p = tmp; |
| 4820 | } |
| 4821 | |
| 4822 | l->allocated = new_size; |
| 4823 | assert(l->allocated == 2 * l->size); |
| 4824 | } |
| 4825 | |
| 4826 | l->p[l->size++] = exp; |
| 4827 | |
| 4828 | ExprList_check_invariants(l); |
| 4829 | return 0; |
| 4830 | } |
| 4831 | |
| 4832 | static void |
| 4833 | ExprList_Dealloc(ExprList *l) |
| 4834 | { |
| 4835 | ExprList_check_invariants(l); |
| 4836 | |
| 4837 | /* If there's been an error, or we've never dynamically allocated, |
| 4838 | do nothing. */ |
| 4839 | if (!l->p || l->p == l->data) { |
| 4840 | /* Do nothing. */ |
| 4841 | } else { |
| 4842 | /* We have dynamically allocated. Free the memory. */ |
| 4843 | PyMem_RawFree(l->p); |
| 4844 | } |
| 4845 | l->p = NULL; |
| 4846 | l->size = -1; |
| 4847 | } |
| 4848 | |
| 4849 | static asdl_seq * |
| 4850 | ExprList_Finish(ExprList *l, PyArena *arena) |
| 4851 | { |
| 4852 | asdl_seq *seq; |
| 4853 | |
| 4854 | ExprList_check_invariants(l); |
| 4855 | |
| 4856 | /* Allocate the asdl_seq and copy the expressions in to it. */ |
| 4857 | seq = _Py_asdl_seq_new(l->size, arena); |
| 4858 | if (seq) { |
| 4859 | Py_ssize_t i; |
| 4860 | for (i = 0; i < l->size; i++) |
| 4861 | asdl_seq_SET(seq, i, l->p[i]); |
| 4862 | } |
| 4863 | ExprList_Dealloc(l); |
| 4864 | return seq; |
| 4865 | } |
| 4866 | |
| 4867 | /* The FstringParser is designed to add a mix of strings and |
| 4868 | f-strings, and concat them together as needed. Ultimately, it |
| 4869 | generates an expr_ty. */ |
| 4870 | typedef struct { |
| 4871 | PyObject *last_str; |
| 4872 | ExprList expr_list; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4873 | int fmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4874 | } FstringParser; |
| 4875 | |
| 4876 | #ifdef NDEBUG |
| 4877 | #define FstringParser_check_invariants(state) |
| 4878 | #else |
| 4879 | static void |
| 4880 | FstringParser_check_invariants(FstringParser *state) |
| 4881 | { |
| 4882 | if (state->last_str) |
| 4883 | assert(PyUnicode_CheckExact(state->last_str)); |
| 4884 | ExprList_check_invariants(&state->expr_list); |
| 4885 | } |
| 4886 | #endif |
| 4887 | |
| 4888 | static void |
| 4889 | FstringParser_Init(FstringParser *state) |
| 4890 | { |
| 4891 | state->last_str = NULL; |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4892 | state->fmode = 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4893 | ExprList_Init(&state->expr_list); |
| 4894 | FstringParser_check_invariants(state); |
| 4895 | } |
| 4896 | |
| 4897 | static void |
| 4898 | FstringParser_Dealloc(FstringParser *state) |
| 4899 | { |
| 4900 | FstringParser_check_invariants(state); |
| 4901 | |
| 4902 | Py_XDECREF(state->last_str); |
| 4903 | ExprList_Dealloc(&state->expr_list); |
| 4904 | } |
| 4905 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4906 | /* Make a Constant node, but decref the PyUnicode object being added. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4907 | static expr_ty |
| 4908 | make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) |
| 4909 | { |
| 4910 | PyObject *s = *str; |
| 4911 | *str = NULL; |
| 4912 | assert(PyUnicode_CheckExact(s)); |
| 4913 | if (PyArena_AddPyObject(c->c_arena, s) < 0) { |
| 4914 | Py_DECREF(s); |
| 4915 | return NULL; |
| 4916 | } |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4917 | return Constant(s, LINENO(n), n->n_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | /* Add a non-f-string (that is, a regular literal string). str is |
| 4921 | decref'd. */ |
| 4922 | static int |
| 4923 | FstringParser_ConcatAndDel(FstringParser *state, PyObject *str) |
| 4924 | { |
| 4925 | FstringParser_check_invariants(state); |
| 4926 | |
| 4927 | assert(PyUnicode_CheckExact(str)); |
| 4928 | |
| 4929 | if (PyUnicode_GET_LENGTH(str) == 0) { |
| 4930 | Py_DECREF(str); |
| 4931 | return 0; |
| 4932 | } |
| 4933 | |
| 4934 | if (!state->last_str) { |
| 4935 | /* We didn't have a string before, so just remember this one. */ |
| 4936 | state->last_str = str; |
| 4937 | } else { |
| 4938 | /* Concatenate this with the previous string. */ |
Serhiy Storchaka | 726fc13 | 2015-12-27 15:44:33 +0200 | [diff] [blame] | 4939 | PyUnicode_AppendAndDel(&state->last_str, str); |
| 4940 | if (!state->last_str) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4941 | return -1; |
| 4942 | } |
| 4943 | FstringParser_check_invariants(state); |
| 4944 | return 0; |
| 4945 | } |
| 4946 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4947 | /* Parse an f-string. The f-string is in *str to end, with no |
| 4948 | 'f' or quotes. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4949 | static int |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4950 | FstringParser_ConcatFstring(FstringParser *state, const char **str, |
| 4951 | const char *end, int raw, int recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4952 | struct compiling *c, const node *n) |
| 4953 | { |
| 4954 | FstringParser_check_invariants(state); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4955 | state->fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4956 | |
| 4957 | /* Parse the f-string. */ |
| 4958 | while (1) { |
| 4959 | PyObject *literal = NULL; |
| 4960 | expr_ty expression = NULL; |
| 4961 | |
| 4962 | /* If there's a zero length literal in front of the |
| 4963 | expression, literal will be NULL. If we're at the end of |
| 4964 | the f-string, expression will be NULL (unless result == 1, |
| 4965 | see below). */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 4966 | int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4967 | &literal, &expression, |
| 4968 | c, n); |
| 4969 | if (result < 0) |
| 4970 | return -1; |
| 4971 | |
| 4972 | /* Add the literal, if any. */ |
| 4973 | if (!literal) { |
| 4974 | /* Do nothing. Just leave last_str alone (and possibly |
| 4975 | NULL). */ |
| 4976 | } else if (!state->last_str) { |
ericvsmith | 11e97f2 | 2017-06-16 06:19:32 -0400 | [diff] [blame] | 4977 | /* Note that the literal can be zero length, if the |
| 4978 | input string is "\\\n" or "\\\r", among others. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4979 | state->last_str = literal; |
| 4980 | literal = NULL; |
| 4981 | } else { |
| 4982 | /* We have a literal, concatenate it. */ |
| 4983 | assert(PyUnicode_GET_LENGTH(literal) != 0); |
| 4984 | if (FstringParser_ConcatAndDel(state, literal) < 0) |
| 4985 | return -1; |
| 4986 | literal = NULL; |
| 4987 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4988 | |
| 4989 | /* We've dealt with the literal now. It can't be leaked on further |
| 4990 | errors. */ |
| 4991 | assert(literal == NULL); |
| 4992 | |
| 4993 | /* See if we should just loop around to get the next literal |
| 4994 | and expression, while ignoring the expression this |
| 4995 | time. This is used for un-doubling braces, as an |
| 4996 | optimization. */ |
| 4997 | if (result == 1) |
| 4998 | continue; |
| 4999 | |
| 5000 | if (!expression) |
| 5001 | /* We're done with this f-string. */ |
| 5002 | break; |
| 5003 | |
| 5004 | /* We know we have an expression. Convert any existing string |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5005 | to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5006 | if (!state->last_str) { |
| 5007 | /* Do nothing. No previous literal. */ |
| 5008 | } else { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5009 | /* Convert the existing last_str literal to a Constant node. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5010 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5011 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5012 | return -1; |
| 5013 | } |
| 5014 | |
| 5015 | if (ExprList_Append(&state->expr_list, expression) < 0) |
| 5016 | return -1; |
| 5017 | } |
| 5018 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5019 | /* If recurse_lvl is zero, then we must be at the end of the |
| 5020 | string. Otherwise, we must be at a right brace. */ |
| 5021 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5022 | if (recurse_lvl == 0 && *str < end-1) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5023 | ast_error(c, n, "f-string: unexpected end of string"); |
| 5024 | return -1; |
| 5025 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5026 | if (recurse_lvl != 0 && **str != '}') { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5027 | ast_error(c, n, "f-string: expecting '}'"); |
| 5028 | return -1; |
| 5029 | } |
| 5030 | |
| 5031 | FstringParser_check_invariants(state); |
| 5032 | return 0; |
| 5033 | } |
| 5034 | |
| 5035 | /* 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] | 5036 | 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] | 5037 | static expr_ty |
| 5038 | FstringParser_Finish(FstringParser *state, struct compiling *c, |
| 5039 | const node *n) |
| 5040 | { |
| 5041 | asdl_seq *seq; |
| 5042 | |
| 5043 | FstringParser_check_invariants(state); |
| 5044 | |
| 5045 | /* If we're just a constant string with no expressions, return |
| 5046 | that. */ |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 5047 | if (!state->fmode) { |
| 5048 | assert(!state->expr_list.size); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5049 | if (!state->last_str) { |
| 5050 | /* Create a zero length string. */ |
| 5051 | state->last_str = PyUnicode_FromStringAndSize(NULL, 0); |
| 5052 | if (!state->last_str) |
| 5053 | goto error; |
| 5054 | } |
| 5055 | return make_str_node_and_del(&state->last_str, c, n); |
| 5056 | } |
| 5057 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5058 | /* 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] | 5059 | last node in our expression list. */ |
| 5060 | if (state->last_str) { |
| 5061 | expr_ty str = make_str_node_and_del(&state->last_str, c, n); |
| 5062 | if (!str || ExprList_Append(&state->expr_list, str) < 0) |
| 5063 | goto error; |
| 5064 | } |
| 5065 | /* This has already been freed. */ |
| 5066 | assert(state->last_str == NULL); |
| 5067 | |
| 5068 | seq = ExprList_Finish(&state->expr_list, c->c_arena); |
| 5069 | if (!seq) |
| 5070 | goto error; |
| 5071 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5072 | return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 5073 | |
| 5074 | error: |
| 5075 | FstringParser_Dealloc(state); |
| 5076 | return NULL; |
| 5077 | } |
| 5078 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5079 | /* Given an f-string (with no 'f' or quotes) that's in *str and ends |
| 5080 | at end, parse it into an expr_ty. Return NULL on error. Adjust |
| 5081 | str to point past the parsed portion. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5082 | static expr_ty |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5083 | 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] | 5084 | struct compiling *c, const node *n) |
| 5085 | { |
| 5086 | FstringParser state; |
| 5087 | |
| 5088 | FstringParser_Init(&state); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5089 | if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl, |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5090 | c, n) < 0) { |
| 5091 | FstringParser_Dealloc(&state); |
| 5092 | return NULL; |
| 5093 | } |
| 5094 | |
| 5095 | return FstringParser_Finish(&state, c, n); |
| 5096 | } |
| 5097 | |
| 5098 | /* n is a Python string literal, including the bracketing quote |
| 5099 | 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] | 5100 | escape sequences (if any). parsestr parses it, and sets *result to |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5101 | 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] | 5102 | *fstr and *fstrlen to the unparsed string object. Return 0 if no |
| 5103 | errors occurred. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5104 | */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5105 | static int |
| 5106 | parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, |
| 5107 | PyObject **result, const char **fstr, Py_ssize_t *fstrlen) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5108 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5109 | size_t len; |
| 5110 | const char *s = STR(n); |
| 5111 | int quote = Py_CHARMASK(*s); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5112 | int fmode = 0; |
| 5113 | *bytesmode = 0; |
| 5114 | *rawmode = 0; |
| 5115 | *result = NULL; |
| 5116 | *fstr = NULL; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 5117 | if (Py_ISALPHA(quote)) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5118 | while (!*bytesmode || !*rawmode) { |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5119 | if (quote == 'b' || quote == 'B') { |
| 5120 | quote = *++s; |
| 5121 | *bytesmode = 1; |
| 5122 | } |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 5123 | else if (quote == 'u' || quote == 'U') { |
| 5124 | quote = *++s; |
| 5125 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5126 | else if (quote == 'r' || quote == 'R') { |
| 5127 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5128 | *rawmode = 1; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5129 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5130 | else if (quote == 'f' || quote == 'F') { |
| 5131 | quote = *++s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5132 | fmode = 1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5133 | } |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 5134 | else { |
| 5135 | break; |
| 5136 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5137 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5138 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5139 | if (fmode && *bytesmode) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5140 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5141 | return -1; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5142 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5143 | if (quote != '\'' && quote != '\"') { |
| 5144 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5145 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5146 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5147 | /* Skip the leading quote char. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5148 | s++; |
| 5149 | len = strlen(s); |
| 5150 | if (len > INT_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5151 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5152 | "string to parse is too long"); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5153 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5154 | } |
| 5155 | if (s[--len] != quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5156 | /* Last quote char must match the first. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5157 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5158 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5159 | } |
| 5160 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5161 | /* A triple quoted string. We've already skipped one quote at |
| 5162 | the start and one at the end of the string. Now skip the |
| 5163 | two at the start. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5164 | s += 2; |
| 5165 | len -= 2; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5166 | /* And check that the last two match. */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5167 | if (s[--len] != quote || s[--len] != quote) { |
| 5168 | PyErr_BadInternalCall(); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5169 | return -1; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5170 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5171 | } |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5172 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5173 | if (fmode) { |
| 5174 | /* Just return the bytes. The caller will parse the resulting |
| 5175 | string. */ |
| 5176 | *fstr = s; |
| 5177 | *fstrlen = len; |
| 5178 | return 0; |
Eric V. Smith | 6a4efce | 2016-09-03 09:18:34 -0400 | [diff] [blame] | 5179 | } |
| 5180 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5181 | /* Not an f-string. */ |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5182 | /* Avoid invoking escape decoding routines if possible. */ |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5183 | *rawmode = *rawmode || strchr(s, '\\') == NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5184 | if (*bytesmode) { |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5185 | /* Disallow non-ASCII characters. */ |
Benjamin Peterson | bd0df50 | 2012-09-02 15:04:51 -0400 | [diff] [blame] | 5186 | const char *ch; |
| 5187 | for (ch = s; *ch; ch++) { |
| 5188 | if (Py_CHARMASK(*ch) >= 0x80) { |
| 5189 | ast_error(c, n, "bytes can only contain ASCII " |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5190 | "literal characters."); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5191 | return -1; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5192 | } |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 5193 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5194 | if (*rawmode) |
| 5195 | *result = PyBytes_FromStringAndSize(s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5196 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5197 | *result = decode_bytes_with_escapes(c, n, s, len); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5198 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5199 | if (*rawmode) |
| 5200 | *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); |
Benjamin Peterson | 768921c | 2016-02-25 23:13:53 -0800 | [diff] [blame] | 5201 | else |
Eric V. Smith | 5646648 | 2016-10-31 14:46:26 -0400 | [diff] [blame] | 5202 | *result = decode_unicode_with_escapes(c, n, s, len); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5203 | } |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5204 | return *result == NULL ? -1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5207 | /* Accepts a STRING+ atom, and produces an expr_ty node. Run through |
| 5208 | each STRING atom, and process it as needed. For bytes, just |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5209 | 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] | 5210 | normal strings and f-strings, concatenate them together. The result |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5211 | 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] | 5212 | node if there's just an f-string (with no leading or trailing |
| 5213 | literals), or a JoinedStr node if there are multiple f-strings or |
| 5214 | any literals involved. */ |
| 5215 | static expr_ty |
| 5216 | parsestrplus(struct compiling *c, const node *n) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5217 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5218 | int bytesmode = 0; |
| 5219 | PyObject *bytes_str = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5220 | int i; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5221 | |
| 5222 | FstringParser state; |
| 5223 | FstringParser_Init(&state); |
| 5224 | |
| 5225 | for (i = 0; i < NCH(n); i++) { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5226 | int this_bytesmode; |
| 5227 | int this_rawmode; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5228 | PyObject *s; |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5229 | const char *fstr; |
| 5230 | Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5231 | |
| 5232 | REQ(CHILD(n, i), STRING); |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5233 | if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s, |
| 5234 | &fstr, &fstrlen) != 0) |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5235 | goto error; |
| 5236 | |
| 5237 | /* Check that we're not mixing bytes with unicode. */ |
| 5238 | if (i != 0 && bytesmode != this_bytesmode) { |
| 5239 | ast_error(c, n, "cannot mix bytes and nonbytes literals"); |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5240 | /* s is NULL if the current string part is an f-string. */ |
| 5241 | Py_XDECREF(s); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5242 | goto error; |
| 5243 | } |
| 5244 | bytesmode = this_bytesmode; |
| 5245 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5246 | if (fstr != NULL) { |
| 5247 | int result; |
| 5248 | assert(s == NULL && !bytesmode); |
| 5249 | /* This is an f-string. Parse and concatenate it. */ |
| 5250 | result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen, |
| 5251 | this_rawmode, 0, c, n); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5252 | if (result < 0) |
| 5253 | goto error; |
| 5254 | } else { |
Eric V. Smith | 9b88fdf | 2016-11-07 17:54:01 -0500 | [diff] [blame] | 5255 | /* A string or byte string. */ |
| 5256 | assert(s != NULL && fstr == NULL); |
| 5257 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5258 | assert(bytesmode ? PyBytes_CheckExact(s) : |
| 5259 | PyUnicode_CheckExact(s)); |
| 5260 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5261 | if (bytesmode) { |
| 5262 | /* For bytes, concat as we go. */ |
| 5263 | if (i == 0) { |
| 5264 | /* First time, just remember this value. */ |
| 5265 | bytes_str = s; |
| 5266 | } else { |
| 5267 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 5268 | if (!bytes_str) |
| 5269 | goto error; |
| 5270 | } |
| 5271 | } else { |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 5272 | /* This is a regular string. Concatenate it. */ |
| 5273 | if (FstringParser_ConcatAndDel(&state, s) < 0) |
| 5274 | goto error; |
| 5275 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5276 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5277 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5278 | if (bytesmode) { |
| 5279 | /* Just return the bytes object and we're done. */ |
| 5280 | if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) |
| 5281 | goto error; |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 5282 | return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5283 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5284 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5285 | /* We're not a bytes string, bytes_str should never have been set. */ |
| 5286 | assert(bytes_str == NULL); |
| 5287 | |
| 5288 | return FstringParser_Finish(&state, c, n); |
| 5289 | |
| 5290 | error: |
| 5291 | Py_XDECREF(bytes_str); |
| 5292 | FstringParser_Dealloc(&state); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5293 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5294 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5295 | |
| 5296 | PyObject * |
| 5297 | _PyAST_GetDocString(asdl_seq *body) |
| 5298 | { |
| 5299 | if (!asdl_seq_LEN(body)) { |
| 5300 | return NULL; |
| 5301 | } |
| 5302 | stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); |
| 5303 | if (st->kind != Expr_kind) { |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | expr_ty e = st->v.Expr.value; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 5307 | if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { |
| 5308 | return e->v.Constant.value; |
| 5309 | } |
| 5310 | return NULL; |
| 5311 | } |