residentsleeper/commands/parser.go

72 lines
1.1 KiB
Go
Raw Normal View History

package commands
type SpaceTokenizer struct {
Input string
}
func (t *SpaceTokenizer) NextToken() string {
if len(t.Input) == 0 {
return ""
}
ch := t.Input[0]
if ch == '"' {
// Scan until closing quote or end of string
for i := 1; i < len(t.Input); i++ {
if t.Input[i] == '"' && t.Input[i-1] != '\\' {
ret := t.Input[1:i]
if i+2 < len(t.Input) {
t.Input = t.Input[i+2:]
} else {
t.Input = ""
}
return ret
}
}
} else {
for i := 0; i < len(t.Input); i++ {
if t.Input[i] == ' ' {
ret := t.Input[0:i]
if i+1 < len(t.Input) {
t.Input = t.Input[i+1:]
} else {
t.Input = ""
}
return ret
}
}
}
ret := t.Input
t.Input = ""
return ret
}
func (t *SpaceTokenizer) Empty() bool {
return t.Input == ""
}
func NewSpaceTokenizer(input string) *SpaceTokenizer {
return &SpaceTokenizer{Input: input}
}
func ParseCommandArguments(command string) []string {
tokenizer := NewSpaceTokenizer(command)
arguments := make([]string, 0)
for {
if tokenizer.Empty() {
break
}
arguments = append(arguments, tokenizer.NextToken())
}
return arguments
}