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