go.uber.org/yarpc@v1.72.1/internal/interpolate/parse.go (about) 1 // Code generated by ragel 2 // @generated 3 4 // Copyright (c) 2022 Uber Technologies, Inc. 5 // 6 // Permission is hereby granted, free of charge, to any person obtaining a copy 7 // of this software and associated documentation files (the "Software"), to deal 8 // in the Software without restriction, including without limitation the rights 9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 // copies of the Software, and to permit persons to whom the Software is 11 // furnished to do so, subject to the following conditions: 12 // 13 // The above copyright notice and this permission notice shall be included in 14 // all copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 // THE SOFTWARE. 23 24 package interpolate 25 26 import "fmt" 27 28 const interpolate_start int = 8 29 const interpolate_first_final int = 8 30 const interpolate_error int = 0 31 32 const interpolate_en_main int = 8 33 34 35 // Parse parses a string for interpolation. 36 // 37 // Variables may be specified anywhere in the string in the format ${foo} or 38 // ${foo:default} where 'default' will be used if the variable foo was unset. 39 func Parse(data string) (out String, _ error) { 40 var ( 41 // Variables used by Ragel 42 cs = 0 // current state 43 p = 0 // current position in data 44 pe = len(data) 45 eof = pe // eof == pe if this is the last data block 46 47 // We use the following variables to actually build the String. 48 49 // Index in data where the currently captured string started. 50 idx int 51 52 v variable // variable being read, if any 53 l literal // literal being read, if any 54 55 // Current term. This is either the variable that we just read or the 56 // literal. We will append it to `out` and move on. 57 t term 58 ) 59 60 { 61 cs = interpolate_start 62 } 63 64 { 65 if p == pe { 66 goto _test_eof 67 } 68 switch cs { 69 case 8: 70 goto st_case_8 71 case 9: 72 goto st_case_9 73 case 1: 74 goto st_case_1 75 case 10: 76 goto st_case_10 77 case 2: 78 goto st_case_2 79 case 3: 80 goto st_case_3 81 case 0: 82 goto st_case_0 83 case 4: 84 goto st_case_4 85 case 5: 86 goto st_case_5 87 case 6: 88 goto st_case_6 89 case 7: 90 goto st_case_7 91 } 92 goto st_out 93 st_case_8: 94 switch data[p] { 95 case 36: 96 goto st1 97 case 92: 98 goto st2 99 } 100 goto tr12 101 tr12: 102 idx = p 103 l = literal(data[idx : p+1]) 104 t = l 105 goto st9 106 tr15: 107 l = literal(data[idx : p+1]) 108 t = l 109 goto st9 110 tr18: 111 out = append(out, t) 112 idx = p 113 l = literal(data[idx : p+1]) 114 t = l 115 goto st9 116 st9: 117 if p++; p == pe { 118 goto _test_eof9 119 } 120 st_case_9: 121 switch data[p] { 122 case 36: 123 goto tr16 124 case 92: 125 goto tr17 126 } 127 goto tr15 128 tr16: 129 out = append(out, t) 130 goto st1 131 st1: 132 if p++; p == pe { 133 goto _test_eof1 134 } 135 st_case_1: 136 if data[p] == 123 { 137 goto st3 138 } 139 goto tr0 140 tr0: 141 l = literal(data[p-1 : p+1]) 142 t = l 143 goto st10 144 tr2: 145 l = literal(data[p : p+1]) 146 t = l 147 goto st10 148 tr8: 149 t = v 150 goto st10 151 tr10: 152 idx = p 153 t = v 154 goto st10 155 st10: 156 if p++; p == pe { 157 goto _test_eof10 158 } 159 st_case_10: 160 switch data[p] { 161 case 36: 162 goto tr16 163 case 92: 164 goto tr17 165 } 166 goto tr18 167 tr17: 168 out = append(out, t) 169 goto st2 170 st2: 171 if p++; p == pe { 172 goto _test_eof2 173 } 174 st_case_2: 175 goto tr2 176 st3: 177 if p++; p == pe { 178 goto _test_eof3 179 } 180 st_case_3: 181 if data[p] == 95 { 182 goto tr3 183 } 184 switch { 185 case data[p] > 90: 186 if 97 <= data[p] && data[p] <= 122 { 187 goto tr3 188 } 189 case data[p] >= 65: 190 goto tr3 191 } 192 goto st0 193 st_case_0: 194 st0: 195 cs = 0 196 goto _out 197 tr3: 198 idx = p 199 v.Name = data[idx : p+1] 200 goto st4 201 tr6: 202 v.Name = data[idx : p+1] 203 goto st4 204 st4: 205 if p++; p == pe { 206 goto _test_eof4 207 } 208 st_case_4: 209 switch data[p] { 210 case 58: 211 goto tr7 212 case 95: 213 goto tr6 214 case 125: 215 goto tr8 216 } 217 switch { 218 case data[p] < 48: 219 if 45 <= data[p] && data[p] <= 46 { 220 goto st5 221 } 222 case data[p] > 57: 223 switch { 224 case data[p] > 90: 225 if 97 <= data[p] && data[p] <= 122 { 226 goto tr6 227 } 228 case data[p] >= 65: 229 goto tr6 230 } 231 default: 232 goto tr6 233 } 234 goto st0 235 st5: 236 if p++; p == pe { 237 goto _test_eof5 238 } 239 st_case_5: 240 if data[p] == 95 { 241 goto tr6 242 } 243 switch { 244 case data[p] < 65: 245 if 48 <= data[p] && data[p] <= 57 { 246 goto tr6 247 } 248 case data[p] > 90: 249 if 97 <= data[p] && data[p] <= 122 { 250 goto tr6 251 } 252 default: 253 goto tr6 254 } 255 goto st0 256 tr7: 257 v.HasDefault = true 258 goto st6 259 st6: 260 if p++; p == pe { 261 goto _test_eof6 262 } 263 st_case_6: 264 if data[p] == 125 { 265 goto tr10 266 } 267 goto tr9 268 tr9: 269 idx = p 270 v.Default = data[idx : p+1] 271 goto st7 272 tr11: 273 v.Default = data[idx : p+1] 274 goto st7 275 st7: 276 if p++; p == pe { 277 goto _test_eof7 278 } 279 st_case_7: 280 if data[p] == 125 { 281 goto tr8 282 } 283 goto tr11 284 st_out: 285 _test_eof9: 286 cs = 9 287 goto _test_eof 288 _test_eof1: 289 cs = 1 290 goto _test_eof 291 _test_eof10: 292 cs = 10 293 goto _test_eof 294 _test_eof2: 295 cs = 2 296 goto _test_eof 297 _test_eof3: 298 cs = 3 299 goto _test_eof 300 _test_eof4: 301 cs = 4 302 goto _test_eof 303 _test_eof5: 304 cs = 5 305 goto _test_eof 306 _test_eof6: 307 cs = 6 308 goto _test_eof 309 _test_eof7: 310 cs = 7 311 goto _test_eof 312 313 _test_eof: 314 { 315 } 316 if p == eof { 317 switch cs { 318 case 9, 10: 319 out = append(out, t) 320 } 321 } 322 323 _out: 324 { 325 } 326 } 327 328 329 if cs < 8 { 330 return out, fmt.Errorf("cannot parse string %q", data) 331 } 332 333 return out, nil 334 }