1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-07-15 01:14:21 +02:00

Add RemappingLexer.

This pattern is used heavily in Pygments for certain lexers,
particularly Lisp variants. See #43.
This commit is contained in:
Alec Thomas
2017-09-27 11:28:51 +10:00
parent c99eebc024
commit 102aa61ac3
2 changed files with 105 additions and 0 deletions

29
remap_test.go Normal file
View File

@ -0,0 +1,29 @@
package chroma
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRemappingLexer(t *testing.T) {
var lexer Lexer = MustNewLexer(nil, Rules{
"root": {
{`\s+`, Whitespace, nil},
{`\w+`, Name, nil},
},
})
lexer = TypeRemappingLexer(lexer, TypeRemappingMap{
{Name, Keyword}: {"if", "else"},
})
it, err := lexer.Tokenise(nil, `if true then print else end`)
assert.NoError(t, err)
expected := []*Token{
{Keyword, "if"}, {TextWhitespace, " "}, {Name, "true"}, {TextWhitespace, " "}, {Name, "then"},
{TextWhitespace, " "}, {Name, "print"}, {TextWhitespace, " "}, {Keyword, "else"},
{TextWhitespace, " "}, {Name, "end"},
}
actual := it.Tokens()
assert.Equal(t, expected, actual)
}