teptris 0.3.19.1-aarch64-linux → 0.3.21.0-aarch64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Steepfile +1 -0
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/schema.h +107 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +656 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +440 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.h +34 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.c +1090 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.h +25 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +734 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +221 -0
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +1034 -0
- data/ext/teptris_ext/materialize.c +87 -1
- data/lib/teptris/3.3/teptris_ext.so +0 -0
- data/lib/teptris/3.4/teptris_ext.so +0 -0
- data/lib/teptris/4.0/teptris_ext.so +0 -0
- data/lib/teptris/schema.rb +64 -0
- data/lib/teptris/version.rb +1 -1
- data/lib/teptris.rb +1 -0
- data/sig/teptris.rbs +32 -0
- metadata +12 -2
|
@@ -0,0 +1,1090 @@
|
|
|
1
|
+
/* A practical ECMA-regex subset compiled to a Thompson NFA and
|
|
2
|
+
* simulated in linear time: immune to ReDoS by construction (no
|
|
3
|
+
* backtracking, no step limits to tune). Supports literals, '.',
|
|
4
|
+
* classes ([...] with ranges, negation, \d \w \s and negations),
|
|
5
|
+
* groups (non-capturing (?:...), named (?<name>...)), alternation,
|
|
6
|
+
* greedy quantifiers (* + ? {n} {n,} {n,m}), anchors (^ $, whole-text
|
|
7
|
+
* semantics), \b \B, \xNN, \uNNNN, and a leading (?i) ASCII
|
|
8
|
+
* case-folding prefix.
|
|
9
|
+
*
|
|
10
|
+
* REJECTED AT COMPILE (clear diagnostics, never silently wrong):
|
|
11
|
+
* backreferences, lookahead, lookbehind, \p{...}, other inline flags.
|
|
12
|
+
* Lazy quantifiers are accepted: they change which match is found,
|
|
13
|
+
* not whether one exists, which is all boolean `pattern` matching
|
|
14
|
+
* needs.
|
|
15
|
+
*
|
|
16
|
+
* Matching is over Unicode code points (the caller hands UTF-8), so
|
|
17
|
+
* '.' matches a whole code point and classes range over code points. */
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
#include <stdlib.h>
|
|
20
|
+
#include <string.h>
|
|
21
|
+
|
|
22
|
+
#include "regex.h"
|
|
23
|
+
|
|
24
|
+
/* ---------------------------------------------------------------- AST -- */
|
|
25
|
+
|
|
26
|
+
enum {
|
|
27
|
+
RA_CHAR,
|
|
28
|
+
RA_ANY,
|
|
29
|
+
RA_CLASS,
|
|
30
|
+
RA_SEQ,
|
|
31
|
+
RA_ALT,
|
|
32
|
+
RA_QUANT,
|
|
33
|
+
RA_BOL,
|
|
34
|
+
RA_EOL,
|
|
35
|
+
RA_WORDB,
|
|
36
|
+
RA_NWORDB
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
typedef struct ra_range {
|
|
40
|
+
uint32_t lo;
|
|
41
|
+
uint32_t hi;
|
|
42
|
+
} ra_range;
|
|
43
|
+
|
|
44
|
+
typedef struct ra ra;
|
|
45
|
+
struct ra {
|
|
46
|
+
int kind;
|
|
47
|
+
uint32_t cp; /* RA_CHAR */
|
|
48
|
+
bool ci; /* RA_CHAR: folded ASCII letter, match both cases */
|
|
49
|
+
ra_range *ranges; /* RA_CLASS */
|
|
50
|
+
int n_ranges;
|
|
51
|
+
bool negated; /* RA_CLASS */
|
|
52
|
+
ra **seq; /* RA_SEQ branches / RA_ALT branches */
|
|
53
|
+
int n;
|
|
54
|
+
ra *child; /* RA_QUANT */
|
|
55
|
+
long qmin, qmax; /* RA_QUANT; qmax -1 = unbounded */
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
typedef struct {
|
|
59
|
+
teptris_arena *a;
|
|
60
|
+
const uint32_t *s;
|
|
61
|
+
size_t n;
|
|
62
|
+
size_t pos;
|
|
63
|
+
bool fold;
|
|
64
|
+
char *err;
|
|
65
|
+
size_t err_cap;
|
|
66
|
+
} pctx;
|
|
67
|
+
|
|
68
|
+
static void pfail(pctx *c, const char *msg) {
|
|
69
|
+
if (c->err != NULL && c->err[0] == '\0') {
|
|
70
|
+
snprintf(c->err, c->err_cap, "%s", msg);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static ra *rn(pctx *c, int kind) {
|
|
75
|
+
ra *r = teptris_arena_alloc(c->a, sizeof(ra));
|
|
76
|
+
*r = (ra){ .kind = kind };
|
|
77
|
+
return r;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static uint32_t fold_cp(uint32_t cp) {
|
|
81
|
+
if (cp >= 'A' && cp <= 'Z') return cp + 32;
|
|
82
|
+
return cp;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static bool is_word_cp(uint32_t cp) {
|
|
86
|
+
if (cp >= 'a' && cp <= 'z') return true;
|
|
87
|
+
if (cp >= 'A' && cp <= 'Z') return true;
|
|
88
|
+
if (cp >= '0' && cp <= '9') return true;
|
|
89
|
+
return cp == '_';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static void utf8_to_cp(pctx *c, const char *src, size_t len) {
|
|
93
|
+
size_t count = 0;
|
|
94
|
+
for (size_t i = 0; i < len;) {
|
|
95
|
+
unsigned char b = (unsigned char)src[i];
|
|
96
|
+
size_t step = b < 0x80 ? 1
|
|
97
|
+
: b < 0xE0 ? 2
|
|
98
|
+
: b < 0xF0 ? 3
|
|
99
|
+
: 4;
|
|
100
|
+
i += step > len - i ? 1 : step;
|
|
101
|
+
count++;
|
|
102
|
+
}
|
|
103
|
+
uint32_t *cps = teptris_arena_alloc(c->a, (count + 1) * sizeof(uint32_t));
|
|
104
|
+
size_t o = 0;
|
|
105
|
+
for (size_t i = 0; i < len;) {
|
|
106
|
+
unsigned char b = (unsigned char)src[i];
|
|
107
|
+
uint32_t cp;
|
|
108
|
+
size_t step;
|
|
109
|
+
if (b < 0x80) {
|
|
110
|
+
cp = b;
|
|
111
|
+
step = 1;
|
|
112
|
+
} else if ((b & 0xE0) == 0xC0 && i + 1 < len &&
|
|
113
|
+
((unsigned char)src[i + 1] & 0xC0) == 0x80) {
|
|
114
|
+
cp = ((uint32_t)(b & 0x1F)) << 6 | ((unsigned char)src[i + 1] & 0x3F);
|
|
115
|
+
step = 2;
|
|
116
|
+
} else if ((b & 0xF0) == 0xE0 && i + 2 < len &&
|
|
117
|
+
((unsigned char)src[i + 1] & 0xC0) == 0x80 &&
|
|
118
|
+
((unsigned char)src[i + 2] & 0xC0) == 0x80) {
|
|
119
|
+
cp = ((uint32_t)(b & 0x0F)) << 12 |
|
|
120
|
+
((uint32_t)((unsigned char)src[i + 1] & 0x3F)) << 6 |
|
|
121
|
+
((unsigned char)src[i + 2] & 0x3F);
|
|
122
|
+
step = 3;
|
|
123
|
+
} else if ((b & 0xF8) == 0xF0 && i + 3 < len &&
|
|
124
|
+
((unsigned char)src[i + 1] & 0xC0) == 0x80 &&
|
|
125
|
+
((unsigned char)src[i + 2] & 0xC0) == 0x80 &&
|
|
126
|
+
((unsigned char)src[i + 3] & 0xC0) == 0x80) {
|
|
127
|
+
cp = ((uint32_t)(b & 0x07)) << 18 |
|
|
128
|
+
((uint32_t)((unsigned char)src[i + 1] & 0x3F)) << 12 |
|
|
129
|
+
((uint32_t)((unsigned char)src[i + 2] & 0x3F)) << 6 |
|
|
130
|
+
((unsigned char)src[i + 3] & 0x3F);
|
|
131
|
+
step = 4;
|
|
132
|
+
} else {
|
|
133
|
+
cp = 0xFFFD; /* invalid byte: one replacement code point */
|
|
134
|
+
step = 1;
|
|
135
|
+
}
|
|
136
|
+
cps[o++] = cp;
|
|
137
|
+
i += step;
|
|
138
|
+
}
|
|
139
|
+
c->s = cps;
|
|
140
|
+
c->n = o;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static int jhexv(uint32_t cp) {
|
|
144
|
+
if (cp >= '0' && cp <= '9') return (int)(cp - '0');
|
|
145
|
+
if (cp >= 'a' && cp <= 'f') return (int)(cp - 'a' + 10);
|
|
146
|
+
if (cp >= 'A' && cp <= 'F') return (int)(cp - 'A' + 10);
|
|
147
|
+
return -1;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/* add a range under the fold flag: ranges intersecting ASCII letters
|
|
151
|
+
* add their case-swapped mirror so both cases always match */
|
|
152
|
+
static void cls_add(pctx *c, ra *cl, uint32_t lo, uint32_t hi) {
|
|
153
|
+
if (c->fold) {
|
|
154
|
+
/* an uppercase intersection mirrors DOWN (fold_cp); a
|
|
155
|
+
* lowercase intersection mirrors UP (+32) */
|
|
156
|
+
if (lo <= 'Z' && hi >= 'A') {
|
|
157
|
+
uint32_t mlo = lo < 'A' ? 'A' : lo;
|
|
158
|
+
uint32_t mhi = hi > 'Z' ? 'Z' : hi;
|
|
159
|
+
if (mlo <= mhi) {
|
|
160
|
+
cl->ranges[cl->n_ranges].lo = fold_cp(mlo);
|
|
161
|
+
cl->ranges[cl->n_ranges].hi = fold_cp(mhi);
|
|
162
|
+
cl->n_ranges++;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (lo <= 'z' && hi >= 'a') {
|
|
166
|
+
uint32_t mlo = lo < 'a' ? 'a' : lo;
|
|
167
|
+
uint32_t mhi = hi > 'z' ? 'z' : hi;
|
|
168
|
+
if (mlo <= mhi) {
|
|
169
|
+
cl->ranges[cl->n_ranges].lo = mlo - 32; /* raised */
|
|
170
|
+
cl->ranges[cl->n_ranges].hi = mhi - 32;
|
|
171
|
+
cl->n_ranges++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
cl->ranges[cl->n_ranges].lo = lo;
|
|
176
|
+
cl->ranges[cl->n_ranges].hi = hi;
|
|
177
|
+
cl->n_ranges++;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* a single code point under fold: the folded char (plus the original
|
|
181
|
+
* when the two differ) */
|
|
182
|
+
static void cls_add_cp(pctx *c, ra *cl, uint32_t cp) {
|
|
183
|
+
if (c->fold && cp >= 'A' && cp <= 'Z') {
|
|
184
|
+
cl->ranges[cl->n_ranges].lo = cp;
|
|
185
|
+
cl->ranges[cl->n_ranges].hi = cp;
|
|
186
|
+
cl->n_ranges++;
|
|
187
|
+
cl->ranges[cl->n_ranges].lo = fold_cp(cp);
|
|
188
|
+
cl->ranges[cl->n_ranges].hi = fold_cp(cp);
|
|
189
|
+
cl->n_ranges++;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (c->fold && cp >= 'a' && cp <= 'z') {
|
|
193
|
+
cl->ranges[cl->n_ranges].lo = fold_cp(cp);
|
|
194
|
+
cl->ranges[cl->n_ranges].hi = fold_cp(cp);
|
|
195
|
+
cl->n_ranges++;
|
|
196
|
+
}
|
|
197
|
+
cl->ranges[cl->n_ranges].lo = cp;
|
|
198
|
+
cl->ranges[cl->n_ranges].hi = cp;
|
|
199
|
+
cl->n_ranges++;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* expand a class shorthand (\d \w \s) into its ranges */
|
|
203
|
+
static void cls_shorthand(pctx *c, ra *cl, uint32_t e) {
|
|
204
|
+
if (e == 'd' || e == 'D') {
|
|
205
|
+
cl->ranges[cl->n_ranges].lo = '0';
|
|
206
|
+
cl->ranges[cl->n_ranges].hi = '9';
|
|
207
|
+
cl->n_ranges++;
|
|
208
|
+
} else if (e == 'w' || e == 'W') {
|
|
209
|
+
cls_add(c, cl, 'a', 'z');
|
|
210
|
+
cls_add(c, cl, 'A', 'Z');
|
|
211
|
+
cls_add(c, cl, '0', '9');
|
|
212
|
+
cls_add(c, cl, '_', '_');
|
|
213
|
+
} else {
|
|
214
|
+
cls_add_cp(c, cl, ' ');
|
|
215
|
+
cls_add_cp(c, cl, '\t');
|
|
216
|
+
cls_add_cp(c, cl, '\n');
|
|
217
|
+
cls_add_cp(c, cl, '\r');
|
|
218
|
+
cls_add_cp(c, cl, '\f');
|
|
219
|
+
cls_add_cp(c, cl, '\v');
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static void cls_reserve(pctx *c, ra *cl, size_t cap) {
|
|
224
|
+
void *nr = teptris_arena_alloc(c->a, cap * sizeof(*cl->ranges));
|
|
225
|
+
if (cl->n_ranges > 0) {
|
|
226
|
+
memcpy(nr, cl->ranges, (size_t)cl->n_ranges * sizeof(*cl->ranges));
|
|
227
|
+
}
|
|
228
|
+
cl->ranges = nr;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/* class escapes: returns the code point, or -1 for a shorthand handled
|
|
232
|
+
* here, or -2 on error */
|
|
233
|
+
static long class_escape(pctx *c, ra *cl) {
|
|
234
|
+
if (c->pos >= c->n) {
|
|
235
|
+
pfail(c, "trailing backslash in pattern");
|
|
236
|
+
return -2;
|
|
237
|
+
}
|
|
238
|
+
uint32_t e = c->s[c->pos];
|
|
239
|
+
switch (e) {
|
|
240
|
+
case 'd': case 'D': case 'w': case 'W': case 's': case 'S':
|
|
241
|
+
c->pos++;
|
|
242
|
+
cls_shorthand(c, cl, e);
|
|
243
|
+
return -1;
|
|
244
|
+
case 'x': {
|
|
245
|
+
c->pos++;
|
|
246
|
+
if (c->pos + 2 > c->n) {
|
|
247
|
+
pfail(c, "incomplete \\x escape");
|
|
248
|
+
return -2;
|
|
249
|
+
}
|
|
250
|
+
int hi = jhexv(c->s[c->pos]);
|
|
251
|
+
int lo = jhexv(c->s[c->pos + 1]);
|
|
252
|
+
if (hi < 0 || lo < 0) {
|
|
253
|
+
pfail(c, "invalid \\x escape");
|
|
254
|
+
return -2;
|
|
255
|
+
}
|
|
256
|
+
c->pos += 2;
|
|
257
|
+
return (long)(hi * 16 + lo);
|
|
258
|
+
}
|
|
259
|
+
case 'u': {
|
|
260
|
+
c->pos++;
|
|
261
|
+
if (c->pos + 4 > c->n) {
|
|
262
|
+
pfail(c, "incomplete \\u escape");
|
|
263
|
+
return -2;
|
|
264
|
+
}
|
|
265
|
+
long cp = 0;
|
|
266
|
+
for (int k = 0; k < 4; k++) {
|
|
267
|
+
int d = jhexv(c->s[c->pos + (size_t)k]);
|
|
268
|
+
if (d < 0) {
|
|
269
|
+
pfail(c, "invalid \\u escape");
|
|
270
|
+
return -2;
|
|
271
|
+
}
|
|
272
|
+
cp = cp * 16 + d;
|
|
273
|
+
}
|
|
274
|
+
c->pos += 4;
|
|
275
|
+
return cp;
|
|
276
|
+
}
|
|
277
|
+
case 'n': c->pos++; return '\n';
|
|
278
|
+
case 'r': c->pos++; return '\r';
|
|
279
|
+
case 't': c->pos++; return '\t';
|
|
280
|
+
case 'f': c->pos++; return '\f';
|
|
281
|
+
case 'v': c->pos++; return '\v';
|
|
282
|
+
case '0': c->pos++; return '\0';
|
|
283
|
+
default:
|
|
284
|
+
if (e >= '1' && e <= '9') {
|
|
285
|
+
pfail(c, "backreferences are not supported");
|
|
286
|
+
return -2;
|
|
287
|
+
}
|
|
288
|
+
c->pos++;
|
|
289
|
+
return (long)e;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/* parse the body of a [...]; c->pos sits after '[' */
|
|
294
|
+
static ra *rclass(pctx *c) {
|
|
295
|
+
ra *cl = rn(c, RA_CLASS);
|
|
296
|
+
if (c->pos < c->n && c->s[c->pos] == '^') {
|
|
297
|
+
cl->negated = true;
|
|
298
|
+
c->pos++;
|
|
299
|
+
}
|
|
300
|
+
size_t cap = 8;
|
|
301
|
+
cls_reserve(c, cl, cap);
|
|
302
|
+
bool first = true;
|
|
303
|
+
while (c->pos < c->n) {
|
|
304
|
+
if (c->s[c->pos] == ']' && !first) {
|
|
305
|
+
c->pos++;
|
|
306
|
+
return cl;
|
|
307
|
+
}
|
|
308
|
+
first = false;
|
|
309
|
+
long lo;
|
|
310
|
+
if (c->s[c->pos] == '\\') {
|
|
311
|
+
c->pos++;
|
|
312
|
+
lo = class_escape(c, cl);
|
|
313
|
+
if (lo == -2) return NULL;
|
|
314
|
+
if (lo == -1) continue; /* shorthand expanded */
|
|
315
|
+
} else {
|
|
316
|
+
lo = (long)c->s[c->pos++];
|
|
317
|
+
}
|
|
318
|
+
if (c->pos + 1 < c->n && c->s[c->pos] == '-' && c->s[c->pos + 1] != ']') {
|
|
319
|
+
c->pos++;
|
|
320
|
+
long hi;
|
|
321
|
+
if (c->s[c->pos] == '\\') {
|
|
322
|
+
c->pos++;
|
|
323
|
+
hi = class_escape(c, cl);
|
|
324
|
+
if (hi == -2) return NULL;
|
|
325
|
+
if (hi == -1) {
|
|
326
|
+
pfail(c, "class shorthand as a range endpoint");
|
|
327
|
+
return NULL;
|
|
328
|
+
}
|
|
329
|
+
} else {
|
|
330
|
+
hi = (long)c->s[c->pos++];
|
|
331
|
+
}
|
|
332
|
+
if (hi < lo) {
|
|
333
|
+
pfail(c, "class range out of order");
|
|
334
|
+
return NULL;
|
|
335
|
+
}
|
|
336
|
+
if ((size_t)cl->n_ranges + 2 >= cap) {
|
|
337
|
+
cap *= 2;
|
|
338
|
+
cls_reserve(c, cl, cap);
|
|
339
|
+
}
|
|
340
|
+
cls_add(c, cl, (uint32_t)lo, (uint32_t)hi);
|
|
341
|
+
} else {
|
|
342
|
+
if ((size_t)cl->n_ranges + 2 >= cap) {
|
|
343
|
+
cap *= 2;
|
|
344
|
+
cls_reserve(c, cl, cap);
|
|
345
|
+
}
|
|
346
|
+
cls_add_cp(c, cl, (uint32_t)lo);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
pfail(c, "unterminated character class");
|
|
350
|
+
return NULL;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/* parse a quantifier after an atom: 1 set, 0 none (position restored),
|
|
354
|
+
* -1 error */
|
|
355
|
+
static int rquant(pctx *c, long *qmin, long *qmax) {
|
|
356
|
+
if (c->pos >= c->n) return 0;
|
|
357
|
+
uint32_t ch = c->s[c->pos];
|
|
358
|
+
if (ch == '*') {
|
|
359
|
+
c->pos++;
|
|
360
|
+
*qmin = 0; *qmax = -1;
|
|
361
|
+
} else if (ch == '+') {
|
|
362
|
+
c->pos++;
|
|
363
|
+
*qmin = 1; *qmax = -1;
|
|
364
|
+
} else if (ch == '?') {
|
|
365
|
+
c->pos++;
|
|
366
|
+
*qmin = 0; *qmax = 1;
|
|
367
|
+
} else if (ch == '{') {
|
|
368
|
+
size_t save = c->pos;
|
|
369
|
+
c->pos++;
|
|
370
|
+
long lo = 0;
|
|
371
|
+
bool any = false;
|
|
372
|
+
while (c->pos < c->n && c->s[c->pos] >= '0' && c->s[c->pos] <= '9') {
|
|
373
|
+
lo = lo * 10 + (long)(c->s[c->pos] - '0');
|
|
374
|
+
c->pos++;
|
|
375
|
+
any = true;
|
|
376
|
+
}
|
|
377
|
+
if (!any) {
|
|
378
|
+
c->pos = save;
|
|
379
|
+
return 0; /* a literal '{' */
|
|
380
|
+
}
|
|
381
|
+
if (c->pos < c->n && c->s[c->pos] == '}') {
|
|
382
|
+
c->pos++;
|
|
383
|
+
*qmin = lo; *qmax = lo;
|
|
384
|
+
} else if (c->pos < c->n && c->s[c->pos] == ',') {
|
|
385
|
+
c->pos++;
|
|
386
|
+
if (c->pos < c->n && c->s[c->pos] == '}') {
|
|
387
|
+
c->pos++;
|
|
388
|
+
*qmin = lo; *qmax = -1;
|
|
389
|
+
} else {
|
|
390
|
+
long hi = 0;
|
|
391
|
+
bool anyh = false;
|
|
392
|
+
while (c->pos < c->n && c->s[c->pos] >= '0' &&
|
|
393
|
+
c->s[c->pos] <= '9') {
|
|
394
|
+
hi = hi * 10 + (long)(c->s[c->pos] - '0');
|
|
395
|
+
c->pos++;
|
|
396
|
+
anyh = true;
|
|
397
|
+
}
|
|
398
|
+
if (!anyh || c->pos >= c->n || c->s[c->pos] != '}') {
|
|
399
|
+
c->pos = save;
|
|
400
|
+
return 0;
|
|
401
|
+
}
|
|
402
|
+
c->pos++;
|
|
403
|
+
*qmin = lo; *qmax = hi;
|
|
404
|
+
if (*qmax < *qmin) {
|
|
405
|
+
pfail(c, "quantifier range out of order");
|
|
406
|
+
return -1;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
} else {
|
|
410
|
+
c->pos = save;
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
} else {
|
|
414
|
+
return 0;
|
|
415
|
+
}
|
|
416
|
+
if (c->pos < c->n && c->s[c->pos] == '?') {
|
|
417
|
+
c->pos++; /* lazy: no semantic difference for boolean matching */
|
|
418
|
+
}
|
|
419
|
+
return 1;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
static ra *ralternation(pctx *c);
|
|
423
|
+
|
|
424
|
+
static ra *ratom(pctx *c) {
|
|
425
|
+
if (c->pos >= c->n) return NULL;
|
|
426
|
+
uint32_t ch = c->s[c->pos];
|
|
427
|
+
switch (ch) {
|
|
428
|
+
case ')':
|
|
429
|
+
return NULL; /* the enclosing group's terminator */
|
|
430
|
+
case '^':
|
|
431
|
+
c->pos++;
|
|
432
|
+
return rn(c, RA_BOL);
|
|
433
|
+
case '$':
|
|
434
|
+
c->pos++;
|
|
435
|
+
return rn(c, RA_EOL);
|
|
436
|
+
case '.':
|
|
437
|
+
c->pos++;
|
|
438
|
+
return rn(c, RA_ANY);
|
|
439
|
+
case '[':
|
|
440
|
+
c->pos++;
|
|
441
|
+
return rclass(c);
|
|
442
|
+
case '(': {
|
|
443
|
+
c->pos++;
|
|
444
|
+
if (c->pos < c->n && c->s[c->pos] == '?') {
|
|
445
|
+
c->pos++;
|
|
446
|
+
if (c->pos < c->n && c->s[c->pos] == ':') {
|
|
447
|
+
c->pos++; /* non-capturing */
|
|
448
|
+
} else if (c->pos < c->n &&
|
|
449
|
+
(c->s[c->pos] == '=' || c->s[c->pos] == '!')) {
|
|
450
|
+
pfail(c, "lookahead is not supported");
|
|
451
|
+
return NULL;
|
|
452
|
+
} else if (c->pos < c->n && c->s[c->pos] == '<') {
|
|
453
|
+
if (c->pos + 1 < c->n &&
|
|
454
|
+
(c->s[c->pos + 1] == '=' || c->s[c->pos + 1] == '!')) {
|
|
455
|
+
pfail(c, "lookbehind is not supported");
|
|
456
|
+
return NULL;
|
|
457
|
+
}
|
|
458
|
+
while (c->pos < c->n && c->s[c->pos] != '>') c->pos++;
|
|
459
|
+
if (c->pos >= c->n) {
|
|
460
|
+
pfail(c, "unterminated group name");
|
|
461
|
+
return NULL;
|
|
462
|
+
}
|
|
463
|
+
c->pos++;
|
|
464
|
+
} else {
|
|
465
|
+
pfail(c, "inline flags other than a leading (?i) are not supported");
|
|
466
|
+
return NULL;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
ra *g = rn(c, RA_ALT);
|
|
470
|
+
ra *body = ralternation(c);
|
|
471
|
+
if (body == NULL && c->err[0] != '\0') return NULL;
|
|
472
|
+
g->seq = teptris_arena_alloc(c->a, sizeof(ra *));
|
|
473
|
+
g->seq[0] = body != NULL ? body : rn(c, RA_SEQ); /* () = empty seq */
|
|
474
|
+
g->n = 1;
|
|
475
|
+
if (c->pos >= c->n || c->s[c->pos] != ')') {
|
|
476
|
+
pfail(c, "unterminated group");
|
|
477
|
+
return NULL;
|
|
478
|
+
}
|
|
479
|
+
c->pos++;
|
|
480
|
+
return g;
|
|
481
|
+
}
|
|
482
|
+
case '\\': {
|
|
483
|
+
c->pos++;
|
|
484
|
+
if (c->pos >= c->n) {
|
|
485
|
+
pfail(c, "trailing backslash in pattern");
|
|
486
|
+
return NULL;
|
|
487
|
+
}
|
|
488
|
+
uint32_t e = c->s[c->pos];
|
|
489
|
+
if (e == 'd' || e == 'w' || e == 's' || e == 'D' || e == 'W' ||
|
|
490
|
+
e == 'S') {
|
|
491
|
+
c->pos++;
|
|
492
|
+
ra *cl = rn(c, RA_CLASS);
|
|
493
|
+
cl->negated = (e == 'D' || e == 'W' || e == 'S');
|
|
494
|
+
cls_reserve(c, cl, 8);
|
|
495
|
+
cls_shorthand(c, cl, e);
|
|
496
|
+
return cl;
|
|
497
|
+
}
|
|
498
|
+
if (e == 'b') {
|
|
499
|
+
c->pos++;
|
|
500
|
+
return rn(c, RA_WORDB);
|
|
501
|
+
}
|
|
502
|
+
if (e == 'B') {
|
|
503
|
+
c->pos++;
|
|
504
|
+
return rn(c, RA_NWORDB);
|
|
505
|
+
}
|
|
506
|
+
long cp = class_escape(c, NULL);
|
|
507
|
+
if (cp == -2) return NULL;
|
|
508
|
+
if (cp == -1) {
|
|
509
|
+
pfail(c, "class shorthand outside a class");
|
|
510
|
+
return NULL;
|
|
511
|
+
}
|
|
512
|
+
ra *r = rn(c, RA_CHAR);
|
|
513
|
+
r->cp = c->fold ? fold_cp((uint32_t)cp) : (uint32_t)cp;
|
|
514
|
+
r->ci = c->fold && cp >= 'a' && cp <= 'z';
|
|
515
|
+
return r;
|
|
516
|
+
}
|
|
517
|
+
default: {
|
|
518
|
+
c->pos++;
|
|
519
|
+
ra *r = rn(c, RA_CHAR);
|
|
520
|
+
r->cp = c->fold ? fold_cp(ch) : ch;
|
|
521
|
+
r->ci = c->fold && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
|
|
522
|
+
return r;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
static ra *rrepeat(pctx *c) {
|
|
528
|
+
ra *atom = ratom(c);
|
|
529
|
+
if (atom == NULL) return NULL;
|
|
530
|
+
long qmin, qmax;
|
|
531
|
+
int st = rquant(c, &qmin, &qmax);
|
|
532
|
+
if (st < 0) return NULL;
|
|
533
|
+
if (st == 0) return atom;
|
|
534
|
+
if (atom->kind == RA_BOL || atom->kind == RA_EOL ||
|
|
535
|
+
atom->kind == RA_WORDB || atom->kind == RA_NWORDB) {
|
|
536
|
+
pfail(c, "cannot quantify an anchor");
|
|
537
|
+
return NULL;
|
|
538
|
+
}
|
|
539
|
+
ra *q = rn(c, RA_QUANT);
|
|
540
|
+
q->child = atom;
|
|
541
|
+
q->qmin = qmin;
|
|
542
|
+
q->qmax = qmax;
|
|
543
|
+
return q;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
static ra *rconcat(pctx *c) {
|
|
547
|
+
size_t cap = 8;
|
|
548
|
+
size_t n = 0;
|
|
549
|
+
ra **seq = teptris_arena_alloc(c->a, cap * sizeof(*seq));
|
|
550
|
+
for (;;) {
|
|
551
|
+
/* alternation splits ABOVE concat: stop at the bar */
|
|
552
|
+
if (c->pos < c->n && c->s[c->pos] == '|') break;
|
|
553
|
+
ra *piece = rrepeat(c);
|
|
554
|
+
if (piece == NULL) {
|
|
555
|
+
if (c->err[0] != '\0') return NULL;
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
if (n == cap) {
|
|
559
|
+
cap *= 2;
|
|
560
|
+
void *ns = teptris_arena_alloc(c->a, cap * sizeof(*seq));
|
|
561
|
+
memcpy(ns, seq, n * sizeof(*seq));
|
|
562
|
+
seq = ns;
|
|
563
|
+
}
|
|
564
|
+
seq[n++] = piece;
|
|
565
|
+
}
|
|
566
|
+
ra *r = rn(c, RA_SEQ);
|
|
567
|
+
r->seq = seq;
|
|
568
|
+
r->n = (int)n;
|
|
569
|
+
return r;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
static ra *ralternation(pctx *c) {
|
|
573
|
+
ra *first = rconcat(c);
|
|
574
|
+
if (first == NULL) return NULL;
|
|
575
|
+
if (c->pos >= c->n || c->s[c->pos] != '|') return first;
|
|
576
|
+
size_t cap = 4;
|
|
577
|
+
size_t n = 0;
|
|
578
|
+
ra **branches = teptris_arena_alloc(c->a, cap * sizeof(*branches));
|
|
579
|
+
branches[n++] = first;
|
|
580
|
+
while (c->pos < c->n && c->s[c->pos] == '|') {
|
|
581
|
+
c->pos++;
|
|
582
|
+
ra *b = rconcat(c);
|
|
583
|
+
if (b == NULL) return NULL;
|
|
584
|
+
if (n == cap) {
|
|
585
|
+
cap *= 2;
|
|
586
|
+
void *nb = teptris_arena_alloc(c->a, cap * sizeof(*branches));
|
|
587
|
+
memcpy(nb, branches, n * sizeof(*branches));
|
|
588
|
+
branches = nb;
|
|
589
|
+
}
|
|
590
|
+
branches[n++] = b;
|
|
591
|
+
}
|
|
592
|
+
ra *r = rn(c, RA_ALT);
|
|
593
|
+
r->seq = branches;
|
|
594
|
+
r->n = (int)n;
|
|
595
|
+
return r;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/* -------------------------------------------------------- NFA compile -- */
|
|
599
|
+
|
|
600
|
+
enum { RS_CHAR, RS_SPLIT, RS_MATCH, RS_BOL, RS_EOL, RS_WORDB, RS_NWORDB };
|
|
601
|
+
|
|
602
|
+
typedef struct {
|
|
603
|
+
int kind;
|
|
604
|
+
bool any;
|
|
605
|
+
ra_range *ranges;
|
|
606
|
+
int n_ranges;
|
|
607
|
+
bool negated;
|
|
608
|
+
int a, b;
|
|
609
|
+
} rstate;
|
|
610
|
+
|
|
611
|
+
struct sch_regex {
|
|
612
|
+
rstate *states;
|
|
613
|
+
int n_states;
|
|
614
|
+
int start;
|
|
615
|
+
int match;
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
#define REGEX_MAX_STATES 20000
|
|
619
|
+
|
|
620
|
+
typedef struct {
|
|
621
|
+
teptris_arena *a;
|
|
622
|
+
rstate *states;
|
|
623
|
+
int n;
|
|
624
|
+
int cap;
|
|
625
|
+
char *err;
|
|
626
|
+
size_t err_cap;
|
|
627
|
+
} nctx;
|
|
628
|
+
|
|
629
|
+
static int nstate(nctx *c, int kind) {
|
|
630
|
+
if (c->n >= REGEX_MAX_STATES) {
|
|
631
|
+
if (c->err[0] == '\0') {
|
|
632
|
+
snprintf(c->err, c->err_cap, "pattern compiles to too many states");
|
|
633
|
+
}
|
|
634
|
+
return -1;
|
|
635
|
+
}
|
|
636
|
+
if (c->n == c->cap) {
|
|
637
|
+
c->cap = c->cap == 0 ? 64 : c->cap * 2;
|
|
638
|
+
void *ns = teptris_arena_alloc(c->a, (size_t)c->cap * sizeof(rstate));
|
|
639
|
+
if (c->n > 0) memcpy(ns, c->states, (size_t)c->n * sizeof(rstate));
|
|
640
|
+
c->states = ns;
|
|
641
|
+
}
|
|
642
|
+
rstate *s = &c->states[c->n];
|
|
643
|
+
memset(s, 0, sizeof(*s));
|
|
644
|
+
s->kind = kind;
|
|
645
|
+
s->a = -1;
|
|
646
|
+
s->b = -1;
|
|
647
|
+
return c->n++;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/* The subgraph under construction is described by an ENTRY state (or
|
|
651
|
+
* -1 when no state exists yet: the piece is pure epsilon) and a
|
|
652
|
+
* DANGLING TAIL: out-slots that the next piece's entry (or, at the
|
|
653
|
+
* very end, the match state) will be patched into. The entry NEVER
|
|
654
|
+
* moves once set; later pieces only consume the tail. This shape makes
|
|
655
|
+
* the entry-loss bug class structurally impossible. */
|
|
656
|
+
typedef struct {
|
|
657
|
+
int state;
|
|
658
|
+
bool is_b;
|
|
659
|
+
} pslot;
|
|
660
|
+
|
|
661
|
+
typedef struct {
|
|
662
|
+
pslot *slots;
|
|
663
|
+
size_t n, cap;
|
|
664
|
+
} plist;
|
|
665
|
+
|
|
666
|
+
static void plist_add(nctx *c, plist *p, int state, bool is_b) {
|
|
667
|
+
if (p->n == p->cap) {
|
|
668
|
+
p->cap = p->cap == 0 ? 8 : p->cap * 2;
|
|
669
|
+
void *np = teptris_arena_alloc(c->a, p->cap * sizeof(pslot));
|
|
670
|
+
if (p->n > 0) memcpy(np, p->slots, p->n * sizeof(pslot));
|
|
671
|
+
p->slots = np;
|
|
672
|
+
}
|
|
673
|
+
p->slots[p->n].state = state;
|
|
674
|
+
p->slots[p->n].is_b = is_b;
|
|
675
|
+
p->n++;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
typedef struct {
|
|
679
|
+
int entry;
|
|
680
|
+
plist tail;
|
|
681
|
+
} frag;
|
|
682
|
+
|
|
683
|
+
static void frag_eps(frag *f) {
|
|
684
|
+
f->entry = -1;
|
|
685
|
+
memset(&f->tail, 0, sizeof(f->tail));
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
static void frag_thread(nctx *c, frag *f, int to) {
|
|
689
|
+
for (size_t i = 0; i < f->tail.n; i++) {
|
|
690
|
+
if (f->tail.slots[i].is_b) {
|
|
691
|
+
c->states[f->tail.slots[i].state].b = to;
|
|
692
|
+
} else {
|
|
693
|
+
c->states[f->tail.slots[i].state].a = to;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
static bool emit_node(nctx *c, ra *r, frag *f) {
|
|
699
|
+
switch (r->kind) {
|
|
700
|
+
case RA_CHAR: {
|
|
701
|
+
int s = nstate(c, RS_CHAR);
|
|
702
|
+
if (s < 0) return false;
|
|
703
|
+
if (r->ci) {
|
|
704
|
+
/* a folded letter matches both cases */
|
|
705
|
+
c->states[s].ranges = teptris_arena_alloc(c->a, 2 * sizeof(ra_range));
|
|
706
|
+
c->states[s].ranges[0].lo = r->cp;
|
|
707
|
+
c->states[s].ranges[0].hi = r->cp;
|
|
708
|
+
c->states[s].ranges[1].lo = fold_cp(r->cp) - 32;
|
|
709
|
+
c->states[s].ranges[1].hi = fold_cp(r->cp) - 32;
|
|
710
|
+
c->states[s].n_ranges = 2;
|
|
711
|
+
} else {
|
|
712
|
+
c->states[s].ranges = teptris_arena_alloc(c->a, sizeof(ra_range));
|
|
713
|
+
c->states[s].ranges[0].lo = r->cp;
|
|
714
|
+
c->states[s].ranges[0].hi = r->cp;
|
|
715
|
+
c->states[s].n_ranges = 1;
|
|
716
|
+
}
|
|
717
|
+
f->entry = s;
|
|
718
|
+
memset(&f->tail, 0, sizeof(f->tail));
|
|
719
|
+
plist_add(c, &f->tail, s, false);
|
|
720
|
+
return true;
|
|
721
|
+
}
|
|
722
|
+
case RA_ANY: {
|
|
723
|
+
int s = nstate(c, RS_CHAR);
|
|
724
|
+
if (s < 0) return false;
|
|
725
|
+
c->states[s].any = true;
|
|
726
|
+
f->entry = s;
|
|
727
|
+
memset(&f->tail, 0, sizeof(f->tail));
|
|
728
|
+
plist_add(c, &f->tail, s, false);
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
case RA_CLASS: {
|
|
732
|
+
int s = nstate(c, RS_CHAR);
|
|
733
|
+
if (s < 0) return false;
|
|
734
|
+
c->states[s].ranges = r->ranges;
|
|
735
|
+
c->states[s].n_ranges = r->n_ranges;
|
|
736
|
+
c->states[s].negated = r->negated;
|
|
737
|
+
f->entry = s;
|
|
738
|
+
memset(&f->tail, 0, sizeof(f->tail));
|
|
739
|
+
plist_add(c, &f->tail, s, false);
|
|
740
|
+
return true;
|
|
741
|
+
}
|
|
742
|
+
case RA_BOL: case RA_EOL: case RA_WORDB: case RA_NWORDB: {
|
|
743
|
+
int kind = r->kind == RA_BOL ? RS_BOL
|
|
744
|
+
: r->kind == RA_EOL ? RS_EOL
|
|
745
|
+
: r->kind == RA_WORDB ? RS_WORDB
|
|
746
|
+
: RS_NWORDB;
|
|
747
|
+
int s = nstate(c, kind);
|
|
748
|
+
if (s < 0) return false;
|
|
749
|
+
f->entry = s;
|
|
750
|
+
memset(&f->tail, 0, sizeof(f->tail));
|
|
751
|
+
plist_add(c, &f->tail, s, false);
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
case RA_SEQ: {
|
|
755
|
+
frag_eps(f);
|
|
756
|
+
for (int i = 0; i < r->n; i++) {
|
|
757
|
+
frag piece = {-1, {0}};
|
|
758
|
+
if (!emit_node(c, r->seq[i], &piece)) return false;
|
|
759
|
+
frag_thread(c, f, piece.entry);
|
|
760
|
+
if (piece.entry >= 0 && f->entry < 0) f->entry = piece.entry;
|
|
761
|
+
f->tail = piece.tail;
|
|
762
|
+
}
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
case RA_ALT: {
|
|
766
|
+
/* N-way: split0 threaded from the tail, routing a to branch 0
|
|
767
|
+
* and b to split1, which routes a to branch 1 and b to
|
|
768
|
+
* split2... the last branch hangs off the last split's b. */
|
|
769
|
+
if (r->n == 1) return emit_node(c, r->seq[0], f);
|
|
770
|
+
frag_eps(f);
|
|
771
|
+
plist merged;
|
|
772
|
+
memset(&merged, 0, sizeof(merged));
|
|
773
|
+
int split = nstate(c, RS_SPLIT);
|
|
774
|
+
if (split < 0) return false;
|
|
775
|
+
frag_thread(c, f, split);
|
|
776
|
+
if (f->entry < 0) f->entry = split;
|
|
777
|
+
int prev_split = split;
|
|
778
|
+
for (int i = 0; i < r->n; i++) {
|
|
779
|
+
frag b = {-1, {0}};
|
|
780
|
+
if (!emit_node(c, r->seq[i], &b)) return false;
|
|
781
|
+
/* emit_node may have grown (and re-allocated) the states
|
|
782
|
+
* array: all writes re-fetch c->states by INDEX, never via
|
|
783
|
+
* held pointers */
|
|
784
|
+
int use = -1;
|
|
785
|
+
bool use_b = false;
|
|
786
|
+
if (i == 0) {
|
|
787
|
+
use = prev_split;
|
|
788
|
+
use_b = false;
|
|
789
|
+
} else if (i == r->n - 1) {
|
|
790
|
+
use = prev_split;
|
|
791
|
+
use_b = true;
|
|
792
|
+
} else {
|
|
793
|
+
int s2 = nstate(c, RS_SPLIT);
|
|
794
|
+
if (s2 < 0) return false;
|
|
795
|
+
c->states[prev_split].b = s2;
|
|
796
|
+
use = s2;
|
|
797
|
+
use_b = false;
|
|
798
|
+
}
|
|
799
|
+
if (b.entry >= 0) {
|
|
800
|
+
if (use_b) {
|
|
801
|
+
c->states[use].b = b.entry;
|
|
802
|
+
} else {
|
|
803
|
+
c->states[use].a = b.entry;
|
|
804
|
+
}
|
|
805
|
+
} else {
|
|
806
|
+
/* an epsilon branch: its entry slot IS an exit */
|
|
807
|
+
plist_add(c, &merged, use, use_b);
|
|
808
|
+
}
|
|
809
|
+
for (size_t k = 0; k < b.tail.n; k++) {
|
|
810
|
+
plist_add(c, &merged, b.tail.slots[k].state,
|
|
811
|
+
b.tail.slots[k].is_b);
|
|
812
|
+
}
|
|
813
|
+
prev_split = use;
|
|
814
|
+
}
|
|
815
|
+
f->tail = merged;
|
|
816
|
+
return true;
|
|
817
|
+
}
|
|
818
|
+
case RA_QUANT: {
|
|
819
|
+
if (r->qmax == 0) {
|
|
820
|
+
frag_eps(f); /* {0} */
|
|
821
|
+
return true;
|
|
822
|
+
}
|
|
823
|
+
/* mandatory copies */
|
|
824
|
+
if (r->qmin >= 1) {
|
|
825
|
+
frag first = {-1, {0}};
|
|
826
|
+
if (!emit_node(c, r->child, &first)) return false;
|
|
827
|
+
frag_thread(c, f, first.entry);
|
|
828
|
+
if (f->entry < 0) f->entry = first.entry;
|
|
829
|
+
f->tail = first.tail;
|
|
830
|
+
for (long k = 1; k < r->qmin; k++) {
|
|
831
|
+
frag piece = {-1, {0}};
|
|
832
|
+
if (!emit_node(c, r->child, &piece)) return false;
|
|
833
|
+
frag_thread(c, f, piece.entry);
|
|
834
|
+
f->tail = piece.tail;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
long extra = r->qmax < 0 ? -1 : r->qmax - r->qmin;
|
|
838
|
+
if (extra < 0) {
|
|
839
|
+
/* the unbounded tail: split{body loops back, b exits} */
|
|
840
|
+
int split = nstate(c, RS_SPLIT);
|
|
841
|
+
if (split < 0) return false;
|
|
842
|
+
frag_thread(c, f, split);
|
|
843
|
+
frag body = {-1, {0}};
|
|
844
|
+
if (!emit_node(c, r->child, &body)) return false;
|
|
845
|
+
c->states[split].a = body.entry >= 0 ? body.entry : -1;
|
|
846
|
+
if (body.entry >= 0) {
|
|
847
|
+
frag_thread(c, &body, split); /* the loop back-edge */
|
|
848
|
+
} else {
|
|
849
|
+
/* an epsilon body (never for real atoms) loops forever:
|
|
850
|
+
* reject rather than emit a live-lock */
|
|
851
|
+
if (c->err[0] == '\0') {
|
|
852
|
+
snprintf(c->err, c->err_cap,
|
|
853
|
+
"quantified subexpression matches empty");
|
|
854
|
+
}
|
|
855
|
+
return false;
|
|
856
|
+
}
|
|
857
|
+
int entry = f->entry; /* frag_eps wipes it; the mandatory
|
|
858
|
+
copies' entry is preserved */
|
|
859
|
+
frag_eps(f);
|
|
860
|
+
f->entry = r->qmin >= 1 ? entry : split;
|
|
861
|
+
plist_add(c, &f->tail, split, true);
|
|
862
|
+
return true;
|
|
863
|
+
}
|
|
864
|
+
for (long k = 0; k < extra; k++) {
|
|
865
|
+
int split = nstate(c, RS_SPLIT);
|
|
866
|
+
if (split < 0) return false;
|
|
867
|
+
frag_thread(c, f, split);
|
|
868
|
+
frag body = {-1, {0}};
|
|
869
|
+
if (!emit_node(c, r->child, &body)) return false;
|
|
870
|
+
c->states[split].a = body.entry >= 0 ? body.entry : -1;
|
|
871
|
+
plist out;
|
|
872
|
+
memset(&out, 0, sizeof(out));
|
|
873
|
+
plist_add(c, &out, split, true);
|
|
874
|
+
for (size_t q = 0; q < body.tail.n; q++) {
|
|
875
|
+
plist_add(c, &out, body.tail.slots[q].state,
|
|
876
|
+
body.tail.slots[q].is_b);
|
|
877
|
+
}
|
|
878
|
+
f->tail = out;
|
|
879
|
+
if (f->entry < 0 && body.entry >= 0) {
|
|
880
|
+
f->entry = split; /* the optional chain is the entry */
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return true;
|
|
884
|
+
}
|
|
885
|
+
default:
|
|
886
|
+
snprintf(c->err, c->err_cap, "internal: unknown AST kind");
|
|
887
|
+
return false;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
struct sch_regex *sch_regex_compile(teptris_arena *a, const char *pattern,
|
|
892
|
+
size_t len, char *err, size_t err_cap) {
|
|
893
|
+
pctx pc;
|
|
894
|
+
memset(&pc, 0, sizeof(pc));
|
|
895
|
+
pc.a = a;
|
|
896
|
+
pc.err = err;
|
|
897
|
+
pc.err_cap = err_cap;
|
|
898
|
+
if (err != NULL && err_cap > 0) err[0] = '\0';
|
|
899
|
+
utf8_to_cp(&pc, pattern, len);
|
|
900
|
+
|
|
901
|
+
/* a leading (?i): ASCII case-fold the rest */
|
|
902
|
+
if (pc.n >= 4 && pc.s[0] == '(' && pc.s[1] == '?' && pc.s[2] == 'i' &&
|
|
903
|
+
pc.s[3] == ')') {
|
|
904
|
+
pc.fold = true;
|
|
905
|
+
pc.pos = 4;
|
|
906
|
+
if (pc.n >= 5 && pc.s[4] == ':') pc.pos = 5;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
ra *ast = ralternation(&pc);
|
|
910
|
+
if (ast == NULL) return NULL;
|
|
911
|
+
if (pc.pos != pc.n) {
|
|
912
|
+
if (err != NULL && err_cap > 0 && err[0] == '\0') {
|
|
913
|
+
snprintf(err, err_cap, "unexpected ')' in pattern");
|
|
914
|
+
}
|
|
915
|
+
return NULL;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
nctx nc;
|
|
919
|
+
memset(&nc, 0, sizeof(nc));
|
|
920
|
+
nc.a = a;
|
|
921
|
+
nc.err = err;
|
|
922
|
+
nc.err_cap = err_cap;
|
|
923
|
+
frag f = {-1, {0}};
|
|
924
|
+
if (!emit_node(&nc, ast, &f)) return NULL;
|
|
925
|
+
int match = nstate(&nc, RS_MATCH);
|
|
926
|
+
if (match < 0) return NULL;
|
|
927
|
+
frag_thread(&nc, &f, match);
|
|
928
|
+
|
|
929
|
+
struct sch_regex *re = teptris_arena_alloc(a, sizeof(*re));
|
|
930
|
+
re->states = nc.states;
|
|
931
|
+
re->n_states = nc.n;
|
|
932
|
+
re->match = match;
|
|
933
|
+
re->start = f.entry >= 0 ? f.entry : match; /* an empty pattern */
|
|
934
|
+
return re;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
static bool cls_match(const rstate *st, uint32_t cp) {
|
|
939
|
+
if (st->any) return true;
|
|
940
|
+
bool in = false;
|
|
941
|
+
for (int i = 0; i < st->n_ranges; i++) {
|
|
942
|
+
if (cp >= st->ranges[i].lo && cp <= st->ranges[i].hi) {
|
|
943
|
+
in = true;
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
return st->negated ? !in : in;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
typedef struct {
|
|
951
|
+
const uint32_t *s;
|
|
952
|
+
int len;
|
|
953
|
+
} sim_ctx;
|
|
954
|
+
|
|
955
|
+
/* epsilon closure of the open set at input position pos; set membership
|
|
956
|
+
* uses generation stamps (open == gen, closed == -gen) */
|
|
957
|
+
static void closure(sim_ctx *sc, const struct sch_regex *re, int *mark,
|
|
958
|
+
int gen, int *stack, int pos) {
|
|
959
|
+
int sp = 0;
|
|
960
|
+
for (int i = 0; i < re->n_states; i++) {
|
|
961
|
+
if (mark[i] == gen) stack[sp++] = i;
|
|
962
|
+
}
|
|
963
|
+
while (sp > 0) {
|
|
964
|
+
int s = stack[--sp];
|
|
965
|
+
const rstate *st = &re->states[s];
|
|
966
|
+
int targets[2];
|
|
967
|
+
int nt = 0;
|
|
968
|
+
if (st->kind == RS_SPLIT) {
|
|
969
|
+
targets[nt++] = st->a;
|
|
970
|
+
targets[nt++] = st->b;
|
|
971
|
+
} else if (st->kind == RS_BOL) {
|
|
972
|
+
if (pos == 0) targets[nt++] = st->a;
|
|
973
|
+
} else if (st->kind == RS_EOL) {
|
|
974
|
+
if (pos >= sc->len) targets[nt++] = st->a;
|
|
975
|
+
} else if (st->kind == RS_WORDB) {
|
|
976
|
+
bool before = pos > 0 && is_word_cp(sc->s[pos - 1]);
|
|
977
|
+
bool after = pos < sc->len && is_word_cp(sc->s[pos]);
|
|
978
|
+
if (before != after) targets[nt++] = st->a;
|
|
979
|
+
} else if (st->kind == RS_NWORDB) {
|
|
980
|
+
bool before = pos > 0 && is_word_cp(sc->s[pos - 1]);
|
|
981
|
+
bool after = pos < sc->len && is_word_cp(sc->s[pos]);
|
|
982
|
+
if (before == after) targets[nt++] = st->a;
|
|
983
|
+
}
|
|
984
|
+
for (int k = 0; k < nt; k++) {
|
|
985
|
+
int t = targets[k];
|
|
986
|
+
if (t >= 0 && mark[t] != gen && mark[t] != -gen) {
|
|
987
|
+
mark[t] = gen;
|
|
988
|
+
stack[sp++] = t;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
mark[s] = -gen; /* closed */
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
bool sch_regex_search(const struct sch_regex *re, const uint32_t *s, int len) {
|
|
996
|
+
if (re->n_states == 0) return false;
|
|
997
|
+
bool matched = false;
|
|
998
|
+
int *mark = calloc((size_t)re->n_states, sizeof(int));
|
|
999
|
+
int *stack = calloc((size_t)re->n_states + 1, sizeof(int));
|
|
1000
|
+
if (mark == NULL || stack == NULL) {
|
|
1001
|
+
free(mark);
|
|
1002
|
+
free(stack);
|
|
1003
|
+
return false;
|
|
1004
|
+
}
|
|
1005
|
+
sim_ctx sc = {s, len};
|
|
1006
|
+
int gen = 0;
|
|
1007
|
+
for (int start = 0; start <= len && !matched; start++) {
|
|
1008
|
+
gen++;
|
|
1009
|
+
if (re->start >= 0 && mark[re->start] != gen && mark[re->start] != -gen) {
|
|
1010
|
+
mark[re->start] = gen;
|
|
1011
|
+
} else if (re->start < 0) {
|
|
1012
|
+
/* the empty pattern: the match state is the entry */
|
|
1013
|
+
mark[re->match] = gen;
|
|
1014
|
+
}
|
|
1015
|
+
closure(&sc, re, mark, gen, stack, start);
|
|
1016
|
+
for (int pos = start; pos <= len; pos++) {
|
|
1017
|
+
if (mark[re->match] == gen || mark[re->match] == -gen) {
|
|
1018
|
+
matched = true;
|
|
1019
|
+
break;
|
|
1020
|
+
}
|
|
1021
|
+
if (pos >= len) break;
|
|
1022
|
+
gen++;
|
|
1023
|
+
for (int i = 0; i < re->n_states; i++) {
|
|
1024
|
+
if (mark[i] == gen - 1 || mark[i] == -(gen - 1)) {
|
|
1025
|
+
if (re->states[i].kind == RS_CHAR) {
|
|
1026
|
+
if (cls_match(&re->states[i], s[pos])) {
|
|
1027
|
+
int t = re->states[i].a;
|
|
1028
|
+
if (t >= 0 && mark[t] != gen && mark[t] != -gen) {
|
|
1029
|
+
mark[t] = gen;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
closure(&sc, re, mark, gen, stack, pos + 1);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
free(mark);
|
|
1039
|
+
free(stack);
|
|
1040
|
+
return matched;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
bool sch_regex_is_match(const struct sch_regex *re, const char *utf8,
|
|
1044
|
+
size_t len) {
|
|
1045
|
+
uint32_t stackbuf[64];
|
|
1046
|
+
uint32_t *cps = stackbuf;
|
|
1047
|
+
if (len > sizeof stackbuf / sizeof stackbuf[0]) {
|
|
1048
|
+
cps = malloc(len * sizeof(uint32_t));
|
|
1049
|
+
if (cps == NULL) return false;
|
|
1050
|
+
}
|
|
1051
|
+
size_t n = 0;
|
|
1052
|
+
size_t i = 0;
|
|
1053
|
+
while (i < len) {
|
|
1054
|
+
unsigned char b = (unsigned char)utf8[i];
|
|
1055
|
+
uint32_t cp;
|
|
1056
|
+
size_t step;
|
|
1057
|
+
if (b < 0x80) {
|
|
1058
|
+
cp = b;
|
|
1059
|
+
step = 1;
|
|
1060
|
+
} else if ((b & 0xE0) == 0xC0 && i + 1 < len &&
|
|
1061
|
+
((unsigned char)utf8[i + 1] & 0xC0) == 0x80) {
|
|
1062
|
+
cp = ((uint32_t)(b & 0x1F)) << 6 | ((unsigned char)utf8[i + 1] & 0x3F);
|
|
1063
|
+
step = 2;
|
|
1064
|
+
} else if ((b & 0xF0) == 0xE0 && i + 2 < len &&
|
|
1065
|
+
((unsigned char)utf8[i + 1] & 0xC0) == 0x80 &&
|
|
1066
|
+
((unsigned char)utf8[i + 2] & 0xC0) == 0x80) {
|
|
1067
|
+
cp = ((uint32_t)(b & 0x0F)) << 12 |
|
|
1068
|
+
((uint32_t)((unsigned char)utf8[i + 1] & 0x3F)) << 6 |
|
|
1069
|
+
((unsigned char)utf8[i + 2] & 0x3F);
|
|
1070
|
+
step = 3;
|
|
1071
|
+
} else if ((b & 0xF8) == 0xF0 && i + 3 < len &&
|
|
1072
|
+
((unsigned char)utf8[i + 1] & 0xC0) == 0x80 &&
|
|
1073
|
+
((unsigned char)utf8[i + 2] & 0xC0) == 0x80 &&
|
|
1074
|
+
((unsigned char)utf8[i + 3] & 0xC0) == 0x80) {
|
|
1075
|
+
cp = ((uint32_t)(b & 0x07)) << 18 |
|
|
1076
|
+
((uint32_t)((unsigned char)utf8[i + 1] & 0x3F)) << 12 |
|
|
1077
|
+
((uint32_t)((unsigned char)utf8[i + 2] & 0x3F)) << 6 |
|
|
1078
|
+
((unsigned char)utf8[i + 3] & 0x3F);
|
|
1079
|
+
step = 4;
|
|
1080
|
+
} else {
|
|
1081
|
+
cp = 0xFFFD;
|
|
1082
|
+
step = 1;
|
|
1083
|
+
}
|
|
1084
|
+
cps[n++] = cp;
|
|
1085
|
+
i += step;
|
|
1086
|
+
}
|
|
1087
|
+
bool m = sch_regex_search(re, cps, (int)n);
|
|
1088
|
+
if (cps != stackbuf) free(cps);
|
|
1089
|
+
return m;
|
|
1090
|
+
}
|