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 void free_decl_type(decl_type
*type
) {
43 typedef struct decl_typedef
{
48 static inline decl_typedef
*init_decl_typedef(char *name
, decl_type
*type
) {
49 decl_typedef
*t
= malloc(sizeof(*t
));
50 t
->alias
= strdup(name
);
55 static inline void free_decl_typedef(decl_typedef
*t
) {
57 free_decl_type(t
->type
);
61 typedef struct decl_typedefs
{
66 static decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
68 defs
= calloc(1, sizeof(*defs
));
70 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
71 defs
->list
[defs
->count
-1] = def
;
75 static void free_decl_typedefs(decl_typedefs
*defs
) {
78 for (i
= 0; i
< defs
->count
; ++i
) {
79 free_decl_typedef(defs
->list
[i
]);
85 typedef struct decl_var
{
87 unsigned pointer_level
;
90 static inline decl_var
*init_decl_var(char *name
, unsigned pl
) {
91 decl_var
*v
= malloc(sizeof(*v
));
92 v
->name
= (char *) strdup((const char *) name
);
93 v
->pointer_level
= pl
;
97 static inline void free_decl_var(decl_var
*var
) {
102 typedef struct decl_arg
{
107 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
108 decl_arg
*arg
= malloc(sizeof(*arg
));
114 static inline void free_decl_arg(decl_arg
*arg
) {
115 free_decl_type(arg
->type
);
116 free_decl_var(arg
->var
);
120 typedef struct decl_vars
{
125 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
126 decl_vars
*vars
= malloc(sizeof(*vars
));
128 vars
->vars
= malloc(sizeof(*vars
->vars
));
133 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
134 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
135 vars
->vars
[vars
->count
-1] = var
;
139 static inline void free_decl_vars(decl_vars
*vars
) {
142 for (i
= 0; i
< vars
->count
; ++i
) {
143 free_decl_var(vars
->vars
[i
]);
149 typedef struct decl_args
{
154 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
155 decl_args
*args
= malloc(sizeof(*args
));
157 args
->args
= malloc(sizeof(*args
->args
));
162 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
163 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
164 args
->args
[args
->count
-1] = arg
;
168 static inline void free_decl_args(decl_args
*args
) {
171 for (i
= 0; i
< args
->count
; ++i
) {
172 free_decl_arg(args
->args
[i
]);
178 typedef struct decl_abi
{
182 static inline decl_abi
*init_decl_abi(char *convention
) {
183 decl_abi
*abi
= malloc(sizeof(*abi
));
184 abi
->convention
= strdup(convention
);
188 static inline void free_decl_abi(decl_abi
*abi
) {
189 free(abi
->convention
);
193 typedef struct decl
{
200 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
201 decl
*d
= malloc(sizeof(*d
));
208 static inline void free_decl(decl
*d
) {
209 free_decl_abi(d
->abi
);
210 free_decl_arg(d
->func
);
211 free_decl_args(d
->args
);
215 typedef struct decls
{
220 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
222 decls
= calloc(1, sizeof(*decls
));
224 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
225 decls
->list
[decls
->count
-1] = decl
;
229 static inline void free_decls(decls
*decls
) {
232 for (i
= 0; i
< decls
->count
; ++i
) {
233 free_decl(decls
->list
[i
]);
239 typedef struct impl_type
{
244 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
245 impl_type
*t
= malloc(sizeof(*t
));
248 t
->name
= (char *) strdup((const char *) name
);
252 static inline void free_impl_type(impl_type
*type
) {
257 typedef struct impl_var
{
259 unsigned reference
:1;
262 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
263 impl_var
*var
= malloc(sizeof(*var
));
264 var
->name
= (char *) strdup((const char *) name
);
265 var
->reference
= is_reference
;
269 static inline void free_impl_var(impl_var
*var
) {
274 typedef struct impl_def_val
{
279 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
280 impl_def_val
*def
= malloc(sizeof(*def
));
282 def
->text
= strdup(T
->text
);
286 static inline void free_impl_def_val(impl_def_val
*def
) {
291 typedef struct impl_arg
{
306 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
307 impl_arg
*arg
= malloc(sizeof(*arg
));
314 static inline void free_impl_arg(impl_arg
*arg
) {
315 free_impl_type(arg
->type
);
316 free_impl_var(arg
->var
);
318 free_impl_def_val(arg
->def
);
323 typedef struct impl_args
{
328 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
329 impl_args
*args
= malloc(sizeof(*args
));
330 args
->args
= malloc(sizeof(*args
->args
));
341 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
342 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
343 args
->args
[args
->count
-1] = arg
;
347 static inline void free_impl_args(impl_args
*args
) {
350 for (i
= 0; i
< args
->count
; ++i
) {
351 free_impl_arg(args
->args
[i
]);
357 typedef struct impl_func
{
360 impl_type
*return_type
;
363 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
) {
364 impl_func
*func
= malloc(sizeof(*func
));
365 func
->name
= strdup(name
);
366 func
->args
= args
? args
: init_impl_args(NULL
);
367 func
->return_type
= type
;
371 static inline void free_impl_func(impl_func
*f
) {
372 free_impl_type(f
->return_type
);
373 free_impl_args(f
->args
);
378 typedef struct let_func
{
383 static inline let_func
*init_let_func(token_t type
, char *name
) {
384 let_func
*func
= malloc(sizeof(*func
));
386 func
->name
= (char *) strdup((const char *) name
);
390 static inline void free_let_func(let_func
*func
) {
395 typedef struct let_value
{
398 unsigned null_pointer_ref
:1;
401 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int null_pointer_ref
) {
402 let_value
*val
= malloc(sizeof(*val
));
403 val
->null_pointer_ref
= null_pointer_ref
;
409 static inline void free_let_value(let_value
*val
) {
411 free_let_func(val
->func
);
414 free_impl_var(val
->var
);
419 typedef struct let_stmt
{
424 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
425 let_stmt
*let
= malloc(sizeof(*let
));
431 static inline void free_let_stmt(let_stmt
*stmt
) {
432 free_decl_var(stmt
->var
);
433 free_let_value(stmt
->val
);
437 typedef struct set_func
{
442 static inline set_func
*init_set_func(token_t type
, char *name
) {
443 set_func
*func
= malloc(sizeof(*func
));
445 func
->name
= (char *) strdup((const char *) name
);
449 static inline void free_set_func(set_func
*func
) {
454 typedef struct set_value
{
459 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
460 set_value
*val
= malloc(sizeof(*val
));
466 static inline void free_set_value(set_value
*val
) {
467 free_set_func(val
->func
);
468 free_decl_vars(val
->vars
);
472 typedef struct set_stmt
{
477 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
478 set_stmt
*set
= malloc(sizeof(*set
));
484 static inline void free_set_stmt(set_stmt
*set
) {
485 free_impl_var(set
->var
);
486 free_set_value(set
->val
);
490 typedef struct ret_stmt
{
495 static inline ret_stmt
*init_ret_stmt(set_func
*func
, decl_var
*decl
) {
496 ret_stmt
*ret
= malloc(sizeof(*ret
));
502 static inline void free_ret_stmt(ret_stmt
*ret
) {
503 free_set_func(ret
->func
);
504 free_decl_var(ret
->decl
);
508 typedef struct impl_stmt
{
518 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
519 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
525 static inline void free_impl_stmt(impl_stmt
*stmt
) {
526 switch (stmt
->type
) {
528 free_let_stmt(stmt
->s
.let
);
531 free_set_stmt(stmt
->s
.set
);
534 free_ret_stmt(stmt
->s
.ret
);
540 typedef struct impl_stmts
{
555 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
556 list
= realloc(list
, count
* sizeof(list
));
557 ((void **)list
)[count
-1] = stmt
;
561 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
562 switch (stmt
->type
) {
564 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
567 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
570 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
576 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
577 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
578 return add_impl_stmt(stmts
, stmt
);
581 static inline void free_impl_stmts(impl_stmts
*stmts
) {
584 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
585 free_let_stmt(stmts
->let
.list
[i
]);
587 free(stmts
->let
.list
);
588 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
589 free_ret_stmt(stmts
->ret
.list
[i
]);
591 free(stmts
->ret
.list
);
592 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
593 free_set_stmt(stmts
->set
.list
[i
]);
595 free(stmts
->set
.list
);
599 typedef struct impl
{
605 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
606 impl
*i
= malloc(sizeof(*i
));
612 static inline void free_impl(impl
*impl
) {
613 free_impl_func(impl
->func
);
614 free_impl_stmts(impl
->stmts
);
618 typedef struct impls
{
623 static impls
*add_impl(impls
*impls
, impl
*impl
) {
625 impls
= calloc(1, sizeof(*impls
));
627 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
628 impls
->list
[impls
->count
-1] = impl
;
632 static void free_impls(impls
*impls
) {
635 for (i
= 0; i
< impls
->count
; ++i
) {
636 free_impl(impls
->list
[i
]);
642 typedef struct PSI_Data
{
650 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
651 memcpy(dest
, src
, sizeof(*dest
));
652 memset(src
, 0, sizeof(*src
));
655 static inline void PSI_DataDtor(PSI_Data
*data
) {
657 free_decl_typedefs(data
->defs
);
660 free_decls(data
->decls
);
663 free_impls(data
->impls
);
673 typedef struct PSI_Parser
{
685 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
688 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
692 if (P
->cur
<= P
->tok
) {
696 token_len
= P
->cur
- P
->tok
;
698 T
= malloc(sizeof(*T
) + token_len
);
702 T
->text
[token_len
] = 0;
703 memcpy(T
->text
, P
->tok
, token_len
);
708 #define PSI_PARSER_DEBUG 0x1
710 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, unsigned flags
);
711 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
712 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
713 token_t
PSI_ParserScan(PSI_Parser
*P
);
714 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
715 void PSI_ParserDtor(PSI_Parser
*P
);
716 void PSI_ParserFree(PSI_Parser
**P
);