-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparser.go
269 lines (236 loc) · 6.33 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
263
264
265
266
267
268
269
package tagexpressions
import (
"bytes"
"fmt"
"strings"
"unicode"
"regexp"
)
const OPERAND = "operand"
const OPERATOR = "operator"
var VALID_TOKEN = regexp.MustCompile(`^(?:@[^@]*|and|or|not|\(|\))$`)
type Evaluatable interface {
Evaluate(variables []string) bool
ToString() string
}
func Parse(infix string) (Evaluatable, error) {
tokens, err := tokenize(infix)
if err != nil {
return nil, err
}
if len(tokens) == 0 {
return &trueExpr{}, nil
}
expressions := &EvaluatableStack{}
operators := &StringStack{}
expectedTokenType := OPERAND
for _, token := range tokens {
if isUnary(token) {
if err := check(infix, expectedTokenType, OPERAND); err != nil {
return nil, err
}
operators.Push(token)
expectedTokenType = OPERAND
} else if isBinary(token) {
if err := check(infix, expectedTokenType, OPERATOR); err != nil {
return nil, err
}
for operators.Len() > 0 &&
isOp(operators.Peek()) &&
((ASSOC[token] == "left" && PREC[token] <= PREC[operators.Peek()]) ||
(ASSOC[token] == "right" && PREC[token] < PREC[operators.Peek()])) {
pushExpr(operators.Pop(), expressions)
}
operators.Push(token)
expectedTokenType = OPERAND
} else if "(" == token {
if err := check(infix, expectedTokenType, OPERAND); err != nil {
return nil, err
}
operators.Push(token)
expectedTokenType = OPERAND
} else if ")" == token {
if err := check(infix, expectedTokenType, OPERATOR); err != nil {
return nil, err
}
for operators.Len() > 0 && operators.Peek() != "(" {
pushExpr(operators.Pop(), expressions)
}
if operators.Len() == 0 {
return nil, fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Unmatched ).", infix)
}
if operators.Peek() == "(" {
operators.Pop()
}
expectedTokenType = OPERATOR
} else {
if err := check(infix, expectedTokenType, OPERAND); err != nil {
return nil, err
}
pushExpr(token, expressions)
expectedTokenType = OPERATOR
}
}
for operators.Len() > 0 {
if operators.Peek() == "(" {
return nil, fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Unmatched (.", infix)
}
pushExpr(operators.Pop(), expressions)
}
return expressions.Pop(), nil
}
var ASSOC = map[string]string{
"or": "left",
"and": "left",
"not": "right",
}
var PREC = map[string]int{
"(": -2,
")": -1,
"or": 0,
"and": 1,
"not": 2,
}
func tokenize(expr string) ([]string, error) {
var tokens []string
var token bytes.Buffer
escaped := false
for _, c := range expr {
if escaped {
if c == '(' || c == ')' || c == '\\' || unicode.IsSpace(c) {
token.WriteRune(c)
escaped = false
} else {
return nil, fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Illegal escape before \"%s\".", expr, string(c))
}
} else if c == '\\' {
escaped = true
} else if c == '(' || c == ')' || unicode.IsSpace(c) {
if token.Len() > 0 {
err := isTokenValid(token.String(), expr)
if err != nil {
return nil, err
}
tokens = append(tokens, token.String())
token.Reset()
}
if !unicode.IsSpace(c) {
tokens = append(tokens, string(c))
}
} else {
token.WriteRune(c)
}
}
if token.Len() > 0 {
err := isTokenValid(token.String(), expr)
if err != nil {
return nil, err
}
tokens = append(tokens, token.String())
}
return tokens, nil
}
func isUnary(token string) bool {
return "not" == token
}
func isBinary(token string) bool {
return "or" == token || "and" == token
}
func isOp(token string) bool {
_, ok := ASSOC[token]
return ok
}
func check(infix, expectedTokenType, tokenType string) error {
if expectedTokenType != tokenType {
return fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Expected %s.", infix, expectedTokenType)
}
return nil
}
func isTokenValid(token string, expr string) error {
if !VALID_TOKEN.MatchString(token) {
return fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Please adhere to the Gherkin tag naming convention, using tags like \"@tag1\" and avoiding more than one \"@\" in the tag name.", expr)
}
return nil
}
func pushExpr(token string, stack *EvaluatableStack) {
if token == "and" {
rightAndExpr := stack.Pop()
stack.Push(&andExpr{
leftExpr: stack.Pop(),
rightExpr: rightAndExpr,
})
} else if token == "or" {
rightOrExpr := stack.Pop()
stack.Push(&orExpr{
leftExpr: stack.Pop(),
rightExpr: rightOrExpr,
})
} else if token == "not" {
stack.Push(¬Expr{expr: stack.Pop()})
} else {
stack.Push(&literalExpr{value: token})
}
}
type literalExpr struct {
value string
}
func (l *literalExpr) Evaluate(variables []string) bool {
for _, variable := range variables {
if variable == l.value {
return true
}
}
return false
}
func (l *literalExpr) ToString() string {
s1 := l.value
s2 := strings.Replace(s1, "\\", "\\\\", -1)
s3 := strings.Replace(s2, "(", "\\(", -1)
s4 := strings.Replace(s3, ")", "\\)", -1)
return strings.Replace(s4, " ", "\\ ", -1)
}
type orExpr struct {
leftExpr Evaluatable
rightExpr Evaluatable
}
func (o *orExpr) Evaluate(variables []string) bool {
return o.leftExpr.Evaluate(variables) || o.rightExpr.Evaluate(variables)
}
func (o *orExpr) ToString() string {
return fmt.Sprintf("( %s or %s )", o.leftExpr.ToString(), o.rightExpr.ToString())
}
type andExpr struct {
leftExpr Evaluatable
rightExpr Evaluatable
}
func (a *andExpr) Evaluate(variables []string) bool {
return a.leftExpr.Evaluate(variables) && a.rightExpr.Evaluate(variables)
}
func (a *andExpr) ToString() string {
return fmt.Sprintf("( %s and %s )", a.leftExpr.ToString(), a.rightExpr.ToString())
}
func isBinaryOperator(e Evaluatable) bool {
_, isBinaryAnd := e.(*andExpr)
_, isBinaryOr := e.(*orExpr)
return isBinaryAnd || isBinaryOr
}
type notExpr struct {
expr Evaluatable
}
func (n *notExpr) Evaluate(variables []string) bool {
return !n.expr.Evaluate(variables)
}
func (n *notExpr) ToString() string {
if isBinaryOperator(n.expr) {
// -- HINT: Binary Operators already have already '( ... )'.
return fmt.Sprintf("not %s", n.expr.ToString())
}
return fmt.Sprintf("not ( %s )", n.expr.ToString())
}
type trueExpr struct{}
func (t *trueExpr) Evaluate(variables []string) bool {
return true
}
func (t *trueExpr) ToString() string {
return "true"
}