9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
17 typedef struct PSI_Token
{
24 typedef struct decl_type
{
27 struct decl_type
*real
;
30 static inline decl_type
*init_decl_type(token_t type
, char *name
) {
31 decl_type
*t
= malloc(sizeof(*t
));
33 t
->name
= strdup(name
);
38 static inline decl_type
*real_decl_type(decl_type
*type
) {
45 static inline void free_decl_type(decl_type
*type
) {
50 typedef struct decl_typedef
{
55 static inline decl_typedef
*init_decl_typedef(char *name
, decl_type
*type
) {
56 decl_typedef
*t
= malloc(sizeof(*t
));
57 t
->alias
= strdup(name
);
62 static inline void free_decl_typedef(decl_typedef
*t
) {
64 free_decl_type(t
->type
);
68 typedef struct decl_typedefs
{
73 static decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
75 defs
= calloc(1, sizeof(*defs
));
77 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
78 defs
->list
[defs
->count
-1] = def
;
82 static void free_decl_typedefs(decl_typedefs
*defs
) {
85 for (i
= 0; i
< defs
->count
; ++i
) {
86 free_decl_typedef(defs
->list
[i
]);
92 typedef union impl_val
{
102 typedef struct decl_var
{
104 unsigned pointer_level
;
105 struct decl_arg
*arg
;
108 static inline decl_var
*init_decl_var(char *name
, unsigned pl
) {
109 decl_var
*v
= malloc(sizeof(*v
));
110 v
->name
= (char *) strdup((const char *) name
);
111 v
->pointer_level
= pl
;
115 static inline void free_decl_var(decl_var
*var
) {
120 typedef struct decl_arg
{
123 struct let_stmt
*let
;
126 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
127 decl_arg
*arg
= malloc(sizeof(*arg
));
134 static inline void free_decl_arg(decl_arg
*arg
) {
135 free_decl_type(arg
->type
);
136 free_decl_var(arg
->var
);
140 typedef struct decl_vars
{
145 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
146 decl_vars
*vars
= malloc(sizeof(*vars
));
148 vars
->vars
= malloc(sizeof(*vars
->vars
));
153 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
154 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
155 vars
->vars
[vars
->count
-1] = var
;
159 static inline void free_decl_vars(decl_vars
*vars
) {
162 for (i
= 0; i
< vars
->count
; ++i
) {
163 free_decl_var(vars
->vars
[i
]);
169 typedef struct decl_args
{
174 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
175 decl_args
*args
= malloc(sizeof(*args
));
177 args
->args
= malloc(sizeof(*args
->args
));
182 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
183 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
184 args
->args
[args
->count
-1] = arg
;
188 static inline void free_decl_args(decl_args
*args
) {
191 for (i
= 0; i
< args
->count
; ++i
) {
192 free_decl_arg(args
->args
[i
]);
198 typedef struct decl_abi
{
202 static inline decl_abi
*init_decl_abi(char *convention
) {
203 decl_abi
*abi
= malloc(sizeof(*abi
));
204 abi
->convention
= strdup(convention
);
208 static inline void free_decl_abi(decl_abi
*abi
) {
209 free(abi
->convention
);
213 typedef struct decl
{
220 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
221 decl
*d
= malloc(sizeof(*d
));
228 static inline void free_decl(decl
*d
) {
229 free_decl_abi(d
->abi
);
230 free_decl_arg(d
->func
);
231 free_decl_args(d
->args
);
235 typedef struct decls
{
240 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
242 decls
= calloc(1, sizeof(*decls
));
244 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
245 decls
->list
[decls
->count
-1] = decl
;
249 static inline void free_decls(decls
*decls
) {
252 for (i
= 0; i
< decls
->count
; ++i
) {
253 free_decl(decls
->list
[i
]);
259 typedef struct impl_type
{
264 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
265 impl_type
*t
= malloc(sizeof(*t
));
268 t
->name
= (char *) strdup((const char *) name
);
272 static inline void free_impl_type(impl_type
*type
) {
277 typedef struct impl_var
{
279 unsigned reference
:1;
282 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
283 impl_var
*var
= malloc(sizeof(*var
));
284 var
->name
= (char *) strdup((const char *) name
);
285 var
->reference
= is_reference
;
289 static inline void free_impl_var(impl_var
*var
) {
294 typedef struct impl_def_val
{
299 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
300 impl_def_val
*def
= malloc(sizeof(*def
));
302 def
->text
= strdup(T
->text
);
306 static inline void free_impl_def_val(impl_def_val
*def
) {
311 typedef struct impl_arg
{
319 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
320 impl_arg
*arg
= malloc(sizeof(*arg
));
327 static inline void free_impl_arg(impl_arg
*arg
) {
328 free_impl_type(arg
->type
);
329 free_impl_var(arg
->var
);
331 free_impl_def_val(arg
->def
);
336 typedef struct impl_args
{
341 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
342 impl_args
*args
= malloc(sizeof(*args
));
343 args
->args
= malloc(sizeof(*args
->args
));
354 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
355 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
356 args
->args
[args
->count
-1] = arg
;
360 static inline void free_impl_args(impl_args
*args
) {
363 for (i
= 0; i
< args
->count
; ++i
) {
364 free_impl_arg(args
->args
[i
]);
370 typedef struct impl_func
{
373 impl_type
*return_type
;
374 unsigned return_reference
:1;
377 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
378 impl_func
*func
= malloc(sizeof(*func
));
379 func
->name
= strdup(name
);
380 func
->args
= args
? args
: init_impl_args(NULL
);
381 func
->return_type
= type
;
382 func
->return_reference
= ret_reference
;
386 static inline void free_impl_func(impl_func
*f
) {
387 free_impl_type(f
->return_type
);
388 free_impl_args(f
->args
);
393 typedef struct let_func
{
398 static inline let_func
*init_let_func(token_t type
, char *name
) {
399 let_func
*func
= malloc(sizeof(*func
));
401 func
->name
= (char *) strdup((const char *) name
);
405 static inline void free_let_func(let_func
*func
) {
410 typedef struct let_value
{
413 unsigned is_reference
:1;
416 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
417 let_value
*val
= malloc(sizeof(*val
));
418 val
->is_reference
= is_reference
;
424 static inline void free_let_value(let_value
*val
) {
426 free_let_func(val
->func
);
429 free_impl_var(val
->var
);
434 typedef struct let_stmt
{
443 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
444 let_stmt
*let
= calloc(1, sizeof(*let
));
450 static inline void free_let_stmt(let_stmt
*stmt
) {
451 free_decl_var(stmt
->var
);
452 free_let_value(stmt
->val
);
456 typedef struct set_func
{
461 static inline set_func
*init_set_func(token_t type
, char *name
) {
462 set_func
*func
= malloc(sizeof(*func
));
464 func
->name
= (char *) strdup((const char *) name
);
468 static inline void free_set_func(set_func
*func
) {
473 typedef struct set_value
{
478 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
479 set_value
*val
= malloc(sizeof(*val
));
485 static inline void free_set_value(set_value
*val
) {
486 free_set_func(val
->func
);
487 free_decl_vars(val
->vars
);
491 typedef struct set_stmt
{
497 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
498 set_stmt
*set
= malloc(sizeof(*set
));
504 static inline void free_set_stmt(set_stmt
*set
) {
505 free_impl_var(set
->var
);
506 free_set_value(set
->val
);
510 typedef struct return_stmt
{
515 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
516 return_stmt
*ret
= malloc(sizeof(*ret
));
522 static inline void free_return_stmt(return_stmt
*ret
) {
523 free_set_func(ret
->func
);
524 free_decl_var(ret
->decl
);
528 typedef struct free_stmt
{
532 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
533 free_stmt
*free_
= malloc(sizeof(*free_
));
538 static inline void free_free_stmt(free_stmt
*free_
) {
539 free_decl_vars(free_
->vars
);
543 typedef struct impl_stmt
{
554 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
555 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
561 static inline void free_impl_stmt(impl_stmt
*stmt
) {
562 switch (stmt
->type
) {
564 free_let_stmt(stmt
->s
.let
);
567 free_set_stmt(stmt
->s
.set
);
570 free_return_stmt(stmt
->s
.ret
);
573 free_free_stmt(stmt
->s
.fre
);
579 typedef struct impl_stmts
{
598 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
599 list
= realloc(list
, count
* sizeof(list
));
600 ((void **)list
)[count
-1] = stmt
;
604 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
605 switch (stmt
->type
) {
607 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
610 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
613 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
616 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
622 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
623 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
624 return add_impl_stmt(stmts
, stmt
);
627 static inline void free_impl_stmts(impl_stmts
*stmts
) {
630 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
631 free_let_stmt(stmts
->let
.list
[i
]);
633 free(stmts
->let
.list
);
634 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
635 free_return_stmt(stmts
->ret
.list
[i
]);
637 free(stmts
->ret
.list
);
638 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
639 free_set_stmt(stmts
->set
.list
[i
]);
641 free(stmts
->set
.list
);
642 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
643 free_free_stmt(stmts
->fre
.list
[i
]);
645 free(stmts
->fre
.list
);
649 typedef struct impl
{
655 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
656 impl
*i
= malloc(sizeof(*i
));
662 static inline void free_impl(impl
*impl
) {
663 free_impl_func(impl
->func
);
664 free_impl_stmts(impl
->stmts
);
668 typedef struct impls
{
673 static impls
*add_impl(impls
*impls
, impl
*impl
) {
675 impls
= calloc(1, sizeof(*impls
));
677 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
678 impls
->list
[impls
->count
-1] = impl
;
682 static void free_impls(impls
*impls
) {
685 for (i
= 0; i
< impls
->count
; ++i
) {
686 free_impl(impls
->list
[i
]);
693 #define PSI_WARNING 32
694 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
696 typedef struct PSI_Data
{
697 #define PSI_DATA_MEMBERS \
698 decl_typedefs *defs; \
707 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
708 memcpy(dest
, src
, sizeof(*dest
));
709 memset(src
, 0, sizeof(*src
));
712 static inline void PSI_DataDtor(PSI_Data
*data
) {
714 free_decl_typedefs(data
->defs
);
717 free_decls(data
->decls
);
720 free_impls(data
->impls
);
730 typedef struct PSI_Parser
{
738 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
741 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
745 if (P
->cur
<= P
->tok
) {
749 token_len
= P
->cur
- P
->tok
;
751 T
= malloc(sizeof(*T
) + token_len
);
755 T
->text
[token_len
] = 0;
756 memcpy(T
->text
, P
->tok
, token_len
);
761 #define PSI_PARSER_DEBUG 0x1
763 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
764 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
765 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
766 token_t
PSI_ParserScan(PSI_Parser
*P
);
767 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
768 void PSI_ParserDtor(PSI_Parser
*P
);
769 void PSI_ParserFree(PSI_Parser
**P
);