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
|
package config
import (
"bufio"
"errors"
"fmt"
"io"
)
// ParseConfig reads and parses Git configuration entries from r.
func ParseConfig(r io.Reader) (*Config, error) {
parser := &configParser{
reader: bufio.NewReader(r),
lineNum: 1,
}
return parser.parse()
}
type configParser struct {
reader *bufio.Reader
lineNum int
currentSection string
currentSubsec string
peeked byte
hasPeeked bool
}
func (p *configParser) parse() (*Config, error) {
cfg := &Config{}
err := p.skipBOM()
if err != nil {
return nil, err
}
for {
ch, err := p.nextChar()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
// Skip leading whitespace between entries.
if isWhitespace(ch) {
continue
}
// Comments
if ch == '#' || ch == ';' {
err := p.skipToEOL()
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
continue
}
// Section header
if ch == '[' {
err := p.parseSection()
if err != nil {
return nil, fmt.Errorf("furgit: config: line %d: %w", p.lineNum, err)
}
continue
}
// Key-value pair
if isLetter(ch) {
p.unreadChar(ch)
err := p.parseKeyValue(cfg)
if err != nil {
return nil, fmt.Errorf("furgit: config: line %d: %w", p.lineNum, err)
}
continue
}
return nil, fmt.Errorf("furgit: config: line %d: unexpected character %q", p.lineNum, ch)
}
return cfg, nil
}
|