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