go-toml/toml.go

68 lines
1.9 KiB
Go

package parser
import (
"fmt"
"strings"
"git.makaay.nl/mauricem/go-parsekit/tokenize"
)
// Easy access to the parsekit.tokenize definitions.
var c, a, m, tok = tokenize.C, tokenize.A, tokenize.M, tokenize.T
type cmdType string
// Command types that are emitted by the parser.
const (
cComment cmdType = "comment" // a # comment at the end of the line
cKey cmdType = "key" // set key name
cKeyDot cmdType = "keydot" // new key stack level
cAssign cmdType = "assign" // assign a value
csetStrVal cmdType = "string" // set a string value
csetIntVal cmdType = "integer" // set an integer value
csetFloatVal cmdType = "float" // set a float value
csetBoolVal cmdType = "boolean" // set a boolean value
coffsetDateTime cmdType = "offset_datetime" // set a date/time value with timezone information
clocalDateTime cmdType = "datetime" // set a local date/time value
clocalDate cmdType = "date" // set a local date value
clocalTime cmdType = "time" // set a local time value
)
type parser struct {
commands []cmd
keyStack []string
}
type cmd struct {
command cmdType
args []interface{}
}
func (cmd *cmd) String() string {
if len(cmd.args) == 0 {
return fmt.Sprintf("%s", cmd.command)
}
args := make([]string, len(cmd.args))
for i, arg := range cmd.args {
switch arg.(type) {
case string:
args[i] = fmt.Sprintf("%q", arg)
default:
args[i] = fmt.Sprintf("%v", arg)
}
}
return fmt.Sprintf("%s(%s)", cmd.command, strings.Join(args, ", "))
}
func (p *parser) emitCommand(command cmdType, args ...interface{}) {
c := cmd{command: command, args: args}
p.commands = append(p.commands, c)
}
// Parse starts the parser for the provided input.
// func Parse(input interface{}) []cmd {
// p := &parser{}
// parse.New(p.startKeyValuePair)(input)
// return p.commands
// }