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