Reformat for better documentation output
This commit is contained in:
+7
-7
@@ -20,7 +20,7 @@ var dateRegexp *regexp.Regexp
|
|||||||
type tokenType int
|
type tokenType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EOF = - (iota + 1)
|
eof = - (iota + 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -108,7 +108,7 @@ func (l *lexer) emitWithValue(t tokenType, value string) {
|
|||||||
func (l *lexer) next() (rune) {
|
func (l *lexer) next() (rune) {
|
||||||
if l.pos >= len(l.input) {
|
if l.pos >= len(l.input) {
|
||||||
l.width = 0
|
l.width = 0
|
||||||
return EOF
|
return eof
|
||||||
}
|
}
|
||||||
var r rune
|
var r rune
|
||||||
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
|
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
|
||||||
@@ -174,7 +174,7 @@ func lexVoid(l *lexer) stateFn {
|
|||||||
l.ignore()
|
l.ignore()
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.next() == EOF { break }
|
if l.next() == eof { break }
|
||||||
}
|
}
|
||||||
|
|
||||||
l.emit(tokenEOF)
|
l.emit(tokenEOF)
|
||||||
@@ -223,7 +223,7 @@ func lexRvalue(l *lexer) stateFn {
|
|||||||
l.ignore()
|
l.ignore()
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.next() == EOF { break }
|
if l.next() == eof { break }
|
||||||
}
|
}
|
||||||
|
|
||||||
l.emit(tokenEOF)
|
l.emit(tokenEOF)
|
||||||
@@ -276,7 +276,7 @@ func lexKey(l *lexer) stateFn {
|
|||||||
func lexComment(l *lexer) stateFn {
|
func lexComment(l *lexer) stateFn {
|
||||||
for {
|
for {
|
||||||
next := l.next()
|
next := l.next()
|
||||||
if next == '\n' || next == EOF {
|
if next == '\n' || next == eof {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -313,7 +313,7 @@ func lexString(l *lexer) stateFn {
|
|||||||
growing_string += string(l.peek())
|
growing_string += string(l.peek())
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.next() == EOF { break }
|
if l.next() == eof { break }
|
||||||
}
|
}
|
||||||
|
|
||||||
return l.errorf("unclosed string")
|
return l.errorf("unclosed string")
|
||||||
@@ -338,7 +338,7 @@ func lexInsideKeyGroup(l *lexer) stateFn {
|
|||||||
return lexVoid
|
return lexVoid
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.next() == EOF { break }
|
if l.next() == eof { break }
|
||||||
}
|
}
|
||||||
return l.errorf("unclosed key group")
|
return l.errorf("unclosed key group")
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -6,24 +6,24 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Given a tree and a key, create the necessary intermediate subtrees to create
|
// createSubTree takes a tree and a key andcreate the necessary intermediate
|
||||||
// a subtree at that point. In-place.
|
// subtrees to create a subtree at that point. In-place.
|
||||||
//
|
//
|
||||||
// e.g. passing a.b.c will create (assuming tree is empty) tree[a], tree[a][b]
|
// e.g. passing a.b.c will create (assuming tree is empty) tree[a], tree[a][b]
|
||||||
// and tree[a][b][c]
|
// and tree[a][b][c]
|
||||||
func createSubTree(tree *tomlTree, key string) {
|
func createSubTree(tree *TomlTree, key string) {
|
||||||
subtree := tree
|
subtree := tree
|
||||||
for _, intermediate_key := range strings.Split(key, ".") {
|
for _, intermediate_key := range strings.Split(key, ".") {
|
||||||
_, exists := (*subtree)[intermediate_key]
|
_, exists := (*subtree)[intermediate_key]
|
||||||
if !exists {
|
if !exists {
|
||||||
(*subtree)[intermediate_key] = make(tomlTree)
|
(*subtree)[intermediate_key] = make(TomlTree)
|
||||||
}
|
}
|
||||||
subtree = (*subtree)[intermediate_key].(*tomlTree)
|
subtree = (*subtree)[intermediate_key].(*TomlTree)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func parse(chan token) *tomlTree {
|
func parse(chan token) *TomlTree {
|
||||||
result := make(tomlTree)
|
result := make(TomlTree)
|
||||||
return &result
|
return &result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "testing"
|
|||||||
|
|
||||||
|
|
||||||
func testCreateSubTree(t *testing.T) {
|
func testCreateSubTree(t *testing.T) {
|
||||||
tree := make(tomlTree)
|
tree := make(TomlTree)
|
||||||
createSubTree(&tree, "a.b.c")
|
createSubTree(&tree, "a.b.c")
|
||||||
tree.Set("a.b.c", 42)
|
tree.Set("a.b.c", 42)
|
||||||
if tree.Get("a.b.c") != 42 {
|
if tree.Get("a.b.c") != 42 {
|
||||||
|
|||||||
+12
-14
@@ -1,18 +1,17 @@
|
|||||||
// TOML interface.
|
// TOML markup language parser.
|
||||||
|
|
||||||
package toml
|
package toml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Definition of a tomlTree
|
// Definition of a TomlTree.
|
||||||
type tomlTree map[string]interface{}
|
// This is the result of the parsing of a TOML file.
|
||||||
|
type TomlTree map[string]interface{}
|
||||||
|
|
||||||
// Retrieve an element from the tree.
|
// Get an element from the tree.
|
||||||
//
|
|
||||||
// If the path described by the key does not exist, nil is returned.
|
// If the path described by the key does not exist, nil is returned.
|
||||||
func (t *tomlTree) Get(key string) interface{} {
|
func (t *TomlTree) Get(key string) interface{} {
|
||||||
subtree := t
|
subtree := t
|
||||||
keys := strings.Split(key, ".")
|
keys := strings.Split(key, ".")
|
||||||
for _, intermediate_key := range keys[:len(keys)-1] {
|
for _, intermediate_key := range keys[:len(keys)-1] {
|
||||||
@@ -20,29 +19,28 @@ func (t *tomlTree) Get(key string) interface{} {
|
|||||||
if !exists {
|
if !exists {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
subtree = (*subtree)[intermediate_key].(*tomlTree)
|
subtree = (*subtree)[intermediate_key].(*TomlTree)
|
||||||
}
|
}
|
||||||
return (*subtree)[keys[len(keys) - 1]]
|
return (*subtree)[keys[len(keys) - 1]]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set an element in the tree.
|
// Set an element in the tree.
|
||||||
//
|
|
||||||
// Creates all necessary intermediates trees, if needed.
|
// Creates all necessary intermediates trees, if needed.
|
||||||
func (t *tomlTree) Set(key string, value interface{}) {
|
func (t *TomlTree) Set(key string, value interface{}) {
|
||||||
subtree := t
|
subtree := t
|
||||||
keys := strings.Split(key, ".")
|
keys := strings.Split(key, ".")
|
||||||
for _, intermediate_key := range keys[:len(keys)-1] {
|
for _, intermediate_key := range keys[:len(keys)-1] {
|
||||||
_, exists := (*subtree)[intermediate_key]
|
_, exists := (*subtree)[intermediate_key]
|
||||||
if !exists {
|
if !exists {
|
||||||
(*subtree)[intermediate_key] = make(tomlTree)
|
(*subtree)[intermediate_key] = make(TomlTree)
|
||||||
}
|
}
|
||||||
subtree = (*subtree)[intermediate_key].(*tomlTree)
|
subtree = (*subtree)[intermediate_key].(*TomlTree)
|
||||||
}
|
}
|
||||||
(*subtree)[keys[len(key) - 1]] = value
|
(*subtree)[keys[len(key) - 1]] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func Load() tomlTree {
|
func Load() TomlTree {
|
||||||
result := make(tomlTree)
|
result := make(TomlTree)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user