aboutsummaryrefslogtreecommitdiffstats
path: root/src/parse.c
blob: 57db35cffdb0ec352080537de2229b1f9dc3b530 (plain)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "parse.h"
#include "err.h"

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

static const char *curfname;

#define ERR_AT(l, c, ...) \
	do { \
		fprintf(stderr, "\x1B[1m%s:%zu:%zu:\x1B[0m ", \
				curfname, (l), (c)); \
		ERR(__VA_ARGS__); \
	} while (0)

#define ERR_END(t) \
	do { \
		if ((t).type == TOK_END) \
			ERR_AT((t).line, (t).col, "unexpected end of file"); \
	} while (0)

#define WARN_AT(l, c, ...) \
	do { \
		fprintf(stderr, "\x1B[1m%s:%zu:%zu:\x1B[0m ", \
				curfname, (l), (c)); \
		WARN(__VA_ARGS__); \
	} while (0)

static struct parse_result_s r;

static enum parse_type_e sub_parse_type(void)
{
	struct tok_s t = tok_get();
	enum parse_type_e type = PARSE_TYPE_BOOL;
	size_t l, c;

	l = t.line;
	c = t.col;

	if (!strcmp(t.val, "array") ) {
		type = PARSE_TYPE_ARRAY_BOOL;
		t = tok_get();
	} else if (!strcmp(t.val, "hash") ) {
		type = PARSE_TYPE_HASH_BOOL;
		t = tok_get();
	}

	ERR_END(t);

	if (t.type != TOK_ID) {
		if (type >= PARSE_TYPE_HASH_BOOL)
			ERR_AT(l, c, "invalid type `hash %s`", t.val);
		else if (type >= PARSE_TYPE_ARRAY_BOOL)
			ERR_AT(l, c, "invalid type `array %s`", t.val);
		else
			ERR_AT(l, c, "invalid type `%s`", t.val);
	}

	if (!strcmp(t.val, "bool"))
		return type;

	if (!strcmp(t.val, "string"))
		return type + PARSE_TYPE_STRING;

	if (!strcmp(t.val, "id"))
		return type + PARSE_TYPE_ID;

	if (!strcmp(t.val, "int"))
		return type + PARSE_TYPE_INT;

	if (!strcmp(t.val, "intl"))
		return type + PARSE_TYPE_INTL;

	if (!strcmp(t.val, "intll"))
		return type + PARSE_TYPE_INTLL;

	if (!strcmp(t.val, "uint"))
		return type + PARSE_TYPE_UINT;

	if (!strcmp(t.val, "uintl"))
		return type + PARSE_TYPE_UINTL;

	if (!strcmp(t.val, "uintll"))
		return type + PARSE_TYPE_UINTLL;

	if (!strcmp(t.val, "float"))
		return type + PARSE_TYPE_FLOAT;

	if (!strcmp(t.val, "double"))
		return type + PARSE_TYPE_DOUBLE;

	if (!strcmp(t.val, "doublel"))
		return type + PARSE_TYPE_DOUBLEL;

	tok_unget(t);

	return type + PARSE_TYPE_DEFTYPE;
}

