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