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