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
;
97 static inline decl_var
*init_decl_var(char *name
, unsigned pl
) {
98 decl_var
*v
= malloc(sizeof(*v
));
99 v
->name
= (char *) strdup((const char *) name
);
100 v
->pointer_level
= pl
;
104 static inline void free_decl_var(decl_var
*var
) {
109 typedef struct decl_arg
{
112 struct let_stmt
*let
;
115 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
116 decl_arg
*arg
= malloc(sizeof(*arg
));
123 static inline void free_decl_arg(decl_arg
*arg
) {
124 free_decl_type(arg
->type
);
125 free_decl_var(arg
->var
);
129 typedef struct decl_vars
{
134 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
135 decl_vars
*vars
= malloc(sizeof(*vars
));
137 vars
->vars
= malloc(sizeof(*vars
->vars
));
142 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
143 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
144 vars
->vars
[vars
->count
-1] = var
;
148 static inline void free_decl_vars(decl_vars
*vars
) {
151 for (i
= 0; i
< vars
->count
; ++i
) {
152 free_decl_var(vars
->vars
[i
]);
158 typedef struct decl_args
{
163 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
164 decl_args
*args
= malloc(sizeof(*args
));
166 args
->args
= malloc(sizeof(*args
->args
));
171 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
172 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
173 args
->args
[args
->count
-1] = arg
;
177 static inline void free_decl_args(decl_args
*args
) {
180 for (i
= 0; i
< args
->count
; ++i
) {
181 free_decl_arg(args
->args
[i
]);
187 typedef struct decl_abi
{
191 static inline decl_abi
*init_decl_abi(char *convention
) {
192 decl_abi
*abi
= malloc(sizeof(*abi
));
193 abi
->convention
= strdup(convention
);
197 static inline void free_decl_abi(decl_abi
*abi
) {
198 free(abi
->convention
);
202 typedef struct decl
{
209 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
210 decl
*d
= malloc(sizeof(*d
));
217 static inline void free_decl(decl
*d
) {
218 free_decl_abi(d
->abi
);
219 free_decl_arg(d
->func
);
220 free_decl_args(d
->args
);
224 typedef struct decls
{
229 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
231 decls
= calloc(1, sizeof(*decls
));
233 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
234 decls
->list
[decls
->count
-1] = decl
;
238 static inline void free_decls(decls
*decls
) {
241 for (i
= 0; i
< decls
->count
; ++i
) {
242 free_decl(decls
->list
[i
]);
248 typedef struct impl_type
{
253 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
254 impl_type
*t
= malloc(sizeof(*t
));
257 t
->name
= (char *) strdup((const char *) name
);
261 static inline void free_impl_type(impl_type
*type
) {
266 typedef struct impl_var
{
268 unsigned reference
:1;
271 static inline impl_var
*init_impl_var(char *name
, int is_reference
) {
272 impl_var
*var
= malloc(sizeof(*var
));
273 var
->name
= (char *) strdup((const char *) name
);
274 var
->reference
= is_reference
;
278 static inline void free_impl_var(impl_var
*var
) {
283 typedef struct impl_def_val
{
288 static inline impl_def_val
*init_impl_def_val(PSI_Token
*T
) {
289 impl_def_val
*def
= malloc(sizeof(*def
));
291 def
->text
= strdup(T
->text
);
295 static inline void free_impl_def_val(impl_def_val
*def
) {
300 typedef union impl_val
{
310 typedef struct impl_arg
{
318 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
319 impl_arg
*arg
= malloc(sizeof(*arg
));
326 static inline void free_impl_arg(impl_arg
*arg
) {
327 free_impl_type(arg
->type
);
328 free_impl_var(arg
->var
);
330 free_impl_def_val(arg
->def
);
335 typedef struct impl_args
{
340 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
341 impl_args
*args
= malloc(sizeof(*args
));
342 args
->args
= malloc(sizeof(*args
->args
));
353 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
354 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
355 args
->args
[args
->count
-1] = arg
;
359 static inline void free_impl_args(impl_args
*args
) {
362 for (i
= 0; i
< args
->count
; ++i
) {
363 free_impl_arg(args
->args
[i
]);
369 typedef struct impl_func
{
372 impl_type
*return_type
;
375 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
) {
376 impl_func
*func
= malloc(sizeof(*func
));
377 func
->name
= strdup(name
);
378 func
->args
= args
? args
: init_impl_args(NULL
);
379 func
->return_type
= type
;
383 static inline void free_impl_func(impl_func
*f
) {
384 free_impl_type(f
->return_type
);
385 free_impl_args(f
->args
);
390 typedef struct let_func
{
395 static inline let_func
*init_let_func(token_t type
, char *name
) {
396 let_func
*func
= malloc(sizeof(*func
));
398 func
->name
= (char *) strdup((const char *) name
);
402 static inline void free_let_func(let_func
*func
) {
407 typedef struct let_value
{
410 unsigned is_reference
:1;
413 static inline let_value
*init_let_value(let_func
*func
, impl_var
*var
, int is_reference
) {
414 let_value
*val
= malloc(sizeof(*val
));
415 val
->is_reference
= is_reference
;
421 static inline void free_let_value(let_value
*val
) {
423 free_let_func(val
->func
);
426 free_impl_var(val
->var
);
431 typedef struct let_stmt
{
437 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
438 let_stmt
*let
= malloc(sizeof(*let
));
445 static inline void free_let_stmt(let_stmt
*stmt
) {
446 free_decl_var(stmt
->var
);
447 free_let_value(stmt
->val
);
451 typedef struct set_func
{
456 static inline set_func
*init_set_func(token_t type
, char *name
) {
457 set_func
*func
= malloc(sizeof(*func
));
459 func
->name
= (char *) strdup((const char *) name
);
463 static inline void free_set_func(set_func
*func
) {
468 typedef struct set_value
{
473 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
474 set_value
*val
= malloc(sizeof(*val
));
480 static inline void free_set_value(set_value
*val
) {
481 free_set_func(val
->func
);
482 free_decl_vars(val
->vars
);
486 typedef struct set_stmt
{
491 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
492 set_stmt
*set
= malloc(sizeof(*set
));
498 static inline void free_set_stmt(set_stmt
*set
) {
499 free_impl_var(set
->var
);
500 free_set_value(set
->val
);
504 typedef struct ret_stmt
{
509 static inline ret_stmt
*init_ret_stmt(set_func
*func
, decl_var
*decl
) {
510 ret_stmt
*ret
= malloc(sizeof(*ret
));
516 static inline void free_ret_stmt(ret_stmt
*ret
) {
517 free_set_func(ret
->func
);
518 free_decl_var(ret
->decl
);
522 typedef struct impl_stmt
{
532 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
533 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
539 static inline void free_impl_stmt(impl_stmt
*stmt
) {
540 switch (stmt
->type
) {
542 free_let_stmt(stmt
->s
.let
);
545 free_set_stmt(stmt
->s
.set
);
548 free_ret_stmt(stmt
->s
.ret
);
554 typedef struct impl_stmts
{
569 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
570 list
= realloc(list
, count
* sizeof(list
));
571 ((void **)list
)[count
-1] = stmt
;
575 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
576 switch (stmt
->type
) {
578 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
581 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
584 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
590 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
591 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
592 return add_impl_stmt(stmts
, stmt
);
595 static inline void free_impl_stmts(impl_stmts
*stmts
) {
598 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
599 free_let_stmt(stmts
->let
.list
[i
]);
601 free(stmts
->let
.list
);
602 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
603 free_ret_stmt(stmts
->ret
.list
[i
]);
605 free(stmts
->ret
.list
);
606 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
607 free_set_stmt(stmts
->set
.list
[i
]);
609 free(stmts
->set
.list
);
613 typedef struct impl
{
619 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
620 impl
*i
= malloc(sizeof(*i
));
626 static inline void free_impl(impl
*impl
) {
627 free_impl_func(impl
->func
);
628 free_impl_stmts(impl
->stmts
);
632 typedef struct impls
{
637 static impls
*add_impl(impls
*impls
, impl
*impl
) {
639 impls
= calloc(1, sizeof(*impls
));
641 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
642 impls
->list
[impls
->count
-1] = impl
;
646 static void free_impls(impls
*impls
) {
649 for (i
= 0; i
< impls
->count
; ++i
) {
650 free_impl(impls
->list
[i
]);
657 #define PSI_WARNING 32
658 typedef void (*psi_error_cb
)(int type
, const char *msg
, ...);
660 typedef struct PSI_Data
{
661 #define PSI_DATA_MEMBERS \
662 decl_typedefs *defs; \
671 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
672 memcpy(dest
, src
, sizeof(*dest
));
673 memset(src
, 0, sizeof(*src
));
676 static inline void PSI_DataDtor(PSI_Data
*data
) {
678 free_decl_typedefs(data
->defs
);
681 free_decls(data
->decls
);
684 free_impls(data
->impls
);
694 typedef struct PSI_Parser
{
702 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
705 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
709 if (P
->cur
<= P
->tok
) {
713 token_len
= P
->cur
- P
->tok
;
715 T
= malloc(sizeof(*T
) + token_len
);
719 T
->text
[token_len
] = 0;
720 memcpy(T
->text
, P
->tok
, token_len
);
725 #define PSI_PARSER_DEBUG 0x1
727 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
728 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
729 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
730 token_t
PSI_ParserScan(PSI_Parser
*P
);
731 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*T
);
732 void PSI_ParserDtor(PSI_Parser
*P
);
733 void PSI_ParserFree(PSI_Parser
**P
);