d4909dd9fa496456c5c790eb18e95e91b66c4cbd
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
);
37 static inline void free_decl_type(decl_type
*type
) {
42 typedef struct decl_typedef
{
47 static inline decl_typedef
*init_decl_typedef(char *name
, decl_type
*type
) {
48 decl_typedef
*t
= malloc(sizeof(*t
));
49 t
->alias
= strdup(name
);
54 static inline void free_decl_typedef(decl_typedef
*t
) {
56 free_decl_type(t
->type
);
60 typedef struct decl_typedefs
{
65 static decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
67 defs
= calloc(1, sizeof(*defs
));
69 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
70 defs
->list
[defs
->count
-1] = def
;
74 static void free_decl_typedefs(decl_typedefs
*defs
) {
77 for (i
= 0; i
< defs
->count
; ++i
) {
78 free_decl_typedef(defs
->list
[i
]);
84 typedef struct decl_var
{
86 unsigned pointer_level
;
89 static inline decl_var
*init_decl_var(char *name
, unsigned pl
) {
90 decl_var
*v
= malloc(sizeof(*v
));
91 v
->name
= (char *) strdup((const char *) name
);
92 v
->pointer_level
= pl
;
96 static inline void free_decl_var(decl_var
*var
) {
101 typedef struct decl_arg
{
106 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
107 decl_arg
*arg
= malloc(sizeof(*arg
));
113 static inline void free_decl_arg(decl_arg
*arg
) {
114 free_decl_type(arg
->type
);
115 free_decl_var(arg
->var
);
119 typedef struct decl_vars
{
124 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
125 decl_vars
*vars
= malloc(sizeof(*vars
));
127 vars
->vars
= malloc(sizeof(*vars
->vars
));
132 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
133 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
134 vars
->vars
[vars
->count
-1] = var
;
138 static inline void free_decl_vars(decl_vars
*vars
) {
141 for (i
= 0; i
< vars
->count
; ++i
) {
142 free_decl_var(vars
->vars
[i
]);
148 typedef struct decl_args
{
153 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
154 decl_args
*args
= malloc(sizeof(*args
));
156 args
->args
= malloc(sizeof(*args
->args
));
161 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
162 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
163 args
->args
[args
->count
-1] = arg
;
167 static inline void free_decl_args(decl_args
*args
) {
170 for (i
= 0; i
< args
->count
; ++i
) {
171 free_decl_arg(args
->args
[i
]);
177 typedef struct decl_abi
{
181 static inline decl_abi
*init_decl_abi(char *convention
) {
182 decl_abi
*abi
= malloc(sizeof(*abi
));
183 abi
->convention
= strdup(convention
);
187 static inline void free_decl_abi(decl_abi
*abi
) {
188 free(abi
->convention
);
192 typedef struct decl
{
199 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
200 decl
*d
= malloc(sizeof(*d
));
207 static inline void free_decl(decl
*d
) {
208 free_decl_abi(d
->abi
);
209 free_decl_arg(d
->func
);
210 free_decl_args(d
->args
);
214 typedef struct decls
{
219 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
221 decls
= calloc(1, sizeof(*decls
));
223 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
224 decls
->list
[decls
->count
-1] = decl
;
228 static inline void free_decls(decls
*decls
) {
231 for (i
= 0; i
< decls
->count
; ++i
) {
232 free_decl(decls
->list
[i
]);
238 typedef struct impl_type
{
243 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
244 impl_type
*t
= malloc(sizeof(*t
));
247 t
->name
= (char *) strdup((const char *) name
);
251 static inline void free_impl_type(impl_type
*type
) {
256 typedef struct impl_var
{
258 unsigned reference
:1;
261 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
262 impl_var
*var
= malloc(sizeof(*var
));
263 var
->name
= (char *) strdup((const char *) name
);
264 var
->reference
= is_reference
;
268 static inline void free_impl_var(impl_var
*var
) {
273 typedef struct impl_def_val
{
278 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
279 impl_def_val
*def
= malloc(sizeof(*def
));
281 def
->text
= strdup(T
->text
);
285 static inline void free_impl_def_val(impl_def_val
*def
) {
290 typedef struct impl_arg
{
305 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
306 impl_arg
*arg
= malloc(sizeof(*arg
));
313 static inline void free_impl_arg(impl_arg
*arg
) {
314 free_impl_type(arg
->type
);
315 free_impl_var(arg
->var
);
317 free_impl_def_val(arg
->def
);
322 typedef struct impl_args
{
327 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
328 impl_args
*args
= malloc(sizeof(*args
));
329 args
->args
= malloc(sizeof(*args
->args
));
340 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
341 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
342 args
->args
[args
->count
-1] = arg
;
346 static inline void free_impl_args(impl_args
*args
) {
349 for (i
= 0; i
< args
->count
; ++i
) {
350 free_impl_arg(args
->args
[i
]);
356 typedef struct impl_func
{
359 impl_type
*return_type
;
362 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
) {
363 impl_func
*func
= malloc(sizeof(*func
));
364 func
->name
= strdup(name
);
365 func
->args
= args
? args
: init_impl_args(NULL
);
366 func
->return_type
= type
;
370 static inline void free_impl_func(impl_func
*f
) {
371 free_impl_type(f
->return_type
);
372 free_impl_args(f
->args
);
377 typedef struct let_func
{
382 static inline let_func
*init_let_func(token_t type
, char *name
) {
383 let_func
*func
= malloc(sizeof(*func
));
385 func
->name
= (char *) strdup((const char *) name
);
389 static inline void free_let_func(let_func
*func
) {
394 typedef struct let_value
{
397 unsigned null_pointer_ref
:1;
400 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int null_pointer_ref
) {
401 let_value
*val
= malloc(sizeof(*val
));
402 val
->null_pointer_ref
= null_pointer_ref
;
408 static inline void free_let_value(let_value
*val
) {
410 free_let_func(val
->func
);
413 free_impl_var(val
->var
);
418 typedef struct let_stmt
{
423 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
424 let_stmt
*let
= malloc(sizeof(*let
));
430 static inline void free_let_stmt(let_stmt
*stmt
) {
431 free_decl_var(stmt
->var
);
432 free_let_value(stmt
->val
);
436 typedef struct set_func
{
441 static inline set_func
*init_set_func(token_t type
, char *name
) {
442 set_func
*func
= malloc(sizeof(*func
));
444 func
->name
= (char *) strdup((const char *) name
);
448 static inline void free_set_func(set_func
*func
) {
453 typedef struct set_value
{
458 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
459 set_value
*val
= malloc(sizeof(*val
));
465 static inline void free_set_value(set_value
*val
) {
466 free_set_func(val
->func
);
467 free_decl_vars(val
->vars
);
471 typedef struct set_stmt
{
476 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
477 set_stmt
*set
= malloc(sizeof(*set
));
483 static inline void free_set_stmt(set_stmt
*set
) {
484 free_impl_var(set
->var
);
485 free_set_value(set
->val
);
489 typedef struct ret_stmt
{
494 static inline ret_stmt
*init_ret_stmt(set_func
*func
, decl_var
*decl
) {
495 ret_stmt
*ret
= malloc(sizeof(*ret
));
501 static inline void free_ret_stmt(ret_stmt
*ret
) {
502 free_set_func(ret
->func
);
503 free_decl_var(ret
->decl
);
507 typedef struct impl_stmt
{
517 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
518 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
524 static inline void free_impl_stmt(impl_stmt
*stmt
) {
525 switch (stmt
->type
) {
527 free_let_stmt(stmt
->s
.let
);
530 free_set_stmt(stmt
->s
.set
);
533 free_ret_stmt(stmt
->s
.ret
);
539 typedef struct impl_stmts
{
554 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
555 list
= realloc(list
, count
* sizeof(list
));
556 ((void **)list
)[count
-1] = stmt
;
560 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
561 switch (stmt
->type
) {
563 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
566 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
569 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
575 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
576 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
577 return add_impl_stmt(stmts
, stmt
);
580 static inline void free_impl_stmts(impl_stmts
*stmts
) {
583 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
584 free_let_stmt(stmts
->let
.list
[i
]);
586 free(stmts
->let
.list
);
587 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
588 free_ret_stmt(stmts
->ret
.list
[i
]);
590 free(stmts
->ret
.list
);
591 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
592 free_set_stmt(stmts
->set
.list
[i
]);
594 free(stmts
->set
.list
);
598 typedef struct impl
{
604 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
605 impl
*i
= malloc(sizeof(*i
));
611 static inline void free_impl(impl
*impl
) {
612 free_impl_func(impl
->func
);
613 free_impl_stmts(impl
->stmts
);
617 typedef struct impls
{
622 static impls
*add_impl(impls
*impls
, impl
*impl
) {
624 impls
= calloc(1, sizeof(*impls
));
626 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
627 impls
->list
[impls
->count
-1] = impl
;
631 static void free_impls(impls
*impls
) {
634 for (i
= 0; i
< impls
->count
; ++i
) {
635 free_impl(impls
->list
[i
]);
641 typedef struct PSI_Data
{
649 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
650 memcpy(dest
, src
, sizeof(*dest
));
651 memset(src
, 0, sizeof(*src
));
654 static inline void PSI_DataDtor(PSI_Data
*data
) {
656 free_decl_typedefs(data
->defs
);
659 free_decls(data
->decls
);
662 free_impls(data
->impls
);
672 typedef struct PSI_Parser
{
684 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
687 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
691 if (P
->cur
<= P
->tok
) {
695 token_len
= P
->cur
- P
->tok
;
697 T
= malloc(sizeof(*T
) + token_len
);
701 T
->text
[token_len
] = 0;
702 memcpy(T
->text
, P
->tok
, token_len
);
707 #define PSI_PARSER_DEBUG 0x1
709 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, unsigned flags
);
710 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
711 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
712 token_t
PSI_ParserScan(PSI_Parser
*P
);
713 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
714 void PSI_ParserDtor(PSI_Parser
*P
);
715 void PSI_ParserFree(PSI_Parser
**P
);