12 %token_type {PSI_Token *}
13 %token_destructor {free($$);}
14 %default_destructor {(void)P;}
15 %extra_argument {PSI_Parser *P}
16 /* TOKEN is defined inside syntax_error */
18 if (TOKEN && TOKEN->type != PSI_T_EOF) {
19 PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text);
21 PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected end of input");
26 %fallback NAME FREE SET LET RETURN LIB INT UNSIGNED.
31 blocks ::= blocks block.
35 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
37 PSI_ParserSyntaxError(P, P->psi.file.ln, T->line, "Extra 'lib %s' statement has no effect", libname->text);
39 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
45 block ::= decl(decl). {
46 P->decls = add_decl(P->decls, decl);
48 block ::= impl(impl). {
49 P->impls = add_impl(P->impls, impl);
51 block ::= decl_typedef(def). {
52 P->defs = add_decl_typedef(P->defs, def);
53 if (def->type->strct) {
54 P->structs = add_decl_struct(P->structs, def->type->strct);
57 block ::= constant(constant). {
58 P->consts = add_constant(P->consts, constant);
60 block ::= decl_struct(strct). {
61 P->structs = add_decl_struct(P->structs, strct);
64 %type decl_struct {decl_struct*}
65 %destructor decl_struct {free_decl_struct($$);}
66 decl_struct(strct) ::= STRUCT NAME(N) struct_size(size_) LBRACE struct_args(args) RBRACE. {
67 strct = init_decl_struct(N->text, args);
72 %type struct_size {size_t}
73 struct_size(size) ::= . {
76 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
77 size = atol(SIZ->text);
81 %token_class const_type_token BOOL INT FLOAT STRING.
82 %type const_type {const_type*}
83 %destructor const_type {free_const_type($$);}
84 const_type(type_) ::= const_type_token(T). {
85 type_ = init_const_type(T->type, T->text);
88 %type constant {constant*}
89 %destructor constant {free_constant($$);}
90 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
91 constant = init_constant(type, T->text, val);
95 %type decl_typedef {decl_typedef*}
96 %destructor decl_typedef {free_decl_typedef($$);}
97 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
98 def = init_decl_typedef(ALIAS->text, type);
101 /* support opaque types */
102 decl_typedef(def) ::= TYPEDEF VOID(V) NAME(ALIAS) EOS. {
103 def = init_decl_typedef(ALIAS->text, init_decl_type(V->type, V->text));
107 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
108 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
113 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
114 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
115 def->type->strct = s;
120 %destructor decl {free_decl($$);}
121 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
122 decl = init_decl(abi, func, args);
125 %type decl_func {decl_arg*}
126 %destructor decl_func {free_decl_arg($$);}
127 decl_func(func) ::= decl_arg(arg). {
130 /* special case for void functions */
131 decl_func(func) ::= VOID(T) NAME(N). {
132 func = init_decl_arg(
133 init_decl_type(T->type, T->text),
134 init_decl_var(N->text, 0, 0)
140 %type decl_abi {decl_abi*}
141 %destructor decl_abi {free_decl_abi($$);}
142 decl_abi(abi) ::= NAME(T). {
143 abi = init_decl_abi(T->text);
147 %type decl_var {decl_var*}
148 %destructor decl_var {free_decl_var($$);}
149 decl_var(var) ::= indirection(p) NAME(T). {
150 var = init_decl_var(T->text, p, 0);
153 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
154 var = init_decl_var(T->text, p+1, atol(D->text));
159 %type decl_vars {decl_vars*}
160 %destructor decl_vars {free_decl_vars($$);}
161 decl_vars(vars) ::= decl_var(var). {
162 vars = init_decl_vars(var);
164 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
165 vars = add_decl_var(vars_, var);
168 %type decl_arg {decl_arg*}
169 %destructor decl_arg {free_decl_arg($$);}
170 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
171 arg_ = init_decl_arg(type, var);
173 /* void pointers need a specific rule */
174 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
175 arg_ = init_decl_arg(
176 init_decl_type(T->type, T->text),
177 init_decl_var(N->text, p, 0)
182 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
183 arg_ = init_decl_arg(
184 init_decl_type(T->type, T->text),
185 init_decl_var(N->text, p, 0)
191 %type decl_args {decl_args*}
192 %destructor decl_args {free_decl_args($$);}
195 decl_args(args) ::= decl_arg(arg). {
196 args = init_decl_args(arg);
198 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
199 args = add_decl_arg(args_, arg);
201 %type struct_args {decl_args*}
202 %destructor struct_args {free_decl_args($$);}
203 struct_args(args) ::= struct_arg(arg). {
204 args = init_decl_args(arg);
206 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
207 args = add_decl_arg(args_, arg);
209 %type struct_arg {decl_arg*}
210 %destructor struct_arg {free_decl_arg($$);}
211 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
212 arg_->layout = layout_;
216 %type struct_layout {decl_struct_layout*}
217 %destructor struct_layout {free_decl_struct_layout($$);}
218 struct_layout(layout) ::= . {
221 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
222 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
227 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
228 %type decl_type {decl_type*}
229 %destructor decl_type {free_decl_type($$);}
230 decl_type(type_) ::= decl_type_token(T). {
231 type_ = init_decl_type(T->type, T->text);
235 decl_type(type_) ::= UNSIGNED NAME(T). {
236 type_ = init_decl_type(T->type, T->text);
237 type_->name = realloc(type_->name, T->size + sizeof("unsigned"));
238 memmove(type_->name + sizeof("unsigned"), type_->name, T->size);
239 memcpy(type_->name, "unsigned", sizeof("unsigned")-1);
240 type_->name[sizeof("unsigned")] = ' ';
241 type_->name[T->size + sizeof("unsigned")] = 0;
243 /* we have to support plain int here because we have it in our lexer rules */
244 decl_type(type_) ::= INT(T). {
245 type_ = init_decl_type(PSI_T_NAME, T->text);
249 decl_type(type_) ::= STRUCT(S) NAME(T). {
250 type_ = init_decl_type(S->type, T->text);
255 %type const_decl_type {decl_type*}
256 %destructor const_decl_type {free_decl_type($$);}
257 const_decl_type(type) ::= decl_type(type_). {
260 const_decl_type(type) ::= CONST decl_type(type_). {
265 %destructor impl {free_impl($$);}
266 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
267 impl = init_impl(func, stmts);
270 %type impl_func {impl_func*}
271 %destructor impl_func {free_impl_func($$);}
272 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
273 func = init_impl_func(NAME->text, args, type, 0);
276 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
277 func = init_impl_func(NAME->text, args, type, 1);
281 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
282 %type impl_def_val {impl_def_val*}
283 %destructor impl_def_val {free_impl_def_val($$);}
284 impl_def_val(def) ::= impl_def_val_token(T). {
285 def = init_impl_def_val(T->type, T->text);
289 %type impl_var {impl_var*}
290 %destructor impl_var {free_impl_var($$);}
291 impl_var(var) ::= DOLLAR NAME(T). {
292 var = init_impl_var(T->text, 0);
295 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
296 var = init_impl_var(T->text, 1);
300 %type impl_arg {impl_arg*}
301 %destructor impl_arg {free_impl_arg($$);}
302 impl_arg(arg) ::= impl_type(type) impl_var(var). {
303 arg = init_impl_arg(type, var, NULL);
305 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
306 arg = init_impl_arg(type, var, def);
309 %type impl_args {impl_args*}
310 %destructor impl_args {free_impl_args($$);}
311 impl_args(args) ::= LPAREN RPAREN. {
314 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
317 %type impl_arg_list {impl_args*}
318 %destructor impl_arg_list {free_impl_args($$);}
319 impl_arg_list(args) ::= impl_arg(arg). {
320 args = init_impl_args(arg);
322 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
323 args = add_impl_arg(args_, arg);
326 %type impl_stmts {impl_stmts*}
327 %destructor impl_stmts {free_impl_stmts($$);}
328 impl_stmts(stmts) ::= impl_stmt(stmt). {
329 stmts = init_impl_stmts(stmt);
331 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
332 stmts = add_impl_stmt(stmts_, stmt);
335 %type impl_stmt {impl_stmt*}
336 %destructor impl_stmt {free_impl_stmt($$);}
337 impl_stmt(stmt) ::= let_stmt(let). {
338 stmt = init_impl_stmt(PSI_T_LET, let);
340 impl_stmt(stmt) ::= set_stmt(set). {
341 stmt = init_impl_stmt(PSI_T_SET, set);
343 impl_stmt(stmt) ::= return_stmt(ret). {
344 stmt = init_impl_stmt(PSI_T_RETURN, ret);
346 impl_stmt(stmt) ::= free_stmt(free). {
347 stmt = init_impl_stmt(PSI_T_FREE, free);
350 %type let_stmt {let_stmt*}
351 %destructor let_stmt {free_let_stmt($$);}
352 let_stmt(let) ::= LET decl_var(var) EOS. {
353 let = init_let_stmt(var, NULL);
355 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
356 let = init_let_stmt(var, val);
359 %type let_value {let_value*}
360 %destructor let_value {free_let_value($$);}
361 let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. {
362 val = init_let_value(
363 init_let_func(F->type, F->text,
372 let_value(val) ::= reference(r) let_func(func) LPAREN impl_var(var) RPAREN. {
373 val = init_let_value(func, var, r);
375 let_value(val) ::= reference(r) NULL. {
376 val = init_let_value(NULL, NULL, r);
379 %token_class let_func_token ARRVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
380 %type let_func {let_func*}
381 %destructor let_func {free_let_func($$);}
382 let_func(func) ::= let_func_token(T). {
383 func = init_let_func(T->type, T->text, NULL);
387 %type set_stmt {set_stmt*}
388 %destructor set_stmt {free_set_stmt($$);}
389 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
390 set = init_set_stmt(var, val);
393 %type set_value {set_value*}
394 %destructor set_value {free_set_value($$);}
395 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
396 val = init_set_value(func, vars);
398 set_value(val) ::= set_func(func_) LPAREN decl_vars(vars_) COMMA set_vals(vals) RPAREN. {
403 %type set_vals {set_value*}
404 %destructor set_vals {free_set_value($$);}
405 set_vals(vals) ::= set_value(val). {
406 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
408 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
409 vals = add_inner_set_value(vals_, val);
412 %token_class set_func_token TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
413 %type set_func {set_func*}
414 %destructor set_func {free_set_func($$);}
415 set_func(func) ::= set_func_token(T). {
416 func = init_set_func(T->type, T->text);
420 %type return_stmt {return_stmt*}
421 %destructor return_stmt {free_return_stmt($$);}
422 return_stmt(ret) ::= RETURN set_value(val) EOS. {
423 ret = init_return_stmt(val);
426 %type free_stmt {free_stmt*}
427 %destructor free_stmt {free_free_stmt($$);}
428 free_stmt(free) ::= FREE free_calls(calls) EOS. {
429 free = init_free_stmt(calls);
432 %type free_calls {free_calls*}
433 %destructor free_calls {free_free_calls($$);}
434 free_calls(calls) ::= free_call(call). {
435 calls = init_free_calls(call);
437 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
438 calls = add_free_call(calls_, call);
441 %type free_call {free_call*}
442 %destructor free_call {free_free_call($$);}
443 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
444 call = init_free_call(F->text, vars);
447 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY.
448 %type impl_type {impl_type*}
449 %destructor impl_type {free_impl_type($$);}
450 impl_type(type_) ::= impl_type_token(T). {
451 type_ = init_impl_type(T->type, T->text);
455 %type reference {char}
456 reference(r) ::= . {r = 0;}
457 reference(r) ::= REFERENCE. {r = 1;}
459 %type indirection {unsigned}
460 indirection(i) ::= . {i = 0;}
461 indirection(i) ::= pointers(p). {i = p;}
463 %type pointers {unsigned}
464 pointers(p) ::= POINTER. {p = 1;}
465 pointers(p) ::= pointers(P) POINTER. {p = P+1;}