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 block ::= constant(constant). {
66 P->consts = add_constant(P->consts, constant);
68 block ::= decl_struct(strct). {
69 P->structs = add_decl_struct(P->structs, strct);
71 block ::= decl_enum(e). {
72 P->enums = add_decl_enum(P->enums, e);
75 optional_name(n) ::= .{
78 optional_name(n) ::= NAME(N). {
81 enum_name(n) ::= ENUM(E) optional_name(N). {
88 PSI_TokenHash(E, digest);
89 n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@");
94 %type decl_enum {decl_enum *}
95 %destructor decl_enum {free_decl_enum($$);}
96 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
97 e = init_decl_enum(N->text, list);
101 %type decl_enum_items {decl_enum_items*}
102 %destructor decl_enum_items {free_decl_enum_items($$);}
103 decl_enum_items(l) ::= decl_enum_item(i). {
104 l = init_decl_enum_items(i);
106 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
107 l = add_decl_enum_item(l_, i);
110 %type decl_enum_item {decl_enum_item*}
111 %destructor decl_enum_item {free_decl_enum_item($$);}
112 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
113 i = init_decl_enum_item(N->text, num);
116 decl_enum_item(i) ::= NAME(N). {
117 i = init_decl_enum_item(N->text, NULL);
121 struct_name(n) ::= STRUCT(S) optional_name(N). {
128 PSI_TokenHash(S, digest);
129 n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@");
133 %type decl_struct_args_block {decl_args*}
134 %destructor decl_struct_args_block {free_decl_args($$);}
135 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
138 %type decl_struct_args {decl_args*}
139 %destructor decl_struct_args {free_decl_args($$);}
140 decl_struct_args(args_) ::= decl_struct_args_block(args). {
143 decl_struct_args(args_) ::= EOS. {
144 args_ = init_decl_args(NULL);
148 %type decl_struct {decl_struct*}
149 %destructor decl_struct {free_decl_struct($$);}
150 decl_struct(strct) ::= struct_name(N) struct_size(size_) decl_struct_args(args). {
151 strct = init_decl_struct(N->text, args);
156 %type struct_size {size_t}
157 struct_size(size) ::= . {
160 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
161 size = atol(SIZ->text);
165 %token_class const_type_token BOOL INT FLOAT STRING.
166 %type const_type {const_type*}
167 %destructor const_type {free_const_type($$);}
168 const_type(type_) ::= const_type_token(T). {
169 type_ = init_const_type(T->type, T->text);
172 %type constant {constant*}
173 %destructor constant {free_constant($$);}
174 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
175 constant = init_constant(type, T->text, val);
179 %type decl_typedef {decl_typedef*}
180 %destructor decl_typedef {free_decl_typedef($$);}
181 decl_typedef(def) ::= TYPEDEF decl_typedef_body(def_) EOS. {
184 %type decl_typedef_body_ex {decl_typedef*}
185 %destructor decl_typedef_body_ex {free_decl_typedef($$);}
186 decl_typedef_body_ex(def) ::= struct_name(N) struct_size(size_) decl_struct_args_block(args) NAME(ALIAS). {
187 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, N->text));
189 def->type->token = PSI_TokenCopy(N);
190 def->type->strct = init_decl_struct(N->text, args);
191 def->type->strct->token = N;
192 def->type->strct->size = size_;
194 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
195 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_ENUM, e->name));
196 def->type->token = PSI_TokenCopy(e->token);
200 %type decl_typedef_body {decl_typedef*}
201 %destructor decl_typedef_body {free_decl_typedef($$);}
202 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
205 decl_typedef_body(def) ::= decl_type(type) NAME(ALIAS). {
206 def = init_decl_typedef(ALIAS->text, type);
209 /* support opaque types */
210 decl_typedef_body(def) ::= VOID(V) indirection(i) NAME(ALIAS). {
211 def = init_decl_typedef(ALIAS->text, init_decl_type(i?PSI_T_POINTER:V->type, V->text));
213 def->type->token = V;
215 decl_typedef_body(def) ::= decl_func(func) LPAREN decl_args(args) RPAREN. {
216 def = init_decl_typedef(func->var->name, init_decl_type(PSI_T_FUNCTION, func->var->name));
217 def->type->token = PSI_TokenCopy(func->token);
218 def->type->func = init_decl(init_decl_abi("default"), func, args);
219 def->token = PSI_TokenCopy(func->token);
223 %destructor decl {free_decl($$);}
224 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
225 decl = init_decl(abi, func, args);
228 %type decl_func {decl_arg*}
229 %destructor decl_func {free_decl_arg($$);}
230 decl_func(func) ::= decl_arg(arg). {
233 /* special case for void functions */
234 decl_func(func) ::= VOID(T) NAME(N). {
235 func = init_decl_arg(
236 init_decl_type(T->type, T->text),
237 init_decl_var(N->text, 0, 0)
239 func->type->token = T;
244 %type decl_abi {decl_abi*}
245 %destructor decl_abi {free_decl_abi($$);}
246 decl_abi(abi) ::= NAME(T). {
247 abi = init_decl_abi(T->text);
251 %type decl_var {decl_var*}
252 %destructor decl_var {free_decl_var($$);}
253 decl_var(var) ::= indirection(p) NAME(T). {
254 var = init_decl_var(T->text, p, 0);
257 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
258 var = init_decl_var(T->text, p+1, atol(D->text));
263 %type decl_vars {decl_vars*}
264 %destructor decl_vars {free_decl_vars($$);}
265 decl_vars(vars) ::= decl_var(var). {
266 vars = init_decl_vars(var);
268 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
269 vars = add_decl_var(vars_, var);
272 %type decl_arg {decl_arg*}
273 %destructor decl_arg {free_decl_arg($$);}
274 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
275 arg_ = init_decl_arg(type, var);
277 /* void pointers need a specific rule */
278 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
279 arg_ = init_decl_arg(
280 init_decl_type(T->type, T->text),
281 init_decl_var(N->text, p, 0)
283 arg_->type->token = T;
284 arg_->var->token = N;
287 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
288 arg_ = init_decl_arg(
289 init_decl_type(T->type, T->text),
290 init_decl_var(N->text, p, 0)
292 arg_->type->token = T;
293 arg_->var->token = N;
297 %type decl_args {decl_args*}
298 %destructor decl_args {free_decl_args($$);}
301 decl_args(args) ::= decl_arg(arg). {
302 args = init_decl_args(arg);
304 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
305 args = add_decl_arg(args_, arg);
307 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
311 %type struct_args {decl_args*}
312 %destructor struct_args {free_decl_args($$);}
313 struct_args(args) ::= struct_arg(arg). {
314 args = init_decl_args(arg);
316 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
317 args = add_decl_arg(args_, arg);
319 %type struct_arg {decl_arg*}
320 %destructor struct_arg {free_decl_arg($$);}
321 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
322 arg_ = init_decl_arg(def->type, init_decl_var(def->alias, 0, 0));
323 arg_->var->arg = arg_;
324 arg_->token = PSI_TokenCopy(def->type->token);
325 arg_->var->token = def->token;
329 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
330 arg_->layout = layout_;
334 %type struct_layout {decl_struct_layout*}
335 %destructor struct_layout {free_decl_struct_layout($$);}
336 struct_layout(layout) ::= . {
339 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
340 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
345 /* un/signed, urgh */
346 decl_scalar_type(type_) ::= CHAR(C). {
349 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
351 type_ = PSI_TokenCat(2, S, s);
358 decl_scalar_type_short(s) ::= . {
362 decl_scalar_type_short(s) ::= INT(I). {
365 decl_scalar_type(type_) ::= INT(I). {
368 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
370 type_ = PSI_TokenCat(2, L, l);
377 decl_scalar_type_long(l) ::= . {
380 decl_scalar_type_long(l) ::= DOUBLE(D). {
383 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
385 l = PSI_TokenCat(2, L, ll);
392 decl_scalar_type_long_long(ll) ::= . {
395 decl_scalar_type_long_long(ll) ::= INT(I). {
398 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
399 PSI_Token *T = PSI_TokenCat(2, U, N);
400 type_ = init_decl_type(T->type, T->text);
405 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
406 PSI_Token *T = PSI_TokenCat(2, S, N);
407 type_ = init_decl_type(T->type, T->text);
412 decl_type(type_) ::= UNSIGNED(U). {
413 type_ = init_decl_type(PSI_T_NAME, U->text);
416 decl_type(type_) ::= SIGNED(S). {
417 type_ = init_decl_type(PSI_T_NAME, S->text);
420 decl_type(type_) ::= decl_scalar_type(N). {
421 type_ = init_decl_type(N->type, N->text);
425 decl_type(type_) ::= STRUCT(S) NAME(T). {
426 type_ = init_decl_type(S->type, T->text);
430 decl_type(type_) ::= ENUM(E) NAME(T). {
431 type_ = init_decl_type(E->type, T->text);
435 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
436 %type decl_type {decl_type*}
437 %destructor decl_type {free_decl_type($$);}
438 decl_type(type_) ::= decl_type_token(T). {
439 type_ = init_decl_type(T->type, T->text);
444 %type const_decl_type {decl_type*}
445 %destructor const_decl_type {free_decl_type($$);}
446 const_decl_type(type) ::= decl_type(type_). {
449 const_decl_type(type) ::= CONST decl_type(type_). {
454 %destructor impl {free_impl($$);}
455 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
456 impl = init_impl(func, stmts);
459 %type impl_func {impl_func*}
460 %destructor impl_func {free_impl_func($$);}
461 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
462 func = init_impl_func(NAME->text, args, type, r);
466 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
467 %type impl_def_val {impl_def_val*}
468 %destructor impl_def_val {free_impl_def_val($$);}
469 impl_def_val(def) ::= impl_def_val_token(T). {
470 def = init_impl_def_val(T->type, T->text);
474 %type impl_var {impl_var*}
475 %destructor impl_var {free_impl_var($$);}
476 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
477 var = init_impl_var(T->text, r);
481 %type impl_arg {impl_arg*}
482 %destructor impl_arg {free_impl_arg($$);}
483 impl_arg(arg) ::= impl_type(type) impl_var(var). {
484 arg = init_impl_arg(type, var, NULL);
486 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
487 arg = init_impl_arg(type, var, def);
490 %type impl_args {impl_args*}
491 %destructor impl_args {free_impl_args($$);}
492 impl_args(args) ::= LPAREN RPAREN. {
495 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
498 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
500 args->vararg.name = va;
503 %type impl_vararg {impl_arg*}
504 %destructor impl_vararg {free_impl_arg($$);}
505 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
506 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
510 %type impl_arg_list {impl_args*}
511 %destructor impl_arg_list {free_impl_args($$);}
512 impl_arg_list(args) ::= impl_arg(arg). {
513 args = init_impl_args(arg);
515 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
516 args = add_impl_arg(args_, arg);
519 %type impl_stmts {impl_stmts*}
520 %destructor impl_stmts {free_impl_stmts($$);}
521 impl_stmts(stmts) ::= impl_stmt(stmt). {
522 stmts = init_impl_stmts(stmt);
524 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
525 stmts = add_impl_stmt(stmts_, stmt);
528 %type impl_stmt {impl_stmt*}
529 %destructor impl_stmt {free_impl_stmt($$);}
530 impl_stmt(stmt) ::= let_stmt(let). {
531 stmt = init_impl_stmt(PSI_T_LET, let);
533 impl_stmt(stmt) ::= set_stmt(set). {
534 stmt = init_impl_stmt(PSI_T_SET, set);
536 impl_stmt(stmt) ::= return_stmt(ret). {
537 stmt = init_impl_stmt(PSI_T_RETURN, ret);
539 impl_stmt(stmt) ::= free_stmt(free). {
540 stmt = init_impl_stmt(PSI_T_FREE, free);
543 %token_class num_exp_token NUMBER NSNAME.
544 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
545 %type num_exp {num_exp*}
546 %destructor num_exp {free_num_exp($$);}
547 num_exp(exp) ::= num_exp_token(tok). {
548 exp = init_num_exp(tok->type, tok->text);
551 num_exp(exp) ::= decl_var(var). {
552 exp = init_num_exp(PSI_T_NAME, var);
553 exp->token = PSI_TokenCopy(var->token);
555 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
556 exp_->operator = operator_->type;
557 exp_->operand = operand_;
562 %type let_stmt {let_stmt*}
563 %destructor let_stmt {free_let_stmt($$);}
564 let_stmt(let) ::= LET decl_var(var) EOS. {
565 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
567 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
568 val->flags.one.is_reference = r ? 1 : 0;
569 let = init_let_stmt(var, val);
571 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
572 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
575 %type let_val {let_val*}
576 %destructor let_val {free_let_val($$);}
577 let_val(val) ::= NULL. {
578 val = init_let_val(PSI_LET_NULL, NULL);
580 let_val(val) ::= num_exp(exp). {
581 val = init_let_val(PSI_LET_NUMEXP, exp);
583 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
584 val = init_let_val(PSI_LET_CALLOC, alloc);
586 let_val(val) ::= let_func(func). {
587 val = init_let_val(PSI_LET_FUNC, func);
590 %type let_calloc {let_calloc*}
591 %destructor let_calloc {free_let_calloc($$);}
592 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
593 alloc = init_let_calloc(nmemb, size);
595 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
596 %type let_func {let_func*}
597 %destructor let_func {free_let_func($$);}
598 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
599 func = init_let_func(T->type, T->text, var);
604 %type set_stmt {set_stmt*}
605 %destructor set_stmt {free_set_stmt($$);}
606 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
607 set = init_set_stmt(var, val);
610 %type set_value {set_value*}
611 %destructor set_value {free_set_value($$);}
612 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
613 val = init_set_value(func, init_decl_vars(var));
615 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
616 val = init_set_value(func, init_decl_vars(var));
619 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
620 free_set_func(func_);
621 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
622 val->func->token = T;
624 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
627 val->vars = init_decl_vars(var);
629 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
633 val->vars = init_decl_vars(var);
635 %type set_vals {set_value*}
636 %destructor set_vals {free_set_value($$);}
637 set_vals(vals) ::= set_value(val). {
638 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
640 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
641 vals = add_inner_set_value(vals_, val);
644 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
645 %type set_func {set_func*}
646 %destructor set_func {free_set_func($$);}
647 set_func(func) ::= set_func_token(T). {
648 func = init_set_func(T->type, T->text);
652 %type return_stmt {return_stmt*}
653 %destructor return_stmt {free_return_stmt($$);}
654 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
655 ret = init_return_stmt(val);
659 %type free_stmt {free_stmt*}
660 %destructor free_stmt {free_free_stmt($$);}
661 free_stmt(free) ::= FREE free_calls(calls) EOS. {
662 free = init_free_stmt(calls);
665 %type free_calls {free_calls*}
666 %destructor free_calls {free_free_calls($$);}
667 free_calls(calls) ::= free_call(call). {
668 calls = init_free_calls(call);
670 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
671 calls = add_free_call(calls_, call);
674 %type free_call {free_call*}
675 %destructor free_call {free_free_call($$);}
676 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
677 call = init_free_call(F->text, vars);
681 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
682 %type impl_type {impl_type*}
683 %destructor impl_type {free_impl_type($$);}
684 impl_type(type_) ::= impl_type_token(T). {
685 type_ = init_impl_type(T->type, T->text);
689 %type reference {char}
690 reference(r) ::= . {r = 0;}
691 reference(r) ::= AMPERSAND. {r = 1;}
693 %type indirection {unsigned}
694 indirection(i) ::= . {i = 0;}
695 indirection(i) ::= pointers(p). {i = p;}
697 %type pointers {unsigned}
698 pointers(p) ::= ASTERISK. {p = 1;}
699 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}