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