From 764b0ad3c0342382ee3f20f702272c0b571e94ed Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 10 Oct 2014 16:07:25 -0700 Subject: [PATCH] Allow escaping quotes in HCL string [GH-14] --- decoder_test.go | 7 +++++++ hcl/lex.go | 13 +++++++++++++ test-fixtures/escape.hcl | 1 + 3 files changed, 21 insertions(+) create mode 100644 test-fixtures/escape.hcl diff --git a/decoder_test.go b/decoder_test.go index 7c7700b..ab19457 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -43,6 +43,13 @@ func TestDecode_interface(t *testing.T) { }, }, }, + { + "escape.hcl", + false, + map[string]interface{}{ + "foo": "bar\"baz", + }, + }, { "multiline_bad.hcl", false, diff --git a/hcl/lex.go b/hcl/lex.go index 23dd1b0..6dfa8fe 100644 --- a/hcl/lex.go +++ b/hcl/lex.go @@ -357,6 +357,19 @@ func (x *hclLex) lexString(yylval *hclSymType) int { return lexEOF } + // If we're escaping a quote, then escape the quote + if c == '\\' { + n := x.next() + switch n { + case '"': + c = n + case 'n': + c = '\n' + default: + x.backup() + } + } + // If we're starting into variable, mark it if braces == 0 && c == '$' && x.peek() == '{' { braces += 1 diff --git a/test-fixtures/escape.hcl b/test-fixtures/escape.hcl new file mode 100644 index 0000000..7f9a566 --- /dev/null +++ b/test-fixtures/escape.hcl @@ -0,0 +1 @@ +foo = "bar\"baz"