Debian Codegraph @

buffer/lexer.go

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