forked from advancingdragon/meta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupport.h
383 lines (374 loc) · 10.7 KB
/
support.h
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifndef META_SUPPORT
#define META_SUPPORT
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <stdbool.h>
#define STACK_DEPTH 20000
static long user_token_stack[STACK_DEPTH];
static long utoken_stack_top=0;
static char *parse_stack[STACK_DEPTH];
static long parse_stack_top=0;
static long token_line=0, token_column=0,
bytes_written=0,input_len=0,
pos=0,pos_last_line=0,line=1;
static FILE *output;
static char *source=NULL, *token=NULL, *capture=NULL,
*input_name=NULL, *output_name=NULL;
static bool test_flag=false, ignore_whitespace=false, mute=false;
static char *expecting=NULL, *unexpected=NULL, *reason=NULL;
static void make_token (int start_pos);
static void no_unused_fwa ();
static void(*on_error)(void) = NULL;
static inline void start_line (void) {
pos_last_line=pos+1;
line++;
}
static char* concatstr (char *txt, const char *concat) {
int txtlen = strlen(txt),
conclen = strlen(concat);
char *newbuf = realloc(txt,txtlen+conclen+1);
if (newbuf == 0) return 0;
strcpy(newbuf+txtlen,concat);
return newbuf;
}
static inline void enter_parse_rule (char *parse_rule) {
parse_stack[parse_stack_top++]=parse_rule;
}
static inline void exit_parse_rule () {
parse_stack_top--;
}
static inline void start_user_token () {
user_token_stack[utoken_stack_top++]=pos;
}
static inline void end_user_token () {
make_token(user_token_stack[--utoken_stack_top]);
}
static inline bool is_space (char c) {
return (c==' ' || c=='\t' || c=='\r' || c=='\n');
}
static void skip_whitespace (void) {
test_flag=false;
while (is_space(source[pos]) && pos<input_len) {
test_flag=true;
if (source[pos] == '\n') {
start_line(); }
pos++; }
}
static void make_token (int start_pos) {
token_line = line;
token_column = start_pos-pos_last_line+1;
int length = pos-start_pos;
if (token) free(token);
token = malloc(length + 1);
token[length] = '\0';
memcpy(token, &source[start_pos], length);
}
static void capture_to_token () {
if (!capture) return;
if (token) free(token);
token=capture;
capture=NULL;
}
static void emit (const char *str) {
if (mute) return;
if (!capture) {
fprintf(output, "%s", str); bytes_written += strlen(str); }
else {
capture = concatstr(capture,str); }
}
static void emit_char (char c) {
if (mute) return;
if (!capture) {
fputc(c, output); bytes_written++; }
else {
capture = concatstr(capture,(char[2]){c,'\0'}); }
}
static void emit_token (int quote) {
if (mute || !token) return;
if (!quote && strlen(token) == 1) {
emit_char(token[0]); return; }
int i=1;
if (token[0] == '\'' || token[0] == '"') {
if (quote) emit_char('"');
for (; token[i+1] != '\0'; i++) {
switch (token[i]) {
case '"':
if (!quote) goto def_case;
emit("\\\"");
break;
case '\n':
if (!quote) goto def_case;
emit("\\n");
break;
case '\r':
if (!quote) goto def_case;
emit("\\r");
break;
case '\\':
if (!quote) {
i++;
switch(token[i]) {
case 'n': emit_char('\n'); break;
case 'r': emit_char('\r'); break;
default: goto def_case; }
break; }
default:
def_case:
emit_char(token[i]); }}
if (quote) emit_char('"');
return; }
emit(token);
}
static void read_literal (const char *literal) {
if (!ignore_whitespace) skip_whitespace();
test_flag=true;
long new_last_line=pos_last_line, new_lines=0, i=0;
for (; (pos+i)<input_len && literal[i]!='\0'; i++) {
if (source[pos+i] != literal[i]) {
test_flag=false;
return; }
if (source[pos+i] == '\n') {
new_last_line=pos+i+1;
new_lines++; }}
if (literal[i]!='\0') {
test_flag=false;
return; }
pos_last_line = new_last_line; line += new_lines; pos += i;
make_token(pos-i);
}
static void read_string () {
if (!ignore_whitespace) skip_whitespace();
test_flag=true;
if (!(pos<input_len && (source[pos] == '\'' || source[pos] == '"'))) {
test_flag=false;
return; }
long new_last_line=pos_last_line, new_lines=0, i=1;
for (; (pos+i)<input_len && source[pos+i] != source[pos]; i++) {
switch (source[pos+i]) {
case '\n': new_last_line=pos+i+1; new_lines++; break;
case '\\': i++; }}
pos += ++i;
pos_last_line = new_last_line; line += new_lines;
make_token(pos-i);
}
static inline bool alpha_und (char c) {
return ('A'<=c && c<='Z') || ('a'<=c && c<='z') || c=='_';
}
static inline bool numeric (char c) {
return ('0'<=c && c<='9');
}
static void read_id () {
if (!ignore_whitespace) skip_whitespace();
test_flag=true;
if (!alpha_und(source[pos])) {
test_flag=false; return; }
long i=1;
for (; (pos+i)<input_len &&
(alpha_und(source[pos+i]) || numeric(source[pos+i])); i++);;
pos += i;
make_token(pos-i);
}
static void read_number () {
if (!ignore_whitespace) skip_whitespace();
test_flag=true;
if (!numeric(source[pos])) {
test_flag=false;
return; }
long i=1;
for (; (pos+i)<input_len && numeric(source[pos+i]); i++);;
pos += i;
make_token(pos-i);
}
static void read_any_between (char first, char last) {
if (pos<input_len && source[pos] >= first && source[pos] <= last) {
if (source[pos] == '\n') {
start_line(); }
pos++;
test_flag=true;
return; }
test_flag=false;
}
static void read_any_but (char first, char last) {
if (pos<input_len && (source[pos]<first || source[pos] > last)) {
if (source[pos] == '\n') {
start_line(); }
pos++;
test_flag=true;
return; }
test_flag=false;
}
static void read_char_eq (char which) {
if (pos<input_len && source[pos] == which) {
if (which == '\n') {
start_line(); }
pos++;
test_flag=true;
return; }
test_flag=false;
}
static void read_char_neq (char which) {
if (pos<input_len && source[pos] != which) {
if (which == '\n') {
start_line(); }
pos++;
test_flag=true;
return; }
test_flag=false;
}
static void initialize_parser () {
if (output) { fclose(output); output=NULL; }
if (token) { free(token); token=NULL; }
if (output_name) {
if ((char*)output_name != (char*)"stdout") free(output_name);
output_name=NULL; }
if (input_name) {
if ((char*)input_name != (char*)"stdin") free(input_name);
input_name=NULL; }
if (source) { free(source); source=NULL; }
if (capture) { free(capture); capture=NULL; }
parse_stack_top=0; utoken_stack_top=0;
pos=0; pos_last_line=0; line=1;
test_flag=false; ignore_whitespace=false; mute=false;
input_len = token_line = token_column = bytes_written = 0;
expecting=NULL; unexpected=NULL;
}
static void finalize_parser () {
fclose(output);
if (output_name) remove(output_name);
exit(1);
}
static void status_line () {
if (input_name) {
fprintf(stderr, "%s:", input_name); }
fprintf(stderr, "%li:%li: Rule %s",
line, pos-pos_last_line+1,
parse_stack[parse_stack_top-1]);
if (token && token[0] != '\0') {
fprintf(stderr,"\nLast token was '%s', starting at %li:%li",
token,token_line,token_column); }
}
static void warning (const char *msg) {
status_line();
fprintf(stderr,"\nWarning: %s\n", msg);
}
static void error (const char *msg) {
status_line();
fprintf(stderr,"\nError: %s\n", msg);
finalize_parser();
}
static void error_if_false () {
if (!test_flag) {
if (on_error) {
on_error();
if(test_flag) return;
else finalize_parser(); }
status_line();
if (reason) {
fputc('\n', stderr);
fprintf(stderr,reason); }
else {
fprintf(stderr,"\nParsing error"); }
if (expecting) {
fprintf(stderr,", expecting %s",expecting); }
if (unexpected) {
fprintf(stderr,", unexpected %s",unexpected); }
fputc('\n', stderr);
finalize_parser(); }
}
static int first_into_second(int argc, char *argv[], void(*func)()) {
FILE *input;
if (argc > 3) {
fprintf(stderr, "Usage: meta [<input>] [<output>]\n");
exit(1); }
if (argc == 1) {
input=stdin;
input_name="stdin";
source=malloc(4194304+1);
input_len=read(0,source,4194304); }
else {
input=fopen(argv[1], "r");
if (input == NULL) {
fprintf(stderr, "Invalid input file\n");
exit(1); }
input_name = argv[1];
fseek(input, 0, SEEK_END);
input_len = ftell(input);
fseek(input, 0, SEEK_SET);
source = malloc(input_len + 1);
fread(source, 1, input_len, input); }
if (argc<3) {
output=stdout;
output_name="stdout"; }
else {
output_name=argv[2];
output=fopen(output_name, "w");
if (output == NULL) {
fprintf(stderr, "Invalid output file\n");
exit(1); }}
source[input_len]='\0';
func();
fprintf(stderr, "%li %li %li\n", input_len, pos, bytes_written);
if (!bytes_written) remove(output_name);
return 0;
}
static void multiple_files (int argc, char *argv[], char* extension, void(*func)()) {
for (int a=1; a<argc; a++) {
fprintf(stderr,"Processing file #%d...\n",a);
initialize_parser();
int input_name_len = strlen(argv[a]);
output_name = malloc(input_name_len + strlen(extension) + 2);
input_name = malloc(input_name_len);
strcpy(output_name,argv[a]);
strcpy(input_name,argv[a]);
strcat(&output_name[input_name_len],extension);
FILE* input=fopen(argv[a], "r");
if (input == NULL) {
fprintf(stderr, "Input file '%s' invalid\n",argv[a]);
exit(1); }
output=fopen(output_name, "w");
if (output == NULL) {
fprintf(stderr, "Can't write to output file '%s'\n",output_name);
exit(1); }
fseek(input, 0, SEEK_END);
input_len=ftell(input);
fseek(input, 0, SEEK_SET);
source=malloc(input_len + 1);
fread(source, 1, input_len, input);
source[input_len]='\0';
func();
fprintf(stderr, "%li %li %li\n", input_len, pos, bytes_written);
if (!bytes_written) {
remove(output_name); } }
}
static int test (int source_len, void(*func)()) {
source=malloc(source_len+1);
int chars=read(STDIN_FILENO,source,source_len);
input_len=chars;
source[chars]='\0';
output=stdout;
input_name="stdin";
output_name="stdout";
func();
return 0;
}
// To mute "unused function" warnings
static void(*no_unused_fwa1[])(char,char) = { read_any_between,read_any_but };
static void(*no_unused_fwa2[])(char) = { read_char_eq,read_char_neq,emit_char };
static void(*no_unused_fwa3[])() = { no_unused_fwa,
start_user_token,end_user_token,capture_to_token,
emit,emit_token,
read_literal,read_id,read_string,read_number,
warning,error,error_if_false,
multiple_files };
static int(*no_unused_fwa4[])() = { first_into_second,test };
static void no_unused_fwa () {
(void)&on_error;
(void)&no_unused_fwa1;
(void)&no_unused_fwa2;
(void)&no_unused_fwa3;
(void)&no_unused_fwa4;
}
#endif