2014-08-05 16:17:40 -04:00
|
|
|
package parser
|
|
|
|
|
2014-10-14 23:33:11 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
2015-01-27 10:57:34 -05:00
|
|
|
"unicode"
|
2014-10-14 23:33:11 -04:00
|
|
|
)
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
// splitCommand takes a single line of text and parses out the cmd and args,
|
|
|
|
// which are used for dispatching to more exact parsing functions.
|
2015-01-27 10:57:34 -05:00
|
|
|
func splitCommand(line string) (string, []string, string, error) {
|
2015-02-04 12:34:25 -05:00
|
|
|
var args string
|
2015-01-27 10:57:34 -05:00
|
|
|
var flags []string
|
2015-02-04 12:34:25 -05:00
|
|
|
|
2015-01-15 09:49:48 -05:00
|
|
|
// Make sure we get the same results irrespective of leading/trailing spaces
|
2015-07-22 01:29:03 -04:00
|
|
|
cmdline := tokenWhitespace.Split(strings.TrimSpace(line), 2)
|
2015-02-04 12:34:25 -05:00
|
|
|
cmd := strings.ToLower(cmdline[0])
|
2014-10-14 23:33:11 -04:00
|
|
|
|
2015-02-04 12:34:25 -05:00
|
|
|
if len(cmdline) == 2 {
|
2015-01-27 10:57:34 -05:00
|
|
|
var err error
|
|
|
|
args, flags, err = extractBuilderFlags(cmdline[1])
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, "", err
|
|
|
|
}
|
2014-10-14 23:33:11 -04:00
|
|
|
}
|
|
|
|
|
2015-01-27 10:57:34 -05:00
|
|
|
return cmd, flags, strings.TrimSpace(args), nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2015-01-27 10:57:34 -05:00
|
|
|
func extractBuilderFlags(line string) (string, []string, error) {
|
|
|
|
// Parses the BuilderFlags and returns the remaining part of the line
|
|
|
|
|
|
|
|
const (
|
|
|
|
inSpaces = iota // looking for start of a word
|
|
|
|
inWord
|
|
|
|
inQuote
|
|
|
|
)
|
|
|
|
|
|
|
|
words := []string{}
|
|
|
|
phase := inSpaces
|
|
|
|
word := ""
|
|
|
|
quote := '\000'
|
|
|
|
blankOK := false
|
|
|
|
var ch rune
|
|
|
|
|
|
|
|
for pos := 0; pos <= len(line); pos++ {
|
|
|
|
if pos != len(line) {
|
|
|
|
ch = rune(line[pos])
|
|
|
|
}
|
|
|
|
|
|
|
|
if phase == inSpaces { // Looking for start of word
|
|
|
|
if pos == len(line) { // end of input
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if unicode.IsSpace(ch) { // skip spaces
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only keep going if the next word starts with --
|
|
|
|
if ch != '-' || pos+1 == len(line) || rune(line[pos+1]) != '-' {
|
|
|
|
return line[pos:], words, nil
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:52:00 -04:00
|
|
|
phase = inWord // found something with "--", fall through
|
2015-01-27 10:57:34 -05:00
|
|
|
}
|
|
|
|
if (phase == inWord || phase == inQuote) && (pos == len(line)) {
|
|
|
|
if word != "--" && (blankOK || len(word) > 0) {
|
|
|
|
words = append(words, word)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if phase == inWord {
|
|
|
|
if unicode.IsSpace(ch) {
|
|
|
|
phase = inSpaces
|
|
|
|
if word == "--" {
|
|
|
|
return line[pos:], words, nil
|
|
|
|
}
|
|
|
|
if blankOK || len(word) > 0 {
|
|
|
|
words = append(words, word)
|
|
|
|
}
|
|
|
|
word = ""
|
|
|
|
blankOK = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '\'' || ch == '"' {
|
|
|
|
quote = ch
|
|
|
|
blankOK = true
|
|
|
|
phase = inQuote
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '\\' {
|
|
|
|
if pos+1 == len(line) {
|
|
|
|
continue // just skip \ at end
|
|
|
|
}
|
|
|
|
pos++
|
|
|
|
ch = rune(line[pos])
|
|
|
|
}
|
|
|
|
word += string(ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if phase == inQuote {
|
|
|
|
if ch == quote {
|
|
|
|
phase = inWord
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch == '\\' {
|
|
|
|
if pos+1 == len(line) {
|
|
|
|
phase = inWord
|
|
|
|
continue // just skip \ at end
|
|
|
|
}
|
|
|
|
pos++
|
|
|
|
ch = rune(line[pos])
|
|
|
|
}
|
|
|
|
word += string(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", words, nil
|
|
|
|
}
|