Skip to content

Commit

Permalink
Fixes pelletier#32 : Ensure keys are correctly parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Dec 6, 2014
1 parent 543444f commit 2f2f286
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
7 changes: 5 additions & 2 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (l *tomlLexer) lexVoid() tomlLexStateFn {
return l.lexRvalue
}

if isKeyChar(next) {
if isKeyStartChar(next) {
return l.lexKey
}

Expand Down Expand Up @@ -250,7 +250,10 @@ func (l *tomlLexer) lexComma() tomlLexStateFn {

func (l *tomlLexer) lexKey() tomlLexStateFn {
l.ignore()
for isKeyChar(l.next()) {
for r := l.next(); isKeyChar(r); r = l.next() {
if (r == '#') {
return l.errorf("keys cannot contain # character")
}
}
l.backup()
l.emit(tokenKey)
Expand Down
15 changes: 6 additions & 9 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,16 @@ func TestBasicKeyAndEqual(t *testing.T) {

func TestKeyWithSharpAndEqual(t *testing.T) {
testFlow(t, "key#name = 5", []token{
token{Position{1, 1}, tokenKey, "key#name"},
token{Position{1, 10}, tokenEqual, "="},
token{Position{1, 12}, tokenInteger, "5"},
token{Position{1, 13}, tokenEOF, ""},
token{Position{1, 1}, tokenError, "keys cannot contain # character"},
})
}

func TestKeyWithSymbolsAndEqual(t *testing.T) {
testFlow(t, "~!@#$^&*()_+-`1234567890[]\\|/?><.,;:' = 5", []token{
token{Position{1, 1}, tokenKey, "~!@#$^&*()_+-`1234567890[]\\|/?><.,;:'"},
token{Position{1, 39}, tokenEqual, "="},
token{Position{1, 41}, tokenInteger, "5"},
token{Position{1, 42}, tokenEOF, ""},
testFlow(t, "~!@$^&*()_+-`1234567890[]\\|/?><.,;:' = 5", []token{
token{Position{1, 1}, tokenKey, "~!@$^&*()_+-`1234567890[]\\|/?><.,;:'"},
token{Position{1, 38}, tokenEqual, "="},
token{Position{1, 40}, tokenInteger, "5"},
token{Position{1, 41}, tokenEOF, ""},
})
}

Expand Down
4 changes: 2 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func TestSimpleKV(t *testing.T) {
// NOTE: from the BurntSushi test suite
// NOTE: this test is pure evil due to the embedded '.'
func TestSpecialKV(t *testing.T) {
tree, err := Load("~!@#$^&*()_+-`1234567890[]\\|/?><.,;: = 1")
tree, err := Load("~!@$^&*()_+-`1234567890[]\\|/?><.,;: = 1")
assertTree(t, tree, err, map[string]interface{}{
"~!@#$^&*()_+-`1234567890[]\\|/?><.,;:": int64(1),
"~!@$^&*()_+-`1234567890[]\\|/?><.,;:": int64(1),
})
}

Expand Down
9 changes: 7 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ func isAlphanumeric(r rune) bool {
}

func isKeyChar(r rune) bool {
// "Keys start with the first non-whitespace character and end with the last
// non-whitespace character before the equals sign."
// Keys start with the first character that isn't whitespace or [ and end
// with the last non-whitespace character before the equals sign. Keys
// cannot contain a # character."
return !(isSpace(r) || r == '\r' || r == '\n' || r == eof || r == '=')
}

func isKeyStartChar(r rune) bool {
return !(isSpace(r) || r == '\r' || r == '\n' || r == eof || r == '[')
}

func isDigit(r rune) bool {
return unicode.IsNumber(r)
}
Expand Down

0 comments on commit 2f2f286

Please sign in to comment.