-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_compiler.c
222 lines (174 loc) · 5.11 KB
/
regex_compiler.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
#include "common.h"
#include "arena_allocator.h"
#include "regex_compiler.h"
#pragma GCC diagnostic ignored "-Wswitch"
#define META 1
#define QUANT 2
// Used for returning to regex_compile() on syntax errors.
static jmp_buf err_jmp_buf;
// Metacharacters never stand for themselves (outside of brackets) and need to
// be escaped. '\0' is included for convenience when scanning for non-literals;
// patterns containing '\0' are not supported.
static const uint8_t char_properties[1 << CHAR_BIT] =
{ ['\0'] = META,
['.'] = META,
['*'] = META | QUANT,
['+'] = META | QUANT,
['?'] = META | QUANT,
['['] = META,
['('] = META,
[')'] = META };
static bool is_literal(char c)
{
return !(char_properties[(uc)c] & META);
}
static bool is_quant(char c)
{
return char_properties[(uc)c] & QUANT;
}
static Regex_node *make_node(Regex_node_type type, Arena *arena)
{
Regex_node *node = arena_alloc(arena, sizeof *node);
node->next = NULL;
node->type = type;
return node;
}
// Parses and returns a (possibly escaped) literal character. Works both
// outside and inside brackets.
static char parse_literal(const char **pat)
{
char res;
if (**pat == '\\') {
++*pat; // Eat "\"
if (**pat == '\0')
// "\" at end of line
longjmp(err_jmp_buf, 1);
}
res = **pat;
++*pat;
return res;
}
// parse_literals() helper. If the input starts with a non-quantified literal
// (a literal not followed by '?', '*', or '+'), move 'pat' past it and return
// true. Otherwise, return false without modifying 'pat'.
static bool skip_nq_literal(const char **pat)
{
const char *tmp;
if (!is_literal(**pat))
return false;
tmp = *pat;
parse_literal(&tmp);
if (is_quant(*tmp))
return false;
*pat = tmp;
return true;
}
// Removes backslashes before escaped characters in 's' in-place
static void deescape(char *s)
{
char *write_pos;
for (write_pos = s;; ++s, ++write_pos) {
if (*s == '\\')
++s;
*write_pos = *s;
if (*write_pos == '\0')
return;
}
}
// If the input has two or more non-quantified literals in a row, returns a
// SPAN node with the literals. Otherwise, returns a CHAR node with the initial
// literal. We must be careful to handle escaped literals correctly.
static Regex_node *parse_literals(const char **pat, Arena *arena)
{
const char *start;
char first_literal;
Regex_node *node;
if (!is_literal(**pat))
longjmp(err_jmp_buf, 1);
start = *pat;
first_literal = parse_literal(pat);
if (!skip_nq_literal(pat)) {
// Single character - use a CHAR node
node = make_node(CHAR, arena);
node->c = first_literal;
}
else {
// Many characters - use a SPAN node
while (skip_nq_literal(pat));
node = make_node(SPAN, arena);
node->s = arena_strndup(arena, start, *pat - start);
deescape(node->s);
}
return node;
}
static Regex_node *parse_set(const char **pat, Arena *arena)
{
Regex_node *node = make_node(SET, arena);
++*pat; // Eat "["
// Create a look-up table for the set
node->set = arena_alloc(arena, sizeof(bool)*(1 << CHAR_BIT));
for (unsigned i = 0; i < 1 << CHAR_BIT; ++i)
node->set[i] = false;
// An initial "]" is considered to be part of the set, so we always include
// the first character
do {
if (**pat == '\0')
longjmp(err_jmp_buf, 1);
node->set[(uc)parse_literal(pat)] = true;
} while (**pat != ']');
++*pat; // Eat "]"
return node;
}
// Modifies the node type according to the quantifier (if any)
static void quantify(const char **pat, Regex_node *node)
{
switch (**pat) {
case '?': ++*pat; node->type += 1; break; // X -> X_ZERO_OR_ONE.
case '*': ++*pat; node->type += 2; break; // X -> X_ZERO_OR_MORE.
case '+': ++*pat; node->type += 3; break; // X -> X_ONE_OR_MORE.
}
}
static Regex_node *parse_regex(const char **pat, Arena *arena);
static Regex_node *parse_node(const char **pat, Arena *arena)
{
Regex_node *node;
switch (**pat) {
case '.':
++*pat; // Eat "."
node = make_node(CHAR_ANY, arena);
break;
case '[':
node = parse_set(pat, arena);
break;
case '(':
++*pat; // Eat "("
node = make_node(SUB, arena);
node->sub = parse_regex(pat, arena);
if (**pat != ')')
longjmp(err_jmp_buf, 1);
++*pat; // Eat ")"
break;
default:
node = parse_literals(pat, arena);
}
quantify(pat, node);
return node;
}
static Regex_node *parse_regex(const char **pat, Arena *arena)
{
Regex_node *start, **cur;
for (cur = &start; **pat != '\0' && **pat != ')'; cur = &(*cur)->next)
*cur = parse_node(pat, arena);
*cur = NULL;
return start;
}
bool regex_compile(const char *pat, Regex *regex, Arena *arena)
{
if (setjmp(err_jmp_buf) == 1)
return false;
regex->start = parse_regex(&pat, arena);
if (*pat != '\0')
// Found extra trailing characters
return false;
return true;
}