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