-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ssh_config): Add first version of ssh_config
- Loading branch information
Showing
31 changed files
with
2,808 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ast | ||
|
||
import ( | ||
"config-lsp/common" | ||
|
||
"github.com/antlr4-go/antlr/v4" | ||
) | ||
|
||
type errorListenerContext struct { | ||
line uint32 | ||
} | ||
|
||
type errorListener struct { | ||
*antlr.DefaultErrorListener | ||
Errors []common.LSPError | ||
context errorListenerContext | ||
} | ||
|
||
func (d *errorListener) SyntaxError( | ||
recognizer antlr.Recognizer, | ||
offendingSymbol interface{}, | ||
_ int, | ||
character int, | ||
message string, | ||
error antlr.RecognitionException, | ||
) { | ||
line := d.context.line | ||
d.Errors = append(d.Errors, common.LSPError{ | ||
Range: common.CreateSingleCharRange(uint32(line), uint32(character)), | ||
Err: common.SyntaxError{ | ||
Message: message, | ||
}, | ||
}) | ||
} | ||
|
||
func createErrorListener( | ||
line uint32, | ||
) errorListener { | ||
return errorListener{ | ||
Errors: make([]common.LSPError, 0), | ||
context: errorListenerContext{ | ||
line: line, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ast | ||
|
||
import "config-lsp/common" | ||
|
||
|
||
type ValidPath string | ||
|
||
func (v ValidPath) AsURI() string { | ||
return "file://" + string(v) | ||
} | ||
|
||
type SSHIndexIncludeValue struct { | ||
common.LocationRange | ||
Value string | ||
|
||
// Actual valid paths, these will be set by the analyzer | ||
Paths []ValidPath | ||
} | ||
|
||
type SSHIndexIncludeLine struct { | ||
Values []*SSHIndexIncludeValue | ||
Option *SSHOption | ||
Block *SSHBlock | ||
} | ||
|
||
type SSHIndexes struct { | ||
Includes []*SSHIndexIncludeLine | ||
} | ||
|
||
func NewSSHIndexes() *SSHIndexes { | ||
return &SSHIndexes{ | ||
Includes: make([]*SSHIndexIncludeLine, 0), | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package ast | ||
|
||
import ( | ||
"config-lsp/common" | ||
commonparser "config-lsp/common/parser" | ||
"config-lsp/handlers/ssh_config/ast/parser" | ||
"config-lsp/handlers/ssh_config/match-parser" | ||
"strings" | ||
|
||
"github.com/emirpasic/gods/maps/treemap" | ||
gods "github.com/emirpasic/gods/utils" | ||
) | ||
|
||
type sshListenerContext struct { | ||
line uint32 | ||
currentOption *SSHOption | ||
currentBlock SSHBlock | ||
currentKeyIsBlockOf *SSHBlockType | ||
currentIndexes *SSHIndexes | ||
} | ||
|
||
func createListenerContext() *sshListenerContext { | ||
context := new(sshListenerContext) | ||
context.currentIndexes = NewSSHIndexes() | ||
|
||
return context | ||
} | ||
|
||
func createListener( | ||
config *SSHConfig, | ||
context *sshListenerContext, | ||
) sshParserListener { | ||
return sshParserListener{ | ||
Config: config, | ||
Errors: make([]common.LSPError, 0), | ||
sshContext: context, | ||
} | ||
} | ||
|
||
type sshParserListener struct { | ||
*parser.BaseConfigListener | ||
Config *SSHConfig | ||
Errors []common.LSPError | ||
sshContext *sshListenerContext | ||
} | ||
|
||
func (s *sshParserListener) EnterEntry(ctx *parser.EntryContext) { | ||
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext) | ||
location.ChangeBothLines(s.sshContext.line) | ||
|
||
value := commonparser.ParseRawString(ctx.GetText(), commonparser.FullFeatures) | ||
option := &SSHOption{ | ||
LocationRange: location, | ||
Value: value, | ||
} | ||
|
||
s.sshContext.currentOption = option | ||
} | ||
|
||
func (s *sshParserListener) EnterKey(ctx *parser.KeyContext) { | ||
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext) | ||
location.ChangeBothLines(s.sshContext.line) | ||
|
||
text := ctx.GetText() | ||
value := commonparser.ParseRawString(text, commonparser.FullFeatures) | ||
key := strings.Trim( | ||
value.Value, | ||
" ", | ||
) | ||
|
||
switch strings.ToLower(text) { | ||
case "match": | ||
value := SSHBlockTypeMatch | ||
s.sshContext.currentKeyIsBlockOf = &value | ||
case "host": | ||
value := SSHBlockTypeHost | ||
s.sshContext.currentKeyIsBlockOf = &value | ||
} | ||
|
||
s.sshContext.currentOption.Key = &SSHKey{ | ||
LocationRange: location, | ||
Value: value, | ||
Key: key, | ||
} | ||
} | ||
|
||
func (s *sshParserListener) EnterSeparator(ctx *parser.SeparatorContext) { | ||
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext) | ||
location.ChangeBothLines(s.sshContext.line) | ||
|
||
text := ctx.GetText() | ||
value := commonparser.ParseRawString(text, commonparser.FullFeatures) | ||
|
||
s.sshContext.currentOption.Separator = &SSHSeparator{ | ||
LocationRange: location, | ||
Value: value, | ||
} | ||
} | ||
|
||
func (s *sshParserListener) EnterValue(ctx *parser.ValueContext) { | ||
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext) | ||
location.ChangeBothLines(s.sshContext.line) | ||
|
||
value := commonparser.ParseRawString(ctx.GetText(), commonparser.FullFeatures) | ||
s.sshContext.currentOption.OptionValue = &SSHValue{ | ||
LocationRange: location, | ||
Value: value, | ||
} | ||
} | ||
|
||
func (s *sshParserListener) ExitEntry(ctx *parser.EntryContext) { | ||
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext) | ||
location.ChangeBothLines(s.sshContext.line) | ||
|
||
defer (func() { | ||
s.sshContext.currentOption = nil | ||
})() | ||
|
||
if s.sshContext.currentKeyIsBlockOf != nil { | ||
switch *s.sshContext.currentKeyIsBlockOf { | ||
case SSHBlockTypeMatch: | ||
var match *matchparser.Match | ||
|
||
matchParser := matchparser.NewMatch() | ||
errors := matchParser.Parse( | ||
s.sshContext.currentOption.OptionValue.Value.Raw, | ||
location.Start.Line, | ||
s.sshContext.currentOption.OptionValue.Start.Character, | ||
) | ||
|
||
if len(errors) > 0 { | ||
for _, err := range errors { | ||
s.Errors = append(s.Errors, common.LSPError{ | ||
Range: err.Range.ShiftHorizontal(s.sshContext.currentOption.Start.Character), | ||
Err: err.Err, | ||
}) | ||
} | ||
} else { | ||
match = matchParser | ||
} | ||
|
||
matchBlock := &SSHMatchBlock{ | ||
LocationRange: location, | ||
MatchOption: s.sshContext.currentOption, | ||
MatchValue: match, | ||
Options: treemap.NewWith(gods.UInt32Comparator), | ||
} | ||
|
||
s.Config.Options.Put( | ||
location.Start.Line, | ||
matchBlock, | ||
) | ||
|
||
s.sshContext.currentKeyIsBlockOf = nil | ||
s.sshContext.currentBlock = matchBlock | ||
} | ||
|
||
return | ||
} | ||
|
||
if s.sshContext.currentBlock == nil { | ||
s.Config.Options.Put( | ||
location.Start.Line, | ||
s.sshContext.currentOption, | ||
) | ||
} else { | ||
block := s.sshContext.currentBlock | ||
block.AddOption(s.sshContext.currentOption) | ||
block.SetEnd(location.End) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package ast | ||
|
||
import ( | ||
"config-lsp/common" | ||
"config-lsp/handlers/ssh_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 NewSSHConfig() *SSHConfig { | ||
config := &SSHConfig{} | ||
config.Clear() | ||
|
||
return config | ||
} | ||
|
||
func (c *SSHConfig) Clear() { | ||
c.Options = treemap.NewWith(gods.UInt32Comparator) | ||
c.CommentLines = map[uint32]struct{}{} | ||
} | ||
|
||
var commentPattern = regexp.MustCompile(`^\s*#.*$`) | ||
var emptyPattern = regexp.MustCompile(`^\s*$`) | ||
|
||
func (c *SSHConfig) 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 *SSHConfig) parseStatement( | ||
context *sshListenerContext, | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ HASH=1 | |
WHITESPACE=2 | ||
STRING=3 | ||
NEWLINE=4 | ||
QUOTED_STRING=5 | ||
'#'=1 |
Oops, something went wrong.