Imports cleanup and removing FILE* parameter from json

This commit is contained in:
Bruce Hill 2020-12-14 22:13:47 -08:00
parent 2fb68fa97f
commit 3acf397e6d
11 changed files with 40 additions and 29 deletions

View File

@ -2,6 +2,11 @@
* compiler.c - Compile strings into BPEG virtual machine code.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "compiler.h"
#include "utils.h"

View File

@ -4,10 +4,8 @@
#ifndef COMPILER__H
#define COMPILER__H
#include <stdlib.h>
#include "types.h"
#include "file_loader.h"
#include "types.h"
__attribute__((nonnull(1,2)))
vm_op_t *bpeg_simplepattern(file_t *f, const char *str);

View File

@ -2,9 +2,12 @@
* grammar.c - Code for defining grammars (sets of rules)
*/
#include "grammar.h"
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "file_loader.h"
#include "grammar.h"
#include "utils.h"
grammar_t *new_grammar(void)

View File

@ -4,9 +4,6 @@
#ifndef GRAMMAR__H
#define GRAMMAR__H
#include <stdlib.h>
#include <string.h>
#include "file_loader.h"
#include "types.h"

31
json.c
View File

@ -1,44 +1,47 @@
/*
* json.c - Code for printing JSON output of matches.
*/
#include <stdio.h>
#include "types.h"
/*
* Print a match as JSON
*/
static int _json_match(FILE *f, const char *text, match_t *m, int comma, int verbose)
static int _json_match(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);
comma |= _json_match(text, child, comma, verbose);
}
return comma;
}
}
if (comma) fprintf(f, ",\n");
if (comma) printf(",\n");
comma = 0;
fprintf(f, "{\"rule\":\"");
printf("{\"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;
case '"': printf("\\\""); break;
case '\\': printf("\\\\"); break;
case '\t': printf("\\t"); break;
case '\n': printf(""); break;
default: printf("%c", *c); break;
}
}
fprintf(f, "\",\"start\":%ld,\"end\":%ld,\"children\":[",
printf("\",\"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);
comma |= _json_match(text, child, comma, verbose);
}
fprintf(f, "]}");
printf("]}");
return 1;
}
void json_match(FILE *f, const char *text, match_t *m, int verbose)
void json_match(const char *text, match_t *m, int verbose)
{
_json_match(f, text, m, 0, verbose);
_json_match(text, m, 0, verbose);
}

2
json.h
View File

@ -5,7 +5,7 @@
#define JSON__H
__attribute__((nonnull))
void json_match(FILE *f, const char *text, match_t *m, int verbose);
void json_match(const char *text, match_t *m, int verbose);
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1

View File

@ -1,6 +1,10 @@
/*
* utils.c - Some helper code for debugging and error logging.
*/
#include <ctype.h>
#include <unistd.h>
#include "utils.h"
/*

View File

@ -4,9 +4,7 @@
#ifndef UTILS__H
#define UTILS__H
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

5
viz.h
View File

@ -1,6 +1,8 @@
/*
* Header file for viz.c (visualizing matches)
*/
#ifndef VIZ__H
#define VIZ__H
typedef struct match_node_s {
match_t *m;
@ -8,3 +10,6 @@ typedef struct match_node_s {
} match_node_t;
void visualize_match(match_t *m);
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1

3
vm.c
View File

@ -3,6 +3,9 @@
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "grammar.h"
#include "types.h"

5
vm.h
View File

@ -4,11 +4,6 @@
#ifndef VM__H
#define VM__H
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include "types.h"
typedef enum {