diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2020-12-14 21:55:36 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2020-12-14 21:55:36 -0800 |
| commit | 1ff4a9eb0548b94839cc936f932f75af38a00f51 (patch) | |
| tree | dbc907c0ae3aa24e0231b43f2ce624d78e836809 | |
| parent | 52f154d53ffa9ac8fc4699d1559f9a68e91ebeeb (diff) | |
Hooked up json
| -rw-r--r-- | bpeg.c | 16 | ||||
| -rw-r--r-- | types.h | 1 | ||||
| -rw-r--r-- | vm.c | 59 | ||||
| -rw-r--r-- | vm.h | 2 |
4 files changed, 25 insertions, 53 deletions
@@ -80,10 +80,10 @@ static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, unsig ++printed_matches; if (flags & BPEG_EXPLAIN) { - if (filename) { + if (filename) printf("\033[1;4m%s\033[0m\n", filename); - } - /* + visualize_match(m); + } else if (flags & BPEG_JSON) { if (printed_matches > 1) fprintf(stdout, ",\n"); printf("{\"filename\":\"%s\",\"text\":\"", filename ? filename : "-"); @@ -96,12 +96,10 @@ static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, unsig default: printf("%c", *c); break; } } - printf("\",\n\"tree\":{\"type\":\"text\",\"start\":%d,\"end\":%ld,\"children\":[", + printf("\",\n\"tree\":{\"rule\":\"text\",\"start\":%d,\"end\":%ld,\"children\":[", 0, f->end - f->contents); - json_match(stdout, f->contents, m); + json_match(stdout, f->contents, m, (flags & BPEG_VERBOSE) ? 1 : 0); printf("]}}\n"); - */ - visualize_match(m); } else { if (printed_matches > 1) fputc('\n', stdout); @@ -152,6 +150,8 @@ int main(int argc, char *argv[]) flags |= BPEG_VERBOSE; } else if (streq(argv[i], "--explain") || streq(argv[i], "-e")) { flags |= BPEG_EXPLAIN; + } else if (streq(argv[i], "--json") || streq(argv[i], "-j")) { + flags |= BPEG_JSON; } else if (streq(argv[i], "--ignore-case") || streq(argv[i], "-i")) { flags |= BPEG_IGNORECASE; } else if (FLAG("--replace") || FLAG("-r")) { @@ -229,6 +229,7 @@ int main(int argc, char *argv[]) check(pattern != NULL, "No such rule: '%s'", rule); int ret = 1; + if (flags & BPEG_JSON) printf("["); if (i < argc) { // Files pass in as command line args: for (int nfiles = 0; i < argc; nfiles++, i++) { @@ -247,6 +248,7 @@ int main(int argc, char *argv[]) // Piped in input: ret &= run_match(g, NULL, pattern, flags); } + if (flags & BPEG_JSON) printf("]"); return ret; } @@ -12,6 +12,7 @@ enum BPEGFlag { BPEG_VERBOSE = 1 << 0, BPEG_IGNORECASE = 1 << 1, BPEG_EXPLAIN = 1 << 2, + BPEG_JSON = 1 << 3, }; /* @@ -595,13 +595,20 @@ void print_match(file_t *f, match_t *m, print_options_t options) /* * Print a match as JSON */ -static int _json_match(FILE *f, const char *text, match_t *m, int comma) -#define VERBOSE_JSON 1 -#if VERBOSE_JSON +static int _json_match(FILE *f, const char *text, match_t *m, int comma, int verbose) { + if (!verbose) { + if (m->op->op != VM_REF) { + for (match_t *child = m->child; child; child = child->nextsibling) { + comma |= _json_match(f, text, child, comma, verbose); + } + return comma; + } + } + if (comma) fprintf(f, ",\n"); comma = 0; - fprintf(f, "{\"type\":\""); + fprintf(f, "{\"rule\":\""); for (const char *c = m->op->start; c < m->op->end; c++) { switch (*c) { case '"': fprintf(f, "\\\""); break; @@ -614,53 +621,15 @@ static int _json_match(FILE *f, const char *text, match_t *m, int comma) fprintf(f, "\",\"start\":%ld,\"end\":%ld,\"children\":[", m->start - text, m->end - text); for (match_t *child = m->child; child; child = child->nextsibling) { - comma |= _json_match(f, text, child, comma); + comma |= _json_match(f, text, child, comma, verbose); } fprintf(f, "]}"); return 1; } -#else -{ - if (m->op->op == VM_STRING) { - if (comma) fprintf(f, ",\n"); - comma = 0; - fprintf(f, "{\"type\":\"\\\""); - for (const char *c = m->op->args.s; *c; c++) { - switch (*c) { - case '"': fprintf(f, "\\\""); break; - case '\\': fprintf(f, "\\\\"); break; - case '\t': fprintf(f, "\\t"); break; - case '\n': fprintf(f, "↵"); break; - default: fprintf(f, "%c", *c); break; - } - } - fprintf(f, "\\\"\",\"start\":%ld,\"end\":%ld,\"children\":[", - m->start - text, m->end - text); - } else if (m->op->op == VM_REF) { - if (comma) fprintf(f, ",\n"); - comma = 0; - fprintf(f, "{\"type\":\"%s\",\"start\":%ld,\"end\":%ld,\"children\":[", - m->op->args.s, m->start - text, m->end - text); - } else if (m->op->op == VM_CAPTURE && m->value.name) { - if (comma) fprintf(f, ",\n"); - comma = 0; - fprintf(f, "{\"type\":\"@%s\",\"start\":%ld,\"end\":%ld,\"children\":[", - m->value.name, m->start - text, m->end - text); - } - for (match_t *child = m->child; child; child = child->nextsibling) { - comma |= _json_match(f, text, child, comma); - } - if (m->op->op == VM_REF || m->op->op == VM_STRING || (m->op->op == VM_CAPTURE && m->value.name)) { - fprintf(f, "]}"); - return 1; - } - return comma; -} -#endif -void json_match(FILE *f, const char *text, match_t *m) +void json_match(FILE *f, const char *text, match_t *m, int verbose) { - _json_match(f, text, m, 0); + _json_match(f, text, m, 0, verbose); } static match_t *match_backref(const char *str, vm_op_t *op, match_t *cap, unsigned int flags) @@ -24,7 +24,7 @@ void destroy_match(match_t **m); __attribute__((nonnull)) void print_match(file_t *f, match_t *m, print_options_t options); __attribute__((nonnull)) -void json_match(FILE *f, const char *text, match_t *m); +void json_match(FILE *f, const char *text, match_t *m, int verbose); #endif // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 |
