From 2ca426a8d1fd678add118322727e17f0850bcc62 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Wed, 27 Sep 2017 21:52:25 +1000 Subject: [PATCH] Make TypeMappingLexer a bit less prone to error. --- lexers/emacs.go | 12 ++++++------ remap.go | 18 ++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lexers/emacs.go b/lexers/emacs.go index aaf4d86..e191d8a 100644 --- a/lexers/emacs.go +++ b/lexers/emacs.go @@ -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}, })) diff --git a/remap.go b/remap.go index 9b5d009..15237b6 100644 --- a/remap.go +++ b/remap.go @@ -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 } }