dae/pkg/config_parser/config_parser.go

35 lines
950 B
Go
Raw Normal View History

2023-01-23 18:54:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
2023-01-23 18:54:21 +07:00
*/
2023-01-27 01:10:27 +07:00
package config_parser
2023-01-23 18:54:21 +07:00
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
2023-03-14 14:01:55 +07:00
"github.com/daeuniverse/dae-config-dist/go/dae_config"
2023-01-23 18:54:21 +07:00
)
2023-01-27 01:10:27 +07:00
func Parse(in string) (sections []*Section, err error) {
2023-01-23 18:54:21 +07:00
errorListener := NewConsoleErrorListener()
2023-01-27 01:10:27 +07:00
lexer := dae_config.Newdae_configLexer(antlr.NewInputStream(in))
2023-01-23 18:54:21 +07:00
lexer.RemoveErrorListeners()
lexer.AddErrorListener(errorListener)
input := antlr.NewCommonTokenStream(lexer, 0)
2023-01-27 01:10:27 +07:00
parser := dae_config.Newdae_configParser(input)
2023-01-23 18:54:21 +07:00
parser.RemoveErrorListeners()
parser.AddErrorListener(errorListener)
parser.BuildParseTrees = true
tree := parser.Start()
2023-01-27 01:10:27 +07:00
walker := NewWalker(parser)
2023-01-23 18:54:21 +07:00
antlr.ParseTreeWalkerDefault.Walk(walker, tree)
if errorListener.ErrorBuilder.Len() != 0 {
2023-01-27 01:10:27 +07:00
return nil, fmt.Errorf("%v", errorListener.ErrorBuilder.String())
2023-01-23 18:54:21 +07:00
}
2023-01-27 01:10:27 +07:00
return walker.Sections, nil
2023-01-23 18:54:21 +07:00
}