Renaming op -> pat, phase 1 refactor

This commit is contained in:
Bruce Hill 2021-01-15 18:38:06 -08:00
parent 631e59bfa7
commit 813d9a76af
8 changed files with 103 additions and 103 deletions

22
bp.c
View File

@ -122,7 +122,7 @@ static int is_text_file(const char *filename)
//
// Print matches in JSON format.
//
static int print_matches_as_json(def_t *defs, file_t *f, vm_op_t *pattern)
static int print_matches_as_json(def_t *defs, file_t *f, pat_t *pattern)
{
int matches = 0;
for (match_t *m = NULL; (m = next_match(defs, f, m, pattern, ignorecase)); ) {
@ -140,7 +140,7 @@ static int print_matches_as_json(def_t *defs, file_t *f, vm_op_t *pattern)
//
// Print matches in a visual explanation style
//
static int explain_matches(def_t *defs, file_t *f, vm_op_t *pattern)
static int explain_matches(def_t *defs, file_t *f, pat_t *pattern)
{
int matches = 0;
for (match_t *m = NULL; (m = next_match(defs, f, m, pattern, ignorecase)); ) {
@ -231,7 +231,7 @@ static void confirm_replacements(file_t *f, match_t *m, confirm_t *confirm)
// Replace a file's contents with the text version of a match.
// (Useful for replacements)
//
static int inplace_modify_file(def_t *defs, file_t *f, vm_op_t *pattern)
static int inplace_modify_file(def_t *defs, file_t *f, pat_t *pattern)
{
char tmp_filename[PATH_MAX+1] = {0};
printer_t pr = {
@ -289,7 +289,7 @@ static int inplace_modify_file(def_t *defs, file_t *f, vm_op_t *pattern)
//
// Print all the matches in a file.
//
static int print_matches(def_t *defs, file_t *f, vm_op_t *pattern)
static int print_matches(def_t *defs, file_t *f, pat_t *pattern)
{
static int printed_filenames = 0;
int matches = 0;
@ -325,7 +325,7 @@ static int print_matches(def_t *defs, file_t *f, vm_op_t *pattern)
// For a given filename, open the file and attempt to match the given pattern
// against it, printing any results according to the flags.
//
static int process_file(def_t *defs, const char *filename, vm_op_t *pattern)
static int process_file(def_t *defs, const char *filename, pat_t *pattern)
{
file_t *f = load_file(NULL, filename);
check(f, "Could not open file: %s", filename);
@ -367,11 +367,11 @@ int main(int argc, char *argv[])
// Define an opcode that is just a reference to the rule `pattern`
file_t *pat_file = spoof_file(&loaded_files, "<pattern>", "pattern");
vm_op_t *pattern = bp_pattern(loaded_files, pat_file->contents);
pat_t *pattern = bp_pattern(loaded_files, pat_file->contents);
// Define an opcode that is just a reference to the rule `replacement`
file_t *rep_file = spoof_file(&loaded_files, "<replacement>", "replacement");
vm_op_t *replacement = bp_pattern(rep_file, rep_file->contents);
pat_t *replacement = bp_pattern(rep_file, rep_file->contents);
// Load builtins:
file_t *xdg_file = load_file(&loaded_files, "/etc/xdg/"BP_NAME"/builtins.bp");
@ -407,7 +407,7 @@ int main(int argc, char *argv[])
// TODO: spoof file as sprintf("pattern => '%s'", flag)
// except that would require handling edge cases like quotation marks etc.
file_t *replace_file = spoof_file(&loaded_files, "<replace argument>", flag);
vm_op_t *rep = bp_replacement(replace_file, pattern, replace_file->contents);
pat_t *rep = bp_replacement(replace_file, pattern, replace_file->contents);
check(rep, "Replacement failed to compile: %s", flag);
defs = with_def(defs, replace_file, strlen("replacement"), "replacement", rep);
pattern = replacement;
@ -428,7 +428,7 @@ int main(int argc, char *argv[])
defs = d;
str = d->op->end;
} else {
vm_op_t *p = bp_pattern(arg_file, str);
pat_t *p = bp_pattern(arg_file, str);
check(p, "Pattern failed to compile: %s", flag);
check(npatterns == 0, "Cannot define multiple patterns");
defs = with_def(defs, arg_file, strlen("pattern"), "pattern", p);
@ -439,7 +439,7 @@ int main(int argc, char *argv[])
}
} else if (FLAG("--pattern-string") || FLAG("-P")) {
file_t *arg_file = spoof_file(&loaded_files, "<pattern argument>", flag);
vm_op_t *p = bp_stringpattern(arg_file, arg_file->contents);
pat_t *p = bp_stringpattern(arg_file, arg_file->contents);
check(p, "Pattern failed to compile: %s", flag);
defs = with_def(defs, arg_file, strlen("pattern"), "pattern", p);
++npatterns;
@ -470,7 +470,7 @@ int main(int argc, char *argv[])
if (npatterns > 0) break;
// TODO: spoof file with quotation marks for better debugging
file_t *arg_file = spoof_file(&loaded_files, "<pattern argument>", argv[i]);
vm_op_t *p = bp_stringpattern(arg_file, arg_file->contents);
pat_t *p = bp_stringpattern(arg_file, arg_file->contents);
check(p, "Pattern failed to compile: %s", argv[i]);
defs = with_def(defs, arg_file, strlen("pattern"), "pattern", p);
++npatterns;

View File

@ -13,21 +13,21 @@
#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(1); } while(0)
__attribute__((nonnull))
static vm_op_t *expand_chain(file_t *f, vm_op_t *first);
static pat_t *expand_chain(file_t *f, pat_t *first);
__attribute__((nonnull))
static vm_op_t *expand_choices(file_t *f, vm_op_t *first);
static pat_t *expand_choices(file_t *f, pat_t *first);
__attribute__((nonnull))
static vm_op_t *_bp_simplepattern(file_t *f, const char *str);
static pat_t *_bp_simplepattern(file_t *f, const char *str);
__attribute__((nonnull(1)))
static vm_op_t *chain_together(file_t *f,vm_op_t *first, vm_op_t *second);
static pat_t *chain_together(file_t *f,pat_t *first, pat_t *second);
__attribute__((nonnull(1,2,3,6)))
static vm_op_t *new_range(file_t *f, const char *start, const char *end, ssize_t min, ssize_t max, vm_op_t *pat, vm_op_t *sep);
static pat_t *new_range(file_t *f, const char *start, const char *end, ssize_t min, ssize_t max, pat_t *pat, pat_t *sep);
//
// Allocate a new opcode for this file (ensuring it will be automatically freed
// when the file is freed)
//
vm_op_t *new_op(file_t *f, const char *start, enum VMOpcode type)
pat_t *new_pat(file_t *f, const char *start, enum VMOpcode type)
{
allocated_op_t *tracker = new(allocated_op_t);
tracker->next = f->ops;
@ -41,9 +41,9 @@ vm_op_t *new_op(file_t *f, const char *start, enum VMOpcode type)
//
// Helper function to initialize a range object.
//
static vm_op_t *new_range(file_t *f, const char *start, const char *end, ssize_t min, ssize_t max, vm_op_t *pat, vm_op_t *sep)
static pat_t *new_range(file_t *f, const char *start, const char *end, ssize_t min, ssize_t max, pat_t *pat, pat_t *sep)
{
vm_op_t *op = new_op(f, start, VM_REPEAT);
pat_t *op = new_pat(f, start, 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
@ -66,9 +66,9 @@ static vm_op_t *new_range(file_t *f, const char *start, const char *end, ssize_t
// 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)
static pat_t *expand_chain(file_t *f, pat_t *first)
{
vm_op_t *second = bp_simplepattern(f, first->end);
pat_t *second = bp_simplepattern(f, first->end);
if (second == NULL) return first;
second = expand_chain(f, second);
if (second->end <= first->end)
@ -82,7 +82,7 @@ static vm_op_t *expand_chain(file_t *f, vm_op_t *first)
// 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)
static pat_t *expand_choices(file_t *f, pat_t *first)
{
first = expand_chain(f, first);
const char *str = first->end;
@ -108,8 +108,8 @@ static vm_op_t *expand_choices(file_t *f, vm_op_t *first)
const char *replacement = xcalloc(sizeof(char), replace_len+1);
memcpy((void*)replacement, repstr, replace_len);
vm_op_t *pat = first;
first = new_op(f, pat->start, VM_REPLACE);
pat_t *pat = first;
first = new_pat(f, pat->start, VM_REPLACE);
first->args.replace.pat = pat;
first->args.replace.text = replacement;
first->args.replace.len = replace_len;
@ -118,11 +118,11 @@ static vm_op_t *expand_choices(file_t *f, vm_op_t *first)
}
if (!matchchar(&str, '/')) return first;
vm_op_t *second = bp_simplepattern(f, str);
pat_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_op(f, first->start, VM_OTHERWISE);
pat_t *choice = new_pat(f, first->start, VM_OTHERWISE);
if (first->len == second->len)
choice->len = first->len;
else choice->len = -1;
@ -136,11 +136,11 @@ static vm_op_t *expand_choices(file_t *f, vm_op_t *first)
// Given two patterns, return a new opcode for the first pattern followed by
// the second. If either pattern is NULL, return the other.
//
static vm_op_t *chain_together(file_t *f, vm_op_t *first, vm_op_t *second)
static pat_t *chain_together(file_t *f, pat_t *first, pat_t *second)
{
if (first == NULL) return second;
if (second == NULL) return first;
vm_op_t *chain = new_op(f, first->start, VM_CHAIN);
pat_t *chain = new_pat(f, first->start, VM_CHAIN);
chain->start = first->start;
if (first->len >= 0 && second->len >= 0)
chain->len = first->len + second->len;
@ -154,9 +154,9 @@ static vm_op_t *chain_together(file_t *f, vm_op_t *first, vm_op_t *second)
//
// Wrapper for _bp_simplepattern() that expands any postfix operators
//
vm_op_t *bp_simplepattern(file_t *f, const char *str)
pat_t *bp_simplepattern(file_t *f, const char *str)
{
vm_op_t *op = _bp_simplepattern(f, str);
pat_t *op = _bp_simplepattern(f, str);
if (op == NULL) return op;
check(op->end != NULL, "op->end is uninitialized!");
@ -165,8 +165,8 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
str = after_spaces(op->end);
while (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);
pat_t *first = op;
pat_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) {
@ -175,7 +175,7 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
"These two patterns cannot possibly give the same result (different lengths: %ld != %ld)",
first->len, second->len);
}
op = new_op(f, str, equal ? VM_EQUAL : VM_NOT_EQUAL);
op = new_pat(f, str, equal ? VM_EQUAL : VM_NOT_EQUAL);
op->end = second->end;
op->len = first->len != -1 ? first->len : second->len;
op->args.multiple.first = first;
@ -190,7 +190,7 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
//
// Compile a string of BP code into virtual machine opcodes
//
static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
static pat_t *_bp_simplepattern(file_t *f, const char *str)
{
str = after_spaces(str);
if (!*str) return NULL;
@ -201,14 +201,14 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
// Any char (dot)
case '.': {
if (*str == '.') { // ".."
vm_op_t *op = new_op(f, start, VM_UPTO_AND);
pat_t *op = new_pat(f, start, VM_UPTO_AND);
++str;
vm_op_t *till = bp_simplepattern(f, str);
pat_t *till = bp_simplepattern(f, str);
op->args.multiple.first = till;
if (till)
str = till->end;
if (matchchar(&str, '%')) {
vm_op_t *skip = bp_simplepattern(f, str);
pat_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;
@ -217,7 +217,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
op->end = str;
return op;
} else {
vm_op_t *op = new_op(f, start, VM_ANYCHAR);
pat_t *op = new_pat(f, start, VM_ANYCHAR);
op->len = 1;
op->end = str;
return op;
@ -225,20 +225,20 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
// Char literals
case '`': {
vm_op_t *all = NULL;
pat_t *all = NULL;
do {
char c = *str;
if (!c || c == '\n')
file_err(f, str, str, "There should be a character here after the '`'");
const char *opstart = str-1;
vm_op_t *op;
pat_t *op;
++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 = new_op(f, opstart, VM_RANGE);
op = new_pat(f, opstart, VM_RANGE);
if (c < c2) {
op->args.range.low = (unsigned char)c;
op->args.range.high = (unsigned char)c2;
@ -248,7 +248,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
++str;
} else {
op = new_op(f, opstart, VM_STRING);
op = new_pat(f, opstart, VM_STRING);
char *s = xcalloc(sizeof(char), 2);
s[0] = c;
op->args.s = s;
@ -260,7 +260,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
if (all == NULL) {
all = op;
} else {
vm_op_t *either = new_op(f, all->start, VM_OTHERWISE);
pat_t *either = new_pat(f, all->start, VM_OTHERWISE);
either->end = op->end;
either->args.multiple.first = all;
either->args.multiple.second = op;
@ -278,12 +278,12 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
file_err(f, str, str, "There should be an escape sequence here after this backslash.");
if (matchchar(&str, 'N')) { // \N (nodent)
vm_op_t *op = new_op(f, start, VM_NODENT);
pat_t *op = new_pat(f, start, VM_NODENT);
op->end = str;
return op;
}
vm_op_t *op;
pat_t *op;
const char *opstart = str;
unsigned char e = unescapechar(str, &str);
if (*str == '-') { // Escape range (e.g. \x00-\xFF)
@ -294,11 +294,11 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
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.");
op = new_op(f, opstart, VM_RANGE);
op = new_pat(f, opstart, VM_RANGE);
op->args.range.low = e;
op->args.range.high = e2;
} else {
op = new_op(f, opstart, VM_STRING);
op = new_pat(f, opstart, VM_STRING);
char *s = xcalloc(sizeof(char), 2);
s[0] = (char)e;
op->args.s = s;
@ -326,7 +326,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
// escaped string, so this is safe to do inplace.
len = unescape_string(literal, literal, len);
vm_op_t *op = new_op(f, start, VM_STRING);
pat_t *op = new_pat(f, start, VM_STRING);
op->len = (ssize_t)len;
op->args.s = literal;
@ -338,9 +338,9 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
// Not <pat>
case '!': {
vm_op_t *p = bp_simplepattern(f, str);
pat_t *p = bp_simplepattern(f, str);
if (!p) file_err(f, str, str, "There should be a pattern after this '!'");
vm_op_t *op = new_op(f, start, VM_NOT);
pat_t *op = new_pat(f, start, VM_NOT);
op->len = 0;
op->args.pat = p;
op->end = p->end;
@ -363,11 +363,11 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
} else {
min = n1, max = n1;
}
vm_op_t *pat = bp_simplepattern(f, str);
pat_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;
vm_op_t *sep = NULL;
pat_t *sep = NULL;
if (matchchar(&str, '%')) {
sep = bp_simplepattern(f, str);
if (!sep)
@ -380,7 +380,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
// Lookbehind
case '<': {
vm_op_t *pat = bp_simplepattern(f, str);
pat_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a pattern after this '<'");
str = pat->end;
@ -389,7 +389,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
"Sorry, variable-length lookbehind patterns like this are not supported.\n"
"Please use a fixed-length lookbehind pattern instead.");
str = pat->end;
vm_op_t *op = new_op(f, start, VM_AFTER);
pat_t *op = new_pat(f, start, VM_AFTER);
op->len = 0;
op->args.pat = pat;
op->end = str;
@ -397,11 +397,11 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
// Lookahead
case '>': {
vm_op_t *pat = bp_simplepattern(f, str);
pat_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a pattern after this '>'");
str = pat->end;
vm_op_t *op = new_op(f, start, VM_BEFORE);
pat_t *op = new_pat(f, start, VM_BEFORE);
op->len = 0;
op->args.pat = pat;
op->end = str;
@ -410,7 +410,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
// Parentheses
case '(': case '{': {
char closing = c == '(' ? ')' : '}';
vm_op_t *op = bp_simplepattern(f, str);
pat_t *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);
@ -423,7 +423,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
// Square brackets
case '[': {
vm_op_t *pat = bp_simplepattern(f, str);
pat_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);
@ -435,11 +435,11 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
// Repeating
case '*': case '+': {
ssize_t min = c == '*' ? 0 : 1;
vm_op_t *pat = bp_simplepattern(f, str);
pat_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;
vm_op_t *sep = NULL;
pat_t *sep = NULL;
if (matchchar(&str, '%')) {
sep = bp_simplepattern(f, str);
if (!sep)
@ -450,13 +450,13 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
}
// Capture
case '@': {
vm_op_t *op = new_op(f, start, VM_CAPTURE);
pat_t *op = new_pat(f, start, 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);
pat_t *pat = bp_simplepattern(f, str);
if (!pat)
file_err(f, str, str, "There should be a valid pattern here to capture after the '@'");
op->args.capture.capture_pat = pat;
@ -475,7 +475,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
if (matchchar(&str, ':')) return NULL; // Don't match definitions
name = strndup(&c, 1);
}
vm_op_t *op = new_op(f, start, VM_REF);
pat_t *op = new_pat(f, start, VM_REF);
op->args.s = name;
op->end = str;
return op;
@ -488,7 +488,7 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
str = after_name(str);
if (matchchar(&str, ':')) // Don't match definitions
return NULL;
vm_op_t *op = new_op(f, start, VM_REF);
pat_t *op = new_pat(f, start, VM_REF);
op->args.s = strndup(refname, (size_t)(str - refname));
op->end = str;
return op;
@ -500,19 +500,19 @@ static vm_op_t *_bp_simplepattern(file_t *f, const char *str)
//
// 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)
pat_t *bp_stringpattern(file_t *f, const char *str)
{
vm_op_t *ret = NULL;
pat_t *ret = NULL;
while (*str) {
char *start = (char*)str;
vm_op_t *interp = NULL;
pat_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_op(f, str-2, VM_NODENT);
interp = new_pat(f, str-2, VM_NODENT);
break;
}
@ -540,7 +540,7 @@ vm_op_t *bp_stringpattern(file_t *f, const char *str)
// escaped string, so this is safe to do inplace.
len = unescape_string(literal, literal, len);
if (len > 0) {
vm_op_t *strop = new_op(f, str, VM_STRING);
pat_t *strop = new_pat(f, str, VM_STRING);
strop->len = (ssize_t)len;
strop->args.s = literal;
strop->end = str;
@ -560,9 +560,9 @@ vm_op_t *bp_stringpattern(file_t *f, const char *str)
// 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)
pat_t *bp_replacement(file_t *f, pat_t *pat, const char *replacement)
{
vm_op_t *op = new_op(f, pat->start, VM_REPLACE);
pat_t *op = new_pat(f, pat->start, VM_REPLACE);
op->end = pat->end;
op->len = pat->len;
op->args.replace.pat = pat;
@ -585,9 +585,9 @@ vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement)
//
// Compile a string representing a BP pattern into an opcode object.
//
vm_op_t *bp_pattern(file_t *f, const char *str)
pat_t *bp_pattern(file_t *f, const char *str)
{
vm_op_t *op = bp_simplepattern(f, str);
pat_t *op = bp_simplepattern(f, str);
if (op != NULL) op = expand_choices(f, op);
return op;
}
@ -602,7 +602,7 @@ def_t *bp_definition(file_t *f, const char *str)
if (!str) return NULL;
size_t namelen = (size_t)(str - name);
if (!matchchar(&str, ':')) return NULL;
vm_op_t *pat = bp_pattern(f, str);
pat_t *pat = bp_pattern(f, str);
if (!pat) return NULL;
matchchar(&pat->end, ';'); // TODO: verify this is safe to mutate
def_t *def = new(def_t);

View File

@ -8,15 +8,15 @@
#include "types.h"
__attribute__((nonnull))
vm_op_t *new_op(file_t *f, const char *start, enum VMOpcode type);
pat_t *new_pat(file_t *f, const char *start, enum VMOpcode type);
__attribute__((nonnull(1,2)))
vm_op_t *bp_simplepattern(file_t *f, const char *str);
pat_t *bp_simplepattern(file_t *f, const char *str);
__attribute__((nonnull(1,2)))
vm_op_t *bp_stringpattern(file_t *f, const char *str);
pat_t *bp_stringpattern(file_t *f, const char *str);
__attribute__((nonnull(1,2)))
vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement);
pat_t *bp_replacement(file_t *f, pat_t *pat, const char *replacement);
__attribute__((nonnull))
vm_op_t *bp_pattern(file_t *f, const char *str);
pat_t *bp_pattern(file_t *f, const char *str);
__attribute__((nonnull))
def_t *bp_definition(file_t *f, const char *str);

View File

@ -13,7 +13,7 @@
//
// Return a new list of definitions with one added to the front
//
def_t *with_def(def_t *defs, file_t *f, size_t namelen, const char *name, vm_op_t *op)
def_t *with_def(def_t *defs, file_t *f, size_t namelen, const char *name, pat_t *op)
{
def_t *def = new(def_t);
def->next = defs;
@ -38,7 +38,7 @@ def_t *load_grammar(def_t *defs, file_t *f)
check(src > name, "Invalid name for definition: %s", name);
size_t namelen = (size_t)(src - name);
check(matchchar(&src, ':'), "Expected ':' in definition");
vm_op_t *op = bp_pattern(f, src);
pat_t *op = bp_pattern(f, src);
if (op == NULL) break;
defs = with_def(defs, f, namelen, name, op);
src = op->end;
@ -70,7 +70,7 @@ def_t *lookup(def_t *defs, const char *name)
//
def_t *with_backref(def_t *defs, file_t *f, const char *name, match_t *m)
{
vm_op_t *op = new_op(f, m->start, VM_BACKREF);
pat_t *op = new_pat(f, m->start, VM_BACKREF);
op->end = m->end;
op->len = -1; // TODO: maybe calculate this? (nontrivial because of replacements)
op->args.backref = m;

View File

@ -8,7 +8,7 @@
#include "types.h"
__attribute__((nonnull(2,4,5), returns_nonnull))
def_t *with_def(def_t *defs, file_t *f, size_t namelen, const char *name, vm_op_t *op);
def_t *with_def(def_t *defs, file_t *f, size_t namelen, const char *name, pat_t *op);
__attribute__((nonnull(2,3,4), returns_nonnull))
def_t *with_backref(def_t *defs, file_t *f, const char *name, match_t *m);
__attribute__((nonnull(2)))

22
types.h
View File

@ -37,7 +37,7 @@ struct match_s; // forward declared to resolve circular struct defs
//
// A struct reperesenting a BP virtual machine operation
//
typedef struct vm_op_s {
typedef struct pat_s {
enum VMOpcode type;
const char *start, *end;
// Length of the match, if constant, otherwise -1
@ -49,19 +49,19 @@ typedef struct vm_op_s {
} range;
struct {
ssize_t min, max;
struct vm_op_s *sep, *repeat_pat;
struct pat_s *sep, *repeat_pat;
} repetitions;
// TODO: use a linked list instead of a binary tree
struct {
struct vm_op_s *first, *second;
struct pat_s *first, *second;
} multiple;
struct {
struct vm_op_s *pat;
struct pat_s *pat;
const char *text;
size_t len;
} replace;
struct {
struct vm_op_s *capture_pat;
struct pat_s *capture_pat;
char *name;
} capture;
struct match_s *backref;
@ -69,11 +69,11 @@ typedef struct vm_op_s {
struct match_s *match;
unsigned int visits;
const char *at;
struct vm_op_s *fallback;
struct pat_s *fallback;
} leftrec;
struct vm_op_s *pat;
struct pat_s *pat;
} args;
} vm_op_t;
} pat_t;
//
// Pattern matching result object
@ -82,7 +82,7 @@ typedef struct match_s {
// Where the match starts and ends (end is after the last character)
const char *start, *end;
struct match_s *child, *nextsibling;
vm_op_t *op;
pat_t *op;
// Intrusive linked list nodes for garbage collection:
struct match_s *next;
#ifdef DEBUG_HEAP
@ -103,7 +103,7 @@ typedef struct def_s {
size_t namelen;
const char *name;
file_t *file;
vm_op_t *op;
pat_t *op;
struct def_s *next;
} def_t;
@ -113,7 +113,7 @@ typedef struct def_s {
//
typedef struct allocated_op_s {
struct allocated_op_s *next;
vm_op_t op;
pat_t op;
} allocated_op_t;
#endif

12
vm.c
View File

@ -123,7 +123,7 @@ static const char *match_backref(const char *str, match_t *cap, unsigned int ign
//
// Find the next match after prev (or the first match if prev is NULL)
//
match_t *next_match(def_t *defs, file_t *f, match_t *prev, vm_op_t *op, unsigned int ignorecase)
match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *op, unsigned int ignorecase)
{
const char *str;
if (prev) {
@ -144,7 +144,7 @@ match_t *next_match(def_t *defs, file_t *f, match_t *prev, vm_op_t *op, unsigned
// a match struct, or NULL if no match is found.
// The returned value should be free()'d to avoid memory leaking.
//
match_t *match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned int ignorecase)
match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int ignorecase)
{
switch (op->type) {
case VM_LEFTRECURSION: {
@ -207,7 +207,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned in
m->start = str;
m->op = op;
vm_op_t *pat = op->args.multiple.first, *skip = op->args.multiple.second;
pat_t *pat = op->args.multiple.first, *skip = op->args.multiple.second;
if (!pat && !skip) {
while (str < f->end && *str != '\n') ++str;
m->end = str;
@ -416,9 +416,9 @@ match_t *match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned in
case VM_REF: {
def_t *def = lookup(defs, op->args.s);
check(def != NULL, "Unknown identifier: '%s'", op->args.s);
vm_op_t *ref = def->op;
pat_t *ref = def->op;
vm_op_t rec_op = {
pat_t rec_op = {
.type = VM_LEFTRECURSION,
.start = ref->start,
.end = ref->end,
@ -667,7 +667,7 @@ size_t free_all_matches(void)
//
// Deallocate memory associated with an op
//
void destroy_op(vm_op_t *op)
void destroy_op(pat_t *op)
{
switch (op->type) {
case VM_STRING: case VM_REF:

6
vm.h
View File

@ -9,13 +9,13 @@
#include "types.h"
__attribute__((nonnull(2,4)))
match_t *next_match(def_t *defs, file_t *f, match_t *prev, vm_op_t *op, unsigned int flags);
match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *op, unsigned int flags);
__attribute__((hot, nonnull(2,3,4)))
match_t *match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned int flags);
match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int flags);
__attribute__((nonnull))
match_t *get_capture(match_t *m, const char **id);
__attribute__((nonnull))
void destroy_op(vm_op_t *op);
void destroy_op(pat_t *op);
match_t *new_match(void);
__attribute__((nonnull))
void recycle_if_unused(match_t **at_m);