| 1 |
package parse |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
) |
| 6 |
|
| 7 |
var nullBuffer = []byte{0} |
| 8 |
|
| 9 |
// Input is a buffered reader that allows peeking forward and shifting, taking an io.Input. |
| 10 |
// It keeps data in-memory until Free, taking a byte length, is called to move beyond the data. |
| 11 |
type Input struct { |
| 12 |
buf []byte |
| 13 |
pos int // index in buf |
| 14 |
start int // index in buf |
| 15 |
err error |
| 16 |
|
| 17 |
restore func() |
| 18 |
} |
| 19 |
|
| 20 |
// NewInput returns a new Input for a given io.Input and uses io.ReadAll to read it into a byte slice. |
| 21 |
// If the io.Input implements Bytes, that is used instead. It will append a NULL at the end of the buffer. |
| 22 |
func NewInput(r io.Reader) *Input { |
| 23 |
var b []byte |
| 24 |
if r != nil { |
| 25 |
if buffer, ok := r.(interface { |
| 26 |
Bytes() []byte |
| 27 |
}); ok { |
| 28 |
b = buffer.Bytes() |
| 29 |
} else { |
| 30 |
var err error |
| 31 |
b, err = io.ReadAll(r) |
| 32 |
if err != nil { |
| 33 |
return &Input{ |
| 34 |
buf: nullBuffer, |
| 35 |
err: err, |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
return NewInputBytes(b) |
| 41 |
} |
| 42 |
|
| 43 |
// NewInputString returns a new Input for a given string and appends NULL at the end. |
| 44 |
func NewInputString(s string) *Input { |
| 45 |
return NewInputBytes([]byte(s)) |
| 46 |
} |
| 47 |
|
| 48 |
// NewInputBytes returns a new Input for a given byte slice and appends NULL at the end. |
| 49 |
// To avoid reallocation, make sure the capacity has room for one more byte. |
| 50 |
func NewInputBytes(b []byte) *Input { |
| 51 |
z := &Input{ |
| 52 |
buf: b, |
| 53 |
} |
| 54 |
|
| 55 |
n := len(b) |
| 56 |
if n == 0 { |
| 57 |
z.buf = nullBuffer |
| 58 |
} else { |
| 59 |
// Append NULL to buffer, but try to avoid reallocation |
| 60 |
if cap(b) > n { |
| 61 |
// Overwrite next byte but restore when done |
| 62 |
b = b[:n+1] |
| 63 |
c := b[n] |
| 64 |
b[n] = 0 |
| 65 |
|
| 66 |
z.buf = b |
| 67 |
z.restore = func() { |
| 68 |
b[n] = c |
| 69 |
} |
| 70 |
} else { |
| 71 |
z.buf = append(b, 0) |
| 72 |
} |
| 73 |
} |
| 74 |
return z |
| 75 |
} |
| 76 |
|
| 77 |
// Restore restores the replaced byte past the end of the buffer by NULL. |
| 78 |
func (z *Input) Restore() { |
| 79 |
if z.restore != nil { |
| 80 |
z.restore() |
| 81 |
z.restore = nil |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// Err returns the error returned from io.Input or io.EOF when the end has been reached. |
| 86 |
func (z *Input) Err() error { |
| 87 |
return z.PeekErr(0) |
| 88 |
} |
| 89 |
|
| 90 |
// PeekErr returns the error at position pos. When pos is zero, this is the same as calling Err(). |
| 91 |
func (z *Input) PeekErr(pos int) error { |
| 92 |
if z.err != nil { |
| 93 |
return z.err |
| 94 |
} else if len(z.buf)-1 <= z.pos+pos { |
| 95 |
return io.EOF |
| 96 |
} |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
// Peek returns the ith byte relative to the end position. |
| 101 |
// Peek returns 0 when an error has occurred, Err returns the erroz. |
| 102 |
func (z *Input) Peek(pos int) byte { |
| 103 |
pos += z.pos |
| 104 |
return z.buf[pos] |
| 105 |
} |
| 106 |
|
| 107 |
// PeekRune returns the rune and rune length of the ith byte relative to the end position. |
| 108 |
func (z *Input) PeekRune(pos int) (rune, int) { |
| 109 |
// from unicode/utf8 |
| 110 |
c := z.Peek(pos) |
| 111 |
if c < 0xC0 || len(z.buf)-1-z.pos < 2 { |
| 112 |
return rune(c), 1 |
| 113 |
} else if c < 0xE0 || len(z.buf)-1-z.pos < 3 { |
| 114 |
return rune(c&0x1F)<<6 | rune(z.Peek(pos+1)&0x3F), 2 |
| 115 |
} else if c < 0xF0 || len(z.buf)-1-z.pos < 4 { |
| 116 |
return rune(c&0x0F)<<12 | rune(z.Peek(pos+1)&0x3F)<<6 | rune(z.Peek(pos+2)&0x3F), 3 |
| 117 |
} |
| 118 |
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 |
| 119 |
} |
| 120 |
|
| 121 |
// Move advances the position. |
| 122 |
func (z *Input) Move(n int) { |
| 123 |
z.pos += n |
| 124 |
} |
| 125 |
|
| 126 |
// MoveRune advances the position by the length of the current rune. |
| 127 |
func (z *Input) MoveRune() { |
| 128 |
c := z.Peek(0) |
| 129 |
if c < 0xC0 || len(z.buf)-1-z.pos < 2 { |
| 130 |
z.pos++ |
| 131 |
} else if c < 0xE0 || len(z.buf)-1-z.pos < 3 { |
| 132 |
z.pos += 2 |
| 133 |
} else if c < 0xF0 || len(z.buf)-1-z.pos < 4 { |
| 134 |
z.pos += 3 |
| 135 |
} else { |
| 136 |
z.pos += 4 |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Pos returns a mark to which can be rewinded. |
| 141 |
func (z *Input) Pos() int { |
| 142 |
return z.pos - z.start |
| 143 |
} |
| 144 |
|
| 145 |
// Rewind rewinds the position to the given position. |
| 146 |
func (z *Input) Rewind(pos int) { |
| 147 |
z.pos = z.start + pos |
| 148 |
} |
| 149 |
|
| 150 |
// Lexeme returns the bytes of the current selection. |
| 151 |
func (z *Input) Lexeme() []byte { |
| 152 |
return z.buf[z.start:z.pos:z.pos] |
| 153 |
} |
| 154 |
|
| 155 |
// Skip collapses the position to the end of the selection. |
| 156 |
func (z *Input) Skip() { |
| 157 |
z.start = z.pos |
| 158 |
} |
| 159 |
|
| 160 |
// Shift returns the bytes of the current selection and collapses the position to the end of the selection. |
| 161 |
func (z *Input) Shift() []byte { |
| 162 |
b := z.buf[z.start:z.pos:z.pos] |
| 163 |
z.start = z.pos |
| 164 |
return b |
| 165 |
} |
| 166 |
|
| 167 |
// Offset returns the character position in the buffez. |
| 168 |
func (z *Input) Offset() int { |
| 169 |
return z.pos |
| 170 |
} |
| 171 |
|
| 172 |
// Bytes returns the underlying buffez. |
| 173 |
func (z *Input) Bytes() []byte { |
| 174 |
return z.buf[: len(z.buf)-1 : len(z.buf)-1] |
| 175 |
} |
| 176 |
|
| 177 |
// Len returns the length of the underlying buffez. |
| 178 |
func (z *Input) Len() int { |
| 179 |
return len(z.buf) - 1 |
| 180 |
} |
| 181 |
|
| 182 |
// Reset resets position to the underlying buffez. |
| 183 |
func (z *Input) Reset() { |
| 184 |
z.start = 0 |
| 185 |
z.pos = 0 |
| 186 |
} |