Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1 | #include <Python.h> |
| 2 | #include <errcode.h> |
Pablo Galindo | 1ed83ad | 2020-06-11 17:30:46 +0100 | [diff] [blame] | 3 | #include "tokenizer.h" |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4 | |
| 5 | #include "pegen.h" |
Pablo Galindo | 1ed83ad | 2020-06-11 17:30:46 +0100 | [diff] [blame] | 6 | #include "string_parser.h" |
Pablo Galindo | 1332226 | 2020-07-27 23:46:59 +0100 | [diff] [blame] | 7 | #include "ast.h" |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 8 | |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 9 | PyObject * |
| 10 | _PyPegen_new_type_comment(Parser *p, char *s) |
| 11 | { |
| 12 | PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); |
| 13 | if (res == NULL) { |
| 14 | return NULL; |
| 15 | } |
| 16 | if (PyArena_AddPyObject(p->arena, res) < 0) { |
| 17 | Py_DECREF(res); |
| 18 | return NULL; |
| 19 | } |
| 20 | return res; |
| 21 | } |
| 22 | |
| 23 | arg_ty |
| 24 | _PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc) |
| 25 | { |
| 26 | if (tc == NULL) { |
| 27 | return a; |
| 28 | } |
| 29 | char *bytes = PyBytes_AsString(tc->bytes); |
| 30 | if (bytes == NULL) { |
| 31 | return NULL; |
| 32 | } |
| 33 | PyObject *tco = _PyPegen_new_type_comment(p, bytes); |
| 34 | if (tco == NULL) { |
| 35 | return NULL; |
| 36 | } |
| 37 | return arg(a->arg, a->annotation, tco, |
| 38 | a->lineno, a->col_offset, a->end_lineno, a->end_col_offset, |
| 39 | p->arena); |
| 40 | } |
| 41 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 42 | static int |
| 43 | init_normalization(Parser *p) |
| 44 | { |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 45 | if (p->normalize) { |
| 46 | return 1; |
| 47 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 48 | PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
| 49 | if (!m) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | p->normalize = PyObject_GetAttrString(m, "normalize"); |
| 54 | Py_DECREF(m); |
| 55 | if (!p->normalize) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | return 1; |
| 60 | } |
| 61 | |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 62 | /* Checks if the NOTEQUAL token is valid given the current parser flags |
| 63 | 0 indicates success and nonzero indicates failure (an exception may be set) */ |
| 64 | int |
Pablo Galindo | 06f8c33 | 2020-10-30 23:48:42 +0000 | [diff] [blame] | 65 | _PyPegen_check_barry_as_flufl(Parser *p, Token* t) { |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 66 | assert(t->bytes != NULL); |
| 67 | assert(t->type == NOTEQUAL); |
| 68 | |
| 69 | char* tok_str = PyBytes_AS_STRING(t->bytes); |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 70 | if (p->flags & PyPARSE_BARRY_AS_BDFL && strcmp(tok_str, "<>") != 0) { |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 71 | RAISE_SYNTAX_ERROR("with Barry as BDFL, use '<>' instead of '!='"); |
| 72 | return -1; |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 73 | } |
| 74 | if (!(p->flags & PyPARSE_BARRY_AS_BDFL)) { |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 75 | return strcmp(tok_str, "!="); |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 80 | PyObject * |
| 81 | _PyPegen_new_identifier(Parser *p, char *n) |
| 82 | { |
| 83 | PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
| 84 | if (!id) { |
| 85 | goto error; |
| 86 | } |
| 87 | /* PyUnicode_DecodeUTF8 should always return a ready string. */ |
| 88 | assert(PyUnicode_IS_READY(id)); |
| 89 | /* Check whether there are non-ASCII characters in the |
| 90 | identifier; if so, normalize to NFKC. */ |
| 91 | if (!PyUnicode_IS_ASCII(id)) |
| 92 | { |
| 93 | PyObject *id2; |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 94 | if (!init_normalization(p)) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 95 | { |
| 96 | Py_DECREF(id); |
| 97 | goto error; |
| 98 | } |
| 99 | PyObject *form = PyUnicode_InternFromString("NFKC"); |
| 100 | if (form == NULL) |
| 101 | { |
| 102 | Py_DECREF(id); |
| 103 | goto error; |
| 104 | } |
| 105 | PyObject *args[2] = {form, id}; |
| 106 | id2 = _PyObject_FastCall(p->normalize, args, 2); |
| 107 | Py_DECREF(id); |
| 108 | Py_DECREF(form); |
| 109 | if (!id2) { |
| 110 | goto error; |
| 111 | } |
| 112 | if (!PyUnicode_Check(id2)) |
| 113 | { |
| 114 | PyErr_Format(PyExc_TypeError, |
| 115 | "unicodedata.normalize() must return a string, not " |
| 116 | "%.200s", |
| 117 | _PyType_Name(Py_TYPE(id2))); |
| 118 | Py_DECREF(id2); |
| 119 | goto error; |
| 120 | } |
| 121 | id = id2; |
| 122 | } |
| 123 | PyUnicode_InternInPlace(&id); |
| 124 | if (PyArena_AddPyObject(p->arena, id) < 0) |
| 125 | { |
| 126 | Py_DECREF(id); |
| 127 | goto error; |
| 128 | } |
| 129 | return id; |
| 130 | |
| 131 | error: |
| 132 | p->error_indicator = 1; |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | static PyObject * |
| 137 | _create_dummy_identifier(Parser *p) |
| 138 | { |
| 139 | return _PyPegen_new_identifier(p, ""); |
| 140 | } |
| 141 | |
| 142 | static inline Py_ssize_t |
Pablo Galindo | 51c5896 | 2020-06-16 16:49:43 +0100 | [diff] [blame] | 143 | byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 144 | { |
| 145 | const char *str = PyUnicode_AsUTF8(line); |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 146 | if (!str) { |
| 147 | return 0; |
| 148 | } |
Pablo Galindo | 51c5896 | 2020-06-16 16:49:43 +0100 | [diff] [blame] | 149 | assert(col_offset >= 0 && (unsigned long)col_offset <= strlen(str)); |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 150 | PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace"); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 151 | if (!text) { |
| 152 | return 0; |
| 153 | } |
| 154 | Py_ssize_t size = PyUnicode_GET_LENGTH(text); |
| 155 | Py_DECREF(text); |
| 156 | return size; |
| 157 | } |
| 158 | |
| 159 | const char * |
| 160 | _PyPegen_get_expr_name(expr_ty e) |
| 161 | { |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame] | 162 | assert(e != NULL); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 163 | switch (e->kind) { |
| 164 | case Attribute_kind: |
| 165 | return "attribute"; |
| 166 | case Subscript_kind: |
| 167 | return "subscript"; |
| 168 | case Starred_kind: |
| 169 | return "starred"; |
| 170 | case Name_kind: |
| 171 | return "name"; |
| 172 | case List_kind: |
| 173 | return "list"; |
| 174 | case Tuple_kind: |
| 175 | return "tuple"; |
| 176 | case Lambda_kind: |
| 177 | return "lambda"; |
| 178 | case Call_kind: |
| 179 | return "function call"; |
| 180 | case BoolOp_kind: |
| 181 | case BinOp_kind: |
| 182 | case UnaryOp_kind: |
| 183 | return "operator"; |
| 184 | case GeneratorExp_kind: |
| 185 | return "generator expression"; |
| 186 | case Yield_kind: |
| 187 | case YieldFrom_kind: |
| 188 | return "yield expression"; |
| 189 | case Await_kind: |
| 190 | return "await expression"; |
| 191 | case ListComp_kind: |
| 192 | return "list comprehension"; |
| 193 | case SetComp_kind: |
| 194 | return "set comprehension"; |
| 195 | case DictComp_kind: |
| 196 | return "dict comprehension"; |
| 197 | case Dict_kind: |
| 198 | return "dict display"; |
| 199 | case Set_kind: |
| 200 | return "set display"; |
| 201 | case JoinedStr_kind: |
| 202 | case FormattedValue_kind: |
| 203 | return "f-string expression"; |
| 204 | case Constant_kind: { |
| 205 | PyObject *value = e->v.Constant.value; |
| 206 | if (value == Py_None) { |
| 207 | return "None"; |
| 208 | } |
| 209 | if (value == Py_False) { |
| 210 | return "False"; |
| 211 | } |
| 212 | if (value == Py_True) { |
| 213 | return "True"; |
| 214 | } |
| 215 | if (value == Py_Ellipsis) { |
| 216 | return "Ellipsis"; |
| 217 | } |
| 218 | return "literal"; |
| 219 | } |
| 220 | case Compare_kind: |
| 221 | return "comparison"; |
| 222 | case IfExp_kind: |
| 223 | return "conditional expression"; |
| 224 | case NamedExpr_kind: |
| 225 | return "named expression"; |
| 226 | default: |
| 227 | PyErr_Format(PyExc_SystemError, |
| 228 | "unexpected expression in assignment %d (line %d)", |
| 229 | e->kind, e->lineno); |
| 230 | return NULL; |
| 231 | } |
| 232 | } |
| 233 | |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 234 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 235 | raise_decode_error(Parser *p) |
| 236 | { |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 237 | assert(PyErr_Occurred()); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 238 | const char *errtype = NULL; |
| 239 | if (PyErr_ExceptionMatches(PyExc_UnicodeError)) { |
| 240 | errtype = "unicode error"; |
| 241 | } |
| 242 | else if (PyErr_ExceptionMatches(PyExc_ValueError)) { |
| 243 | errtype = "value error"; |
| 244 | } |
| 245 | if (errtype) { |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 246 | PyObject *type; |
| 247 | PyObject *value; |
| 248 | PyObject *tback; |
| 249 | PyObject *errstr; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 250 | PyErr_Fetch(&type, &value, &tback); |
| 251 | errstr = PyObject_Str(value); |
| 252 | if (errstr) { |
| 253 | RAISE_SYNTAX_ERROR("(%s) %U", errtype, errstr); |
| 254 | Py_DECREF(errstr); |
| 255 | } |
| 256 | else { |
| 257 | PyErr_Clear(); |
| 258 | RAISE_SYNTAX_ERROR("(%s) unknown error", errtype); |
| 259 | } |
| 260 | Py_XDECREF(type); |
| 261 | Py_XDECREF(value); |
| 262 | Py_XDECREF(tback); |
| 263 | } |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 264 | |
| 265 | return -1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void |
| 269 | raise_tokenizer_init_error(PyObject *filename) |
| 270 | { |
| 271 | if (!(PyErr_ExceptionMatches(PyExc_LookupError) |
| 272 | || PyErr_ExceptionMatches(PyExc_ValueError) |
| 273 | || PyErr_ExceptionMatches(PyExc_UnicodeDecodeError))) { |
| 274 | return; |
| 275 | } |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 276 | PyObject *errstr = NULL; |
| 277 | PyObject *tuple = NULL; |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 278 | PyObject *type; |
| 279 | PyObject *value; |
| 280 | PyObject *tback; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 281 | PyErr_Fetch(&type, &value, &tback); |
| 282 | errstr = PyObject_Str(value); |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 283 | if (!errstr) { |
| 284 | goto error; |
| 285 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 286 | |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 287 | PyObject *tmp = Py_BuildValue("(OiiO)", filename, 0, -1, Py_None); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 288 | if (!tmp) { |
| 289 | goto error; |
| 290 | } |
| 291 | |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 292 | tuple = PyTuple_Pack(2, errstr, tmp); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 293 | Py_DECREF(tmp); |
| 294 | if (!value) { |
| 295 | goto error; |
| 296 | } |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 297 | PyErr_SetObject(PyExc_SyntaxError, tuple); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 298 | |
| 299 | error: |
| 300 | Py_XDECREF(type); |
| 301 | Py_XDECREF(value); |
| 302 | Py_XDECREF(tback); |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 303 | Py_XDECREF(errstr); |
| 304 | Py_XDECREF(tuple); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 307 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 308 | tokenizer_error(Parser *p) |
| 309 | { |
| 310 | if (PyErr_Occurred()) { |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | const char *msg = NULL; |
| 315 | PyObject* errtype = PyExc_SyntaxError; |
| 316 | switch (p->tok->done) { |
| 317 | case E_TOKEN: |
| 318 | msg = "invalid token"; |
| 319 | break; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 320 | case E_EOFS: |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 321 | RAISE_SYNTAX_ERROR("EOF while scanning triple-quoted string literal"); |
| 322 | return -1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 323 | case E_EOLS: |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 324 | RAISE_SYNTAX_ERROR("EOL while scanning string literal"); |
| 325 | return -1; |
Lysandros Nikolaou | d55133f | 2020-04-28 03:23:35 +0300 | [diff] [blame] | 326 | case E_EOF: |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 327 | RAISE_SYNTAX_ERROR("unexpected EOF while parsing"); |
| 328 | return -1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 329 | case E_DEDENT: |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 330 | RAISE_INDENTATION_ERROR("unindent does not match any outer indentation level"); |
| 331 | return -1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 332 | case E_INTR: |
| 333 | if (!PyErr_Occurred()) { |
| 334 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 335 | } |
| 336 | return -1; |
| 337 | case E_NOMEM: |
| 338 | PyErr_NoMemory(); |
| 339 | return -1; |
| 340 | case E_TABSPACE: |
| 341 | errtype = PyExc_TabError; |
| 342 | msg = "inconsistent use of tabs and spaces in indentation"; |
| 343 | break; |
| 344 | case E_TOODEEP: |
| 345 | errtype = PyExc_IndentationError; |
| 346 | msg = "too many levels of indentation"; |
| 347 | break; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 348 | case E_LINECONT: |
| 349 | msg = "unexpected character after line continuation character"; |
| 350 | break; |
| 351 | default: |
| 352 | msg = "unknown parsing error"; |
| 353 | } |
| 354 | |
| 355 | PyErr_Format(errtype, msg); |
| 356 | // There is no reliable column information for this error |
| 357 | PyErr_SyntaxLocationObject(p->tok->filename, p->tok->lineno, 0); |
| 358 | |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | void * |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 363 | _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...) |
| 364 | { |
| 365 | Token *t = p->known_err_token != NULL ? p->known_err_token : p->tokens[p->fill - 1]; |
Pablo Galindo | 51c5896 | 2020-06-16 16:49:43 +0100 | [diff] [blame] | 366 | Py_ssize_t col_offset; |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 367 | if (t->col_offset == -1) { |
| 368 | col_offset = Py_SAFE_DOWNCAST(p->tok->cur - p->tok->buf, |
| 369 | intptr_t, int); |
| 370 | } else { |
| 371 | col_offset = t->col_offset + 1; |
| 372 | } |
| 373 | |
| 374 | va_list va; |
| 375 | va_start(va, errmsg); |
| 376 | _PyPegen_raise_error_known_location(p, errtype, t->lineno, |
| 377 | col_offset, errmsg, va); |
| 378 | va_end(va); |
| 379 | |
| 380 | return NULL; |
| 381 | } |
| 382 | |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 383 | void * |
| 384 | _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype, |
Pablo Galindo | 51c5896 | 2020-06-16 16:49:43 +0100 | [diff] [blame] | 385 | Py_ssize_t lineno, Py_ssize_t col_offset, |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 386 | const char *errmsg, va_list va) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 387 | { |
| 388 | PyObject *value = NULL; |
| 389 | PyObject *errstr = NULL; |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 390 | PyObject *error_line = NULL; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 391 | PyObject *tmp = NULL; |
Lysandros Nikolaou | 7f06af6 | 2020-05-04 03:20:09 +0300 | [diff] [blame] | 392 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 393 | |
Lysandros Nikolaou | 2e0a920 | 2020-06-26 14:24:05 +0300 | [diff] [blame] | 394 | if (p->start_rule == Py_fstring_input) { |
| 395 | const char *fstring_msg = "f-string: "; |
| 396 | Py_ssize_t len = strlen(fstring_msg) + strlen(errmsg); |
| 397 | |
Lysandros Nikolaou | 6dcbc24 | 2020-06-27 20:47:00 +0300 | [diff] [blame] | 398 | char *new_errmsg = PyMem_Malloc(len + 1); // Lengths of both strings plus NULL character |
Lysandros Nikolaou | 2e0a920 | 2020-06-26 14:24:05 +0300 | [diff] [blame] | 399 | if (!new_errmsg) { |
| 400 | return (void *) PyErr_NoMemory(); |
| 401 | } |
| 402 | |
| 403 | // Copy both strings into new buffer |
| 404 | memcpy(new_errmsg, fstring_msg, strlen(fstring_msg)); |
| 405 | memcpy(new_errmsg + strlen(fstring_msg), errmsg, strlen(errmsg)); |
| 406 | new_errmsg[len] = 0; |
| 407 | errmsg = new_errmsg; |
| 408 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 409 | errstr = PyUnicode_FromFormatV(errmsg, va); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 410 | if (!errstr) { |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | if (p->start_rule == Py_file_input) { |
Lysandros Nikolaou | 861efc6 | 2020-06-20 15:57:27 +0300 | [diff] [blame] | 415 | error_line = PyErr_ProgramTextObject(p->tok->filename, (int) lineno); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 418 | if (!error_line) { |
Pablo Galindo | bcc3036 | 2020-05-14 21:11:48 +0100 | [diff] [blame] | 419 | Py_ssize_t size = p->tok->inp - p->tok->buf; |
Pablo Galindo | bcc3036 | 2020-05-14 21:11:48 +0100 | [diff] [blame] | 420 | error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace"); |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 421 | if (!error_line) { |
| 422 | goto error; |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 423 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Lysandros Nikolaou | 1f0f4ab | 2020-06-28 02:41:48 +0300 | [diff] [blame] | 426 | if (p->start_rule == Py_fstring_input) { |
| 427 | col_offset -= p->starting_col_offset; |
| 428 | } |
Pablo Galindo | 51c5896 | 2020-06-16 16:49:43 +0100 | [diff] [blame] | 429 | Py_ssize_t col_number = col_offset; |
| 430 | |
| 431 | if (p->tok->encoding != NULL) { |
| 432 | col_number = byte_offset_to_character_offset(error_line, col_offset); |
| 433 | } |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 434 | |
| 435 | tmp = Py_BuildValue("(OiiN)", p->tok->filename, lineno, col_number, error_line); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 436 | if (!tmp) { |
| 437 | goto error; |
| 438 | } |
| 439 | value = PyTuple_Pack(2, errstr, tmp); |
| 440 | Py_DECREF(tmp); |
| 441 | if (!value) { |
| 442 | goto error; |
| 443 | } |
| 444 | PyErr_SetObject(errtype, value); |
| 445 | |
| 446 | Py_DECREF(errstr); |
| 447 | Py_DECREF(value); |
Lysandros Nikolaou | 2e0a920 | 2020-06-26 14:24:05 +0300 | [diff] [blame] | 448 | if (p->start_rule == Py_fstring_input) { |
Lysandros Nikolaou | 6dcbc24 | 2020-06-27 20:47:00 +0300 | [diff] [blame] | 449 | PyMem_Free((void *)errmsg); |
Lysandros Nikolaou | 2e0a920 | 2020-06-26 14:24:05 +0300 | [diff] [blame] | 450 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 451 | return NULL; |
| 452 | |
| 453 | error: |
| 454 | Py_XDECREF(errstr); |
Lysandros Nikolaou | a15c9b3 | 2020-05-13 22:36:27 +0300 | [diff] [blame] | 455 | Py_XDECREF(error_line); |
Lysandros Nikolaou | 2e0a920 | 2020-06-26 14:24:05 +0300 | [diff] [blame] | 456 | if (p->start_rule == Py_fstring_input) { |
Lysandros Nikolaou | 6dcbc24 | 2020-06-27 20:47:00 +0300 | [diff] [blame] | 457 | PyMem_Free((void *)errmsg); |
Lysandros Nikolaou | 2e0a920 | 2020-06-26 14:24:05 +0300 | [diff] [blame] | 458 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 459 | return NULL; |
| 460 | } |
| 461 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 462 | #if 0 |
| 463 | static const char * |
| 464 | token_name(int type) |
| 465 | { |
| 466 | if (0 <= type && type <= N_TOKENS) { |
| 467 | return _PyParser_TokenNames[type]; |
| 468 | } |
| 469 | return "<Huh?>"; |
| 470 | } |
| 471 | #endif |
| 472 | |
| 473 | // Here, mark is the start of the node, while p->mark is the end. |
| 474 | // If node==NULL, they should be the same. |
| 475 | int |
| 476 | _PyPegen_insert_memo(Parser *p, int mark, int type, void *node) |
| 477 | { |
| 478 | // Insert in front |
| 479 | Memo *m = PyArena_Malloc(p->arena, sizeof(Memo)); |
| 480 | if (m == NULL) { |
| 481 | return -1; |
| 482 | } |
| 483 | m->type = type; |
| 484 | m->node = node; |
| 485 | m->mark = p->mark; |
| 486 | m->next = p->tokens[mark]->memo; |
| 487 | p->tokens[mark]->memo = m; |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | // Like _PyPegen_insert_memo(), but updates an existing node if found. |
| 492 | int |
| 493 | _PyPegen_update_memo(Parser *p, int mark, int type, void *node) |
| 494 | { |
| 495 | for (Memo *m = p->tokens[mark]->memo; m != NULL; m = m->next) { |
| 496 | if (m->type == type) { |
| 497 | // Update existing node. |
| 498 | m->node = node; |
| 499 | m->mark = p->mark; |
| 500 | return 0; |
| 501 | } |
| 502 | } |
| 503 | // Insert new node. |
| 504 | return _PyPegen_insert_memo(p, mark, type, node); |
| 505 | } |
| 506 | |
| 507 | // Return dummy NAME. |
| 508 | void * |
| 509 | _PyPegen_dummy_name(Parser *p, ...) |
| 510 | { |
| 511 | static void *cache = NULL; |
| 512 | |
| 513 | if (cache != NULL) { |
| 514 | return cache; |
| 515 | } |
| 516 | |
| 517 | PyObject *id = _create_dummy_identifier(p); |
| 518 | if (!id) { |
| 519 | return NULL; |
| 520 | } |
| 521 | cache = Name(id, Load, 1, 0, 1, 0, p->arena); |
| 522 | return cache; |
| 523 | } |
| 524 | |
| 525 | static int |
| 526 | _get_keyword_or_name_type(Parser *p, const char *name, int name_len) |
| 527 | { |
Lysandros Nikolaou | 782f44b | 2020-07-07 01:42:21 +0300 | [diff] [blame] | 528 | assert(name_len > 0); |
Pablo Galindo | 1ac0cbc | 2020-07-06 20:31:16 +0100 | [diff] [blame] | 529 | if (name_len >= p->n_keyword_lists || |
| 530 | p->keywords[name_len] == NULL || |
| 531 | p->keywords[name_len]->type == -1) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 532 | return NAME; |
| 533 | } |
Pablo Galindo | 1ac0cbc | 2020-07-06 20:31:16 +0100 | [diff] [blame] | 534 | for (KeywordToken *k = p->keywords[name_len]; k != NULL && k->type != -1; k++) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 535 | if (strncmp(k->str, name, name_len) == 0) { |
| 536 | return k->type; |
| 537 | } |
| 538 | } |
| 539 | return NAME; |
| 540 | } |
| 541 | |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 542 | static int |
| 543 | growable_comment_array_init(growable_comment_array *arr, size_t initial_size) { |
| 544 | assert(initial_size > 0); |
| 545 | arr->items = PyMem_Malloc(initial_size * sizeof(*arr->items)); |
| 546 | arr->size = initial_size; |
| 547 | arr->num_items = 0; |
| 548 | |
| 549 | return arr->items != NULL; |
| 550 | } |
| 551 | |
| 552 | static int |
| 553 | growable_comment_array_add(growable_comment_array *arr, int lineno, char *comment) { |
| 554 | if (arr->num_items >= arr->size) { |
| 555 | size_t new_size = arr->size * 2; |
| 556 | void *new_items_array = PyMem_Realloc(arr->items, new_size * sizeof(*arr->items)); |
| 557 | if (!new_items_array) { |
| 558 | return 0; |
| 559 | } |
| 560 | arr->items = new_items_array; |
| 561 | arr->size = new_size; |
| 562 | } |
| 563 | |
| 564 | arr->items[arr->num_items].lineno = lineno; |
| 565 | arr->items[arr->num_items].comment = comment; // Take ownership |
| 566 | arr->num_items++; |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | static void |
| 571 | growable_comment_array_deallocate(growable_comment_array *arr) { |
| 572 | for (unsigned i = 0; i < arr->num_items; i++) { |
| 573 | PyMem_Free(arr->items[i].comment); |
| 574 | } |
| 575 | PyMem_Free(arr->items); |
| 576 | } |
| 577 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 578 | int |
| 579 | _PyPegen_fill_token(Parser *p) |
| 580 | { |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 581 | const char *start; |
| 582 | const char *end; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 583 | int type = PyTokenizer_Get(p->tok, &start, &end); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 584 | |
| 585 | // Record and skip '# type: ignore' comments |
| 586 | while (type == TYPE_IGNORE) { |
| 587 | Py_ssize_t len = end - start; |
| 588 | char *tag = PyMem_Malloc(len + 1); |
| 589 | if (tag == NULL) { |
| 590 | PyErr_NoMemory(); |
| 591 | return -1; |
| 592 | } |
| 593 | strncpy(tag, start, len); |
| 594 | tag[len] = '\0'; |
| 595 | // Ownership of tag passes to the growable array |
| 596 | if (!growable_comment_array_add(&p->type_ignore_comments, p->tok->lineno, tag)) { |
| 597 | PyErr_NoMemory(); |
| 598 | return -1; |
| 599 | } |
| 600 | type = PyTokenizer_Get(p->tok, &start, &end); |
| 601 | } |
| 602 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 603 | if (type == ENDMARKER && p->start_rule == Py_single_input && p->parsing_started) { |
| 604 | type = NEWLINE; /* Add an extra newline */ |
| 605 | p->parsing_started = 0; |
| 606 | |
Pablo Galindo | b94dbd7 | 2020-04-27 18:35:58 +0100 | [diff] [blame] | 607 | if (p->tok->indent && !(p->flags & PyPARSE_DONT_IMPLY_DEDENT)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 608 | p->tok->pendin = -p->tok->indent; |
| 609 | p->tok->indent = 0; |
| 610 | } |
| 611 | } |
| 612 | else { |
| 613 | p->parsing_started = 1; |
| 614 | } |
| 615 | |
| 616 | if (p->fill == p->size) { |
| 617 | int newsize = p->size * 2; |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 618 | Token **new_tokens = PyMem_Realloc(p->tokens, newsize * sizeof(Token *)); |
| 619 | if (new_tokens == NULL) { |
| 620 | PyErr_NoMemory(); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 621 | return -1; |
| 622 | } |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 623 | p->tokens = new_tokens; |
| 624 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 625 | for (int i = p->size; i < newsize; i++) { |
| 626 | p->tokens[i] = PyMem_Malloc(sizeof(Token)); |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 627 | if (p->tokens[i] == NULL) { |
| 628 | p->size = i; // Needed, in order to cleanup correctly after parser fails |
| 629 | PyErr_NoMemory(); |
| 630 | return -1; |
| 631 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 632 | memset(p->tokens[i], '\0', sizeof(Token)); |
| 633 | } |
| 634 | p->size = newsize; |
| 635 | } |
| 636 | |
| 637 | Token *t = p->tokens[p->fill]; |
| 638 | t->type = (type == NAME) ? _get_keyword_or_name_type(p, start, (int)(end - start)) : type; |
| 639 | t->bytes = PyBytes_FromStringAndSize(start, end - start); |
| 640 | if (t->bytes == NULL) { |
| 641 | return -1; |
| 642 | } |
| 643 | PyArena_AddPyObject(p->arena, t->bytes); |
| 644 | |
| 645 | int lineno = type == STRING ? p->tok->first_lineno : p->tok->lineno; |
| 646 | const char *line_start = type == STRING ? p->tok->multi_line_start : p->tok->line_start; |
Pablo Galindo | 2208134 | 2020-04-29 02:04:06 +0100 | [diff] [blame] | 647 | int end_lineno = p->tok->lineno; |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 648 | int col_offset = -1; |
| 649 | int end_col_offset = -1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 650 | if (start != NULL && start >= line_start) { |
Pablo Galindo | 2208134 | 2020-04-29 02:04:06 +0100 | [diff] [blame] | 651 | col_offset = (int)(start - line_start); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 652 | } |
| 653 | if (end != NULL && end >= p->tok->line_start) { |
Pablo Galindo | 2208134 | 2020-04-29 02:04:06 +0100 | [diff] [blame] | 654 | end_col_offset = (int)(end - p->tok->line_start); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | t->lineno = p->starting_lineno + lineno; |
| 658 | t->col_offset = p->tok->lineno == 1 ? p->starting_col_offset + col_offset : col_offset; |
| 659 | t->end_lineno = p->starting_lineno + end_lineno; |
| 660 | t->end_col_offset = p->tok->lineno == 1 ? p->starting_col_offset + end_col_offset : end_col_offset; |
| 661 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 662 | p->fill += 1; |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 663 | |
| 664 | if (type == ERRORTOKEN) { |
| 665 | if (p->tok->done == E_DECODE) { |
| 666 | return raise_decode_error(p); |
| 667 | } |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 668 | return tokenizer_error(p); |
| 669 | |
Batuhan Taskaya | 76c1b4d | 2020-05-01 16:13:43 +0300 | [diff] [blame] | 670 | } |
| 671 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | // Instrumentation to count the effectiveness of memoization. |
| 676 | // The array counts the number of tokens skipped by memoization, |
| 677 | // indexed by type. |
| 678 | |
| 679 | #define NSTATISTICS 2000 |
| 680 | static long memo_statistics[NSTATISTICS]; |
| 681 | |
| 682 | void |
| 683 | _PyPegen_clear_memo_statistics() |
| 684 | { |
| 685 | for (int i = 0; i < NSTATISTICS; i++) { |
| 686 | memo_statistics[i] = 0; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | PyObject * |
| 691 | _PyPegen_get_memo_statistics() |
| 692 | { |
| 693 | PyObject *ret = PyList_New(NSTATISTICS); |
| 694 | if (ret == NULL) { |
| 695 | return NULL; |
| 696 | } |
| 697 | for (int i = 0; i < NSTATISTICS; i++) { |
| 698 | PyObject *value = PyLong_FromLong(memo_statistics[i]); |
| 699 | if (value == NULL) { |
| 700 | Py_DECREF(ret); |
| 701 | return NULL; |
| 702 | } |
| 703 | // PyList_SetItem borrows a reference to value. |
| 704 | if (PyList_SetItem(ret, i, value) < 0) { |
| 705 | Py_DECREF(ret); |
| 706 | return NULL; |
| 707 | } |
| 708 | } |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | int // bool |
| 713 | _PyPegen_is_memoized(Parser *p, int type, void *pres) |
| 714 | { |
| 715 | if (p->mark == p->fill) { |
| 716 | if (_PyPegen_fill_token(p) < 0) { |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 717 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 718 | return -1; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | Token *t = p->tokens[p->mark]; |
| 723 | |
| 724 | for (Memo *m = t->memo; m != NULL; m = m->next) { |
| 725 | if (m->type == type) { |
| 726 | if (0 <= type && type < NSTATISTICS) { |
| 727 | long count = m->mark - p->mark; |
| 728 | // A memoized negative result counts for one. |
| 729 | if (count <= 0) { |
| 730 | count = 1; |
| 731 | } |
| 732 | memo_statistics[type] += count; |
| 733 | } |
| 734 | p->mark = m->mark; |
| 735 | *(void **)(pres) = m->node; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 736 | return 1; |
| 737 | } |
| 738 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
Pablo Galindo | 1df5a9e | 2020-04-23 12:42:13 +0100 | [diff] [blame] | 742 | |
| 743 | int |
| 744 | _PyPegen_lookahead_with_name(int positive, expr_ty (func)(Parser *), Parser *p) |
| 745 | { |
| 746 | int mark = p->mark; |
| 747 | void *res = func(p); |
| 748 | p->mark = mark; |
| 749 | return (res != NULL) == positive; |
| 750 | } |
| 751 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 752 | int |
Pablo Galindo | 404b23b | 2020-05-27 00:15:52 +0100 | [diff] [blame] | 753 | _PyPegen_lookahead_with_string(int positive, expr_ty (func)(Parser *, const char*), Parser *p, const char* arg) |
| 754 | { |
| 755 | int mark = p->mark; |
| 756 | void *res = func(p, arg); |
| 757 | p->mark = mark; |
| 758 | return (res != NULL) == positive; |
| 759 | } |
| 760 | |
| 761 | int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 762 | _PyPegen_lookahead_with_int(int positive, Token *(func)(Parser *, int), Parser *p, int arg) |
| 763 | { |
| 764 | int mark = p->mark; |
| 765 | void *res = func(p, arg); |
| 766 | p->mark = mark; |
| 767 | return (res != NULL) == positive; |
| 768 | } |
| 769 | |
| 770 | int |
| 771 | _PyPegen_lookahead(int positive, void *(func)(Parser *), Parser *p) |
| 772 | { |
| 773 | int mark = p->mark; |
Pablo Galindo | 1df5a9e | 2020-04-23 12:42:13 +0100 | [diff] [blame] | 774 | void *res = (void*)func(p); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 775 | p->mark = mark; |
| 776 | return (res != NULL) == positive; |
| 777 | } |
| 778 | |
| 779 | Token * |
| 780 | _PyPegen_expect_token(Parser *p, int type) |
| 781 | { |
| 782 | if (p->mark == p->fill) { |
| 783 | if (_PyPegen_fill_token(p) < 0) { |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 784 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 785 | return NULL; |
| 786 | } |
| 787 | } |
| 788 | Token *t = p->tokens[p->mark]; |
| 789 | if (t->type != type) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 790 | return NULL; |
| 791 | } |
| 792 | p->mark += 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 793 | return t; |
| 794 | } |
| 795 | |
Guido van Rossum | b45af1a | 2020-05-26 10:58:44 -0700 | [diff] [blame] | 796 | expr_ty |
| 797 | _PyPegen_expect_soft_keyword(Parser *p, const char *keyword) |
| 798 | { |
| 799 | if (p->mark == p->fill) { |
| 800 | if (_PyPegen_fill_token(p) < 0) { |
| 801 | p->error_indicator = 1; |
| 802 | return NULL; |
| 803 | } |
| 804 | } |
| 805 | Token *t = p->tokens[p->mark]; |
| 806 | if (t->type != NAME) { |
| 807 | return NULL; |
| 808 | } |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 809 | char *s = PyBytes_AsString(t->bytes); |
Guido van Rossum | b45af1a | 2020-05-26 10:58:44 -0700 | [diff] [blame] | 810 | if (!s) { |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 811 | p->error_indicator = 1; |
Guido van Rossum | b45af1a | 2020-05-26 10:58:44 -0700 | [diff] [blame] | 812 | return NULL; |
| 813 | } |
| 814 | if (strcmp(s, keyword) != 0) { |
| 815 | return NULL; |
| 816 | } |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 817 | return _PyPegen_name_token(p); |
Guido van Rossum | b45af1a | 2020-05-26 10:58:44 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 820 | Token * |
| 821 | _PyPegen_get_last_nonnwhitespace_token(Parser *p) |
| 822 | { |
| 823 | assert(p->mark >= 0); |
| 824 | Token *token = NULL; |
| 825 | for (int m = p->mark - 1; m >= 0; m--) { |
| 826 | token = p->tokens[m]; |
| 827 | if (token->type != ENDMARKER && (token->type < NEWLINE || token->type > DEDENT)) { |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | return token; |
| 832 | } |
| 833 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 834 | expr_ty |
| 835 | _PyPegen_name_token(Parser *p) |
| 836 | { |
| 837 | Token *t = _PyPegen_expect_token(p, NAME); |
| 838 | if (t == NULL) { |
| 839 | return NULL; |
| 840 | } |
| 841 | char* s = PyBytes_AsString(t->bytes); |
| 842 | if (!s) { |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 843 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 844 | return NULL; |
| 845 | } |
| 846 | PyObject *id = _PyPegen_new_identifier(p, s); |
| 847 | if (id == NULL) { |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 848 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 849 | return NULL; |
| 850 | } |
| 851 | return Name(id, Load, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset, |
| 852 | p->arena); |
| 853 | } |
| 854 | |
| 855 | void * |
| 856 | _PyPegen_string_token(Parser *p) |
| 857 | { |
| 858 | return _PyPegen_expect_token(p, STRING); |
| 859 | } |
| 860 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 861 | static PyObject * |
| 862 | parsenumber_raw(const char *s) |
| 863 | { |
| 864 | const char *end; |
| 865 | long x; |
| 866 | double dx; |
| 867 | Py_complex compl; |
| 868 | int imflag; |
| 869 | |
| 870 | assert(s != NULL); |
| 871 | errno = 0; |
| 872 | end = s + strlen(s) - 1; |
| 873 | imflag = *end == 'j' || *end == 'J'; |
| 874 | if (s[0] == '0') { |
| 875 | x = (long)PyOS_strtoul(s, (char **)&end, 0); |
| 876 | if (x < 0 && errno == 0) { |
| 877 | return PyLong_FromString(s, (char **)0, 0); |
| 878 | } |
| 879 | } |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 880 | else { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 881 | x = PyOS_strtol(s, (char **)&end, 0); |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 882 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 883 | if (*end == '\0') { |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 884 | if (errno != 0) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 885 | return PyLong_FromString(s, (char **)0, 0); |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 886 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 887 | return PyLong_FromLong(x); |
| 888 | } |
| 889 | /* XXX Huge floats may silently fail */ |
| 890 | if (imflag) { |
| 891 | compl.real = 0.; |
| 892 | compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 893 | if (compl.imag == -1.0 && PyErr_Occurred()) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 894 | return NULL; |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 895 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 896 | return PyComplex_FromCComplex(compl); |
| 897 | } |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 898 | dx = PyOS_string_to_double(s, NULL, NULL); |
| 899 | if (dx == -1.0 && PyErr_Occurred()) { |
| 900 | return NULL; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 901 | } |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 902 | return PyFloat_FromDouble(dx); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static PyObject * |
| 906 | parsenumber(const char *s) |
| 907 | { |
Pablo Galindo | fb61c42 | 2020-06-15 14:23:43 +0100 | [diff] [blame] | 908 | char *dup; |
| 909 | char *end; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 910 | PyObject *res = NULL; |
| 911 | |
| 912 | assert(s != NULL); |
| 913 | |
| 914 | if (strchr(s, '_') == NULL) { |
| 915 | return parsenumber_raw(s); |
| 916 | } |
| 917 | /* Create a duplicate without underscores. */ |
| 918 | dup = PyMem_Malloc(strlen(s) + 1); |
| 919 | if (dup == NULL) { |
| 920 | return PyErr_NoMemory(); |
| 921 | } |
| 922 | end = dup; |
| 923 | for (; *s; s++) { |
| 924 | if (*s != '_') { |
| 925 | *end++ = *s; |
| 926 | } |
| 927 | } |
| 928 | *end = '\0'; |
| 929 | res = parsenumber_raw(dup); |
| 930 | PyMem_Free(dup); |
| 931 | return res; |
| 932 | } |
| 933 | |
| 934 | expr_ty |
| 935 | _PyPegen_number_token(Parser *p) |
| 936 | { |
| 937 | Token *t = _PyPegen_expect_token(p, NUMBER); |
| 938 | if (t == NULL) { |
| 939 | return NULL; |
| 940 | } |
| 941 | |
| 942 | char *num_raw = PyBytes_AsString(t->bytes); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 943 | if (num_raw == NULL) { |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 944 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 945 | return NULL; |
| 946 | } |
| 947 | |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 948 | if (p->feature_version < 6 && strchr(num_raw, '_') != NULL) { |
| 949 | p->error_indicator = 1; |
Shantanu | c3f0014 | 2020-05-04 01:13:30 -0700 | [diff] [blame] | 950 | return RAISE_SYNTAX_ERROR("Underscores in numeric literals are only supported " |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 951 | "in Python 3.6 and greater"); |
| 952 | } |
| 953 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 954 | PyObject *c = parsenumber(num_raw); |
| 955 | |
| 956 | if (c == NULL) { |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 957 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | if (PyArena_AddPyObject(p->arena, c) < 0) { |
| 962 | Py_DECREF(c); |
Lysandros Nikolaou | 526e23f | 2020-05-27 19:04:11 +0300 | [diff] [blame] | 963 | p->error_indicator = 1; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 964 | return NULL; |
| 965 | } |
| 966 | |
| 967 | return Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset, |
| 968 | p->arena); |
| 969 | } |
| 970 | |
Lysandros Nikolaou | 6d65087 | 2020-04-29 04:42:27 +0300 | [diff] [blame] | 971 | static int // bool |
| 972 | newline_in_string(Parser *p, const char *cur) |
| 973 | { |
Pablo Galindo | 2e6593d | 2020-06-06 00:52:27 +0100 | [diff] [blame] | 974 | for (const char *c = cur; c >= p->tok->buf; c--) { |
| 975 | if (*c == '\'' || *c == '"') { |
Lysandros Nikolaou | 6d65087 | 2020-04-29 04:42:27 +0300 | [diff] [blame] | 976 | return 1; |
| 977 | } |
| 978 | } |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | /* Check that the source for a single input statement really is a single |
| 983 | statement by looking at what is left in the buffer after parsing. |
| 984 | Trailing whitespace and comments are OK. */ |
| 985 | static int // bool |
| 986 | bad_single_statement(Parser *p) |
| 987 | { |
| 988 | const char *cur = strchr(p->tok->buf, '\n'); |
| 989 | |
| 990 | /* Newlines are allowed if preceded by a line continuation character |
| 991 | or if they appear inside a string. */ |
Pablo Galindo | e68c678 | 2020-10-25 23:03:41 +0000 | [diff] [blame] | 992 | if (!cur || (cur != p->tok->buf && *(cur - 1) == '\\') |
| 993 | || newline_in_string(p, cur)) { |
Lysandros Nikolaou | 6d65087 | 2020-04-29 04:42:27 +0300 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | char c = *cur; |
| 997 | |
| 998 | for (;;) { |
| 999 | while (c == ' ' || c == '\t' || c == '\n' || c == '\014') { |
| 1000 | c = *++cur; |
| 1001 | } |
| 1002 | |
| 1003 | if (!c) { |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | if (c != '#') { |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
| 1011 | /* Suck up comment. */ |
| 1012 | while (c && c != '\n') { |
| 1013 | c = *++cur; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1018 | void |
| 1019 | _PyPegen_Parser_Free(Parser *p) |
| 1020 | { |
| 1021 | Py_XDECREF(p->normalize); |
| 1022 | for (int i = 0; i < p->size; i++) { |
| 1023 | PyMem_Free(p->tokens[i]); |
| 1024 | } |
| 1025 | PyMem_Free(p->tokens); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1026 | growable_comment_array_deallocate(&p->type_ignore_comments); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1027 | PyMem_Free(p); |
| 1028 | } |
| 1029 | |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1030 | static int |
| 1031 | compute_parser_flags(PyCompilerFlags *flags) |
| 1032 | { |
| 1033 | int parser_flags = 0; |
| 1034 | if (!flags) { |
| 1035 | return 0; |
| 1036 | } |
| 1037 | if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT) { |
| 1038 | parser_flags |= PyPARSE_DONT_IMPLY_DEDENT; |
| 1039 | } |
| 1040 | if (flags->cf_flags & PyCF_IGNORE_COOKIE) { |
| 1041 | parser_flags |= PyPARSE_IGNORE_COOKIE; |
| 1042 | } |
| 1043 | if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL) { |
| 1044 | parser_flags |= PyPARSE_BARRY_AS_BDFL; |
| 1045 | } |
| 1046 | if (flags->cf_flags & PyCF_TYPE_COMMENTS) { |
| 1047 | parser_flags |= PyPARSE_TYPE_COMMENTS; |
| 1048 | } |
Guido van Rossum | 9d197c7 | 2020-06-27 17:33:49 -0700 | [diff] [blame] | 1049 | if ((flags->cf_flags & PyCF_ONLY_AST) && flags->cf_feature_version < 7) { |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 1050 | parser_flags |= PyPARSE_ASYNC_HACKS; |
| 1051 | } |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1052 | return parser_flags; |
| 1053 | } |
| 1054 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1055 | Parser * |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1056 | _PyPegen_Parser_New(struct tok_state *tok, int start_rule, int flags, |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 1057 | int feature_version, int *errcode, PyArena *arena) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1058 | { |
| 1059 | Parser *p = PyMem_Malloc(sizeof(Parser)); |
| 1060 | if (p == NULL) { |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 1061 | return (Parser *) PyErr_NoMemory(); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1062 | } |
| 1063 | assert(tok != NULL); |
Guido van Rossum | d9d6ead | 2020-05-01 09:42:32 -0700 | [diff] [blame] | 1064 | tok->type_comments = (flags & PyPARSE_TYPE_COMMENTS) > 0; |
| 1065 | tok->async_hacks = (flags & PyPARSE_ASYNC_HACKS) > 0; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1066 | p->tok = tok; |
| 1067 | p->keywords = NULL; |
| 1068 | p->n_keyword_lists = -1; |
| 1069 | p->tokens = PyMem_Malloc(sizeof(Token *)); |
| 1070 | if (!p->tokens) { |
| 1071 | PyMem_Free(p); |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 1072 | return (Parser *) PyErr_NoMemory(); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1073 | } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1074 | p->tokens[0] = PyMem_Calloc(1, sizeof(Token)); |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 1075 | if (!p->tokens) { |
| 1076 | PyMem_Free(p->tokens); |
| 1077 | PyMem_Free(p); |
| 1078 | return (Parser *) PyErr_NoMemory(); |
| 1079 | } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1080 | if (!growable_comment_array_init(&p->type_ignore_comments, 10)) { |
| 1081 | PyMem_Free(p->tokens[0]); |
| 1082 | PyMem_Free(p->tokens); |
| 1083 | PyMem_Free(p); |
| 1084 | return (Parser *) PyErr_NoMemory(); |
| 1085 | } |
| 1086 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1087 | p->mark = 0; |
| 1088 | p->fill = 0; |
| 1089 | p->size = 1; |
| 1090 | |
| 1091 | p->errcode = errcode; |
| 1092 | p->arena = arena; |
| 1093 | p->start_rule = start_rule; |
| 1094 | p->parsing_started = 0; |
| 1095 | p->normalize = NULL; |
| 1096 | p->error_indicator = 0; |
| 1097 | |
| 1098 | p->starting_lineno = 0; |
| 1099 | p->starting_col_offset = 0; |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1100 | p->flags = flags; |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 1101 | p->feature_version = feature_version; |
Lysandros Nikolaou | 2f37c35 | 2020-05-07 13:37:51 +0300 | [diff] [blame] | 1102 | p->known_err_token = NULL; |
Pablo Galindo | 800a35c6 | 2020-05-25 18:38:45 +0100 | [diff] [blame] | 1103 | p->level = 0; |
Lysandros Nikolaou | bca7014 | 2020-10-27 00:42:04 +0200 | [diff] [blame] | 1104 | p->call_invalid_rules = 0; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1105 | |
| 1106 | return p; |
| 1107 | } |
| 1108 | |
Lysandros Nikolaou | bca7014 | 2020-10-27 00:42:04 +0200 | [diff] [blame] | 1109 | static void |
| 1110 | reset_parser_state(Parser *p) |
| 1111 | { |
| 1112 | for (int i = 0; i < p->fill; i++) { |
| 1113 | p->tokens[i]->memo = NULL; |
| 1114 | } |
| 1115 | p->mark = 0; |
| 1116 | p->call_invalid_rules = 1; |
| 1117 | } |
| 1118 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1119 | void * |
| 1120 | _PyPegen_run_parser(Parser *p) |
| 1121 | { |
| 1122 | void *res = _PyPegen_parse(p); |
| 1123 | if (res == NULL) { |
Lysandros Nikolaou | bca7014 | 2020-10-27 00:42:04 +0200 | [diff] [blame] | 1124 | reset_parser_state(p); |
| 1125 | _PyPegen_parse(p); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1126 | if (PyErr_Occurred()) { |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | if (p->fill == 0) { |
| 1130 | RAISE_SYNTAX_ERROR("error at start before reading any input"); |
| 1131 | } |
| 1132 | else if (p->tok->done == E_EOF) { |
| 1133 | RAISE_SYNTAX_ERROR("unexpected EOF while parsing"); |
| 1134 | } |
| 1135 | else { |
| 1136 | if (p->tokens[p->fill-1]->type == INDENT) { |
| 1137 | RAISE_INDENTATION_ERROR("unexpected indent"); |
| 1138 | } |
| 1139 | else if (p->tokens[p->fill-1]->type == DEDENT) { |
| 1140 | RAISE_INDENTATION_ERROR("unexpected unindent"); |
| 1141 | } |
| 1142 | else { |
| 1143 | RAISE_SYNTAX_ERROR("invalid syntax"); |
| 1144 | } |
| 1145 | } |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | |
Lysandros Nikolaou | 6d65087 | 2020-04-29 04:42:27 +0300 | [diff] [blame] | 1149 | if (p->start_rule == Py_single_input && bad_single_statement(p)) { |
| 1150 | p->tok->done = E_BADSINGLE; // This is not necessary for now, but might be in the future |
| 1151 | return RAISE_SYNTAX_ERROR("multiple statements found while compiling a single statement"); |
| 1152 | } |
| 1153 | |
Pablo Galindo | 1332226 | 2020-07-27 23:46:59 +0100 | [diff] [blame] | 1154 | #if defined(Py_DEBUG) && defined(Py_BUILD_CORE) |
| 1155 | if (p->start_rule == Py_single_input || |
| 1156 | p->start_rule == Py_file_input || |
| 1157 | p->start_rule == Py_eval_input) |
| 1158 | { |
Batuhan Taskaya | 3af4b58 | 2020-10-30 14:48:41 +0300 | [diff] [blame] | 1159 | if (!PyAST_Validate(res)) { |
| 1160 | return NULL; |
| 1161 | } |
Pablo Galindo | 1332226 | 2020-07-27 23:46:59 +0100 | [diff] [blame] | 1162 | } |
| 1163 | #endif |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1164 | return res; |
| 1165 | } |
| 1166 | |
| 1167 | mod_ty |
| 1168 | _PyPegen_run_parser_from_file_pointer(FILE *fp, int start_rule, PyObject *filename_ob, |
| 1169 | const char *enc, const char *ps1, const char *ps2, |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1170 | PyCompilerFlags *flags, int *errcode, PyArena *arena) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1171 | { |
| 1172 | struct tok_state *tok = PyTokenizer_FromFile(fp, enc, ps1, ps2); |
| 1173 | if (tok == NULL) { |
| 1174 | if (PyErr_Occurred()) { |
| 1175 | raise_tokenizer_init_error(filename_ob); |
| 1176 | return NULL; |
| 1177 | } |
| 1178 | return NULL; |
| 1179 | } |
| 1180 | // This transfers the ownership to the tokenizer |
| 1181 | tok->filename = filename_ob; |
| 1182 | Py_INCREF(filename_ob); |
| 1183 | |
| 1184 | // From here on we need to clean up even if there's an error |
| 1185 | mod_ty result = NULL; |
| 1186 | |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1187 | int parser_flags = compute_parser_flags(flags); |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 1188 | Parser *p = _PyPegen_Parser_New(tok, start_rule, parser_flags, PY_MINOR_VERSION, |
| 1189 | errcode, arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1190 | if (p == NULL) { |
| 1191 | goto error; |
| 1192 | } |
| 1193 | |
| 1194 | result = _PyPegen_run_parser(p); |
| 1195 | _PyPegen_Parser_Free(p); |
| 1196 | |
| 1197 | error: |
| 1198 | PyTokenizer_Free(tok); |
| 1199 | return result; |
| 1200 | } |
| 1201 | |
| 1202 | mod_ty |
| 1203 | _PyPegen_run_parser_from_file(const char *filename, int start_rule, |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1204 | PyObject *filename_ob, PyCompilerFlags *flags, PyArena *arena) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1205 | { |
| 1206 | FILE *fp = fopen(filename, "rb"); |
| 1207 | if (fp == NULL) { |
| 1208 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | |
| 1212 | mod_ty result = _PyPegen_run_parser_from_file_pointer(fp, start_rule, filename_ob, |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1213 | NULL, NULL, NULL, flags, NULL, arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1214 | |
| 1215 | fclose(fp); |
| 1216 | return result; |
| 1217 | } |
| 1218 | |
| 1219 | mod_ty |
| 1220 | _PyPegen_run_parser_from_string(const char *str, int start_rule, PyObject *filename_ob, |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1221 | PyCompilerFlags *flags, PyArena *arena) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1222 | { |
| 1223 | int exec_input = start_rule == Py_file_input; |
| 1224 | |
| 1225 | struct tok_state *tok; |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1226 | if (flags == NULL || flags->cf_flags & PyCF_IGNORE_COOKIE) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1227 | tok = PyTokenizer_FromUTF8(str, exec_input); |
| 1228 | } else { |
| 1229 | tok = PyTokenizer_FromString(str, exec_input); |
| 1230 | } |
| 1231 | if (tok == NULL) { |
| 1232 | if (PyErr_Occurred()) { |
| 1233 | raise_tokenizer_init_error(filename_ob); |
| 1234 | } |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | // This transfers the ownership to the tokenizer |
| 1238 | tok->filename = filename_ob; |
| 1239 | Py_INCREF(filename_ob); |
| 1240 | |
| 1241 | // We need to clear up from here on |
| 1242 | mod_ty result = NULL; |
| 1243 | |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 1244 | int parser_flags = compute_parser_flags(flags); |
Guido van Rossum | 9d197c7 | 2020-06-27 17:33:49 -0700 | [diff] [blame] | 1245 | int feature_version = flags && (flags->cf_flags & PyCF_ONLY_AST) ? |
| 1246 | flags->cf_feature_version : PY_MINOR_VERSION; |
Lysandros Nikolaou | 3e0a6f3 | 2020-05-01 06:27:52 +0300 | [diff] [blame] | 1247 | Parser *p = _PyPegen_Parser_New(tok, start_rule, parser_flags, feature_version, |
| 1248 | NULL, arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1249 | if (p == NULL) { |
| 1250 | goto error; |
| 1251 | } |
| 1252 | |
| 1253 | result = _PyPegen_run_parser(p); |
| 1254 | _PyPegen_Parser_Free(p); |
| 1255 | |
| 1256 | error: |
| 1257 | PyTokenizer_Free(tok); |
| 1258 | return result; |
| 1259 | } |
| 1260 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1261 | asdl_stmt_seq* |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1262 | _PyPegen_interactive_exit(Parser *p) |
| 1263 | { |
| 1264 | if (p->errcode) { |
| 1265 | *(p->errcode) = E_EOF; |
| 1266 | } |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | |
| 1270 | /* Creates a single-element asdl_seq* that contains a */ |
| 1271 | asdl_seq * |
| 1272 | _PyPegen_singleton_seq(Parser *p, void *a) |
| 1273 | { |
| 1274 | assert(a != NULL); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1275 | asdl_seq *seq = (asdl_seq*)_Py_asdl_generic_seq_new(1, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1276 | if (!seq) { |
| 1277 | return NULL; |
| 1278 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1279 | asdl_seq_SET_UNTYPED(seq, 0, a); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1280 | return seq; |
| 1281 | } |
| 1282 | |
| 1283 | /* Creates a copy of seq and prepends a to it */ |
| 1284 | asdl_seq * |
| 1285 | _PyPegen_seq_insert_in_front(Parser *p, void *a, asdl_seq *seq) |
| 1286 | { |
| 1287 | assert(a != NULL); |
| 1288 | if (!seq) { |
| 1289 | return _PyPegen_singleton_seq(p, a); |
| 1290 | } |
| 1291 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1292 | asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1293 | if (!new_seq) { |
| 1294 | return NULL; |
| 1295 | } |
| 1296 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1297 | asdl_seq_SET_UNTYPED(new_seq, 0, a); |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1298 | for (Py_ssize_t i = 1, l = asdl_seq_LEN(new_seq); i < l; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1299 | asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i - 1)); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1300 | } |
| 1301 | return new_seq; |
| 1302 | } |
| 1303 | |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1304 | /* Creates a copy of seq and appends a to it */ |
| 1305 | asdl_seq * |
| 1306 | _PyPegen_seq_append_to_end(Parser *p, asdl_seq *seq, void *a) |
| 1307 | { |
| 1308 | assert(a != NULL); |
| 1309 | if (!seq) { |
| 1310 | return _PyPegen_singleton_seq(p, a); |
| 1311 | } |
| 1312 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1313 | asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(asdl_seq_LEN(seq) + 1, p->arena); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1314 | if (!new_seq) { |
| 1315 | return NULL; |
| 1316 | } |
| 1317 | |
| 1318 | for (Py_ssize_t i = 0, l = asdl_seq_LEN(new_seq); i + 1 < l; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1319 | asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq, i)); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1320 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1321 | asdl_seq_SET_UNTYPED(new_seq, asdl_seq_LEN(new_seq) - 1, a); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1322 | return new_seq; |
| 1323 | } |
| 1324 | |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1325 | static Py_ssize_t |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1326 | _get_flattened_seq_size(asdl_seq *seqs) |
| 1327 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1328 | Py_ssize_t size = 0; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1329 | for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1330 | asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1331 | size += asdl_seq_LEN(inner_seq); |
| 1332 | } |
| 1333 | return size; |
| 1334 | } |
| 1335 | |
| 1336 | /* Flattens an asdl_seq* of asdl_seq*s */ |
| 1337 | asdl_seq * |
| 1338 | _PyPegen_seq_flatten(Parser *p, asdl_seq *seqs) |
| 1339 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1340 | Py_ssize_t flattened_seq_size = _get_flattened_seq_size(seqs); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1341 | assert(flattened_seq_size > 0); |
| 1342 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1343 | asdl_seq *flattened_seq = (asdl_seq*)_Py_asdl_generic_seq_new(flattened_seq_size, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1344 | if (!flattened_seq) { |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | |
| 1348 | int flattened_seq_idx = 0; |
| 1349 | for (Py_ssize_t i = 0, l = asdl_seq_LEN(seqs); i < l; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1350 | asdl_seq *inner_seq = asdl_seq_GET_UNTYPED(seqs, i); |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1351 | for (Py_ssize_t j = 0, li = asdl_seq_LEN(inner_seq); j < li; j++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1352 | asdl_seq_SET_UNTYPED(flattened_seq, flattened_seq_idx++, asdl_seq_GET_UNTYPED(inner_seq, j)); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | assert(flattened_seq_idx == flattened_seq_size); |
| 1356 | |
| 1357 | return flattened_seq; |
| 1358 | } |
| 1359 | |
| 1360 | /* Creates a new name of the form <first_name>.<second_name> */ |
| 1361 | expr_ty |
| 1362 | _PyPegen_join_names_with_dot(Parser *p, expr_ty first_name, expr_ty second_name) |
| 1363 | { |
| 1364 | assert(first_name != NULL && second_name != NULL); |
| 1365 | PyObject *first_identifier = first_name->v.Name.id; |
| 1366 | PyObject *second_identifier = second_name->v.Name.id; |
| 1367 | |
| 1368 | if (PyUnicode_READY(first_identifier) == -1) { |
| 1369 | return NULL; |
| 1370 | } |
| 1371 | if (PyUnicode_READY(second_identifier) == -1) { |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | const char *first_str = PyUnicode_AsUTF8(first_identifier); |
| 1375 | if (!first_str) { |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | const char *second_str = PyUnicode_AsUTF8(second_identifier); |
| 1379 | if (!second_str) { |
| 1380 | return NULL; |
| 1381 | } |
Pablo Galindo | 9f27dd3 | 2020-04-24 01:13:33 +0100 | [diff] [blame] | 1382 | Py_ssize_t len = strlen(first_str) + strlen(second_str) + 1; // +1 for the dot |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1383 | |
| 1384 | PyObject *str = PyBytes_FromStringAndSize(NULL, len); |
| 1385 | if (!str) { |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
| 1389 | char *s = PyBytes_AS_STRING(str); |
| 1390 | if (!s) { |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | |
| 1394 | strcpy(s, first_str); |
| 1395 | s += strlen(first_str); |
| 1396 | *s++ = '.'; |
| 1397 | strcpy(s, second_str); |
| 1398 | s += strlen(second_str); |
| 1399 | *s = '\0'; |
| 1400 | |
| 1401 | PyObject *uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), PyBytes_GET_SIZE(str), NULL); |
| 1402 | Py_DECREF(str); |
| 1403 | if (!uni) { |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | PyUnicode_InternInPlace(&uni); |
| 1407 | if (PyArena_AddPyObject(p->arena, uni) < 0) { |
| 1408 | Py_DECREF(uni); |
| 1409 | return NULL; |
| 1410 | } |
| 1411 | |
| 1412 | return _Py_Name(uni, Load, EXTRA_EXPR(first_name, second_name)); |
| 1413 | } |
| 1414 | |
| 1415 | /* Counts the total number of dots in seq's tokens */ |
| 1416 | int |
| 1417 | _PyPegen_seq_count_dots(asdl_seq *seq) |
| 1418 | { |
| 1419 | int number_of_dots = 0; |
| 1420 | for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1421 | Token *current_expr = asdl_seq_GET_UNTYPED(seq, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1422 | switch (current_expr->type) { |
| 1423 | case ELLIPSIS: |
| 1424 | number_of_dots += 3; |
| 1425 | break; |
| 1426 | case DOT: |
| 1427 | number_of_dots += 1; |
| 1428 | break; |
| 1429 | default: |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 1430 | Py_UNREACHABLE(); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | return number_of_dots; |
| 1435 | } |
| 1436 | |
| 1437 | /* Creates an alias with '*' as the identifier name */ |
| 1438 | alias_ty |
| 1439 | _PyPegen_alias_for_star(Parser *p) |
| 1440 | { |
| 1441 | PyObject *str = PyUnicode_InternFromString("*"); |
| 1442 | if (!str) { |
| 1443 | return NULL; |
| 1444 | } |
| 1445 | if (PyArena_AddPyObject(p->arena, str) < 0) { |
| 1446 | Py_DECREF(str); |
| 1447 | return NULL; |
| 1448 | } |
| 1449 | return alias(str, NULL, p->arena); |
| 1450 | } |
| 1451 | |
| 1452 | /* Creates a new asdl_seq* with the identifiers of all the names in seq */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1453 | asdl_identifier_seq * |
| 1454 | _PyPegen_map_names_to_ids(Parser *p, asdl_expr_seq *seq) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1455 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1456 | Py_ssize_t len = asdl_seq_LEN(seq); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1457 | assert(len > 0); |
| 1458 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1459 | asdl_identifier_seq *new_seq = _Py_asdl_identifier_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1460 | if (!new_seq) { |
| 1461 | return NULL; |
| 1462 | } |
| 1463 | for (Py_ssize_t i = 0; i < len; i++) { |
| 1464 | expr_ty e = asdl_seq_GET(seq, i); |
| 1465 | asdl_seq_SET(new_seq, i, e->v.Name.id); |
| 1466 | } |
| 1467 | return new_seq; |
| 1468 | } |
| 1469 | |
| 1470 | /* Constructs a CmpopExprPair */ |
| 1471 | CmpopExprPair * |
| 1472 | _PyPegen_cmpop_expr_pair(Parser *p, cmpop_ty cmpop, expr_ty expr) |
| 1473 | { |
| 1474 | assert(expr != NULL); |
| 1475 | CmpopExprPair *a = PyArena_Malloc(p->arena, sizeof(CmpopExprPair)); |
| 1476 | if (!a) { |
| 1477 | return NULL; |
| 1478 | } |
| 1479 | a->cmpop = cmpop; |
| 1480 | a->expr = expr; |
| 1481 | return a; |
| 1482 | } |
| 1483 | |
| 1484 | asdl_int_seq * |
| 1485 | _PyPegen_get_cmpops(Parser *p, asdl_seq *seq) |
| 1486 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1487 | Py_ssize_t len = asdl_seq_LEN(seq); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1488 | assert(len > 0); |
| 1489 | |
| 1490 | asdl_int_seq *new_seq = _Py_asdl_int_seq_new(len, p->arena); |
| 1491 | if (!new_seq) { |
| 1492 | return NULL; |
| 1493 | } |
| 1494 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1495 | CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1496 | asdl_seq_SET(new_seq, i, pair->cmpop); |
| 1497 | } |
| 1498 | return new_seq; |
| 1499 | } |
| 1500 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1501 | asdl_expr_seq * |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1502 | _PyPegen_get_exprs(Parser *p, asdl_seq *seq) |
| 1503 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1504 | Py_ssize_t len = asdl_seq_LEN(seq); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1505 | assert(len > 0); |
| 1506 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1507 | asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1508 | if (!new_seq) { |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1512 | CmpopExprPair *pair = asdl_seq_GET_UNTYPED(seq, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1513 | asdl_seq_SET(new_seq, i, pair->expr); |
| 1514 | } |
| 1515 | return new_seq; |
| 1516 | } |
| 1517 | |
| 1518 | /* Creates an asdl_seq* where all the elements have been changed to have ctx as context */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1519 | static asdl_expr_seq * |
| 1520 | _set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1521 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1522 | Py_ssize_t len = asdl_seq_LEN(seq); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1523 | if (len == 0) { |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1527 | asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1528 | if (!new_seq) { |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | for (Py_ssize_t i = 0; i < len; i++) { |
| 1532 | expr_ty e = asdl_seq_GET(seq, i); |
| 1533 | asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx)); |
| 1534 | } |
| 1535 | return new_seq; |
| 1536 | } |
| 1537 | |
| 1538 | static expr_ty |
| 1539 | _set_name_context(Parser *p, expr_ty e, expr_context_ty ctx) |
| 1540 | { |
| 1541 | return _Py_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e)); |
| 1542 | } |
| 1543 | |
| 1544 | static expr_ty |
| 1545 | _set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx) |
| 1546 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1547 | return _Py_Tuple( |
| 1548 | _set_seq_context(p, e->v.Tuple.elts, ctx), |
| 1549 | ctx, |
| 1550 | EXTRA_EXPR(e, e)); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | static expr_ty |
| 1554 | _set_list_context(Parser *p, expr_ty e, expr_context_ty ctx) |
| 1555 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1556 | return _Py_List( |
| 1557 | _set_seq_context(p, e->v.List.elts, ctx), |
| 1558 | ctx, |
| 1559 | EXTRA_EXPR(e, e)); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | static expr_ty |
| 1563 | _set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx) |
| 1564 | { |
| 1565 | return _Py_Subscript(e->v.Subscript.value, e->v.Subscript.slice, ctx, EXTRA_EXPR(e, e)); |
| 1566 | } |
| 1567 | |
| 1568 | static expr_ty |
| 1569 | _set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx) |
| 1570 | { |
| 1571 | return _Py_Attribute(e->v.Attribute.value, e->v.Attribute.attr, ctx, EXTRA_EXPR(e, e)); |
| 1572 | } |
| 1573 | |
| 1574 | static expr_ty |
| 1575 | _set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx) |
| 1576 | { |
| 1577 | return _Py_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx), ctx, EXTRA_EXPR(e, e)); |
| 1578 | } |
| 1579 | |
| 1580 | /* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */ |
| 1581 | expr_ty |
| 1582 | _PyPegen_set_expr_context(Parser *p, expr_ty expr, expr_context_ty ctx) |
| 1583 | { |
| 1584 | assert(expr != NULL); |
| 1585 | |
| 1586 | expr_ty new = NULL; |
| 1587 | switch (expr->kind) { |
| 1588 | case Name_kind: |
| 1589 | new = _set_name_context(p, expr, ctx); |
| 1590 | break; |
| 1591 | case Tuple_kind: |
| 1592 | new = _set_tuple_context(p, expr, ctx); |
| 1593 | break; |
| 1594 | case List_kind: |
| 1595 | new = _set_list_context(p, expr, ctx); |
| 1596 | break; |
| 1597 | case Subscript_kind: |
| 1598 | new = _set_subscript_context(p, expr, ctx); |
| 1599 | break; |
| 1600 | case Attribute_kind: |
| 1601 | new = _set_attribute_context(p, expr, ctx); |
| 1602 | break; |
| 1603 | case Starred_kind: |
| 1604 | new = _set_starred_context(p, expr, ctx); |
| 1605 | break; |
| 1606 | default: |
| 1607 | new = expr; |
| 1608 | } |
| 1609 | return new; |
| 1610 | } |
| 1611 | |
| 1612 | /* Constructs a KeyValuePair that is used when parsing a dict's key value pairs */ |
| 1613 | KeyValuePair * |
| 1614 | _PyPegen_key_value_pair(Parser *p, expr_ty key, expr_ty value) |
| 1615 | { |
| 1616 | KeyValuePair *a = PyArena_Malloc(p->arena, sizeof(KeyValuePair)); |
| 1617 | if (!a) { |
| 1618 | return NULL; |
| 1619 | } |
| 1620 | a->key = key; |
| 1621 | a->value = value; |
| 1622 | return a; |
| 1623 | } |
| 1624 | |
| 1625 | /* Extracts all keys from an asdl_seq* of KeyValuePair*'s */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1626 | asdl_expr_seq * |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1627 | _PyPegen_get_keys(Parser *p, asdl_seq *seq) |
| 1628 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1629 | Py_ssize_t len = asdl_seq_LEN(seq); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1630 | asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1631 | if (!new_seq) { |
| 1632 | return NULL; |
| 1633 | } |
| 1634 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1635 | KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1636 | asdl_seq_SET(new_seq, i, pair->key); |
| 1637 | } |
| 1638 | return new_seq; |
| 1639 | } |
| 1640 | |
| 1641 | /* Extracts all values from an asdl_seq* of KeyValuePair*'s */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1642 | asdl_expr_seq * |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1643 | _PyPegen_get_values(Parser *p, asdl_seq *seq) |
| 1644 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1645 | Py_ssize_t len = asdl_seq_LEN(seq); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1646 | asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1647 | if (!new_seq) { |
| 1648 | return NULL; |
| 1649 | } |
| 1650 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1651 | KeyValuePair *pair = asdl_seq_GET_UNTYPED(seq, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1652 | asdl_seq_SET(new_seq, i, pair->value); |
| 1653 | } |
| 1654 | return new_seq; |
| 1655 | } |
| 1656 | |
| 1657 | /* Constructs a NameDefaultPair */ |
| 1658 | NameDefaultPair * |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1659 | _PyPegen_name_default_pair(Parser *p, arg_ty arg, expr_ty value, Token *tc) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1660 | { |
| 1661 | NameDefaultPair *a = PyArena_Malloc(p->arena, sizeof(NameDefaultPair)); |
| 1662 | if (!a) { |
| 1663 | return NULL; |
| 1664 | } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 1665 | a->arg = _PyPegen_add_type_comment_to_arg(p, arg, tc); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1666 | a->value = value; |
| 1667 | return a; |
| 1668 | } |
| 1669 | |
| 1670 | /* Constructs a SlashWithDefault */ |
| 1671 | SlashWithDefault * |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1672 | _PyPegen_slash_with_default(Parser *p, asdl_arg_seq *plain_names, asdl_seq *names_with_defaults) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1673 | { |
| 1674 | SlashWithDefault *a = PyArena_Malloc(p->arena, sizeof(SlashWithDefault)); |
| 1675 | if (!a) { |
| 1676 | return NULL; |
| 1677 | } |
| 1678 | a->plain_names = plain_names; |
| 1679 | a->names_with_defaults = names_with_defaults; |
| 1680 | return a; |
| 1681 | } |
| 1682 | |
| 1683 | /* Constructs a StarEtc */ |
| 1684 | StarEtc * |
| 1685 | _PyPegen_star_etc(Parser *p, arg_ty vararg, asdl_seq *kwonlyargs, arg_ty kwarg) |
| 1686 | { |
| 1687 | StarEtc *a = PyArena_Malloc(p->arena, sizeof(StarEtc)); |
| 1688 | if (!a) { |
| 1689 | return NULL; |
| 1690 | } |
| 1691 | a->vararg = vararg; |
| 1692 | a->kwonlyargs = kwonlyargs; |
| 1693 | a->kwarg = kwarg; |
| 1694 | return a; |
| 1695 | } |
| 1696 | |
| 1697 | asdl_seq * |
| 1698 | _PyPegen_join_sequences(Parser *p, asdl_seq *a, asdl_seq *b) |
| 1699 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1700 | Py_ssize_t first_len = asdl_seq_LEN(a); |
| 1701 | Py_ssize_t second_len = asdl_seq_LEN(b); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1702 | asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(first_len + second_len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1703 | if (!new_seq) { |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | |
| 1707 | int k = 0; |
| 1708 | for (Py_ssize_t i = 0; i < first_len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1709 | asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(a, i)); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1710 | } |
| 1711 | for (Py_ssize_t i = 0; i < second_len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1712 | asdl_seq_SET_UNTYPED(new_seq, k++, asdl_seq_GET_UNTYPED(b, i)); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | return new_seq; |
| 1716 | } |
| 1717 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1718 | static asdl_arg_seq* |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1719 | _get_names(Parser *p, asdl_seq *names_with_defaults) |
| 1720 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1721 | Py_ssize_t len = asdl_seq_LEN(names_with_defaults); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1722 | asdl_arg_seq *seq = _Py_asdl_arg_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1723 | if (!seq) { |
| 1724 | return NULL; |
| 1725 | } |
| 1726 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1727 | NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1728 | asdl_seq_SET(seq, i, pair->arg); |
| 1729 | } |
| 1730 | return seq; |
| 1731 | } |
| 1732 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1733 | static asdl_expr_seq * |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1734 | _get_defaults(Parser *p, asdl_seq *names_with_defaults) |
| 1735 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 1736 | Py_ssize_t len = asdl_seq_LEN(names_with_defaults); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1737 | asdl_expr_seq *seq = _Py_asdl_expr_seq_new(len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1738 | if (!seq) { |
| 1739 | return NULL; |
| 1740 | } |
| 1741 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1742 | NameDefaultPair *pair = asdl_seq_GET_UNTYPED(names_with_defaults, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1743 | asdl_seq_SET(seq, i, pair->value); |
| 1744 | } |
| 1745 | return seq; |
| 1746 | } |
| 1747 | |
| 1748 | /* Constructs an arguments_ty object out of all the parsed constructs in the parameters rule */ |
| 1749 | arguments_ty |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1750 | _PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default, |
| 1751 | SlashWithDefault *slash_with_default, asdl_arg_seq *plain_names, |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1752 | asdl_seq *names_with_default, StarEtc *star_etc) |
| 1753 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1754 | asdl_arg_seq *posonlyargs; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1755 | if (slash_without_default != NULL) { |
| 1756 | posonlyargs = slash_without_default; |
| 1757 | } |
| 1758 | else if (slash_with_default != NULL) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1759 | asdl_arg_seq *slash_with_default_names = |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1760 | _get_names(p, slash_with_default->names_with_defaults); |
| 1761 | if (!slash_with_default_names) { |
| 1762 | return NULL; |
| 1763 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1764 | posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences( |
| 1765 | p, |
| 1766 | (asdl_seq*)slash_with_default->plain_names, |
| 1767 | (asdl_seq*)slash_with_default_names); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1768 | if (!posonlyargs) { |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | } |
| 1772 | else { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1773 | posonlyargs = _Py_asdl_arg_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1774 | if (!posonlyargs) { |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | } |
| 1778 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1779 | asdl_arg_seq *posargs; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1780 | if (plain_names != NULL && names_with_default != NULL) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1781 | asdl_arg_seq *names_with_default_names = _get_names(p, names_with_default); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1782 | if (!names_with_default_names) { |
| 1783 | return NULL; |
| 1784 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1785 | posargs = (asdl_arg_seq*)_PyPegen_join_sequences( |
| 1786 | p, |
| 1787 | (asdl_seq*)plain_names, |
| 1788 | (asdl_seq*)names_with_default_names); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1789 | if (!posargs) { |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | } |
| 1793 | else if (plain_names == NULL && names_with_default != NULL) { |
| 1794 | posargs = _get_names(p, names_with_default); |
| 1795 | if (!posargs) { |
| 1796 | return NULL; |
| 1797 | } |
| 1798 | } |
| 1799 | else if (plain_names != NULL && names_with_default == NULL) { |
| 1800 | posargs = plain_names; |
| 1801 | } |
| 1802 | else { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1803 | posargs = _Py_asdl_arg_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1804 | if (!posargs) { |
| 1805 | return NULL; |
| 1806 | } |
| 1807 | } |
| 1808 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1809 | asdl_expr_seq *posdefaults; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1810 | if (slash_with_default != NULL && names_with_default != NULL) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1811 | asdl_expr_seq *slash_with_default_values = |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1812 | _get_defaults(p, slash_with_default->names_with_defaults); |
| 1813 | if (!slash_with_default_values) { |
| 1814 | return NULL; |
| 1815 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1816 | asdl_expr_seq *names_with_default_values = _get_defaults(p, names_with_default); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1817 | if (!names_with_default_values) { |
| 1818 | return NULL; |
| 1819 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1820 | posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences( |
| 1821 | p, |
| 1822 | (asdl_seq*)slash_with_default_values, |
| 1823 | (asdl_seq*)names_with_default_values); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1824 | if (!posdefaults) { |
| 1825 | return NULL; |
| 1826 | } |
| 1827 | } |
| 1828 | else if (slash_with_default == NULL && names_with_default != NULL) { |
| 1829 | posdefaults = _get_defaults(p, names_with_default); |
| 1830 | if (!posdefaults) { |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | } |
| 1834 | else if (slash_with_default != NULL && names_with_default == NULL) { |
| 1835 | posdefaults = _get_defaults(p, slash_with_default->names_with_defaults); |
| 1836 | if (!posdefaults) { |
| 1837 | return NULL; |
| 1838 | } |
| 1839 | } |
| 1840 | else { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1841 | posdefaults = _Py_asdl_expr_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1842 | if (!posdefaults) { |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | arg_ty vararg = NULL; |
| 1848 | if (star_etc != NULL && star_etc->vararg != NULL) { |
| 1849 | vararg = star_etc->vararg; |
| 1850 | } |
| 1851 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1852 | asdl_arg_seq *kwonlyargs; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1853 | if (star_etc != NULL && star_etc->kwonlyargs != NULL) { |
| 1854 | kwonlyargs = _get_names(p, star_etc->kwonlyargs); |
| 1855 | if (!kwonlyargs) { |
| 1856 | return NULL; |
| 1857 | } |
| 1858 | } |
| 1859 | else { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1860 | kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1861 | if (!kwonlyargs) { |
| 1862 | return NULL; |
| 1863 | } |
| 1864 | } |
| 1865 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1866 | asdl_expr_seq *kwdefaults; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1867 | if (star_etc != NULL && star_etc->kwonlyargs != NULL) { |
| 1868 | kwdefaults = _get_defaults(p, star_etc->kwonlyargs); |
| 1869 | if (!kwdefaults) { |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | } |
| 1873 | else { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1874 | kwdefaults = _Py_asdl_expr_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1875 | if (!kwdefaults) { |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | arg_ty kwarg = NULL; |
| 1881 | if (star_etc != NULL && star_etc->kwarg != NULL) { |
| 1882 | kwarg = star_etc->kwarg; |
| 1883 | } |
| 1884 | |
| 1885 | return _Py_arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, |
| 1886 | posdefaults, p->arena); |
| 1887 | } |
| 1888 | |
| 1889 | /* Constructs an empty arguments_ty object, that gets used when a function accepts no |
| 1890 | * arguments. */ |
| 1891 | arguments_ty |
| 1892 | _PyPegen_empty_arguments(Parser *p) |
| 1893 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1894 | asdl_arg_seq *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1895 | if (!posonlyargs) { |
| 1896 | return NULL; |
| 1897 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1898 | asdl_arg_seq *posargs = _Py_asdl_arg_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1899 | if (!posargs) { |
| 1900 | return NULL; |
| 1901 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1902 | asdl_expr_seq *posdefaults = _Py_asdl_expr_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1903 | if (!posdefaults) { |
| 1904 | return NULL; |
| 1905 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1906 | asdl_arg_seq *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1907 | if (!kwonlyargs) { |
| 1908 | return NULL; |
| 1909 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1910 | asdl_expr_seq *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1911 | if (!kwdefaults) { |
| 1912 | return NULL; |
| 1913 | } |
| 1914 | |
Batuhan Taskaya | 02a1603 | 2020-10-10 20:14:59 +0300 | [diff] [blame] | 1915 | return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, posdefaults, |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1916 | p->arena); |
| 1917 | } |
| 1918 | |
| 1919 | /* Encapsulates the value of an operator_ty into an AugOperator struct */ |
| 1920 | AugOperator * |
| 1921 | _PyPegen_augoperator(Parser *p, operator_ty kind) |
| 1922 | { |
| 1923 | AugOperator *a = PyArena_Malloc(p->arena, sizeof(AugOperator)); |
| 1924 | if (!a) { |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | a->kind = kind; |
| 1928 | return a; |
| 1929 | } |
| 1930 | |
| 1931 | /* Construct a FunctionDef equivalent to function_def, but with decorators */ |
| 1932 | stmt_ty |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1933 | _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty function_def) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1934 | { |
| 1935 | assert(function_def != NULL); |
| 1936 | if (function_def->kind == AsyncFunctionDef_kind) { |
| 1937 | return _Py_AsyncFunctionDef( |
| 1938 | function_def->v.FunctionDef.name, function_def->v.FunctionDef.args, |
| 1939 | function_def->v.FunctionDef.body, decorators, function_def->v.FunctionDef.returns, |
| 1940 | function_def->v.FunctionDef.type_comment, function_def->lineno, |
| 1941 | function_def->col_offset, function_def->end_lineno, function_def->end_col_offset, |
| 1942 | p->arena); |
| 1943 | } |
| 1944 | |
| 1945 | return _Py_FunctionDef(function_def->v.FunctionDef.name, function_def->v.FunctionDef.args, |
| 1946 | function_def->v.FunctionDef.body, decorators, |
| 1947 | function_def->v.FunctionDef.returns, |
| 1948 | function_def->v.FunctionDef.type_comment, function_def->lineno, |
| 1949 | function_def->col_offset, function_def->end_lineno, |
| 1950 | function_def->end_col_offset, p->arena); |
| 1951 | } |
| 1952 | |
| 1953 | /* Construct a ClassDef equivalent to class_def, but with decorators */ |
| 1954 | stmt_ty |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1955 | _PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty class_def) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1956 | { |
| 1957 | assert(class_def != NULL); |
| 1958 | return _Py_ClassDef(class_def->v.ClassDef.name, class_def->v.ClassDef.bases, |
| 1959 | class_def->v.ClassDef.keywords, class_def->v.ClassDef.body, decorators, |
| 1960 | class_def->lineno, class_def->col_offset, class_def->end_lineno, |
| 1961 | class_def->end_col_offset, p->arena); |
| 1962 | } |
| 1963 | |
| 1964 | /* Construct a KeywordOrStarred */ |
| 1965 | KeywordOrStarred * |
| 1966 | _PyPegen_keyword_or_starred(Parser *p, void *element, int is_keyword) |
| 1967 | { |
| 1968 | KeywordOrStarred *a = PyArena_Malloc(p->arena, sizeof(KeywordOrStarred)); |
| 1969 | if (!a) { |
| 1970 | return NULL; |
| 1971 | } |
| 1972 | a->element = element; |
| 1973 | a->is_keyword = is_keyword; |
| 1974 | return a; |
| 1975 | } |
| 1976 | |
| 1977 | /* Get the number of starred expressions in an asdl_seq* of KeywordOrStarred*s */ |
| 1978 | static int |
| 1979 | _seq_number_of_starred_exprs(asdl_seq *seq) |
| 1980 | { |
| 1981 | int n = 0; |
| 1982 | for (Py_ssize_t i = 0, l = asdl_seq_LEN(seq); i < l; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1983 | KeywordOrStarred *k = asdl_seq_GET_UNTYPED(seq, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1984 | if (!k->is_keyword) { |
| 1985 | n++; |
| 1986 | } |
| 1987 | } |
| 1988 | return n; |
| 1989 | } |
| 1990 | |
| 1991 | /* Extract the starred expressions of an asdl_seq* of KeywordOrStarred*s */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1992 | asdl_expr_seq * |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1993 | _PyPegen_seq_extract_starred_exprs(Parser *p, asdl_seq *kwargs) |
| 1994 | { |
| 1995 | int new_len = _seq_number_of_starred_exprs(kwargs); |
| 1996 | if (new_len == 0) { |
| 1997 | return NULL; |
| 1998 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1999 | asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(new_len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2000 | if (!new_seq) { |
| 2001 | return NULL; |
| 2002 | } |
| 2003 | |
| 2004 | int idx = 0; |
| 2005 | for (Py_ssize_t i = 0, len = asdl_seq_LEN(kwargs); i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2006 | KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2007 | if (!k->is_keyword) { |
| 2008 | asdl_seq_SET(new_seq, idx++, k->element); |
| 2009 | } |
| 2010 | } |
| 2011 | return new_seq; |
| 2012 | } |
| 2013 | |
| 2014 | /* Return a new asdl_seq* with only the keywords in kwargs */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2015 | asdl_keyword_seq* |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2016 | _PyPegen_seq_delete_starred_exprs(Parser *p, asdl_seq *kwargs) |
| 2017 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2018 | Py_ssize_t len = asdl_seq_LEN(kwargs); |
| 2019 | Py_ssize_t new_len = len - _seq_number_of_starred_exprs(kwargs); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2020 | if (new_len == 0) { |
| 2021 | return NULL; |
| 2022 | } |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2023 | asdl_keyword_seq *new_seq = _Py_asdl_keyword_seq_new(new_len, p->arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2024 | if (!new_seq) { |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | |
| 2028 | int idx = 0; |
| 2029 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2030 | KeywordOrStarred *k = asdl_seq_GET_UNTYPED(kwargs, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2031 | if (k->is_keyword) { |
| 2032 | asdl_seq_SET(new_seq, idx++, k->element); |
| 2033 | } |
| 2034 | } |
| 2035 | return new_seq; |
| 2036 | } |
| 2037 | |
| 2038 | expr_ty |
| 2039 | _PyPegen_concatenate_strings(Parser *p, asdl_seq *strings) |
| 2040 | { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2041 | Py_ssize_t len = asdl_seq_LEN(strings); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2042 | assert(len > 0); |
| 2043 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2044 | Token *first = asdl_seq_GET_UNTYPED(strings, 0); |
| 2045 | Token *last = asdl_seq_GET_UNTYPED(strings, len - 1); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2046 | |
| 2047 | int bytesmode = 0; |
| 2048 | PyObject *bytes_str = NULL; |
| 2049 | |
| 2050 | FstringParser state; |
| 2051 | _PyPegen_FstringParser_Init(&state); |
| 2052 | |
| 2053 | for (Py_ssize_t i = 0; i < len; i++) { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2054 | Token *t = asdl_seq_GET_UNTYPED(strings, i); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2055 | |
| 2056 | int this_bytesmode; |
| 2057 | int this_rawmode; |
| 2058 | PyObject *s; |
| 2059 | const char *fstr; |
| 2060 | Py_ssize_t fstrlen = -1; |
| 2061 | |
Lysandros Nikolaou | 2f37c35 | 2020-05-07 13:37:51 +0300 | [diff] [blame] | 2062 | if (_PyPegen_parsestr(p, &this_bytesmode, &this_rawmode, &s, &fstr, &fstrlen, t) != 0) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2063 | goto error; |
| 2064 | } |
| 2065 | |
| 2066 | /* Check that we are not mixing bytes with unicode. */ |
| 2067 | if (i != 0 && bytesmode != this_bytesmode) { |
| 2068 | RAISE_SYNTAX_ERROR("cannot mix bytes and nonbytes literals"); |
| 2069 | Py_XDECREF(s); |
| 2070 | goto error; |
| 2071 | } |
| 2072 | bytesmode = this_bytesmode; |
| 2073 | |
| 2074 | if (fstr != NULL) { |
| 2075 | assert(s == NULL && !bytesmode); |
| 2076 | |
| 2077 | int result = _PyPegen_FstringParser_ConcatFstring(p, &state, &fstr, fstr + fstrlen, |
| 2078 | this_rawmode, 0, first, t, last); |
| 2079 | if (result < 0) { |
| 2080 | goto error; |
| 2081 | } |
| 2082 | } |
| 2083 | else { |
| 2084 | /* String or byte string. */ |
| 2085 | assert(s != NULL && fstr == NULL); |
| 2086 | assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s)); |
| 2087 | |
| 2088 | if (bytesmode) { |
| 2089 | if (i == 0) { |
| 2090 | bytes_str = s; |
| 2091 | } |
| 2092 | else { |
| 2093 | PyBytes_ConcatAndDel(&bytes_str, s); |
| 2094 | if (!bytes_str) { |
| 2095 | goto error; |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | else { |
| 2100 | /* This is a regular string. Concatenate it. */ |
| 2101 | if (_PyPegen_FstringParser_ConcatAndDel(&state, s) < 0) { |
| 2102 | goto error; |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | if (bytesmode) { |
| 2109 | if (PyArena_AddPyObject(p->arena, bytes_str) < 0) { |
| 2110 | goto error; |
| 2111 | } |
| 2112 | return Constant(bytes_str, NULL, first->lineno, first->col_offset, last->end_lineno, |
| 2113 | last->end_col_offset, p->arena); |
| 2114 | } |
| 2115 | |
| 2116 | return _PyPegen_FstringParser_Finish(p, &state, first, last); |
| 2117 | |
| 2118 | error: |
| 2119 | Py_XDECREF(bytes_str); |
| 2120 | _PyPegen_FstringParser_Dealloc(&state); |
| 2121 | if (PyErr_Occurred()) { |
| 2122 | raise_decode_error(p); |
| 2123 | } |
| 2124 | return NULL; |
| 2125 | } |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 2126 | |
| 2127 | mod_ty |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2128 | _PyPegen_make_module(Parser *p, asdl_stmt_seq *a) { |
| 2129 | asdl_type_ignore_seq *type_ignores = NULL; |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 2130 | Py_ssize_t num = p->type_ignore_comments.num_items; |
| 2131 | if (num > 0) { |
| 2132 | // Turn the raw (comment, lineno) pairs into TypeIgnore objects in the arena |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2133 | type_ignores = _Py_asdl_type_ignore_seq_new(num, p->arena); |
Guido van Rossum | c001c09 | 2020-04-30 12:12:19 -0700 | [diff] [blame] | 2134 | if (type_ignores == NULL) { |
| 2135 | return NULL; |
| 2136 | } |
| 2137 | for (int i = 0; i < num; i++) { |
| 2138 | PyObject *tag = _PyPegen_new_type_comment(p, p->type_ignore_comments.items[i].comment); |
| 2139 | if (tag == NULL) { |
| 2140 | return NULL; |
| 2141 | } |
| 2142 | type_ignore_ty ti = TypeIgnore(p->type_ignore_comments.items[i].lineno, tag, p->arena); |
| 2143 | if (ti == NULL) { |
| 2144 | return NULL; |
| 2145 | } |
| 2146 | asdl_seq_SET(type_ignores, i, ti); |
| 2147 | } |
| 2148 | } |
| 2149 | return Module(a, type_ignores, p->arena); |
| 2150 | } |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2151 | |
| 2152 | // Error reporting helpers |
| 2153 | |
| 2154 | expr_ty |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 2155 | _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type) |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2156 | { |
| 2157 | if (e == NULL) { |
| 2158 | return NULL; |
| 2159 | } |
| 2160 | |
| 2161 | #define VISIT_CONTAINER(CONTAINER, TYPE) do { \ |
| 2162 | Py_ssize_t len = asdl_seq_LEN(CONTAINER->v.TYPE.elts);\ |
| 2163 | for (Py_ssize_t i = 0; i < len; i++) {\ |
| 2164 | expr_ty other = asdl_seq_GET(CONTAINER->v.TYPE.elts, i);\ |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 2165 | expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\ |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2166 | if (child != NULL) {\ |
| 2167 | return child;\ |
| 2168 | }\ |
| 2169 | }\ |
| 2170 | } while (0) |
| 2171 | |
| 2172 | // We only need to visit List and Tuple nodes recursively as those |
| 2173 | // are the only ones that can contain valid names in targets when |
| 2174 | // they are parsed as expressions. Any other kind of expression |
| 2175 | // that is a container (like Sets or Dicts) is directly invalid and |
| 2176 | // we don't need to visit it recursively. |
| 2177 | |
| 2178 | switch (e->kind) { |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 2179 | case List_kind: |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2180 | VISIT_CONTAINER(e, List); |
| 2181 | return NULL; |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 2182 | case Tuple_kind: |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2183 | VISIT_CONTAINER(e, Tuple); |
| 2184 | return NULL; |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2185 | case Starred_kind: |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 2186 | if (targets_type == DEL_TARGETS) { |
| 2187 | return e; |
| 2188 | } |
| 2189 | return _PyPegen_get_invalid_target(e->v.Starred.value, targets_type); |
| 2190 | case Compare_kind: |
| 2191 | // This is needed, because the `a in b` in `for a in b` gets parsed |
| 2192 | // as a comparison, and so we need to search the left side of the comparison |
| 2193 | // for invalid targets. |
| 2194 | if (targets_type == FOR_TARGETS) { |
| 2195 | cmpop_ty cmpop = (cmpop_ty) asdl_seq_GET(e->v.Compare.ops, 0); |
| 2196 | if (cmpop == In) { |
| 2197 | return _PyPegen_get_invalid_target(e->v.Compare.left, targets_type); |
| 2198 | } |
| 2199 | return NULL; |
| 2200 | } |
| 2201 | return e; |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 2202 | case Name_kind: |
| 2203 | case Subscript_kind: |
| 2204 | case Attribute_kind: |
| 2205 | return NULL; |
| 2206 | default: |
| 2207 | return e; |
| 2208 | } |
Lysandros Nikolaou | 75b863a | 2020-05-18 22:14:47 +0300 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) { |
| 2212 | int kwarg_unpacking = 0; |
| 2213 | for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) { |
| 2214 | keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i); |
| 2215 | if (!keyword->arg) { |
| 2216 | kwarg_unpacking = 1; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | const char *msg = NULL; |
| 2221 | if (kwarg_unpacking) { |
| 2222 | msg = "positional argument follows keyword argument unpacking"; |
| 2223 | } else { |
| 2224 | msg = "positional argument follows keyword argument"; |
| 2225 | } |
| 2226 | |
| 2227 | return RAISE_SYNTAX_ERROR(msg); |
| 2228 | } |
Lysandros Nikolaou | ae14583 | 2020-05-22 03:56:52 +0300 | [diff] [blame] | 2229 | |
| 2230 | void * |
| 2231 | _PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args) |
| 2232 | { |
| 2233 | /* The rule that calls this function is 'args for_if_clauses'. |
| 2234 | For the input f(L, x for x in y), L and x are in args and |
| 2235 | the for is parsed as a for_if_clause. We have to check if |
| 2236 | len <= 1, so that input like dict((a, b) for a, b in x) |
| 2237 | gets successfully parsed and then we pass the last |
| 2238 | argument (x in the above example) as the location of the |
| 2239 | error */ |
| 2240 | Py_ssize_t len = asdl_seq_LEN(args->v.Call.args); |
| 2241 | if (len <= 1) { |
| 2242 | return NULL; |
| 2243 | } |
| 2244 | |
| 2245 | return RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
| 2246 | (expr_ty) asdl_seq_GET(args->v.Call.args, len - 1), |
| 2247 | "Generator expression must be parenthesized" |
| 2248 | ); |
| 2249 | } |
Pablo Galindo | 4a97b15 | 2020-09-02 17:44:19 +0100 | [diff] [blame] | 2250 | |
| 2251 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2252 | expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b, |
Pablo Galindo | 315a61f | 2020-09-03 15:29:32 +0100 | [diff] [blame] | 2253 | int lineno, int col_offset, int end_lineno, |
| 2254 | int end_col_offset, PyArena *arena) { |
Pablo Galindo | 4a97b15 | 2020-09-02 17:44:19 +0100 | [diff] [blame] | 2255 | Py_ssize_t args_len = asdl_seq_LEN(a); |
| 2256 | Py_ssize_t total_len = args_len; |
| 2257 | |
| 2258 | if (b == NULL) { |
Pablo Galindo | 315a61f | 2020-09-03 15:29:32 +0100 | [diff] [blame] | 2259 | return _Py_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset, |
| 2260 | end_lineno, end_col_offset, arena); |
Pablo Galindo | 4a97b15 | 2020-09-02 17:44:19 +0100 | [diff] [blame] | 2261 | |
| 2262 | } |
| 2263 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2264 | asdl_expr_seq *starreds = _PyPegen_seq_extract_starred_exprs(p, b); |
| 2265 | asdl_keyword_seq *keywords = _PyPegen_seq_delete_starred_exprs(p, b); |
Pablo Galindo | 4a97b15 | 2020-09-02 17:44:19 +0100 | [diff] [blame] | 2266 | |
| 2267 | if (starreds) { |
| 2268 | total_len += asdl_seq_LEN(starreds); |
| 2269 | } |
| 2270 | |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2271 | asdl_expr_seq *args = _Py_asdl_expr_seq_new(total_len, arena); |
Pablo Galindo | 4a97b15 | 2020-09-02 17:44:19 +0100 | [diff] [blame] | 2272 | |
| 2273 | Py_ssize_t i = 0; |
| 2274 | for (i = 0; i < args_len; i++) { |
| 2275 | asdl_seq_SET(args, i, asdl_seq_GET(a, i)); |
| 2276 | } |
| 2277 | for (; i < total_len; i++) { |
| 2278 | asdl_seq_SET(args, i, asdl_seq_GET(starreds, i - args_len)); |
| 2279 | } |
| 2280 | |
Pablo Galindo | 315a61f | 2020-09-03 15:29:32 +0100 | [diff] [blame] | 2281 | return _Py_Call(_PyPegen_dummy_name(p), args, keywords, lineno, |
| 2282 | col_offset, end_lineno, end_col_offset, arena); |
Pablo Galindo | 4a97b15 | 2020-09-02 17:44:19 +0100 | [diff] [blame] | 2283 | } |