blob: b66d5a83a84f64197c1fcc8a839dc207fe55b6ec [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
Lysandros Nikolaou96459302020-05-25 22:51:58 +03004static int
5_mode_str_to_int(char *mode_str)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01006{
Pablo Galindoc5fc1562020-04-22 23:29:27 +01007 int mode;
8 if (strcmp(mode_str, "exec") == 0) {
9 mode = Py_file_input;
10 }
11 else if (strcmp(mode_str, "eval") == 0) {
12 mode = Py_eval_input;
13 }
14 else if (strcmp(mode_str, "single") == 0) {
15 mode = Py_single_input;
16 }
17 else {
Lysandros Nikolaou96459302020-05-25 22:51:58 +030018 mode = -1;
19 }
20 return mode;
21}
22
23static mod_ty
24_run_parser(char *str, char *filename, int mode, PyCompilerFlags *flags, PyArena *arena, int oldparser)
25{
26 mod_ty mod;
27 if (!oldparser) {
28 mod = PyPegen_ASTFromString(str, filename, mode, flags, arena);
29 }
30 else {
31 mod = PyParser_ASTFromString(str, filename, mode, flags, arena);
32 }
33 return mod;
34}
35
36PyObject *
37_Py_compile_string(PyObject *self, PyObject *args, PyObject *kwds)
38{
39 static char *keywords[] = {"string", "filename", "mode", "oldparser", NULL};
40 char *the_string;
41 char *filename = "<string>";
42 char *mode_str = "exec";
43 int oldparser = 0;
44
45 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ssp", keywords,
46 &the_string, &filename, &mode_str, &oldparser)) {
47 return NULL;
48 }
49
50 int mode = _mode_str_to_int(mode_str);
51 if (mode == -1) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +010052 return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'eval' or 'single'");
53 }
54
Lysandros Nikolaou96459302020-05-25 22:51:58 +030055 PyCompilerFlags flags = _PyCompilerFlags_INIT;
56 flags.cf_flags = PyCF_IGNORE_COOKIE;
57
Pablo Galindoc5fc1562020-04-22 23:29:27 +010058 PyArena *arena = PyArena_New();
59 if (arena == NULL) {
60 return NULL;
61 }
62
Lysandros Nikolaou96459302020-05-25 22:51:58 +030063 mod_ty mod = _run_parser(the_string, filename, mode, &flags, arena, oldparser);
64 if (mod == NULL) {
65 PyArena_Free(arena);
66 return NULL;
67 }
68
69 PyObject *filename_ob = PyUnicode_DecodeFSDefault(filename);
70 if (filename_ob == NULL) {
71 PyArena_Free(arena);
72 return NULL;
73 }
74 PyCodeObject *result = PyAST_CompileObject(mod, filename_ob, &flags, -1, arena);
75 Py_XDECREF(filename_ob);
76 PyArena_Free(arena);
77 return (PyObject *)result;
78}
79
80PyObject *
81_Py_parse_string(PyObject *self, PyObject *args, PyObject *kwds)
82{
83 static char *keywords[] = {"string", "filename", "mode", "oldparser", NULL};
84 char *the_string;
85 char *filename = "<string>";
86 char *mode_str = "exec";
87 int oldparser = 0;
88
89 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ssp", keywords,
90 &the_string, &filename, &mode_str, &oldparser)) {
91 return NULL;
92 }
93
94 int mode = _mode_str_to_int(mode_str);
95 if (mode == -1) {
96 return PyErr_Format(PyExc_ValueError, "mode must be either 'exec' or 'eval' or 'single'");
97 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +010098
99 PyCompilerFlags flags = _PyCompilerFlags_INIT;
100 flags.cf_flags = PyCF_IGNORE_COOKIE;
101
Lysandros Nikolaou96459302020-05-25 22:51:58 +0300102 PyArena *arena = PyArena_New();
103 if (arena == NULL) {
104 return NULL;
Lysandros Nikolaou69e802e2020-04-30 01:53:30 +0300105 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100106
Lysandros Nikolaou96459302020-05-25 22:51:58 +0300107 mod_ty mod = _run_parser(the_string, filename, mode, &flags, arena, oldparser);
108 if (mod == NULL) {
109 PyArena_Free(arena);
110 return NULL;
111 }
112
113 PyObject *result = PyAST_mod2obj(mod);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100114 PyArena_Free(arena);
115 return result;
116}
117
118static PyMethodDef ParseMethods[] = {
Lysandros Nikolaou96459302020-05-25 22:51:58 +0300119 {
120 "parse_string",
121 (PyCFunction)(void (*)(void))_Py_parse_string,
122 METH_VARARGS|METH_KEYWORDS,
123 "Parse a string, return an AST."
124 },
125 {
126 "compile_string",
127 (PyCFunction)(void (*)(void))_Py_compile_string,
128 METH_VARARGS|METH_KEYWORDS,
129 "Compile a string, return a code object."
130 },
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100131 {NULL, NULL, 0, NULL} /* Sentinel */
132};
133
134static struct PyModuleDef parsemodule = {
135 PyModuleDef_HEAD_INIT,
136 .m_name = "peg_parser",
137 .m_doc = "A parser.",
138 .m_methods = ParseMethods,
139};
140
141PyMODINIT_FUNC
142PyInit__peg_parser(void)
143{
144 return PyModule_Create(&parsemodule);
145}