in internal/pkg/agent/transpiler/vars.go [233:297]
func extractVars(i string, defaultProvider string) ([]varI, error) {
const out = rune(0)
quote := out
constant := false
escape := false
is := make([]rune, 0, len(i))
res := make([]varI, 0)
for _, r := range i {
if r == '|' {
if escape {
return nil, fmt.Errorf(`variable pipe cannot be escaped; remove \ before |`)
}
if quote == out {
if constant {
res = append(res, &constString{string(is)})
} else if len(is) > 0 {
if is[len(is)-1] == '.' {
return nil, fmt.Errorf("variable cannot end with '.'")
}
res = append(res, &varString{maybeAddDefaultProvider(string(is), defaultProvider)})
}
is = is[:0] // slice to zero length; to keep allocated memory
constant = false
} else {
is = append(is, r)
}
continue
}
if !escape && (r == '"' || r == '\'') {
if quote == out {
// start of unescaped quote
quote = r
constant = true
} else if quote == r {
// end of unescaped quote
quote = out
} else {
is = append(is, r)
}
continue
}
// escape because of backslash (\); except when it is the second backslash of a pair
escape = !escape && r == '\\'
if r == '\\' {
if !escape {
is = append(is, r)
}
} else if quote != out || !unicode.IsSpace(r) {
is = append(is, r)
}
}
if quote != out {
return nil, fmt.Errorf(`starting %s is missing ending %s`, string(quote), string(quote))
}
if constant {
res = append(res, &constString{string(is)})
} else if len(is) > 0 {
if is[len(is)-1] == '.' {
return nil, fmt.Errorf("variable cannot end with '.'")
}
res = append(res, &varString{maybeAddDefaultProvider(string(is), defaultProvider)})
}
return res, nil
}