Repository navigation
Expand file tree
/
Copy pathphrase.go
More file actions
290 lines (231 loc) · 5.67 KB
/
Copy pathphrase.go
File metadata and controls
290 lines (231 loc) · 5.67 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
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
package parser
import (
"errors"
"io"
"slices"
)
// PhraseType represents the type of phrase being read.
//
// Negative values are reserved for this package.
type PhraseType int
// Constants PhraseError (-2) and PhraseDone (-1).
const (
PhraseDone PhraseType = -1 - iota
PhraseError
)
// PhraseFunc is the type that the worker types implement in order to be used
// by the Phraser.
type PhraseFunc func(*Parser) (Phrase, PhraseFunc)
// Phrase represents a collection of tokens that have meaning together.
type Phrase struct {
Type PhraseType
Data []Token
}
// Parser is a type used to get tokens or phrases (collection of token) from an
// input.
type Parser struct {
Tokeniser
state PhraseFunc
tokens []Token
peekedToken bool
}
// GetPhrase runs the state machine and retrieves a single Phrase and possibly
// an error.
func (p *Parser) GetPhrase() (Phrase, error) {
if errors.Is(p.Err, io.EOF) {
return Phrase{
Type: PhraseDone,
Data: make([]Token, 0),
}, io.EOF
}
if p.state == nil {
p.Err = ErrNoState
p.state = (*Parser).Error
}
var ph Phrase
ph, p.state = p.state(p)
if ph.Type == PhraseError {
if errors.Is(p.Err, io.EOF) {
p.Err = io.ErrUnexpectedEOF
}
return ph, p.Err
}
return ph, nil
}
// GetToken runs the state machine and retrieves a single Token and possibly
// an error.
//
// If a Token has already been 'peek'ed, that token will be returned without
// running the state machine.
func (p *Parser) GetToken() (Token, error) {
tk := p.get()
if tk.Type == TokenError {
return tk, p.Err
}
return tk, nil
}
// Iter yields each Phrase as it's returned, stopping after yielding a
// PhraseDone or PhraseError Phrase.
func (p *Parser) Iter(yield func(Phrase) bool) {
for {
if ph, _ := p.GetPhrase(); !yield(ph) || ph.Type == PhraseDone || ph.Type == PhraseError {
break
}
}
}
// PhraserState allows the internal state of the Phraser to be set.
func (p *Parser) PhraserState(pf PhraseFunc) {
p.state = pf
}
func (p *Parser) get() Token {
if p.peekedToken {
p.peekedToken = false
return p.tokens[len(p.tokens)-1]
} else if len(p.tokens) > 0 && p.tokens[len(p.tokens)-1].Type < 0 {
return p.tokens[len(p.tokens)-1]
}
tk := p.Tokeniser.get()
p.tokens = append(p.tokens, tk)
return tk
}
func (p *Parser) backup() {
p.peekedToken = true
}
// Accept will accept a token with one of the given types, returning true if
// one is read and false otherwise.
func (p *Parser) Accept(types ...TokenType) bool {
tk := p.get()
if slices.Contains(types, tk.Type) {
return true
}
p.backup()
return false
}
// Next returns the next Token and advances the read position.
func (p *Parser) Next() Token {
return p.get()
}
// Peek takes a look at the upcoming Token and returns it.
func (p *Parser) Peek() Token {
tk := p.get()
p.backup()
return tk
}
// Get retrieves a slice of the Tokens that have been read so far.
func (p *Parser) Get() []Token {
var toRet []Token
if p.peekedToken {
tk := p.tokens[len(p.tokens)-1]
toRet = slices.Clone(p.tokens[:len(p.tokens)-1])
p.tokens[0] = tk
p.tokens = p.tokens[:1]
} else {
toRet = slices.Clone(p.tokens)
p.tokens = p.tokens[:0]
}
return toRet
}
// Len returns how many tokens have been read.
func (p *Parser) Len() int {
l := len(p.tokens)
if p.peekedToken {
l--
}
return l
}
// AcceptRun will keep Accepting tokens as long as they match one of the
// given types.
//
// It will return the type of the token that made it stop.
func (p *Parser) AcceptRun(types ...TokenType) TokenType {
Loop:
for {
tk := p.get()
for _, t := range types {
if tk.Type == t {
continue Loop
}
}
p.backup()
return tk.Type
}
}
// AcceptToken will accept a token matching one of the ones provided exactly,
// returning true if one is read and false otherwise.
func (p *Parser) AcceptToken(tokens ...Token) bool {
tk := p.get()
if slices.Contains(tokens, tk) {
return true
}
p.backup()
return false
}
// Except will Accept a token that is not one of the types given. Returns true
// if it Accepted a token.
func (p *Parser) Except(types ...TokenType) bool {
tk := p.get()
if slices.Contains(types, tk.Type) {
p.backup()
return false
}
return true
}
// ExceptRun will keep Accepting tokens as long as they do not match one of the
// given types.
//
// It will return the type of the token that made it stop.
func (p *Parser) ExceptRun(types ...TokenType) TokenType {
for {
tk := p.get()
for _, t := range types {
if tk.Type == t || tk.Type < 0 {
p.backup()
return tk.Type
}
}
}
}
// Return simplifies the returning from PhraseFns, taking a PhraseType and a
// next TokenFn, default to Done.
//
// The returned phrase is of the type specified with the data set to the output
// of p.Get().
func (p *Parser) Return(typ PhraseType, fn PhraseFunc) (Phrase, PhraseFunc) {
if fn == nil {
fn = (*Parser).Done
}
return Phrase{
Type: typ,
Data: p.Get(),
}, fn
}
// ReturnError simplifies the handling of errors, setting the error and calling
// Phraser.Error().
func (p *Parser) ReturnError(err error) (Phrase, PhraseFunc) {
p.Err = err
return p.Error()
}
// Done is a PhraseFunc that is used to indicate that there are no more phrases
// to parse.
func (p *Parser) Done() (Phrase, PhraseFunc) {
p.Err = io.EOF
return Phrase{
Type: PhraseDone,
Data: make([]Token, 0),
}, (*Parser).Done
}
// Error represents an error state for the phraser.
//
// The error value should be set in Parser.Err and then this func should be
// called.
func (p *Parser) Error() (Phrase, PhraseFunc) {
if p.Err == nil {
p.Err = ErrUnknownError
}
return Phrase{
Type: PhraseError,
Data: []Token{
{Type: TokenError, Data: p.Err.Error()},
},
}, (*Parser).Error
}