ast.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef __AST_H_
  17. #define __AST_H_
  18. #include <stddef.h>
  19. struct jp_opcode {
  20. int type;
  21. struct jp_opcode *next;
  22. struct jp_opcode *down;
  23. struct jp_opcode *sibling;
  24. char *str;
  25. int num;
  26. };
  27. struct jp_state {
  28. struct jp_opcode *pool;
  29. struct jp_opcode *path;
  30. int error_pos;
  31. int error_code;
  32. int off;
  33. };
  34. static inline struct jp_opcode *
  35. append_op(struct jp_opcode *a, struct jp_opcode *b)
  36. {
  37. struct jp_opcode *tail = a;
  38. while (tail->sibling)
  39. tail = tail->sibling;
  40. tail->sibling = b;
  41. return a;
  42. }
  43. struct jp_opcode *jp_alloc_op(struct jp_state *s, int type, int num, char *str, ...);
  44. struct jp_state *jp_parse(const char *expr);
  45. void jp_free(struct jp_state *s);
  46. void *ParseAlloc(void *(*mfunc)(size_t));
  47. void Parse(void *pParser, int type, struct jp_opcode *op, struct jp_state *s);
  48. void ParseFree(void *pParser, void (*ffunc)(void *));
  49. #endif /* __AST_H_ */