api refactoring
[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'", TOKEN->text); } 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 set_stmt {set_stmt*}
122 %destructor set_stmt {free_set_stmt($$);}
123 %type set_value {set_value*}
124 %destructor set_value {free_set_value($$);}
125 %type set_vals {set_value*}
126 %destructor set_vals {free_set_value($$);}
127 %type set_func {set_func*}
128 %destructor set_func {free_set_func($$);}
129 %type return_stmt {return_stmt*}
130 %destructor return_stmt {free_return_stmt($$);}
131 %type free_stmt {free_stmt*}
132 %destructor free_stmt {free_free_stmt($$);}
133 %type free_calls {free_calls*}
134 %destructor free_calls {free_free_calls($$);}
135 %type free_call {free_call*}
136 %destructor free_call {free_free_call($$);}
137 %type impl_type {impl_type*}
138 %destructor impl_type {free_impl_type($$);}
139 %type reference {char}
140 %type indirection {unsigned}
141 %type pointers {unsigned}
142 file ::= blocks.
143 blocks ::= block.
144 blocks ::= blocks block.
145 block ::= EOF.
146 block ::= EOS.
147 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
148 if (P->psi.file.ln) {
149 P->error(P, token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
150 } else {
151 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
152 }
153 free(libname);
154 free(token);
155 }
156 block ::= decl(decl). {
157 P->decls = add_decl(P->decls, decl);
158 }
159 block ::= impl(impl). {
160 P->impls = add_impl(P->impls, impl);
161 }
162 block ::= decl_typedef(def). {
163 P->defs = add_decl_typedef(P->defs, def);
164 switch (def->type->type) {
165 case PSI_T_STRUCT:
166 if (def->type->real.strct) {
167 P->structs = add_decl_struct(P->structs, def->type->real.strct);
168 }
169 break;
170 case PSI_T_UNION:
171 if (def->type->real.unn) {
172 P->unions = add_decl_union(P->unions, def->type->real.unn);
173 }
174 break;
175 case PSI_T_ENUM:
176 if (def->type->real.enm) {
177 P->enums = add_decl_enum(P->enums, def->type->real.enm);
178 }
179 break;
180 }
181 }
182 block ::= constant(constant). {
183 P->consts = add_constant(P->consts, constant);
184 }
185 block ::= decl_struct(strct). {
186 P->structs = add_decl_struct(P->structs, strct);
187 }
188 block ::= decl_union(u). {
189 P->unions = add_decl_union(P->unions, u);
190 }
191 block ::= decl_enum(e). {
192 P->enums = add_decl_enum(P->enums, e);
193 }
194 optional_name(n) ::= . {
195 n = NULL;
196 }
197 optional_name(n) ::= NAME(N). {
198 n = N;
199 }
200 enum_name(n) ::= ENUM(E) optional_name(N). {
201 if (N) {
202 n = N;
203 free(E);
204 } else {
205 char digest[17];
206 psi_token_hash(E, digest);
207 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
208 }
209 }
210 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
211 e = init_decl_enum(N->text, list);
212 e->token = N;
213 }
214 decl_enum_items(l) ::= decl_enum_item(i). {
215 l = init_decl_enum_items(i);
216 }
217 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
218 l = add_decl_enum_item(l_, i);
219 }
220 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
221 i = init_decl_enum_item(N->text, num);
222 i->token = N;
223 }
224 decl_enum_item(i) ::= NAME(N). {
225 i = init_decl_enum_item(N->text, NULL);
226 i->token = N;
227 }
228 union_name(n) ::= UNION(U) optional_name(N). {
229 if (N) {
230 n = N;
231 free(U);
232 } else {
233 char digest[17];
234 psi_token_hash(U, digest);
235 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
236 }
237 }
238 struct_name(n) ::= STRUCT(S) optional_name(N). {
239 if (N) {
240 n = N;
241 free(S);
242 } else {
243 char digest[17];
244 psi_token_hash(S, digest);
245 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
246 }
247 }
248 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
249 args_ = args;
250 }
251 decl_struct_args(args_) ::= decl_struct_args_block(args). {
252 args_ = args;
253 }
254 decl_struct_args(args_) ::= EOS. {
255 args_ = init_decl_args(NULL);
256 }
257 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
258 strct = init_decl_struct(N->text, args);
259 strct->align = as.pos;
260 strct->size = as.len;
261 strct->token = N;
262 }
263 align_and_size(as) ::= . {
264 as.pos = 0;
265 as.len = 0;
266 }
267 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
268 as.pos = atol(A->text);
269 as.len = atol(S->text);
270 free(A);
271 free(S);
272 }
273 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
274 u = init_decl_union(N->text, args);
275 u->align = as.pos;
276 u->size = as.len;
277 u->token = N;
278 }
279 const_type(type_) ::= const_type_token(T). {
280 type_ = init_const_type(T->type, T->text);
281 free(T);
282 }
283 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
284 constant = init_constant(type, T->text, val);
285 free(T);
286 }
287 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
288 def = def_;
289 def->token = T;
290 }
291 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
292 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
293 def->type->token = psi_token_copy(N);
294 def->type->real.strct = init_decl_struct(N->text, args);
295 def->type->real.strct->token = N;
296 def->type->real.strct->align = as.pos;
297 def->type->real.strct->size = as.len;
298 }
299 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
300 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
301 def->type->token = psi_token_copy(N);
302 def->type->real.unn = init_decl_union(N->text, args);
303 def->type->real.unn->token = N;
304 def->type->real.unn->align = as.pos;
305 def->type->real.unn->size = as.len;
306 }
307 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
308 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
309 def->var->token = ALIAS;
310 def->type->token = psi_token_copy(e->token);
311 def->type->real.enm = e;
312 }
313 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
314 def = def_;
315 }
316 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
317 args = args_;
318 }
319 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
320 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
321 def->type->token = psi_token_copy(func_->token);
322 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
323 }
324 decl_typedef_body(def) ::= decl_arg(arg). {
325 def = arg;
326 }
327 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
328 decl = init_decl(abi, func, args);
329 }
330 decl_func(func) ::= decl_arg(arg). {
331 func = arg;
332 }
333 decl_func(func) ::= VOID(T) NAME(N). {
334 func = init_decl_arg(
335 init_decl_type(T->type, T->text),
336 init_decl_var(N->text, 0, 0)
337 );
338 func->type->token = T;
339 func->var->token = N;
340 func->token = N;
341 }
342 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
343 decl_arg *func_ = init_decl_arg(
344 init_decl_type(T->type, T->text),
345 init_decl_var(N->text, decl_i, 0)
346 );
347 func_->type->token = T;
348 func_->var->token = N;
349 func_->token = N;
350 def = init_decl_arg(
351 init_decl_type(PSI_T_FUNCTION, func_->var->name),
352 copy_decl_var(func_->var)
353 );
354 def->var->pointer_level = type_i;
355 def->type->token = psi_token_copy(func_->token);
356 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
357 }
358 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
359 decl_arg *func_ = init_decl_arg(
360 init_decl_type(T->type, T->text),
361 init_decl_var(N->text, decl_i, 0)
362 );
363 func_->type->token = T;
364 func_->var->token = N;
365 func_->token = N;
366 def = init_decl_arg(
367 init_decl_type(PSI_T_FUNCTION, func_->var->name),
368 copy_decl_var(func_->var)
369 );
370 def->var->pointer_level = type_i;
371 def->type->token = psi_token_copy(func_->token);
372 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
373 }
374 decl_abi(abi) ::= NAME(T). {
375 abi = init_decl_abi(T->text);
376 abi->token = T;
377 }
378 decl_var(var) ::= indirection(p) NAME(T). {
379 var = init_decl_var(T->text, p, 0);
380 var->token = T;
381 }
382 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
383 var = init_decl_var(T->text, p+1, atol(D->text));
384 var->token = T;
385 free(D);
386 }
387 decl_vars(vars) ::= decl_var(var). {
388 vars = init_decl_vars(var);
389 }
390 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
391 vars = add_decl_var(vars_, var);
392 }
393 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
394 arg_ = init_decl_arg(type, var);
395 }
396 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
397 decl_arg *func_ = init_decl_arg(
398 type_,
399 init_decl_var(N->text, decl_i, 0)
400 );
401 func_->var->token = N;
402 func_->token = N;
403 def = init_decl_arg(
404 init_decl_type(PSI_T_FUNCTION, func_->var->name),
405 copy_decl_var(func_->var)
406 );
407 def->var->pointer_level = type_i;
408 def->type->token = psi_token_copy(func_->token);
409 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
410 }
411 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
412 arg_ = init_decl_arg(
413 init_decl_type(T->type, T->text),
414 init_decl_var(N->text, p, 0)
415 );
416 arg_->type->token = T;
417 arg_->var->token = N;
418 arg_->token = N;
419 }
420 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
421 arg_ = init_decl_arg(
422 init_decl_type(T->type, T->text),
423 init_decl_var(N->text, p, 0)
424 );
425 arg_->type->token = T;
426 arg_->var->token = N;
427 arg_->token = N;
428 }
429 decl_args ::= .
430 decl_args ::= VOID.
431 decl_args(args) ::= decl_arg(arg). {
432 args = init_decl_args(arg);
433 }
434 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
435 args = add_decl_arg(args_, arg);
436 }
437 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
438 args = args_;
439 args->varargs = 1;
440 }
441 struct_args(args) ::= struct_arg(arg). {
442 args = init_decl_args(arg);
443 }
444 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
445 args = add_decl_arg(args_, arg);
446 }
447 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
448 arg_ = def;
449 switch (def->type->type) {
450 case PSI_T_STRUCT:
451 if (def->type->real.strct) {
452 P->structs = add_decl_struct(P->structs, def->type->real.strct);
453 }
454 break;
455 case PSI_T_UNION:
456 if (def->type->real.unn) {
457 P->unions = add_decl_union(P->unions, def->type->real.unn);
458 }
459 break;
460 case PSI_T_ENUM:
461 if (def->type->real.enm) {
462 P->enums = add_decl_enum(P->enums, def->type->real.enm);
463 }
464 break;
465 }
466 }
467 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
468 arg_->layout = layout_;
469 arg = arg_;
470 }
471 struct_layout(layout) ::= . {
472 layout = NULL;
473 }
474 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
475 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
476 free(POS);
477 free(SIZ);
478 }
479 decl_scalar_type(type_) ::= CHAR(C). {
480 type_ = C;
481 }
482 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
483 if (s) {
484 type_ = psi_token_cat(2, S, s);
485 free(S);
486 free(s);
487 } else {
488 type_ = S;
489 }
490 }
491 decl_scalar_type_short(s) ::= . {
492 s = NULL;
493 }
494 decl_scalar_type_short(s) ::= INT(I). {
495 s = I;
496 }
497 decl_scalar_type(type_) ::= INT(I). {
498 type_ = I;
499 }
500 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
501 if (l) {
502 type_ = psi_token_cat(2, L, l);
503 free(L);
504 free(l);
505 } else {
506 type_ = L;
507 }
508 }
509 decl_scalar_type_long(l) ::= . {
510 l = NULL;
511 }
512 decl_scalar_type_long(l) ::= DOUBLE(D). {
513 l = D;
514 }
515 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
516 if (ll) {
517 l = psi_token_cat(2, L, ll);
518 free(L);
519 free(ll);
520 } else {
521 l = L;
522 }
523 }
524 decl_scalar_type_long_long(ll) ::= . {
525 ll = NULL;
526 }
527 decl_scalar_type_long_long(ll) ::= INT(I). {
528 ll = I;
529 }
530 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
531 struct psi_token *T = psi_token_cat(2, U, N);
532 type_ = init_decl_type(T->type, T->text);
533 type_->token = T;
534 free(U);
535 free(N);
536 }
537 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
538 struct psi_token *T = psi_token_cat(2, S, N);
539 type_ = init_decl_type(T->type, T->text);
540 type_->token = T;
541 free(S);
542 free(N);
543 }
544 decl_type(type_) ::= UNSIGNED(U). {
545 type_ = init_decl_type(PSI_T_NAME, U->text);
546 type_->token = U;
547 }
548 decl_type(type_) ::= SIGNED(S). {
549 type_ = init_decl_type(PSI_T_NAME, S->text);
550 type_->token = S;
551 }
552 decl_type(type_) ::= decl_scalar_type(N). {
553 type_ = init_decl_type(N->type, N->text);
554 type_->token = N;
555 }
556 decl_type(type_) ::= STRUCT(S) NAME(T). {
557 type_ = init_decl_type(S->type, T->text);
558 type_->token = T;
559 free(S);
560 }
561 decl_type(type_) ::= UNION(U) NAME(T). {
562 type_ = init_decl_type(U->type, T->text);
563 type_->token = T;
564 free(U);
565 }
566 decl_type(type_) ::= ENUM(E) NAME(T). {
567 type_ = init_decl_type(E->type, T->text);
568 type_->token = T;
569 free(E);
570 }
571 decl_type(type_) ::= decl_type_token(T). {
572 type_ = init_decl_type(T->type, T->text);
573 type_->token = T;
574 }
575 const_decl_type(type) ::= decl_type(type_). {
576 type = type_;
577 }
578 const_decl_type(type) ::= CONST decl_type(type_). {
579 type = type_;
580 }
581 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
582 impl = init_impl(func, stmts);
583 }
584 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
585 func = init_impl_func(NAME->text, args, type, r);
586 func->token = NAME;
587 }
588 impl_def_val(def) ::= impl_def_val_token(T). {
589 def = init_impl_def_val(T->type, T->text);
590 free(T);
591 }
592 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
593 var = init_impl_var(T->text, r);
594 var->token = T;
595 }
596 impl_arg(arg) ::= impl_type(type) impl_var(var). {
597 arg = init_impl_arg(type, var, NULL);
598 }
599 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
600 arg = init_impl_arg(type, var, def);
601 }
602 impl_args(args) ::= LPAREN RPAREN. {
603 args = NULL;
604 }
605 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
606 args = args_;
607 }
608 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
609 args = args_;
610 args->vararg.name = va;
611 }
612 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR_NAME(T). {
613 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
614 free(T);
615 }
616 impl_arg_list(args) ::= impl_arg(arg). {
617 args = init_impl_args(arg);
618 }
619 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
620 args = add_impl_arg(args_, arg);
621 }
622 impl_stmts(stmts) ::= impl_stmt(stmt). {
623 stmts = init_impl_stmts(stmt);
624 }
625 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
626 stmts = add_impl_stmt(stmts_, stmt);
627 }
628 impl_stmt(stmt) ::= let_stmt(let). {
629 stmt = init_impl_stmt(PSI_T_LET, let);
630 }
631 impl_stmt(stmt) ::= set_stmt(set). {
632 stmt = init_impl_stmt(PSI_T_SET, set);
633 }
634 impl_stmt(stmt) ::= return_stmt(ret). {
635 stmt = init_impl_stmt(PSI_T_RETURN, ret);
636 }
637 impl_stmt(stmt) ::= free_stmt(free). {
638 stmt = init_impl_stmt(PSI_T_FREE, free);
639 }
640 num_exp(exp) ::= num_exp_token(tok). {
641 exp = init_num_exp(tok->type, tok->text);
642 exp->token = tok;
643 }
644 num_exp(exp) ::= decl_var(var). {
645 exp = init_num_exp(PSI_T_NAME, var);
646 exp->token = psi_token_copy(var->token);
647 }
648 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
649 exp_->operator = operator_->type;
650 exp_->operand = operand_;
651 exp = exp_;
652 free(operator_);
653 }
654 let_stmt(let) ::= LET decl_var(var) EOS. {
655 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
656 }
657 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
658 val->flags.one.is_reference = r ? 1 : 0;
659 let = init_let_stmt(var, val);
660 }
661 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
662 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
663 }
664 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
665 alloc = init_let_calloc(nmemb, size);
666 }
667 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
668 func = init_let_func(T->type, T->text, var);
669 free(T);
670 }
671 callback_arg_list ::= .
672 callback_arg_list(args) ::= callback_args(args_). {
673 args = args_;
674 }
675 callback_args(args) ::= set_value(val). {
676 args = init_set_values(val);
677 }
678 callback_args(args) ::= callback_args(args_) COMMA set_value(val). {
679 args = add_set_value(args_, val);
680 }
681 let_val(val) ::= NULL. {
682 val = init_let_val(PSI_LET_NULL, NULL);
683 }
684 let_val(val) ::= num_exp(exp). {
685 val = init_let_val(PSI_LET_NUMEXP, exp);
686 }
687 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
688 val = init_let_val(PSI_LET_CALLOC, alloc);
689 }
690 let_val(val) ::= let_func(func). {
691 val = init_let_val(PSI_LET_FUNC, func);
692 }
693 let_val(val) ::= CALLBACK let_func_token(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
694 val = init_let_val(PSI_LET_CALLBACK, init_let_callback(
695 init_let_func(F->type, F->text, var), args_));
696 free(F);
697 }
698 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
699 set = init_set_stmt(var, val);
700 }
701 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
702 val = init_set_value(func, init_decl_vars(var));
703 }
704 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
705 val = init_set_value(func, init_decl_vars(var));
706 val->num = num_;
707 }
708 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
709 free_set_func(func_);
710 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
711 val->func->token = T;
712 }
713 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
714 val = vals;
715 val->func = func_;
716 val->vars = init_decl_vars(var);
717 }
718 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
719 val = vals;
720 val->func = func_;
721 val->num = num_;
722 val->vars = init_decl_vars(var);
723 }
724 set_vals(vals) ::= set_value(val). {
725 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
726 }
727 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
728 vals = add_inner_set_value(vals_, val);
729 }
730 set_func(func) ::= set_func_token(T). {
731 func = init_set_func(T->type, T->text);
732 func->token = T;
733 }
734 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
735 ret = init_return_stmt(val);
736 ret->token = T;
737 }
738 free_stmt(free) ::= FREE free_calls(calls) EOS. {
739 free = init_free_stmt(calls);
740 }
741 free_calls(calls) ::= free_call(call). {
742 calls = init_free_calls(call);
743 }
744 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
745 calls = add_free_call(calls_, call);
746 }
747 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
748 call = init_free_call(F->text, vars);
749 call->token = F;
750 }
751 impl_type(type_) ::= impl_type_token(T). {
752 type_ = init_impl_type(T->type, T->text);
753 free(T);
754 }
755 reference(r) ::= . {
756 r = 0;
757 }
758 reference(r) ::= AMPERSAND. {
759 r = 1;
760 }
761 indirection(i) ::= .{
762 i = 0;
763 }
764 indirection(i) ::= pointers(p). {
765 i = p;
766 }
767 pointers(p) ::= ASTERISK. {
768 p = 1;
769 }
770 pointers(p) ::= pointers(p_) ASTERISK. {
771 p = p_+1;
772 }