62 lines
1.4 KiB
Go
62 lines
1.4 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 = "key" // set key name
|
|
cKeyDot = "keydot" // new key stack level
|
|
cAssign = "assign" // assign a value
|
|
csetStrVal = "string" // set a string value
|
|
csetIntVal = "integer" // set an integer 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
|
|
// }
|