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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
/*
* vm.c - Code for the BP virtual machine that performs the matching.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "grammar.h"
#include "types.h"
#include "utils.h"
#include "vm.h"
// UTF8-compliant char iteration
static inline const char *next_char(file_t *f, const char *str)
{
char c = *str;
++str;
if (__builtin_expect(!(c & 0x80), 1))
return str;
if (__builtin_expect(str < f->end && !!(*str & 0x80), 1))
++str;
if (c > '\xDF' && __builtin_expect(str < f->end && !!(*str & 0x80), 1))
++str;
if (c > '\xEF' && __builtin_expect(str < f->end && !!(*str & 0x80), 1))
++str;
return str;
}
/*
* Recursively deallocate a match object and set to NULL
*/
void destroy_match(match_t **m)
{
if (!*m) return;
destroy_match(&((*m)->child));
destroy_match(&((*m)->nextsibling));
*m = NULL;
}
typedef struct recursive_ref_s {
const vm_op_t *op;
const char *pos;
struct recursive_ref_s *prev;
int hit;
match_t *result;
} recursive_ref_t;
/*
* Attempt to match text against a previously captured value.
* Return the character position after the backref has matched, or NULL if no match has occurred.
*/
static const char *match_backref(const char *str, vm_op_t *op, match_t *cap, unsigned int flags)
{
check(op->type == VM_BACKREF, "Attempt to match backref against something that's not a backref");
if (cap->op->type == VM_REPLACE) {
const char *text = cap->op->args.replace.text;
const char *end = &text[cap->op->args.replace.len];
for (const char *r = text; r < end; ) {
if (*r == '\\') {
++r;
if (*(str++) != unescapechar(r, &r))
return NULL;
} else if (*r != '@') {
if (*(str++) != *r)
return NULL;
++r;
continue;
}
++r;
match_t *value = get_capture(cap, &r);
if (value != NULL) {
str = match_backref(str, op, value, flags);
if (str == NULL) return NULL;
}
}
} else {
const char *prev = cap->start;
for (match_t *child = cap->child; child; child = child->nextsibling) {
if (child->start > prev) {
size_t len = (size_t)(child->start - prev);
if ((flags & BP_IGNORECASE) ? memicmp(str, prev, len) != 0
: memcmp(str, prev, len) != 0) {
return NULL;
}
str += len;
prev = child->start;
}
if (child->start < prev) continue;
str = match_backref(str, op, child, flags);
if (str == NULL) return NULL;
prev = child->end;
}
if (cap->end > prev) {
size_t len = (size_t)(cap->end - prev);
if ((flags & BP_IGNORECASE) ? memicmp(str, prev, len) != 0
: memcmp(str, prev, len) != 0) {
return NULL;
}
str += len;
}
}
return str;
}
/*
* Run virtual machine operation against a string and return
* a match struct, or NULL if no match is found.
* The returned value should be free()'d to avoid memory leaking.
*/
static match_t *_match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned int flags, recursive_ref_t *rec)
{
switch (op->type) {
case VM_ANYCHAR: {
if (str >= f->end || *str == '\n')
return NULL;
match_t *m = new(match_t);
m->op = op;
m->start = str;
m->end = next_char(f, str);
return m;
}
case VM_STRING: {
if (&str[op->len] > f->end) return NULL;
if ((flags & BP_IGNORECASE) ? memicmp(str, op->args.s, (size_t)op->len) != 0
: memcmp(str, op->args.s, (size_t)op->len) != 0)
return NULL;
match_t *m = new(match_t);
m->op = op;
m->start = str;
m->end = str + op->len;
return m;
}
case VM_RANGE: {
if (str >= f->end) return NULL;
if ((unsigned char)*str < op->args.range.low || (unsigned char)*str > op->args.range.high)
return NULL;
match_t *m = new(match_t);
m->op = op;
m->start = str;
m->end = str + 1;
return m;
}
case VM_NOT: {
match_t *m = _match(defs, f, str, op->args.pat, flags, rec);
if (m != NULL) {
destroy_match(&m);
return NULL;
}
m = new(match_t);
m->op = op;
m->start = str;
m->end = str;
return m;
}
case VM_UPTO_AND: {
match_t *m = new(match_t);
m->start = str;
m->op = op;
if (!op->args.multiple.first && !op->args.multiple.second) {
while (str < f->end && *str != '\n') ++str;
} else {
match_t **dest = &m->child;
for (const char *prev = NULL; prev < str; ) {
prev = str;
if (op->args.multiple.first) {
match_t *p = _match(defs, f, str, op->args.multiple.first, flags, rec);
if (p) {
*dest = p;
m->end = p->end;
return m;
}
} else if (str == f->end) {
m->end = str;
return m;
}
if (op->args.multiple.second) {
match_t *p = _match(defs, f, str, op->args.multiple.second, flags, rec);
if (p) {
*dest = p;
dest = &p->nextsibling;
str = p->end;
continue;
}
}
// This isn't in the for() structure because there needs to
// be at least once chance to match the pattern, even if
// we're at the end of the string already (e.g. "..$").
if (str < f->end && *str != '\n')
str = next_char(f, str);
}
destroy_match(&m);
return NULL;
}
m->end = str;
return m;
}
case VM_REPEAT: {
match_t *m = new(match_t);
m->start = str;
m->end = str;
m->op = op;
match_t **dest = &m->child;
size_t reps = 0;
ssize_t max = op->args.repetitions.max;
for (reps = 0; max == -1 || reps < (size_t)max; ++reps) {
const char *start = str;
// Separator
match_t *sep = NULL;
if (op->args.repetitions.sep != NULL && reps > 0) {
sep = _match(defs, f, str, op->args.repetitions.sep, flags, rec);
if (sep == NULL) break;
str = sep->end;
}
match_t *p = _match(defs, f, str, op->args.repetitions.repeat_pat, flags, rec);
if (p == NULL) {
str = start;
destroy_match(&sep);
break;
}
if (p->end == start && reps > 0) {
// Since no forward progress was made on either `pat` or
// `sep` and BP does not have mutable state, it's
// guaranteed that no progress will be made on the next
// loop either. We know that this will continue to loop
// until reps==max, so let's just cut to the chase instead
// of looping infinitely.
destroy_match(&sep);
destroy_match(&p);
if (op->args.repetitions.max == -1)
reps = ~(size_t)0;
else
reps = (size_t)op->args.repetitions.max;
break;
}
if (sep) {
*dest = sep;
dest = &sep->nextsibling;
}
*dest = p;
dest = &p->nextsibling;
str = p->end;
}
if (reps < (size_t)op->args.repetitions.min) {
destroy_match(&m);
return NULL;
}
m->end = str;
return m;
}
case VM_AFTER: {
ssize_t backtrack = op->args.pat->len;
check(backtrack != -1, "'<' is only allowed for fixed-length operations");
if (str - backtrack < f->contents) return NULL;
match_t *before = _match(defs, f, str - backtrack, op->args.pat, flags, rec);
if (before == NULL) return NULL;
match_t *m = new(match_t);
m->start = str;
m->end = str;
m->op = op;
m->child = before;
return m;
}
case VM_BEFORE: {
match_t *after = _match(defs, f, str, op->args.pat, flags, rec);
if (after == NULL) return NULL;
match_t *m = new(match_t);
m->start = str;
m->end = str;
m->op = op;
m->child = after;
return m;
}
case VM_CAPTURE: {
match_t *p = _match(defs, f, str, op->args.pat, flags, rec);
if (p == NULL) return NULL;
match_t *m = new(match_t);
m->start = str;
m->end = p->end;
m->op = op;
m->child = p;
return m;
}
case VM_HIDE: {
match_t *p = _match(defs, f, str, op->args.pat, flags, rec);
if (p == NULL) return NULL;
match_t *m = new(match_t);
m->start = str;
m->end = p->end;
m->op = op;
m->child = p;
return m;
}
case VM_OTHERWISE: {
match_t *m = _match(defs, f, str, op->args.multiple.first, flags, rec);
if (m == NULL) m = _match(defs, f, str, op->args.multiple.second, flags, rec);
return m;
}
case VM_CHAIN: {
match_t *m1 = _match(defs, f, str, op->args.multiple.first, flags, rec);
if (m1 == NULL) return NULL;
match_t *m2;
{ // Push backrefs and run matching, then cleanup
def_t *defs2 = with_backrefs(defs, f, m1);
m2 = _match(defs2, f, m1->end, op->args.multiple.second, flags, rec);
while (defs2 != defs) {
def_t *next = defs2->next;
defs2->next = NULL;
// Deliberate memory leak, if there is a match, then the op
// will be stored on the match and can't be freed here.
// There's currently no refcounting on ops but that should
// be how to prevent a memory leak from this.
// TODO: add refcounting to ops?
if (m2 == NULL) {
xfree(&defs2->op);
}
xfree(&defs2);
defs2 = next;
}
}
if (m2 == NULL) {
destroy_match(&m1);
return NULL;
}
match_t *m = new(match_t);
m->start = str;
m->end = m2->end;
m->op = op;
m->child = m1;
m1->nextsibling = m2;
return m;
}
case VM_EQUAL: case VM_NOT_EQUAL: {
match_t *m1 = _match(defs, f, str, op->args.multiple.first, flags, rec);
if (m1 == NULL) return NULL;
// <p1>==<p2> matches iff the text of <p1> matches <p2>
// <p1>!=<p2> matches iff the text of <p1> does not match <p2>
file_t inner = {
.filename=f->filename,
.contents=(char*)m1->start, .end=(char*)m1->end,
.lines=f->lines, // I think this works, but am not 100% sure
.nlines=1 + get_line_number(f, m1->end)-get_line_number(f, m1->start),
.mmapped=f->mmapped,
};
match_t *m2 = _match(defs, &inner, str, op->args.multiple.second, flags, rec);
if ((m2 == NULL) == (op->type == VM_EQUAL)) {
destroy_match(&m1);
destroy_match(&m2);
return NULL;
}
match_t *m = new(match_t);
m->start = m1->start;
m->end = m1->end;
m->op = op;
m->child = m1;
if (op->type == VM_EQUAL) {
m1->nextsibling = m2;
} else {
destroy_match(&m2);
}
return m;
}
case VM_REPLACE: {
match_t *p = NULL;
if (op->args.replace.pat) {
p = _match(defs, f, str, op->args.replace.pat, flags, rec);
if (p == NULL) return NULL;
}
match_t *m = new(match_t);
m->start = str;
m->op = op;
if (p) {
m->child = p;
m->end = p->end;
} else {
m->end = m->start;
}
return m;
}
case VM_REF: {
vm_op_t *r = lookup(defs, op->args.s);
check(r != NULL, "Unknown identifier: '%s'", op->args.s);
// Prevent infinite left recursion:
for (recursive_ref_t *p = rec; p; p = p->prev) {
if (p->pos == str && p->op == r) {
++p->hit;
return p->result;
}
}
recursive_ref_t wrap = {
.op = r,
.pos = str,
.prev = rec,
.hit = 0,
.result = NULL,
};
match_t *best = NULL;
left_recursive:;
match_t *p = _match(defs, f, str, r, flags, &wrap);
if (p == NULL) return best;
if (wrap.hit && (best == NULL || p->end > best->end)) {
best = p;
wrap.hit = 0;
wrap.result = p;
goto left_recursive;
} else if (best == NULL) {
best = p;
}
match_t *m = new(match_t);
m->start = best->start;
m->end = best->end;
m->op = op;
m->child = best;
return m;
}
case VM_BACKREF: {
const char *end = match_backref(str, op, op->args.backref, flags);
if (end == NULL) return NULL;
match_t *m = new(match_t);
m->op = op;
m->start = str;
m->end = end;
return m;
}
case VM_NODENT: {
if (*str != '\n') return NULL;
const char *start = str;
size_t linenum = get_line_number(f, str);
const char *p = get_line(f, linenum);
if (p < f->contents) p=f->contents; // Can happen with recursive matching
// Current indentation:
char denter = *p;
int dents = 0;
if (denter == ' ' || denter == '\t') {
for (; *p == denter && p < f->end; ++p) ++dents;
}
// Subsequent indentation:
while (*str == '\n') ++str;
for (int i = 0; i < dents; i++) {
if (str[i] != denter || &str[i] >= f->end) return NULL;
}
match_t *m = new(match_t);
m->start = start;
m->end = &str[dents];
m->op = op;
return m;
}
default: {
fprintf(stderr, "Unknown opcode: %d", op->type);
_exit(1);
return NULL;
}
}
}
/*
* Get a specific numbered pattern capture.
*/
static match_t *get_capture_by_num(match_t *m, int *n)
{
if (*n == 0) return m;
if (m->op->type == VM_CAPTURE && *n == 1) return m;
if (m->op->type == VM_CAPTURE) --(*n);
for (match_t *c = m->child; c; c = c->nextsibling) {
match_t *cap = get_capture_by_num(c, n);
if (cap) return cap;
}
return NULL;
}
/*
* Get a capture with a specific name.
*/
static match_t *get_capture_by_name(match_t *m, const char *name)
{
if (m->op->type == VM_CAPTURE && m->op->args.capture.name
&& streq(m->op->args.capture.name, name))
return m;
for (match_t *c = m->child; c; c = c->nextsibling) {
match_t *cap = get_capture_by_name(c, name);
if (cap) return cap;
}
return NULL;
}
/*
* Get a capture by name.
*/
match_t *get_capture(match_t *m, const char **r)
{
if (isdigit(**r)) {
int n = (int)strtol(*r, (char**)r, 10);
return get_capture_by_num(m->child, &n);
} else {
const char *end = after_name(*r);
if (end == *r) return NULL;
char *name = strndup(*r, (size_t)(end-*r));
match_t *cap = get_capture_by_name(m, name);
xfree(&name);
*r = end;
if (**r == ';') ++(*r);
return cap;
}
return NULL;
}
match_t *match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned int flags)
{
return _match(defs, f, str, op, flags, NULL);
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|