parser: add mmap and string parser
[m6w6/ext-psi] / src / parser_proc.y
1 %include {
2 #include "php_psi_stdinc.h"
3
4 #include <assert.h>
5 #include <stdarg.h>
6
7 #include "plist.h"
8 #include "parser.h"
9 }
10 %name psi_parser_proc_
11 %token_prefix PSI_T_
12 %token_type {struct psi_token *}
13 %token_destructor {free($$);}
14 %default_destructor {(void)P;}
15 %extra_argument {struct psi_parser *P}
16 %syntax_error { ++P->errors; if (TOKEN && TOKEN->type != PSI_T_EOF) { psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN->text, TOKEN->col); } else { psi_error(PSI_WARNING, P->file.fn, P->line, "PSI syntax error: Unexpected end of input"); } }
17 %nonassoc NAME.
18 %left PLUS MINUS.
19 %left ASTERISK SLASH.
20 %nonassoc AMPERSAND.
21 %fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT.
22 %token_class const_type_token BOOL INT FLOAT STRING.
23 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
24 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
25 %token_class num_exp_token NUMBER NSNAME.
26 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
27 %token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT.
28 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.
29 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
30 %type decl_enum {struct psi_decl_enum *}
31 %destructor decl_enum {psi_decl_enum_free(&$$);}
32 %type decl_enum_items {struct psi_plist*}
33 %destructor decl_enum_items {psi_plist_free($$);}
34 %type decl_enum_item {struct psi_decl_enum_item*}
35 %destructor decl_enum_item {psi_decl_enum_item_free(&$$);}
36 %type decl_struct_args_block {struct psi_plist*}
37 %destructor decl_struct_args_block {psi_plist_free($$);}
38 %type decl_struct_args {struct psi_plist*}
39 %destructor decl_struct_args {psi_plist_free($$);}
40 %type decl_struct {struct psi_decl_struct*}
41 %destructor decl_struct {psi_decl_struct_free(&$$);}
42 %type align_and_size {struct psi_layout}
43 %type decl_union {struct psi_decl_union*}
44 %destructor decl_union {psi_decl_union_free(&$$);}
45 %type const_type {struct psi_const_type*}
46 %destructor const_type {psi_const_type_free(&$$);}
47 %type constant {struct psi_const*}
48 %destructor constant {psi_const_free(&$$);}
49 %type decl_typedef {struct psi_decl_arg*}
50 %destructor decl_typedef {psi_decl_arg_free(&$$);}
51 %type decl_typedef_body_ex {struct psi_decl_arg*}
52 %destructor decl_typedef_body_ex {psi_decl_arg_free(&$$);}
53 %type decl_typedef_body {struct psi_decl_arg*}
54 %destructor decl_typedef_body {psi_decl_arg_free(&$$);}
55 %type decl_typedef_body_fn_args {struct psi_plist*}
56 %destructor decl_typedef_body_fn_args {psi_plist_free($$);}
57 %type decl {struct psi_decl*}
58 %destructor decl {psi_decl_free(&$$);}
59 %type decl_func {struct psi_decl_arg*}
60 %destructor decl_func {psi_decl_arg_free(&$$);}
61 %type decl_abi {struct psi_decl_abi*}
62 %destructor decl_abi {psi_decl_abi_free(&$$);}
63 %type decl_var {struct psi_decl_var*}
64 %destructor decl_var {psi_decl_var_free(&$$);}
65 %type decl_vars {struct psi_plist*}
66 %destructor decl_vars {psi_plist_free($$);}
67 %type decl_arg {struct psi_decl_arg*}
68 %destructor decl_arg {psi_decl_arg_free(&$$);}
69 %type decl_args {struct psi_plist*}
70 %destructor decl_args {psi_plist_free($$);}
71 %type struct_args {struct psi_plist*}
72 %destructor struct_args {psi_plist_free($$);}
73 %type struct_arg {struct psi_decl_arg*}
74 %destructor struct_arg {psi_decl_arg_free(&$$);}
75 %type decl_layout {struct psi_layout*}
76 %destructor decl_layout {psi_layout_free(&$$);}
77 %type decl_type {struct psi_decl_type*}
78 %destructor decl_type {psi_decl_type_free(&$$);}
79 %type const_decl_type {struct psi_decl_type*}
80 %destructor const_decl_type {psi_decl_type_free(&$$);}
81 %type impl {struct psi_impl*}
82 %destructor impl {psi_impl_free(&$$);}
83 %type impl_func {struct psi_impl_func*}
84 %destructor impl_func {psi_impl_func_free(&$$);}
85 %type impl_def_val {struct psi_impl_def_val*}
86 %destructor impl_def_val {psi_impl_def_val_free(&$$);}
87 %type impl_var {struct psi_impl_var*}
88 %destructor impl_var {psi_impl_var_free(&$$);}
89 %type impl_arg {struct psi_impl_arg*}
90 %destructor impl_arg {psi_impl_arg_free(&$$);}
91 %type impl_args {struct psi_plist*}
92 %destructor impl_args {psi_plist_free($$);}
93 %type impl_vararg {struct psi_impl_arg*}
94 %destructor impl_vararg {psi_impl_arg_free(&$$);}
95 %type impl_stmts {struct psi_plist*}
96 %destructor impl_stmts {psi_plist_free($$);}
97 %type impl_stmt {struct psi_token**}
98 %destructor impl_stmt {psi_impl_stmt_free(&$$);}
99 %type num_exp {struct psi_num_exp*}
100 %destructor num_exp {psi_num_exp_free(&$$);}
101 %type let_stmt {struct psi_let_stmt*}
102 %destructor let_stmt {psi_let_stmt_free(&$$);}
103 %type let_calloc {struct psi_let_calloc*}
104 %destructor let_calloc {psi_let_calloc_free(&$$);}
105 %type let_func {struct psi_let_func*}
106 %destructor let_func {psi_let_func_free(&$$);}
107 %type callback_arg_list {struct psi_plist *}
108 %destructor callback_arg_list {psi_plist_free($$);}
109 %type callback_args {struct psi_plist *}
110 %destructor callback_args {psi_plist_free($$);}
111 %type let_callback {struct psi_let_callback*}
112 %destructor let_callback {psi_let_callback_free(&$$);}
113 %type let_exp {struct psi_let_exp*}
114 %destructor let_exp {psi_let_exp_free(&$$);}
115 %type let_exps {struct psi_plist*}
116 %destructor let_exps {psi_plist_free($$);}
117 %type set_stmt {struct psi_set_stmt*}
118 %destructor set_stmt {psi_set_stmt_free(&$$);}
119 %type set_exp {struct psi_set_exp*}
120 %destructor set_exp {psi_set_exp_free(&$$);}
121 %type set_exps {struct psi_plist*}
122 %destructor set_exps {psi_plist_free($$);}
123 %type set_func {struct psi_set_func*}
124 %destructor set_func {psi_set_func_free(&$$);}
125 %type return_stmt {struct psi_return_stmt*}
126 %destructor return_stmt {psi_return_stmt_free(&$$);}
127 %type free_stmt {struct psi_free_stmt*}
128 %destructor free_stmt {psi_free_stmt_free(&$$);}
129 %type free_exps {struct psi_plist*}
130 %destructor free_exps {psi_plist_free($$);}
131 %type free_exp {struct psi_free_exp*}
132 %destructor free_exp {psi_free_exp_free(&$$);}
133 %type impl_type {struct psi_impl_type*}
134 %destructor impl_type {psi_impl_type_free(&$$);}
135 %type reference {bool}
136 %type indirection {unsigned}
137 %type pointers {unsigned}
138 file ::= blocks.
139 blocks ::= block.
140 blocks ::= blocks block.
141 block ::= EOF.
142 block ::= EOS.
143 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
144 if (P->file.ln) {
145 P->error(PSI_DATA(P), token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
146 } else {
147 P->file.ln = strndup(libname->text + 1, libname->size - 2);
148 }
149 free(libname);
150 free(token);
151 }
152 block ::= decl(decl). {
153 if (!P->decls) {
154 P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
155 }
156 P->decls = psi_plist_add(P->decls, &decl);
157 }
158 block ::= impl(impl). {
159 if (!P->impls) {
160 P->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
161 }
162 P->impls = psi_plist_add(P->impls, &impl);
163 }
164 block ::= decl_typedef(def). {
165 if (!P->types) {
166 P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
167 }
168 P->types = psi_plist_add(P->types, &def);
169 switch (def->type->type) {
170 case PSI_T_STRUCT:
171 if (def->type->real.strct) {
172 if (!P->structs) {
173 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
174 }
175 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
176 }
177 break;
178 case PSI_T_UNION:
179 if (def->type->real.unn) {
180 if (!P->unions) {
181 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
182 }
183 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
184 }
185 break;
186 case PSI_T_ENUM:
187 if (def->type->real.enm) {
188 if (!P->enums) {
189 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
190 }
191 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
192 }
193 break;
194 }
195 }
196 block ::= constant(constant). {
197 if (!P->consts) {
198 P->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
199 }
200 P->consts = psi_plist_add(P->consts, &constant);
201 }
202 block ::= decl_struct(strct). {
203 if (!P->structs) {
204 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
205 }
206 P->structs = psi_plist_add(P->structs, &strct);
207 }
208 block ::= decl_union(u). {
209 if (!P->unions) {
210 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
211 }
212 P->unions = psi_plist_add(P->unions, &u);
213 }
214 block ::= decl_enum(e). {
215 if (!P->enums) {
216 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
217 }
218 P->enums = psi_plist_add(P->enums, &e);
219 }
220 optional_name(n) ::= . {
221 n = NULL;
222 }
223 optional_name(n) ::= NAME(N). {
224 n = N;
225 }
226 align_and_size(as) ::= . {
227 as.pos = 0;
228 as.len = 0;
229 }
230 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
231 as.pos = atol(A->text);
232 as.len = atol(S->text);
233 free(A);
234 free(S);
235 }
236 enum_name(n) ::= ENUM(E) optional_name(N). {
237 if (N) {
238 n = N;
239 free(E);
240 } else {
241 char digest[17];
242 psi_token_hash(E, digest);
243 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
244 }
245 }
246 struct_name(n) ::= STRUCT(S) optional_name(N). {
247 if (N) {
248 n = N;
249 free(S);
250 } else {
251 char digest[17];
252 psi_token_hash(S, digest);
253 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
254 }
255 }
256 union_name(n) ::= UNION(U) optional_name(N). {
257 if (N) {
258 n = N;
259 free(U);
260 } else {
261 char digest[17];
262 psi_token_hash(U, digest);
263 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
264 }
265 }
266 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
267 e = psi_decl_enum_init(N->text, list);
268 e->token = N;
269 }
270 decl_enum_items(l) ::= decl_enum_item(i). {
271 l = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_enum_item_free),
272 &i);
273 }
274 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
275 l = psi_plist_add(l_, &i);
276 }
277 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
278 i = psi_decl_enum_item_init(N->text, num);
279 i->token = N;
280 }
281 decl_enum_item(i) ::= NAME(N). {
282 i = psi_decl_enum_item_init(N->text, NULL);
283 i->token = N;
284 }
285 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
286 args_ = args;
287 }
288 decl_struct_args(args_) ::= decl_struct_args_block(args). {
289 args_ = args;
290 }
291 decl_struct_args(args_) ::= EOS. {
292 args_ = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
293 }
294 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
295 strct = psi_decl_struct_init(N->text, args);
296 strct->align = as.pos;
297 strct->size = as.len;
298 strct->token = N;
299 }
300 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
301 u = psi_decl_union_init(N->text, args);
302 u->align = as.pos;
303 u->size = as.len;
304 u->token = N;
305 }
306 const_type(type_) ::= const_type_token(T). {
307 type_ = psi_const_type_init(T->type, T->text);
308 free(T);
309 }
310 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
311 constant = psi_const_init(type, T->text, val);
312 constant->token = T;
313 }
314 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
315 def = def_;
316 def->token = T;
317 }
318 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
319 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, N->text), var);
320 def->type->token = psi_token_copy(N);
321 def->type->real.strct = psi_decl_struct_init(N->text, args);
322 def->type->real.strct->token = N;
323 def->type->real.strct->align = as.pos;
324 def->type->real.strct->size = as.len;
325 }
326 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
327 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, N->text), var);
328 def->type->token = psi_token_copy(N);
329 def->type->real.unn = psi_decl_union_init(N->text, args);
330 def->type->real.unn->token = N;
331 def->type->real.unn->align = as.pos;
332 def->type->real.unn->size = as.len;
333 }
334 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
335 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_ENUM, e->name), psi_decl_var_init(ALIAS->text, 0, 0));
336 def->var->token = ALIAS;
337 def->type->token = psi_token_copy(e->token);
338 def->type->real.enm = e;
339 }
340 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
341 def = def_;
342 }
343 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
344 args = args_;
345 }
346 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
347 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_FUNCTION, func_->var->name), psi_decl_var_copy(func_->var));
348 def->type->token = psi_token_copy(func_->token);
349 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
350 }
351 decl_typedef_body(def) ::= decl_arg(arg). {
352 def = arg;
353 }
354 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
355 decl = psi_decl_init(abi, func, args);
356 }
357 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) COMMA ELLIPSIS RPAREN EOS. {
358 decl = psi_decl_init(abi, func, args);
359 decl->varargs = 1;
360 }
361 decl_func(func) ::= decl_arg(arg). {
362 func = arg;
363 }
364 decl_func(func) ::= VOID(T) NAME(N). {
365 func = psi_decl_arg_init(
366 psi_decl_type_init(T->type, T->text),
367 psi_decl_var_init(N->text, 0, 0)
368 );
369 func->type->token = T;
370 func->var->token = N;
371 func->token = N;
372 }
373 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
374 struct psi_decl_arg *func_ = psi_decl_arg_init(
375 psi_decl_type_init(T->type, T->text),
376 psi_decl_var_init(N->text, decl_i, 0)
377 );
378 func_->type->token = T;
379 func_->var->token = N;
380 func_->token = N;
381 def = psi_decl_arg_init(
382 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
383 psi_decl_var_copy(func_->var)
384 );
385 def->var->pointer_level = type_i;
386 def->type->token = psi_token_copy(func_->token);
387 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
388 }
389 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
390 struct psi_decl_arg *func_ = psi_decl_arg_init(
391 psi_decl_type_init(T->type, T->text),
392 psi_decl_var_init(N->text, decl_i, 0)
393 );
394 func_->type->token = T;
395 func_->var->token = N;
396 func_->token = N;
397 def = psi_decl_arg_init(
398 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
399 psi_decl_var_copy(func_->var)
400 );
401 def->var->pointer_level = type_i;
402 def->type->token = psi_token_copy(func_->token);
403 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
404 }
405 decl_abi(abi) ::= NAME(T). {
406 abi = psi_decl_abi_init(T->text);
407 abi->token = T;
408 }
409 decl_var_array_size(as) ::= . {
410 as = NULL;
411 }
412 decl_var_array_size(as) ::= LBRACKET NUMBER(D) RBRACKET. {
413 as = D;
414 }
415 decl_var(var) ::= NAME(T) decl_var_array_size(as). {
416 var = psi_decl_var_init(T->text, 0, as?atol(as->text):0);
417 var->token = T;
418 if (as) {
419 free(as);
420 }
421 }
422 decl_var(var) ::= pointers(p) NAME(T) decl_var_array_size(as). {
423 var = psi_decl_var_init(T->text, p+!!as, as?atol(as->text):0);
424 var->token = T;
425 if (as) {
426 free(as);
427 }
428 }
429 decl_vars(vars) ::= decl_var(var). {
430 vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free),
431 &var);
432 }
433 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
434 vars = psi_plist_add(vars_, &var);
435 }
436 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
437 arg_ = psi_decl_arg_init(type, var);
438 }
439 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
440 struct psi_decl_arg *func_ = psi_decl_arg_init(
441 type_,
442 psi_decl_var_init(N->text, decl_i, 0)
443 );
444 func_->var->token = N;
445 func_->token = N;
446 def = psi_decl_arg_init(
447 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
448 psi_decl_var_copy(func_->var)
449 );
450 def->var->pointer_level = type_i;
451 def->type->token = psi_token_copy(func_->token);
452 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
453 }
454 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
455 arg_ = psi_decl_arg_init(
456 psi_decl_type_init(T->type, T->text),
457 psi_decl_var_init(N->text, p, 0)
458 );
459 arg_->type->token = T;
460 arg_->var->token = N;
461 arg_->token = N;
462 }
463 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
464 arg_ = psi_decl_arg_init(
465 psi_decl_type_init(T->type, T->text),
466 psi_decl_var_init(N->text, p, 0)
467 );
468 arg_->type->token = T;
469 arg_->var->token = N;
470 arg_->token = N;
471 }
472 decl_args ::= .
473 decl_args ::= VOID.
474 decl_args(args) ::= decl_arg(arg). {
475 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
476 &arg);
477 }
478 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
479 args = psi_plist_add(args_, &arg);
480 }
481 struct_args(args) ::= struct_arg(arg). {
482 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
483 &arg);
484 }
485 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
486 args = psi_plist_add(args_, &arg);
487 }
488 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
489 arg_ = def;
490 switch (def->type->type) {
491 case PSI_T_STRUCT:
492 if (def->type->real.strct) {
493 if (!P->structs) {
494 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
495 }
496 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
497 }
498 break;
499 case PSI_T_UNION:
500 if (def->type->real.unn) {
501 if (!P->unions) {
502 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
503 }
504 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
505 }
506 break;
507 case PSI_T_ENUM:
508 if (def->type->real.enm) {
509 if (!P->enums) {
510 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
511 }
512 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
513 }
514 break;
515 }
516 }
517 struct_arg(arg) ::= decl_arg(arg_) decl_layout(layout_) EOS. {
518 arg_->layout = layout_;
519 arg = arg_;
520 }
521 decl_layout(layout) ::= . {
522 layout = NULL;
523 }
524 decl_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
525 layout = psi_layout_init(atol(POS->text), atol(SIZ->text));
526 free(POS);
527 free(SIZ);
528 }
529 decl_scalar_type(type_) ::= CHAR(C). {
530 type_ = C;
531 }
532 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
533 if (s) {
534 type_ = psi_token_cat(2, S, s);
535 free(S);
536 free(s);
537 } else {
538 type_ = S;
539 }
540 }
541 decl_scalar_type_short(s) ::= . {
542 s = NULL;
543 }
544 decl_scalar_type_short(s) ::= INT(I). {
545 s = I;
546 }
547 decl_scalar_type(type_) ::= INT(I). {
548 type_ = I;
549 }
550 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
551 if (l) {
552 type_ = psi_token_cat(2, L, l);
553 free(L);
554 free(l);
555 } else {
556 type_ = L;
557 }
558 }
559 decl_scalar_type_long(l) ::= . {
560 l = NULL;
561 }
562 decl_scalar_type_long(l) ::= DOUBLE(D). {
563 l = D;
564 }
565 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
566 if (ll) {
567 l = psi_token_cat(2, L, ll);
568 free(L);
569 free(ll);
570 } else {
571 l = L;
572 }
573 }
574 decl_scalar_type_long_long(ll) ::= . {
575 ll = NULL;
576 }
577 decl_scalar_type_long_long(ll) ::= INT(I). {
578 ll = I;
579 }
580 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
581 struct psi_token *T = psi_token_cat(2, U, N);
582 type_ = psi_decl_type_init(T->type, T->text);
583 type_->token = T;
584 free(U);
585 free(N);
586 }
587 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
588 struct psi_token *T = psi_token_cat(2, S, N);
589 type_ = psi_decl_type_init(T->type, T->text);
590 type_->token = T;
591 free(S);
592 free(N);
593 }
594 decl_type(type_) ::= UNSIGNED(U). {
595 type_ = psi_decl_type_init(PSI_T_NAME, U->text);
596 type_->token = U;
597 }
598 decl_type(type_) ::= SIGNED(S). {
599 type_ = psi_decl_type_init(PSI_T_NAME, S->text);
600 type_->token = S;
601 }
602 decl_type(type_) ::= decl_scalar_type(N). {
603 type_ = psi_decl_type_init(N->type, N->text);
604 type_->token = N;
605 }
606 decl_type(type_) ::= STRUCT(S) NAME(T). {
607 type_ = psi_decl_type_init(S->type, T->text);
608 type_->token = T;
609 free(S);
610 }
611 decl_type(type_) ::= UNION(U) NAME(T). {
612 type_ = psi_decl_type_init(U->type, T->text);
613 type_->token = T;
614 free(U);
615 }
616 decl_type(type_) ::= ENUM(E) NAME(T). {
617 type_ = psi_decl_type_init(E->type, T->text);
618 type_->token = T;
619 free(E);
620 }
621 decl_type(type_) ::= decl_type_token(T). {
622 type_ = psi_decl_type_init(T->type, T->text);
623 type_->token = T;
624 }
625 const_decl_type(type) ::= decl_type(type_). {
626 type = type_;
627 }
628 const_decl_type(type) ::= CONST decl_type(type_). {
629 type = type_;
630 }
631 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
632 impl = psi_impl_init(func, stmts);
633 }
634 impl(impl) ::= STATIC impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
635 func->static_memory = 1;
636 impl = psi_impl_init(func, stmts);
637 }
638 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
639 func = psi_impl_func_init(NAME->text, NULL, type);
640 func->token = NAME;
641 func->return_reference = r;
642 }
643 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
644 func = psi_impl_func_init(NAME->text, args, type);
645 func->token = NAME;
646 func->return_reference = r;
647 }
648 impl_func(func) ::= FUNCTION reference(func_r) NSNAME(NAME) LPAREN impl_args(args) COMMA impl_type(va_type) reference(va_r) ELLIPSIS DOLLAR_NAME(T) RPAREN COLON impl_type(func_type). {
649 func = psi_impl_func_init(NAME->text, args, func_type);
650 func->token = NAME;
651 func->return_reference = func_r;
652 func->vararg = psi_impl_arg_init(va_type, psi_impl_var_init(T->text, va_r), NULL);
653 free(T);
654 }
655 impl_def_val(def) ::= impl_def_val_token(T). {
656 def = psi_impl_def_val_init(T->type, T->text);
657 def->token = T;
658 }
659 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
660 var = psi_impl_var_init(T->text, r);
661 var->token = T;
662 }
663 impl_type(type_) ::= impl_type_token(T). {
664 type_ = psi_impl_type_init(T->type, T->text);
665 free(T);
666 }
667 impl_arg(arg) ::= impl_type(type) impl_var(var). {
668 arg = psi_impl_arg_init(type, var, NULL);
669 }
670 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
671 arg = psi_impl_arg_init(type, var, def);
672 }
673 impl_args(args) ::= impl_arg(arg). {
674 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_arg_free),
675 &arg);
676 }
677 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
678 args = psi_plist_add(args_, &arg);
679 }
680 impl_stmts(stmts) ::= impl_stmt(stmt). {
681 stmts = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_stmt_free),
682 &stmt);
683 }
684 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
685 stmts = psi_plist_add(stmts_, &stmt);
686 }
687 impl_stmt(i) ::= return_stmt(r). {
688 i = (struct psi_token**) r;
689 }
690 impl_stmt(i) ::= let_stmt(l). {
691 i = (struct psi_token**) l;
692 }
693 impl_stmt(i) ::= set_stmt(s). {
694 i = (struct psi_token**) s;
695 }
696 impl_stmt(i) ::= free_stmt(f). {
697 i = (struct psi_token**) f;
698 }
699 num_exp(exp) ::= num_exp_token(tok). {
700 exp = psi_num_exp_init(tok->type, tok->text);
701 exp->token = tok;
702 }
703 num_exp(exp) ::= decl_var(var). {
704 exp = psi_num_exp_init(PSI_T_NAME, var);
705 exp->token = psi_token_copy(var->token);
706 }
707 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
708 exp_->op = operator_->type;
709 exp_->operand = operand_;
710 exp = exp_;
711 free(operator_);
712 }
713 let_exp(val) ::= NULL. {
714 val = psi_let_exp_init(PSI_LET_NULL, NULL);
715 }
716 let_exp(val) ::= AMPERSAND NULL. {
717 val = psi_let_exp_init(PSI_LET_NULL, NULL);
718 val->is_reference = 1;
719 }
720 let_exp(val) ::= let_callback(cb). {
721 val = psi_let_exp_init(PSI_LET_CALLBACK, cb);
722 }
723 let_exp(val) ::= let_calloc(ca). {
724 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
725 }
726 let_exp(val) ::= AMPERSAND let_calloc(ca). {
727 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
728 val->is_reference = 1;
729 }
730 let_exp(val) ::= let_func(fn). {
731 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
732 }
733 let_exp(val) ::= AMPERSAND let_func(fn). {
734 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
735 val->is_reference = 1;
736 }
737 let_exp(val) ::= num_exp(exp). {
738 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
739 }
740 let_exp(val) ::= AMPERSAND num_exp(exp). {
741 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
742 val->is_reference = 1;
743 }
744 let_exp(exp) ::= decl_var(var_) EQUALS let_exp(val). {
745 exp = val;
746 exp->var = var_;
747 }
748 let_stmt(let) ::= LET(T) let_exp(val) EOS. {
749 let = psi_let_stmt_init(val);
750 let->token = T;
751 }
752 let_stmt(let) ::= TEMP(T) decl_var(var) EQUALS reference(r) decl_var(val_) EOS. {
753 let = psi_let_stmt_init(psi_let_exp_init_ex(var, PSI_LET_TMP, val_));
754 let->token = T;
755 let->exp->is_reference = r ? 1 : 0;
756 }
757 let_callback(cb) ::= CALLBACK(T) callback_rval(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
758 cb = psi_let_callback_init(psi_let_func_init(F->type, F->text, var), args_);
759 cb->token = T;
760 free(F);
761 }
762 let_calloc(alloc) ::= CALLOC(T) LPAREN num_exp(nmemb) COMMA num_exp(size) RPAREN. {
763 alloc = psi_let_calloc_init(nmemb, size);
764 alloc->token = T;
765 }
766 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
767 func = psi_let_func_init(T->type, T->text, var);
768 func->token = T;
769 }
770 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) COMMA let_exps(vals) RPAREN. {
771 func = psi_let_func_init(T->type, T->text, var);
772 func->token = T;
773 func->inner = vals;
774 }
775 let_exps(exps) ::= let_exp(exp). {
776 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_let_exp_free),
777 &exp);
778 }
779 let_exps(exps) ::= let_exps(exps_) COMMA let_exp(exp). {
780 exps = psi_plist_add(exps_, &exp);
781 }
782 callback_arg_list ::= .
783 callback_arg_list(args) ::= callback_args(args_). {
784 args = args_;
785 }
786 callback_args(args) ::= set_exp(val). {
787 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
788 &val);
789 }
790 callback_args(args) ::= callback_args(args_) COMMA set_exp(val). {
791 args = psi_plist_add(args_, &val);
792 }
793 callback_rval(rval) ::= let_func_token(F). {
794 rval = F;
795 }
796 callback_rval(rval) ::= VOID(V). {
797 rval = V;
798 }
799 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) RPAREN. {
800 func = psi_set_func_init(T->type, T->text, var);
801 func->token = T;
802 }
803 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA set_exps(vals) RPAREN. {
804 func = psi_set_func_init(T->type, T->text, var);
805 func->token = T;
806 func->inner = vals;
807 }
808 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA ELLIPSIS RPAREN. {
809 func = psi_set_func_init(T->type, T->text, var);
810 func->token = T;
811 func->recursive = 1;
812 }
813 set_exp(val) ::= set_func(fn). {
814 val = psi_set_exp_init(PSI_SET_FUNC, fn);
815 }
816 set_exp(val) ::= num_exp(num). {
817 val = psi_set_exp_init(PSI_SET_NUMEXP, num);
818 }
819 set_exps(exps) ::= set_exp(exp). {
820 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
821 &exp);
822 }
823 set_exps(exps) ::= set_exps(exps_) COMMA set_exp(exp). {
824 exps = psi_plist_add(exps_, &exp);
825 }
826 set_exp(exp) ::= impl_var(var_) EQUALS set_exp(val). {
827 exp = val;
828 exp->var = var_;
829 }
830 set_stmt(set) ::= SET(T) set_exp(exp) EOS. {
831 set = psi_set_stmt_init(exp);
832 set->token = T;
833 }
834 return_stmt(ret) ::= RETURN(T) set_func(func) EOS. {
835 ret = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, func));
836 ret->token = T;
837 }
838 free_stmt(free) ::= FREE free_exps(calls) EOS. {
839 free = psi_free_stmt_init(calls);
840 }
841 free_exps(calls) ::= free_exp(call). {
842 calls = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free),
843 &call);
844 }
845 free_exps(calls) ::= free_exps(calls_) COMMA free_exp(call). {
846 calls = psi_plist_add(calls_, &call);
847 }
848 free_exp(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
849 call = psi_free_exp_init(F->text, vars);
850 call->token = F;
851 }
852 reference(r) ::= . {
853 r = false;
854 }
855 reference(r) ::= AMPERSAND. {
856 r = true;
857 }
858 indirection(i) ::= .{
859 i = 0;
860 }
861 indirection(i) ::= pointers(p). {
862 i = p;
863 }
864 pointers(p) ::= ASTERISK. {
865 p = 1;
866 }
867 pointers(p) ::= pointers(p_) ASTERISK. {
868 p = p_+1;
869 }