Run gofmt
This commit is contained in:
@@ -44,10 +44,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type token struct {
|
type token struct {
|
||||||
typ tokenType
|
typ tokenType
|
||||||
val string
|
val string
|
||||||
line int
|
line int
|
||||||
col int
|
col int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i token) String() string {
|
func (i token) String() string {
|
||||||
@@ -65,7 +65,7 @@ func (i token) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i token) Pos() string {
|
func (i token) Pos() string {
|
||||||
return fmt.Sprintf("(%d, %d)", i.line + 1, i.col + 1)
|
return fmt.Sprintf("(%d, %d)", i.line+1, i.col+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isSpace(r rune) bool {
|
func isSpace(r rune) bool {
|
||||||
@@ -99,8 +99,8 @@ type lexer struct {
|
|||||||
width int
|
width int
|
||||||
tokens chan token
|
tokens chan token
|
||||||
depth int
|
depth int
|
||||||
line int
|
line int
|
||||||
col int
|
col int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) run() {
|
func (l *lexer) run() {
|
||||||
@@ -111,31 +111,31 @@ func (l *lexer) run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) nextStart() {
|
func (l *lexer) nextStart() {
|
||||||
// iterate by runes (utf8 characters)
|
// iterate by runes (utf8 characters)
|
||||||
// search for newlines and advance line/col counts
|
// search for newlines and advance line/col counts
|
||||||
for i:=l.start; i<l.pos; {
|
for i := l.start; i < l.pos; {
|
||||||
r, width := utf8.DecodeRuneInString(l.input[i:])
|
r, width := utf8.DecodeRuneInString(l.input[i:])
|
||||||
if r == '\n' {
|
if r == '\n' {
|
||||||
l.line += 1
|
l.line += 1
|
||||||
l.col = 0
|
l.col = 0
|
||||||
} else {
|
} else {
|
||||||
l.col += 1
|
l.col += 1
|
||||||
}
|
}
|
||||||
i += width
|
i += width
|
||||||
// fmt.Printf("'%c'\n", r)
|
// fmt.Printf("'%c'\n", r)
|
||||||
}
|
}
|
||||||
// advance start position to next token
|
// advance start position to next token
|
||||||
l.start = l.pos
|
l.start = l.pos
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) emit(t tokenType) {
|
func (l *lexer) emit(t tokenType) {
|
||||||
l.tokens <- token{t, l.input[l.start:l.pos], l.line, l.col}
|
l.tokens <- token{t, l.input[l.start:l.pos], l.line, l.col}
|
||||||
l.nextStart()
|
l.nextStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) emitWithValue(t tokenType, value string) {
|
func (l *lexer) emitWithValue(t tokenType, value string) {
|
||||||
l.tokens <- token{t, value, l.line, l.col}
|
l.tokens <- token{t, value, l.line, l.col}
|
||||||
l.nextStart()
|
l.nextStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) next() rune {
|
func (l *lexer) next() rune {
|
||||||
@@ -150,7 +150,7 @@ func (l *lexer) next() rune {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) ignore() {
|
func (l *lexer) ignore() {
|
||||||
l.nextStart()
|
l.nextStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lexer) backup() {
|
func (l *lexer) backup() {
|
||||||
@@ -161,8 +161,8 @@ func (l *lexer) errorf(format string, args ...interface{}) stateFn {
|
|||||||
l.tokens <- token{
|
l.tokens <- token{
|
||||||
tokenError,
|
tokenError,
|
||||||
fmt.Sprintf(format, args...),
|
fmt.Sprintf(format, args...),
|
||||||
l.line,
|
l.line,
|
||||||
l.col,
|
l.col,
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -7,12 +7,12 @@ func testFlow(t *testing.T, input string, expectedFlow []token) {
|
|||||||
for _, expected := range expectedFlow {
|
for _, expected := range expectedFlow {
|
||||||
token := <-ch
|
token := <-ch
|
||||||
if token != expected {
|
if token != expected {
|
||||||
t.Log("While testing: ", input)
|
t.Log("While testing: ", input)
|
||||||
t.Log("compared", token, "to", expected)
|
t.Log("compared", token, "to", expected)
|
||||||
t.Log(token.val, "<->", expected.val)
|
t.Log(token.val, "<->", expected.val)
|
||||||
t.Log(token.typ, "<->", expected.typ)
|
t.Log(token.typ, "<->", expected.typ)
|
||||||
t.Log(token.line, "<->", expected.line)
|
t.Log(token.line, "<->", expected.line)
|
||||||
t.Log(token.col, "<->", expected.col)
|
t.Log(token.col, "<->", expected.col)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,6 @@ func TestKeyWithSharpAndEqual(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestKeyWithSymbolsAndEqual(t *testing.T) {
|
func TestKeyWithSymbolsAndEqual(t *testing.T) {
|
||||||
testFlow(t, "~!@#$^&*()_+-`1234567890[]\\|/?><.,;:' = 5", []token{
|
testFlow(t, "~!@#$^&*()_+-`1234567890[]\\|/?><.,;:' = 5", []token{
|
||||||
token{tokenKey, "~!@#$^&*()_+-`1234567890[]\\|/?><.,;:'", 0, 0},
|
token{tokenKey, "~!@#$^&*()_+-`1234567890[]\\|/?><.,;:'", 0, 0},
|
||||||
@@ -139,7 +138,7 @@ func TestKeyEqualStringEscape(t *testing.T) {
|
|||||||
testFlow(t, `foo = "hello\""`, []token{
|
testFlow(t, `foo = "hello\""`, []token{
|
||||||
token{tokenKey, "foo", 0, 0},
|
token{tokenKey, "foo", 0, 0},
|
||||||
token{tokenEqual, "=", 0, 4},
|
token{tokenEqual, "=", 0, 4},
|
||||||
token{tokenString, "hello\"" ,0, 7},
|
token{tokenString, "hello\"", 0, 7},
|
||||||
token{tokenEOF, "", 0, 15},
|
token{tokenEOF, "", 0, 15},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ type parser struct {
|
|||||||
type parserStateFn func(*parser) parserStateFn
|
type parserStateFn func(*parser) parserStateFn
|
||||||
|
|
||||||
// Formats and panics an error message based on a token
|
// Formats and panics an error message based on a token
|
||||||
func (p *parser) raiseError(tok *token, msg string, args... interface{}) {
|
func (p *parser) raiseError(tok *token, msg string, args ...interface{}) {
|
||||||
panic(tok.Pos() + ": " + fmt.Sprintf(msg, args...))
|
panic(tok.Pos() + ": " + fmt.Sprintf(msg, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) run() {
|
func (p *parser) run() {
|
||||||
@@ -135,8 +135,8 @@ func parseGroup(p *parser) parserStateFn {
|
|||||||
}
|
}
|
||||||
p.seenGroupKeys = append(p.seenGroupKeys, key.val)
|
p.seenGroupKeys = append(p.seenGroupKeys, key.val)
|
||||||
if err := p.tree.createSubTree(key.val); err != nil {
|
if err := p.tree.createSubTree(key.val); err != nil {
|
||||||
p.raiseError(key, "%s", err)
|
p.raiseError(key, "%s", err)
|
||||||
}
|
}
|
||||||
p.assume(tokenRightBracket)
|
p.assume(tokenRightBracket)
|
||||||
p.currentGroup = strings.Split(key.val, ".")
|
p.currentGroup = strings.Split(key.val, ".")
|
||||||
return parseStart(p)
|
return parseStart(p)
|
||||||
@@ -211,7 +211,7 @@ func parseRvalue(p *parser) interface{} {
|
|||||||
p.raiseError(tok, "%s", tok)
|
p.raiseError(tok, "%s", tok)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.raiseError(tok, "never reached")
|
p.raiseError(tok, "never reached")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ func (t *TomlTree) SetPath(keys []string, value interface{}) {
|
|||||||
// and tree[a][b][c]
|
// and tree[a][b][c]
|
||||||
//
|
//
|
||||||
// Returns nil on success, error object on failure
|
// Returns nil on success, error object on failure
|
||||||
func (t *TomlTree) createSubTree(key string) error{
|
func (t *TomlTree) createSubTree(key string) error {
|
||||||
subtree := t
|
subtree := t
|
||||||
for _, intermediate_key := range strings.Split(key, ".") {
|
for _, intermediate_key := range strings.Split(key, ".") {
|
||||||
if intermediate_key == "" {
|
if intermediate_key == "" {
|
||||||
@@ -157,7 +157,7 @@ func (t *TomlTree) createSubTree(key string) error{
|
|||||||
}
|
}
|
||||||
subtree = ((*subtree)[intermediate_key]).(*TomlTree)
|
subtree = ((*subtree)[intermediate_key]).(*TomlTree)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// encodes a string to a TOML-compliant string value
|
// encodes a string to a TOML-compliant string value
|
||||||
|
|||||||
Reference in New Issue
Block a user