lang: Improve string interpolation

The original string interpolation was based on hil which didn't allow
proper escaping, since they used a different escape pattern. Secondly,
the golang Unquote function didn't deal with the variable substitution,
which meant it had to be performed in a second step.

Most importantly, because we did this partial job in Unquote (the fact
that is strips the leading and trailing quotes tricked me into thinking
I was done with interpolation!) it was impossible to remedy the
remaining parts in a second pass with hil. Both operations needs to be
done in a single step. This is logical when you aren't tunnel visioned.

This patch replaces both of these so that string interpolation works
properly. This removes the ability to allow inline function calls in a
string, however this was an incidental feature, and it's not clear that
having it is a good idea. It also requires you wrap the var name with
curly braces. (They are not optional.)

This comes with a load of tests, but I think I got some of it wrong,
since I'm quite new at ragel. If you find something, please say so =D In
any case, this is much better than the original hil implementation, and
easy for a new contributor to patch to make the necessary fixes.
This commit is contained in:
James Shubin
2020-01-31 11:47:06 -05:00
parent 5257496214
commit 400b58c0e9
36 changed files with 779 additions and 75 deletions

View File

@@ -24,6 +24,7 @@ import (
"go/token"
"log"
"os"
"regexp"
"strings"
"unicode"
)
@@ -250,6 +251,9 @@ func IsNewStart(word string) bool {
if word == "*" { // bullets
return true
}
if IsNumberBullet(word) {
return true
}
return false
}
@@ -269,3 +273,12 @@ func IsSpecialLine(line string) bool {
return false
}
// IsNumberBullet returns true if the word starts with a number bullet like 42).
func IsNumberBullet(word string) bool {
matched, err := regexp.MatchString(`[0-9]+\)*`, word)
if err != nil {
return false
}
return matched
}

View File

@@ -11,8 +11,9 @@ import "world"
$rand = random1(8)
$exchanged = world.exchange("keyns", $rand)
$host = sys.hostname()
file "/tmp/mgmt/exchange-${sys.hostname()}" {
file "/tmp/mgmt/exchange-${host}" {
state => $const.res.file.state.exists,
content => template("Found: {{ . }}\n", $exchanged),
}

View File

@@ -49,6 +49,7 @@ gml="$gml --exclude=lang/lexer.nn.go"
gml="$gml --exclude=lang/y.go"
gml="$gml --exclude=bindata/bindata.go"
gml="$gml --exclude=lang/types/kind_stringer.go"
gml="$gml --exclude=lang/interpolate/parse.generated.go"
gometalinter="$gml" # final
echo "Using: $gometalinter"

View File

@@ -113,6 +113,10 @@ function reflowed-comments() {
return 0
fi
if [ "$1" = './lang/interpolate/parse.generated.go' ]; then
return 0
fi
./test/comment_parser "$1"
}

View File

@@ -16,7 +16,7 @@ while IFS='' read -r line; do # find what header should look like
done < "$FILE"
find_files() {
git ls-files | grep '\.go$' | grep -v '^examples/' | grep -v '^test/'
git ls-files | grep -E '\.go$|\.rl$' | grep -v '^examples/' | grep -v '^test/'
}
bad_files=$(