-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathambiguity.go
339 lines (301 loc) · 9.12 KB
/
ambiguity.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2014-2016 Dave Pifke.
//
// Redistribution and use in source and binary forms, with or without
// modification, is permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package fastmatch
import (
"bytes"
"sort"
"strconv"
)
// ErrAmbiguous is returned when Generate or GenerateReverse is passed
// ambiguous matches: cases where it's possible for the same input string to
// match different return values.
type ErrAmbiguous struct {
keys []map[string]bool
}
// add provides one or more keys that are ambiguous with each other.
func (e *ErrAmbiguous) add(backToOrig map[string][]string, keys ...string) {
origKeys := make([]string, 0, len(keys))
for _, key := range keys {
if k, found := backToOrig[key]; found {
origKeys = append(origKeys, k...)
} else {
origKeys = append(origKeys, key)
}
}
for n := range e.keys {
for existing := range e.keys[n] {
for _, other := range origKeys {
if existing == other {
for _, key := range origKeys {
e.keys[n][key] = true
}
return
}
}
}
}
e.keys = append(e.keys, make(map[string]bool, len(origKeys)))
for _, key := range origKeys {
e.keys[len(e.keys)-1][key] = true
}
}
// sliceOfStringSlices implements strings.Sortable on a slice of string
// slices. The first-level slice is sorted according to the first element in
// each second-level slice.
type sliceOfStringSlices [][]string
func (s sliceOfStringSlices) Len() int { return len(s) }
func (s sliceOfStringSlices) Swap(a, b int) { s[a], s[b] = s[b], s[a] }
func (s sliceOfStringSlices) Less(a, b int) bool {
if len(s[a]) == 0 && len(s[b]) > 0 {
return true
} else if len(s[b]) == 0 {
return false
}
return s[a][0] < s[b][0]
}
// sort reorders elements in the proper order.
func (s sliceOfStringSlices) sort() {
for n := range s {
sort.Strings(s[n])
}
sort.Sort(s)
}
// sortedKeys collapses our map of duplicate keys to nested slices, in
// lexicographical order.
func (e *ErrAmbiguous) sortedKeys() [][]string {
var keys sliceOfStringSlices
for n, ambiguous := range e.keys {
keys = append(keys, make([]string, 0, len(ambiguous)))
for key := range ambiguous {
keys[n] = append(keys[n], key)
}
}
keys.sort()
return keys
}
func (e *ErrAmbiguous) Error() string {
var b bytes.Buffer
for _, group := range e.sortedKeys() {
if b.Len() == 0 {
b.WriteString("ambiguous matches: ")
} else {
b.Write([]byte{';', ' '})
}
first := true
for _, key := range group {
if !first {
b.Write([]byte{',', ' '})
} else {
first = false
}
b.WriteString(strconv.Quote(key))
}
}
return b.String()
}
// seenCases stores a collection of unique return value/key mappings.
type seenCases map[string]map[string]bool
// collapse returns lists of unique return values and keys.
func (sc seenCases) collapse() ([]string, []string) {
var rets, keys []string
for ret, keysMap := range sc {
rets = append(rets, ret)
for key := range keysMap {
keys = append(keys, key)
}
}
return rets, keys
}
// disambiguate maps final state and rune to a seenCases collection.
type disambiguate struct {
cases map[uint64]map[rune]seenCases
keys map[string]bool
}
// foreach iterates over unique final states, calling the supplied function
// with the possible return values and keys for each.
func (d *disambiguate) foreach(f func([]string, []string)) {
for sum := range d.cases {
for r := range d.cases[sum] {
rets, keys := d.cases[sum][r].collapse()
f(rets, keys)
}
}
}
// add indexes a possible final state.
func (d *disambiguate) add(sum uint64, r rune, ret, key string) {
if d.cases == nil {
d.cases = make(map[uint64]map[rune]seenCases)
}
if _, exists := d.cases[sum]; !exists {
d.cases[sum] = make(map[rune]seenCases)
}
if _, exists := d.cases[sum][r]; !exists {
d.cases[sum][r] = make(seenCases)
}
if _, exists := d.cases[sum][r][ret]; !exists {
d.cases[sum][r][ret] = make(map[string]bool)
}
d.cases[sum][r][ret][key] = true
if d.keys == nil {
d.keys = make(map[string]bool)
}
d.keys[key] = true
}
// indexNoMore iterates over the final states for partial matches in a
// stateMachine, adding them to the disambiguation index.
func (d *disambiguate) indexNoMore(state *stateMachine, cases map[string]string) {
state.foreachNoMore(func(_ int, r rune, key string) {
sum := state.finalState(key)
d.add(sum, r, cases[key], key)
// finalIdx is the index within state.final of our terminal
// rune. changesIdx is index within state.changes.
var finalIdx, changesIdx int
if state.offset == 0 {
finalIdx = len(key) - 1
changesIdx = finalIdx
} else {
// state.final[0] is the collapsed state from the
// previous state machine.
finalIdx = len(key) - state.offset
changesIdx = finalIdx - 1
}
// Add longer keys which share the same final rune and
// intermediate state:
for other, values := range state.final {
if len(other) <= len(key) {
continue // not longer
}
if values[finalIdx] != state.changes[changesIdx][r] {
continue // different final rune
}
var otherSum uint64
for n := 0; n < finalIdx; n++ {
otherSum += values[n]
}
if otherSum != sum {
continue // different intermediate state
}
d.add(sum, r, cases[other], other)
}
})
}
// indexFinal iterates over the final states in a stateMachine. States which
// were not already indexed by indexNoMore will be added to the disambiguation
// index.
func (d *disambiguate) indexFinal(state *stateMachine, cases map[string]string) {
for key := range state.final {
if d.keys[key] {
continue // already indexed by indexNoMore()
}
d.add(state.finalState(key), 0, cases[key], key)
}
}
// shortestString returns the shortest string from a list of strings.
func shortestString(ss []string) string {
var shortest string
for _, s := range ss {
if shortest == "" || len(s) < len(shortest) {
shortest = s
}
}
return shortest
}
// checkAmbiguity verifies there is exactly one possible return value for each
// final state, returning an error if any matches are ambiguous.
func (state *stateMachine) checkAmbiguity(cases, origCases map[string]string, backToOrig map[string][]string) error {
e := new(ErrAmbiguous)
// Keys which got mangled or truncated to the same value (due to
// StopUpon, Ignore, or IgnoreExcept) are caught first.
for _, keys := range backToOrig {
if len(keys) <= 1 {
continue
}
rets := make(map[string]bool, len(keys))
for _, key := range keys {
rets[origCases[key]] = true
}
if len(rets) > 1 {
// Only an issue if they have different return values.
e.add(nil, keys...)
}
}
// Now perform a more exhaustive search.
for {
d := new(disambiguate)
d.indexNoMore(state, cases)
if state.continued == nil {
d.indexFinal(state, cases)
}
d.foreach(func(rets []string, keys []string) {
if len(rets) == 1 {
// Not ambiguous, but delete all but the
// shortest key with this return value. This
// eliminates duplicate and unreachable case
// statements.
shortest := shortestString(keys)
for _, key := range keys {
if key != shortest {
state.deleteKey(key)
}
}
} else if len(keys) > 1 {
e.add(backToOrig, keys...)
}
})
if state.continued == nil {
break
} else {
state = state.continued
}
}
if len(e.keys) == 0 {
return nil
}
return e
}
// checkReverseAmbiguity verifies that each return value maps to at most a
// single key.
//
// Having multiple keys return the same value is no problem for Generate, but
// causes duplicate/ambiguous case statements in GenerateReverse.
func checkReverseAmbiguity(cases map[string]string) error {
d := make(map[string][]string, len(cases))
for key := range cases {
d[cases[key]] = append(d[cases[key]], key)
}
e := new(ErrAmbiguous)
for _, keys := range d {
if len(keys) > 1 {
e.add(nil, keys...)
}
}
if len(e.keys) == 0 {
return nil
}
return e
}