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