Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 1 | #include <Python.h> |
Lysandros Nikolaou | ebebb64 | 2020-04-23 18:36:06 +0300 | [diff] [blame] | 2 | #include "pegen_interface.h" |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3 | |
| 4 | PyObject * |
| 5 | _Py_parse_file(PyObject *self, PyObject *args, PyObject *kwds) |
| 6 | { |
| 7 | static char *keywords[] = {"file", "mode", NULL}; |
| 8 | char *filename; |
| 9 | char *mode_str = "exec"; |
| 10 | |
| 11 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", keywords, &filename, &mode_str)) { |
| 12 | return NULL; |
| 13 | } |
| 14 | |
| 15 | int mode; |
| 16 | if (strcmp(mode_str, "exec") == 0) { |
| 17 | mode = Py_file_input; |
| 18 | } |
| 19 | else if (strcmp(mode_str, "single") == 0) { |
| 20 | mode = Py_single_input; |
| 21 | } |
| 22 | else { |
| 23 | return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'single'"); |
| 24 | } |
| 25 | |
| 26 | PyArena *arena = PyArena_New(); |
| 27 | if (arena == NULL) { |
| 28 | return NULL; |
| 29 | } |
| 30 | |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame^] | 31 | PyCompilerFlags flags = _PyCompilerFlags_INIT; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 32 | PyObject *result = NULL; |
| 33 | |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame^] | 34 | mod_ty res = PyPegen_ASTFromFile(filename, mode, &flags, arena); |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 35 | if (res == NULL) { |
| 36 | goto error; |
| 37 | } |
| 38 | result = PyAST_mod2obj(res); |
| 39 | |
| 40 | error: |
| 41 | PyArena_Free(arena); |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | PyObject * |
| 46 | _Py_parse_string(PyObject *self, PyObject *args, PyObject *kwds) |
| 47 | { |
| 48 | static char *keywords[] = {"string", "mode", NULL}; |
| 49 | char *the_string; |
| 50 | char *mode_str = "exec"; |
| 51 | |
| 52 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", keywords, &the_string, &mode_str)) { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | int mode; |
| 57 | if (strcmp(mode_str, "exec") == 0) { |
| 58 | mode = Py_file_input; |
| 59 | } |
| 60 | else if (strcmp(mode_str, "eval") == 0) { |
| 61 | mode = Py_eval_input; |
| 62 | } |
| 63 | else if (strcmp(mode_str, "single") == 0) { |
| 64 | mode = Py_single_input; |
| 65 | } |
| 66 | else { |
| 67 | return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'eval' or 'single'"); |
| 68 | } |
| 69 | |
| 70 | PyArena *arena = PyArena_New(); |
| 71 | if (arena == NULL) { |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | PyObject *result = NULL; |
| 76 | |
| 77 | PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 78 | flags.cf_flags = PyCF_IGNORE_COOKIE; |
| 79 | |
| 80 | mod_ty res = PyPegen_ASTFromString(the_string, mode, &flags, arena); |
| 81 | if (res == NULL) { |
| 82 | goto error; |
| 83 | } |
| 84 | result = PyAST_mod2obj(res); |
| 85 | |
| 86 | error: |
| 87 | PyArena_Free(arena); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | static PyMethodDef ParseMethods[] = { |
| 92 | {"parse_file", (PyCFunction)(void (*)(void))_Py_parse_file, METH_VARARGS|METH_KEYWORDS, "Parse a file."}, |
| 93 | {"parse_string", (PyCFunction)(void (*)(void))_Py_parse_string, METH_VARARGS|METH_KEYWORDS,"Parse a string."}, |
| 94 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 95 | }; |
| 96 | |
| 97 | static struct PyModuleDef parsemodule = { |
| 98 | PyModuleDef_HEAD_INIT, |
| 99 | .m_name = "peg_parser", |
| 100 | .m_doc = "A parser.", |
| 101 | .m_methods = ParseMethods, |
| 102 | }; |
| 103 | |
| 104 | PyMODINIT_FUNC |
| 105 | PyInit__peg_parser(void) |
| 106 | { |
| 107 | return PyModule_Create(&parsemodule); |
| 108 | } |