From 38ed25ce9bf87039a6180e386674d737484c4e39 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 16 Sep 2020 20:57:59 -0700 Subject: Moving more behavior over to definitions --- bpeg.c | 2 +- grammars/builtins.bpeg | 12 ++++++++---- vm.c | 21 +++++---------------- vm.h | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/bpeg.c b/bpeg.c index 237eef4..e2e5286 100644 --- a/bpeg.c +++ b/bpeg.c @@ -54,7 +54,7 @@ static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, unsig file_t *f = load_file(filename); match_t *m = match(g, f, f->contents, pattern, flags); if (m != NULL && m->end > m->start + 1) { - print_match(f, m, isatty(STDOUT_FILENO) ? "\033[0m" : NULL, (flags & BPEG_VERBOSE) != 0); + print_match(f, m); destroy_file(&f); return 0; } else { diff --git a/grammars/builtins.bpeg b/grammars/builtins.bpeg index 68a7791..4b200a9 100644 --- a/grammars/builtins.bpeg +++ b/grammars/builtins.bpeg @@ -4,17 +4,21 @@ no = !(/); # Meta-rules for acting on everything pattern = !(/); # Not defined by default replacement = !(/); # Not defined by default -replace-all = add-filename 1+(...@replacement) ...; +replace-all = add-filename 1+(...@hl-replacement) ...; find-all = add-filename 1+find-next%\n 0-1{!<\n => "\n"}; find-next = matching-line / {..\n =>} find-next; -only-matches = 1+{...@pattern=>'@1\n'}; -matching-line = add-linenum 1+(..@pattern) ..$; +only-matches = 1+{...hl-pattern=>'@1\n'}; +matching-line = add-line-number 1+(..hl-pattern) ..$; is-tty = no; # Defined as either always-match or always-fail, depending on stdout print-line-numbers = is-tty; print-filenames = is-tty; add-filename = 0-1(print-filenames (is-tty {=>"\033[33;1;4m@&:\033[0m\n"} / {=>"@&:\n"})); -add-linenum = 0-1(print-line-numbers (is-tty {=>"\033[2m@#\033[5G|\033[0m "} / {=>"@#| "})); +add-line-number = 0-1(print-line-numbers (is-tty {=>"\033[2m@#\033[5G|\033[0m "} / {=>"@#| "})); +highlight-matches = is-tty; +hl-pattern = { @pattern @0-1{highlight-matches => "\033[1;31m"} @0-1{highlight-matches => "\033[0m"} => "@2@1@3" }; +highlight-replacements = is-tty; +hl-replacement = { @replacement @0-1{highlight-replacements => "\033[1;35m"} @0-1{highlight-replacements => "\033[0m"} => "@2@1@3" }; # Helper definitions (commonly used) indent = \n|1+(\t/' '); diff --git a/vm.c b/vm.c index da2b7df..9579aee 100644 --- a/vm.c +++ b/vm.c @@ -529,10 +529,9 @@ static match_t *get_cap(match_t *m, const char **r) /* * Print a match with replacements and highlighting. */ -void print_match(file_t *f, match_t *m, const char *color, int verbose) +void print_match(file_t *f, match_t *m) { if (m->op->op == VM_REPLACE) { - if (color) printf("\033[0;34m"); for (const char *r = m->name_or_replacement; *r; ) { if (*r == '\\') { ++r; @@ -564,18 +563,12 @@ void print_match(file_t *f, match_t *m, const char *color, int verbose) } match_t *cap = get_cap(m, &r); if (cap != NULL) { - print_match(f, cap, color ? "\033[0;35m" : NULL, verbose); - if (color) printf("\033[0;34m"); + print_match(f, cap); } else { fputc('@', stdout); } } } else { - const char *name = m->name_or_replacement; - if (verbose && m->op->op == VM_REF && name) - printf(color ? "\033[0;2;35m{%s:" : "{%s", name); - //if (m->op->op == VM_CAPTURE && name) - // printf("\033[0;2;33m[%s:", name); const char *prev = m->start; for (match_t *child = m->child; child; child = child->nextsibling) { // Skip children from e.g. zero-width matches like >@foo @@ -583,16 +576,12 @@ void print_match(file_t *f, match_t *m, const char *color, int verbose) prev <= child->end && child->end <= m->end)) continue; if (child->start > prev) - printf("%s%.*s", color ? color : "", (int)(child->start - prev), prev); - print_match(f, child, color ? (m->op->op == VM_CAPTURE ? "\033[0;31;1m" : color) : NULL, verbose); + printf("%.*s", (int)(child->start - prev), prev); + print_match(f, child); prev = child->end; } if (m->end > prev) - printf("%s%.*s", color ? color : "", (int)(m->end - prev), prev); - if (verbose && m->op->op == VM_REF && name) - printf(color ? "\033[0;2;35m}" : "}"); - //if (m->op->op == VM_CAPTURE && name) - // printf("\033[0;2;33m]"); + printf("%.*s", (int)(m->end - prev), prev); } } diff --git a/vm.h b/vm.h index 2e2f888..e5e82bd 100644 --- a/vm.h +++ b/vm.h @@ -15,7 +15,7 @@ const char *opcode_name(enum VMOpcode o); match_t *match(grammar_t *g, file_t *f, const char *str, vm_op_t *op, unsigned int flags); void destroy_match(match_t **m); void print_pattern(vm_op_t *op); -void print_match(file_t *f, match_t *m, const char *color, int verbose); +void print_match(file_t *f, match_t *m); #endif // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 -- cgit v1.2.3