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 inline 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
;
99 static inline decl_var
*init_decl_var(char *name
, unsigned pl
, unsigned as
) {
100 decl_var
*v
= malloc(sizeof(*v
));
101 v
->name
= (char *) strdup((const char *) name
);
102 v
->pointer_level
= pl
;
107 static inline void free_decl_var(decl_var
*var
) {
112 typedef struct decl_arg
{
115 struct let_stmt
*let
;
118 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
119 decl_arg
*arg
= malloc(sizeof(*arg
));
126 static inline void free_decl_arg(decl_arg
*arg
) {
127 free_decl_type(arg
->type
);
128 free_decl_var(arg
->var
);
132 typedef struct decl_vars
{
137 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
138 decl_vars
*vars
= malloc(sizeof(*vars
));
140 vars
->vars
= malloc(sizeof(*vars
->vars
));
145 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
146 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
147 vars
->vars
[vars
->count
-1] = var
;
151 static inline void free_decl_vars(decl_vars
*vars
) {
154 for (i
= 0; i
< vars
->count
; ++i
) {
155 free_decl_var(vars
->vars
[i
]);
161 typedef struct decl_args
{
166 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
167 decl_args
*args
= malloc(sizeof(*args
));
169 args
->args
= malloc(sizeof(*args
->args
));
174 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
175 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
176 args
->args
[args
->count
-1] = arg
;
180 static inline void free_decl_args(decl_args
*args
) {
183 for (i
= 0; i
< args
->count
; ++i
) {
184 free_decl_arg(args
->args
[i
]);
190 typedef struct decl_abi
{
194 static inline decl_abi
*init_decl_abi(char *convention
) {
195 decl_abi
*abi
= malloc(sizeof(*abi
));
196 abi
->convention
= strdup(convention
);
200 static inline void free_decl_abi(decl_abi
*abi
) {
201 free(abi
->convention
);
205 typedef struct decl
{
212 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
213 decl
*d
= malloc(sizeof(*d
));
220 static inline void free_decl(decl
*d
) {
221 free_decl_abi(d
->abi
);
222 free_decl_arg(d
->func
);
223 free_decl_args(d
->args
);
227 typedef struct decls
{
232 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
234 decls
= calloc(1, sizeof(*decls
));
236 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
237 decls
->list
[decls
->count
-1] = decl
;
241 static inline void free_decls(decls
*decls
) {
244 for (i
= 0; i
< decls
->count
; ++i
) {
245 free_decl(decls
->list
[i
]);
251 typedef union impl_val
{
262 static inline impl_val
*deref_impl_val(unsigned level
, impl_val
*ret_val
, decl_arg
*darg
) {
265 for (i
= level
; i
< darg
->var
->pointer_level
&& ret_val
->ptr
; ++i
) {
266 ret_val
= *(void **)ret_val
;
271 typedef struct impl_type
{
276 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
277 impl_type
*t
= malloc(sizeof(*t
));
280 t
->name
= (char *) strdup((const char *) name
);
284 static inline void free_impl_type(impl_type
*type
) {
289 typedef struct impl_var
{
291 unsigned reference
:1;
294 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
295 impl_var
*var
= malloc(sizeof(*var
));
296 var
->name
= (char *) strdup((const char *) name
);
297 var
->reference
= is_reference
;
301 static inline void free_impl_var(impl_var
*var
) {
306 typedef struct impl_def_val
{
311 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
312 impl_def_val
*def
= malloc(sizeof(*def
));
314 def
->text
= strdup(T
->text
);
318 static inline void free_impl_def_val(impl_def_val
*def
) {
323 typedef struct impl_arg
{
331 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
332 impl_arg
*arg
= malloc(sizeof(*arg
));
339 static inline void free_impl_arg(impl_arg
*arg
) {
340 free_impl_type(arg
->type
);
341 free_impl_var(arg
->var
);
343 free_impl_def_val(arg
->def
);
348 typedef struct impl_args
{
353 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
354 impl_args
*args
= malloc(sizeof(*args
));
355 args
->args
= malloc(sizeof(*args
->args
));
366 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
367 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
368 args
->args
[args
->count
-1] = arg
;
372 static inline void free_impl_args(impl_args
*args
) {
375 for (i
= 0; i
< args
->count
; ++i
) {
376 free_impl_arg(args
->args
[i
]);
382 typedef struct impl_func
{
385 impl_type
*return_type
;
386 unsigned return_reference
:1;
389 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
390 impl_func
*func
= malloc(sizeof(*func
));
391 func
->name
= strdup(name
);
392 func
->args
= args
? args
: init_impl_args(NULL
);
393 func
->return_type
= type
;
394 func
->return_reference
= ret_reference
;
398 static inline void free_impl_func(impl_func
*f
) {
399 free_impl_type(f
->return_type
);
400 free_impl_args(f
->args
);
405 typedef struct let_func
{
410 static inline let_func
*init_let_func(token_t type
, char *name
) {
411 let_func
*func
= malloc(sizeof(*func
));
413 func
->name
= (char *) strdup((const char *) name
);
417 static inline void free_let_func(let_func
*func
) {
422 typedef struct let_value
{
425 unsigned is_reference
:1;
428 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
429 let_value
*val
= malloc(sizeof(*val
));
430 val
->is_reference
= is_reference
;
436 static inline void free_let_value(let_value
*val
) {
438 free_let_func(val
->func
);
441 free_impl_var(val
->var
);
446 typedef struct let_stmt
{
455 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
456 let_stmt
*let
= calloc(1, sizeof(*let
));
462 static inline void free_let_stmt(let_stmt
*stmt
) {
463 free_decl_var(stmt
->var
);
464 free_let_value(stmt
->val
);
468 typedef struct set_func
{
473 static inline set_func
*init_set_func(token_t type
, char *name
) {
474 set_func
*func
= malloc(sizeof(*func
));
476 func
->name
= (char *) strdup((const char *) name
);
480 static inline void free_set_func(set_func
*func
) {
485 typedef struct set_value
{
490 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
491 set_value
*val
= malloc(sizeof(*val
));
497 static inline void free_set_value(set_value
*val
) {
498 free_set_func(val
->func
);
499 free_decl_vars(val
->vars
);
503 typedef struct set_stmt
{
509 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
510 set_stmt
*set
= malloc(sizeof(*set
));
516 static inline void free_set_stmt(set_stmt
*set
) {
517 free_impl_var(set
->var
);
518 free_set_value(set
->val
);
522 typedef struct return_stmt
{
527 static inline return_stmt
*init_return_stmt(set_func
*func
, decl_var
*decl
) {
528 return_stmt
*ret
= malloc(sizeof(*ret
));
534 static inline void free_return_stmt(return_stmt
*ret
) {
535 free_set_func(ret
->func
);
536 free_decl_var(ret
->decl
);
540 typedef struct free_stmt
{
544 static inline free_stmt
*init_free_stmt(decl_vars
*vars
) {
545 free_stmt
*free_
= malloc(sizeof(*free_
));
550 static inline void free_free_stmt(free_stmt
*free_
) {
551 free_decl_vars(free_
->vars
);
555 typedef struct impl_stmt
{
566 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
567 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
573 static inline void free_impl_stmt(impl_stmt
*stmt
) {
574 switch (stmt
->type
) {
576 free_let_stmt(stmt
->s
.let
);
579 free_set_stmt(stmt
->s
.set
);
582 free_return_stmt(stmt
->s
.ret
);
585 free_free_stmt(stmt
->s
.fre
);
591 typedef struct impl_stmts
{
610 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
611 list
= realloc(list
, count
* sizeof(list
));
612 ((void **)list
)[count
-1] = stmt
;
616 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
617 switch (stmt
->type
) {
619 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
622 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
625 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
628 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
635 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
636 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
637 return add_impl_stmt(stmts
, stmt
);
640 static inline void free_impl_stmts(impl_stmts
*stmts
) {
643 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
644 free_let_stmt(stmts
->let
.list
[i
]);
646 free(stmts
->let
.list
);
647 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
648 free_return_stmt(stmts
->ret
.list
[i
]);
650 free(stmts
->ret
.list
);
651 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
652 free_set_stmt(stmts
->set
.list
[i
]);
654 free(stmts
->set
.list
);
655 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
656 free_free_stmt(stmts
->fre
.list
[i
]);
658 free(stmts
->fre
.list
);
662 typedef struct impl
{
668 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
669 impl
*i
= malloc(sizeof(*i
));
675 static inline void free_impl(impl
*impl
) {
676 free_impl_func(impl
->func
);
677 free_impl_stmts(impl
->stmts
);
681 typedef struct impls
{
686 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
688 impls
= calloc(1, sizeof(*impls
));
690 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
691 impls
->list
[impls
->count
-1] = impl
;
695 static void free_impls(impls
*impls
) {
698 for (i
= 0; i
< impls
->count
; ++i
) {
699 free_impl(impls
->list
[i
]);
705 typedef struct const_type
{
710 static inline const_type
*init_const_type(token_t type
, const char *name
) {
711 const_type
*ct
= malloc(sizeof(*ct
));
713 ct
->name
= strdup(name
);
717 static inline void free_const_type(const_type
*type
) {
722 typedef struct constant
{
728 static inline constant
*init_constant(const_type
*type
, char *name
, impl_def_val
*val
) {
729 constant
*c
= malloc(sizeof(*c
));
731 c
->name
= strdup(name
);
736 static inline void free_constant(constant
*constant
) {
737 free_const_type(constant
->type
);
738 free(constant
->name
);
739 free_impl_def_val(constant
->val
);
743 typedef struct constants
{
748 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
750 constants
= calloc(1, sizeof(*constants
));
752 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
753 constants
->list
[constants
->count
-1] = constant
;
757 static inline void free_constants(constants
*c
) {
760 for (i
= 0; i
< c
->count
; ++i
) {
761 free_constant(c
->list
[i
]);
768 #define PSI_WARNING 32
769 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
771 #define PSI_DATA_MEMBERS \
773 decl_typedefs *defs; \
779 typedef struct PSI_Data
{
783 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
784 memcpy(dest
, src
, sizeof(*dest
));
785 memset(src
, 0, sizeof(*src
));
788 static inline void PSI_DataDtor(PSI_Data
*data
) {
790 free_constants(data
->consts
);
793 free_decl_typedefs(data
->defs
);
796 free_decls(data
->decls
);
799 free_impls(data
->impls
);
809 typedef struct PSI_Parser
{
817 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
820 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
824 if (P
->cur
<= P
->tok
) {
828 token_len
= P
->cur
- P
->tok
;
830 T
= malloc(sizeof(*T
) + token_len
);
834 T
->text
[token_len
] = 0;
835 memcpy(T
->text
, P
->tok
, token_len
);
840 #define PSI_PARSER_DEBUG 0x1
842 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
843 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
844 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
845 token_t
PSI_ParserScan(PSI_Parser
*P
);
846 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
847 void PSI_ParserDtor(PSI_Parser
*P
);
848 void PSI_ParserFree(PSI_Parser
**P
);