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 CALLOC LIB STRING.
37 blocks ::= blocks block.
42 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
44 P->error(P, T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
46 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
52 block ::= decl(decl). {
53 P->decls = add_decl(P->decls, decl);
55 block ::= impl(impl). {
56 P->impls = add_impl(P->impls, impl);
58 block ::= decl_typedef(def). {
59 P->defs = add_decl_typedef(P->defs, def);
60 if (def->type->strct) {
61 P->structs = add_decl_struct(P->structs, def->type->strct);
64 P->enums = add_decl_enum(P->enums, def->type->enm);
67 block ::= constant(constant). {
68 P->consts = add_constant(P->consts, constant);
70 block ::= decl_struct(strct). {
71 P->structs = add_decl_struct(P->structs, strct);
73 block ::= decl_union(u). {
74 P->unions = add_decl_union(P->unions, u);
76 block ::= decl_enum(e). {
77 P->enums = add_decl_enum(P->enums, e);
80 optional_name(n) ::= .{
83 optional_name(n) ::= NAME(N). {
87 enum_name(n) ::= ENUM(E) optional_name(N). {
94 PSI_TokenHash(E, digest);
95 n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@");
99 %type decl_enum {decl_enum *}
100 %destructor decl_enum {free_decl_enum($$);}
101 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
102 e = init_decl_enum(N->text, list);
106 %type decl_enum_items {decl_enum_items*}
107 %destructor decl_enum_items {free_decl_enum_items($$);}
108 decl_enum_items(l) ::= decl_enum_item(i). {
109 l = init_decl_enum_items(i);
111 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
112 l = add_decl_enum_item(l_, i);
115 %type decl_enum_item {decl_enum_item*}
116 %destructor decl_enum_item {free_decl_enum_item($$);}
117 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
118 i = init_decl_enum_item(N->text, num);
121 decl_enum_item(i) ::= NAME(N). {
122 i = init_decl_enum_item(N->text, NULL);
126 union_name(n) ::= UNION(U) optional_name(N). {
133 PSI_TokenHash(U, digest);
134 n = PSI_TokenTranslit(PSI_TokenAppend(U, 1, digest), " ", "@");
138 struct_name(n) ::= STRUCT(S) optional_name(N). {
145 PSI_TokenHash(S, digest);
146 n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@");
150 %type decl_struct_args_block {decl_args*}
151 %destructor decl_struct_args_block {free_decl_args($$);}
152 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
155 %type decl_struct_args {decl_args*}
156 %destructor decl_struct_args {free_decl_args($$);}
157 decl_struct_args(args_) ::= decl_struct_args_block(args). {
160 decl_struct_args(args_) ::= EOS. {
161 args_ = init_decl_args(NULL);
165 %type decl_struct {decl_struct*}
166 %destructor decl_struct {free_decl_struct($$);}
167 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
168 strct = init_decl_struct(N->text, args);
174 %type align_and_size { struct {size_t a; size_t s; } }
175 align_and_size(as) ::= . {
179 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
180 as.a = atol(A->text);
181 as.s = atol(S->text);
186 %type decl_union {decl_union*}
187 %destructor decl_union {free_decl_union($$);}
188 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
189 u = init_decl_union(N->text, args);
195 %token_class const_type_token BOOL INT FLOAT STRING.
196 %type const_type {const_type*}
197 %destructor const_type {free_const_type($$);}
198 const_type(type_) ::= const_type_token(T). {
199 type_ = init_const_type(T->type, T->text);
202 %type constant {constant*}
203 %destructor constant {free_constant($$);}
204 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
205 constant = init_constant(type, T->text, val);
209 %type decl_typedef {decl_arg*}
210 %destructor decl_typedef {
212 if ($$->type->strct) {
213 free_decl_struct($$->type->strct);
216 free_decl_enum($$->type->enm);
218 if ($$->type->func) {
219 free_decl($$->type->func);
222 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
226 %type decl_typedef_body_ex {decl_arg*}
227 %destructor decl_typedef_body_ex {
229 if ($$->type->strct) {
230 free_decl_struct($$->type->strct);
233 free_decl_enum($$->type->enm);
236 free_decl_union($$->type->unn);
238 if ($$->type->func) {
239 free_decl($$->type->func);
242 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
243 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
244 def->type->token = PSI_TokenCopy(N);
245 def->type->strct = init_decl_struct(N->text, args);
246 def->type->strct->token = N;
247 def->type->strct->align = as.a;
248 def->type->strct->size = as.s;
250 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
251 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
252 def->type->token = PSI_TokenCopy(N);
253 def->type->unn = init_decl_union(N->text, args);
254 def->type->unn->token = N;
255 def->type->unn->align = as.a;
256 def->type->unn->size = as.s;
258 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
259 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
260 def->var->token = ALIAS;
261 def->type->token = PSI_TokenCopy(e->token);
264 %type decl_typedef_body {decl_arg*}
265 %destructor decl_typedef_body {
267 if ($$->type->strct) {
268 free_decl_struct($$->type->strct);
271 free_decl_enum($$->type->enm);
274 free_decl_union($$->type->unn);
276 if ($$->type->func) {
277 free_decl($$->type->func);
280 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
283 decl_typedef_body(def) ::= decl_func(func_) LPAREN decl_args(args) RPAREN. {
284 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
285 def->type->token = PSI_TokenCopy(func_->token);
286 def->type->func = init_decl(init_decl_abi("default"), func_, args);
288 decl_typedef_body(def) ::= decl_arg(arg). {
293 %destructor decl {free_decl($$);}
294 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
295 decl = init_decl(abi, func, args);
298 %type decl_func {decl_arg*}
299 %destructor decl_func {free_decl_arg($$);}
300 decl_func(func) ::= decl_arg(arg). {
303 /* special case for void functions */
304 decl_func(func) ::= VOID(T) NAME(N). {
305 func = init_decl_arg(
306 init_decl_type(T->type, T->text),
307 init_decl_var(N->text, 0, 0)
309 func->type->token = T;
310 func->var->token = N;
314 %type decl_abi {decl_abi*}
315 %destructor decl_abi {free_decl_abi($$);}
316 decl_abi(abi) ::= NAME(T). {
317 abi = init_decl_abi(T->text);
321 %type decl_var {decl_var*}
322 %destructor decl_var {free_decl_var($$);}
323 decl_var(var) ::= indirection(p) NAME(T). {
324 var = init_decl_var(T->text, p, 0);
327 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
328 var = init_decl_var(T->text, p+1, atol(D->text));
333 %type decl_vars {decl_vars*}
334 %destructor decl_vars {free_decl_vars($$);}
335 decl_vars(vars) ::= decl_var(var). {
336 vars = init_decl_vars(var);
338 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
339 vars = add_decl_var(vars_, var);
342 %type decl_arg {decl_arg*}
343 %destructor decl_arg {free_decl_arg($$);}
344 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
345 arg_ = init_decl_arg(type, var);
347 /* void pointers need a specific rule */
348 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
349 arg_ = init_decl_arg(
350 init_decl_type(T->type, T->text),
351 init_decl_var(N->text, p, 0)
353 arg_->type->token = T;
354 arg_->var->token = N;
357 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
358 arg_ = init_decl_arg(
359 init_decl_type(T->type, T->text),
360 init_decl_var(N->text, p, 0)
362 arg_->type->token = T;
363 arg_->var->token = N;
367 %type decl_args {decl_args*}
368 %destructor decl_args {free_decl_args($$);}
371 decl_args(args) ::= decl_arg(arg). {
372 args = init_decl_args(arg);
374 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
375 args = add_decl_arg(args_, arg);
377 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
381 %type struct_args {decl_args*}
382 %destructor struct_args {free_decl_args($$);}
383 struct_args(args) ::= struct_arg(arg). {
384 args = init_decl_args(arg);
386 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
387 args = add_decl_arg(args_, arg);
389 %type struct_arg {decl_arg*}
390 %destructor struct_arg {
392 if ($$->type->strct) {
393 free_decl_struct($$->type->strct);
396 free_decl_enum($$->type->enm);
398 if ($$->type->func) {
399 free_decl($$->type->func);
402 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
404 if (def->type->strct) {
405 P->structs = add_decl_struct(P->structs, def->type->strct);
407 if (def->type->enm) {
408 P->enums = add_decl_enum(P->enums, def->type->enm);
411 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
412 arg_->layout = layout_;
416 %type struct_layout {decl_struct_layout*}
417 %destructor struct_layout {free_decl_struct_layout($$);}
418 struct_layout(layout) ::= . {
421 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
422 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
427 /* un/signed, urgh */
428 decl_scalar_type(type_) ::= CHAR(C). {
431 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
433 type_ = PSI_TokenCat(2, S, s);
440 decl_scalar_type_short(s) ::= . {
444 decl_scalar_type_short(s) ::= INT(I). {
447 decl_scalar_type(type_) ::= INT(I). {
450 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
452 type_ = PSI_TokenCat(2, L, l);
459 decl_scalar_type_long(l) ::= . {
462 decl_scalar_type_long(l) ::= DOUBLE(D). {
465 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
467 l = PSI_TokenCat(2, L, ll);
474 decl_scalar_type_long_long(ll) ::= . {
477 decl_scalar_type_long_long(ll) ::= INT(I). {
480 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
481 PSI_Token *T = PSI_TokenCat(2, U, N);
482 type_ = init_decl_type(T->type, T->text);
487 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
488 PSI_Token *T = PSI_TokenCat(2, S, N);
489 type_ = init_decl_type(T->type, T->text);
494 decl_type(type_) ::= UNSIGNED(U). {
495 type_ = init_decl_type(PSI_T_NAME, U->text);
498 decl_type(type_) ::= SIGNED(S). {
499 type_ = init_decl_type(PSI_T_NAME, S->text);
502 decl_type(type_) ::= decl_scalar_type(N). {
503 type_ = init_decl_type(N->type, N->text);
507 decl_type(type_) ::= STRUCT(S) NAME(T). {
508 type_ = init_decl_type(S->type, T->text);
512 decl_type(type_) ::= UNION(U) NAME(T). {
513 type_ = init_decl_type(U->type, T->text);
517 decl_type(type_) ::= ENUM(E) NAME(T). {
518 type_ = init_decl_type(E->type, T->text);
522 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
523 %type decl_type {decl_type*}
524 %destructor decl_type {free_decl_type($$);}
525 decl_type(type_) ::= decl_type_token(T). {
526 type_ = init_decl_type(T->type, T->text);
531 %type const_decl_type {decl_type*}
532 %destructor const_decl_type {free_decl_type($$);}
533 const_decl_type(type) ::= decl_type(type_). {
536 const_decl_type(type) ::= CONST decl_type(type_). {
541 %destructor impl {free_impl($$);}
542 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
543 impl = init_impl(func, stmts);
546 %type impl_func {impl_func*}
547 %destructor impl_func {free_impl_func($$);}
548 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
549 func = init_impl_func(NAME->text, args, type, r);
553 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
554 %type impl_def_val {impl_def_val*}
555 %destructor impl_def_val {free_impl_def_val($$);}
556 impl_def_val(def) ::= impl_def_val_token(T). {
557 def = init_impl_def_val(T->type, T->text);
561 %type impl_var {impl_var*}
562 %destructor impl_var {free_impl_var($$);}
563 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
564 var = init_impl_var(T->text, r);
568 %type impl_arg {impl_arg*}
569 %destructor impl_arg {free_impl_arg($$);}
570 impl_arg(arg) ::= impl_type(type) impl_var(var). {
571 arg = init_impl_arg(type, var, NULL);
573 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
574 arg = init_impl_arg(type, var, def);
577 %type impl_args {impl_args*}
578 %destructor impl_args {free_impl_args($$);}
579 impl_args(args) ::= LPAREN RPAREN. {
582 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
585 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
587 args->vararg.name = va;
590 %type impl_vararg {impl_arg*}
591 %destructor impl_vararg {free_impl_arg($$);}
592 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
593 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
597 %type impl_arg_list {impl_args*}
598 %destructor impl_arg_list {free_impl_args($$);}
599 impl_arg_list(args) ::= impl_arg(arg). {
600 args = init_impl_args(arg);
602 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
603 args = add_impl_arg(args_, arg);
606 %type impl_stmts {impl_stmts*}
607 %destructor impl_stmts {free_impl_stmts($$);}
608 impl_stmts(stmts) ::= impl_stmt(stmt). {
609 stmts = init_impl_stmts(stmt);
611 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
612 stmts = add_impl_stmt(stmts_, stmt);
615 %type impl_stmt {impl_stmt*}
616 %destructor impl_stmt {free_impl_stmt($$);}
617 impl_stmt(stmt) ::= let_stmt(let). {
618 stmt = init_impl_stmt(PSI_T_LET, let);
620 impl_stmt(stmt) ::= set_stmt(set). {
621 stmt = init_impl_stmt(PSI_T_SET, set);
623 impl_stmt(stmt) ::= return_stmt(ret). {
624 stmt = init_impl_stmt(PSI_T_RETURN, ret);
626 impl_stmt(stmt) ::= free_stmt(free). {
627 stmt = init_impl_stmt(PSI_T_FREE, free);
630 %token_class num_exp_token NUMBER NSNAME.
631 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
632 %type num_exp {num_exp*}
633 %destructor num_exp {free_num_exp($$);}
634 num_exp(exp) ::= num_exp_token(tok). {
635 exp = init_num_exp(tok->type, tok->text);
638 num_exp(exp) ::= decl_var(var). {
639 exp = init_num_exp(PSI_T_NAME, var);
640 exp->token = PSI_TokenCopy(var->token);
642 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
643 exp_->operator = operator_->type;
644 exp_->operand = operand_;
649 %type let_stmt {let_stmt*}
650 %destructor let_stmt {free_let_stmt($$);}
651 let_stmt(let) ::= LET decl_var(var) EOS. {
652 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
654 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
655 val->flags.one.is_reference = r ? 1 : 0;
656 let = init_let_stmt(var, val);
658 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
659 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
662 %type let_val {let_val*}
663 %destructor let_val {free_let_val($$);}
664 let_val(val) ::= NULL. {
665 val = init_let_val(PSI_LET_NULL, NULL);
667 let_val(val) ::= num_exp(exp). {
668 val = init_let_val(PSI_LET_NUMEXP, exp);
670 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
671 val = init_let_val(PSI_LET_CALLOC, alloc);
673 let_val(val) ::= let_func(func). {
674 val = init_let_val(PSI_LET_FUNC, func);
677 %type let_calloc {let_calloc*}
678 %destructor let_calloc {free_let_calloc($$);}
679 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
680 alloc = init_let_calloc(nmemb, size);
682 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
683 %type let_func {let_func*}
684 %destructor let_func {free_let_func($$);}
685 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
686 func = init_let_func(T->type, T->text, var);
691 %type set_stmt {set_stmt*}
692 %destructor set_stmt {free_set_stmt($$);}
693 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
694 set = init_set_stmt(var, val);
697 %type set_value {set_value*}
698 %destructor set_value {free_set_value($$);}
699 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
700 val = init_set_value(func, init_decl_vars(var));
702 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
703 val = init_set_value(func, init_decl_vars(var));
706 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
707 free_set_func(func_);
708 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
709 val->func->token = T;
711 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
714 val->vars = init_decl_vars(var);
716 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
720 val->vars = init_decl_vars(var);
722 %type set_vals {set_value*}
723 %destructor set_vals {free_set_value($$);}
724 set_vals(vals) ::= set_value(val). {
725 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
727 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
728 vals = add_inner_set_value(vals_, val);
731 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
732 %type set_func {set_func*}
733 %destructor set_func {free_set_func($$);}
734 set_func(func) ::= set_func_token(T). {
735 func = init_set_func(T->type, T->text);
739 %type return_stmt {return_stmt*}
740 %destructor return_stmt {free_return_stmt($$);}
741 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
742 ret = init_return_stmt(val);
746 %type free_stmt {free_stmt*}
747 %destructor free_stmt {free_free_stmt($$);}
748 free_stmt(free) ::= FREE free_calls(calls) EOS. {
749 free = init_free_stmt(calls);
752 %type free_calls {free_calls*}
753 %destructor free_calls {free_free_calls($$);}
754 free_calls(calls) ::= free_call(call). {
755 calls = init_free_calls(call);
757 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
758 calls = add_free_call(calls_, call);
761 %type free_call {free_call*}
762 %destructor free_call {free_free_call($$);}
763 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
764 call = init_free_call(F->text, vars);
768 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
769 %type impl_type {impl_type*}
770 %destructor impl_type {free_impl_type($$);}
771 impl_type(type_) ::= impl_type_token(T). {
772 type_ = init_impl_type(T->type, T->text);
776 %type reference {char}
777 reference(r) ::= . {r = 0;}
778 reference(r) ::= AMPERSAND. {r = 1;}
780 %type indirection {unsigned}
781 indirection(i) ::= . {i = 0;}
782 indirection(i) ::= pointers(p). {i = p;}
784 %type pointers {unsigned}
785 pointers(p) ::= ASTERISK. {p = 1;}
786 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}