-
Notifications
You must be signed in to change notification settings - Fork 107
/
pattern.go
550 lines (509 loc) · 11.7 KB
/
pattern.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package docopt
import (
"fmt"
"reflect"
"strings"
)
type patternType uint
const (
// leaf
patternArgument patternType = 1 << iota
patternCommand
patternOption
// branch
patternRequired
patternOptionAL
patternOptionSSHORTCUT // Marker/placeholder for [options] shortcut.
patternOneOrMore
patternEither
patternLeaf = patternArgument +
patternCommand +
patternOption
patternBranch = patternRequired +
patternOptionAL +
patternOptionSSHORTCUT +
patternOneOrMore +
patternEither
patternAll = patternLeaf + patternBranch
patternDefault = 0
)
func (pt patternType) String() string {
switch pt {
case patternArgument:
return "argument"
case patternCommand:
return "command"
case patternOption:
return "option"
case patternRequired:
return "required"
case patternOptionAL:
return "optional"
case patternOptionSSHORTCUT:
return "optionsshortcut"
case patternOneOrMore:
return "oneormore"
case patternEither:
return "either"
case patternLeaf:
return "leaf"
case patternBranch:
return "branch"
case patternAll:
return "all"
case patternDefault:
return "default"
}
return ""
}
type pattern struct {
t patternType
children patternList
name string
value interface{}
short string
long string
argcount int
}
type patternList []*pattern
func newBranchPattern(t patternType, pl ...*pattern) *pattern {
var p pattern
p.t = t
p.children = make(patternList, len(pl))
copy(p.children, pl)
return &p
}
func newRequired(pl ...*pattern) *pattern {
return newBranchPattern(patternRequired, pl...)
}
func newEither(pl ...*pattern) *pattern {
return newBranchPattern(patternEither, pl...)
}
func newOneOrMore(pl ...*pattern) *pattern {
return newBranchPattern(patternOneOrMore, pl...)
}
func newOptional(pl ...*pattern) *pattern {
return newBranchPattern(patternOptionAL, pl...)
}
func newOptionsShortcut() *pattern {
var p pattern
p.t = patternOptionSSHORTCUT
return &p
}
func newLeafPattern(t patternType, name string, value interface{}) *pattern {
// default: value=nil
var p pattern
p.t = t
p.name = name
p.value = value
return &p
}
func newArgument(name string, value interface{}) *pattern {
// default: value=nil
return newLeafPattern(patternArgument, name, value)
}
func newCommand(name string, value interface{}) *pattern {
// default: value=false
var p pattern
p.t = patternCommand
p.name = name
p.value = value
return &p
}
func newOption(short, long string, argcount int, value interface{}) *pattern {
// default: "", "", 0, false
var p pattern
p.t = patternOption
p.short = short
p.long = long
if long != "" {
p.name = long
} else {
p.name = short
}
p.argcount = argcount
if value == false && argcount > 0 {
p.value = nil
} else {
p.value = value
}
return &p
}
func (p *pattern) flat(types patternType) (patternList, error) {
if p.t&patternLeaf != 0 {
if types == patternDefault {
types = patternAll
}
if p.t&types != 0 {
return patternList{p}, nil
}
return patternList{}, nil
}
if p.t&patternBranch != 0 {
if p.t&types != 0 {
return patternList{p}, nil
}
result := patternList{}
for _, child := range p.children {
childFlat, err := child.flat(types)
if err != nil {
return nil, err
}
result = append(result, childFlat...)
}
return result, nil
}
return nil, newError("unknown pattern type: %d, %d", p.t, types)
}
func (p *pattern) fix() error {
err := p.fixIdentities(nil)
if err != nil {
return err
}
p.fixRepeatingArguments()
return nil
}
func (p *pattern) fixIdentities(uniq patternList) error {
// Make pattern-tree tips point to same object if they are equal.
if p.t&patternBranch == 0 {
return nil
}
if uniq == nil {
pFlat, err := p.flat(patternDefault)
if err != nil {
return err
}
uniq = pFlat.unique()
}
for i, child := range p.children {
if child.t&patternBranch == 0 {
ind, err := uniq.index(child)
if err != nil {
return err
}
p.children[i] = uniq[ind]
} else {
err := child.fixIdentities(uniq)
if err != nil {
return err
}
}
}
return nil
}
func (p *pattern) fixRepeatingArguments() {
// Fix elements that should accumulate/increment values.
var either []patternList
for _, child := range p.transform().children {
either = append(either, child.children)
}
for _, cas := range either {
casMultiple := patternList{}
for _, e := range cas {
if cas.count(e) > 1 {
casMultiple = append(casMultiple, e)
}
}
for _, e := range casMultiple {
if e.t == patternArgument || e.t == patternOption && e.argcount > 0 {
switch e.value.(type) {
case string:
e.value = strings.Fields(e.value.(string))
case []string:
default:
e.value = []string{}
}
}
if e.t == patternCommand || e.t == patternOption && e.argcount == 0 {
e.value = 0
}
}
}
}
func (p *pattern) match(left *patternList, collected *patternList) (bool, *patternList, *patternList) {
if collected == nil {
collected = &patternList{}
}
if p.t&patternRequired != 0 {
l := left
c := collected
for _, p := range p.children {
var matched bool
matched, l, c = p.match(l, c)
if !matched {
return false, left, collected
}
}
return true, l, c
} else if p.t&patternOptionAL != 0 || p.t&patternOptionSSHORTCUT != 0 {
for _, p := range p.children {
_, left, collected = p.match(left, collected)
}
return true, left, collected
} else if p.t&patternOneOrMore != 0 {
if len(p.children) != 1 {
panic("OneOrMore.match(): assert len(p.children) == 1")
}
l := left
c := collected
var lAlt *patternList
matched := true
times := 0
for matched {
// could it be that something didn't match but changed l or c?
matched, l, c = p.children[0].match(l, c)
if matched {
times++
}
if lAlt == l {
break
}
lAlt = l
}
if times >= 1 {
return true, l, c
}
return false, left, collected
} else if p.t&patternEither != 0 {
type outcomeStruct struct {
matched bool
left *patternList
collected *patternList
length int
}
outcomes := []outcomeStruct{}
for _, p := range p.children {
matched, l, c := p.match(left, collected)
outcome := outcomeStruct{matched, l, c, len(*l)}
if matched {
outcomes = append(outcomes, outcome)
}
}
if len(outcomes) > 0 {
minLen := outcomes[0].length
minIndex := 0
for i, v := range outcomes {
if v.length < minLen {
minIndex = i
}
}
return outcomes[minIndex].matched, outcomes[minIndex].left, outcomes[minIndex].collected
}
return false, left, collected
} else if p.t&patternLeaf != 0 {
pos, match := p.singleMatch(left)
var increment interface{}
if match == nil {
return false, left, collected
}
leftAlt := make(patternList, len((*left)[:pos]), len((*left)[:pos])+len((*left)[pos+1:]))
copy(leftAlt, (*left)[:pos])
leftAlt = append(leftAlt, (*left)[pos+1:]...)
sameName := patternList{}
for _, a := range *collected {
if a.name == p.name {
sameName = append(sameName, a)
}
}
switch p.value.(type) {
case int, []string:
switch p.value.(type) {
case int:
increment = 1
case []string:
switch match.value.(type) {
case string:
increment = []string{match.value.(string)}
default:
increment = match.value
}
}
if len(sameName) == 0 {
match.value = increment
collectedMatch := make(patternList, len(*collected), len(*collected)+1)
copy(collectedMatch, *collected)
collectedMatch = append(collectedMatch, match)
return true, &leftAlt, &collectedMatch
}
switch sameName[0].value.(type) {
case int:
sameName[0].value = sameName[0].value.(int) + increment.(int)
case []string:
sameName[0].value = append(sameName[0].value.([]string), increment.([]string)...)
}
return true, &leftAlt, collected
}
collectedMatch := make(patternList, len(*collected), len(*collected)+1)
copy(collectedMatch, *collected)
collectedMatch = append(collectedMatch, match)
return true, &leftAlt, &collectedMatch
}
panic("unmatched type")
}
func (p *pattern) singleMatch(left *patternList) (int, *pattern) {
if p.t&patternArgument != 0 {
for n, pat := range *left {
if pat.t&patternArgument != 0 {
return n, newArgument(p.name, pat.value)
}
}
return -1, nil
} else if p.t&patternCommand != 0 {
for n, pat := range *left {
if pat.t&patternArgument != 0 {
if pat.value == p.name {
return n, newCommand(p.name, true)
}
break
}
}
return -1, nil
} else if p.t&patternOption != 0 {
for n, pat := range *left {
if p.name == pat.name {
return n, pat
}
}
return -1, nil
}
panic("unmatched type")
}
func (p *pattern) String() string {
if p.t&patternOption != 0 {
return fmt.Sprintf("%s(%s, %s, %d, %+v)", p.t, p.short, p.long, p.argcount, p.value)
} else if p.t&patternLeaf != 0 {
return fmt.Sprintf("%s(%s, %+v)", p.t, p.name, p.value)
} else if p.t&patternBranch != 0 {
result := ""
for i, child := range p.children {
if i > 0 {
result += ", "
}
result += child.String()
}
return fmt.Sprintf("%s(%s)", p.t, result)
}
panic("unmatched type")
}
func (p *pattern) transform() *pattern {
/*
Expand pattern into an (almost) equivalent one, but with single Either.
Example: ((-a | -b) (-c | -d)) => (-a -c | -a -d | -b -c | -b -d)
Quirks: [-a] => (-a), (-a...) => (-a -a)
*/
result := []patternList{}
groups := []patternList{patternList{p}}
parents := patternRequired +
patternOptionAL +
patternOptionSSHORTCUT +
patternEither +
patternOneOrMore
for len(groups) > 0 {
children := groups[0]
groups = groups[1:]
var child *pattern
for _, c := range children {
if c.t&parents != 0 {
child = c
break
}
}
if child != nil {
children.remove(child)
if child.t&patternEither != 0 {
for _, c := range child.children {
r := patternList{}
r = append(r, c)
r = append(r, children...)
groups = append(groups, r)
}
} else if child.t&patternOneOrMore != 0 {
r := patternList{}
r = append(r, child.children.double()...)
r = append(r, children...)
groups = append(groups, r)
} else {
r := patternList{}
r = append(r, child.children...)
r = append(r, children...)
groups = append(groups, r)
}
} else {
result = append(result, children)
}
}
either := patternList{}
for _, e := range result {
either = append(either, newRequired(e...))
}
return newEither(either...)
}
func (p *pattern) eq(other *pattern) bool {
return reflect.DeepEqual(p, other)
}
func (pl patternList) unique() patternList {
table := make(map[string]bool)
result := patternList{}
for _, v := range pl {
if !table[v.String()] {
table[v.String()] = true
result = append(result, v)
}
}
return result
}
func (pl patternList) index(p *pattern) (int, error) {
for i, c := range pl {
if c.eq(p) {
return i, nil
}
}
return -1, newError("%s not in list", p)
}
func (pl patternList) count(p *pattern) int {
count := 0
for _, c := range pl {
if c.eq(p) {
count++
}
}
return count
}
func (pl patternList) diff(l patternList) patternList {
lAlt := make(patternList, len(l))
copy(lAlt, l)
result := make(patternList, 0, len(pl))
for _, v := range pl {
if v != nil {
match := false
for i, w := range lAlt {
if w.eq(v) {
match = true
lAlt[i] = nil
break
}
}
if match == false {
result = append(result, v)
}
}
}
return result
}
func (pl patternList) double() patternList {
l := len(pl)
result := make(patternList, l*2)
copy(result, pl)
copy(result[l:2*l], pl)
return result
}
func (pl *patternList) remove(p *pattern) {
(*pl) = pl.diff(patternList{p})
}
func (pl patternList) dictionary() map[string]interface{} {
dict := make(map[string]interface{})
for _, a := range pl {
dict[a.name] = a.value
}
return dict
}