simplify parser
[m6w6/ext-psi] / src / parser.h
1 #ifndef _PSI_PARSER_H
2 #define _PSI_PARSER_H
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8
9 #include <Zend/zend_types.h>
10
11 #include "parser_proc.h"
12
13 #define BSIZE 256
14
15 typedef int token_t;
16
17 /* in php_psi.h */
18 size_t psi_t_alignment(token_t);
19 size_t psi_t_size(token_t);
20
21 typedef struct PSI_Token {
22 token_t type;
23 unsigned line;
24 size_t size;
25 char text[1];
26 } PSI_Token;
27
28 typedef struct decl_type {
29 char *name;
30 token_t type;
31 struct decl_type *real;
32 struct decl_struct *strct;
33 } decl_type;
34
35 static inline decl_type *init_decl_type(token_t type, char *name) {
36 decl_type *t = calloc(1, sizeof(*t));
37 t->type = type;
38 t->name = strdup(name);
39 return t;
40 }
41
42 static inline decl_type *real_decl_type(decl_type *type) {
43 while (type->real) {
44 type = type->real;
45 }
46 return type;
47 }
48
49 static inline void free_decl_type(decl_type *type) {
50 free(type->name);
51 free(type);
52 }
53
54 typedef struct decl_typedef {
55 char *alias;
56 decl_type *type;
57 } decl_typedef;
58
59 static inline decl_typedef *init_decl_typedef(char *name, decl_type *type) {
60 decl_typedef *t = calloc(1, sizeof(*t));
61 t->alias = strdup(name);
62 t->type = type;
63 return t;
64 }
65
66 static inline void free_decl_typedef(decl_typedef *t) {
67 free(t->alias);
68 free_decl_type(t->type);
69 free(t);
70 }
71
72 typedef struct decl_typedefs {
73 size_t count;
74 decl_typedef **list;
75 } decl_typedefs;
76
77 static inline decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
78 if (!defs) {
79 defs = calloc(1, sizeof(*defs));
80 }
81 defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list));
82 defs->list[defs->count-1] = def;
83 return defs;
84 }
85
86 static void free_decl_typedefs(decl_typedefs *defs) {
87 size_t i;
88
89 for (i = 0; i < defs->count; ++i) {
90 free_decl_typedef(defs->list[i]);
91 }
92 free(defs->list);
93 free(defs);
94 }
95
96 typedef struct decl_var {
97 char *name;
98 unsigned pointer_level;
99 unsigned array_size;
100 struct decl_arg *arg;
101 } decl_var;
102
103 static inline decl_var *init_decl_var(char *name, unsigned pl, unsigned as) {
104 decl_var *v = calloc(1, sizeof(*v));
105 v->name = (char *) strdup((const char *) name);
106 v->pointer_level = pl;
107 v->array_size = as;
108 return v;
109 }
110
111 static inline void free_decl_var(decl_var *var) {
112 free(var->name);
113 free(var);
114 }
115
116 typedef struct decl_arg {
117 decl_type *type;
118 decl_var *var;
119 struct let_stmt *let;
120 } decl_arg;
121
122 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
123 decl_arg *arg = calloc(1, sizeof(*arg));
124 arg->type = type;
125 arg->var = var;
126 arg->let = NULL;
127 return arg;
128 }
129
130 static inline void free_decl_arg(decl_arg *arg) {
131 free_decl_type(arg->type);
132 free_decl_var(arg->var);
133 free(arg);
134 }
135
136 typedef struct decl_vars {
137 decl_var **vars;
138 size_t count;
139 } decl_vars;
140
141 static inline decl_vars *init_decl_vars(decl_var *var) {
142 decl_vars *vars = calloc(1, sizeof(*vars));
143 vars->count = 1;
144 vars->vars = calloc(1, sizeof(*vars->vars));
145 vars->vars[0] = var;
146 return vars;
147 }
148
149 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
150 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
151 vars->vars[vars->count-1] = var;
152 return vars;
153 }
154
155 static inline void free_decl_vars(decl_vars *vars) {
156 size_t i;
157
158 for (i = 0; i < vars->count; ++i) {
159 free_decl_var(vars->vars[i]);
160 }
161 free(vars->vars);
162 free(vars);
163 }
164
165 typedef struct decl_args {
166 decl_arg **args;
167 size_t count;
168 } decl_args;
169
170 static inline decl_args *init_decl_args(decl_arg *arg) {
171 decl_args *args = calloc(1, sizeof(*args));
172 args->count = 1;
173 args->args = calloc(1, sizeof(*args->args));
174 args->args[0] = arg;
175 return args;
176 }
177
178 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
179 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
180 args->args[args->count-1] = arg;
181 return args;
182 }
183
184 static inline void free_decl_args(decl_args *args) {
185 size_t i;
186
187 for (i = 0; i < args->count; ++i) {
188 free_decl_arg(args->args[i]);
189 }
190 free(args->args);
191 free(args);
192 }
193
194 typedef struct decl_abi {
195 char *convention;
196 } decl_abi;
197
198 static inline decl_abi *init_decl_abi(char *convention) {
199 decl_abi *abi = calloc(1, sizeof(*abi));
200 abi->convention = strdup(convention);
201 return abi;
202 }
203
204 static inline void free_decl_abi(decl_abi *abi) {
205 free(abi->convention);
206 free(abi);
207 }
208
209 typedef struct decl {
210 decl_abi *abi;
211 decl_arg *func;
212 decl_args *args;
213 void *dlptr;
214 } decl;
215
216 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
217 decl *d = calloc(1, sizeof(*d));
218 d->abi = abi;
219 d->func = func;
220 d->args = args;
221 return d;
222 }
223
224 static inline void free_decl(decl *d) {
225 free_decl_abi(d->abi);
226 free_decl_arg(d->func);
227 if (d->args) {
228 free_decl_args(d->args);
229 }
230 free(d);
231 }
232
233 typedef struct decls {
234 size_t count;
235 decl **list;
236 } decls;
237
238 static inline decls *add_decl(decls *decls, decl *decl) {
239 if (!decls) {
240 decls = calloc(1, sizeof(*decls));
241 }
242 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
243 decls->list[decls->count-1] = decl;
244 return decls;
245 }
246
247 static inline void free_decls(decls *decls) {
248 size_t i;
249
250 for (i = 0; i < decls->count; ++i) {
251 free_decl(decls->list[i]);
252 }
253 free(decls->list);
254 free(decls);
255 }
256
257 typedef struct decl_struct_layout {
258 size_t pos;
259 size_t len;
260 } decl_struct_layout;
261
262 typedef struct decl_struct {
263 char *name;
264 decl_args *args;
265 decl_struct_layout *layout;
266 } decl_struct;
267
268 static inline decl_struct *init_decl_struct(char *name, decl_args *args) {
269 decl_struct *s = calloc(1, sizeof(*s));
270 s->name = strdup(name);
271 s->args = args;
272 return s;
273 }
274
275 static inline void free_decl_struct(decl_struct *s) {
276 if (s->args) {
277 free_decl_args(s->args);
278 }
279 if (s->layout) {
280 free(s->layout);
281 }
282 free(s->name);
283 free(s);
284 }
285
286 static inline size_t decl_struct_size(decl_struct *s) {
287 size_t c = s->args->count - 1;
288 decl_type *type = real_decl_type(s->args->args[c]->type);
289 return s->layout[c].pos + psi_t_alignment(type->type);
290 }
291
292 typedef struct decl_structs {
293 size_t count;
294 decl_struct **list;
295 } decl_structs;
296
297 static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
298 if (!ss) {
299 ss = calloc(1, sizeof(*ss));
300 }
301 ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
302 ss->list[ss->count-1] = s;
303 return ss;
304 }
305
306 static inline void free_decl_structs(decl_structs *ss) {
307 size_t i;
308
309 for (i = 0; i < ss->count; ++i) {
310 free_decl_struct(ss->list[i]);
311 }
312 free(ss->list);
313 free(ss);
314 }
315
316 typedef union impl_val {
317 char cval;
318 int8_t i8;
319 short sval;
320 int16_t i16;
321 int ival;
322 int32_t i32;
323 long lval;
324 int64_t i64;
325 float fval;
326 double dval;
327 union {
328 zend_bool bval;
329 zend_long lval;
330 zend_string *str;
331 } zend;
332 void *ptr;
333 uint8_t _dbg[sizeof(void *)];
334 } impl_val;
335
336 static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
337 unsigned i;
338
339 if (var->arg->var != var) for (i = 0; i < var->pointer_level; ++i) {
340 ret_val = *(void **) ret_val;
341 }
342 return ret_val;
343 }
344
345 typedef struct impl_type {
346 char *name;
347 token_t type;
348 } impl_type;
349
350 static inline impl_type *init_impl_type(token_t type, char *name) {
351 impl_type *t = calloc(1, sizeof(*t));
352
353 t->type = type;
354 t->name = (char *) strdup((const char *) name);
355 return t;
356 }
357
358 static inline void free_impl_type(impl_type *type) {
359 free(type->name);
360 free(type);
361 }
362
363 typedef struct impl_var {
364 char *name;
365 unsigned reference:1;
366 } impl_var;
367
368 static inline impl_var *init_impl_var(char *name, int is_reference) {
369 impl_var *var = calloc(1, sizeof(*var));
370 var->name = (char *) strdup((const char *) name);
371 var->reference = is_reference;
372 return var;
373 }
374
375 static inline void free_impl_var(impl_var *var) {
376 free(var->name);
377 free(var);
378 }
379
380 typedef struct impl_def_val {
381 token_t type;
382 char *text;
383 } impl_def_val;
384
385 static inline impl_def_val *init_impl_def_val(PSI_Token *T) {
386 impl_def_val *def = calloc(1, sizeof(*def));
387 def->type = T->type;
388 def->text = strdup(T->text);
389 return def;
390 }
391
392 static inline void free_impl_def_val(impl_def_val *def) {
393 free(def->text);
394 free(def);
395 }
396
397 typedef struct impl_arg {
398 impl_type *type;
399 impl_var *var;
400 impl_def_val *def;
401 impl_val val;
402 zval *_zv;
403 } impl_arg;
404
405 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
406 impl_arg *arg = calloc(1, sizeof(*arg));
407 arg->type = type;
408 arg->var = var;
409 arg->def = def;
410 return arg;
411 }
412
413 static inline void free_impl_arg(impl_arg *arg) {
414 free_impl_type(arg->type);
415 free_impl_var(arg->var);
416 if (arg->def) {
417 free_impl_def_val(arg->def);
418 }
419 free(arg);
420 }
421
422 typedef struct impl_args {
423 impl_arg **args;
424 size_t count;
425 } impl_args;
426
427 static inline impl_args *init_impl_args(impl_arg *arg) {
428 impl_args *args = calloc(1, sizeof(*args));
429 args->args = calloc(1, sizeof(*args->args));
430 if (arg) {
431 args->count = 1;
432 args->args[0] = arg;
433 } else {
434 args->count = 0;
435 args->args = NULL;
436 }
437 return args;
438 }
439
440 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
441 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
442 args->args[args->count-1] = arg;
443 return args;
444 }
445
446 static inline void free_impl_args(impl_args *args) {
447 size_t i;
448
449 for (i = 0; i < args->count; ++i) {
450 free_impl_arg(args->args[i]);
451 }
452 free(args->args);
453 free(args);
454 }
455
456 typedef struct impl_func {
457 char *name;
458 impl_args *args;
459 impl_type *return_type;
460 unsigned return_reference:1;
461 } impl_func;
462
463 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
464 impl_func *func = calloc(1, sizeof(*func));
465 func->name = strdup(name);
466 func->args = args ? args : init_impl_args(NULL);
467 func->return_type = type;
468 func->return_reference = ret_reference;
469 return func;
470 }
471
472 static inline void free_impl_func(impl_func *f) {
473 free_impl_type(f->return_type);
474 free_impl_args(f->args);
475 free(f->name);
476 free(f);
477 }
478
479 typedef struct let_calloc {
480 size_t n;
481 decl_type *type;
482 } let_calloc;
483
484 static inline let_calloc *init_let_calloc(long n, decl_type *type) {
485 let_calloc *alloc = calloc(1, sizeof(*alloc));
486 alloc->n = n;
487 alloc->type = type;
488 return alloc;
489 }
490
491 static inline void free_let_calloc(let_calloc *alloc) {
492 free_decl_type(alloc->type);
493 free(alloc);
494 }
495
496 typedef struct let_func {
497 token_t type;
498 char *name;
499 let_calloc *alloc;
500 } let_func;
501
502 static inline let_func *init_let_func(token_t type, char *name, let_calloc *alloc) {
503 let_func *func = calloc(1, sizeof(*func));
504 func->type = type;
505 func->name = strdup(name);
506 func->alloc = alloc;
507 return func;
508 }
509
510 static inline void free_let_func(let_func *func) {
511 if (func->alloc) {
512 free_let_calloc(func->alloc);
513 }
514 free(func->name);
515 free(func);
516 }
517
518 typedef struct let_value {
519 let_func *func;
520 impl_var *var;
521 unsigned is_reference:1;
522 } let_value;
523
524 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
525 let_value *val = calloc(1, sizeof(*val));
526 val->is_reference = is_reference;
527 val->func = func;
528 val->var = var;
529 return val;
530 }
531
532 static inline void free_let_value(let_value *val) {
533 if (val->func) {
534 free_let_func(val->func);
535 }
536 if (val->var) {
537 free_impl_var(val->var);
538 }
539 free(val);
540 }
541
542 typedef struct let_stmt {
543 decl_var *var;
544 let_value *val;
545 impl_arg *arg;
546 impl_val out;
547 void *ptr;
548 void *mem;
549 } let_stmt;
550
551 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
552 let_stmt *let = calloc(1, sizeof(*let));
553 let->var = var;
554 let->val = val;
555 return let;
556 }
557
558 static inline void free_let_stmt(let_stmt *stmt) {
559 free_decl_var(stmt->var);
560 if (stmt->val) {
561 free_let_value(stmt->val);
562 }
563 free(stmt);
564 }
565
566 typedef struct set_func {
567 token_t type;
568 char *name;
569 } set_func;
570
571 static inline set_func *init_set_func(token_t type, char *name) {
572 set_func *func = calloc(1, sizeof(*func));
573 func->type = type;
574 func->name = (char *) strdup((const char *) name);
575 return func;
576 }
577
578 static inline void free_set_func(set_func *func) {
579 free(func->name);
580 free(func);
581 }
582
583 typedef struct set_value {
584 set_func *func;
585 decl_vars *vars;
586 } set_value;
587
588 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
589 set_value *val = calloc(1, sizeof(*val));
590 val->func = func;
591 val->vars = vars;
592 return val;
593 }
594
595 static inline void free_set_value(set_value *val) {
596 free_set_func(val->func);
597 free_decl_vars(val->vars);
598 free(val);
599 }
600
601 typedef struct set_stmt {
602 impl_var *var;
603 set_value *val;
604 impl_arg *arg;
605 } set_stmt;
606
607 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
608 set_stmt *set = calloc(1, sizeof(*set));
609 set->var = var;
610 set->val = val;
611 return set;
612 }
613
614 static inline void free_set_stmt(set_stmt *set) {
615 free_impl_var(set->var);
616 free_set_value(set->val);
617 free(set);
618 }
619
620 typedef struct return_stmt {
621 set_func *func;
622 decl_var *decl;
623 } return_stmt;
624
625 static inline return_stmt *init_return_stmt(set_func *func, decl_var *decl) {
626 return_stmt *ret = calloc(1, sizeof(*ret));
627 ret->func = func;
628 ret->decl = decl;
629 return ret;
630 }
631
632 static inline void free_return_stmt(return_stmt *ret) {
633 free_set_func(ret->func);
634 free_decl_var(ret->decl);
635 free(ret);
636 }
637
638 typedef struct free_stmt {
639 decl_vars *vars;
640 } free_stmt;
641
642 static inline free_stmt *init_free_stmt(decl_vars *vars) {
643 free_stmt *free_ = calloc(1, sizeof(*free_));
644 free_->vars = vars;
645 return free_;
646 }
647
648 static inline void free_free_stmt(free_stmt *free_) {
649 free_decl_vars(free_->vars);
650 free(free_);
651 }
652
653 typedef struct impl_stmt {
654 token_t type;
655 union {
656 let_stmt *let;
657 set_stmt *set;
658 return_stmt *ret;
659 free_stmt *fre;
660 void *ptr;
661 } s;
662 } impl_stmt;
663
664 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
665 impl_stmt *stmt = calloc(1, sizeof(*stmt));
666 stmt->type = type;
667 stmt->s.ptr = ptr;
668 return stmt;
669 }
670
671 static inline void free_impl_stmt(impl_stmt *stmt) {
672 switch (stmt->type) {
673 case PSI_T_LET:
674 free_let_stmt(stmt->s.let);
675 break;
676 case PSI_T_SET:
677 free_set_stmt(stmt->s.set);
678 break;
679 case PSI_T_RETURN:
680 free_return_stmt(stmt->s.ret);
681 break;
682 case PSI_T_FREE:
683 free_free_stmt(stmt->s.fre);
684 break;
685 }
686 free(stmt);
687 }
688
689 typedef struct impl_stmts {
690 struct {
691 return_stmt **list;
692 size_t count;
693 } ret;
694 struct {
695 let_stmt **list;
696 size_t count;
697 } let;
698 struct {
699 set_stmt **list;
700 size_t count;
701 } set;
702 struct {
703 free_stmt **list;
704 size_t count;
705 } fre;
706 } impl_stmts;
707
708 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
709 list = realloc(list, count * sizeof(list));
710 ((void **)list)[count-1] = stmt;
711 return list;
712 }
713
714 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
715 switch (stmt->type) {
716 case PSI_T_RETURN:
717 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
718 break;
719 case PSI_T_LET:
720 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
721 break;
722 case PSI_T_SET:
723 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
724 break;
725 case PSI_T_FREE:
726 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
727 break;
728 }
729 free(stmt);
730 return stmts;
731 }
732
733 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
734 impl_stmts *stmts = calloc(1, sizeof(*stmts));
735 return add_impl_stmt(stmts, stmt);
736 }
737
738 static inline void free_impl_stmts(impl_stmts *stmts) {
739 size_t i;
740
741 for (i = 0; i < stmts->let.count; ++i) {
742 free_let_stmt(stmts->let.list[i]);
743 }
744 free(stmts->let.list);
745 for (i = 0; i < stmts->ret.count; ++i) {
746 free_return_stmt(stmts->ret.list[i]);
747 }
748 free(stmts->ret.list);
749 for (i = 0; i < stmts->set.count; ++i) {
750 free_set_stmt(stmts->set.list[i]);
751 }
752 free(stmts->set.list);
753 for (i = 0; i < stmts->fre.count; ++i) {
754 free_free_stmt(stmts->fre.list[i]);
755 }
756 free(stmts->fre.list);
757 free(stmts);
758 }
759
760 typedef struct impl {
761 impl_func *func;
762 impl_stmts *stmts;
763 decl *decl;
764 } impl;
765
766 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
767 impl *i = calloc(1, sizeof(*i));
768 i->func = func;
769 i->stmts = stmts;
770 return i;
771 }
772
773 static inline void free_impl(impl *impl) {
774 free_impl_func(impl->func);
775 free_impl_stmts(impl->stmts);
776 free(impl);
777 }
778
779 typedef struct impls {
780 size_t count;
781 impl **list;
782 } impls;
783
784 static inline impls *add_impl(impls *impls, impl *impl) {
785 if (!impls) {
786 impls = calloc(1, sizeof(*impls));
787 }
788 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
789 impls->list[impls->count-1] = impl;
790 return impls;
791 }
792
793 static void free_impls(impls *impls) {
794 size_t i;
795
796 for (i = 0; i < impls->count; ++i) {
797 free_impl(impls->list[i]);
798 }
799 free(impls->list);
800 free(impls);
801 }
802
803 typedef struct const_type {
804 token_t type;
805 char *name;
806 } const_type;
807
808 static inline const_type *init_const_type(token_t type, const char *name) {
809 const_type *ct = calloc(1, sizeof(*ct));
810 ct->type = type;
811 ct->name = strdup(name);
812 return ct;
813 }
814
815 static inline void free_const_type(const_type *type) {
816 free(type->name);
817 free(type);
818 }
819
820 typedef struct constant {
821 const_type *type;
822 char *name;
823 impl_def_val *val;
824 } constant;
825
826 static inline constant *init_constant(const_type *type, char *name, impl_def_val *val) {
827 constant *c = calloc(1, sizeof(*c));
828 c->type = type;
829 c->name = strdup(name);
830 c->val = val;
831 return c;
832 }
833
834 static inline void free_constant(constant *constant) {
835 free_const_type(constant->type);
836 free(constant->name);
837 free_impl_def_val(constant->val);
838 free(constant);
839 }
840
841 typedef struct constants {
842 size_t count;
843 constant **list;
844 } constants;
845
846 static inline constants *add_constant(constants *constants, constant *constant) {
847 if (!constants) {
848 constants = calloc(1, sizeof(*constants));
849 }
850 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
851 constants->list[constants->count-1] = constant;
852 return constants;
853 }
854
855 static inline void free_constants(constants *c) {
856 size_t i;
857
858 for (i = 0; i < c->count; ++i) {
859 free_constant(c->list[i]);
860 }
861 free(c->list);
862 free(c);
863 }
864
865 #define PSI_ERROR 16
866 #define PSI_WARNING 32
867 typedef void (*psi_error_cb)(int type, const char *msg, ...);
868
869 #define PSI_DATA_MEMBERS \
870 constants *consts; \
871 decl_typedefs *defs; \
872 decl_structs *structs; \
873 decls *decls; \
874 impls *impls; \
875 char *lib; \
876 char *fn; \
877 psi_error_cb error
878 typedef struct PSI_Data {
879 PSI_DATA_MEMBERS;
880 } PSI_Data;
881
882 static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
883 memcpy(dest, src, sizeof(*dest));
884 memset(src, 0, sizeof(*src));
885 }
886
887 static inline void PSI_DataDtor(PSI_Data *data) {
888 if (data->consts) {
889 free_constants(data->consts);
890 }
891 if (data->defs) {
892 free_decl_typedefs(data->defs);
893 }
894 if (data->structs) {
895 free_decl_structs(data->structs);
896 }
897 if (data->decls) {
898 free_decls(data->decls);
899 }
900 if (data->impls) {
901 free_impls(data->impls);
902 }
903 if (data->lib) {
904 free(data->lib);
905 }
906 if (data->fn) {
907 free(data->fn);
908 }
909 }
910
911 typedef struct PSI_Parser {
912 PSI_DATA_MEMBERS;
913 FILE *fp;
914 unsigned flags;
915 unsigned errors;
916 void *proc;
917 size_t line;
918 token_t num;
919 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
920 } PSI_Parser;
921
922 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
923 PSI_Token *T;
924 size_t token_len;
925
926 if (P->cur <= P->tok) {
927 return NULL;
928 }
929
930 token_len = P->cur - P->tok;
931
932 T = calloc(1, sizeof(*T) + token_len);
933 T->type = P->num;
934 T->line = P->line;
935 T->size = token_len;
936 T->text[token_len] = 0;
937 memcpy(T->text, P->tok, token_len);
938
939 return T;
940 }
941
942 #define PSI_PARSER_DEBUG 0x1
943
944 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
945 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
946 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
947 token_t PSI_ParserScan(PSI_Parser *P);
948 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
949 void PSI_ParserDtor(PSI_Parser *P);
950 void PSI_ParserFree(PSI_Parser **P);
951
952 #endif