b97d28f7b00e4dfc23cd29c4a80abed630a62dc0
[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
18
19
20 %name psi_parser_proc_
21 %token_prefix PSI_T_
22 %token_type {struct psi_token *}
23 %token_destructor {free($$);}
24 %default_destructor {(void)P;}
25 %extra_argument {struct psi_parser *P}
26
27
28 %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"); }}
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 %token_class const_type_token BOOL INT FLOAT STRING.
35 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
36 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
37 %token_class num_exp_token NUMBER NSNAME.
38 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
39 %token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
40 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.
41 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
42
43 %type decl_enum {decl_enum *}
44 %destructor decl_enum {free_decl_enum($$);}
45 %type decl_enum_items {decl_enum_items*}
46 %destructor decl_enum_items {free_decl_enum_items($$);}
47 %type decl_enum_item {decl_enum_item*}
48 %destructor decl_enum_item {free_decl_enum_item($$);}
49 %type decl_struct_args_block {decl_args*}
50 %destructor decl_struct_args_block {free_decl_args($$);}
51 %type decl_struct_args {decl_args*}
52 %destructor decl_struct_args {free_decl_args($$);}
53 %type decl_struct {decl_struct*}
54 %destructor decl_struct {free_decl_struct($$);}
55 %type align_and_size {decl_struct_layout}
56 %type decl_union {decl_union*}
57 %destructor decl_union {free_decl_union($$);}
58 %type const_type {const_type*}
59 %destructor const_type {free_const_type($$);}
60 %type constant {constant*}
61 %destructor constant {free_constant($$);}
62 %type decl_typedef {decl_arg*}
63 %destructor decl_typedef {free_decl_arg($$);}
64 %type decl_typedef_body_ex {decl_arg*}
65 %destructor decl_typedef_body_ex {free_decl_arg($$);}
66 %type decl_typedef_body {decl_arg*}
67 %destructor decl_typedef_body {free_decl_arg($$);}
68 %type decl_typedef_body_fn_args {decl_args *}
69 %destructor decl_typedef_body_fn_args {free_decl_args($$);}
70 %type decl {decl*}
71 %destructor decl {free_decl($$);}
72 %type decl_func {decl_arg*}
73 %destructor decl_func {free_decl_arg($$);}
74 %type decl_abi {decl_abi*}
75 %destructor decl_abi {free_decl_abi($$);}
76 %type decl_var {decl_var*}
77 %destructor decl_var {free_decl_var($$);}
78 %type decl_vars {decl_vars*}
79 %destructor decl_vars {free_decl_vars($$);}
80 %type decl_arg {decl_arg*}
81 %destructor decl_arg {free_decl_arg($$);}
82 %type decl_args {decl_args*}
83 %destructor decl_args {free_decl_args($$);}
84 %type struct_args {decl_args*}
85 %destructor struct_args {free_decl_args($$);}
86 %type struct_arg {decl_arg*}
87 %destructor struct_arg {free_decl_arg($$);}
88 %type struct_layout {decl_struct_layout*}
89 %destructor struct_layout {free_decl_struct_layout($$);}
90 %type decl_type {decl_type*}
91 %destructor decl_type {free_decl_type($$);}
92 %type const_decl_type {decl_type*}
93 %destructor const_decl_type {free_decl_type($$);}
94 %type impl {impl*}
95 %destructor impl {free_impl($$);}
96 %type impl_func {impl_func*}
97 %destructor impl_func {free_impl_func($$);}
98 %type impl_def_val {impl_def_val*}
99 %destructor impl_def_val {free_impl_def_val($$);}
100 %type impl_var {impl_var*}
101 %destructor impl_var {free_impl_var($$);}
102 %type impl_arg {impl_arg*}
103 %destructor impl_arg {free_impl_arg($$);}
104 %type impl_args {impl_args*}
105 %destructor impl_args {free_impl_args($$);}
106 %type impl_vararg {impl_arg*}
107 %destructor impl_vararg {free_impl_arg($$);}
108 %type impl_arg_list {impl_args*}
109 %destructor impl_arg_list {free_impl_args($$);}
110 %type impl_stmts {impl_stmts*}
111 %destructor impl_stmts {free_impl_stmts($$);}
112 %type impl_stmt {impl_stmt*}
113 %destructor impl_stmt {free_impl_stmt($$);}
114 %type num_exp {num_exp*}
115 %destructor num_exp {free_num_exp($$);}
116 %type let_stmt {let_stmt*}
117 %destructor let_stmt {free_let_stmt($$);}
118 %type let_calloc {let_calloc*}
119 %destructor let_calloc {free_let_calloc($$);}
120 %type let_func {let_func*}
121 %destructor let_func {free_let_func($$);}
122 %type callback_arg_list {set_values *}
123 %destructor callback_arg_list {free_set_values($$);}
124 %type callback_args {set_values *}
125 %destructor callback_args {free_set_values($$);}
126 %type let_val {let_val*}
127 %destructor let_val {free_let_val($$);}
128 %type let_vals {let_vals*}
129 %destructor let_vals {free_let_vals($$);}
130 %type set_stmt {set_stmt*}
131 %destructor set_stmt {free_set_stmt($$);}
132 %type set_value {set_value*}
133 %destructor set_value {free_set_value($$);}
134 %type set_vals {set_value*}
135 %destructor set_vals {free_set_value($$);}
136 %type set_func {set_func*}
137 %destructor set_func {free_set_func($$);}
138 %type return_stmt {return_stmt*}
139 %destructor return_stmt {free_return_stmt($$);}
140 %type free_stmt {free_stmt*}
141 %destructor free_stmt {free_free_stmt($$);}
142 %type free_calls {free_calls*}
143 %destructor free_calls {free_free_calls($$);}
144 %type free_call {free_call*}
145 %destructor free_call {free_free_call($$);}
146 %type impl_type {impl_type*}
147 %destructor impl_type {free_impl_type($$);}
148 %type reference {char}
149 %type indirection {unsigned}
150 %type pointers {unsigned}
151
152 file ::= blocks.
153 blocks ::= block.
154 blocks ::= blocks block.
155
156 block ::= EOF.
157 block ::= EOS.
158
159 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
160 if (P->psi.file.ln) {
161 P->error(P, token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
162 } else {
163 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
164 }
165 free(libname);
166 free(token);
167 }
168 block ::= decl(decl). {
169 P->decls = add_decl(P->decls, decl);
170 }
171
172 block ::= impl(impl). {
173 P->impls = add_impl(P->impls, impl);
174 }
175
176 block ::= decl_typedef(def). {
177 P->defs = add_decl_typedef(P->defs, def);
178 switch (def->type->type) {
179 case PSI_T_STRUCT:
180 if (def->type->real.strct) {
181 P->structs = add_decl_struct(P->structs, def->type->real.strct);
182 }
183 break;
184 case PSI_T_UNION:
185 if (def->type->real.unn) {
186 P->unions = add_decl_union(P->unions, def->type->real.unn);
187 }
188 break;
189 case PSI_T_ENUM:
190 if (def->type->real.enm) {
191 P->enums = add_decl_enum(P->enums, def->type->real.enm);
192 }
193 break;
194 }
195 }
196
197 block ::= constant(constant). {
198 P->consts = add_constant(P->consts, constant);
199 }
200
201 block ::= decl_struct(strct). {
202 P->structs = add_decl_struct(P->structs, strct);
203 }
204
205 block ::= decl_union(u). {
206 P->unions = add_decl_union(P->unions, u);
207 }
208
209 block ::= decl_enum(e). {
210 P->enums = add_decl_enum(P->enums, e);
211 }
212
213 optional_name(n) ::= . {
214 n = NULL;
215 }
216
217 optional_name(n) ::= NAME(N). {
218
219 n = N;
220 }
221
222 enum_name(n) ::= ENUM(E) optional_name(N). {
223
224 if (N) {
225 n = N;
226 free(E);
227 } else {
228 char digest[17];
229
230 psi_token_hash(E, digest);
231 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
232 }
233 }
234
235 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
236
237
238
239
240 e = init_decl_enum(N->text, list);
241 e->token = N;
242 }
243
244 decl_enum_items(l) ::= decl_enum_item(i). {
245
246 l = init_decl_enum_items(i);
247 }
248 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
249
250
251
252 l = add_decl_enum_item(l_, i);
253 }
254
255 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
256
257
258
259 i = init_decl_enum_item(N->text, num);
260 i->token = N;
261 }
262 decl_enum_item(i) ::= NAME(N). {
263
264 i = init_decl_enum_item(N->text, NULL);
265 i->token = N;
266 }
267
268 union_name(n) ::= UNION(U) optional_name(N). {
269
270
271 if (N) {
272 n = N;
273 free(U);
274 } else {
275 char digest[17];
276
277 psi_token_hash(U, digest);
278 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
279 }
280 }
281
282 struct_name(n) ::= STRUCT(S) optional_name(N). {
283
284
285 if (N) {
286 n = N;
287 free(S);
288 } else {
289 char digest[17];
290
291 psi_token_hash(S, digest);
292 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
293 }
294 }
295
296 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
297
298
299
300 args_ = args;
301 }
302
303 decl_struct_args(args_) ::= decl_struct_args_block(args). {
304
305 args_ = args;
306 }
307 decl_struct_args(args_) ::= EOS. {
308
309 args_ = init_decl_args(NULL);
310 }
311
312 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
313
314
315
316
317 strct = init_decl_struct(N->text, args);
318 strct->align = as.pos;
319 strct->size = as.len;
320 strct->token = N;
321 }
322
323 align_and_size(as) ::= . {
324 as.pos = 0;
325 as.len = 0;
326 }
327 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
328
329
330
331
332
333
334
335 as.pos = atol(A->text);
336 as.len = atol(S->text);
337 free(A);
338 free(S);
339 }
340
341 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
342
343
344
345
346 u = init_decl_union(N->text, args);
347 u->align = as.pos;
348 u->size = as.len;
349 u->token = N;
350 }
351
352 const_type(type_) ::= const_type_token(T). {
353
354 type_ = init_const_type(T->type, T->text);
355 free(T);
356 }
357
358 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
359
360
361
362
363
364
365 constant = init_constant(type, T->text, val);
366 free(T);
367 }
368
369 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
370
371
372
373 def = def_;
374 def->token = T;
375 }
376
377 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
378
379
380
381
382 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
383 def->type->token = psi_token_copy(N);
384 def->type->real.strct = init_decl_struct(N->text, args);
385 def->type->real.strct->token = N;
386 def->type->real.strct->align = as.pos;
387 def->type->real.strct->size = as.len;
388 }
389 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
390
391
392
393
394 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
395 def->type->token = psi_token_copy(N);
396 def->type->real.unn = init_decl_union(N->text, args);
397 def->type->real.unn->token = N;
398 def->type->real.unn->align = as.pos;
399 def->type->real.unn->size = as.len;
400 }
401 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
402
403
404 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
405 def->var->token = ALIAS;
406 def->type->token = psi_token_copy(e->token);
407 def->type->real.enm = e;
408 }
409
410 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
411
412 def = def_;
413 }
414
415 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
416
417
418
419 args = args_;
420 }
421 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
422
423
424 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
425 def->type->token = psi_token_copy(func_->token);
426 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
427 }
428 decl_typedef_body(def) ::= decl_arg(arg). {
429
430 def = arg;
431 }
432
433 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
434
435
436
437
438
439
440 decl = init_decl(abi, func, args);
441 }
442
443 decl_func(func) ::= decl_arg(arg). {
444
445 func = arg;
446 }
447
448 decl_func(func) ::= VOID(T) NAME(N). {
449
450
451 func = init_decl_arg(
452 init_decl_type(T->type, T->text),
453 init_decl_var(N->text, 0, 0)
454 );
455 func->type->token = T;
456 func->var->token = N;
457 func->token = N;
458 }
459 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
460
461
462
463
464
465
466
467 decl_arg *func_ = init_decl_arg(
468 init_decl_type(T->type, T->text),
469 init_decl_var(N->text, decl_i, 0)
470 );
471 func_->type->token = T;
472 func_->var->token = N;
473 func_->token = N;
474
475 def = init_decl_arg(
476 init_decl_type(PSI_T_FUNCTION, func_->var->name),
477 copy_decl_var(func_->var)
478 );
479 def->var->pointer_level = type_i;
480 def->type->token = psi_token_copy(func_->token);
481 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
482 }
483 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
484 decl_arg *func_ = init_decl_arg(
485 init_decl_type(T->type, T->text),
486 init_decl_var(N->text, decl_i, 0)
487 );
488 func_->type->token = T;
489 func_->var->token = N;
490 func_->token = N;
491
492 def = init_decl_arg(
493 init_decl_type(PSI_T_FUNCTION, func_->var->name),
494 copy_decl_var(func_->var)
495 );
496 def->var->pointer_level = type_i;
497 def->type->token = psi_token_copy(func_->token);
498 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
499 }
500
501 decl_abi(abi) ::= NAME(T). {
502
503 abi = init_decl_abi(T->text);
504 abi->token = T;
505 }
506
507 decl_var(var) ::= indirection(p) NAME(T). {
508
509
510 var = init_decl_var(T->text, p, 0);
511 var->token = T;
512 }
513 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
514
515
516
517
518
519 var = init_decl_var(T->text, p+1, atol(D->text));
520 var->token = T;
521 free(D);
522 }
523
524 decl_vars(vars) ::= decl_var(var). {
525
526 vars = init_decl_vars(var);
527 }
528 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
529
530
531
532 vars = add_decl_var(vars_, var);
533 }
534
535 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
536
537
538 arg_ = init_decl_arg(type, var);
539 }
540 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
541
542
543
544
545
546
547
548 decl_arg *func_ = init_decl_arg(
549 type_,
550 init_decl_var(N->text, decl_i, 0)
551 );
552 func_->var->token = N;
553 func_->token = N;
554
555 def = init_decl_arg(
556 init_decl_type(PSI_T_FUNCTION, func_->var->name),
557 copy_decl_var(func_->var)
558 );
559 def->var->pointer_level = type_i;
560 def->type->token = psi_token_copy(func_->token);
561 def->type->real.func = init_decl(init_decl_abi("default"), func_, args);
562 }
563
564
565 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
566
567
568
569 arg_ = init_decl_arg(
570 init_decl_type(T->type, T->text),
571 init_decl_var(N->text, p, 0)
572 );
573 arg_->type->token = T;
574 arg_->var->token = N;
575 arg_->token = N;
576 }
577 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
578
579
580
581
582 arg_ = init_decl_arg(
583 init_decl_type(T->type, T->text),
584 init_decl_var(N->text, p, 0)
585 );
586 arg_->type->token = T;
587 arg_->var->token = N;
588 arg_->token = N;
589 }
590
591 decl_args ::= .
592 decl_args ::= VOID.
593 decl_args(args) ::= decl_arg(arg). {
594
595 args = init_decl_args(arg);
596 }
597 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
598
599
600
601 args = add_decl_arg(args_, arg);
602 }
603 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
604
605
606
607 args = args_;
608 args->varargs = 1;
609 }
610
611 struct_args(args) ::= struct_arg(arg). {
612
613 args = init_decl_args(arg);
614 }
615 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
616
617
618 args = add_decl_arg(args_, arg);
619 }
620
621 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
622
623
624 arg_ = def;
625 switch (def->type->type) {
626 case PSI_T_STRUCT:
627 if (def->type->real.strct) {
628 P->structs = add_decl_struct(P->structs, def->type->real.strct);
629 }
630 break;
631 case PSI_T_UNION:
632 if (def->type->real.unn) {
633 P->unions = add_decl_union(P->unions, def->type->real.unn);
634 }
635 break;
636 case PSI_T_ENUM:
637 if (def->type->real.enm) {
638 P->enums = add_decl_enum(P->enums, def->type->real.enm);
639 }
640 break;
641 }
642 }
643 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
644
645
646
647 arg_->layout = layout_;
648 arg = arg_;
649 }
650
651 struct_layout(layout) ::= . {
652 layout = NULL;
653 }
654 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
655
656
657
658
659
660
661
662 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
663 free(POS);
664 free(SIZ);
665 }
666
667
668 decl_scalar_type(type_) ::= CHAR(C). {
669
670 type_ = C;
671 }
672 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
673
674
675 if (s) {
676 type_ = psi_token_cat(2, S, s);
677 free(S);
678 free(s);
679 } else {
680 type_ = S;
681 }
682 }
683 decl_scalar_type_short(s) ::= . {
684 s = NULL;
685 }
686
687 decl_scalar_type_short(s) ::= INT(I). {
688
689 s = I;
690 }
691 decl_scalar_type(type_) ::= INT(I). {
692
693 type_ = I;
694 }
695 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
696
697
698 if (l) {
699 type_ = psi_token_cat(2, L, l);
700 free(L);
701 free(l);
702 } else {
703 type_ = L;
704 }
705 }
706 decl_scalar_type_long(l) ::= . {
707 l = NULL;
708 }
709 decl_scalar_type_long(l) ::= DOUBLE(D). {
710
711 l = D;
712 }
713 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
714
715
716 if (ll) {
717 l = psi_token_cat(2, L, ll);
718 free(L);
719 free(ll);
720 } else {
721 l = L;
722 }
723 }
724 decl_scalar_type_long_long(ll) ::= . {
725 ll = NULL;
726 }
727 decl_scalar_type_long_long(ll) ::= INT(I). {
728
729 ll = I;
730 }
731 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
732
733
734 struct psi_token *T = psi_token_cat(2, U, N);
735 type_ = init_decl_type(T->type, T->text);
736 type_->token = T;
737 free(U);
738 free(N);
739 }
740 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
741
742
743 struct psi_token *T = psi_token_cat(2, S, N);
744 type_ = init_decl_type(T->type, T->text);
745 type_->token = T;
746 free(S);
747 free(N);
748 }
749 decl_type(type_) ::= UNSIGNED(U). {
750
751 type_ = init_decl_type(PSI_T_NAME, U->text);
752 type_->token = U;
753 }
754 decl_type(type_) ::= SIGNED(S). {
755
756 type_ = init_decl_type(PSI_T_NAME, S->text);
757 type_->token = S;
758 }
759 decl_type(type_) ::= decl_scalar_type(N). {
760
761 type_ = init_decl_type(N->type, N->text);
762 type_->token = N;
763 }
764
765 decl_type(type_) ::= STRUCT(S) NAME(T). {
766
767
768 type_ = init_decl_type(S->type, T->text);
769 type_->token = T;
770 free(S);
771 }
772 decl_type(type_) ::= UNION(U) NAME(T). {
773
774
775 type_ = init_decl_type(U->type, T->text);
776 type_->token = T;
777 free(U);
778 }
779 decl_type(type_) ::= ENUM(E) NAME(T). {
780
781
782 type_ = init_decl_type(E->type, T->text);
783 type_->token = T;
784 free(E);
785 }
786 decl_type(type_) ::= decl_type_token(T). {
787
788 type_ = init_decl_type(T->type, T->text);
789 type_->token = T;
790 }
791
792
793 const_decl_type(type) ::= decl_type(type_). {
794
795 type = type_;
796 }
797 const_decl_type(type) ::= CONST decl_type(type_). {
798
799
800 type = type_;
801 }
802
803 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
804
805
806
807
808 impl = init_impl(func, stmts);
809 }
810
811 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
812
813
814
815
816
817
818 func = init_impl_func(NAME->text, args, type, r);
819 func->token = NAME;
820 }
821
822 impl_def_val(def) ::= impl_def_val_token(T). {
823
824 def = init_impl_def_val(T->type, T->text);
825 free(T);
826 }
827
828 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
829
830
831 var = init_impl_var(T->text, r);
832 var->token = T;
833 }
834
835 impl_arg(arg) ::= impl_type(type) impl_var(var). {
836
837
838 arg = init_impl_arg(type, var, NULL);
839 }
840 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
841
842
843
844
845 arg = init_impl_arg(type, var, def);
846 }
847
848 impl_args(args) ::= LPAREN RPAREN. {
849
850
851 args = NULL;
852 }
853 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
854
855
856
857 args = args_;
858 }
859 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
860
861
862
863
864
865 args = args_;
866 args->vararg.name = va;
867 }
868
869 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR_NAME(T). {
870
871
872
873
874 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
875 free(T);
876 }
877
878 impl_arg_list(args) ::= impl_arg(arg). {
879
880 args = init_impl_args(arg);
881 }
882 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
883
884
885
886 args = add_impl_arg(args_, arg);
887 }
888
889 impl_stmts(stmts) ::= impl_stmt(stmt). {
890
891 stmts = init_impl_stmts(stmt);
892 }
893 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
894
895
896 stmts = add_impl_stmt(stmts_, stmt);
897 }
898
899 impl_stmt(stmt) ::= let_stmt(let). {
900
901 stmt = init_impl_stmt(PSI_T_LET, let);
902 }
903 impl_stmt(stmt) ::= set_stmt(set). {
904
905 stmt = init_impl_stmt(PSI_T_SET, set);
906 }
907 impl_stmt(stmt) ::= return_stmt(ret). {
908
909 stmt = init_impl_stmt(PSI_T_RETURN, ret);
910 }
911 impl_stmt(stmt) ::= free_stmt(free). {
912
913 stmt = init_impl_stmt(PSI_T_FREE, free);
914 }
915
916 num_exp(exp) ::= num_exp_token(tok). {
917
918 exp = init_num_exp(tok->type, tok->text);
919 exp->token = tok;
920 }
921 num_exp(exp) ::= decl_var(var). {
922
923 exp = init_num_exp(PSI_T_NAME, var);
924 exp->token = psi_token_copy(var->token);
925 }
926 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
927
928
929
930 exp_->operator = operator_->type;
931 exp_->operand = operand_;
932 exp = exp_;
933 free(operator_);
934 }
935
936 let_stmt(let) ::= LET decl_var(var) EOS. {
937
938
939
940 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
941 }
942 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
943
944
945
946
947
948
949 val->flags.one.is_reference = r ? 1 : 0;
950 let = init_let_stmt(var, val);
951 }
952 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
953
954
955
956
957
958 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
959 }
960
961 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
962
963
964
965 alloc = init_let_calloc(nmemb, size);
966 }
967
968 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
969
970
971
972
973 func = init_let_func(T->type, T->text, var);
974 free(T);
975 }
976
977 let_vals(vals) ::= let_val(val). {
978
979 vals = init_let_vals(val);
980 }
981 let_vals(vals) ::= let_vals(vals_) COMMA let_val(val). {
982
983
984
985 vals = add_let_val(vals_, val);
986 }
987
988 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) COMMA let_vals(vals) RPAREN. {
989
990
991
992
993
994
995 func = init_let_func(T->type, T->text, var);
996 func->inner = vals;
997 free(T);
998 }
999
1000 callback_arg_list ::= .
1001 callback_arg_list(args) ::= callback_args(args_). {
1002
1003 args = args_;
1004 }
1005
1006 callback_args(args) ::= set_value(val). {
1007
1008 args = init_set_values(val);
1009 }
1010 callback_args(args) ::= callback_args(args_) COMMA set_value(val). {
1011
1012
1013
1014 args = add_set_value(args_, val);
1015 }
1016 callback_rval(rval) ::= let_func_token(F). {
1017
1018 rval = F;
1019 }
1020 callback_rval(rval) ::= VOID(V). {
1021
1022 rval = V;
1023 }
1024
1025 let_val(val) ::= NULL. {
1026
1027 val = init_let_val(PSI_LET_NULL, NULL);
1028 }
1029 let_val(val) ::= num_exp(exp). {
1030
1031 val = init_let_val(PSI_LET_NUMEXP, exp);
1032 }
1033 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
1034
1035
1036
1037
1038 val = init_let_val(PSI_LET_CALLOC, alloc);
1039 }
1040 let_val(val) ::= let_func(func_). {
1041
1042 val = init_let_val(PSI_LET_FUNC, func_);
1043 }
1044 let_val(val) ::= CALLBACK callback_rval(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
1045 val = init_let_val(PSI_LET_CALLBACK, init_let_callback(
1046 init_let_func(F->type, F->text, var), args_));
1047 free(F);
1048 }
1049
1050 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
1051
1052
1053
1054
1055
1056 set = init_set_stmt(var, val);
1057 }
1058
1059 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
1060
1061
1062
1063
1064 val = init_set_value(func, init_decl_vars(var));
1065 }
1066 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
1067
1068
1069
1070
1071
1072
1073 val = init_set_value(func, init_decl_vars(var));
1074 val->num = num_;
1075 }
1076 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
1077
1078
1079
1080
1081
1082
1083 free_set_func(func_);
1084 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
1085 val->func->token = T;
1086 }
1087 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
1088
1089
1090
1091
1092
1093
1094 val = vals;
1095 val->func = func_;
1096 val->vars = init_decl_vars(var);
1097 }
1098 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
1099 val = vals;
1100 val->func = func_;
1101 val->num = num_;
1102 val->vars = init_decl_vars(var);
1103 }
1104
1105 set_vals(vals) ::= set_value(val). {
1106
1107 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
1108 }
1109 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
1110
1111
1112
1113 vals = add_inner_set_value(vals_, val);
1114 }
1115
1116 set_func(func) ::= set_func_token(T). {
1117
1118 func = init_set_func(T->type, T->text);
1119 func->token = T;
1120 }
1121
1122 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
1123
1124
1125
1126 ret = init_return_stmt(val);
1127 ret->token = T;
1128 }
1129
1130 free_stmt(free) ::= FREE free_calls(calls) EOS. {
1131
1132
1133
1134 free = init_free_stmt(calls);
1135 }
1136
1137 free_calls(calls) ::= free_call(call). {
1138
1139 calls = init_free_calls(call);
1140 }
1141 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
1142
1143
1144
1145 calls = add_free_call(calls_, call);
1146 }
1147
1148 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
1149
1150
1151
1152
1153 call = init_free_call(F->text, vars);
1154 call->token = F;
1155 }
1156
1157 impl_type(type_) ::= impl_type_token(T). {
1158
1159 type_ = init_impl_type(T->type, T->text);
1160 free(T);
1161 }
1162
1163 reference(r) ::= . {
1164 r = 0;
1165 }
1166 reference(r) ::= AMPERSAND. {
1167
1168 r = 1;
1169 }
1170
1171 indirection(i) ::= .{
1172 i = 0;
1173 }
1174 indirection(i) ::= pointers(p). {
1175
1176 i = p;
1177 }
1178
1179 pointers(p) ::= ASTERISK. {
1180
1181 p = 1;
1182 }
1183 pointers(p) ::= pointers(p_) ASTERISK. {
1184
1185
1186 p = p_+1;
1187 }