split decl to allow functors as arg
[m6w6/ext-psi] / src / parser_proc_grammar.y
1 %code top {
2 #include "php_psi_stdinc.h"
3 }
4
5 %code {
6 #include <assert.h>
7 #include <stdarg.h>
8
9 #include "plist.h"
10 #include "parser.h"
11 #define YYDEBUG 1
12
13 static int psi_parser_proc_lex(YYSTYPE *u, struct psi_parser *P, struct psi_plist *tokens, size_t *index);
14 static void psi_parser_proc_error(struct psi_parser *P, struct psi_plist *tokens, size_t *index, const char *msg);
15
16 static inline void psi_parser_proc_add_struct(struct psi_parser *P, struct psi_decl_struct *strct)
17 {
18 assert(strct);
19 if (!P->structs) {
20 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
21 }
22 P->structs = psi_plist_add(P->structs, &strct);
23 }
24 static inline void psi_parser_proc_add_union(struct psi_parser *P, struct psi_decl_union *u)
25 {
26 assert(u);
27 if (!P->unions) {
28 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
29 }
30 P->unions = psi_plist_add(P->unions, &u);
31 }
32 static inline void psi_parser_proc_add_enum(struct psi_parser *P, struct psi_decl_enum *e)
33 {
34 assert(e);
35 if (!P->enums) {
36 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
37 }
38 P->enums = psi_plist_add(P->enums, &e);
39 }
40 static inline void psi_parser_proc_add_from_typedef(struct psi_parser *P, struct psi_decl_arg *def)
41 {
42 if (def->type->real.def) {
43 switch (def->type->type) {
44 case PSI_T_STRUCT:
45 psi_parser_proc_add_struct(P, def->type->real.strct);
46 break;
47 case PSI_T_UNION:
48 psi_parser_proc_add_union(P, def->type->real.unn);
49 break;
50 case PSI_T_ENUM:
51 psi_parser_proc_add_enum(P, def->type->real.enm);
52 break;
53 default:
54 break;
55 }
56 }
57 }
58 static inline void psi_parser_proc_add_typedef(struct psi_parser *P, struct psi_decl_arg *def)
59 {
60 assert(def);
61 if (!P->types) {
62 P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
63 }
64 P->types = psi_plist_add(P->types, &def);
65 psi_parser_proc_add_from_typedef(P, def);
66 }
67 static inline void psi_parser_proc_add_const(struct psi_parser *P, struct psi_const *cnst) {
68 assert(cnst);
69 if (!P->consts) {
70 P->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
71 }
72 P->consts = psi_plist_add(P->consts, &cnst);
73
74 }
75 static inline void psi_parser_proc_add_decl(struct psi_parser *P, struct psi_decl *decl) {
76 assert(decl);
77 if (!P->decls) {
78 P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
79 }
80 P->decls = psi_plist_add(P->decls, &decl);
81 }
82 static inline void psi_parser_proc_add_impl(struct psi_parser *P, struct psi_impl *impl) {
83 assert(impl);
84 if (!P->impls) {
85 P->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
86 }
87 P->impls = psi_plist_add(P->impls, &impl);
88 }
89
90 /* end code */
91 }
92
93 %code requires {
94 #include "plist.h"
95 #include "types/layout.h"
96
97 struct psi_parser;
98
99 }
100
101 %require "3.0"
102 %language "c"
103 %name-prefix "psi_parser_proc_"
104 %token-table
105 %verbose
106 %glr-parser
107
108 %param {struct psi_parser *P} {struct psi_plist *tokens} {size_t *index}
109
110 %define api.pure true
111 %define api.token.prefix {PSI_T_}
112 %define api.value.type union
113
114 %define parse.error verbose
115 %define parse.trace true
116
117 %initial-action {
118 }
119
120 %start file
121
122 %token <struct psi_token *> BOOL
123 %token <struct psi_token *> CHAR
124 %token <struct psi_token *> SHORT
125 %token <struct psi_token *> INT
126 %token <struct psi_token *> SIGNED
127 %token <struct psi_token *> UNSIGNED
128 %token <struct psi_token *> LONG
129 %token <struct psi_token *> FLOAT
130 %token <struct psi_token *> DOUBLE
131 %token <struct psi_token *> STRING
132 %token <struct psi_token *> MIXED
133 %token <struct psi_token *> ARRAY
134 %token <struct psi_token *> OBJECT
135 %token <struct psi_token *> CALLABLE
136 %token <struct psi_token *> VOID
137 %token <struct psi_token *> ZVAL
138 %token <struct psi_token *> INT8
139 %token <struct psi_token *> UINT8
140 %token <struct psi_token *> INT16
141 %token <struct psi_token *> UINT16
142 %token <struct psi_token *> INT32
143 %token <struct psi_token *> UINT32
144 %token <struct psi_token *> INT64
145 %token <struct psi_token *> UINT64
146 %token <struct psi_token *> NULL
147 %token <struct psi_token *> TRUE
148 %token <struct psi_token *> FALSE
149 %token <struct psi_token *> NAME
150 %token <struct psi_token *> NSNAME
151 %token <struct psi_token *> DOLLAR_NAME
152 %token <struct psi_token *> NUMBER
153 %token <struct psi_token *> QUOTED_STRING
154 %token <struct psi_token *> QUOTED_CHAR
155
156 %token <struct psi_token *> EOF 0 "end of file"
157 %token <struct psi_token *> EOL "end of line"
158 %token <struct psi_token *> EOS ";"
159 %token <struct psi_token *> LPAREN "("
160 %token <struct psi_token *> RPAREN ")"
161 %token <struct psi_token *> COMMA ","
162 %token <struct psi_token *> COLON ":"
163 %token <struct psi_token *> LBRACE "{"
164 %token <struct psi_token *> RBRACE "}"
165 %token <struct psi_token *> LBRACKET "["
166 %token <struct psi_token *> RBRACKET "]"
167 %token <struct psi_token *> EQUALS "="
168 %token <struct psi_token *> HASH "#"
169 %token <struct psi_token *> PIPE "|"
170 %token <struct psi_token *> CARET "^"
171 %token <struct psi_token *> AMPERSAND "&"
172 %token <struct psi_token *> LSHIFT "<<"
173 %token <struct psi_token *> RSHIFT ">>"
174 %token <struct psi_token *> PLUS "+"
175 %token <struct psi_token *> MINUS "-"
176 %token <struct psi_token *> ASTERISK "*"
177 %token <struct psi_token *> SLASH "/"
178 %token <struct psi_token *> MODULO "%"
179 %token <struct psi_token *> LCHEVR "<"
180 %token <struct psi_token *> RCHEVR ">"
181 %token <struct psi_token *> CMP_GE ">="
182 %token <struct psi_token *> CMP_LE "<="
183 %token <struct psi_token *> OR "||"
184 %token <struct psi_token *> AND "&&"
185 %token <struct psi_token *> CMP_EQ "=="
186 %token <struct psi_token *> CMP_NE "!="
187 %token <struct psi_token *> TILDE "~"
188 %token <struct psi_token *> NOT "!"
189 %token <struct psi_token *> PERIOD "."
190 %token <struct psi_token *> BACKSLASH "\\"
191 %token <struct psi_token *> ELLIPSIS "..."
192 %token <struct psi_token *> IIF "?"
193
194 %token <struct psi_token *> PRAGMA
195 %token <struct psi_token *> PRAGMA_ONCE
196 %token <struct psi_token *> LINE
197 %token <struct psi_token *> ERROR
198 %token <struct psi_token *> WARNING
199 %token <struct psi_token *> IF
200 %token <struct psi_token *> IFDEF
201 %token <struct psi_token *> IFNDEF
202 %token <struct psi_token *> ELSE
203 %token <struct psi_token *> ELIF
204 %token <struct psi_token *> ENDIF
205 %token <struct psi_token *> DEFINE
206 %token <struct psi_token *> DEFINED
207 %token <struct psi_token *> UNDEF
208 %token <struct psi_token *> IMPORT
209 %token <struct psi_token *> INCLUDE
210 %token <struct psi_token *> INCLUDE_NEXT
211
212 %token <struct psi_token *> TYPEDEF
213 %token <struct psi_token *> STRUCT
214 %token <struct psi_token *> UNION
215 %token <struct psi_token *> ENUM
216 %token <struct psi_token *> CONST
217 %token <struct psi_token *> LIB
218 %token <struct psi_token *> STATIC
219 %token <struct psi_token *> CALLBACK
220 %token <struct psi_token *> FUNCTION
221 %token <struct psi_token *> LET
222 %token <struct psi_token *> SET
223 %token <struct psi_token *> TEMP
224 %token <struct psi_token *> FREE
225 %token <struct psi_token *> RETURN
226 %token <struct psi_token *> PRE_ASSERT
227 %token <struct psi_token *> POST_ASSERT
228 %token <struct psi_token *> BOOLVAL
229 %token <struct psi_token *> INTVAL
230 %token <struct psi_token *> STRVAL
231 %token <struct psi_token *> PATHVAL
232 %token <struct psi_token *> STRLEN
233 %token <struct psi_token *> FLOATVAL
234 %token <struct psi_token *> ARRVAL
235 %token <struct psi_token *> OBJVAL
236 %token <struct psi_token *> COUNT
237 %token <struct psi_token *> CALLOC
238 %token <struct psi_token *> TO_BOOL
239 %token <struct psi_token *> TO_INT
240 %token <struct psi_token *> TO_STRING
241 %token <struct psi_token *> TO_FLOAT
242 %token <struct psi_token *> TO_ARRAY
243 %token <struct psi_token *> TO_OBJECT
244
245 %token <struct psi_token *> COMMENT
246 %token <struct psi_token *> WHITESPACE
247 %token <struct psi_token *> NO_WHITESPACE
248 %token <struct psi_token *> CPP_HEADER
249 %token <struct psi_token *> CPP_ATTRIBUTE
250 %token <struct psi_token *> CPP_EXTENSION
251 %token <struct psi_token *> CPP_PASTE
252 %token <struct psi_token *> CPP_RESTRICT
253 %token <struct psi_token *> CPP_ASM
254
255 %precedence IIF COLON
256 %precedence OR
257 %precedence AND
258 %precedence PIPE
259 %precedence CARET
260 %precedence AMPERSAND
261 %precedence CMP_EQ CMP_NE
262 %precedence LCHEVR CMP_LE RCHEVR CMP_GE
263 %precedence LSHIFT RSHIFT
264 %precedence PLUS MINUS
265 %precedence ASTERISK SLASH MODULO
266 //%precedence NOT TILDE
267 %precedence BINARY
268 %precedence UNARY
269
270 %type <struct psi_token *> lib optional_name enum_name struct_name union_name
271 %destructor {psi_token_free(&$$);} lib optional_name enum_name struct_name union_name
272
273 %type <struct psi_token *> cpp_message_token cpp_include_token cpp_header_token cpp_no_arg_token cpp_name_arg_token cpp_exp_arg_token cpp_special_name_token
274 %destructor {} cpp_message_token cpp_include_token cpp_header_token cpp_no_arg_token cpp_name_arg_token cpp_exp_arg_token cpp_special_name_token
275
276 %type <struct psi_token *> name_token any_noeol_token binary_op_token unary_op_token
277 %destructor {} name_token any_noeol_token binary_op_token unary_op_token
278
279 %type <struct psi_token *> int_width int_width_types int_signed_types
280 %destructor {psi_token_free(&$$);} int_width int_width_types int_signed_types
281
282 %type <struct psi_token *> signed_long_types signed_short_types int_signed
283 %destructor {} signed_long_types signed_short_types int_signed
284
285 %type <struct psi_cpp_exp *> cpp cpp_exp
286 %destructor {psi_cpp_exp_free(&$$);} cpp cpp_exp
287 %type <struct psi_cpp_macro_decl *> cpp_macro_decl
288 %destructor {psi_cpp_macro_decl_free(&$$);} cpp_macro_decl
289 %type <struct psi_plist *> cpp_macro_sig cpp_macro_sig_args cpp_macro_decl_tokens cpp_macro_decl_token_list cpp_macro_call_args cpp_macro_call_arg_list
290 %destructor {psi_plist_free($$);} cpp_macro_sig cpp_macro_sig_args cpp_macro_decl_tokens cpp_macro_decl_token_list cpp_macro_call_args cpp_macro_call_arg_list
291 %type <struct psi_num_exp *> cpp_macro_exp
292 %destructor {psi_num_exp_free(&$$);} cpp_macro_exp
293
294 %type <struct psi_token *> constant_type_token impl_def_val_token
295 %destructor {} constant_type_token impl_def_val_token
296
297 %type <struct psi_const *> constant
298 %destructor {psi_const_free(&$$);} constant
299 %type <struct psi_const_type *> constant_type
300 %destructor {psi_const_type_free(&$$);} constant_type
301 %type <struct psi_impl_def_val *> impl_def_val
302 %destructor {psi_impl_def_val_free(&$$);} impl_def_val
303
304 %type <struct psi_token *> decl_real_type decl_int_type decl_type_simple
305 %destructor {psi_token_free(&$$);} decl_real_type decl_int_type decl_type_simple
306
307 %type <struct psi_token *> decl_stdint_type
308 %destructor {} decl_stdint_type
309
310 %type <struct psi_decl_type *> decl_type const_decl_type decl_type_complex
311 %destructor {psi_decl_type_free(&$$);} decl_type const_decl_type decl_type_complex
312 %type <struct psi_decl *> decl_stmt decl decl_body decl_func_body decl_functor_body
313 %destructor {psi_decl_free(&$$);} decl_stmt decl decl_body decl_func_body decl_functor_body
314 %type <struct psi_decl_arg *> decl_typedef decl_func decl_functor decl_arg struct_arg typedef
315 %destructor {psi_decl_arg_free(&$$);} decl_typedef decl_func decl_functor decl_arg struct_arg typedef
316 %type <struct psi_decl_var *> decl_var
317 %destructor {psi_decl_var_free(&$$);} decl_var
318 %type <struct psi_decl_struct *> decl_struct
319 %destructor {psi_decl_struct_free(&$$);} decl_struct
320 %type <struct psi_decl_union *> decl_union
321 %destructor {psi_decl_union_free(&$$);} decl_union
322 %type <struct psi_decl_enum *> decl_enum
323 %destructor {psi_decl_enum_free(&$$);} decl_enum
324 %type <struct psi_decl_enum_item *> decl_enum_item
325 %destructor {psi_decl_enum_item_free(&$$);} decl_enum_item
326 %type <struct psi_plist *> decl_args decl_struct_args struct_args_block struct_args decl_enum_items decl_vars
327 %destructor {psi_plist_free($$);} decl_args decl_struct_args struct_args_block struct_args decl_enum_items decl_vars
328
329 %type <struct psi_layout> align_and_size
330 %destructor {} align_and_size
331 %type <struct psi_layout *> decl_layout
332 %destructor {psi_layout_free(&$$);} decl_layout
333
334 %type <struct psi_impl *> impl
335 %destructor {psi_impl_free(&$$);} impl
336 %type <struct psi_impl_func *> impl_func
337 %destructor {psi_impl_func_free(&$$);} impl_func
338 %type <struct psi_impl_arg *> impl_arg
339 %destructor {psi_impl_arg_free(&$$);} impl_arg
340 %type <struct psi_impl_type *> impl_type
341 %destructor {psi_impl_type_free(&$$);} impl_type
342 %type <struct psi_impl_var *> impl_var
343 %destructor {psi_impl_var_free(&$$);} impl_var
344
345 %type <struct psi_let_stmt *> let_stmt
346 %destructor {psi_let_stmt_free(&$$);} let_stmt
347 %type <struct psi_let_exp *> let_exp let_exp_byref let_exp_assign
348 %destructor {psi_let_exp_free(&$$);} let_exp let_exp_byref let_exp_assign
349 %type <struct psi_let_calloc *> let_calloc
350 %destructor {psi_let_calloc_free(&$$);} let_calloc
351 %type <struct psi_let_callback *> let_callback
352 %destructor {psi_let_callback_free(&$$);} let_callback
353 %type <struct psi_let_func *> let_func
354 %destructor {psi_let_func_free(&$$);} let_func
355 %type <struct psi_set_stmt *> set_stmt
356 %destructor {psi_set_stmt_free(&$$);} set_stmt
357 %type <struct psi_set_exp *> set_exp
358 %destructor {psi_set_exp_free(&$$);} set_exp
359 %type <struct psi_set_func *> set_func
360 %destructor {psi_set_func_free(&$$);} set_func
361 %type <struct psi_assert_stmt *> assert_stmt
362 %destructor {psi_assert_stmt_free(&$$);} assert_stmt
363 %type <struct psi_return_stmt *> return_stmt
364 %destructor {psi_return_stmt_free(&$$);} return_stmt
365 %type <struct psi_free_stmt *> free_stmt
366 %destructor {psi_free_stmt_free(&$$);} free_stmt
367 %type <struct psi_free_exp *> free_exp
368 %destructor {psi_free_exp_free(&$$);} free_exp
369
370 %type <struct psi_token **> impl_stmt
371 %destructor {psi_impl_stmt_free(&$$);} impl_stmt
372 %type <struct psi_token *> impl_type_token callback_rval let_func_token set_func_token assert_stmt_token
373 %destructor {psi_token_free(&$$);} impl_type_token callback_rval let_func_token set_func_token assert_stmt_token
374 %type <struct psi_plist *> impl_args impl_stmts let_exps let_func_exps callback_arg_list callback_args set_exps set_func_exps free_exps
375 %destructor {psi_plist_free($$);} impl_args impl_stmts let_exps let_func_exps callback_arg_list callback_args set_exps set_func_exps free_exps
376
377 %type <struct psi_num_exp *> num_exp
378 %destructor {psi_num_exp_free(&$$);} num_exp
379 %type <struct psi_number *> number
380 %destructor {psi_number_free(&$$);} number
381
382 %type <size_t> indirection pointers asterisks array_size
383 %destructor {} indirection pointers asterisks array_size
384 %type <bool> reference
385 %destructor {} reference
386
387 %%
388
389 /* rules */
390
391
392 binary_op_token: PIPE | CARET | AMPERSAND | LSHIFT | RSHIFT | PLUS | MINUS | ASTERISK | SLASH | MODULO | RCHEVR | LCHEVR | CMP_GE | CMP_LE | OR | AND | CMP_EQ | CMP_NE ;
393 unary_op_token: TILDE | NOT | PLUS | MINUS ;
394 name_token: NAME | TEMP | FREE | SET | LET | CALLOC | CALLBACK | LIB | BOOL | STRING | ERROR | WARNING | LINE | PRAGMA_ONCE | PRAGMA | let_func_token | set_func_token;
395 any_noeol_token: BOOL | CHAR | SHORT | INT | SIGNED | UNSIGNED | LONG | FLOAT | DOUBLE | STRING | MIXED | ARRAY | OBJECT | CALLABLE | VOID | ZVAL | INT8 | UINT8 | INT16 | UINT16 | INT32 | UINT32 | INT64 | UINT64 | NULL | TRUE | FALSE | NAME | NSNAME | DOLLAR_NAME | NUMBER | QUOTED_STRING | QUOTED_CHAR | EOF | EOS | LPAREN | RPAREN | COMMA | COLON | LBRACE | RBRACE | LBRACKET | RBRACKET | EQUALS | HASH | PIPE | CARET | AMPERSAND | LSHIFT | RSHIFT | PLUS | MINUS | ASTERISK | SLASH | MODULO | LCHEVR | RCHEVR | CMP_GE | CMP_LE | OR | AND | CMP_EQ | CMP_NE | TILDE | NOT | PERIOD | BACKSLASH | ELLIPSIS | ERROR | WARNING | LINE | PRAGMA | PRAGMA_ONCE | IIF | IF | IFDEF | IFNDEF | ELSE | ELIF | ENDIF | DEFINE | DEFINED | UNDEF | INCLUDE | TYPEDEF | STRUCT | UNION | ENUM | CONST | LIB | STATIC | CALLBACK | FUNCTION | LET | SET | TEMP | FREE | RETURN | PRE_ASSERT | POST_ASSERT | BOOLVAL | INTVAL | STRVAL | PATHVAL | STRLEN | FLOATVAL | ARRVAL | OBJVAL | COUNT | CALLOC | TO_BOOL | TO_INT | TO_STRING | TO_FLOAT | TO_ARRAY | TO_OBJECT | COMMENT | CPP_HEADER | CPP_PASTE | CPP_RESTRICT | CPP_EXTENSION | CPP_ASM;
396
397
398 file:
399 %empty
400 | blocks
401 ;
402 blocks:
403 block
404 | blocks block
405 ;
406 block:
407 EOS
408 | EOL
409 | COMMENT
410 | cpp {
411 if ($cpp) {
412 psi_cpp_exp_exec($cpp, P->preproc, PSI_DATA(P));
413 psi_cpp_exp_free(&$cpp);
414 }
415 }
416 | lib {
417 if (P->file.ln) {
418 P->error(PSI_DATA(P), $lib, PSI_WARNING,
419 "Extra 'lib \"%s\"' statement has no effect", $lib->text);
420 } else {
421 P->file.ln = strndup($lib->text, $lib->size);
422 }
423 }
424 | constant {
425 psi_parser_proc_add_const(P, $constant);
426 }
427 | decl_stmt {
428 psi_parser_proc_add_decl(P, $decl_stmt);
429 }
430 | decl_ext_var_stmt
431 | decl_typedef[def] {
432 psi_parser_proc_add_typedef(P, $def);
433 }
434 | decl_struct[struct] {
435 psi_parser_proc_add_struct(P, $struct);
436 }
437 | decl_union[union] {
438 psi_parser_proc_add_union(P, $union);
439 }
440 | decl_enum[enum] {
441 psi_parser_proc_add_enum(P, $enum);
442 }
443 | impl {
444 psi_parser_proc_add_impl(P, $impl);
445 }
446 ;
447
448 lib:
449 LIB QUOTED_STRING EOS {
450 $lib = $QUOTED_STRING;
451 }
452 ;
453
454 cpp:
455 HASH EOL {
456 $cpp = NULL;
457 }
458 | HASH cpp_exp[exp] EOL {
459 $cpp = $exp;
460 }
461 ;
462
463 cpp_exp[exp]:
464 cpp_message_token cpp_macro_decl_tokens[tokens] {
465 if ($tokens) {
466 struct psi_token *msg = NULL;
467
468 if (psi_plist_get($tokens, 0, &msg)) {
469 size_t index = 1;
470 struct psi_token *next;
471
472 msg = psi_token_copy(msg);
473 while (psi_plist_get($tokens, index++, &next)) {
474 struct psi_token *old = msg;
475 msg = psi_token_cat(" ", 2, msg, next);
476 free(old);
477 }
478 }
479 psi_plist_free($tokens);
480
481 $exp = psi_cpp_exp_init($cpp_message_token->type, msg);
482 } else {
483 $exp = psi_cpp_exp_init($cpp_message_token->type, NULL);
484 }
485 $exp->token = psi_token_copy($cpp_message_token);
486 }
487 | cpp_include_token[INCLUDE] cpp_header_token {
488 $exp = psi_cpp_exp_init($INCLUDE->type, psi_token_copy($cpp_header_token));
489 $exp->token = psi_token_copy($INCLUDE);
490 }
491 | cpp_no_arg_token {
492 $exp = psi_cpp_exp_init($cpp_no_arg_token->type, NULL);
493 $exp->token = psi_token_copy($cpp_no_arg_token);
494 }
495 | cpp_name_arg_token cpp_special_name_token[name_token] {
496 $name_token->type = PSI_T_NAME;
497 $exp = psi_cpp_exp_init($cpp_name_arg_token->type, psi_token_copy($name_token));
498 $exp->token = psi_token_copy($cpp_name_arg_token);
499 }
500 | DEFINE cpp_macro_decl {
501 $exp = psi_cpp_exp_init($DEFINE->type, $cpp_macro_decl);
502 $exp->token = psi_token_copy($DEFINE);
503 }
504 | cpp_exp_arg_token cpp_macro_exp {
505 $exp = psi_cpp_exp_init($cpp_exp_arg_token->type, $cpp_macro_exp);
506 $exp->token = psi_token_copy($cpp_exp_arg_token);
507 }
508 | PRAGMA_ONCE {
509 $exp = psi_cpp_exp_init($PRAGMA_ONCE->type, NULL);
510 $exp->token = psi_token_copy($PRAGMA_ONCE);
511 }
512 | cpp_ignored_token cpp_macro_decl_tokens[tokens] {
513 psi_plist_free($tokens);
514 $exp = NULL;
515 }
516 ;
517
518 cpp_ignored_token:
519 LINE
520 | PRAGMA
521 ;
522
523 cpp_message_token:
524 ERROR
525 | WARNING
526 ;
527
528 cpp_include_token:
529 IMPORT
530 | INCLUDE
531 | INCLUDE_NEXT
532 ;
533
534 cpp_header_token:
535 QUOTED_STRING
536 | CPP_HEADER
537 ;
538
539 cpp_no_arg_token:
540 ELSE
541 | ENDIF
542 ;
543
544 cpp_name_arg_token:
545 IFDEF
546 | IFNDEF
547 | UNDEF
548 ;
549
550 cpp_exp_arg_token:
551 IF
552 | ELIF
553 ;
554
555 cpp_special_name_token:
556 name_token
557 | NULL
558 | TRUE
559 | FALSE
560 | CPP_RESTRICT
561 | CPP_EXTENSION
562 ;
563
564 cpp_macro_decl[macro]:
565 name_token NO_WHITESPACE LPAREN cpp_macro_sig RPAREN cpp_macro_decl_tokens {
566 $name_token->type = PSI_T_NAME;
567 $macro = psi_cpp_macro_decl_init($cpp_macro_sig, $cpp_macro_decl_tokens, NULL);
568 $macro->token = psi_token_copy($name_token);
569 }
570 | cpp_special_name_token[name_token] cpp_macro_decl_tokens {
571 $name_token->type = PSI_T_NAME;
572 $macro = psi_cpp_macro_decl_init(NULL, $cpp_macro_decl_tokens, NULL);
573 $macro->token = psi_token_copy($name_token);
574 }
575 ;
576
577 cpp_macro_sig[sig]:
578 %empty {
579 $sig = psi_plist_init(NULL);
580 }
581 | cpp_macro_sig_args
582 ;
583
584 cpp_macro_sig_args[args]:
585 name_token {
586 $name_token = psi_token_copy($name_token);
587 $name_token->type = PSI_T_NAME;
588 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_token_free), &$name_token);
589 }
590 | cpp_macro_sig_args[args_] COMMA name_token {
591 $name_token = psi_token_copy($name_token);
592 $name_token->type = PSI_T_NAME;
593 $args = psi_plist_add($args_, &$name_token);
594 }
595 ;
596
597 cpp_macro_decl_tokens[tokens]:
598 %empty {
599 $tokens = NULL;
600 }
601 | cpp_macro_decl_token_list
602 ;
603
604 cpp_macro_decl_token_list[tokens]:
605 any_noeol_token[token] {
606 $token = psi_token_copy($token);
607 $tokens = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_token_free), &$token);
608 }
609 | cpp_macro_decl_token_list[tokens_] any_noeol_token[token] {
610 $token = psi_token_copy($token);
611 $tokens = psi_plist_add($tokens_, &$token);
612 }
613 ;
614
615 cpp_macro_exp[exp]:
616 LPAREN cpp_macro_exp[exp_] RPAREN {
617 $exp = psi_num_exp_init_unary($LPAREN->type, $exp_);
618 $exp->token = psi_token_copy($LPAREN);
619 }
620 | unary_op_token cpp_macro_exp[exp_] %prec UNARY {
621 $exp = psi_num_exp_init_unary($unary_op_token->type, $exp_);
622 $exp->token = psi_token_copy($unary_op_token);
623 }
624 | cpp_macro_exp[lhs] binary_op_token cpp_macro_exp[rhs] %prec BINARY {
625 $exp = psi_num_exp_init_binary($binary_op_token->type, $lhs, $rhs);
626 $exp->token = psi_token_copy($binary_op_token);
627 }
628 | cpp_macro_exp[cond] IIF cpp_macro_exp[truthy] COLON cpp_macro_exp[falsy] {
629 $exp = psi_num_exp_init_ternary($IIF->type, $cond, $truthy, $falsy);
630 $exp->token = psi_token_copy($IIF);
631 }
632
633 | DEFINED name_token {
634 {
635 uint8_t exists;
636
637 $name_token->type = PSI_T_NAME;
638 exists = psi_cpp_defined(P->preproc, $name_token);
639 $exp = psi_num_exp_init_num(psi_number_init(PSI_T_UINT8, &exists, 0));
640 $exp->token = psi_token_copy($name_token);
641 }
642 }
643 | DEFINED LPAREN name_token RPAREN {
644 {
645 uint8_t exists;
646
647 $name_token->type = PSI_T_NAME;
648 exists = psi_cpp_defined(P->preproc, $name_token);
649 $exp = psi_num_exp_init_num(psi_number_init(PSI_T_UINT8, &exists, 0));
650 $exp->token = psi_token_copy($name_token);
651 }
652 }
653 | NUMBER {
654 $exp = psi_num_exp_init_num(psi_number_init($NUMBER->type, $NUMBER->text, $NUMBER->flags));
655 $exp->token = psi_token_copy($NUMBER);
656 $exp->data.n->token = psi_token_copy($NUMBER);
657 }
658 | QUOTED_CHAR {
659 $exp = psi_num_exp_init_num(psi_number_init($QUOTED_CHAR->type, $QUOTED_CHAR->text, 0));
660 $exp->token = psi_token_copy($QUOTED_CHAR);
661 $exp->data.n->token = psi_token_copy($QUOTED_CHAR);
662 }
663 | name_token {
664 $name_token->type = PSI_T_NAME;
665 $exp = psi_num_exp_init_num(psi_number_init(PSI_T_DEFINE, $name_token->text, 0));
666 $exp->token = psi_token_copy($name_token);
667 $exp->data.n->token = psi_token_copy($name_token);
668 }
669 | name_token LPAREN cpp_macro_call_args RPAREN {
670 $name_token->type = PSI_T_NAME;
671 $exp = psi_num_exp_init_num(psi_number_init(PSI_T_FUNCTION,
672 psi_cpp_macro_call_init($name_token->text, $cpp_macro_call_args), 0));
673 $exp->token = psi_token_copy($name_token);
674 }
675 ;
676
677 cpp_macro_call_args[args]:
678 %empty {
679 $args = NULL;
680 }
681 | cpp_macro_call_arg_list
682 ;
683
684 cpp_macro_call_arg_list[args]:
685 cpp_macro_exp {
686 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_num_exp_free),
687 &$cpp_macro_exp);
688 }
689 | cpp_macro_call_arg_list[args_] COMMA cpp_macro_exp {
690 $args = psi_plist_add($args_, &$cpp_macro_exp);
691 }
692 ;
693
694 constant[const]:
695 CONST constant_type NSNAME EQUALS impl_def_val EOS {
696 $const = psi_const_init($constant_type, $NSNAME->text, $impl_def_val);
697 $const->token = psi_token_copy($NSNAME);
698 }
699 ;
700
701 constant_type[type]:
702 constant_type_token[token] {
703 $type = psi_const_type_init($token->type, $token->text);
704 }
705 ;
706
707 constant_type_token:
708 BOOL
709 | INT
710 | FLOAT
711 | STRING
712 ;
713
714 impl_def_val[val]:
715 %empty {
716 $val = NULL;
717 }
718 | impl_def_val_token[token] {
719 $val = psi_impl_def_val_init($token->type, $token->text);
720 $val->token = psi_token_copy($token);
721 }
722 ;
723
724 impl_def_val_token:
725 NULL
726 | NUMBER
727 | TRUE
728 | FALSE
729 | QUOTED_STRING
730 ;
731
732 decl_typedef[def]:
733 TYPEDEF typedef[def_] EOS {
734 $def = $def_;
735 }
736 ;
737
738 typedef[def]:
739 decl_arg
740 | decl_func_body[decl] {
741 $def = psi_decl_arg_init(
742 psi_decl_type_init(PSI_T_FUNCTION, $decl->func->var->name),
743 psi_decl_var_copy($decl->func->var)
744 );
745 $def->type->token = psi_token_copy($decl->func->token);
746 $def->type->real.func = $decl;
747 }
748 | decl_enum[enum] name_token {
749 $name_token->type = PSI_T_NAME;
750 $def = psi_decl_arg_init(
751 psi_decl_type_init(PSI_T_ENUM, $enum->name),
752 psi_decl_var_init($name_token->text, 0, 0)
753 );
754 $def->var->token = psi_token_copy($name_token);
755 $def->type->token = psi_token_copy($enum->token);
756 $def->type->real.enm = $enum;
757 }
758 | struct_name[struct] align_and_size[as] struct_args_block[args] decl_var[var] {
759 $def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, $struct->text), $var);
760 $def->type->token = $struct;
761 $def->type->real.strct = psi_decl_struct_init($struct->text, $args);
762 $def->type->real.strct->token = psi_token_copy($struct);
763 $def->type->real.strct->align = $as.pos;
764 $def->type->real.strct->size = $as.len;
765 }
766 | union_name[union] align_and_size[as] struct_args_block[args] decl_var[var] {
767 $def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, $union->text), $var);
768 $def->type->token = $union;
769 $def->type->real.unn = psi_decl_union_init($union->text, $args);
770 $def->type->real.unn->token = psi_token_copy($union);
771 $def->type->real.unn->align = $as.pos;
772 $def->type->real.unn->size = $as.len;
773 }
774 | const_decl_type[type] decl_stdint_type[stdint] {
775 $stdint->type = PSI_T_NAME;
776 $def = psi_decl_arg_init($type, psi_decl_var_init($stdint->text, 0, 0));
777 $def->var->token = psi_token_copy($stdint);
778 }
779 ;
780
781 const_decl_type[type]:
782 CONST decl_type[type_] {
783 $type = $type_;
784 }
785 | decl_type
786 ;
787
788 decl_type[type]:
789 decl_type_simple[token] {
790 $type = psi_decl_type_init($token->type, $token->text);
791 $type->token = $token;
792 }
793 | decl_type_complex
794 ;
795
796 decl_type_complex[type]:
797 STRUCT[token] name_token {
798 $name_token->type = PSI_T_NAME;
799 $type = psi_decl_type_init($token->type, $name_token->text);
800 $type->token = psi_token_copy($name_token);
801 }
802 | UNION[token] name_token {
803 $name_token->type = PSI_T_NAME;
804 $type = psi_decl_type_init($token->type, $name_token->text);
805 $type->token = psi_token_copy($name_token);
806 }
807 | ENUM[token] name_token {
808 $name_token->type = PSI_T_NAME;
809 $type = psi_decl_type_init($token->type, $name_token->text);
810 $type->token = psi_token_copy($name_token);
811 }
812 ;
813
814 decl_type_simple[type]:
815 decl_int_type
816 | decl_real_type
817 | decl_stdint_type[type_] {
818 $type = psi_token_copy($type_);
819 }
820 | NAME[type_] {
821 $type = psi_token_copy($type_);
822 }
823 ;
824
825 decl_real_type[type]:
826 FLOAT[type_] {
827 $type = psi_token_copy($type_);
828 }
829 | DOUBLE[type_] {
830 $type = psi_token_copy($type_);
831 }
832 | LONG DOUBLE {
833 $type = psi_token_cat(" ", 2, $LONG, $DOUBLE);
834 }
835 ;
836
837 decl_stdint_type[type]:
838 INT8
839 | UINT8
840 | INT16
841 | UINT16
842 | INT32
843 | UINT32
844 | INT64
845 | UINT64
846 ;
847
848 int_signed[i]:
849 SIGNED
850 | UNSIGNED
851 ;
852
853 int_width[i]:
854 SHORT {
855 $i = psi_token_copy($SHORT);
856 }
857 | LONG {
858 $i = psi_token_copy($LONG);
859 }
860 | LONG[l1] LONG[l2] {
861 $i = psi_token_cat(" ", 2, $l1, $l2);
862 }
863 ;
864
865 decl_int_type[type]:
866 CHAR {
867 $type = psi_token_copy($CHAR);
868 }
869 | INT {
870 $type = psi_token_copy($INT);
871 }
872 | int_signed int_signed_types {
873 if ($2) {
874 $type = psi_token_cat(" ", 2, $1, $2);
875 free($2);
876 } else {
877 $type = psi_token_copy($1);
878 }
879 }
880 | int_width int_width_types {
881 if ($2) {
882 $type = psi_token_cat(" ", 2, $1, $2);
883 free($1);
884 free($2);
885 } else {
886 $type = $1;
887 }
888 }
889 ;
890
891 int_signed_types[type]:
892 %empty {
893 $type = NULL;
894 }
895 | CHAR {
896 $type = psi_token_copy($CHAR);
897 }
898 | SHORT signed_short_types {
899 if ($2) {
900 $type = psi_token_cat(" ", 2, $1, $2);
901 } else {
902 $type = psi_token_copy($1);
903 }
904 }
905 | INT {
906 $type = psi_token_copy($INT);
907 }
908 | LONG signed_long_types {
909 if ($2) {
910 $type = psi_token_cat(" ", 2, $1, $2);
911 } else {
912 $type = psi_token_copy($1);
913 }
914 }
915 ;
916
917 signed_short_types[type]:
918 %empty {
919 $type = NULL;
920 }
921 | INT
922 ;
923 signed_long_types[type]:
924 %empty {
925 $type = NULL;
926 }
927 | INT
928 | LONG
929 ;
930
931 int_width_types[type]:
932 %empty {
933 $type = NULL;
934 }
935 | INT {
936 $type = psi_token_copy($INT);
937 }
938 | int_signed int_signed_types {
939 if ($2) {
940 $type = psi_token_cat(" ", 2, $1, $2);
941 free($2);
942 } else {
943 $type = psi_token_copy($1);
944 }
945 }
946 ;
947
948 decl_stmt:
949 decl decl_asm EOS {
950 $decl_stmt = $decl;
951 }
952 | CPP_EXTENSION decl decl_asm EOS {
953 $decl_stmt = $decl;
954 }
955 ;
956
957 decl_asm:
958 %empty
959 | CPP_ASM LPAREN ignored_quoted_strings RPAREN
960 ;
961
962 ignored_quoted_strings:
963 QUOTED_STRING
964 | ignored_quoted_strings QUOTED_STRING
965 ;
966
967 decl_ext_var_stmt:
968 decl_ext_var EOS
969 ;
970
971 decl_ext_var:
972 NAME decl_arg decl_ext_var_list {
973 psi_decl_arg_free(&$decl_arg);
974 }
975 ;
976
977 decl_ext_var_list:
978 %empty
979 | COMMA decl_vars {
980 psi_plist_free($decl_vars);
981 }
982 ;
983
984 decl:
985 decl_body
986 | NAME[abi] decl_body {
987 $decl = $decl_body;
988 $decl->abi = psi_decl_abi_init($abi->text);
989 }
990 ;
991
992 decl_body:
993 decl_func_body
994 | decl_functor_body
995 ;
996
997 decl_func_body[decl]:
998 decl_func[func] LPAREN decl_args[args] RPAREN array_size[as] {
999 $decl = psi_decl_init($func, $args);
1000 if ($as) {
1001 $decl->func->var->pointer_level += 1;
1002 $decl->func->var->array_size = $as;
1003 }
1004 }
1005 | decl_func[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
1006 $decl = psi_decl_init($func, $args);
1007 $decl->varargs = 1;
1008 if ($as) {
1009 $decl->func->var->pointer_level += 1;
1010 $decl->func->var->array_size = $as;
1011 }
1012 }
1013 ;
1014
1015 decl_functor_body[decl]:
1016 decl_functor[func] LPAREN decl_args[args] RPAREN array_size[as] {
1017 $decl = psi_decl_init($func, $args);
1018 if ($as) {
1019 $decl->func->var->pointer_level += 1;
1020 $decl->func->var->array_size = $as;
1021 }
1022 }
1023 | decl_functor[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
1024 $decl = psi_decl_init($func, $args);
1025 $decl->varargs = 1;
1026 if ($as) {
1027 $decl->func->var->pointer_level += 1;
1028 $decl->func->var->array_size = $as;
1029 }
1030 }
1031 ;
1032
1033 decl_functor[arg]:
1034 const_decl_type[type] indirection[i] LPAREN indirection[unused1] name_token[NAME] RPAREN {
1035 (void) $unused1;
1036 $NAME->type = PSI_T_NAME;
1037 $arg = psi_decl_arg_init($type, psi_decl_var_init($NAME->text, $i, 0));
1038 $arg->var->token = psi_token_copy($NAME);
1039 $arg->token = psi_token_copy($NAME);
1040 }
1041 | CONST VOID pointers LPAREN indirection[unused1] name_token[NAME] RPAREN {
1042 (void) $unused1;
1043 $NAME->type = PSI_T_NAME;
1044 $arg = psi_decl_arg_init(
1045 psi_decl_type_init($VOID->type, $VOID->text),
1046 psi_decl_var_init($NAME->text, $pointers, 0)
1047 );
1048 $arg->type->token = psi_token_copy($VOID);
1049 $arg->var->token = psi_token_copy($NAME);
1050 $arg->token = psi_token_copy($NAME);
1051 }
1052 | VOID pointers LPAREN indirection[unused1] name_token[NAME] RPAREN {
1053 (void) $unused1;
1054 $NAME->type = PSI_T_NAME;
1055 $arg = psi_decl_arg_init(
1056 psi_decl_type_init($VOID->type, $VOID->text),
1057 psi_decl_var_init($NAME->text, $pointers, 0)
1058 );
1059 $arg->type->token = psi_token_copy($VOID);
1060 $arg->var->token = psi_token_copy($NAME);
1061 $arg->token = psi_token_copy($NAME);
1062 }
1063 | VOID LPAREN indirection[unused1] name_token[NAME] RPAREN {
1064 (void) $unused1;
1065 $NAME->type = PSI_T_NAME;
1066 $arg = psi_decl_arg_init(
1067 psi_decl_type_init($VOID->type, $VOID->text),
1068 psi_decl_var_init($NAME->text, 0, 0)
1069 );
1070 $arg->type->token = psi_token_copy($VOID);
1071 $arg->var->token = psi_token_copy($NAME);
1072 $arg->token = psi_token_copy($NAME);
1073 }
1074 ;
1075
1076 decl_func[func]:
1077 decl_arg
1078 | VOID name_token[NAME] {
1079 $func = psi_decl_arg_init(
1080 psi_decl_type_init($VOID->type, $VOID->text),
1081 psi_decl_var_init($NAME->text, 0, 0)
1082 );
1083 $func->type->token = psi_token_copy($VOID);
1084 $func->var->token = psi_token_copy($NAME);
1085 $func->token = psi_token_copy($NAME);
1086 }
1087 ;
1088
1089 decl_args[args]:
1090 %empty {
1091 $args = NULL;
1092 }
1093 | VOID {
1094 $args = NULL;
1095 }
1096 | decl_arg[arg] {
1097 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &$arg);
1098 }
1099 | decl_args[args_] COMMA decl_arg[arg] {
1100 $args = psi_plist_add($args_, &$arg);
1101 }
1102 ;
1103
1104 decl_arg[arg]:
1105 decl_functor_body[decl] {
1106 $arg = psi_decl_arg_init(
1107 psi_decl_type_init(PSI_T_FUNCTION, $decl->func->var->name),
1108 psi_decl_var_copy($decl->func->var)
1109 );
1110 $arg->type->token = psi_token_copy($decl->func->token);
1111 $arg->type->real.func = $decl;
1112 }
1113 | const_decl_type[type] decl_var[var] {
1114 $arg = psi_decl_arg_init($type, $var);
1115 }
1116 | CONST VOID pointers name_token[NAME] {
1117 $NAME->type = PSI_T_NAME;
1118 $arg = psi_decl_arg_init(
1119 psi_decl_type_init($VOID->type, $VOID->text),
1120 psi_decl_var_init($NAME->text, $pointers, 0)
1121 );
1122 $arg->type->token = psi_token_copy($VOID);
1123 $arg->var->token = psi_token_copy($NAME);
1124 $arg->token = psi_token_copy($NAME);
1125 }
1126 | VOID pointers name_token[NAME] {
1127 $NAME->type = PSI_T_NAME;
1128 $arg = psi_decl_arg_init(
1129 psi_decl_type_init($VOID->type, $VOID->text),
1130 psi_decl_var_init($NAME->text, $pointers, 0)
1131 );
1132 $arg->type->token = psi_token_copy($VOID);
1133 $arg->var->token = psi_token_copy($NAME);
1134 $arg->token = psi_token_copy($NAME);
1135 }
1136 ;
1137
1138 decl_var[var]:
1139 pointers name_token array_size {
1140 $name_token->type = PSI_T_NAME;
1141 $var = psi_decl_var_init($name_token->text, $pointers + !! $array_size, $array_size);
1142 $var->token = psi_token_copy($name_token);
1143 }
1144 | name_token array_size {
1145 $name_token->type = PSI_T_NAME;
1146 $var = psi_decl_var_init($name_token->text, !! $array_size, $array_size);
1147 $var->token = psi_token_copy($name_token);
1148 }
1149 ;
1150
1151 decl_union[union]:
1152 UNION name_token align_and_size[as] decl_struct_args[args] {
1153 $name_token->type = PSI_T_NAME;
1154 $union = psi_decl_union_init($name_token->text, $args);
1155 $union->align = $as.pos;
1156 $union->size = $as.len;
1157 $union->token = psi_token_copy($name_token);
1158 }
1159 ;
1160
1161 decl_struct[struct]:
1162 STRUCT name_token align_and_size[as] decl_struct_args[args] {
1163 $name_token->type = PSI_T_NAME;
1164 $struct = psi_decl_struct_init($name_token->text, $args);
1165 $struct->align = $as.pos;
1166 $struct->size = $as.len;
1167 $struct->token = psi_token_copy($name_token);
1168 }
1169 ;
1170
1171 decl_struct_args[args]:
1172 EOS {
1173 $args = NULL;
1174 }
1175 | struct_args_block
1176 ;
1177
1178 struct_args_block[args]:
1179 LBRACE struct_args RBRACE {
1180 $args = $struct_args;
1181 }
1182 ;
1183
1184 struct_args[args]:
1185 struct_arg[arg] {
1186 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &$arg);
1187 }
1188 | struct_args[args_] struct_arg[arg] {
1189 $args = psi_plist_add($args_, &$arg);
1190 }
1191 ;
1192
1193 struct_arg[arg]:
1194 typedef[arg_] decl_layout[layout] EOS {
1195 $arg = $arg_;
1196 $arg->layout = $layout;
1197 psi_parser_proc_add_from_typedef(P, $arg);
1198 }
1199 ;
1200
1201 decl_enum[enum]:
1202 enum_name LBRACE decl_enum_items[list] RBRACE {
1203 $enum = psi_decl_enum_init($enum_name->text, $list);
1204 $enum->token = $enum_name;
1205 }
1206 ;
1207
1208 decl_enum_items[list]:
1209 decl_enum_item[i] {
1210 $list = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_enum_item_free), &$i);
1211 }
1212 | decl_enum_items[list_] COMMA decl_enum_item[i] {
1213 $list = psi_plist_add($list_, &$i);
1214 }
1215 ;
1216
1217 decl_enum_item[i]:
1218 name_token {
1219 $name_token->type = PSI_T_NAME;
1220 $i = psi_decl_enum_item_init($name_token->text, NULL);
1221 $i->token = psi_token_copy($name_token);
1222 }
1223 | name_token EQUALS num_exp {
1224 $name_token->type = PSI_T_NAME;
1225 $i = psi_decl_enum_item_init($name_token->text, $num_exp);
1226 $i->token = psi_token_copy($name_token);
1227 }
1228 ;
1229
1230 num_exp[exp]:
1231 number {
1232 $exp = psi_num_exp_init_num($number);
1233 $exp->token = psi_token_copy($number->token);
1234 }
1235 | LPAREN const_decl_type[type] RPAREN num_exp[exp_] %prec UNARY {
1236 $exp = psi_num_exp_init_cast($type, $exp_);
1237 $exp->token = psi_token_copy($type->token);
1238 }
1239 | LPAREN num_exp[exp_] RPAREN {
1240 $exp = psi_num_exp_init_unary(PSI_T_LPAREN, $exp_);
1241 $exp->token = psi_token_copy($LPAREN);
1242 }
1243 | num_exp[lhs] binary_op_token[op] num_exp[rhs] %prec BINARY {
1244 $exp = psi_num_exp_init_binary($op->type, $lhs, $rhs);
1245 $exp->token = psi_token_copy($op);
1246 }
1247 | unary_op_token[op] num_exp[exp_] %prec UNARY {
1248 $exp = psi_num_exp_init_unary($op->type, $exp_);
1249 $exp->token = psi_token_copy($op);
1250 }
1251 | num_exp[cond] IIF num_exp[truthy] COLON num_exp[falsy] {
1252 $exp = psi_num_exp_init_ternary($IIF->type, $cond, $truthy, $falsy);
1253 $exp->token = psi_token_copy($IIF);
1254 }
1255 ;
1256
1257 number[num]:
1258 NUMBER[token] {
1259 $num = psi_number_init($token->type, $token->text, $token->flags);
1260 $num->token = psi_token_copy($token);
1261 }
1262 | NSNAME[token] {
1263 $num = psi_number_init($token->type, $token->text, 0);
1264 $num->token = psi_token_copy($token);
1265 }
1266 | QUOTED_CHAR[token] {
1267 $num = psi_number_init($token->type, $token->text, 0);
1268 $num->token = psi_token_copy($token);
1269 }
1270 | decl_var {
1271 $num = psi_number_init(PSI_T_NAME, $decl_var, 0);
1272 $num->token = psi_token_copy($decl_var->token);
1273 }
1274 ;
1275
1276 enum_name[name]:
1277 ENUM optional_name[name_] {
1278 if ($name_) {
1279 $name = psi_token_copy($name_);
1280 } else {
1281 char digest[17];
1282
1283 psi_token_hash($ENUM, digest);
1284 $name = psi_token_append("@", psi_token_copy($ENUM), 1, digest);
1285 }
1286 }
1287 ;
1288
1289 union_name[name]:
1290 UNION optional_name[name_] {
1291 if ($name_) {
1292 $name = psi_token_copy($name_);
1293 } else {
1294 char digest[17];
1295
1296 psi_token_hash($UNION, digest);
1297 $name = psi_token_append("@", psi_token_copy($UNION), 1, digest);
1298 }
1299 }
1300 ;
1301
1302 struct_name[name]:
1303 STRUCT optional_name[name_] {
1304 if ($name_) {
1305 $name = psi_token_copy($name_);
1306 } else {
1307 char digest[17];
1308
1309 psi_token_hash($STRUCT, digest);
1310 $name = psi_token_append("@", psi_token_copy($STRUCT), 1, digest);
1311 }
1312 }
1313 ;
1314
1315 optional_name[name]:
1316 %empty {
1317 $name = NULL;
1318 }
1319 | name_token {
1320 $name = $name_token;
1321 $name->type = PSI_T_NAME;
1322 }
1323 ;
1324
1325 decl_layout[l]:
1326 %empty {
1327 $l = NULL;
1328 }
1329 | COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN {
1330 $l = psi_layout_init(atol($align->text), atol($size->text));
1331 }
1332 ;
1333
1334 align_and_size[as]:
1335 %empty {
1336 $as.pos = 0;
1337 $as.len = 0;
1338 }
1339 | COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN {
1340 $as.pos = atol($align->text);
1341 $as.len = atol($size->text);
1342 }
1343 ;
1344
1345 array_size[as]:
1346 %empty {
1347 $as = 0;
1348 }
1349 | LBRACKET NUMBER RBRACKET {
1350 $as = atol($NUMBER->text);
1351 }
1352 ;
1353
1354 indirection[i]:
1355 %empty {
1356 $i = 0;
1357 }
1358 | pointers[p] %prec UNARY {
1359 $i = $p;
1360 }
1361 ;
1362
1363 pointers[p]:
1364 asterisks
1365 | asterisks[a] CPP_RESTRICT {
1366 $p = $a;
1367 }
1368 ;
1369
1370 asterisks[a]:
1371 ASTERISK {
1372 $a = 1;
1373 }
1374 | asterisks[a_] ASTERISK {
1375 $a = $a_ + 1;
1376 }
1377 ;
1378
1379 /*
1380 *
1381 * impl
1382 *
1383 */
1384
1385 impl:
1386 impl_func[func] LBRACE impl_stmts[stmts] RBRACE {
1387 $impl = psi_impl_init($func, $stmts);
1388 }
1389 | STATIC impl_func[func] LBRACE impl_stmts[stmts] RBRACE {
1390 $impl = psi_impl_init($func, $stmts);
1391 $func->static_memory = 1;
1392 }
1393 ;
1394
1395 impl_func[func]:
1396 FUNCTION reference[r] NSNAME LPAREN RPAREN COLON impl_type[type] {
1397 $func = psi_impl_func_init($NSNAME->text, NULL, $type);
1398 $func->token = psi_token_copy($NSNAME);
1399 $func->return_reference = $r;
1400 }
1401 | FUNCTION reference[r] NSNAME LPAREN impl_args[args] RPAREN COLON impl_type[type] {
1402 $func = psi_impl_func_init($NSNAME->text, $args, $type);
1403 $func->token = psi_token_copy($NSNAME);
1404 $func->return_reference = $r;
1405 }
1406 | FUNCTION reference[r] NSNAME LPAREN impl_args[args] COMMA impl_type[va_type] reference[va_r] ELLIPSIS DOLLAR_NAME RPAREN COLON impl_type[type] {
1407 $func = psi_impl_func_init($NSNAME->text, $args, $type);
1408 $func->token = psi_token_copy($NSNAME);
1409 $func->return_reference = $r;
1410 $func->vararg = psi_impl_arg_init($va_type, psi_impl_var_init($DOLLAR_NAME->text, $va_r), NULL);
1411 $func->vararg->var->token = psi_token_copy($DOLLAR_NAME);
1412 }
1413 ;
1414
1415 impl_args[args]:
1416 impl_arg[arg] {
1417 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_arg_free), &$arg);
1418 }
1419 | impl_args[args_] COMMA impl_arg[arg] {
1420 $args = psi_plist_add($args_, &$arg);
1421 }
1422 ;
1423
1424 impl_arg[arg]:
1425 impl_type[type] impl_var[var] {
1426 $arg = psi_impl_arg_init($type, $var, NULL);
1427 }
1428 | impl_type[type] impl_var[var] EQUALS impl_def_val[val] {
1429 $arg = psi_impl_arg_init($type, $var, $val);
1430 }
1431 ;
1432
1433 impl_var[var]:
1434 reference DOLLAR_NAME {
1435 $var = psi_impl_var_init($DOLLAR_NAME->text, $reference);
1436 $var->token = psi_token_copy($DOLLAR_NAME);
1437 }
1438 ;
1439
1440 impl_type[type]:
1441 impl_type_token[token] {
1442 $type = psi_impl_type_init($token->type, $token->text);
1443 $type->token = psi_token_copy($token);
1444 }
1445 ;
1446
1447 impl_type_token:
1448 VOID
1449 | MIXED
1450 | BOOL
1451 | INT
1452 | FLOAT
1453 | STRING
1454 | ARRAY
1455 | OBJECT
1456 | CALLABLE
1457 ;
1458
1459 impl_stmts[stmts]:
1460 impl_stmt[stmt] {
1461 $stmts = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_stmt_free), &$stmt);
1462 }
1463 | impl_stmts[stmts_] impl_stmt[stmt] {
1464 $stmts = psi_plist_add($stmts_, &$stmt);
1465 }
1466 ;
1467
1468 impl_stmt[stmt]:
1469 return_stmt[s] {
1470 $stmt = (struct psi_token **) $s;
1471 }
1472 | let_stmt[s] {
1473 $stmt = (struct psi_token **) $s;
1474 }
1475 | set_stmt[s] {
1476 $stmt = (struct psi_token **) $s;
1477 }
1478 | assert_stmt[s] {
1479 $stmt = (struct psi_token **) $s;
1480 }
1481 | free_stmt[s] {
1482 $stmt = (struct psi_token **) $s;
1483 }
1484 ;
1485
1486 let_stmt[let]:
1487 LET let_exp_assign[exp] EOS {
1488 $let = psi_let_stmt_init($exp);
1489 $let->token = psi_token_copy($LET);
1490 }
1491 | TEMP decl_var[var] EQUALS reference decl_var[val] EOS {
1492 $let = psi_let_stmt_init(psi_let_exp_init_ex($var, PSI_LET_TMP, $val));
1493 $let->token = psi_token_copy($TEMP);
1494 $let->exp->is_reference = $reference;
1495 }
1496 ;
1497
1498 let_exp[exp]:
1499 let_exp_assign
1500 | byref let_exp_byref[exp_] {
1501 $exp = $exp_;
1502 $exp->is_reference = true;
1503 }
1504 | let_exp_byref[exp_] {
1505 $exp = $exp_;
1506 $exp->is_reference = false;
1507 }
1508 ;
1509
1510 let_exp_byref[exp]:
1511 NULL {
1512 $exp = psi_let_exp_init(PSI_LET_NULL, NULL);
1513 }
1514 | let_calloc[calloc] {
1515 $exp = psi_let_exp_init(PSI_LET_CALLOC, $calloc);
1516 }
1517 | let_callback[callback] {
1518 $exp = psi_let_exp_init(PSI_LET_CALLBACK, $callback);
1519 }
1520 | let_func[func] {
1521 $exp = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, $func);
1522 }
1523 | num_exp[num] {
1524 $exp = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, $num);
1525 }
1526 ;
1527
1528 let_exp_assign[exp]:
1529 decl_var[var] EQUALS let_exp_byref[exp_] {
1530 $exp = $exp_;
1531 $exp->var = $var;
1532 }
1533 | decl_var[var] EQUALS byref let_exp_byref[exp_] {
1534 $exp = $exp_;
1535 $exp->is_reference = 1;
1536 $exp->var = $var;
1537 }
1538 ;
1539
1540 let_calloc[calloc]:
1541 CALLOC LPAREN num_exp[nmemb] COMMA num_exp[size] RPAREN {
1542 $calloc = psi_let_calloc_init($nmemb, $size);
1543 $calloc->token = psi_token_copy($CALLOC);
1544 }
1545 ;
1546
1547 let_callback[callback]:
1548 CALLBACK callback_rval[func] LPAREN impl_var[var] LPAREN callback_arg_list[args] RPAREN RPAREN {
1549 $callback = psi_let_callback_init(psi_let_func_init($func->type, $func->text, $var), $args);
1550 $callback->func->token = psi_token_copy($func);
1551 $callback->token = psi_token_copy($CALLBACK);
1552 }
1553 ;
1554
1555 let_func[func]:
1556 let_func_token[token] LPAREN impl_var[var] let_func_exps[exps] RPAREN {
1557 $func = psi_let_func_init($token->type, $token->text, $var);
1558 $func->token = psi_token_copy($token);
1559 $func->inner = $exps;
1560 }
1561 ;
1562
1563 let_func_token:
1564 ZVAL
1565 | OBJVAL
1566 | ARRVAL
1567 | PATHVAL
1568 | STRLEN
1569 | STRVAL
1570 | FLOATVAL
1571 | INTVAL
1572 | BOOLVAL
1573 | COUNT
1574 ;
1575
1576 let_func_exps[exps]:
1577 %empty {
1578 $exps = NULL;
1579 }
1580 | COMMA let_exps[exps_] {
1581 $exps = $exps_;
1582 }
1583 ;
1584
1585 let_exps[exps]:
1586 let_exp[exp] {
1587 $exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_let_exp_free), &$exp);
1588 }
1589 | let_exps[exps_] COMMA let_exp[exp] {
1590 $exps = psi_plist_add($exps_, &$exp);
1591 }
1592 ;
1593
1594 callback_rval[rval]:
1595 VOID
1596 | let_func_token
1597 ;
1598
1599 callback_arg_list[list]:
1600 %empty {
1601 $list = NULL;
1602 }
1603 | callback_args[args] {
1604 $list = $args;
1605 }
1606 ;
1607
1608 callback_args[args]:
1609 set_exp[exp] {
1610 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free), &$exp);
1611 }
1612 | callback_args[args_] COMMA set_exp[exp] {
1613 $args = psi_plist_add($args_, &$exp);
1614 }
1615 ;
1616
1617 return_stmt[return]:
1618 RETURN set_func[func] EOS {
1619 $return = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, $func));
1620 $return->token = psi_token_copy($RETURN);
1621 }
1622 ;
1623
1624 set_stmt[set]:
1625 SET set_exp[exp] EOS {
1626 $set = psi_set_stmt_init($exp);
1627 $set->token = psi_token_copy($SET);
1628 }
1629 ;
1630
1631 set_exp[exp]:
1632 set_func[func] {
1633 $exp = psi_set_exp_init(PSI_SET_FUNC, $func);
1634 }
1635 | num_exp[num] {
1636 $exp = psi_set_exp_init(PSI_SET_NUMEXP, $num);
1637 }
1638 | impl_var[var] EQUALS set_exp[exp_] {
1639 $exp = $exp_;
1640 $exp->var = $var;
1641 }
1642 ;
1643
1644 set_func[func]:
1645 set_func_token[token] LPAREN decl_var[var] set_func_exps[exps] RPAREN {
1646 $func = psi_set_func_init($token->type, $token->text, $var);
1647 $func->token = psi_token_copy($token);
1648 $func->inner = $exps;
1649 }
1650 | set_func_token[token] LPAREN decl_var[var] COMMA ELLIPSIS RPAREN {
1651 $func = psi_set_func_init($token->type, $token->text, $var);
1652 $func->token = psi_token_copy($token);
1653 $func->recursive = 1;
1654 }
1655 ;
1656
1657 set_func_token:
1658 TO_OBJECT
1659 | TO_ARRAY
1660 | TO_STRING
1661 | TO_INT
1662 | TO_FLOAT
1663 | TO_BOOL
1664 | ZVAL
1665 | VOID
1666 ;
1667
1668 set_func_exps[exps]:
1669 %empty {
1670 $exps = NULL;
1671 }
1672 | COMMA set_exps[exps_] {
1673 $exps = $exps_;
1674 }
1675 ;
1676
1677 set_exps[exps]:
1678 set_exp[exp] {
1679 $exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free), &$exp);
1680 }
1681 | set_exps[exps_] COMMA set_exp[exp] {
1682 $exps = psi_plist_add($exps_, &$exp);
1683 }
1684 ;
1685
1686 assert_stmt[assert]:
1687 assert_stmt_token[token] num_exp[exp] EOS {
1688 $assert = psi_assert_stmt_init((enum psi_assert_kind) $token->type, $exp);
1689 $assert->token = psi_token_copy($token);
1690 }
1691 ;
1692
1693 assert_stmt_token:
1694 PRE_ASSERT
1695 | POST_ASSERT
1696 ;
1697
1698 free_stmt[free]:
1699 FREE free_exps[exps] EOS {
1700 $free = psi_free_stmt_init($exps);
1701 $free->token = psi_token_copy($FREE);
1702 }
1703 ;
1704
1705 free_exps[exps]:
1706 free_exp[exp] {
1707 $exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free), &$exp);
1708 }
1709 | free_exps[exps_] COMMA free_exp[exp] {
1710 $exps = psi_plist_add($exps_, &$exp);
1711 }
1712 ;
1713
1714 free_exp[exp]:
1715 name_token[NAME] LPAREN decl_vars[vars] RPAREN {
1716 $NAME->type = PSI_T_NAME;
1717 $exp = psi_free_exp_init($NAME->text, $vars);
1718 $exp->token = psi_token_copy($NAME);
1719 }
1720 ;
1721
1722 decl_vars[vars]:
1723 decl_var[var] {
1724 $vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free), &$var);
1725 }
1726 | decl_vars[vars_] COMMA decl_var[var] {
1727 $vars = psi_plist_add($vars_, &$var);
1728 }
1729 ;
1730
1731 reference:
1732 %empty {
1733 $reference = false;
1734 }
1735 | byref {
1736 $reference = true;
1737 }
1738 ;
1739
1740 byref:
1741 AMPERSAND %prec UNARY
1742 ;
1743
1744 %%
1745
1746 /* epilogue */
1747
1748 static int psi_parser_proc_lex(YYSTYPE *lvalp, struct psi_parser *P, struct psi_plist *tokens, size_t *index)
1749 {
1750 struct psi_token *token;
1751
1752 if (psi_plist_get(tokens, (*index)++, &token)) {
1753 if (P->flags & PSI_DEBUG) {
1754 psi_token_dump(2, token);
1755 }
1756
1757 *((struct psi_token **)lvalp) = token;
1758 return token->type;
1759 } else {
1760 (*index)--;
1761 PSI_DEBUG_PRINT(P, "EOF(%d)\n", PSI_T_EOF);
1762 }
1763
1764 return PSI_T_EOF;
1765 }
1766
1767 static void psi_parser_proc_error(struct psi_parser *P, struct psi_plist *tokens, size_t *index, const char *msg)
1768 {
1769 struct psi_token *T = NULL;
1770 size_t last;
1771
1772 if (*index == 0) {
1773 last = 0;
1774 } else {
1775 last = --(*index);
1776 }
1777
1778 psi_plist_get(tokens, last, &T);
1779 if (T) {
1780 P->error(PSI_DATA(P), T, PSI_WARNING, "PSI %s at col %u", msg, T->col);
1781 } else {
1782 P->error(PSI_DATA(P), NULL, PSI_WARNING, "PSI %s", msg);
1783 }
1784 P->errors++;
1785 }
1786