forked from sblinch/kdl-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkdl.go
More file actions
62 lines (49 loc) · 1.75 KB
/
Copy pathkdl.go
File metadata and controls
62 lines (49 loc) · 1.75 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
package kdl
import (
"io"
"github.com/sblinch/kdl-go/document"
"github.com/sblinch/kdl-go/internal/generator"
"github.com/sblinch/kdl-go/internal/parser"
"github.com/sblinch/kdl-go/internal/tokenizer"
)
func parse(s *tokenizer.Scanner) (*document.Document, error) {
defer s.Close()
p := parser.New()
opts := parser.ParseContextOptions{RelaxedNonCompliant: s.RelaxedNonCompliant}
if s.ParseComments {
opts.Flags |= parser.ParseComments
}
c := p.NewContextOptions(opts)
for s.Scan() {
if err := p.Parse(c, s.Token()); err != nil {
return nil, err
}
}
if s.Err() != nil {
return nil, s.Err()
}
return c.Document(), nil
}
type ParseOptions = parser.ParseContextOptions
var DefaultParseOptions = parser.ParseContextOptions{}
// Parse parses a KDL document from r and returns the parsed Document, or a non-nil error on failure
func Parse(r io.Reader) (*document.Document, error) {
return ParseWithOptions(r, DefaultParseOptions)
}
func ParseWithOptions(r io.Reader, opts ParseOptions) (*document.Document, error) {
s := tokenizer.New(r)
s.RelaxedNonCompliant = opts.RelaxedNonCompliant
s.ParseComments = opts.Flags.Has(parser.ParseComments)
return parse(s)
}
type GenerateOptions = generator.Options
var DefaultGenerateOptions = generator.DefaultOptions
// Generate writes to w a well-formatted KDL document generated from doc, or a non-nil error on failure
func Generate(doc *document.Document, w io.Writer) error {
return GenerateWithOptions(doc, w, DefaultGenerateOptions)
}
// GenerateWithOptions writes to w a well-formatted KDL document generated from doc, or a non-nil error on failure
func GenerateWithOptions(doc *document.Document, w io.Writer, opts GenerateOptions) error {
g := generator.NewOptions(w, opts)
return g.Generate(doc)
}