robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
2.4 KiB92 linesraw
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 {
13  TChar = 0,  /* 'n' = char */
14  TSet,  /* the set is encoded in 'u.set' and the next 'u.set.size' bytes */
15  TAny,
16  TTrue,
17  TFalse,
18  TUTFR,  /* range of UTF-8 codepoints; 'n' has initial codepoint;
19             'cap' has length; 'key' has first byte;
20             extra info is similar for end codepoint */
21  TRep,  /* 'sib1'* */
22  TSeq,  /* 'sib1' 'sib2' */
23  TChoice,  /* 'sib1' / 'sib2' */
24  TNot,  /* !'sib1' */
25  TAnd,  /* &'sib1' */
26  TCall,  /* ktable[key] is rule's key; 'sib2' is rule being called */
27  TOpenCall,  /* ktable[key] is rule's key */
28  TRule,  /* ktable[key] is rule's key (but key == 0 for unused rules);
29             'sib1' is rule's pattern pre-rule; 'sib2' is next rule;
30             extra info 'n' is rule's sequential number */
31  TXInfo,  /* extra info */
32  TGrammar,  /* 'sib1' is initial (and first) rule */
33  TBehind,  /* 'sib1' is pattern, 'n' is how much to go back */
34  TCapture,  /* captures: 'cap' is kind of capture (enum 'CapKind');
35                ktable[key] is Lua value associated with capture;
36                'sib1' is capture body */
37  TRunTime  /* 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 {
49  byte tag;
50  byte cap;  /* kind of capture (if it is a capture) */
51  unsigned short key;  /* key in ktable for Lua data (0 if no key) */
52  union {
53    int ps;  /* occasional second child */
54    int n;  /* occasional counter */
55    struct {
56      byte offset;  /* compact set offset (in bytes) */
57      byte size;  /* compact set size (in bytes) */
58      byte deflt;  /* default value */
59      byte 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 {
74  union Instruction *code;
75  TTree 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