442f93d84f070bf037e73d216e831ac48a694c77
[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
313 %destructor {psi_decl_free(&$$);} decl_stmt decl
314 %type <struct psi_decl_arg *> decl_typedef decl_fn decl_func decl_functor decl_arg struct_arg typedef
315 %destructor {psi_decl_arg_free(&$$);} decl_typedef decl_fn 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 {
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_fn[func] LPAREN decl_args[args] RPAREN array_size[as] {
986 $decl = psi_decl_init(psi_decl_abi_init("default"), $func, $args);
987 if ($as) {
988 $decl->func->var->pointer_level += 1;
989 $decl->func->var->array_size = $as;
990 }
991 }
992 | decl_fn[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
993 $decl = psi_decl_init(psi_decl_abi_init("default"), $func, $args);
994 $decl->varargs = 1;
995 if ($as) {
996 $decl->func->var->pointer_level += 1;
997 $decl->func->var->array_size = $as;
998 }
999 }
1000 | NAME[abi] decl_fn[func] LPAREN decl_args[args] RPAREN array_size[as] {
1001 $decl = psi_decl_init(psi_decl_abi_init($abi->text), $func, $args);
1002 if ($as) {
1003 $decl->func->var->pointer_level += 1;
1004 $decl->func->var->array_size = $as;
1005 }
1006 }
1007 | NAME[abi] decl_fn[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
1008 $decl = psi_decl_init(psi_decl_abi_init($abi->text), $func, $args);
1009 $decl->varargs = 1;
1010 if ($as) {
1011 $decl->func->var->pointer_level += 1;
1012 $decl->func->var->array_size = $as;
1013 }
1014 }
1015 ;
1016
1017 decl_fn:
1018 decl_func
1019 | decl_functor
1020 ;
1021
1022 decl_functor[arg]:
1023 const_decl_type[type] indirection[i] LPAREN indirection[unused1] name_token[NAME] RPAREN {
1024 (void) $unused1;
1025 $NAME->type = PSI_T_NAME;
1026 $arg = psi_decl_arg_init($type, psi_decl_var_init($NAME->text, $i, 0));
1027 $arg->var->token = psi_token_copy($NAME);
1028 $arg->token = psi_token_copy($NAME);
1029 }
1030 | CONST VOID pointers LPAREN indirection[unused1] name_token[NAME] RPAREN {
1031 (void) $unused1;
1032 $NAME->type = PSI_T_NAME;
1033 $arg = psi_decl_arg_init(
1034 psi_decl_type_init($VOID->type, $VOID->text),
1035 psi_decl_var_init($NAME->text, $pointers, 0)
1036 );
1037 $arg->type->token = psi_token_copy($VOID);
1038 $arg->var->token = psi_token_copy($NAME);
1039 $arg->token = psi_token_copy($NAME);
1040 }
1041 | 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 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, 0, 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 ;
1064
1065 decl_func[func]:
1066 decl_arg
1067 | VOID name_token[NAME] {
1068 $func = psi_decl_arg_init(
1069 psi_decl_type_init($VOID->type, $VOID->text),
1070 psi_decl_var_init($NAME->text, 0, 0)
1071 );
1072 $func->type->token = psi_token_copy($VOID);
1073 $func->var->token = psi_token_copy($NAME);
1074 $func->token = psi_token_copy($NAME);
1075 }
1076 ;
1077
1078 decl_args[args]:
1079 %empty {
1080 $args = NULL;
1081 }
1082 | VOID {
1083 $args = NULL;
1084 }
1085 | decl_arg[arg] {
1086 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &$arg);
1087 }
1088 | decl_args[args_] COMMA decl_arg[arg] {
1089 $args = psi_plist_add($args_, &$arg);
1090 }
1091 ;
1092
1093 decl_arg[arg]:
1094 const_decl_type[type] decl_var[var] {
1095 $arg = psi_decl_arg_init($type, $var);
1096 }
1097 | CONST VOID pointers name_token[NAME] {
1098 $NAME->type = PSI_T_NAME;
1099 $arg = psi_decl_arg_init(
1100 psi_decl_type_init($VOID->type, $VOID->text),
1101 psi_decl_var_init($NAME->text, $pointers, 0)
1102 );
1103 $arg->type->token = psi_token_copy($VOID);
1104 $arg->var->token = psi_token_copy($NAME);
1105 $arg->token = psi_token_copy($NAME);
1106 }
1107 | VOID pointers name_token[NAME] {
1108 $NAME->type = PSI_T_NAME;
1109 $arg = psi_decl_arg_init(
1110 psi_decl_type_init($VOID->type, $VOID->text),
1111 psi_decl_var_init($NAME->text, $pointers, 0)
1112 );
1113 $arg->type->token = psi_token_copy($VOID);
1114 $arg->var->token = psi_token_copy($NAME);
1115 $arg->token = psi_token_copy($NAME);
1116 }
1117 ;
1118
1119 decl_var[var]:
1120 pointers name_token array_size {
1121 $name_token->type = PSI_T_NAME;
1122 $var = psi_decl_var_init($name_token->text, $pointers + !! $array_size, $array_size);
1123 $var->token = psi_token_copy($name_token);
1124 }
1125 | name_token array_size {
1126 $name_token->type = PSI_T_NAME;
1127 $var = psi_decl_var_init($name_token->text, !! $array_size, $array_size);
1128 $var->token = psi_token_copy($name_token);
1129 }
1130 ;
1131
1132 decl_union[union]:
1133 UNION name_token align_and_size[as] decl_struct_args[args] {
1134 $name_token->type = PSI_T_NAME;
1135 $union = psi_decl_union_init($name_token->text, $args);
1136 $union->align = $as.pos;
1137 $union->size = $as.len;
1138 $union->token = psi_token_copy($name_token);
1139 }
1140 ;
1141
1142 decl_struct[struct]:
1143 STRUCT name_token align_and_size[as] decl_struct_args[args] {
1144 $name_token->type = PSI_T_NAME;
1145 $struct = psi_decl_struct_init($name_token->text, $args);
1146 $struct->align = $as.pos;
1147 $struct->size = $as.len;
1148 $struct->token = psi_token_copy($name_token);
1149 }
1150 ;
1151
1152 decl_struct_args[args]:
1153 EOS {
1154 $args = NULL;
1155 }
1156 | struct_args_block
1157 ;
1158
1159 struct_args_block[args]:
1160 LBRACE struct_args RBRACE {
1161 $args = $struct_args;
1162 }
1163 ;
1164
1165 struct_args[args]:
1166 struct_arg[arg] {
1167 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &$arg);
1168 }
1169 | struct_args[args_] struct_arg[arg] {
1170 $args = psi_plist_add($args_, &$arg);
1171 }
1172 ;
1173
1174 struct_arg[arg]:
1175 typedef[arg_] decl_layout[layout] EOS {
1176 $arg = $arg_;
1177 $arg->layout = $layout;
1178 psi_parser_proc_add_from_typedef(P, $arg);
1179 }
1180 ;
1181
1182 decl_enum[enum]:
1183 enum_name LBRACE decl_enum_items[list] RBRACE {
1184 $enum = psi_decl_enum_init($enum_name->text, $list);
1185 $enum->token = $enum_name;
1186 }
1187 ;
1188
1189 decl_enum_items[list]:
1190 decl_enum_item[i] {
1191 $list = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_enum_item_free), &$i);
1192 }
1193 | decl_enum_items[list_] COMMA decl_enum_item[i] {
1194 $list = psi_plist_add($list_, &$i);
1195 }
1196 ;
1197
1198 decl_enum_item[i]:
1199 name_token {
1200 $name_token->type = PSI_T_NAME;
1201 $i = psi_decl_enum_item_init($name_token->text, NULL);
1202 $i->token = psi_token_copy($name_token);
1203 }
1204 | name_token EQUALS num_exp {
1205 $name_token->type = PSI_T_NAME;
1206 $i = psi_decl_enum_item_init($name_token->text, $num_exp);
1207 $i->token = psi_token_copy($name_token);
1208 }
1209 ;
1210
1211 num_exp[exp]:
1212 number {
1213 $exp = psi_num_exp_init_num($number);
1214 $exp->token = psi_token_copy($number->token);
1215 }
1216 | LPAREN const_decl_type[type] RPAREN num_exp[exp_] %prec UNARY {
1217 $exp = psi_num_exp_init_cast($type, $exp_);
1218 $exp->token = psi_token_copy($type->token);
1219 }
1220 | LPAREN num_exp[exp_] RPAREN {
1221 $exp = psi_num_exp_init_unary(PSI_T_LPAREN, $exp_);
1222 $exp->token = psi_token_copy($LPAREN);
1223 }
1224 | num_exp[lhs] binary_op_token[op] num_exp[rhs] %prec BINARY {
1225 $exp = psi_num_exp_init_binary($op->type, $lhs, $rhs);
1226 $exp->token = psi_token_copy($op);
1227 }
1228 | unary_op_token[op] num_exp[exp_] %prec UNARY {
1229 $exp = psi_num_exp_init_unary($op->type, $exp_);
1230 $exp->token = psi_token_copy($op);
1231 }
1232 | num_exp[cond] IIF num_exp[truthy] COLON num_exp[falsy] {
1233 $exp = psi_num_exp_init_ternary($IIF->type, $cond, $truthy, $falsy);
1234 $exp->token = psi_token_copy($IIF);
1235 }
1236 ;
1237
1238 number[num]:
1239 NUMBER[token] {
1240 $num = psi_number_init($token->type, $token->text, $token->flags);
1241 $num->token = psi_token_copy($token);
1242 }
1243 | NSNAME[token] {
1244 $num = psi_number_init($token->type, $token->text, 0);
1245 $num->token = psi_token_copy($token);
1246 }
1247 | QUOTED_CHAR[token] {
1248 $num = psi_number_init($token->type, $token->text, 0);
1249 $num->token = psi_token_copy($token);
1250 }
1251 | decl_var {
1252 $num = psi_number_init(PSI_T_NAME, $decl_var, 0);
1253 $num->token = psi_token_copy($decl_var->token);
1254 }
1255 ;
1256
1257 enum_name[name]:
1258 ENUM optional_name[name_] {
1259 if ($name_) {
1260 $name = psi_token_copy($name_);
1261 } else {
1262 char digest[17];
1263
1264 psi_token_hash($ENUM, digest);
1265 $name = psi_token_append("@", psi_token_copy($ENUM), 1, digest);
1266 }
1267 }
1268 ;
1269
1270 union_name[name]:
1271 UNION optional_name[name_] {
1272 if ($name_) {
1273 $name = psi_token_copy($name_);
1274 } else {
1275 char digest[17];
1276
1277 psi_token_hash($UNION, digest);
1278 $name = psi_token_append("@", psi_token_copy($UNION), 1, digest);
1279 }
1280 }
1281 ;
1282
1283 struct_name[name]:
1284 STRUCT optional_name[name_] {
1285 if ($name_) {
1286 $name = psi_token_copy($name_);
1287 } else {
1288 char digest[17];
1289
1290 psi_token_hash($STRUCT, digest);
1291 $name = psi_token_append("@", psi_token_copy($STRUCT), 1, digest);
1292 }
1293 }
1294 ;
1295
1296 optional_name[name]:
1297 %empty {
1298 $name = NULL;
1299 }
1300 | name_token {
1301 $name = $name_token;
1302 $name->type = PSI_T_NAME;
1303 }
1304 ;
1305
1306 decl_layout[l]:
1307 %empty {
1308 $l = NULL;
1309 }
1310 | COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN {
1311 $l = psi_layout_init(atol($align->text), atol($size->text));
1312 }
1313 ;
1314
1315 align_and_size[as]:
1316 %empty {
1317 $as.pos = 0;
1318 $as.len = 0;
1319 }
1320 | COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN {
1321 $as.pos = atol($align->text);
1322 $as.len = atol($size->text);
1323 }
1324 ;
1325
1326 array_size[as]:
1327 %empty {
1328 $as = 0;
1329 }
1330 | LBRACKET NUMBER RBRACKET {
1331 $as = atol($NUMBER->text);
1332 }
1333 ;
1334
1335 indirection[i]:
1336 %empty {
1337 $i = 0;
1338 }
1339 | pointers[p] %prec UNARY {
1340 $i = $p;
1341 }
1342 ;
1343
1344 pointers[p]:
1345 asterisks
1346 | asterisks[a] CPP_RESTRICT {
1347 $p = $a;
1348 }
1349 ;
1350
1351 asterisks[a]:
1352 ASTERISK {
1353 $a = 1;
1354 }
1355 | asterisks[a_] ASTERISK {
1356 $a = $a_ + 1;
1357 }
1358 ;
1359
1360 /*
1361 *
1362 * impl
1363 *
1364 */
1365
1366 impl:
1367 impl_func[func] LBRACE impl_stmts[stmts] RBRACE {
1368 $impl = psi_impl_init($func, $stmts);
1369 }
1370 | STATIC impl_func[func] LBRACE impl_stmts[stmts] RBRACE {
1371 $impl = psi_impl_init($func, $stmts);
1372 $func->static_memory = 1;
1373 }
1374 ;
1375
1376 impl_func[func]:
1377 FUNCTION reference[r] NSNAME LPAREN RPAREN COLON impl_type[type] {
1378 $func = psi_impl_func_init($NSNAME->text, NULL, $type);
1379 $func->token = psi_token_copy($NSNAME);
1380 $func->return_reference = $r;
1381 }
1382 | FUNCTION reference[r] NSNAME LPAREN impl_args[args] RPAREN COLON impl_type[type] {
1383 $func = psi_impl_func_init($NSNAME->text, $args, $type);
1384 $func->token = psi_token_copy($NSNAME);
1385 $func->return_reference = $r;
1386 }
1387 | FUNCTION reference[r] NSNAME LPAREN impl_args[args] COMMA impl_type[va_type] reference[va_r] ELLIPSIS DOLLAR_NAME RPAREN COLON impl_type[type] {
1388 $func = psi_impl_func_init($NSNAME->text, $args, $type);
1389 $func->token = psi_token_copy($NSNAME);
1390 $func->return_reference = $r;
1391 $func->vararg = psi_impl_arg_init($va_type, psi_impl_var_init($DOLLAR_NAME->text, $va_r), NULL);
1392 $func->vararg->var->token = psi_token_copy($DOLLAR_NAME);
1393 }
1394 ;
1395
1396 impl_args[args]:
1397 impl_arg[arg] {
1398 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_arg_free), &$arg);
1399 }
1400 | impl_args[args_] COMMA impl_arg[arg] {
1401 $args = psi_plist_add($args_, &$arg);
1402 }
1403 ;
1404
1405 impl_arg[arg]:
1406 impl_type[type] impl_var[var] {
1407 $arg = psi_impl_arg_init($type, $var, NULL);
1408 }
1409 | impl_type[type] impl_var[var] EQUALS impl_def_val[val] {
1410 $arg = psi_impl_arg_init($type, $var, $val);
1411 }
1412 ;
1413
1414 impl_var[var]:
1415 reference DOLLAR_NAME {
1416 $var = psi_impl_var_init($DOLLAR_NAME->text, $reference);
1417 $var->token = psi_token_copy($DOLLAR_NAME);
1418 }
1419 ;
1420
1421 impl_type[type]:
1422 impl_type_token[token] {
1423 $type = psi_impl_type_init($token->type, $token->text);
1424 $type->token = psi_token_copy($token);
1425 }
1426 ;
1427
1428 impl_type_token:
1429 VOID
1430 | MIXED
1431 | BOOL
1432 | INT
1433 | FLOAT
1434 | STRING
1435 | ARRAY
1436 | OBJECT
1437 | CALLABLE
1438 ;
1439
1440 impl_stmts[stmts]:
1441 impl_stmt[stmt] {
1442 $stmts = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_stmt_free), &$stmt);
1443 }
1444 | impl_stmts[stmts_] impl_stmt[stmt] {
1445 $stmts = psi_plist_add($stmts_, &$stmt);
1446 }
1447 ;
1448
1449 impl_stmt[stmt]:
1450 return_stmt[s] {
1451 $stmt = (struct psi_token **) $s;
1452 }
1453 | let_stmt[s] {
1454 $stmt = (struct psi_token **) $s;
1455 }
1456 | set_stmt[s] {
1457 $stmt = (struct psi_token **) $s;
1458 }
1459 | assert_stmt[s] {
1460 $stmt = (struct psi_token **) $s;
1461 }
1462 | free_stmt[s] {
1463 $stmt = (struct psi_token **) $s;
1464 }
1465 ;
1466
1467 let_stmt[let]:
1468 LET let_exp_assign[exp] EOS {
1469 $let = psi_let_stmt_init($exp);
1470 $let->token = psi_token_copy($LET);
1471 }
1472 | TEMP decl_var[var] EQUALS reference decl_var[val] EOS {
1473 $let = psi_let_stmt_init(psi_let_exp_init_ex($var, PSI_LET_TMP, $val));
1474 $let->token = psi_token_copy($TEMP);
1475 $let->exp->is_reference = $reference;
1476 }
1477 ;
1478
1479 let_exp[exp]:
1480 let_exp_assign
1481 | byref let_exp_byref[exp_] {
1482 $exp = $exp_;
1483 $exp->is_reference = true;
1484 }
1485 | let_exp_byref[exp_] {
1486 $exp = $exp_;
1487 $exp->is_reference = false;
1488 }
1489 ;
1490
1491 let_exp_byref[exp]:
1492 NULL {
1493 $exp = psi_let_exp_init(PSI_LET_NULL, NULL);
1494 }
1495 | let_calloc[calloc] {
1496 $exp = psi_let_exp_init(PSI_LET_CALLOC, $calloc);
1497 }
1498 | let_callback[callback] {
1499 $exp = psi_let_exp_init(PSI_LET_CALLBACK, $callback);
1500 }
1501 | let_func[func] {
1502 $exp = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, $func);
1503 }
1504 | num_exp[num] {
1505 $exp = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, $num);
1506 }
1507 ;
1508
1509 let_exp_assign[exp]:
1510 decl_var[var] EQUALS let_exp_byref[exp_] {
1511 $exp = $exp_;
1512 $exp->var = $var;
1513 }
1514 | decl_var[var] EQUALS byref let_exp_byref[exp_] {
1515 $exp = $exp_;
1516 $exp->is_reference = 1;
1517 $exp->var = $var;
1518 }
1519 ;
1520
1521 let_calloc[calloc]:
1522 CALLOC LPAREN num_exp[nmemb] COMMA num_exp[size] RPAREN {
1523 $calloc = psi_let_calloc_init($nmemb, $size);
1524 $calloc->token = psi_token_copy($CALLOC);
1525 }
1526 ;
1527
1528 let_callback[callback]:
1529 CALLBACK callback_rval[func] LPAREN impl_var[var] LPAREN callback_arg_list[args] RPAREN RPAREN {
1530 $callback = psi_let_callback_init(psi_let_func_init($func->type, $func->text, $var), $args);
1531 $callback->func->token = psi_token_copy($func);
1532 $callback->token = psi_token_copy($CALLBACK);
1533 }
1534 ;
1535
1536 let_func[func]:
1537 let_func_token[token] LPAREN impl_var[var] let_func_exps[exps] RPAREN {
1538 $func = psi_let_func_init($token->type, $token->text, $var);
1539 $func->token = psi_token_copy($token);
1540 $func->inner = $exps;
1541 }
1542 ;
1543
1544 let_func_token:
1545 ZVAL
1546 | OBJVAL
1547 | ARRVAL
1548 | PATHVAL
1549 | STRLEN
1550 | STRVAL
1551 | FLOATVAL
1552 | INTVAL
1553 | BOOLVAL
1554 | COUNT
1555 ;
1556
1557 let_func_exps[exps]:
1558 %empty {
1559 $exps = NULL;
1560 }
1561 | COMMA let_exps[exps_] {
1562 $exps = $exps_;
1563 }
1564 ;
1565
1566 let_exps[exps]:
1567 let_exp[exp] {
1568 $exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_let_exp_free), &$exp);
1569 }
1570 | let_exps[exps_] COMMA let_exp[exp] {
1571 $exps = psi_plist_add($exps_, &$exp);
1572 }
1573 ;
1574
1575 callback_rval[rval]:
1576 VOID
1577 | let_func_token
1578 ;
1579
1580 callback_arg_list[list]:
1581 %empty {
1582 $list = NULL;
1583 }
1584 | callback_args[args] {
1585 $list = $args;
1586 }
1587 ;
1588
1589 callback_args[args]:
1590 set_exp[exp] {
1591 $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free), &$exp);
1592 }
1593 | callback_args[args_] COMMA set_exp[exp] {
1594 $args = psi_plist_add($args_, &$exp);
1595 }
1596 ;
1597
1598 return_stmt[return]:
1599 RETURN set_func[func] EOS {
1600 $return = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, $func));
1601 $return->token = psi_token_copy($RETURN);
1602 }
1603 ;
1604
1605 set_stmt[set]:
1606 SET set_exp[exp] EOS {
1607 $set = psi_set_stmt_init($exp);
1608 $set->token = psi_token_copy($SET);
1609 }
1610 ;
1611
1612 set_exp[exp]:
1613 set_func[func] {
1614 $exp = psi_set_exp_init(PSI_SET_FUNC, $func);
1615 }
1616 | num_exp[num] {
1617 $exp = psi_set_exp_init(PSI_SET_NUMEXP, $num);
1618 }
1619 | impl_var[var] EQUALS set_exp[exp_] {
1620 $exp = $exp_;
1621 $exp->var = $var;
1622 }
1623 ;
1624
1625 set_func[func]:
1626 set_func_token[token] LPAREN decl_var[var] set_func_exps[exps] RPAREN {
1627 $func = psi_set_func_init($token->type, $token->text, $var);
1628 $func->token = psi_token_copy($token);
1629 $func->inner = $exps;
1630 }
1631 | set_func_token[token] LPAREN decl_var[var] COMMA ELLIPSIS RPAREN {
1632 $func = psi_set_func_init($token->type, $token->text, $var);
1633 $func->token = psi_token_copy($token);
1634 $func->recursive = 1;
1635 }
1636 ;
1637
1638 set_func_token:
1639 TO_OBJECT
1640 | TO_ARRAY
1641 | TO_STRING
1642 | TO_INT
1643 | TO_FLOAT
1644 | TO_BOOL
1645 | ZVAL
1646 | VOID
1647 ;
1648
1649 set_func_exps[exps]:
1650 %empty {
1651 $exps = NULL;
1652 }
1653 | COMMA set_exps[exps_] {
1654 $exps = $exps_;
1655 }
1656 ;
1657
1658 set_exps[exps]:
1659 set_exp[exp] {
1660 $exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free), &$exp);
1661 }
1662 | set_exps[exps_] COMMA set_exp[exp] {
1663 $exps = psi_plist_add($exps_, &$exp);
1664 }
1665 ;
1666
1667 assert_stmt[assert]:
1668 assert_stmt_token[token] num_exp[exp] EOS {
1669 $assert = psi_assert_stmt_init((enum psi_assert_kind) $token->type, $exp);
1670 $assert->token = psi_token_copy($token);
1671 }
1672 ;
1673
1674 assert_stmt_token:
1675 PRE_ASSERT
1676 | POST_ASSERT
1677 ;
1678
1679 free_stmt[free]:
1680 FREE free_exps[exps] EOS {
1681 $free = psi_free_stmt_init($exps);
1682 $free->token = psi_token_copy($FREE);
1683 }
1684 ;
1685
1686 free_exps[exps]:
1687 free_exp[exp] {
1688 $exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free), &$exp);
1689 }
1690 | free_exps[exps_] COMMA free_exp[exp] {
1691 $exps = psi_plist_add($exps_, &$exp);
1692 }
1693 ;
1694
1695 free_exp[exp]:
1696 name_token[NAME] LPAREN decl_vars[vars] RPAREN {
1697 $NAME->type = PSI_T_NAME;
1698 $exp = psi_free_exp_init($NAME->text, $vars);
1699 $exp->token = psi_token_copy($NAME);
1700 }
1701 ;
1702
1703 decl_vars[vars]:
1704 decl_var[var] {
1705 $vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free), &$var);
1706 }
1707 | decl_vars[vars_] COMMA decl_var[var] {
1708 $vars = psi_plist_add($vars_, &$var);
1709 }
1710 ;
1711
1712 reference:
1713 %empty {
1714 $reference = false;
1715 }
1716 | byref {
1717 $reference = true;
1718 }
1719 ;
1720
1721 byref:
1722 AMPERSAND %prec UNARY
1723 ;
1724
1725 %%
1726
1727 /* epilogue */
1728
1729 static int psi_parser_proc_lex(YYSTYPE *lvalp, struct psi_parser *P, struct psi_plist *tokens, size_t *index)
1730 {
1731 struct psi_token *token;
1732
1733 if (psi_plist_get(tokens, (*index)++, &token)) {
1734 if (P->flags & PSI_DEBUG) {
1735 psi_token_dump(2, token);
1736 }
1737
1738 *((struct psi_token **)lvalp) = token;
1739 return token->type;
1740 } else {
1741 (*index)--;
1742 PSI_DEBUG_PRINT(P, "EOF(%d)\n", PSI_T_EOF);
1743 }
1744
1745 return PSI_T_EOF;
1746 }
1747
1748 static void psi_parser_proc_error(struct psi_parser *P, struct psi_plist *tokens, size_t *index, const char *msg)
1749 {
1750 struct psi_token *T = NULL;
1751 size_t last;
1752
1753 if (*index == 0) {
1754 last = 0;
1755 } else {
1756 last = --(*index);
1757 }
1758
1759 psi_plist_get(tokens, last, &T);
1760 if (T) {
1761 P->error(PSI_DATA(P), T, PSI_WARNING, "PSI %s at col %u", msg, T->col);
1762 } else {
1763 P->error(PSI_DATA(P), NULL, PSI_WARNING, "PSI %s", msg);
1764 }
1765 P->errors++;
1766 }
1767