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