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