Apply go fmt
This commit is contained in:
+29
-25
@@ -10,14 +10,13 @@ import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
|
||||
var dateRegexp *regexp.Regexp
|
||||
|
||||
// Define tokens
|
||||
type tokenType int
|
||||
|
||||
const (
|
||||
eof = - (iota + 1)
|
||||
eof = -(iota + 1)
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -44,7 +43,6 @@ type token struct {
|
||||
val string
|
||||
}
|
||||
|
||||
|
||||
func (i token) String() string {
|
||||
switch i.typ {
|
||||
case tokenEOF:
|
||||
@@ -54,12 +52,11 @@ func (i token) String() string {
|
||||
}
|
||||
|
||||
if len(i.val) > 10 {
|
||||
return fmt.Sprintf("%.10q...", i.val);
|
||||
return fmt.Sprintf("%.10q...", i.val)
|
||||
}
|
||||
return fmt.Sprintf("%q", i.val)
|
||||
}
|
||||
|
||||
|
||||
func isSpace(r rune) bool {
|
||||
return r == ' ' || r == '\t'
|
||||
}
|
||||
@@ -72,22 +69,20 @@ func isDigit(r rune) bool {
|
||||
return r >= '0' && r <= '9'
|
||||
}
|
||||
|
||||
|
||||
// Define lexer
|
||||
type lexer struct {
|
||||
input string
|
||||
start int
|
||||
pos int
|
||||
width int
|
||||
input string
|
||||
start int
|
||||
pos int
|
||||
width int
|
||||
tokens chan token
|
||||
}
|
||||
|
||||
|
||||
func (l *lexer) run() {
|
||||
for state := lexVoid; state != nil; {
|
||||
state = state(l)
|
||||
}
|
||||
close (l.tokens)
|
||||
close(l.tokens)
|
||||
}
|
||||
|
||||
func (l *lexer) emit(t tokenType) {
|
||||
@@ -100,8 +95,7 @@ func (l *lexer) emitWithValue(t tokenType, value string) {
|
||||
l.start = l.pos
|
||||
}
|
||||
|
||||
|
||||
func (l *lexer) next() (rune) {
|
||||
func (l *lexer) next() rune {
|
||||
if l.pos >= len(l.input) {
|
||||
l.width = 0
|
||||
return eof
|
||||
@@ -146,7 +140,6 @@ func (l *lexer) follow(next string) bool {
|
||||
return strings.HasPrefix(l.input[l.pos:], next)
|
||||
}
|
||||
|
||||
|
||||
// Define state functions
|
||||
type stateFn func(*lexer) stateFn
|
||||
|
||||
@@ -170,7 +163,9 @@ func lexVoid(l *lexer) stateFn {
|
||||
l.ignore()
|
||||
}
|
||||
|
||||
if l.next() == eof { break }
|
||||
if l.next() == eof {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
l.emit(tokenEOF)
|
||||
@@ -222,7 +217,9 @@ func lexRvalue(l *lexer) stateFn {
|
||||
l.ignore()
|
||||
}
|
||||
|
||||
if l.next() == eof { break }
|
||||
if l.next() == eof {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
l.emit(tokenEOF)
|
||||
@@ -311,7 +308,9 @@ func lexString(l *lexer) stateFn {
|
||||
growing_string += string(l.peek())
|
||||
}
|
||||
|
||||
if l.next() == eof { break }
|
||||
if l.next() == eof {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return l.errorf("unclosed string")
|
||||
@@ -336,7 +335,9 @@ func lexInsideKeyGroup(l *lexer) stateFn {
|
||||
return lexVoid
|
||||
}
|
||||
|
||||
if l.next() == eof { break }
|
||||
if l.next() == eof {
|
||||
break
|
||||
}
|
||||
}
|
||||
return l.errorf("unclosed key group")
|
||||
}
|
||||
@@ -350,13 +351,17 @@ func lexRightBracket(l *lexer) stateFn {
|
||||
|
||||
func lexNumber(l *lexer) stateFn {
|
||||
l.ignore()
|
||||
if !l.accept("+") { l.accept("-") }
|
||||
if !l.accept("+") {
|
||||
l.accept("-")
|
||||
}
|
||||
point_seen := false
|
||||
digit_seen := false
|
||||
for {
|
||||
next := l.next()
|
||||
if next == '.' { point_seen = true
|
||||
} else if isDigit(next) { digit_seen = true
|
||||
if next == '.' {
|
||||
point_seen = true
|
||||
} else if isDigit(next) {
|
||||
digit_seen = true
|
||||
} else {
|
||||
l.backup()
|
||||
break
|
||||
@@ -378,11 +383,10 @@ func init() {
|
||||
dateRegexp = regexp.MustCompile("^\\d{1,4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z")
|
||||
}
|
||||
|
||||
|
||||
// Entry point
|
||||
func lex(input string) (*lexer, chan token) {
|
||||
l := &lexer {
|
||||
input: input,
|
||||
l := &lexer{
|
||||
input: input,
|
||||
tokens: make(chan token),
|
||||
}
|
||||
go l.run()
|
||||
|
||||
@@ -5,7 +5,7 @@ import "testing"
|
||||
func testFlow(t *testing.T, input string, expectedFlow []token) {
|
||||
_, ch := lex(input)
|
||||
for _, expected := range expectedFlow {
|
||||
token := <- ch
|
||||
token := <-ch
|
||||
if token != expected {
|
||||
t.Log("compared", token, "to", expected)
|
||||
t.Log(token.val, "<->", expected.val)
|
||||
@@ -14,10 +14,10 @@ func testFlow(t *testing.T, input string, expectedFlow []token) {
|
||||
}
|
||||
}
|
||||
|
||||
tok, ok := <- ch
|
||||
tok, ok := <-ch
|
||||
if ok {
|
||||
t.Log("channel is not closed!")
|
||||
t.Log(len(ch) + 1, "tokens remaining:")
|
||||
t.Log(len(ch)+1, "tokens remaining:")
|
||||
|
||||
t.Log("token ->", tok)
|
||||
for token := range ch {
|
||||
@@ -43,7 +43,6 @@ func TestUnclosedKeyGroup(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func TestComment(t *testing.T) {
|
||||
testFlow(t, "# blahblah", []token{
|
||||
token{tokenEOF, ""},
|
||||
|
||||
+22
-14
@@ -8,10 +8,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
type parser struct {
|
||||
flow chan token
|
||||
tree *TomlTree
|
||||
flow chan token
|
||||
tree *TomlTree
|
||||
tokensBuffer []token
|
||||
currentGroup string
|
||||
}
|
||||
@@ -29,7 +28,7 @@ func (p *parser) peek() *token {
|
||||
return &(p.tokensBuffer[0])
|
||||
}
|
||||
|
||||
tok, ok := <- p.flow
|
||||
tok, ok := <-p.flow
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
@@ -53,7 +52,7 @@ func (p *parser) getToken() *token {
|
||||
p.tokensBuffer = p.tokensBuffer[1:]
|
||||
return &tok
|
||||
}
|
||||
tok, ok := <- p.flow
|
||||
tok, ok := <-p.flow
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
@@ -120,15 +119,21 @@ func parseRvalue(p *parser) interface{} {
|
||||
return false
|
||||
case tokenInteger:
|
||||
val, err := strconv.ParseInt(tok.val, 10, 64)
|
||||
if err != nil { panic(err) }
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
case tokenFloat:
|
||||
val, err := strconv.ParseFloat(tok.val, 64)
|
||||
if err != nil { panic(err) }
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
case tokenDate:
|
||||
val, err := time.Parse(time.RFC3339, tok.val)
|
||||
if err != nil { panic(err) }
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
case tokenLeftBracket:
|
||||
return parseArray(p)
|
||||
@@ -143,7 +148,9 @@ func parseArray(p *parser) []interface{} {
|
||||
array := make([]interface{}, 0)
|
||||
for {
|
||||
follow := p.peek()
|
||||
if follow == nil { panic("unterminated array") }
|
||||
if follow == nil {
|
||||
panic("unterminated array")
|
||||
}
|
||||
if follow.typ == tokenRightBracket {
|
||||
p.getToken()
|
||||
return array
|
||||
@@ -151,7 +158,9 @@ func parseArray(p *parser) []interface{} {
|
||||
val := parseRvalue(p)
|
||||
array = append(array, val)
|
||||
follow = p.peek()
|
||||
if follow == nil { panic("unterminated array") }
|
||||
if follow == nil {
|
||||
panic("unterminated array")
|
||||
}
|
||||
if follow.typ != tokenRightBracket && follow.typ != tokenComma {
|
||||
panic("missing comma")
|
||||
}
|
||||
@@ -162,12 +171,11 @@ func parseArray(p *parser) []interface{} {
|
||||
return array
|
||||
}
|
||||
|
||||
|
||||
func parse(flow chan token) *TomlTree {
|
||||
result := make(TomlTree)
|
||||
parser := &parser {
|
||||
flow: flow,
|
||||
tree: &result,
|
||||
parser := &parser{
|
||||
flow: flow,
|
||||
tree: &result,
|
||||
tokensBuffer: make([]token, 0),
|
||||
currentGroup: "",
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
func assertTree(t *testing.T, tree *TomlTree, ref map[string]interface{}) {
|
||||
for k, v := range ref {
|
||||
if fmt.Sprintf("%v", tree.Get(k)) != fmt.Sprintf("%v", v) {
|
||||
@@ -25,7 +24,6 @@ func TestCreateSubTree(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestSimpleKV(t *testing.T) {
|
||||
tree := Load("a = 42")
|
||||
assertTree(t, tree, map[string]interface{}{
|
||||
@@ -81,18 +79,18 @@ func TestNestedKeys(t *testing.T) {
|
||||
func TestArraySimple(t *testing.T) {
|
||||
tree := Load("a = [42, 21, 10]")
|
||||
assertTree(t, tree, map[string]interface{}{
|
||||
"a": []int64{int64(42), int64(21), int64(10),},
|
||||
"a": []int64{int64(42), int64(21), int64(10)},
|
||||
})
|
||||
|
||||
tree = Load("a = [42, 21, 10,]")
|
||||
assertTree(t, tree, map[string]interface{}{
|
||||
"a": []int64{int64(42), int64(21), int64(10),},
|
||||
"a": []int64{int64(42), int64(21), int64(10)},
|
||||
})
|
||||
}
|
||||
|
||||
func TestArrayNested(t *testing.T) {
|
||||
tree := Load("a = [[42, 21], [10]]")
|
||||
assertTree(t, tree, map[string]interface{}{
|
||||
"a": [][]int64{[]int64{int64(42), int64(21),}, []int64{int64(10),},},
|
||||
"a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}},
|
||||
})
|
||||
}
|
||||
|
||||
+2
-3
@@ -30,7 +30,7 @@ func (t *TomlTree) Get(key string) interface{} {
|
||||
}
|
||||
subtree = (*subtree)[intermediate_key].(*TomlTree)
|
||||
}
|
||||
return (*subtree)[keys[len(keys) - 1]]
|
||||
return (*subtree)[keys[len(keys)-1]]
|
||||
}
|
||||
|
||||
// Set an element in the tree.
|
||||
@@ -46,7 +46,7 @@ func (t *TomlTree) Set(key string, value interface{}) {
|
||||
}
|
||||
subtree = (*subtree)[intermediate_key].(*TomlTree)
|
||||
}
|
||||
(*subtree)[keys[len(keys) - 1]] = value
|
||||
(*subtree)[keys[len(keys)-1]] = value
|
||||
}
|
||||
|
||||
// createSubTree takes a tree and a key andcreate the necessary intermediate
|
||||
@@ -66,7 +66,6 @@ func (t *TomlTree) createSubTree(key string) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func Load(content string) *TomlTree {
|
||||
_, flow := lex(content)
|
||||
return parse(flow)
|
||||
|
||||
Reference in New Issue
Block a user