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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
/*
* compiler.c - Compile strings into BP virtual machine code.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "compiler.h"
#include "utils.h"
#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); _exit(1); } while(0)
static vm_op_t *expand_chain(file_t *f, vm_op_t *first);
static vm_op_t *expand_choices(file_t *f, vm_op_t *first);
static vm_op_t *chain_together(vm_op_t *first, vm_op_t *second);
static void set_range(vm_op_t *op, ssize_t min, ssize_t max, vm_op_t *pat, vm_op_t *sep);
/*
* Helper function to initialize a range object.
*/
static void set_range(vm_op_t *op, ssize_t min, ssize_t max, vm_op_t *pat, vm_op_t *sep)
{
op->op = VM_REPEAT;
if (pat->len >= 0 && (sep == NULL || sep->len >= 0) && min == max && min >= 0)
op->len = pat->len * min + (sep == NULL || min == 0 ? 0 : sep->len * (min-1));
else
op->len = -1;
op->args.repetitions.min = min;
op->args.repetitions.max = max;
op->args.repetitions.repeat_pat = pat;
op->args.repetitions.sep = sep;
if (!op->start) op->start = pat->start;
if (!op->end) op->end = pat->end;
if (sep) {
if (sep->start < op->start) op->start = sep->start;
if (sep->end > op->end) op->end = sep->end;
}
}
/*
* Take an opcode and expand it into a chain of patterns if it's
* followed by any patterns (e.g. "`x `y"), otherwise return
* the original input.
*/
static vm_op_t *expand_chain(file_t *f, vm_op_t *first)
{
vm_op_t *second = bp_simplepattern(f, first->end);
if (second == NULL) return first;
second = expand_chain(f, second);
if (second->end <= first->end)
file_err(f, second->end, second->end,
"This chain is not parsing properly");
return chain_together(first, second);
}
/*
* Take an opcode and expand it into a chain of choices if it's
* followed by any "/"-separated patterns (e.g. "`x/`y"), otherwise
* return the original input.
*/
static vm_op_t *expand_choices(file_t *f, vm_op_t *first)
{
first = expand_chain(f, first);
const char *str = first->end;
if (!matchchar(&str, '/')) return first;
vm_op_t *second = bp_simplepattern(f, str);
if (!second)
file_err(f, str, str, "There should be a pattern here after a '/'");
second = expand_choices(f, second);
vm_op_t *choice = new(vm_op_t);
choice->op = VM_OTHERWISE;
choice->start = first->start;
if (first->len == second->len)
choice->len = first->len;
else choice->len = -1;
choice->end = second->end;
choice->args.multiple.first = first;
choice->args.multiple.second = second;
return choice;
}
static vm_op_t *chain_together(vm_op_t *first, vm_op_t *second)
{
if (first == NULL) return second;
if (second == NULL) return first;
vm_op_t *chain = new(vm_op_t);
chain->op = VM_CHAIN;
chain->start = first->start;
if (first->len >= 0 && second->len >= 0)
chain->len = first->len + second->len;
else chain->len = -1;
chain->end = second->end;
chain->args.multiple.first = first;
chain->args.multiple.second = second;
return chain;
}
/*
* Compile a string of BP code into virtual machine opcodes
*/
vm_op_t *bp_simplepattern(file_t *f, const char *str)
{
str = after_spaces(str);
if (!*str) return NULL;
vm_op_t *op = new(vm_op_t);
op->start = str;
op->len = -1;
char c = *str;
const char *origin = str;
++str;
switch (c) {
// Any char (dot)
case '.': {
if (*str == '.') { // ".."
++str;
vm_op_t *till = bp_simplepattern(f, str);
op->op = VM_UPTO_AND;
op->len = -1;
op->args.multiple.first = till;
if (till)
str = till->end;
if (matchchar(&str, '%')) {
vm_op_t *skip = bp_simplepattern(f, str);
if (!skip)
file_err(f, str, str, "There should be a pattern to skip here after the '%%'");
op->args.multiple.second = skip;
str = skip->end;
}
break;
} else {
op->op = VM_ANYCHAR;
op->len = 1;
break;
}
}
// Char literals
case '`': {
vm_op_t *all = NULL;
do {
char c = *str;
if (!c || c == '\n')
file_err(f, str, str, "There should be a character here after the '`'");
if (op == NULL)
op = new(vm_op_t);
op->start = str-1;
op->len = 1;
++str;
if (matchchar(&str, '-')) { // Range
char c2 = *str;
if (!c2 || c2 == '\n')
file_err(f, str, str, "There should be a character here to complete the character range.");
op->op = VM_RANGE;
if (c < c2) {
op->args.range.low = (unsigned char)c;
op->args.range.high = (unsigned char)c2;
} else {
op->args.range.low = (unsigned char)c2;
op->args.range.high = (unsigned char)c;
}
++str;
} else {
op->op = VM_STRING;
char *s = xcalloc(sizeof(char), 2);
s[0] = c;
op->args.s = s;
}
op->end = str;
if (all == NULL) {
all = op;
} else {
vm_op_t *either = new(vm_op_t);
either->op = VM_OTHERWISE;
either->start = all->start;
either->end = op->end;
either->args.multiple.first = all;
either->args.multiple.second = op;
either->len = 1;
all = either;
}
op = NULL;
} while (matchchar(&str, ','));
op = all;
break;
}
// Escapes
case '\\': {
if (!*str || *str == '\n')
file_err(f, str, str, "There should be an escape sequence here after this backslash.");
if (matchchar(&str, 'N')) { // \N (nodent)
op->op = VM_NODENT;
break;
}
op->len = 1;
unsigned char e = unescapechar(str, &str);
if (*str == '-') { // Escape range (e.g. \x00-\xFF)
++str;
const char *seqstart = str;
unsigned char e2 = unescapechar(str, &str);
if (str == seqstart)
file_err(f, seqstart, str+1, "This value isn't a valid escape sequence");
if (e2 < e)
file_err(f, origin, str, "Escape ranges should be low-to-high, but this is high-to-low.");
op->op = VM_RANGE;
op->args.range.low = e;
op->args.range.high = e2;
} else {
op->op = VM_STRING;
char *s = xcalloc(sizeof(char), 2);
s[0] = (char)e;
op->args.s = s;
}
break;
}
// String literal
case '"': case '\'': case '\002': {
char endquote = c == '\002' ? '\003' : c;
char *start = (char*)str;
for (; *str && *str != endquote; str++) {
if (*str == '\\') {
if (!str[1] || str[1] == '\n')
file_err(f, str, str+1,
"There should be an escape sequence after this backslash.");
++str;
}
}
size_t len = (size_t)(str - start);
char *literal = xcalloc(sizeof(char), len+1);
memcpy(literal, start, len);
len = unescape_string(literal, literal, len);
op->op = VM_STRING;
op->len = (ssize_t)len;
op->args.s = literal;
if (!matchchar(&str, endquote))
file_err(f, origin, str, "This string doesn't have a closing quote.");
break;
}
// Not <pat>
case '!': {
vm_op_t *p = bp_simplepattern(f, str);
if (!p) file_err(f, str, str, "There should be a pattern after this '!'");
str = p->end;
op->op = VM_NOT;
op->len = 0;
op->args.pat = p;
break;
}
// Number of repetitions: <N>(-<N> / - / + / "")
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': {
ssize_t min = -1, max = -1;
--str;
long n1 = strtol(str, (char**)&str, 10);
if (matchchar(&str, '-')) {
str = after_spaces(str);
const char *start = str;
long n2 = strtol(str, (char**)&str, 10);
if (str == start) min = 0, max = n1;
else min = n1, max = n2;
} else if (matchchar(&str, '+')) {
min = n1, max = -1;
} else {
min = n1, max = n1;
}
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a pattern after this repetition count.");
str = pat->end;
str = after_spaces(str);
vm_op_t *sep = NULL;
if (matchchar(&str, '%')) {
sep = bp_simplepattern(f, str);
if (!sep)
file_err(f, str, str, "There should be a separator pattern after this '%%'");
str = sep->end;
} else {
str = pat->end;
}
set_range(op, min, max, pat, sep);
break;
}
// Lookbehind
case '<': {
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a pattern after this '<'");
str = pat->end;
if (pat->len == -1)
file_err(f, origin, pat->end,
"Sorry, variable-length lookbehind patterns like this are not supported.\n"
"Please use a fixed-length lookbehind pattern instead.");
str = pat->end;
op->op = VM_AFTER;
op->len = 0;
op->args.pat = pat;
break;
}
// Lookahead
case '>': {
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a pattern after this '>'");
str = pat->end;
op->op = VM_BEFORE;
op->len = 0;
op->args.pat = pat;
break;
}
// Parentheses
case '(': case '{': {
char closing = c == '(' ? ')' : '}';
free(op);
op = bp_simplepattern(f, str);
if (!op)
file_err(f, str, str, "There should be a valid pattern after this parenthesis.");
op = expand_choices(f, op);
str = op->end;
str = after_spaces(str);
if (!matchchar(&str, closing))
file_err(f, origin, str, "This parenthesis group isn't properly closed.");
op->start = origin;
op->end = str;
break;
}
// Square brackets
case '[': {
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a valid pattern after this square bracket.");
pat = expand_choices(f, pat);
str = pat->end;
str = after_spaces(str);
if (!matchchar(&str, ']'))
file_err(f, origin, str, "This square bracket group isn't properly closed.");
set_range(op, 0, 1, pat, NULL);
break;
}
// Repeating
case '*': case '+': {
ssize_t min = c == '*' ? 0 : 1;
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a valid pattern here after the '%c'", c);
str = pat->end;
str = after_spaces(str);
vm_op_t *sep = NULL;
if (matchchar(&str, '%')) {
sep = bp_simplepattern(f, str);
if (!sep)
file_err(f, str, str, "There should be a separator pattern after the '%%' here.");
str = sep->end;
}
set_range(op, min, -1, pat, sep);
break;
}
// Capture
case '@': {
op->op = VM_CAPTURE;
const char *a = *str == '!' ? &str[1] : after_name(str);
if (a > str && after_spaces(a)[0] == '=' && after_spaces(a)[1] != '>') {
op->args.capture.name = strndup(str, (size_t)(a-str));
str = after_spaces(a) + 1;
}
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a valid pattern here to capture after the '@'");
str = pat->end;
op->args.capture.capture_pat = pat;
op->len = pat->len;
break;
}
// Hide
case '~': {
vm_op_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a pattern after this '~'");
str = pat->end;
op->op = VM_HIDE;
op->len = 0;
op->args.pat = pat;
break;
}
// Special rules:
case '_': case '^': case '$': case '|': {
if (matchchar(&str, c)) { // double __, ^^, $$
char tmp[3] = {c, c, '\0'};
op->args.s = strdup(tmp);
} else {
op->args.s = strndup(&c, 1);
}
if (*after_spaces(str) == ':') {
free((char*)op->args.s);
free(op);
return NULL;
}
op->op = VM_REF;
break;
}
default: {
// Reference
if (isalpha(c)) {
--str;
const char *refname = str;
str = after_name(str);
if (*after_spaces(str) == ':') {
free(op);
return NULL;
}
op->op = VM_REF;
op->args.s = strndup(refname, (size_t)(str - refname));
break;
} else {
free(op);
return NULL;
}
}
}
op->end = str;
// Postfix operators:
postfix:
str = after_spaces(str);
if (str+2 < f->end && matchstr(&str, "=>")) { // Replacement <pat> => <pat>
str = after_spaces(str);
char quote = *str;
if (!(matchchar(&str, '"') || matchchar(&str, '\'')))
file_err(f, str, str, "There should be a string literal as a replacement here.");
const char *repstr = str;
for (; *str && *str != quote; str++) {
if (*str == '\\') {
if (!str[1] || str[1] == '\n')
file_err(f, str, str+1,
"There should be an escape sequence after this backslash.");
++str;
}
}
if (!matchchar(&str, quote))
file_err(f, &repstr[-1], str, "This string doesn't have a closing quote.");
size_t replace_len = (size_t)(str-repstr-1);
const char *replacement = xcalloc(sizeof(char), replace_len+1);
memcpy((void*)replacement, repstr, replace_len);
vm_op_t *pat = op;
op = new(vm_op_t);
op->op = VM_REPLACE;
op->args.replace.pat = pat;
op->args.replace.text = replacement;
op->args.replace.len = replace_len;
op->len = pat->len;
op->start = pat->start;
op->end = str;
goto postfix;
} else if (str+2 < f->end && (matchstr(&str, "!=") || matchstr(&str, "=="))) { // Equality <pat1>==<pat2> and inequality <pat1>!=<pat2>
int equal = str[-2] == '=';
vm_op_t *first = op;
vm_op_t *second = bp_simplepattern(f, str);
if (!second)
file_err(f, str, str, "The '%c=' operator expects a pattern before and after.", equal?'=':'!');
if (equal) {
if (!(first->len == -1 || second->len == -1 || first->len == second->len))
file_err(f, origin, second->end,
"These two patterns cannot possibly give the same result (different lengths: %ld != %ld)",
first->len, second->len);
}
op = new(vm_op_t);
op->op = equal ? VM_EQUAL : VM_NOT_EQUAL;
op->start = str;
op->end = second->end;
op->len = first->len != -1 ? first->len : second->len;
op->args.multiple.first = first;
op->args.multiple.second = second;
str = op->end;
goto postfix;
}
return op;
}
/*
* Similar to bp_simplepattern, except that the pattern begins with an implicit, unclosable quote.
*/
vm_op_t *bp_stringpattern(file_t *f, const char *str)
{
vm_op_t *ret = NULL;
while (*str) {
vm_op_t *strop = new(vm_op_t);
strop->start = str;
strop->len = 0;
strop->op = VM_STRING;
char *start = (char*)str;
vm_op_t *interp = NULL;
for (; *str; str++) {
if (*str == '\\') {
if (!str[1] || str[1] == '\n')
file_err(f, str, str, "There should be an escape sequence or pattern here after this backslash.");
if (matchchar(&str, 'N')) { // \N (nodent)
interp = new(vm_op_t);
interp->op = VM_NODENT;
break;
}
const char *after_escape;
unsigned char e = unescapechar(&str[1], &after_escape);
if (e != str[1]) {
str = after_escape - 1;
continue;
}
if (str[1] == '\\') {
++str;
continue;
}
interp = bp_simplepattern(f, str + 1);
if (interp == NULL)
file_err(f, str, str+1, "This isn't a valid escape sequence or pattern.");
break;
}
}
// End of string
size_t len = (size_t)(str - start);
char *literal = xcalloc(sizeof(char), len+1);
memcpy(literal, start, len);
len = unescape_string(literal, literal, len);
strop->len = (ssize_t)len;
strop->args.s = literal;
strop->end = str;
if (strop->len == 0) {
free(strop);
strop = NULL;
} else {
ret = chain_together(ret, strop);
}
if (interp) {
ret = chain_together(ret, interp);
str = interp->end;
// allow terminating seq
matchchar(&str, ';');
}
}
return ret;
}
/*
* Given a pattern and a replacement string, compile the two into a replacement
* VM opcode.
*/
vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement)
{
vm_op_t *op = new(vm_op_t);
op->op = VM_REPLACE;
op->start = pat->start;
op->len = pat->len;
op->args.replace.pat = pat;
const char *p = replacement;
for (; *p; p++) {
if (*p == '\\') {
if (!p[1] || p[1] == '\n')
file_err(f, p, p, "There should be an escape sequence or pattern here after this backslash.");
++p;
}
}
size_t rlen = (size_t)(p-replacement);
char *rcpy = xcalloc(sizeof(char), rlen + 1);
memcpy(rcpy, replacement, rlen);
op->args.replace.text = rcpy;
op->args.replace.len = rlen;
return op;
}
vm_op_t *bp_pattern(file_t *f, const char *str)
{
vm_op_t *op = bp_simplepattern(f, str);
if (op != NULL) op = expand_choices(f, op);
return op;
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|