1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

Make TypeMappingLexer a bit less prone to error.

This commit is contained in:
Alec Thomas 2017-09-27 21:52:25 +10:00
parent cbc3d5b9f0
commit 2ca426a8d1
2 changed files with 14 additions and 16 deletions

View File

@ -572,10 +572,10 @@ var Emacslisp = Register(TypeRemappingLexer(MustNewLexer(
{`"`, LiteralString, Pop(1)},
},
},
), TypeRemappingMap{
{NameVariable, NameFunction}: emacsBuiltinFunction,
{NameVariable, NameKeyword}: emacsSpecialForms,
{NameVariable, NameException}: emacsErrorKeywords,
{NameVariable, NameBuiltin}: append(emacsBuiltinFunctionHighlighted, emacsMacros...),
{NameVariable, KeywordPseudo}: emacsLambdaListKeywords,
), TypeMapping{
{NameVariable, NameFunction, emacsBuiltinFunction},
{NameVariable, NameBuiltin, emacsSpecialForms},
{NameVariable, NameException, emacsErrorKeywords},
{NameVariable, NameBuiltin, append(emacsBuiltinFunctionHighlighted, emacsMacros...)},
{NameVariable, KeywordPseudo, emacsLambdaListKeywords},
}))

View File

@ -36,30 +36,29 @@ func (r *remappingLexer) Tokenise(options *TokeniseOptions, text string) (Iterat
}, nil
}
type TypeMapping struct {
From TokenType
To TokenType
type TypeMapping []struct {
From, To TokenType
Words []string
}
type TypeRemappingMap map[TypeMapping][]string
// TypeRemappingLexer remaps types of tokens coming from a parent Lexer.
//
// eg. Map "defvaralias" tokens of type NameVariable to NameFunction:
//
// mapping := TypeRemappingMap{
// {NameVariable, NameFunction}: {"defvaralias"},
// mapping := TypeMapping{
// {NameVariable, NameFunction, []string{"defvaralias"},
// }
// lexer = TypeRemappingLexer(lexer, mapping)
func TypeRemappingLexer(lexer Lexer, mapping TypeRemappingMap) Lexer {
func TypeRemappingLexer(lexer Lexer, mapping TypeMapping) Lexer {
// Lookup table for fast remapping.
lut := map[TokenType]map[string]TokenType{}
for rt, kl := range mapping {
for _, rt := range mapping {
km, ok := lut[rt.From]
if !ok {
km = map[string]TokenType{}
lut[rt.From] = km
}
for _, k := range kl {
for _, k := range rt.Words {
km[k] = rt.To
}
@ -67,7 +66,6 @@ func TypeRemappingLexer(lexer Lexer, mapping TypeRemappingMap) Lexer {
return RemappingLexer(lexer, func(t *Token) []*Token {
if k, ok := lut[t.Type]; ok {
if tt, ok := k[t.Value]; ok {
t = t.Clone()
t.Type = tt
}
}