Skip to content

Commit

Permalink
allow escaped double-quote in "raw" string literals
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
speter committed Mar 28, 2018
1 parent 920df12 commit f02745a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
4 changes: 2 additions & 2 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
2 changes: 2 additions & 0 deletions testdata/issue12.gcfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[section]
name = \"value\"

0 comments on commit f02745a

Please sign in to comment.