forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvslice.go
230 lines (209 loc) · 4.86 KB
/
kvslice.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
package config
import (
"errors"
"strconv"
"strings"
)
// parseKVSlice parses a configuration string in the form
//
// key=val;key=val,key=val;key=val
//
// into a list of string maps. maps are separated by comma and key/value
// pairs within a map are separated by semicolons. The first key/value
// pair of a map can omit the key and its value will be stored under the
// empty key. This allows support of legacy configuration formats which
// are
//
// val;opt1=val1;opt2=val2;...
func parseKVSlice(in string) ([]map[string]string, error) {
var keyOrFirstVal string
maps := []map[string]string{}
m := map[string]string{}
newMap := func() {
if len(m) > 0 {
maps = append(maps, m)
m = map[string]string{}
}
}
s := []rune(in)
state := stateFirstKey
for {
if len(s) == 0 {
break
}
typ, val, n := lex(s)
s = s[n:]
// fmt.Println("parse:", "typ:", typ, "val:", val, "state:", string(state), "s:", string(s))
switch state {
case stateFirstKey:
switch typ {
case itemText:
keyOrFirstVal = strings.TrimSpace(val)
state = stateAfterFirstKey
case itemComma, itemSemicolon:
continue
default:
return nil, errors.New(val)
}
// the first value is allowed to omit the key
// a=b;c=d and b;c=d are valid
case stateAfterFirstKey:
switch typ {
case itemEqual:
state = stateVal
case itemComma:
if keyOrFirstVal != "" {
m[""] = keyOrFirstVal
}
newMap()
state = stateFirstKey
case itemSemicolon:
if keyOrFirstVal != "" {
m[""] = keyOrFirstVal
}
state = stateKey
default:
return nil, errors.New(val)
}
case stateKey:
switch typ {
case itemText:
keyOrFirstVal = strings.TrimSpace(val)
state = stateEqual
case itemComma, itemSemicolon:
continue
default:
return nil, errors.New(val)
}
case stateEqual:
switch typ {
case itemEqual:
state = stateVal
default:
return nil, errors.New(val)
}
case stateVal:
switch typ {
case itemText:
m[keyOrFirstVal] = val
state = stateNextKVOrNewMap
default:
return nil, errors.New(val)
}
case stateNextKVOrNewMap:
switch typ {
case itemComma:
newMap()
state = stateFirstKey
case itemSemicolon:
state = stateKey
default:
return nil, errors.New(val)
}
}
}
if state == stateAfterFirstKey && keyOrFirstVal != "" {
m[""] = keyOrFirstVal
}
if len(m) > 0 {
maps = append(maps, m)
}
if len(maps) == 0 {
return nil, nil
}
return maps, nil
}
type itemType string
const (
itemText itemType = "TEXT"
itemEqual = "EQUAL"
itemSemicolon = "SEMICOLON"
itemComma = "COMMA"
itemError = "ERROR"
)
func (t itemType) String() string {
return string(t)
}
type state string
const (
// lexer states
stateStart state = "start"
stateText = "text"
stateQText = "qtext"
stateQTextEnd = "qtextend"
stateQTextEsc = "qtextesc"
// parser states
stateFirstKey = "first-key"
stateKey = "key"
stateEqual = "equal"
stateVal = "val"
stateNextKVOrNewMap = "comma-semicolon"
stateAfterFirstKey = "equal-comma-semicolon"
)
func lex(s []rune) (itemType, string, int) {
isComma := func(r rune) bool { return r == ',' }
isSemicolon := func(r rune) bool { return r == ';' }
isEqual := func(r rune) bool { return r == '=' }
isEscape := func(r rune) bool { return r == '\\' }
isQuote := func(r rune) bool { return r == '"' || r == '\'' }
var quote rune
state := stateStart
for i, r := range s {
// fmt.Println("lex:", "i:", i, "r:", string(r), "state:", string(state))
switch state {
case stateStart:
switch {
case isComma(r):
return itemComma, string(r), 1
case isSemicolon(r):
return itemSemicolon, string(r), 1
case isEqual(r):
return itemEqual, string(r), 1
case isQuote(r):
quote = r
state = stateQText
default:
state = stateText
}
case stateText:
switch {
case isComma(r) || isSemicolon(r) || isEqual(r):
return itemText, string(s[:i]), i
default:
// state = stateText
}
case stateQText:
switch {
case r == quote:
state = stateQTextEnd
case isEscape(r):
state = stateQTextEsc
default:
// state = stateQText
}
case stateQTextEsc:
state = stateQText
case stateQTextEnd:
v, err := strconv.Unquote(string(s[:i]))
if err != nil {
return itemError, "invalid escape sequence", i
}
return itemText, v, i
}
}
// fmt.Println("lex:", "state:", string(state))
switch state {
case stateQText:
return itemError, "unbalanced quotes", len(s)
case stateQTextEsc:
return itemError, "unterminated escape sequence", len(s)
case stateQTextEnd:
v, err := strconv.Unquote(string(s))
if err != nil {
return itemError, "invalid escape sequence", len(s)
}
return itemText, v, len(s)
default:
return itemText, string(s), len(s)
}
}