Debian Codegraph @

syntax/braces.go

raw
1 // Copyright (c) 2018, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package syntax
5
6 import (
7 "slices"
8 "strconv"
9 "strings"
10 )
11
12 var (
13 litLeftBrace = &Lit{Value: "{"}
14 litComma = &Lit{Value: ","}
15 litDots = &Lit{Value: ".."}
16 litRightBrace = &Lit{Value: "}"}
17 )
18
19 // SplitBraces parses brace expansions within a word's literal parts.
20 // If any valid brace expansions are found, they are replaced with BraceExp nodes,
21 // and the function returns true.
22 // Otherwise, the word is left untouched and the function returns false.
23 //
24 // For example, a literal word "foo{bar,baz}" will result in a word containing
25 // the literal "foo", and a brace expansion with the elements "bar" and "baz".
26 //
27 // It does not return an error; malformed brace expansions are simply skipped.
28 // For example, the literal word "a{b" is left unchanged.
29 func SplitBraces(word *Word) bool {
30 if !slices.ContainsFunc(word.Parts, func(part WordPart) bool {
31 lit, ok := part.(*Lit)
32 return ok && strings.Contains(lit.Value, "{")
33 }) {
34 // In the common case where a word has no braces, skip any allocs.
35 return false
36 }
37 top := &Word{}
38 acc := top
39 var cur *BraceExp
40 var open []*BraceExp
41
42 pop := func() *BraceExp {
43 old := cur
44 open = open[:len(open)-1]
45 if len(open) == 0 {
46 cur = nil
47 acc = top
48 } else {
49 cur = open[len(open)-1]
50 acc = cur.Elems[len(cur.Elems)-1]
51 }
52 return old
53 }
54 addLit := func(lit *Lit) {
55 acc.Parts = append(acc.Parts, lit)
56 }
57
58 for _, wp := range word.Parts {
59 lit, ok := wp.(*Lit)
60 if !ok {
61 acc.Parts = append(acc.Parts, wp)
62 continue
63 }
64 last := 0
65 for j := 0; j < len(lit.Value); j++ {
66 addlitidx := func() {
67 if last == j {
68 return // empty lit
69 }
70 l2 := *lit
71 l2.Value = l2.Value[last:j]
72 addLit(&l2)
73 }
74 switch lit.Value[j] {
75 case '\\':
76 j++
77 continue
78 case '{':
79 addlitidx()
80 acc = &Word{}
81 cur = &BraceExp{Elems: []*Word{acc}}
82 open = append(open, cur)
83 case ',':
84 if cur == nil {
85 continue
86 }
87 addlitidx()
88 if cur.Sequence {
89 // A comma inside a sequence like {1..2,3} makes it
90 // a list expansion where the ".." are literal.
91 merged := cur.Elems[0]
92 for _, elem := range cur.Elems[1:] {
93 merged.Parts = append(merged.Parts, litDots)
94 merged.Parts = append(merged.Parts, elem.Parts...)
95 }
96 cur.Sequence = false
97 cur.Elems = cur.Elems[:1]
98 cur.Elems[0] = merged
99 }
100 acc = &Word{}
101 cur.Elems = append(cur.Elems, acc)
102 case '.':
103 if cur == nil {
104 continue
105 }
106 if j+1 >= len(lit.Value) || lit.Value[j+1] != '.' {
107 continue
108 }
109 if !cur.Sequence && len(cur.Elems) > 1 {
110 // ".." inside a list expansion like {1,2..3} is literal.
111 continue
112 }
113 addlitidx()
114 cur.Sequence = true
115 acc = &Word{}
116 cur.Elems = append(cur.Elems, acc)
117 j++
118 case '}':
119 if cur == nil {
120 continue
121 }
122 addlitidx()
123 br := pop()
124 if len(br.Elems) == 1 {
125 // return {x} to a non-brace
126 addLit(litLeftBrace)
127 acc.Parts = append(acc.Parts, br.Elems[0].Parts...)
128 addLit(litRightBrace)
129 break
130 }
131 if !br.Sequence {
132 acc.Parts = append(acc.Parts, br)
133 break
134 }
135 var chars [2]bool
136 broken := false
137 for i, elem := range br.Elems[:2] {
138 val := elem.Lit()
139 // ParseInt with bit size 64 to ensure consistent behavior on 32-bit platforms.
140 if _, err := strconv.ParseInt(val, 10, 64); err == nil {
141 } else if len(val) == 1 && asciiLetter(val[0]) {
142 chars[i] = true
143 } else {
144 broken = true
145 }
146 }
147 if len(br.Elems) == 3 {
148 // increment must be a number
149 val := br.Elems[2].Lit()
150 // ParseInt with bit size 64 to ensure consistent behavior on 32-bit platforms.
151 if _, err := strconv.ParseInt(val, 10, 64); err != nil {
152 broken = true
153 }
154 } else if len(br.Elems) > 3 {
155 // sequences like {1..2..3..4} are literal in bash
156 broken = true
157 }
158 // are start and end both chars or
159 // non-chars?
160 if chars[0] != chars[1] {
161 broken = true
162 }
163 if !broken {
164 acc.Parts = append(acc.Parts, br)
165 break
166 }
167 // return broken {x..y[..incr]} to a non-brace
168 addLit(litLeftBrace)
169 for i, elem := range br.Elems {
170 if i > 0 {
171 addLit(litDots)
172 }
173 acc.Parts = append(acc.Parts, elem.Parts...)
174 }
175 addLit(litRightBrace)
176 default:
177 continue
178 }
179 last = j + 1
180 }
181 if last == 0 {
182 addLit(lit)
183 } else {
184 left := *lit
185 left.Value = left.Value[last:]
186 addLit(&left)
187 }
188 }
189 // open braces that were never closed fall back to non-braces
190 for acc != top {
191 br := pop()
192 addLit(litLeftBrace)
193 for i, elem := range br.Elems {
194 if i > 0 {
195 if br.Sequence {
196 addLit(litDots)
197 } else {
198 addLit(litComma)
199 }
200 }
201 acc.Parts = append(acc.Parts, elem.Parts...)
202 }
203 }
204 *word = *top
205 return true
206 }