summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/miekg/dns/scanner.go
blob: 424e5af9f593136782d9482193cee9b7b76930e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package dns

// Implement a simple scanner, return a byte stream from an io reader.

import (
	"bufio"
	"context"
	"io"
	"text/scanner"
)

type scan struct {
	src      *bufio.Reader
	position scanner.Position
	eof      bool // Have we just seen a eof
	ctx      context.Context
}

func scanInit(r io.Reader) (*scan, context.CancelFunc) {
	s := new(scan)
	s.src = bufio.NewReader(r)
	s.position.Line = 1

	ctx, cancel := context.WithCancel(context.Background())
	s.ctx = ctx

	return s, cancel
}

// 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
	}
	select {
	case <-s.ctx.Done():
		return c, context.Canceled
	default:
		break
	}

	// 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
}