29bf9d326ce17727b66bd18969c9d774110de184
9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
15 #define PSI_T_POINTER PSI_T_ASTERISK
16 #define PSI_T_LONG_DOUBLE (PSI_T_DOUBLE << 16)
21 size_t psi_t_alignment(token_t
);
22 size_t psi_t_size(token_t
);
24 typedef struct PSI_Token
{
31 typedef union impl_val
{
52 uint8_t _dbg
[sizeof(void *)];
55 typedef struct decl_type
{
59 struct decl_type
*real
;
60 struct decl_struct
*strct
;
63 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
64 decl_type
*t
= calloc(1, sizeof(*t
));
66 t
->name
= strdup(name
);
70 static inline decl_type
*init_decl_type_ex(token_t type
, int argc
, ...) {
74 size_t len
, pos
= 0, all
= 0;
75 decl_type
*t
= calloc(1, sizeof(*t
));
78 for (i
= 0; i
< argc
; ++i
) {
79 arg
= va_arg(argv
, char *);
80 len
= va_arg(argv
, size_t);
85 ptr
= realloc(ptr
, 1 + (all
+= len
));
87 ptr
= malloc(1 + (all
= len
));
89 memcpy(ptr
+ pos
, arg
, len
);
104 static inline decl_type
*real_decl_type(decl_type
*type
) {
111 static inline void free_decl_type(decl_type
*type
) {
119 typedef struct decl_typedef
{
125 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
126 decl_typedef
*t
= calloc(1, sizeof(*t
));
127 t
->alias
= strdup(name
);
132 static inline void free_decl_typedef(decl_typedef
*t
) {
137 free_decl_type(t
->type
);
141 typedef struct decl_typedefs
{
146 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
148 defs
= calloc(1, sizeof(*defs
));
150 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
151 defs
->list
[defs
->count
-1] = def
;
155 static void free_decl_typedefs(decl_typedefs
*defs
) {
158 for (i
= 0; i
< defs
->count
; ++i
) {
159 free_decl_typedef(defs
->list
[i
]);
165 typedef struct decl_var
{
168 unsigned pointer_level
;
170 struct decl_arg
*arg
;
173 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
174 decl_var
*v
= calloc(1, sizeof(*v
));
175 v
->name
= (char *) strdup((const char *) name
);
176 v
->pointer_level
= pl
;
181 static inline void free_decl_var(decl_var
*var
) {
189 typedef struct decl_struct_layout
{
192 } decl_struct_layout
;
194 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
195 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
202 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
206 typedef struct decl_arg
{
210 decl_struct_layout
*layout
;
211 struct let_stmt
*let
;
217 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
218 decl_arg
*arg
= calloc(1, sizeof(*arg
));
219 arg
->token
= var
->token
;
223 arg
->ptr
= &arg
->val
;
227 static inline void free_decl_arg(decl_arg
*arg
) {
228 free_decl_type(arg
->type
);
229 free_decl_var(arg
->var
);
231 free_decl_struct_layout(arg
->layout
);
236 typedef struct decl_vars
{
241 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
242 decl_vars
*vars
= calloc(1, sizeof(*vars
));
245 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
251 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
252 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
253 vars
->vars
[vars
->count
-1] = var
;
257 static inline void free_decl_vars(decl_vars
*vars
) {
260 for (i
= 0; i
< vars
->count
; ++i
) {
261 free_decl_var(vars
->vars
[i
]);
267 typedef struct decl_args
{
273 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
274 decl_args
*args
= calloc(1, sizeof(*args
));
277 args
->args
= calloc(1, sizeof(*args
->args
));
283 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
284 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
285 args
->args
[args
->count
-1] = arg
;
289 static inline void free_decl_args(decl_args
*args
) {
292 for (i
= 0; i
< args
->count
; ++i
) {
293 free_decl_arg(args
->args
[i
]);
299 typedef struct decl_abi
{
304 static inline decl_abi
*init_decl_abi(const char *convention
) {
305 decl_abi
*abi
= calloc(1, sizeof(*abi
));
306 abi
->convention
= strdup(convention
);
310 static inline void free_decl_abi(decl_abi
*abi
) {
314 free(abi
->convention
);
318 typedef struct decl_callinfo
{
326 typedef struct decl
{
334 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
335 decl
*d
= calloc(1, sizeof(*d
));
342 static inline void free_decl(decl
*d
) {
343 free_decl_abi(d
->abi
);
344 free_decl_arg(d
->func
);
346 free_decl_args(d
->args
);
351 typedef struct decls
{
356 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
358 decls
= calloc(1, sizeof(*decls
));
360 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
361 decls
->list
[decls
->count
-1] = decl
;
365 static inline void free_decls(decls
*decls
) {
368 for (i
= 0; i
< decls
->count
; ++i
) {
369 free_decl(decls
->list
[i
]);
375 typedef struct decl_struct
{
382 void (*dtor
)(void *type
);
386 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
387 decl_struct
*s
= calloc(1, sizeof(*s
));
388 s
->name
= strdup(name
);
393 static inline void free_decl_struct(decl_struct
*s
) {
398 free_decl_args(s
->args
);
404 typedef struct decl_structs
{
409 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
411 ss
= calloc(1, sizeof(*ss
));
413 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
414 ss
->list
[ss
->count
-1] = s
;
418 static inline void free_decl_structs(decl_structs
*ss
) {
421 for (i
= 0; i
< ss
->count
; ++i
) {
422 free_decl_struct(ss
->list
[i
]);
428 typedef struct impl_type
{
433 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
434 impl_type
*t
= calloc(1, sizeof(*t
));
437 t
->name
= strdup(name
);
441 static inline void free_impl_type(impl_type
*type
) {
446 typedef struct impl_var
{
449 unsigned reference
:1;
452 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
453 impl_var
*var
= calloc(1, sizeof(*var
));
454 var
->name
= strdup(name
);
455 var
->reference
= is_reference
;
459 static inline void free_impl_var(impl_var
*var
) {
467 typedef struct impl_def_val
{
472 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
473 impl_def_val
*def
= calloc(1, sizeof(*def
));
475 def
->text
= strdup(text
);
479 static inline void free_impl_def_val(impl_def_val
*def
) {
484 typedef struct const_type
{
489 static inline const_type
*init_const_type(token_t type
, const char *name
) {
490 const_type
*ct
= calloc(1, sizeof(*ct
));
492 ct
->name
= strdup(name
);
496 static inline void free_const_type(const_type
*type
) {
501 typedef struct constant
{
507 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
508 constant
*c
= calloc(1, sizeof(*c
));
510 c
->name
= strdup(name
);
515 static inline void free_constant(constant
*constant
) {
516 free_const_type(constant
->type
);
517 free(constant
->name
);
518 free_impl_def_val(constant
->val
);
522 typedef struct constants
{
527 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
529 constants
= calloc(1, sizeof(*constants
));
531 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
532 constants
->list
[constants
->count
-1] = constant
;
536 static inline void free_constants(constants
*c
) {
539 for (i
= 0; i
< c
->count
; ++i
) {
540 free_constant(c
->list
[i
]);
546 typedef struct impl_arg
{
554 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
555 impl_arg
*arg
= calloc(1, sizeof(*arg
));
562 static inline void free_impl_arg(impl_arg
*arg
) {
563 free_impl_type(arg
->type
);
564 free_impl_var(arg
->var
);
566 free_impl_def_val(arg
->def
);
571 typedef struct impl_vararg
{
573 struct impl_args
*args
;
579 typedef struct impl_args
{
585 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
586 impl_args
*args
= calloc(1, sizeof(*args
));
589 args
->args
= calloc(1, sizeof(*args
->args
));
595 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
596 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
597 args
->args
[args
->count
-1] = arg
;
601 static inline void free_impl_args(impl_args
*args
) {
604 for (i
= 0; i
< args
->count
; ++i
) {
605 free_impl_arg(args
->args
[i
]);
607 if (args
->vararg
.name
) {
608 free_impl_arg(args
->vararg
.name
);
614 typedef struct impl_func
{
618 impl_type
*return_type
;
619 unsigned return_reference
:1;
622 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
623 impl_func
*func
= calloc(1, sizeof(*func
));
624 func
->name
= strdup(name
);
625 func
->args
= args
? args
: init_impl_args(NULL
);
626 func
->return_type
= type
;
627 func
->return_reference
= ret_reference
;
631 static inline void free_impl_func(impl_func
*f
) {
635 free_impl_type(f
->return_type
);
636 free_impl_args(f
->args
);
641 typedef struct num_exp
{
650 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
651 struct num_exp
*operand
;
654 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
655 num_exp
*exp
= calloc(1, sizeof(*exp
));
656 switch (exp
->t
= t
) {
659 exp
->u
.numb
= strdup(num
);
664 EMPTY_SWITCH_DEFAULT_CASE();
669 static inline void free_num_exp(num_exp
*exp
) {
680 free_decl_var(exp
->u
.dvar
);
682 EMPTY_SWITCH_DEFAULT_CASE();
685 free_num_exp(exp
->operand
);
690 typedef struct let_calloc
{
695 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
696 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
697 alloc
->nmemb
= nmemb
;
702 static inline void free_let_calloc(let_calloc
*alloc
) {
703 free_num_exp(alloc
->nmemb
);
704 free_num_exp(alloc
->size
);
708 typedef struct let_func
{
715 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
716 let_func
*func
= calloc(1, sizeof(*func
));
718 func
->name
= strdup(name
);
723 static inline void free_let_func(let_func
*func
) {
724 free_impl_var(func
->var
);
729 #define PSI_LET_REFERENCE 0x1;
730 typedef struct let_val
{
746 unsigned is_reference
:1;
752 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
753 let_val
*let
= calloc(1, sizeof(*let
));
754 switch (let
->kind
= kind
) {
758 let
->data
.num
= data
;
761 let
->data
.alloc
= data
;
764 let
->data
.func
= data
;
767 let
->data
.var
= data
;
769 EMPTY_SWITCH_DEFAULT_CASE();
774 static inline void free_let_val(let_val
*let
) {
779 free_num_exp(let
->data
.num
);
782 free_let_calloc(let
->data
.alloc
);
785 free_let_func(let
->data
.func
);
788 free_decl_var(let
->data
.var
);
790 EMPTY_SWITCH_DEFAULT_CASE();
795 typedef struct let_stmt
{
802 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
803 let_stmt
*let
= calloc(1, sizeof(*let
));
809 static inline void free_let_stmt(let_stmt
*stmt
) {
811 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
812 free_decl_arg(stmt
->var
->arg
);
814 free_let_val(stmt
->val
);
816 free_decl_var(stmt
->var
);
822 typedef struct set_func
{
826 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
829 static inline set_func
*init_set_func(token_t type
, const char *name
) {
830 set_func
*func
= calloc(1, sizeof(*func
));
832 func
->name
= strdup(name
);
836 static inline void free_set_func(set_func
*func
) {
844 typedef struct set_value
{
849 struct set_value
*set
;
852 struct set_value
**inner
;
856 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
857 set_value
*val
= calloc(1, sizeof(*val
));
862 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
863 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
864 val
->inner
[val
->count
-1] = inner
;
865 inner
->outer
.set
= val
;
869 static inline void free_set_value(set_value
*val
) {
871 free_set_func(val
->func
);
874 free_decl_vars(val
->vars
);
876 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
878 for (i
= 0; i
< val
->count
; ++i
) {
879 free_set_value(val
->inner
[i
]);
884 free_num_exp(val
->num
);
889 typedef struct set_stmt
{
895 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
896 set_stmt
*set
= calloc(1, sizeof(*set
));
902 static inline void free_set_stmt(set_stmt
*set
) {
903 free_impl_var(set
->var
);
904 free_set_value(set
->val
);
908 typedef struct return_stmt
{
914 static inline return_stmt
*init_return_stmt(set_value
*val
) {
915 return_stmt
*ret
= calloc(1, sizeof(*ret
));
920 static inline void free_return_stmt(return_stmt
*ret
) {
924 free_set_value(ret
->set
);
928 typedef struct free_call
{
935 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
936 free_call
*f
= calloc(1, sizeof(*f
));
937 f
->func
= strdup(func
);
942 static inline void free_free_call(free_call
*f
) {
947 free_decl_vars(f
->vars
);
951 typedef struct free_calls
{
956 static inline free_calls
*init_free_calls(free_call
*f
) {
957 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
960 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
966 static inline void free_free_calls(free_calls
*fcs
) {
969 for (i
= 0; i
< fcs
->count
; ++i
) {
970 free_free_call(fcs
->list
[i
]);
976 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
977 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
978 fcs
->list
[fcs
->count
-1] = f
;
982 typedef struct free_stmt
{
986 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
987 free_stmt
*f
= calloc(1, sizeof(*f
));
992 static inline void free_free_stmt(free_stmt
*f
) {
993 free_free_calls(f
->calls
);
997 typedef struct impl_stmt
{
1008 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1009 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1015 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1016 switch (stmt
->type
) {
1018 free_let_stmt(stmt
->s
.let
);
1021 free_set_stmt(stmt
->s
.set
);
1024 free_return_stmt(stmt
->s
.ret
);
1027 free_free_stmt(stmt
->s
.fre
);
1033 typedef struct impl_stmts
{
1052 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1053 list
= realloc(list
, count
* sizeof(list
));
1054 ((void **)list
)[count
-1] = stmt
;
1058 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1059 switch (stmt
->type
) {
1061 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1064 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1067 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1070 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1077 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1078 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1079 return add_impl_stmt(stmts
, stmt
);
1082 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1085 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1086 free_let_stmt(stmts
->let
.list
[i
]);
1088 free(stmts
->let
.list
);
1089 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1090 free_return_stmt(stmts
->ret
.list
[i
]);
1092 free(stmts
->ret
.list
);
1093 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1094 free_set_stmt(stmts
->set
.list
[i
]);
1096 free(stmts
->set
.list
);
1097 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1098 free_free_stmt(stmts
->fre
.list
[i
]);
1100 free(stmts
->fre
.list
);
1104 typedef struct impl
{
1110 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1111 impl
*i
= calloc(1, sizeof(*i
));
1117 static inline void free_impl(impl
*impl
) {
1118 free_impl_func(impl
->func
);
1119 free_impl_stmts(impl
->stmts
);
1123 typedef struct impls
{
1128 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1130 impls
= calloc(1, sizeof(*impls
));
1132 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1133 impls
->list
[impls
->count
-1] = impl
;
1137 static void free_impls(impls
*impls
) {
1140 for (i
= 0; i
< impls
->count
; ++i
) {
1141 free_impl(impls
->list
[i
]);
1147 typedef struct decl_file
{
1152 static inline void free_decl_file(decl_file
*file
) {
1159 memset(file
, 0, sizeof(*file
));
1162 typedef struct decl_libs
{
1167 static inline void free_decl_libs(decl_libs
*libs
) {
1170 for (i
= 0; i
< libs
->count
; ++i
) {
1172 dlclose(libs
->dl
[i
]);
1177 memset(libs
, 0, sizeof(*libs
));
1180 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1181 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1182 libs
->dl
[libs
->count
-1] = dlopened
;
1185 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1188 ZEND_ASSERT(var
->arg
->var
!= var
);
1190 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1191 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1192 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1194 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1196 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1198 ret_val
= *(void **) ret_val
;
1203 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1204 impl_val
*val
, *val_ptr
;
1207 ZEND_ASSERT(var
->arg
->var
== var
);
1209 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1210 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1211 var
->array_size
, var
->arg
->var
->array_size
);
1213 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
1217 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1218 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1220 fprintf(stderr
, "++\n");
1222 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1223 val_ptr
= val_ptr
->ptr
;
1229 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1230 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1232 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1238 #define PSI_ERROR 16
1239 #define PSI_WARNING 32
1240 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1242 #define PSI_DATA(D) ((PSI_Data *) (D))
1243 #define PSI_DATA_MEMBERS \
1244 constants *consts; \
1245 decl_typedefs *defs; \
1246 decl_structs *structs; \
1254 typedef struct PSI_Data
{
1258 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1260 dest
= malloc(sizeof(*dest
));
1262 memcpy(dest
, src
, sizeof(*dest
));
1263 memset(src
, 0, sizeof(*src
));
1267 static inline void PSI_DataDtor(PSI_Data
*data
) {
1269 free_constants(data
->consts
);
1272 free_decl_typedefs(data
->defs
);
1274 if (data
->structs
) {
1275 free_decl_structs(data
->structs
);
1278 free_decls(data
->decls
);
1281 free_impls(data
->impls
);
1283 free_decl_file(&data
->psi
.file
);
1286 typedef struct PSI_Parser
{
1291 unsigned flags
, errors
, line
, col
;
1292 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1295 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1296 return sizeof(PSI_Token
) + token_len
+ fname_len
+ sizeof(unsigned) + 2;
1299 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1301 size_t token_len
, fname_len
;
1304 if (P
->cur
< P
->tok
) {
1309 token_len
= P
->cur
- P
->tok
;
1310 fname_len
= strlen(P
->psi
.file
.fn
);
1312 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1313 T
->type
= token_typ
;
1314 T
->size
= token_len
;
1315 T
->text
= &T
->buf
[0];
1316 T
->file
= &T
->buf
[token_len
+ 1];
1317 T
->line
= (void *) &T
->buf
[fname_len
+ token_len
+ 2];
1319 memcpy(T
->text
, P
->tok
, token_len
);
1320 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1321 memcpy(T
->line
, &P
->line
, sizeof(unsigned));
1326 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1327 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1328 PSI_Token
*ptr
= malloc(strct_len
);
1330 memcpy(ptr
, src
, strct_len
);
1332 ptr
->text
= &ptr
->buf
[0];
1333 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1338 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1341 PSI_Token
*T
= NULL
;
1343 va_start(argv
, argc
);
1344 for (i
= 0; i
< argc
; ++i
) {
1345 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1348 size_t fname_len
= strlen(T
->file
);
1350 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+ arg
->size
, fname_len
));
1351 memmove(&T
->buf
[T
->size
+ 1], T
->file
, fname_len
+ 1);
1352 memcpy(T
->file
- 1, arg
->text
, arg
->size
+ 1);
1353 T
->file
= &T
->buf
[T
->size
+ 1];
1355 T
= PSI_TokenCopy(arg
);
1356 T
->type
= PSI_T_NAME
;
1364 static inline const char *PSI_TokenLocation(PSI_Token
*t
) {
1365 return t
? t
->file
: "<builtin>:0:0";
1368 #define PSI_PARSER_DEBUG 0x1
1370 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1371 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1372 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1373 token_t
PSI_ParserScan(PSI_Parser
*P
);
1374 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1375 void PSI_ParserDtor(PSI_Parser
*P
);
1376 void PSI_ParserFree(PSI_Parser
**P
);