-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
262 lines (252 loc) · 6.41 KB
/
parser.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
255
256
257
258
259
260
261
262
package filterql
import "fmt"
type ParseError struct {
Err error
Pos int
}
func (e *ParseError) Error() string {
return fmt.Sprintf("%d: %+v", e.Pos, e.Err)
}
func parseError(err error, pos int) *ParseError {
return &ParseError{Err: err, Pos: pos}
}
func Parse(code string, cfg *ParseConfig) (BoolAst, error) {
if cfg == nil {
cfg = &defaultConfig
}
if cacher := cfg.Cache; cacher != nil {
if cond, found := cacher.Load(code); found {
return cond, nil
}
}
ts := NewTokenStream(code)
ts.Next()
if cond, err := parseCondition(ts, cfg); err != nil {
return nil, err
} else if ts.Current.Type != TOKEN_EOF {
return nil, ErrUnexpectedToken
} else {
if cacher := cfg.Cache; cacher != nil {
cacher.Store(code, cond)
}
return cond, nil
}
}
func parseCondition(ts *TokenStream, cfg *ParseConfig) (BoolAst, error) {
children := make([]BoolAst, 1)
item, err := parseItem(ts, cfg)
if err != nil {
return nil, err
}
children[0] = item
for ts.Current.Type == TOKEN_OR {
if !ts.Next() {
return nil, parseError(ErrUnexpectedEnd, ts.index)
}
item, err := parseItem(ts, cfg)
if err != nil {
return nil, err
}
switch it := item.(type) {
case *ORs:
for _, child := range it.Children {
children = append(children, child)
}
default:
children = append(children, item)
}
}
if len(children) > 1 {
return &ORs{Children: children}, nil
} else {
return children[0], nil
}
}
func parseItem(ts *TokenStream, cfg *ParseConfig) (BoolAst, error) {
var children []BoolAst
atom, err := parseAtom(ts, cfg)
if err != nil {
return nil, err
}
children = []BoolAst{atom}
for ts.Current.Type == TOKEN_AND {
if !ts.Next() {
return nil, parseError(ErrUnexpectedEnd, ts.index)
}
atom, err := parseAtom(ts, cfg)
if err != nil {
return nil, err
}
switch it := atom.(type) {
case *ANDs:
for _, child := range it.Children {
children = append(children, child)
}
default:
children = append(children, atom)
}
}
if len(children) > 1 {
return &ANDs{Children: children}, nil
} else {
return children[0], nil
}
}
func parseAtom(ts *TokenStream, cfg *ParseConfig) (BoolAst, error) {
if ts.Current.Type == TOKEN_LEFT_BRACKET {
if !ts.Next() {
return nil, parseError(ErrUnexpectedEnd, ts.index)
}
if cond, err := parseCondition(ts, cfg); err != nil {
return nil, err
} else if ts.Current.Type != TOKEN_RIGHT_BRACKET {
return nil, parseError(ErrUnexpectedToken, ts.index)
} else {
ts.Next()
return cond, nil
}
} else if ts.Current.Type == TOKEN_NOT {
if !ts.Next() {
return nil, parseError(ErrUnexpectedEnd, ts.index)
}
atom, err := parseAtom(ts, cfg)
if err != nil {
return nil, err
} else if n, is := atom.(CanNot); is {
return n.Not(), nil
} else {
return &NOT{Child: atom}, nil
}
}
call, err := parseCall(ts, cfg)
if err != nil {
return nil, err
}
op := ts.Current.Type
not := false
switch op {
case TOKEN_OP_EQ, TOKEN_OP_NE, TOKEN_OP_GT, TOKEN_OP_GE, TOKEN_OP_LT, TOKEN_OP_LE:
if typ, err := nextMustBe(ts, TOKEN_STR, TOKEN_INT, TOKEN_ID); err != nil {
return nil, err
} else {
if typ == TOKEN_INT {
defer ts.Next()
return newCallThenCompare(call, op, tokenToInt(ts.Current.Text)), nil
} else if typ == TOKEN_STR {
defer ts.Next()
return newCallThenCompare(call, op, tokenToStr(ts.Current.Text)), nil
} else if call2, err := parseCall(ts, cfg); err != nil {
return nil, err
} else {
return &CompareWithCall{Left: call, Op: op, Right: call2}, nil
}
}
return nil, parseError(ErrUnexpectedToken, ts.index)
case TOKEN_NOT:
if _, err := nextMustBe(ts, TOKEN_OP_IN); err != nil {
return nil, err
}
not = true
fallthrough
case TOKEN_OP_IN:
if typ, err := nextMustBe(ts, TOKEN_LEFT_BRACKET, TOKEN_ID); err != nil {
return nil, err
} else if typ == TOKEN_ID {
if call2, err := parseCall(ts, cfg); err != nil {
return nil, err
} else {
return &InWithCall{Left: call, Right: call2, NotIn: not}, nil
}
}
choiceType, err := nextMustBe(ts, TOKEN_INT, TOKEN_STR)
if err != nil {
return nil, err
}
choices := [][]rune{ts.Current.Text}
for {
spType, err := nextMustBe(ts, TOKEN_COMMA, TOKEN_RIGHT_BRACKET)
if err != nil {
return nil, err
}
if spType == TOKEN_RIGHT_BRACKET {
break
}
if _, err := nextMustBe(ts, choiceType); err != nil {
return nil, err
}
choices = append(choices, ts.Current.Text)
}
ts.Next()
switch choiceType {
case TOKEN_INT:
if len(choices) == 1 {
if not {
return newCallThenCompare(call, TOKEN_OP_NE, tokenToInt(choices[0])), nil
} else {
return newCallThenCompare(call, TOKEN_OP_EQ, tokenToInt(choices[0])), nil
}
}
in := &In[int]{Call: call, Choices: make([]int, len(choices))}
for i, choice := range choices {
in.Choices[i] = tokenToInt(choice)
}
return newCallThenIn(in.Call, in.Choices, not), nil
case TOKEN_STR:
if len(choices) == 1 {
if not {
return newCallThenCompare(call, TOKEN_OP_NE, tokenToStr(choices[0])), nil
} else {
return newCallThenCompare(call, TOKEN_OP_EQ, tokenToStr(choices[0])), nil
}
}
in := &In[string]{Call: call, Choices: make([]string, len(choices))}
for i, choice := range choices {
in.Choices[i] = tokenToStr(choice)
}
return newCallThenIn(in.Call, in.Choices, not), nil
default:
panic("invalid choice type")
}
default:
return call, nil
}
}
func parseCall(ts *TokenStream, cfg *ParseConfig) (Call, error) {
if ts.Current.Type != TOKEN_ID {
return nil, parseError(ErrUnexpectedToken, ts.index)
}
name := string(ts.Current.Text)
if _, err := nextMustBe(ts, TOKEN_LEFT_BRACKET); err != nil {
return nil, err
}
var call Call
if typ, err := nextMustBe(ts, TOKEN_STR, TOKEN_INT); err != nil {
return nil, err
} else {
switch typ {
case TOKEN_INT:
call, err = newCall(cfg.IntMethods, cfg.DefaultIntMethod, name, tokenToInt(ts.Current.Text))
case TOKEN_STR:
call, err = newCall(cfg.StrMethods, cfg.DefaultStrMethod, name, tokenToStr(ts.Current.Text))
}
if err != nil {
return nil, err
}
}
if _, err := nextMustBe(ts, TOKEN_RIGHT_BRACKET); err != nil {
return nil, err
}
ts.Next()
return call, nil
}
func nextMustBe(ts *TokenStream, types ...int) (int, error) {
if !ts.Next() {
return TOKEN_NONE, parseError(ErrUnexpectedEnd, ts.index)
}
for _, t := range types {
if ts.Current.Type == t {
return t, nil
}
}
return TOKEN_NONE, parseError(ErrUnexpectedToken, ts.index)
}