-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
411 lines (393 loc) · 8.78 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package lscolors
import (
"errors"
"fmt"
"strings"
)
type LSColors struct {
LeftOfColorSequence *string // lc
RightOfColorSequence *string // rc
EndColor *string // ec
ResetOrdinaryColor *string // rs
Normal *string // no
FileDefault *string // fi
Directory *string // di
Symlink *string // ln
Pipe *string // pi
Socket *string // so
BlockDevice *string // bd
CharDevice *string // cd
MissingFile *string // mi
OrphanedSymlink *string // or
Executable *string // ex
Door *string // do
SetUID *string // su
SetGID *string // sg
Sticky *string // st
OtherWritable *string // ow
OtherWritableSticky *string // tw
Cap *string // ca
MultiHardLink *string // mh
ClearToEndOfLine *string // cl
Extensions []LSColorsExtensions // *.xxx
Unknowns map[string][]string
}
func newStr(s string) *string {
return &s
}
func LSColorsDefault() LSColors {
return LSColors{
LeftOfColorSequence: newStr("\033["),
RightOfColorSequence: newStr("m"),
EndColor: nil,
ResetOrdinaryColor: newStr("0"),
Directory: newStr("01;34"),
Symlink: newStr("01;36"),
Pipe: newStr("33"),
Socket: newStr("01;35"),
BlockDevice: newStr("01;33"),
CharDevice: newStr("01;33"),
MissingFile: nil,
OrphanedSymlink: nil,
Executable: newStr("01;32"),
Door: newStr("01;35"),
SetUID: newStr("37;41"),
SetGID: newStr("30;43"),
Sticky: newStr("37;44"),
OtherWritable: newStr("34;42"),
OtherWritableSticky: newStr("30;42"),
Cap: nil,
MultiHardLink: nil,
ClearToEndOfLine: newStr("\033[K"),
Unknowns: make(map[string][]string),
}
}
type LSColorsExtensions struct {
Extension string
Sequence string
ExactMatch bool
}
type ErrorWithPosition struct {
err error
pos int
}
func (e *ErrorWithPosition) Error() string {
return fmt.Sprintf("error in column %d: %s", e.pos, e.err.Error())
}
func (e *ErrorWithPosition) Unwrap() error {
return e.err
}
func (e *ErrorWithPosition) Position() int {
return e.pos
}
func addPosition(e error, basePosition int) error {
if err, ok := e.(*ErrorWithPosition); ok {
return &ErrorWithPosition{
err: err.err,
pos: err.pos + basePosition,
}
} else {
return &ErrorWithPosition{
err: e,
pos: basePosition,
}
}
}
type parseLSColorsState int
const (
ps_START parseLSColorsState = iota
ps_INDICATOR
ps_EXTENSION
)
/*
ParseLS_COLORS parse string as LS_COLORS environment variable.
if allowUnknown is true, it accepts unknown indicator and add to .Unknowns field.
if fails, it returns *ErrorWithPosition error. error position can be got by calling Position()
*/
func ParseLS_COLORS(s string, allowUnknown bool) (*LSColors, error) {
ret := LSColorsDefault()
state := ps_START
i := 0
extensions := []LSColorsExtensions{}
LOOP:
for {
switch state {
case ps_START:
if len(s) <= i {
break LOOP
}
switch s[i] {
case ':':
i++
case '*':
i++
state = ps_EXTENSION
default:
state = ps_INDICATOR
}
case ps_INDICATOR:
label, err := getToken(s[i:], true)
if err != nil {
return nil, addPosition(err, i)
}
i += len(label)
if s[i] != '=' {
return nil, &ErrorWithPosition{
err: fmt.Errorf("unexpected character '%c'", s[i]),
pos: i,
}
}
i++
seq, err := getToken(s[i:], false)
if err != nil {
return nil, addPosition(err, i)
}
err = setByIndicator(&ret, label, seq, allowUnknown)
if err != nil {
return nil, addPosition(err, i-1-len(label))
}
i += len(seq)
state = ps_START
case ps_EXTENSION:
ext := LSColorsExtensions{
ExactMatch: false,
}
pattern, err := getToken(s[i:], true)
if err != nil {
return nil, addPosition(err, i)
}
i += len(pattern)
ext.Extension = pattern
if s[i] != '=' {
return nil, &ErrorWithPosition{
err: fmt.Errorf("unexpected character '%c'", s[i]),
pos: i,
}
}
i++
seq, err := getToken(s[i:], false)
if err != nil {
return nil, addPosition(err, i)
}
ext.Sequence = seq
extensions = append(extensions, ext)
i += len(seq)
state = ps_START
default:
return nil, fmt.Errorf("unexpected state: %v", state)
}
}
insensitiveCount := make(map[string]int, len(extensions))
for _, ext := range extensions {
l := strings.ToLower(ext.Extension)
if _, exists := insensitiveCount[l]; !exists {
insensitiveCount[l] = 0
}
insensitiveCount[l] = insensitiveCount[l] + 1
}
nExtensions := len(extensions)
ret.Extensions = make([]LSColorsExtensions, len(extensions))
for i, ext := range extensions {
l := strings.ToLower(ext.Extension)
if 1 < insensitiveCount[l] {
ext.ExactMatch = true
}
ret.Extensions[nExtensions-1-i] = ext
}
return &ret, nil
}
func setByIndicator(c *LSColors, label string, value string, allowUnknown bool) error {
switch label {
case "lc":
c.LeftOfColorSequence = &value
case "rc":
c.RightOfColorSequence = &value
case "ec":
c.EndColor = &value
case "rs":
c.ResetOrdinaryColor = &value
case "no":
c.Normal = &value
case "fi":
c.FileDefault = &value
case "di":
c.Directory = &value
case "ln":
c.Symlink = &value
case "pi":
c.Pipe = &value
case "so":
c.Socket = &value
case "bd":
c.BlockDevice = &value
case "cd":
c.CharDevice = &value
case "mi":
c.MissingFile = &value
case "or":
c.OrphanedSymlink = &value
case "ex":
c.Executable = &value
case "do":
c.Door = &value
case "su":
c.SetUID = &value
case "sg":
c.SetGID = &value
case "st":
c.Sticky = &value
case "ow":
c.OtherWritable = &value
case "tw":
c.OtherWritableSticky = &value
case "ca":
c.Cap = &value
case "mh":
c.MultiHardLink = &value
case "cl":
c.ClearToEndOfLine = &value
default:
if allowUnknown {
c.Unknowns[label] = append(c.Unknowns[label], value)
} else {
return fmt.Errorf("unrecognized prefix: %#v", label)
}
}
return nil
}
type getTokenState int
const (
st_GND getTokenState = iota
st_BACKSLASH
st_OCTAL
st_HEX
st_CARET
)
// getToken takes string as part of LS_COLORS variable
func getToken(s string, equals_end bool) (string, error) {
state := st_GND
buf := strings.Builder{}
i := 0
var num byte = 0
LOOP:
for {
switch state {
case st_GND:
if len(s) <= i {
break LOOP
}
switch s[i] {
case ':':
break LOOP
case '\\':
state = st_BACKSLASH
i++
case '^':
state = st_CARET
i++
case '=':
if equals_end {
break LOOP
}
fallthrough
default:
buf.WriteByte(s[i])
i++
}
case st_BACKSLASH:
if len(s) <= i {
return "", &ErrorWithPosition{
err: errors.New("unexpected end of string"),
pos: i,
}
}
switch s[i] {
case '0':
fallthrough
case '1':
fallthrough
case '2':
fallthrough
case '3':
fallthrough
case '4':
fallthrough
case '5':
fallthrough
case '6':
fallthrough
case '7':
state = st_OCTAL
num = s[i] - '0'
case 'x':
fallthrough
case 'X':
state = st_HEX
num = 0
case 'a':
num = '\a'
case 'b':
num = '\b'
case 'e':
num = 27
case 'f':
num = '\f'
case 'n':
num = '\n'
case 'r':
num = '\r'
case 't':
num = '\t'
case 'v':
num = '\v'
case '?':
num = 127
case '_':
num = ' '
default:
num = s[i]
}
if state == st_BACKSLASH {
buf.WriteByte(num)
state = st_GND
i++
}
i++
case st_OCTAL:
if '0' <= s[i] && s[i] <= '7' {
buf.WriteByte(num)
state = st_GND
} else {
num = (num << 3) + (s[i] - '0')
}
case st_HEX:
if '0' <= s[i] && s[i] <= '9' {
num = (num << 4) + (s[i] - '0')
} else if 'a' <= s[i] && s[i] <= 'f' {
num = (num << 4) + (s[i] - 'a') + 10
} else if 'A' <= s[i] && s[i] <= 'F' {
num = (num << 4) + (s[i] - 'A') + 10
} else {
buf.WriteByte(num)
state = st_GND
}
case st_CARET:
state = st_GND
if '@' <= s[i] && s[i] <= '~' {
buf.WriteByte(s[i] & 037)
i++
} else if s[i] == '?' {
buf.WriteByte(127)
i++
} else {
return "", &ErrorWithPosition{
err: fmt.Errorf("unexpected character: '%c'", s[i]),
pos: i,
}
}
default:
return "", fmt.Errorf("unexpected state: %v", state)
}
}
return buf.String(), nil
}