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