1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "pegen.h"
#include "pycore_compile.h" // _PyAST_Compile()
PyObject *
_build_return_object(mod_ty module, int mode, PyObject *filename_ob, PyArena *arena)
{
PyObject *result = NULL;
if (mode == 2) {
result = (PyObject *)_PyAST_Compile(module, filename_ob, NULL, -1, arena);
} else if (mode == 1) {
result = PyAST_mod2obj(module);
} else {
result = Py_None;
Py_INCREF(result);
}
return result;
}
static PyObject *
parse_file(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"file", "mode", NULL};
const char *filename;
int mode = 2;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", keywords, &filename, &mode)) {
return NULL;
}
if (mode < 0 || mode > 2) {
return PyErr_Format(PyExc_ValueError, "Bad mode, must be 0 <= mode <= 2");
}
PyArena *arena = PyArena_New();
if (arena == NULL) {
return NULL;
}
PyObject *result = NULL;
PyObject *filename_ob = PyUnicode_FromString(filename);
if (filename_ob == NULL) {
goto error;
}
PyCompilerFlags flags = _PyCompilerFlags_INIT;
mod_ty res = _PyPegen_run_parser_from_file(filename, Py_file_input, filename_ob, &flags, arena);
if (res == NULL) {
goto error;
}
result = _build_return_object(res, mode, filename_ob, arena);
error:
Py_XDECREF(filename_ob);
PyArena_Free(arena);
return result;
}
static PyObject *
parse_string(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"str", "mode", NULL};
const char *the_string;
int mode = 2;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", keywords, &the_string, &mode)) {
return NULL;
}
if (mode < 0 || mode > 2) {
return PyErr_Format(PyExc_ValueError, "Bad mode, must be 0 <= mode <= 2");
}
PyArena *arena = PyArena_New();
if (arena == NULL) {
return NULL;
}
PyObject *result = NULL;
PyObject *filename_ob = PyUnicode_FromString("<string>");
if (filename_ob == NULL) {
goto error;
}
PyCompilerFlags flags = _PyCompilerFlags_INIT;
mod_ty res = _PyPegen_run_parser_from_string(the_string, Py_file_input, filename_ob,
&flags, arena);
if (res == NULL) {
goto error;
}
result = _build_return_object(res, mode, filename_ob, arena);
error:
Py_XDECREF(filename_ob);
PyArena_Free(arena);
return result;
}
static PyObject *
clear_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
{
_PyPegen_clear_memo_statistics();
Py_RETURN_NONE;
}
static PyObject *
get_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
{
return _PyPegen_get_memo_statistics();
}
// TODO: Write to Python's sys.stdout instead of C's stdout.
static PyObject *
dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
{
PyObject *list = _PyPegen_get_memo_statistics();
if (list == NULL) {
return NULL;
}
Py_ssize_t len = PyList_Size(list);
for (Py_ssize_t i = 0; i < len; i++) {
PyObject *value = PyList_GetItem(list, i); // Borrowed reference.
long count = PyLong_AsLong(value);
if (count < 0) {
break;
}
if (count > 0) {
printf("%4zd %9ld\n", i, count);
}
}
Py_DECREF(list);
Py_RETURN_NONE;
}
static PyMethodDef ParseMethods[] = {
{"parse_file", (PyCFunction)(void(*)(void))parse_file, METH_VARARGS|METH_KEYWORDS, "Parse a file."},
{"parse_string", (PyCFunction)(void(*)(void))parse_string, METH_VARARGS|METH_KEYWORDS, "Parse a string."},
{"clear_memo_stats", clear_memo_stats, METH_NOARGS},
{"dump_memo_stats", dump_memo_stats, METH_NOARGS},
{"get_memo_stats", get_memo_stats, METH_NOARGS},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef parsemodule = {
PyModuleDef_HEAD_INIT,
.m_name = "parse",
.m_doc = "A parser.",
.m_methods = ParseMethods,
};
PyMODINIT_FUNC
PyInit_parse(void)
{
return PyModule_Create(&parsemodule);
}
|