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:
29
remap_test.go
Normal file
29
remap_test.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user