1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-11-23 22:24:39 +02:00

Initial commit! Working!

This commit is contained in:
Alec Thomas
2017-06-02 00:17:21 +10:00
parent 3de978543f
commit b2fb8edf77
16 changed files with 962 additions and 0 deletions

31
coalesce.go Normal file
View File

@@ -0,0 +1,31 @@
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
}
func (d *coalescer) Tokenise(text string) ([]Token, error) {
in, err := d.Lexer.Tokenise(text)
if err != nil {
return in, err
}
out := []Token{}
for _, token := range in {
if len(out) == 0 {
out = append(out, token)
continue
}
last := &out[len(out)-1]
if last.Type == token.Type {
last.Value += token.Value
} else {
out = append(out, token)
}
}
return out, err
}