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 struct decl_var
{
94 unsigned pointer_level
;
98 static inline decl_var
*init_decl_var(char *name
, unsigned pl
) {
99 decl_var
*v
= malloc(sizeof(*v
));
100 v
->name
= (char *) strdup((const char *) name
);
101 v
->pointer_level
= pl
;
105 static inline void free_decl_var(decl_var
*var
) {
110 typedef struct decl_arg
{
113 struct let_stmt
*let
;
116 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
117 decl_arg
*arg
= malloc(sizeof(*arg
));
124 static inline void free_decl_arg(decl_arg
*arg
) {
125 free_decl_type(arg
->type
);
126 free_decl_var(arg
->var
);
130 typedef struct decl_vars
{
135 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
136 decl_vars
*vars
= malloc(sizeof(*vars
));
138 vars
->vars
= malloc(sizeof(*vars
->vars
));
143 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
144 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
145 vars
->vars
[vars
->count
-1] = var
;
149 static inline void free_decl_vars(decl_vars
*vars
) {
152 for (i
= 0; i
< vars
->count
; ++i
) {
153 free_decl_var(vars
->vars
[i
]);
159 typedef struct decl_args
{
164 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
165 decl_args
*args
= malloc(sizeof(*args
));
167 args
->args
= malloc(sizeof(*args
->args
));
172 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
173 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
174 args
->args
[args
->count
-1] = arg
;
178 static inline void free_decl_args(decl_args
*args
) {
181 for (i
= 0; i
< args
->count
; ++i
) {
182 free_decl_arg(args
->args
[i
]);
188 typedef struct decl_abi
{
192 static inline decl_abi
*init_decl_abi(char *convention
) {
193 decl_abi
*abi
= malloc(sizeof(*abi
));
194 abi
->convention
= strdup(convention
);
198 static inline void free_decl_abi(decl_abi
*abi
) {
199 free(abi
->convention
);
203 typedef struct decl
{
210 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
211 decl
*d
= malloc(sizeof(*d
));
218 static inline void free_decl(decl
*d
) {
219 free_decl_abi(d
->abi
);
220 free_decl_arg(d
->func
);
221 free_decl_args(d
->args
);
225 typedef struct decls
{
230 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
232 decls
= calloc(1, sizeof(*decls
));
234 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
235 decls
->list
[decls
->count
-1] = decl
;
239 static inline void free_decls(decls
*decls
) {
242 for (i
= 0; i
< decls
->count
; ++i
) {
243 free_decl(decls
->list
[i
]);
249 typedef union impl_val
{
260 static inline impl_val
*deref_impl_val(unsigned level
, impl_val
*ret_val
, decl_arg
*darg
) {
263 for (i
= level
; i
< darg
->var
->pointer_level
&& ret_val
->ptr
; ++i
) {
264 ret_val
= *(void **)ret_val
;
269 typedef struct impl_type
{
274 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
275 impl_type
*t
= malloc(sizeof(*t
));
278 t
->name
= (char *) strdup((const char *) name
);
282 static inline void free_impl_type(impl_type
*type
) {
287 typedef struct impl_var
{
289 unsigned reference
:1;
292 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
293 impl_var
*var
= malloc(sizeof(*var
));
294 var
->name
= (char *) strdup((const char *) name
);
295 var
->reference
= is_reference
;
299 static inline void free_impl_var(impl_var
*var
) {
304 typedef struct impl_def_val
{
309 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
310 impl_def_val
*def
= malloc(sizeof(*def
));
312 def
->text
= strdup(T
->text
);
316 static inline void free_impl_def_val(impl_def_val
*def
) {
321 typedef struct impl_arg
{
329 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
330 impl_arg
*arg
= malloc(sizeof(*arg
));
337 static inline void free_impl_arg(impl_arg
*arg
) {
338 free_impl_type(arg
->type
);
339 free_impl_var(arg
->var
);
341 free_impl_def_val(arg
->def
);
346 typedef struct impl_args
{
351 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
352 impl_args
*args
= malloc(sizeof(*args
));
353 args
->args
= malloc(sizeof(*args
->args
));
364 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
365 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
366 args
->args
[args
->count
-1] = arg
;
370 static inline void free_impl_args(impl_args
*args
) {
373 for (i
= 0; i
< args
->count
; ++i
) {
374 free_impl_arg(args
->args
[i
]);
380 typedef struct impl_func
{
383 impl_type
*return_type
;
384 unsigned return_reference
:1;
387 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
388 impl_func
*func
= malloc(sizeof(*func
));
389 func
->name
= strdup(name
);
390 func
->args
= args
? args
: init_impl_args(NULL
);
391 func
->return_type
= type
;
392 func
->return_reference
= ret_reference
;
396 static inline void free_impl_func(impl_func
*f
) {
397 free_impl_type(f
->return_type
);
398 free_impl_args(f
->args
);
403 typedef struct let_func
{
408 static inline let_func
*init_let_func(token_t type
, char *name
) {
409 let_func
*func
= malloc(sizeof(*func
));
411 func
->name
= (char *) strdup((const char *) name
);
415 static inline void free_let_func(let_func
*func
) {
420 typedef struct let_value
{
423 unsigned is_reference
:1;
426 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
427 let_value
*val
= malloc(sizeof(*val
));
428 val
->is_reference
= is_reference
;
434 static inline void free_let_value(let_value
*val
) {
436 free_let_func(val
->func
);
439 free_impl_var(val
->var
);
444 typedef struct let_stmt
{
453 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
454 let_stmt
*let
= calloc(1, sizeof(*let
));
460 static inline void free_let_stmt(let_stmt
*stmt
) {
461 free_decl_var(stmt
->var
);
462 free_let_value(stmt
->val
);
466 typedef struct set_func
{
471 static inline set_func
*init_set_func(token_t type
, char *name
) {
472 set_func
*func
= malloc(sizeof(*func
));
474 func
->name
= (char *) strdup((const char *) name
);
478 static inline void free_set_func(set_func
*func
) {
483 typedef struct set_value
{
488 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
489 set_value
*val
= malloc(sizeof(*val
));
495 static inline void free_set_value(set_value
*val
) {
496 free_set_func(val
->func
);
497 free_decl_vars(val
->vars
);
501 typedef struct set_stmt
{
507 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
508 set_stmt
*set
= malloc(sizeof(*set
));
514 static inline void free_set_stmt(set_stmt
*set
) {
515 free_impl_var(set
->var
);
516 free_set_value(set
->val
);
520 typedef struct return_stmt
{
525 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
526 return_stmt
*ret
= malloc(sizeof(*ret
));
532 static inline void free_return_stmt(return_stmt
*ret
) {
533 free_set_func(ret
->func
);
534 free_decl_var(ret
->decl
);
538 typedef struct free_stmt
{
542 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
543 free_stmt
*free_
= malloc(sizeof(*free_
));
548 static inline void free_free_stmt(free_stmt
*free_
) {
549 free_decl_vars(free_
->vars
);
553 typedef struct impl_stmt
{
564 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
565 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
571 static inline void free_impl_stmt(impl_stmt
*stmt
) {
572 switch (stmt
->type
) {
574 free_let_stmt(stmt
->s
.let
);
577 free_set_stmt(stmt
->s
.set
);
580 free_return_stmt(stmt
->s
.ret
);
583 free_free_stmt(stmt
->s
.fre
);
589 typedef struct impl_stmts
{
608 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
609 list
= realloc(list
, count
* sizeof(list
));
610 ((void **)list
)[count
-1] = stmt
;
614 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
615 switch (stmt
->type
) {
617 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
620 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
623 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
626 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
633 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
634 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
635 return add_impl_stmt(stmts
, stmt
);
638 static inline void free_impl_stmts(impl_stmts
*stmts
) {
641 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
642 free_let_stmt(stmts
->let
.list
[i
]);
644 free(stmts
->let
.list
);
645 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
646 free_return_stmt(stmts
->ret
.list
[i
]);
648 free(stmts
->ret
.list
);
649 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
650 free_set_stmt(stmts
->set
.list
[i
]);
652 free(stmts
->set
.list
);
653 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
654 free_free_stmt(stmts
->fre
.list
[i
]);
656 free(stmts
->fre
.list
);
660 typedef struct impl
{
666 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
667 impl
*i
= malloc(sizeof(*i
));
673 static inline void free_impl(impl
*impl
) {
674 free_impl_func(impl
->func
);
675 free_impl_stmts(impl
->stmts
);
679 typedef struct impls
{
684 static impls
*add_impl(impls
*impls
, impl
*impl
) {
686 impls
= calloc(1, sizeof(*impls
));
688 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
689 impls
->list
[impls
->count
-1] = impl
;
693 static void free_impls(impls
*impls
) {
696 for (i
= 0; i
< impls
->count
; ++i
) {
697 free_impl(impls
->list
[i
]);
703 typedef struct const_type
{
708 static inline const_type
*init_const_type(token_t type
, const char *name
) {
709 const_type
*ct
= malloc(sizeof(*ct
));
711 ct
->name
= strdup(name
);
715 static inline void free_const_type(const_type
*type
) {
720 typedef struct constant
{
726 static inline constant
*init_constant(const_type
*type
, char *name
, impl_def_val
*val
) {
727 constant
*c
= malloc(sizeof(*c
));
729 c
->name
= strdup(name
);
734 static inline void free_constant(constant
*constant
) {
735 free_const_type(constant
->type
);
736 free(constant
->name
);
737 free_impl_def_val(constant
->val
);
741 typedef struct constants
{
746 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
748 constants
= calloc(1, sizeof(*constants
));
750 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
751 constants
->list
[constants
->count
-1] = constant
;
755 static inline void free_constants(constants
*c
) {
758 for (i
= 0; i
< c
->count
; ++i
) {
759 free_constant(c
->list
[i
]);
766 #define PSI_WARNING 32
767 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
769 #define PSI_DATA_MEMBERS \
771 decl_typedefs *defs; \
777 typedef struct PSI_Data
{
781 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
782 memcpy(dest
, src
, sizeof(*dest
));
783 memset(src
, 0, sizeof(*src
));
786 static inline void PSI_DataDtor(PSI_Data
*data
) {
788 free_constants(data
->consts
);
791 free_decl_typedefs(data
->defs
);
794 free_decls(data
->decls
);
797 free_impls(data
->impls
);
807 typedef struct PSI_Parser
{
815 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
818 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
822 if (P
->cur
<= P
->tok
) {
826 token_len
= P
->cur
- P
->tok
;
828 T
= malloc(sizeof(*T
) + token_len
);
832 T
->text
[token_len
] = 0;
833 memcpy(T
->text
, P
->tok
, token_len
);
838 #define PSI_PARSER_DEBUG 0x1
840 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
841 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
842 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
843 token_t
PSI_ParserScan(PSI_Parser
*P
);
844 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
845 void PSI_ParserDtor(PSI_Parser
*P
);
846 void PSI_ParserFree(PSI_Parser
**P
);