-
Notifications
You must be signed in to change notification settings - Fork 17
/
token.go
254 lines (232 loc) · 4.88 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package sigma
import (
"context"
"github.com/gobwas/glob"
)
var eof = rune(0)
// Item is lexical token along with respective plaintext value
// Item is communicated between lexer and parser
type Item struct {
T Token
Val string
globVal *glob.Glob // Do NOT access directly, us the Item.Glob() function instead
globCompFail bool // prevents us from trying to re-compile a failed globVal over and over...
}
func (i Item) String() string { return i.Val }
// Item.Glob() - Wraps getting the compiled glob of Item.Val to ensure it is compiled properly.
// Do NOT access globVal directly as it won't be compiled until the first call to Item.Glob()
func (i *Item) Glob() *glob.Glob {
if i.globVal == nil && !i.globCompFail {
newVal := escapeSigmaForGlob(i.Val)
newGlob, err := glob.Compile(newVal)
if err != nil {
i.globCompFail = true
return nil
}
i.globVal = &newGlob
}
return i.globVal
}
func genItems(t []Item) <-chan Item {
tx := make(chan Item) // unbuffered
go func(ctx context.Context) {
defer close(tx)
for _, item := range t {
tx <- item
}
}(context.TODO())
return tx
}
// Token is a lexical token extracted from condition field
type Token int
const (
TokBegin Token = iota
// Helpers for internal stuff
TokErr
TokUnsupp
TokNil
// user-defined word
TokIdentifier
TokIdentifierWithWildcard
TokIdentifierAll
// Literals
TokLitEof
// Separators
TokSepLpar
TokSepRpar
TokSepPipe
// Operators
TokOpEq
TokOpGt
TokOpGte
TokOpLt
TokOpLte
// Keywords
TokKeywordAnd
TokKeywordOr
TokKeywordNot
TokKeywordAgg
// TODO
TokKeywordNear
TokKeywordBy
// Statements
TokStOne
TokStAll
)
// String documents human readable textual value of token
// For visual debugging, so symbols will be written out and everything is uppercased
func (t Token) String() string {
switch t {
case TokIdentifier:
return "IDENT"
case TokIdentifierWithWildcard:
return "WILDCARDIDENT"
case TokIdentifierAll:
return "THEM"
case TokSepLpar:
return "LPAR"
case TokSepRpar:
return "RPAR"
case TokSepPipe:
return "PIPE"
case TokOpEq:
return "EQ"
case TokOpGt:
return "GT"
case TokOpGte:
return "GTE"
case TokOpLt:
return "LT"
case TokOpLte:
return "LTE"
case TokKeywordAnd:
return "AND"
case TokKeywordOr:
return "OR"
case TokKeywordNot:
return "NOT"
case TokStAll:
return "ALL"
case TokStOne:
return "ONE"
case TokKeywordAgg:
return "AGG"
case TokLitEof:
return "EOF"
case TokErr:
return "ERR"
case TokUnsupp:
return "UNSUPPORTED"
case TokBegin:
return "BEGINNING"
case TokNil:
return "NIL"
default:
return "Unk"
}
}
// Literal documents plaintext values of a token
// Uses special symbols and expressions, as used in a rule
func (t Token) Literal() string {
switch t {
case TokIdentifier, TokIdentifierWithWildcard:
return "keywords"
case TokIdentifierAll:
return "them"
case TokSepLpar:
return "("
case TokSepRpar:
return ")"
case TokSepPipe:
return "|"
case TokOpEq:
return "="
case TokOpGt:
return ">"
case TokOpGte:
return ">="
case TokOpLt:
return "<"
case TokOpLte:
return "<="
case TokKeywordAnd:
return "and"
case TokKeywordOr:
return "or"
case TokKeywordNot:
return "not"
case TokStAll:
return "all of"
case TokStOne:
return "1 of"
case TokLitEof, TokNil:
return ""
default:
return "Err"
}
}
// Rune returns UTF-8 numeric value of symbol
func (t Token) Rune() rune {
switch t {
case TokSepLpar:
return '('
case TokSepRpar:
return ')'
case TokSepPipe:
return '|'
default:
return eof
}
}
// validTokenSequence detects invalid token sequences
// not meant to be a perfect validator, simply a quick check before parsing
func validTokenSequence(t1, t2 Token) bool {
switch t2 {
case TokStAll, TokStOne:
switch t1 {
case TokBegin, TokSepLpar, TokKeywordAnd, TokKeywordOr, TokKeywordNot:
return true
}
case TokIdentifierAll:
switch t1 {
case TokStAll, TokStOne:
return true
}
case TokIdentifier, TokIdentifierWithWildcard:
switch t1 {
case TokSepLpar, TokBegin, TokKeywordAnd, TokKeywordOr, TokKeywordNot, TokStOne, TokStAll:
return true
}
case TokKeywordAnd, TokKeywordOr:
switch t1 {
case TokIdentifier, TokIdentifierAll, TokIdentifierWithWildcard, TokSepRpar:
return true
}
case TokKeywordNot:
switch t1 {
case TokKeywordAnd, TokKeywordOr, TokSepLpar, TokBegin:
return true
}
case TokSepLpar:
switch t1 {
case TokKeywordAnd, TokKeywordOr, TokKeywordNot, TokBegin, TokSepLpar:
return true
}
case TokSepRpar:
switch t1 {
case TokIdentifier, TokIdentifierAll, TokIdentifierWithWildcard, TokSepLpar, TokSepRpar:
return true
}
case TokLitEof:
switch t1 {
case TokIdentifier, TokIdentifierAll, TokIdentifierWithWildcard, TokSepRpar:
return true
}
case TokSepPipe:
switch t1 {
case TokIdentifier, TokIdentifierAll, TokIdentifierWithWildcard, TokSepRpar:
return true
}
}
return false
}