-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
102 lines (83 loc) · 1.76 KB
/
parser.go
File metadata and controls
102 lines (83 loc) · 1.76 KB
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
package main
import (
"errors"
"strings"
"unicode/utf8"
)
func ParseLine(line string) ([]string, error) {
parts := make([]string, 0, strings.Count(line, " "))
part := make([]rune, 0, len(line))
i := 0
for i < len(line) {
r, d := utf8.DecodeRuneInString(line[i:])
switch r {
case '"':
i += d
endFound := false
for !endFound && i < len(line) {
r, d = utf8.DecodeRuneInString(line[i:])
switch r {
case '"':
endFound = true
i += d
case '\\':
i += d
r, d = utf8.DecodeRuneInString(line[i:])
part = append(part, r)
i += d
default:
part = append(part, r)
i += d
}
}
if !endFound {
return nil, errors.New("Invalid line.")
}
case '\'':
i += d
endFound := false
for !endFound && i < len(line) {
r, d = utf8.DecodeRuneInString(line[i:])
switch r {
case '\'':
endFound = true
i += d
default:
part = append(part, r)
i += d
}
}
if !endFound {
return nil, errors.New("Invalid line.")
}
case ' ':
parts = append(parts, string(part))
part = make([]rune, 0, len(line)-i)
i += d
case '\\':
i += d
r, d = utf8.DecodeRuneInString(line[i:])
part = append(part, r)
i += d
default:
part = append(part, r)
i += d
}
}
parts = append(parts, string(part))
return parts, nil
}
func ParseSirenfile(file string) ([][]string, error) {
lines := strings.Split(file, "\n")
commands := make([][]string, 0, len(lines))
for _, line := range lines {
if line != "" && line[0] != '#' && line[0] != '.' {
parts, err := ParseLine(line)
if err != nil {
return nil, err
}
commands = append(commands, parts)
}
}
return commands, nil
}