-
Notifications
You must be signed in to change notification settings - Fork 8
/
scanner.go
212 lines (192 loc) · 5.22 KB
/
scanner.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package flag
import (
"strings"
)
const (
argumentFlagSplittable = iota + 1
argumentFlag
argumentValue
argumentPending
)
type argument struct {
Type int
Value string
// attached value by '=', boolean flag value is optional,
// it can only use this approach to change it's value as `false`, otherwise
// it will affects later positional flag and non-flag value parsing.
AttachValid bool
Attached string
}
type scanArgs struct {
Flags []argument
FirstSubset string
Sets map[string]*scanArgs
}
type scanner struct {
SubsetStack []string
Result scanArgs
}
func (s *scanner) appendArg(arg argument, isSubset bool) {
curr := &s.Result
for _, subset := range s.SubsetStack {
set := curr.Sets[subset]
if set == nil {
set = &scanArgs{}
if curr.Sets == nil {
curr.Sets = make(map[string]*scanArgs)
}
curr.Sets[subset] = set
}
if len(curr.Sets) == 1 {
curr.FirstSubset = subset
}
curr = set
}
if !isSubset || len(curr.Flags) == 0 {
curr.Flags = append(curr.Flags, arg)
}
}
func (s *scanner) isAlphabet(r rune) bool {
return ('a' <= r && r <= 'z') || 'A' <= r && r <= 'Z'
}
func (s *scanner) checkSplits(f *FlagSet, rs []rune) (allFlag, firstFlag bool) {
allFlag = true
for i, r := range rs {
isFlag := f.isFlagOrSubset("-" + string(r))
if isFlag {
if i == 0 && s.isAlphabet(r) && !s.isAlphabet(rs[i+1]) {
firstFlag = true
}
} else {
allFlag = false
break
}
}
return
}
func (s *scanner) stackTopFlagSet(f *FlagSet, stack []string) *FlagSet {
curr := f
for _, subset := range stack {
curr = &curr.subsets[curr.subsetIndexes[subset]]
}
return curr
}
func (s *scanner) reverseIterStack(f *FlagSet, fn func(*FlagSet, int) (result, continu bool)) (result bool) {
for i := len(s.SubsetStack); ; {
currSet := s.stackTopFlagSet(f, s.SubsetStack[:i])
result, continu := fn(currSet, i)
if !continu {
return result
}
i--
if i < 0 {
result, _ := fn(nil, i)
return result
}
}
}
func (s *scanner) tryAppendFlagOrSubset(f *FlagSet, arg argument, mustAppend bool) bool {
return s.reverseIterStack(f, func(currSet *FlagSet, i int) (result, continu bool) {
if currSet == nil {
if mustAppend {
s.appendArg(arg, false)
}
return mustAppend, false
}
isFlag, isSubset := currSet.isFlag(arg.Value), currSet.isSubset(arg.Value)
if !isFlag && !isSubset {
return false, true
}
s.SubsetStack = s.SubsetStack[:i]
if isSubset {
s.SubsetStack = append(s.SubsetStack, arg.Value)
}
arg.Type = argumentFlag
s.appendArg(arg, isSubset)
return true, false
})
}
func (s *scanner) appendSplittable(f *FlagSet, arg argument) {
flagRunes := []rune(arg.Value[1:])
s.reverseIterStack(f, func(currSet *FlagSet, i int) (result, continu bool) {
if currSet == nil {
arg.Type = argumentFlag
s.appendArg(arg, false)
return false, false
}
allFlag, firstFlag := s.checkSplits(currSet, flagRunes)
if allFlag || firstFlag {
s.SubsetStack = s.SubsetStack[:i]
}
if allFlag {
for i, r := range flagRunes {
if i == len(flagRunes)-1 {
s.appendArg(argument{Type: argumentFlag, Value: "-" + string(r), Attached: arg.Attached, AttachValid: arg.AttachValid}, false)
} else {
s.appendArg(argument{Type: argumentFlag, Value: "-" + string(r)}, false)
}
}
return false, false
}
if firstFlag && !arg.AttachValid {
s.appendArg(argument{Type: argumentFlag, Value: "-" + string(flagRunes[0])}, false)
s.appendArg(argument{Type: argumentValue, Value: string(flagRunes[1:])}, false)
return false, false
}
return false, true
})
}
func (s *scanner) append(f *FlagSet, arg argument) {
switch arg.Type {
case argumentValue:
s.appendArg(arg, false)
case argumentFlag, argumentPending:
s.tryAppendFlagOrSubset(f, arg, true)
case argumentFlagSplittable:
s.appendSplittable(f, arg)
}
}
func (s *scanner) canBeSplitBy(arg, sep string) bool {
index := strings.Index(arg, sep)
return index > 0 && index <= len(arg)-1
}
func (s *scanner) scanArg(f *FlagSet, args []string, i int) (consumed int) {
curr := args[i]
consumed = 1
switch {
case i == 0:
s.append(f, argument{Type: argumentFlag, Value: curr})
case curr == "--":
if i != len(args)-1 {
curr = args[i+1]
consumed++
s.append(f, argument{Type: argumentValue, Value: curr})
}
case curr == "--*":
for j := i + 1; j < len(args); j++ {
curr = args[j]
s.append(f, argument{Type: argumentValue, Value: curr})
consumed++
}
case strings.HasPrefix(curr, "-") && s.canBeSplitBy(curr[1:], "="):
secs := strings.SplitN(curr, "=", 2)
typ := argumentFlag
if !strings.HasPrefix(secs[0], "--") && len(secs[0]) >= 3 {
typ = argumentFlagSplittable
}
s.append(f, argument{Type: typ, Value: secs[0], Attached: secs[1], AttachValid: true})
case strings.HasPrefix(curr, "--"):
s.append(f, argument{Type: argumentFlag, Value: curr})
case curr != flagNamePositional && s.tryAppendFlagOrSubset(f, argument{Type: argumentPending, Value: curr}, false):
case curr != "-" && strings.HasPrefix(curr, "-"):
s.append(f, argument{Type: argumentFlagSplittable, Value: curr})
default:
s.append(f, argument{Type: argumentValue, Value: curr})
}
return
}
func (s *scanner) scan(f *FlagSet, args []string) {
for i, l := 0, len(args); i < l; {
i += s.scanArg(f, args, i)
}
}