8 void psi_error(int, const char *, int, const char *, ...);
13 %token_type {PSI_Token *}
14 %token_destructor {free($$);}
15 %default_destructor {(void)P;}
16 %extra_argument {PSI_Parser *P}
17 /* TOKEN is defined inside syntax_error */
20 if (TOKEN && TOKEN->type != PSI_T_EOF) {
21 psi_error(PSI_WARNING, TOKEN->file, *TOKEN->line, "PSI syntax error: Unexpected token '%s'", TOKEN->text);
23 psi_error(PSI_WARNING, P->psi.file.fn, P->line, "PSI syntax error: Unexpected end of input");
30 %fallback NAME TEMP FREE SET LET RETURN LIB INT UNSIGNED.
35 blocks ::= blocks block.
39 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
41 P->error(T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
43 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
49 block ::= decl(decl). {
50 P->decls = add_decl(P->decls, decl);
52 block ::= impl(impl). {
53 P->impls = add_impl(P->impls, impl);
55 block ::= decl_typedef(def). {
56 P->defs = add_decl_typedef(P->defs, def);
57 if (def->type->strct) {
58 P->structs = add_decl_struct(P->structs, def->type->strct);
61 block ::= constant(constant). {
62 P->consts = add_constant(P->consts, constant);
64 block ::= decl_struct(strct). {
65 P->structs = add_decl_struct(P->structs, strct);
68 %type decl_struct {decl_struct*}
69 %destructor decl_struct {free_decl_struct($$);}
70 decl_struct(strct) ::= STRUCT NAME(N) struct_size(size_) LBRACE struct_args(args) RBRACE. {
71 strct = init_decl_struct(N->text, args);
76 %type struct_size {size_t}
77 struct_size(size) ::= . {
80 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
81 size = atol(SIZ->text);
85 %token_class const_type_token BOOL INT FLOAT STRING.
86 %type const_type {const_type*}
87 %destructor const_type {free_const_type($$);}
88 const_type(type_) ::= const_type_token(T). {
89 type_ = init_const_type(T->type, T->text);
92 %type constant {constant*}
93 %destructor constant {free_constant($$);}
94 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
95 constant = init_constant(type, T->text, val);
99 %type decl_typedef {decl_typedef*}
100 %destructor decl_typedef {free_decl_typedef($$);}
101 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
102 def = init_decl_typedef(ALIAS->text, type);
105 /* support opaque types */
106 decl_typedef(def) ::= TYPEDEF VOID(V) NAME(ALIAS) EOS. {
107 def = init_decl_typedef(ALIAS->text, init_decl_type(V->type, V->text));
109 def->type->token = V;
111 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
112 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
114 def->type->token = N;
117 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
118 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
120 def->type->token = PSI_TokenCopy(s->token);
121 def->type->strct = s;
125 %destructor decl {free_decl($$);}
126 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
127 decl = init_decl(abi, func, args);
130 %type decl_func {decl_arg*}
131 %destructor decl_func {free_decl_arg($$);}
132 decl_func(func) ::= decl_arg(arg). {
135 /* special case for void functions */
136 decl_func(func) ::= VOID(T) NAME(N). {
137 func = init_decl_arg(
138 init_decl_type(T->type, T->text),
139 init_decl_var(N->text, 0, 0)
141 func->type->token = T;
146 %type decl_abi {decl_abi*}
147 %destructor decl_abi {free_decl_abi($$);}
148 decl_abi(abi) ::= NAME(T). {
149 abi = init_decl_abi(T->text);
153 %type decl_var {decl_var*}
154 %destructor decl_var {free_decl_var($$);}
155 decl_var(var) ::= indirection(p) NAME(T). {
156 var = init_decl_var(T->text, p, 0);
159 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
160 var = init_decl_var(T->text, p+1, atol(D->text));
165 %type decl_vars {decl_vars*}
166 %destructor decl_vars {free_decl_vars($$);}
167 decl_vars(vars) ::= decl_var(var). {
168 vars = init_decl_vars(var);
170 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
171 vars = add_decl_var(vars_, var);
174 %type decl_arg {decl_arg*}
175 %destructor decl_arg {free_decl_arg($$);}
176 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
177 arg_ = init_decl_arg(type, var);
179 /* void pointers need a specific rule */
180 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
181 arg_ = init_decl_arg(
182 init_decl_type(T->type, T->text),
183 init_decl_var(N->text, p, 0)
185 arg_->type->token = T;
186 arg_->var->token = N;
189 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
190 arg_ = init_decl_arg(
191 init_decl_type(T->type, T->text),
192 init_decl_var(N->text, p, 0)
194 arg_->type->token = T;
195 arg_->var->token = N;
199 %type decl_args {decl_args*}
200 %destructor decl_args {free_decl_args($$);}
203 decl_args(args) ::= decl_arg(arg). {
204 args = init_decl_args(arg);
206 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
207 args = add_decl_arg(args_, arg);
209 decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. {
213 %type struct_args {decl_args*}
214 %destructor struct_args {free_decl_args($$);}
215 struct_args(args) ::= struct_arg(arg). {
216 args = init_decl_args(arg);
218 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
219 args = add_decl_arg(args_, arg);
221 %type struct_arg {decl_arg*}
222 %destructor struct_arg {free_decl_arg($$);}
223 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
224 arg_->layout = layout_;
228 %type struct_layout {decl_struct_layout*}
229 %destructor struct_layout {free_decl_struct_layout($$);}
230 struct_layout(layout) ::= . {
233 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
234 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
239 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
240 %type decl_type {decl_type*}
241 %destructor decl_type {free_decl_type($$);}
242 decl_type(type_) ::= decl_type_token(T). {
243 type_ = init_decl_type(T->type, T->text);
247 decl_type(type_) ::= UNSIGNED NAME(T). {
248 type_ = init_decl_type(T->type, T->text);
250 type_->name = realloc(type_->name, T->size + sizeof("unsigned"));
251 memmove(type_->name + sizeof("unsigned"), type_->name, T->size);
252 memcpy(type_->name, "unsigned", sizeof("unsigned")-1);
253 type_->name[sizeof("unsigned")] = ' ';
254 type_->name[T->size + sizeof("unsigned")] = 0;
256 /* we have to support plain int here because we have it in our lexer rules */
257 decl_type(type_) ::= INT(T). {
258 type_ = init_decl_type(PSI_T_NAME, T->text);
262 decl_type(type_) ::= STRUCT(S) NAME(T). {
263 type_ = init_decl_type(S->type, T->text);
268 %type const_decl_type {decl_type*}
269 %destructor const_decl_type {free_decl_type($$);}
270 const_decl_type(type) ::= decl_type(type_). {
273 const_decl_type(type) ::= CONST decl_type(type_). {
278 %destructor impl {free_impl($$);}
279 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
280 impl = init_impl(func, stmts);
283 %type impl_func {impl_func*}
284 %destructor impl_func {free_impl_func($$);}
285 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). {
286 func = init_impl_func(NAME->text, args, type, r);
290 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
291 %type impl_def_val {impl_def_val*}
292 %destructor impl_def_val {free_impl_def_val($$);}
293 impl_def_val(def) ::= impl_def_val_token(T). {
294 def = init_impl_def_val(T->type, T->text);
298 %type impl_var {impl_var*}
299 %destructor impl_var {free_impl_var($$);}
300 impl_var(var) ::= reference(r) DOLLAR NAME(T). {
301 var = init_impl_var(T->text, r);
305 %type impl_arg {impl_arg*}
306 %destructor impl_arg {free_impl_arg($$);}
307 impl_arg(arg) ::= impl_type(type) impl_var(var). {
308 arg = init_impl_arg(type, var, NULL);
310 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
311 arg = init_impl_arg(type, var, def);
314 %type impl_args {impl_args*}
315 %destructor impl_args {free_impl_args($$);}
316 impl_args(args) ::= LPAREN RPAREN. {
319 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
322 impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
324 args->vararg.name = va;
327 %type impl_vararg {impl_arg*}
328 %destructor impl_vararg {free_impl_arg($$);}
329 impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
330 va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
334 %type impl_arg_list {impl_args*}
335 %destructor impl_arg_list {free_impl_args($$);}
336 impl_arg_list(args) ::= impl_arg(arg). {
337 args = init_impl_args(arg);
339 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
340 args = add_impl_arg(args_, arg);
343 %type impl_stmts {impl_stmts*}
344 %destructor impl_stmts {free_impl_stmts($$);}
345 impl_stmts(stmts) ::= impl_stmt(stmt). {
346 stmts = init_impl_stmts(stmt);
348 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
349 stmts = add_impl_stmt(stmts_, stmt);
352 %type impl_stmt {impl_stmt*}
353 %destructor impl_stmt {free_impl_stmt($$);}
354 impl_stmt(stmt) ::= let_stmt(let). {
355 stmt = init_impl_stmt(PSI_T_LET, let);
357 impl_stmt(stmt) ::= set_stmt(set). {
358 stmt = init_impl_stmt(PSI_T_SET, set);
360 impl_stmt(stmt) ::= return_stmt(ret). {
361 stmt = init_impl_stmt(PSI_T_RETURN, ret);
363 impl_stmt(stmt) ::= free_stmt(free). {
364 stmt = init_impl_stmt(PSI_T_FREE, free);
367 %token_class num_exp_token NUMBER NSNAME.
368 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
369 %type num_exp {num_exp*}
370 %destructor num_exp {free_num_exp($$);}
371 num_exp(exp) ::= num_exp_token(tok). {
372 exp = init_num_exp(tok->type, tok->text);
375 num_exp(exp) ::= decl_var(var). {
376 exp = init_num_exp(PSI_T_NAME, var);
377 exp->token = PSI_TokenCopy(var->token);
379 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
380 exp_->operator = operator_->type;
381 exp_->operand = operand_;
386 %type let_stmt {let_stmt*}
387 %destructor let_stmt {free_let_stmt($$);}
388 let_stmt(let) ::= LET decl_var(var) EOS. {
389 let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL));
391 let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. {
392 val->flags.one.is_reference = r ? 1 : 0;
393 let = init_let_stmt(var, val);
395 let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
396 let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
399 %type let_val {let_val*}
400 %destructor let_val {free_let_val($$);}
401 let_val(val) ::= NULL. {
402 val = init_let_val(PSI_LET_NULL, NULL);
404 let_val(val) ::= num_exp(exp). {
405 val = init_let_val(PSI_LET_NUMEXP, exp);
407 let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
408 val = init_let_val(PSI_LET_CALLOC, alloc);
410 let_val(val) ::= let_func(func). {
411 val = init_let_val(PSI_LET_FUNC, func);
414 %type let_calloc {let_calloc*}
415 %destructor let_calloc {free_let_calloc($$);}
416 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
417 alloc = init_let_calloc(nmemb, size);
419 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
420 %type let_func {let_func*}
421 %destructor let_func {free_let_func($$);}
422 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
423 func = init_let_func(T->type, T->text, var);
428 %type set_stmt {set_stmt*}
429 %destructor set_stmt {free_set_stmt($$);}
430 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
431 set = init_set_stmt(var, val);
434 %type set_value {set_value*}
435 %destructor set_value {free_set_value($$);}
436 set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. {
437 val = init_set_value(func, init_decl_vars(var));
439 set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. {
440 val = init_set_value(func, init_decl_vars(var));
443 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. {
444 free_set_func(func_);
445 val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var));
446 val->func->token = T;
448 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. {
451 val->vars = init_decl_vars(var);
453 set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. {
457 val->vars = init_decl_vars(var);
459 %type set_vals {set_value*}
460 %destructor set_vals {free_set_value($$);}
461 set_vals(vals) ::= set_value(val). {
462 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
464 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
465 vals = add_inner_set_value(vals_, val);
468 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
469 %type set_func {set_func*}
470 %destructor set_func {free_set_func($$);}
471 set_func(func) ::= set_func_token(T). {
472 func = init_set_func(T->type, T->text);
476 %type return_stmt {return_stmt*}
477 %destructor return_stmt {free_return_stmt($$);}
478 return_stmt(ret) ::= RETURN(T) set_value(val) EOS. {
479 ret = init_return_stmt(val);
483 %type free_stmt {free_stmt*}
484 %destructor free_stmt {free_free_stmt($$);}
485 free_stmt(free) ::= FREE free_calls(calls) EOS. {
486 free = init_free_stmt(calls);
489 %type free_calls {free_calls*}
490 %destructor free_calls {free_free_calls($$);}
491 free_calls(calls) ::= free_call(call). {
492 calls = init_free_calls(call);
494 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
495 calls = add_free_call(calls_, call);
498 %type free_call {free_call*}
499 %destructor free_call {free_free_call($$);}
500 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
501 call = init_free_call(F->text, vars);
505 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
506 %type impl_type {impl_type*}
507 %destructor impl_type {free_impl_type($$);}
508 impl_type(type_) ::= impl_type_token(T). {
509 type_ = init_impl_type(T->type, T->text);
513 %type reference {char}
514 reference(r) ::= . {r = 0;}
515 reference(r) ::= AMPERSAND. {r = 1;}
517 %type indirection {unsigned}
518 indirection(i) ::= . {i = 0;}
519 indirection(i) ::= pointers(p). {i = p;}
521 %type pointers {unsigned}
522 pointers(p) ::= ASTERISK. {p = 1;}
523 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}