-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
249 lines (221 loc) · 5.51 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
package gasegment
import (
"fmt"
"regexp"
"strings"
)
var SegmentConditionRe,
SequenceSeparatorRe,
AndSeparatorRe,
OrSeparatorRe,
OpSeparatorRe *regexp.Regexp
func init() {
SegmentConditionRe = regexp.MustCompile(`(?:\\.|[^\\])(;)(?:users::|sessions::)?(?:condition::|sequence::)`)
SequenceSeparatorRe = regexp.MustCompile(`(?:\\.|[^\\])(;->>|;->)`)
AndSeparatorRe = regexp.MustCompile(`(?:\\.|[^\\])(;)`)
OrSeparatorRe = regexp.MustCompile(`(?:\\.|[^\\])(,)`)
// ORDER IS IMPORTANT.
// e.g. using invalid ordering - such as `LessThan, LesthanEqual`, then, regexp is />|>=/, so, ">=" is matching at `>`(LessThan). NG
ops := []Operator{
Equal,
NotEqual,
LessThanEqual,
GreaterThanEqual,
NotBetween,
Between,
GreaterThan,
LessThan,
NotInList,
InList,
ContainsSubstring,
NotContainsSubstring,
Regexp,
NotRegexp,
}
opbuf := make([]string, len(ops))
for i, op := range ops {
opbuf[i] = regexp.QuoteMeta(op.String())
}
OpSeparatorRe = regexp.MustCompile(strings.Join(opbuf, "|"))
}
func MustParse(definition string) Segments {
s, err := Parse(definition)
if err != nil {
panic(err)
}
return s
}
func Parse(definition string) (Segments, error) {
parts := splitByFirstRegexpGroup(definition, SegmentConditionRe)
ret := []Segment{}
var lastScope SegmentScope
for i := 0; i < len(parts); i += 2 {
sd := parts[i]
s, err := parseSegment(sd)
if err != nil {
return nil, err
}
if s.Scope.String() == "" {
if lastScope.String() == "" {
return Segments{}, fmt.Errorf("no segment scope (user:: or session::)")
}
s.Scope = lastScope
}
ret = append(ret, s)
lastScope = s.Scope
}
return Segments(ret), nil
}
func parseSegment(definition string) (Segment, error) {
s := definition
sg := Segment{}
// parse scope
if strings.HasPrefix(s, "sessions::") {
sg.Scope = SessionScope
s = s[len("sessions::"):]
}
if strings.HasPrefix(s, "users::") {
sg.Scope = UserScope
s = s[len("users::"):]
}
// condition
if strings.HasPrefix(s, "condition::") {
sg.Type = ConditionSegment
s = s[len("condition::"):]
c, err := parseCondition(s)
if err != nil {
return Segment{}, err
}
sg.Condition = c
} else if strings.HasPrefix(s, "sequence::") {
sg.Type = SequenceSegment
s = s[len("sequence::"):]
sq, err := parseSequence(s)
if err != nil {
return Segment{}, err
}
sg.Sequence = sq
} else {
return sg, fmt.Errorf(fmt.Sprintf("unknown segment condition %s", s))
}
return sg, nil
}
func parseSequence(definition string) (Sequence, error) {
s := definition
seq := Sequence{}
// not?
if strings.HasPrefix(s, "!") {
seq.Not = true
s = s[len("!"):]
}
// FirstHitMatchesFirstStep?
if strings.HasPrefix(s, "^") {
seq.FirstHitMatchesFirstStep = true
s = s[len("^"):]
}
// Sequence
steps := []SequenceStep{}
sParts := splitByFirstRegexpGroup(s, SequenceSeparatorRe)
first, err := parseAndExpression(sParts[0])
if err != nil {
return Sequence{}, err
}
steps = append(steps, SequenceStep{
Type: FirstStep,
AndExpression: first,
})
for i := 1; i < len(sParts); i += 2 {
step := SequenceStep{}
typeStr := sParts[i]
ae, err := parseAndExpression(sParts[i+1])
if err != nil {
return Sequence{}, err
}
step.Type = SequenceStepType(typeStr)
step.AndExpression = ae
steps = append(steps, step)
}
seq.SequenceSteps = SequenceSteps(steps)
return seq, nil
}
func parseCondition(definition string) (Condition, error) {
s := definition
c := Condition{}
if strings.HasPrefix(s, "!") {
c.Exclude = true
s = s[len("!"):]
}
ae, err := parseAndExpression(s)
if err != nil {
return Condition{}, err
}
c.AndExpression = ae
return c, nil
}
func parseAndExpression(definition string) (AndExpression, error) {
parts := splitByFirstRegexpGroup(definition, AndSeparatorRe)
orExpressions := []OrExpression{}
for i := 0; i < len(parts); i += 2 {
or, err := parseOrExpression(parts[i])
if err != nil {
return AndExpression{}, err
}
orExpressions = append(orExpressions, or)
}
return AndExpression(orExpressions), nil
}
func parseOrExpression(definition string) (OrExpression, error) {
parts := splitByFirstRegexpGroup(definition, OrSeparatorRe)
expressions := []Expression{}
for i := 0; i < len(parts); i += 2 {
or, err := parseExpression(parts[i])
if err != nil {
return OrExpression{}, err
}
expressions = append(expressions, or)
}
return OrExpression(expressions), nil
}
func parseExpression(definition string) (Expression, error) {
e := Expression{}
s := definition
mss := []MetricScope{PerHit, PerUser, PerSession}
for _, ms := range mss {
if strings.HasPrefix(s, ms.String()) {
e.MetricScope = ms
s = s[len(ms.String()):]
break
}
}
idxes := OpSeparatorRe.FindAllStringIndex(s, -1)
if len(idxes) == 0 {
return Expression{}, fmt.Errorf(fmt.Sprintf("invalid expression: %s", definition))
}
opi := idxes[0]
e.Target = DimensionOrMetric(s[:opi[0]])
if e.Target == "" {
return Expression{}, fmt.Errorf("empty dimension or metric")
}
e.Operator = Operator(s[opi[0]:opi[1]])
e.Value = UnEscapeExpressionValue(s[opi[1]:])
return e, nil
}
func splitByFirstRegexpGroup(s string, r *regexp.Regexp) []string {
indexes := r.FindAllStringSubmatchIndex(s, -1)
if len(indexes) == 0 {
return []string{s}
}
ret := make([]string, len(indexes)*2+1)
last := []int{0, 0}
for i, idx := range indexes {
if len(idx) < 4 {
panic("regexp must contains group")
}
pos := []int{idx[2], idx[3]}
ret[i*2] = s[last[1]:pos[0]]
ret[i*2+1] = s[pos[0]:pos[1]]
last = pos
}
ret[len(ret)-1] = s[last[1]:]
return ret
}