-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
executable file
·451 lines (347 loc) · 9.67 KB
/
parser.go
File metadata and controls
executable file
·451 lines (347 loc) · 9.67 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package regexl
import (
"fmt"
"slices"
"strconv"
"strings"
"unicode/utf8"
)
var (
keywords = []string{"select"}
)
type Parser struct {
Query string
}
func NewParser(query string) *Parser {
return &Parser{
Query: query,
}
}
var _ error = &ParserError{}
type ParserError struct {
Err error
Pos TokenPos
}
func (te *ParserError) Error() string {
if te == nil || te.Err == nil {
return ""
}
return fmt.Sprintf("parser error: loc=%d; err=%s", te.Pos, te.Err.Error())
}
func (p *Parser) Tokenize() (tokens []Token, err error) {
if p.Query == "" {
return []Token{}, nil
}
tokens = make([]Token, 0, 50)
getToken := func(index int) *Token {
if len(tokens) == 0 {
return nil
}
if index < 0 {
return &tokens[len(tokens)+index]
}
return &tokens[index]
}
/*
Trims surrounding space of the token's value, then if the token is not empty it appends a copy of it to the list of tokens, and then makes the passed token empty.
Returns the last appended token using getToken(-1). The just passed token is returned (its copy appended to the array) if the passed token wasn't empty, otherwise the last appended token is returned.
The return can be null if no tokens have been appended
*/
addToken := func(t *Token) (latestToken *Token) {
if t.Type != TokenType_String {
t.Val = strings.TrimSpace(t.Val)
}
if t.IsEmpty() {
return getToken(-1)
}
tokens = append(tokens, *t)
t.MakeEmpty()
return getToken(-1)
}
/*
Try to assign a type to a token that is probably some literal like a string or number, and is no-op if token is already typed.
Possible callers are:
* ',' or '}' of an object param
* ')' closing a function
*/
tryAssignTypeToPossibleLiteralToken := func(t *Token) {
if t.IsEmpty() || t.Type != TokenType_Unknown {
return
}
trimmedVal := strings.TrimSpace(t.Val)
if trimmedVal == "false" || trimmedVal == "true" {
t.Type = TokenType_Bool
} else if _, err := strconv.ParseInt(trimmedVal, 10, 64); err == nil {
t.Type = TokenType_Int
} else if _, err := strconv.ParseFloat(trimmedVal, 64); err == nil {
t.Type = TokenType_Float
}
}
inString := false
inComment := false
token := &Token{}
token.MakeEmpty()
for runeStartByteIndex, c := range p.Query {
if inComment {
if c != '\n' {
token.Val += string(c)
continue
}
// Remove the second '/' of the comment start
token.Val = token.Val[1:]
addToken(token)
inComment = false
}
if inString {
if c != '\'' && c != '\\' {
token.Val += string(c)
continue
}
if c == '\'' {
addToken(token)
// token.Val = "'"
// token.Type = TokenType_Single_Quote
// token.Loc = runeStartByteIndex
// addToken(token)
inString = false
continue
}
// Handle backslash in string as it might escape the end string character
nextRune, err := p.GetNextRuneByByteIndex(runeStartByteIndex)
if err != nil {
return tokens, &ParserError{
Err: err,
Pos: TokenPos(runeStartByteIndex),
}
}
if nextRune == '\'' {
token.Val += string(c)
inString = false
addToken(token)
continue
}
// Its just a normal backslash not escaping anything so we let it be
token.Val += string(c)
continue
}
switch c {
// General token 'end' signifiers
case '\n':
fallthrough
case '\t':
fallthrough
case ' ':
prevToken := addToken(token)
tryAssignTypeToPossibleLiteralToken(prevToken)
// Handle keywords and use is empty to protect against nil
if !prevToken.IsEmpty() && prevToken.Type == TokenType_Unknown {
if slices.Contains(keywords, prevToken.Val) {
prevToken.Type = TokenType_Keyword
}
}
case ':':
prevToken := addToken(token)
if !prevToken.IsEmpty() {
prevToken.Type = TokenType_Object_Param
}
token.Val = ":"
token.Type = TokenType_Colon
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case '+':
addToken(token)
token.Val = "+"
token.Type = TokenType_Plus
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case ',':
prevToken := addToken(token)
tryAssignTypeToPossibleLiteralToken(prevToken)
token.Val = ","
token.Type = TokenType_Comma
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case '(':
prevToken := addToken(token)
if !prevToken.IsEmpty() {
prevToken.Type = TokenType_Function_Name
}
token.Val = "("
token.Type = TokenType_OpenBracket
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case ')':
prevToken := addToken(token)
tryAssignTypeToPossibleLiteralToken(prevToken)
token.Val = ")"
token.Type = TokenType_CloseBracket
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case '{':
addToken(token)
token.Val = "{"
token.Type = TokenType_OpenCurlyBracket
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case '}':
prevToken := addToken(token)
tryAssignTypeToPossibleLiteralToken(prevToken)
token.Val = "}"
token.Type = TokenType_CloseCurlyBracket
token.Pos = TokenPos(runeStartByteIndex)
addToken(token)
case '\'':
addToken(token)
// token.Val = "'"
// token.Type = TokenType_Single_Quote
// token.Loc = runeStartByteIndex
// addToken(token)
inString = true
token.Type = TokenType_String
token.Pos = TokenPos(runeStartByteIndex)
case '/':
nextRune, err := p.GetNextRuneByByteIndex(runeStartByteIndex)
if err != nil {
return tokens, &ParserError{
Err: err,
Pos: TokenPos(runeStartByteIndex),
}
}
if nextRune != '/' {
return tokens, &ParserError{
Err: fmt.Errorf("found '/' in an unexpected location. '/' can only be used for comments or in strings"),
Pos: TokenPos(runeStartByteIndex),
}
}
addToken(token)
token.Type = TokenType_Comment
token.Pos = TokenPos(runeStartByteIndex)
inComment = true
default:
token.Val += string(c)
if !token.HasLoc() {
token.Pos = TokenPos(runeStartByteIndex)
}
}
}
err = p.ValidateTokens(tokens)
return tokens, err
}
func (p *Parser) ValidateTokens(tokens []Token) error {
type OpenBracketsList struct {
Bracket *Token
Next *OpenBracketsList
}
bracketsCounter := 0
openBracketsList := &OpenBracketsList{}
addOpenBracket := func(b *Token) {
if b.Type != TokenType_OpenBracket && b.Type != TokenType_OpenCurlyBracket {
panic("can't add anything other than open bracket or open curly bracket to open brackets list")
}
// If first is empty use it
if openBracketsList.Bracket == nil {
openBracketsList.Bracket = b
return
}
// Go to end of list and add the new item
var curr *OpenBracketsList = openBracketsList
for curr.Next != nil {
curr = curr.Next
}
curr.Next = &OpenBracketsList{
Bracket: b,
Next: nil,
}
}
removeLastBracket := func() {
var curr *OpenBracketsList = openBracketsList
// Handle one item remaining
if curr.Next == nil {
curr.Bracket = nil
}
// Remove from end of list
for curr.Next != nil && curr.Next.Next != nil {
curr = curr.Next
}
curr.Next = nil
}
selectCounter := 0
for i := 0; i < len(tokens); i++ {
t := &tokens[i]
switch t.Type {
case TokenType_Keyword:
if t.Val == "select" {
selectCounter++
if selectCounter > 1 {
return &ParserError{
Err: fmt.Errorf("invalid regexl query: found multiple 'select' keywords while only one is allowed; token=%+v; query=%s", t, p.Query),
Pos: t.Pos,
}
}
}
case TokenType_Unknown:
return &ParserError{
Err: fmt.Errorf("invalid regexl query: found token with type=unknown after tokenization; token=%+v; query=%s", t, p.Query),
Pos: t.Pos,
}
case TokenType_OpenBracket:
fallthrough
case TokenType_OpenCurlyBracket:
bracketsCounter++
addOpenBracket(t)
case TokenType_CloseBracket:
fallthrough
case TokenType_CloseCurlyBracket:
bracketsCounter--
removeLastBracket()
if bracketsCounter >= 0 {
continue
}
if t.Type == TokenType_CloseCurlyBracket {
return &ParserError{
Err: fmt.Errorf("invalid regexl query: found a closed curly bracket without an opening curly bracket; token=%+v; query=%s", t, p.Query),
Pos: t.Pos,
}
}
return &ParserError{
Err: fmt.Errorf("invalid regexl query: found a closed bracket without an opening bracket; token=%+v; query=%s", t, p.Query),
Pos: t.Pos,
}
}
}
// Negative case is handled inside the switch case, so this is for brackets that opened but didn't close
// We only show the first unclosed bracket, but we can use the tree to show all of them
if bracketsCounter != 0 {
return &ParserError{
Err: fmt.Errorf("invalid regexl query: found an opening bracket without a closing bracket pair; first unclosed bracket token=%+v; query=%s", openBracketsList.Bracket, p.Query),
Pos: openBracketsList.Bracket.Pos,
}
}
if selectCounter == 0 {
return &ParserError{
Err: fmt.Errorf("invalid regexl query: 'select' keyword is required but wasn't found; query=%s", p.Query),
}
}
return nil
}
func (p *Parser) GetRuneByByteIndex(index int) (rune, error) {
r, _ := utf8.DecodeRuneInString(p.Query[index:])
if r == utf8.RuneError {
return 0, fmt.Errorf("decoding utf8 query failed. index=%d", index)
}
return r, nil
}
func (p *Parser) GetNextRuneByByteIndex(index int) (rune, error) {
if index >= len(p.Query) {
return 0, fmt.Errorf("getting next rune failed because index is out of range. index=%d; queryLen=%d", index, len(p.Query))
}
r, rLen := utf8.DecodeRuneInString(p.Query[index:])
if r == utf8.RuneError {
return 0, fmt.Errorf("decoding utf8 query failed. index=%d", index)
}
r, _ = utf8.DecodeRuneInString(p.Query[index+rLen:])
if r == utf8.RuneError {
return 0, fmt.Errorf("decoding utf8 query failed. index=%d", index+rLen)
}
return r, nil
}