diff options
| -rw-r--r-- | match.c | 60 | ||||
| -rw-r--r-- | match.h | 2 | ||||
| -rw-r--r-- | print.c | 59 |
3 files changed, 59 insertions, 62 deletions
@@ -2,7 +2,6 @@ // match.c - Code for the BP virtual machine that performs the matching. // -#include <ctype.h> #include <err.h> #include <limits.h> #include <stdbool.h> @@ -30,10 +29,6 @@ static match_t *in_use_matches = NULL; __attribute__((nonnull(1))) static inline pat_t *deref(def_t *defs, pat_t *pat); -__attribute__((nonnull)) -static match_t *get_capture_by_num(match_t *m, int *n); -__attribute__((nonnull, pure)) -static match_t *get_capture_by_name(match_t *m, const char *name); __attribute__((hot, nonnull(2,3,4))) static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool ignorecase); @@ -562,61 +557,6 @@ static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool } // -// Get a specific numbered pattern capture. -// -static match_t *get_capture_by_num(match_t *m, int *n) -{ - if (*n == 0) return m; - if (m->pat->type == BP_CAPTURE && *n == 1) return m; - if (m->pat->type == BP_CAPTURE) --(*n); - if (m->children) { - for (int i = 0; m->children[i]; i++) { - match_t *cap = get_capture_by_num(m->children[i], n); - if (cap) return cap; - } - } - return NULL; -} - -// -// Get a capture with a specific name. -// -static match_t *get_capture_by_name(match_t *m, const char *name) -{ - if (m->pat->type == BP_CAPTURE && m->pat->args.capture.name - && strncmp(m->pat->args.capture.name, name, m->pat->args.capture.namelen) == 0) - return m; - if (m->children) { - for (int i = 0; m->children[i]; i++) { - match_t *cap = get_capture_by_name(m->children[i], name); - if (cap) return cap; - } - } - return NULL; -} - -// -// Get a capture by identifier (name or number). -// Update *id to point to after the identifier (if found). -// -match_t *get_capture(match_t *m, const char **id) -{ - if (isdigit(**id)) { - int n = (int)strtol(*id, (char**)id, 10); - return get_capture_by_num(m, &n); - } else { - const char *end = after_name(*id); - if (end == *id) return NULL; - char *name = strndup(*id, (size_t)(end-*id)); - match_t *cap = get_capture_by_name(m, name); - delete(&name); - *id = end; - if (**id == ';') ++(*id); - return cap; - } -} - -// // Return a match object which can be used (may be allocated or recycled). // match_t *new_match(def_t *defs, pat_t *pat, const char *start, const char *end, match_t *children[]) @@ -15,8 +15,6 @@ match_t *new_match(def_t *defs, pat_t *pat, const char *start, const char *end, __attribute__((nonnull(2,4))) match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *pat, pat_t *skip, bool ignorecase); __attribute__((nonnull)) -match_t *get_capture(match_t *m, const char **id); -__attribute__((nonnull)) void recycle_if_unused(match_t **at_m); size_t free_all_matches(void); size_t recycle_all_matches(void); @@ -2,6 +2,7 @@ // print.c - Code for printing and visualizing matches. // +#include <ctype.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -114,6 +115,64 @@ static const char *context_after(printer_t *pr, const char *pos) } // +// Get a specific numbered pattern capture. +// +__attribute__((nonnull)) +static match_t *get_capture_by_num(match_t *m, int *n) +{ + if (*n == 0) return m; + if (m->pat->type == BP_CAPTURE && *n == 1) return m; + if (m->pat->type == BP_CAPTURE) --(*n); + if (m->children) { + for (int i = 0; m->children[i]; i++) { + match_t *cap = get_capture_by_num(m->children[i], n); + if (cap) return cap; + } + } + return NULL; +} + +// +// Get a capture with a specific name. +// +__attribute__((nonnull, pure)) +static match_t *get_capture_by_name(match_t *m, const char *name) +{ + if (m->pat->type == BP_CAPTURE && m->pat->args.capture.name + && strncmp(m->pat->args.capture.name, name, m->pat->args.capture.namelen) == 0) + return m; + if (m->children) { + for (int i = 0; m->children[i]; i++) { + match_t *cap = get_capture_by_name(m->children[i], name); + if (cap) return cap; + } + } + return NULL; +} + +// +// Get a capture by identifier (name or number). +// Update *id to point to after the identifier (if found). +// +__attribute__((nonnull)) +static match_t *get_capture(match_t *m, const char **id) +{ + if (isdigit(**id)) { + int n = (int)strtol(*id, (char**)id, 10); + return get_capture_by_num(m, &n); + } else { + const char *end = after_name(*id); + if (end == *id) return NULL; + char *name = strndup(*id, (size_t)(end-*id)); + match_t *cap = get_capture_by_name(m, name); + delete(&name); + *id = end; + if (**id == ';') ++(*id); + return cap; + } +} + +// // Print the text of a match (no context). // static void _print_match(FILE *out, printer_t *pr, match_t *m) |
