caddyfile: Support backticks as quotes (closes #2591) (#3242)

This commit is contained in:
Matt Holt 2020-05-05 12:27:49 -06:00 committed by GitHub
parent e051e119d1
commit 52305618df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 5 deletions

View file

@ -73,7 +73,7 @@ func (l *lexer) load(input io.Reader) error {
// a token was loaded; false otherwise. // a token was loaded; false otherwise.
func (l *lexer) next() bool { func (l *lexer) next() bool {
var val []rune var val []rune
var comment, quoted, escaped bool var comment, quoted, btQuoted, escaped bool
makeToken := func() bool { makeToken := func() bool {
l.token.Text = string(val) l.token.Text = string(val)
@ -92,13 +92,13 @@ func (l *lexer) next() bool {
panic(err) panic(err)
} }
if !escaped && ch == '\\' { if !escaped && !btQuoted && ch == '\\' {
escaped = true escaped = true
continue continue
} }
if quoted { if quoted || btQuoted {
if escaped { if quoted && escaped {
// all is literal in quoted area, // all is literal in quoted area,
// so only escape quotes // so only escape quotes
if ch != '"' { if ch != '"' {
@ -106,7 +106,10 @@ func (l *lexer) next() bool {
} }
escaped = false escaped = false
} else { } else {
if ch == '"' { if quoted && ch == '"' {
return makeToken()
}
if btQuoted && ch == '`' {
return makeToken() return makeToken()
} }
} }
@ -151,6 +154,10 @@ func (l *lexer) next() bool {
quoted = true quoted = true
continue continue
} }
if ch == '`' {
btQuoted = true
continue
}
} }
if escaped { if escaped {

View file

@ -199,6 +199,38 @@ func TestLexer(t *testing.T) {
{Line: 1, Text: ":8080"}, {Line: 1, Text: ":8080"},
}, },
}, },
{
input: "simple `backtick quoted` string",
expected: []Token{
{Line: 1, Text: `simple`},
{Line: 1, Text: `backtick quoted`},
{Line: 1, Text: `string`},
},
},
{
input: "multiline `backtick\nquoted\n` string",
expected: []Token{
{Line: 1, Text: `multiline`},
{Line: 1, Text: "backtick\nquoted\n"},
{Line: 3, Text: `string`},
},
},
{
input: "nested `\"quotes inside\" backticks` string",
expected: []Token{
{Line: 1, Text: `nested`},
{Line: 1, Text: `"quotes inside" backticks`},
{Line: 1, Text: `string`},
},
},
{
input: "reverse-nested \"`backticks` inside\" quotes",
expected: []Token{
{Line: 1, Text: `reverse-nested`},
{Line: 1, Text: "`backticks` inside"},
{Line: 1, Text: `quotes`},
},
},
} }
for i, testCase := range testCases { for i, testCase := range testCases {