...

Source file src/github.com/alecthomas/chroma/lexers/d/d.go

Documentation: github.com/alecthomas/chroma/lexers/d

     1  package d
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/internal"
     6  )
     7  
     8  // D lexer. https://dlang.org/spec/lex.html
     9  var D = internal.Register(MustNewLazyLexer(
    10  	&Config{
    11  		Name:      "D",
    12  		Aliases:   []string{"d"},
    13  		Filenames: []string{"*.d", "*.di"},
    14  		MimeTypes: []string{"text/x-d"},
    15  		EnsureNL:  true,
    16  	},
    17  	dRules,
    18  ))
    19  
    20  func dRules() Rules {
    21  	return Rules{
    22  		"root": {
    23  			{`[^\S\n]+`, Text, nil},
    24  
    25  			// https://dlang.org/spec/lex.html#comment
    26  			{`//.*?\n`, CommentSingle, nil},
    27  			{`/\*.*?\*/`, CommentMultiline, nil},
    28  			{`/\+.*?\+/`, CommentMultiline, nil},
    29  
    30  			// https://dlang.org/spec/lex.html#keywords
    31  			{`(asm|assert|body|break|case|cast|catch|continue|default|debug|delete|deprecated|do|else|finally|for|foreach|foreach_reverse|goto|if|in|invariant|is|macro|mixin|new|out|pragma|return|super|switch|this|throw|try|version|while|with)\b`, Keyword, nil},
    32  			{`__(FILE|FILE_FULL_PATH|MODULE|LINE|FUNCTION|PRETTY_FUNCTION|DATE|EOF|TIME|TIMESTAMP|VENDOR|VERSION)__\b`, NameBuiltin, nil},
    33  			{`__(traits|vector|parameters)\b`, NameBuiltin, nil},
    34  			{`((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)((?:[^\W\d]|\$)[\w$]*)(\s*)(\()`, ByGroups(UsingSelf("root"), NameFunction, Text, Operator), nil},
    35  
    36  			// https://dlang.org/spec/attribute.html#uda
    37  			{`@[\w.]*`, NameDecorator, nil},
    38  			{`(abstract|auto|alias|align|const|delegate|enum|export|final|function|inout|lazy|nothrow|override|package|private|protected|public|pure|static|synchronized|template|volatile|__gshared)\b`, KeywordDeclaration, nil},
    39  
    40  			// https://dlang.org/spec/type.html#basic-data-types
    41  			{`(void|bool|byte|ubyte|short|ushort|int|uint|long|ulong|cent|ucent|float|double|real|ifloat|idouble|ireal|cfloat|cdouble|creal|char|wchar|dchar|string|wstring|dstring)\b`, KeywordType, nil},
    42  			{`(module)(\s+)`, ByGroups(KeywordNamespace, Text), Push("import")},
    43  			{`(true|false|null)\b`, KeywordConstant, nil},
    44  			{`(class|interface|struct|template|union)(\s+)`, ByGroups(KeywordDeclaration, Text), Push("class")},
    45  			{`(import)(\s+)`, ByGroups(KeywordNamespace, Text), Push("import")},
    46  
    47  			// https://dlang.org/spec/lex.html#string_literals
    48  			// TODO support delimited strings
    49  			{`[qr]?"(\\\\|\\"|[^"])*"[cwd]?`, LiteralString, nil},
    50  			{"(`)([^`]*)(`)[cwd]?", LiteralString, nil},
    51  			{`'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'`, LiteralStringChar, nil},
    52  			{`(\.)((?:[^\W\d]|\$)[\w$]*)`, ByGroups(Operator, NameAttribute), nil},
    53  			{`^\s*([^\W\d]|\$)[\w$]*:`, NameLabel, nil},
    54  
    55  			// https://dlang.org/spec/lex.html#floatliteral
    56  			{`([0-9][0-9_]*\.([0-9][0-9_]*)?|\.[0-9][0-9_]*)([eE][+\-]?[0-9][0-9_]*)?[fFL]?i?|[0-9][eE][+\-]?[0-9][0-9_]*[fFL]?|[0-9]([eE][+\-]?[0-9][0-9_]*)?[fFL]|0[xX]([0-9a-fA-F][0-9a-fA-F_]*\.?|([0-9a-fA-F][0-9a-fA-F_]*)?\.[0-9a-fA-F][0-9a-fA-F_]*)[pP][+\-]?[0-9][0-9_]*[fFL]?`, LiteralNumberFloat, nil},
    57  			// https://dlang.org/spec/lex.html#integerliteral
    58  			{`0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?`, LiteralNumberHex, nil},
    59  			{`0[bB][01][01_]*[lL]?`, LiteralNumberBin, nil},
    60  			{`0[0-7_]+[lL]?`, LiteralNumberOct, nil},
    61  			{`0|[1-9][0-9_]*[lL]?`, LiteralNumberInteger, nil},
    62  			{`([~^*!%&\[\](){}<>|+=:;,./?-]|q{)`, Operator, nil},
    63  			{`([^\W\d]|\$)[\w$]*`, Name, nil},
    64  			{`\n`, Text, nil},
    65  		},
    66  		"class": {
    67  			{`([^\W\d]|\$)[\w$]*`, NameClass, Pop(1)},
    68  		},
    69  		"import": {
    70  			{`[\w.]+\*?`, NameNamespace, Pop(1)},
    71  		},
    72  	}
    73  }
    74  

View as plain text