Support quoted map keys too

This commit is contained in:
Derek Collison
2013-08-07 09:17:32 -07:00
parent b2187a7514
commit d7876007d9
2 changed files with 31 additions and 1 deletions

View File

@@ -434,13 +434,28 @@ func lexMapKeyStart(lx *lexer) stateFn {
return lexCommentStart
}
lx.backup()
case r == sqStringStart || r == dqStringStart:
lx.next()
return lexSkip(lx, lexMapQuotedKey)
}
lx.ignore()
lx.next()
return lexMapKey
}
// lexKey consumes the text of a key. Assumes that the first character (which
// lexMapQuotedKey consumes the text of a key between quotes.
func lexMapQuotedKey(lx *lexer) stateFn {
r := lx.peek()
if r == sqStringEnd || r == dqStringEnd {
lx.emit(itemKey)
lx.next()
return lexSkip(lx, lexMapKeyEnd)
}
lx.next()
return lexMapQuotedKey
}
// lexMapKey consumes the text of a key. Assumes that the first character (which
// is not whitespace) has already been consumed.
func lexMapKey(lx *lexer) stateFn {
r := lx.peek()

View File

@@ -424,3 +424,18 @@ func TestNonQuotedStrings(t *testing.T) {
lx := lex(noquotes)
expect(t, lx, expectedItems)
}
func TestMapQuotedKeys(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemMapStart, "", 1},
{itemKey, "bar", 1},
{itemInteger, "4242", 1},
{itemMapEnd, "", 1},
{itemEOF, "", 1},
}
lx := lex("foo = {'bar' = 4242}")
expect(t, lx, expectedItems)
lx = lex("foo = {\"bar\" = 4242}")
expect(t, lx, expectedItems)
}