Fixes #22: Fixes style issues

This commit is contained in:
Thomas Pelletier
2014-08-29 18:24:51 +02:00
parent a2495b4806
commit fb5423fba2
4 changed files with 114 additions and 115 deletions
+50 -52
View File
@@ -45,7 +45,7 @@ const (
tokenEOL
)
var tokenTypeNames []string = []string{
var tokenTypeNames = []string{
"EOF",
"Comment",
"Key",
@@ -142,10 +142,10 @@ func (l *lexer) nextStart() {
for i := l.start; i < l.pos; {
r, width := utf8.DecodeRuneInString(l.input[i:])
if r == '\n' {
l.line += 1
l.line++
l.col = 1
} else {
l.col += 1
l.col++
}
i += width
}
@@ -262,10 +262,10 @@ func lexRvalue(l *lexer) stateFn {
case '=':
return l.errorf("cannot have multiple equals for the same key")
case '[':
l.depth += 1
l.depth++
return lexLeftBracket
case ']':
l.depth -= 1
l.depth--
return lexRightBracket
case '#':
return lexComment
@@ -275,12 +275,11 @@ func lexRvalue(l *lexer) stateFn {
return lexComma
case '\n':
l.ignore()
l.pos += 1
l.pos++
if l.depth == 0 {
return lexVoid
} else {
return lexRvalue
}
return lexRvalue
}
if l.follow("true") {
@@ -373,70 +372,70 @@ func lexComment(l *lexer) stateFn {
func lexLeftBracket(l *lexer) stateFn {
l.ignore()
l.pos += 1
l.pos++
l.emit(tokenLeftBracket)
return lexRvalue
}
func lexString(l *lexer) stateFn {
l.pos += 1
l.pos++
l.ignore()
growing_string := ""
growingString := ""
for {
if l.peek() == '"' {
l.emitWithValue(tokenString, growing_string)
l.pos += 1
l.emitWithValue(tokenString, growingString)
l.pos++
l.ignore()
return lexRvalue
}
if l.follow("\\\"") {
l.pos += 1
growing_string += "\""
l.pos++
growingString += "\""
} else if l.follow("\\n") {
l.pos += 1
growing_string += "\n"
l.pos++
growingString += "\n"
} else if l.follow("\\b") {
l.pos += 1
growing_string += "\b"
l.pos++
growingString += "\b"
} else if l.follow("\\f") {
l.pos += 1
growing_string += "\f"
l.pos++
growingString += "\f"
} else if l.follow("\\/") {
l.pos += 1
growing_string += "/"
l.pos++
growingString += "/"
} else if l.follow("\\t") {
l.pos += 1
growing_string += "\t"
l.pos++
growingString += "\t"
} else if l.follow("\\r") {
l.pos += 1
growing_string += "\r"
l.pos++
growingString += "\r"
} else if l.follow("\\\\") {
l.pos += 1
growing_string += "\\"
l.pos++
growingString += "\\"
} else if l.follow("\\u") {
l.pos += 2
code := ""
for i := 0; i < 4; i++ {
c := l.peek()
l.pos += 1
l.pos++
if !isHexDigit(c) {
return l.errorf("unfinished unicode escape")
}
code = code + string(c)
}
l.pos -= 1
l.pos--
intcode, err := strconv.ParseInt(code, 16, 32)
if err != nil {
return l.errorf("invalid unicode escape: \\u" + code)
}
growing_string += string(rune(intcode))
growingString += string(rune(intcode))
} else if l.follow("\\") {
l.pos += 1
l.pos++
return l.errorf("invalid escape sequence: \\" + string(l.peek()))
} else {
growing_string += string(l.peek())
growingString += string(l.peek())
}
if l.next() == eof {
@@ -449,18 +448,17 @@ func lexString(l *lexer) stateFn {
func lexKeyGroup(l *lexer) stateFn {
l.ignore()
l.pos += 1
l.pos++
if l.peek() == '[' {
// token '[[' signifies an array of anonymous key groups
l.pos += 1
l.pos++
l.emit(tokenDoubleLeftBracket)
return lexInsideKeyGroupArray
} else {
// vanilla key group
l.emit(tokenLeftBracket)
return lexInsideKeyGroup
}
// vanilla key group
l.emit(tokenLeftBracket)
return lexInsideKeyGroup
}
func lexInsideKeyGroupArray(l *lexer) stateFn {
@@ -470,11 +468,11 @@ func lexInsideKeyGroupArray(l *lexer) stateFn {
l.emit(tokenKeyGroupArray)
}
l.ignore()
l.pos += 1
l.pos++
if l.peek() != ']' {
break // error
}
l.pos += 1
l.pos++
l.emit(tokenDoubleRightBracket)
return lexVoid
} else if l.peek() == '[' {
@@ -495,7 +493,7 @@ func lexInsideKeyGroup(l *lexer) stateFn {
l.emit(tokenKeyGroup)
}
l.ignore()
l.pos += 1
l.pos++
l.emit(tokenRightBracket)
return lexVoid
} else if l.peek() == '[' {
@@ -511,7 +509,7 @@ func lexInsideKeyGroup(l *lexer) stateFn {
func lexRightBracket(l *lexer) stateFn {
l.ignore()
l.pos += 1
l.pos++
l.emit(tokenRightBracket)
return lexRvalue
}
@@ -521,33 +519,33 @@ func lexNumber(l *lexer) stateFn {
if !l.accept("+") {
l.accept("-")
}
point_seen := false
digit_seen := false
pointSeen := false
digitSeen := false
for {
next := l.next()
if next == '.' {
if point_seen {
if pointSeen {
return l.errorf("cannot have two dots in one float")
}
if !isDigit(l.peek()) {
return l.errorf("float cannot end with a dot")
}
point_seen = true
pointSeen = true
} else if isDigit(next) {
digit_seen = true
digitSeen = true
} else {
l.backup()
break
}
if point_seen && !digit_seen {
if pointSeen && !digitSeen {
return l.errorf("cannot start float with a dot")
}
}
if !digit_seen {
if !digitSeen {
return l.errorf("no digit in that number")
}
if point_seen {
if pointSeen {
l.emit(tokenFloat)
} else {
l.emit(tokenInteger)
+25 -25
View File
@@ -94,7 +94,7 @@ func parseStart(p *parser) parserStateFn {
}
func parseGroupArray(p *parser) parserStateFn {
start_token := p.getToken() // discard the [[
startToken := p.getToken() // discard the [[
key := p.getToken()
if key.typ != tokenKeyGroupArray {
p.raiseError(key, "unexpected token %s, was expecting a key group array", key)
@@ -103,21 +103,21 @@ func parseGroupArray(p *parser) parserStateFn {
// get or create group array element at the indicated part in the path
keys := strings.Split(key.val, ".")
p.tree.createSubTree(keys[:len(keys)-1]) // create parent entries
dest_tree := p.tree.GetPath(keys)
destTree := p.tree.GetPath(keys)
var array []*TomlTree
if dest_tree == nil {
if destTree == nil {
array = make([]*TomlTree, 0)
} else if dest_tree.([]*TomlTree) != nil {
array = dest_tree.([]*TomlTree)
} else if destTree.([]*TomlTree) != nil {
array = destTree.([]*TomlTree)
} else {
p.raiseError(key, "key %s is already assigned and not of type group array", key)
}
p.currentGroup = keys
// add a new tree to the end of the group array
new_tree := newTomlTree()
new_tree.position = start_token.Position
array = append(array, new_tree)
newTree := newTomlTree()
newTree.position = startToken.Position
array = append(array, newTree)
p.tree.SetPath(p.currentGroup, array)
// remove all keys that were children of this group array
@@ -144,7 +144,7 @@ func parseGroupArray(p *parser) parserStateFn {
}
func parseGroup(p *parser) parserStateFn {
start_token := p.getToken() // discard the [
startToken := p.getToken() // discard the [
key := p.getToken()
if key.typ != tokenKeyGroup {
p.raiseError(key, "unexpected token %s, was expecting a key group", key)
@@ -162,8 +162,8 @@ func parseGroup(p *parser) parserStateFn {
}
p.assume(tokenRightBracket)
p.currentGroup = keys
target_tree := p.tree.GetPath(p.currentGroup).(*TomlTree)
target_tree.position = start_token.Position
targetTree := p.tree.GetPath(p.currentGroup).(*TomlTree)
targetTree.position = startToken.Position
return parseStart(p)
}
@@ -171,33 +171,33 @@ func parseAssign(p *parser) parserStateFn {
key := p.getToken()
p.assume(tokenEqual)
value := parseRvalue(p)
var group_key []string
var groupKey []string
if len(p.currentGroup) > 0 {
group_key = p.currentGroup
groupKey = p.currentGroup
} else {
group_key = []string{}
groupKey = []string{}
}
// find the group to assign, looking out for arrays of groups
var target_node *TomlTree
switch node := p.tree.GetPath(group_key).(type) {
var targetNode *TomlTree
switch node := p.tree.GetPath(groupKey).(type) {
case []*TomlTree:
target_node = node[len(node)-1]
targetNode = node[len(node)-1]
case *TomlTree:
target_node = node
targetNode = node
default:
p.raiseError(key, "Unknown group type for path: %s",
strings.Join(group_key, "."))
strings.Join(groupKey, "."))
}
// assign value to the found group
local_key := []string{key.val}
final_key := append(group_key, key.val)
if target_node.GetPath(local_key) != nil {
localKey := []string{key.val}
finalKey := append(groupKey, key.val)
if targetNode.GetPath(localKey) != nil {
p.raiseError(key, "The following key was defined twice: %s",
strings.Join(final_key, "."))
strings.Join(finalKey, "."))
}
target_node.values[key.val] = &tomlValue{value, key.Position}
targetNode.values[key.val] = &tomlValue{value, key.Position}
return parseStart(p)
}
@@ -244,7 +244,7 @@ func parseRvalue(p *parser) interface{} {
}
func parseArray(p *parser) []interface{} {
array := make([]interface{}, 0)
var array []interface{}
arrayType := reflect.TypeOf(nil)
for {
follow := p.peek()
+3 -1
View File
@@ -6,7 +6,7 @@ import (
"fmt"
)
// position within a TOML document
// Position within a TOML document
type Position struct {
Line int // line within the document
Col int // column within the line
@@ -18,6 +18,8 @@ func (p *Position) String() string {
return fmt.Sprintf("(%d, %d)", p.Line, p.Col)
}
// Invalid returns wheter or not the position is valid (i.e. with negative or
// null values)
func (p *Position) Invalid() bool {
return p.Line <= 0 || p.Col <= 0
}
+36 -37
View File
@@ -1,4 +1,4 @@
// TOML markup language parser.
// Package toml is a TOML markup language parser.
//
// This version supports the specification as described in
// https://github.com/toml-lang/toml/blob/master/versions/toml-v0.2.0.md
@@ -19,8 +19,7 @@ type tomlValue struct {
position Position
}
// Definition of a TomlTree.
// This is the result of the parsing of a TOML file.
// TomlTree is the result of the parsing of a TOML file.
type TomlTree struct {
values map[string]interface{}
position Position
@@ -41,7 +40,7 @@ func (t *TomlTree) Has(key string) bool {
return t.HasPath(strings.Split(key, "."))
}
// Returns true if the given path of keys exists, false otherwise.
// HasPath returns true if the given path of keys exists, false otherwise.
func (t *TomlTree) HasPath(keys []string) bool {
return t.GetPath(keys) != nil
}
@@ -49,8 +48,8 @@ func (t *TomlTree) HasPath(keys []string) bool {
// Keys returns the keys of the toplevel tree.
// Warning: this is a costly operation.
func (t *TomlTree) Keys() []string {
keys := make([]string, 0)
for k, _ := range t.values {
var keys []string
for k := range t.values {
keys = append(keys, k)
}
return keys
@@ -67,15 +66,15 @@ func (t *TomlTree) Get(key string) interface{} {
return t.GetPath(strings.Split(key, "."))
}
// Returns the element in the tree indicated by 'keys'.
// GetPath returns the element in the tree indicated by 'keys'.
// If keys is of length zero, the current tree is returned.
func (t *TomlTree) GetPath(keys []string) interface{} {
if len(keys) == 0 {
return t
}
subtree := t
for _, intermediate_key := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediate_key]
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return nil
}
@@ -101,6 +100,7 @@ func (t *TomlTree) GetPath(keys []string) interface{} {
}
}
// GetPosition returns the position of the given key.
func (t *TomlTree) GetPosition(key string) Position {
if key == "" {
return Position{0, 0}
@@ -108,15 +108,15 @@ func (t *TomlTree) GetPosition(key string) Position {
return t.GetPositionPath(strings.Split(key, "."))
}
// Returns the element in the tree indicated by 'keys'.
// GetPositionPath returns the element in the tree indicated by 'keys'.
// If keys is of length zero, the current tree is returned.
func (t *TomlTree) GetPositionPath(keys []string) Position {
if len(keys) == 0 {
return t.position
}
subtree := t
for _, intermediate_key := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediate_key]
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return Position{0, 0}
}
@@ -150,7 +150,7 @@ func (t *TomlTree) GetPositionPath(keys []string) Position {
}
}
// Same as Get but with a default value
// GetDefault works like Get but with a default value
func (t *TomlTree) GetDefault(key string, def interface{}) interface{} {
val := t.Get(key)
if val == nil {
@@ -166,16 +166,16 @@ func (t *TomlTree) Set(key string, value interface{}) {
t.SetPath(strings.Split(key, "."), value)
}
// Set an element in the tree.
// SetPath sets an element in the tree.
// Keys is an array of path elements (e.g. {"a","b","c"}).
// Creates all necessary intermediates trees, if needed.
func (t *TomlTree) SetPath(keys []string, value interface{}) {
subtree := t
for _, intermediate_key := range keys[:len(keys)-1] {
nextTree, exists := subtree.values[intermediate_key]
for _, intermediateKey := range keys[:len(keys)-1] {
nextTree, exists := subtree.values[intermediateKey]
if !exists {
nextTree = newTomlTree()
subtree.values[intermediate_key] = &nextTree // add new element here
subtree.values[intermediateKey] = &nextTree // add new element here
}
switch node := nextTree.(type) {
case *TomlTree:
@@ -184,7 +184,7 @@ func (t *TomlTree) SetPath(keys []string, value interface{}) {
// go to most recent element
if len(node) == 0 {
// create element if it does not exist
subtree.values[intermediate_key] = append(node, newTomlTree())
subtree.values[intermediateKey] = append(node, newTomlTree())
}
subtree = node[len(node)-1]
}
@@ -201,14 +201,14 @@ func (t *TomlTree) SetPath(keys []string, value interface{}) {
// Returns nil on success, error object on failure
func (t *TomlTree) createSubTree(keys []string) error {
subtree := t
for _, intermediate_key := range keys {
if intermediate_key == "" {
for _, intermediateKey := range keys {
if intermediateKey == "" {
return fmt.Errorf("empty intermediate table")
}
nextTree, exists := subtree.values[intermediate_key]
nextTree, exists := subtree.values[intermediateKey]
if !exists {
nextTree = newTomlTree()
subtree.values[intermediate_key] = nextTree
subtree.values[intermediateKey] = nextTree
}
switch node := nextTree.(type) {
@@ -218,7 +218,7 @@ func (t *TomlTree) createSubTree(keys []string) error {
subtree = node
default:
return fmt.Errorf("unknown type for path %s (%s)",
strings.Join(keys, "."), intermediate_key)
strings.Join(keys, "."), intermediateKey)
}
}
return nil
@@ -228,7 +228,7 @@ func (t *TomlTree) createSubTree(keys []string) error {
func encodeTomlString(value string) string {
result := ""
for _, rr := range value {
int_rr := uint16(rr)
intRr := uint16(rr)
switch rr {
case '\b':
result += "\\b"
@@ -245,8 +245,8 @@ func encodeTomlString(value string) string {
case '\\':
result += "\\\\"
default:
if int_rr < 0x001F {
result += fmt.Sprintf("\\u%0.4X", int_rr)
if intRr < 0x001F {
result += fmt.Sprintf("\\u%0.4X", intRr)
} else {
result += string(rr)
}
@@ -269,9 +269,8 @@ func toTomlValue(item interface{}, indent int) string {
case bool:
if value {
return "true"
} else {
return "false"
}
return "false"
case time.Time:
return tab + value.Format(time.RFC3339)
case []interface{}:
@@ -291,24 +290,24 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
result := ""
for k, v := range t.values {
// figure out the keyspace
combined_key := k
combinedKey := k
if keyspace != "" {
combined_key = keyspace + "." + combined_key
combinedKey = keyspace + "." + combinedKey
}
// output based on type
switch node := v.(type) {
case []*TomlTree:
for _, item := range node {
if len(item.Keys()) > 0 {
result += fmt.Sprintf("\n%s[[%s]]\n", indent, combined_key)
result += fmt.Sprintf("\n%s[[%s]]\n", indent, combinedKey)
}
result += item.toToml(indent+" ", combined_key)
result += item.toToml(indent+" ", combinedKey)
}
case *TomlTree:
if len(node.Keys()) > 0 {
result += fmt.Sprintf("\n%s[%s]\n", indent, combined_key)
result += fmt.Sprintf("\n%s[%s]\n", indent, combinedKey)
}
result += node.toToml(indent+" ", combined_key)
result += node.toToml(indent+" ", combinedKey)
case *tomlValue:
result += fmt.Sprintf("%s%s = %s\n", indent, k, toTomlValue(node.value, 0))
default:
@@ -318,13 +317,13 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
return result
}
// Generates a human-readable representation of the current tree.
// ToString generates a human-readable representation of the current tree.
// Output spans multiple lines, and is suitable for ingest by a TOML parser
func (t *TomlTree) ToString() string {
return t.toToml("", "")
}
// Create a TomlTree from a string.
// Load creates a TomlTree from a string.
func Load(content string) (tree *TomlTree, err error) {
defer func() {
if r := recover(); r != nil {
@@ -339,7 +338,7 @@ func Load(content string) (tree *TomlTree, err error) {
return
}
// Create a TomlTree from a file.
// LoadFile creates a TomlTree from a file.
func LoadFile(path string) (tree *TomlTree, err error) {
buff, ferr := ioutil.ReadFile(path)
if ferr != nil {