This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
/
parse.go
145 lines (130 loc) · 3.18 KB
/
parse.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
package handler
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/crufter/lexer"
)
var quoteEscape = fmt.Sprint(0x10FFFF)
var singleQuoteEscape = fmt.Sprint(0x10FFFE)
var backtickEscape = fmt.Sprint(0x10FFFD)
const (
itemIgnore = iota
// nouns
itemAnd
itemInt
itemFieldName
itemString
itemBoolTrue
itemBoolFalse
// ops
itemEquals
itemNotEquals
itemLessThan
itemGreaterThan
itemLessThanEquals
itemGreaterThanEquals
)
var opToString = map[int]string{
itemEquals: "==",
itemNotEquals: "!=",
itemLessThan: "<",
itemGreaterThan: ">",
itemLessThanEquals: "<=",
itemGreaterThanEquals: ">=",
}
var expressions = []lexer.TokenExpr{
{`[ ]+`, itemIgnore}, // Whitespace
{`==`, itemEquals},
{`!=`, itemNotEquals},
{`false`, itemBoolFalse},
{`true`, itemBoolTrue},
{`and`, itemAnd},
{`<=`, itemLessThanEquals},
{`>=`, itemGreaterThanEquals},
{`<`, itemLessThan},
{`>`, itemGreaterThan},
{`[0-9]+`, itemInt},
{`"(?:[^"\\]|\\.)*"`, itemString},
{"`" + `(?:[^"\\]|\\.)*` + "`", itemString},
{`'(?:[^"\\]|\\.)*'`, itemString},
{`[\<\>\!\=\+\-\|\&\*\/A-Za-z][A-Za-z0-9_\.]*`, itemFieldName},
}
type Query struct {
Field string
Op int
Value interface{}
}
func Parse(q string) ([]Query, error) {
if strings.Contains(q, quoteEscape) {
return nil, errors.New("query contains illegal max rune")
}
q = strings.Replace(q, `""`, quoteEscape, -1)
q = strings.Replace(q, "``", singleQuoteEscape, -1)
q = strings.Replace(q, "''", backtickEscape, -1)
tokens, err := lexer.Lex(q, expressions)
if err != nil {
return nil, err
}
queries := []Query{}
current := Query{}
for i, token := range tokens {
// and tokens should trigger a query
// save and reset
if token.Typ == itemAnd {
queries = append(queries, current)
current = Query{}
continue
}
// is an op
if token.Typ >= itemEquals {
current.Op = token.Typ
continue
}
// is a value
switch token.Typ {
case itemFieldName:
current.Field = token.Text
case itemString:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with strings", opToString[current.Op])
}
if len(token.Text) < 2 {
return nil, fmt.Errorf("string literal too short: '%v'", token.Text)
}
to := token.Text[1 : len(token.Text)-1]
to = strings.Replace(to, quoteEscape, `"`, -1)
to = strings.Replace(to, singleQuoteEscape, `'`, -1)
to = strings.Replace(to, backtickEscape, "`", -1)
current.Value = to
case itemBoolTrue:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[current.Op])
}
current.Value = true
case itemBoolFalse:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[current.Op])
}
current.Value = false
case itemInt:
num, err := strconv.ParseInt(token.Text, 10, 64)
if err != nil {
return nil, err
}
current.Value = num
}
// if we are at last position, save last query
if i == len(tokens)-1 {
queries = append(queries, current)
}
}
return queries, nil
}