1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-11-27 22:38:42 +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)}, {`"`, LiteralString, Pop(1)},
}, },
}, },
), TypeRemappingMap{ ), TypeMapping{
{NameVariable, NameFunction}: emacsBuiltinFunction, {NameVariable, NameFunction, emacsBuiltinFunction},
{NameVariable, NameKeyword}: emacsSpecialForms, {NameVariable, NameBuiltin, emacsSpecialForms},
{NameVariable, NameException}: emacsErrorKeywords, {NameVariable, NameException, emacsErrorKeywords},
{NameVariable, NameBuiltin}: append(emacsBuiltinFunctionHighlighted, emacsMacros...), {NameVariable, NameBuiltin, append(emacsBuiltinFunctionHighlighted, emacsMacros...)},
{NameVariable, KeywordPseudo}: emacsLambdaListKeywords, {NameVariable, KeywordPseudo, emacsLambdaListKeywords},
})) }))

View File

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