2023-01-23 18:54:21 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2023-02-18 17:27:28 +07:00
|
|
|
* Copyright (c) 2022-2023, v2rayA Organization <team@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"
|
2023-01-24 13:25:21 +07:00
|
|
|
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
|
2023-01-27 01:10:27 +07:00
|
|
|
"github.com/v2rayA/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
|
|
|
}
|