aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2020-12-14 22:13:47 -0800
committerBruce Hill <bruce@bruce-hill.com>2020-12-14 22:13:47 -0800
commit3acf397e6d772778490d7da9f33f3a8f939c9efd (patch)
tree28b8ba262bb1b40670f87e55b183acf2f3bda6bd
parent2fb68fa97f096e5bd0f3ff81d31c3bc61719ae8a (diff)
Imports cleanup and removing FILE* parameter from json
-rw-r--r--compiler.c5
-rw-r--r--compiler.h4
-rw-r--r--grammar.c5
-rw-r--r--grammar.h3
-rw-r--r--json.c31
-rw-r--r--json.h2
-rw-r--r--utils.c4
-rw-r--r--utils.h2
-rw-r--r--viz.h5
-rw-r--r--vm.c3
-rw-r--r--vm.h5
11 files changed, 40 insertions, 29 deletions
diff --git a/compiler.c b/compiler.c
index f178ce6..4efecc6 100644
--- a/compiler.c
+++ b/compiler.c
@@ -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"
diff --git a/compiler.h b/compiler.h
index 9e750b6..f7e8b43 100644
--- a/compiler.h
+++ b/compiler.h
@@ -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);
diff --git a/grammar.c b/grammar.c
index d55865a..85a9610 100644
--- a/grammar.c
+++ b/grammar.c
@@ -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)
diff --git a/grammar.h b/grammar.h
index 985f814..8e1980d 100644
--- a/grammar.h
+++ b/grammar.h
@@ -4,9 +4,6 @@
#ifndef GRAMMAR__H
#define GRAMMAR__H
-#include <stdlib.h>
-#include <string.h>
-
#include "file_loader.h"
#include "types.h"
diff --git a/json.c b/json.c
index d45e79c..2c9b50a 100644
--- a/json.c
+++ b/json.c
@@ -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);
}
diff --git a/json.h b/json.h
index 2a5a62a..5542256 100644
--- a/json.h
+++ b/json.h
@@ -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
diff --git a/utils.c b/utils.c
index 05eaec3..2cc5258 100644
--- a/utils.c
+++ b/utils.c
@@ -1,6 +1,10 @@
/*
* utils.c - Some helper code for debugging and error logging.
*/
+
+#include <ctype.h>
+#include <unistd.h>
+
#include "utils.h"
/*
diff --git a/utils.h b/utils.h
index d9636ce..bd79e3f 100644
--- a/utils.h
+++ b/utils.h
@@ -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>
diff --git a/viz.h b/viz.h
index 80d8cee..a918661 100644
--- a/viz.h
+++ b/viz.h
@@ -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
diff --git a/vm.c b/vm.c
index 6d40507..63c35e3 100644
--- a/vm.c
+++ b/vm.c
@@ -3,6 +3,9 @@
*/
#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
#include "grammar.h"
#include "types.h"
diff --git a/vm.h b/vm.h
index 6dde1d3..2b34594 100644
--- a/vm.h
+++ b/vm.h
@@ -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 {