f56f278eaf86257b91b8ebe973ea581d18999765
[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 = malloc(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 = malloc(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 = malloc(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 = malloc(sizeof(*vars));
143 vars->count = 1;
144 vars->vars = malloc(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 = malloc(sizeof(*args));
172 args->count = 1;
173 args->args = malloc(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 = malloc(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 = malloc(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 free(s->name);
280 free(s);
281 }
282
283 static inline size_t decl_struct_size(decl_struct *s) {
284 size_t c = s->args->count - 1;
285 decl_type *type = real_decl_type(s->args->args[c]->type);
286 return s->layout[c].pos + psi_t_alignment(type->type);
287 }
288
289 typedef struct decl_structs {
290 size_t count;
291 decl_struct **list;
292 } decl_structs;
293
294 static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
295 if (!ss) {
296 ss = calloc(1, sizeof(*ss));
297 }
298 ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
299 ss->list[ss->count-1] = s;
300 return ss;
301 }
302
303 static inline void free_decl_structs(decl_structs *ss) {
304 size_t i;
305
306 for (i = 0; i < ss->count; ++i) {
307 free_decl_struct(ss->list[i]);
308 }
309 free(ss->list);
310 free(ss);
311 }
312
313 typedef union impl_val {
314 unsigned char bval;
315 char cval;
316 short sval;
317 int ival;
318 float fval;
319 double dval;
320 zend_long lval;
321 zend_string *str;
322 void *ptr;
323 } impl_val;
324
325 static inline impl_val *deref_impl_val(unsigned level, impl_val *ret_val, decl_arg *darg) {
326 unsigned i;
327
328 for (i = level; i < darg->var->pointer_level && ret_val->ptr; ++i) {
329 ret_val = *(void **)ret_val;
330 }
331 return ret_val;
332 }
333
334 typedef struct impl_type {
335 char *name;
336 token_t type;
337 } impl_type;
338
339 static inline impl_type *init_impl_type(token_t type, char *name) {
340 impl_type *t = malloc(sizeof(*t));
341
342 t->type = type;
343 t->name = (char *) strdup((const char *) name);
344 return t;
345 }
346
347 static inline void free_impl_type(impl_type *type) {
348 free(type->name);
349 free(type);
350 }
351
352 typedef struct impl_var {
353 char *name;
354 unsigned reference:1;
355 } impl_var;
356
357 static inline impl_var *init_impl_var(char *name, int is_reference) {
358 impl_var *var = malloc(sizeof(*var));
359 var->name = (char *) strdup((const char *) name);
360 var->reference = is_reference;
361 return var;
362 }
363
364 static inline void free_impl_var(impl_var *var) {
365 free(var->name);
366 free(var);
367 }
368
369 typedef struct impl_def_val {
370 token_t type;
371 char *text;
372 } impl_def_val;
373
374 static inline impl_def_val *init_impl_def_val(PSI_Token *T) {
375 impl_def_val *def = malloc(sizeof(*def));
376 def->type = T->type;
377 def->text = strdup(T->text);
378 return def;
379 }
380
381 static inline void free_impl_def_val(impl_def_val *def) {
382 free(def->text);
383 free(def);
384 }
385
386 typedef struct impl_arg {
387 impl_type *type;
388 impl_var *var;
389 impl_def_val *def;
390 impl_val val;
391 zval *_zv;
392 } impl_arg;
393
394 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
395 impl_arg *arg = malloc(sizeof(*arg));
396 arg->type = type;
397 arg->var = var;
398 arg->def = def;
399 return arg;
400 }
401
402 static inline void free_impl_arg(impl_arg *arg) {
403 free_impl_type(arg->type);
404 free_impl_var(arg->var);
405 if (arg->def) {
406 free_impl_def_val(arg->def);
407 }
408 free(arg);
409 }
410
411 typedef struct impl_args {
412 impl_arg **args;
413 size_t count;
414 } impl_args;
415
416 static inline impl_args *init_impl_args(impl_arg *arg) {
417 impl_args *args = malloc(sizeof(*args));
418 args->args = malloc(sizeof(*args->args));
419 if (arg) {
420 args->count = 1;
421 args->args[0] = arg;
422 } else {
423 args->count = 0;
424 args->args = NULL;
425 }
426 return args;
427 }
428
429 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
430 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
431 args->args[args->count-1] = arg;
432 return args;
433 }
434
435 static inline void free_impl_args(impl_args *args) {
436 size_t i;
437
438 for (i = 0; i < args->count; ++i) {
439 free_impl_arg(args->args[i]);
440 }
441 free(args->args);
442 free(args);
443 }
444
445 typedef struct impl_func {
446 char *name;
447 impl_args *args;
448 impl_type *return_type;
449 unsigned return_reference:1;
450 } impl_func;
451
452 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
453 impl_func *func = malloc(sizeof(*func));
454 func->name = strdup(name);
455 func->args = args ? args : init_impl_args(NULL);
456 func->return_type = type;
457 func->return_reference = ret_reference;
458 return func;
459 }
460
461 static inline void free_impl_func(impl_func *f) {
462 free_impl_type(f->return_type);
463 free_impl_args(f->args);
464 free(f->name);
465 free(f);
466 }
467
468 typedef struct let_func {
469 token_t type;
470 char *name;
471 size_t size;
472 } let_func;
473
474 static inline let_func *init_let_func(token_t type, char *name, size_t size) {
475 let_func *func = malloc(sizeof(*func));
476 func->type = type;
477 func->name = strdup(name);
478 func->size = size;
479 return func;
480 }
481
482 static inline void free_let_func(let_func *func) {
483 free(func->name);
484 free(func);
485 }
486
487 typedef struct let_value {
488 let_func *func;
489 impl_var *var;
490 unsigned is_reference:1;
491 } let_value;
492
493 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
494 let_value *val = malloc(sizeof(*val));
495 val->is_reference = is_reference;
496 val->func = func;
497 val->var = var;
498 return val;
499 }
500
501 static inline void free_let_value(let_value *val) {
502 if (val->func) {
503 free_let_func(val->func);
504 }
505 if (val->var) {
506 free_impl_var(val->var);
507 }
508 free(val);
509 }
510
511 typedef struct let_stmt {
512 decl_var *var;
513 let_value *val;
514 impl_arg *arg;
515 impl_val out;
516 void *ptr;
517 void *mem;
518 } let_stmt;
519
520 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
521 let_stmt *let = calloc(1, sizeof(*let));
522 let->var = var;
523 let->val = val;
524 return let;
525 }
526
527 static inline void free_let_stmt(let_stmt *stmt) {
528 free_decl_var(stmt->var);
529 if (stmt->val) {
530 free_let_value(stmt->val);
531 }
532 free(stmt);
533 }
534
535 typedef struct set_func {
536 token_t type;
537 char *name;
538 } set_func;
539
540 static inline set_func *init_set_func(token_t type, char *name) {
541 set_func *func = malloc(sizeof(*func));
542 func->type = type;
543 func->name = (char *) strdup((const char *) name);
544 return func;
545 }
546
547 static inline void free_set_func(set_func *func) {
548 free(func->name);
549 free(func);
550 }
551
552 typedef struct set_value {
553 set_func *func;
554 decl_vars *vars;
555 } set_value;
556
557 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
558 set_value *val = malloc(sizeof(*val));
559 val->func = func;
560 val->vars = vars;
561 return val;
562 }
563
564 static inline void free_set_value(set_value *val) {
565 free_set_func(val->func);
566 free_decl_vars(val->vars);
567 free(val);
568 }
569
570 typedef struct set_stmt {
571 impl_var *var;
572 set_value *val;
573 impl_arg *arg;
574 } set_stmt;
575
576 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
577 set_stmt *set = malloc(sizeof(*set));
578 set->var = var;
579 set->val = val;
580 return set;
581 }
582
583 static inline void free_set_stmt(set_stmt *set) {
584 free_impl_var(set->var);
585 free_set_value(set->val);
586 free(set);
587 }
588
589 typedef struct return_stmt {
590 set_func *func;
591 decl_var *decl;
592 } return_stmt;
593
594 static inline return_stmt *init_return_stmt(set_func *func, decl_var *decl) {
595 return_stmt *ret = malloc(sizeof(*ret));
596 ret->func = func;
597 ret->decl = decl;
598 return ret;
599 }
600
601 static inline void free_return_stmt(return_stmt *ret) {
602 free_set_func(ret->func);
603 free_decl_var(ret->decl);
604 free(ret);
605 }
606
607 typedef struct free_stmt {
608 decl_vars *vars;
609 } free_stmt;
610
611 static inline free_stmt *init_free_stmt(decl_vars *vars) {
612 free_stmt *free_ = malloc(sizeof(*free_));
613 free_->vars = vars;
614 return free_;
615 }
616
617 static inline void free_free_stmt(free_stmt *free_) {
618 free_decl_vars(free_->vars);
619 free(free_);
620 }
621
622 typedef struct impl_stmt {
623 token_t type;
624 union {
625 let_stmt *let;
626 set_stmt *set;
627 return_stmt *ret;
628 free_stmt *fre;
629 void *ptr;
630 } s;
631 } impl_stmt;
632
633 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
634 impl_stmt *stmt = malloc(sizeof(*stmt));
635 stmt->type = type;
636 stmt->s.ptr = ptr;
637 return stmt;
638 }
639
640 static inline void free_impl_stmt(impl_stmt *stmt) {
641 switch (stmt->type) {
642 case PSI_T_LET:
643 free_let_stmt(stmt->s.let);
644 break;
645 case PSI_T_SET:
646 free_set_stmt(stmt->s.set);
647 break;
648 case PSI_T_RETURN:
649 free_return_stmt(stmt->s.ret);
650 break;
651 case PSI_T_FREE:
652 free_free_stmt(stmt->s.fre);
653 break;
654 }
655 free(stmt);
656 }
657
658 typedef struct impl_stmts {
659 struct {
660 return_stmt **list;
661 size_t count;
662 } ret;
663 struct {
664 let_stmt **list;
665 size_t count;
666 } let;
667 struct {
668 set_stmt **list;
669 size_t count;
670 } set;
671 struct {
672 free_stmt **list;
673 size_t count;
674 } fre;
675 } impl_stmts;
676
677 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
678 list = realloc(list, count * sizeof(list));
679 ((void **)list)[count-1] = stmt;
680 return list;
681 }
682
683 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
684 switch (stmt->type) {
685 case PSI_T_RETURN:
686 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
687 break;
688 case PSI_T_LET:
689 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
690 break;
691 case PSI_T_SET:
692 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
693 break;
694 case PSI_T_FREE:
695 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
696 break;
697 }
698 free(stmt);
699 return stmts;
700 }
701
702 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
703 impl_stmts *stmts = calloc(1, sizeof(*stmts));
704 return add_impl_stmt(stmts, stmt);
705 }
706
707 static inline void free_impl_stmts(impl_stmts *stmts) {
708 size_t i;
709
710 for (i = 0; i < stmts->let.count; ++i) {
711 free_let_stmt(stmts->let.list[i]);
712 }
713 free(stmts->let.list);
714 for (i = 0; i < stmts->ret.count; ++i) {
715 free_return_stmt(stmts->ret.list[i]);
716 }
717 free(stmts->ret.list);
718 for (i = 0; i < stmts->set.count; ++i) {
719 free_set_stmt(stmts->set.list[i]);
720 }
721 free(stmts->set.list);
722 for (i = 0; i < stmts->fre.count; ++i) {
723 free_free_stmt(stmts->fre.list[i]);
724 }
725 free(stmts->fre.list);
726 free(stmts);
727 }
728
729 typedef struct impl {
730 impl_func *func;
731 impl_stmts *stmts;
732 decl *decl;
733 } impl;
734
735 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
736 impl *i = malloc(sizeof(*i));
737 i->func = func;
738 i->stmts = stmts;
739 return i;
740 }
741
742 static inline void free_impl(impl *impl) {
743 free_impl_func(impl->func);
744 free_impl_stmts(impl->stmts);
745 free(impl);
746 }
747
748 typedef struct impls {
749 size_t count;
750 impl **list;
751 } impls;
752
753 static inline impls *add_impl(impls *impls, impl *impl) {
754 if (!impls) {
755 impls = calloc(1, sizeof(*impls));
756 }
757 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
758 impls->list[impls->count-1] = impl;
759 return impls;
760 }
761
762 static void free_impls(impls *impls) {
763 size_t i;
764
765 for (i = 0; i < impls->count; ++i) {
766 free_impl(impls->list[i]);
767 }
768 free(impls->list);
769 free(impls);
770 }
771
772 typedef struct const_type {
773 token_t type;
774 char *name;
775 } const_type;
776
777 static inline const_type *init_const_type(token_t type, const char *name) {
778 const_type *ct = malloc(sizeof(*ct));
779 ct->type = type;
780 ct->name = strdup(name);
781 return ct;
782 }
783
784 static inline void free_const_type(const_type *type) {
785 free(type->name);
786 free(type);
787 }
788
789 typedef struct constant {
790 const_type *type;
791 char *name;
792 impl_def_val *val;
793 } constant;
794
795 static inline constant *init_constant(const_type *type, char *name, impl_def_val *val) {
796 constant *c = malloc(sizeof(*c));
797 c->type = type;
798 c->name = strdup(name);
799 c->val = val;
800 return c;
801 }
802
803 static inline void free_constant(constant *constant) {
804 free_const_type(constant->type);
805 free(constant->name);
806 free_impl_def_val(constant->val);
807 free(constant);
808 }
809
810 typedef struct constants {
811 size_t count;
812 constant **list;
813 } constants;
814
815 static inline constants *add_constant(constants *constants, constant *constant) {
816 if (!constants) {
817 constants = calloc(1, sizeof(*constants));
818 }
819 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
820 constants->list[constants->count-1] = constant;
821 return constants;
822 }
823
824 static inline void free_constants(constants *c) {
825 size_t i;
826
827 for (i = 0; i < c->count; ++i) {
828 free_constant(c->list[i]);
829 }
830 free(c->list);
831 free(c);
832 }
833
834 #define PSI_ERROR 16
835 #define PSI_WARNING 32
836 typedef void (*psi_error_cb)(int type, const char *msg, ...);
837
838 #define PSI_DATA_MEMBERS \
839 constants *consts; \
840 decl_typedefs *defs; \
841 decl_structs *structs; \
842 decls *decls; \
843 impls *impls; \
844 char *lib; \
845 char *fn; \
846 psi_error_cb error
847 typedef struct PSI_Data {
848 PSI_DATA_MEMBERS;
849 } PSI_Data;
850
851 static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
852 memcpy(dest, src, sizeof(*dest));
853 memset(src, 0, sizeof(*src));
854 }
855
856 static inline void PSI_DataDtor(PSI_Data *data) {
857 if (data->consts) {
858 free_constants(data->consts);
859 }
860 if (data->defs) {
861 free_decl_typedefs(data->defs);
862 }
863 if (data->structs) {
864 free_decl_structs(data->structs);
865 }
866 if (data->decls) {
867 free_decls(data->decls);
868 }
869 if (data->impls) {
870 free_impls(data->impls);
871 }
872 if (data->lib) {
873 free(data->lib);
874 }
875 if (data->fn) {
876 free(data->fn);
877 }
878 }
879
880 typedef struct PSI_Parser {
881 PSI_DATA_MEMBERS;
882 FILE *fp;
883 unsigned flags;
884 unsigned errors;
885 void *proc;
886 size_t line;
887 token_t num;
888 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
889 } PSI_Parser;
890
891 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
892 PSI_Token *T;
893 size_t token_len;
894
895 if (P->cur <= P->tok) {
896 return NULL;
897 }
898
899 token_len = P->cur - P->tok;
900
901 T = malloc(sizeof(*T) + token_len);
902 T->type = P->num;
903 T->line = P->line;
904 T->size = token_len;
905 T->text[token_len] = 0;
906 memcpy(T->text, P->tok, token_len);
907
908 return T;
909 }
910
911 #define PSI_PARSER_DEBUG 0x1
912
913 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
914 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
915 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
916 token_t PSI_ParserScan(PSI_Parser *P);
917 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
918 void PSI_ParserDtor(PSI_Parser *P);
919 void PSI_ParserFree(PSI_Parser **P);
920
921 #endif