From 22bdcc2db11ec9553f0f0dedf0b384cab7d43d50 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 6 Nov 2015 17:55:07 -0800 Subject: [PATCH] hcl/scanner: scan interpolations properly --- hcl/scanner/scanner.go | 14 +++++++++++++- hcl/scanner/scanner_test.go | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hcl/scanner/scanner.go b/hcl/scanner/scanner.go index 32a1864..87003f6 100644 --- a/hcl/scanner/scanner.go +++ b/hcl/scanner/scanner.go @@ -375,6 +375,7 @@ func (s *Scanner) scanExponent(ch rune) rune { // scanString scans a quoted string func (s *Scanner) scanString() { + braces := 0 for { // '"' opening already consumed // read character after quote @@ -385,10 +386,21 @@ func (s *Scanner) scanString() { return } - if ch == '"' { + if ch == '"' && braces == 0 { break } + // If we're going into a ${} then we can ignore quotes for awhile + if braces == 0 && ch == '$' && s.peek() == '{' { + braces++ + s.next() + } else if braces > 0 && ch == '{' { + braces++ + } + if braces > 0 && ch == '}' { + braces-- + } + if ch == '\\' { s.scanEscape() } diff --git a/hcl/scanner/scanner_test.go b/hcl/scanner/scanner_test.go index 9f27b0d..37696e3 100644 --- a/hcl/scanner/scanner_test.go +++ b/hcl/scanner/scanner_test.go @@ -74,6 +74,7 @@ var tokenLists = map[string][]tokenPair{ {token.STRING, `" "`}, {token.STRING, `"a"`}, {token.STRING, `"本"`}, + {token.STRING, `"${file("foo")}"`}, {token.STRING, `"\a"`}, {token.STRING, `"\b"`}, {token.STRING, `"\f"`},