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.

cpp.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package c
  2. import (
  3. . "github.com/alecthomas/chroma" // nolint
  4. "github.com/alecthomas/chroma/lexers/internal"
  5. )
  6. // CPP lexer.
  7. var CPP = internal.Register(MustNewLexer(
  8. &Config{
  9. Name: "C++",
  10. Aliases: []string{"cpp", "c++"},
  11. Filenames: []string{"*.cpp", "*.hpp", "*.c++", "*.h++", "*.cc", "*.hh", "*.cxx", "*.hxx", "*.C", "*.H", "*.cp", "*.CPP"},
  12. MimeTypes: []string{"text/x-c++hdr", "text/x-c++src"},
  13. EnsureNL: true,
  14. },
  15. Rules{
  16. "statements": {
  17. {Words(``, `\b`, `catch`, `const_cast`, `delete`, `dynamic_cast`, `explicit`, `export`, `friend`, `mutable`, `namespace`, `new`, `operator`, `private`, `protected`, `public`, `reinterpret_cast`, `restrict`, `static_cast`, `template`, `this`, `throw`, `throws`, `try`, `typeid`, `typename`, `using`, `virtual`, `constexpr`, `nullptr`, `decltype`, `thread_local`, `alignas`, `alignof`, `static_assert`, `noexcept`, `override`, `final`, `concept`, `requires`, `consteval`, `co_await`, `co_return`, `co_yield`), Keyword, nil},
  18. {`(enum)\b(\s+)(class)\b(\s*)`, ByGroups(Keyword, Text, Keyword, Text), Push("classname")},
  19. {`(class|struct|enum|union)\b(\s*)`, ByGroups(Keyword, Text), Push("classname")},
  20. {`\[\[.+\]\]`, NameAttribute, nil},
  21. {`(R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")`, ByGroups(LiteralStringAffix, LiteralString, LiteralStringDelimiter, LiteralStringDelimiter, LiteralString, LiteralStringDelimiter, LiteralString), nil},
  22. {`(u8|u|U)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")},
  23. {`(L?)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")},
  24. {`(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')`, ByGroups(LiteralStringAffix, LiteralStringChar, LiteralStringChar, LiteralStringChar), nil},
  25. {`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
  26. {`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
  27. {`0[xX]([0-9A-Fa-f]('?[0-9A-Fa-f]+)*)[LlUu]*`, LiteralNumberHex, nil},
  28. {`0('?[0-7]+)+[LlUu]*`, LiteralNumberOct, nil},
  29. {`0[Bb][01]('?[01]+)*[LlUu]*`, LiteralNumberBin, nil},
  30. {`[0-9]('?[0-9]+)*[LlUu]*`, LiteralNumberInteger, nil},
  31. {`\*/`, Error, nil},
  32. {`[~!%^&*+=|?:<>/-]`, Operator, nil},
  33. {`[()\[\],.]`, Punctuation, nil},
  34. {Words(``, `\b`, `asm`, `auto`, `break`, `case`, `const`, `continue`, `default`, `do`, `else`, `enum`, `extern`, `for`, `goto`, `if`, `register`, `restricted`, `return`, `sizeof`, `static`, `struct`, `switch`, `typedef`, `union`, `volatile`, `while`), Keyword, nil},
  35. {`(bool|int|long|float|short|double|char((8|16|32)_t)?|wchar_t|unsigned|signed|void|u?int(_fast|_least|)(8|16|32|64)_t)\b`, KeywordType, nil},
  36. {Words(``, `\b`, `inline`, `_inline`, `__inline`, `naked`, `restrict`, `thread`, `typename`), KeywordReserved, nil},
  37. {`(__m(128i|128d|128|64))\b`, KeywordReserved, nil},
  38. {Words(`__`, `\b`, `asm`, `int8`, `based`, `except`, `int16`, `stdcall`, `cdecl`, `fastcall`, `int32`, `declspec`, `finally`, `int64`, `try`, `leave`, `w64`, `unaligned`, `raise`, `noop`, `identifier`, `forceinline`, `assume`), KeywordReserved, nil},
  39. {`(true|false|NULL)\b`, NameBuiltin, nil},
  40. {`([a-zA-Z_]\w*)(\s*)(:)(?!:)`, ByGroups(NameLabel, Text, Punctuation), nil},
  41. {`[a-zA-Z_]\w*`, Name, nil},
  42. },
  43. "root": {
  44. Include("whitespace"),
  45. {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")},
  46. {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil},
  47. Default(Push("statement")),
  48. {Words(`__`, `\b`, `virtual_inheritance`, `uuidof`, `super`, `single_inheritance`, `multiple_inheritance`, `interface`, `event`), KeywordReserved, nil},
  49. {`__(offload|blockingoffload|outer)\b`, KeywordPseudo, nil},
  50. },
  51. "classname": {
  52. {`(\[\[.+\]\])(\s*)`, ByGroups(NameAttribute, Text), nil},
  53. {`[a-zA-Z_]\w*`, NameClass, Pop(1)},
  54. {`\s*(?=[>{])`, Text, Pop(1)},
  55. },
  56. "whitespace": {
  57. {`^#if\s+0`, CommentPreproc, Push("if0")},
  58. {`^#`, CommentPreproc, Push("macro")},
  59. {`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")},
  60. {`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")},
  61. {`\n`, Text, nil},
  62. {`\s+`, Text, nil},
  63. {`\\\n`, Text, nil},
  64. {`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
  65. {`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
  66. {`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
  67. },
  68. "statement": {
  69. Include("whitespace"),
  70. Include("statements"),
  71. {`[{]`, Punctuation, Push("root")},
  72. {`[;}]`, Punctuation, Pop(1)},
  73. },
  74. "function": {
  75. Include("whitespace"),
  76. Include("statements"),
  77. {`;`, Punctuation, nil},
  78. {`\{`, Punctuation, Push()},
  79. {`\}`, Punctuation, Pop(1)},
  80. },
  81. "string": {
  82. {`"`, LiteralString, Pop(1)},
  83. {`\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})`, LiteralStringEscape, nil},
  84. {`[^\\"\n]+`, LiteralString, nil},
  85. {`\\\n`, LiteralString, nil},
  86. {`\\`, LiteralString, nil},
  87. },
  88. "macro": {
  89. {`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil},
  90. {`[^/\n]+`, CommentPreproc, nil},
  91. {`/[*](.|\n)*?[*]/`, CommentMultiline, nil},
  92. {`//.*?\n`, CommentSingle, Pop(1)},
  93. {`/`, CommentPreproc, nil},
  94. {`(?<=\\)\n`, CommentPreproc, nil},
  95. {`\n`, CommentPreproc, Pop(1)},
  96. },
  97. "if0": {
  98. {`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()},
  99. {`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)},
  100. {`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)},
  101. {`.*?\n`, Comment, nil},
  102. },
  103. },
  104. ))