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 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
383 decl_struct
*s
= calloc(1, sizeof(*s
));
384 s
->name
= strdup(name
);
389 static inline void free_decl_struct(decl_struct
*s
) {
394 free_decl_args(s
->args
);
400 typedef struct decl_structs
{
405 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
407 ss
= calloc(1, sizeof(*ss
));
409 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
410 ss
->list
[ss
->count
-1] = s
;
414 static inline void free_decl_structs(decl_structs
*ss
) {
417 for (i
= 0; i
< ss
->count
; ++i
) {
418 free_decl_struct(ss
->list
[i
]);
424 typedef struct impl_type
{
429 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
430 impl_type
*t
= calloc(1, sizeof(*t
));
433 t
->name
= strdup(name
);
437 static inline void free_impl_type(impl_type
*type
) {
442 typedef struct impl_var
{
445 unsigned reference
:1;
448 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
449 impl_var
*var
= calloc(1, sizeof(*var
));
450 var
->name
= strdup(name
);
451 var
->reference
= is_reference
;
455 static inline void free_impl_var(impl_var
*var
) {
463 typedef struct impl_def_val
{
468 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
469 impl_def_val
*def
= calloc(1, sizeof(*def
));
471 def
->text
= strdup(text
);
475 static inline void free_impl_def_val(impl_def_val
*def
) {
480 typedef struct const_type
{
485 static inline const_type
*init_const_type(token_t type
, const char *name
) {
486 const_type
*ct
= calloc(1, sizeof(*ct
));
488 ct
->name
= strdup(name
);
492 static inline void free_const_type(const_type
*type
) {
497 typedef struct constant
{
503 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
504 constant
*c
= calloc(1, sizeof(*c
));
506 c
->name
= strdup(name
);
511 static inline void free_constant(constant
*constant
) {
512 free_const_type(constant
->type
);
513 free(constant
->name
);
514 free_impl_def_val(constant
->val
);
518 typedef struct constants
{
523 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
525 constants
= calloc(1, sizeof(*constants
));
527 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
528 constants
->list
[constants
->count
-1] = constant
;
532 static inline void free_constants(constants
*c
) {
535 for (i
= 0; i
< c
->count
; ++i
) {
536 free_constant(c
->list
[i
]);
542 typedef struct impl_arg
{
550 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
551 impl_arg
*arg
= calloc(1, sizeof(*arg
));
558 static inline void free_impl_arg(impl_arg
*arg
) {
559 free_impl_type(arg
->type
);
560 free_impl_var(arg
->var
);
562 free_impl_def_val(arg
->def
);
567 typedef struct impl_vararg
{
569 struct impl_args
*args
;
575 typedef struct impl_args
{
581 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
582 impl_args
*args
= calloc(1, sizeof(*args
));
585 args
->args
= calloc(1, sizeof(*args
->args
));
591 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
592 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
593 args
->args
[args
->count
-1] = arg
;
597 static inline void free_impl_args(impl_args
*args
) {
600 for (i
= 0; i
< args
->count
; ++i
) {
601 free_impl_arg(args
->args
[i
]);
603 if (args
->vararg
.name
) {
604 free_impl_arg(args
->vararg
.name
);
610 typedef struct impl_func
{
614 impl_type
*return_type
;
615 unsigned return_reference
:1;
618 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
619 impl_func
*func
= calloc(1, sizeof(*func
));
620 func
->name
= strdup(name
);
621 func
->args
= args
? args
: init_impl_args(NULL
);
622 func
->return_type
= type
;
623 func
->return_reference
= ret_reference
;
627 static inline void free_impl_func(impl_func
*f
) {
631 free_impl_type(f
->return_type
);
632 free_impl_args(f
->args
);
637 typedef struct num_exp
{
646 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
647 struct num_exp
*operand
;
650 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
651 num_exp
*exp
= calloc(1, sizeof(*exp
));
652 switch (exp
->t
= t
) {
655 exp
->u
.numb
= strdup(num
);
660 EMPTY_SWITCH_DEFAULT_CASE();
665 static inline void free_num_exp(num_exp
*exp
) {
676 free_decl_var(exp
->u
.dvar
);
678 EMPTY_SWITCH_DEFAULT_CASE();
681 free_num_exp(exp
->operand
);
686 typedef struct let_calloc
{
691 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
692 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
693 alloc
->nmemb
= nmemb
;
698 static inline void free_let_calloc(let_calloc
*alloc
) {
699 free_num_exp(alloc
->nmemb
);
700 free_num_exp(alloc
->size
);
704 typedef struct let_func
{
711 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
712 let_func
*func
= calloc(1, sizeof(*func
));
714 func
->name
= strdup(name
);
719 static inline void free_let_func(let_func
*func
) {
720 free_impl_var(func
->var
);
725 #define PSI_LET_REFERENCE 0x1;
726 typedef struct let_val
{
742 unsigned is_reference
:1;
748 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
749 let_val
*let
= calloc(1, sizeof(*let
));
750 switch (let
->kind
= kind
) {
754 let
->data
.num
= data
;
757 let
->data
.alloc
= data
;
760 let
->data
.func
= data
;
763 let
->data
.var
= data
;
765 EMPTY_SWITCH_DEFAULT_CASE();
770 static inline void free_let_val(let_val
*let
) {
775 free_num_exp(let
->data
.num
);
778 free_let_calloc(let
->data
.alloc
);
781 free_let_func(let
->data
.func
);
784 free_decl_var(let
->data
.var
);
786 EMPTY_SWITCH_DEFAULT_CASE();
791 typedef struct let_stmt
{
798 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
799 let_stmt
*let
= calloc(1, sizeof(*let
));
805 static inline void free_let_stmt(let_stmt
*stmt
) {
807 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
808 free_decl_arg(stmt
->var
->arg
);
810 free_let_val(stmt
->val
);
812 free_decl_var(stmt
->var
);
818 typedef struct set_func
{
822 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
825 static inline set_func
*init_set_func(token_t type
, const char *name
) {
826 set_func
*func
= calloc(1, sizeof(*func
));
828 func
->name
= strdup(name
);
832 static inline void free_set_func(set_func
*func
) {
840 typedef struct set_value
{
845 struct set_value
*set
;
848 struct set_value
**inner
;
852 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
853 set_value
*val
= calloc(1, sizeof(*val
));
858 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
859 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
860 val
->inner
[val
->count
-1] = inner
;
861 inner
->outer
.set
= val
;
865 static inline void free_set_value(set_value
*val
) {
867 free_set_func(val
->func
);
870 free_decl_vars(val
->vars
);
872 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
874 for (i
= 0; i
< val
->count
; ++i
) {
875 free_set_value(val
->inner
[i
]);
880 free_num_exp(val
->num
);
885 typedef struct set_stmt
{
891 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
892 set_stmt
*set
= calloc(1, sizeof(*set
));
898 static inline void free_set_stmt(set_stmt
*set
) {
899 free_impl_var(set
->var
);
900 free_set_value(set
->val
);
904 typedef struct return_stmt
{
910 static inline return_stmt
*init_return_stmt(set_value
*val
) {
911 return_stmt
*ret
= calloc(1, sizeof(*ret
));
916 static inline void free_return_stmt(return_stmt
*ret
) {
920 free_set_value(ret
->set
);
924 typedef struct free_call
{
931 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
932 free_call
*f
= calloc(1, sizeof(*f
));
933 f
->func
= strdup(func
);
938 static inline void free_free_call(free_call
*f
) {
943 free_decl_vars(f
->vars
);
947 typedef struct free_calls
{
952 static inline free_calls
*init_free_calls(free_call
*f
) {
953 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
956 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
962 static inline void free_free_calls(free_calls
*fcs
) {
965 for (i
= 0; i
< fcs
->count
; ++i
) {
966 free_free_call(fcs
->list
[i
]);
972 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
973 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
974 fcs
->list
[fcs
->count
-1] = f
;
978 typedef struct free_stmt
{
982 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
983 free_stmt
*f
= calloc(1, sizeof(*f
));
988 static inline void free_free_stmt(free_stmt
*f
) {
989 free_free_calls(f
->calls
);
993 typedef struct impl_stmt
{
1004 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1005 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1011 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1012 switch (stmt
->type
) {
1014 free_let_stmt(stmt
->s
.let
);
1017 free_set_stmt(stmt
->s
.set
);
1020 free_return_stmt(stmt
->s
.ret
);
1023 free_free_stmt(stmt
->s
.fre
);
1029 typedef struct impl_stmts
{
1048 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1049 list
= realloc(list
, count
* sizeof(list
));
1050 ((void **)list
)[count
-1] = stmt
;
1054 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1055 switch (stmt
->type
) {
1057 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1060 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1063 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1066 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1073 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1074 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1075 return add_impl_stmt(stmts
, stmt
);
1078 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1081 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1082 free_let_stmt(stmts
->let
.list
[i
]);
1084 free(stmts
->let
.list
);
1085 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1086 free_return_stmt(stmts
->ret
.list
[i
]);
1088 free(stmts
->ret
.list
);
1089 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1090 free_set_stmt(stmts
->set
.list
[i
]);
1092 free(stmts
->set
.list
);
1093 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1094 free_free_stmt(stmts
->fre
.list
[i
]);
1096 free(stmts
->fre
.list
);
1100 typedef struct impl
{
1106 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1107 impl
*i
= calloc(1, sizeof(*i
));
1113 static inline void free_impl(impl
*impl
) {
1114 free_impl_func(impl
->func
);
1115 free_impl_stmts(impl
->stmts
);
1119 typedef struct impls
{
1124 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1126 impls
= calloc(1, sizeof(*impls
));
1128 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1129 impls
->list
[impls
->count
-1] = impl
;
1133 static void free_impls(impls
*impls
) {
1136 for (i
= 0; i
< impls
->count
; ++i
) {
1137 free_impl(impls
->list
[i
]);
1143 typedef struct decl_file
{
1148 static inline void free_decl_file(decl_file
*file
) {
1155 memset(file
, 0, sizeof(*file
));
1158 typedef struct decl_libs
{
1163 static inline void free_decl_libs(decl_libs
*libs
) {
1166 for (i
= 0; i
< libs
->count
; ++i
) {
1168 dlclose(libs
->dl
[i
]);
1173 memset(libs
, 0, sizeof(*libs
));
1176 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1177 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1178 libs
->dl
[libs
->count
-1] = dlopened
;
1181 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1184 ZEND_ASSERT(var
->arg
->var
!= var
);
1186 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1187 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1188 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1190 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1192 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1194 ret_val
= *(void **) ret_val
;
1199 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1200 impl_val
*val
, *val_ptr
;
1203 ZEND_ASSERT(var
->arg
->var
== var
);
1205 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1206 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1207 var
->array_size
, var
->arg
->var
->array_size
);
1209 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
1213 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1214 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1216 fprintf(stderr
, "++\n");
1218 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1219 val_ptr
= val_ptr
->ptr
;
1225 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1226 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1228 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1234 #define PSI_ERROR 16
1235 #define PSI_WARNING 32
1236 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1238 #define PSI_DATA(D) ((PSI_Data *) (D))
1239 #define PSI_DATA_MEMBERS \
1240 constants *consts; \
1241 decl_typedefs *defs; \
1242 decl_structs *structs; \
1250 typedef struct PSI_Data
{
1254 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1256 dest
= malloc(sizeof(*dest
));
1258 memcpy(dest
, src
, sizeof(*dest
));
1259 memset(src
, 0, sizeof(*src
));
1263 static inline void PSI_DataDtor(PSI_Data
*data
) {
1265 free_constants(data
->consts
);
1268 free_decl_typedefs(data
->defs
);
1270 if (data
->structs
) {
1271 free_decl_structs(data
->structs
);
1274 free_decls(data
->decls
);
1277 free_impls(data
->impls
);
1279 free_decl_file(&data
->psi
.file
);
1282 typedef struct PSI_Parser
{
1287 unsigned flags
, errors
, line
, col
;
1288 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1291 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1292 return sizeof(PSI_Token
) + token_len
+ fname_len
+ sizeof(unsigned) + 2;
1295 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1297 size_t token_len
, fname_len
;
1300 if (P
->cur
< P
->tok
) {
1305 token_len
= P
->cur
- P
->tok
;
1306 fname_len
= strlen(P
->psi
.file
.fn
);
1308 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1309 T
->type
= token_typ
;
1310 T
->size
= token_len
;
1311 T
->text
= &T
->buf
[0];
1312 T
->file
= &T
->buf
[token_len
+ 1];
1313 T
->line
= (void *) &T
->buf
[fname_len
+ token_len
+ 2];
1315 memcpy(T
->text
, P
->tok
, token_len
);
1316 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1317 memcpy(T
->line
, &P
->line
, sizeof(unsigned));
1322 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1323 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1324 PSI_Token
*ptr
= malloc(strct_len
);
1326 memcpy(ptr
, src
, strct_len
);
1328 ptr
->text
= &ptr
->buf
[0];
1329 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1334 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1337 PSI_Token
*T
= NULL
;
1339 va_start(argv
, argc
);
1340 for (i
= 0; i
< argc
; ++i
) {
1341 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1344 size_t fname_len
= strlen(T
->file
);
1346 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+ arg
->size
, fname_len
));
1347 memmove(&T
->buf
[T
->size
+ 1], T
->file
, fname_len
+ 1);
1348 memcpy(T
->file
- 1, arg
->text
, arg
->size
+ 1);
1349 T
->file
= &T
->buf
[T
->size
+ 1];
1351 T
= PSI_TokenCopy(arg
);
1352 T
->type
= PSI_T_NAME
;
1360 static inline const char *PSI_TokenLocation(PSI_Token
*t
) {
1361 return t
? t
->file
: "<builtin>:0:0";
1364 #define PSI_PARSER_DEBUG 0x1
1366 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1367 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1368 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1369 token_t
PSI_ParserScan(PSI_Parser
*P
);
1370 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1371 void PSI_ParserDtor(PSI_Parser
*P
);
1372 void PSI_ParserFree(PSI_Parser
**P
);