aboutsummaryrefslogtreecommitdiff
path: root/loader.go
blob: 7f582376a515010a7c321a43aebeb84889f0d72e (plain) (blame)
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
package conf

import (
	"fmt"
	"os"

	"go.neonxp.ru/conf/internal/ast"
	"go.neonxp.ru/conf/internal/parser"
	"go.neonxp.ru/conf/model"
)

func New() *Conf {
	return &Conf{
		root: model.Body{},
	}
}

type Conf struct {
	root model.Body
}

func (c *Conf) LoadFile(filename string) error {
	content, err := os.ReadFile(filename)
	if err != nil {
		return fmt.Errorf("failed load file: %w", err)
	}

	return c.Load(filename, content)
}

func (c *Conf) Load(name string, input []byte) error {
	p := &parser.Parser{}
	astSlice, err := p.Parse(name, input)
	if err != nil {
		return fmt.Errorf("failed parse conf content: %w", err)
	}

	astTree := ast.Parse(p, astSlice)

	doc, err := ast.ToDoc(astTree[0])
	if err != nil {
		return fmt.Errorf("failed build Doc: %w", err)
	}

	c.root = doc

	return nil
}

func (c *Conf) Process(visitor model.Visitor) error {
	return c.root.Execute(visitor)
}