-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
199 lines (175 loc) · 4.36 KB
/
Copy pathstate.go
File metadata and controls
199 lines (175 loc) · 4.36 KB
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
package pars
import (
"bytes"
"errors"
"io"
)
const (
bufferReadSize = 4096
)
// State represents a parser state, which is a convenience wrapper for an
// io.Reader object with buffering and backtracking.
type State struct {
rd io.Reader
buf []byte
off int
end int
err error
pos Position
stk *stack
}
// NewState creates a new state from the given io.Reader.
func NewState(r io.Reader) *State {
switch rd := r.(type) {
case *State:
return rd
default:
return &State{
rd: r,
buf: make([]byte, 0),
off: 0,
end: -1,
err: nil,
pos: Position{0, 0},
stk: newStack(),
}
}
}
// FromBytes creates a new state from the given bytes.
func FromBytes(p []byte) *State {
return &State{
rd: &bytes.Buffer{},
buf: p,
off: 0,
end: -1,
err: nil,
pos: Position{0, 0},
stk: newStack(),
}
}
// FromString creates a new state from the given string.
func FromString(s string) *State {
return FromBytes([]byte(s))
}
// Read satisfies the io.Reader interface.
func (s *State) Read(p []byte) (int, error) {
err := s.Request(len(p))
n := copy(p, s.Buffer())
s.Advance()
return n, err
}
// ReadByte satisfies the io.ByteReader interace.
func (s *State) ReadByte() (byte, error) {
if err := s.Request(1); err != nil {
return 0, err
}
c := s.buf[s.off]
s.Advance()
return c, nil
}
// Request checks if the state contains at least the given number of bytes,
// additionally reading from the io.Reader object as necessary when the
// internal buffer is exhausted. If the call to Read for the io.Reader object
// returns an error, Request will return the corresponding error. A subsequent
// call to Advance will advance the state offset as far as possible.
func (s *State) Request(n int) error {
for len(s.buf) < s.off+n && s.err == nil {
p := make([]byte, bufferReadSize)
var m int
m, s.err = s.rd.Read(p)
s.buf = append(s.buf, p[:m]...)
}
switch {
case len(s.buf) < s.off+n:
s.end = len(s.buf)
return s.err
default:
s.end = s.off + n
return nil
}
}
// Advance the state by the amount given in a previous Request call.
func (s *State) Advance() {
if s.end < 0 {
panic("no previous call to Request")
}
for _, b := range s.buf[s.off:s.end] {
if b == '\n' {
s.pos.Line++
s.pos.Byte = 0
} else {
s.pos.Byte++
}
}
s.off, s.end = s.end, -1
s.autoclear()
}
// Buffer returns the range of bytes guaranteed by a Request call.
func (s State) Buffer() []byte { return s.buf[s.off:s.end] }
// Dump returns the entire remaining buffer content. Note that the returned
// byte slice will not always contain the entirety of the bytes that can be
// read by the io.Reader object.
func (s State) Dump() []byte { return s.buf[s.off:] }
// Offset returns the current state offset.
func (s State) Offset() int { return s.off }
// Position returns the current line and byte position of the state.
func (s State) Position() Position { return s.pos }
// Push the current state position for backtracking.
func (s *State) Push() { s.stk.Push(s.off, s.pos) }
// Pushed tests if the state has been pushed at least once.
func (s State) Pushed() bool { return !s.stk.Empty() }
// Pop will backtrack to the most recently pushed state.
func (s *State) Pop() {
if !s.stk.Empty() {
s.off, s.pos = s.stk.Pop()
s.autoclear()
}
}
// Drop will discard the most recently pushed state.
func (s *State) Drop() {
if !s.stk.Empty() {
s.stk.Pop()
s.autoclear()
}
}
func (s *State) autoclear() {
if s.stk.Empty() {
s.Clear()
}
}
// Clear will discard the buffer contents prior to the current state offset
// and drop all pushed states.
func (s *State) Clear() {
s.buf = s.buf[s.off:]
s.off = 0
s.stk.Reset()
}
// Skip the given state for the given number of bytes.
func Skip(state *State, n int) error {
if err := state.Request(n); err != nil {
return err
}
state.Advance()
return nil
}
// Next attempts to retrieve the next byte in the given state.
func Next(state *State) (byte, error) {
if err := state.Request(1); err != nil {
return 0, err
}
return state.Buffer()[0], nil
}
// Trail will return the extent of the state buffer spanning from the most
// recently pushed state position to the current state position.
func Trail(state *State) ([]byte, error) {
if !state.Pushed() {
return nil, errors.New("failed to backtrack")
}
off := state.Offset()
state.Pop()
n := off - state.Offset()
state.Request(n)
p := state.Buffer()
state.Advance()
return p, nil
}