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 ( import (
"config-lsp/common" "config-lsp/common"
commonparser "config-lsp/common/parser" 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" "config-lsp/utils"
"errors" "errors"
"fmt" "fmt"
@ -39,13 +39,13 @@ func createListener(
} }
type matchParserListener struct { type matchParserListener struct {
*parser.BaseMatchListener *parser2.BaseMatchListener
match *Match match *Match
Errors []common.LSPError Errors []common.LSPError
matchContext *matchListenerContext 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 := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line) location.ChangeBothLines(s.matchContext.line)
@ -58,7 +58,7 @@ func (s *matchParserListener) EnterMatchEntry(ctx *parser.MatchEntryContext) {
s.matchContext.currentEntry = entry s.matchContext.currentEntry = entry
} }
func (s *matchParserListener) ExitMatchEntry(ctx *parser.MatchEntryContext) { func (s *matchParserListener) ExitMatchEntry(ctx *parser2.MatchEntryContext) {
s.matchContext.currentEntry = nil s.matchContext.currentEntry = nil
} }
@ -72,7 +72,7 @@ var availableCriteria = map[string]MatchCriteriaType{
string(MatchCriteriaTypeAddress): MatchCriteriaTypeAddress, 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 := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line) 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 := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line) 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 := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line) 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 := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext).ShiftHorizontal(s.matchContext.startCharacter)
location.ChangeBothLines(s.matchContext.line) location.ChangeBothLines(s.matchContext.line)

View File

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

View File

@ -3,7 +3,7 @@ package ast
import ( import (
"config-lsp/common" "config-lsp/common"
commonparser "config-lsp/common/parser" 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" "github.com/emirpasic/gods/maps/treemap"
) )

View File

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

View File

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

View File

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

View File

@ -88,15 +88,15 @@ Match Address 192.168.0.1
rawSecondEntry, _ := p.Options.Get(uint32(2)) rawSecondEntry, _ := p.Options.Get(uint32(2))
secondEntry := rawSecondEntry.(*SSHDMatchBlock) secondEntry := rawSecondEntry.(*SSHDMatchBlock)
if !(secondEntry.MatchEntry.Value.Value == "Match Address 192.168.0.1") { 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.MatchEntry.Value) 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) { 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) 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) 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)) _, matchBlock := p.FindOption(uint32(0))
if !(matchBlock.MatchEntry.Value.Value == "Match User lena User root") { 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.MatchEntry.Value) t.Errorf("Expected match block to be 'Match User lena User root', but got: %v", matchBlock.MatchOption.Value)
} }
if !(len(matchBlock.MatchValue.Entries) == 2) { if !(len(matchBlock.MatchValue.Entries) == 2) {
@ -157,8 +157,8 @@ func TestIncompleteMatchBlock(
_, matchBlock := p.FindOption(uint32(0)) _, matchBlock := p.FindOption(uint32(0))
if !(matchBlock.MatchEntry.Value.Value == "Match User lena User ") { if !(matchBlock.MatchOption.Value.Value == "Match User lena User ") {
t.Errorf("Expected match block to be 'Match User lena User ', but got: %v", matchBlock.MatchEntry.Value) 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") { 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)) 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) 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)) 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) 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)) rawFourthEntry, _ := p.Options.Get(uint32(135))
fourthEntry := rawFourthEntry.(*SSHDMatchBlock) fourthEntry := rawFourthEntry.(*SSHDMatchBlock)
if !(fourthEntry.MatchEntry.Value.Value == "Match User anoncvs") { if !(fourthEntry.MatchOption.Value.Value == "Match User anoncvs") {
t.Errorf("Expected fourth entry to be 'Match User anoncvs', but got: %v", fourthEntry.MatchEntry.Value) 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") { 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)) rawSixthEntry, _ := p.Options.Get(uint32(142))
sixthEntry := rawSixthEntry.(*SSHDMatchBlock) sixthEntry := rawSixthEntry.(*SSHDMatchBlock)
if !(sixthEntry.MatchEntry.Value.Value == "Match Address 172.22.100.0/24,172.22.5.0/24,127.0.0.1") { 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.MatchEntry.Value) 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") { 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.MatchEntry.Value) 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) { 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 ( import (
"config-lsp/common" "config-lsp/common"
commonparser "config-lsp/common/parser" 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" "github.com/emirpasic/gods/maps/treemap"
) )
@ -47,8 +46,8 @@ type SSHDOption struct {
type SSHDMatchBlock struct { type SSHDMatchBlock struct {
common.LocationRange common.LocationRange
MatchEntry *SSHDOption MatchOption *SSHDOption
MatchValue *matchparser.Match MatchValue *matchparser.Match
// [uint32]*SSHDOption -> line number -> *SSHDOption // [uint32]*SSHDOption -> line number -> *SSHDOption
Options *treemap.Map Options *treemap.Map

View File

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

View File

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

View File

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

View File

@ -30,7 +30,7 @@ func CreateIndexes(config ast.SSHDConfig) (*SSHDIndexes, []common.LSPError) {
case *ast.SSHDMatchBlock: case *ast.SSHDMatchBlock:
matchBlock := entry.(*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() it := matchBlock.Options.Iterator()
for it.Next() { for it.Next() {