summaryrefslogtreecommitdiff
path: root/parser.c
blob: a1b275f88edeac643241d4e9965dc3f37ce4bdff (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
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>

#include "list.h"
#include "qmic.h"

const char *qmi_package;

struct list_head qmi_consts = LIST_INIT(qmi_consts);
struct list_head qmi_messages = LIST_INIT(qmi_messages);
struct list_head qmi_structs = LIST_INIT(qmi_structs);

enum {
	TOK_CONST = 256,
	TOK_ID,
	TOK_MESSAGE,
	TOK_NUM,
	TOK_PACKAGE,
	TOK_STRUCT,
	TOK_TYPE,
	TOK_REQUIRED,
	TOK_OPTIONAL,
};

struct token {
	int id;
	char *str;
	unsigned num;
	struct qmi_struct *qmi_struct;
};

static char scratch_buf[128];
static unsigned scratch_pos;

static int yyline = 1;

static int input()
{
	static char input_buf[128];
	static unsigned input_pos;
	static unsigned input_len;
	int ret;
	int ch;

	if (scratch_pos) {
		ch = scratch_buf[--scratch_pos];
		goto out;
	}

	if (input_pos < input_len) {
		ch = input_buf[input_pos++];
		goto out;
	}

	ret = read(0, input_buf, sizeof(input_buf));
	if (ret <= 0)
		return ret;

	input_pos = 0;
	input_len = ret;

	ch = input_buf[input_pos++];
out:
	if (ch == '\n')
		yyline++;
	return ch;
}

static void unput(int ch)
{
	assert(scratch_pos < 128);
	scratch_buf[scratch_pos++] = ch;

	if (ch == '\n')
		yyline--;
}

struct symbol {
	int token;
	const char *name;

	int type;
	struct qmi_struct *qmi_struct;

	struct list_head node;
};

static struct list_head symbols = LIST_INIT(symbols);

static void symbol_add(const char *name, int token, ...)
{
	struct symbol *sym;
	va_list ap;

	va_start(ap, token);

	sym = malloc(sizeof(struct symbol));
	sym->token = token;
	sym->name = name;

	switch (token) {
	case TOK_MESSAGE:
		sym->type = va_arg(ap, int);
		break;
	case TOK_TYPE:
		sym->type = va_arg(ap, int);
		if (sym->type == TYPE_STRUCT)
			sym->qmi_struct = va_arg(ap, struct qmi_struct *);
		break;
	}

	list_add(&symbols, &sym->node);

	va_end(ap);
}

static struct token yylex()
{
	struct symbol *sym;
	struct token token = {};
	char buf[128];
	char *p = buf;
	int base;
	int ch;

	while ((ch = input()) && isspace(ch))
		;

	if (isalpha(ch)) {
		do {
			*p++ = ch;
			ch = input();
		} while (isalnum(ch) || ch == '_');
		unput(ch);
		*p = '\0';

		token.str = strdup(buf);
		list_for_each_entry(sym, &symbols, node) {
			if (strcmp(buf, sym->name) == 0) {
				token.id = sym->token;
				token.num = sym->type;
				token.qmi_struct = sym->qmi_struct;
				return token;
			}
		}

		token.id = TOK_ID;

		return token;
	} else if (isdigit(ch)) {
		do {
			*p++ = ch;
			ch = input();
		} while (isdigit(ch) || (p - buf == 1 && ch == 'x'));
		unput(ch);
		*p = '\0';

		if (buf[0] == '0' && buf[1] == 'x')
			base = 16;
		else if (buf[0] == '0')
			base = 8;
		else
			base = 10;

		token.id = TOK_NUM;
		token.num = strtol(buf, NULL, base);
		return token;
	}

	token.id = ch;
	return token;
}

static struct token curr_token;

static void yyerror(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);

	printf("parse error on line %u:\n\t", yyline);
	vprintf(fmt, ap);
	printf("\n");

	va_end(ap);

	exit(1);
}

static void token_init(void)
{
	curr_token = yylex();
}

static int token_accept(int id, struct token *tok)
{
	if (curr_token.id == id) {
		if (tok)
			*tok = curr_token;
		else if (curr_token.str)
			free(curr_token.str);

		curr_token = yylex();
		return 1;
	} else {
		return 0;
	}
}

static void token_expect(int id, struct token *tok)
{
	if (!token_accept(id, tok)) {
		switch (id) {
		case TOK_CONST:
			yyerror("expected const");
		case TOK_ID:
			yyerror("expected identifier");
		case TOK_MESSAGE:
			yyerror("expected message");
		case TOK_NUM:
			yyerror("expected num");
		case TOK_PACKAGE:
			yyerror("expected package");
		case TOK_STRUCT:
			yyerror("expected struct");
		case TOK_TYPE:
			yyerror("expected type");
		case TOK_REQUIRED:
			yyerror("expected required");
		case TOK_OPTIONAL:
			yyerror("expected optional");
		default:
			yyerror("expected '%c'", id);
		}
	}
}

static const char *parse_package()
{
	struct token tok;

	if (!token_accept(TOK_ID, &tok))
		yyerror("expected identifier");

	token_expect(';', NULL);
	return tok.str;
}

