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