1# -*- coding: utf-8 -*- 2""" 3 pygments.lexers.c_cpp 4 ~~~~~~~~~~~~~~~~~~~~~ 5 6 Lexers for C/C++ languages. 7 8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. 9 :license: BSD, see LICENSE for details. 10""" 11 12import re 13 14from pygments.lexer import RegexLexer, include, bygroups, using, \ 15 this, inherit, default, words 16from pygments.util import get_bool_opt 17from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 18 Number, Punctuation, Error 19 20__all__ = ['CLexer', 'CppLexer'] 21 22 23class CFamilyLexer(RegexLexer): 24 """ 25 For C family source code. This is used as a base class to avoid repetitious 26 definitions. 27 """ 28 29 #: optional Comment or Whitespace 30 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' 31 32 # The trailing ?, rather than *, avoids a geometric performance drop here. 33 #: only one /* */ style comment 34 _ws1 = r'\s*(?:/[*].*?[*]/\s*)?' 35 36 # Hexadecimal part in an hexadecimal integer/floating-point literal. 37 # This includes decimal separators matching. 38 _hexpart = r'[0-9a-fA-F](\'?[0-9a-fA-F])*' 39 # Decimal part in an decimal integer/floating-point literal. 40 # This includes decimal separators matching. 41 _decpart = r'\d(\'?\d)*' 42 # Integer literal suffix (e.g. 'ull' or 'll'). 43 _intsuffix = r'(([uU][lL]{0,2})|[lL]{1,2}[uU]?)?' 44 45 # Identifier regex with C and C++ Universal Character Name (UCN) support. 46 _ident = r'(?:[a-zA-Z_$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})*' 47 48 tokens = { 49 'whitespace': [ 50 # preprocessor directives: without whitespace 51 (r'^#if\s+0', Comment.Preproc, 'if0'), 52 ('^#', Comment.Preproc, 'macro'), 53 # or with whitespace 54 ('^(' + _ws1 + r')(#if\s+0)', 55 bygroups(using(this), Comment.Preproc), 'if0'), 56 ('^(' + _ws1 + ')(#)', 57 bygroups(using(this), Comment.Preproc), 'macro'), 58 (r'\n', Text), 59 (r'\s+', Text), 60 (r'\\\n', Text), # line continuation 61 (r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single), 62 (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline), 63 # Open until EOF, so no ending delimeter 64 (r'/(\\\n)?[*][\w\W]*', Comment.Multiline), 65 ], 66 'statements': [ 67 (r'([LuU]|u8)?(")', bygroups(String.Affix, String), 'string'), 68 (r"([LuU]|u8)?(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')", 69 bygroups(String.Affix, String.Char, String.Char, String.Char)), 70 71 # Hexadecimal floating-point literals (C11, C++17) 72 (r'0[xX](' + _hexpart + r'\.' + _hexpart + r'|\.' + _hexpart + r'|' + _hexpart + r')[pP][+-]?' + _hexpart + r'[lL]?', Number.Float), 73 74 (r'(-)?(' + _decpart + r'\.' + _decpart + r'|\.' + _decpart + r'|' + _decpart + r')[eE][+-]?' + _decpart + r'[fFlL]?', Number.Float), 75 (r'(-)?((' + _decpart + r'\.(' + _decpart + r')?|\.' + _decpart + r')[fFlL]?)|(' + _decpart + r'[fFlL])', Number.Float), 76 (r'(-)?0[xX]' + _hexpart + _intsuffix, Number.Hex), 77 (r'(-)?0[bB][01](\'?[01])*' + _intsuffix, Number.Bin), 78 (r'(-)?0(\'?[0-7])+' + _intsuffix, Number.Oct), 79 (r'(-)?' + _decpart + _intsuffix, Number.Integer), 80 (r'\*/', Error), 81 (r'[~!%^&*+=|?:<>/-]', Operator), 82 (r'[()\[\],.]', Punctuation), 83 (r'(struct|union)(\s+)', bygroups(Keyword, Text), 'classname'), 84 (words(('asm', 'auto', 'break', 'case', 'const', 'continue', 85 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto', 86 'if', 'register', 'restricted', 'return', 'sizeof', 'struct', 87 'static', 'switch', 'typedef', 'volatile', 'while', 'union', 88 'thread_local', 'alignas', 'alignof', 'static_assert', '_Pragma'), 89 suffix=r'\b'), Keyword), 90 (r'(bool|int|long|float|short|double|char|unsigned|signed|void)\b', 91 Keyword.Type), 92 (words(('inline', '_inline', '__inline', 'naked', 'restrict', 93 'thread'), suffix=r'\b'), Keyword.Reserved), 94 # Vector intrinsics 95 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved), 96 # Microsoft-isms 97 (words(( 98 'asm', 'int8', 'based', 'except', 'int16', 'stdcall', 'cdecl', 99 'fastcall', 'int32', 'declspec', 'finally', 'int64', 'try', 100 'leave', 'wchar_t', 'w64', 'unaligned', 'raise', 'noop', 101 'identifier', 'forceinline', 'assume'), 102 prefix=r'__', suffix=r'\b'), Keyword.Reserved), 103 (r'(true|false|NULL)\b', Name.Builtin), 104 (r'(' + _ident + r')(\s*)(:)(?!:)', bygroups(Name.Label, Text, Punctuation)), 105 (_ident, Name) 106 ], 107 'root': [ 108 include('whitespace'), 109 # functions 110 (r'((?:' + _ident + r'(?:[&*\s])+))' # return arguments 111 r'(' + _ident + r')' # method name 112 r'(\s*\([^;]*?\))' # signature 113 r'([^;{]*)(\{)', 114 bygroups(using(this), Name.Function, using(this), using(this), 115 Punctuation), 116 'function'), 117 # function declarations 118 (r'((?:' + _ident + r'(?:[&*\s])+))' # return arguments 119 r'(' + _ident + r')' # method name 120 r'(\s*\([^;]*?\))' # signature 121 r'([^;]*)(;)', 122 bygroups(using(this), Name.Function, using(this), using(this), 123 Punctuation)), 124 default('statement'), 125 ], 126 'statement': [ 127 include('whitespace'), 128 include('statements'), 129 (r'\}', Punctuation), 130 (r'[{;]', Punctuation, '#pop'), 131 ], 132 'function': [ 133 include('whitespace'), 134 include('statements'), 135 (';', Punctuation), 136 (r'\{', Punctuation, '#push'), 137 (r'\}', Punctuation, '#pop'), 138 ], 139 'string': [ 140 (r'"', String, '#pop'), 141 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|' 142 r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape), 143 (r'[^\\"\n]+', String), # all other characters 144 (r'\\\n', String), # line continuation 145 (r'\\', String), # stray backslash 146 ], 147 'macro': [ 148 (r'(include)('+_ws1+r')("[^"]+")([^\n]*)', bygroups(Comment.Preproc, using(this), Comment.PreprocFile, Comment.Single)), 149 (r'(include)('+_ws1+r')(<[^>]+>)([^\n]*)', bygroups(Comment.Preproc, using(this), Comment.PreprocFile, Comment.Single)), 150 (r'[^/\n]+', Comment.Preproc), 151 (r'/[*](.|\n)*?[*]/', Comment.Multiline), 152 (r'//.*?\n', Comment.Single, '#pop'), 153 (r'/', Comment.Preproc), 154 (r'(?<=\\)\n', Comment.Preproc), 155 (r'\n', Comment.Preproc, '#pop'), 156 ], 157 'if0': [ 158 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), 159 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'), 160 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), 161 (r'.*?\n', Comment), 162 ], 163 'classname': [ 164 (_ident, Name.Class, '#pop'), 165 # template specification 166 (r'\s*(?=>)', Text, '#pop'), 167 default('#pop') 168 ] 169 } 170 171 stdlib_types = { 172 'size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 'sig_atomic_t', 'fpos_t', 173 'clock_t', 'time_t', 'va_list', 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', 174 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'} 175 c99_types = { 176 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t', 177 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 'int_least16_t', 178 'int_least32_t', 'int_least64_t', 'uint_least8_t', 'uint_least16_t', 179 'uint_least32_t', 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t', 180 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 'uint_fast64_t', 181 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'} 182 linux_types = { 183 'clockid_t', 'cpu_set_t', 'cpumask_t', 'dev_t', 'gid_t', 'id_t', 'ino_t', 'key_t', 184 'mode_t', 'nfds_t', 'pid_t', 'rlim_t', 'sig_t', 'sighandler_t', 'siginfo_t', 185 'sigset_t', 'sigval_t', 'socklen_t', 'timer_t', 'uid_t'} 186 c11_atomic_types = { 187 'atomic_bool', 'atomic_char', 'atomic_schar', 'atomic_uchar', 'atomic_short', 188 'atomic_ushort', 'atomic_int', 'atomic_uint', 'atomic_long', 'atomic_ulong', 189 'atomic_llong', 'atomic_ullong', 'atomic_char16_t', 'atomic_char32_t', 'atomic_wchar_t', 190 'atomic_int_least8_t', 'atomic_uint_least8_t', 'atomic_int_least16_t', 191 'atomic_uint_least16_t', 'atomic_int_least32_t', 'atomic_uint_least32_t', 192 'atomic_int_least64_t', 'atomic_uint_least64_t', 'atomic_int_fast8_t', 193 'atomic_uint_fast8_t', 'atomic_int_fast16_t', 'atomic_uint_fast16_t', 194 'atomic_int_fast32_t', 'atomic_uint_fast32_t', 'atomic_int_fast64_t', 195 'atomic_uint_fast64_t', 'atomic_intptr_t', 'atomic_uintptr_t', 'atomic_size_t', 196 'atomic_ptrdiff_t', 'atomic_intmax_t', 'atomic_uintmax_t'} 197 198 def __init__(self, **options): 199 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True) 200 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True) 201 self.c11highlighting = get_bool_opt(options, 'c11highlighting', True) 202 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True) 203 RegexLexer.__init__(self, **options) 204 205 def get_tokens_unprocessed(self, text): 206 for index, token, value in \ 207 RegexLexer.get_tokens_unprocessed(self, text): 208 if token is Name: 209 if self.stdlibhighlighting and value in self.stdlib_types: 210 token = Keyword.Type 211 elif self.c99highlighting and value in self.c99_types: 212 token = Keyword.Type 213 elif self.c11highlighting and value in self.c11_atomic_types: 214 token = Keyword.Type 215 elif self.platformhighlighting and value in self.linux_types: 216 token = Keyword.Type 217 yield index, token, value 218 219 220class CLexer(CFamilyLexer): 221 """ 222 For C source code with preprocessor directives. 223 224 Additional options accepted: 225 226 `stdlibhighlighting` 227 Highlight common types found in the C/C++ standard library (e.g. `size_t`). 228 (default: ``True``). 229 230 `c99highlighting` 231 Highlight common types found in the C99 standard library (e.g. `int8_t`). 232 Actually, this includes all fixed-width integer types. 233 (default: ``True``). 234 235 `c11highlighting` 236 Highlight atomic types found in the C11 standard library (e.g. `atomic_bool`). 237 (default: ``True``). 238 239 `platformhighlighting` 240 Highlight common types found in the platform SDK headers (e.g. `clockid_t` on Linux). 241 (default: ``True``). 242 """ 243 name = 'C' 244 aliases = ['c'] 245 filenames = ['*.c', '*.h', '*.idc'] 246 mimetypes = ['text/x-chdr', 'text/x-csrc'] 247 priority = 0.1 248 249 tokens = { 250 'statements': [ 251 (words(( 252 '_Alignas', '_Alignof', '_Noreturn', '_Generic', '_Thread_local', 253 '_Static_assert', '_Imaginary', 'noreturn', 'imaginary', 'complex'), 254 suffix=r'\b'), Keyword), 255 (words(('_Bool', '_Complex', '_Atomic'), suffix=r'\b'), Keyword.Type), 256 inherit 257 ] 258 } 259 260 def analyse_text(text): 261 if re.search(r'^\s*#include [<"]', text, re.MULTILINE): 262 return 0.1 263 if re.search(r'^\s*#ifn?def ', text, re.MULTILINE): 264 return 0.1 265 266 267class CppLexer(CFamilyLexer): 268 """ 269 For C++ source code with preprocessor directives. 270 271 Additional options accepted: 272 273 `stdlibhighlighting` 274 Highlight common types found in the C/C++ standard library (e.g. `size_t`). 275 (default: ``True``). 276 277 `c99highlighting` 278 Highlight common types found in the C99 standard library (e.g. `int8_t`). 279 Actually, this includes all fixed-width integer types. 280 (default: ``True``). 281 282 `c11highlighting` 283 Highlight atomic types found in the C11 standard library (e.g. `atomic_bool`). 284 (default: ``True``). 285 286 `platformhighlighting` 287 Highlight common types found in the platform SDK headers (e.g. `clockid_t` on Linux). 288 (default: ``True``). 289 """ 290 name = 'C++' 291 aliases = ['cpp', 'c++'] 292 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', 293 '*.cc', '*.hh', '*.cxx', '*.hxx', 294 '*.C', '*.H', '*.cp', '*.CPP'] 295 mimetypes = ['text/x-c++hdr', 'text/x-c++src'] 296 priority = 0.1 297 298 tokens = { 299 'statements': [ 300 (r'(class|concept|typename)(\s+)', bygroups(Keyword, Text), 'classname'), 301 (words(( 302 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit', 303 'export', 'friend', 'mutable', 'namespace', 'new', 'operator', 304 'private', 'protected', 'public', 'reinterpret_cast', 'class', 305 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws', 306 'try', 'typeid', 'using', 'virtual', 'constexpr', 'nullptr', 'concept', 307 'decltype', 'noexcept', 'override', 'final', 'constinit', 'consteval', 308 'co_await', 'co_return', 'co_yield', 'requires', 'import', 'module', 309 'typename'), 310 suffix=r'\b'), Keyword), 311 (r'char(16_t|32_t|8_t)\b', Keyword.Type), 312 (r'(enum)(\s+)', bygroups(Keyword, Text), 'enumname'), 313 314 # C++11 raw strings 315 (r'((?:[LuU]|u8)?R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")', 316 bygroups(String.Affix, String, String.Delimiter, String.Delimiter, 317 String, String.Delimiter, String)), 318 inherit, 319 ], 320 'root': [ 321 inherit, 322 # C++ Microsoft-isms 323 (words(('virtual_inheritance', 'uuidof', 'super', 'single_inheritance', 324 'multiple_inheritance', 'interface', 'event'), 325 prefix=r'__', suffix=r'\b'), Keyword.Reserved), 326 # Offload C++ extensions, http://offload.codeplay.com/ 327 (r'__(offload|blockingoffload|outer)\b', Keyword.Pseudo), 328 ], 329 'enumname': [ 330 include('whitespace'), 331 # 'enum class' and 'enum struct' C++11 support 332 (words(('class', 'struct'), suffix=r'\b'), Keyword), 333 (CFamilyLexer._ident, Name.Class, '#pop'), 334 # template specification 335 (r'\s*(?=>)', Text, '#pop'), 336 default('#pop') 337 ] 338 } 339 340 def analyse_text(text): 341 if re.search('#include <[a-z_]+>', text): 342 return 0.2 343 if re.search('using namespace ', text): 344 return 0.4 345