d.go

 1package d
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// D lexer. https://dlang.org/spec/lex.html
 9var D = internal.Register(MustNewLexer(
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	Rules{
18		"root": {
19			{`[^\S\n]+`, Text, nil},
20
21			// https://dlang.org/spec/lex.html#comment
22			{`//.*?\n`, CommentSingle, nil},
23			{`/\*.*?\*/`, CommentMultiline, nil},
24			{`/\+.*?\+/`, CommentMultiline, nil},
25
26			// https://dlang.org/spec/lex.html#keywords
27			{`(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},
28			{`__(FILE|FILE_FULL_PATH|MODULE|LINE|FUNCTION|PRETTY_FUNCTION|DATE|EOF|TIME|TIMESTAMP|VENDOR|VERSION)__\b`, NameBuiltin, nil},
29			{`__(traits|vector|parameters)\b`, NameBuiltin, nil},
30			{`((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)((?:[^\W\d]|\$)[\w$]*)(\s*)(\()`, ByGroups(UsingSelf("root"), NameFunction, Text, Operator), nil},
31
32			// https://dlang.org/spec/attribute.html#uda
33			{`@[\w.]*`, NameDecorator, nil},
34			{`(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},
35
36			// https://dlang.org/spec/type.html#basic-data-types
37			{`(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},
38			{`(module)(\s+)`, ByGroups(KeywordNamespace, Text), Push("import")},
39			{`(true|false|null)\b`, KeywordConstant, nil},
40			{`(class|interface|struct|template|union)(\s+)`, ByGroups(KeywordDeclaration, Text), Push("class")},
41			{`(import)(\s+)`, ByGroups(KeywordNamespace, Text), Push("import")},
42
43			// https://dlang.org/spec/lex.html#string_literals
44			// TODO support delimited strings
45			{`[qr]?"(\\\\|\\"|[^"])*"[cwd]?`, LiteralString, nil},
46			{"(`)([^`]*)(`)[cwd]?", LiteralString, nil},
47			{`'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'`, LiteralStringChar, nil},
48			{`(\.)((?:[^\W\d]|\$)[\w$]*)`, ByGroups(Operator, NameAttribute), nil},
49			{`^\s*([^\W\d]|\$)[\w$]*:`, NameLabel, nil},
50
51			// https://dlang.org/spec/lex.html#floatliteral
52			{`([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},
53			// https://dlang.org/spec/lex.html#integerliteral
54			{`0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?`, LiteralNumberHex, nil},
55			{`0[bB][01][01_]*[lL]?`, LiteralNumberBin, nil},
56			{`0[0-7_]+[lL]?`, LiteralNumberOct, nil},
57			{`0|[1-9][0-9_]*[lL]?`, LiteralNumberInteger, nil},
58			{`([~^*!%&\[\](){}<>|+=:;,./?-]|q{)`, Operator, nil},
59			{`([^\W\d]|\$)[\w$]*`, Name, nil},
60			{`\n`, Text, nil},
61		},
62		"class": {
63			{`([^\W\d]|\$)[\w$]*`, NameClass, Pop(1)},
64		},
65		"import": {
66			{`[\w.]+\*?`, NameNamespace, Pop(1)},
67		},
68	},
69))