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