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