Colin Cross | d00350c | 2017-11-17 10:55:38 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 19 | "unicode" |
| 20 | ) |
| 21 | |
| 22 | // A MakeString is a string that may contain variable substitutions in it. |
| 23 | // It can be considered as an alternating list of raw Strings and variable |
| 24 | // substitutions, where the first and last entries in the list must be raw |
| 25 | // Strings (possibly empty). A MakeString that starts with a variable |
| 26 | // will have an empty first raw string, and a MakeString that ends with a |
| 27 | // variable will have an empty last raw string. Two sequential Variables |
| 28 | // will have an empty raw string between them. |
| 29 | // |
| 30 | // The MakeString is stored as two lists, a list of raw Strings and a list |
| 31 | // of Variables. The raw string list is always one longer than the variable |
| 32 | // list. |
| 33 | type MakeString struct { |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 34 | StringPos Pos |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 35 | Strings []string |
| 36 | Variables []Variable |
| 37 | } |
| 38 | |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 39 | func SimpleMakeString(s string, pos Pos) *MakeString { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 40 | return &MakeString{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 41 | StringPos: pos, |
| 42 | Strings: []string{s}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Jeff Gaston | fd4ce1b | 2017-05-31 15:44:17 -0700 | [diff] [blame] | 46 | func (ms *MakeString) Clone() (result *MakeString) { |
| 47 | clone := *ms |
| 48 | return &clone |
| 49 | } |
| 50 | |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 51 | func (ms *MakeString) Pos() Pos { |
| 52 | return ms.StringPos |
| 53 | } |
| 54 | |
| 55 | func (ms *MakeString) End() Pos { |
| 56 | pos := ms.StringPos |
| 57 | if len(ms.Strings) > 1 { |
| 58 | pos = ms.Variables[len(ms.Variables)-1].End() |
| 59 | } |
| 60 | return Pos(int(pos) + len(ms.Strings[len(ms.Strings)-1])) |
| 61 | } |
| 62 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 63 | func (ms *MakeString) appendString(s string) { |
| 64 | if len(ms.Strings) == 0 { |
| 65 | ms.Strings = []string{s} |
| 66 | return |
| 67 | } else { |
| 68 | ms.Strings[len(ms.Strings)-1] += s |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func (ms *MakeString) appendVariable(v Variable) { |
| 73 | if len(ms.Strings) == 0 { |
| 74 | ms.Strings = []string{"", ""} |
| 75 | ms.Variables = []Variable{v} |
| 76 | } else { |
| 77 | ms.Strings = append(ms.Strings, "") |
| 78 | ms.Variables = append(ms.Variables, v) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (ms *MakeString) appendMakeString(other *MakeString) { |
| 83 | last := len(ms.Strings) - 1 |
| 84 | ms.Strings[last] += other.Strings[0] |
| 85 | ms.Strings = append(ms.Strings, other.Strings[1:]...) |
| 86 | ms.Variables = append(ms.Variables, other.Variables...) |
| 87 | } |
| 88 | |
| 89 | func (ms *MakeString) Value(scope Scope) string { |
| 90 | if len(ms.Strings) == 0 { |
| 91 | return "" |
| 92 | } else { |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 93 | ret := unescape(ms.Strings[0]) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 94 | for i := range ms.Strings[1:] { |
| 95 | ret += ms.Variables[i].Value(scope) |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 96 | ret += unescape(ms.Strings[i+1]) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 97 | } |
| 98 | return ret |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func (ms *MakeString) Dump() string { |
| 103 | if len(ms.Strings) == 0 { |
| 104 | return "" |
| 105 | } else { |
| 106 | ret := ms.Strings[0] |
| 107 | for i := range ms.Strings[1:] { |
| 108 | ret += ms.Variables[i].Dump() |
| 109 | ret += ms.Strings[i+1] |
| 110 | } |
| 111 | return ret |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func (ms *MakeString) Const() bool { |
| 116 | return len(ms.Strings) <= 1 |
| 117 | } |
| 118 | |
| 119 | func (ms *MakeString) Empty() bool { |
| 120 | return len(ms.Strings) == 0 || (len(ms.Strings) == 1 && ms.Strings[0] == "") |
| 121 | } |
| 122 | |
| 123 | func (ms *MakeString) Split(sep string) []*MakeString { |
| 124 | return ms.SplitN(sep, -1) |
| 125 | } |
| 126 | |
| 127 | func (ms *MakeString) SplitN(sep string, n int) []*MakeString { |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 128 | return ms.splitNFunc(n, func(s string, n int) []string { |
| 129 | return splitAnyN(s, sep, n) |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | func (ms *MakeString) Words() []*MakeString { |
| 134 | return ms.splitNFunc(-1, splitWords) |
| 135 | } |
| 136 | |
| 137 | func (ms *MakeString) splitNFunc(n int, splitFunc func(s string, n int) []string) []*MakeString { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 138 | ret := []*MakeString{} |
| 139 | |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 140 | curMs := SimpleMakeString("", ms.Pos()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 141 | |
| 142 | var i int |
| 143 | var s string |
| 144 | for i, s = range ms.Strings { |
| 145 | if n != 0 { |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 146 | split := splitFunc(s, n) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 147 | if n != -1 { |
| 148 | if len(split) > n { |
| 149 | panic("oops!") |
| 150 | } else { |
| 151 | n -= len(split) |
| 152 | } |
| 153 | } |
| 154 | curMs.appendString(split[0]) |
| 155 | |
| 156 | for _, r := range split[1:] { |
| 157 | ret = append(ret, curMs) |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 158 | curMs = SimpleMakeString(r, ms.Pos()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 159 | } |
| 160 | } else { |
| 161 | curMs.appendString(s) |
| 162 | } |
| 163 | |
| 164 | if i < len(ms.Strings)-1 { |
| 165 | curMs.appendVariable(ms.Variables[i]) |
| 166 | } |
| 167 | } |
| 168 | |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 169 | if !curMs.Empty() { |
| 170 | ret = append(ret, curMs) |
| 171 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 172 | return ret |
| 173 | } |
| 174 | |
| 175 | func (ms *MakeString) TrimLeftSpaces() { |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 176 | l := len(ms.Strings[0]) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 177 | ms.Strings[0] = strings.TrimLeftFunc(ms.Strings[0], unicode.IsSpace) |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 178 | ms.StringPos += Pos(len(ms.Strings[0]) - l) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | func (ms *MakeString) TrimRightSpaces() { |
| 182 | last := len(ms.Strings) - 1 |
| 183 | ms.Strings[last] = strings.TrimRightFunc(ms.Strings[last], unicode.IsSpace) |
| 184 | } |
| 185 | |
| 186 | func (ms *MakeString) TrimRightOne() { |
| 187 | last := len(ms.Strings) - 1 |
| 188 | if len(ms.Strings[last]) > 1 { |
| 189 | ms.Strings[last] = ms.Strings[last][0 : len(ms.Strings[last])-1] |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func (ms *MakeString) EndsWith(ch rune) bool { |
| 194 | s := ms.Strings[len(ms.Strings)-1] |
| 195 | return s[len(s)-1] == uint8(ch) |
| 196 | } |
| 197 | |
Jeff Gaston | fd4ce1b | 2017-05-31 15:44:17 -0700 | [diff] [blame] | 198 | func (ms *MakeString) ReplaceLiteral(input string, output string) { |
| 199 | for i := range ms.Strings { |
| 200 | ms.Strings[i] = strings.Replace(ms.Strings[i], input, output, -1) |
| 201 | } |
| 202 | } |
| 203 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 204 | func splitAnyN(s, sep string, n int) []string { |
| 205 | ret := []string{} |
| 206 | for n == -1 || n > 1 { |
| 207 | index := strings.IndexAny(s, sep) |
| 208 | if index >= 0 { |
| 209 | ret = append(ret, s[0:index]) |
| 210 | s = s[index+1:] |
| 211 | if n > 0 { |
| 212 | n-- |
| 213 | } |
| 214 | } else { |
| 215 | break |
| 216 | } |
| 217 | } |
| 218 | ret = append(ret, s) |
| 219 | return ret |
| 220 | } |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 221 | |
| 222 | func splitWords(s string, n int) []string { |
| 223 | ret := []string{} |
| 224 | preserve := "" |
| 225 | for n == -1 || n > 1 { |
| 226 | index := strings.IndexAny(s, " \t") |
| 227 | if index == 0 && len(preserve) == 0 { |
| 228 | s = s[1:] |
| 229 | } else if index >= 0 { |
| 230 | escapeCount := 0 |
| 231 | for i := index - 1; i >= 0; i-- { |
| 232 | if s[i] != '\\' { |
| 233 | break |
| 234 | } |
| 235 | escapeCount += 1 |
| 236 | } |
| 237 | |
| 238 | if escapeCount%2 == 1 { |
| 239 | preserve += s[0 : index+1] |
| 240 | s = s[index+1:] |
| 241 | continue |
| 242 | } |
| 243 | |
| 244 | ret = append(ret, preserve+s[0:index]) |
| 245 | s = s[index+1:] |
| 246 | preserve = "" |
| 247 | if n > 0 { |
| 248 | n-- |
| 249 | } |
| 250 | } else { |
| 251 | break |
| 252 | } |
| 253 | } |
| 254 | if preserve != "" || s != "" || len(ret) == 0 { |
| 255 | ret = append(ret, preserve+s) |
| 256 | } |
| 257 | return ret |
| 258 | } |
| 259 | |
| 260 | func unescape(s string) string { |
| 261 | ret := "" |
| 262 | for { |
| 263 | index := strings.IndexByte(s, '\\') |
| 264 | if index < 0 { |
| 265 | break |
| 266 | } |
| 267 | |
| 268 | if index+1 == len(s) { |
| 269 | break |
| 270 | } |
| 271 | |
| 272 | switch s[index+1] { |
| 273 | case ' ', '\\', '#', ':', '*', '[', '|', '\t', '\n', '\r': |
| 274 | ret += s[:index] + s[index+1:index+2] |
| 275 | default: |
| 276 | ret += s[:index+2] |
| 277 | } |
| 278 | s = s[index+2:] |
| 279 | } |
| 280 | return ret + s |
| 281 | } |