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.
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) struct_size(size_) decl_struct_args(args). {
168 strct = init_decl_struct(N->text, args);
173 %type struct_size {size_t}
174 struct_size(size) ::= . {
177 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
178 size = atol(SIZ->text);
182 %type decl_union {decl_union*}
183 %destructor decl_union {free_decl_union($$);}
184 decl_union(u) ::= UNION NAME(N) struct_size(size_) decl_struct_args(args). {
185 u = init_decl_union(N->text, args);
190 %token_class const_type_token BOOL INT FLOAT STRING.
191 %type const_type {const_type*}
192 %destructor const_type {free_const_type($$);}
193 const_type(type_) ::= const_type_token(T). {
194 type_ = init_const_type(T->type, T->text);
197 %type constant {constant*}
198 %destructor constant {free_constant($$);}
199 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
200 constant = init_constant(type, T->text, val);
204 %type decl_typedef {decl_arg*}
205 %destructor decl_typedef {
207 if ($$->type->strct) {
208 free_decl_struct($$->type->strct);
211 free_decl_enum($$->type->enm);
213 if ($$->type->func) {
214 free_decl($$->type->func);
217 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
221 %type decl_typedef_body_ex {decl_arg*}
222 %destructor decl_typedef_body_ex {
224 if ($$->type->strct) {
225 free_decl_struct($$->type->strct);
228 free_decl_enum($$->type->enm);
231 free_decl_union($$->type->unn);
233 if ($$->type->func) {
234 free_decl($$->type->func);
237 decl_typedef_body_ex(def) ::= struct_name(N) struct_size(size_) decl_struct_args_block(args) decl_var(var). {
238 def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
239 def->type->token = PSI_TokenCopy(N);
240 def->type->strct = init_decl_struct(N->text, args);
241 def->type->strct->token = N;
242 def->type->strct->size = size_;
244 decl_typedef_body_ex(def) ::= union_name(N) struct_size(size_) decl_struct_args_block(args) decl_var(var). {
245 def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
246 def->type->token = PSI_TokenCopy(N);
247 def->type->unn = init_decl_union(N->text, args);
248 def->type->unn->token = N;
249 def->type->unn->size = size_;
251 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
252 def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
253 def->var->token = ALIAS;
254 def->type->token = PSI_TokenCopy(e->token);
257 %type decl_typedef_body {decl_arg*}
258 %destructor decl_typedef_body {
260 if ($$->type->strct) {
261 free_decl_struct($$->type->strct);
264 free_decl_enum($$->type->enm);
267 free_decl_union($$->type->unn);
269 if ($$->type->func) {
270 free_decl($$->type->func);
273 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
276 decl_typedef_body(def) ::= decl_func(func_) LPAREN decl_args(args) RPAREN. {
277 def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
278 def->type->token = PSI_TokenCopy(func_->token);
279 def->type->func = init_decl(init_decl_abi("default"), func_, args);
281 decl_typedef_body(def) ::= decl_arg(arg). {
286 %destructor decl {free_decl($$);}
287 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
288 decl = init_decl(abi, func, args);
291 %type decl_func {decl_arg*}
292 %destructor decl_func {free_decl_arg($$);}
293 decl_func(func) ::= decl_arg(arg). {
296 /* special case for void functions */
297 decl_func(func) ::= VOID(T) NAME(N). {
298 func = init_decl_arg(
299 init_decl_type(T->type, T->text),
300 init_decl_var(N->text, 0, 0)
302 func->type->token = T;
303 func->var->token = N;
307 %type decl_abi {decl_abi*}
308 %destructor decl_abi {free_decl_abi($$);}
309 decl_abi(abi) ::= NAME(T). {
310 abi = init_decl_abi(T->text);
314 %type decl_var {decl_var*}
315 %destructor decl_var {free_decl_var($$);}
316 decl_var(var) ::= indirection(p) NAME(T). {
317 var = init_decl_var(T->text, p, 0);
320 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
321 var = init_decl_var(T->text, p+1, atol(D->text));
326 %type decl_vars {decl_vars*}
327 %destructor decl_vars {free_decl_vars($$);}
328 decl_vars(vars) ::= decl_var(var). {
329 vars = init_decl_vars(var);
331 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
332 vars = add_decl_var(vars_, var);
335 %type decl_arg {decl_arg*}
336 %destructor decl_arg {free_decl_arg($$);}
337 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
338 arg_ = init_decl_arg(type, var);
340 /* void pointers need a specific rule */
341 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
342 arg_ = init_decl_arg(
343 init_decl_type(T->type, T->text),
344 init_decl_var(N->text, p, 0)
346 arg_->type->token = T;
347 arg_->var->token = N;
350 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
351 arg_ = init_decl_arg(
352 init_decl_type(T->type, T->text),
353 init_decl_var(N->text, p, 0)
355 arg_->type->token = T;
356 arg_->var->token = N;
360 %type decl_args {decl_args*}
361 %destructor decl_args {free_decl_args($$);}
364 decl_args(args) ::= decl_arg(arg). {
365 args = init_decl_args(arg);
367 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
368 args = add_decl_arg(args_, arg);
370 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
374 %type struct_args {decl_args*}
375 %destructor struct_args {free_decl_args($$);}
376 struct_args(args) ::= struct_arg(arg). {
377 args = init_decl_args(arg);
379 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
380 args = add_decl_arg(args_, arg);
382 %type struct_arg {decl_arg*}
383 %destructor struct_arg {
385 if ($$->type->strct) {
386 free_decl_struct($$->type->strct);
389 free_decl_enum($$->type->enm);
391 if ($$->type->func) {
392 free_decl($$->type->func);
395 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
397 if (def->type->strct) {
398 P->structs = add_decl_struct(P->structs, def->type->strct);
400 if (def->type->enm) {
401 P->enums = add_decl_enum(P->enums, def->type->enm);
404 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
405 arg_->layout = layout_;
409 %type struct_layout {decl_struct_layout*}
410 %destructor struct_layout {free_decl_struct_layout($$);}
411 struct_layout(layout) ::= . {
414 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
415 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
420 /* un/signed, urgh */
421 decl_scalar_type(type_) ::= CHAR(C). {
424 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
426 type_ = PSI_TokenCat(2, S, s);
433 decl_scalar_type_short(s) ::= . {
437 decl_scalar_type_short(s) ::= INT(I). {
440 decl_scalar_type(type_) ::= INT(I). {
443 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
445 type_ = PSI_TokenCat(2, L, l);
452 decl_scalar_type_long(l) ::= . {
455 decl_scalar_type_long(l) ::= DOUBLE(D). {
458 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
460 l = PSI_TokenCat(2, L, ll);
467 decl_scalar_type_long_long(ll) ::= . {
470 decl_scalar_type_long_long(ll) ::= INT(I). {
473 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
474 PSI_Token *T = PSI_TokenCat(2, U, N);
475 type_ = init_decl_type(T->type, T->text);
480 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
481 PSI_Token *T = PSI_TokenCat(2, S, N);
482 type_ = init_decl_type(T->type, T->text);
487 decl_type(type_) ::= UNSIGNED(U). {
488 type_ = init_decl_type(PSI_T_NAME, U->text);
491 decl_type(type_) ::= SIGNED(S). {
492 type_ = init_decl_type(PSI_T_NAME, S->text);
495 decl_type(type_) ::= decl_scalar_type(N). {
496 type_ = init_decl_type(N->type, N->text);
500 decl_type(type_) ::= STRUCT(S) NAME(T). {
501 type_ = init_decl_type(S->type, T->text);
505 decl_type(type_) ::= UNION(U) NAME(T). {
506 type_ = init_decl_type(U->type, T->text);
510 decl_type(type_) ::= ENUM(E) NAME(T). {
511 type_ = init_decl_type(E->type, T->text);
515 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
516 %type decl_type {decl_type*}
517 %destructor decl_type {free_decl_type($$);}
518 decl_type(type_) ::= decl_type_token(T). {
519 type_ = init_decl_type(T->type, T->text);
524 %type const_decl_type {decl_type*}
525 %destructor const_decl_type {free_decl_type($$);}
526 const_decl_type(type) ::= decl_type(type_). {
529 const_decl_type(type) ::= CONST decl_type(type_). {
534 %destructor impl {free_impl($$);}
535 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
536 impl = init_impl(func, stmts);
539 %type impl_func {impl_func*}
540 %destructor impl_func {free_impl_func($$);}
541 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
542 func = init_impl_func(NAME->text, args, type, r);
546 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
547 %type impl_def_val {impl_def_val*}
548 %destructor impl_def_val {free_impl_def_val($$);}
549 impl_def_val(def) ::= impl_def_val_token(T). {
550 def = init_impl_def_val(T->type, T->text);
554 %type impl_var {impl_var*}
555 %destructor impl_var {free_impl_var($$);}
556 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
557 var = init_impl_var(T->text, r);
561 %type impl_arg {impl_arg*}
562 %destructor impl_arg {free_impl_arg($$);}
563 impl_arg(arg) ::= impl_type(type) impl_var(var). {
564 arg = init_impl_arg(type, var, NULL);
566 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
567 arg = init_impl_arg(type, var, def);
570 %type impl_args {impl_args*}
571 %destructor impl_args {free_impl_args($$);}
572 impl_args(args) ::= LPAREN RPAREN. {
575 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
578 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
580 args->vararg.name = va;
583 %type impl_vararg {impl_arg*}
584 %destructor impl_vararg {free_impl_arg($$);}
585 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
586 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
590 %type impl_arg_list {impl_args*}
591 %destructor impl_arg_list {free_impl_args($$);}
592 impl_arg_list(args) ::= impl_arg(arg). {
593 args = init_impl_args(arg);
595 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
596 args = add_impl_arg(args_, arg);
599 %type impl_stmts {impl_stmts*}
600 %destructor impl_stmts {free_impl_stmts($$);}
601 impl_stmts(stmts) ::= impl_stmt(stmt). {
602 stmts = init_impl_stmts(stmt);
604 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
605 stmts = add_impl_stmt(stmts_, stmt);
608 %type impl_stmt {impl_stmt*}
609 %destructor impl_stmt {free_impl_stmt($$);}
610 impl_stmt(stmt) ::= let_stmt(let). {
611 stmt = init_impl_stmt(PSI_T_LET, let);
613 impl_stmt(stmt) ::= set_stmt(set). {
614 stmt = init_impl_stmt(PSI_T_SET, set);
616 impl_stmt(stmt) ::= return_stmt(ret). {
617 stmt = init_impl_stmt(PSI_T_RETURN, ret);
619 impl_stmt(stmt) ::= free_stmt(free). {
620 stmt = init_impl_stmt(PSI_T_FREE, free);
623 %token_class num_exp_token NUMBER NSNAME.
624 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
625 %type num_exp {num_exp*}
626 %destructor num_exp {free_num_exp($$);}
627 num_exp(exp) ::= num_exp_token(tok). {
628 exp = init_num_exp(tok->type, tok->text);
631 num_exp(exp) ::= decl_var(var). {
632 exp = init_num_exp(PSI_T_NAME, var);
633 exp->token = PSI_TokenCopy(var->token);
635 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
636 exp_->operator = operator_->type;
637 exp_->operand = operand_;
642 %type let_stmt {let_stmt*}
643 %destructor let_stmt {free_let_stmt($$);}
644 let_stmt(let) ::= LET decl_var(var) EOS. {
645 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
647 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
648 val->flags.one.is_reference = r ? 1 : 0;
649 let = init_let_stmt(var, val);
651 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
652 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
655 %type let_val {let_val*}
656 %destructor let_val {free_let_val($$);}
657 let_val(val) ::= NULL. {
658 val = init_let_val(PSI_LET_NULL, NULL);
660 let_val(val) ::= num_exp(exp). {
661 val = init_let_val(PSI_LET_NUMEXP, exp);
663 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
664 val = init_let_val(PSI_LET_CALLOC, alloc);
666 let_val(val) ::= let_func(func). {
667 val = init_let_val(PSI_LET_FUNC, func);
670 %type let_calloc {let_calloc*}
671 %destructor let_calloc {free_let_calloc($$);}
672 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
673 alloc = init_let_calloc(nmemb, size);
675 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
676 %type let_func {let_func*}
677 %destructor let_func {free_let_func($$);}
678 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
679 func = init_let_func(T->type, T->text, var);
684 %type set_stmt {set_stmt*}
685 %destructor set_stmt {free_set_stmt($$);}
686 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
687 set = init_set_stmt(var, val);
690 %type set_value {set_value*}
691 %destructor set_value {free_set_value($$);}
692 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
693 val = init_set_value(func, init_decl_vars(var));
695 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
696 val = init_set_value(func, init_decl_vars(var));
699 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
700 free_set_func(func_);
701 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
702 val->func->token = T;
704 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
707 val->vars = init_decl_vars(var);
709 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
713 val->vars = init_decl_vars(var);
715 %type set_vals {set_value*}
716 %destructor set_vals {free_set_value($$);}
717 set_vals(vals) ::= set_value(val). {
718 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
720 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
721 vals = add_inner_set_value(vals_, val);
724 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
725 %type set_func {set_func*}
726 %destructor set_func {free_set_func($$);}
727 set_func(func) ::= set_func_token(T). {
728 func = init_set_func(T->type, T->text);
732 %type return_stmt {return_stmt*}
733 %destructor return_stmt {free_return_stmt($$);}
734 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
735 ret = init_return_stmt(val);
739 %type free_stmt {free_stmt*}
740 %destructor free_stmt {free_free_stmt($$);}
741 free_stmt(free) ::= FREE free_calls(calls) EOS. {
742 free = init_free_stmt(calls);
745 %type free_calls {free_calls*}
746 %destructor free_calls {free_free_calls($$);}
747 free_calls(calls) ::= free_call(call). {
748 calls = init_free_calls(call);
750 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
751 calls = add_free_call(calls_, call);
754 %type free_call {free_call*}
755 %destructor free_call {free_free_call($$);}
756 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
757 call = init_free_call(F->text, vars);
761 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
762 %type impl_type {impl_type*}
763 %destructor impl_type {free_impl_type($$);}
764 impl_type(type_) ::= impl_type_token(T). {
765 type_ = init_impl_type(T->type, T->text);
769 %type reference {char}
770 reference(r) ::= . {r = 0;}
771 reference(r) ::= AMPERSAND. {r = 1;}
773 %type indirection {unsigned}
774 indirection(i) ::= . {i = 0;}
775 indirection(i) ::= pointers(p). {i = p;}
777 %type pointers {unsigned}
778 pointers(p) ::= ASTERISK. {p = 1;}
779 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}