diff options
| -rw-r--r-- | bp.c | 4 | ||||
| -rw-r--r-- | compiler.c | 14 | ||||
| -rw-r--r-- | file_loader.c | 8 | ||||
| -rw-r--r-- | file_loader.h | 2 | ||||
| -rw-r--r-- | grammar.c | 2 | ||||
| -rw-r--r-- | json.c | 4 | ||||
| -rw-r--r-- | printing.c | 27 | ||||
| -rw-r--r-- | types.h | 10 | ||||
| -rw-r--r-- | vm.c | 52 |
9 files changed, 60 insertions, 63 deletions
@@ -179,7 +179,7 @@ static void sig_handler(int sig) { (void)sig; cleanup(); } static void confirm_replacements(file_t *f, match_t *m, confirm_t *confirm) { if (*confirm == CONFIRM_ALL) return; - if (m->op->type == VM_REPLACE) { + if (m->pat->type == VM_REPLACE) { if (*confirm == CONFIRM_NONE) { m->skip_replacement = 1; goto check_children; @@ -426,7 +426,7 @@ int main(int argc, char *argv[]) if (d) { d->next = defs; defs = d; - str = d->op->end; + str = d->pat->end; } else { pat_t *p = bp_pattern(arg_file, str); check(p, "Pattern failed to compile: %s", flag); @@ -30,12 +30,12 @@ static pat_t *new_range(file_t *f, const char *start, const char *end, ssize_t m pat_t *new_pat(file_t *f, const char *start, enum pattype_e type) { allocated_pat_t *tracker = new(allocated_pat_t); - tracker->next = f->ops; - f->ops = tracker; - tracker->op.type = type; - tracker->op.start = start; - tracker->op.len = -1; - return &tracker->op; + tracker->next = f->pats; + f->pats = tracker; + tracker->pat.type = type; + tracker->pat.start = start; + tracker->pat.len = -1; + return &tracker->pat; } // @@ -609,7 +609,7 @@ def_t *bp_definition(file_t *f, const char *str) def->file = f; def->namelen = namelen; def->name = name; - def->op = pat; + def->pat = pat; return def; } diff --git a/file_loader.c b/file_loader.c index f54a4d2..0d92241 100644 --- a/file_loader.c +++ b/file_loader.c @@ -151,10 +151,10 @@ void destroy_file(file_t **f) } } - for (allocated_pat_t *next; (*f)->ops; (*f)->ops = next) { - next = (*f)->ops->next; - destroy_pat(&(*f)->ops->op); - xfree(&(*f)->ops); + for (allocated_pat_t *next; (*f)->pats; (*f)->pats = next) { + next = (*f)->pats->next; + destroy_pat(&(*f)->pats->pat); + xfree(&(*f)->pats); } xfree(f); diff --git a/file_loader.h b/file_loader.h index 1e76357..78f3757 100644 --- a/file_loader.h +++ b/file_loader.h @@ -13,7 +13,7 @@ typedef struct file_s { const char *filename; char *contents, **lines, *end; size_t nlines; - struct allocated_pat_s *ops; + struct allocated_pat_s *pats; unsigned int mmapped:1; } file_t; @@ -20,7 +20,7 @@ def_t *with_def(def_t *defs, file_t *f, size_t namelen, const char *name, pat_t def->file = f; def->namelen = namelen; def->name = name; - def->op = op; + def->pat = op; return def; } @@ -17,7 +17,7 @@ static int _json_match(const char *text, match_t *m, int comma, unsigned int ver static int _json_match(const char *text, match_t *m, int comma, unsigned int verbose) { if (!verbose) { - if (m->op->type != VM_REF) { + if (m->pat->type != VM_REF) { for (match_t *child = m->child; child; child = child->nextsibling) { comma |= _json_match(text, child, comma, verbose); } @@ -28,7 +28,7 @@ static int _json_match(const char *text, match_t *m, int comma, unsigned int ver if (comma) printf(",\n"); comma = 0; printf("{\"rule\":\""); - for (const char *c = m->op->start; c < m->op->end; c++) { + for (const char *c = m->pat->start; c < m->pat->end; c++) { switch (*c) { case '"': printf("\\\""); break; case '\\': printf("\\\\"); break; @@ -60,8 +60,8 @@ static void _visualize_matches(match_node_t *firstmatch, int depth, const char * for (match_node_t *p = firstmatch; p; p = p->next) if (height_of_match(p->m) > height_of_match(viz)) viz = p->m; - const char *viz_type = viz->op->start; - size_t viz_typelen = (size_t)(viz->op->end - viz->op->start); + const char *viz_type = viz->pat->start; + size_t viz_typelen = (size_t)(viz->pat->end - viz->pat->start); // Backrefs use added dim quote marks to indicate that the pattern is a // literal string being matched. (Backrefs have start/end inside the text @@ -86,18 +86,15 @@ static void _visualize_matches(match_node_t *firstmatch, int depth, const char * match_node_t *children = NULL; match_node_t **nextchild = &children; -#define RIGHT_TYPE(m) (m->m->op->end == m->m->op->start + viz_typelen && strncmp(m->m->op->start, viz_type, viz_typelen) == 0) +#define RIGHT_TYPE(m) (m->m->pat->end == m->m->pat->start + viz_typelen && strncmp(m->m->pat->start, viz_type, viz_typelen) == 0) // Print nonzero-width first: for (match_node_t *m = firstmatch; m; m = m->next) { - //tree_text = byteslice(text, tree['start'], tree['end']).replace('\n', '↵') if (RIGHT_TYPE(m)) { - //if (m->m->op->op != VM_REF) { - for (match_t *c = m->m->child; c; c = c->nextsibling) { - *nextchild = new(match_node_t); - (*nextchild)->m = c; - nextchild = &((*nextchild)->next); - } - //} + for (match_t *c = m->m->child; c; c = c->nextsibling) { + *nextchild = new(match_node_t); + (*nextchild)->m = c; + nextchild = &((*nextchild)->next); + } if (m->m->end == m->m->start) continue; printf("\033[%ldG\033[0;2m%s\033[0;7;%sm", 1+2*(m->m->start - text), V, color); for (const char *c = m->m->start; c < m->m->end; ++c) { @@ -260,7 +257,7 @@ static const char *context_after(printer_t *pr, const char *pos) void _print_match(FILE *out, printer_t *pr, match_t *m) { pr->pos = m->start; - if (m->op->type == VM_REPLACE) { + if (m->pat->type == VM_REPLACE) { if (m->skip_replacement) { _print_match(out, pr, m->child); return; @@ -270,8 +267,8 @@ void _print_match(FILE *out, printer_t *pr, match_t *m) size_t line = line_start; if (pr->use_color) fprintf(out, "%s", color_replace); - const char *text = m->op->args.replace.text; - const char *end = &text[m->op->args.replace.len]; + const char *text = m->pat->args.replace.text; + const char *end = &text[m->pat->args.replace.len]; // TODO: clean up the line numbering code for (const char *r = text; r < end; ) { @@ -372,7 +369,7 @@ void print_match(FILE *out, printer_t *pr, match_t *m) int print_errors(printer_t *pr, match_t *m) { int ret = 0; - if (m->op->type == VM_CAPTURE && m->op->args.capture.name && streq(m->op->args.capture.name, "!")) { + if (m->pat->type == VM_CAPTURE && m->pat->args.capture.name && streq(m->pat->args.capture.name, "!")) { printf("\033[31;1m"); print_match(stdout, pr, m); printf("\033[0m\n"); @@ -80,7 +80,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; - pat_t *op; + pat_t *pat; // Intrusive linked list nodes for garbage collection: struct match_s *next; #ifdef DEBUG_HEAP @@ -101,17 +101,17 @@ typedef struct def_s { size_t namelen; const char *name; file_t *file; - pat_t *op; + pat_t *pat; struct def_s *next; } def_t; // -// Structure used for tracking allocated ops, which must be freed when the file -// is freed. +// Structure used for tracking allocated patterns, which must be freed when the +// file is freed. // typedef struct allocated_pat_s { struct allocated_pat_s *next; - pat_t op; + pat_t pat; } allocated_pat_t; #endif @@ -68,9 +68,9 @@ static inline const char *next_char(file_t *f, const char *str) // static const char *match_backref(const char *str, match_t *cap, unsigned int ignorecase) { - if (cap->op->type == VM_REPLACE) { - const char *text = cap->op->args.replace.text; - const char *end = &text[cap->op->args.replace.len]; + if (cap->pat->type == VM_REPLACE) { + const char *text = cap->pat->args.replace.text; + const char *end = &text[cap->pat->args.replace.len]; for (const char *r = text; r < end; ) { if (*r == '\\') { ++r; @@ -164,7 +164,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int if (str >= f->end || *str == '\n') return NULL; match_t *m = new_match(); - m->op = op; + m->pat = op; m->start = str; m->end = next_char(f, str); return m; @@ -175,7 +175,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int : memcmp(str, op->args.s, (size_t)op->len) != 0) return NULL; match_t *m = new_match(); - m->op = op; + m->pat = op; m->start = str; m->end = str + op->len; return m; @@ -185,7 +185,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int if ((unsigned char)*str < op->args.range.low || (unsigned char)*str > op->args.range.high) return NULL; match_t *m = new_match(); - m->op = op; + m->pat = op; m->start = str; m->end = str + 1; return m; @@ -197,7 +197,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int return NULL; } m = new_match(); - m->op = op; + m->pat = op; m->start = str; m->end = str; return m; @@ -205,7 +205,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int case VM_UPTO_AND: { match_t *m = new_match(); m->start = str; - m->op = op; + m->pat = op; pat_t *pat = op->args.multiple.first, *skip = op->args.multiple.second; if (!pat && !skip) { @@ -250,7 +250,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = str; m->end = str; - m->op = op; + m->pat = op; match_t **dest = &m->child; size_t reps = 0; @@ -310,7 +310,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = str; m->end = str; - m->op = op; + m->pat = op; ADD_OWNER(m->child, before); return m; } @@ -320,7 +320,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = str; m->end = str; - m->op = op; + m->pat = op; ADD_OWNER(m->child, after); return m; } @@ -330,7 +330,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = str; m->end = p->end; - m->op = op; + m->pat = op; ADD_OWNER(m->child, p); return m; } @@ -346,8 +346,8 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m2; { // Push backrefs and run matching, then cleanup def_t *defs2 = defs; - if (m1->op->type == VM_CAPTURE && m1->op->args.capture.name) - defs2 = with_backref(defs2, f, m1->op->args.capture.name, m1); + if (m1->pat->type == VM_CAPTURE && m1->pat->args.capture.name) + defs2 = with_backref(defs2, f, m1->pat->args.capture.name, m1); // def_t *defs2 = with_backrefs(defs, f, m1); m2 = match(defs2, f, m1->end, op->args.multiple.second, ignorecase); free_defs(&defs2, defs); @@ -360,7 +360,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = str; m->end = m2->end; - m->op = op; + m->pat = op; ADD_OWNER(m->child, m1); ADD_OWNER(m1->nextsibling, m2); return m; @@ -387,7 +387,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = m1->start; m->end = m1->end; - m->op = op; + m->pat = op; ADD_OWNER(m->child, m1); if (op->type == VM_EQUAL) { ADD_OWNER(m1->nextsibling, m2); @@ -404,7 +404,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int } match_t *m = new_match(); m->start = str; - m->op = op; + m->pat = op; if (p) { ADD_OWNER(m->child, p); m->end = p->end; @@ -416,7 +416,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int case VM_REF: { def_t *def = lookup(defs, op->args.s); check(def != NULL, "Unknown identifier: '%s'", op->args.s); - pat_t *ref = def->op; + pat_t *ref = def->pat; pat_t rec_op = { .type = VM_LEFTRECURSION, @@ -434,7 +434,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int .namelen = def->namelen, .name = def->name, .file = def->file, - .op = &rec_op, + .pat = &rec_op, .next = defs, }; @@ -470,7 +470,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int // match results. // OPTIMIZE: remove this if necessary match_t *m2 = new_match(); - m2->op = op; + m2->pat = op; m2->start = m->start; m2->end = m->end; ADD_OWNER(m2->child, m); @@ -480,7 +480,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int const char *end = match_backref(str, op->args.backref, ignorecase); if (end == NULL) return NULL; match_t *m = new_match(); - m->op = op; + m->pat = op; m->start = str; m->end = end; return m; @@ -509,7 +509,7 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int match_t *m = new_match(); m->start = start; m->end = &str[dents]; - m->op = op; + m->pat = op; return m; } default: { @@ -526,8 +526,8 @@ match_t *match(def_t *defs, file_t *f, const char *str, pat_t *op, unsigned int static match_t *get_capture_by_num(match_t *m, int *n) { if (*n == 0) return m; - if (m->op->type == VM_CAPTURE && *n == 1) return m; - if (m->op->type == VM_CAPTURE) --(*n); + if (m->pat->type == VM_CAPTURE && *n == 1) return m; + if (m->pat->type == VM_CAPTURE) --(*n); for (match_t *c = m->child; c; c = c->nextsibling) { match_t *cap = get_capture_by_num(c, n); if (cap) return cap; @@ -540,8 +540,8 @@ static match_t *get_capture_by_num(match_t *m, int *n) // static match_t *get_capture_by_name(match_t *m, const char *name) { - if (m->op->type == VM_CAPTURE && m->op->args.capture.name - && streq(m->op->args.capture.name, name)) + if (m->pat->type == VM_CAPTURE && m->pat->args.capture.name + && streq(m->pat->args.capture.name, name)) return m; for (match_t *c = m->child; c; c = c->nextsibling) { match_t *cap = get_capture_by_name(c, name); |
