This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
expression.go
307 lines (279 loc) · 6.76 KB
/
expression.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
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
package query
import (
"fmt"
"math/big"
"strings"
"time"
"github.com/hyperledger/burrow/logging/errors"
)
const (
// DateLayout defines a layout for all dates (`DATE date`)
DateLayout = "2006-01-02"
// TimeLayout defines a layout for all times (`TIME time`)
TimeLayout = time.RFC3339
)
// Operator is an operator that defines some kind of relation between tag and
// operand (equality, etc.).
type Operator uint8
const (
OpTerminal Operator = iota
OpAnd
OpOr
OpLessEqual
OpGreaterEqual
OpLess
OpGreater
OpEqual
OpContains
)
var opNames = map[Operator]string{
OpAnd: "AND",
OpOr: "OR",
OpLessEqual: "<=",
OpGreaterEqual: ">=",
OpLess: "<",
OpGreater: ">",
OpEqual: "=",
OpContains: "CONTAINS",
}
func (op Operator) String() string {
return opNames[op]
}
// Instruction is a container suitable for the code tape and the stack to hold values an operations
type instruction struct {
op Operator
tag *string
string *string
time *time.Time
number *big.Float
match bool
}
func (in *instruction) String() string {
switch {
case in.op != OpTerminal:
return in.op.String()
case in.tag != nil:
return *in.tag
case in.string != nil:
return "'" + *in.string + "'"
case in.time != nil:
return in.time.String()
case in.number != nil:
return in.number.String()
default:
if in.match {
return "true"
}
return "false"
}
}
// A Boolean expression for the query grammar
type Expression struct {
// This is our 'bytecode'
code []*instruction
errors errors.MultipleErrors
explainer func(format string, args ...interface{})
}
// Evaluate expects an Execute() to have filled the code of the Expression so it can be run in the little stack machine
// below
func (e *Expression) Evaluate(getTagValue func(tag string) (interface{}, bool)) (bool, error) {
if len(e.errors) > 0 {
return false, e.errors
}
var left, right *instruction
stack := make([]*instruction, 0, len(e.code))
for _, in := range e.code {
if in.op == OpTerminal {
// just push terminals on to the stack
stack = append(stack, in)
continue
}
if len(stack) < 2 {
return false, fmt.Errorf("cannot pop from stack for query expression [%v] because stack has "+
"fewer than 2 elements", e)
}
stack, left, right = pop(stack)
ins := &instruction{}
switch in.op {
case OpAnd:
ins.match = left.match && right.match
case OpOr:
ins.match = left.match || right.match
default:
// We have a a non-terminal, non-connective operation
tagValue, ok := getTagValue(*left.tag)
// No match if we can't get tag value
if ok {
switch {
case right.string != nil:
ins.match = compareString(in.op, tagValue, *right.string)
case right.number != nil:
ins.match = compareNumber(in.op, tagValue, right.number)
case right.time != nil:
ins.match = compareTime(in.op, tagValue, *right.time)
}
}
// Uncomment this for a little bit of debug:
//e.explainf("%v := %v\n", left, tagValue)
}
// Uncomment this for a little bit of debug:
//e.explainf("%v %v %v => %v\n", left, in.op, right, ins.match)
// Push whether this was a match back on to stack
stack = append(stack, ins)
}
if len(stack) != 1 {
return false, fmt.Errorf("stack for query expression [%v] should have exactly one element after "+
"evaulation but has %d", e, len(stack))
}
return stack[0].match, nil
}
func (e *Expression) explainf(fmt string, args ...interface{}) {
if e.explainer != nil {
e.explainer(fmt, args...)
}
}
func pop(stack []*instruction) ([]*instruction, *instruction, *instruction) {
return stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
}
func compareString(op Operator, tagValue interface{}, value string) bool {
tagString := StringFromValue(tagValue)
switch op {
case OpContains:
return strings.Contains(tagString, value)
case OpEqual:
return tagString == value
}
return false
}
func compareNumber(op Operator, tagValue interface{}, value *big.Float) bool {
tagNumber := new(big.Float)
switch n := tagValue.(type) {
case string:
f, _, err := big.ParseFloat(n, 10, 64, big.ToNearestEven)
if err != nil {
return false
}
tagNumber.Set(f)
case *big.Float:
tagNumber.Set(n)
case *big.Int:
tagNumber.SetInt(n)
case float32:
tagNumber.SetFloat64(float64(n))
case float64:
tagNumber.SetFloat64(n)
case int:
tagNumber.SetInt64(int64(n))
case int32:
tagNumber.SetInt64(int64(n))
case int64:
tagNumber.SetInt64(n)
case uint:
tagNumber.SetUint64(uint64(n))
case uint32:
tagNumber.SetUint64(uint64(n))
case uint64:
tagNumber.SetUint64(n)
default:
return false
}
cmp := tagNumber.Cmp(value)
switch op {
case OpLessEqual:
return cmp < 1
case OpGreaterEqual:
return cmp > -1
case OpLess:
return cmp == -1
case OpGreater:
return cmp == 1
case OpEqual:
return cmp == 0
}
return false
}
func compareTime(op Operator, tagValue interface{}, value time.Time) bool {
var tagTime time.Time
var err error
switch t := tagValue.(type) {
case time.Time:
tagTime = t
case int64:
// Hmmm, should we?
tagTime = time.Unix(t, 0)
case string:
tagTime, err = time.Parse(TimeLayout, t)
if err != nil {
tagTime, err = time.Parse(DateLayout, t)
if err != nil {
return false
}
}
default:
return false
}
switch op {
case OpLessEqual:
return tagTime.Before(value) || tagTime.Equal(value)
case OpGreaterEqual:
return tagTime.Equal(value) || tagTime.After(value)
case OpLess:
return tagTime.Before(value)
case OpGreater:
return tagTime.After(value)
case OpEqual:
return tagTime.Equal(value)
}
return false
}
// These methods implement the various visitors that are called in the PEG grammar with statements like
// { p.Operator(OpEqual) }
func (e *Expression) String() string {
strs := make([]string, len(e.code))
for i, in := range e.code {
strs[i] = in.String()
}
return strings.Join(strs, ", ")
}
func (e *Expression) Operator(operator Operator) {
e.code = append(e.code, &instruction{
op: operator,
})
}
// Terminals...
func (e *Expression) Tag(value string) {
e.code = append(e.code, &instruction{
tag: &value,
})
}
func (e *Expression) Time(value string) {
t, err := time.Parse(TimeLayout, value)
e.pushErr(err)
e.code = append(e.code, &instruction{
time: &t,
})
}
func (e *Expression) Date(value string) {
date, err := time.Parse(DateLayout, value)
e.pushErr(err)
e.code = append(e.code, &instruction{
time: &date,
})
}
func (e *Expression) Number(value string) {
number, _, err := big.ParseFloat(value, 10, 64, big.ToNearestEven)
e.pushErr(err)
e.code = append(e.code, &instruction{
number: number,
})
}
func (e *Expression) Value(value string) {
e.code = append(e.code, &instruction{
string: &value,
})
}
func (e *Expression) pushErr(err error) {
if err != nil {
e.errors = append(e.errors, err)
}
}