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