1 #define _CONCAT(x,y) x##y
2 #define CONCAT(x,y) _CONCAT(x,y)
3 #define COUNTED(x) CONCAT(parse_ ##x## _, __LINE__)
6 #define DEF(dn, dv) dn dv
7 #define PASS(nt, rule) nt ::= rule.
8 #define PARSE(nt, rule) nt ::= rule.
9 #define PARSE_NAMED(nt, nt_name, rule) NAMED(nt, nt_name) ::= rule.
10 #define PARSE_TYPED(nt, nt_name, rule) TYPED(nt, nt_name) ::= rule.
12 #define NAMED(t, name) t(name)
13 #define TYPED(t, name) t(name)
14 #define TOKEN_TYPE(token, type_) %type token {type_}
15 #define TOKEN_DTOR(token, dtor) %destructor token {dtor}
21 #define PASS(nt, rule) \
22 static void COUNTED(nt) (struct psi_parser *P) { \
25 #define PARSE(nt, rule) \
26 static void COUNTED(nt) (struct psi_parser *P rule)
27 #define PARSE_NAMED(nt, nt_name, rule) \
28 static void COUNTED(nt) (struct psi_parser *P NAMED(nt, nt_name) rule)
29 #define PARSE_TYPED(nt, nt_name, rule) \
30 static void COUNTED(nt) (struct psi_parser *P TYPED(nt, nt_name) rule)
32 #define NAMED(t, name) , struct psi_token *name
33 #define TYPED(t, name) , TOKEN_TYPE_NAME(t) name
34 #define TOKEN_TYPE_NAME(token) ##token##_parse_t
35 #define TOKEN_TYPE(token, type) typedef type TOKEN_TYPE_NAME(token);
36 #define TOKEN_DTOR(token, dtor)
39 DEF(%name
, psi_parser_proc_
)
40 DEF(%token_prefix
, PSI_T_
)
41 DEF(%token_type
, {struct psi_token
*})
42 DEF(%token_destructor
, {free($$
);})
43 DEF(%default_destructor
, {(void)P
;})
44 DEF(%extra_argument
, {struct psi_parser
*P
})
46 /* TOKEN is defined inside syntax_error */
49 if (TOKEN
&& TOKEN
->type
!= PSI_T_EOF
) {
50 psi_error(PSI_WARNING
, TOKEN
->file
, TOKEN
->line
, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN
->text
, TOKEN
->col
);
52 psi_error(PSI_WARNING
, P
->psi
.file
.fn
, P
->line
, "PSI syntax error: Unexpected end of input");
57 DEF(%left
, PLUS MINUS
.)
58 DEF(%left
, ASTERISK SLASH
.)
59 DEF(%nonassoc
, AMPERSAND
.)
60 DEF(%fallback
, NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT
.)
62 DEF(%token_class
, const_type_token BOOL INT FLOAT STRING
.)
63 DEF(%token_class
, decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME
.)
64 DEF(%token_class
, impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING
.)
65 DEF(%token_class
, num_exp_token NUMBER NSNAME
.)
66 DEF(%token_class
, num_exp_op_token PLUS MINUS ASTERISK SLASH
.)
67 DEF(%token_class
, let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT
.)
68 DEF(%token_class
, set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID
.)
69 DEF(%token_class
, impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE
.)
71 TOKEN_TYPE(decl_enum
, decl_enum
*)
72 TOKEN_DTOR(decl_enum
, free_decl_enum($$
);)
73 TOKEN_TYPE(decl_enum_items
, decl_enum_items
*)
74 TOKEN_DTOR(decl_enum_items
, free_decl_enum_items($$
);)
75 TOKEN_TYPE(decl_enum_item
, decl_enum_item
*)
76 TOKEN_DTOR(decl_enum_item
, free_decl_enum_item($$
);)
77 TOKEN_TYPE(decl_struct_args_block
, decl_args
*)
78 TOKEN_DTOR(decl_struct_args_block
, free_decl_args($$
);) /* there was a typo */
79 TOKEN_TYPE(decl_struct_args
, decl_args
*)
80 TOKEN_DTOR(decl_struct_args
, free_decl_args($$
);)
81 TOKEN_TYPE(decl_struct
, decl_struct
*)
82 TOKEN_DTOR(decl_struct
, free_decl_struct($$
);)
83 TOKEN_TYPE(align_and_size
, decl_struct_layout
)
84 TOKEN_TYPE(decl_union
, decl_union
*)
85 TOKEN_DTOR(decl_union
, free_decl_union($$
);)
86 TOKEN_TYPE(const_type
, const_type
*)
87 TOKEN_DTOR(const_type
, free_const_type($$
);)
88 TOKEN_TYPE(constant
, constant
*)
89 TOKEN_DTOR(constant
, free_constant($$
);)
90 TOKEN_TYPE(decl_typedef
, decl_arg
*)
91 TOKEN_DTOR(decl_typedef
, free_decl_arg($$
);)
92 TOKEN_TYPE(decl_typedef_body_ex
, decl_arg
*)
93 TOKEN_DTOR(decl_typedef_body_ex
, free_decl_arg($$
);)
94 TOKEN_TYPE(decl_typedef_body
, decl_arg
*)
95 TOKEN_DTOR(decl_typedef_body
, free_decl_arg($$
);)
96 TOKEN_TYPE(decl_typedef_body_fn_args
, decl_args
*)
97 TOKEN_DTOR(decl_typedef_body_fn_args
, free_decl_args($$
);)
98 TOKEN_TYPE(decl
, decl
*)
99 TOKEN_DTOR(decl
, free_decl($$
);)
100 TOKEN_TYPE(decl_func
, decl_arg
*)
101 TOKEN_DTOR(decl_func
, free_decl_arg($$
);)
102 TOKEN_TYPE(decl_abi
, decl_abi
*)
103 TOKEN_DTOR(decl_abi
, free_decl_abi($$
);)
104 TOKEN_TYPE(decl_var
, decl_var
*)
105 TOKEN_DTOR(decl_var
, free_decl_var($$
);)
106 TOKEN_TYPE(decl_vars
, decl_vars
*)
107 TOKEN_DTOR(decl_vars
, free_decl_vars($$
);)
108 TOKEN_TYPE(decl_arg
, decl_arg
*)
109 TOKEN_DTOR(decl_arg
, free_decl_arg($$
);)
110 TOKEN_TYPE(decl_args
, decl_args
*)
111 TOKEN_DTOR(decl_args
, free_decl_args($$
);)
112 TOKEN_TYPE(struct_args
, decl_args
*)
113 TOKEN_DTOR(struct_args
, free_decl_args($$
);)
114 TOKEN_TYPE(struct_arg
, decl_arg
*)
115 TOKEN_DTOR(struct_arg
, free_decl_arg($$
);)
116 TOKEN_TYPE(struct_layout
, decl_struct_layout
*)
117 TOKEN_DTOR(struct_layout
, free_decl_struct_layout($$
);)
118 TOKEN_TYPE(decl_type
, decl_type
*)
119 TOKEN_DTOR(decl_type
, free_decl_type($$
);)
120 TOKEN_TYPE(const_decl_type
, decl_type
*)
121 TOKEN_DTOR(const_decl_type
, free_decl_type($$
);)
122 TOKEN_TYPE(impl
, impl
*)
123 TOKEN_DTOR(impl
, free_impl($$
);)
124 TOKEN_TYPE(impl_func
, impl_func
*)
125 TOKEN_DTOR(impl_func
, free_impl_func($$
);)
126 TOKEN_TYPE(impl_def_val
, impl_def_val
*)
127 TOKEN_DTOR(impl_def_val
, free_impl_def_val($$
);)
128 TOKEN_TYPE(impl_var
, impl_var
*)
129 TOKEN_DTOR(impl_var
, free_impl_var($$
);)
130 TOKEN_TYPE(impl_arg
, impl_arg
*)
131 TOKEN_DTOR(impl_arg
, free_impl_arg($$
);)
132 TOKEN_TYPE(impl_args
, impl_args
*)
133 TOKEN_DTOR(impl_args
, free_impl_args($$
);)
134 TOKEN_TYPE(impl_vararg
, impl_arg
*)
135 TOKEN_DTOR(impl_vararg
, free_impl_arg($$
);)
136 TOKEN_TYPE(impl_arg_list
, impl_args
*)
137 TOKEN_DTOR(impl_arg_list
, free_impl_args($$
);)
138 TOKEN_TYPE(impl_stmts
, impl_stmts
*)
139 TOKEN_DTOR(impl_stmts
, free_impl_stmts($$
);)
140 TOKEN_TYPE(impl_stmt
, impl_stmt
*)
141 TOKEN_DTOR(impl_stmt
, free_impl_stmt($$
);)
142 TOKEN_TYPE(num_exp
, num_exp
*)
143 TOKEN_DTOR(num_exp
, free_num_exp($$
);)
144 TOKEN_TYPE(let_stmt
, let_stmt
*)
145 TOKEN_DTOR(let_stmt
, free_let_stmt($$
);)
146 TOKEN_TYPE(let_calloc
, let_calloc
*)
147 TOKEN_DTOR(let_calloc
, free_let_calloc($$
);)
148 TOKEN_TYPE(let_func
, let_func
*)
149 TOKEN_DTOR(let_func
, free_let_func($$
);)
150 TOKEN_TYPE(callback_arg_list
, set_values
*)
151 TOKEN_DTOR(callback_arg_list
, free_set_values($$
);)
152 TOKEN_TYPE(callback_args
, set_values
*)
153 TOKEN_DTOR(callback_args
, free_set_values($$
);)
154 TOKEN_TYPE(let_val
, let_val
*)
155 TOKEN_DTOR(let_val
, free_let_val($$
);)
156 TOKEN_TYPE(let_vals
, let_vals
*)
157 TOKEN_DTOR(let_vals
, free_let_vals($$
);)
158 TOKEN_TYPE(set_stmt
, set_stmt
*)
159 TOKEN_DTOR(set_stmt
, free_set_stmt($$
);)
160 TOKEN_TYPE(set_value
, set_value
*)
161 TOKEN_DTOR(set_value
, free_set_value($$
);)
162 TOKEN_TYPE(set_vals
, set_value
*)
163 TOKEN_DTOR(set_vals
, free_set_value($$
);)
164 TOKEN_TYPE(set_func
, set_func
*)
165 TOKEN_DTOR(set_func
, free_set_func($$
);)
166 TOKEN_TYPE(return_stmt
, return_stmt
*)
167 TOKEN_DTOR(return_stmt
, free_return_stmt($$
);)
168 TOKEN_TYPE(free_stmt
, free_stmt
*)
169 TOKEN_DTOR(free_stmt
, free_free_stmt($$
);)
170 TOKEN_TYPE(free_calls
, free_calls
*)
171 TOKEN_DTOR(free_calls
, free_free_calls($$
);)
172 TOKEN_TYPE(free_call
, free_call
*)
173 TOKEN_DTOR(free_call
, free_free_call($$
);)
174 TOKEN_TYPE(impl_type
, impl_type
*)
175 TOKEN_DTOR(impl_type
, free_impl_type($$
);)
176 TOKEN_TYPE(reference
, char)
177 TOKEN_TYPE(indirection
, unsigned)
178 TOKEN_TYPE(pointers
, unsigned)
182 PASS(blocks
, blocks block
)
187 PARSE(block
, NAMED(LIB
, token
) NAMED(QUOTED_STRING
, libname
) TOKEN(EOS
)) {
188 if (P
->psi
.file
.ln
) {
189 P
->error(P
, token
, PSI_WARNING
, "Extra 'lib %s' statement has no effect", libname
->text
);
191 P
->psi
.file
.ln
= strndup(libname
->text
+ 1, libname
->size
- 2);
196 PARSE(block
, TYPED(decl
, decl
)) {
197 P
->decls
= add_decl(P
->decls
, decl
);
200 PARSE(block
, TYPED(impl
, impl
)) {
201 P
->impls
= add_impl(P
->impls
, impl
);
204 PARSE(block
, TYPED(decl_typedef
, def
)) {
205 P
->defs
= add_decl_typedef(P
->defs
, def
);
206 switch (def
->type
->type
) {
208 if (def
->type
->real
.strct
) {
209 P
->structs
= add_decl_struct(P
->structs
, def
->type
->real
.strct
);
213 if (def
->type
->real
.unn
) {
214 P
->unions
= add_decl_union(P
->unions
, def
->type
->real
.unn
);
218 if (def
->type
->real
.enm
) {
219 P
->enums
= add_decl_enum(P
->enums
, def
->type
->real
.enm
);
225 PARSE(block
, TYPED(constant
, constant
)) {
226 P
->consts
= add_constant(P
->consts
, constant
);
229 PARSE(block
, TYPED(decl_struct
, strct
)) {
230 P
->structs
= add_decl_struct(P
->structs
, strct
);
233 PARSE(block
, TYPED(decl_union
, u
)) {
234 P
->unions
= add_decl_union(P
->unions
, u
);
237 PARSE(block
, TYPED(decl_enum
, e
)) {
238 P
->enums
= add_decl_enum(P
->enums
, e
);
241 PARSE_NAMED(optional_name
, n
, ) {
245 PARSE_NAMED(optional_name
, n
,
250 PARSE_NAMED(enum_name
, n
,
251 NAMED(ENUM
, E
) NAMED(optional_name
, N
)) {
258 psi_token_hash(E
, digest
);
259 n
= psi_token_translit(psi_token_append(E
, 1, digest
), " ", "@");
263 PARSE_TYPED(decl_enum
, e
,
266 TYPED(decl_enum_items
, list
)
268 e
= init_decl_enum(N
->text
, list
);
272 PARSE_TYPED(decl_enum_items
, l
,
273 TYPED(decl_enum_item
, i
)) {
274 l
= init_decl_enum_items(i
);
276 PARSE_TYPED(decl_enum_items
, l
,
277 TYPED(decl_enum_items
, l_
)
279 TYPED(decl_enum_item
, i
)) {
280 l
= add_decl_enum_item(l_
, i
);
283 PARSE_TYPED(decl_enum_item
, i
,
286 TYPED(num_exp
, num
)) {
287 i
= init_decl_enum_item(N
->text
, num
);
290 PARSE_TYPED(decl_enum_item
, i
,
292 i
= init_decl_enum_item(N
->text
, NULL
);
296 PARSE_NAMED(union_name
, n
,
298 NAMED(optional_name
, N
)) {
305 psi_token_hash(U
, digest
);
306 n
= psi_token_translit(psi_token_append(U
, 1, digest
), " ", "@");
310 PARSE_NAMED(struct_name
, n
,
312 NAMED(optional_name
, N
)) {
319 psi_token_hash(S
, digest
);
320 n
= psi_token_translit(psi_token_append(S
, 1, digest
), " ", "@");
324 PARSE_TYPED(decl_struct_args_block
, args_
,
326 TYPED(struct_args
, args
)
331 PARSE_TYPED(decl_struct_args
, args_
,
332 TYPED(decl_struct_args_block
, args
)) {
335 PARSE_TYPED(decl_struct_args
, args_
,
337 args_
= init_decl_args(NULL
);
340 PARSE_TYPED(decl_struct
, strct
,
343 TYPED(align_and_size
, as
)
344 TYPED(decl_struct_args
, args
)) {
345 strct
= init_decl_struct(N
->text
, args
);
346 strct
->align
= as
.pos
;
347 strct
->size
= as
.len
;
351 PARSE_TYPED(align_and_size
, as
, ) {
355 PARSE_TYPED(align_and_size
, as
,
363 as
.pos
= atol(A
->text
);
364 as
.len
= atol(S
->text
);
369 PARSE_TYPED(decl_union
, u
,
372 TYPED(align_and_size
, as
)
373 TYPED(decl_struct_args
, args
)) {
374 u
= init_decl_union(N
->text
, args
);
380 PARSE_TYPED(const_type
, type_
,
381 NAMED(const_type_token
, T
)) {
382 type_
= init_const_type(T
->type
, T
->text
);
386 PARSE_TYPED(constant
, constant
,
388 TYPED(const_type
, type
)
391 TYPED(impl_def_val
, val
)
393 constant
= init_constant(type
, T
->text
, val
);
397 PARSE_TYPED(decl_typedef
, def
,
399 TYPED(decl_typedef_body
, def_
)
405 PARSE_TYPED(decl_typedef_body_ex
, def
,
406 NAMED(struct_name
, N
)
407 TYPED(align_and_size
, as
)
408 TYPED(decl_struct_args_block
, args
)
409 TYPED(decl_var
, var
)) {
410 def
= init_decl_arg(init_decl_type(PSI_T_STRUCT
, N
->text
), var
);
411 def
->type
->token
= psi_token_copy(N
);
412 def
->type
->real
.strct
= init_decl_struct(N
->text
, args
);
413 def
->type
->real
.strct
->token
= N
;
414 def
->type
->real
.strct
->align
= as
.pos
;
415 def
->type
->real
.strct
->size
= as
.len
;
417 PARSE_TYPED(decl_typedef_body_ex
, def
,
419 TYPED(align_and_size
, as
)
420 TYPED(decl_struct_args_block
, args
)
421 TYPED(decl_var
, var
)) {
422 def
= init_decl_arg(init_decl_type(PSI_T_UNION
, N
->text
), var
);
423 def
->type
->token
= psi_token_copy(N
);
424 def
->type
->real
.unn
= init_decl_union(N
->text
, args
);
425 def
->type
->real
.unn
->token
= N
;
426 def
->type
->real
.unn
->align
= as
.pos
;
427 def
->type
->real
.unn
->size
= as
.len
;
429 PARSE_TYPED(decl_typedef_body_ex
, def
,
431 NAMED(NAME
, ALIAS
)) {
432 def
= init_decl_arg(init_decl_type(PSI_T_ENUM
, e
->name
), init_decl_var(ALIAS
->text
, 0, 0));
433 def
->var
->token
= ALIAS
;
434 def
->type
->token
= psi_token_copy(e
->token
);
435 def
->type
->real
.enm
= e
;
438 PARSE_TYPED(decl_typedef_body
, def
,
439 TYPED(decl_typedef_body_ex
, def_
)) {
443 PARSE_TYPED(decl_typedef_body_fn_args
, args
,
445 TYPED(decl_args
, args_
)
449 PARSE_TYPED(decl_typedef_body
, def
,
450 TYPED(decl_func
, func_
)
451 TYPED(decl_typedef_body_fn_args
, args
)) {
452 def
= init_decl_arg(init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
), copy_decl_var(func_
->var
));
453 def
->type
->token
= psi_token_copy(func_
->token
);
454 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
456 PARSE_TYPED(decl_typedef_body
, def
,
457 TYPED(decl_arg
, arg
)) {
461 PARSE_TYPED(decl
, decl
,
463 TYPED(decl_func
, func
)
465 TYPED(decl_args
, args
)
468 decl
= init_decl(abi
, func
, args
);
471 PARSE_TYPED(decl_func
, func
,
472 TYPED(decl_arg
, arg
)) {
475 /* special case for void functions */
476 PARSE_TYPED(decl_func
, func
,
479 func
= init_decl_arg(
480 init_decl_type(T
->type
, T
->text
),
481 init_decl_var(N
->text
, 0, 0)
483 func
->type
->token
= T
;
484 func
->var
->token
= N
;
487 PARSE_TYPED(decl_typedef_body
, def
,
489 TYPED(indirection
, decl_i
)
491 TYPED(indirection
, type_i
)
494 TYPED(decl_typedef_body_fn_args
, args
)) {
495 decl_arg
*func_
= init_decl_arg(
496 init_decl_type(T
->type
, T
->text
),
497 init_decl_var(N
->text
, decl_i
, 0)
499 func_
->type
->token
= T
;
500 func_
->var
->token
= N
;
504 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
505 copy_decl_var(func_
->var
)
507 def
->var
->pointer_level
= type_i
;
508 def
->type
->token
= psi_token_copy(func_
->token
);
509 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
511 PARSE_TYPED(decl_typedef_body
, def
,
514 TYPED(pointers
, decl_i
)
516 TYPED(indirection
, type_i
)
519 TYPED(decl_typedef_body_fn_args
, args
)) {
520 decl_arg
*func_
= init_decl_arg(
521 init_decl_type(T
->type
, T
->text
),
522 init_decl_var(N
->text
, decl_i
, 0)
524 func_
->type
->token
= T
;
525 func_
->var
->token
= N
;
529 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
530 copy_decl_var(func_
->var
)
532 def
->var
->pointer_level
= type_i
;
533 def
->type
->token
= psi_token_copy(func_
->token
);
534 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
537 PARSE_TYPED(decl_abi
, abi
,
539 abi
= init_decl_abi(T
->text
);
543 PARSE_TYPED(decl_var
, var
,
545 NAMED(decl_var_array_size
, as
)) {
546 var
= init_decl_var(T
->text
, 0, as
?atol(as
->text
):0);
552 PARSE_TYPED(decl_var
, var
,
555 NAMED(decl_var_array_size
, as
)) {
556 var
= init_decl_var(T
->text
, p
+!!as
, as
?atol(as
->text
):0);
562 PARSE_NAMED(decl_var_array_size
, as
, ) {
565 PARSE_NAMED(decl_var_array_size
, as
,
572 PARSE_TYPED(decl_vars
, vars
,
573 TYPED(decl_var
, var
)) {
574 vars
= init_decl_vars(var
);
576 PARSE_TYPED(decl_vars
, vars
,
577 TYPED(decl_vars
, vars_
)
579 TYPED(decl_var
, var
)) {
580 vars
= add_decl_var(vars_
, var
);
583 PARSE_TYPED(decl_arg
, arg_
,
584 TYPED(const_decl_type
, type
)
585 TYPED(decl_var
, var
)) {
586 arg_
= init_decl_arg(type
, var
);
588 PARSE_TYPED(decl_typedef_body
, def
,
589 TYPED(const_decl_type
, type_
)
590 TYPED(indirection
, decl_i
)
592 TYPED(indirection
, type_i
)
595 TYPED(decl_typedef_body_fn_args
, args
)) {
596 decl_arg
*func_
= init_decl_arg(
598 init_decl_var(N
->text
, decl_i
, 0)
600 func_
->var
->token
= N
;
604 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
605 copy_decl_var(func_
->var
)
607 def
->var
->pointer_level
= type_i
;
608 def
->type
->token
= psi_token_copy(func_
->token
);
609 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
612 /* void pointers need a specific rule */
613 PARSE_TYPED(decl_arg
, arg_
,
617 arg_
= init_decl_arg(
618 init_decl_type(T
->type
, T
->text
),
619 init_decl_var(N
->text
, p
, 0)
621 arg_
->type
->token
= T
;
622 arg_
->var
->token
= N
;
625 PARSE_TYPED(decl_arg
, arg_
,
630 arg_
= init_decl_arg(
631 init_decl_type(T
->type
, T
->text
),
632 init_decl_var(N
->text
, p
, 0)
634 arg_
->type
->token
= T
;
635 arg_
->var
->token
= N
;
640 PASS(decl_args
, VOID
)
641 PARSE_TYPED(decl_args
, args
,
642 TYPED(decl_arg
, arg
)) {
643 args
= init_decl_args(arg
);
645 PARSE_TYPED(decl_args
, args
,
646 TYPED(decl_args
, args_
)
648 TYPED(decl_arg
, arg
)) {
649 args
= add_decl_arg(args_
, arg
);
651 PARSE_TYPED(decl_args
, args
,
652 TYPED(decl_args
, args_
)
659 PARSE_TYPED(struct_args
, args
,
660 TYPED(struct_arg
, arg
)) {
661 args
= init_decl_args(arg
);
663 PARSE_TYPED(struct_args
, args
,
664 TYPED(struct_args
, args_
)
665 TYPED(struct_arg
, arg
)) {
666 args
= add_decl_arg(args_
, arg
);
669 PARSE_TYPED(struct_arg
, arg_
,
670 TYPED(decl_typedef_body_ex
, def
)
673 switch (def
->type
->type
) {
675 if (def
->type
->real
.strct
) {
676 P
->structs
= add_decl_struct(P
->structs
, def
->type
->real
.strct
);
680 if (def
->type
->real
.unn
) {
681 P
->unions
= add_decl_union(P
->unions
, def
->type
->real
.unn
);
685 if (def
->type
->real
.enm
) {
686 P
->enums
= add_decl_enum(P
->enums
, def
->type
->real
.enm
);
691 PARSE_TYPED(struct_arg
, arg
,
692 TYPED(decl_arg
, arg_
)
693 TYPED(struct_layout
, layout_
)
695 arg_
->layout
= layout_
;
699 PARSE_TYPED(struct_layout
, layout
, ) {
702 PARSE_TYPED(struct_layout
, layout
,
710 layout
= init_decl_struct_layout(atol(POS
->text
), atol(SIZ
->text
));
715 /* un/signed, urgh */
716 PARSE_NAMED(decl_scalar_type
, type_
,
720 PARSE_NAMED(decl_scalar_type
, type_
,
722 NAMED(decl_scalar_type_short
, s
)) {
724 type_
= psi_token_cat(2, S
, s
);
731 PARSE_NAMED(decl_scalar_type_short
, s
, ) {
735 PARSE_NAMED(decl_scalar_type_short
, s
,
739 PARSE_NAMED(decl_scalar_type
, type_
,
743 PARSE_NAMED(decl_scalar_type
, type_
,
745 NAMED(decl_scalar_type_long
, l
)) {
747 type_
= psi_token_cat(2, L
, l
);
754 PARSE_NAMED(decl_scalar_type_long
, l
, ) {
757 PARSE_NAMED(decl_scalar_type_long
, l
,
761 PARSE_NAMED(decl_scalar_type_long
, l
,
763 NAMED(decl_scalar_type_long_long
, ll
)) {
765 l
= psi_token_cat(2, L
, ll
);
772 PARSE_NAMED(decl_scalar_type_long_long
, ll
, ) {
775 PARSE_NAMED(decl_scalar_type_long_long
, ll
,
779 PARSE_TYPED(decl_type
, type_
,
781 NAMED(decl_scalar_type
, N
)) {
782 struct psi_token
*T
= psi_token_cat(2, U
, N
);
783 type_
= init_decl_type(T
->type
, T
->text
);
788 PARSE_TYPED(decl_type
, type_
,
790 NAMED(decl_scalar_type
, N
)) {
791 struct psi_token
*T
= psi_token_cat(2, S
, N
);
792 type_
= init_decl_type(T
->type
, T
->text
);
797 PARSE_TYPED(decl_type
, type_
,
798 NAMED(UNSIGNED
, U
)) {
799 type_
= init_decl_type(PSI_T_NAME
, U
->text
);
802 PARSE_TYPED(decl_type
, type_
,
804 type_
= init_decl_type(PSI_T_NAME
, S
->text
);
807 PARSE_TYPED(decl_type
, type_
,
808 NAMED(decl_scalar_type
, N
)) {
809 type_
= init_decl_type(N
->type
, N
->text
);
813 PARSE_TYPED(decl_type
, type_
,
816 type_
= init_decl_type(S
->type
, T
->text
);
820 PARSE_TYPED(decl_type
, type_
,
823 type_
= init_decl_type(U
->type
, T
->text
);
827 PARSE_TYPED(decl_type
, type_
,
830 type_
= init_decl_type(E
->type
, T
->text
);
834 PARSE_TYPED(decl_type
, type_
,
835 NAMED(decl_type_token
, T
)) {
836 type_
= init_decl_type(T
->type
, T
->text
);
841 PARSE_TYPED(const_decl_type
, type
,
842 TYPED(decl_type
, type_
)) {
845 PARSE_TYPED(const_decl_type
, type
,
847 TYPED(decl_type
, type_
)) {
851 PARSE_TYPED(impl
, impl
,
852 TYPED(impl_func
, func
)
854 TYPED(impl_stmts
, stmts
)
856 impl
= init_impl(func
, stmts
);
859 PARSE_TYPED(impl_func
, func
,
863 TYPED(impl_args
, args
)
865 TYPED(impl_type
, type
)) {
866 func
= init_impl_func(NAME
->text
, args
, type
, r
);
870 PARSE_TYPED(impl_def_val
, def
,
871 NAMED(impl_def_val_token
, T
)) {
872 def
= init_impl_def_val(T
->type
, T
->text
);
876 PARSE_TYPED(impl_var
, var
,
878 NAMED(DOLLAR_NAME
, T
)) {
879 var
= init_impl_var(T
->text
, r
);
883 PARSE_TYPED(impl_arg
, arg
,
884 TYPED(impl_type
, type
)
885 TYPED(impl_var
, var
)) {
886 arg
= init_impl_arg(type
, var
, NULL
);
888 PARSE_TYPED(impl_arg
, arg
,
889 TYPED(impl_type
, type
)
892 TYPED(impl_def_val
, def
)) {
893 arg
= init_impl_arg(type
, var
, def
);
896 PARSE_TYPED(impl_args
, args
,
901 PARSE_TYPED(impl_args
, args
,
903 TYPED(impl_arg_list
, args_
)
907 PARSE_TYPED(impl_args
, args
,
909 TYPED(impl_arg_list
, args_
)
911 TYPED(impl_vararg
, va
)
914 args
->vararg
.name
= va
;
917 PARSE_TYPED(impl_vararg
, va
,
918 TYPED(impl_type
, type
)
921 NAMED(DOLLAR_NAME
, T
)) {
922 va
= init_impl_arg(type
, init_impl_var(T
->text
, r
), NULL
);
926 PARSE_TYPED(impl_arg_list
, args
,
927 TYPED(impl_arg
, arg
)) {
928 args
= init_impl_args(arg
);
930 PARSE_TYPED(impl_arg_list
, args
,
931 TYPED(impl_arg_list
, args_
)
933 TYPED(impl_arg
, arg
)) {
934 args
= add_impl_arg(args_
, arg
);
937 PARSE_TYPED(impl_stmts
, stmts
,
938 TYPED(impl_stmt
, stmt
)) {
939 stmts
= init_impl_stmts(stmt
);
941 PARSE_TYPED(impl_stmts
, stmts
,
942 TYPED(impl_stmts
, stmts_
)
943 TYPED(impl_stmt
, stmt
)) {
944 stmts
= add_impl_stmt(stmts_
, stmt
);
947 PARSE_TYPED(impl_stmt
, stmt
,
948 TYPED(let_stmt
, let
)) {
949 stmt
= init_impl_stmt(PSI_T_LET
, let
);
951 PARSE_TYPED(impl_stmt
, stmt
,
952 TYPED(set_stmt
, set
)) {
953 stmt
= init_impl_stmt(PSI_T_SET
, set
);
955 PARSE_TYPED(impl_stmt
, stmt
,
956 TYPED(return_stmt
, ret
)) {
957 stmt
= init_impl_stmt(PSI_T_RETURN
, ret
);
959 PARSE_TYPED(impl_stmt
, stmt
,
960 TYPED(free_stmt
, free
)) {
961 stmt
= init_impl_stmt(PSI_T_FREE
, free
);
964 PARSE_TYPED(num_exp
, exp
,
965 NAMED(num_exp_token
, tok
)) {
966 exp
= init_num_exp(tok
->type
, tok
->text
);
969 PARSE_TYPED(num_exp
, exp
,
970 TYPED(decl_var
, var
)) {
971 exp
= init_num_exp(PSI_T_NAME
, var
);
972 exp
->token
= psi_token_copy(var
->token
);
974 PARSE_TYPED(num_exp
, exp
,
976 NAMED(num_exp_op_token
, operator_
)
977 TYPED(num_exp
, operand_
)) {
978 exp_
->operator = operator_
->type
;
979 exp_
->operand
= operand_
;
984 TOKEN_TYPE(let_exp
, let_val
*)
985 TOKEN_DTOR(let_exp
, free_let_val($$
);)
987 TOKEN_TYPE(let_callback
, let_callback
*)
988 TOKEN_DTOR(let_callback
, free_let_callback($$
);)
993 PARSE_TYPED(let_val
, val
,
995 val
= init_let_val(PSI_LET_NULL
, NULL
);
1000 PARSE_TYPED(let_val
, val
,
1003 val
= init_let_val(PSI_LET_NULL
, NULL
);
1004 val
->is_reference
= 1;
1009 PARSE_TYPED(let_val
, val
,
1010 TYPED(let_callback
, cb
)) {
1011 val
= init_let_val(PSI_LET_CALLBACK
, cb
);
1016 PARSE_TYPED(let_val
, val
,
1017 TYPED(let_calloc
, ca
)) {
1018 val
= init_let_val(PSI_LET_CALLOC
, ca
);
1023 PARSE_TYPED(let_val
, val
,
1025 TYPED(let_calloc
, ca
)) {
1026 val
= init_let_val(PSI_LET_CALLOC
, ca
);
1027 val
->is_reference
= 1;
1032 PARSE_TYPED(let_val
, val
,
1033 TYPED(let_func
, fn
)) {
1034 val
= init_let_val_ex(NULL
, PSI_LET_FUNC
, fn
);
1039 PARSE_TYPED(let_val
, val
,
1041 TYPED(let_func
, fn
)) {
1042 val
= init_let_val_ex(NULL
, PSI_LET_FUNC
, fn
);
1043 val
->is_reference
= 1;
1048 PARSE_TYPED(let_val
, val
,
1049 TYPED(num_exp
, exp
)) {
1050 val
= init_let_val_ex(NULL
, PSI_LET_NUMEXP
, exp
);
1055 PARSE_TYPED(let_val
, val
,
1057 TYPED(num_exp
, exp
)) {
1058 val
= init_let_val_ex(NULL
, PSI_LET_NUMEXP
, exp
);
1059 val
->is_reference
= 1;
1063 * let_exp: var = val
1065 PARSE_TYPED(let_exp
, exp
,
1066 TYPED(decl_var
, var_
)
1068 TYPED(let_val
, val
)) {
1074 * let_stmt: LET exp ;
1076 PARSE_TYPED(let_stmt
, let
,
1080 let
= init_let_stmt(val
);
1085 * let_stmt: TEMP foo = bar ;
1087 PARSE_TYPED(let_stmt
, let
,
1089 TYPED(decl_var
, var
)
1092 TYPED(decl_var
, val_
)
1094 let
= init_let_stmt(init_let_val_ex(var
, PSI_LET_TMP
, val_
));
1096 let
->val
->is_reference
= r
? 1 : 0;
1100 * let_callback: CALLBACK cast($var($args))
1102 PARSE_TYPED(let_callback
, cb
,
1104 NAMED(callback_rval
, F
)
1106 TYPED(impl_var
, var
)
1108 TYPED(callback_arg_list
, args_
)
1111 cb
= init_let_callback(init_let_func(F
->type
, F
->text
, var
), args_
);
1116 * let_calloc: CALLOC ( num_exp nmemb , num_exp size )
1118 PARSE_TYPED(let_calloc
, alloc
,
1121 TYPED(num_exp
, nmemb
)
1123 TYPED(num_exp
, size
)
1125 alloc
= init_let_calloc(nmemb
, size
);
1129 * let_func: FUNC ( $var )
1131 PARSE_TYPED(let_func
, func
,
1132 NAMED(let_func_token
, T
)
1134 TYPED(impl_var
, var
)
1136 func
= init_let_func(T
->type
, T
->text
, var
);
1140 * let_func: FUNC ( $var (,exps)? )
1142 PARSE_TYPED(let_func
, func
,
1143 NAMED(let_func_token
, T
)
1145 TYPED(impl_var
, var
)
1147 TYPED(let_vals
, vals
)
1149 func
= init_let_func(T
->type
, T
->text
, var
);
1154 * let_vals: exps = exp
1156 PARSE_TYPED(let_vals
, vals
,
1157 TYPED(let_exp
, val
)) {
1158 vals
= init_let_vals(val
);
1160 PARSE_TYPED(let_vals
, vals
,
1161 TYPED(let_val
, val
)) {
1162 vals
= init_let_vals(val
);
1165 * let_vals: exps = exps, exp
1167 PARSE_TYPED(let_vals
, vals
,
1168 TYPED(let_vals
, vals_
)
1170 TYPED(let_exp
, val
)) {
1171 vals
= add_let_val(vals_
, val
);
1173 PARSE_TYPED(let_vals
, vals
,
1174 TYPED(let_vals
, vals_
)
1176 TYPED(let_val
, val
)) {
1177 vals
= add_let_val(vals_
, val
);
1180 PASS(callback_arg_list
, )
1181 PARSE_TYPED(callback_arg_list
, args
,
1182 TYPED(callback_args
, args_
)) {
1186 PARSE_TYPED(callback_args
, args
,
1187 TYPED(set_value
, val
)) {
1188 args
= init_set_values(val
);
1190 PARSE_TYPED(callback_args
, args
,
1191 TYPED(callback_args
, args_
)
1193 TYPED(set_value
, val
)) {
1194 args
= add_set_value(args_
, val
);
1196 PARSE_NAMED(callback_rval
, rval
,
1197 NAMED(let_func_token
, F
)) {
1200 PARSE_NAMED(callback_rval
, rval
,
1205 PARSE_TYPED(set_stmt
, set
,
1207 TYPED(impl_var
, var
)
1209 TYPED(set_value
, val
)
1211 set
= init_set_stmt(var
, val
);
1214 PARSE_TYPED(set_value
, val
,
1215 TYPED(set_func
, func
)
1217 TYPED(decl_var
, var
)
1219 val
= init_set_value(func
, init_decl_vars(var
));
1221 PARSE_TYPED(set_value
, val
,
1222 TYPED(set_func
, func
)
1224 TYPED(decl_var
, var
)
1226 TYPED(num_exp
, num_
)
1228 val
= init_set_value(func
, init_decl_vars(var
));
1231 PARSE_TYPED(set_value
, val
,
1232 TYPED(set_func
, func_
)
1234 TYPED(decl_var
, var
)
1238 free_set_func(func_
);
1239 val
= init_set_value(init_set_func(T
->type
, T
->text
), init_decl_vars(var
));
1240 val
->func
->token
= T
;
1242 PARSE_TYPED(set_value
, val
,
1243 TYPED(set_func
, func_
)
1245 TYPED(decl_var
, var
)
1247 TYPED(set_vals
, vals
)
1251 val
->vars
= init_decl_vars(var
);
1253 PARSE_TYPED(set_value
, val
,
1254 TYPED(set_func
, func_
)
1256 TYPED(decl_var
, var
)
1258 TYPED(num_exp
, num_
)
1260 TYPED(set_vals
, vals
)
1265 val
->vars
= init_decl_vars(var
);
1268 PARSE_TYPED(set_vals
, vals
,
1269 TYPED(set_value
, val
)) {
1270 vals
= add_inner_set_value(init_set_value(NULL
, NULL
), val
);
1272 PARSE_TYPED(set_vals
, vals
,
1273 TYPED(set_vals
, vals_
)
1275 TYPED(set_value
, val
)) {
1276 vals
= add_inner_set_value(vals_
, val
);
1279 PARSE_TYPED(set_func
, func
,
1280 NAMED(set_func_token
, T
)) {
1281 func
= init_set_func(T
->type
, T
->text
);
1285 PARSE_TYPED(return_stmt
, ret
,
1287 TYPED(set_value
, val
)
1289 ret
= init_return_stmt(val
);
1293 PARSE_TYPED(free_stmt
, free
,
1295 TYPED(free_calls
, calls
)
1297 free
= init_free_stmt(calls
);
1300 PARSE_TYPED(free_calls
, calls
,
1301 TYPED(free_call
, call
)) {
1302 calls
= init_free_calls(call
);
1304 PARSE_TYPED(free_calls
, calls
,
1305 TYPED(free_calls
, calls_
)
1307 TYPED(free_call
, call
)) {
1308 calls
= add_free_call(calls_
, call
);
1311 PARSE_TYPED(free_call
, call
,
1314 TYPED(decl_vars
, vars
)
1316 call
= init_free_call(F
->text
, vars
);
1320 PARSE_TYPED(impl_type
, type_
,
1321 NAMED(impl_type_token
, T
)) {
1322 type_
= init_impl_type(T
->type
, T
->text
);
1326 PARSE_TYPED(reference
, r
, ) {
1329 PARSE_TYPED(reference
, r
,
1334 PARSE_TYPED(indirection
, i
, ){
1337 PARSE_TYPED(indirection
, i
,
1338 TYPED(pointers
, p
)) {
1342 PARSE_TYPED(pointers
, p
,
1346 PARSE_TYPED(pointers
, p
,