-
Notifications
You must be signed in to change notification settings - Fork 831
/
parser.go
218 lines (176 loc) · 3.79 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
package dotenv
import (
"bufio"
"bytes"
"fmt"
"regexp"
"strings"
"go.mozilla.org/sops/v3"
)
const (
stateKey int = iota
stateValue
stateComment
)
var KeyRegexp = regexp.MustCompile(`^[A-Za-z_]+[A-Za-z0-9_]*$`)
func Parse(data []byte) (items []sops.TreeItem, err error) {
nextState := stateKey
var currentKey string
for _, token := range tokenize(data) {
state := nextState
if token == "#" {
nextState = stateComment
continue
}
if token == "=" {
nextState = stateValue
continue
}
if isAllWhitespace(token) {
continue
}
if state == stateComment {
nextState = stateKey
items = append(items, sops.TreeItem{Key: sops.Comment{token}, Value: nil})
}
if state == stateKey {
if KeyRegexp.MatchString(token) {
currentKey = token
} else {
return nil, fmt.Errorf("invalid dotenv key: %q", token)
}
}
if state == stateValue {
nextState = stateKey
items = append(items, sops.TreeItem{Key: currentKey, Value: parseValue(token)})
}
}
return
}
func parseValue(value string) string {
if value[0] == '"' {
expanded := strings.Replace(value, "\\n", "\n", -1)
expanded = strings.Replace(expanded, "\\\n", "\\n", -1)
return expanded[1 : len(expanded)-1]
}
if value[0] == '\'' {
return value[1 : len(value)-1]
}
return strings.Trim(value, "\t ")
}
func tokenize(data []byte) []string {
scanner := bufio.NewScanner(bytes.NewReader(data))
nextState := stateKey
split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
state := nextState
if len(data) == 0 {
return 0, nil, nil
}
if isWhitespace(data[0]) {
advance, token, err = consumeWhitespace(data)
return
}
if state == stateKey && data[0] == '#' {
advance, token, err = 1, []byte{data[0]}, nil
nextState = stateComment
return
}
if state == stateComment {
advance, token, err = consumeLine(data)
nextState = stateKey
return
}
if data[0] == '=' {
advance, token, err = 1, []byte{data[0]}, nil
nextState = stateValue
return
}
if state == stateKey {
advance, token, err = consumeKey(data)
nextState = stateValue
return
}
if state == stateValue && (data[0] == '"' || data[0] == '\'') {
advance, token, err = consumeQuoted(data)
nextState = stateKey
return
}
if state == stateValue {
advance, token, err = consumeLine(data)
nextState = stateKey
return
}
return
}
scanner.Split(split)
tokens := []string{}
for scanner.Scan() {
tokens = append(tokens, scanner.Text())
}
return tokens
}
func consumeWhitespace(data []byte) (advance int, token []byte, err error) {
for _, b := range data {
if isWhitespace(b) {
advance += 1
token = append(token, b)
} else {
break
}
}
return
}
func consumeQuoted(data []byte) (advance int, token []byte, err error) {
advance = 1
stopAt := data[0]
escaping := false
token = append(token, stopAt)
for _, b := range data[1:] {
advance += 1
if !escaping && b == stopAt {
token = append(token, stopAt)
return
}
if escaping {
token = append(token, '\\')
escaping = false
}
if b == '\\' {
escaping = true
continue
}
token = append(token, b)
}
return 0, nil, fmt.Errorf("missing closing quotation mark")
}
func consumeKey(data []byte) (advance int, token []byte, err error) {
for _, b := range data {
if b == '=' {
return
}
advance += 1
token = append(token, b)
}
return
}
func consumeLine(data []byte) (advance int, token []byte, err error) {
for _, b := range data {
advance += 1
if b == '\n' || b == '\r' {
return
}
token = append(token, b)
}
return
}
func isWhitespace(b byte) bool {
return b == ' ' || b == '\t' || b == '\r' || b == '\n'
}
func isAllWhitespace(s string) bool {
for _, b := range []byte(s) {
if !isWhitespace(b) {
return false
}
}
return true
}