-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy paths-expression.go
272 lines (249 loc) · 6.57 KB
/
s-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
// Package evaluator treats the element within expression with three types, each type has its own array form:
//
// - number
// For convenience, we treat float64, int64 and so on as type of number. For example, float 100.0 is equal to int 100, but not euqal to string "100"
// - string
// character string quoted with `, ', or " are treated as type of string. You can convert type string to any other defined type you like by type convert functions which are mentioned later
// - function or variable
// character string without quotes are regarded as type of function or variable which depends on whether this function exists. For example in expression (age birthdate), both age and birthdate is unquoted. age is type of function because we have registered a function named age, while birthdate is type of variable for not found. The program will come to errors if there is neither parameter nor function named birthdate when evaluating
package evaluator
import (
queue "container/list"
"errors"
"fmt"
"reflect"
"strconv"
"unicode"
"unicode/utf8"
"github.com/nullne/evaluator/function"
)
var (
// ErrUnexpectedEnd occurs most time with the missing parenthesis
ErrUnexpectedEnd = errors.New("unexpected end")
// ErrNilInput means the input is nil
ErrNilInput = errors.New("nil input")
// ErrLeftOverText indicates there are some of the expression are left pared
ErrLeftOverText = errors.New("left over text")
// ErrUnmatchedParenthesis indicated the mismatching parenthesis
ErrUnmatchedParenthesis = errors.New("unmatched parenthesis")
)
// string without quoted is variable name
type varString string
func (q varString) String() string {
return string(q)
}
// dynamic types for i are string, qString, float64, list
type sexp struct {
// type of i must NOT be sexp
i interface{}
}
func (exp sexp) evaluate(ps Params) (interface{}, error) {
if l, isList := exp.i.(list); isList {
if len(l) == 0 {
return make([]interface{}, 0), nil
}
params := make([]interface{}, 0, len(l))
for _, p := range l {
v, err := p.evaluate(ps)
if err != nil {
return nil, err
}
params = append(params, v)
}
if fn, ok := params[0].(function.Func); ok {
return fn(params[1:]...)
}
return params, nil
}
if val, ok := exp.i.(varString); ok {
s := string(val)
if fn, err := function.Get(s); err == nil {
return fn, nil
}
return ps.Get(s)
}
return exp.i, nil
}
func (exp sexp) properties() []string {
if l, isList := exp.i.(list); isList {
if len(l) == 0 {
return nil
}
var props []string
for _, p := range l {
props = append(props, p.properties()...)
}
return props
}
if val, ok := exp.i.(varString); ok {
s := string(val)
if _, err := function.Get(s); err == nil {
return nil
}
return []string{s}
}
return nil
}
func parse(exp string) (sexp, error) {
data := []byte(exp)
tokens := queue.New()
ss:
for i := 0; i < len(data); {
advance, token, err := scan(data[i:])
if err != nil {
return sexp{}, err
}
i += advance
if t, ok := token.(byte); ok && t == ')' {
ins := queue.New()
for e := tokens.Back(); e != nil; e = tokens.Back() {
tokens.Remove(e)
if v, ok := e.Value.(byte); ok && v == '(' {
exps := make(list, 0, ins.Len())
for e := ins.Back(); e != nil; e = e.Prev() {
if p, ok := e.Value.(sexp); ok {
exps = append(exps, p)
} else {
exps = append(exps, sexp{e.Value})
}
}
tokens.PushBack(sexp{exps})
continue ss
}
ins.PushBack(e.Value)
}
return sexp{}, ErrUnmatchedParenthesis
}
tokens.PushBack(token)
}
if tokens.Len() == 0 {
return sexp{}, ErrNilInput
} else if tokens.Len() != 1 {
return sexp{}, ErrLeftOverText
}
if exp, ok := tokens.Back().Value.(sexp); ok {
if l, ok := exp.i.(list); ok && len(l) == 0 {
return sexp{}, ErrNilInput
}
return exp, nil
}
return sexp{tokens.Back().Value}, nil
}
func (exp sexp) String() string {
return fmt.Sprintf("%v", exp.i)
}
func (exp sexp) dump(i int) {
fmt.Printf("%*s%v: ", i*3, "", reflect.TypeOf(exp.i))
if l, isList := exp.i.(list); isList {
fmt.Printf("%d elements: %s\n", len(l), l)
for _, e := range l {
e.dump(i + 1)
}
} else {
fmt.Println(exp)
}
}
type list []sexp
func (l list) String() string {
if len(l) == 0 {
return "[]"
}
b := fmt.Sprintf("[%v", l[0])
for _, s := range l[1:] {
b = fmt.Sprintf("%s %v", b, s)
}
return b + "]"
}
func scan(data []byte) (advance int, token interface{}, err error) {
length := len(data)
start := 0
for width := 0; start < length; start += width {
var r rune
r, width = utf8.DecodeRune(data[start:])
if !unicode.IsSpace(r) {
break
}
}
if start >= length {
return start, nil, nil
}
if b := data[start]; b == '\'' || b == '"' || b == '`' {
advance, token, err = scanStringWithQuotesStriped(data[start:])
return start + advance, string(token.([]byte)), err
}
for width, i := 0, start; i < length; i += width {
if b := data[i]; b == ')' || b == '(' {
if i == start {
return start + 1, data[i], nil
}
return i, convert(data[start:i]), nil
}
var r rune
r, width = utf8.DecodeRune(data[i:])
if unicode.IsSpace(r) {
return i, convert(data[start:i]), nil
}
}
return len(data), convert(data[start:]), nil
}
func convert(data []byte) interface{} {
// if v, err := strconv.ParseInt(string(data), 10, 0); err == nil {
// return v
// }
if v, err := strconv.ParseFloat(string(data), 64); err == nil {
return v
}
return varString(data)
}
// scanStringWithQuotesStriped scan string surrounded with ', " or something like this, a single character
func scanStringWithQuotesStriped(data []byte) (advance int, token []byte, err error) {
length := len(data)
if length == 0 {
return 0, nil, nil
}
// escape character positions
ecps := make([]int, 0, len(data))
defer func() {
// remove escape character
if err != nil {
return
}
tokenLen := len(token)
tmp := make([]byte, 0, tokenLen)
s := 0
for _, p := range ecps {
tmp = append(tmp, token[s:p]...)
s = p + 1
}
if s < tokenLen {
tmp = append(tmp, token[s:]...)
}
token = tmp
}()
delim := data[0]
ecp := -1
for i := 1; i < length; i++ {
if data[i] == '\\' {
ecp = i
}
if data[i] == delim {
escapeEscaped := continuousCharacterCountFromBack(data[1:i], '\\')%2 == 0
if ecp == i-1 && !escapeEscaped {
ecps = append(ecps, ecp-1)
} else {
advance, token = i+1, data[1:i]
return
}
}
}
return 0, nil, ErrUnexpectedEnd
}
func continuousCharacterCountFromBack(data []byte, key byte) int {
length := len(data)
for i := length; i > 0; i-- {
if data[i-1] != key {
return length - i
}
}
return len(data)
}