| 1 |
// Copyright (c) 2017, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package syntax |
| 5 |
|
| 6 |
import "strings" |
| 7 |
|
| 8 |
// Simplify modifies a node to remove redundant pieces of syntax, and returns |
| 9 |
// whether any changes were made. |
| 10 |
// |
| 11 |
// The changes currently applied are: |
| 12 |
// |
| 13 |
// Remove clearly useless parentheses $(( (expr) )) |
| 14 |
// Remove dollars from vars in exprs (($var)) |
| 15 |
// Remove duplicate subshells $( (stmts) ) |
| 16 |
// Remove redundant quotes [[ "$var" == str ]] |
| 17 |
// Merge negations with unary operators [[ ! -n $var ]] |
| 18 |
// Use single quotes to shorten literals "\$foo" |
| 19 |
func Simplify(n Node) bool { |
| 20 |
s := simplifier{keepParams: make(map[ArithmExpr]bool)} |
| 21 |
for node := range Preorder(n) { |
| 22 |
s.visit(node) |
| 23 |
} |
| 24 |
return s.modified |
| 25 |
} |
| 26 |
|
| 27 |
type simplifier struct { |
| 28 |
modified bool |
| 29 |
|
| 30 |
// keepParams holds arithmetic nodes within an array index; |
| 31 |
// see [simplifier.markIndex]. |
| 32 |
keepParams map[ArithmExpr]bool |
| 33 |
} |
| 34 |
|
| 35 |
// markIndex records an array index so that its parameters are not inlined, |
| 36 |
// as x[i] and x[$i] differ when x is an associative array. |
| 37 |
// Nested arithmetic expansions like x[$((i))] are left out, |
| 38 |
// as those are always arithmetic and never string keys. |
| 39 |
func (s *simplifier) markIndex(expr ArithmExpr) { |
| 40 |
switch expr := expr.(type) { |
| 41 |
case *BinaryArithm: |
| 42 |
s.keepParams[expr] = true |
| 43 |
s.markIndex(expr.X) |
| 44 |
s.markIndex(expr.Y) |
| 45 |
case *ParenArithm: |
| 46 |
s.keepParams[expr] = true |
| 47 |
s.markIndex(expr.X) |
| 48 |
case *UnaryArithm: |
| 49 |
// visit never inlines the parameters of a unary expression. |
| 50 |
s.markIndex(expr.X) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func (s *simplifier) visit(node Node) { |
| 55 |
switch node := node.(type) { |
| 56 |
case *Assign: |
| 57 |
node.Index = s.removeParensArithm(node.Index) |
| 58 |
s.markIndex(node.Index) |
| 59 |
case *ArrayElem: |
| 60 |
s.markIndex(node.Index) |
| 61 |
case *ParamExp: |
| 62 |
node.Index = s.removeParensArithm(node.Index) |
| 63 |
s.markIndex(node.Index) |
| 64 |
|
| 65 |
if node.Slice == nil { |
| 66 |
break |
| 67 |
} |
| 68 |
node.Slice.Offset = s.removeParensArithm(node.Slice.Offset) |
| 69 |
node.Slice.Offset = s.inlineSimpleParams(node.Slice.Offset) |
| 70 |
node.Slice.Length = s.removeParensArithm(node.Slice.Length) |
| 71 |
node.Slice.Length = s.inlineSimpleParams(node.Slice.Length) |
| 72 |
case *ArithmExp: |
| 73 |
node.X = s.removeParensArithm(node.X) |
| 74 |
node.X = s.inlineSimpleParams(node.X) |
| 75 |
case *ArithmCmd: |
| 76 |
node.X = s.removeParensArithm(node.X) |
| 77 |
node.X = s.inlineSimpleParams(node.X) |
| 78 |
case *ParenArithm: |
| 79 |
node.X = s.removeParensArithm(node.X) |
| 80 |
if !s.keepParams[node] { |
| 81 |
node.X = s.inlineSimpleParams(node.X) |
| 82 |
} |
| 83 |
case *BinaryArithm: |
| 84 |
if !s.keepParams[node] { |
| 85 |
node.X = s.inlineSimpleParams(node.X) |
| 86 |
node.Y = s.inlineSimpleParams(node.Y) |
| 87 |
} |
| 88 |
case *CmdSubst: |
| 89 |
node.Stmts = s.inlineSubshell(node.Stmts) |
| 90 |
case *Subshell: |
| 91 |
node.Stmts = s.inlineSubshell(node.Stmts) |
| 92 |
case *Word: |
| 93 |
node.Parts = s.simplifyWord(node.Parts) |
| 94 |
case *TestClause: |
| 95 |
node.X = s.removeParensTest(node.X) |
| 96 |
node.X = s.removeNegateTest(node.X) |
| 97 |
case *ParenTest: |
| 98 |
node.X = s.removeParensTest(node.X) |
| 99 |
node.X = s.removeNegateTest(node.X) |
| 100 |
case *BinaryTest: |
| 101 |
node.X = s.unquoteParams(node.X) |
| 102 |
node.X = s.removeNegateTest(node.X) |
| 103 |
if node.Op == TsMatchShort { |
| 104 |
s.modified = true |
| 105 |
node.Op = TsMatch |
| 106 |
} |
| 107 |
switch node.Op { |
| 108 |
case TsMatch, TsNoMatch: |
| 109 |
// unquoting enables globbing |
| 110 |
case TsReMatch: |
| 111 |
// unquoting turns a literal string into a regular expression |
| 112 |
default: |
| 113 |
node.Y = s.unquoteParams(node.Y) |
| 114 |
} |
| 115 |
node.Y = s.removeNegateTest(node.Y) |
| 116 |
case *UnaryTest: |
| 117 |
node.X = s.unquoteParams(node.X) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func (s *simplifier) simplifyWord(wps []WordPart) []WordPart { |
| 122 |
parts: |
| 123 |
for i, wp := range wps { |
| 124 |
dq, _ := wp.(*DblQuoted) |
| 125 |
if dq == nil || len(dq.Parts) != 1 { |
| 126 |
break |
| 127 |
} |
| 128 |
lit, _ := dq.Parts[0].(*Lit) |
| 129 |
if lit == nil { |
| 130 |
break |
| 131 |
} |
| 132 |
var sb strings.Builder |
| 133 |
escaped := false |
| 134 |
for _, r := range lit.Value { |
| 135 |
switch r { |
| 136 |
case '\\': |
| 137 |
escaped = !escaped |
| 138 |
if escaped { |
| 139 |
continue |
| 140 |
} |
| 141 |
case '\'': |
| 142 |
continue parts |
| 143 |
case '$', '"', '`': |
| 144 |
escaped = false |
| 145 |
default: |
| 146 |
if escaped { |
| 147 |
continue parts |
| 148 |
} |
| 149 |
escaped = false |
| 150 |
} |
| 151 |
sb.WriteRune(r) |
| 152 |
} |
| 153 |
newVal := sb.String() |
| 154 |
if newVal == lit.Value { |
| 155 |
break |
| 156 |
} |
| 157 |
s.modified = true |
| 158 |
wps[i] = &SglQuoted{ |
| 159 |
Left: dq.Pos(), |
| 160 |
Right: dq.End(), |
| 161 |
Dollar: dq.Dollar, |
| 162 |
Value: newVal, |
| 163 |
} |
| 164 |
} |
| 165 |
return wps |
| 166 |
} |
| 167 |
|
| 168 |
func (s *simplifier) removeParensArithm(x ArithmExpr) ArithmExpr { |
| 169 |
for { |
| 170 |
par, _ := x.(*ParenArithm) |
| 171 |
if par == nil { |
| 172 |
return x |
| 173 |
} |
| 174 |
s.modified = true |
| 175 |
x = par.X |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func (s *simplifier) inlineSimpleParams(x ArithmExpr) ArithmExpr { |
| 180 |
w, _ := x.(*Word) |
| 181 |
if w == nil || len(w.Parts) != 1 { |
| 182 |
return x |
| 183 |
} |
| 184 |
pe, _ := w.Parts[0].(*ParamExp) |
| 185 |
if pe == nil || pe.Param == nil || !ValidName(pe.Param.Value) { |
| 186 |
// Not a POSIX-like parameter expansion, or not a valid name without `$`, like $3. |
| 187 |
return x |
| 188 |
} |
| 189 |
if !pe.simple() { |
| 190 |
// A complex parameter expansion can't be simplified. |
| 191 |
// |
| 192 |
// Note that index expressions can't generally be simplified |
| 193 |
// either. It's fine to turn ${a[0]} into a[0], but others like |
| 194 |
// a[*] are invalid in many shells including Bash. |
| 195 |
return x |
| 196 |
} |
| 197 |
s.modified = true |
| 198 |
return &Word{Parts: []WordPart{pe.Param}} |
| 199 |
} |
| 200 |
|
| 201 |
func (s *simplifier) inlineSubshell(stmts []*Stmt) []*Stmt { |
| 202 |
for len(stmts) == 1 { |
| 203 |
st := stmts[0] |
| 204 |
if st.Negated || st.Background || st.Coprocess || st.Disown || |
| 205 |
len(st.Redirs) > 0 { |
| 206 |
break |
| 207 |
} |
| 208 |
sub, _ := st.Cmd.(*Subshell) |
| 209 |
if sub == nil { |
| 210 |
break |
| 211 |
} |
| 212 |
s.modified = true |
| 213 |
stmts = sub.Stmts |
| 214 |
} |
| 215 |
return stmts |
| 216 |
} |
| 217 |
|
| 218 |
func (s *simplifier) unquoteParams(x TestExpr) TestExpr { |
| 219 |
w, _ := x.(*Word) |
| 220 |
if w == nil || len(w.Parts) != 1 { |
| 221 |
return x |
| 222 |
} |
| 223 |
dq, _ := w.Parts[0].(*DblQuoted) |
| 224 |
if dq == nil || len(dq.Parts) != 1 { |
| 225 |
return x |
| 226 |
} |
| 227 |
if _, ok := dq.Parts[0].(*ParamExp); !ok { |
| 228 |
return x |
| 229 |
} |
| 230 |
s.modified = true |
| 231 |
w.Parts = dq.Parts |
| 232 |
return w |
| 233 |
} |
| 234 |
|
| 235 |
func (s *simplifier) removeParensTest(x TestExpr) TestExpr { |
| 236 |
for { |
| 237 |
par, _ := x.(*ParenTest) |
| 238 |
if par == nil { |
| 239 |
return x |
| 240 |
} |
| 241 |
s.modified = true |
| 242 |
x = par.X |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func (s *simplifier) removeNegateTest(x TestExpr) TestExpr { |
| 247 |
u, _ := x.(*UnaryTest) |
| 248 |
if u == nil || u.Op != TsNot { |
| 249 |
return x |
| 250 |
} |
| 251 |
switch y := u.X.(type) { |
| 252 |
case *UnaryTest: |
| 253 |
switch y.Op { |
| 254 |
case TsEmpStr: |
| 255 |
y.Op = TsNempStr |
| 256 |
s.modified = true |
| 257 |
return y |
| 258 |
case TsNempStr: |
| 259 |
y.Op = TsEmpStr |
| 260 |
s.modified = true |
| 261 |
return y |
| 262 |
case TsNot: |
| 263 |
s.modified = true |
| 264 |
return y.X |
| 265 |
} |
| 266 |
case *BinaryTest: |
| 267 |
switch y.Op { |
| 268 |
case TsMatch: |
| 269 |
y.Op = TsNoMatch |
| 270 |
s.modified = true |
| 271 |
return y |
| 272 |
case TsNoMatch: |
| 273 |
y.Op = TsMatch |
| 274 |
s.modified = true |
| 275 |
return y |
| 276 |
} |
| 277 |
} |
| 278 |
return x |
| 279 |
} |