forked from alecthomas/chroma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
43 lines (39 loc) · 975 Bytes
/
iterator.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
package chroma
// An Iterator across tokens.
//
// nil will be returned at the end of the Token stream.
//
// If an error occurs within an Iterator, it may propagate this in a panic. Formatters should recover.
type Iterator func() *Token
// Tokens consumes all tokens from the iterator and returns them as a slice.
func (i Iterator) Tokens() []*Token {
out := []*Token{}
for t := i(); t != nil; t = i() {
out = append(out, t)
}
return out
}
// Concaterator concatenates tokens from a series of iterators.
func Concaterator(iterators ...Iterator) Iterator {
return func() *Token {
for len(iterators) > 0 {
t := iterators[0]()
if t != nil {
return t
}
iterators = iterators[1:]
}
return nil
}
}
// Literator converts a sequence of literal Tokens into an Iterator.
func Literator(tokens ...*Token) Iterator {
return func() (out *Token) {
if len(tokens) == 0 {
return nil
}
token := tokens[0]
tokens = tokens[1:]
return token
}
}