validation and marshaling of structs/unions
[m6w6/ext-psi] / src / parser_proc.y
1 %include {
2 #ifdef HAVE_CONFIG_H
3 # include "config.h"
4 #else
5 # include "php_config.h"
6 #endif
7
8 #include <stddef.h>
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13
14 #include "parser.h"
15 }
16 %include { void psi_error(int, const char *, int, const char *, ...); }
17 %name psi_parser_proc_
18 %token_prefix PSI_T_
19 %token_type {struct psi_token *}
20 %token_destructor {free($$);}
21 %default_destructor {(void)P;}
22 %extra_argument {struct psi_parser *P}
23 %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->psi.file.fn, P->line, "PSI syntax error: Unexpected end of input"); } }
24 %nonassoc NAME.
25 %left PLUS MINUS.
26 %left SLASH ASTERISK.
27 %fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING.
28 %token_class const_type_token BOOL INT FLOAT STRING.
29 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
30 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
31 %token_class num_exp_token NUMBER NSNAME.
32 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
33 %token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
34 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.
35 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
36 %type decl_enum {decl_enum *}
37 %destructor decl_enum {free_decl_enum($$);}
38 %type decl_enum_items {decl_enum_items*}
39 %destructor decl_enum_items {free_decl_enum_items($$);}
40 %type decl_enum_item {decl_enum_item*}
41 %destructor decl_enum_item {free_decl_enum_item($$);}
42 %type decl_struct_args_block {decl_args*}
43 %destructor decl_struct_args_block {free_decl_args($$);}
44 %type decl_struct_args {decl_args*}
45 %destructor decl_struct_args {free_decl_args($$);}
46 %type decl_struct {decl_struct*}
47 %destructor decl_struct {free_decl_struct($$);}
48 %type align_and_size {decl_struct_layout}
49 %type decl_union {decl_union*}
50 %destructor decl_union {free_decl_union($$);}
51 %type const_type {const_type*}
52 %destructor const_type {free_const_type($$);}
53 %type constant {constant*}
54 %destructor constant {free_constant($$);}
55 %type decl_typedef {decl_arg*}
56 %destructor decl_typedef {free_decl_arg($$);}
57 %type decl_typedef_body_ex {decl_arg*}
58 %destructor decl_typedef_body_ex {free_decl_arg($$);}
59 %type decl_typedef_body {decl_arg*}
60 %destructor decl_typedef_body {free_decl_arg($$);}
61 %type decl_typedef_body_fn_args {decl_args *}
62 %destructor decl_typedef_body_fn_args {free_decl_args($$);}
63 %type decl {decl*}
64 %destructor decl {free_decl($$);}
65 %type decl_func {decl_arg*}
66 %destructor decl_func {free_decl_arg($$);}
67 %type decl_abi {decl_abi*}
68 %destructor decl_abi {free_decl_abi($$);}
69 %type decl_var {decl_var*}
70 %destructor decl_var {free_decl_var($$);}
71 %type decl_vars {decl_vars*}
72 %destructor decl_vars {free_decl_vars($$);}
73 %type decl_arg {decl_arg*}
74 %destructor decl_arg {free_decl_arg($$);}
75 %type decl_args {decl_args*}
76 %destructor decl_args {free_decl_args($$);}
77 %type struct_args {decl_args*}
78 %destructor struct_args {free_decl_args($$);}
79 %type struct_arg {decl_arg*}
80 %destructor struct_arg {free_decl_arg($$);}
81 %type struct_layout {decl_struct_layout*}
82 %destructor struct_layout {free_decl_struct_layout($$);}
83 %type decl_type {decl_type*}
84 %destructor decl_type {free_decl_type($$);}
85 %type const_decl_type {decl_type*}
86 %destructor const_decl_type {free_decl_type($$);}
87 %type impl {impl*}
88 %destructor impl {free_impl($$);}
89 %type impl_func {impl_func*}
90 %destructor impl_func {free_impl_func($$);}
91 %type impl_def_val {impl_def_val*}
92 %destructor impl_def_val {free_impl_def_val($$);}
93 %type impl_var {impl_var*}
94 %destructor impl_var {free_impl_var($$);}
95 %type impl_arg {impl_arg*}
96 %destructor impl_arg {free_impl_arg($$);}
97 %type impl_args {impl_args*}
98 %destructor impl_args {free_impl_args($$);}
99 %type impl_vararg {impl_arg*}
100 %destructor impl_vararg {free_impl_arg($$);}
101 %type impl_arg_list {impl_args*}
102 %destructor impl_arg_list {free_impl_args($$);}
103 %type impl_stmts {impl_stmts*}
104 %destructor impl_stmts {free_impl_stmts($$);}
105 %type impl_stmt {impl_stmt*}
106 %destructor impl_stmt {free_impl_stmt($$);}
107 %type num_exp {num_exp*}
108 %destructor num_exp {free_num_exp($$);}
109 %type let_stmt {let_stmt*}
110 %destructor let_stmt {free_let_stmt($$);}
111 %type let_calloc {let_calloc*}
112 %destructor let_calloc {free_let_calloc($$);}
113 %type let_func {let_func*}
114 %destructor let_func {free_let_func($$);}
115 %type callback_arg_list {set_values *}
116 %destructor callback_arg_list {free_set_values($$);}
117 %type callback_args {set_values *}
118 %destructor callback_args {free_set_values($$);}
119 %type let_val {let_val*}
120 %destructor let_val {free_let_val($$);}
121 %type let_vals {let_vals*}
122 %destructor let_vals {free_let_vals($$);}
123 %type set_stmt {set_stmt*}
124 %destructor set_stmt {free_set_stmt($$);}
125 %type set_value {set_value*}
126 %destructor set_value {free_set_value($$);}
127 %type set_vals {set_value*}
128 %destructor set_vals {free_set_value($$);}
129 %type set_func {set_func*}
130 %destructor set_func {free_set_func($$);}
131 %type return_stmt {return_stmt*}
132 %destructor return_stmt {free_return_stmt($$);}
133 %type free_stmt {free_stmt*}
134 %destructor free_stmt {free_free_stmt($$);}
135 %type free_calls {free_calls*}
136 %destructor free_calls {free_free_calls($$);}
137 %type free_call {free_call*}
138 %destructor free_call {free_free_call($$);}
139 %type impl_type {impl_type*}
140 %destructor impl_type {free_impl_type($$);}
141 %type reference {char}
142 %type indirection {unsigned}
143 %type pointers {unsigned}
144 file ::= blocks.
145 blocks ::= block.
146 blocks ::= blocks block.
147 block ::= EOF.
148 block ::= EOS.
149 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
150 if (P->psi.file.ln) {
151 P->error(P, token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
152 } else {
153 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
154 }
155 free(libname);
156 free(token);
157 }
158 block ::= decl(decl). {
159 P->decls = add_decl(P->decls, decl);
160 }
161 block ::= impl(impl). {
162 P->impls = add_impl(P->impls, impl);
163 }
164 block ::= decl_typedef(def). {
165 P->defs = add_decl_typedef(P->defs, def);
166 switch (def->type->type) {
167 case PSI_T_STRUCT:
168 if (def->type->real.strct) {
169 P->structs = add_decl_struct(P->structs, def->type->real.strct);
170 }
171 break;
172 case PSI_T_UNION:
173 if (def->type->real.unn) {
174 P->unions = add_decl_union(P->unions, def->type->real.unn);
175 }
176 break;
177 case PSI_T_ENUM:
178 if (def->type->real.enm) {
179 P->enums = add_decl_enum(P->enums, def->type->real.enm);
180 }
181 break;
182 }
183 }
184 block ::= constant(constant). {
185 P->consts = add_constant(P->consts, constant);
186 }
187 block ::= decl_struct(strct). {
188 P->structs = add_decl_struct(P->structs, strct);
189 }
190 block ::= decl_union(u). {
191 P->unions = add_decl_union(P->unions, u);
192 }
193 block ::= decl_enum(e). {
194 P->enums = add_decl_enum(P->enums, e);
195 }
196 optional_name(n) ::= . {
197 n = NULL;
198 }
199 optional_name(n) ::= NAME(N). {
200 n = N;
201 }
202 enum_name(n) ::= ENUM(E) optional_name(N). {
203 if (N) {
204 n = N;
205 free(E);
206 } else {
207 char digest[17];
208 psi_token_hash(E, digest);
209 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
210 }
211 }
212 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
213 e = init_decl_enum(N->text, list);
214 e->token = N;
215 }
216 decl_enum_items(l) ::= decl_enum_item(i). {
217 l = init_decl_enum_items(i);
218 }
219 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
220 l = add_decl_enum_item(l_, i);
221 }
222 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
223 i = init_decl_enum_item(N->text, num);
224 i->token = N;
225 }
226 decl_enum_item(i) ::= NAME(N). {
227 i = init_decl_enum_item(N->text, NULL);
228 i->token = N;
229 }
230 union_name(n) ::= UNION(U) optional_name(N). {
231 if (N) {
232 n = N;
233 free(U);
234 } else {
235 char digest[17];
236 psi_token_hash(U, digest);
237 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
238 }
239 }
240 struct_name(n) ::= STRUCT(S) optional_name(N). {
241 if (N) {
242 n = N;
243 free(S);
244 } else {
245 char digest[17];
246 psi_token_hash(S, digest);
247 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
248 }
249 }
250 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
251 args_ = args;
252 }
253 decl_struct_args(args_) ::= decl_struct_args_block(args). {
254 args_ = args;
255 }
256 decl_struct_args(args_) ::= EOS. {
257 args_ = init_decl_args(NULL);
258 }
259 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
260 strct = init_decl_struct(N->text, args);
261 strct->align = as.pos;
262 strct->size = as.len;
263 strct->token = N;
264 }
265 align_and_size(as) ::= . {
266 as.pos = 0;
267 as.len = 0;
268 }
269 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
270 as.pos = atol(A->text);
271 as.len = atol(S->text);
272 free(A);
273 free(S);
274 }
275 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
276 u = init_decl_union(N->text, args);
277 u->align = as.pos;
278 u->size = as.len;
279 u->token = N;
280 }
281 const_type(type_) ::= const_type_token(T). {
282 type_ = init_const_type(T->type, T->text);
283 free(T);
284 }
285 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
286 constant = init_constant(type, T->text, val);
287 free(T);
288 }
289 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
290 def = def_;
291 def->token = T;
292 }
293 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
294 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
295 def->type->token = psi_token_copy(N);
296 def->type->real.strct = init_decl_struct(N->text, args);
297 def->type->real.strct->token = N;
298 def->type->real.strct->align = as.pos;
299 def->type->real.strct->size = as.len;
300 }
301 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
302 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
303 def->type->token = psi_token_copy(N);
304 def->type->real.unn = init_decl_union(N->text, args);
305 def->type->real.unn->token = N;
306 def->type->real.unn->align = as.pos;
307 def->type->real.unn->size = as.len;
308 }
309 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
310 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
311 def->var->token = ALIAS;
312 def->type->token = psi_token_copy(e->token);
313 def->type->real.enm = e;
314 }
315 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
316 def = def_;
317 }
318 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
319 args = args_;
320 }
321 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
322 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
323 def->type->token = psi_token_copy(func_->token);
324 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
325 }
326 decl_typedef_body(def) ::= decl_arg(arg). {
327 def = arg;
328 }
329 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
330 decl = init_decl(abi, func, args);
331 }
332 decl_func(func) ::= decl_arg(arg). {
333 func = arg;
334 }
335 decl_func(func) ::= VOID(T) NAME(N). {
336 func = init_decl_arg(
337 init_decl_type(T->type, T->text),
338 init_decl_var(N->text, 0, 0)
339 );
340 func->type->token = T;
341 func->var->token = N;
342 func->token = N;
343 }
344 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
345 decl_arg *func_ = init_decl_arg(
346 init_decl_type(T->type, T->text),
347 init_decl_var(N->text, decl_i, 0)
348 );
349 func_->type->token = T;
350 func_->var->token = N;
351 func_->token = N;
352 def = init_decl_arg(
353 init_decl_type(PSI_T_FUNCTION, func_->var->name),
354 copy_decl_var(func_->var)
355 );
356 def->var->pointer_level = type_i;
357 def->type->token = psi_token_copy(func_->token);
358 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
359 }
360 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
361 decl_arg *func_ = init_decl_arg(
362 init_decl_type(T->type, T->text),
363 init_decl_var(N->text, decl_i, 0)
364 );
365 func_->type->token = T;
366 func_->var->token = N;
367 func_->token = N;
368 def = init_decl_arg(
369 init_decl_type(PSI_T_FUNCTION, func_->var->name),
370 copy_decl_var(func_->var)
371 );
372 def->var->pointer_level = type_i;
373 def->type->token = psi_token_copy(func_->token);
374 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
375 }
376 decl_abi(abi) ::= NAME(T). {
377 abi = init_decl_abi(T->text);
378 abi->token = T;
379 }
380 decl_var(var) ::= indirection(p) NAME(T). {
381 var = init_decl_var(T->text, p, 0);
382 var->token = T;
383 }
384 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
385 var = init_decl_var(T->text, p+1, atol(D->text));
386 var->token = T;
387 free(D);
388 }
389 decl_vars(vars) ::= decl_var(var). {
390 vars = init_decl_vars(var);
391 }
392 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
393 vars = add_decl_var(vars_, var);
394 }
395 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
396 arg_ = init_decl_arg(type, var);
397 }
398 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
399 decl_arg *func_ = init_decl_arg(
400 type_,
401 init_decl_var(N->text, decl_i, 0)
402 );
403 func_->var->token = N;
404 func_->token = N;
405 def = init_decl_arg(
406 init_decl_type(PSI_T_FUNCTION, func_->var->name),
407 copy_decl_var(func_->var)
408 );
409 def->var->pointer_level = type_i;
410 def->type->token = psi_token_copy(func_->token);
411 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
412 }
413 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
414 arg_ = init_decl_arg(
415 init_decl_type(T->type, T->text),
416 init_decl_var(N->text, p, 0)
417 );
418 arg_->type->token = T;
419 arg_->var->token = N;
420 arg_->token = N;
421 }
422 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
423 arg_ = init_decl_arg(
424 init_decl_type(T->type, T->text),
425 init_decl_var(N->text, p, 0)
426 );
427 arg_->type->token = T;
428 arg_->var->token = N;
429 arg_->token = N;
430 }
431 decl_args ::= .
432 decl_args ::= VOID.
433 decl_args(args) ::= decl_arg(arg). {
434 args = init_decl_args(arg);
435 }
436 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
437 args = add_decl_arg(args_, arg);
438 }
439 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
440 args = args_;
441 args->varargs = 1;
442 }
443 struct_args(args) ::= struct_arg(arg). {
444 args = init_decl_args(arg);
445 }
446 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
447 args = add_decl_arg(args_, arg);
448 }
449 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
450 arg_ = def;
451 switch (def->type->type) {
452 case PSI_T_STRUCT:
453 if (def->type->real.strct) {
454 P->structs = add_decl_struct(P->structs, def->type->real.strct);
455 }
456 break;
457 case PSI_T_UNION:
458 if (def->type->real.unn) {
459 P->unions = add_decl_union(P->unions, def->type->real.unn);
460 }
461 break;
462 case PSI_T_ENUM:
463 if (def->type->real.enm) {
464 P->enums = add_decl_enum(P->enums, def->type->real.enm);
465 }
466 break;
467 }
468 }
469 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
470 arg_->layout = layout_;
471 arg = arg_;
472 }
473 struct_layout(layout) ::= . {
474 layout = NULL;
475 }
476 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
477 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
478 free(POS);
479 free(SIZ);
480 }
481 decl_scalar_type(type_) ::= CHAR(C). {
482 type_ = C;
483 }
484 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
485 if (s) {
486 type_ = psi_token_cat(2, S, s);
487 free(S);
488 free(s);
489 } else {
490 type_ = S;
491 }
492 }
493 decl_scalar_type_short(s) ::= . {
494 s = NULL;
495 }
496 decl_scalar_type_short(s) ::= INT(I). {
497 s = I;
498 }
499 decl_scalar_type(type_) ::= INT(I). {
500 type_ = I;
501 }
502 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
503 if (l) {
504 type_ = psi_token_cat(2, L, l);
505 free(L);
506 free(l);
507 } else {
508 type_ = L;
509 }
510 }
511 decl_scalar_type_long(l) ::= . {
512 l = NULL;
513 }
514 decl_scalar_type_long(l) ::= DOUBLE(D). {
515 l = D;
516 }
517 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
518 if (ll) {
519 l = psi_token_cat(2, L, ll);
520 free(L);
521 free(ll);
522 } else {
523 l = L;
524 }
525 }
526 decl_scalar_type_long_long(ll) ::= . {
527 ll = NULL;
528 }
529 decl_scalar_type_long_long(ll) ::= INT(I). {
530 ll = I;
531 }
532 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
533 struct psi_token *T = psi_token_cat(2, U, N);
534 type_ = init_decl_type(T->type, T->text);
535 type_->token = T;
536 free(U);
537 free(N);
538 }
539 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
540 struct psi_token *T = psi_token_cat(2, S, N);
541 type_ = init_decl_type(T->type, T->text);
542 type_->token = T;
543 free(S);
544 free(N);
545 }
546 decl_type(type_) ::= UNSIGNED(U). {
547 type_ = init_decl_type(PSI_T_NAME, U->text);
548 type_->token = U;
549 }
550 decl_type(type_) ::= SIGNED(S). {
551 type_ = init_decl_type(PSI_T_NAME, S->text);
552 type_->token = S;
553 }
554 decl_type(type_) ::= decl_scalar_type(N). {
555 type_ = init_decl_type(N->type, N->text);
556 type_->token = N;
557 }
558 decl_type(type_) ::= STRUCT(S) NAME(T). {
559 type_ = init_decl_type(S->type, T->text);
560 type_->token = T;
561 free(S);
562 }
563 decl_type(type_) ::= UNION(U) NAME(T). {
564 type_ = init_decl_type(U->type, T->text);
565 type_->token = T;
566 free(U);
567 }
568 decl_type(type_) ::= ENUM(E) NAME(T). {
569 type_ = init_decl_type(E->type, T->text);
570 type_->token = T;
571 free(E);
572 }
573 decl_type(type_) ::= decl_type_token(T). {
574 type_ = init_decl_type(T->type, T->text);
575 type_->token = T;
576 }
577 const_decl_type(type) ::= decl_type(type_). {
578 type = type_;
579 }
580 const_decl_type(type) ::= CONST decl_type(type_). {
581 type = type_;
582 }
583 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
584 impl = init_impl(func, stmts);
585 }
586 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
587 func = init_impl_func(NAME->text, args, type, r);
588 func->token = NAME;
589 }
590 impl_def_val(def) ::= impl_def_val_token(T). {
591 def = init_impl_def_val(T->type, T->text);
592 free(T);
593 }
594 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
595 var = init_impl_var(T->text, r);
596 var->token = T;
597 }
598 impl_arg(arg) ::= impl_type(type) impl_var(var). {
599 arg = init_impl_arg(type, var, NULL);
600 }
601 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
602 arg = init_impl_arg(type, var, def);
603 }
604 impl_args(args) ::= LPAREN RPAREN. {
605 args = NULL;
606 }
607 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
608 args = args_;
609 }
610 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
611 args = args_;
612 args->vararg.name = va;
613 }
614 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR_NAME(T). {
615 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
616 free(T);
617 }
618 impl_arg_list(args) ::= impl_arg(arg). {
619 args = init_impl_args(arg);
620 }
621 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
622 args = add_impl_arg(args_, arg);
623 }
624 impl_stmts(stmts) ::= impl_stmt(stmt). {
625 stmts = init_impl_stmts(stmt);
626 }
627 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
628 stmts = add_impl_stmt(stmts_, stmt);
629 }
630 impl_stmt(stmt) ::= let_stmt(let). {
631 stmt = init_impl_stmt(PSI_T_LET, let);
632 }
633 impl_stmt(stmt) ::= set_stmt(set). {
634 stmt = init_impl_stmt(PSI_T_SET, set);
635 }
636 impl_stmt(stmt) ::= return_stmt(ret). {
637 stmt = init_impl_stmt(PSI_T_RETURN, ret);
638 }
639 impl_stmt(stmt) ::= free_stmt(free). {
640 stmt = init_impl_stmt(PSI_T_FREE, free);
641 }
642 num_exp(exp) ::= num_exp_token(tok). {
643 exp = init_num_exp(tok->type, tok->text);
644 exp->token = tok;
645 }
646 num_exp(exp) ::= decl_var(var). {
647 exp = init_num_exp(PSI_T_NAME, var);
648 exp->token = psi_token_copy(var->token);
649 }
650 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
651 exp_->operator = operator_->type;
652 exp_->operand = operand_;
653 exp = exp_;
654 free(operator_);
655 }
656 let_stmt(let) ::= LET decl_var(var) EOS. {
657 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
658 }
659 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
660 val->flags.one.is_reference = r ? 1 : 0;
661 let = init_let_stmt(var, val);
662 }
663 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
664 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
665 }
666 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
667 alloc = init_let_calloc(nmemb, size);
668 }
669 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
670 func = init_let_func(T->type, T->text, var);
671 free(T);
672 }
673 let_vals(vals) ::= let_val(val). {
674 vals = init_let_vals(val);
675 }
676 let_vals(vals) ::= let_vals(vals_) COMMA let_val(val). {
677 vals = add_let_val(vals_, val);
678 }
679 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) COMMA let_vals(vals) RPAREN. {
680 func = init_let_func(T->type, T->text, var);
681 func->inner = vals;
682 free(T);
683 }
684 callback_arg_list ::= .
685 callback_arg_list(args) ::= callback_args(args_). {
686 args = args_;
687 }
688 callback_args(args) ::= set_value(val). {
689 args = init_set_values(val);
690 }
691 callback_args(args) ::= callback_args(args_) COMMA set_value(val). {
692 args = add_set_value(args_, val);
693 }
694 callback_rval(rval) ::= let_func_token(F). {
695 rval = F;
696 }
697 callback_rval(rval) ::= VOID(V). {
698 rval = V;
699 }
700 let_val(val) ::= NULL. {
701 val = init_let_val(PSI_LET_NULL, NULL);
702 }
703 let_val(val) ::= num_exp(exp). {
704 val = init_let_val(PSI_LET_NUMEXP, exp);
705 }
706 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
707 val = init_let_val(PSI_LET_CALLOC, alloc);
708 }
709 let_val(val) ::= let_func(func_). {
710 val = init_let_val(PSI_LET_FUNC, func_);
711 }
712 let_val(val) ::= CALLBACK callback_rval(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
713 val = init_let_val(PSI_LET_CALLBACK, init_let_callback(
714 init_let_func(F->type, F->text, var), args_));
715 free(F);
716 }
717 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
718 set = init_set_stmt(var, val);
719 }
720 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
721 val = init_set_value(func, init_decl_vars(var));
722 }
723 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
724 val = init_set_value(func, init_decl_vars(var));
725 val->num = num_;
726 }
727 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
728 free_set_func(func_);
729 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
730 val->func->token = T;
731 }
732 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
733 val = vals;
734 val->func = func_;
735 val->vars = init_decl_vars(var);
736 }
737 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
738 val = vals;
739 val->func = func_;
740 val->num = num_;
741 val->vars = init_decl_vars(var);
742 }
743 set_vals(vals) ::= set_value(val). {
744 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
745 }
746 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
747 vals = add_inner_set_value(vals_, val);
748 }
749 set_func(func) ::= set_func_token(T). {
750 func = init_set_func(T->type, T->text);
751 func->token = T;
752 }
753 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
754 ret = init_return_stmt(val);
755 ret->token = T;
756 }
757 free_stmt(free) ::= FREE free_calls(calls) EOS. {
758 free = init_free_stmt(calls);
759 }
760 free_calls(calls) ::= free_call(call). {
761 calls = init_free_calls(call);
762 }
763 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
764 calls = add_free_call(calls_, call);
765 }
766 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
767 call = init_free_call(F->text, vars);
768 call->token = F;
769 }
770 impl_type(type_) ::= impl_type_token(T). {
771 type_ = init_impl_type(T->type, T->text);
772 free(T);
773 }
774 reference(r) ::= . {
775 r = 0;
776 }
777 reference(r) ::= AMPERSAND. {
778 r = 1;
779 }
780 indirection(i) ::= .{
781 i = 0;
782 }
783 indirection(i) ::= pointers(p). {
784 i = p;
785 }
786 pointers(p) ::= ASTERISK. {
787 p = 1;
788 }
789 pointers(p) ::= pointers(p_) ASTERISK. {
790 p = p_+1;
791 }