53 lines
1.3 KiB
Go
53 lines
1.3 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
|
|
ItemAssignment // Value assignment coming up (=)
|
|
ItemString // A value of type string
|
|
)
|
|
|
|
// Item represents a lexer item returned from the scanner.
|
|
type Item struct {
|
|
Type itemType //Type, e.g. ItemComment, ItemString
|
|
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 ItemKey:
|
|
return fmt.Sprintf("[%s]", i.Value)
|
|
case ItemKeyDot:
|
|
return "."
|
|
case ItemAssignment:
|
|
return "="
|
|
}
|
|
return fmt.Sprintf("%s(%s)", i.Type, i.Value)
|
|
}
|
|
|
|
// String returns a string representation of the lexer item type.
|
|
func (i itemType) String() string {
|
|
switch i {
|
|
case ItemError:
|
|
return "ERR"
|
|
case ItemComment:
|
|
return "#"
|
|
case ItemString:
|
|
return "STR"
|
|
default:
|
|
panic(fmt.Sprintf("No translation available for type id %d", i))
|
|
}
|
|
}
|