forked from mvdan/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbraces.go
178 lines (172 loc) · 3.82 KB
/
braces.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
// Copyright (c) 2018, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package syntax
import "strconv"
var (
litLeftBrace = &Lit{Value: "{"}
litComma = &Lit{Value: ","}
litDots = &Lit{Value: ".."}
litRightBrace = &Lit{Value: "}"}
)
// SplitBraces parses brace expansions within a word's literal parts. If any
// valid brace expansions are found, they are replaced with BraceExp nodes, and
// the function returns true. Otherwise, the word is left untouched and the
// function returns false.
//
// For example, a literal word "foo{bar,baz}" will result in a word containing
// the literal "foo", and a brace expansion with the elements "bar" and "baz".
//
// It does not return an error; malformed brace expansions are simply skipped.
// For example, the literal word "a{b" is left unchanged.
func SplitBraces(word *Word) bool {
toSplit := false
top := &Word{}
acc := top
var cur *BraceExp
open := []*BraceExp{}
pop := func() *BraceExp {
old := cur
open = open[:len(open)-1]
if len(open) == 0 {
cur = nil
acc = top
} else {
cur = open[len(open)-1]
acc = cur.Elems[len(cur.Elems)-1]
}
return old
}
addLit := func(lit *Lit) {
acc.Parts = append(acc.Parts, lit)
}
for _, wp := range word.Parts {
lit, ok := wp.(*Lit)
if !ok {
acc.Parts = append(acc.Parts, wp)
continue
}
last := 0
for j := 0; j < len(lit.Value); j++ {
addlitidx := func() {
if last == j {
return // empty lit
}
l2 := *lit
l2.Value = l2.Value[last:j]
addLit(&l2)
}
switch lit.Value[j] {
case '{':
addlitidx()
acc = &Word{}
cur = &BraceExp{Elems: []*Word{acc}}
open = append(open, cur)
case ',':
if cur == nil {
continue
}
addlitidx()
acc = &Word{}
cur.Elems = append(cur.Elems, acc)
case '.':
if cur == nil {
continue
}
if j+1 >= len(lit.Value) || lit.Value[j+1] != '.' {
continue
}
addlitidx()
cur.Sequence = true
acc = &Word{}
cur.Elems = append(cur.Elems, acc)
j++
case '}':
if cur == nil {
continue
}
toSplit = true
addlitidx()
br := pop()
if len(br.Elems) == 1 {
// return {x} to a non-brace
addLit(litLeftBrace)
acc.Parts = append(acc.Parts, br.Elems[0].Parts...)
addLit(litRightBrace)
break
}
if !br.Sequence {
acc.Parts = append(acc.Parts, br)
break
}
var chars [2]bool
broken := false
for i, elem := range br.Elems[:2] {
val := elem.Lit()
if _, err := strconv.Atoi(val); err == nil {
} else if len(val) == 1 &&
(('a' <= val[0] && val[0] <= 'z') ||
('A' <= val[0] && val[0] <= 'Z')) {
chars[i] = true
} else {
broken = true
}
}
if len(br.Elems) == 3 {
// increment must be a number
val := br.Elems[2].Lit()
if _, err := strconv.Atoi(val); err != nil {
broken = true
}
}
// are start and end both chars or
// non-chars?
if chars[0] != chars[1] {
broken = true
}
if !broken {
acc.Parts = append(acc.Parts, br)
break
}
// return broken {x..y[..incr]} to a non-brace
addLit(litLeftBrace)
for i, elem := range br.Elems {
if i > 0 {
addLit(litDots)
}
acc.Parts = append(acc.Parts, elem.Parts...)
}
addLit(litRightBrace)
default:
continue
}
last = j + 1
}
if last == 0 {
addLit(lit)
} else {
left := *lit
left.Value = left.Value[last:]
addLit(&left)
}
}
if !toSplit {
return false
}
// open braces that were never closed fall back to non-braces
for acc != top {
br := pop()
addLit(litLeftBrace)
for i, elem := range br.Elems {
if i > 0 {
if br.Sequence {
addLit(litDots)
} else {
addLit(litComma)
}
}
acc.Parts = append(acc.Parts, elem.Parts...)
}
}
*word = *top
return true
}