refactor: Move match parser to common package

This commit is contained in:
Myzel394 2024-09-21 18:41:20 +02:00
parent a3e7a8c9b9
commit 250ec83a59
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
26 changed files with 46 additions and 48 deletions

View File

@ -3,7 +3,7 @@ package matchparser
import (
"config-lsp/common"
commonparser "config-lsp/common/parser"
"config-lsp/handlers/sshd_config/fields/match-parser/parser"
parser2 "config-lsp/common/parsers/openssh-match-parser/parser"
"config-lsp/utils"
"errors"
"fmt"
@ -39,13 +39,13 @@ func createListener(
}
type matchParserListener struct {
*parser.BaseMatchListener
*parser2.BaseMatchListener
match *Match
Errors []common.LSPError
matchContext *matchListenerContext
}
func (s *matchParserListener) EnterMatchEntry(ctx *parser.MatchEntryContext) {
func (s *matchParserListener) EnterMatchEntry(ctx *parser2.MatchEntryContext) {
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line)
@ -58,7 +58,7 @@ func (s *matchParserListener) EnterMatchEntry(ctx *parser.MatchEntryContext) {
s.matchContext.currentEntry = entry
}
func (s *matchParserListener) ExitMatchEntry(ctx *parser.MatchEntryContext) {
func (s *matchParserListener) ExitMatchEntry(ctx *parser2.MatchEntryContext) {
s.matchContext.currentEntry = nil
}
@ -72,7 +72,7 @@ var availableCriteria = map[string]MatchCriteriaType{
string(MatchCriteriaTypeAddress): MatchCriteriaTypeAddress,
}
func (s *matchParserListener) EnterCriteria(ctx *parser.CriteriaContext) {
func (s *matchParserListener) EnterCriteria(ctx *parser2.CriteriaContext) {
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line)
@ -95,7 +95,7 @@ func (s *matchParserListener) EnterCriteria(ctx *parser.CriteriaContext) {
}
}
func (s *matchParserListener) EnterSeparator(ctx *parser.SeparatorContext) {
func (s *matchParserListener) EnterSeparator(ctx *parser2.SeparatorContext) {
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line)
@ -104,7 +104,7 @@ func (s *matchParserListener) EnterSeparator(ctx *parser.SeparatorContext) {
}
}
func (s *matchParserListener) EnterValues(ctx *parser.ValuesContext) {
func (s *matchParserListener) EnterValues(ctx *parser2.ValuesContext) {
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line)
@ -114,7 +114,7 @@ func (s *matchParserListener) EnterValues(ctx *parser.ValuesContext) {
}
}
func (s *matchParserListener) EnterValue(ctx *parser.ValueContext) {
func (s *matchParserListener) EnterValue(ctx *parser2.ValueContext) {
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line)

View File

@ -2,7 +2,7 @@ package matchparser
import (
"config-lsp/common"
"config-lsp/handlers/sshd_config/fields/match-parser/parser"
parser2 "config-lsp/common/parsers/openssh-match-parser/parser"
"github.com/antlr4-go/antlr/v4"
)
@ -27,14 +27,14 @@ func (m *Match) Parse(
stream := antlr.NewInputStream(input)
lexerErrorListener := createErrorListener(context.line)
lexer := parser.NewMatchLexer(stream)
lexer := parser2.NewMatchLexer(stream)
lexer.RemoveErrorListeners()
lexer.AddErrorListener(&lexerErrorListener)
tokenStream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
parserErrorListener := createErrorListener(context.line)
antlrParser := parser.NewMatchParser(tokenStream)
antlrParser := parser2.NewMatchParser(tokenStream)
antlrParser.RemoveErrorListeners()
antlrParser.AddErrorListener(&parserErrorListener)

View File

@ -3,7 +3,7 @@ package ast
import (
"config-lsp/common"
commonparser "config-lsp/common/parser"
matchparser "config-lsp/handlers/sshd_config/fields/match-parser"
"config-lsp/common/parsers/openssh-match-parser"
"github.com/emirpasic/gods/maps/treemap"
)

View File

@ -2,8 +2,8 @@ package analyzer
import (
"config-lsp/common"
"config-lsp/common/parsers/openssh-match-parser"
sshdconfig "config-lsp/handlers/sshd_config"
matchparser "config-lsp/handlers/sshd_config/fields/match-parser"
"config-lsp/utils"
"errors"
"fmt"

View File

@ -109,7 +109,7 @@ func checkMatchBlock(
) []common.LSPError {
errs := make([]common.LSPError, 0)
matchOption := matchBlock.MatchEntry.OptionValue
matchOption := matchBlock.MatchOption.OptionValue
if matchOption != nil {
invalidValues := fields.Options["Match"].CheckIsValid(matchOption.Value.Value)

View File

@ -3,8 +3,8 @@ package ast
import (
"config-lsp/common"
commonparser "config-lsp/common/parser"
matchparser2 "config-lsp/common/parsers/openssh-match-parser"
"config-lsp/handlers/sshd_config/ast/parser"
matchparser "config-lsp/handlers/sshd_config/fields/match-parser"
"strings"
"github.com/emirpasic/gods/maps/treemap"
@ -113,10 +113,10 @@ func (s *sshParserListener) ExitEntry(ctx *parser.EntryContext) {
if s.sshContext.isKeyAMatchBlock {
// Add new match block
var match *matchparser.Match
var match *matchparser2.Match
if s.sshContext.currentOption.OptionValue != nil {
matchParser := matchparser.NewMatch()
matchParser := matchparser2.NewMatch()
errors := matchParser.Parse(
s.sshContext.currentOption.OptionValue.Value.Raw,
location.Start.Line,
@ -137,7 +137,7 @@ func (s *sshParserListener) ExitEntry(ctx *parser.EntryContext) {
matchBlock := &SSHDMatchBlock{
LocationRange: location,
MatchEntry: s.sshContext.currentOption,
MatchOption: s.sshContext.currentOption,
MatchValue: match,
Options: treemap.NewWith(gods.UInt32Comparator),
}

View File

@ -88,15 +88,15 @@ Match Address 192.168.0.1
rawSecondEntry, _ := p.Options.Get(uint32(2))
secondEntry := rawSecondEntry.(*SSHDMatchBlock)
if !(secondEntry.MatchEntry.Value.Value == "Match Address 192.168.0.1") {
t.Errorf("Expected second entry to be 'Match Address 192.168.0.1', but got: %v", secondEntry.MatchEntry.Value)
if !(secondEntry.MatchOption.Value.Value == "Match Address 192.168.0.1") {
t.Errorf("Expected second entry to be 'Match Address 192.168.0.1', but got: %v", secondEntry.MatchOption.Value)
}
if !(secondEntry.Start.Line == 2 && secondEntry.Start.Character == 0 && secondEntry.End.Line == 3 && secondEntry.End.Character == 27) {
t.Errorf("Expected second entry's location to be 2:0-3:25, but got: %v", secondEntry.LocationRange)
}
if !(secondEntry.MatchValue.Entries[0].Criteria.Type == "Address" && secondEntry.MatchValue.Entries[0].Values.Values[0].Value.Value == "192.168.0.1" && secondEntry.MatchEntry.OptionValue.Start.Character == 6) {
if !(secondEntry.MatchValue.Entries[0].Criteria.Type == "Address" && secondEntry.MatchValue.Entries[0].Values.Values[0].Value.Value == "192.168.0.1" && secondEntry.MatchOption.OptionValue.Start.Character == 6) {
t.Errorf("Expected second entry to be 'Match Address 192.168.0.1', but got: %v", secondEntry.MatchValue)
}
@ -126,8 +126,8 @@ Match User lena User root
_, matchBlock := p.FindOption(uint32(0))
if !(matchBlock.MatchEntry.Value.Value == "Match User lena User root") {
t.Errorf("Expected match block to be 'Match User lena User root', but got: %v", matchBlock.MatchEntry.Value)
if !(matchBlock.MatchOption.Value.Value == "Match User lena User root") {
t.Errorf("Expected match block to be 'Match User lena User root', but got: %v", matchBlock.MatchOption.Value)
}
if !(len(matchBlock.MatchValue.Entries) == 2) {
@ -157,8 +157,8 @@ func TestIncompleteMatchBlock(
_, matchBlock := p.FindOption(uint32(0))
if !(matchBlock.MatchEntry.Value.Value == "Match User lena User ") {
t.Errorf("Expected match block to be 'Match User lena User ', but got: %v", matchBlock.MatchEntry.Value)
if !(matchBlock.MatchOption.Value.Value == "Match User lena User ") {
t.Errorf("Expected match block to be 'Match User lena User ', but got: %v", matchBlock.MatchOption.Value)
}
if !(matchBlock.MatchValue.Entries[0].Criteria.Type == "User" && matchBlock.MatchValue.Entries[0].Values.Values[0].Value.Value == "lena") {
@ -231,12 +231,12 @@ Match Address 192.168.0.2
}
emptyOption, matchBlock := p.FindOption(uint32(5))
if !(emptyOption == nil && matchBlock.MatchEntry.Value.Value == "Match User lena" && matchBlock.MatchValue.Entries[0].Values.Values[0].Value.Value == "lena") {
if !(emptyOption == nil && matchBlock.MatchOption.Value.Value == "Match User lena" && matchBlock.MatchValue.Entries[0].Values.Values[0].Value.Value == "lena") {
t.Errorf("Expected empty option and match block to be 'Match User lena', but got: %v, %v", emptyOption, matchBlock)
}
matchOption, matchBlock := p.FindOption(uint32(2))
if !(matchOption.Value.Value == "Match User lena" && matchBlock.MatchEntry.Value.Value == "Match User lena" && matchBlock.MatchValue.Entries[0].Values.Values[0].Value.Value == "lena" && matchBlock.MatchEntry.OptionValue.Start.Character == 6) {
if !(matchOption.Value.Value == "Match User lena" && matchBlock.MatchOption.Value.Value == "Match User lena" && matchBlock.MatchValue.Entries[0].Values.Values[0].Value.Value == "lena" && matchBlock.MatchOption.OptionValue.Start.Character == 6) {
t.Errorf("Expected match option to be 'Match User lena', but got: %v, %v", matchOption, matchBlock)
}
@ -502,8 +502,8 @@ Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1
rawFourthEntry, _ := p.Options.Get(uint32(135))
fourthEntry := rawFourthEntry.(*SSHDMatchBlock)
if !(fourthEntry.MatchEntry.Value.Value == "Match User anoncvs") {
t.Errorf("Expected fourth entry to be 'Match User anoncvs', but got: %v", fourthEntry.MatchEntry.Value)
if !(fourthEntry.MatchOption.Value.Value == "Match User anoncvs") {
t.Errorf("Expected fourth entry to be 'Match User anoncvs', but got: %v", fourthEntry.MatchOption.Value)
}
if !(fourthEntry.MatchValue.Entries[0].Criteria.Type == "User" && fourthEntry.MatchValue.Entries[0].Values.Values[0].Value.Value == "anoncvs") {
@ -522,12 +522,12 @@ Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1
rawSixthEntry, _ := p.Options.Get(uint32(142))
sixthEntry := rawSixthEntry.(*SSHDMatchBlock)
if !(sixthEntry.MatchEntry.Value.Value == "Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1") {
t.Errorf("Expected sixth entry to be 'Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1', but got: %v", sixthEntry.MatchEntry.Value)
if !(sixthEntry.MatchOption.Value.Value == "Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1") {
t.Errorf("Expected sixth entry to be 'Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1', but got: %v", sixthEntry.MatchOption.Value)
}
if !(sixthEntry.MatchEntry.Key.Value.Value == "Match" && sixthEntry.MatchEntry.OptionValue.Value.Value == "Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1") {
t.Errorf("Expected sixth entry to be 'Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1', but got: %v", sixthEntry.MatchEntry.Value)
if !(sixthEntry.MatchOption.Key.Value.Value == "Match" && sixthEntry.MatchOption.OptionValue.Value.Value == "Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1") {
t.Errorf("Expected sixth entry to be 'Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1', but got: %v", sixthEntry.MatchOption.Value)
}
if !(sixthEntry.MatchValue.Entries[0].Criteria.Type == "Address" && len(sixthEntry.MatchValue.Entries[0].Values.Values) == 3) {

View File

@ -3,8 +3,7 @@ package ast
import (
"config-lsp/common"
commonparser "config-lsp/common/parser"
matchparser "config-lsp/handlers/sshd_config/fields/match-parser"
"config-lsp/common/parsers/openssh-match-parser"
"github.com/emirpasic/gods/maps/treemap"
)
@ -47,7 +46,7 @@ type SSHDOption struct {
type SSHDMatchBlock struct {
common.LocationRange
MatchEntry *SSHDOption
MatchOption *SSHDOption
MatchValue *matchparser.Match
// [uint32]*SSHDOption -> line number -> *SSHDOption

View File

@ -13,7 +13,7 @@ func (m SSHDMatchBlock) GetType() SSHDEntryType {
}
func (m SSHDMatchBlock) GetOption() SSHDOption {
return *m.MatchEntry
return *m.MatchOption
}
func (c SSHDConfig) FindMatchBlock(line uint32) *SSHDMatchBlock {
@ -37,8 +37,8 @@ func (c SSHDConfig) FindOption(line uint32) (*SSHDOption, *SSHDMatchBlock) {
matchBlock := c.FindMatchBlock(line)
if matchBlock != nil {
if line == matchBlock.MatchEntry.Start.Line {
return matchBlock.MatchEntry, matchBlock
if line == matchBlock.MatchOption.Start.Line {
return matchBlock.MatchOption, matchBlock
}
rawEntry, found := matchBlock.Options.Get(line)
@ -55,7 +55,7 @@ func (c SSHDConfig) FindOption(line uint32) (*SSHDOption, *SSHDMatchBlock) {
if found {
switch rawEntry.(type) {
case *SSHDMatchBlock:
return rawEntry.(*SSHDMatchBlock).MatchEntry, rawEntry.(*SSHDMatchBlock)
return rawEntry.(*SSHDMatchBlock).MatchOption, rawEntry.(*SSHDMatchBlock)
case *SSHDOption:
return rawEntry.(*SSHDOption), nil
}
@ -78,7 +78,7 @@ func (c SSHDConfig) GetAllOptions() []*SSHDOption {
case *SSHDOption:
options = append(options, entry)
case *SSHDMatchBlock:
options = append(options, entry.MatchEntry)
options = append(options, entry.MatchOption)
for _, rawOption := range entry.Options.Values() {
options = append(options, rawOption.(*SSHDOption))

View File

@ -2,10 +2,9 @@ package handlers
import (
"config-lsp/common"
"config-lsp/common/parsers/openssh-match-parser"
sshdconfig "config-lsp/handlers/sshd_config"
"config-lsp/handlers/sshd_config/fields"
matchparser "config-lsp/handlers/sshd_config/fields/match-parser"
protocol "github.com/tliron/glsp/protocol_3_16"
)

View File

@ -2,8 +2,8 @@ package handlers
import (
"config-lsp/common/formatting"
"config-lsp/common/parsers/openssh-match-parser"
"config-lsp/handlers/sshd_config/ast"
matchparser "config-lsp/handlers/sshd_config/fields/match-parser"
"config-lsp/utils"
"fmt"
"strings"
@ -57,10 +57,10 @@ func formatSSHDMatchBlock(
edits := make([]protocol.TextEdit, 0)
edits = append(edits, protocol.TextEdit{
Range: matchBlock.MatchEntry.ToLSPRange(),
Range: matchBlock.MatchOption.ToLSPRange(),
NewText: matchTemplate.Format(
options,
matchBlock.MatchEntry.Key.Key,
matchBlock.MatchOption.Key.Key,
formatMatchToString(matchBlock.MatchValue),
),
})

View File

@ -30,7 +30,7 @@ func CreateIndexes(config ast.SSHDConfig) (*SSHDIndexes, []common.LSPError) {
case *ast.SSHDMatchBlock:
matchBlock := entry.(*ast.SSHDMatchBlock)
errs = append(errs, addOption(indexes, matchBlock.MatchEntry, matchBlock)...)
errs = append(errs, addOption(indexes, matchBlock.MatchOption, matchBlock)...)
it := matchBlock.Options.Iterator()
for it.Next() {