Skip to content

Commit f02745a

Browse files
committed
allow escaped double-quote in "raw" string literals
Fixes #12
1 parent 920df12 commit f02745a

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

issues_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,19 @@ func TestIssue11(t *testing.T) {
9292
}
9393
}
9494
}
95+
96+
// Escaped double quote should be supported in "raw" string literals
97+
func TestIssue12(t *testing.T) {
98+
var c struct {
99+
Section struct {
100+
Name string
101+
}
102+
}
103+
err := ReadFileInto(&c, "testdata/issue12.gcfg")
104+
if err != nil {
105+
t.Fatalf("fail: want ok, got error %v", err)
106+
}
107+
if c.Section.Name != `"value"` {
108+
t.Errorf("fail: want `\"value\"`, got %q", c.Section.Name)
109+
}
110+
}

read_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ var readtests = []struct {
124124
{"[section]\nname=\"va\\\"lue\"", &cBasic{Section: cBasicS1{Name: "va\"lue"}}, true},
125125
{"[section]\nname=\"va\\nlue\"", &cBasic{Section: cBasicS1{Name: "va\nlue"}}, true},
126126
{"[section]\nname=\"va\\tlue\"", &cBasic{Section: cBasicS1{Name: "va\tlue"}}, true},
127+
{"[section]\nname=\\\"value\\\"", &cBasic{Section: cBasicS1{Name: `"value"`}}, true},
127128
{"\n[section]\nname=\\", &cBasic{}, false},
128129
{"\n[section]\nname=\\a", &cBasic{}, false},
129130
{"\n[section]\nname=\"val\\a\"", &cBasic{}, false},

scanner/scanner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ loop:
231231
hasCR = true
232232
s.next()
233233
}
234-
if s.ch != '\n' {
235-
s.error(offs, "unquoted '\\' must be followed by new line")
234+
if s.ch != '\n' && s.ch != '"' {
235+
s.error(offs, "unquoted '\\' must be followed by new line or double quote")
236236
break loop
237237
}
238238
s.next()

scanner/scanner_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ var tokens = [...]elt{
8080
{token.STRING, `"foo" "bar"`, literal, "=", " "},
8181
{token.STRING, "foo\\\nbar", literal, "=", ""},
8282
{token.STRING, "foo\\\r\nbar", literal, "=", ""},
83+
{token.STRING, `\"foobar\"`, literal, "=", ""},
8384
}
8485

8586
const whitespace = " \t \n\n\n" // to separate tokens
@@ -381,8 +382,8 @@ var errors = []struct {
381382
{"\"\n", token.STRING, 0, "string not terminated"},
382383
{`="`, token.STRING, 1, "string not terminated"},
383384
{"=\"\n", token.STRING, 1, "string not terminated"},
384-
{"=\\", token.STRING, 1, "unquoted '\\' must be followed by new line"},
385-
{"=\\\r", token.STRING, 1, "unquoted '\\' must be followed by new line"},
385+
{"=\\", token.STRING, 1, "unquoted '\\' must be followed by new line or double quote"},
386+
{"=\\\r", token.STRING, 1, "unquoted '\\' must be followed by new line or double quote"},
386387
{`"\z"`, token.STRING, 2, "unknown escape sequence"},
387388
{`"\a"`, token.STRING, 2, "unknown escape sequence"},
388389
{`"\b"`, token.STRING, 2, "unknown escape sequence"},

testdata/issue12.gcfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[section]
2+
name = \"value\"

0 commit comments

Comments
 (0)