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