diff --git a/issues_test.go b/issues_test.go index 872abdb..3d3e039 100644 --- a/issues_test.go +++ b/issues_test.go @@ -92,3 +92,19 @@ func TestIssue11(t *testing.T) { } } } + +// Escaped double quote should be supported in "raw" string literals +func TestIssue12(t *testing.T) { + var c struct { + Section struct { + Name string + } + } + err := ReadFileInto(&c, "testdata/issue12.gcfg") + if err != nil { + t.Fatalf("fail: want ok, got error %v", err) + } + if c.Section.Name != `"value"` { + t.Errorf("fail: want `\"value\"`, got %q", c.Section.Name) + } +} diff --git a/read_test.go b/read_test.go index 7d532e3..ba3cdeb 100644 --- a/read_test.go +++ b/read_test.go @@ -124,6 +124,7 @@ var readtests = []struct { {"[section]\nname=\"va\\\"lue\"", &cBasic{Section: cBasicS1{Name: "va\"lue"}}, true}, {"[section]\nname=\"va\\nlue\"", &cBasic{Section: cBasicS1{Name: "va\nlue"}}, true}, {"[section]\nname=\"va\\tlue\"", &cBasic{Section: cBasicS1{Name: "va\tlue"}}, true}, + {"[section]\nname=\\\"value\\\"", &cBasic{Section: cBasicS1{Name: `"value"`}}, true}, {"\n[section]\nname=\\", &cBasic{}, false}, {"\n[section]\nname=\\a", &cBasic{}, false}, {"\n[section]\nname=\"val\\a\"", &cBasic{}, false}, diff --git a/scanner/scanner.go b/scanner/scanner.go index bbbdbf5..6d0eee9 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -231,8 +231,8 @@ loop: hasCR = true s.next() } - if s.ch != '\n' { - s.error(offs, "unquoted '\\' must be followed by new line") + if s.ch != '\n' && s.ch != '"' { + s.error(offs, "unquoted '\\' must be followed by new line or double quote") break loop } s.next() diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index bef219d..53501f3 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -80,6 +80,7 @@ var tokens = [...]elt{ {token.STRING, `"foo" "bar"`, literal, "=", " "}, {token.STRING, "foo\\\nbar", literal, "=", ""}, {token.STRING, "foo\\\r\nbar", literal, "=", ""}, + {token.STRING, `\"foobar\"`, literal, "=", ""}, } const whitespace = " \t \n\n\n" // to separate tokens @@ -381,8 +382,8 @@ var errors = []struct { {"\"\n", token.STRING, 0, "string not terminated"}, {`="`, token.STRING, 1, "string not terminated"}, {"=\"\n", token.STRING, 1, "string not terminated"}, - {"=\\", token.STRING, 1, "unquoted '\\' must be followed by new line"}, - {"=\\\r", token.STRING, 1, "unquoted '\\' must be followed by new line"}, + {"=\\", token.STRING, 1, "unquoted '\\' must be followed by new line or double quote"}, + {"=\\\r", token.STRING, 1, "unquoted '\\' must be followed by new line or double quote"}, {`"\z"`, token.STRING, 2, "unknown escape sequence"}, {`"\a"`, token.STRING, 2, "unknown escape sequence"}, {`"\b"`, token.STRING, 2, "unknown escape sequence"}, diff --git a/testdata/issue12.gcfg b/testdata/issue12.gcfg new file mode 100644 index 0000000..1fe48d9 --- /dev/null +++ b/testdata/issue12.gcfg @@ -0,0 +1,2 @@ +[section] + name = \"value\"