aboutsummaryrefslogtreecommitdiff
path: root/loader.go
diff options
context:
space:
mode:
Diffstat (limited to 'loader.go')
-rw-r--r--loader.go30
1 files changed, 23 insertions, 7 deletions
diff --git a/loader.go b/loader.go
index e9757a6..7f58237 100644
--- a/loader.go
+++ b/loader.go
@@ -9,28 +9,44 @@ import (
"go.neonxp.ru/conf/model"
)
-func LoadFile(filename string) (*model.Doc, error) {
+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 nil, fmt.Errorf("failed load file: %w", err)
+ return fmt.Errorf("failed load file: %w", err)
}
- return Load(filename, content)
+ return c.Load(filename, content)
}
-func Load(name string, input []byte) (*model.Doc, error) {
+func (c *Conf) Load(name string, input []byte) error {
p := &parser.Parser{}
astSlice, err := p.Parse(name, input)
if err != nil {
- return nil, fmt.Errorf("failed parse conf content: %w", err)
+ return fmt.Errorf("failed parse conf content: %w", err)
}
astTree := ast.Parse(p, astSlice)
doc, err := ast.ToDoc(astTree[0])
if err != nil {
- return nil, fmt.Errorf("failed build Doc: %w", err)
+ return fmt.Errorf("failed build Doc: %w", err)
}
- return doc, nil
+ c.root = doc
+
+ return nil
+}
+
+func (c *Conf) Process(visitor model.Visitor) error {
+ return c.root.Execute(visitor)
}