-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
45 lines (37 loc) · 863 Bytes
/
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
package lexer
import (
"fmt"
"strings"
)
type kp map[string]string
type section map[string]kp
func parse(d string) (section, error) {
if d == "" {
return nil, fmt.Errorf("parse error: nothing to parse")
}
sections := make(section, 0)
var sec string
var key string
l := lex(d)
for t := range l.items {
if (t.typ == itemSection || t.typ == itemKey || t.typ == itemValue) && strings.ContainsAny(t.val, "\n") {
return nil, fmt.Errorf("parse error: %s can't contain new lines", t.val)
}
switch t.typ {
case itemSection:
sec = t.val
_, ok := sections[sec]
if !ok {
sections[sec] = make(kp, 0)
}
case itemKey:
key = strings.TrimSpace(t.val)
case itemValue:
s := sections[sec]
s[key] = strings.TrimSpace(t.val)
case itemError:
return nil, fmt.Errorf("parse error: %s", t.val)
}
}
return sections, nil
}