forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.go
351 lines (275 loc) · 8.05 KB
/
parser.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package cli
import (
"fmt"
"strconv"
)
type Parser interface {
Match([]string) ([]string, bool)
Capture([]string) ([]string, map[string]interface{})
}
type EqualParser struct {
value string
}
func (self EqualParser) Match(values []string) ([]string, bool) {
if len(values) == 0 {
return values, false
}
if self.value == values[0] {
return values[1:], true
}
return values, false
}
func (self EqualParser) Capture(values []string) ([]string, map[string]interface{}) {
remainingValues, _ := self.Match(values)
return remainingValues, nil
}
func (self EqualParser) String() string {
return fmt.Sprintf("EqualParser '%s'", self.value)
}
type CaptureGroupParser struct {
value string
}
func (self CaptureGroupParser) Match(values []string) ([]string, bool) {
if len(values) == 0 {
return values, false
}
return values[1:], true
}
func (self CaptureGroupParser) key() string {
return self.value[1 : len(self.value)-1]
}
func (self CaptureGroupParser) Capture(values []string) ([]string, map[string]interface{}) {
if remainingValues, ok := self.Match(values); ok {
return remainingValues, map[string]interface{}{self.key(): values[0]}
}
return values, nil
}
func (self CaptureGroupParser) String() string {
return fmt.Sprintf("CaptureGroupParser '%s'", self.value)
}
type BoolFlagParser struct {
pattern string
key string
omitValue bool
defaultValue bool
}
func (self BoolFlagParser) Match(values []string) ([]string, bool) {
if self.omitValue {
return flagKeyMatch(self.pattern, values, 0)
}
remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
if !ok {
return remaining, false
}
// Check that value is a valid boolean
if _, err := strconv.ParseBool(value); err != nil {
return remaining, false
}
return remaining, true
}
func (self BoolFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
if self.omitValue {
remaining, ok := flagKeyMatch(self.pattern, values, 0)
return remaining, map[string]interface{}{self.key: ok}
}
remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
if !ok {
return remaining, map[string]interface{}{self.key: self.defaultValue}
}
b, _ := strconv.ParseBool(value)
return remaining, map[string]interface{}{self.key: b}
}
func (self BoolFlagParser) String() string {
return fmt.Sprintf("BoolFlagParser '%s'", self.pattern)
}
type StringFlagParser struct {
pattern string
key string
defaultValue string
}
func (self StringFlagParser) Match(values []string) ([]string, bool) {
remaining, _, ok := flagKeyValueMatch(self.pattern, values, 0)
return remaining, ok
}
func (self StringFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
if !ok {
return remaining, map[string]interface{}{self.key: self.defaultValue}
}
return remaining, map[string]interface{}{self.key: value}
}
func (self StringFlagParser) String() string {
return fmt.Sprintf("StringFlagParser '%s'", self.pattern)
}
type IntFlagParser struct {
pattern string
key string
defaultValue int64
}
func (self IntFlagParser) Match(values []string) ([]string, bool) {
remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
if !ok {
return remaining, false
}
// Check that value is a valid integer
if _, err := strconv.ParseInt(value, 10, 64); err != nil {
return remaining, false
}
return remaining, true
}
func (self IntFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
remaining, value, ok := flagKeyValueMatch(self.pattern, values, 0)
if !ok {
return remaining, map[string]interface{}{self.key: self.defaultValue}
}
n, _ := strconv.ParseInt(value, 10, 64)
return remaining, map[string]interface{}{self.key: n}
}
func (self IntFlagParser) String() string {
return fmt.Sprintf("IntFlagParser '%s'", self.pattern)
}
type StringSliceFlagParser struct {
pattern string
key string
defaultValue []string
}
func (self StringSliceFlagParser) Match(values []string) ([]string, bool) {
if len(values) < 2 {
return values, false
}
var remainingValues []string
for i := 0; i < len(values); i++ {
if values[i] == self.pattern && i+1 < len(values) {
i++
continue
}
remainingValues = append(remainingValues, values[i])
}
return remainingValues, len(values) != len(remainingValues)
}
func (self StringSliceFlagParser) Capture(values []string) ([]string, map[string]interface{}) {
remainingValues, ok := self.Match(values)
if !ok {
return values, map[string]interface{}{self.key: self.defaultValue}
}
var captured []string
for i := 0; i < len(values); i++ {
if values[i] == self.pattern && i+1 < len(values) {
captured = append(captured, values[i+1])
}
}
return remainingValues, map[string]interface{}{self.key: captured}
}
func (self StringSliceFlagParser) String() string {
return fmt.Sprintf("StringSliceFlagParser '%s'", self.pattern)
}
type FlagParser struct {
parsers []Parser
}
func (self FlagParser) Match(values []string) ([]string, bool) {
remainingValues := values
for _, parser := range self.parsers {
remainingValues, _ = parser.Match(remainingValues)
}
return remainingValues, true
}
func (self FlagParser) Capture(values []string) ([]string, map[string]interface{}) {
captured := map[string]interface{}{}
remainingValues := values
for _, parser := range self.parsers {
var data map[string]interface{}
remainingValues, data = parser.Capture(remainingValues)
for key, value := range data {
captured[key] = value
}
}
return remainingValues, captured
}
func (self FlagParser) String() string {
return fmt.Sprintf("FlagParser %v", self.parsers)
}
type ShortCircuitParser struct {
parsers []Parser
}
func (self ShortCircuitParser) Match(values []string) ([]string, bool) {
remainingValues := values
for _, parser := range self.parsers {
var ok bool
remainingValues, ok = parser.Match(remainingValues)
if ok {
return remainingValues, true
}
}
return remainingValues, false
}
func (self ShortCircuitParser) Capture(values []string) ([]string, map[string]interface{}) {
if len(self.parsers) == 0 {
return values, nil
}
for _, parser := range self.parsers {
if _, ok := parser.Match(values); ok {
return parser.Capture(values)
}
}
// No parsers matched at this point,
// just return the capture value of the first one
return self.parsers[0].Capture(values)
}
func (self ShortCircuitParser) String() string {
return fmt.Sprintf("ShortCircuitParser %v", self.parsers)
}
type CompleteParser struct {
parsers []Parser
}
func (self CompleteParser) Match(values []string) ([]string, bool) {
remainingValues := copySlice(values)
for _, parser := range self.parsers {
var ok bool
remainingValues, ok = parser.Match(remainingValues)
if !ok {
return remainingValues, false
}
}
return remainingValues, len(remainingValues) == 0
}
func (self CompleteParser) Capture(values []string) ([]string, map[string]interface{}) {
remainingValues := copySlice(values)
data := map[string]interface{}{}
for _, parser := range self.parsers {
var captured map[string]interface{}
remainingValues, captured = parser.Capture(remainingValues)
for key, value := range captured {
data[key] = value
}
}
return remainingValues, data
}
func (self CompleteParser) String() string {
return fmt.Sprintf("CompleteParser %v", self.parsers)
}
func flagKeyValueMatch(key string, values []string, index int) ([]string, string, bool) {
if index > len(values)-2 {
return values, "", false
}
if values[index] == key {
value := values[index+1]
remaining := append(copySlice(values[:index]), values[index+2:]...)
return remaining, value, true
}
return flagKeyValueMatch(key, values, index+1)
}
func flagKeyMatch(key string, values []string, index int) ([]string, bool) {
if index > len(values)-1 {
return values, false
}
if values[index] == key {
remaining := append(copySlice(values[:index]), values[index+1:]...)
return remaining, true
}
return flagKeyMatch(key, values, index+1)
}
func copySlice(a []string) []string {
b := make([]string, len(a))
copy(b, a)
return b
}