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

@@ -772,8 +772,7 @@ func TestAstFunc1(t *testing.T) {
return
}
if fail1 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -839,8 +838,7 @@ func TestAstFunc1(t *testing.T) {
return
}
if fail2 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -872,8 +870,7 @@ func TestAstFunc1(t *testing.T) {
return
}
if fail3 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -897,8 +894,7 @@ func TestAstFunc1(t *testing.T) {
return
}
if fail4 && err != nil { // can't process graph if it's nil
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -977,6 +973,7 @@ func TestAstFunc2(t *testing.T) {
const magicError = "# err: "
const magicError1 = "err1: "
const magicError9 = "err9: " // TODO: rename
const magicError8 = "err8: " // TODO: rename
// TODO: move them all down by one
const magicError2 = "err2: "
const magicError3 = "err3: "
@@ -1013,6 +1010,7 @@ func TestAstFunc2(t *testing.T) {
type errs struct {
fail1 bool
fail9 bool // TODO: rename
fail8 bool // TODO: rename
// TODO: move them all down by one
fail2 bool
fail3 bool
@@ -1073,6 +1071,7 @@ func TestAstFunc2(t *testing.T) {
errStr := ""
fail1 := false
fail9 := false // TODO: rename
fail8 := false // TODO: rename
// TODO: move them all down by one
fail2 := false
fail3 := false
@@ -1093,6 +1092,11 @@ func TestAstFunc2(t *testing.T) {
str = errStr
fail9 = true
}
if strings.HasPrefix(str, magicError8) { // TODO: rename
errStr = strings.TrimPrefix(str, magicError8)
str = errStr
fail8 = true
}
// TODO: move them all down by one
if strings.HasPrefix(str, magicError2) {
errStr = strings.TrimPrefix(str, magicError2)
@@ -1130,6 +1134,7 @@ func TestAstFunc2(t *testing.T) {
errs: errs{
fail1: fail1,
fail9: fail9, // TODO: rename
fail8: fail8, // TODO: rename
// TODO: move them all down by one
fail2: fail2,
fail3: fail3,
@@ -1171,6 +1176,7 @@ func TestAstFunc2(t *testing.T) {
src := dir + path // location of the test
fail1 := errs.fail1
fail9 := errs.fail9 // TODO: rename
fail8 := errs.fail8 // TODO: rename
// TODO: move them all down by one
fail2 := errs.fail2
fail3 := errs.fail3
@@ -1252,8 +1258,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail1 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -1305,8 +1310,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail9 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -1322,9 +1326,24 @@ func TestAstFunc2(t *testing.T) {
}
iast, err := ast.Interpolate()
if err != nil {
if (!fail || !fail8) && err != nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: interpolate failed with: %+v", index, err)
t.Errorf("test #%d: Interpolate failed with: %+v", index, err)
return
}
if fail8 && err != nil {
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
t.Logf("test #%d: err: %s", index, s)
t.Logf("test #%d: exp: %s", index, expstr)
}
return // fail happened during lex parse, don't run init/interpolate!
}
if fail8 && err == nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: Interpolate passed, expected fail", index)
return
}
@@ -1336,8 +1355,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail2 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -1369,8 +1387,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail3 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -1394,8 +1411,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail4 && err != nil { // can't process graph if it's nil
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -1501,8 +1517,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail5 && err != nil { // can't process graph if it's nil
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
@@ -1525,8 +1540,7 @@ func TestAstFunc2(t *testing.T) {
return
}
if fail6 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
s := err.Error() // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)