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 | |
Lysandros Nikolaou | 03b7642 | 2020-05-01 20:30:51 +0300 | [diff] [blame] | 34 | mod_ty res = PyPegen_ASTFromFilename(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 | { |
Lysandros Nikolaou | 69e802e | 2020-04-30 01:53:30 +0300 | [diff] [blame] | 48 | static char *keywords[] = {"string", "mode", "oldparser", NULL}; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 49 | char *the_string; |
| 50 | char *mode_str = "exec"; |
Lysandros Nikolaou | 69e802e | 2020-04-30 01:53:30 +0300 | [diff] [blame] | 51 | int oldparser = 0; |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 52 | |
Lysandros Nikolaou | 69e802e | 2020-04-30 01:53:30 +0300 | [diff] [blame] | 53 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|sp", keywords, |
| 54 | &the_string, &mode_str, &oldparser)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | int mode; |
| 59 | if (strcmp(mode_str, "exec") == 0) { |
| 60 | mode = Py_file_input; |
| 61 | } |
| 62 | else if (strcmp(mode_str, "eval") == 0) { |
| 63 | mode = Py_eval_input; |
| 64 | } |
| 65 | else if (strcmp(mode_str, "single") == 0) { |
| 66 | mode = Py_single_input; |
| 67 | } |
| 68 | else { |
| 69 | return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'eval' or 'single'"); |
| 70 | } |
| 71 | |
| 72 | PyArena *arena = PyArena_New(); |
| 73 | if (arena == NULL) { |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | PyObject *result = NULL; |
| 78 | |
| 79 | PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 80 | flags.cf_flags = PyCF_IGNORE_COOKIE; |
| 81 | |
Lysandros Nikolaou | 69e802e | 2020-04-30 01:53:30 +0300 | [diff] [blame] | 82 | mod_ty res; |
| 83 | if (oldparser) { |
| 84 | res = PyParser_ASTFromString(the_string, "<string>", mode, &flags, arena); |
| 85 | } |
| 86 | else { |
Lysandros Nikolaou | 03b7642 | 2020-05-01 20:30:51 +0300 | [diff] [blame] | 87 | res = PyPegen_ASTFromString(the_string, "<string>", mode, &flags, arena); |
Lysandros Nikolaou | 69e802e | 2020-04-30 01:53:30 +0300 | [diff] [blame] | 88 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 89 | if (res == NULL) { |
| 90 | goto error; |
| 91 | } |
| 92 | result = PyAST_mod2obj(res); |
| 93 | |
| 94 | error: |
| 95 | PyArena_Free(arena); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | static PyMethodDef ParseMethods[] = { |
| 100 | {"parse_file", (PyCFunction)(void (*)(void))_Py_parse_file, METH_VARARGS|METH_KEYWORDS, "Parse a file."}, |
| 101 | {"parse_string", (PyCFunction)(void (*)(void))_Py_parse_string, METH_VARARGS|METH_KEYWORDS,"Parse a string."}, |
| 102 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 103 | }; |
| 104 | |
| 105 | static struct PyModuleDef parsemodule = { |
| 106 | PyModuleDef_HEAD_INIT, |
| 107 | .m_name = "peg_parser", |
| 108 | .m_doc = "A parser.", |
| 109 | .m_methods = ParseMethods, |
| 110 | }; |
| 111 | |
| 112 | PyMODINIT_FUNC |
| 113 | PyInit__peg_parser(void) |
| 114 | { |
| 115 | return PyModule_Create(&parsemodule); |
| 116 | } |