Skip to content

Commit

Permalink
refactor: Move match parser to common package
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 21, 2024
1 parent a3e7a8c commit 250ec83
Show file tree
Hide file tree
Showing 26 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion handlers/ssh_config/ast/ssh_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion handlers/sshd_config/analyzer/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion handlers/sshd_config/analyzer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions handlers/sshd_config/ast/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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),
}
Expand Down
30 changes: 15 additions & 15 deletions handlers/sshd_config/ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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") {
Expand All @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions handlers/sshd_config/ast/sshd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -47,8 +46,8 @@ type SSHDOption struct {

type SSHDMatchBlock struct {
common.LocationRange
MatchEntry *SSHDOption
MatchValue *matchparser.Match
MatchOption *SSHDOption
MatchValue *matchparser.Match

// [uint32]*SSHDOption -> line number -> *SSHDOption
Options *treemap.Map
Expand Down
10 changes: 5 additions & 5 deletions handlers/sshd_config/ast/sshd_config_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions handlers/sshd_config/handlers/completions_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
6 changes: 3 additions & 3 deletions handlers/sshd_config/handlers/formatting_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
),
})
Expand Down
2 changes: 1 addition & 1 deletion handlers/sshd_config/indexes/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 250ec83

Please sign in to comment.