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(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 block ::= constant(constant). {
65 P->consts = add_constant(P->consts, constant);
67 block ::= decl_struct(strct). {
68 P->structs = add_decl_struct(P->structs, strct);
71 struct_name(n) ::= STRUCT NAME(N). {
75 %type decl_struct_args {decl_args*}
76 %destructor decl_struct_args {free_decl_args($$);}
77 decl_struct_args(args_) ::= LBRACE struct_args(args) RBRACE. {
80 decl_struct_args(args_) ::= EOS. {
81 args_ = init_decl_args(NULL);
85 %type decl_struct {decl_struct*}
86 %destructor decl_struct {free_decl_struct($$);}
87 decl_struct(strct) ::= struct_name(N) struct_size(size_) decl_struct_args(args). {
88 strct = init_decl_struct(N->text, args);
93 %type struct_size {size_t}
94 struct_size(size) ::= . {
97 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
98 size = atol(SIZ->text);
102 %token_class const_type_token BOOL INT FLOAT STRING.
103 %type const_type {const_type*}
104 %destructor const_type {free_const_type($$);}
105 const_type(type_) ::= const_type_token(T). {
106 type_ = init_const_type(T->type, T->text);
109 %type constant {constant*}
110 %destructor constant {free_constant($$);}
111 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
112 constant = init_constant(type, T->text, val);
116 %type decl_typedef {decl_typedef*}
117 %destructor decl_typedef {free_decl_typedef($$);}
118 decl_typedef(def) ::= TYPEDEF decl_typedef_body(def_) EOS. {
121 %type decl_typedef_body {decl_typedef*}
122 %destructor decl_typedef_body {free_decl_typedef($$);}
123 decl_typedef_body(def) ::= decl_type(type) NAME(ALIAS). {
124 def = init_decl_typedef(ALIAS->text, type);
128 /* support opaque types */
129 decl_typedef_body(def) ::= VOID(V) indirection(i) NAME(ALIAS). {
130 def = init_decl_typedef(ALIAS->text, init_decl_type(i?PSI_T_POINTER:V->type, V->text));
132 def->type->token = V;
136 %destructor decl {free_decl($$);}
137 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
138 decl = init_decl(abi, func, args);
141 %type decl_func {decl_arg*}
142 %destructor decl_func {free_decl_arg($$);}
143 decl_func(func) ::= decl_arg(arg). {
146 /* special case for void functions */
147 decl_func(func) ::= VOID(T) NAME(N). {
148 func = init_decl_arg(
149 init_decl_type(T->type, T->text),
150 init_decl_var(N->text, 0, 0)
152 func->type->token = T;
157 %type decl_abi {decl_abi*}
158 %destructor decl_abi {free_decl_abi($$);}
159 decl_abi(abi) ::= NAME(T). {
160 abi = init_decl_abi(T->text);
164 %type decl_var {decl_var*}
165 %destructor decl_var {free_decl_var($$);}
166 decl_var(var) ::= indirection(p) NAME(T). {
167 var = init_decl_var(T->text, p, 0);
170 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
171 var = init_decl_var(T->text, p+1, atol(D->text));
176 %type decl_vars {decl_vars*}
177 %destructor decl_vars {free_decl_vars($$);}
178 decl_vars(vars) ::= decl_var(var). {
179 vars = init_decl_vars(var);
181 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
182 vars = add_decl_var(vars_, var);
185 %type decl_arg {decl_arg*}
186 %destructor decl_arg {free_decl_arg($$);}
187 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
188 arg_ = init_decl_arg(type, var);
190 /* void pointers need a specific rule */
191 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
192 arg_ = init_decl_arg(
193 init_decl_type(T->type, T->text),
194 init_decl_var(N->text, p, 0)
196 arg_->type->token = T;
197 arg_->var->token = N;
200 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
201 arg_ = init_decl_arg(
202 init_decl_type(T->type, T->text),
203 init_decl_var(N->text, p, 0)
205 arg_->type->token = T;
206 arg_->var->token = N;
210 %type decl_args {decl_args*}
211 %destructor decl_args {free_decl_args($$);}
214 decl_args(args) ::= decl_arg(arg). {
215 args = init_decl_args(arg);
217 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
218 args = add_decl_arg(args_, arg);
220 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
224 %type struct_args {decl_args*}
225 %destructor struct_args {free_decl_args($$);}
226 struct_args(args) ::= struct_arg(arg). {
227 args = init_decl_args(arg);
229 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
230 args = add_decl_arg(args_, arg);
232 %type struct_arg {decl_arg*}
233 %destructor struct_arg {free_decl_arg($$);}
234 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
235 arg_->layout = layout_;
239 %type struct_layout {decl_struct_layout*}
240 %destructor struct_layout {free_decl_struct_layout($$);}
241 struct_layout(layout) ::= . {
244 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
245 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
250 /* un/signed, urgh */
251 decl_scalar_type(type_) ::= CHAR(C). {
254 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
256 type_ = PSI_TokenCat(2, S, s);
263 decl_scalar_type_short(s) ::= . {
267 decl_scalar_type_short(s) ::= INT(I). {
270 decl_scalar_type(type_) ::= INT(I). {
273 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
275 type_ = PSI_TokenCat(2, L, l);
282 decl_scalar_type_long(l) ::= . {
285 decl_scalar_type_long(l) ::= DOUBLE(D). {
288 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
290 l = PSI_TokenCat(2, L, ll);
297 decl_scalar_type_long_long(ll) ::= . {
300 decl_scalar_type_long_long(ll) ::= INT(I). {
303 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
304 PSI_Token *T = PSI_TokenCat(2, U, N);
305 type_ = init_decl_type(T->type, T->text);
310 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
311 PSI_Token *T = PSI_TokenCat(2, S, N);
312 type_ = init_decl_type(T->type, T->text);
317 decl_type(type_) ::= UNSIGNED(U). {
318 type_ = init_decl_type(PSI_T_NAME, U->text);
321 decl_type(type_) ::= SIGNED(S). {
322 type_ = init_decl_type(PSI_T_NAME, S->text);
325 decl_type(type_) ::= decl_scalar_type(N). {
326 type_ = init_decl_type(N->type, N->text);
330 decl_type(type_) ::= STRUCT(S) NAME(T). {
331 type_ = init_decl_type(S->type, T->text);
335 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
336 %type decl_type {decl_type*}
337 %destructor decl_type {free_decl_type($$);}
338 decl_type(type_) ::= decl_type_token(T). {
339 type_ = init_decl_type(T->type, T->text);
344 %type const_decl_type {decl_type*}
345 %destructor const_decl_type {free_decl_type($$);}
346 const_decl_type(type) ::= decl_type(type_). {
349 const_decl_type(type) ::= CONST decl_type(type_). {
354 %destructor impl {free_impl($$);}
355 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
356 impl = init_impl(func, stmts);
359 %type impl_func {impl_func*}
360 %destructor impl_func {free_impl_func($$);}
361 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
362 func = init_impl_func(NAME->text, args, type, r);
366 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
367 %type impl_def_val {impl_def_val*}
368 %destructor impl_def_val {free_impl_def_val($$);}
369 impl_def_val(def) ::= impl_def_val_token(T). {
370 def = init_impl_def_val(T->type, T->text);
374 %type impl_var {impl_var*}
375 %destructor impl_var {free_impl_var($$);}
376 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
377 var = init_impl_var(T->text, r);
381 %type impl_arg {impl_arg*}
382 %destructor impl_arg {free_impl_arg($$);}
383 impl_arg(arg) ::= impl_type(type) impl_var(var). {
384 arg = init_impl_arg(type, var, NULL);
386 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
387 arg = init_impl_arg(type, var, def);
390 %type impl_args {impl_args*}
391 %destructor impl_args {free_impl_args($$);}
392 impl_args(args) ::= LPAREN RPAREN. {
395 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
398 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
400 args->vararg.name = va;
403 %type impl_vararg {impl_arg*}
404 %destructor impl_vararg {free_impl_arg($$);}
405 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
406 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
410 %type impl_arg_list {impl_args*}
411 %destructor impl_arg_list {free_impl_args($$);}
412 impl_arg_list(args) ::= impl_arg(arg). {
413 args = init_impl_args(arg);
415 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
416 args = add_impl_arg(args_, arg);
419 %type impl_stmts {impl_stmts*}
420 %destructor impl_stmts {free_impl_stmts($$);}
421 impl_stmts(stmts) ::= impl_stmt(stmt). {
422 stmts = init_impl_stmts(stmt);
424 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
425 stmts = add_impl_stmt(stmts_, stmt);
428 %type impl_stmt {impl_stmt*}
429 %destructor impl_stmt {free_impl_stmt($$);}
430 impl_stmt(stmt) ::= let_stmt(let). {
431 stmt = init_impl_stmt(PSI_T_LET, let);
433 impl_stmt(stmt) ::= set_stmt(set). {
434 stmt = init_impl_stmt(PSI_T_SET, set);
436 impl_stmt(stmt) ::= return_stmt(ret). {
437 stmt = init_impl_stmt(PSI_T_RETURN, ret);
439 impl_stmt(stmt) ::= free_stmt(free). {
440 stmt = init_impl_stmt(PSI_T_FREE, free);
443 %token_class num_exp_token NUMBER NSNAME.
444 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
445 %type num_exp {num_exp*}
446 %destructor num_exp {free_num_exp($$);}
447 num_exp(exp) ::= num_exp_token(tok). {
448 exp = init_num_exp(tok->type, tok->text);
451 num_exp(exp) ::= decl_var(var). {
452 exp = init_num_exp(PSI_T_NAME, var);
453 exp->token = PSI_TokenCopy(var->token);
455 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
456 exp_->operator = operator_->type;
457 exp_->operand = operand_;
462 %type let_stmt {let_stmt*}
463 %destructor let_stmt {free_let_stmt($$);}
464 let_stmt(let) ::= LET decl_var(var) EOS. {
465 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
467 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
468 val->flags.one.is_reference = r ? 1 : 0;
469 let = init_let_stmt(var, val);
471 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
472 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
475 %type let_val {let_val*}
476 %destructor let_val {free_let_val($$);}
477 let_val(val) ::= NULL. {
478 val = init_let_val(PSI_LET_NULL, NULL);
480 let_val(val) ::= num_exp(exp). {
481 val = init_let_val(PSI_LET_NUMEXP, exp);
483 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
484 val = init_let_val(PSI_LET_CALLOC, alloc);
486 let_val(val) ::= let_func(func). {
487 val = init_let_val(PSI_LET_FUNC, func);
490 %type let_calloc {let_calloc*}
491 %destructor let_calloc {free_let_calloc($$);}
492 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
493 alloc = init_let_calloc(nmemb, size);
495 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
496 %type let_func {let_func*}
497 %destructor let_func {free_let_func($$);}
498 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
499 func = init_let_func(T->type, T->text, var);
504 %type set_stmt {set_stmt*}
505 %destructor set_stmt {free_set_stmt($$);}
506 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
507 set = init_set_stmt(var, val);
510 %type set_value {set_value*}
511 %destructor set_value {free_set_value($$);}
512 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
513 val = init_set_value(func, init_decl_vars(var));
515 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
516 val = init_set_value(func, init_decl_vars(var));
519 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
520 free_set_func(func_);
521 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
522 val->func->token = T;
524 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
527 val->vars = init_decl_vars(var);
529 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
533 val->vars = init_decl_vars(var);
535 %type set_vals {set_value*}
536 %destructor set_vals {free_set_value($$);}
537 set_vals(vals) ::= set_value(val). {
538 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
540 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
541 vals = add_inner_set_value(vals_, val);
544 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
545 %type set_func {set_func*}
546 %destructor set_func {free_set_func($$);}
547 set_func(func) ::= set_func_token(T). {
548 func = init_set_func(T->type, T->text);
552 %type return_stmt {return_stmt*}
553 %destructor return_stmt {free_return_stmt($$);}
554 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
555 ret = init_return_stmt(val);
559 %type free_stmt {free_stmt*}
560 %destructor free_stmt {free_free_stmt($$);}
561 free_stmt(free) ::= FREE free_calls(calls) EOS. {
562 free = init_free_stmt(calls);
565 %type free_calls {free_calls*}
566 %destructor free_calls {free_free_calls($$);}
567 free_calls(calls) ::= free_call(call). {
568 calls = init_free_calls(call);
570 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
571 calls = add_free_call(calls_, call);
574 %type free_call {free_call*}
575 %destructor free_call {free_free_call($$);}
576 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
577 call = init_free_call(F->text, vars);
581 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
582 %type impl_type {impl_type*}
583 %destructor impl_type {free_impl_type($$);}
584 impl_type(type_) ::= impl_type_token(T). {
585 type_ = init_impl_type(T->type, T->text);
589 %type reference {char}
590 reference(r) ::= . {r = 0;}
591 reference(r) ::= AMPERSAND. {r = 1;}
593 %type indirection {unsigned}
594 indirection(i) ::= . {i = 0;}
595 indirection(i) ::= pointers(p). {i = p;}
597 %type pointers {unsigned}
598 pointers(p) ::= ASTERISK. {p = 1;}
599 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}