aboutsummaryrefslogtreecommitdiff
path: root/bpeg.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2020-09-17 00:29:11 -0700
committerBruce Hill <bruce@bruce-hill.com>2020-09-17 00:29:11 -0700
commit67e538e774b37749c53c553b941736a281e6ac8f (patch)
treecc02c6622c831b2cb2dad7e3128e9a9e3dbb3ded /bpeg.c
parent2477d9869c295cbaa1e948fc6e40190aa7149295 (diff)
Some error handling
Diffstat (limited to 'bpeg.c')
-rw-r--r--bpeg.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/bpeg.c b/bpeg.c
index 55c2e98..1c934ab 100644
--- a/bpeg.c
+++ b/bpeg.c
@@ -49,11 +49,28 @@ static char *getflag(const char *flag, char *argv[], int *i)
return NULL;
}
+static int print_errors(file_t *f, match_t *m)
+{
+ int ret = 0;
+ if (m->op->op == VM_CAPTURE && m->value.name && streq(m->value.name, "!")) {
+ printf("\033[31;1m");
+ print_match(f, m);
+ printf("\033[0m\n");
+ fprint_line(stdout, f, m->start, m->end, "");
+ return 1;
+ }
+ if (m->child) ret += print_errors(f, m->child);
+ if (m->nextsibling) ret += print_errors(f, m->nextsibling);
+ return ret;
+}
+
static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, unsigned int flags)
{
file_t *f = load_file(filename);
check(f, "Could not open file: %s", filename);
match_t *m = match(g, f, f->contents, pattern, flags);
+ if (m && print_errors(f, m) > 0)
+ _exit(1);
if (m != NULL && m->end > m->start + 1) {
print_match(f, m);
destroy_file(&f);