flush
[m6w6/ext-psi] / idl / parser_proc.y
1 %include {
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdarg.h>
6
7 #include "parser.h"
8
9 static void syntax_error(const char *fn, size_t ln, const char *msg, ...) {
10 fprintf(stderr, "WARNING: Syntax error on line %zu in '%s'%s", ln, fn, msg ? ": ": "\n");
11 if (msg) {
12 va_list argv;
13
14 va_start(argv, msg);
15 vfprintf(stderr, msg, argv);
16 va_end(argv);
17 }
18 }
19 }
20
21 %name PSI_ParserProc
22 %token_prefix PSI_T_
23 %token_type {PSI_Token *}
24 %token_destructor {free($$);}
25 %extra_argument {PSI_Parser *P}
26 /* TOKEN is defined inside syntax_error */
27 %syntax_error {
28 syntax_error(P->fn, P->line, "Unexpected token '%s'.\n", TOKEN->text);
29 }
30 file ::= blocks.
31
32 blocks ::= block.
33 blocks ::= blocks block.
34
35 block ::= COMMENT.
36
37 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
38 if (P->lib) {
39 syntax_error(P->fn, T->line, "Extra 'lib %s' statement has no effect.\n", libname->text);
40 } else {
41 P->lib = strndup(libname->text + 1, libname->size - 2);
42 }
43 free(libname);
44 free(T);
45 }
46
47 block ::= decl(decl). {
48 P->decls = add_decl(P->decls, decl);
49 }
50 block ::= impl(impl). {
51 P->impls = add_impl(P->impls, impl);
52 }
53 block ::= decl_typedef(def). {
54 P->defs = add_decl_typedef(P->defs, def);
55 }
56
57 %type decl_typedef {decl_typedef*}
58 decl_typedef(def) ::= TYPEDEF NAME(ALIAS) decl_type(type) EOS. {
59 def = init_decl_typedef(ALIAS->text, type);
60 free(ALIAS);
61 }
62
63 %type decl {decl*}
64 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
65 decl = init_decl(abi, func, args);
66 }
67
68 %type decl_abi {decl_abi*}
69 decl_abi(abi) ::= NAME(T). {
70 abi = init_decl_abi(T->text);
71 }
72
73 %type decl_var {decl_var*}
74 decl_var(var) ::= NAME(T). {
75 var = init_decl_var(T->text, 0);
76 free(T);
77 }
78 decl_var(var) ::= pointers(p) NAME(T). {
79 var = init_decl_var(T->text, p);
80 free(T);
81 }
82
83 %type decl_vars {decl_vars*}
84 decl_vars(vars) ::= decl_var(var). {
85 vars = init_decl_vars(var);
86 }
87 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
88 vars = add_decl_var(vars_, var);
89 }
90
91 %type decl_arg {decl_arg*}
92 decl_arg(arg) ::= decl_type(type) decl_var(var). {
93 arg = init_decl_arg(type, var);
94 }
95
96 %type decl_args {decl_args*}
97 decl_args(args) ::= decl_arg(arg). {
98 args = init_decl_args(arg);
99 }
100 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
101 args = add_decl_arg(args_, arg);
102 }
103
104 %type decl_type {decl_type*}
105 decl_type(type_) ::= VOID(T). {
106 type_ = init_decl_type(T->type, T->text);
107 free(T);
108 }
109 decl_type(type_) ::= INT(T). {
110 type_ = init_decl_type(T->type, T->text);
111 free(T);
112 }
113 decl_type(type_) ::= FLOAT(T). {
114 type_ = init_decl_type(T->type, T->text);
115 free(T);
116 }
117 decl_type(type_) ::= DOUBLE(T). {
118 type_ = init_decl_type(T->type, T->text);
119 free(T);
120 }
121 decl_type(type_) ::= SINT8(T). {
122 type_ = init_decl_type(T->type, T->text);
123 free(T);
124 }
125 decl_type(type_) ::= UINT8(T). {
126 type_ = init_decl_type(T->type, T->text);
127 free(T);
128 }
129 decl_type(type_) ::= SINT16(T). {
130 type_ = init_decl_type(T->type, T->text);
131 free(T);
132 }
133 decl_type(type_) ::= UINT16(T). {
134 type_ = init_decl_type(T->type, T->text);
135 free(T);
136 }
137 decl_type(type_) ::= SINT32(T). {
138 type_ = init_decl_type(T->type, T->text);
139 free(T);
140 }
141 decl_type(type_) ::= UINT32(T). {
142 type_ = init_decl_type(T->type, T->text);
143 free(T);
144 }
145 decl_type(type_) ::= SINT64(T). {
146 type_ = init_decl_type(T->type, T->text);
147 free(T);
148 }
149 decl_type(type_) ::= UINT64(T). {
150 type_ = init_decl_type(T->type, T->text);
151 free(T);
152 }
153 decl_type(type_) ::= NAME(T). {
154 type_ = init_decl_type(T->type, T->text);
155 free(T);
156 }
157
158 %type impl {impl*}
159 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
160 impl = init_impl(func, stmts);
161 }
162
163 %type impl_func {impl_func*}
164 impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
165 func = init_impl_func(NAME->text, args, type);
166 free(NAME);
167 }
168 impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
169 func = init_impl_func(NAME->text, NULL, type);
170 free(NAME);
171 }
172
173 %type impl_def_val {impl_def_val*}
174 impl_def_val(def) ::= NULL. {
175 /* FIXME */
176 def = init_impl_def_val();
177 }
178 impl_def_val(def) ::= number. {
179 /* FIXME */
180 def = init_impl_def_val();
181 }
182
183 %type impl_var {impl_var*}
184 impl_var(var) ::= DOLLAR NAME(T). {
185 var = init_impl_var(T->text, 0);
186 free(T);
187 }
188 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
189 var = init_impl_var(T->text, 1);
190 free(T);
191 }
192
193 %type impl_arg {impl_arg*}
194 impl_arg(arg) ::= impl_type(type) impl_var(var). {
195 arg = init_impl_arg(type, var, NULL);
196 }
197 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
198 arg = init_impl_arg(type, var, def);
199 }
200
201 %type impl_args {impl_args*}
202 impl_args(args) ::= impl_arg(arg). {
203 args = init_impl_args(arg);
204 }
205 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
206 args = add_impl_arg(args_, arg);
207 }
208
209 %type impl_stmts {impl_stmts*}
210 impl_stmts(stmts) ::= impl_stmt(stmt). {
211 stmts = init_impl_stmts(stmt);
212 }
213 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
214 stmts = add_impl_stmt(stmts_, stmt);
215 }
216
217 %type impl_stmt {impl_stmt*}
218 impl_stmt(stmt) ::= let_stmt(let). {
219 stmt = init_impl_stmt(PSI_T_LET, let);
220 }
221 impl_stmt(stmt) ::= set_stmt(set). {
222 stmt = init_impl_stmt(PSI_T_SET, set);
223 }
224 impl_stmt(stmt) ::= ret_stmt(ret). {
225 stmt = init_impl_stmt(PSI_T_RET, ret);
226 }
227
228 %type let_stmt {let_stmt*}
229 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
230 let = init_let_stmt(var, val);
231 }
232
233 %type let_value {let_value*}
234 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
235 val = init_let_value(func, var, 0);
236 }
237 let_value(val) ::= REFERENCE NULL. {
238 val = init_let_value(NULL, NULL, 1);
239 }
240 let_value(val) ::= NULL. {
241 val = init_let_value(NULL, NULL, 0);
242 }
243
244 %type let_func {let_func*}
245 let_func(func) ::= STRVAL(T). {
246 func = init_let_func(T->type, T->text);
247 free(T);
248 }
249 let_func(func) ::= INTVAL(T). {
250 func = init_let_func(T->type, T->text);
251 free(T);
252 }
253 let_func(func) ::= FLOATVAL(T). {
254 func = init_let_func(T->type, T->text);
255 free(T);
256 }
257 let_func(func) ::= BOOLVAL(T). {
258 func = init_let_func(T->type, T->text);
259 free(T);
260 }
261
262 %type set_stmt {set_stmt*}
263 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
264 set = init_set_stmt(var, val);
265 }
266
267 %type set_value {set_value*}
268 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
269 val = init_set_value(func, vars);
270 }
271
272 %type set_func {set_func*}
273 set_func(func) ::= TO_STRING(T). {
274 func = init_set_func(T->type, T->text);
275 free(T);
276 }
277 set_func(func) ::= TO_INT(T). {
278 func = init_set_func(T->type, T->text);
279 free(T);
280 }
281 set_func(func) ::= TO_FLOAT(T). {
282 func = init_set_func(T->type, T->text);
283 free(T);
284 }
285 set_func(func) ::= TO_BOOL(T). {
286 func = init_set_func(T->type, T->text);
287 free(T);
288 }
289
290 %type ret_stmt {ret_stmt*}
291 ret_stmt(ret) ::= RET set_func(func) LPAREN decl_var(var) RPAREN EOS. {
292 ret = init_ret_stmt(func, var);
293 }
294
295 %type impl_type {impl_type*}
296 impl_type(type_) ::= VOID(T). {
297 type_ = init_impl_type(T->type, T->text);
298 free(T);
299 }
300 impl_type(type_) ::= MIXED(T). {
301 type_ = init_impl_type(T->type, T->text);
302 free(T);
303 }
304 impl_type(type_) ::= BOOL(T). {
305 type_ = init_impl_type(T->type, T->text);
306 free(T);
307 }
308 impl_type(type_) ::= INT(T). {
309 type_ = init_impl_type(T->type, T->text);
310 free(T);
311 }
312 impl_type(type_) ::= FLOAT(T). {
313 type_ = init_impl_type(T->type, T->text);
314 free(T);
315 }
316 impl_type(type_) ::= STRING(T). {
317 type_ = init_impl_type(T->type, T->text);
318 free(T);
319 }
320 impl_type(type_) ::= ARRAY(T). {
321 type_ = init_impl_type(T->type, T->text);
322 free(T);
323 }
324
325 digits ::= DIGIT.
326 digits ::= digits DIGIT.
327 decimals ::= digits DOT digits.
328 decimals ::= DOT digits.
329 decimals ::= digits DOT.
330 number ::= digits.
331 number ::= PLUS digits.
332 number ::= MINUS digits.
333 number ::= decimals.
334 number ::= MINUS decimals.
335 number ::= PLUS decimals.
336
337 %type pointers {unsigned}
338 pointers(p) ::= POINTER. {++p;}
339 pointers(p) ::= pointers(P) POINTER. {p = ++P;}