go-toml/lexer/items.go

55 lines
1.5 KiB
Go

package lexer
import "fmt"
// itemType represents the type of lexer items.
type itemType int
// Definition of all the lexer item types for the TOML lexer.
const (
ItemError itemType = iota // An error occurred
ItemEOF // End of input reached
ItemComment // Comment string, starts with # till en of line
ItemKey // Key of a key/value pair
ItemKeyDot // Dot for a dotted key
ItemKeyValueAssignment // Equal sign for a key/value pair assignment
ItemStringValue // A value of type string
)
// Item represents a lexer item returned from the scanner.
type Item struct {
Type itemType //Type, e.g. itemNumber, itemSquareBracket
Value string // Value, e.g. "10.42", "["
}
// String returns a string representation of the lexer item.
func (i Item) String() string {
switch i.Type {
case ItemEOF:
return "EOF"
case ItemError:
return "Error: " + i.Value
}
return fmt.Sprintf("%s(%q)", i.Type, i.Value)
}
// String returns a string representation of the lexer item type.
func (i itemType) String() string {
switch i {
case ItemError:
return "Error"
case ItemComment:
return "Comment"
case ItemKey:
return "Key"
case ItemKeyDot:
return "KeyDot"
case ItemKeyValueAssignment:
return "Assignment"
case ItemStringValue:
return "StringValue"
default:
return fmt.Sprintf("<type id %d>", i)
}
}