blob: 3b27b2c9cbaa24ba48e58453901d36b3295c4870 [file] [log] [blame]
Pablo Galindoc5fc1562020-04-22 23:29:27 +01001#include <Python.h>
Lysandros Nikolaouebebb642020-04-23 18:36:06 +03002#include "pegen_interface.h"
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003
4PyObject *
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 Galindo2b74c832020-04-27 18:02:07 +010031 PyCompilerFlags flags = _PyCompilerFlags_INIT;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010032 PyObject *result = NULL;
33
Lysandros Nikolaou03b76422020-05-01 20:30:51 +030034 mod_ty res = PyPegen_ASTFromFilename(filename, mode, &flags, arena);
Pablo Galindoc5fc1562020-04-22 23:29:27 +010035 if (res == NULL) {
36 goto error;
37 }
38 result = PyAST_mod2obj(res);
39
40error:
41 PyArena_Free(arena);
42 return result;
43}
44
45PyObject *
46_Py_parse_string(PyObject *self, PyObject *args, PyObject *kwds)
47{
Lysandros Nikolaou69e802e2020-04-30 01:53:30 +030048 static char *keywords[] = {"string", "mode", "oldparser", NULL};
Pablo Galindoc5fc1562020-04-22 23:29:27 +010049 char *the_string;
50 char *mode_str = "exec";
Lysandros Nikolaou69e802e2020-04-30 01:53:30 +030051 int oldparser = 0;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010052
Lysandros Nikolaou69e802e2020-04-30 01:53:30 +030053 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|sp", keywords,
54 &the_string, &mode_str, &oldparser)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +010055 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 Nikolaou69e802e2020-04-30 01:53:30 +030082 mod_ty res;
83 if (oldparser) {
84 res = PyParser_ASTFromString(the_string, "<string>", mode, &flags, arena);
85 }
86 else {
Lysandros Nikolaou03b76422020-05-01 20:30:51 +030087 res = PyPegen_ASTFromString(the_string, "<string>", mode, &flags, arena);
Lysandros Nikolaou69e802e2020-04-30 01:53:30 +030088 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010089 if (res == NULL) {
90 goto error;
91 }
92 result = PyAST_mod2obj(res);
93
94error:
95 PyArena_Free(arena);
96 return result;
97}
98
99static 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
105static struct PyModuleDef parsemodule = {
106 PyModuleDef_HEAD_INIT,
107 .m_name = "peg_parser",
108 .m_doc = "A parser.",
109 .m_methods = ParseMethods,
110};
111
112PyMODINIT_FUNC
113PyInit__peg_parser(void)
114{
115 return PyModule_Create(&parsemodule);
116}