-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.c
277 lines (223 loc) · 6.19 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2014-2017 Katherine Flavel
*
* See LICENCE for the full copyright terms.
*/
#define _POSIX_C_SOURCE 2
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "txt.h"
#include "ast.h"
#include "parsing_error.h"
#include "rewrite.h"
#include "xalloc.h"
#include "rrd/node.h"
#include "bnf/io.h"
#include "blab/io.h"
#include "ebnfhtml5/io.h"
#include "wsn/io.h"
#include "abnf/io.h"
#include "iso-ebnf/io.h"
#include "rbnf/io.h"
#include "sid/io.h"
#include "dot/io.h"
#include "rrdot/io.h"
#include "rrdump/io.h"
#include "rrtdump/io.h"
#include "rrparcon/io.h"
#include "rrll/io.h"
#include "rrta/io.h"
#include "rrtext/io.h"
#include "svg/io.h"
#include "html5/io.h"
#include "json/io.h"
int debug = 0;
int prettify = 1;
int allow_undefined = 1;
const char *css_file;
/*
* emscripten's musl will call __wait from getopt() (via getopt_msg and
* then flockfile), which is not present for standalone wasm because that
* has no pthread support, and so a definition for __wait is not included.
*/
#ifdef __EMSCRIPTEN__
void __wait(int a, int b, int c, int d) { }
#endif
struct io {
const char *name;
struct ast_rule *(*in)(int (*f)(void *), void *, parsing_error_queue*);
int (*out)(const struct ast_rule *);
enum ast_features ast_unsupported;
enum rrd_features rrd_unsupported;
} io[] = {
{ "bnf", bnf_input, bnf_output, bnf_ast_unsupported, 0 },
{ "blab", NULL, blab_output, blab_ast_unsupported, 0 },
{ "ebnfhtml5", NULL, ebnf_html5_output, ebnf_html5_ast_unsupported, 0 },
{ "ebnfxhtml5", NULL, ebnf_xhtml5_output, ebnf_html5_ast_unsupported, 0 },
{ "wsn", wsn_input, wsn_output, wsn_ast_unsupported, 0 },
{ "abnf", abnf_input, abnf_output, 0, 0 },
{ "iso-ebnf", iso_ebnf_input, iso_ebnf_output, iso_ebnf_ast_unsupported, 0 },
{ "rbnf", rbnf_input, rbnf_output, rbnf_ast_unsupported, 0 },
{ "sid", NULL, sid_output, sid_ast_unsupported, 0 },
{ "dot", NULL, dot_output, 0, 0 },
{ "rrdot", NULL, rrdot_output, 0, 0 },
{ "rrdump", NULL, rrdump_output, 0, 0 },
{ "rrtdump", NULL, rrtdump_output, 0, 0 },
{ "rrparcon", NULL, rrparcon_output, rrparcon_ast_unsupported, rrparcon_rrd_unsupported },
{ "rrll", NULL, rrll_output, rrll_ast_unsupported, rrll_rrd_unsupported },
{ "rrta", NULL, rrta_output, rrta_ast_unsupported, rrta_rrd_unsupported },
{ "rrtext", NULL, rrtext_output, 0, 0 },
{ "rrutf8", NULL, rrutf8_output, 0, 0 },
{ "svg", NULL, svg_output, 0, 0 },
{ "html5", NULL, html5_output, 0, 0 },
{ "xhtml5", NULL, xhtml5_output, 0, 0 },
{ "json", NULL, json_output, json_ast_unsupported, 0 }
};
enum io_dir {
IO_IN,
IO_OUT
};
static void
xusage(void)
{
printf("usage: kgt [-nu] [-w <whitelist>] [-l <language>] [ -e <language> ]\n");
exit(EXIT_FAILURE);
}
static int
kgt_fgetc(void *opaque)
{
FILE *f;
f = opaque;
assert(f != NULL);
return fgetc(f);
}
static struct io *
lang(enum io_dir dir, const char *s)
{
size_t i;
for (i = 0; i < sizeof io / sizeof *io; i++) {
if (dir == IO_IN && io[i].in == NULL) {
continue;
}
if (dir == IO_OUT && io[i].out == NULL) {
continue;
}
if (0 == strcmp(s, io[i].name)) {
return &io[i];
}
}
fprintf(stderr, "Unrecognised %s language \"%s\"; supported languages are:",
dir == IO_IN ? "input" : "output",
s);
for (i = 0; i < sizeof io / sizeof *io; i++) {
if (dir == IO_IN && io[i].in == NULL) {
continue;
}
if (dir == IO_OUT && io[i].out == NULL) {
continue;
}
fprintf(stderr, " %s", io[i].name);
}
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
struct ast_rule *g;
struct io *in, *out;
const char *filter;
parsing_error_queue errors = NULL;
in = lang(IO_IN, "bnf");
out = in;
filter = NULL;
{
int c;
while (c = getopt(argc, argv, "hw:c:gnl:e:u"), c != -1) {
switch (c) {
case 'l': in = lang(IO_IN, optarg); break;
case 'e': out = lang(IO_OUT, optarg); break;
case 'c': css_file = optarg; break;
case 'w': filter = optarg; break; /* comma-separated whitelist of rule names */
case 'g': debug = 1; break;
case 'n': prettify = 0; break;
case 'u': allow_undefined = 0; break;
case '?':
default:
xusage();
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
xusage();
}
}
assert(io->in != NULL);
assert(io->out != NULL);
g = in->in(kgt_fgetc, stdin, &errors);
{
int error_count = 0;
while (errors) {
parsing_error error;
parsing_error_queue_pop(&errors, &error);
fprintf(stderr, "%u:%u: %s\n", error.line, error.col, error.description);
error_count += 1;
}
if (error_count != 0) {
fprintf(stderr, "KGT: Exiting. %d errors reported\n", error_count);
exit(EXIT_FAILURE);
}
}
{
unsigned v;
for (v = out->ast_unsupported; v != 0; v &= v - 1) {
/* TODO: expose these rewritings as CLI options too; set as bits in v */
/* TODO: option to query if output is possible without rewriting */
switch (v & -v) {
case FEATURE_AST_CI_LITERAL:
if (!rewrite_ci_literals(g)) {
return EXIT_FAILURE;
}
break;
case FEATURE_AST_INVISIBLE: rewrite_invisible(g); break;
case FEATURE_AST_BINARY:
if (ast_binary(g)) {
fprintf(stderr, "Binary strings not supported for this output language\n");
}
break;
}
}
}
if (filter != NULL) {
struct ast_rule *new, **tail;
struct ast_rule *p, *next;
new = NULL;
tail = &new;
for (p = g; p != NULL; p = next) {
char *tmp, *save;
const char *t;
next = p->next;
tmp = xstrdup(filter);
for (t = strtok_r(tmp, ",", &save); t != NULL; t = strtok_r(NULL, ",", &save)) {
if (0 == strcmp(p->name, t)) {
p->next = *tail;
*tail = p;
break;
}
/* TODO: otherwise free *p */
}
free(tmp);
}
g = new;
}
if (!out->out(g)) {
return EXIT_FAILURE;
}
/* TODO: free ast */
return EXIT_SUCCESS;
}