diff --git a/parse_test.go b/parse_test.go index 6030a0fd..bff093fe 100644 --- a/parse_test.go +++ b/parse_test.go @@ -703,3 +703,19 @@ func TestILNP(t *testing.T) { } } } + +func TestComment(t *testing.T) { + zone := ` +foo. IN A 10.0.0.1 ; this is a comment1 +foo. IN A ( + 10.0.0.2 ; this is a comment2 +) +; this is a comment3 +foo. IN A 10.0.0.3 +` + for x := range ParseZone(strings.NewReader(zone), ".", "") { + if x.Error == nil { + fmt.Printf("%s\n", x.RR.String()) + } + } +} diff --git a/zscan.go b/zscan.go index c56fa1e8..784b261f 100644 --- a/zscan.go +++ b/zscan.go @@ -305,7 +305,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { t <- Token{Error: &ParseError{f, "expecting $TTL value, not this...", l}} return } - if e := slurpRemainder(c, f); e != nil { + if e, _ := slurpRemainder(c, f); e != nil { t <- Token{Error: e} return } @@ -327,7 +327,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { t <- Token{Error: &ParseError{f, "expecting $ORIGIN value, not this...", l}} return } - if e := slurpRemainder(c, f); e != nil { + if e, _ := slurpRemainder(c, f); e != nil { t <- Token{Error: e} } if _, _, ok := IsDomainName(l.token); !ok { @@ -628,7 +628,8 @@ func zlexer(s *scan, c chan lex) { owner = true l.value = _NEWLINE l.token = "\n" - debug.Printf("[3 %+v]", l.token) + l.comment = string(com[:comi]) + debug.Printf("[3 %+v %+v]", l.token, l.comment) c <- l comi = 0 break @@ -878,24 +879,21 @@ func locCheckEast(token string, longitude uint32) (uint32, bool) { return longitude, false } -// "Eat" the rest of the "line" -func slurpRemainder(c chan lex, f string) *ParseError { +// "Eat" the rest of the "line". Return potential comments +func slurpRemainder(c chan lex, f string) (*ParseError, string) { l := <-c switch l.value { case _BLANK: l = <-c if l.value != _NEWLINE && l.value != _EOF { - return &ParseError{f, "garbage after rdata", l} + return &ParseError{f, "garbage after rdata", l}, "" } - // Ok case _NEWLINE: - // Ok case _EOF: - // Ok default: - return &ParseError{f, "garbage after rdata", l} + return &ParseError{f, "garbage after rdata", l}, "" } - return nil + return nil, l.comment } // Parse a 64 bit-like ipv6 address: "0014:4fff:ff20:ee64" diff --git a/zscan_rr.go b/zscan_rr.go index 7672297c..546a5270 100644 --- a/zscan_rr.go +++ b/zscan_rr.go @@ -12,7 +12,7 @@ import ( // After the rdata there may come a _BLANK and then a _NEWLINE // or immediately a _NEWLINE. If this is not the case we flag // an *ParseError: garbage after rdata. -func setRR(h RR_Header, c chan lex, o, f string) (RR, *ParseError) { +func setRR(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) { var r RR e := new(ParseError) switch h.Rrtype { @@ -146,31 +146,30 @@ func setRR(h RR_Header, c chan lex, o, f string) (RR, *ParseError) { } Slurp: if e != nil { - return nil, e + return nil, e, "" } - if se := slurpRemainder(c, f); se != nil { - return nil, se + if se, com := slurpRemainder(c, f); se != nil { + return nil, se, com } - return r, e + return r, e, "" } // A remainder of the rdata with embedded spaces, return the parsed string (sans the spaces) // or an error -func endingToString(c chan lex, errstr, f string) (string, *ParseError) { +func endingToString(c chan lex, errstr, f string) (string, *ParseError, string) { s := "" l := <-c // _STRING for l.value != _NEWLINE && l.value != _EOF { switch l.value { case _STRING: s += l.token - case _BLANK: - // Ok + case _BLANK: // Ok default: - return "", &ParseError{f, errstr, l} + return "", &ParseError{f, errstr, l}, "" } l = <-c } - return s, nil + return s, nil, l.comment } // A remainder of the rdata with embedded spaces, return the parsed string slice (sans the spaces)