lptree.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #if !defined(lptree_h)
  2. #define lptree_h
  3. #include "lptypes.h"
  4. /*
  5. ** types of trees
  6. */
  7. typedef enum TTag {
  8. TChar = 0, /* 'n' = char */
  9. TSet, /* the set is encoded in 'u.set' and the next 'u.set.size' bytes */
  10. TAny,
  11. TTrue,
  12. TFalse,
  13. TUTFR, /* range of UTF-8 codepoints; 'n' has initial codepoint;
  14. 'cap' has length; 'key' has first byte;
  15. extra info is similar for end codepoint */
  16. TRep, /* 'sib1'* */
  17. TSeq, /* 'sib1' 'sib2' */
  18. TChoice, /* 'sib1' / 'sib2' */
  19. TNot, /* !'sib1' */
  20. TAnd, /* &'sib1' */
  21. TCall, /* ktable[key] is rule's key; 'sib2' is rule being called */
  22. TOpenCall, /* ktable[key] is rule's key */
  23. TRule, /* ktable[key] is rule's key (but key == 0 for unused rules);
  24. 'sib1' is rule's pattern pre-rule; 'sib2' is next rule;
  25. extra info 'n' is rule's sequential number */
  26. TXInfo, /* extra info */
  27. TGrammar, /* 'sib1' is initial (and first) rule */
  28. TBehind, /* 'sib1' is pattern, 'n' is how much to go back */
  29. TCapture, /* captures: 'cap' is kind of capture (enum 'CapKind');
  30. ktable[key] is Lua value associated with capture;
  31. 'sib1' is capture body */
  32. TRunTime /* run-time capture: 'key' is Lua function;
  33. 'sib1' is capture body */
  34. } TTag;
  35. /*
  36. ** Tree trees
  37. ** The first child of a tree (if there is one) is immediately after
  38. ** the tree. A reference to a second child (ps) is its position
  39. ** relative to the position of the tree itself.
  40. */
  41. typedef struct TTree {
  42. byte tag;
  43. byte cap; /* kind of capture (if it is a capture) */
  44. unsigned short key; /* key in ktable for Lua data (0 if no key) */
  45. union {
  46. int ps; /* occasional second child */
  47. int n; /* occasional counter */
  48. struct {
  49. byte offset; /* compact set offset (in bytes) */
  50. byte size; /* compact set size (in bytes) */
  51. byte deflt; /* default value */
  52. byte bitmap[1]; /* bitmap (open array) */
  53. } set; /* for compact sets */
  54. } u;
  55. } TTree;
  56. /* access to charset */
  57. #define treebuffer(t) ((t)->u.set.bitmap)
  58. /*
  59. ** A complete pattern has its tree plus, if already compiled,
  60. ** its corresponding code
  61. */
  62. typedef struct Pattern {
  63. union Instruction *code;
  64. TTree tree[1];
  65. } Pattern;
  66. /* number of children for each tree */
  67. extern const byte numsiblings[];
  68. /* access to children */
  69. #define sib1(t) ((t) + 1)
  70. #define sib2(t) ((t) + (t)->u.ps)
  71. #endif