Support multiline string literals in HCL

This allows multiline strings to be parsed in order to make HIL
interpolations nicer in Terraform:

```
my_complex_thing = "${merge(var.list1,
                            var.list2,
                            var.list3)}"
```
This commit is contained in:
James Nugent 2016-07-11 14:31:33 -06:00
parent 364df43084
commit a55c206bd0
6 changed files with 11 additions and 8 deletions

View File

@ -79,6 +79,12 @@ func TestDecode_interface(t *testing.T) {
true,
nil,
},
{
"multiline_literal.hcl",
false,
map[string]interface{}{"multiline_literal": testhelper.Unix2dos(`hello
world`)},
},
{
"multiline_no_marker.hcl",
true,

View File

@ -469,7 +469,7 @@ func (s *Scanner) scanString() {
// read character after quote
ch := s.next()
if ch == '\n' || ch < 0 || ch == eof {
if ch < 0 || ch == eof {
s.err("literal not terminated")
return
}

View File

@ -10,7 +10,7 @@ import (
"github.com/hashicorp/hcl/hcl/token"
)
var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
var f100 = strings.Repeat("f", 100)
type tokenPair struct {
tok token.Type
@ -494,7 +494,7 @@ func TestError(t *testing.T) {
testError(t, `"`, "1:2", "literal not terminated", token.STRING)
testError(t, `"abc`, "1:5", "literal not terminated", token.STRING)
testError(t, `"abc`+"\n", "1:5", "literal not terminated", token.STRING)
testError(t, `"abc`+"\n", "2:1", "literal not terminated", token.STRING)
testError(t, `/*/`, "1:4", "comment not terminated", token.COMMENT)
}

View File

@ -27,9 +27,6 @@ func Unquote(s string) (t string, err error) {
if quote != '"' {
return "", ErrSyntax
}
if contains(s, '\n') {
return "", ErrSyntax
}
// Is it trivial? Avoid allocation.
if !contains(s, '\\') && !contains(s, quote) && !contains(s, '$') {

View File

@ -65,8 +65,6 @@ var misquoted = []string{
"`\"",
`"\'"`,
`'\"'`,
"\"\n\"",
"\"\\n\n\"",
"'\n'",
`"${"`,
`"${foo{}"`,

View File

@ -0,0 +1,2 @@
multiline_literal = "hello
world"