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 %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
46 %type decl_typedef {decl_typedef*}
47 decl_typedef(def) ::= TYPEDEF NAME(ALIAS) decl_type(type) EOS. {
48 def = init_decl_typedef(ALIAS->text, type);
49 free(ALIAS);
50 }
51
52 %type decl {decl*}
53 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
54 decl = init_decl(abi, func, args);
55 }
56
57 %type decl_abi {decl_abi*}
58 decl_abi(abi) ::= NAME(T). {
59 abi = init_decl_abi(T->text);
60 }
61
62 %type decl_var {decl_var*}
63 decl_var(var) ::= NAME(T). {
64 var = init_decl_var(T->text, 0);
65 free(T);
66 }
67 decl_var(var) ::= pointers(p) NAME(T). {
68 var = init_decl_var(T->text, p);
69 free(T);
70 }
71
72 %type decl_vars {decl_vars*}
73 decl_vars(vars) ::= decl_var(var). {
74 vars = init_decl_vars(var);
75 }
76 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
77 vars = add_decl_var(vars_, var);
78 }
79
80 %type decl_arg {decl_arg*}
81 decl_arg(arg) ::= decl_type(type) decl_var(var). {
82 arg = init_decl_arg(type, var);
83 }
84
85 %type decl_args {decl_args*}
86 decl_args ::= VOID.
87 decl_args(args) ::= decl_arg(arg). {
88 args = init_decl_args(arg);
89 }
90 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
91 args = add_decl_arg(args_, arg);
92 }
93
94 %type decl_type {decl_type*}
95 decl_type(type_) ::= VOID(T). {
96 type_ = init_decl_type(T->type, T->text);
97 free(T);
98 }
99 decl_type(type_) ::= CHAR(T). {
100 type_ = init_decl_type(T->type, T->text);
101 free(T);
102 }
103 decl_type(type_) ::= SHORT(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_) ::= LONG(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) impl_args(args) COLON impl_type(type). {
167 func = init_impl_func(NAME->text, args, type, 0);
168 free(NAME);
169 }
170 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
171 func = init_impl_func(NAME->text, args, type, 1);
172 free(NAME);
173 }
174
175 %type impl_def_val {impl_def_val*}
176 impl_def_val(def) ::= NULL(T). {
177 def = init_impl_def_val(T);
178 }
179 impl_def_val(def) ::= NUMBER(T). {
180 def = init_impl_def_val(T);
181 }
182 impl_def_val(def) ::= TRUE(T). {
183 def = init_impl_def_val(T);
184 }
185 impl_def_val(def) ::= FALSE(T). {
186 def = init_impl_def_val(T);
187 }
188 impl_def_val(def) ::= QUOTED_STRING(T). {
189 def = init_impl_def_val(T);
190 }
191
192 %type impl_var {impl_var*}
193 impl_var(var) ::= DOLLAR NAME(T). {
194 var = init_impl_var(T->text, 0);
195 free(T);
196 }
197 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
198 var = init_impl_var(T->text, 1);
199 free(T);
200 }
201
202 %type impl_arg {impl_arg*}
203 impl_arg(arg) ::= impl_type(type) impl_var(var). {
204 arg = init_impl_arg(type, var, NULL);
205 }
206 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
207 arg = init_impl_arg(type, var, def);
208 }
209
210 %type impl_args {impl_args*}
211 impl_args(args) ::= LPAREN RPAREN. {
212 args = NULL;
213 }
214 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
215 args = args_;
216 }
217 %type impl_arg_list {impl_args*}
218 impl_arg_list(args) ::= impl_arg(arg). {
219 args = init_impl_args(arg);
220 }
221 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
222 args = add_impl_arg(args_, arg);
223 }
224
225 %type impl_stmts {impl_stmts*}
226 impl_stmts(stmts) ::= impl_stmt(stmt). {
227 stmts = init_impl_stmts(stmt);
228 }
229 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
230 stmts = add_impl_stmt(stmts_, stmt);
231 }
232
233 %type impl_stmt {impl_stmt*}
234 impl_stmt(stmt) ::= let_stmt(let). {
235 stmt = init_impl_stmt(PSI_T_LET, let);
236 }
237 impl_stmt(stmt) ::= set_stmt(set). {
238 stmt = init_impl_stmt(PSI_T_SET, set);
239 }
240 impl_stmt(stmt) ::= return_stmt(ret). {
241 stmt = init_impl_stmt(PSI_T_RETURN, ret);
242 }
243 impl_stmt(stmt) ::= free_stmt(free). {
244 stmt = init_impl_stmt(PSI_T_FREE, free);
245 }
246
247 %type let_stmt {let_stmt*}
248 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
249 let = init_let_stmt(var, val);
250 }
251
252 %type let_value {let_value*}
253 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
254 val = init_let_value(func, var, 0);
255 }
256 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
257 val = init_let_value(func, var, 1);
258 }
259 let_value(val) ::= REFERENCE NULL. {
260 val = init_let_value(NULL, NULL, 1);
261 }
262 let_value(val) ::= NULL. {
263 val = init_let_value(NULL, NULL, 0);
264 }
265
266 %type let_func {let_func*}
267 let_func(func) ::= STRLEN(T). {
268 func = init_let_func(T->type, T->text);
269 free(T);
270 }
271 let_func(func) ::= STRVAL(T). {
272 func = init_let_func(T->type, T->text);
273 free(T);
274 }
275 let_func(func) ::= INTVAL(T). {
276 func = init_let_func(T->type, T->text);
277 free(T);
278 }
279 let_func(func) ::= FLOATVAL(T). {
280 func = init_let_func(T->type, T->text);
281 free(T);
282 }
283 let_func(func) ::= BOOLVAL(T). {
284 func = init_let_func(T->type, T->text);
285 free(T);
286 }
287
288 %type set_stmt {set_stmt*}
289 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
290 set = init_set_stmt(var, val);
291 }
292
293 %type set_value {set_value*}
294 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
295 val = init_set_value(func, vars);
296 }
297
298 %type set_func {set_func*}
299 set_func(func) ::= TO_STRING(T). {
300 func = init_set_func(T->type, T->text);
301 free(T);
302 }
303 set_func(func) ::= TO_INT(T). {
304 func = init_set_func(T->type, T->text);
305 free(T);
306 }
307 set_func(func) ::= TO_FLOAT(T). {
308 func = init_set_func(T->type, T->text);
309 free(T);
310 }
311 set_func(func) ::= TO_BOOL(T). {
312 func = init_set_func(T->type, T->text);
313 free(T);
314 }
315 set_func(func) ::= VOID(T). {
316 func = init_set_func(T->type, T->text);
317 free(T);
318 }
319
320 %type return_stmt {return_stmt*}
321 return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
322 ret = init_return_stmt(func, var);
323 }
324
325 %type free_stmt {free_stmt*}
326 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
327 free = init_free_stmt(vars);
328 }
329
330 %type impl_type {impl_type*}
331 impl_type(type_) ::= VOID(T). {
332 type_ = init_impl_type(T->type, T->text);
333 free(T);
334 }
335 impl_type(type_) ::= MIXED(T). {
336 type_ = init_impl_type(T->type, T->text);
337 free(T);
338 }
339 impl_type(type_) ::= BOOL(T). {
340 type_ = init_impl_type(T->type, T->text);
341 free(T);
342 }
343 impl_type(type_) ::= INT(T). {
344 type_ = init_impl_type(T->type, T->text);
345 free(T);
346 }
347 impl_type(type_) ::= FLOAT(T). {
348 type_ = init_impl_type(T->type, T->text);
349 free(T);
350 }
351 impl_type(type_) ::= STRING(T). {
352 type_ = init_impl_type(T->type, T->text);
353 free(T);
354 }
355 impl_type(type_) ::= ARRAY(T). {
356 type_ = init_impl_type(T->type, T->text);
357 free(T);
358 }
359
360 %type pointers {unsigned}
361 pointers(p) ::= POINTER. {++p;}
362 pointers(p) ::= pointers(P) POINTER. {p = P+1;}