Skip to content

Commit

Permalink
Keys are allowed to contain whitespace.
Browse files Browse the repository at this point in the history
Fixes #45.

This is not a breaking change. All previous valid TOML documents
are still valid.
  • Loading branch information
BurntSushi committed Jul 17, 2014
1 parent 1dad399 commit 1ebea13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
6 changes: 0 additions & 6 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"strings"
"time"
"unicode"
)

type tomlEncodeError struct{ error }
Expand Down Expand Up @@ -512,10 +511,5 @@ func isValidKeyName(s string) bool {
if len(s) == 0 {
return false
}
for _, r := range s {
if unicode.IsSpace(r) {
return false
}
}
return true
}
20 changes: 11 additions & 9 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package toml

import (
"fmt"
"strings"
"unicode/utf8"
)

Expand Down Expand Up @@ -112,6 +113,11 @@ func (lx *lexer) emit(typ itemType) {
lx.start = lx.pos
}

func (lx *lexer) emitTrim(typ itemType) {
lx.items <- item{typ, strings.TrimSpace(lx.current()), lx.line}
lx.start = lx.pos
}

func (lx *lexer) next() (r rune) {
if lx.pos >= len(lx.input) {
lx.width = 0
Expand Down Expand Up @@ -315,14 +321,14 @@ func lexKey(lx *lexer) stateFn {
// last non-whitespace character before the equals sign."
// Note here that whitespace is either a tab or a space.
// But we'll call it quits if we see a new line too.
if isWhitespace(r) || isNL(r) {
lx.emit(itemText)
if isNL(r) {
lx.emitTrim(itemText)
return lexKeyEnd
}

// Let's also call it quits if we see an equals sign.
if r == keySep {
lx.emit(itemText)
lx.emitTrim(itemText)
return lexKeyEnd
}

Expand All @@ -331,14 +337,10 @@ func lexKey(lx *lexer) stateFn {
}

// lexKeyEnd consumes the end of a key (up to the key separator).
// Assumes that the first whitespace character after a key (or the '='
// separator) has NOT been consumed.
// Assumes that any whitespace after a key has been consumed.
func lexKeyEnd(lx *lexer) stateFn {
r := lx.next()
switch {
case isWhitespace(r) || isNL(r):
return lexSkip(lx, lexKeyEnd)
case r == keySep:
if r == keySep {
return lexSkip(lx, lexValue)
}
return lx.errorf("Expected key separator %q, but got %q instead.",
Expand Down

0 comments on commit 1ebea13

Please sign in to comment.