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