Merge pull request #4 from alexstaubo/master

Support underscores and uppercase letters in key names
This commit is contained in:
Thomas Pelletier
2013-03-19 01:29:52 -07:00
2 changed files with 28 additions and 6 deletions
+7 -6
View File
@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"unicode"
"unicode/utf8" "unicode/utf8"
) )
@@ -61,12 +62,12 @@ func isSpace(r rune) bool {
return r == ' ' || r == '\t' return r == ' ' || r == '\t'
} }
func isAlpha(r rune) bool { func isAlphanumeric(r rune) bool {
return r >= 'a' && r <= 'z' return unicode.IsLetter(r) || r == '_'
} }
func isDigit(r rune) bool { func isDigit(r rune) bool {
return r >= '0' && r <= '9' return unicode.IsNumber(r)
} }
// Define lexer // Define lexer
@@ -156,7 +157,7 @@ func lexVoid(l *lexer) stateFn {
return lexEqual return lexEqual
} }
if isAlpha(next) { if isAlphanumeric(next) {
return lexKey return lexKey
} }
@@ -207,7 +208,7 @@ func lexRvalue(l *lexer) stateFn {
return lexFalse return lexFalse
} }
if isAlpha(next) { if isAlphanumeric(next) {
return lexKey return lexKey
} }
@@ -269,7 +270,7 @@ func lexComma(l *lexer) stateFn {
func lexKey(l *lexer) stateFn { func lexKey(l *lexer) stateFn {
l.ignore() l.ignore()
for isAlpha(l.next()) { for isAlphanumeric(l.next()) {
} }
l.backup() l.backup()
l.emit(tokenKey) l.emit(tokenKey)
+21
View File
@@ -77,6 +77,27 @@ func TestBasicKey(t *testing.T) {
}) })
} }
func TestBasicKeyWithUnderscore(t *testing.T) {
testFlow(t, "hello_hello", []token{
token{tokenKey, "hello_hello"},
token{tokenEOF, ""},
})
}
func TestBasicKeyWithUppercaseMix(t *testing.T) {
testFlow(t, "helloHELLOHello", []token{
token{tokenKey, "helloHELLOHello"},
token{tokenEOF, ""},
})
}
func TestBasicKeyWithInternationalCharacters(t *testing.T) {
testFlow(t, "héllÖ", []token{
token{tokenKey, "héllÖ"},
token{tokenEOF, ""},
})
}
func TestBasicKeyAndEqual(t *testing.T) { func TestBasicKeyAndEqual(t *testing.T) {
testFlow(t, "hello =", []token{ testFlow(t, "hello =", []token{
token{tokenKey, "hello"}, token{tokenKey, "hello"},