-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.go
265 lines (231 loc) · 7.98 KB
/
translate.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
package main
import (
"fmt"
"strings"
"unicode"
)
func translateTextQuery(query string) (*searchClause, error) {
out, _, err := translateTextQueryInternal([]rune(query), 0, false)
return out, err
}
type translationStatus struct {
Word []rune
Words [][]rune
Clauses []*searchClause
Operations []string
Negation bool
}
func isWordEqualTo(word []rune, target string) bool {
if len(word) != len(target) {
return false
}
// Loop over 'target' so we can easily extract runes.
for i, x := range target {
if x != word[i] {
return false
}
}
return true
}
func translateTextQueryInternal(query []rune, at int, open_par bool) (*searchClause, int, error) {
var status translationStatus
status.Word = []rune{}
status.Words = [][]rune{}
status.Clauses = []*searchClause{}
status.Operations = []string{}
closing_par := false;
original := at
for at < len(query) {
c := query[at];
if c == '(' {
if isWordEqualTo(status.Word, "AND") || isWordEqualTo(status.Word, "OR") {
err := translateAddOperation(&status, at)
if err != nil {
return nil, 0, err
}
} else if isWordEqualTo(status.Word, "NOT") {
if len(status.Words) > 0 || len(status.Operations) < len(status.Clauses) {
return nil, 0, fmt.Errorf("illegal placement of NOT at position %d", at)
}
status.Negation = true
status.Word = []rune{}
} else if len(status.Word) > 0 || len(status.Words) > 0 {
return nil, 0, fmt.Errorf("search clauses must be separated by AND or OR at position %d", at)
}
nested, at2, err := translateTextQueryInternal(query, at + 1, true)
if err != nil {
return nil, 0, err
}
at = at2
if status.Negation {
nested = &searchClause{ Type: "not", Child: nested }
status.Negation = false
}
status.Clauses = append(status.Clauses, nested)
continue
}
if c == ')' {
if !open_par {
return nil, 0, fmt.Errorf("unmatched closing parenthesis at position %d", at)
}
closing_par = true
at++
break
}
if unicode.IsSpace(c) {
if isWordEqualTo(status.Word, "AND") || isWordEqualTo(status.Word, "OR") {
err := translateAddOperation(&status, at)
if err != nil {
return nil, 0, err
}
} else if isWordEqualTo(status.Word, "NOT") {
if len(status.Words) > 0 || len(status.Operations) < len(status.Clauses) {
return nil, 0, fmt.Errorf("illegal placement of NOT at position %d", at)
}
status.Negation = true
status.Word = []rune{}
} else if len(status.Word) > 0 {
status.Words = append(status.Words, status.Word)
status.Word = []rune{}
}
} else {
status.Word = append(status.Word, c)
}
at++
}
if len(status.Operations) == len(status.Clauses) {
if len(status.Word) > 0 {
if isWordEqualTo(status.Word, "AND") || isWordEqualTo(status.Word, "OR") {
return nil, 0, fmt.Errorf("trailing AND/OR at position %d", at)
}
status.Words = append(status.Words, status.Word)
}
err := translateTextClause(&status, at)
if err != nil {
return nil, 0, err
}
}
if open_par && !closing_par {
return nil, 0, fmt.Errorf("unmatched opening parenthesis at position %d", original - 1)
}
// Finding the stretches of ANDs first.
if len(status.Operations) > 0 {
tmp_clauses := [][]*searchClause{}
active_clauses := []*searchClause{ status.Clauses[0] }
for o, op := range status.Operations {
if op == "AND" {
active_clauses = append(active_clauses, status.Clauses[o + 1])
} else {
tmp_clauses = append(tmp_clauses, active_clauses)
active_clauses = []*searchClause{ status.Clauses[o + 1] }
}
}
tmp_clauses = append(tmp_clauses, active_clauses)
status.Clauses = []*searchClause{}
for _, tmp := range tmp_clauses {
if len(tmp) > 1 {
status.Clauses = append(status.Clauses, &searchClause{ Type: "and", Children: tmp })
} else {
status.Clauses = append(status.Clauses, tmp[0])
}
}
}
// Finally, resolving the ORs.
var output *searchClause
if len(status.Clauses) > 1 {
output = &searchClause{ Type: "or", Children: status.Clauses }
} else {
output = status.Clauses[0]
}
return output, at, nil
}
func translateTextClause(status *translationStatus, at int) error {
if len(status.Words) == 0 {
return fmt.Errorf("no search terms at position %d", at)
}
first_word := status.Words[0]
fi := -1
for i, x := range first_word {
if x == ':' {
fi = i
break
}
}
if fi == 0 {
return fmt.Errorf("search field should be non-empty for terms ending at %d", at)
}
field := ""
if fi > 0 {
field = string(first_word[:fi])
leftover := first_word[fi+1:]
if len(leftover) == 0 {
if len(status.Words) == 1 {
return fmt.Errorf("no search terms at position %d after removing the search field", at)
}
status.Words = status.Words[1:]
} else {
status.Words[0] = leftover
}
}
converted := []*searchClause{}
for _, x := range status.Words {
word := string(x)
converted = append(converted, &searchClause{ Type: "text", Text: word, Field: field, IsPattern: strings.ContainsAny(word, "*?") })
}
var new_component *searchClause
if len(converted) == 1 {
new_component = converted[0]
} else {
new_component = &searchClause{ Type: "and", Children: converted }
}
if status.Negation {
// Two-step replacement, otherwise it becomes a circular reference.
replacement := &searchClause{ Type: "not", Child: new_component }
new_component = replacement
status.Negation = false
}
status.Clauses = append(status.Clauses, new_component)
status.Words = [][]rune{}
return nil
}
func translateAddOperation(status *translationStatus, at int) error {
if len(status.Operations) > len(status.Clauses) {
return fmt.Errorf("multiple AND or OR keywords at position %d", at)
}
if len(status.Operations) == len(status.Clauses) {
// Operations are binary, so if there wasn't already a preceding
// clause, then we must try to add a text clause.
err := translateTextClause(status, at)
if err != nil {
return err
}
}
status.Operations = append(status.Operations, string(status.Word))
status.Word = []rune{}
return nil
}
func translateQuery(query *searchClause) (*searchClause, error) {
if query.Type == "text" {
out, err := translateTextQuery(query.Text)
return out, err
}
if query.Type == "and" || query.Type == "or" {
out_child := []*searchClause{}
for _, x := range query.Children {
out, err := translateQuery(x)
if err != nil {
return nil, err
}
out_child = append(out_child, out)
}
return &searchClause{ Type: query.Type, Children: out_child }, nil
}
if query.Type == "not" {
out, err := translateQuery(query.Child)
if err != nil {
return nil, err
}
return &searchClause{ Type: query.Type, Child: out }, nil
}
return query, nil
}