blob: e1ec36e07bd57fd9cc7c62f9a99474828270de69 [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
Pablo Galindo2b74c832020-04-27 18:02:07 +010034 mod_ty res = PyPegen_ASTFromFile(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{
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
86error:
87 PyArena_Free(arena);
88 return result;
89}
90
91static 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
97static struct PyModuleDef parsemodule = {
98 PyModuleDef_HEAD_INIT,
99 .m_name = "peg_parser",
100 .m_doc = "A parser.",
101 .m_methods = ParseMethods,
102};
103
104PyMODINIT_FUNC
105PyInit__peg_parser(void)
106{
107 return PyModule_Create(&parsemodule);
108}