-
Notifications
You must be signed in to change notification settings - Fork 2
/
reprs.rl
208 lines (162 loc) · 6.16 KB
/
reprs.rl
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h>
#include <stdint.h>
/* Docstrings */
static char module_docstring[] = "This module provides blazing-fast reprs and evals implementation for python\n";
static char reprs_docstring[] = "reprs function return repr(str) for string";
static char evals_docstring[] = "evals function return eval(str) for string";
/* Available functions */
static PyObject *_reprs(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *_evals(PyObject *self, PyObject *args, PyObject *kwargs);
/* Module specification */
static PyMethodDef module_methods[] = {
{"reprs", (PyCFunction)_reprs, METH_VARARGS | METH_KEYWORDS, reprs_docstring},
{"evals", (PyCFunction)_evals, METH_VARARGS | METH_KEYWORDS, evals_docstring},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef reprs = {
PyModuleDef_HEAD_INIT,
"reprs", /* name of module */
module_docstring, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods
};
PyMODINIT_FUNC PyInit__reprs(void) {
return PyModule_Create(&reprs);
}
static char hexdigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
%%{
machine reprs_fsm;
alphtype unsigned char;
need_escape = ('"' @ { *out++ = '"'; }
| "'" @ { *out++ = '\''; }
| '\\'@ { *out++ = '\\'; }
| '\t' @ { *out++ = 't'; }
| '\n' @ { *out++ = 'n'; }
| '\r' @ { *out++ = 'r'; }
) > { *out++ = '\\';};
normal = ((0x20 .. 0x7e) - need_escape) @ { *out++ = fc; };
octet = normal | need_escape
| (any - normal - need_escape) @ {
*out++ = '\\';
*out++ = 'x';
*out++ = hexdigit[fc / 16];
*out++ = hexdigit[fc % 16];
};
main := octet*;
}%%
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-const-variable"
%% write data;
#pragma GCC diagnostic pop
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
static PyObject *_reprs(PyObject *self, PyObject *args, PyObject *kwargs) {
Py_ssize_t size;
const char * str;
static char *kwlist[] = {
"str",
NULL
};
/* Parse the input tuple */
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#", kwlist, &str, &size)) {
return NULL;
}
if (str == NULL || size == 0) {
Py_INCREF(Py_None);
return Py_None;
}
char * buffer = (char *)malloc(size * 4);
if (buffer == NULL) {
return NULL;
}
char * out = buffer;
uint8_t *p = (uint8_t*) str;
uint8_t *pe = p + size;
int cs;
%% write init;
%% write exec;
PyObject * ret = PyBytes_FromStringAndSize(buffer, out-buffer);
free(buffer);
return ret;
}
#pragma GCC diagnostic pop
static int hexvalue[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
%%{
machine evals_fsm;
alphtype unsigned char;
escape = 0x5c;
escape_sequence = escape escape @ { value = '\\'; }
| escape 't' @ { value = '\t'; }
| escape 'b' @ { value = '\b'; }
| escape 'n' @ { value = '\n'; }
| escape 'r' @ { value = '\r'; }
| escape 'v' @ { value = '\v'; }
| escape 'f' @ { value = '\f'; }
| escape 'a' @ { value = '\a'; }
| escape '"' @ { value = '"'; }
| escape "'" @ { value = '\''; }
| escape 'x' @ { value = 0; } (xdigit @ {
value = value * 16 + hexvalue[(uint32_t)(fc)];
}){2};
main := |*
any => { *out++ = fc; };
escape_sequence => { *out++ = (uint8_t)value; };
*|;
}%%
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-const-variable"
%% write data;
#pragma GCC diagnostic pop
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
static PyObject *_evals(PyObject *self, PyObject *args, PyObject *kwargs) {
Py_ssize_t size;
const char * str;
static char *kwlist[] = {
"str",
NULL
};
/* Parse the input tuple */
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#", kwlist, &str, &size)) {
return NULL;
}
if (str == NULL || size == 0) {
Py_INCREF(Py_None);
return Py_None;
}
char * buffer = (char *)malloc(size);
if (buffer == NULL) {
return NULL;
}
uint8_t * out = (uint8_t *)buffer;
uint8_t *p = (uint8_t*) str;
uint8_t *pe = p + size;
uint8_t *eof = pe;
int cs, act;
uint8_t *ts, *te;
int value = 0;
%% write init;
%% write exec;
PyObject * ret = PyBytes_FromStringAndSize(buffer, (char *)out-buffer);
free(buffer);
return ret;
}
#pragma GCC diagnostic pop