scanner.go 808 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package dns
  2. // Implement a simple scanner, return a byte stream from an io reader.
  3. import (
  4. "bufio"
  5. "io"
  6. "text/scanner"
  7. )
  8. type scan struct {
  9. src *bufio.Reader
  10. position scanner.Position
  11. eof bool // Have we just seen a eof
  12. }
  13. func scanInit(r io.Reader) *scan {
  14. s := new(scan)
  15. s.src = bufio.NewReader(r)
  16. s.position.Line = 1
  17. return s
  18. }
  19. // tokenText returns the next byte from the input
  20. func (s *scan) tokenText() (byte, error) {
  21. c, err := s.src.ReadByte()
  22. if err != nil {
  23. return c, err
  24. }
  25. // delay the newline handling until the next token is delivered,
  26. // fixes off-by-one errors when reporting a parse error.
  27. if s.eof == true {
  28. s.position.Line++
  29. s.position.Column = 0
  30. s.eof = false
  31. }
  32. if c == '\n' {
  33. s.eof = true
  34. return c, nil
  35. }
  36. s.position.Column++
  37. return c, nil
  38. }