-
Notifications
You must be signed in to change notification settings - Fork 10
/
source.go
328 lines (291 loc) · 7.04 KB
/
source.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package parse
import (
"bytes"
"errors"
"io"
"io/ioutil"
"unicode/utf8"
)
type stack struct {
buf []breakInfo
}
type breakInfo struct {
nextIdx int
}
func (s *stack) Push(info breakInfo) {
s.buf = append(s.buf, info)
}
func (s *stack) Pop() breakInfo {
last := len(s.buf) - 1
brkInfo := s.buf[last]
s.buf = s.buf[:last]
return brkInfo
}
func (s *stack) Empty() bool {
return len(s.buf) == 0
}
// Source is generalization of io.Reader and []byte.
// It supports read ahead.
// It supports read byte by byte.
// It supports read unicode code point by code point (as rune or []byte).
// It supports savepoint and rollback.
type Source struct {
err error
reader io.Reader
readBytes []byte
buf []byte
nextIdx int
savepointStack *stack
}
const (
_maxBufLen = 40
)
// NewSource construct a source from io.Reader.
// At least one byte should be read from the io.Reader, otherwise error will be returned.
func NewSource(reader io.Reader, bufLen int) (*Source, error) {
if bufLen > _maxBufLen {
bufLen = _maxBufLen
}
buf := make([]byte, bufLen)
n, err := reader.Read(buf)
if n == 0 {
return nil, err
}
readByes := make([]byte, n)
copy(readByes, buf)
return &Source{
reader: reader,
readBytes: readByes,
buf: buf,
savepointStack: new(stack),
}, nil
}
// NewSourceString construct a source from string. Len should >= 1.
func NewSourceString(str string) (*Source, error) {
if len(str) == 0 {
return nil, errors.New("source string is empty")
}
reader := bytes.NewReader([]byte(str))
return NewSource(reader, _maxBufLen)
}
// StoreSavepoint mark current position, and start recording.
// Later we can rollback to current position.
// Make sure there's no error, rollback will clear the error
func (src *Source) StoreSavepoint() {
src.savepointStack.Push(breakInfo{nextIdx: src.nextIdx})
}
var errNoSavepoint = errors.New("no savepoint in stack")
// DeleteSavepoint delete the lastest savepoint
func (src *Source) DeleteSavepoint() {
if src.savepointStack.Empty() {
src.ReportError(errNoSavepoint)
return
}
src.savepointStack.Pop()
}
// RollbackToSavepoint rollback the cursor to previous savepoint.
// The bytes read from reader will be replayed.
func (src *Source) RollbackToSavepoint() {
if src.savepointStack.Empty() {
src.ReportError(errNoSavepoint)
return
}
brkInfo := src.savepointStack.Pop()
src.nextIdx = brkInfo.nextIdx
src.err = nil
}
// Peek1 return the first byte in the buffer to parse.
func (src *Source) Peek1() byte {
if src.Error() != nil {
return 0x0
}
if src.nextIdx < len(src.readBytes) {
return src.readBytes[src.nextIdx]
}
src.consume()
if src.nextIdx < len(src.readBytes) {
return src.readBytes[src.nextIdx]
}
// EOF
src.ReportError(io.ErrUnexpectedEOF)
return 0x0 //NULL
}
// Peek peeks as many bytes as possible without triggering consume
func (src *Source) Peek() []byte {
return src.readBytes[src.nextIdx:]
}
// PeekAll peek all of the rest bytes
func (src *Source) PeekAll() []byte {
data, _ := ioutil.ReadAll(src.reader)
if len(data) > 0 {
src.readBytes = append(src.readBytes, data...)
}
return src.readBytes[src.nextIdx:]
}
// PeekN return the first N bytes in the buffer to parse.
// If N is longer than current buffer, it will read from reader.
// The cursor will not be moved.
func (src *Source) PeekN(n int) []byte {
rest := len(src.readBytes) - src.nextIdx
for src.Error() == nil && rest < n {
src.consume()
rest = len(src.readBytes) - src.nextIdx
}
if rest >= n {
return src.readBytes[src.nextIdx : src.nextIdx+n]
}
//EOF
src.ReportError(io.ErrUnexpectedEOF)
return src.readBytes[src.nextIdx:]
}
// ReadByte is the same as Read1, just for implementing the io.ByteReader interface
func (src *Source) ReadByte() (byte, error) {
return src.Read1(), src.Error()
}
// Read1 like ConsumeN, with N == 1
func (src *Source) Read1() byte {
if src.Error() != nil {
return 0x0
}
b := src.Peek1()
if src.Error() == nil {
src.nextIdx++
}
return b
}
// ReadN read N bytes and move cursor forward
func (src *Source) ReadN(n int) []byte {
if src.Error() != nil {
return nil
}
buf := src.PeekN(n)
src.nextIdx += len(buf)
return buf
}
// ReadAll read all bytes until EOF
func (src *Source) ReadAll() []byte {
buf := src.PeekAll()
src.nextIdx += len(buf)
return buf
}
var errExpectedBytesNotFound = errors.New(`expected bytes not found`)
// Expect1 like Read1
// bytes will not be consumed if not match
func (src *Source) Expect1(b1 byte) bool {
if src.Peek1() == b1 {
src.nextIdx++
return true
}
return false
}
// Expect2 like ReadN, with N == 2.
// bytes will not be consumed if not match
func (src *Source) Expect2(b1, b2 byte) bool {
buf := src.PeekN(2)
if len(buf) < 2 {
return false
}
if buf[0] == b1 && buf[1] == b2 {
src.nextIdx += 2
return true
}
return false
}
// Expect3 like ReadN, with N == 3.
// bytes will not be consumed if not match
func (src *Source) Expect3(b1, b2, b3 byte) bool {
buf := src.PeekN(3)
if len(buf) < 3 {
return false
}
if buf[0] == b1 && buf[1] == b2 && buf[2] == b3 {
src.nextIdx += 3
return true
}
return false
}
// Expect4 like ReadN, with N == 4.
// bytes will not be consumed if not match
func (src *Source) Expect4(b1, b2, b3, b4 byte) bool {
buf := src.PeekN(4)
if len(buf) < 4 {
return false
}
if buf[0] == b1 && buf[1] == b2 && buf[2] == b3 && buf[3] == b4 {
src.nextIdx += 4
return true
}
return false
}
// ExpectN like ReadN.
// bytes will not be consumed if not match
func (src *Source) Expect(expect []byte) bool {
buf := src.PeekN(len(expect))
if len(buf) < len(expect) {
return false
}
for i, e := range expect {
if buf[i] != e {
return false
}
}
src.nextIdx += len(expect)
return true
}
// consume will fill the readBytes with more bytes from reader
func (src *Source) consume() {
if src.reader == nil {
src.ReportError(io.EOF)
return
}
n, err := src.reader.Read(src.buf)
if err != nil || n == 0 {
src.ReportError(err)
return
}
src.readBytes = append(src.readBytes, src.buf[:n]...)
}
// PeekRune read unicode code point as rune, without moving cursor.
func (src *Source) PeekRune() (rune, int) {
p0 := src.Peek1()
x := first[p0]
if x >= as {
return utf8.DecodeRune(src.readBytes[src.nextIdx:])
}
sz := x & 7
fullBuf := src.PeekN(int(sz))
return utf8.DecodeRune(fullBuf)
}
// PeekUtf8 read one full code point without decoding into rune
func (src *Source) PeekUtf8() []byte {
p0 := src.Peek1()
x := first[p0]
if x >= as {
return []byte{p0}
}
sz := int(x & 7)
fullBuf := src.PeekN(sz)
return fullBuf
}
// ReportError set the source in error condition.
func (src *Source) ReportError(err error) {
if src.err == nil || src.err == io.EOF {
src.err = err
}
}
// Error tells if the source is in error condition.
// EOF is considered as error.
func (src *Source) Error() error {
return src.err
}
// FatalError tells if the source is in fatal error condition
func (src *Source) FatalError() error {
if src.err == io.EOF {
return nil
}
return src.err
}
// ResetError clear the error
func (src *Source) ResetError() {
src.err = nil
}