-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.c
336 lines (259 loc) · 7.24 KB
/
regex.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* regex.c: Procedures for dealing with regular expressions.
*
* Copyright © 2012 Zachary Catlin. See LICENSE for terms.
*/
#include "regex.h"
#ifndef ML_UTILS_H
#include "utils.h"
#endif
#ifndef ML_STDIO_H
#define ML_STDIO_H
#include <stdio.h>
#endif
static regex_t * alloc_regex(size_t array_sz, const char *fname, int line)
{
regex_t *ptr, **init_array;
ptr = mod_2(1, regex_t, fname, line);
ptr->next = NULL;
if(array_sz <= 0)
return ptr;
init_array = mod_2(array_sz, regex_t *, fname, line);
ptr->data.list.n_enc = 0;
ptr->data.list.array_sz = array_sz;
ptr->data.list.enc = init_array;
return ptr;
}
regex_t * mk_char_rx_impl(char c, const char *fname, int line)
{
regex_t *rx = alloc_regex(0, fname, line);
rx->type = R_CHAR;
rx->data.c = 0xff & (int)c;
return rx;
}
regex_t * mk_char_class_rx_impl(int is_inverted, const char *fname, int line)
{
int i;
regex_t *rx = alloc_regex(0, fname, line);
rx->type = R_CLASS;
rx->data.cls.is_inverted = is_inverted;
for(i = 0; i < CLASS_SZ; ++i)
rx->data.cls.set[i] = 0;
return rx;
}
void add_to_char_class_impl(regex_t *rx, char c, const char *fname, int line)
{
int ic = 0xff & (int) c;
if(rx == NULL || rx->type != R_CLASS) {
fprintf(stderr, "%s:%d: Invalid argument to add_to_char_class\n",
fname, line);
exit(1);
}
rx->data.cls.set[ic / ML_UINT_BIT] |= 1 << (ic % ML_UINT_BIT);
}
regex_t * mk_any_rx_impl(const char *fname, int line)
{
regex_t *rx = alloc_regex(0, fname, line);
rx->type = R_ANY;
return rx;
}
#define DEF_ARRAY_SZ 16
regex_t * mk_option_rx_impl(const char *fname, int line)
{
regex_t *rx = alloc_regex(DEF_ARRAY_SZ, fname, line);
rx->type = R_OPTION;
return rx;
}
regex_t * mk_concat_rx_impl(size_t start_len, const char *fname, int line)
{
regex_t *rx = alloc_regex((start_len <= 0) ? DEF_ARRAY_SZ : start_len,
fname, line);
rx->type = R_CONCAT;
return rx;
}
void add_enc_rx_impl(regex_t *rx, regex_t *enc, const char *fname, int line)
{
size_t i, new_sz;
regex_t **new_enc;
if(rx == NULL || (rx->type != R_OPTION && rx->type != R_CONCAT)) {
fprintf(stderr, "%s:%d: Invalid argument to add_enc_rx\n",
fname, line);
exit(1);
}
if(enc == NULL) {
fprintf(stderr, "%s:%d: NULL enclosed regex for add_enc_rx\n",
fname, line);
exit(1);
}
if(rx->data.list.n_enc + 1 >= rx->data.list.array_sz) {
new_sz = rx->data.list.array_sz * 2 + 1;
new_enc = mod_2(new_sz, regex_t *, fname, line);
for(i = 0; i < rx->data.list.n_enc; ++i)
new_enc[i] = rx->data.list.enc[i];
free(rx->data.list.enc);
rx->data.list.enc = new_enc;
rx->data.list.array_sz = new_sz;
}
rx->data.list.enc[rx->data.list.n_enc++] = enc;
}
regex_t * mk_maybe_rx_impl(regex_t *enc, const char *fname, int line)
{
regex_t *rx;
if(enc == NULL) {
fprintf(stderr, "%s:%d: NULL enclosed regex for mk_maybe_rx\n",
fname, line);
exit(1);
}
rx = alloc_regex(0, fname, line);
rx->type = R_MAYBE;
rx->data.enc = enc;
return rx;
}
regex_t * mk_star_rx_impl(regex_t *enc, const char *fname, int line)
{
regex_t *rx;
if(enc == NULL) {
fprintf(stderr, "%s:%d: NULL enclosed regex for mk_star_rx\n",
fname, line);
exit(1);
}
rx = alloc_regex(0, fname, line);
rx->type = R_STAR;
rx->data.enc = enc;
return rx;
}
regex_t * mk_plus_rx_impl(regex_t *enc, const char *fname, int line)
{
regex_t *rx;
if(enc == NULL) {
fprintf(stderr, "%s:%d: NULL enclosed regex for mk_plus_rx\n",
fname, line);
exit(1);
}
rx = alloc_regex(0, fname, line);
rx->type = R_PLUS;
rx->data.enc = enc;
return rx;
}
regex_t * mk_num_rx_impl(regex_t *enc, int min, int max,
const char *fname, int line)
{
regex_t *rx;
if(enc == NULL) {
fprintf(stderr, "%s:%d: NULL enclosed regex for mk_num_rx\n",
fname, line);
exit(1);
}
rx = alloc_regex(0, fname, line);
rx->type = R_NUM;
rx->data.num.enc = enc;
rx->data.num.min = min;
rx->data.num.max = max;
return rx;
}
regex_t * mk_zero_rx_impl(const char *fname, int line)
{
regex_t *rx = alloc_regex(0, fname, line);
rx->type = R_ZERO;
return rx;
}
regex_t * mk_paren_rx_impl(const char *fname, int line)
{
regex_t *rx = alloc_regex(0, fname, line);
rx->type = R_PAREN;
return rx;
}
void free_regex_tree_impl(regex_t *rx, const char *fname, int line)
{
size_t i;
if(rx == NULL) {
fprintf(stderr, "%s:%d: Tried to free a NULL object!\n", fname, line);
exit(1);
}
switch(rx->type) {
case R_OPTION:
case R_CONCAT:
for(i = 0; i < rx->data.list.n_enc; ++i)
free_regex_tree_impl(rx->data.list.enc[i], fname, line);
free(rx->data.list.enc);
break;
case R_MAYBE:
case R_STAR:
case R_PLUS:
free_regex_tree_impl(rx->data.enc, fname, line);
break;
case R_NUM:
free_regex_tree_impl(rx->data.num.enc, fname, line);
break;
default:
;
}
free(rx);
}
static void print_escaped_char(FILE *f, int c)
{
if(c == '\n')
fputs("\\n", f);
else if(c == '\\' || c == '\'')
fprintf(f, "\\%c", c);
else if(c < 0x20 || c >= 0x7f)
fprintf(f, "\\x%02x", 0xff & c);
else
fputc(c, f);
}
static void print_regex_tree_int(FILE *f, regex_t *rx, size_t depth)
{
size_t i;
int j;
regex_type t;
for(i = 0; i < depth; ++i)
fputc(' ', f);
if(rx == NULL) {
fputs("[NULL]\n", f);
return;
}
t = rx->type;
switch(t) {
case R_CHAR:
fputs("char: \'", f);
print_escaped_char(f, rx->data.c);
fputs("\'\n", f);
break;
case R_CLASS:
fprintf(f, "class%s: [", (rx->data.cls.is_inverted) ? " [inv]" : "");
for(i = 0; i < CLASS_SZ; ++i) {
for(j = 0; j < ML_UINT_BIT; ++j)
if(rx->data.cls.set[i] & (1 << j))
print_escaped_char(f, j + ML_UINT_BIT * i);
}
fputs("]\n", f);
break;
case R_ANY:
case R_ZERO:
case R_PAREN:
fputs((t == R_ANY) ? "any\n" : (t == R_ZERO) ? "zero\n" : "paren\n",
f);
break;
case R_OPTION:
case R_CONCAT:
fputs((t == R_OPTION) ? "option:\n" : "concat\n", f);
for(i = 0; i < rx->data.list.n_enc; ++i)
print_regex_tree_int(f, rx->data.list.enc[i], depth+1);
break;
case R_MAYBE:
case R_STAR:
case R_PLUS:
fputs((t == R_MAYBE) ? "maybe\n" : (t == R_STAR) ? "star\n"
: "plus\n", f);
print_regex_tree_int(f, rx->data.enc, depth+1);
break;
case R_NUM:
fprintf(f, "num[%d,%d]:\n", rx->data.num.min, rx->data.num.max);
print_regex_tree_int(f, rx->data.num.enc, depth+1);
break;
}
}
void print_regex_tree(FILE *f, regex_t *rx)
{
print_regex_tree_int(f, rx, 0);
}