static void sub_parse_deftype(size_t line, size_t col, bool is_union)
{
	struct tok_s t;
	enum parse_type_e type;
	struct parse_deftype_s *dtp;
	struct parse_deftype_s dt = {
		.line = line,
		.col = col,
		.is_used = false,
		.is_union = is_union,
		.member_list_len = 0,
	};
	unsigned i, j;

	t = tok_get();
	ERR_END(t);
	if (t.type != TOK_ID) {
		ERR_AT(t.line, t.col, "unexpected token `%s` (expected %s name)",
				t.val, (dt.is_union ? "union" : "struct"));
	}

	if (
			!strcmp(t.val, "hash") || !strcmp(t.val, "array") ||
			!strcmp(t.val, "bool") ||
			!strcmp(t.val, "string") || !strcmp(t.val, "id") ||
			!strcmp(t.val, "int") || !strcmp(t.val, "intl") ||
			!strcmp(t.val, "intll") ||
			!strcmp(t.val, "uint") || !strcmp(t.val, "uintl") ||
			!strcmp(t.val, "uintll") ||
			!strcmp(t.val, "float") || !strcmp(t.val, "double") ||
			!strcmp(t.val, "doublell")
	) {
		ERR_AT(dt.line, dt.col,
				"defined type conflicts with builtin type `%s`", t.val);
	}

	HASH_FIND_STR(r.deftypes, t.val, dtp);
	if (dtp != NULL) {
		ERR_AT(dt.line, dt.col,
				"type `%s` redefined (previous definition was at line %zu)",
				t.val, dtp->line);
	}

	strcpy(dt.name, t.val);

	t = tok_get();
	ERR_END(t);
	if (t.type != TOK_LBRACE)
		ERR_AT(t.line, t.col, "unexpected token `%s` (expected `{`)", t.val);

	while (true) {
		if (dt.member_list_len == PARSE_DEFTYPE_MAX_LEN) {
			ERR_AT(dt.line, dt.col, "%s %s has too many members",
					(dt.is_union ? "union" : "struct"), dt.name);
		}

		t = tok_get();
		if (t.type == TOK_RBRACE) {
			if (dt.member_list_len < 2) {
				ERR_AT(dt.line, dt.col, "%s `%s` must specify at fewest two members",
						(dt.is_union ? "union" : "struct"), dt.name);
			}

			break;
		}

		tok_unget(t);

		type = sub_parse_type();

		if (type >= PARSE_TYPE_ARRAY_BOOL) {
			ERR_AT(t.line, t.col, "defined types may not contain arrays or hashes");
		}

		if (type == PARSE_TYPE_DEFTYPE) {
			t = tok_get();
			ERR_AT(t.line, t.col, "defined types may not contain other defined types");
		}

		t = tok_get();
		ERR_END(t);
		if (t.type != TOK_ID) {
			if (t.type == TOK_RBRACE || t.type == TOK_COMMA) {
				ERR_AT(t.line, t.col, "missing member name in %s `%s'",
						(dt.is_union ? "union" : "struct"), dt.name);
			}

			ERR_AT(t.line, t.col, "bad %s member name `%s`",
					(dt.is_union ? "union" : "struct"), t.val);
		}

		strcpy(dt.member_name_list[dt.member_list_len], t.val);
		dt.member_type_list[dt.member_list_len] = type;
		dt.member_list_len++;

		t = tok_get();
		if (t.type != TOK_COMMA)
			tok_unget(t);
	}

	for (i = 0; i < dt.member_list_len; i++) {
		for (j = i + 1; j < dt.member_list_len; j++) {
			if (!strcmp(dt.member_name_list[i], dt.member_name_list[j]) ) {
				ERR_AT(dt.line, dt.col, "%s `%s` contains multiple members named %s",
						(dt.is_union ? "union" : "struct"), dt.name,
						dt.member_name_list[i]);
			}

		}
	}

	TRYALLOC(dtp, 1);
	memcpy(dtp, &dt, sizeof(*dtp));

	HASH_ADD_STR(r.deftypes, name, dtp);
}

static bool sub_parse_op(void)
{
	struct tok_s t = tok_get();

	switch (t.type) {
	case TOK_OP_STRUCT:
		sub_parse_deftype(t.line, t.col, false);
		return true;

	case TOK_OP_UNION:
		sub_parse_deftype(t.line, t.col, true);
		return true;

	case TOK_OP_SUFFIX:
		if (r.suffix_seen) {
			WARN_AT(t.line, t.col,
					"function-suffix redefined (previous value was `%s`)",
					r.suffix);
		}

		t = tok_get();
		ERR_END(t);
		if (t.type != TOK_ID)
			ERR_AT(t.line, t.col, "invalid function-suffix `%s`", t.val);

		strcpy(r.suffix, t.val);

		r.suffix_seen = true;

		return true;

	default:
		tok_unget(t);
		return false;
	}
}

static bool sub_parse_rule(void)
{
	struct parse_var_s *vp;
	struct parse_var_s v;
	struct tok_s t = tok_get();

	if (t.type != TOK_BANG) {
		if (t.type != TOK_QMARK) {
			tok_unget(t);
			return false;
		}
		v.is_required = false;
	} else {
		v.is_required = true;
	}

	v.line = t.line;
	v.col = t.col;

	t = tok_get();
	ERR_END(t);
	if (t.type != TOK_ID) {
		ERR_AT(t.line, t.col,
				"unexpected token `%s`, (expected variable name)", t.val);
	}

	HASH_FIND_STR(r.vars, t.val, vp);
	if (vp != NULL) {
		ERR_AT(v.line, v.col,
				"`%s` redefined (previous definition was at line %zu)",
				t.val, vp->line);
	}

	strcpy(v.name, t.val);

	t = tok_get();
	ERR_END(t);
	if (t.type != TOK_EQUAL)
		ERR_AT(t.line, t.col, "unexpected token `%s`, (expected `=`)", t.val);

	v.type = sub_parse_type();

	if (v.type == PARSE_TYPE_DEFTYPE
			|| v.type == PARSE_TYPE_ARRAY_DEFTYPE
			|| v.type == PARSE_TYPE_HASH_DEFTYPE
	) {
		t = tok_get();
		strcpy(v.deftype_name, t.val);
	}

	TRYALLOC(vp, 1);
	memcpy(vp, &v, sizeof(*vp));
	HASH_ADD_STR(r.vars, name, vp);

	return true;
}

