-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand.go
103 lines (83 loc) · 1.92 KB
/
expand.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
package braceexpansion
func (t *Tree) Expand() []string {
return t.Root.Expand(true)
}
func (l ListNode) Expand(root bool) []string {
// empty brace expressions like "{}" are printed as regular text:
if len(l.Phrases) == 0 {
return []string{l.Tree.opts.OpenBrace + l.Tree.opts.CloseBrace}
}
// if ListNode has only one child, it may be treated as
// optional depending on ParseOpts, but not at root level:
if len(l.Phrases) == 1 {
lines := l.Phrases[0].Expand()
if root {
return lines
} else {
if l.Tree.opts.TreatSingleAsOptional {
lines = append(lines, "")
return lines
} else {
result := []string{}
for _, line := range lines {
result = append(result, l.Tree.opts.OpenBrace+line+l.Tree.opts.CloseBrace)
}
return result
}
}
}
lines := []string{}
for _, phrase := range l.Phrases {
for _, line := range phrase.Expand() {
lines = append(lines, line)
}
}
return lines
}
func (p PhraseNode) Expand() []string {
sets := [][]string{}
for _, part := range p.Parts {
set := p.expandPart(part)
sets = append(sets, set)
}
result := Cartesian(sets)
return result
}
func (p *PhraseNode) expandPart(part Node) []string {
result := []string{}
switch node := part.(type) {
case TextNode:
for _, line := range node.Expand() {
result = append(result, line)
}
case ListNode:
for _, line := range node.Expand(false) {
result = append(result, line)
}
default:
panic("unexpected node type")
}
return result
}
func (t TextNode) Expand() []string {
return []string{t.text}
}
func Cartesian(sets [][]string) []string {
if len(sets) == 0 {
return []string{}
}
result := sets[0]
for i := 1; i < len(sets); i++ {
result = Cartesian2(result, sets[i])
}
return result
}
func Cartesian2(first, second []string) []string {
result := []string{}
for _, e1 := range first {
for _, e2 := range second {
result = append(result, e1+e2)
}
}
return result
}