forked from binhonglee/kdlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
50 lines (41 loc) · 855 Bytes
/
parse.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
package kdl
import (
"bufio"
"bytes"
"io"
"os"
"strings"
)
//go:generate go run internal/tools/generate_test_cases/generate.go
func parse(br innerReader) (Document, error) {
doc := NewDocument()
r := wrapReader(br)
nodes, err := readNodes(&r)
if err != nil {
return doc, addErrPosInfo(err, &r)
}
doc.Nodes = nodes
return doc, nil
}
func ParseReader(r io.Reader) (Document, error) {
br := bufio.NewReader(r)
return parse(br)
}
func ParseBytes(b []byte) (Document, error) {
bb := bytes.NewReader(b)
return ParseReader(bb)
}
func ParseString(s string) (Document, error) {
sr := strings.NewReader(s)
br := bufio.NewReader(sr)
return parse(br)
}
func ParseFile(path string) (Document, error) {
f, err := os.Open(path)
if err != nil {
return NewDocument(), err
}
defer f.Close()
br := bufio.NewReader(f)
return parse(br)
}