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