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