fix: fix(hosts): Improve hosts parser

This commit is contained in:
Myzel394 2024-09-08 20:24:49 +02:00
parent 0c520addf5
commit b27081aa8e
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
17 changed files with 722 additions and 846 deletions

View File

@ -77,6 +77,7 @@ func (p *AliasesParser) parseStatement(
errors := lexerErrorListener.Errors
errors = append(errors, parserErrorListener.Errors...)
errors = append(errors, listener.Errors...)
return errors
}

View File

@ -13,7 +13,7 @@ aliases
;
alias
: DOMAIN
: domain
;
hostname
@ -21,17 +21,26 @@ hostname
;
domain
: DOMAIN
: (STRING)+ (DOT STRING*)*
;
ipAddress
: (ipv4Address | ipv6Address)
;
ipv4Address
: (STRING DOT)+ STRING (ipRange? | ipPort?)
;
ipv6Address
: (((STRING COLON)+ STRING) | (STRING? COLON COLON STRING)) (ipRange? | ipPort?)
;
/*
ipv4Address
: singleIPv4Address
// Allow optional range to tell user ranges are not allowed
ipRange?
(ipRange? | ipPort?)
;
singleIPv4Address
@ -42,7 +51,7 @@ singleIPv4Address
ipv6Address
: singleIPv6Address
// Allow optional range to tell user ranges are not allowed
ipRange?
(ipRange? | ipPort?)
;
singleIPv6Address
@ -50,19 +59,24 @@ singleIPv6Address
;
ipv4Digit
: DIGITS
: STRING
;
ipv6Octet
: OCTETS
: STRING
;
*/
ipRange
: SLASH ipRangeBits
;
ipRangeBits
: DIGITS
: STRING
;
ipPort
: COLON STRING
;
comment
@ -101,26 +115,6 @@ NEWLINE
: [\r]? [\n]
;
DIGITS
: DIGIT+
;
fragment DIGIT
: [0-9]
;
OCTETS
: OCTET+
;
fragment OCTET
: [0-9a-fA-F]
;
DOMAIN
: ((STRING)+ (DOT [a-zA-Z]+)*)
;
fragment STRING
: ~(' ' | '\t' | '\n' | '\r' | '#' | '.')
STRING
: [a-zA-Z0-9_\-]+
;

View File