static void qmi_const_parse()
{
	struct qmi_const *qc;
	struct token num_tok;
	struct token id_tok;

	token_expect(TOK_ID, &id_tok);
	token_expect('=', NULL);
	token_expect(TOK_NUM, &num_tok);
	token_expect(';', NULL);

	qc = malloc(sizeof(struct qmi_const));
	qc->name = id_tok.str;
	qc->value = num_tok.num;

	list_add(&qmi_consts, &qc->node);
}

static void qmi_message_parse(enum message_type message_type)
{
	struct qmi_message_member *qmm;
	struct qmi_message *qm;
	struct token msg_id_tok;
	struct token type_tok;
	struct token num_tok;
	struct token id_tok;
	unsigned int array_size;
	bool array_fixed = false;
	bool required;

	token_expect(TOK_ID, &msg_id_tok);
	token_expect('{', NULL);

	qm = calloc(1, sizeof(struct qmi_message));
	qm->name = msg_id_tok.str;
	qm->type = message_type;
	list_init(&qm->members);

	while (!token_accept('}', NULL)) {
		if (token_accept(TOK_REQUIRED, NULL))
			required = true;
		else if (token_accept(TOK_OPTIONAL, NULL))
			required = false;
		else
			yyerror("expected required, optional or '}'");

		token_expect(TOK_TYPE, &type_tok);
		token_expect(TOK_ID, &id_tok);

		if (token_accept('[', NULL)) {
			token_expect(TOK_NUM, &num_tok);
			array_size = num_tok.num;
			token_expect(']', NULL);

			array_fixed = true;
		} else if(token_accept('(', NULL)) {
			token_expect(TOK_NUM, &num_tok);
			array_size = num_tok.num;
			token_expect(')', NULL);
		} else {
			array_size = 0;
		}

		token_expect('=', NULL);
		token_expect(TOK_NUM, &num_tok);
		token_expect(';', NULL);

		qmm = calloc(1, sizeof(struct qmi_message_member));
		qmm->name = id_tok.str;
		qmm->type = type_tok.num;
		if (type_tok.str)
			free(type_tok.str);
		qmm->qmi_struct = type_tok.qmi_struct;
		qmm->id = num_tok.num;
		qmm->required = required;
		qmm->array_size = array_size;
		qmm->array_fixed = array_fixed;

		list_add(&qm->members, &qmm->node);
	}

	if (token_accept('=', NULL)) {
		token_expect(TOK_NUM, &num_tok);

		qm->msg_id = num_tok.num;
	}

	token_expect(';', NULL);

	list_add(&qmi_messages, &qm->node);
}

static void qmi_struct_parse(void)
{
	struct qmi_struct_member *qsm;
	struct token struct_id_tok;
	struct qmi_struct *qs;
	struct token type_tok;
	struct token id_tok;

	token_expect(TOK_ID, &struct_id_tok);
	token_expect('{', NULL);

	qs = malloc(sizeof(struct qmi_struct));
	qs->name = struct_id_tok.str;
	list_init(&qs->members);

	while (token_accept(TOK_TYPE, &type_tok)) {
		token_expect(TOK_ID, &id_tok);
		token_expect(';', NULL);

		qsm = malloc(sizeof(struct qmi_struct_member));
		qsm->name = id_tok.str;
		qsm->type = type_tok.num;
		if (type_tok.str)
			free(type_tok.str);

		list_add(&qs->members, &qsm->node);
	}

	token_expect('}', NULL);
	token_expect(';', NULL);

	list_add(&qmi_structs, &qs->node);

	symbol_add(qs->name, TOK_TYPE, TYPE_STRUCT, qs);
}

void qmi_parse(void)
{
	struct token tok;

	/* PACKAGE ID<string> ';' */
	/* CONST ID<string> '=' NUM<num> ';' */
	/* STRUCT ID<string> '{' ... '}' ';' */
		/* TYPE<type*> ID<string> ';' */
	/* MESSAGE ID<string> '{' ... '}' ';' */
		/* (REQUIRED | OPTIONAL) TYPE<type*> ID<string> '=' NUM<num> ';' */

	symbol_add("const", TOK_CONST);
	symbol_add("optional", TOK_OPTIONAL);
	symbol_add("message", TOK_MESSAGE, MESSAGE_RESPONSE); /* backward compatible with early hacking */
	symbol_add("request", TOK_MESSAGE, MESSAGE_REQUEST);
	symbol_add("response", TOK_MESSAGE, MESSAGE_RESPONSE);
	symbol_add("indication", TOK_MESSAGE, MESSAGE_INDICATION);
	symbol_add("package", TOK_PACKAGE);
	symbol_add("required", TOK_REQUIRED);
	symbol_add("struct", TOK_STRUCT);
	symbol_add("string", TOK_TYPE, TYPE_STRING);
	symbol_add("u8", TOK_TYPE, TYPE_U8);
	symbol_add("u16", TOK_TYPE, TYPE_U16);
	symbol_add("u32", TOK_TYPE, TYPE_U32);
	symbol_add("u64", TOK_TYPE, TYPE_U64);

	token_init();
	while (!token_accept(0, NULL)) {
		if (token_accept(TOK_PACKAGE, NULL)) {
			qmi_package = parse_package();
		} else if (token_accept(TOK_CONST, NULL)) {
			qmi_const_parse();
		} else if (token_accept(TOK_STRUCT, NULL)) {
			qmi_struct_parse();
		} else if (token_accept(TOK_MESSAGE, &tok)) {
			qmi_message_parse(tok.num);
			free(tok.str);
		} else {
			yyerror("unexpected symbol");
			break;
		}
	}
}