-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenbuf.go
More file actions
34 lines (29 loc) · 770 Bytes
/
tokenbuf.go
File metadata and controls
34 lines (29 loc) · 770 Bytes
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
package gmars
import "fmt"
// butTokenReader implements the same interface as a streaming parser to let
// us cache and reuse the token stream instead of making multiple passes with
// the lexer
type bufTokenReader struct {
tokens []token
i int
}
func newBufTokenReader(tokens []token) *bufTokenReader {
return &bufTokenReader{tokens: tokens}
}
func (r *bufTokenReader) NextToken() (token, error) {
if r.i >= len(r.tokens) {
return token{}, fmt.Errorf("no more tokens")
}
next := r.tokens[r.i]
r.i++
return next, nil
}
func (r *bufTokenReader) Tokens() ([]token, error) {
if r.i >= len(r.tokens) {
return nil, fmt.Errorf("no more tokens")
}
subslice := r.tokens[r.i:]
ret := make([]token, len(subslice))
copy(ret, subslice)
return ret, nil
}