Skip to content

Commit

Permalink
lexers: Add BIND DNS Zone lexer
Browse files Browse the repository at this point in the history
Closes #623
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
  • Loading branch information
ahmubashshir authored and alecthomas committed Jun 27, 2022
1 parent f941d46 commit 5ce1d5d
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lexers/dns.go
@@ -0,0 +1,17 @@
package lexers

import (
"regexp"
)

// TODO(moorereason): can this be factored away?
var zoneAnalyserRe = regexp.MustCompile(`(?m)^@\s+IN\s+SOA\s+`)

func init() { // nolint: gochecknoinits
Get("dns").SetAnalyser(func(text string) float32 {
if zoneAnalyserRe.FindString(text) != "" {
return 1.0
}
return 0.0
})
}
42 changes: 42 additions & 0 deletions lexers/embedded/dns.xml
@@ -0,0 +1,42 @@
<?xml version="1.0"?>
<lexer>
<config>
<name>dns</name>
<alias>zone</alias>
<alias>bind</alias>
</config>
<rules>
<state name="root">
<rule pattern="\b(IN|A|AAAA|AFSDB|APL|CAA|CDNSKEY|CDS|CERT|CNAME|DHCID|DLV|DNAME|DNSKEY|DS|HIP|IPSECKEY|KEY|KX|LOC|MX|NAPTR|NS|NSEC|NSEC3|NSEC3PARAM|PTR|RRSIG|RP|SIG|SOA|SRV|SSHFP|TA|TKEY|TLSA|TSIG|TXT)\b">
<token type="Keyword"/>
</rule>
<rule pattern=";.*(\S|$)">
<token type="Comment"/>
</rule>
<rule pattern="\b((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}))|:)))\b">
<token type="LiteralNumberIntegerLong"/>
</rule>
<rule pattern="\b((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\b">
<token type="LiteralNumberInteger"/>
</rule>
<rule pattern="\b\d+[dhwm]?">
<token type="LiteralStringChar"/>
</rule>
<rule pattern="\b([[:alnum:].-])+\.($|\s)">
<token type="NameProperty"/>
</rule>
<rule pattern="^(@|[[:alnum:]-]+)">
<token type="NameClass"/>
</rule>
<rule pattern="^\$(TTL|GENERATE|INCLUDE|ORIGIN)">
<token type="NameAttribute"/>
</rule>
<rule pattern="\(|\)">
<token type="Punctuation"/>
</rule>
<rule pattern="[\r\n\s\t]+">
<token type="TextWhitespace"/>
</rule>
</state>
</rules>
</lexer>
19 changes: 19 additions & 0 deletions lexers/testdata/dns.actual
@@ -0,0 +1,19 @@
; /etc/bind/zones/example.org
$TTL 3h;
@ IN SOA ns.example.org. example.org. (
1 ; serial
3h; refresh timeout
1h; retry timeout
1w; expire timeout
1h; negative caching TTL
)
@ IN NS ns.example.org. ; name server
@ IN MX 10 example.org. ; mail exchange server

ns IN A 1.2.3.4
@ IN A 1.2.3.4

ns IN AAAA ef::1
@ IN AAAA ef::1

www IN CNAME example.org.
98 changes: 98 additions & 0 deletions lexers/testdata/dns.expected
@@ -0,0 +1,98 @@
[
{"type":"Comment","value":"; /etc/bind/zones/example.org"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameAttribute","value":"$TTL"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralStringChar","value":"3h"},
{"type":"Comment","value":";"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameClass","value":"@"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"SOA"},
{"type":"TextWhitespace","value":"\t"},
{"type":"NameProperty","value":"ns.example.org. example.org. "},
{"type":"Punctuation","value":"("},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"LiteralStringChar","value":"1"},
{"type":"TextWhitespace","value":" "},
{"type":"Comment","value":"; serial"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"LiteralStringChar","value":"3h"},
{"type":"Comment","value":"; refresh timeout"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"LiteralStringChar","value":"1h"},
{"type":"Comment","value":"; retry timeout"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"LiteralStringChar","value":"1w"},
{"type":"Comment","value":"; expire timeout"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"LiteralStringChar","value":"1h"},
{"type":"Comment","value":"; negative caching TTL"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameClass","value":"@"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"NS"},
{"type":"TextWhitespace","value":"\t\t"},
{"type":"NameProperty","value":"ns.example.org. "},
{"type":"TextWhitespace","value":" "},
{"type":"Comment","value":"; name server"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameClass","value":"@"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"MX"},
{"type":"TextWhitespace","value":"\t"},
{"type":"LiteralStringChar","value":"10"},
{"type":"TextWhitespace","value":"\t"},
{"type":"NameProperty","value":"example.org. "},
{"type":"TextWhitespace","value":" "},
{"type":"Comment","value":"; mail exchange server"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"NameClass","value":"ns"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"A"},
{"type":"TextWhitespace","value":"\t\t"},
{"type":"LiteralNumberInteger","value":"1.2.3.4"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameClass","value":"@"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"A"},
{"type":"TextWhitespace","value":"\t\t"},
{"type":"LiteralNumberInteger","value":"1.2.3.4"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"NameClass","value":"ns"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"AAAA"},
{"type":"TextWhitespace","value":"\t"},
{"type":"LiteralNumberIntegerLong","value":"ef::1"},
{"type":"TextWhitespace","value":"\n"},
{"type":"NameClass","value":"@"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"AAAA"},
{"type":"TextWhitespace","value":"\t"},
{"type":"LiteralNumberIntegerLong","value":"ef::1"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"NameClass","value":"www"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"IN"},
{"type":"TextWhitespace","value":"\t"},
{"type":"Keyword","value":"CNAME"},
{"type":"TextWhitespace","value":"\t"},
{"type":"NameProperty","value":"example.org."},
{"type":"TextWhitespace","value":"\n"}
]

0 comments on commit 5ce1d5d

Please sign in to comment.