-
Notifications
You must be signed in to change notification settings - Fork 5
/
eql.go
258 lines (225 loc) · 5.04 KB
/
eql.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
package eqlang
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"strings"
"unicode"
)
var (
// ErrEndRequired is returned if eql script doesn't end by `%>`.
ErrEndRequired = errors.New("eql script requires `%>` to end")
// ErrEndOfString is returned if string doesn't end.
ErrEndOfString = errors.New("string doesn't end")
)
// -----------------------------------------------------------------------------
// Parse parses eql source into qlang code.
//
func Parse(source string) (code []byte, err error) {
var b bytes.Buffer
for {
pos := strings.Index(source, "<%")
if pos < 0 {
err = parseText(&b, source)
break
}
if pos > 0 {
err = parseText(&b, source[:pos])
if err != nil {
return
}
}
source, err = parseEql(&b, source[pos+2:])
if err != nil {
return
}
}
code = b.Bytes()
return
}
func parseText(b *bytes.Buffer, source string) (err error) {
b.WriteString("print(eql.Subst(`")
for {
pos := strings.IndexByte(source, '`')
if pos < 0 {
b.WriteString(source)
break
}
b.WriteString(source[:pos])
pos2 := pos + 1
for ; pos2 < len(source); pos2++ {
if source[pos2] != '`' {
break
}
}
b.WriteString("` + \"")
b.WriteString(source[pos:pos2])
b.WriteString("\" + `")
source = source[pos2:]
}
b.WriteString("`)); ")
return
}
func parseEql(b *bytes.Buffer, source string) (ret string, err error) {
fexpr := strings.HasPrefix(source, "=")
if fexpr {
b.WriteString("print(")
source = source[1:]
}
for {
pos := strings.IndexAny(source, "%\"`")
if pos < 0 {
return "", ErrEndRequired
}
if c := source[pos]; c == '%' {
if strings.HasPrefix(source[pos+1:], ">") {
ret = source[pos+2:]
b.WriteString(source[:pos])
if fexpr {
b.WriteString("); ")
} else if strings.HasPrefix(ret, "\n") {
ret = ret[1:]
b.WriteString("\n")
} else {
b.WriteString("; ")
}
return
}
b.WriteString(source[:pos+1])
source = source[pos+1:]
} else {
n := findEnd(source[pos+1:], c)
if n < 0 {
return "", ErrEndOfString
}
n += pos + 2
b.WriteString(source[:n])
source = source[n:]
}
}
}
func findEnd(line string, c byte) int {
for i := 0; i < len(line); i++ {
switch line[i] {
case c:
return i
case '\\':
i++
}
}
return -1
}
// -----------------------------------------------------------------------------
// Variables represent how to get value of a variable.
//
type Variables interface {
GetVar(name string) (v interface{}, ok bool)
}
type mapVars map[string]interface{}
type mapStrings map[string]string
func (vars mapVars) GetVar(name string) (v interface{}, ok bool) {
v, ok = vars[name]
return
}
func (vars mapStrings) GetVar(name string) (v interface{}, ok bool) {
v, ok = vars[name]
return
}
// Subst substs variables in text.
//
func Subst(text string, lang interface{}) string {
var vars Variables
switch v := lang.(type) {
case map[string]interface{}:
vars = mapVars(v)
case map[string]string:
vars = mapStrings(v)
case Variables:
vars = v
default:
panic(fmt.Sprintf("eql.Subst: unsupported lang type `%v`", reflect.TypeOf(lang)))
}
return subst(text, vars)
}
func subst(text string, vars Variables) string {
var b bytes.Buffer
for {
pos := strings.IndexByte(text, '$')
if pos < 0 || pos+1 >= len(text) {
if b.Len() == 0 {
return text
}
b.WriteString(text)
break
}
switch c := text[pos+1]; {
case c == '$':
b.WriteString(text[:pos+1])
text = text[pos+2:]
case (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'):
b.WriteString(text[:pos])
pos1 := pos + 2
n := strings.IndexFunc(text[pos1:], func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsDigit(c)
})
if n < 0 {
n = len(text) - pos1
}
pos2 := pos1 + n
key := text[pos+1 : pos2]
val, ok := vars.GetVar(key)
if !ok {
panic("variable not found: " + key)
}
b.WriteString(fmt.Sprint(val))
text = text[pos2:]
default:
b.WriteString(text[:pos+1])
text = text[pos+1:]
}
}
return b.String()
}
// -----------------------------------------------------------------------------
// Input decodes a json string.
//
func Input(input string) (ret map[string]interface{}, err error) {
err = json.NewDecoder(strings.NewReader(input)).Decode(&ret)
return
}
// InputFile decodes a json file.
//
func InputFile(input string) (ret map[string]interface{}, err error) {
in := os.Stdin
if input != "-" {
f, err1 := os.Open(input)
if err1 != nil {
return nil, err1
}
defer f.Close()
in = f
}
err = json.NewDecoder(in).Decode(&ret)
return
}
// -----------------------------------------------------------------------------
// Exports is the export table of this module.
//
var Exports = map[string]interface{}{
"new": New,
"parse": Parse,
"subst": Subst,
"input": Input,
"inputFile": InputFile,
"New": New,
"Parse": Parse,
"Subst": Subst,
"Input": Input,
"InputFile": InputFile,
"ErrEndRequired": ErrEndRequired,
"ErrEndOfString": ErrEndOfString,
}
// -----------------------------------------------------------------------------