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 LET(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}
22 #define PARSE(nt, rule) \
23 static void COUNTED(nt) (struct psi_parser *P rule)
24 #define PARSE_NAMED(nt, nt_name, rule) \
25 static void COUNTED(nt) (struct psi_parser *P NAMED(nt, nt_name) rule)
26 #define PARSE_TYPED(nt, nt_name, rule) \
27 static void COUNTED(nt) (struct psi_parser *P TYPED(nt, nt_name) rule)
29 #define NAMED(t, name) , struct psi_token *name
30 #define TYPED(t, name) , TOKEN_TYPE_NAME(t) name
31 #define TOKEN_TYPE_NAME(token) _##token##_type
32 #define TOKEN_TYPE(token, type) typedef type TOKEN_TYPE_NAME(token);
33 #define TOKEN_DTOR(token, dtor)
36 DEF(%name
, psi_parser_proc_
)
37 DEF(%token_prefix
, PSI_T_
)
38 DEF(%token_type
, {struct psi_token
*})
39 DEF(%token_destructor
, {free($$
);})
40 DEF(%default_destructor
, {(void)P
;})
41 DEF(%extra_argument
, {struct psi_parser
*P
})
43 /* TOKEN is defined inside syntax_error */
46 if (TOKEN
&& TOKEN
->type
!= PSI_T_EOF
) {
47 psi_error(PSI_WARNING
, TOKEN
->file
, TOKEN
->line
, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN
->text
, TOKEN
->col
);
49 psi_error(PSI_WARNING
, P
->psi
.file
.fn
, P
->line
, "PSI syntax error: Unexpected end of input");
54 DEF(%left
, PLUS MINUS
.)
55 DEF(%left
, SLASH ASTERISK
.)
56 DEF(%fallback
, NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING
.)
58 DEF(%token_class
, const_type_token BOOL INT FLOAT STRING
.)
59 DEF(%token_class
, decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME
.)
60 DEF(%token_class
, impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING
.)
61 DEF(%token_class
, num_exp_token NUMBER NSNAME
.)
62 DEF(%token_class
, num_exp_op_token PLUS MINUS ASTERISK SLASH
.)
63 DEF(%token_class
, let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL
.)
64 DEF(%token_class
, set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID
.)
65 DEF(%token_class
, impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE
.)
67 TOKEN_TYPE(decl_enum
, decl_enum
*)
68 TOKEN_DTOR(decl_enum
, free_decl_enum($$
);)
69 TOKEN_TYPE(decl_enum_items
, decl_enum_items
*)
70 TOKEN_DTOR(decl_enum_items
, free_decl_enum_items($$
);)
71 TOKEN_TYPE(decl_enum_item
, decl_enum_item
*)
72 TOKEN_DTOR(decl_enum_item
, free_decl_enum_item($$
);)
73 TOKEN_TYPE(decl_struct_args_block
, decl_args
*)
74 TOKEN_DTOR(decl_struct_args_block
, free_decl_args($$
);) /* there was a typo */
75 TOKEN_TYPE(decl_struct_args
, decl_args
*)
76 TOKEN_DTOR(decl_struct_args
, free_decl_args($$
);)
77 TOKEN_TYPE(decl_struct
, decl_struct
*)
78 TOKEN_DTOR(decl_struct
, free_decl_struct($$
);)
79 TOKEN_TYPE(align_and_size
, decl_struct_layout
)
80 TOKEN_TYPE(decl_union
, decl_union
*)
81 TOKEN_DTOR(decl_union
, free_decl_union($$
);)
82 TOKEN_TYPE(const_type
, const_type
*)
83 TOKEN_DTOR(const_type
, free_const_type($$
);)
84 TOKEN_TYPE(constant
, constant
*)
85 TOKEN_DTOR(constant
, free_constant($$
);)
86 TOKEN_TYPE(decl_typedef
, decl_arg
*)
87 TOKEN_DTOR(decl_typedef
, free_decl_arg($$
);)
88 TOKEN_TYPE(decl_typedef_body_ex
, decl_arg
*)
89 TOKEN_DTOR(decl_typedef_body_ex
, free_decl_arg($$
);)
90 TOKEN_TYPE(decl_typedef_body
, decl_arg
*)
91 TOKEN_DTOR(decl_typedef_body
, free_decl_arg($$
);)
92 TOKEN_TYPE(decl_typedef_body_fn_args
, decl_args
*)
93 TOKEN_DTOR(decl_typedef_body_fn_args
, free_decl_args($$
);)
94 TOKEN_TYPE(decl
, decl
*)
95 TOKEN_DTOR(decl
, free_decl($$
);)
96 TOKEN_TYPE(decl_func
, decl_arg
*)
97 TOKEN_DTOR(decl_func
, free_decl_arg($$
);)
98 TOKEN_TYPE(decl_abi
, decl_abi
*)
99 TOKEN_DTOR(decl_abi
, free_decl_abi($$
);)
100 TOKEN_TYPE(decl_var
, decl_var
*)
101 TOKEN_DTOR(decl_var
, free_decl_var($$
);)
102 TOKEN_TYPE(decl_vars
, decl_vars
*)
103 TOKEN_DTOR(decl_vars
, free_decl_vars($$
);)
104 TOKEN_TYPE(decl_arg
, decl_arg
*)
105 TOKEN_DTOR(decl_arg
, free_decl_arg($$
);)
106 TOKEN_TYPE(decl_args
, decl_args
*)
107 TOKEN_DTOR(decl_args
, free_decl_args($$
);)
108 TOKEN_TYPE(struct_args
, decl_args
*)
109 TOKEN_DTOR(struct_args
, free_decl_args($$
);)
110 TOKEN_TYPE(struct_arg
, decl_arg
*)
111 TOKEN_DTOR(struct_arg
, free_decl_arg($$
);)
112 TOKEN_TYPE(struct_layout
, decl_struct_layout
*)
113 TOKEN_DTOR(struct_layout
, free_decl_struct_layout($$
);)
114 TOKEN_TYPE(decl_type
, decl_type
*)
115 TOKEN_DTOR(decl_type
, free_decl_type($$
);)
116 TOKEN_TYPE(const_decl_type
, decl_type
*)
117 TOKEN_DTOR(const_decl_type
, free_decl_type($$
);)
118 TOKEN_TYPE(impl
, impl
*)
119 TOKEN_DTOR(impl
, free_impl($$
);)
120 TOKEN_TYPE(impl_func
, impl_func
*)
121 TOKEN_DTOR(impl_func
, free_impl_func($$
);)
122 TOKEN_TYPE(impl_def_val
, impl_def_val
*)
123 TOKEN_DTOR(impl_def_val
, free_impl_def_val($$
);)
124 TOKEN_TYPE(impl_var
, impl_var
*)
125 TOKEN_DTOR(impl_var
, free_impl_var($$
);)
126 TOKEN_TYPE(impl_arg
, impl_arg
*)
127 TOKEN_DTOR(impl_arg
, free_impl_arg($$
);)
128 TOKEN_TYPE(impl_args
, impl_args
*)
129 TOKEN_DTOR(impl_args
, free_impl_args($$
);)
130 TOKEN_TYPE(impl_vararg
, impl_arg
*)
131 TOKEN_DTOR(impl_vararg
, free_impl_arg($$
);)
132 TOKEN_TYPE(impl_arg_list
, impl_args
*)
133 TOKEN_DTOR(impl_arg_list
, free_impl_args($$
);)
134 TOKEN_TYPE(impl_stmts
, impl_stmts
*)
135 TOKEN_DTOR(impl_stmts
, free_impl_stmts($$
);)
136 TOKEN_TYPE(impl_stmt
, impl_stmt
*)
137 TOKEN_DTOR(impl_stmt
, free_impl_stmt($$
);)
138 TOKEN_TYPE(num_exp
, num_exp
*)
139 TOKEN_DTOR(num_exp
, free_num_exp($$
);)
140 TOKEN_TYPE(let_stmt
, let_stmt
*)
141 TOKEN_DTOR(let_stmt
, free_let_stmt($$
);)
142 TOKEN_TYPE(let_calloc
, let_calloc
*)
143 TOKEN_DTOR(let_calloc
, free_let_calloc($$
);)
144 TOKEN_TYPE(let_func
, let_func
*)
145 TOKEN_DTOR(let_func
, free_let_func($$
);)
146 TOKEN_TYPE(callback_arg_list
, set_values
*)
147 TOKEN_DTOR(callback_arg_list
, free_set_values($$
);)
148 TOKEN_TYPE(callback_args
, set_values
*)
149 TOKEN_DTOR(callback_args
, free_set_values($$
);)
150 TOKEN_TYPE(let_val
, let_val
*)
151 TOKEN_DTOR(let_val
, free_let_val($$
);)
152 TOKEN_TYPE(let_vals
, let_vals
*)
153 TOKEN_DTOR(let_vals
, free_let_vals($$
);)
154 TOKEN_TYPE(set_stmt
, set_stmt
*)
155 TOKEN_DTOR(set_stmt
, free_set_stmt($$
);)
156 TOKEN_TYPE(set_value
, set_value
*)
157 TOKEN_DTOR(set_value
, free_set_value($$
);)
158 TOKEN_TYPE(set_vals
, set_value
*)
159 TOKEN_DTOR(set_vals
, free_set_value($$
);)
160 TOKEN_TYPE(set_func
, set_func
*)
161 TOKEN_DTOR(set_func
, free_set_func($$
);)
162 TOKEN_TYPE(return_stmt
, return_stmt
*)
163 TOKEN_DTOR(return_stmt
, free_return_stmt($$
);)
164 TOKEN_TYPE(free_stmt
, free_stmt
*)
165 TOKEN_DTOR(free_stmt
, free_free_stmt($$
);)
166 TOKEN_TYPE(free_calls
, free_calls
*)
167 TOKEN_DTOR(free_calls
, free_free_calls($$
);)
168 TOKEN_TYPE(free_call
, free_call
*)
169 TOKEN_DTOR(free_call
, free_free_call($$
);)
170 TOKEN_TYPE(impl_type
, impl_type
*)
171 TOKEN_DTOR(impl_type
, free_impl_type($$
);)
172 TOKEN_TYPE(reference
, char)
173 TOKEN_TYPE(indirection
, unsigned)
174 TOKEN_TYPE(pointers
, unsigned)
178 LET(blocks
, blocks block
)
183 PARSE(block
, NAMED(LIB
, token
) NAMED(QUOTED_STRING
, libname
) TOKEN(EOS
)) {
184 if (P
->psi
.file
.ln
) {
185 P
->error(P
, token
, PSI_WARNING
, "Extra 'lib %s' statement has no effect", libname
->text
);
187 P
->psi
.file
.ln
= strndup(libname
->text
+ 1, libname
->size
- 2);
192 PARSE(block
, TYPED(decl
, decl
)) {
193 P
->decls
= add_decl(P
->decls
, decl
);
196 PARSE(block
, TYPED(impl
, impl
)) {
197 P
->impls
= add_impl(P
->impls
, impl
);
200 PARSE(block
, TYPED(decl_typedef
, def
)) {
201 P
->defs
= add_decl_typedef(P
->defs
, def
);
202 switch (def
->type
->type
) {
204 if (def
->type
->real
.strct
) {
205 P
->structs
= add_decl_struct(P
->structs
, def
->type
->real
.strct
);
209 if (def
->type
->real
.unn
) {
210 P
->unions
= add_decl_union(P
->unions
, def
->type
->real
.unn
);
214 if (def
->type
->real
.enm
) {
215 P
->enums
= add_decl_enum(P
->enums
, def
->type
->real
.enm
);
221 PARSE(block
, TYPED(constant
, constant
)) {
222 P
->consts
= add_constant(P
->consts
, constant
);
225 PARSE(block
, TYPED(decl_struct
, strct
)) {
226 P
->structs
= add_decl_struct(P
->structs
, strct
);
229 PARSE(block
, TYPED(decl_union
, u
)) {
230 P
->unions
= add_decl_union(P
->unions
, u
);
233 PARSE(block
, TYPED(decl_enum
, e
)) {
234 P
->enums
= add_decl_enum(P
->enums
, e
);
237 PARSE_NAMED(optional_name
, n
, ) {
241 PARSE_NAMED(optional_name
, n
,
246 PARSE_NAMED(enum_name
, n
,
247 NAMED(ENUM
, E
) NAMED(optional_name
, N
)) {
254 psi_token_hash(E
, digest
);
255 n
= psi_token_translit(psi_token_append(E
, 1, digest
), " ", "@");
259 PARSE_TYPED(decl_enum
, e
,
262 TYPED(decl_enum_items
, list
)
264 e
= init_decl_enum(N
->text
, list
);
268 PARSE_TYPED(decl_enum_items
, l
,
269 TYPED(decl_enum_item
, i
)) {
270 l
= init_decl_enum_items(i
);
272 PARSE_TYPED(decl_enum_items
, l
,
273 TYPED(decl_enum_items
, l_
)
275 TYPED(decl_enum_item
, i
)) {
276 l
= add_decl_enum_item(l_
, i
);
279 PARSE_TYPED(decl_enum_item
, i
,
282 TYPED(num_exp
, num
)) {
283 i
= init_decl_enum_item(N
->text
, num
);
286 PARSE_TYPED(decl_enum_item
, i
,
288 i
= init_decl_enum_item(N
->text
, NULL
);
292 PARSE_NAMED(union_name
, n
,
294 NAMED(optional_name
, N
)) {
301 psi_token_hash(U
, digest
);
302 n
= psi_token_translit(psi_token_append(U
, 1, digest
), " ", "@");
306 PARSE_NAMED(struct_name
, n
,
308 NAMED(optional_name
, N
)) {
315 psi_token_hash(S
, digest
);
316 n
= psi_token_translit(psi_token_append(S
, 1, digest
), " ", "@");
320 PARSE_TYPED(decl_struct_args_block
, args_
,
322 TYPED(struct_args
, args
)
327 PARSE_TYPED(decl_struct_args
, args_
,
328 TYPED(decl_struct_args_block
, args
)) {
331 PARSE_TYPED(decl_struct_args
, args_
,
333 args_
= init_decl_args(NULL
);
336 PARSE_TYPED(decl_struct
, strct
,
339 TYPED(align_and_size
, as
)
340 TYPED(decl_struct_args
, args
)) {
341 strct
= init_decl_struct(N
->text
, args
);
342 strct
->align
= as
.pos
;
343 strct
->size
= as
.len
;
347 PARSE_TYPED(align_and_size
, as
, ) {
351 PARSE_TYPED(align_and_size
, as
,
359 as
.pos
= atol(A
->text
);
360 as
.len
= atol(S
->text
);
365 PARSE_TYPED(decl_union
, u
,
368 TYPED(align_and_size
, as
)
369 TYPED(decl_struct_args
, args
)) {
370 u
= init_decl_union(N
->text
, args
);
376 PARSE_TYPED(const_type
, type_
,
377 NAMED(const_type_token
, T
)) {
378 type_
= init_const_type(T
->type
, T
->text
);
382 PARSE_TYPED(constant
, constant
,
384 TYPED(const_type
, type
)
387 TYPED(impl_def_val
, val
)
389 constant
= init_constant(type
, T
->text
, val
);
393 PARSE_TYPED(decl_typedef
, def
,
395 TYPED(decl_typedef_body
, def_
)
401 PARSE_TYPED(decl_typedef_body_ex
, def
,
402 NAMED(struct_name
, N
)
403 TYPED(align_and_size
, as
)
404 TYPED(decl_struct_args_block
, args
)
405 TYPED(decl_var
, var
)) {
406 def
= init_decl_arg(init_decl_type(PSI_T_STRUCT
, N
->text
), var
);
407 def
->type
->token
= psi_token_copy(N
);
408 def
->type
->real
.strct
= init_decl_struct(N
->text
, args
);
409 def
->type
->real
.strct
->token
= N
;
410 def
->type
->real
.strct
->align
= as
.pos
;
411 def
->type
->real
.strct
->size
= as
.len
;
413 PARSE_TYPED(decl_typedef_body_ex
, def
,
415 TYPED(align_and_size
, as
)
416 TYPED(decl_struct_args_block
, args
)
417 TYPED(decl_var
, var
)) {
418 def
= init_decl_arg(init_decl_type(PSI_T_UNION
, N
->text
), var
);
419 def
->type
->token
= psi_token_copy(N
);
420 def
->type
->real
.unn
= init_decl_union(N
->text
, args
);
421 def
->type
->real
.unn
->token
= N
;
422 def
->type
->real
.unn
->align
= as
.pos
;
423 def
->type
->real
.unn
->size
= as
.len
;
425 PARSE_TYPED(decl_typedef_body_ex
, def
,
427 NAMED(NAME
, ALIAS
)) {
428 def
= init_decl_arg(init_decl_type(PSI_T_ENUM
, e
->name
), init_decl_var(ALIAS
->text
, 0, 0));
429 def
->var
->token
= ALIAS
;
430 def
->type
->token
= psi_token_copy(e
->token
);
431 def
->type
->real
.enm
= e
;
434 PARSE_TYPED(decl_typedef_body
, def
,
435 TYPED(decl_typedef_body_ex
, def_
)) {
439 PARSE_TYPED(decl_typedef_body_fn_args
, args
,
441 TYPED(decl_args
, args_
)
445 PARSE_TYPED(decl_typedef_body
, def
,
446 TYPED(decl_func
, func_
)
447 TYPED(decl_typedef_body_fn_args
, args
)) {
448 def
= init_decl_arg(init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
), copy_decl_var(func_
->var
));
449 def
->type
->token
= psi_token_copy(func_
->token
);
450 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
452 PARSE_TYPED(decl_typedef_body
, def
,
453 TYPED(decl_arg
, arg
)) {
457 PARSE_TYPED(decl
, decl
,
459 TYPED(decl_func
, func
)
461 TYPED(decl_args
, args
)
464 decl
= init_decl(abi
, func
, args
);
467 PARSE_TYPED(decl_func
, func
,
468 TYPED(decl_arg
, arg
)) {
471 /* special case for void functions */
472 PARSE_TYPED(decl_func
, func
,
475 func
= init_decl_arg(
476 init_decl_type(T
->type
, T
->text
),
477 init_decl_var(N
->text
, 0, 0)
479 func
->type
->token
= T
;
480 func
->var
->token
= N
;
483 PARSE_TYPED(decl_typedef_body
, def
,
485 TYPED(indirection
, decl_i
)
487 TYPED(indirection
, type_i
)
490 TYPED(decl_typedef_body_fn_args
, args
)) {
491 decl_arg
*func_
= init_decl_arg(
492 init_decl_type(T
->type
, T
->text
),
493 init_decl_var(N
->text
, decl_i
, 0)
495 func_
->type
->token
= T
;
496 func_
->var
->token
= N
;
500 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
501 copy_decl_var(func_
->var
)
503 def
->var
->pointer_level
= type_i
;
504 def
->type
->token
= psi_token_copy(func_
->token
);
505 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
507 PARSE_TYPED(decl_typedef_body
, def
,
510 TYPED(pointers
, decl_i
)
512 TYPED(indirection
, type_i
)
515 TYPED(decl_typedef_body_fn_args
, args
)) {
516 decl_arg
*func_
= init_decl_arg(
517 init_decl_type(T
->type
, T
->text
),
518 init_decl_var(N
->text
, decl_i
, 0)
520 func_
->type
->token
= T
;
521 func_
->var
->token
= N
;
525 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
526 copy_decl_var(func_
->var
)
528 def
->var
->pointer_level
= type_i
;
529 def
->type
->token
= psi_token_copy(func_
->token
);
530 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
533 PARSE_TYPED(decl_abi
, abi
,
535 abi
= init_decl_abi(T
->text
);
539 PARSE_TYPED(decl_var
, var
,
540 TYPED(indirection
, p
)
542 var
= init_decl_var(T
->text
, p
, 0);
545 PARSE_TYPED(decl_var
, var
,
546 TYPED(indirection
, p
)
551 var
= init_decl_var(T
->text
, p
+1, atol(D
->text
));
556 PARSE_TYPED(decl_vars
, vars
,
557 TYPED(decl_var
, var
)) {
558 vars
= init_decl_vars(var
);
560 PARSE_TYPED(decl_vars
, vars
,
561 TYPED(decl_vars
, vars_
)
563 TYPED(decl_var
, var
)) {
564 vars
= add_decl_var(vars_
, var
);
567 PARSE_TYPED(decl_arg
, arg_
,
568 TYPED(const_decl_type
, type
)
569 TYPED(decl_var
, var
)) {
570 arg_
= init_decl_arg(type
, var
);
572 PARSE_TYPED(decl_typedef_body
, def
,
573 TYPED(const_decl_type
, type_
)
574 TYPED(indirection
, decl_i
)
576 TYPED(indirection
, type_i
)
579 TYPED(decl_typedef_body_fn_args
, args
)) {
580 decl_arg
*func_
= init_decl_arg(
582 init_decl_var(N
->text
, decl_i
, 0)
584 func_
->var
->token
= N
;
588 init_decl_type(PSI_T_FUNCTION
, func_
->var
->name
),
589 copy_decl_var(func_
->var
)
591 def
->var
->pointer_level
= type_i
;
592 def
->type
->token
= psi_token_copy(func_
->token
);
593 def
->type
->real
.func
= init_decl(init_decl_abi("default"), func_
, args
);
596 /* void pointers need a specific rule */
597 PARSE_TYPED(decl_arg
, arg_
,
601 arg_
= init_decl_arg(
602 init_decl_type(T
->type
, T
->text
),
603 init_decl_var(N
->text
, p
, 0)
605 arg_
->type
->token
= T
;
606 arg_
->var
->token
= N
;
609 PARSE_TYPED(decl_arg
, arg_
,
614 arg_
= init_decl_arg(
615 init_decl_type(T
->type
, T
->text
),
616 init_decl_var(N
->text
, p
, 0)
618 arg_
->type
->token
= T
;
619 arg_
->var
->token
= N
;
625 PARSE_TYPED(decl_args
, args
,
626 TYPED(decl_arg
, arg
)) {
627 args
= init_decl_args(arg
);
629 PARSE_TYPED(decl_args
, args
,
630 TYPED(decl_args
, args_
)
632 TYPED(decl_arg
, arg
)) {
633 args
= add_decl_arg(args_
, arg
);
635 PARSE_TYPED(decl_args
, args
,
636 TYPED(decl_args
, args_
)
643 PARSE_TYPED(struct_args
, args
,
644 TYPED(struct_arg
, arg
)) {
645 args
= init_decl_args(arg
);
647 PARSE_TYPED(struct_args
, args
,
648 TYPED(struct_args
, args_
)
649 TYPED(struct_arg
, arg
)) {
650 args
= add_decl_arg(args_
, arg
);
653 PARSE_TYPED(struct_arg
, arg_
,
654 TYPED(decl_typedef_body_ex
, def
)
657 switch (def
->type
->type
) {
659 if (def
->type
->real
.strct
) {
660 P
->structs
= add_decl_struct(P
->structs
, def
->type
->real
.strct
);
664 if (def
->type
->real
.unn
) {
665 P
->unions
= add_decl_union(P
->unions
, def
->type
->real
.unn
);
669 if (def
->type
->real
.enm
) {
670 P
->enums
= add_decl_enum(P
->enums
, def
->type
->real
.enm
);
675 PARSE_TYPED(struct_arg
, arg
,
676 TYPED(decl_arg
, arg_
)
677 TYPED(struct_layout
, layout_
)
679 arg_
->layout
= layout_
;
683 PARSE_TYPED(struct_layout
, layout
, ) {
686 PARSE_TYPED(struct_layout
, layout
,
694 layout
= init_decl_struct_layout(atol(POS
->text
), atol(SIZ
->text
));
699 /* un/signed, urgh */
700 PARSE_NAMED(decl_scalar_type
, type_
,
704 PARSE_NAMED(decl_scalar_type
, type_
,
706 NAMED(decl_scalar_type_short
, s
)) {
708 type_
= psi_token_cat(2, S
, s
);
715 PARSE_NAMED(decl_scalar_type_short
, s
, ) {
719 PARSE_NAMED(decl_scalar_type_short
, s
,
723 PARSE_NAMED(decl_scalar_type
, type_
,
727 PARSE_NAMED(decl_scalar_type
, type_
,
729 NAMED(decl_scalar_type_long
, l
)) {
731 type_
= psi_token_cat(2, L
, l
);
738 PARSE_NAMED(decl_scalar_type_long
, l
, ) {
741 PARSE_NAMED(decl_scalar_type_long
, l
,
745 PARSE_NAMED(decl_scalar_type_long
, l
,
747 NAMED(decl_scalar_type_long_long
, ll
)) {
749 l
= psi_token_cat(2, L
, ll
);
756 PARSE_NAMED(decl_scalar_type_long_long
, ll
, ) {
759 PARSE_NAMED(decl_scalar_type_long_long
, ll
,
763 PARSE_TYPED(decl_type
, type_
,
765 NAMED(decl_scalar_type
, N
)) {
766 struct psi_token
*T
= psi_token_cat(2, U
, N
);
767 type_
= init_decl_type(T
->type
, T
->text
);
772 PARSE_TYPED(decl_type
, type_
,
774 NAMED(decl_scalar_type
, N
)) {
775 struct psi_token
*T
= psi_token_cat(2, S
, N
);
776 type_
= init_decl_type(T
->type
, T
->text
);
781 PARSE_TYPED(decl_type
, type_
,
782 NAMED(UNSIGNED
, U
)) {
783 type_
= init_decl_type(PSI_T_NAME
, U
->text
);
786 PARSE_TYPED(decl_type
, type_
,
788 type_
= init_decl_type(PSI_T_NAME
, S
->text
);
791 PARSE_TYPED(decl_type
, type_
,
792 NAMED(decl_scalar_type
, N
)) {
793 type_
= init_decl_type(N
->type
, N
->text
);
797 PARSE_TYPED(decl_type
, type_
,
800 type_
= init_decl_type(S
->type
, T
->text
);
804 PARSE_TYPED(decl_type
, type_
,
807 type_
= init_decl_type(U
->type
, T
->text
);
811 PARSE_TYPED(decl_type
, type_
,
814 type_
= init_decl_type(E
->type
, T
->text
);
818 PARSE_TYPED(decl_type
, type_
,
819 NAMED(decl_type_token
, T
)) {
820 type_
= init_decl_type(T
->type
, T
->text
);
825 PARSE_TYPED(const_decl_type
, type
,
826 TYPED(decl_type
, type_
)) {
829 PARSE_TYPED(const_decl_type
, type
,
831 TYPED(decl_type
, type_
)) {
835 PARSE_TYPED(impl
, impl
,
836 TYPED(impl_func
, func
)
838 TYPED(impl_stmts
, stmts
)
840 impl
= init_impl(func
, stmts
);
843 PARSE_TYPED(impl_func
, func
,
847 TYPED(impl_args
, args
)
849 TYPED(impl_type
, type
)) {
850 func
= init_impl_func(NAME
->text
, args
, type
, r
);
854 PARSE_TYPED(impl_def_val
, def
,
855 NAMED(impl_def_val_token
, T
)) {
856 def
= init_impl_def_val(T
->type
, T
->text
);
860 PARSE_TYPED(impl_var
, var
,
862 NAMED(DOLLAR_NAME
, T
)) {
863 var
= init_impl_var(T
->text
, r
);
867 PARSE_TYPED(impl_arg
, arg
,
868 TYPED(impl_type
, type
)
869 TYPED(impl_var
, var
)) {
870 arg
= init_impl_arg(type
, var
, NULL
);
872 PARSE_TYPED(impl_arg
, arg
,
873 TYPED(impl_type
, type
)
876 TYPED(impl_def_val
, def
)) {
877 arg
= init_impl_arg(type
, var
, def
);
880 PARSE_TYPED(impl_args
, args
,
885 PARSE_TYPED(impl_args
, args
,
887 TYPED(impl_arg_list
, args_
)
891 PARSE_TYPED(impl_args
, args
,
893 TYPED(impl_arg_list
, args_
)
895 TYPED(impl_vararg
, va
)
898 args
->vararg
.name
= va
;
901 PARSE_TYPED(impl_vararg
, va
,
902 TYPED(impl_type
, type
)
905 NAMED(DOLLAR_NAME
, T
)) {
906 va
= init_impl_arg(type
, init_impl_var(T
->text
, r
), NULL
);
910 PARSE_TYPED(impl_arg_list
, args
,
911 TYPED(impl_arg
, arg
)) {
912 args
= init_impl_args(arg
);
914 PARSE_TYPED(impl_arg_list
, args
,
915 TYPED(impl_arg_list
, args_
)
917 TYPED(impl_arg
, arg
)) {
918 args
= add_impl_arg(args_
, arg
);
921 PARSE_TYPED(impl_stmts
, stmts
,
922 TYPED(impl_stmt
, stmt
)) {
923 stmts
= init_impl_stmts(stmt
);
925 PARSE_TYPED(impl_stmts
, stmts
,
926 TYPED(impl_stmts
, stmts_
)
927 TYPED(impl_stmt
, stmt
)) {
928 stmts
= add_impl_stmt(stmts_
, stmt
);
931 PARSE_TYPED(impl_stmt
, stmt
,
932 TYPED(let_stmt
, let
)) {
933 stmt
= init_impl_stmt(PSI_T_LET
, let
);
935 PARSE_TYPED(impl_stmt
, stmt
,
936 TYPED(set_stmt
, set
)) {
937 stmt
= init_impl_stmt(PSI_T_SET
, set
);
939 PARSE_TYPED(impl_stmt
, stmt
,
940 TYPED(return_stmt
, ret
)) {
941 stmt
= init_impl_stmt(PSI_T_RETURN
, ret
);
943 PARSE_TYPED(impl_stmt
, stmt
,
944 TYPED(free_stmt
, free
)) {
945 stmt
= init_impl_stmt(PSI_T_FREE
, free
);
948 PARSE_TYPED(num_exp
, exp
,
949 NAMED(num_exp_token
, tok
)) {
950 exp
= init_num_exp(tok
->type
, tok
->text
);
953 PARSE_TYPED(num_exp
, exp
,
954 TYPED(decl_var
, var
)) {
955 exp
= init_num_exp(PSI_T_NAME
, var
);
956 exp
->token
= psi_token_copy(var
->token
);
958 PARSE_TYPED(num_exp
, exp
,
960 NAMED(num_exp_op_token
, operator_
)
961 TYPED(num_exp
, operand_
)) {
962 exp_
->operator = operator_
->type
;
963 exp_
->operand
= operand_
;
968 PARSE_TYPED(let_stmt
, let
,
972 let
= init_let_stmt(var
, init_let_val(PSI_LET_NULL
, NULL
));
974 PARSE_TYPED(let_stmt
, let
,
981 val
->flags
.one
.is_reference
= r
? 1 : 0;
982 let
= init_let_stmt(var
, val
);
984 PARSE_TYPED(let_stmt
, let
,
990 let
= init_let_stmt(var
, init_let_val(PSI_LET_TMP
, val
));
993 PARSE_TYPED(let_calloc
, alloc
,
994 TYPED(num_exp
, nmemb
)
996 TYPED(num_exp
, size
)) {
997 alloc
= init_let_calloc(nmemb
, size
);
1000 PARSE_TYPED(let_func
, func
,
1001 NAMED(let_func_token
, T
)
1003 TYPED(impl_var
, var
)
1005 func
= init_let_func(T
->type
, T
->text
, var
);
1009 PARSE_TYPED(let_vals
, vals
,
1010 TYPED(let_val
, val
)) {
1011 vals
= init_let_vals(val
);
1013 PARSE_TYPED(let_vals
, vals
,
1014 TYPED(let_vals
, vals_
)
1016 TYPED(let_val
, val
)) {
1017 vals
= add_let_val(vals_
, val
);
1020 PARSE_TYPED(let_func
, func
,
1021 NAMED(let_func_token
, T
)
1023 TYPED(impl_var
, var
)
1025 TYPED(let_vals
, vals
)
1027 func
= init_let_func(T
->type
, T
->text
, var
);
1032 LET(callback_arg_list
, )
1033 PARSE_TYPED(callback_arg_list
, args
,
1034 TYPED(callback_args
, args_
)) {
1038 PARSE_TYPED(callback_args
, args
,
1039 TYPED(set_value
, val
)) {
1040 args
= init_set_values(val
);
1042 PARSE_TYPED(callback_args
, args
,
1043 TYPED(callback_args
, args_
)
1045 TYPED(set_value
, val
)) {
1046 args
= add_set_value(args_
, val
);
1048 PARSE_NAMED(callback_rval
, rval
,
1049 NAMED(let_func_token
, F
)) {
1052 PARSE_NAMED(callback_rval
, rval
,
1057 PARSE_TYPED(let_val
, val
,
1059 val
= init_let_val(PSI_LET_NULL
, NULL
);
1061 PARSE_TYPED(let_val
, val
,
1062 TYPED(num_exp
, exp
)) {
1063 val
= init_let_val(PSI_LET_NUMEXP
, exp
);
1065 PARSE_TYPED(let_val
, val
,
1068 TYPED(let_calloc
, alloc
)
1070 val
= init_let_val(PSI_LET_CALLOC
, alloc
);
1072 PARSE_TYPED(let_val
, val
,
1073 TYPED(let_func
, func_
)) {
1074 val
= init_let_val(PSI_LET_FUNC
, func_
);
1076 PARSE_TYPED(let_val
, val
,
1078 NAMED(callback_rval
, F
)
1080 TYPED(impl_var
, var
)
1082 TYPED(callback_arg_list
, args_
)
1085 val
= init_let_val(PSI_LET_CALLBACK
, init_let_callback(
1086 init_let_func(F
->type
, F
->text
, var
), args_
));
1090 PARSE_TYPED(set_stmt
, set
,
1092 TYPED(impl_var
, var
)
1094 TYPED(set_value
, val
)
1096 set
= init_set_stmt(var
, val
);
1099 PARSE_TYPED(set_value
, val
,
1100 TYPED(set_func
, func
)
1102 TYPED(decl_var
, var
)
1104 val
= init_set_value(func
, init_decl_vars(var
));
1106 PARSE_TYPED(set_value
, val
,
1107 TYPED(set_func
, func
)
1109 TYPED(decl_var
, var
)
1111 TYPED(num_exp
, num_
)
1113 val
= init_set_value(func
, init_decl_vars(var
));
1116 PARSE_TYPED(set_value
, val
,
1117 TYPED(set_func
, func_
)
1119 TYPED(decl_var
, var
)
1123 free_set_func(func_
);
1124 val
= init_set_value(init_set_func(T
->type
, T
->text
), init_decl_vars(var
));
1125 val
->func
->token
= T
;
1127 PARSE_TYPED(set_value
, val
,
1128 TYPED(set_func
, func_
)
1130 TYPED(decl_var
, var
)
1132 TYPED(set_vals
, vals
)
1136 val
->vars
= init_decl_vars(var
);
1138 PARSE_TYPED(set_value
, val
,
1139 TYPED(set_func
, func_
)
1141 TYPED(decl_var
, var
)
1143 TYPED(num_exp
, num_
)
1145 TYPED(set_vals
, vals
)
1150 val
->vars
= init_decl_vars(var
);
1153 PARSE_TYPED(set_vals
, vals
,
1154 TYPED(set_value
, val
)) {
1155 vals
= add_inner_set_value(init_set_value(NULL
, NULL
), val
);
1157 PARSE_TYPED(set_vals
, vals
,
1158 TYPED(set_vals
, vals_
)
1160 TYPED(set_value
, val
)) {
1161 vals
= add_inner_set_value(vals_
, val
);
1164 PARSE_TYPED(set_func
, func
,
1165 NAMED(set_func_token
, T
)) {
1166 func
= init_set_func(T
->type
, T
->text
);
1170 PARSE_TYPED(return_stmt
, ret
,
1172 TYPED(set_value
, val
)
1174 ret
= init_return_stmt(val
);
1178 PARSE_TYPED(free_stmt
, free
,
1180 TYPED(free_calls
, calls
)
1182 free
= init_free_stmt(calls
);
1185 PARSE_TYPED(free_calls
, calls
,
1186 TYPED(free_call
, call
)) {
1187 calls
= init_free_calls(call
);
1189 PARSE_TYPED(free_calls
, calls
,
1190 TYPED(free_calls
, calls_
)
1192 TYPED(free_call
, call
)) {
1193 calls
= add_free_call(calls_
, call
);
1196 PARSE_TYPED(free_call
, call
,
1199 TYPED(decl_vars
, vars
)
1201 call
= init_free_call(F
->text
, vars
);
1205 PARSE_TYPED(impl_type
, type_
,
1206 NAMED(impl_type_token
, T
)) {
1207 type_
= init_impl_type(T
->type
, T
->text
);
1211 PARSE_TYPED(reference
, r
, ) {
1214 PARSE_TYPED(reference
, r
,
1219 PARSE_TYPED(indirection
, i
, ){
1222 PARSE_TYPED(indirection
, i
,
1223 TYPED(pointers
, p
)) {
1227 PARSE_TYPED(pointers
, p
,
1231 PARSE_TYPED(pointers
, p
,