2016-02-13 00:50:37 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
// Implement a simple scanner, return a byte stream from an io reader.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-03-21 13:44:58 +00:00
|
|
|
"context"
|
2016-02-13 00:50:37 +00:00
|
|
|
"io"
|
|
|
|
"text/scanner"
|
|
|
|
)
|
|
|
|
|
|
|
|
type scan struct {
|
|
|
|
src *bufio.Reader
|
|
|
|
position scanner.Position
|
|
|
|
eof bool // Have we just seen a eof
|
2018-03-21 13:44:58 +00:00
|
|
|
ctx context.Context
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:44:58 +00:00
|
|
|
func scanInit(r io.Reader) (*scan, context.CancelFunc) {
|
2016-02-13 00:50:37 +00:00
|
|
|
s := new(scan)
|
|
|
|
s.src = bufio.NewReader(r)
|
|
|
|
s.position.Line = 1
|
2018-03-21 13:44:58 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
s.ctx = ctx
|
|
|
|
|
|
|
|
return s, cancel
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tokenText returns the next byte from the input
|
|
|
|
func (s *scan) tokenText() (byte, error) {
|
|
|
|
c, err := s.src.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
2018-03-21 13:44:58 +00:00
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return c, context.Canceled
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-02-13 00:50:37 +00:00
|
|
|
// delay the newline handling until the next token is delivered,
|
|
|
|
// fixes off-by-one errors when reporting a parse error.
|
|
|
|
if s.eof == true {
|
|
|
|
s.position.Line++
|
|
|
|
s.position.Column = 0
|
|
|
|
s.eof = false
|
|
|
|
}
|
|
|
|
if c == '\n' {
|
|
|
|
s.eof = true
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
s.position.Column++
|
|
|
|
return c, nil
|
|
|
|
}
|