...

Source file src/github.com/alecthomas/chroma/lexers/c/cpp.go

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

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

View as plain text