mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package ast
|
|
|
|
import (
|
|
"config-lsp/common"
|
|
"config-lsp/handlers/sshd_config/ast/parser"
|
|
"config-lsp/utils"
|
|
"regexp"
|
|
|
|
"github.com/antlr4-go/antlr/v4"
|
|
"github.com/emirpasic/gods/maps/treemap"
|
|
|
|
gods "github.com/emirpasic/gods/utils"
|
|
)
|
|
|
|
func NewSSHDConfig() *SSHDConfig {
|
|
config := &SSHDConfig{}
|
|
config.Clear()
|
|
|
|
return config
|
|
}
|
|
|
|
func (c *SSHDConfig) Clear() {
|
|
c.Options = treemap.NewWith(gods.UInt32Comparator)
|
|
c.CommentLines = map[uint32]struct{}{}
|
|
}
|
|
|
|
var commentPattern = regexp.MustCompile(`^\s*#.*$`)
|
|
var emptyPattern = regexp.MustCompile(`^\s*$`)
|
|
|
|
func (c *SSHDConfig) Parse(input string) []common.LSPError {
|
|
errors := make([]common.LSPError, 0)
|
|
lines := utils.SplitIntoLines(input)
|
|
context := createListenerContext()
|
|
|
|
for rawLineNumber, line := range lines {
|
|
context.line = uint32(rawLineNumber)
|
|
|
|
if emptyPattern.MatchString(line) {
|
|
continue
|
|
}
|
|
|
|
if commentPattern.MatchString(line) {
|
|
c.CommentLines[context.line] = struct{}{}
|
|
continue
|
|
}
|
|
|
|
errors = append(
|
|
errors,
|
|
c.parseStatement(context, line)...,
|
|
)
|
|
}
|
|
|
|
return errors
|
|
}
|
|
|
|
func (c *SSHDConfig) parseStatement(
|
|
context *sshdListenerContext,
|
|
input string,
|
|
) []common.LSPError {
|
|
stream := antlr.NewInputStream(input)
|
|
|
|
lexerErrorListener := createErrorListener(context.line)
|
|
lexer := parser.NewConfigLexer(stream)
|
|
lexer.RemoveErrorListeners()
|
|
lexer.AddErrorListener(&lexerErrorListener)
|
|
|
|
tokenStream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
|
|
|
|
parserErrorListener := createErrorListener(context.line)
|
|
antlrParser := parser.NewConfigParser(tokenStream)
|
|
antlrParser.RemoveErrorListeners()
|
|
antlrParser.AddErrorListener(&parserErrorListener)
|
|
|
|
listener := createListener(c, context)
|
|
antlr.ParseTreeWalkerDefault.Walk(
|
|
&listener,
|
|
antlrParser.LineStatement(),
|
|
)
|
|
|
|
errors := lexerErrorListener.Errors
|
|
errors = append(errors, parserErrorListener.Errors...)
|
|
errors = append(errors, listener.Errors...)
|
|
|
|
return errors
|
|
}
|