2017-06-02 00:17:21 +10:00
|
|
|
package chroma
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-06-05 10:29:50 +10:00
|
|
|
func (d *coalescer) Tokenise(options *TokeniseOptions, text string, out func(*Token)) error {
|
2017-07-19 23:51:16 -07:00
|
|
|
var prev *Token
|
2017-06-05 10:29:50 +10:00
|
|
|
return d.Lexer.Tokenise(options, text, func(token *Token) {
|
2017-07-19 23:51:16 -07:00
|
|
|
if prev == nil {
|
|
|
|
prev = token
|
2017-06-02 00:17:21 +10:00
|
|
|
} else {
|
2017-07-19 23:51:16 -07:00
|
|
|
if prev.Type == token.Type && len(prev.Value) < 8192 {
|
|
|
|
prev.Value += token.Value
|
2017-06-02 15:15:15 +10:00
|
|
|
} else {
|
2017-07-19 23:51:16 -07:00
|
|
|
out(prev)
|
|
|
|
prev = token
|
2017-06-02 15:15:15 +10:00
|
|
|
}
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|
2017-07-19 23:51:16 -07:00
|
|
|
if token.Type == EOF {
|
|
|
|
out(token)
|
|
|
|
}
|
2017-06-02 15:15:15 +10:00
|
|
|
})
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|