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