You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d.go 3.2KB

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