summaryrefslogtreecommitdiffstats
path: root/src/args-bison.y
blob: 64c88be5e281aed00d9d4737b8d515c363576ba9 (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
/*
  Copyright (C) 2008, 2009 Jiri Olsa <olsajiri@gmail.com>

  This file is part of the latrace.

  The latrace is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  The latrace is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with the latrace (file COPYING).  If not, see 
  <http://www.gnu.org/licenses/>.
*/


%{

#define YYERROR_VERBOSE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

int yylex (void);
void yyerror(const char *m);

static struct lt_config_shared *scfg;
static int struct_alive = 0;

#define ERROR(fmt, args...) \
do { \
	char ebuf[1024]; \
	sprintf(ebuf, fmt, ## args); \
	yyerror(ebuf); \
	YYERROR; \
} while(0)

#define CHK_TYPEDEF(ret, base, new, pointer) \
do { \
	switch(ret) { \
	case -1: \
		ERROR("unknown typedef - %s%s%s\n", base, (pointer ? "* " : " "), new); \
		break; \
	case  1: \
		ERROR("typedef alrady defined - %s%s%s\n", base, (pointer ? "* " : " "), new); \
		break; \
	case  2: \
		ERROR("typedef limit reached(%d) - %s%s%s\n", \
		         LT_ARGS_DEF_TYPEDEF_NUM, base, (pointer ? "* " : " "), new); \
		break; \
	}; \
} while(0)

#define GET_LIST_HEAD(head) \
do { \
 	if (NULL == (head = (struct lt_list_head*) malloc(sizeof(*head)))) \
		ERROR("failed to allocate list head"); \
	lt_init_list_head(head); \
} while(0)

%}

%token NAME FILENAME STRUCT ENUM TYPEDEF INCLUDE END POINTER

%union
{
	char *s;
	struct lt_arg *arg;
	struct lt_enum_elem *enum_elem;
	struct lt_list_head *head;
}

%type <s>         NAME
%type <s>         FILENAME
%type <head>      STRUCT_DEF
%type <head>      ENUM_DEF
%type <s>         ENUM_REF
%type <enum_elem> ENUM_ELEM
%type <head>      ARGS
%type <arg>       DEF

%%
entry: 
entry struct_def
|
entry enum_def
| 
entry func_def
|
entry type_def
|
entry include_def
|
entry END
{
	if (lt_args_buf_close(scfg))
		return 0;
}
|
/* left blank intentionally */

/* struct definitions */
struct_def:
STRUCT NAME '{' STRUCT_DEF '}' ';'
{
	switch(lt_args_add_struct(scfg, $2, $4)) {
	case -1:
		ERROR("failed to add struct %s\n", $2);
	case 1:
		ERROR("struct limit reached(%d) - %s\n", LT_ARGS_DEF_STRUCT_NUM, $2);
	};

	/* force creation of the new list head */
	struct_alive = 0;
}

STRUCT_DEF:
STRUCT_DEF DEF ';'
{
	struct lt_arg *def     = $2;
	struct lt_list_head *h = $1;

	if (!struct_alive++)
		GET_LIST_HEAD(h);

	lt_list_add_tail(&def->args_list, h);
	$$ = h;
}
| /* left blank intentionally,
     XXX this could be done like the args_def, but user needs to be 
     able to create an empty structure, so thats why we play 
     with the global struct_alive thingie... 
     there could be better way probably */
{
}

/* enum definitions */
enum_def:
ENUM NAME '{' ENUM_DEF '}' ';'
{
	switch(lt_args_add_enum(scfg, $2, $4)) {
	case -1:
		ERROR("failed to add enum %s\n", $2);
	case 1:
		ERROR("enum limit reached(%d) - %s\n", LT_ARGS_DEF_STRUCT_NUM, $2);
	};
}

ENUM_DEF:
ENUM_DEF ',' ENUM_ELEM
{
	struct lt_enum_elem *enum_elem = $3;
	struct lt_list_head *h = $1;

	lt_list_add_tail(&enum_elem->list, h);
	$$ = h;
}
| ENUM_ELEM
{
	struct lt_list_head *h;
	struct lt_enum_elem *enum_elem = $1;

	GET_LIST_HEAD(h);
	lt_list_add_tail(&enum_elem->list, h);
	$$ = h;
}

ENUM_ELEM:
NAME '=' NAME
{
	if (NULL == ($$ = lt_args_get_enum(scfg, $1, $3)))
		ERROR("failed to add enum '%s = %s'\n", $1, $3);
}
|
NAME
{
	if (NULL == ($$ = lt_args_get_enum(scfg, $1, NULL)))
		ERROR("failed to add enum '%s = undef'\n", $1);
}

type_def:
TYPEDEF NAME NAME ';'
{
	int ret = lt_args_add_typedef(scfg, $2, $3, 0);
	CHK_TYPEDEF(ret, $2, $3, 0);
}
|
TYPEDEF NAME POINTER NAME ';'
{
	int ret = lt_args_add_typedef(scfg, $2, $4, 1);
	CHK_TYPEDEF(ret, $2, $4, 1);
}

/* function definitions */
func_def:
DEF '(' ARGS ')' ';'
{
	struct lt_arg *arg = $1;

	if (lt_args_add_sym(scfg, arg, $3))
		ERROR("failed to add symbol %s\n", arg->name);

	/* force cration of the new list head */
	$3 = NULL;
}

ARGS:
ARGS ',' DEF
{
	struct lt_arg *def     = $3;
	struct lt_list_head *h = $1;

	lt_list_add_tail(&def->args_list, h);
	$$ = h;
}
| DEF
{
	struct lt_list_head *h;
	struct lt_arg *def = $1;

	GET_LIST_HEAD(h);
	lt_list_add_tail(&def->args_list, h);
	$$ = h;
}
| NAME
{
	GET_LIST_HEAD($$);
}
| /* left intentionaly blank */
{
	GET_LIST_HEAD($$);
}

DEF:
NAME NAME ENUM_REF
{
	struct lt_arg *arg;

	if (NULL == (arg = lt_args_getarg(scfg, $1, $2, 0, 1, $3)))
		ERROR("unknown argument type - %s\n", $1);

	$$ = arg;
}
|
NAME POINTER NAME ENUM_REF
{
	struct lt_arg *arg;
	if (NULL == (arg = lt_args_getarg(scfg, $1, $3, 1, 1, $4)))
		ERROR("unknown argument type - %s\n", $1);

	$$ = arg;
}
|
STRUCT NAME NAME
{
	struct lt_arg *arg;
	if (NULL == (arg = lt_args_getarg(scfg, $2, $3, 0, 1, NULL)))
		ERROR("unknown argument type - %s\n", $2);

	$$ = arg;
}
|
STRUCT NAME POINTER NAME ENUM_REF
{
	struct lt_arg *arg;
	if (NULL == (arg = lt_args_getarg(scfg, $2, $4, 1, 1, $5)))
		ERROR("unknown argument type - %s\n", $2);

	$$ = arg;
}

ENUM_REF:
'=' NAME
{
	$$ = $2;
}
| 
{
	$$ = NULL;
}

/* include definitions */
include_def: INCLUDE '"' FILENAME '"'
{
	if (lt_args_buf_open(scfg, $3))
		ERROR("failed to process include");
}

%%

int lt_args_parse_init(struct lt_config_shared *cfg)
{
	scfg = cfg;
	return 0;
}