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

Improve Elixir lexer based on Pygments

It appears that not all the rules from the Pygments Elixir lexer were
correctly ported over to Chroma. It was missing mostly the keywords and
declarations that are processed in the `get_tokens_unprocessed` function of the
Pygments Elixir lexer.

This commit adds the missing lines to the Elixir lexer and updates the
tests accordingly.
This commit is contained in:
Tiemen Waterreus 2020-09-03 10:40:47 +02:00 committed by Alec Thomas
parent 6d6e9cd020
commit 2365845022
3 changed files with 85 additions and 0 deletions

View File

@ -28,6 +28,13 @@ var Elixir = internal.Register(MustNewLexer(
{`:"`, LiteralStringSymbol, Push("string_double_atom")},
{`:'`, LiteralStringSymbol, Push("string_single_atom")},
{`((?:\.\.\.|<<>>|%\{\}|%|\{\})|(?:(?:\.\.\.|[a-z_]\w*[!?]?)|[A-Z]\w*(?:\.[A-Z]\w*)*|(?:\<\<\<|\>\>\>|\|\|\||\&\&\&|\^\^\^|\~\~\~|\=\=\=|\!\=\=|\~\>\>|\<\~\>|\|\~\>|\<\|\>|\=\=|\!\=|\<\=|\>\=|\&\&|\|\||\<\>|\+\+|\-\-|\|\>|\=\~|\-\>|\<\-|\||\.|\=|\~\>|\<\~|\<|\>|\+|\-|\*|\/|\!|\^|\&)))(:)(?=\s|\n)`, ByGroups(LiteralStringSymbol, Punctuation), nil},
{`(fn|do|end|after|else|rescue|catch)\b`, Keyword, nil},
{`(not|and|or|when|in)\b`, OperatorWord, nil},
{`(case|cond|for|if|unless|try|receive|raise|quote|unquote|unquote_splicing|throw|super|while)\b`, Keyword, nil},
{`(def|defp|defmodule|defprotocol|defmacro|defmacrop|defdelegate|defexception|defstruct|defimpl|defcallback)\b`, KeywordDeclaration, nil},
{`(import|require|use|alias)\b`, KeywordNamespace, nil},
{`(nil|true|false)\b`, NameConstant, nil},
{`(_|__MODULE__|__DIR__|__ENV__|__CALLER__)\b`, NamePseudo, nil},
{`@(?:\.\.\.|[a-z_]\w*[!?]?)`, NameAttribute, nil},
{`(?:\.\.\.|[a-z_]\w*[!?]?)`, Name, nil},
{`(%?)([A-Z]\w*(?:\.[A-Z]\w*)*)`, ByGroups(Punctuation, NameClass), nil},

12
lexers/testdata/elixir.actual vendored Normal file
View File

@ -0,0 +1,12 @@
defmodule Test do
defmodule Formatter do
defstruct [:name]
end
def hello(name \\ "chroma") do
case name do
"chroma" -> IO.puts("Hello, CHROMA!!")
other -> raise "no others please"
end
end
end

66
lexers/testdata/elixir.expected vendored Normal file
View File

@ -0,0 +1,66 @@
[
{"type":"KeywordDeclaration","value":"defmodule"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Test"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"do"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"defmodule"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Formatter"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"do"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"defstruct"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"["},
{"type":"LiteralStringSymbol","value":":name"},
{"type":"Punctuation","value":"]"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"end"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordDeclaration","value":"def"},
{"type":"Text","value":" "},
{"type":"Name","value":"hello"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"name"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"\\\\"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"chroma\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"do"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"case"},
{"type":"Text","value":" "},
{"type":"Name","value":"name"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"do"},
{"type":"Text","value":"\n "},
{"type":"LiteralStringDouble","value":"\"chroma\""},
{"type":"Text","value":" "},
{"type":"Operator","value":"-\u003e"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"IO"},
{"type":"Operator","value":"."},
{"type":"Name","value":"puts"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"Hello, CHROMA!!\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"other"},
{"type":"Text","value":" "},
{"type":"Operator","value":"-\u003e"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"raise"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"no others please\""},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"end"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"end"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"end"},
{"type":"Text","value":"\n"}
]