Moved json code into its own file
This commit is contained in:
parent
e638a517bf
commit
2fb68fa97f
2
Makefile
2
Makefile
@ -7,7 +7,7 @@ CWARN=-Wall -Wpedantic -Wextra -Wno-unknown-pragmas -Wno-missing-field-initializ
|
|||||||
G ?=
|
G ?=
|
||||||
O ?= -O3
|
O ?= -O3
|
||||||
|
|
||||||
CFILES=compiler.c grammar.c utils.c vm.c file_loader.c viz.c
|
CFILES=compiler.c grammar.c utils.c vm.c file_loader.c viz.c json.c
|
||||||
OBJFILES=$(CFILES:.c=.o)
|
OBJFILES=$(CFILES:.c=.o)
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
44
json.c
Normal file
44
json.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* json.c - Code for printing JSON output of matches.
|
||||||
|
*/
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print a match as 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, "{\"rule\":\"");
|
||||||
|
for (const char *c = m->op->start; c < m->op->end; 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);
|
||||||
|
for (match_t *child = m->child; child; child = child->nextsibling) {
|
||||||
|
comma |= _json_match(f, text, child, comma, verbose);
|
||||||
|
}
|
||||||
|
fprintf(f, "]}");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void json_match(FILE *f, const char *text, match_t *m, int verbose)
|
||||||
|
{
|
||||||
|
_json_match(f, text, m, 0, verbose);
|
||||||
|
}
|
11
json.h
Normal file
11
json.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* json.h - Header file for JSON output.
|
||||||
|
*/
|
||||||
|
#ifndef JSON__H
|
||||||
|
#define JSON__H
|
||||||
|
|
||||||
|
__attribute__((nonnull))
|
||||||
|
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
|
43
vm.c
43
vm.c
@ -4,9 +4,10 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "vm.h"
|
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
|
#include "types.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include "vm.h"
|
||||||
|
|
||||||
static match_t *match_backref(const char *str, vm_op_t *op, match_t *m, unsigned int flags);
|
static match_t *match_backref(const char *str, vm_op_t *op, match_t *m, unsigned int flags);
|
||||||
static size_t push_backrefs(grammar_t *g, match_t *m);
|
static size_t push_backrefs(grammar_t *g, match_t *m);
|
||||||
@ -592,46 +593,6 @@ void print_match(file_t *f, match_t *m, print_options_t options)
|
|||||||
_print_match(f, m, &state, options);
|
_print_match(f, m, &state, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Print a match as 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, "{\"rule\":\"");
|
|
||||||
for (const char *c = m->op->start; c < m->op->end; 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);
|
|
||||||
for (match_t *child = m->child; child; child = child->nextsibling) {
|
|
||||||
comma |= _json_match(f, text, child, comma, verbose);
|
|
||||||
}
|
|
||||||
fprintf(f, "]}");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void json_match(FILE *f, const char *text, match_t *m, int verbose)
|
|
||||||
{
|
|
||||||
_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)
|
static match_t *match_backref(const char *str, vm_op_t *op, match_t *cap, unsigned int flags)
|
||||||
{
|
{
|
||||||
check(op->op == VM_BACKREF, "Attempt to match backref against something that's not a backref");
|
check(op->op == VM_BACKREF, "Attempt to match backref against something that's not a backref");
|
||||||
|
Loading…
Reference in New Issue
Block a user