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 PSI_ParserSyntaxError(P, P->fn, P->line, "Unexpected token '%s'", TOKEN->text);
23 blocks ::= blocks block.
27 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
29 PSI_ParserSyntaxError(P, P->fn, T->line, "Extra 'lib %s' statement has no effect", libname->text);
31 P->lib = strndup(libname->text + 1, libname->size - 2);
37 block ::= decl(decl). {
38 P->decls = add_decl(P->decls, decl);
40 block ::= impl(impl). {
41 P->impls = add_impl(P->impls, impl);
43 block ::= decl_typedef(def). {
44 P->defs = add_decl_typedef(P->defs, def);
45 if (def->type->strct) {
46 P->structs = add_decl_struct(P->structs, def->type->strct);
49 block ::= constant(constant). {
50 P->consts = add_constant(P->consts, constant);
52 block ::= decl_struct(strct). {
53 P->structs = add_decl_struct(P->structs, strct);
56 %type decl_struct {decl_struct*}
57 %destructor decl_struct {free_decl_struct($$);}
58 decl_struct(strct) ::= STRUCT NAME(N) LBRACE struct_args(args) RBRACE. {
59 strct = init_decl_struct(N->text, args);
63 %type const_type {const_type*}
64 %destructor const_type {free_const_type($$);}
65 const_type(type_) ::= BOOL(T). {
66 type_ = init_const_type(T->type, T->text);
69 const_type(type_) ::= INT(T). {
70 type_ = init_const_type(T->type, T->text);
73 const_type(type_) ::= FLOAT(T). {
74 type_ = init_const_type(T->type, T->text);
77 const_type(type_) ::= STRING(T). {
78 type_ = init_const_type(T->type, T->text);
81 %type constant {constant*}
82 %destructor constant {free_constant($$);}
83 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
84 constant = init_constant(type, T->text, val);
88 %type decl_typedef {decl_typedef*}
89 %destructor decl_typedef {free_decl_typedef($$);}
90 decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. {
91 def = init_decl_typedef(ALIAS->text, type);
94 decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. {
95 def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text));
100 decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
101 def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, s->name));
102 def->type->strct = s;
107 %destructor decl {free_decl($$);}
108 decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
109 decl = init_decl(abi, func, args);
112 %type decl_abi {decl_abi*}
113 %destructor decl_abi {free_decl_abi($$);}
114 decl_abi(abi) ::= NAME(T). {
115 abi = init_decl_abi(T->text);
119 %type decl_var {decl_var*}
120 %destructor decl_var {free_decl_var($$);}
121 decl_var(var) ::= NAME(T). {
122 var = init_decl_var(T->text, 0, 0);
125 decl_var(var) ::= pointers(p) NAME(T). {
126 var = init_decl_var(T->text, p, 0);
129 decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
130 var = init_decl_var(T->text, 1, atol(D->text));
134 decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
135 var = init_decl_var(T->text, p+1, atol(D->text));
140 %type decl_vars {decl_vars*}
141 %destructor decl_vars {free_decl_vars($$);}
142 decl_vars(vars) ::= decl_var(var). {
143 vars = init_decl_vars(var);
145 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
146 vars = add_decl_var(vars_, var);
149 %type decl_arg {decl_arg*}
150 %destructor decl_arg {free_decl_arg($$);}
151 decl_arg(arg_) ::= decl_type(type) decl_var(var). {
152 arg_ = var->arg = init_decl_arg(type, var);
155 %type decl_args {decl_args*}
156 %destructor decl_args {free_decl_args($$);}
158 decl_args(args) ::= decl_arg(arg). {
159 args = init_decl_args(arg);
161 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
162 args = add_decl_arg(args_, arg);
164 %type struct_args {decl_args*}
165 %destructor struct_args {free_decl_args($$);}
166 struct_args(args) ::= decl_arg(arg) EOS. {
167 args = init_decl_args(arg);
169 struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. {
170 args = add_decl_arg(args_, arg);
173 %type decl_type {decl_type*}
174 %destructor decl_type {free_decl_type($$);}
175 decl_type(type_) ::= VOID(T). {
176 type_ = init_decl_type(T->type, T->text);
179 decl_type(type_) ::= CHAR(T). {
180 type_ = init_decl_type(T->type, T->text);
183 decl_type(type_) ::= SHORT(T). {
184 type_ = init_decl_type(T->type, T->text);
187 decl_type(type_) ::= INT(T). {
188 type_ = init_decl_type(T->type, T->text);
191 decl_type(type_) ::= LONG(T). {
192 type_ = init_decl_type(T->type, T->text);
195 decl_type(type_) ::= FLOAT(T). {
196 type_ = init_decl_type(T->type, T->text);
199 decl_type(type_) ::= DOUBLE(T). {
200 type_ = init_decl_type(T->type, T->text);
203 decl_type(type_) ::= SIZE_T(T). {
204 type_ = init_decl_type(T->type, T->text);
207 decl_type(type_) ::= SINT8(T). {
208 type_ = init_decl_type(T->type, T->text);
211 decl_type(type_) ::= UINT8(T). {
212 type_ = init_decl_type(T->type, T->text);
215 decl_type(type_) ::= SINT16(T). {
216 type_ = init_decl_type(T->type, T->text);
219 decl_type(type_) ::= UINT16(T). {
220 type_ = init_decl_type(T->type, T->text);
223 decl_type(type_) ::= SINT32(T). {
224 type_ = init_decl_type(T->type, T->text);
227 decl_type(type_) ::= UINT32(T). {
228 type_ = init_decl_type(T->type, T->text);
231 decl_type(type_) ::= SINT64(T). {
232 type_ = init_decl_type(T->type, T->text);
235 decl_type(type_) ::= UINT64(T). {
236 type_ = init_decl_type(T->type, T->text);
239 decl_type(type_) ::= NAME(T). {
240 type_ = init_decl_type(T->type, T->text);
245 %destructor impl {free_impl($$);}
246 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
247 impl = init_impl(func, stmts);
250 %type impl_func {impl_func*}
251 %destructor impl_func {free_impl_func($$);}
252 impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). {
253 func = init_impl_func(NAME->text, args, type, 0);
256 impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
257 func = init_impl_func(NAME->text, args, type, 1);
261 %type impl_def_val {impl_def_val*}
262 %destructor impl_def_val {free_impl_def_val($$);}
263 impl_def_val(def) ::= NULL(T). {
264 def = init_impl_def_val(T);
267 impl_def_val(def) ::= NUMBER(T). {
268 def = init_impl_def_val(T);
271 impl_def_val(def) ::= TRUE(T). {
272 def = init_impl_def_val(T);
275 impl_def_val(def) ::= FALSE(T). {
276 def = init_impl_def_val(T);
279 impl_def_val(def) ::= QUOTED_STRING(T). {
280 def = init_impl_def_val(T);
284 %type impl_var {impl_var*}
285 %destructor impl_var {free_impl_var($$);}
286 impl_var(var) ::= DOLLAR NAME(T). {
287 var = init_impl_var(T->text, 0);
290 impl_var(var) ::= REFERENCE DOLLAR NAME(T). {
291 var = init_impl_var(T->text, 1);
295 %type impl_arg {impl_arg*}
296 %destructor impl_arg {free_impl_arg($$);}
297 impl_arg(arg) ::= impl_type(type) impl_var(var). {
298 arg = init_impl_arg(type, var, NULL);
300 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
301 arg = init_impl_arg(type, var, def);
304 %type impl_args {impl_args*}
305 %destructor impl_args {free_impl_args($$);}
306 impl_args(args) ::= LPAREN RPAREN. {
309 impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
312 %type impl_arg_list {impl_args*}
313 %destructor impl_arg_list {free_impl_args($$);}
314 impl_arg_list(args) ::= impl_arg(arg). {
315 args = init_impl_args(arg);
317 impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
318 args = add_impl_arg(args_, arg);
321 %type impl_stmts {impl_stmts*}
322 %destructor impl_stmts {free_impl_stmts($$);}
323 impl_stmts(stmts) ::= impl_stmt(stmt). {
324 stmts = init_impl_stmts(stmt);
326 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
327 stmts = add_impl_stmt(stmts_, stmt);
330 %type impl_stmt {impl_stmt*}
331 %destructor impl_stmt {free_impl_stmt($$);}
332 impl_stmt(stmt) ::= let_stmt(let). {
333 stmt = init_impl_stmt(PSI_T_LET, let);
335 impl_stmt(stmt) ::= set_stmt(set). {
336 stmt = init_impl_stmt(PSI_T_SET, set);
338 impl_stmt(stmt) ::= return_stmt(ret). {
339 stmt = init_impl_stmt(PSI_T_RETURN, ret);
341 impl_stmt(stmt) ::= free_stmt(free). {
342 stmt = init_impl_stmt(PSI_T_FREE, free);
345 %type let_stmt {let_stmt*}
346 %destructor let_stmt {free_let_stmt($$);}
347 let_stmt(let) ::= LET decl_var(var) EOS. {
348 let = init_let_stmt(var, NULL);
350 let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
351 let = init_let_stmt(var, val);
354 %type let_value {let_value*}
355 %destructor let_value {free_let_value($$);}
356 let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. {
357 val = init_let_value(
358 init_let_func(F->type, F->text,
367 let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
368 val = init_let_value(func, var, 0);
370 let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
371 val = init_let_value(func, var, 1);
373 let_value(val) ::= REFERENCE NULL. {
374 val = init_let_value(NULL, NULL, 1);
376 let_value(val) ::= NULL. {
377 val = init_let_value(NULL, NULL, 0);
380 %type let_func {let_func*}
381 %destructor let_func {free_let_func($$);}
382 let_func(func) ::= STRLEN(T). {
383 func = init_let_func(T->type, T->text, NULL);
386 let_func(func) ::= STRVAL(T). {
387 func = init_let_func(T->type, T->text, NULL);
390 let_func(func) ::= INTVAL(T). {
391 func = init_let_func(T->type, T->text, NULL);
394 let_func(func) ::= FLOATVAL(T). {
395 func = init_let_func(T->type, T->text, NULL);
398 let_func(func) ::= BOOLVAL(T). {
399 func = init_let_func(T->type, T->text, NULL);
402 let_func(func) ::= ARRVAL(T). {
403 func = init_let_func(T->type, T->text, NULL);
407 %type set_stmt {set_stmt*}
408 %destructor set_stmt {free_set_stmt($$);}
409 set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
410 set = init_set_stmt(var, val);
413 %type set_value {set_value*}
414 %destructor set_value {free_set_value($$);}
415 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
416 val = init_set_value(func, vars);
419 %type set_func {set_func*}
420 %destructor set_func {free_set_func($$);}
421 set_func(func) ::= TO_ARRAY(T). {
422 func = init_set_func(T->type, T->text);
425 set_func(func) ::= TO_STRING(T). {
426 func = init_set_func(T->type, T->text);
429 set_func(func) ::= TO_INT(T). {
430 func = init_set_func(T->type, T->text);
433 set_func(func) ::= TO_FLOAT(T). {
434 func = init_set_func(T->type, T->text);
437 set_func(func) ::= TO_BOOL(T). {
438 func = init_set_func(T->type, T->text);
441 set_func(func) ::= VOID(T). {
442 func = init_set_func(T->type, T->text);
446 %type return_stmt {return_stmt*}
447 %destructor return_stmt {free_return_stmt($$);}
448 return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
449 ret = init_return_stmt(func, var);
452 %type free_stmt {free_stmt*}
453 %destructor free_stmt {free_free_stmt($$);}
454 free_stmt(free) ::= FREE decl_vars(vars) EOS. {
455 free = init_free_stmt(vars);
458 %type impl_type {impl_type*}
459 %destructor impl_type {free_impl_type($$);}
460 impl_type(type_) ::= VOID(T). {
461 type_ = init_impl_type(T->type, T->text);
464 impl_type(type_) ::= MIXED(T). {
465 type_ = init_impl_type(T->type, T->text);
468 impl_type(type_) ::= BOOL(T). {
469 type_ = init_impl_type(T->type, T->text);
472 impl_type(type_) ::= INT(T). {
473 type_ = init_impl_type(T->type, T->text);
476 impl_type(type_) ::= FLOAT(T). {
477 type_ = init_impl_type(T->type, T->text);
480 impl_type(type_) ::= STRING(T). {
481 type_ = init_impl_type(T->type, T->text);
484 impl_type(type_) ::= ARRAY(T). {
485 type_ = init_impl_type(T->type, T->text);
489 %type pointers {unsigned}
490 pointers(p) ::= POINTER. {++p;}
491 pointers(p) ::= pointers(P) POINTER. {p = P+1;}