forked from shomali11/commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommander.go
163 lines (138 loc) · 3.67 KB
/
commander.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package commander
import (
"regexp"
"strings"
"github.com/shomali11/proper"
)
const (
escapeCharacter = "\\"
ignoreCase = "(?i)"
parameterPattern = "<\\S+>"
lazyParameterPattern = "<\\S+\\?>"
spacePattern = "\\s+"
inputPattern = "(.+)"
lazyInputPattern = "(.+?)"
preCommandPattern = "(\\s|^)"
postCommandPattern = "(\\s|$)"
)
const (
notParameter = iota
greedyParameter
lazyParameter
)
var (
regexCharacters = []string{"\\", "(", ")", "{", "}", "[", "]", "?", ".", "+", "|", "^", "$"}
)
// NewCommand creates a new Command object from the format passed in
func NewCommand(format string) *Command {
tokens := tokenize(format)
expressions := generate(tokens)
return &Command{tokens: tokens, expressions: expressions}
}
// Token represents the Token object
type Token struct {
Word string
Type int
}
func (t Token) IsParameter() bool {
return t.Type != notParameter
}
// Command represents the Command object
type Command struct {
tokens []*Token
expressions []*regexp.Regexp
}
// Match takes in the command and the text received, attempts to find the pattern and extract the parameters
func (c *Command) Match(text string) (*proper.Properties, bool) {
if len(c.expressions) == 0 {
return nil, false
}
for _, expression := range c.expressions {
matches := expression.FindStringSubmatch(text)
if len(matches) == 0 {
continue
}
values := matches[2 : len(matches)-1]
valueIndex := 0
parameters := make(map[string]string)
for i := 0; i < len(c.tokens) && valueIndex < len(values); i++ {
token := c.tokens[i]
if !token.IsParameter() {
continue
}
parameters[token.Word] = values[valueIndex]
valueIndex++
}
return proper.NewProperties(parameters), true
}
return nil, false
}
// Tokenize returns Command info as tokens
func (c *Command) Tokenize() []*Token {
return c.tokens
}
func escape(text string) string {
for _, character := range regexCharacters {
text = strings.Replace(text, character, escapeCharacter+character, -1)
}
return text
}
func tokenize(format string) []*Token {
parameterRegex := regexp.MustCompile(parameterPattern)
lazyParameterRegex := regexp.MustCompile(lazyParameterPattern)
words := strings.Fields(format)
tokens := make([]*Token, len(words))
for i, word := range words {
switch {
case lazyParameterRegex.MatchString(word):
tokens[i] = &Token{Word: word[1 : len(word)-2], Type: lazyParameter}
case parameterRegex.MatchString(word):
tokens[i] = &Token{Word: word[1 : len(word)-1], Type: greedyParameter}
default:
tokens[i] = &Token{Word: word, Type: notParameter}
}
}
return tokens
}
func generate(tokens []*Token) []*regexp.Regexp {
regexps := []*regexp.Regexp{}
if len(tokens) == 0 {
return regexps
}
for index := len(tokens) - 1; index >= -1; index-- {
regex := compile(create(tokens, index))
regexps = append(regexps, regex)
}
return regexps
}
func create(tokens []*Token, boundary int) []*Token {
newTokens := []*Token{}
for i := 0; i < len(tokens); i++ {
if !tokens[i].IsParameter() || i <= boundary {
newTokens = append(newTokens, tokens[i])
}
}
return newTokens
}
func compile(tokens []*Token) *regexp.Regexp {
if len(tokens) == 0 {
return nil
}
pattern := preCommandPattern + getInputPattern(tokens[0])
for index := 1; index < len(tokens); index++ {
currentToken := tokens[index]
pattern += spacePattern + getInputPattern(currentToken)
}
pattern += postCommandPattern
return regexp.MustCompile(ignoreCase + pattern)
}
func getInputPattern(token *Token) string {
switch token.Type {
case lazyParameter:
return lazyInputPattern
case greedyParameter:
return inputPattern
default:
return escape(token.Word)
}
}