@ -18,7 +18,7 @@ func TestValidSimpleExampleWorks(
errors := parser.Parse(input)
if len(errors) != 0 {
t.Errorf("Expected no errors, but got %v", errors)
t.Fatalf("Expected no errors, but got %v", errors)
}
if !(len(parser.Tree.Entries) == 1) {
@ -85,7 +85,7 @@ func TestValidComplexExampleWorks(
errors := parser.Parse(input)
if len(errors) != 0 {
t.Errorf("Expected no errors, but got %v", errors)
t.Fatalf("Expected no errors, but got %v", errors)
}
if !(len(parser.Tree.Entries) == 3) {
@ -120,7 +120,7 @@ func TestInvalidExampleWorks(
errors := parser.Parse(input)
if len(errors) == 0 {
t.Errorf("Expected errors, but got none")
t.Fatalf("Expected errors, but got none")
}
if !(len(parser.Tree.Entries) == 1) {

View File

@ -96,7 +96,7 @@ func TestResolverEntriesWithComplexOverlapping(
resolver, errors := createResolverFromParser(parser)
if !(len(errors) == 1) {
t.Errorf("Expected 1 error, but got %v", len(errors))
t.Fatalf("Expected 1 error, but got %v", len(errors))
}
if len(resolver.Entries) != 3 {

View File

@ -4,7 +4,9 @@ import (
"config-lsp/common"
docvalues "config-lsp/doc-values"
parser2 "config-lsp/handlers/hosts/ast/parser"
"errors"
"net"
"regexp"
"github.com/antlr4-go/antlr/v4"
)
@ -21,7 +23,7 @@ type hostsParserListener struct {
}
func (s *hostsParserListener) EnterComment(ctx *parser2.CommentContext) {
line := uint32(s.hostsContext.line)
line := s.hostsContext.line
s.Parser.CommentLines[line] = struct{}{}
}
@ -34,6 +36,8 @@ func (s *hostsParserListener) EnterEntry(ctx *parser2.EntryContext) {
}
}
var containsPortPattern = regexp.MustCompile(`:[0-9]+$`)
func (s *hostsParserListener) EnterIpAddress(ctx *parser2.IpAddressContext) {
location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext)
location.ChangeBothLines(s.hostsContext.line)
@ -41,10 +45,18 @@ func (s *hostsParserListener) EnterIpAddress(ctx *parser2.IpAddressContext) {
ip := net.ParseIP(ctx.GetText())
if ip == nil {
if containsPortPattern.MatchString(ctx.GetText()) {
s.Errors = append(s.Errors, common.LSPError{
Range: location,
Err: errors.New("Port numbers are not allowed in IP addresses"),
})
} else {
s.Errors = append(s.Errors, common.LSPError{
Range: location,
Err: docvalues.InvalidIPAddress{},
})
}
return
}

View File

@ -45,6 +45,7 @@ func (p *HostsParser) parseStatement(
)
errors = append(errors, errorListener.Errors...)
errors = append(errors, listener.Errors...)
return errors
}

View File

@ -8,8 +8,6 @@ null
null
null
null
null
null
token symbolic names:
null
@ -20,9 +18,7 @@ COLON
HASHTAG
SEPARATOR
NEWLINE
DIGITS
OCTETS
DOMAIN
STRING
rule names:
lineStatement
@ -33,16 +29,13 @@ hostname
domain
ipAddress
ipv4Address
singleIPv4Address
ipv6Address
singleIPv6Address
ipv4Digit
ipv6Octet
ipRange
ipRangeBits
ipPort
comment
leadingComment
atn:
[4, 1, 10, 110, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 1, 0, 3, 0, 36, 8, 0, 1, 0, 1, 0, 3, 0, 40, 8, 0, 1, 0, 3, 0, 43, 8, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 52, 8, 1, 1, 2, 1, 2, 3, 2, 56, 8, 2, 4, 2, 58, 8, 2, 11, 2, 12, 2, 59, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 3, 6, 70, 8, 6, 1, 7, 1, 7, 3, 7, 74, 8, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 3, 9, 86, 8, 9, 1, 10, 1, 10, 1, 10, 4, 10, 91, 8, 10, 11, 10, 12, 10, 92, 1, 10, 1, 10, 1, 11, 1, 11, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 1, 14, 1, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 0, 0, 17, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 0, 0, 102, 0, 35, 1, 0, 0, 0, 2, 46, 1, 0, 0, 0, 4, 57, 1, 0, 0, 0, 6, 61, 1, 0, 0, 0, 8, 63, 1, 0, 0, 0, 10, 65, 1, 0, 0, 0, 12, 69, 1, 0, 0, 0, 14, 71, 1, 0, 0, 0, 16, 75, 1, 0, 0, 0, 18, 83, 1, 0, 0, 0, 20, 90, 1, 0, 0, 0, 22, 96, 1, 0, 0, 0, 24, 98, 1, 0, 0, 0, 26, 100, 1, 0, 0, 0, 28, 103, 1, 0, 0, 0, 30, 105, 1, 0, 0, 0, 32, 107, 1, 0, 0, 0, 34, 36, 5, 6, 0, 0, 35, 34, 1, 0, 0, 0, 35, 36, 1, 0, 0, 0, 36, 37, 1, 0, 0, 0, 37, 39, 3, 2, 1, 0, 38, 40, 5, 6, 0, 0, 39, 38, 1, 0, 0, 0, 39, 40, 1, 0, 0, 0, 40, 42, 1, 0, 0, 0, 41, 43, 3, 32, 16, 0, 42, 41, 1, 0, 0, 0, 42, 43, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 45, 5, 0, 0, 1, 45, 1, 1, 0, 0, 0, 46, 47, 3, 12, 6, 0, 47, 48, 5, 6, 0, 0, 48, 51, 3, 8, 4, 0, 49, 50, 5, 6, 0, 0, 50, 52, 3, 4, 2, 0, 51, 49, 1, 0, 0, 0, 51, 52, 1, 0, 0, 0, 52, 3, 1, 0, 0, 0, 53, 55, 3, 6, 3, 0, 54, 56, 5, 6, 0, 0, 55, 54, 1, 0, 0, 0, 55, 56, 1, 0, 0, 0, 56, 58, 1, 0, 0, 0, 57, 53, 1, 0, 0, 0, 58, 59, 1, 0, 0, 0, 59, 57, 1, 0, 0, 0, 59, 60, 1, 0, 0, 0, 60, 5, 1, 0, 0, 0, 61, 62, 5, 10, 0, 0, 62, 7, 1, 0, 0, 0, 63, 64, 3, 10, 5, 0, 64, 9, 1, 0, 0, 0, 65, 66, 5, 10, 0, 0, 66, 11, 1, 0, 0, 0, 67, 70, 3, 14, 7, 0, 68, 70, 3, 18, 9, 0, 69, 67, 1, 0, 0, 0, 69, 68, 1, 0, 0, 0, 70, 13, 1, 0, 0, 0, 71, 73, 3, 16, 8, 0, 72, 74, 3, 26, 13, 0, 73, 72, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 15, 1, 0, 0, 0, 75, 76, 3, 22, 11, 0, 76, 77, 5, 3, 0, 0, 77, 78, 3, 22, 11, 0, 78, 79, 5, 3, 0, 0, 79, 80, 3, 22, 11, 0, 80, 81, 5, 3, 0, 0, 81, 82, 3, 22, 11, 0, 82, 17, 1, 0, 0, 0, 83, 85, 3, 20, 10, 0, 84, 86, 3, 26, 13, 0, 85, 84, 1, 0, 0, 0, 85, 86, 1, 0, 0, 0, 86, 19, 1, 0, 0, 0, 87, 88, 3, 24, 12, 0, 88, 89, 5, 4, 0, 0, 89, 91, 1, 0, 0, 0, 90, 87, 1, 0, 0, 0, 91, 92, 1, 0, 0, 0, 92, 90, 1, 0, 0, 0, 92, 93, 1, 0, 0, 0, 93, 94, 1, 0, 0, 0, 94, 95, 3, 24, 12, 0, 95, 21, 1, 0, 0, 0, 96, 97, 5, 8, 0, 0, 97, 23, 1, 0, 0, 0, 98, 99, 5, 9, 0, 0, 99, 25, 1, 0, 0, 0, 100, 101, 5, 2, 0, 0, 101, 102, 3, 28, 14, 0, 102, 27, 1, 0, 0, 0, 103, 104, 5, 8, 0, 0, 104, 29, 1, 0, 0, 0, 105, 106, 5, 1, 0, 0, 106, 31, 1, 0, 0, 0, 107, 108, 5, 1, 0, 0, 108, 33, 1, 0, 0, 0, 10, 35, 39, 42, 51, 55, 59, 69, 73, 85, 92]
[4, 1, 8, 131, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2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 0, 0, 20, 121, 1, 0, 0, 0, 22, 123, 1, 0, 0, 0, 24, 126, 1, 0, 0, 0, 26, 128, 1, 0, 0, 0, 28, 30, 5, 6, 0, 0, 29, 28, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 33, 3, 2, 1, 0, 32, 34, 5, 6, 0, 0, 33, 32, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 36, 1, 0, 0, 0, 35, 37, 3, 26, 13, 0, 36, 35, 1, 0, 0, 0, 36, 37, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 39, 5, 0, 0, 1, 39, 1, 1, 0, 0, 0, 40, 41, 3, 12, 6, 0, 41, 42, 5, 6, 0, 0, 42, 45, 3, 8, 4, 0, 43, 44, 5, 6, 0, 0, 44, 46, 3, 4, 2, 0, 45, 43, 1, 0, 0, 0, 45, 46, 1, 0, 0, 0, 46, 3, 1, 0, 0, 0, 47, 49, 3, 6, 3, 0, 48, 50, 5, 6, 0, 0, 49, 48, 1, 0, 0, 0, 49, 50, 1, 0, 0, 0, 50, 52, 1, 0, 0, 0, 51, 47, 1, 0, 0, 0, 52, 53, 1, 0, 0, 0, 53, 51, 1, 0, 0, 0, 53, 54, 1, 0, 0, 0, 54, 5, 1, 0, 0, 0, 55, 56, 3, 10, 5, 0, 56, 7, 1, 0, 0, 0, 57, 58, 3, 10, 5, 0, 58, 9, 1, 0, 0, 0, 59, 61, 5, 8, 0, 0, 60, 59, 1, 0, 0, 0, 61, 62, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 73, 1, 0, 0, 0, 64, 68, 5, 3, 0, 0, 65, 67, 5, 8, 0, 0, 66, 65, 1, 0, 0, 0, 67, 70, 1, 0, 0, 0, 68, 66, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69, 72, 1, 0, 0, 0, 70, 68, 1, 0, 0, 0, 71, 64, 1, 0, 0, 0, 72, 75, 1, 0, 0, 0, 73, 71, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 11, 1, 0, 0, 0, 75, 73, 1, 0, 0, 0, 76, 79, 3, 14, 7, 0, 77, 79, 3, 16, 8, 0, 78, 76, 1, 0, 0, 0, 78, 77, 1, 0, 0, 0, 79, 13, 1, 0, 0, 0, 80, 81, 5, 8, 0, 0, 81, 83, 5, 3, 0, 0, 82, 80, 1, 0, 0, 0, 83, 84, 1, 0, 0, 0, 84, 82, 1, 0, 0, 0, 84, 85, 1, 0, 0, 0, 85, 86, 1, 0, 0, 0, 86, 93, 5, 8, 0, 0, 87, 89, 3, 18, 9, 0, 88, 87, 1, 0, 0, 0, 88, 89, 1, 0, 0, 0, 89, 94, 1, 0, 0, 0, 90, 92, 3, 22, 11, 0, 91, 90, 1, 0, 0, 0, 91, 92, 1, 0, 0, 0, 92, 94, 1, 0, 0, 0, 93, 88, 1, 0, 0, 0, 93, 91, 1, 0, 0, 0, 94, 15, 1, 0, 0, 0, 95, 96, 5, 8, 0, 0, 96, 98, 5, 4, 0, 0, 97, 95, 1, 0, 0, 0, 98, 99, 1, 0, 0, 0, 99, 97, 1, 0, 0, 0, 99, 100, 1, 0, 0, 0, 100, 101, 1, 0, 0, 0, 101, 109, 5, 8, 0, 0, 102, 104, 5, 8, 0, 0, 103, 102, 1, 0, 0, 0, 103, 104, 1, 0, 0, 0, 104, 105, 1, 0, 0, 0, 105, 106, 5, 4, 0, 0, 106, 107, 5, 4, 0, 0, 107, 109, 5, 8, 0, 0, 108, 97, 1, 0, 0, 0, 108, 103, 1, 0, 0, 0, 109, 116, 1, 0, 0, 0, 110, 112, 3, 18, 9, 0, 111, 110, 1, 0, 0, 0, 111, 112, 1, 0, 0, 0, 112, 117, 1, 0, 0, 0, 113, 115, 3, 22, 11, 0, 114, 113, 1, 0, 0, 0, 114, 115, 1, 0, 0, 0, 115, 117, 1, 0, 0, 0, 116, 111, 1, 0, 0, 0, 116, 114, 1, 0, 0, 0, 117, 17, 1, 0, 0, 0, 118, 119, 5, 2, 0, 0, 119, 120, 3, 20, 10, 0, 120, 19, 1, 0, 0, 0, 121, 122, 5, 8, 0, 0, 122, 21, 1, 0, 0, 0, 123, 124, 5, 4, 0, 0, 124, 125, 5, 8, 0, 0, 125, 23, 1, 0, 0, 0, 126, 127, 5, 1, 0, 0, 127, 25, 1, 0, 0, 0, 128, 129, 5, 1, 0, 0, 129, 27, 1, 0, 0, 0, 20, 29, 33, 36, 45, 49, 53, 62, 68, 73, 78, 84, 88, 91, 93, 99, 103, 108, 111, 114, 116]

View File

@ -5,9 +5,7 @@ COLON=4
HASHTAG=5
SEPARATOR=6
NEWLINE=7
DIGITS=8
OCTETS=9
DOMAIN=10
STRING=8
'/'=2
'.'=3
':'=4

View File

@ -8,8 +8,6 @@ null
null
null
null
null
null
token symbolic names:
null
@ -20,9 +18,7 @@ COLON
HASHTAG
SEPARATOR
NEWLINE
DIGITS
OCTETS
DOMAIN
STRING
rule names:
COMMENTLINE
@ -32,11 +28,6 @@ COLON
HASHTAG
SEPARATOR
NEWLINE
DIGITS
DIGIT
OCTETS
OCTET
DOMAIN
STRING
channel names:
@ -47,4 +38,4 @@ mode names:
DEFAULT_MODE
atn:
[4, 0, 10, 83, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 1, 0, 1, 0, 4, 0, 30, 8, 0, 11, 0, 12, 0, 31, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 4, 5, 43, 8, 5, 11, 5, 12, 5, 44, 1, 6, 3, 6, 48, 8, 6, 1, 6, 1, 6, 1, 7, 4, 7, 53, 8, 7, 11, 7, 12, 7, 54, 1, 8, 1, 8, 1, 9, 4, 9, 60, 8, 9, 11, 9, 12, 9, 61, 1, 10, 1, 10, 1, 11, 4, 11, 67, 8, 11, 11, 11, 12, 11, 68, 1, 11, 1, 11, 4, 11, 73, 8, 11, 11, 11, 12, 11, 74, 5, 11, 77, 8, 11, 10, 11, 12, 11, 80, 9, 11, 1, 12, 1, 12, 0, 0, 13, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 0, 19, 9, 21, 0, 23, 10, 25, 0, 1, 0, 8, 2, 0, 10, 10, 13, 13, 2, 0, 9, 9, 32, 32, 1, 0, 13, 13, 1, 0, 10, 10, 1, 0, 48, 57, 3, 0, 48, 57, 65, 70, 97, 102, 2, 0, 65, 90, 97, 122, 5, 0, 9, 10, 13, 13, 32, 32, 35, 35, 46, 46, 87, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 1, 27, 1, 0, 0, 0, 3, 33, 1, 0, 0, 0, 5, 35, 1, 0, 0, 0, 7, 37, 1, 0, 0, 0, 9, 39, 1, 0, 0, 0, 11, 42, 1, 0, 0, 0, 13, 47, 1, 0, 0, 0, 15, 52, 1, 0, 0, 0, 17, 56, 1, 0, 0, 0, 19, 59, 1, 0, 0, 0, 21, 63, 1, 0, 0, 0, 23, 66, 1, 0, 0, 0, 25, 81, 1, 0, 0, 0, 27, 29, 3, 9, 4, 0, 28, 30, 8, 0, 0, 0, 29, 28, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 29, 1, 0, 0, 0, 31, 32, 1, 0, 0, 0, 32, 2, 1, 0, 0, 0, 33, 34, 5, 47, 0, 0, 34, 4, 1, 0, 0, 0, 35, 36, 5, 46, 0, 0, 36, 6, 1, 0, 0, 0, 37, 38, 5, 58, 0, 0, 38, 8, 1, 0, 0, 0, 39, 40, 5, 35, 0, 0, 40, 10, 1, 0, 0, 0, 41, 43, 7, 1, 0, 0, 42, 41, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 44, 45, 1, 0, 0, 0, 45, 12, 1, 0, 0, 0, 46, 48, 7, 2, 0, 0, 47, 46, 1, 0, 0, 0, 47, 48, 1, 0, 0, 0, 48, 49, 1, 0, 0, 0, 49, 50, 7, 3, 0, 0, 50, 14, 1, 0, 0, 0, 51, 53, 3, 17, 8, 0, 52, 51, 1, 0, 0, 0, 53, 54, 1, 0, 0, 0, 54, 52, 1, 0, 0, 0, 54, 55, 1, 0, 0, 0, 55, 16, 1, 0, 0, 0, 56, 57, 7, 4, 0, 0, 57, 18, 1, 0, 0, 0, 58, 60, 3, 21, 10, 0, 59, 58, 1, 0, 0, 0, 60, 61, 1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 61, 62, 1, 0, 0, 0, 62, 20, 1, 0, 0, 0, 63, 64, 7, 5, 0, 0, 64, 22, 1, 0, 0, 0, 65, 67, 3, 25, 12, 0, 66, 65, 1, 0, 0, 0, 67, 68, 1, 0, 0, 0, 68, 66, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69, 78, 1, 0, 0, 0, 70, 72, 3, 5, 2, 0, 71, 73, 7, 6, 0, 0, 72, 71, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 72, 1, 0, 0, 0, 74, 75, 1, 0, 0, 0, 75, 77, 1, 0, 0, 0, 76, 70, 1, 0, 0, 0, 77, 80, 1, 0, 0, 0, 78, 76, 1, 0, 0, 0, 78, 79, 1, 0, 0, 0, 79, 24, 1, 0, 0, 0, 80, 78, 1, 0, 0, 0, 81, 82, 8, 7, 0, 0, 82, 26, 1, 0, 0, 0, 9, 0, 31, 44, 47, 54, 61, 68, 74, 78, 0]
[4, 0, 8, 46, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2,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33, 34, 1, 0, 0, 0, 34, 32, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 12, 1, 0, 0, 0, 36, 38, 7, 2, 0, 0, 37, 36, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 39, 1, 0, 0, 0, 39, 40, 7, 3, 0, 0, 40, 14, 1, 0, 0, 0, 41, 43, 7, 4, 0, 0, 42, 41, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 44, 45, 1, 0, 0, 0, 45, 16, 1, 0, 0, 0, 5, 0, 21, 34, 37, 44, 0]

View File

@ -5,9 +5,7 @@ COLON=4
HASHTAG=5
SEPARATOR=6
NEWLINE=7
DIGITS=8
OCTETS=9
DOMAIN=10
STRING=8
'/'=2
'.'=3
':'=4

View File

@ -69,36 +69,12 @@ func (s *BaseHostsListener) EnterIpv4Address(ctx *Ipv4AddressContext) {}
// ExitIpv4Address is called when production ipv4Address is exited.
func (s *BaseHostsListener) ExitIpv4Address(ctx *Ipv4AddressContext) {}
// EnterSingleIPv4Address is called when production singleIPv4Address is entered.
func (s *BaseHostsListener) EnterSingleIPv4Address(ctx *SingleIPv4AddressContext) {}
// ExitSingleIPv4Address is called when production singleIPv4Address is exited.
func (s *BaseHostsListener) ExitSingleIPv4Address(ctx *SingleIPv4AddressContext) {}
// EnterIpv6Address is called when production ipv6Address is entered.
func (s *BaseHostsListener) EnterIpv6Address(ctx *Ipv6AddressContext) {}
// ExitIpv6Address is called when production ipv6Address is exited.
func (s *BaseHostsListener) ExitIpv6Address(ctx *Ipv6AddressContext) {}
// EnterSingleIPv6Address is called when production singleIPv6Address is entered.
func (s *BaseHostsListener) EnterSingleIPv6Address(ctx *SingleIPv6AddressContext) {}
// ExitSingleIPv6Address is called when production singleIPv6Address is exited.
func (s *BaseHostsListener) ExitSingleIPv6Address(ctx *SingleIPv6AddressContext) {}
// EnterIpv4Digit is called when production ipv4Digit is entered.
func (s *BaseHostsListener) EnterIpv4Digit(ctx *Ipv4DigitContext) {}
// ExitIpv4Digit is called when production ipv4Digit is exited.
func (s *BaseHostsListener) ExitIpv4Digit(ctx *Ipv4DigitContext) {}
// EnterIpv6Octet is called when production ipv6Octet is entered.
func (s *BaseHostsListener) EnterIpv6Octet(ctx *Ipv6OctetContext) {}
// ExitIpv6Octet is called when production ipv6Octet is exited.
func (s *BaseHostsListener) ExitIpv6Octet(ctx *Ipv6OctetContext) {}
// EnterIpRange is called when production ipRange is entered.
func (s *BaseHostsListener) EnterIpRange(ctx *IpRangeContext) {}
@ -111,6 +87,12 @@ func (s *BaseHostsListener) EnterIpRangeBits(ctx *IpRangeBitsContext) {}
// ExitIpRangeBits is called when production ipRangeBits is exited.
func (s *BaseHostsListener) ExitIpRangeBits(ctx *IpRangeBitsContext) {}
// EnterIpPort is called when production ipPort is entered.
func (s *BaseHostsListener) EnterIpPort(ctx *IpPortContext) {}
// ExitIpPort is called when production ipPort is exited.
func (s *BaseHostsListener) ExitIpPort(ctx *IpPortContext) {}
// EnterComment is called when production comment is entered.
func (s *BaseHostsListener) EnterComment(ctx *CommentContext) {}

View File

@ -47,51 +47,35 @@ func hostslexerLexerInit() {
}
staticData.SymbolicNames = []string{
"", "COMMENTLINE", "SLASH", "DOT", "COLON", "HASHTAG", "SEPARATOR",
"NEWLINE", "DIGITS", "OCTETS", "DOMAIN",
"NEWLINE", "STRING",
}
staticData.RuleNames = []string{
"COMMENTLINE", "SLASH", "DOT", "COLON", "HASHTAG", "SEPARATOR", "NEWLINE",
"DIGITS", "DIGIT", "OCTETS", "OCTET", "DOMAIN", "STRING",
"STRING",
}
staticData.PredictionContextCache = antlr.NewPredictionContextCache()
staticData.serializedATN = []int32{
4, 0, 10, 83, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2,
4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2,
10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 1, 0, 1, 0, 4, 0, 30, 8, 0, 11,
0, 12, 0, 31, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 4,
5, 43, 8, 5, 11, 5, 12, 5, 44, 1, 6, 3, 6, 48, 8, 6, 1, 6, 1, 6, 1, 7,
4, 7, 53, 8, 7, 11, 7, 12, 7, 54, 1, 8, 1, 8, 1, 9, 4, 9, 60, 8, 9, 11,
9, 12, 9, 61, 1, 10, 1, 10, 1, 11, 4, 11, 67, 8, 11, 11, 11, 12, 11, 68,
1, 11, 1, 11, 4, 11, 73, 8, 11, 11, 11, 12, 11, 74, 5, 11, 77, 8, 11, 10,
11, 12, 11, 80, 9, 11, 1, 12, 1, 12, 0, 0, 13, 1, 1, 3, 2, 5, 3, 7, 4,
9, 5, 11, 6, 13, 7, 15, 8, 17, 0, 19, 9, 21, 0, 23, 10, 25, 0, 1, 0, 8,
2, 0, 10, 10, 13, 13, 2, 0, 9, 9, 32, 32, 1, 0, 13, 13, 1, 0, 10, 10, 1,
0, 48, 57, 3, 0, 48, 57, 65, 70, 97, 102, 2, 0, 65, 90, 97, 122, 5, 0,
9, 10, 13, 13, 32, 32, 35, 35, 46, 46, 87, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0,
0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0,
0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 23, 1,
0, 0, 0, 1, 27, 1, 0, 0, 0, 3, 33, 1, 0, 0, 0, 5, 35, 1, 0, 0, 0, 7, 37,
1, 0, 0, 0, 9, 39, 1, 0, 0, 0, 11, 42, 1, 0, 0, 0, 13, 47, 1, 0, 0, 0,
15, 52, 1, 0, 0, 0, 17, 56, 1, 0, 0, 0, 19, 59, 1, 0, 0, 0, 21, 63, 1,
0, 0, 0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 0, 0, 11, 32, 1, 0, 0, 0, 13, 37, 1, 0, 0,
0, 15, 42, 1, 0, 0, 0, 17, 19, 3, 9, 4, 0, 18, 20, 8, 0, 0, 0, 19, 18,
1, 0, 0, 0, 20, 21, 1, 0, 0, 0, 21, 19, 1, 0, 0, 0, 21, 22, 1, 0, 0, 0,
22, 2, 1, 0, 0, 0, 23, 24, 5, 47, 0, 0, 24, 4, 1, 0, 0, 0, 25, 26, 5, 46,
0, 0, 26, 6, 1, 0, 0, 0, 27, 28, 5, 58, 0, 0, 28, 8, 1, 0, 0, 0, 29, 30,
5, 35, 0, 0, 30, 10, 1, 0, 0, 0, 31, 33, 7, 1, 0, 0, 32, 31, 1, 0, 0, 0,
33, 34, 1, 0, 0, 0, 34, 32, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 12, 1,
0, 0, 0, 36, 38, 7, 2, 0, 0, 37, 36, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38,
39, 1, 0, 0, 0, 39, 40, 7, 3, 0, 0, 40, 14, 1, 0, 0, 0, 41, 43, 7, 4, 0,
0, 42, 41, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 44, 45,
1, 0, 0, 0, 45, 12, 1, 0, 0, 0, 46, 48, 7, 2, 0, 0, 47, 46, 1, 0, 0, 0,
47, 48, 1, 0, 0, 0, 48, 49, 1, 0, 0, 0, 49, 50, 7, 3, 0, 0, 50, 14, 1,
0, 0, 0, 51, 53, 3, 17, 8, 0, 52, 51, 1, 0, 0, 0, 53, 54, 1, 0, 0, 0, 54,
52, 1, 0, 0, 0, 54, 55, 1, 0, 0, 0, 55, 16, 1, 0, 0, 0, 56, 57, 7, 4, 0,
0, 57, 18, 1, 0, 0, 0, 58, 60, 3, 21, 10, 0, 59, 58, 1, 0, 0, 0, 60, 61,
1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 61, 62, 1, 0, 0, 0, 62, 20, 1, 0, 0, 0,
63, 64, 7, 5, 0, 0, 64, 22, 1, 0, 0, 0, 65, 67, 3, 25, 12, 0, 66, 65, 1,
0, 0, 0, 67, 68, 1, 0, 0, 0, 68, 66, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69,
78, 1, 0, 0, 0, 70, 72, 3, 5, 2, 0, 71, 73, 7, 6, 0, 0, 72, 71, 1, 0, 0,
0, 73, 74, 1, 0, 0, 0, 74, 72, 1, 0, 0, 0, 74, 75, 1, 0, 0, 0, 75, 77,
1, 0, 0, 0, 76, 70, 1, 0, 0, 0, 77, 80, 1, 0, 0, 0, 78, 76, 1, 0, 0, 0,
78, 79, 1, 0, 0, 0, 79, 24, 1, 0, 0, 0, 80, 78, 1, 0, 0, 0, 81, 82, 8,
7, 0, 0, 82, 26, 1, 0, 0, 0, 9, 0, 31, 44, 47, 54, 61, 68, 74, 78, 0,
1, 0, 0, 0, 45, 16, 1, 0, 0, 0, 5, 0, 21, 34, 37, 44, 0,
}
deserializer := antlr.NewATNDeserializer(nil)
staticData.atn = deserializer.Deserialize(staticData.serializedATN)
@ -139,7 +123,5 @@ const (
HostsLexerHASHTAG = 5
HostsLexerSEPARATOR = 6
HostsLexerNEWLINE = 7
HostsLexerDIGITS = 8
HostsLexerOCTETS = 9
HostsLexerDOMAIN = 10
HostsLexerSTRING = 8
)

View File

@ -32,27 +32,18 @@ type HostsListener interface {
// EnterIpv4Address is called when entering the ipv4Address production.
EnterIpv4Address(c *Ipv4AddressContext)
// EnterSingleIPv4Address is called when entering the singleIPv4Address production.
EnterSingleIPv4Address(c *SingleIPv4AddressContext)
// EnterIpv6Address is called when entering the ipv6Address production.
EnterIpv6Address(c *Ipv6AddressContext)
// EnterSingleIPv6Address is called when entering the singleIPv6Address production.
EnterSingleIPv6Address(c *SingleIPv6AddressContext)
// EnterIpv4Digit is called when entering the ipv4Digit production.
EnterIpv4Digit(c *Ipv4DigitContext)
// EnterIpv6Octet is called when entering the ipv6Octet production.
EnterIpv6Octet(c *Ipv6OctetContext)
// EnterIpRange is called when entering the ipRange production.
EnterIpRange(c *IpRangeContext)
// EnterIpRangeBits is called when entering the ipRangeBits production.
EnterIpRangeBits(c *IpRangeBitsContext)
// EnterIpPort is called when entering the ipPort production.
EnterIpPort(c *IpPortContext)
// EnterComment is called when entering the comment production.
EnterComment(c *CommentContext)
@ -83,27 +74,18 @@ type HostsListener interface {
// ExitIpv4Address is called when exiting the ipv4Address production.
ExitIpv4Address(c *Ipv4AddressContext)
// ExitSingleIPv4Address is called when exiting the singleIPv4Address production.
ExitSingleIPv4Address(c *SingleIPv4AddressContext)
// ExitIpv6Address is called when exiting the ipv6Address production.
ExitIpv6Address(c *Ipv6AddressContext)
// ExitSingleIPv6Address is called when exiting the singleIPv6Address production.
ExitSingleIPv6Address(c *SingleIPv6AddressContext)
// ExitIpv4Digit is called when exiting the ipv4Digit production.
ExitIpv4Digit(c *Ipv4DigitContext)
// ExitIpv6Octet is called when exiting the ipv6Octet production.
ExitIpv6Octet(c *Ipv6OctetContext)
// ExitIpRange is called when exiting the ipRange production.
ExitIpRange(c *IpRangeContext)
// ExitIpRangeBits is called when exiting the ipRangeBits production.
ExitIpRangeBits(c *IpRangeBitsContext)
// ExitIpPort is called when exiting the ipPort production.
ExitIpPort(c *IpPortContext)
// ExitComment is called when exiting the comment production.
ExitComment(c *CommentContext)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,20 @@
package ast
import (
"config-lsp/utils"
"testing"
)
func TestParserInvalidWithPort(
t *testing.T,
) {
input := utils.Dedent(`
1.2.3.4:80 hello.com
`)
parser := NewHostsParser()
errors := parser.Parse(input)
if len(errors) != 1 {
t.Fatalf("Expected 1 error, but got %v", errors)
}
}

View File

@ -29,9 +29,9 @@ func TextDocumentDidChange(
return err.ToDiagnostic()
},
)...)
}
} else {
diagnostics = append(diagnostics, analyzer.Analyze(document)...)
}
if len(diagnostics) > 0 {
common.SendDiagnostics(context, params.TextDocument.URI, diagnostics)

View File

@ -7,6 +7,8 @@ import (
"config-lsp/handlers/hosts/ast"
"config-lsp/handlers/hosts/indexes"
"config-lsp/utils"
"fmt"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
@ -26,18 +28,22 @@ func TextDocumentDidOpen(
hosts.DocumentParserMap[params.TextDocument.URI] = &document
errors := parser.Parse(params.TextDocument.Text)
diagnostics := make([]protocol.Diagnostic, 0)
diagnostics := utils.Map(
println(fmt.Sprintf("Errors: %v", errors))
if len(errors) > 0 {
diagnostics = utils.Map(
errors,
func(err common.LSPError) protocol.Diagnostic {
return err.ToDiagnostic()
},
)
} else {
diagnostics = append(
diagnostics,
analyzer.Analyze(&document)...,
)
}
if len(diagnostics) > 0 {
common.SendDiagnostics(context, params.TextDocument.URI, diagnostics)