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