5582922453923a3a4ce8a0a1dc2e8e61577d1799
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
{
46 #ifdef HAVE_LONG_DOUBLE
55 uint8_t _dbg
[sizeof(void *)];
58 typedef struct decl_type
{
62 struct decl_type
*real
;
63 struct decl_struct
*strct
;
66 static inline decl_type
*init_decl_type(token_t type
, const char *name
) {
67 decl_type
*t
= calloc(1, sizeof(*t
));
69 t
->name
= strdup(name
);
73 static inline decl_type
*init_decl_type_ex(token_t type
, int argc
, ...) {
77 size_t len
, pos
= 0, all
= 0;
78 decl_type
*t
= calloc(1, sizeof(*t
));
81 for (i
= 0; i
< argc
; ++i
) {
82 arg
= va_arg(argv
, char *);
83 len
= va_arg(argv
, size_t);
88 ptr
= realloc(ptr
, 1 + (all
+= len
));
90 ptr
= malloc(1 + (all
= len
));
92 memcpy(ptr
+ pos
, arg
, len
);
107 static inline decl_type
*real_decl_type(decl_type
*type
) {
114 static inline void free_decl_type(decl_type
*type
) {
122 typedef struct decl_typedef
{
128 static inline decl_typedef
*init_decl_typedef(const char *name
, decl_type
*type
) {
129 decl_typedef
*t
= calloc(1, sizeof(*t
));
130 t
->alias
= strdup(name
);
135 static inline void free_decl_typedef(decl_typedef
*t
) {
140 free_decl_type(t
->type
);
144 typedef struct decl_typedefs
{
149 static inline decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
151 defs
= calloc(1, sizeof(*defs
));
153 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
154 defs
->list
[defs
->count
-1] = def
;
158 static void free_decl_typedefs(decl_typedefs
*defs
) {
161 for (i
= 0; i
< defs
->count
; ++i
) {
162 free_decl_typedef(defs
->list
[i
]);
168 typedef struct decl_var
{
171 unsigned pointer_level
;
173 struct decl_arg
*arg
;
176 static inline decl_var
*init_decl_var(const char *name
, unsigned pl
, unsigned as
) {
177 decl_var
*v
= calloc(1, sizeof(*v
));
178 v
->name
= (char *) strdup((const char *) name
);
179 v
->pointer_level
= pl
;
184 static inline void free_decl_var(decl_var
*var
) {
192 typedef struct decl_struct_layout
{
195 } decl_struct_layout
;
197 static inline decl_struct_layout
*init_decl_struct_layout(size_t pos
, size_t len
) {
198 decl_struct_layout
*l
= calloc(1, sizeof(*l
));
205 static inline void free_decl_struct_layout(decl_struct_layout
*l
) {
209 typedef struct decl_arg
{
213 decl_struct_layout
*layout
;
214 struct let_stmt
*let
;
220 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
221 decl_arg
*arg
= calloc(1, sizeof(*arg
));
222 arg
->token
= var
->token
;
226 arg
->ptr
= &arg
->val
;
230 static inline void free_decl_arg(decl_arg
*arg
) {
231 free_decl_type(arg
->type
);
232 free_decl_var(arg
->var
);
234 free_decl_struct_layout(arg
->layout
);
239 typedef struct decl_vars
{
244 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
245 decl_vars
*vars
= calloc(1, sizeof(*vars
));
248 vars
->vars
= calloc(1, sizeof(*vars
->vars
));
254 static inline decl_vars
*add_decl_var(decl_vars
*vars
, decl_var
*var
) {
255 vars
->vars
= realloc(vars
->vars
, ++vars
->count
* sizeof(*vars
->vars
));
256 vars
->vars
[vars
->count
-1] = var
;
260 static inline void free_decl_vars(decl_vars
*vars
) {
263 for (i
= 0; i
< vars
->count
; ++i
) {
264 free_decl_var(vars
->vars
[i
]);
270 typedef struct decl_args
{
276 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
277 decl_args
*args
= calloc(1, sizeof(*args
));
280 args
->args
= calloc(1, sizeof(*args
->args
));
286 static inline decl_args
*add_decl_arg(decl_args
*args
, decl_arg
*arg
) {
287 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
288 args
->args
[args
->count
-1] = arg
;
292 static inline void free_decl_args(decl_args
*args
) {
295 for (i
= 0; i
< args
->count
; ++i
) {
296 free_decl_arg(args
->args
[i
]);
302 typedef struct decl_abi
{
307 static inline decl_abi
*init_decl_abi(const char *convention
) {
308 decl_abi
*abi
= calloc(1, sizeof(*abi
));
309 abi
->convention
= strdup(convention
);
313 static inline void free_decl_abi(decl_abi
*abi
) {
317 free(abi
->convention
);
321 typedef struct decl_callinfo
{
329 typedef struct decl
{
337 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
338 decl
*d
= calloc(1, sizeof(*d
));
345 static inline void free_decl(decl
*d
) {
346 free_decl_abi(d
->abi
);
347 free_decl_arg(d
->func
);
349 free_decl_args(d
->args
);
354 typedef struct decls
{
359 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
361 decls
= calloc(1, sizeof(*decls
));
363 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
364 decls
->list
[decls
->count
-1] = decl
;
368 static inline void free_decls(decls
*decls
) {
371 for (i
= 0; i
< decls
->count
; ++i
) {
372 free_decl(decls
->list
[i
]);
378 typedef struct decl_struct
{
385 void (*dtor
)(void *type
);
389 static inline decl_struct
*init_decl_struct(const char *name
, decl_args
*args
) {
390 decl_struct
*s
= calloc(1, sizeof(*s
));
391 s
->name
= strdup(name
);
396 static inline void free_decl_struct(decl_struct
*s
) {
401 free_decl_args(s
->args
);
403 if (s
->engine
.type
&& s
->engine
.dtor
) {
404 s
->engine
.dtor(s
->engine
.type
);
410 typedef struct decl_structs
{
415 static inline decl_structs
*add_decl_struct(decl_structs
*ss
, decl_struct
*s
) {
417 ss
= calloc(1, sizeof(*ss
));
419 ss
->list
= realloc(ss
->list
, ++ss
->count
* sizeof(*ss
->list
));
420 ss
->list
[ss
->count
-1] = s
;
424 static inline void free_decl_structs(decl_structs
*ss
) {
427 for (i
= 0; i
< ss
->count
; ++i
) {
428 free_decl_struct(ss
->list
[i
]);
434 typedef struct impl_type
{
439 static inline impl_type
*init_impl_type(token_t type
, const char *name
) {
440 impl_type
*t
= calloc(1, sizeof(*t
));
443 t
->name
= strdup(name
);
447 static inline void free_impl_type(impl_type
*type
) {
452 typedef struct impl_var
{
455 unsigned reference
:1;
458 static inline impl_var
*init_impl_var(const char *name
, int is_reference
) {
459 impl_var
*var
= calloc(1, sizeof(*var
));
460 var
->name
= strdup(name
);
461 var
->reference
= is_reference
;
465 static inline void free_impl_var(impl_var
*var
) {
473 typedef struct impl_def_val
{
478 static inline impl_def_val
*init_impl_def_val(token_t t
, const char *text
) {
479 impl_def_val
*def
= calloc(1, sizeof(*def
));
481 def
->text
= strdup(text
);
485 static inline void free_impl_def_val(impl_def_val
*def
) {
490 typedef struct const_type
{
495 static inline const_type
*init_const_type(token_t type
, const char *name
) {
496 const_type
*ct
= calloc(1, sizeof(*ct
));
498 ct
->name
= strdup(name
);
502 static inline void free_const_type(const_type
*type
) {
507 typedef struct constant
{
513 static inline constant
*init_constant(const_type
*type
, const char *name
, impl_def_val
*val
) {
514 constant
*c
= calloc(1, sizeof(*c
));
516 c
->name
= strdup(name
);
521 static inline void free_constant(constant
*constant
) {
522 free_const_type(constant
->type
);
523 free(constant
->name
);
524 free_impl_def_val(constant
->val
);
528 typedef struct constants
{
533 static inline constants
*add_constant(constants
*constants
, constant
*constant
) {
535 constants
= calloc(1, sizeof(*constants
));
537 constants
->list
= realloc(constants
->list
, ++constants
->count
* sizeof(*constants
->list
));
538 constants
->list
[constants
->count
-1] = constant
;
542 static inline void free_constants(constants
*c
) {
545 for (i
= 0; i
< c
->count
; ++i
) {
546 free_constant(c
->list
[i
]);
552 typedef struct impl_arg
{
560 static inline impl_arg
*init_impl_arg(impl_type
*type
, impl_var
*var
, impl_def_val
*def
) {
561 impl_arg
*arg
= calloc(1, sizeof(*arg
));
568 static inline void free_impl_arg(impl_arg
*arg
) {
569 free_impl_type(arg
->type
);
570 free_impl_var(arg
->var
);
572 free_impl_def_val(arg
->def
);
577 typedef struct impl_vararg
{
579 struct impl_args
*args
;
585 typedef struct impl_args
{
591 static inline impl_args
*init_impl_args(impl_arg
*arg
) {
592 impl_args
*args
= calloc(1, sizeof(*args
));
595 args
->args
= calloc(1, sizeof(*args
->args
));
601 static inline impl_args
*add_impl_arg(impl_args
*args
, impl_arg
*arg
) {
602 args
->args
= realloc(args
->args
, ++args
->count
* sizeof(*args
->args
));
603 args
->args
[args
->count
-1] = arg
;
607 static inline void free_impl_args(impl_args
*args
) {
610 for (i
= 0; i
< args
->count
; ++i
) {
611 free_impl_arg(args
->args
[i
]);
613 if (args
->vararg
.name
) {
614 free_impl_arg(args
->vararg
.name
);
620 typedef struct impl_func
{
624 impl_type
*return_type
;
625 unsigned return_reference
:1;
628 static inline impl_func
*init_impl_func(char *name
, impl_args
*args
, impl_type
*type
, int ret_reference
) {
629 impl_func
*func
= calloc(1, sizeof(*func
));
630 func
->name
= strdup(name
);
631 func
->args
= args
? args
: init_impl_args(NULL
);
632 func
->return_type
= type
;
633 func
->return_reference
= ret_reference
;
637 static inline void free_impl_func(impl_func
*f
) {
641 free_impl_type(f
->return_type
);
642 free_impl_args(f
->args
);
647 typedef struct num_exp
{
656 int (*calculator
)(int t1
, impl_val
*v1
, int t2
, impl_val
*v2
, impl_val
*res
);
657 struct num_exp
*operand
;
660 static inline num_exp
*init_num_exp(token_t t
, void *num
) {
661 num_exp
*exp
= calloc(1, sizeof(*exp
));
662 switch (exp
->t
= t
) {
665 exp
->u
.numb
= strdup(num
);
670 EMPTY_SWITCH_DEFAULT_CASE();
675 static inline void free_num_exp(num_exp
*exp
) {
686 free_decl_var(exp
->u
.dvar
);
688 EMPTY_SWITCH_DEFAULT_CASE();
691 free_num_exp(exp
->operand
);
696 typedef struct let_calloc
{
701 static inline let_calloc
*init_let_calloc(num_exp
*nmemb
, num_exp
*size
) {
702 let_calloc
*alloc
= calloc(1, sizeof(*alloc
));
703 alloc
->nmemb
= nmemb
;
708 static inline void free_let_calloc(let_calloc
*alloc
) {
709 free_num_exp(alloc
->nmemb
);
710 free_num_exp(alloc
->size
);
714 typedef struct let_func
{
721 static inline let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
722 let_func
*func
= calloc(1, sizeof(*func
));
724 func
->name
= strdup(name
);
729 static inline void free_let_func(let_func
*func
) {
730 free_impl_var(func
->var
);
735 #define PSI_LET_REFERENCE 0x1;
736 typedef struct let_val
{
752 unsigned is_reference
:1;
758 static inline let_val
*init_let_val(enum let_val_kind kind
, void *data
) {
759 let_val
*let
= calloc(1, sizeof(*let
));
760 switch (let
->kind
= kind
) {
764 let
->data
.num
= data
;
767 let
->data
.alloc
= data
;
770 let
->data
.func
= data
;
773 let
->data
.var
= data
;
775 EMPTY_SWITCH_DEFAULT_CASE();
780 static inline void free_let_val(let_val
*let
) {
785 free_num_exp(let
->data
.num
);
788 free_let_calloc(let
->data
.alloc
);
791 free_let_func(let
->data
.func
);
794 free_decl_var(let
->data
.var
);
796 EMPTY_SWITCH_DEFAULT_CASE();
801 typedef struct let_stmt
{
808 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_val
*val
) {
809 let_stmt
*let
= calloc(1, sizeof(*let
));
815 static inline void free_let_stmt(let_stmt
*stmt
) {
817 if (stmt
->val
->kind
== PSI_LET_TMP
&& stmt
->var
->arg
) {
818 free_decl_arg(stmt
->var
->arg
);
820 free_let_val(stmt
->val
);
822 free_decl_var(stmt
->var
);
828 typedef struct set_func
{
832 void (*handler
)(zval
*, struct set_value
*set
, impl_val
*ret_val
);
835 static inline set_func
*init_set_func(token_t type
, const char *name
) {
836 set_func
*func
= calloc(1, sizeof(*func
));
838 func
->name
= strdup(name
);
842 static inline void free_set_func(set_func
*func
) {
850 typedef struct set_value
{
855 struct set_value
*set
;
858 struct set_value
**inner
;
862 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
863 set_value
*val
= calloc(1, sizeof(*val
));
868 static inline set_value
*add_inner_set_value(set_value
*val
, set_value
*inner
) {
869 val
->inner
= realloc(val
->inner
, ++val
->count
* sizeof(*val
->inner
));
870 val
->inner
[val
->count
-1] = inner
;
871 inner
->outer
.set
= val
;
875 static inline void free_set_value(set_value
*val
) {
877 free_set_func(val
->func
);
880 free_decl_vars(val
->vars
);
882 if (val
->inner
&& (!val
->outer
.set
|| val
->outer
.set
->inner
!= val
->inner
)) {
884 for (i
= 0; i
< val
->count
; ++i
) {
885 free_set_value(val
->inner
[i
]);
890 free_num_exp(val
->num
);
895 typedef struct set_stmt
{
901 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
902 set_stmt
*set
= calloc(1, sizeof(*set
));
908 static inline void free_set_stmt(set_stmt
*set
) {
909 free_impl_var(set
->var
);
910 free_set_value(set
->val
);
914 typedef struct return_stmt
{
920 static inline return_stmt
*init_return_stmt(set_value
*val
) {
921 return_stmt
*ret
= calloc(1, sizeof(*ret
));
926 static inline void free_return_stmt(return_stmt
*ret
) {
930 free_set_value(ret
->set
);
934 typedef struct free_call
{
941 static inline free_call
*init_free_call(const char *func
, decl_vars
*vars
) {
942 free_call
*f
= calloc(1, sizeof(*f
));
943 f
->func
= strdup(func
);
948 static inline void free_free_call(free_call
*f
) {
953 free_decl_vars(f
->vars
);
957 typedef struct free_calls
{
962 static inline free_calls
*init_free_calls(free_call
*f
) {
963 free_calls
*fcs
= calloc(1, sizeof(*fcs
));
966 fcs
->list
= calloc(1, sizeof(*fcs
->list
));
972 static inline void free_free_calls(free_calls
*fcs
) {
975 for (i
= 0; i
< fcs
->count
; ++i
) {
976 free_free_call(fcs
->list
[i
]);
982 static inline free_calls
*add_free_call(free_calls
*fcs
, free_call
*f
) {
983 fcs
->list
= realloc(fcs
->list
, ++fcs
->count
* sizeof(*fcs
->list
));
984 fcs
->list
[fcs
->count
-1] = f
;
988 typedef struct free_stmt
{
992 static inline free_stmt
*init_free_stmt(free_calls
*calls
) {
993 free_stmt
*f
= calloc(1, sizeof(*f
));
998 static inline void free_free_stmt(free_stmt
*f
) {
999 free_free_calls(f
->calls
);
1003 typedef struct impl_stmt
{
1014 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
1015 impl_stmt
*stmt
= calloc(1, sizeof(*stmt
));
1021 static inline void free_impl_stmt(impl_stmt
*stmt
) {
1022 switch (stmt
->type
) {
1024 free_let_stmt(stmt
->s
.let
);
1027 free_set_stmt(stmt
->s
.set
);
1030 free_return_stmt(stmt
->s
.ret
);
1033 free_free_stmt(stmt
->s
.fre
);
1039 typedef struct impl_stmts
{
1058 static inline void *add_impl_stmt_ex(void *list
, size_t count
, void *stmt
) {
1059 list
= realloc(list
, count
* sizeof(list
));
1060 ((void **)list
)[count
-1] = stmt
;
1064 static inline impl_stmts
*add_impl_stmt(impl_stmts
*stmts
, impl_stmt
*stmt
) {
1065 switch (stmt
->type
) {
1067 stmts
->ret
.list
= add_impl_stmt_ex(stmts
->ret
.list
, ++stmts
->ret
.count
, stmt
->s
.ret
);
1070 stmts
->let
.list
= add_impl_stmt_ex(stmts
->let
.list
, ++stmts
->let
.count
, stmt
->s
.let
);
1073 stmts
->set
.list
= add_impl_stmt_ex(stmts
->set
.list
, ++stmts
->set
.count
, stmt
->s
.set
);
1076 stmts
->fre
.list
= add_impl_stmt_ex(stmts
->fre
.list
, ++stmts
->fre
.count
, stmt
->s
.fre
);
1083 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
1084 impl_stmts
*stmts
= calloc(1, sizeof(*stmts
));
1085 return add_impl_stmt(stmts
, stmt
);
1088 static inline void free_impl_stmts(impl_stmts
*stmts
) {
1091 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
1092 free_let_stmt(stmts
->let
.list
[i
]);
1094 free(stmts
->let
.list
);
1095 for (i
= 0; i
< stmts
->ret
.count
; ++i
) {
1096 free_return_stmt(stmts
->ret
.list
[i
]);
1098 free(stmts
->ret
.list
);
1099 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
1100 free_set_stmt(stmts
->set
.list
[i
]);
1102 free(stmts
->set
.list
);
1103 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
1104 free_free_stmt(stmts
->fre
.list
[i
]);
1106 free(stmts
->fre
.list
);
1110 typedef struct impl
{
1116 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
1117 impl
*i
= calloc(1, sizeof(*i
));
1123 static inline void free_impl(impl
*impl
) {
1124 free_impl_func(impl
->func
);
1125 free_impl_stmts(impl
->stmts
);
1129 typedef struct impls
{
1134 static inline impls
*add_impl(impls
*impls
, impl
*impl
) {
1136 impls
= calloc(1, sizeof(*impls
));
1138 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
1139 impls
->list
[impls
->count
-1] = impl
;
1143 static void free_impls(impls
*impls
) {
1146 for (i
= 0; i
< impls
->count
; ++i
) {
1147 free_impl(impls
->list
[i
]);
1153 typedef struct decl_file
{
1158 static inline void free_decl_file(decl_file
*file
) {
1165 memset(file
, 0, sizeof(*file
));
1168 typedef struct decl_libs
{
1173 static inline void free_decl_libs(decl_libs
*libs
) {
1176 for (i
= 0; i
< libs
->count
; ++i
) {
1178 dlclose(libs
->dl
[i
]);
1183 memset(libs
, 0, sizeof(*libs
));
1186 static inline void add_decl_lib(decl_libs
*libs
, void *dlopened
) {
1187 libs
->dl
= realloc(libs
->dl
, ++libs
->count
* sizeof(*libs
->dl
));
1188 libs
->dl
[libs
->count
-1] = dlopened
;
1191 static inline impl_val
*deref_impl_val(impl_val
*ret_val
, decl_var
*var
) {
1194 ZEND_ASSERT(var
->arg
->var
!= var
);
1196 fprintf(stderr
, "deref: %s pl=%u:%u as=%u:%u %p\n",
1197 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1198 var
->array_size
, var
->arg
->var
->array_size
, ret_val
);
1200 for (i
= 0; i
< var
->pointer_level
; ++i
) {
1202 fprintf(stderr
, "-- %p %p %p\n", ret_val
, *(void**)ret_val
, ret_val
->ptr
);
1204 ret_val
= *(void **) ret_val
;
1209 static inline impl_val
*enref_impl_val(void *ptr
, decl_var
*var
) {
1210 impl_val
*val
, *val_ptr
;
1213 ZEND_ASSERT(var
->arg
->var
== var
);
1215 fprintf(stderr
, "enref: %s pl=%u:%u as=%u:%u\n",
1216 var
->name
, var
->pointer_level
, var
->arg
->var
->pointer_level
,
1217 var
->array_size
, var
->arg
->var
->array_size
);
1219 if (!var
->pointer_level
){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1223 val
= val_ptr
= calloc(var
->pointer_level
+ 1, sizeof(void *));
1224 for (i
= !var
->arg
->var
->array_size
; i
< var
->pointer_level
; ++i
) {
1226 fprintf(stderr
, "++\n");
1228 val_ptr
->ptr
= (void **) val_ptr
+ 1;
1229 val_ptr
= val_ptr
->ptr
;
1235 static inline impl_val
*struct_member_ref(decl_arg
*set_arg
, impl_val
*struct_ptr
, impl_val
**to_free
) {
1236 void *ptr
= (char *) struct_ptr
+ set_arg
->layout
->pos
;
1238 fprintf(stderr
, "struct member %s: %p\n", set_arg
->var
->name
, ptr
);
1244 #define PSI_ERROR 16
1245 #define PSI_WARNING 32
1246 typedef void (*psi_error_cb
)(PSI_Token
*token
, int type
, const char *msg
, ...);
1248 #define PSI_DATA(D) ((PSI_Data *) (D))
1249 #define PSI_DATA_MEMBERS \
1250 constants *consts; \
1251 decl_typedefs *defs; \
1252 decl_structs *structs; \
1260 typedef struct PSI_Data
{
1264 static inline PSI_Data
*PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
1266 dest
= malloc(sizeof(*dest
));
1268 memcpy(dest
, src
, sizeof(*dest
));
1269 memset(src
, 0, sizeof(*src
));
1273 static inline void PSI_DataDtor(PSI_Data
*data
) {
1275 free_constants(data
->consts
);
1278 free_decl_typedefs(data
->defs
);
1280 if (data
->structs
) {
1281 free_decl_structs(data
->structs
);
1284 free_decls(data
->decls
);
1287 free_impls(data
->impls
);
1289 free_decl_file(&data
->psi
.file
);
1292 typedef struct PSI_Parser
{
1297 unsigned flags
, errors
, line
, col
;
1298 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
1301 static inline size_t PSI_TokenAllocSize(size_t token_len
, size_t fname_len
) {
1302 return sizeof(PSI_Token
) + token_len
+ fname_len
+ 2;
1305 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
1307 size_t token_len
, fname_len
;
1310 if (P
->cur
< P
->tok
) {
1315 token_len
= P
->cur
- P
->tok
;
1316 fname_len
= strlen(P
->psi
.file
.fn
);
1318 T
= calloc(1, PSI_TokenAllocSize(token_len
, fname_len
));
1319 T
->type
= token_typ
;
1320 T
->size
= token_len
;
1321 T
->text
= &T
->buf
[0];
1322 T
->file
= &T
->buf
[token_len
+ 1];
1325 memcpy(T
->text
, P
->tok
, token_len
);
1326 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
1331 static inline PSI_Token
*PSI_TokenCopy(PSI_Token
*src
) {
1332 size_t strct_len
= PSI_TokenAllocSize(src
->size
, strlen(src
->file
));
1333 PSI_Token
*ptr
= malloc(strct_len
);
1335 memcpy(ptr
, src
, strct_len
);
1337 ptr
->text
= &ptr
->buf
[0];
1338 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
1343 static inline PSI_Token
*PSI_TokenCat(unsigned argc
, ...) {
1346 PSI_Token
*T
= NULL
;
1348 va_start(argv
, argc
);
1349 for (i
= 0; i
< argc
; ++i
) {
1350 PSI_Token
*arg
= va_arg(argv
, PSI_Token
*);
1353 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
1355 T
= realloc(T
, PSI_TokenAllocSize(T
->size
+= arg
->size
+ 1, fname_len
));
1356 T
->text
= &T
->buf
[0];
1357 T
->file
= &T
->buf
[T
->size
+ 1];
1358 T
->buf
[token_len
] = ' ';
1359 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
1360 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
1362 T
= PSI_TokenCopy(arg
);
1363 T
->type
= PSI_T_NAME
;
1371 static inline const char *PSI_TokenLocation(PSI_Token
*t
) {
1372 return t
? t
->file
: "<builtin>:0:0";
1375 #define PSI_PARSER_DEBUG 0x1
1377 PSI_Parser
*PSI_ParserInit(PSI_Parser
*P
, const char *filename
, psi_error_cb error
, unsigned flags
);
1378 void PSI_ParserSyntaxError(PSI_Parser
*P
, const char *fn
, size_t ln
, const char *msg
, ...);
1379 size_t PSI_ParserFill(PSI_Parser
*P
, size_t n
);
1380 token_t
PSI_ParserScan(PSI_Parser
*P
);
1381 void PSI_ParserParse(PSI_Parser
*P
, PSI_Token
*src
);
1382 void PSI_ParserDtor(PSI_Parser
*P
);
1383 void PSI_ParserFree(PSI_Parser
**P
);