1package c 2 3import ( 4 . "github.com/alecthomas/chroma" // nolint 5 "github.com/alecthomas/chroma/lexers/internal" 6) 7 8// CPP lexer. 9var CPP = internal.Register(MustNewLexer( 10 &Config{ 11 Name: "C++", 12 Aliases: []string{"cpp", "c++"}, 13 Filenames: []string{"*.cpp", "*.hpp", "*.c++", "*.h++", "*.cc", "*.hh", "*.cxx", "*.hxx", "*.C", "*.H", "*.cp", "*.CPP"}, 14 MimeTypes: []string{"text/x-c++hdr", "text/x-c++src"}, 15 EnsureNL: true, 16 }, 17 Rules{ 18 "statements": { 19 {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}, 20 {`(enum)\b(\s+)(class)\b(\s*)`, ByGroups(Keyword, Text, Keyword, Text), Push("classname")}, 21 {`(class|struct|enum|union)\b(\s*)`, ByGroups(Keyword, Text), Push("classname")}, 22 {`\[\[.+\]\]`, NameAttribute, nil}, 23 {`(R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")`, ByGroups(LiteralStringAffix, LiteralString, LiteralStringDelimiter, LiteralStringDelimiter, LiteralString, LiteralStringDelimiter, LiteralString), nil}, 24 {`(u8|u|U)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")}, 25 {`(L?)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")}, 26 {`(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')`, ByGroups(LiteralStringAffix, LiteralStringChar, LiteralStringChar, LiteralStringChar), nil}, 27 {`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil}, 28 {`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil}, 29 {`0[xX]([0-9A-Fa-f]('?[0-9A-Fa-f]+)*)[LlUu]*`, LiteralNumberHex, nil}, 30 {`0('?[0-7]+)+[LlUu]*`, LiteralNumberOct, nil}, 31 {`0[Bb][01]('?[01]+)*[LlUu]*`, LiteralNumberBin, nil}, 32 {`[0-9]('?[0-9]+)*[LlUu]*`, LiteralNumberInteger, nil}, 33 {`\*/`, Error, nil}, 34 {`[~!%^&*+=|?:<>/-]`, Operator, nil}, 35 {`[()\[\],.]`, Punctuation, nil}, 36 {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}, 37 {`(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}, 38 {Words(``, `\b`, `inline`, `_inline`, `__inline`, `naked`, `restrict`, `thread`, `typename`), KeywordReserved, nil}, 39 {`(__m(128i|128d|128|64))\b`, KeywordReserved, nil}, 40 {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}, 41 {`(true|false|NULL)\b`, NameBuiltin, nil}, 42 {`([a-zA-Z_]\w*)(\s*)(:)(?!:)`, ByGroups(NameLabel, Text, Punctuation), nil}, 43 {`[a-zA-Z_]\w*`, Name, nil}, 44 }, 45 "root": { 46 Include("whitespace"), 47 {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")}, 48 {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil}, 49 Default(Push("statement")), 50 {Words(`__`, `\b`, `virtual_inheritance`, `uuidof`, `super`, `single_inheritance`, `multiple_inheritance`, `interface`, `event`), KeywordReserved, nil}, 51 {`__(offload|blockingoffload|outer)\b`, KeywordPseudo, nil}, 52 }, 53 "classname": { 54 {`(\[\[.+\]\])(\s*)`, ByGroups(NameAttribute, Text), nil}, 55 {`[a-zA-Z_]\w*`, NameClass, Pop(1)}, 56 {`\s*(?=[>{])`, Text, Pop(1)}, 57 }, 58 "whitespace": { 59 {`^#if\s+0`, CommentPreproc, Push("if0")}, 60 {`^#`, CommentPreproc, Push("macro")}, 61 {`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")}, 62 {`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")}, 63 {`\n`, Text, nil}, 64 {`\s+`, Text, nil}, 65 {`\\\n`, Text, nil}, 66 {`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil}, 67 {`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil}, 68 {`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil}, 69 }, 70 "statement": { 71 Include("whitespace"), 72 Include("statements"), 73 {`[{]`, Punctuation, Push("root")}, 74 {`[;}]`, Punctuation, Pop(1)}, 75 }, 76 "function": { 77 Include("whitespace"), 78 Include("statements"), 79 {`;`, Punctuation, nil}, 80 {`\{`, Punctuation, Push()}, 81 {`\}`, Punctuation, Pop(1)}, 82 }, 83 "string": { 84 {`"`, LiteralString, Pop(1)}, 85 {`\\([\\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}, 86 {`[^\\"\n]+`, LiteralString, nil}, 87 {`\\\n`, LiteralString, nil}, 88 {`\\`, LiteralString, nil}, 89 }, 90 "macro": { 91 {`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil}, 92 {`[^/\n]+`, CommentPreproc, nil}, 93 {`/[*](.|\n)*?[*]/`, CommentMultiline, nil}, 94 {`//.*?\n`, CommentSingle, Pop(1)}, 95 {`/`, CommentPreproc, nil}, 96 {`(?<=\\)\n`, CommentPreproc, nil}, 97 {`\n`, CommentPreproc, Pop(1)}, 98 }, 99 "if0": { 100 {`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()}, 101 {`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)}, 102 {`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)}, 103 {`.*?\n`, Comment, nil}, 104 }, 105 }, 106)) 107