10 void psi_error(int, const char *, int, const char *, ...);
15 %token_type {PSI_Token *}
16 %token_destructor {free($$);}
17 %default_destructor {(void)P;}
18 %extra_argument {PSI_Parser *P}
19 /* TOKEN is defined inside syntax_error */
22 if (TOKEN && TOKEN->type != PSI_T_EOF) {
23 psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s'", TOKEN->text);
25 psi_error(PSI_WARNING, P->psi.file.fn, P->line, "PSI syntax error: Unexpected end of input");
32 %fallback NAME TEMP FREE SET LET RETURN LIB STRING.
33 %fallback STRUCT UNION.
38 blocks ::= blocks block.
43 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
45 P->error(T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
47 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
53 block ::= decl(decl). {
54 P->decls = add_decl(P->decls, decl);
56 block ::= impl(impl). {
57 P->impls = add_impl(P->impls, impl);
59 block ::= decl_typedef(def). {
60 P->defs = add_decl_typedef(P->defs, def);
61 if (def->type->strct) {
62 P->structs = add_decl_struct(P->structs, def->type->strct);
65 P->enums = add_decl_enum(P->enums, def->type->enm);
68 block ::= constant(constant). {
69 P->consts = add_constant(P->consts, constant);
71 block ::= decl_struct(strct). {
72 P->structs = add_decl_struct(P->structs, strct);
74 block ::= decl_enum(e). {
75 P->enums = add_decl_enum(P->enums, e);
78 optional_name(n) ::= .{
81 optional_name(n) ::= NAME(N). {
84 enum_name(n) ::= ENUM(E) optional_name(N). {
91 PSI_TokenHash(E, digest);
92 n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@");
96 %type decl_enum {decl_enum *}
97 %destructor decl_enum {free_decl_enum($$);}
98 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
99 e = init_decl_enum(N->text, list);
103 %type decl_enum_items {decl_enum_items*}
104 %destructor decl_enum_items {free_decl_enum_items($$);}
105 decl_enum_items(l) ::= decl_enum_item(i). {
106 l = init_decl_enum_items(i);
108 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
109 l = add_decl_enum_item(l_, i);
112 %type decl_enum_item {decl_enum_item*}
113 %destructor decl_enum_item {free_decl_enum_item($$);}
114 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
115 i = init_decl_enum_item(N->text, num);
118 decl_enum_item(i) ::= NAME(N). {
119 i = init_decl_enum_item(N->text, NULL);
123 struct_name(n) ::= STRUCT(S) optional_name(N). {
130 PSI_TokenHash(S, digest);
131 n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@");
135 %type decl_struct_args_block {decl_args*}
136 %destructor decl_struct_args_block {free_decl_args($$);}
137 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
140 %type decl_struct_args {decl_args*}
141 %destructor decl_struct_args {free_decl_args($$);}
142 decl_struct_args(args_) ::= decl_struct_args_block(args). {
145 decl_struct_args(args_) ::= EOS. {
146 args_ = init_decl_args(NULL);
150 %type decl_struct {decl_struct*}
151 %destructor decl_struct {free_decl_struct($$);}
152 decl_struct(strct) ::= struct_name(N) struct_size(size_) decl_struct_args(args). {
153 strct = init_decl_struct(N->text, args);
158 %type struct_size {size_t}
159 struct_size(size) ::= . {
162 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
163 size = atol(SIZ->text);
167 %token_class const_type_token BOOL INT FLOAT STRING.
168 %type const_type {const_type*}
169 %destructor const_type {free_const_type($$);}
170 const_type(type_) ::= const_type_token(T). {
171 type_ = init_const_type(T->type, T->text);
174 %type constant {constant*}
175 %destructor constant {free_constant($$);}
176 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
177 constant = init_constant(type, T->text, val);
181 %type decl_typedef {decl_arg*}
182 %destructor decl_typedef {
184 if ($$->type->strct) {
185 free_decl_struct($$->type->strct);
188 free_decl_enum($$->type->enm);
190 if ($$->type->func) {
191 free_decl($$->type->func);
194 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
198 %type decl_typedef_body_ex {decl_arg*}
199 %destructor decl_typedef_body_ex {
201 if ($$->type->strct) {
202 free_decl_struct($$->type->strct);
205 free_decl_enum($$->type->enm);
207 if ($$->type->func) {
208 free_decl($$->type->func);
211 decl_typedef_body_ex(def) ::= struct_name(N) struct_size(size_) decl_struct_args_block(args) decl_var(var). {
212 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
213 def->type->token = PSI_TokenCopy(N);
214 def->type->strct = init_decl_struct(N->text, args);
215 def->type->strct->token = N;
216 def->type->strct->size = size_;
218 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
219 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
220 def->var->token = ALIAS;
221 def->type->token = PSI_TokenCopy(e->token);
224 %type decl_typedef_body {decl_arg*}
225 %destructor decl_typedef_body {
227 if ($$->type->strct) {
228 free_decl_struct($$->type->strct);
231 free_decl_enum($$->type->enm);
233 if ($$->type->func) {
234 free_decl($$->type->func);
237 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
240 decl_typedef_body(def) ::= decl_func(func_) LPAREN decl_args(args) RPAREN. {
241 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
242 def->type->token = PSI_TokenCopy(func_->token);
243 def->type->func = init_decl(init_decl_abi("default"), func_, args);
245 decl_typedef_body(def) ::= decl_arg(arg). {
250 %destructor decl {free_decl($$);}
251 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
252 decl = init_decl(abi, func, args);
255 %type decl_func {decl_arg*}
256 %destructor decl_func {free_decl_arg($$);}
257 decl_func(func) ::= decl_arg(arg). {
260 /* special case for void functions */
261 decl_func(func) ::= VOID(T) NAME(N). {
262 func = init_decl_arg(
263 init_decl_type(T->type, T->text),
264 init_decl_var(N->text, 0, 0)
266 func->type->token = T;
270 %type decl_abi {decl_abi*}
271 %destructor decl_abi {free_decl_abi($$);}
272 decl_abi(abi) ::= NAME(T). {
273 abi = init_decl_abi(T->text);
277 %type decl_var {decl_var*}
278 %destructor decl_var {free_decl_var($$);}
279 decl_var(var) ::= indirection(p) NAME(T). {
280 var = init_decl_var(T->text, p, 0);
283 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
284 var = init_decl_var(T->text, p+1, atol(D->text));
289 %type decl_vars {decl_vars*}
290 %destructor decl_vars {free_decl_vars($$);}
291 decl_vars(vars) ::= decl_var(var). {
292 vars = init_decl_vars(var);
294 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
295 vars = add_decl_var(vars_, var);
298 %type decl_arg {decl_arg*}
299 %destructor decl_arg {free_decl_arg($$);}
300 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
301 arg_ = init_decl_arg(type, var);
303 /* void pointers need a specific rule */
304 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
305 arg_ = init_decl_arg(
306 init_decl_type(T->type, T->text),
307 init_decl_var(N->text, p, 0)
309 arg_->type->token = T;
310 arg_->var->token = N;
313 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
314 arg_ = init_decl_arg(
315 init_decl_type(T->type, T->text),
316 init_decl_var(N->text, p, 0)
318 arg_->type->token = T;
319 arg_->var->token = N;
323 %type decl_args {decl_args*}
324 %destructor decl_args {free_decl_args($$);}
327 decl_args(args) ::= decl_arg(arg). {
328 args = init_decl_args(arg);
330 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
331 args = add_decl_arg(args_, arg);
333 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
337 %type struct_args {decl_args*}
338 %destructor struct_args {free_decl_args($$);}
339 struct_args(args) ::= struct_arg(arg). {
340 args = init_decl_args(arg);
342 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
343 args = add_decl_arg(args_, arg);
345 %type struct_arg {decl_arg*}
346 %destructor struct_arg {free_decl_arg($$);}
347 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
350 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
351 arg_->layout = layout_;
355 %type struct_layout {decl_struct_layout*}
356 %destructor struct_layout {free_decl_struct_layout($$);}
357 struct_layout(layout) ::= . {
360 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
361 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
366 /* un/signed, urgh */
367 decl_scalar_type(type_) ::= CHAR(C). {
370 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
372 type_ = PSI_TokenCat(2, S, s);
379 decl_scalar_type_short(s) ::= . {
383 decl_scalar_type_short(s) ::= INT(I). {
386 decl_scalar_type(type_) ::= INT(I). {
389 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
391 type_ = PSI_TokenCat(2, L, l);
398 decl_scalar_type_long(l) ::= . {
401 decl_scalar_type_long(l) ::= DOUBLE(D). {
404 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
406 l = PSI_TokenCat(2, L, ll);
413 decl_scalar_type_long_long(ll) ::= . {
416 decl_scalar_type_long_long(ll) ::= INT(I). {
419 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
420 PSI_Token *T = PSI_TokenCat(2, U, N);
421 type_ = init_decl_type(T->type, T->text);
426 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
427 PSI_Token *T = PSI_TokenCat(2, S, N);
428 type_ = init_decl_type(T->type, T->text);
433 decl_type(type_) ::= UNSIGNED(U). {
434 type_ = init_decl_type(PSI_T_NAME, U->text);
437 decl_type(type_) ::= SIGNED(S). {
438 type_ = init_decl_type(PSI_T_NAME, S->text);
441 decl_type(type_) ::= decl_scalar_type(N). {
442 type_ = init_decl_type(N->type, N->text);
446 decl_type(type_) ::= STRUCT(S) NAME(T). {
447 type_ = init_decl_type(S->type, T->text);
451 decl_type(type_) ::= ENUM(E) NAME(T). {
452 type_ = init_decl_type(E->type, T->text);
456 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
457 %type decl_type {decl_type*}
458 %destructor decl_type {free_decl_type($$);}
459 decl_type(type_) ::= decl_type_token(T). {
460 type_ = init_decl_type(T->type, T->text);
465 %type const_decl_type {decl_type*}
466 %destructor const_decl_type {free_decl_type($$);}
467 const_decl_type(type) ::= decl_type(type_). {
470 const_decl_type(type) ::= CONST decl_type(type_). {
475 %destructor impl {free_impl($$);}
476 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
477 impl = init_impl(func, stmts);
480 %type impl_func {impl_func*}
481 %destructor impl_func {free_impl_func($$);}
482 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
483 func = init_impl_func(NAME->text, args, type, r);
487 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
488 %type impl_def_val {impl_def_val*}
489 %destructor impl_def_val {free_impl_def_val($$);}
490 impl_def_val(def) ::= impl_def_val_token(T). {
491 def = init_impl_def_val(T->type, T->text);
495 %type impl_var {impl_var*}
496 %destructor impl_var {free_impl_var($$);}
497 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
498 var = init_impl_var(T->text, r);
502 %type impl_arg {impl_arg*}
503 %destructor impl_arg {free_impl_arg($$);}
504 impl_arg(arg) ::= impl_type(type) impl_var(var). {
505 arg = init_impl_arg(type, var, NULL);
507 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
508 arg = init_impl_arg(type, var, def);
511 %type impl_args {impl_args*}
512 %destructor impl_args {free_impl_args($$);}
513 impl_args(args) ::= LPAREN RPAREN. {
516 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
519 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
521 args->vararg.name = va;
524 %type impl_vararg {impl_arg*}
525 %destructor impl_vararg {free_impl_arg($$);}
526 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
527 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
531 %type impl_arg_list {impl_args*}
532 %destructor impl_arg_list {free_impl_args($$);}
533 impl_arg_list(args) ::= impl_arg(arg). {
534 args = init_impl_args(arg);
536 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
537 args = add_impl_arg(args_, arg);
540 %type impl_stmts {impl_stmts*}
541 %destructor impl_stmts {free_impl_stmts($$);}
542 impl_stmts(stmts) ::= impl_stmt(stmt). {
543 stmts = init_impl_stmts(stmt);
545 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
546 stmts = add_impl_stmt(stmts_, stmt);
549 %type impl_stmt {impl_stmt*}
550 %destructor impl_stmt {free_impl_stmt($$);}
551 impl_stmt(stmt) ::= let_stmt(let). {
552 stmt = init_impl_stmt(PSI_T_LET, let);
554 impl_stmt(stmt) ::= set_stmt(set). {
555 stmt = init_impl_stmt(PSI_T_SET, set);
557 impl_stmt(stmt) ::= return_stmt(ret). {
558 stmt = init_impl_stmt(PSI_T_RETURN, ret);
560 impl_stmt(stmt) ::= free_stmt(free). {
561 stmt = init_impl_stmt(PSI_T_FREE, free);
564 %token_class num_exp_token NUMBER NSNAME.
565 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
566 %type num_exp {num_exp*}
567 %destructor num_exp {free_num_exp($$);}
568 num_exp(exp) ::= num_exp_token(tok). {
569 exp = init_num_exp(tok->type, tok->text);
572 num_exp(exp) ::= decl_var(var). {
573 exp = init_num_exp(PSI_T_NAME, var);
574 exp->token = PSI_TokenCopy(var->token);
576 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
577 exp_->operator = operator_->type;
578 exp_->operand = operand_;
583 %type let_stmt {let_stmt*}
584 %destructor let_stmt {free_let_stmt($$);}
585 let_stmt(let) ::= LET decl_var(var) EOS. {
586 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
588 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
589 val->flags.one.is_reference = r ? 1 : 0;
590 let = init_let_stmt(var, val);
592 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
593 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
596 %type let_val {let_val*}
597 %destructor let_val {free_let_val($$);}
598 let_val(val) ::= NULL. {
599 val = init_let_val(PSI_LET_NULL, NULL);
601 let_val(val) ::= num_exp(exp). {
602 val = init_let_val(PSI_LET_NUMEXP, exp);
604 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
605 val = init_let_val(PSI_LET_CALLOC, alloc);
607 let_val(val) ::= let_func(func). {
608 val = init_let_val(PSI_LET_FUNC, func);
611 %type let_calloc {let_calloc*}
612 %destructor let_calloc {free_let_calloc($$);}
613 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
614 alloc = init_let_calloc(nmemb, size);
616 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
617 %type let_func {let_func*}
618 %destructor let_func {free_let_func($$);}
619 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
620 func = init_let_func(T->type, T->text, var);
625 %type set_stmt {set_stmt*}
626 %destructor set_stmt {free_set_stmt($$);}
627 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
628 set = init_set_stmt(var, val);
631 %type set_value {set_value*}
632 %destructor set_value {free_set_value($$);}
633 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
634 val = init_set_value(func, init_decl_vars(var));
636 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
637 val = init_set_value(func, init_decl_vars(var));
640 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
641 free_set_func(func_);
642 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
643 val->func->token = T;
645 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
648 val->vars = init_decl_vars(var);
650 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
654 val->vars = init_decl_vars(var);
656 %type set_vals {set_value*}
657 %destructor set_vals {free_set_value($$);}
658 set_vals(vals) ::= set_value(val). {
659 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
661 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
662 vals = add_inner_set_value(vals_, val);
665 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
666 %type set_func {set_func*}
667 %destructor set_func {free_set_func($$);}
668 set_func(func) ::= set_func_token(T). {
669 func = init_set_func(T->type, T->text);
673 %type return_stmt {return_stmt*}
674 %destructor return_stmt {free_return_stmt($$);}
675 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
676 ret = init_return_stmt(val);
680 %type free_stmt {free_stmt*}
681 %destructor free_stmt {free_free_stmt($$);}
682 free_stmt(free) ::= FREE free_calls(calls) EOS. {
683 free = init_free_stmt(calls);
686 %type free_calls {free_calls*}
687 %destructor free_calls {free_free_calls($$);}
688 free_calls(calls) ::= free_call(call). {
689 calls = init_free_calls(call);
691 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
692 calls = add_free_call(calls_, call);
695 %type free_call {free_call*}
696 %destructor free_call {free_free_call($$);}
697 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
698 call = init_free_call(F->text, vars);
702 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
703 %type impl_type {impl_type*}
704 %destructor impl_type {free_impl_type($$);}
705 impl_type(type_) ::= impl_type_token(T). {
706 type_ = init_impl_type(T->type, T->text);
710 %type reference {char}
711 reference(r) ::= . {r = 0;}
712 reference(r) ::= AMPERSAND. {r = 1;}
714 %type indirection {unsigned}
715 indirection(i) ::= . {i = 0;}
716 indirection(i) ::= pointers(p). {i = p;}
718 %type pointers {unsigned}
719 pointers(p) ::= ASTERISK. {p = 1;}
720 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}