bp/pattern.c

609 lines
23 KiB
C
Raw Normal View History

//
2021-01-15 19:32:37 -08:00
// pattern.c - Compile strings into BP pattern objects that can be matched against.
//
#include <ctype.h>
#include <err.h>
2021-01-18 11:15:53 -08:00
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "definitions.h"
2021-01-26 17:58:46 -08:00
#include "files.h"
2021-01-15 19:32:37 -08:00
#include "pattern.h"
2020-09-11 01:28:06 -07:00
#include "utils.h"
2021-05-20 15:27:24 -07:00
#include "utf8.h"
2020-09-11 01:28:06 -07:00
__attribute__((nonnull))
static pat_t *expand_replacements(file_t *f, pat_t *replace_pat);
__attribute__((nonnull))
2021-01-15 18:38:06 -08:00
static pat_t *expand_chain(file_t *f, pat_t *first);
__attribute__((nonnull))
2021-01-15 18:38:06 -08:00
static pat_t *expand_choices(file_t *f, pat_t *first);
__attribute__((nonnull))
2021-01-15 18:38:06 -08:00
static pat_t *_bp_simplepattern(file_t *f, const char *str);
__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-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-05-20 15:27:24 -07:00
pat_t *new_pat(file_t *f, const char *start, const char *end, size_t minlen, ssize_t maxlen, enum pattype_e type)
{
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.end = end;
2021-05-20 15:27:24 -07:00
tracker->pat.min_matchlen = minlen;
tracker->pat.max_matchlen = maxlen;
2021-01-15 19:02:36 -08:00
return &tracker->pat;
}
2020-09-11 01:28:06 -07: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-05-20 15:27:24 -07:00
size_t minlen = min*repeating->min_matchlen + (min > 0 ? min-1 : 0)*(sep ? sep->min_matchlen : 0);
2021-05-22 22:02:22 -07:00
ssize_t maxlen = (max == -1 || UNBOUNDED(repeating) || (max != 0 && max != 1 && sep && UNBOUNDED(sep))) ? (ssize_t)-1
2021-05-20 15:27:24 -07:00
: max*repeating->max_matchlen + (ssize_t)(max > 0 ? min-1 : 0)*(ssize_t)(sep ? sep->min_matchlen : 0);
pat_t *range = new_pat(f, start, end, minlen, maxlen, BP_REPEAT);
2021-01-15 19:21:41 -08:00
range->args.repetitions.min = min;
range->args.repetitions.max = max;
range->args.repetitions.repeat_pat = repeating;
range->args.repetitions.sep = sep;
return range;
2020-09-11 01:28:06 -07: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
// 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");
return chain_together(f, first, second);
2020-09-11 01:28:06 -07:00
}
//
// Match trailing => replacements (with optional pattern beforehand)
//
static pat_t *expand_replacements(file_t *f, pat_t *replace_pat)
2020-09-11 01:28:06 -07:00
{
const char *str = replace_pat->end;
while (matchstr(&str, "=>")) {
const char *repstr;
size_t replen;
if (matchchar(&str, '"') || matchchar(&str, '\'')) {
char quote = str[-1];
repstr = str;
for (; *str && *str != quote; str = next_char(f, 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 = next_char(f, str);
}
2021-01-05 19:31:50 -08:00
}
replen = (size_t)(str-repstr);
(void)matchchar(&str, quote);
} else {
repstr = "";
replen = 0;
2021-01-05 19:31:50 -08:00
}
2021-05-20 15:27:24 -07:00
pat_t *pat = new_pat(f, replace_pat->start, str, replace_pat->min_matchlen,
replace_pat->max_matchlen, BP_REPLACE);
pat->args.replace.pat = replace_pat;
pat->args.replace.text = repstr;
pat->args.replace.len = replen;
replace_pat = pat;
2021-01-05 19:31:50 -08:00
}
return replace_pat;
}
2021-01-05 19:31:50 -08:00
//
// Take a pattern and parse any "=>" replacements and then 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 pat_t *expand_choices(file_t *f, pat_t *first)
{
first = expand_chain(f, first);
first = expand_replacements(f, first);
const char *str = first->end;
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);
if (matchstr(&str, "=>"))
second = expand_replacements(f, second ? second : new_pat(f, str-2, str-2, 0, 0, BP_STRING));
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);
return either_pat(f, first, second);
2020-09-11 01:28:06 -07:00
}
//
2021-01-15 18:57:09 -08:00
// Given two patterns, return a new pattern for the first pattern followed by
// the second. If either pattern is NULL, return the other.
//
pat_t *chain_together(file_t *f, pat_t *first, pat_t *second)
2020-09-11 01:28:06 -07:00
{
if (first == NULL) return second;
if (second == NULL) return first;
2021-05-20 15:27:24 -07:00
size_t minlen = first->min_matchlen + second->min_matchlen;
2021-05-22 22:02:22 -07:00
ssize_t maxlen = (UNBOUNDED(first) || UNBOUNDED(second)) ? (ssize_t)-1 : first->max_matchlen + second->max_matchlen;
2021-05-20 15:27:24 -07:00
pat_t *chain = new_pat(f, first->start, second->end, minlen, maxlen, BP_CHAIN);
2020-09-11 01:28:06 -07:00
chain->args.multiple.first = first;
chain->args.multiple.second = second;
// If `first` is an UPTO operator (..) or contains one, then let it know
// that `second` is what it's up *to*.
for (pat_t *p = first; p; ) {
if (p->type == BP_UPTO) {
p->args.multiple.first = second;
2021-05-20 15:27:24 -07:00
p->min_matchlen = second->min_matchlen;
p->max_matchlen = -1;
break;
} else if (p->type == BP_CAPTURE) {
p = p->args.capture.capture_pat;
2021-02-07 10:54:02 -08:00
} else if (p->type == BP_CHAIN) {
p = p->args.multiple.second;
} else if (p->type == BP_MATCH || p->type == BP_NOT_MATCH) {
p = p->args.pat;
} else break;
}
2020-09-11 01:28:06 -07:00
return chain;
}
//
// Given two patterns, return a new pattern for matching either the first
// pattern or the second. If either pattern is NULL, return the other.
//
pat_t *either_pat(file_t *f, pat_t *first, pat_t *second)
{
if (first == NULL) return second;
if (second == NULL) return first;
2021-05-20 15:27:24 -07:00
size_t minlen = first->min_matchlen < second->min_matchlen ? first->min_matchlen : second->min_matchlen;
2021-05-22 22:02:22 -07:00
ssize_t maxlen = (UNBOUNDED(first) || UNBOUNDED(second)) ? (ssize_t)-1 :
2021-05-20 15:27:24 -07:00
(first->max_matchlen > second->max_matchlen ? first->max_matchlen : second->max_matchlen);
pat_t *either = new_pat(f, first->start, second->end, minlen, maxlen, BP_OTHERWISE);
either->args.multiple.first = first;
either->args.multiple.second = second;
return either;
}
//
// Wrapper for _bp_simplepattern() that expands any postfix operators
//
2021-01-18 09:15:25 -08:00
static pat_t *bp_simplepattern(file_t *f, const char *str)
{
2021-01-15 19:21:41 -08:00
pat_t *pat = _bp_simplepattern(f, str);
if (pat == NULL) return pat;
if (pat->end == NULL)
errx(EXIT_FAILURE, "pat->end is uninitialized!");
// Expand postfix operators (if any)
2021-01-15 19:21:41 -08:00
str = after_spaces(pat->end);
while (str+2 < f->end) {
enum pattype_e type;
if (matchchar(&str, '~'))
type = BP_MATCH;
else if (matchstr(&str, "!~"))
type = BP_NOT_MATCH;
else break;
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);
if (!second)
file_err(f, str, str, "The '%s' operator expects a pattern before and after.", type == BP_MATCH ? "~" : "!~");
2021-05-20 15:27:24 -07:00
pat = new_pat(f, str, second->end, first->min_matchlen, first->max_matchlen, type);
2021-01-15 19:21:41 -08:00
pat->args.multiple.first = first;
pat->args.multiple.second = second;
str = pat->end;
str = after_spaces(str);
}
2021-01-15 19:21:41 -08:00
return pat;
}
//
2021-01-15 18:57:09 -08:00
// Compile a string of BP code into a BP pattern object.
//
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);
if (!*str) return NULL;
const char *start = str;
2020-09-11 01:28:06 -07:00
char c = *str;
2021-05-20 15:27:24 -07:00
str = next_char(f, str);
2020-09-11 01:28:06 -07:00
switch (c) {
// Any char (dot)
2020-09-11 01:28:06 -07:00
case '.': {
if (*str == '.') { // ".."
pat_t *skip = NULL;
2021-05-20 15:27:24 -07:00
str = next_char(f, str);
2020-12-14 18:11:33 -08:00
if (matchchar(&str, '%')) {
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 '%%'");
str = skip->end;
}
2021-05-20 15:27:24 -07:00
pat_t *upto = new_pat(f, start, str, 0, -1, BP_UPTO);
upto->args.multiple.second = skip;
2021-01-15 19:21:41 -08:00
return upto;
} else {
2021-05-20 15:27:24 -07:00
return new_pat(f, start, str, 1, UTF8_MAXCHARLEN, BP_ANYCHAR);
}
2020-09-11 01:28:06 -07:00
}
// Char literals
case '`': {
2021-01-15 18:38:06 -08:00
pat_t *all = NULL;
do { // Comma-separated items:
2021-05-20 15:27:24 -07:00
if (str >= f->end || !*str || *str == '\n')
2020-12-19 18:53:51 -08:00
file_err(f, str, str, "There should be a character here after the '`'");
2021-05-20 15:27:24 -07:00
const char *c1_loc = str;
str = next_char(f, c1_loc);
2020-12-19 18:53:51 -08:00
if (matchchar(&str, '-')) { // Range
const char *c2_loc = str;
if (next_char(f, c1_loc) > c1_loc+1 || next_char(f, c2_loc) > c2_loc+1)
file_err(f, start, next_char(f, c2_loc), "Sorry, UTF-8 character ranges are not yet supported.");
char c1 = *c1_loc, c2 = *c2_loc;
2020-12-19 18:53:51 -08:00
if (!c2 || c2 == '\n')
file_err(f, str, str, "There should be a character here to complete the character range.");
2021-05-20 15:27:24 -07:00
if (c1 > c2) { // Swap order
char tmp = c1;
c1 = c2;
c2 = tmp;
2020-12-19 18:53:51 -08:00
}
str = next_char(f, c2_loc);
pat_t *pat = new_pat(f, start == c1_loc - 1 ? start : c1_loc, str, 1, 1, BP_RANGE);
2021-05-20 15:27:24 -07:00
pat->args.range.low = (unsigned char)c1;
pat->args.range.high = (unsigned char)c2;
all = either_pat(f, all, pat);
2020-12-19 18:53:51 -08:00
} else {
size_t len = (size_t)(str - c1_loc);
pat_t *pat = new_pat(f, c1_loc, str, len, (ssize_t)len, BP_STRING);
2021-05-20 15:27:24 -07:00
pat->args.string = c1_loc;
all = either_pat(f, all, pat);
2020-12-19 18:53:51 -08:00
}
} while (matchchar(&str, ','));
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.");
pat_t *all = NULL;
do { // Comma-separated items:
if (matchchar(&str, 'N')) { // \N (nodent)
all = either_pat(f, all, new_pat(f, start, str, 1, -1, BP_NODENT));
continue;
}
const char *opstart = str;
unsigned char e = (unsigned char)unescapechar(str, &str);
if (matchchar(&str, '-')) { // Escape range (e.g. \x00-\xFF)
if (next_char(f, str) != str+1)
file_err(f, start, next_char(f, str), "Sorry, UTF8 escape sequences are not supported.");
const char *seqstart = str;
unsigned char e2 = (unsigned char)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, start, str, "Escape ranges should be low-to-high, but this is high-to-low.");
pat_t *esc = new_pat(f, opstart, str, 1, 1, BP_RANGE);
esc->args.range.low = e;
esc->args.range.high = e2;
all = either_pat(f, all, esc);
} else if (str > opstart) {
pat_t *esc = new_pat(f, start, str, 1, 1, BP_STRING);
char *s = xcalloc(sizeof(char), 2);
s[0] = (char)e;
esc->args.string = s;
all = either_pat(f, all, esc);
} else {
const char *next = next_char(f, opstart);
size_t len = (size_t)(next-opstart);
pat_t *esc = new_pat(f, start, next, len, (ssize_t)len, BP_STRING);
char *s = xcalloc(sizeof(char), 1+len);
memcpy(s, opstart, len);
esc->args.string = s;
all = either_pat(f, all, esc);
}
} while (matchchar(&str, ','));
return all;
2020-09-11 01:28:06 -07:00
}
// String literal
case '"': case '\'': case '{': case '\002': {
char endquote = c == '{' ? '}' : (c == '\002' ? '\003' : c);
char *litstart = (char*)str;
while (str < f->end && *str != endquote)
2021-05-20 15:27:24 -07:00
str = next_char(f, str);
size_t len = (size_t)(str - litstart);
str = next_char(f, str);
2020-09-11 01:28:06 -07:00
2021-05-20 15:27:24 -07:00
pat_t *pat = new_pat(f, start, str, len, (ssize_t)len, BP_STRING);
pat->args.string = litstart;
if (c == '{') { // Surround with `|` word boundaries
2021-05-20 15:27:24 -07:00
pat_t *left = new_pat(f, start, start+1, 0, -1, BP_REF);
2021-05-11 19:06:41 -07:00
left->args.ref.name = "left-word-edge";
left->args.ref.len = strlen(left->args.ref.name);
2021-05-20 15:27:24 -07:00
pat_t *right = new_pat(f, str-1, str, 0, -1, BP_REF);
2021-05-11 19:06:41 -07:00
right->args.ref.name = "right-word-edge";
right->args.ref.len = strlen(right->args.ref.name);
pat = chain_together(f, left, chain_together(f, pat, right));
}
2021-01-15 19:21:41 -08:00
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-05-20 15:27:24 -07:00
pat_t *not = new_pat(f, start, p->end, 0, 0, BP_NOT);
2021-01-15 19:21:41 -08:00
not->args.pat = p;
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);
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;
str = behind->end;
2021-05-20 15:27:24 -07:00
pat_t *pat = new_pat(f, start, str, 0, 0, BP_AFTER);
2021-01-15 19:21:41 -08:00
pat->args.pat = behind;
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-05-20 15:27:24 -07:00
pat_t *pat = new_pat(f, start, str, 0, 0, BP_BEFORE);
2021-01-15 19:21:41 -08:00
pat->args.pat = ahead;
return pat;
2020-09-11 01:28:06 -07:00
}
// Parentheses
case '(': {
if (matchstr(&str, "!)")) { // (!) errors
pat_t *pat = bp_simplepattern(f, str);
2021-05-20 15:27:24 -07:00
if (!pat) pat = new_pat(f, str, str, 0, 0, BP_STRING);
pat = expand_replacements(f, pat);
2021-05-20 15:27:24 -07:00
pat_t *error = new_pat(f, start, pat->end, pat->min_matchlen, pat->max_matchlen, BP_ERROR);
error->args.pat = pat;
return error;
}
pat_t *pat = bp_pattern(f, str);
2021-01-15 19:21:41 -08:00
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
str = pat->end;
(void)matchchar(&str, ')');
2021-01-15 19:21:41 -08:00
pat->start = start;
pat->end = str;
return pat;
2020-09-11 01:28:06 -07:00
}
// Square brackets
case '[': {
pat_t *maybe = bp_pattern(f, str);
2021-01-15 19:21:41 -08:00
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
str = maybe->end;
(void)matchchar(&str, ']');
2021-01-15 19:21:41 -08:00
return new_range(f, start, str, 0, 1, maybe, NULL);
}
// 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;
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.");
str = sep->end;
}
2021-01-15 19:21:41 -08:00
return new_range(f, start, str, min, -1, repeating, sep);
}
2020-09-11 01:28:06 -07:00
// Capture
case '@': {
const char *name = NULL;
size_t namelen = 0;
const char *a = after_name(str);
if (a > str && after_spaces(a)[0] == '=' && after_spaces(a)[1] != '>') {
name = str;
namelen = (size_t)(a-str);
str = after_spaces(a) + 1;
2020-09-11 01:28:06 -07: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 here to capture after the '@'");
2021-05-20 15:27:24 -07:00
pat_t *capture = new_pat(f, start, pat->end, pat->min_matchlen, pat->max_matchlen, BP_CAPTURE);
capture->args.capture.capture_pat = pat;
capture->args.capture.name = name;
capture->args.capture.namelen = namelen;
2021-01-15 19:21:41 -08:00
return capture;
2020-09-11 01:28:06 -07:00
}
// Start of file/line:
case '^': {
if (matchchar(&str, '^'))
2021-05-20 15:27:24 -07:00
return new_pat(f, start, str, 0, 0, BP_START_OF_FILE);
return new_pat(f, start, str, 0, 0, BP_START_OF_LINE);
}
// End of file/line:
case '$': {
if (matchchar(&str, '$'))
2021-05-20 15:27:24 -07:00
return new_pat(f, start, str, 0, 0, BP_END_OF_FILE);
return new_pat(f, start, str, 0, 0, BP_END_OF_LINE);
}
// Whitespace:
case '_': {
size_t namelen = 1;
if (matchchar(&str, '_')) // double __ (whitespace with newlines)
++namelen;
if (matchchar(&str, ':')) return NULL; // Don't match definitions
2021-05-20 15:27:24 -07:00
pat_t *ref = new_pat(f, start, str, 0, -1, BP_REF);
ref->args.ref.name = start;
ref->args.ref.len = namelen;
2021-01-15 19:21:41 -08:00
return ref;
2020-09-11 01:28:06 -07:00
}
default: {
// Reference
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-05-20 15:27:24 -07:00
pat_t *ref = new_pat(f, start, str, 0, -1, BP_REF);
ref->args.ref.name = refname;
ref->args.ref.len = (size_t)(str - refname);
2021-01-15 19:21:41 -08:00
return ref;
2020-09-28 17:56:02 -07:00
}
2020-09-13 20:33:11 -07:00
}
2020-09-11 01:28:06 -07: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) {
char *start = (char*)str;
2021-01-15 18:38:06 -08:00
pat_t *interp = NULL;
2021-05-20 15:28:08 -07:00
for (; str < f->end; str = next_char(f, str)) {
if (*str == '\\' && str+1 < f->end) {
interp = bp_simplepattern(f, str + 1);
if (interp) break;
// If there is no interpolated value, this is just a plain ol' regular backslash
2020-09-11 01:28:06 -07:00
}
}
// End of string
2021-05-20 15:27:24 -07:00
size_t len = (size_t)(str - start);
2021-01-13 18:56:22 -08:00
if (len > 0) {
2021-05-20 15:27:24 -07:00
pat_t *str_chunk = new_pat(f, start, str, len, (ssize_t)len, BP_STRING);
str_chunk->args.string = start;
ret = chain_together(f, ret, str_chunk);
2020-09-11 01:28:06 -07:00
}
if (interp) {
ret = chain_together(f, ret, interp);
2020-09-11 01:28:06 -07:00
str = interp->end;
// allow terminating seq
(void)matchchar(&str, ';');
2020-09-11 01:28:06 -07:00
}
}
return ret;
}
//
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-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-05-20 15:27:24 -07:00
pat_t *pat = new_pat(f, replacepat->start, replacepat->end, replacepat->min_matchlen, replacepat->max_matchlen, BP_REPLACE);
2021-01-15 19:21:41 -08:00
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;
}
}
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);
if (matchstr(&str, "=>"))
pat = expand_replacements(f, pat ? pat : new_pat(f, str-2, str-2, 0, 0, BP_STRING));
2021-01-15 19:21:41 -08:00
return pat;
2020-09-11 01:28:06 -07:00
}
//
// Match a definition (id__`:__pattern)
//
def_t *bp_definition(def_t *defs, file_t *f, const char *str)
{
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;
(void)matchchar(&defpat->end, ';'); // TODO: verify this is safe to mutate
return with_def(defs, namelen, name, defpat);
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1