2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-15 19:32:37 -08:00
|
|
|
// pattern.c - Compile strings into BP pattern objects that can be matched against.
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2020-09-11 01:28:06 -07:00
|
|
|
|
2020-12-14 22:13:47 -08:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-01-17 13:33:10 -08:00
|
|
|
#include "definitions.h"
|
2021-01-15 19:32:37 -08:00
|
|
|
#include "pattern.h"
|
2020-09-11 01:28:06 -07:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-01-13 19:01:49 -08:00
|
|
|
#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(1); } while(0)
|
2020-09-28 21:30:43 -07:00
|
|
|
|
2021-01-12 22:22:38 -08:00
|
|
|
__attribute__((nonnull))
|
2021-01-15 18:38:06 -08:00
|
|
|
static pat_t *expand_chain(file_t *f, pat_t *first);
|
2021-01-12 22:22:38 -08:00
|
|
|
__attribute__((nonnull))
|
2021-01-15 18:38:06 -08:00
|
|
|
static pat_t *expand_choices(file_t *f, pat_t *first);
|
2021-01-13 01:48:36 -08:00
|
|
|
__attribute__((nonnull))
|
2021-01-15 18:38:06 -08:00
|
|
|
static pat_t *_bp_simplepattern(file_t *f, const char *str);
|
2021-01-13 01:48:36 -08:00
|
|
|
__attribute__((nonnull(1,2,3,6)))
|
2021-01-18 10:47:20 -08:00
|
|
|
static pat_t *new_range(file_t *f, const char *start, const char *end, size_t min, ssize_t max, pat_t *repeating, pat_t *sep);
|
2021-01-18 09:15:25 -08:00
|
|
|
__attribute__((nonnull(1,2)))
|
|
|
|
static pat_t *bp_simplepattern(file_t *f, const char *str);
|
2021-01-13 01:48:36 -08:00
|
|
|
|
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Allocate a new pattern for this file (ensuring it will be automatically
|
|
|
|
// freed when the file is freed)
|
2021-01-13 01:48:36 -08:00
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
pat_t *new_pat(file_t *f, const char *start, enum pattype_e type)
|
2021-01-13 01:48:36 -08:00
|
|
|
{
|
2021-01-15 18:58:44 -08:00
|
|
|
allocated_pat_t *tracker = new(allocated_pat_t);
|
2021-01-15 19:02:36 -08:00
|
|
|
tracker->next = f->pats;
|
|
|
|
f->pats = tracker;
|
|
|
|
tracker->pat.type = type;
|
|
|
|
tracker->pat.start = start;
|
|
|
|
tracker->pat.len = -1;
|
|
|
|
return &tracker->pat;
|
2021-01-13 01:48:36 -08:00
|
|
|
}
|
2020-09-11 01:28:06 -07:00
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// Helper function to initialize a range object.
|
|
|
|
//
|
2021-01-18 10:47:20 -08:00
|
|
|
static pat_t *new_range(file_t *f, const char *start, const char *end, size_t min, ssize_t max, pat_t *repeating, pat_t *sep)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *range = new_pat(f, start, BP_REPEAT);
|
2021-01-18 10:47:20 -08:00
|
|
|
if (repeating->len >= 0 && (sep == NULL || sep->len >= 0) && (ssize_t)min == max)
|
|
|
|
range->len = (ssize_t)repeating->len * (ssize_t)min + (sep == NULL || min == 0 ? 0 : (ssize_t)sep->len * (ssize_t)(min-1));
|
2020-09-11 01:28:06 -07:00
|
|
|
else
|
2021-01-15 19:21:41 -08:00
|
|
|
range->len = -1;
|
|
|
|
range->args.repetitions.min = min;
|
|
|
|
range->args.repetitions.max = max;
|
|
|
|
range->args.repetitions.repeat_pat = repeating;
|
|
|
|
range->args.repetitions.sep = sep;
|
2021-01-18 09:34:52 -08:00
|
|
|
range->start = repeating->start;
|
|
|
|
range->end = repeating->end;
|
2020-09-28 17:42:38 -07:00
|
|
|
if (sep) {
|
2021-01-15 19:21:41 -08:00
|
|
|
if (sep->start < range->start) range->start = sep->start;
|
|
|
|
if (sep->end > range->end) range->end = sep->end;
|
2020-09-28 17:42:38 -07:00
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
range->end = end;
|
|
|
|
return range;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Take a pattern and expand it into a chain of patterns if it's followed by
|
2021-01-12 21:04:43 -08:00
|
|
|
// any patterns (e.g. "`x `y"), otherwise return the original input.
|
|
|
|
//
|
2021-01-15 18:38:06 -08:00
|
|
|
static pat_t *expand_chain(file_t *f, pat_t *first)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *second = bp_simplepattern(f, first->end);
|
2020-09-11 01:28:06 -07:00
|
|
|
if (second == NULL) return first;
|
2020-09-16 19:35:43 -07:00
|
|
|
second = expand_chain(f, second);
|
2020-09-28 21:30:43 -07:00
|
|
|
if (second->end <= first->end)
|
|
|
|
file_err(f, second->end, second->end,
|
|
|
|
"This chain is not parsing properly");
|
2021-01-13 01:48:36 -08:00
|
|
|
return chain_together(f, first, second);
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Take a pattern and parse any "=>" replacements and then expand it into a
|
2021-01-12 21:04:43 -08:00
|
|
|
// chain of choices if it's followed by any "/"-separated patterns (e.g.
|
|
|
|
// "`x/`y"), otherwise return the original input.
|
|
|
|
//
|
2021-01-15 18:38:06 -08:00
|
|
|
static pat_t *expand_choices(file_t *f, pat_t *first)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2020-09-16 19:35:43 -07:00
|
|
|
first = expand_chain(f, first);
|
2020-09-11 01:28:06 -07:00
|
|
|
const char *str = first->end;
|
2021-01-05 19:31:50 -08:00
|
|
|
|
|
|
|
while (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.");
|
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *replacepat = first;
|
2021-01-16 10:29:09 -08:00
|
|
|
first = new_pat(f, replacepat->start, BP_REPLACE);
|
2021-01-15 19:21:41 -08:00
|
|
|
first->args.replace.pat = replacepat;
|
2021-01-17 23:13:47 -08:00
|
|
|
first->args.replace.text = repstr;
|
|
|
|
first->args.replace.len = (size_t)(str-repstr-1);
|
2021-01-15 19:21:41 -08:00
|
|
|
first->len = replacepat->len;
|
2021-01-05 19:31:50 -08:00
|
|
|
first->end = str;
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:28:06 -07:00
|
|
|
if (!matchchar(&str, '/')) return first;
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *second = bp_simplepattern(f, str);
|
2020-09-28 21:30:43 -07:00
|
|
|
if (!second)
|
|
|
|
file_err(f, str, str, "There should be a pattern here after a '/'");
|
2020-09-16 19:35:43 -07:00
|
|
|
second = expand_choices(f, second);
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *choice = new_pat(f, first->start, BP_OTHERWISE);
|
2020-09-11 01:28:06 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:22:38 -08:00
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Given two patterns, return a new pattern for the first pattern followed by
|
2021-01-12 22:22:38 -08:00
|
|
|
// the second. If either pattern is NULL, return the other.
|
|
|
|
//
|
2021-01-17 13:33:10 -08:00
|
|
|
pat_t *chain_together(file_t *f, pat_t *first, pat_t *second)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2021-01-12 18:08:27 -08:00
|
|
|
if (first == NULL) return second;
|
|
|
|
if (second == NULL) return first;
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *chain = new_pat(f, first->start, BP_CHAIN);
|
2020-09-11 01:28:06 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-13 01:48:36 -08:00
|
|
|
// Wrapper for _bp_simplepattern() that expands any postfix operators
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-18 09:15:25 -08:00
|
|
|
static pat_t *bp_simplepattern(file_t *f, const char *str)
|
2021-01-13 01:48:36 -08:00
|
|
|
{
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *pat = _bp_simplepattern(f, str);
|
|
|
|
if (pat == NULL) return pat;
|
2021-01-13 01:48:36 -08:00
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
check(pat->end != NULL, "pat->end is uninitialized!");
|
2021-01-13 01:48:36 -08:00
|
|
|
|
|
|
|
// Expand postfix operators (if any)
|
2021-01-15 19:21:41 -08:00
|
|
|
str = after_spaces(pat->end);
|
2021-01-13 01:48:36 -08:00
|
|
|
while (str+2 < f->end && (matchstr(&str, "!=") || matchstr(&str, "=="))) { // Equality <pat1>==<pat2> and inequality <pat1>!=<pat2>
|
|
|
|
int equal = str[-2] == '=';
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *first = pat;
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *second = bp_simplepattern(f, str);
|
2021-01-13 01:48:36 -08:00
|
|
|
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))
|
2021-01-15 19:21:41 -08:00
|
|
|
file_err(f, pat->start, second->end,
|
2021-01-13 01:48:36 -08:00
|
|
|
"These two patterns cannot possibly give the same result (different lengths: %ld != %ld)",
|
|
|
|
first->len, second->len);
|
|
|
|
}
|
2021-01-16 10:29:09 -08:00
|
|
|
pat = new_pat(f, str, equal ? BP_EQUAL : BP_NOT_EQUAL);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->end = second->end;
|
|
|
|
pat->len = first->len != -1 ? first->len : second->len;
|
|
|
|
pat->args.multiple.first = first;
|
|
|
|
pat->args.multiple.second = second;
|
|
|
|
str = pat->end;
|
2021-01-13 01:48:36 -08:00
|
|
|
str = after_spaces(str);
|
|
|
|
}
|
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
return pat;
|
2021-01-13 01:48:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Compile a string of BP code into a BP pattern object.
|
2021-01-13 01:48:36 -08:00
|
|
|
//
|
2021-01-15 18:38:06 -08:00
|
|
|
static pat_t *_bp_simplepattern(file_t *f, const char *str)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
|
|
|
str = after_spaces(str);
|
2020-09-16 22:36:38 -07:00
|
|
|
if (!*str) return NULL;
|
2021-01-13 01:48:36 -08:00
|
|
|
const char *start = str;
|
2020-09-11 01:28:06 -07:00
|
|
|
char c = *str;
|
|
|
|
++str;
|
|
|
|
switch (c) {
|
2021-01-05 00:09:30 -08:00
|
|
|
// Any char (dot)
|
2020-09-11 01:28:06 -07:00
|
|
|
case '.': {
|
2020-09-17 20:10:09 -07:00
|
|
|
if (*str == '.') { // ".."
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *upto = new_pat(f, start, BP_UPTO_AND);
|
2020-09-17 20:10:09 -07:00
|
|
|
++str;
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *till = bp_simplepattern(f, str);
|
2021-01-15 19:21:41 -08:00
|
|
|
upto->args.multiple.first = till;
|
2020-09-16 17:57:56 -07:00
|
|
|
if (till)
|
|
|
|
str = till->end;
|
2020-12-14 18:11:33 -08:00
|
|
|
if (matchchar(&str, '%')) {
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *skip = bp_simplepattern(f, str);
|
2020-12-14 18:11:33 -08:00
|
|
|
if (!skip)
|
|
|
|
file_err(f, str, str, "There should be a pattern to skip here after the '%%'");
|
2021-01-15 19:21:41 -08:00
|
|
|
upto->args.multiple.second = skip;
|
2020-12-14 18:11:33 -08:00
|
|
|
str = skip->end;
|
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
upto->end = str;
|
|
|
|
return upto;
|
2020-09-13 22:04:51 -07:00
|
|
|
} else {
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *dot = new_pat(f, start, BP_ANYCHAR);
|
2021-01-15 19:21:41 -08:00
|
|
|
dot->len = 1;
|
|
|
|
dot->end = str;
|
|
|
|
return dot;
|
2020-09-13 22:04:51 -07:00
|
|
|
}
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Char literals
|
|
|
|
case '`': {
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *all = NULL;
|
2020-12-19 18:53:51 -08:00
|
|
|
do {
|
|
|
|
char c = *str;
|
|
|
|
if (!c || c == '\n')
|
|
|
|
file_err(f, str, str, "There should be a character here after the '`'");
|
2021-01-13 01:48:36 -08:00
|
|
|
const char *opstart = str-1;
|
2020-12-19 18:53:51 -08:00
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
++str;
|
2020-12-19 18:53:51 -08:00
|
|
|
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.");
|
2021-01-16 10:29:09 -08:00
|
|
|
pat = new_pat(f, opstart, BP_RANGE);
|
2020-12-19 18:53:51 -08:00
|
|
|
if (c < c2) {
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->args.range.low = (unsigned char)c;
|
|
|
|
pat->args.range.high = (unsigned char)c2;
|
2020-12-19 18:53:51 -08:00
|
|
|
} else {
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->args.range.low = (unsigned char)c2;
|
|
|
|
pat->args.range.high = (unsigned char)c;
|
2020-12-19 18:53:51 -08:00
|
|
|
}
|
|
|
|
++str;
|
|
|
|
} else {
|
2021-01-16 10:29:09 -08:00
|
|
|
pat = new_pat(f, opstart, BP_STRING);
|
2020-12-19 18:53:51 -08:00
|
|
|
char *s = xcalloc(sizeof(char), 2);
|
|
|
|
s[0] = c;
|
2021-01-17 23:28:19 -08:00
|
|
|
pat->args.string = s;
|
2020-12-19 18:53:51 -08:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->len = 1;
|
|
|
|
pat->end = str;
|
2020-12-19 18:53:51 -08:00
|
|
|
|
|
|
|
if (all == NULL) {
|
2021-01-15 19:21:41 -08:00
|
|
|
all = pat;
|
2020-12-19 18:53:51 -08:00
|
|
|
} else {
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *either = new_pat(f, all->start, BP_OTHERWISE);
|
2021-01-15 19:21:41 -08:00
|
|
|
either->end = pat->end;
|
2020-12-19 18:53:51 -08:00
|
|
|
either->args.multiple.first = all;
|
2021-01-15 19:21:41 -08:00
|
|
|
either->args.multiple.second = pat;
|
2020-12-19 18:53:51 -08:00
|
|
|
either->len = 1;
|
|
|
|
all = either;
|
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
pat = NULL;
|
2020-12-19 18:53:51 -08:00
|
|
|
} while (matchchar(&str, ','));
|
|
|
|
|
2021-01-13 01:48:36 -08:00
|
|
|
return all;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Escapes
|
|
|
|
case '\\': {
|
2020-09-28 21:30:43 -07:00
|
|
|
if (!*str || *str == '\n')
|
|
|
|
file_err(f, str, str, "There should be an escape sequence here after this backslash.");
|
2020-12-14 21:28:00 -08:00
|
|
|
|
|
|
|
if (matchchar(&str, 'N')) { // \N (nodent)
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *nodent = new_pat(f, start, BP_NODENT);
|
2021-01-15 19:21:41 -08:00
|
|
|
nodent->end = str;
|
|
|
|
return nodent;
|
2020-12-14 21:28:00 -08:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *esc;
|
2021-01-13 01:48:36 -08:00
|
|
|
const char *opstart = str;
|
2021-01-17 23:06:37 -08:00
|
|
|
unsigned char e = (unsigned char)unescapechar(str, &str);
|
2020-09-11 01:28:06 -07:00
|
|
|
if (*str == '-') { // Escape range (e.g. \x00-\xFF)
|
|
|
|
++str;
|
2020-09-28 21:30:43 -07:00
|
|
|
const char *seqstart = str;
|
2021-01-17 23:06:37 -08:00
|
|
|
unsigned char e2 = (unsigned char)unescapechar(str, &str);
|
2020-09-28 21:30:43 -07:00
|
|
|
if (str == seqstart)
|
|
|
|
file_err(f, seqstart, str+1, "This value isn't a valid escape sequence");
|
|
|
|
if (e2 < e)
|
2021-01-13 01:48:36 -08:00
|
|
|
file_err(f, start, str, "Escape ranges should be low-to-high, but this is high-to-low.");
|
2021-01-16 10:29:09 -08:00
|
|
|
esc = new_pat(f, opstart, BP_RANGE);
|
2021-01-15 19:21:41 -08:00
|
|
|
esc->args.range.low = e;
|
|
|
|
esc->args.range.high = e2;
|
2020-09-11 01:28:06 -07:00
|
|
|
} else {
|
2021-01-16 10:29:09 -08:00
|
|
|
esc = new_pat(f, opstart, BP_STRING);
|
2020-12-17 19:49:56 -08:00
|
|
|
char *s = xcalloc(sizeof(char), 2);
|
|
|
|
s[0] = (char)e;
|
2021-01-17 23:28:19 -08:00
|
|
|
esc->args.string = s;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
esc->len = 1;
|
|
|
|
esc->end = str;
|
|
|
|
return esc;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// String literal
|
|
|
|
case '"': case '\'': case '\002': {
|
|
|
|
char endquote = c == '\002' ? '\003' : c;
|
2021-01-13 01:48:36 -08:00
|
|
|
char *litstart = (char*)str;
|
2020-09-11 01:28:06 -07:00
|
|
|
for (; *str && *str != endquote; str++) {
|
|
|
|
if (*str == '\\') {
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!str[1] || str[1] == '\n')
|
|
|
|
file_err(f, str, str+1,
|
|
|
|
"There should be an escape sequence after this backslash.");
|
2020-09-11 01:28:06 -07:00
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 01:48:36 -08:00
|
|
|
size_t len = (size_t)(str - litstart);
|
2020-12-17 19:49:56 -08:00
|
|
|
char *literal = xcalloc(sizeof(char), len+1);
|
2021-01-13 01:48:36 -08:00
|
|
|
memcpy(literal, litstart, len);
|
2021-01-12 18:41:31 -08:00
|
|
|
// Note: an unescaped string is guaranteed to be no longer than the
|
|
|
|
// escaped string, so this is safe to do inplace.
|
2020-09-11 01:28:06 -07:00
|
|
|
len = unescape_string(literal, literal, len);
|
|
|
|
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *pat = new_pat(f, start, BP_STRING);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->len = (ssize_t)len;
|
2021-01-17 23:28:19 -08:00
|
|
|
pat->args.string = literal;
|
2020-09-11 01:28:06 -07:00
|
|
|
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!matchchar(&str, endquote))
|
2021-01-13 01:48:36 -08:00
|
|
|
file_err(f, start, str, "This string doesn't have a closing quote.");
|
|
|
|
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->end = str;
|
|
|
|
return pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Not <pat>
|
|
|
|
case '!': {
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *p = bp_simplepattern(f, str);
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!p) file_err(f, str, str, "There should be a pattern after this '!'");
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *not = new_pat(f, start, BP_NOT);
|
2021-01-15 19:21:41 -08:00
|
|
|
not->len = 0;
|
|
|
|
not->args.pat = p;
|
|
|
|
not->end = p->end;
|
|
|
|
return not;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// 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': {
|
2021-01-18 10:47:20 -08:00
|
|
|
size_t min = 0;
|
|
|
|
ssize_t max = -1;
|
2020-09-11 01:28:06 -07:00
|
|
|
--str;
|
|
|
|
long n1 = strtol(str, (char**)&str, 10);
|
|
|
|
if (matchchar(&str, '-')) {
|
|
|
|
str = after_spaces(str);
|
2021-01-13 01:48:36 -08:00
|
|
|
const char *numstart = str;
|
2020-09-11 01:28:06 -07:00
|
|
|
long n2 = strtol(str, (char**)&str, 10);
|
2021-01-18 10:47:20 -08:00
|
|
|
if (str == numstart) min = 0, max = (ssize_t)n1;
|
|
|
|
else min = (size_t)n1, max = (ssize_t)n2;
|
2020-09-11 01:28:06 -07:00
|
|
|
} else if (matchchar(&str, '+')) {
|
2021-01-18 10:47:20 -08:00
|
|
|
min = (size_t)n1, max = -1;
|
2020-09-11 01:28:06 -07:00
|
|
|
} else {
|
2021-01-18 10:47:20 -08:00
|
|
|
min = (size_t)n1, max = (ssize_t)n1;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *repeating = bp_simplepattern(f, str);
|
|
|
|
if (!repeating)
|
2020-09-28 22:02:00 -07:00
|
|
|
file_err(f, str, str, "There should be a pattern after this repetition count.");
|
2021-01-15 19:21:41 -08:00
|
|
|
str = repeating->end;
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *sep = NULL;
|
2020-09-11 01:28:06 -07:00
|
|
|
if (matchchar(&str, '%')) {
|
2020-12-30 19:42:47 -08:00
|
|
|
sep = bp_simplepattern(f, str);
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!sep)
|
|
|
|
file_err(f, str, str, "There should be a separator pattern after this '%%'");
|
2020-09-11 01:28:06 -07:00
|
|
|
str = sep->end;
|
|
|
|
} else {
|
2021-01-15 19:21:41 -08:00
|
|
|
str = repeating->end;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
return new_range(f, start, str, min, max, repeating, sep);
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Lookbehind
|
|
|
|
case '<': {
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *behind = bp_simplepattern(f, str);
|
|
|
|
if (!behind)
|
2020-09-28 21:30:43 -07:00
|
|
|
file_err(f, str, str, "There should be a pattern after this '<'");
|
2021-01-15 19:21:41 -08:00
|
|
|
str = behind->end;
|
|
|
|
if (behind->len == -1)
|
|
|
|
file_err(f, start, behind->end,
|
2020-09-28 21:30:43 -07:00
|
|
|
"Sorry, variable-length lookbehind patterns like this are not supported.\n"
|
|
|
|
"Please use a fixed-length lookbehind pattern instead.");
|
2021-01-15 19:21:41 -08:00
|
|
|
str = behind->end;
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *pat = new_pat(f, start, BP_AFTER);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->len = 0;
|
|
|
|
pat->args.pat = behind;
|
|
|
|
pat->end = str;
|
|
|
|
return pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Lookahead
|
|
|
|
case '>': {
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *ahead = bp_simplepattern(f, str);
|
|
|
|
if (!ahead)
|
2020-09-28 21:30:43 -07:00
|
|
|
file_err(f, str, str, "There should be a pattern after this '>'");
|
2021-01-15 19:21:41 -08:00
|
|
|
str = ahead->end;
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *pat = new_pat(f, start, BP_BEFORE);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->len = 0;
|
|
|
|
pat->args.pat = ahead;
|
|
|
|
pat->end = str;
|
|
|
|
return pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Parentheses
|
2020-12-30 15:30:19 -08:00
|
|
|
case '(': case '{': {
|
|
|
|
char closing = c == '(' ? ')' : '}';
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *pat = bp_simplepattern(f, str);
|
|
|
|
if (!pat)
|
2020-09-28 22:02:00 -07:00
|
|
|
file_err(f, str, str, "There should be a valid pattern after this parenthesis.");
|
2021-01-15 19:21:41 -08:00
|
|
|
pat = expand_choices(f, pat);
|
|
|
|
str = pat->end;
|
2020-12-30 15:30:19 -08:00
|
|
|
if (!matchchar(&str, closing))
|
2021-01-13 01:48:36 -08:00
|
|
|
file_err(f, start, str, "This parenthesis group isn't properly closed.");
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->start = start;
|
|
|
|
pat->end = str;
|
|
|
|
return pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
2020-09-28 16:14:06 -07:00
|
|
|
// Square brackets
|
|
|
|
case '[': {
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *maybe = bp_simplepattern(f, str);
|
|
|
|
if (!maybe)
|
2020-09-28 22:02:00 -07:00
|
|
|
file_err(f, str, str, "There should be a valid pattern after this square bracket.");
|
2021-01-15 19:21:41 -08:00
|
|
|
maybe = expand_choices(f, maybe);
|
|
|
|
str = maybe->end;
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!matchchar(&str, ']'))
|
2021-01-13 01:48:36 -08:00
|
|
|
file_err(f, start, str, "This square bracket group isn't properly closed.");
|
2021-01-15 19:21:41 -08:00
|
|
|
return new_range(f, start, str, 0, 1, maybe, NULL);
|
2020-09-28 16:14:06 -07:00
|
|
|
}
|
2020-09-28 18:08:23 -07:00
|
|
|
// Repeating
|
|
|
|
case '*': case '+': {
|
2021-01-18 10:47:20 -08:00
|
|
|
size_t min = (size_t)(c == '*' ? 0 : 1);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *repeating = bp_simplepattern(f, str);
|
|
|
|
if (!repeating)
|
2020-09-28 22:02:00 -07:00
|
|
|
file_err(f, str, str, "There should be a valid pattern here after the '%c'", c);
|
2021-01-15 19:21:41 -08:00
|
|
|
str = repeating->end;
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *sep = NULL;
|
2020-09-28 18:08:23 -07:00
|
|
|
if (matchchar(&str, '%')) {
|
2020-12-30 19:42:47 -08:00
|
|
|
sep = bp_simplepattern(f, str);
|
2020-09-28 21:30:43 -07:00
|
|
|
if (!sep)
|
2020-09-28 22:02:00 -07:00
|
|
|
file_err(f, str, str, "There should be a separator pattern after the '%%' here.");
|
2020-09-28 18:08:23 -07:00
|
|
|
str = sep->end;
|
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
return new_range(f, start, str, min, -1, repeating, sep);
|
2020-09-28 18:08:23 -07:00
|
|
|
}
|
2020-09-11 01:28:06 -07:00
|
|
|
// Capture
|
|
|
|
case '@': {
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *capture = new_pat(f, start, BP_CAPTURE);
|
2020-09-28 16:14:06 -07:00
|
|
|
const char *a = *str == '!' ? &str[1] : after_name(str);
|
2020-09-28 17:10:13 -07:00
|
|
|
if (a > str && after_spaces(a)[0] == '=' && after_spaces(a)[1] != '>') {
|
2021-01-17 23:28:19 -08:00
|
|
|
capture->args.capture.name = str;
|
|
|
|
capture->args.capture.namelen = (size_t)(a-str);
|
2020-09-28 17:10:13 -07:00
|
|
|
str = after_spaces(a) + 1;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *captured = bp_simplepattern(f, str);
|
|
|
|
if (!captured)
|
2020-09-28 22:02:00 -07:00
|
|
|
file_err(f, str, str, "There should be a valid pattern here to capture after the '@'");
|
2021-01-15 19:21:41 -08:00
|
|
|
capture->args.capture.capture_pat = captured;
|
|
|
|
capture->len = captured->len;
|
|
|
|
capture->end = captured->end;
|
|
|
|
return capture;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
// Special rules:
|
2020-12-14 21:28:00 -08:00
|
|
|
case '_': case '^': case '$': case '|': {
|
2021-01-17 23:28:19 -08:00
|
|
|
size_t namelen = 1;
|
|
|
|
if (matchchar(&str, c)) // double __, ^^, $$
|
|
|
|
++namelen;
|
|
|
|
if (matchchar(&str, ':')) return NULL; // Don't match definitions
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *ref = new_pat(f, start, BP_REF);
|
2021-01-17 23:28:19 -08:00
|
|
|
ref->args.name.name = start;
|
|
|
|
ref->args.name.len = namelen;
|
2021-01-15 19:21:41 -08:00
|
|
|
ref->end = str;
|
|
|
|
return ref;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Reference
|
2021-01-13 01:48:36 -08:00
|
|
|
if (!isalpha(c)) return NULL;
|
|
|
|
--str;
|
|
|
|
const char *refname = str;
|
|
|
|
str = after_name(str);
|
|
|
|
if (matchchar(&str, ':')) // Don't match definitions
|
2020-09-11 01:28:06 -07:00
|
|
|
return NULL;
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *ref = new_pat(f, start, BP_REF);
|
2021-01-17 23:28:19 -08:00
|
|
|
ref->args.name.name = refname;
|
|
|
|
ref->args.name.len = (size_t)(str - refname);
|
2021-01-15 19:21:41 -08:00
|
|
|
ref->end = str;
|
|
|
|
return ref;
|
2020-09-28 17:56:02 -07:00
|
|
|
}
|
2020-09-13 20:33:11 -07:00
|
|
|
}
|
2021-01-13 01:48:36 -08:00
|
|
|
return NULL;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
|
|
|
// Similar to bp_simplepattern, except that the pattern begins with an implicit, unclosable quote.
|
|
|
|
//
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *bp_stringpattern(file_t *f, const char *str)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *ret = NULL;
|
2020-09-11 01:28:06 -07:00
|
|
|
while (*str) {
|
2020-12-17 19:49:56 -08:00
|
|
|
char *start = (char*)str;
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *interp = NULL;
|
2020-09-11 01:28:06 -07:00
|
|
|
for (; *str; str++) {
|
|
|
|
if (*str == '\\') {
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!str[1] || str[1] == '\n')
|
|
|
|
file_err(f, str, str, "There should be an escape sequence or pattern here after this backslash.");
|
2020-12-14 21:28:00 -08:00
|
|
|
|
|
|
|
if (matchchar(&str, 'N')) { // \N (nodent)
|
2021-01-16 10:29:09 -08:00
|
|
|
interp = new_pat(f, str-2, BP_NODENT);
|
2020-12-14 21:28:00 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-09-28 15:13:54 -07:00
|
|
|
const char *after_escape;
|
2021-01-17 23:06:37 -08:00
|
|
|
char e = unescapechar(&str[1], &after_escape);
|
2021-01-17 13:33:10 -08:00
|
|
|
// If there is not a special escape sequence (\n, \x0A, etc.)
|
|
|
|
// or \\, \", \', \`, then check for an interpolated value:
|
|
|
|
// The special cases for single and double quotes aren't
|
|
|
|
// needed, but there's no known legitimate use case for
|
|
|
|
// interpolating a literal string, and users might escape
|
|
|
|
// quotes out of paranoia, and we want to support that. String
|
|
|
|
// literal interpolations can be done with \("...") anyways.
|
|
|
|
if (e == str[1] && e != '\'' && e != '"' && e != '\\' && e != '`') {
|
|
|
|
interp = bp_simplepattern(f, str + 1);
|
|
|
|
if (interp) break;
|
2020-09-28 15:13:54 -07:00
|
|
|
}
|
2021-01-17 13:33:10 -08:00
|
|
|
str = after_escape - 1; // Otherwise treat as a literal character
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of string
|
2020-12-17 19:49:56 -08:00
|
|
|
size_t len = (size_t)(str - start);
|
|
|
|
char *literal = xcalloc(sizeof(char), len+1);
|
|
|
|
memcpy(literal, start, len);
|
2021-01-12 18:41:31 -08:00
|
|
|
// Note: an unescaped string is guaranteed to be no longer than the
|
|
|
|
// escaped string, so this is safe to do inplace.
|
2020-09-11 01:28:06 -07:00
|
|
|
len = unescape_string(literal, literal, len);
|
2021-01-13 18:56:22 -08:00
|
|
|
if (len > 0) {
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *strop = new_pat(f, str, BP_STRING);
|
2021-01-13 18:56:22 -08:00
|
|
|
strop->len = (ssize_t)len;
|
2021-01-17 23:28:19 -08:00
|
|
|
strop->args.string = literal;
|
2021-01-13 18:56:22 -08:00
|
|
|
strop->end = str;
|
2021-01-13 01:48:36 -08:00
|
|
|
ret = chain_together(f, ret, strop);
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
if (interp) {
|
2021-01-13 01:48:36 -08:00
|
|
|
ret = chain_together(f, ret, interp);
|
2020-09-11 01:28:06 -07:00
|
|
|
str = interp->end;
|
|
|
|
// allow terminating seq
|
2021-01-18 09:52:35 -08:00
|
|
|
(void)matchchar(&str, ';');
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Given a pattern and a replacement string, compile the two into a BP
|
|
|
|
// replace pattern.
|
2021-01-12 21:04:43 -08:00
|
|
|
//
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *bp_replacement(file_t *f, pat_t *replacepat, const char *replacement)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2021-01-16 10:29:09 -08:00
|
|
|
pat_t *pat = new_pat(f, replacepat->start, BP_REPLACE);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->end = replacepat->end;
|
|
|
|
pat->len = replacepat->len;
|
|
|
|
pat->args.replace.pat = replacepat;
|
2020-09-11 01:28:06 -07:00
|
|
|
const char *p = replacement;
|
|
|
|
for (; *p; p++) {
|
|
|
|
if (*p == '\\') {
|
2020-09-28 22:02:00 -07:00
|
|
|
if (!p[1] || p[1] == '\n')
|
|
|
|
file_err(f, p, p, "There should be an escape sequence or pattern here after this backslash.");
|
2020-09-11 01:28:06 -07:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 19:49:56 -08:00
|
|
|
size_t rlen = (size_t)(p-replacement);
|
|
|
|
char *rcpy = xcalloc(sizeof(char), rlen + 1);
|
|
|
|
memcpy(rcpy, replacement, rlen);
|
2021-01-15 19:21:41 -08:00
|
|
|
pat->args.replace.text = rcpy;
|
|
|
|
pat->args.replace.len = rlen;
|
|
|
|
return pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:28:44 -08:00
|
|
|
//
|
2021-01-15 18:57:09 -08:00
|
|
|
// Compile a string representing a BP pattern into a pattern object.
|
2021-01-12 21:28:44 -08:00
|
|
|
//
|
2021-01-15 18:38:06 -08:00
|
|
|
pat_t *bp_pattern(file_t *f, const char *str)
|
2020-09-11 01:28:06 -07:00
|
|
|
{
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *pat = bp_simplepattern(f, str);
|
|
|
|
if (pat != NULL) pat = expand_choices(f, pat);
|
|
|
|
return pat;
|
2020-09-11 01:28:06 -07:00
|
|
|
}
|
2020-09-11 01:38:44 -07:00
|
|
|
|
2021-01-15 12:40:19 -08:00
|
|
|
//
|
|
|
|
// Match a definition (id__`:__pattern)
|
|
|
|
//
|
2021-01-17 13:33:10 -08:00
|
|
|
def_t *bp_definition(def_t *defs, file_t *f, const char *str)
|
2021-01-15 12:40:19 -08:00
|
|
|
{
|
|
|
|
const char *name = after_spaces(str);
|
|
|
|
str = after_name(name);
|
|
|
|
if (!str) return NULL;
|
|
|
|
size_t namelen = (size_t)(str - name);
|
|
|
|
if (!matchchar(&str, ':')) return NULL;
|
2021-01-15 19:21:41 -08:00
|
|
|
pat_t *defpat = bp_pattern(f, str);
|
|
|
|
if (!defpat) return NULL;
|
2021-01-18 09:52:35 -08:00
|
|
|
(void)matchchar(&defpat->end, ';'); // TODO: verify this is safe to mutate
|
2021-01-17 13:33:10 -08:00
|
|
|
return with_def(defs, namelen, name, defpat);
|
2021-01-15 12:40:19 -08:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:39:28 -08:00
|
|
|
//
|
|
|
|
// Deallocate memory referenced inside a pattern struct
|
|
|
|
//
|
|
|
|
void destroy_pat(pat_t *pat)
|
|
|
|
{
|
2021-01-17 23:28:19 -08:00
|
|
|
if (pat->type == BP_STRING)
|
|
|
|
xfree(&pat->args.string);
|
2021-01-15 19:39:28 -08:00
|
|
|
}
|
|
|
|
|
2020-09-11 01:38:44 -07:00
|
|
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|