9 #include <Zend/zend_types.h>
11 #include "parser_proc.h"
15 #define PSI_T_POINTER PSI_T_ASTERISK
19 size_t psi_t_alignment(token_t
);
20 size_t psi_t_size(token_t
);
22 typedef struct PSI_Token
{
29 typedef union impl_val
{
50 uint8_t _dbg
[sizeof(void *)];
53 typedef struct decl_type
{
57 struct decl_type
*real
;
58 struct decl_struct
*strct
;
61 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
62 decl_type
*t
= calloc(1, sizeof(*t
));
64 t
->name
= strdup(name
);
68 static inline decl_type
*init_decl_type_ex(token_t type
, int argc
, ...) {
72 size_t len
, pos
= 0, all
= 0;
73 decl_type
*t
= calloc(1, sizeof(*t
));
76 for (i
= 0; i
< argc
; ++i
) {
77 arg
= va_arg(argv
, char *);
78 len
= va_arg(argv
, size_t);
83 ptr
= realloc(ptr
, 1 + (all
+= len
));
85 ptr
= malloc(ptr
, 1 + (all
= len
));
87 memcpy(ptr
+ pos
, arg
, len
);
102 static inline decl_type
*real_decl_type(decl_type
*type
) {
109 static inline void free_decl_type(decl_type
*type
) {
117 typedef struct decl_typedef
{
123 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
124 decl_typedef
*t
= calloc(1, sizeof(*t
));
125 t
->alias
= strdup(name
);
130 static inline void free_decl_typedef(decl_typedef
*t
) {
135 free_decl_type(t
->type
);
139 typedef struct decl_typedefs
{
144 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
146 defs
= calloc(1, sizeof(*defs
));
148 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
149 defs
->list
[defs
->count
-1] = def
;
153 static void free_decl_typedefs(decl_typedefs
*defs
) {
156 for (i
= 0; i
< defs
->count
; ++i
) {
157 free_decl_typedef(defs
->list
[i
]);
163 typedef struct decl_var
{
166 unsigned pointer_level
;
168 struct decl_arg
*arg
;
171 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
172 decl_var
*v
= calloc(1, sizeof(*v
));
173 v
->name
= (char *) strdup((const char *) name
);
174 v
->pointer_level
= pl
;
179 static inline void free_decl_var(decl_var
*var
) {
187 typedef struct decl_struct_layout
{
190 } decl_struct_layout
;
192 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
193 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
200 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
204 typedef struct decl_arg
{
208 decl_struct_layout
*layout
;
209 struct let_stmt
*let
;
215 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
216 decl_arg
*arg
= calloc(1, sizeof(*arg
));
217 arg
->token
= var
->token
;
221 arg
->ptr
= &arg
->val
;
225 static inline void free_decl_arg(decl_arg
*arg
) {
226 free_decl_type(arg
->type
);
227 free_decl_var(arg
->var
);
229 free_decl_struct_layout(arg
->layout
);
234 typedef struct decl_vars
{
239 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
240 decl_vars
*vars
= calloc(1, sizeof(*vars
));
243 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
249 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
250 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
251 vars
->vars
[vars
->count
-1] = var
;
255 static inline void free_decl_vars(decl_vars
*vars
) {
258 for (i
= 0; i
< vars
->count
; ++i
) {
259 free_decl_var(vars
->vars
[i
]);
265 typedef struct decl_args
{
271 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
272 decl_args
*args
= calloc(1, sizeof(*args
));
275 args
->args
= calloc(1, sizeof(*args
->args
));
281 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
282 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
283 args
->args
[args
->count
-1] = arg
;
287 static inline void free_decl_args(decl_args
*args
) {
290 for (i
= 0; i
< args
->count
; ++i
) {
291 free_decl_arg(args
->args
[i
]);
297 typedef struct decl_abi
{
302 static inline decl_abi
*init_decl_abi(const char *convention
) {
303 decl_abi
*abi
= calloc(1, sizeof(*abi
));
304 abi
->convention
= strdup(convention
);
308 static inline void free_decl_abi(decl_abi
*abi
) {
312 free(abi
->convention
);
316 typedef struct decl_callinfo
{
324 typedef struct decl
{
332 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
333 decl
*d
= calloc(1, sizeof(*d
));
340 static inline void free_decl(decl
*d
) {
341 free_decl_abi(d
->abi
);
342 free_decl_arg(d
->func
);
344 free_decl_args(d
->args
);
349 typedef struct decls
{
354 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
356 decls
= calloc(1, sizeof(*decls
));
358 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
359 decls
->list
[decls
->count
-1] = decl
;
363 static inline void free_decls(decls
*decls
) {
366 for (i
= 0; i
< decls
->count
; ++i
) {
367 free_decl(decls
->list
[i
]);
373 typedef struct decl_struct
{
380 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
381 decl_struct
*s
= calloc(1, sizeof(*s
));
382 s
->name
= strdup(name
);
387 static inline void free_decl_struct(decl_struct
*s
) {
392 free_decl_args(s
->args
);
398 typedef struct decl_structs
{
403 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
405 ss
= calloc(1, sizeof(*ss
));
407 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
408 ss
->list
[ss
->count
-1] = s
;
412 static inline void free_decl_structs(decl_structs
*ss
) {
415 for (i
= 0; i
< ss
->count
; ++i
) {
416 free_decl_struct(ss
->list
[i
]);
422 typedef struct impl_type
{
427 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
428 impl_type
*t
= calloc(1, sizeof(*t
));
431 t
->name
= strdup(name
);
435 static inline void free_impl_type(impl_type
*type
) {
440 typedef struct impl_var
{
443 unsigned reference
:1;
446 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
447 impl_var
*var
= calloc(1, sizeof(*var
));
448 var
->name
= strdup(name
);
449 var
->reference
= is_reference
;
453 static inline void free_impl_var(impl_var
*var
) {
461 typedef struct impl_def_val
{
466 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
467 impl_def_val
*def
= calloc(1, sizeof(*def
));
469 def
->text
= strdup(text
);
473 static inline void free_impl_def_val(impl_def_val
*def
) {
478 typedef struct const_type
{
483 static inline const_type
*init_const_type(token_t type
, const char *name
) {
484 const_type
*ct
= calloc(1, sizeof(*ct
));
486 ct
->name
= strdup(name
);
490 static inline void free_const_type(const_type
*type
) {
495 typedef struct constant
{
501 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
502 constant
*c
= calloc(1, sizeof(*c
));
504 c
->name
= strdup(name
);
509 static inline void free_constant(constant
*constant
) {
510 free_const_type(constant
->type
);
511 free(constant
->name
);
512 free_impl_def_val(constant
->val
);
516 typedef struct constants
{
521 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
523 constants
= calloc(1, sizeof(*constants
));
525 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
526 constants
->list
[constants
->count
-1] = constant
;
530 static inline void free_constants(constants
*c
) {
533 for (i
= 0; i
< c
->count
; ++i
) {
534 free_constant(c
->list
[i
]);
540 typedef struct impl_arg
{
548 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
549 impl_arg
*arg
= calloc(1, sizeof(*arg
));
556 static inline void free_impl_arg(impl_arg
*arg
) {
557 free_impl_type(arg
->type
);
558 free_impl_var(arg
->var
);
560 free_impl_def_val(arg
->def
);
565 typedef struct impl_vararg
{
567 struct impl_args
*args
;
573 typedef struct impl_args
{
579 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
580 impl_args
*args
= calloc(1, sizeof(*args
));
583 args
->args
= calloc(1, sizeof(*args
->args
));
589 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
590 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
591 args
->args
[args
->count
-1] = arg
;
595 static inline void free_impl_args(impl_args
*args
) {
598 for (i
= 0; i
< args
->count
; ++i
) {
599 free_impl_arg(args
->args
[i
]);
601 if (args
->vararg
.name
) {
602 free_impl_arg(args
->vararg
.name
);
608 typedef struct impl_func
{
612 impl_type
*return_type
;
613 unsigned return_reference
:1;
616 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
617 impl_func
*func
= calloc(1, sizeof(*func
));
618 func
->name
= strdup(name
);
619 func
->args
= args
? args
: init_impl_args(NULL
);
620 func
->return_type
= type
;
621 func
->return_reference
= ret_reference
;
625 static inline void free_impl_func(impl_func
*f
) {
629 free_impl_type(f
->return_type
);
630 free_impl_args(f
->args
);
635 typedef struct num_exp
{
644 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
645 struct num_exp
*operand
;
648 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
649 num_exp
*exp
= calloc(1, sizeof(*exp
));
650 switch (exp
->t
= t
) {
653 exp
->u
.numb
= strdup(num
);
658 EMPTY_SWITCH_DEFAULT_CASE();
663 static inline void free_num_exp(num_exp
*exp
) {
674 free_decl_var(exp
->u
.dvar
);
676 EMPTY_SWITCH_DEFAULT_CASE();
679 free_num_exp(exp
->operand
);
684 typedef struct let_calloc
{
689 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
690 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
691 alloc
->nmemb
= nmemb
;
696 static inline void free_let_calloc(let_calloc
*alloc
) {
697 free_num_exp(alloc
->nmemb
);
698 free_num_exp(alloc
->size
);
702 typedef struct let_func
{
709 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
710 let_func
*func
= calloc(1, sizeof(*func
));
712 func
->name
= strdup(name
);
717 static inline void free_let_func(let_func
*func
) {
718 free_impl_var(func
->var
);
723 #define PSI_LET_REFERENCE 0x1;
724 typedef struct let_val
{
740 unsigned is_reference
:1;
746 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
747 let_val
*let
= calloc(1, sizeof(*let
));
748 switch (let
->kind
= kind
) {
752 let
->data
.num
= data
;
755 let
->data
.alloc
= data
;
758 let
->data
.func
= data
;
761 let
->data
.var
= data
;
763 EMPTY_SWITCH_DEFAULT_CASE();
768 static inline void free_let_val(let_val
*let
) {
773 free_num_exp(let
->data
.num
);
776 free_let_calloc(let
->data
.alloc
);
779 free_let_func(let
->data
.func
);
782 free_decl_var(let
->data
.var
);
784 EMPTY_SWITCH_DEFAULT_CASE();
789 typedef struct let_stmt
{
796 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
797 let_stmt
*let
= calloc(1, sizeof(*let
));
803 static inline void free_let_stmt(let_stmt
*stmt
) {
805 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
806 free_decl_arg(stmt
->var
->arg
);
808 free_let_val(stmt
->val
);
810 free_decl_var(stmt
->var
);
816 typedef struct set_func
{
820 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
823 static inline set_func
*init_set_func(token_t type
, const char *name
) {
824 set_func
*func
= calloc(1, sizeof(*func
));
826 func
->name
= strdup(name
);
830 static inline void free_set_func(set_func
*func
) {
838 typedef struct set_value
{
843 struct set_value
*set
;
846 struct set_value
**inner
;
850 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
851 set_value
*val
= calloc(1, sizeof(*val
));
856 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
857 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
858 val
->inner
[val
->count
-1] = inner
;
859 inner
->outer
.set
= val
;
863 static inline void free_set_value(set_value
*val
) {
865 free_set_func(val
->func
);
868 free_decl_vars(val
->vars
);
870 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
872 for (i
= 0; i
< val
->count
; ++i
) {
873 free_set_value(val
->inner
[i
]);
878 free_num_exp(val
->num
);
883 typedef struct set_stmt
{
889 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
890 set_stmt
*set
= calloc(1, sizeof(*set
));
896 static inline void free_set_stmt(set_stmt
*set
) {
897 free_impl_var(set
->var
);
898 free_set_value(set
->val
);
902 typedef struct return_stmt
{
908 static inline return_stmt
*init_return_stmt(set_value
*val
) {
909 return_stmt
*ret
= calloc(1, sizeof(*ret
));
914 static inline void free_return_stmt(return_stmt
*ret
) {
918 free_set_value(ret
->set
);
922 typedef struct free_call
{
929 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
930 free_call
*f
= calloc(1, sizeof(*f
));
931 f
->func
= strdup(func
);
936 static inline void free_free_call(free_call
*f
) {
941 free_decl_vars(f
->vars
);
945 typedef struct free_calls
{
950 static inline free_calls
*init_free_calls(free_call
*f
) {
951 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
954 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
960 static inline void free_free_calls(free_calls
*fcs
) {
963 for (i
= 0; i
< fcs
->count
; ++i
) {
964 free_free_call(fcs
->list
[i
]);
970 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
971 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
972 fcs
->list
[fcs
->count
-1] = f
;
976 typedef struct free_stmt
{
980 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
981 free_stmt
*f
= calloc(1, sizeof(*f
));
986 static inline void free_free_stmt(free_stmt
*f
) {
987 free_free_calls(f
->calls
);
991 typedef struct impl_stmt
{
1002 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1003 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1009 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1010 switch (stmt
->type
) {
1012 free_let_stmt(stmt
->s
.let
);
1015 free_set_stmt(stmt
->s
.set
);
1018 free_return_stmt(stmt
->s
.ret
);
1021 free_free_stmt(stmt
->s
.fre
);
1027 typedef struct impl_stmts
{
1046 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1047 list
= realloc(list
, count
* sizeof(list
));
1048 ((void **)list
)[count
-1] = stmt
;
1052 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1053 switch (stmt
->type
) {
1055 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1058 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1061 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1064 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1071 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1072 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1073 return add_impl_stmt(stmts
, stmt
);
1076 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1079 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1080 free_let_stmt(stmts
->let
.list
[i
]);
1082 free(stmts
->let
.list
);
1083 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1084 free_return_stmt(stmts
->ret
.list
[i
]);
1086 free(stmts
->ret
.list
);
1087 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1088 free_set_stmt(stmts
->set
.list
[i
]);
1090 free(stmts
->set
.list
);
1091 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1092 free_free_stmt(stmts
->fre
.list
[i
]);
1094 free(stmts
->fre
.list
);
1098 typedef struct impl
{
1104 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1105 impl
*i
= calloc(1, sizeof(*i
));
1111 static inline void free_impl(impl
*impl
) {
1112 free_impl_func(impl
->func
);
1113 free_impl_stmts(impl
->stmts
);
1117 typedef struct impls
{
1122 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1124 impls
= calloc(1, sizeof(*impls
));
1126 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1127 impls
->list
[impls
->count
-1] = impl
;
1131 static void free_impls(impls
*impls
) {
1134 for (i
= 0; i
< impls
->count
; ++i
) {
1135 free_impl(impls
->list
[i
]);
1141 typedef struct decl_file
{
1146 static inline void free_decl_file(decl_file
*file
) {
1153 memset(file
, 0, sizeof(*file
));
1156 typedef struct decl_libs
{
1161 static inline void free_decl_libs(decl_libs
*libs
) {
1164 for (i
= 0; i
< libs
->count
; ++i
) {
1166 dlclose(libs
->dl
[i
]);
1171 memset(libs
, 0, sizeof(*libs
));
1174 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1175 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1176 libs
->dl
[libs
->count
-1] = dlopened
;
1179 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1182 ZEND_ASSERT(var
->arg
->var
!= var
);
1184 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1185 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1186 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1188 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1190 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1192 ret_val
= *(void **) ret_val
;
1197 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1198 impl_val
*val
, *val_ptr
;
1201 ZEND_ASSERT(var
->arg
->var
== var
);
1203 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1204 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1205 var
->array_size
, var
->arg
->var
->array_size
);
1207 if (!var
->pointer_level
&& real_decl_type(var
->arg
->type
)->type
!= PSI_T_STRUCT
) {
1211 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1212 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1214 fprintf(stderr
, "++\n");
1216 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1217 val_ptr
= val_ptr
->ptr
;
1223 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1224 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1226 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1232 #define PSI_ERROR 16
1233 #define PSI_WARNING 32
1234 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1236 #define PSI_DATA(D) ((PSI_Data *) (D))
1237 #define PSI_DATA_MEMBERS \
1238 constants *consts; \
1239 decl_typedefs *defs; \
1240 decl_structs *structs; \
1248 typedef struct PSI_Data
{
1252 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1254 dest
= malloc(sizeof(*dest
));
1256 memcpy(dest
, src
, sizeof(*dest
));
1257 memset(src
, 0, sizeof(*src
));
1261 static inline void PSI_DataDtor(PSI_Data
*data
) {
1263 free_constants(data
->consts
);
1266 free_decl_typedefs(data
->defs
);
1268 if (data
->structs
) {
1269 free_decl_structs(data
->structs
);
1272 free_decls(data
->decls
);
1275 free_impls(data
->impls
);
1277 free_decl_file(&data
->psi
.file
);
1280 typedef struct PSI_Parser
{
1285 unsigned flags
, errors
, line
, col
;
1286 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1289 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1290 return sizeof(PSI_Token
) + token_len
+ fname_len
+ sizeof(unsigned) + 2;
1293 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1295 size_t token_len
, fname_len
;
1298 if (P
->cur
< P
->tok
) {
1303 token_len
= P
->cur
- P
->tok
;
1304 fname_len
= strlen(P
->psi
.file
.fn
);
1306 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
);
1307 T
->type
= token_typ
;
1308 T
->size
= token_len
;
1309 T
->text
= &T
->buf
[0];
1310 T
->file
= &T
->buf
[token_len
+ 1];
1311 T
->line
= (void *) &T
->buf
[fname_len
+ token_len
+ 2];
1313 memcpy(T
->text
, P
->tok
, token_len
);
1314 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1315 memcpy(T
->line
, &P
->line
, sizeof(unsigned));
1320 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1321 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1322 PSI_Token
*ptr
= malloc(strct_len
);
1324 memcpy(ptr
, src
, strct_len
);
1326 ptr
->text
= &ptr
->buf
[0];
1327 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1332 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1335 PSI_Token
*T
= NULL
;
1337 va_start(argv
, argc
);
1338 for (i
= 0; i
< argc
; ++i
) {
1339 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1342 size_t fname_len
= strlen(T
->file
);
1344 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+ arg
->size
, fname_len
));
1345 memmove(&T
->buf
[T
->size
+ 1], T
->file
, fname_len
+ 1);
1346 memcpy(T
->file
- 1, arg
->text
, arg
->size
+ 1)
1347 T
->file
= &T
->buf
[T
->size
+ 1];
1349 T
= PSI_TokenCopy(arg
);
1350 T
->type
= PSI_T_NAME
;
1358 static inline const char *PSI_TokenLocation(PSI_Token
*t
) {
1359 return t
? t
->file
: "<builtin>:0:0";
1362 #define PSI_PARSER_DEBUG 0x1
1364 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1365 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1366 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1367 token_t
PSI_ParserScan(PSI_Parser
*P
);
1368 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1369 void PSI_ParserDtor(PSI_Parser
*P
);
1370 void PSI_ParserFree(PSI_Parser
**P
);