lexer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package buffer // import "github.com/tdewolff/parse/buffer"
  2. import (
  3. "io"
  4. "io/ioutil"
  5. )
  6. var nullBuffer = []byte{0}
  7. // Lexer is a buffered reader that allows peeking forward and shifting, taking an io.Reader.
  8. // It keeps data in-memory until Free, taking a byte length, is called to move beyond the data.
  9. type Lexer struct {
  10. buf []byte
  11. pos int // index in buf
  12. start int // index in buf
  13. err error
  14. restore func()
  15. }
  16. // NewLexerBytes returns a new Lexer for a given io.Reader, and uses ioutil.ReadAll to read it into a byte slice.
  17. // If the io.Reader implements Bytes, that is used instead.
  18. // It will append a NULL at the end of the buffer.
  19. func NewLexer(r io.Reader) *Lexer {
  20. var b []byte
  21. if r != nil {
  22. if buffer, ok := r.(interface {
  23. Bytes() []byte
  24. }); ok {
  25. b = buffer.Bytes()
  26. } else {
  27. var err error
  28. b, err = ioutil.ReadAll(r)
  29. if err != nil {
  30. return &Lexer{
  31. buf: []byte{0},
  32. err: err,
  33. }
  34. }
  35. }
  36. }
  37. return NewLexerBytes(b)
  38. }
  39. // NewLexerBytes returns a new Lexer for a given byte slice, and appends NULL at the end.
  40. // To avoid reallocation, make sure the capacity has room for one more byte.
  41. func NewLexerBytes(b []byte) *Lexer {
  42. z := &Lexer{
  43. buf: b,
  44. }
  45. n := len(b)
  46. if n == 0 {
  47. z.buf = nullBuffer
  48. } else if b[n-1] != 0 {
  49. // Append NULL to buffer, but try to avoid reallocation
  50. if cap(b) > n {
  51. // Overwrite next byte but restore when done
  52. b = b[:n+1]
  53. c := b[n]
  54. b[n] = 0
  55. z.buf = b
  56. z.restore = func() {
  57. b[n] = c
  58. }
  59. } else {
  60. z.buf = append(b, 0)
  61. }
  62. }
  63. return z
  64. }
  65. // Restore restores the replaced byte past the end of the buffer by NULL.
  66. func (z *Lexer) Restore() {
  67. if z.restore != nil {
  68. z.restore()
  69. z.restore = nil
  70. }
  71. }
  72. // Err returns the error returned from io.Reader or io.EOF when the end has been reached.
  73. func (z *Lexer) Err() error {
  74. if z.err != nil {
  75. return z.err
  76. } else if z.pos >= len(z.buf)-1 {
  77. return io.EOF
  78. }
  79. return nil
  80. }
  81. // Peek returns the ith byte relative to the end position.
  82. // Peek returns 0 when an error has occurred, Err returns the error.
  83. func (z *Lexer) Peek(pos int) byte {
  84. pos += z.pos
  85. return z.buf[pos]
  86. }
  87. // PeekRune returns the rune and rune length of the ith byte relative to the end position.
  88. func (z *Lexer) PeekRune(pos int) (rune, int) {
  89. // from unicode/utf8
  90. c := z.Peek(pos)
  91. if c < 0xC0 || z.Peek(pos+1) == 0 {
  92. return rune(c), 1
  93. } else if c < 0xE0 || z.Peek(pos+2) == 0 {
  94. return rune(c&0x1F)<<6 | rune(z.Peek(pos+1)&0x3F), 2
  95. } else if c < 0xF0 || z.Peek(pos+3) == 0 {
  96. return rune(c&0x0F)<<12 | rune(z.Peek(pos+1)&0x3F)<<6 | rune(z.Peek(pos+2)&0x3F), 3
  97. }
  98. return rune(c&0x07)<<18 | rune(z.Peek(pos+1)&0x3F)<<12 | rune(z.Peek(pos+2)&0x3F)<<6 | rune(z.Peek(pos+3)&0x3F), 4
  99. }
  100. // Move advances the position.
  101. func (z *Lexer) Move(n int) {
  102. z.pos += n
  103. }
  104. // Pos returns a mark to which can be rewinded.
  105. func (z *Lexer) Pos() int {
  106. return z.pos - z.start
  107. }
  108. // Rewind rewinds the position to the given position.
  109. func (z *Lexer) Rewind(pos int) {
  110. z.pos = z.start + pos
  111. }
  112. // Lexeme returns the bytes of the current selection.
  113. func (z *Lexer) Lexeme() []byte {
  114. return z.buf[z.start:z.pos]
  115. }
  116. // Skip collapses the position to the end of the selection.
  117. func (z *Lexer) Skip() {
  118. z.start = z.pos
  119. }
  120. // Shift returns the bytes of the current selection and collapses the position to the end of the selection.
  121. func (z *Lexer) Shift() []byte {
  122. b := z.buf[z.start:z.pos]
  123. z.start = z.pos
  124. return b
  125. }
  126. // Offset returns the character position in the buffer.
  127. func (z *Lexer) Offset() int {
  128. return z.pos
  129. }
  130. // Bytes returns the underlying buffer.
  131. func (z *Lexer) Bytes() []byte {
  132. return z.buf
  133. }