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