Repository navigation
Expand file tree
/
Copy pathtokeniser.go
More file actions
391 lines (313 loc) · 7.89 KB
/
Copy pathtokeniser.go
File metadata and controls
391 lines (313 loc) · 7.89 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package parser
import (
"errors"
"io"
"slices"
"strings"
"unicode"
"unicode/utf8"
)
// TokenType represents the type of token being read.
//
// Negative values are reserved for this package.
type TokenType int
// Constants TokenError (-2) and TokenDone (-1).
const (
TokenDone TokenType = -1 - iota
TokenError
)
// Token represents data parsed from the stream.
type Token struct {
Type TokenType
Data string
}
// TokenFunc is the type that the worker funcs implement in order to be used by
// the tokeniser.
type TokenFunc func(*Tokeniser) (Token, TokenFunc)
// State represents a position in the byte stream of the Tokeniser.
type State interface {
// Reset returns the byte stream to the position it was in when this
// object was created.
//
// Only valid until Tokeniser.Get is called.
Reset() bool
}
type tokeniser interface {
backup()
get() string
length() int
next() rune
reset()
state() State
sub() tokeniser
slice(int, int) (string, int)
}
// Tokeniser is a state machine to generate tokens from an input.
type Tokeniser struct {
tokeniser
Err error
state TokenFunc
}
// GetToken runs the state machine and retrieves a single token and possible an
// error.
func (t *Tokeniser) GetToken() (Token, error) {
tk := t.get()
if tk.Type == TokenError {
return tk, t.Err
}
return tk, nil
}
// Iter yields each token as it's returned, stopping after yielding a TokenDone
// or TokenError Token.
func (t *Tokeniser) Iter(yield func(Token) bool) {
for {
if tk := t.get(); !yield(tk) || tk.Type == TokenDone || tk.Type == TokenError {
break
}
}
}
// GetError returns any error that has been generated by the Tokeniser.
func (t *Tokeniser) GetError() error {
return t.Err
}
// TokeniserState allows the internal state of the Tokeniser to be set.
func (t *Tokeniser) TokeniserState(tf TokenFunc) {
t.state = tf
}
func (t *Tokeniser) get() Token {
if errors.Is(t.Err, io.EOF) {
return Token{
Type: TokenDone,
Data: "",
}
}
if t.state == nil {
t.Err = ErrNoState
t.state = (*Tokeniser).Error
}
var tk Token
tk, t.state = t.state(t)
if tk.Type == TokenError && errors.Is(t.Err, io.EOF) {
t.Err = io.ErrUnexpectedEOF
}
return tk
}
// Accept returns true if the next character to be read is contained within the
// given string.
//
// Upon true, it advances the read position, otherwise the position remains the
// same.
func (t *Tokeniser) Accept(chars string) bool {
if !strings.ContainsRune(chars, t.next()) {
t.backup()
return false
}
return true
}
// AcceptRune returns true if the next character to be read is the rune
// specified
//
// Upon true, it advances the read position, otherwise the position remains the
// same.
func (t *Tokeniser) AcceptRune(r rune) bool {
if t.next() == r {
return true
}
t.backup()
return false
}
// Next returns the next rune and advances the read position.
func (t *Tokeniser) Next() rune {
return t.next()
}
// Peek returns the next rune without advancing the read position.
func (t *Tokeniser) Peek() rune {
r := t.next()
t.backup()
return r
}
// Get returns a string of everything that has been read so far and resets
// the string for the next round of parsing.
func (t *Tokeniser) Get() string {
return t.tokeniser.get()
}
// Len returns the number of bytes that has been read since the last Get.
func (t *Tokeniser) Len() int {
return t.length()
}
// AcceptRun reads from the string as long as the read character is in the
// given string.
//
// Returns the rune that stopped the run.
func (t *Tokeniser) AcceptRun(chars string) rune {
for {
if c := t.next(); !strings.ContainsRune(chars, c) {
t.backup()
return c
}
}
}
// AcceptString attempts to accept each character from the given string, in
// order, returning the number of characters accepted before a failure.
func (t *Tokeniser) AcceptString(str string, caseInsensitive bool) int {
for n, r := range str {
if p := t.Peek(); p < 0 || !runeComparison(p, r, caseInsensitive) {
return n
}
t.Next()
}
return len(str)
}
func runeComparison(a, b rune, caseInsensitive bool) bool {
if caseInsensitive {
al := unicode.SimpleFold(a)
bl := unicode.SimpleFold(b)
return a == b || a == bl || bl == a || al == bl
}
return a == b
}
// AcceptWord attempts to parse one of the words (string of characters)
// provided in the slice.
//
// Returns the longest word parsed, or empty string if no words matched.
func (t *Tokeniser) AcceptWord(words []string, caseInsensitive bool) string {
words = slices.Clone(words)
return t.acceptWord(words, caseInsensitive)
}
func (t *Tokeniser) acceptWord(words []string, caseInsensitive bool) string {
s := t.State()
var sb strings.Builder
for len(words) > 0 {
char := t.Next()
sb.WriteRune(char)
if char < 0 {
break
}
var found bool
newWords := words[:0]
for _, word := range words {
if len(word) > 0 {
r, s := utf8.DecodeRuneInString(word)
if r == utf8.RuneError && s == 1 {
r = rune(word[0])
}
if runeComparison(char, r, caseInsensitive) {
word = word[s:]
found = found || word == ""
newWords = append(newWords, word)
}
}
}
words = newWords
if found {
if len(words) > 0 {
sb.WriteString(t.acceptWord(words, caseInsensitive))
}
return sb.String()
}
}
s.Reset()
return ""
}
// Except returns true if the next character to be read is not contained within
// the given string.
// Upon true, it advances the read position, otherwise the position remains the
// same.
func (t *Tokeniser) Except(chars string) bool {
if r := t.next(); r == -1 || strings.ContainsRune(chars, r) {
t.backup()
return false
}
return true
}
// Reset restores the state to after the last Get() call (or init, it Get() has
// not been called).
func (t *Tokeniser) Reset() {
t.reset()
}
// Retrieve the current Tokeniser state that allows you to reset to that point.
// State is only valid until next 'Get' call.
func (t *Tokeniser) State() State {
return t.tokeniser.state()
}
// SubTokeniser create a new Tokeniser that uses this existing tokeniser as its
// source.
//
// This allows the sub-tokenisers Get method to be called without calling it on
// its parent.
func (t *Tokeniser) SubTokeniser() *Tokeniser {
return &Tokeniser{
tokeniser: t.tokeniser.sub(),
}
}
// ExceptRun reads from the string as long as the read character is not in the
// given string.
//
// Returns the rune that stopped the run.
func (t *Tokeniser) ExceptRun(chars string) rune {
for {
if r := t.next(); r == -1 || strings.ContainsRune(chars, r) {
t.backup()
return r
}
}
}
// Return simplifies the returning from TokenFns, taking a TokenType and a next
// TokenFn, default to Done.
//
// The returned token is of the type specified with the data set to the output
// of t.Get().
func (t *Tokeniser) Return(typ TokenType, fn TokenFunc) (Token, TokenFunc) {
if fn == nil {
fn = (*Tokeniser).Done
}
return Token{
Type: typ,
Data: t.Get(),
}, fn
}
// ReturnError simplifies the handling of errors, setting the error and calling
// Tokeniser.Error().
func (t *Tokeniser) ReturnError(err error) (Token, TokenFunc) {
t.Err = err
return t.Error()
}
// Done is a TokenFunc that is used to indicate that there are no more tokens to
// parse.
func (t *Tokeniser) Done() (Token, TokenFunc) {
t.Err = io.EOF
return Token{
Type: TokenDone,
Data: "",
}, (*Tokeniser).Done
}
// Error represents an error state for the parser.
//
// The error value should be set in Tokeniser.Err and then this func should be
// called.
func (t *Tokeniser) Error() (Token, TokenFunc) {
if t.Err == nil {
t.Err = ErrUnknownError
}
return Token{
Type: TokenError,
Data: t.Err.Error(),
}, (*Tokeniser).Error
}
type sub struct {
tokeniser
tState, start int
}
func (s *sub) get() string {
if s.start < 0 {
return ""
}
var str string
str, s.start = s.slice(s.tState, s.start)
return str
}
// Errors.
var (
ErrNoState = errors.New("no state")
ErrUnknownError = errors.New("unknown error")
)