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 %left PLUS MINUS SLASH ASTERISK.
27 %fallback NAME FREE SET LET RETURN LIB INT UNSIGNED.
32 blocks ::= blocks block.
36 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
38 PSI_ParserSyntaxError(P, P->psi.file.ln, T->line, "Extra 'lib %s' statement has no effect", libname->text);
40 P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
46 block ::= decl(decl). {
47 P->decls = add_decl(P->decls, decl);
49 block ::= impl(impl). {
50 P->impls = add_impl(P->impls, impl);
52 block ::= decl_typedef(def). {
53 P->defs = add_decl_typedef(P->defs, def);
54 if (def->type->strct) {
55 P->structs = add_decl_struct(P->structs, def->type->strct);
58 block ::= constant(constant). {
59 P->consts = add_constant(P->consts, constant);
61 block ::= decl_struct(strct). {
62 P->structs = add_decl_struct(P->structs, strct);
65 %type decl_struct {decl_struct*}
66 %destructor decl_struct {free_decl_struct($$);}
67 decl_struct(strct) ::= STRUCT NAME(N) struct_size(size_) LBRACE struct_args(args) RBRACE. {
68 strct = init_decl_struct(N->text, args);
73 %type struct_size {size_t}
74 struct_size(size) ::= . {
77 struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
78 size = atol(SIZ->text);
82 %token_class const_type_token BOOL INT FLOAT STRING.
83 %type const_type {const_type*}
84 %destructor const_type {free_const_type($$);}
85 const_type(type_) ::= const_type_token(T). {
86 type_ = init_const_type(T->type, T->text);
89 %type constant {constant*}
90 %destructor constant {free_constant($$);}
91 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
92 constant = init_constant(type, T->text, val);
96 %type decl_typedef {decl_typedef*}
97 %destructor decl_typedef {free_decl_typedef($$);}
98 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
99 def = init_decl_typedef(ALIAS->text, type);
102 /* support opaque types */
103 decl_typedef(def) ::= TYPEDEF VOID(V) NAME(ALIAS) EOS. {
104 def = init_decl_typedef(ALIAS->text, init_decl_type(V->type, V->text));
108 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
109 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
114 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
115 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
116 def->type->strct = s;
121 %destructor decl {free_decl($$);}
122 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
123 decl = init_decl(abi, func, args);
126 %type decl_func {decl_arg*}
127 %destructor decl_func {free_decl_arg($$);}
128 decl_func(func) ::= decl_arg(arg). {
131 /* special case for void functions */
132 decl_func(func) ::= VOID(T) NAME(N). {
133 func = init_decl_arg(
134 init_decl_type(T->type, T->text),
135 init_decl_var(N->text, 0, 0)
141 %type decl_abi {decl_abi*}
142 %destructor decl_abi {free_decl_abi($$);}
143 decl_abi(abi) ::= NAME(T). {
144 abi = init_decl_abi(T->text);
148 %type decl_var {decl_var*}
149 %destructor decl_var {free_decl_var($$);}
150 decl_var(var) ::= indirection(p) NAME(T). {
151 var = init_decl_var(T->text, p, 0);
154 decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
155 var = init_decl_var(T->text, p+1, atol(D->text));
160 %type decl_vars {decl_vars*}
161 %destructor decl_vars {free_decl_vars($$);}
162 decl_vars(vars) ::= decl_var(var). {
163 vars = init_decl_vars(var);
165 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
166 vars = add_decl_var(vars_, var);
169 %type decl_arg {decl_arg*}
170 %destructor decl_arg {free_decl_arg($$);}
171 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
172 arg_ = init_decl_arg(type, var);
174 /* void pointers need a specific rule */
175 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
176 arg_ = init_decl_arg(
177 init_decl_type(T->type, T->text),
178 init_decl_var(N->text, p, 0)
183 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
184 arg_ = init_decl_arg(
185 init_decl_type(T->type, T->text),
186 init_decl_var(N->text, p, 0)
192 %type decl_args {decl_args*}
193 %destructor decl_args {free_decl_args($$);}
196 decl_args(args) ::= decl_arg(arg). {
197 args = init_decl_args(arg);
199 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
200 args = add_decl_arg(args_, arg);
202 %type struct_args {decl_args*}
203 %destructor struct_args {free_decl_args($$);}
204 struct_args(args) ::= struct_arg(arg). {
205 args = init_decl_args(arg);
207 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
208 args = add_decl_arg(args_, arg);
210 %type struct_arg {decl_arg*}
211 %destructor struct_arg {free_decl_arg($$);}
212 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
213 arg_->layout = layout_;
217 %type struct_layout {decl_struct_layout*}
218 %destructor struct_layout {free_decl_struct_layout($$);}
219 struct_layout(layout) ::= . {
222 struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
223 layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text));
228 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
229 %type decl_type {decl_type*}
230 %destructor decl_type {free_decl_type($$);}
231 decl_type(type_) ::= decl_type_token(T). {
232 type_ = init_decl_type(T->type, T->text);
236 decl_type(type_) ::= UNSIGNED NAME(T). {
237 type_ = init_decl_type(T->type, T->text);
238 type_->name = realloc(type_->name, T->size + sizeof("unsigned"));
239 memmove(type_->name + sizeof("unsigned"), type_->name, T->size);
240 memcpy(type_->name, "unsigned", sizeof("unsigned")-1);
241 type_->name[sizeof("unsigned")] = ' ';
242 type_->name[T->size + sizeof("unsigned")] = 0;
244 /* we have to support plain int here because we have it in our lexer rules */
245 decl_type(type_) ::= INT(T). {
246 type_ = init_decl_type(PSI_T_NAME, T->text);
250 decl_type(type_) ::= STRUCT(S) NAME(T). {
251 type_ = init_decl_type(S->type, T->text);
256 %type const_decl_type {decl_type*}
257 %destructor const_decl_type {free_decl_type($$);}
258 const_decl_type(type) ::= decl_type(type_). {
261 const_decl_type(type) ::= CONST decl_type(type_). {
266 %destructor impl {free_impl($$);}
267 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
268 impl = init_impl(func, stmts);
271 %type impl_func {impl_func*}
272 %destructor impl_func {free_impl_func($$);}
273 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
274 func = init_impl_func(NAME->text, args, type, 0);
277 impl_func(func) ::= FUNCTION AMPERSAND NSNAME(NAME) impl_args(args) COLON impl_type(type). {
278 func = init_impl_func(NAME->text, args, type, 1);
282 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
283 %type impl_def_val {impl_def_val*}
284 %destructor impl_def_val {free_impl_def_val($$);}
285 impl_def_val(def) ::= impl_def_val_token(T). {
286 def = init_impl_def_val(T->type, T->text);
290 %type impl_var {impl_var*}
291 %destructor impl_var {free_impl_var($$);}
292 impl_var(var) ::= DOLLAR NAME(T). {
293 var = init_impl_var(T->text, 0);
296 impl_var(var) ::= AMPERSAND DOLLAR NAME(T). {
297 var = init_impl_var(T->text, 1);
301 %type impl_arg {impl_arg*}
302 %destructor impl_arg {free_impl_arg($$);}
303 impl_arg(arg) ::= impl_type(type) impl_var(var). {
304 arg = init_impl_arg(type, var, NULL);
306 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
307 arg = init_impl_arg(type, var, def);
310 %type impl_args {impl_args*}
311 %destructor impl_args {free_impl_args($$);}
312 impl_args(args) ::= LPAREN RPAREN. {
315 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
318 %type impl_arg_list {impl_args*}
319 %destructor impl_arg_list {free_impl_args($$);}
320 impl_arg_list(args) ::= impl_arg(arg). {
321 args = init_impl_args(arg);
323 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
324 args = add_impl_arg(args_, arg);
327 %type impl_stmts {impl_stmts*}
328 %destructor impl_stmts {free_impl_stmts($$);}
329 impl_stmts(stmts) ::= impl_stmt(stmt). {
330 stmts = init_impl_stmts(stmt);
332 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
333 stmts = add_impl_stmt(stmts_, stmt);
336 %type impl_stmt {impl_stmt*}
337 %destructor impl_stmt {free_impl_stmt($$);}
338 impl_stmt(stmt) ::= let_stmt(let). {
339 stmt = init_impl_stmt(PSI_T_LET, let);
341 impl_stmt(stmt) ::= set_stmt(set). {
342 stmt = init_impl_stmt(PSI_T_SET, set);
344 impl_stmt(stmt) ::= return_stmt(ret). {
345 stmt = init_impl_stmt(PSI_T_RETURN, ret);
347 impl_stmt(stmt) ::= free_stmt(free). {
348 stmt = init_impl_stmt(PSI_T_FREE, free);
351 %type let_stmt {let_stmt*}
352 %destructor let_stmt {free_let_stmt($$);}
353 let_stmt(let) ::= LET decl_var(var) EOS. {
354 let = init_let_stmt(var, NULL);
356 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
357 let = init_let_stmt(var, val);
360 %type let_value {let_value*}
361 %destructor let_value {free_let_value($$);}
362 let_value(val) ::= CALLOC(F) LPAREN let_calloc(alloc) RPAREN. {
363 val = init_let_value(init_let_func(F->type, F->text, alloc), NULL, 0);
367 %type let_calloc {let_calloc*}
368 %destructor let_calloc {free_let_calloc($$);}
369 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
370 alloc = init_let_calloc(nmemb, size);
373 %token_class num_exp_token NUMBER NSNAME.
374 %token_class num_exp_op_token PLUS MINUS ASTERISK SLASH.
375 %type num_exp {num_exp*}
376 %destructor num_exp {free_num_exp($$);}
377 num_exp(exp) ::= num_exp_token(tok). {
378 exp = init_num_exp(tok->type, tok->text);
381 num_exp(exp) ::= decl_var(var). {
382 exp = init_num_exp(PSI_T_NAME, var);
384 num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). {
385 exp_->operator = operator_->type;
386 exp_->operand = operand_;
391 let_value(val) ::= reference(r) let_func(func) LPAREN impl_var(var) RPAREN. {
392 val = init_let_value(func, var, r);
394 let_value(val) ::= reference(r) NULL. {
395 val = init_let_value(NULL, NULL, r);
398 %token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
399 %type let_func {let_func*}
400 %destructor let_func {free_let_func($$);}
401 let_func(func) ::= let_func_token(T). {
402 func = init_let_func(T->type, T->text, NULL);
406 %type set_stmt {set_stmt*}
407 %destructor set_stmt {free_set_stmt($$);}
408 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
409 set = init_set_stmt(var, val);
412 %type set_value {set_value*}
413 %destructor set_value {free_set_value($$);}
414 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
415 val = init_set_value(func, vars);
417 set_value(val) ::= set_func(func_) LPAREN decl_vars(vars_) COMMA set_vals(vals) RPAREN. {
422 %type set_vals {set_value*}
423 %destructor set_vals {free_set_value($$);}
424 set_vals(vals) ::= set_value(val). {
425 vals = add_inner_set_value(init_set_value(NULL, NULL), val);
427 set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
428 vals = add_inner_set_value(vals_, val);
431 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
432 %type set_func {set_func*}
433 %destructor set_func {free_set_func($$);}
434 set_func(func) ::= set_func_token(T). {
435 func = init_set_func(T->type, T->text);
439 %type return_stmt {return_stmt*}
440 %destructor return_stmt {free_return_stmt($$);}
441 return_stmt(ret) ::= RETURN set_value(val) EOS. {
442 ret = init_return_stmt(val);
445 %type free_stmt {free_stmt*}
446 %destructor free_stmt {free_free_stmt($$);}
447 free_stmt(free) ::= FREE free_calls(calls) EOS. {
448 free = init_free_stmt(calls);
451 %type free_calls {free_calls*}
452 %destructor free_calls {free_free_calls($$);}
453 free_calls(calls) ::= free_call(call). {
454 calls = init_free_calls(call);
456 free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
457 calls = add_free_call(calls_, call);
460 %type free_call {free_call*}
461 %destructor free_call {free_free_call($$);}
462 free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
463 call = init_free_call(F->text, vars);
466 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
467 %type impl_type {impl_type*}
468 %destructor impl_type {free_impl_type($$);}
469 impl_type(type_) ::= impl_type_token(T). {
470 type_ = init_impl_type(T->type, T->text);
474 %type reference {char}
475 reference(r) ::= . {r = 0;}
476 reference(r) ::= AMPERSAND. {r = 1;}
478 %type indirection {unsigned}
479 indirection(i) ::= . {i = 0;}
480 indirection(i) ::= pointers(p). {i = p;}
482 %type pointers {unsigned}
483 pointers(p) ::= ASTERISK. {p = 1;}
484 pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}