static int sub_sort_deftypes(struct parse_deftype_s *d1,
		struct parse_deftype_s *d2)
{
	return strcmp(d1->name, d2->name);
}

static int sub_sort_vars(struct parse_var_s *v1, struct parse_var_s *v2)
{
	return strcmp(v1->name, v2->name);
}

struct parse_result_s parse(FILE *f, const char *fname)
{
	size_t i, j;
	struct tok_s t;

	struct parse_var_s *vcur, *vtmp;
	struct parse_deftype_s *dcur, *dtmp;

	r.suffix_seen = false;
	r.deftypes = NULL;
	r.vars = NULL;
	curfname = fname;

	tok_reset(f);

	while (sub_parse_op() || sub_parse_rule());

	t = tok_get();
	if (t.type != TOK_END) {
		if (t.type == TOK_UNKNWN)
			ERR_AT(t.line, t.col, "unrecognised token `%s`", t.val);
		else
			ERR_AT(t.line, t.col, "unexpected token `%s`", t.val);
	}

	if (r.vars == NULL) {
		fprintf(stderr, "\x1B[1m%s:\x1B[0m ", fname);
		ERR("config must specify at fewest one variable rule");
	}

	HASH_ITER(hh, r.vars, vcur, vtmp) {
		switch (vcur->type) {
		case PARSE_TYPE_DEFTYPE:
		case PARSE_TYPE_ARRAY_DEFTYPE:
		case PARSE_TYPE_HASH_DEFTYPE:
			HASH_FIND_STR(r.deftypes, vcur->deftype_name, dcur);
			if (dcur == NULL) {
				ERR_AT(vcur->line, vcur->col,
						"rule for variable `%s` references undefined type `%s`",
						vcur->name, vcur->deftype_name);
			}
			dcur->is_used = true;
		default:
			continue;
		}
	}

	HASH_ITER(hh, r.deftypes, dcur, dtmp) {
		if (!dcur->is_used) {
			WARN_AT(dcur->line, dcur->col,
					"type `%s` defined but not used",
					dcur->name);
		}
	}

	if (!r.suffix_seen) {

		j = 0;

		for (i = strlen(fname); i > 0 && fname[i] != '/'
				&& fname[i] != '\\'; i--);

		j = i + (fname[i] == '/' || fname[i] == '\\');

		for (i = j; fname[i] != '\0' && fname[i] != '.'; i++) {
			if (!isalnum(fname[i]) && fname[i] != '_') {
				fprintf(stderr, "\x1B[1m%s:\x1B[0m ", fname);
				ERR("no function suffix specified, and could not generate one");
			}
			r.suffix[i - j] = fname[i];
		}

		r.suffix[i - j] = '\0';

		if (r.suffix[0] == '\0') {
			fprintf(stderr, "\x1B[1m%s:\x1B[0m ", fname);
			ERR("no function suffix specified, and could not generate one");
		}

		fprintf(stderr, "\x1B[1m%s:\x1B[0m ", fname);
		WARN("no function suffix specified. using `%s`...", r.suffix);
	}

	HASH_SORT(r.deftypes, sub_sort_deftypes);
	HASH_SORT(r.vars, sub_sort_vars);

	return r;
}

void parse_result_wipe(struct parse_result_s *r)
{
	struct parse_var_s *vcur, *vtmp;
	struct parse_deftype_s *dcur, *dtmp;

	assert(r != NULL);

	if (r->vars != NULL) {
		HASH_ITER(hh, r->vars, vcur, vtmp) {
			HASH_DEL(r->vars, vcur);
			free(vcur);
		}
	}

	if (r->deftypes != NULL) {
		HASH_ITER(hh, r->deftypes, dcur, dtmp) {
			HASH_DEL(r->deftypes, dcur);
			free(dcur);
		}
	}

}