-
-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathcoalesce.go
More file actions
44 lines (39 loc) · 927 Bytes
/
Copy pathcoalesce.go
File metadata and controls
44 lines (39 loc) · 927 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
35
36
37
38
39
40
41
42
43
44
package chroma
import "iter"
// Coalesce is a Lexer interceptor that collapses runs of common types into a single token.
func Coalesce(lexer Lexer) Lexer { return &coalescer{lexer} }
type coalescer struct{ Lexer }
func (d *coalescer) SetTracing(enable bool) {
if l, ok := d.Lexer.(TracingLexer); ok {
l.SetTracing(enable)
}
}
func (d *coalescer) Tokenise(options *TokeniseOptions, text string) (iter.Seq[Token], error) {
it, err := d.Lexer.Tokenise(options, text)
if err != nil {
return nil, err
}
return func(yield func(Token) bool) {
var prev *Token
for token := range it {
if len(token.Value) == 0 {
continue
}
if prev == nil {
t := token
prev = &t
} else if prev.Type == token.Type && len(prev.Value) < 8192 {
prev.Value += token.Value
} else {
if !yield(*prev) {
return
}
t := token
prev = &t
}
}
if prev != nil {
yield(*prev)
}
}, nil
}