1"""
2 pygments.lexers.c_cpp
3 ~~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for C/C++ languages.
6
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11import re
12
13from pygments.lexer import RegexLexer, include, bygroups, using, \
14 this, inherit, default, words
15from pygments.util import get_bool_opt
16from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
17 Number, Punctuation, Whitespace
18
19__all__ = ['CLexer', 'CppLexer']
20
21
22class CFamilyLexer(RegexLexer):
23 """
24 For C family source code. This is used as a base class to avoid repetitious
25 definitions.
26 """
27
28 # The trailing ?, rather than *, avoids a geometric performance drop here.
29 #: only one /* */ style comment
30 _ws1 = r'\s*(?:/[*].*?[*]/\s*)?'
31
32 # Hexadecimal part in an hexadecimal integer/floating-point literal.
33 # This includes decimal separators matching.
34 _hexpart = r'[0-9a-fA-F](\'?[0-9a-fA-F])*'
35 # Decimal part in an decimal integer/floating-point literal.
36 # This includes decimal separators matching.
37 _decpart = r'\d(\'?\d)*'
38 # Integer literal suffix (e.g. 'ull' or 'll').
39 _intsuffix = r'(([uU]?[zZ])|([zZ][uU])|([uU][lL]{0,2})|([lL]{1,2}[uU]?))?'
40
41 # Identifier regex with C and C++ Universal Character Name (UCN) support.
42 _ident = r'(?!\d)(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})+'
43 _namespaced_ident = r'(?!\d)(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|::)+'
44
45 # Single and multiline comment regexes
46 # Beware not to use *? for the inner content! When these regexes
47 # are embedded in larger regexes, that can cause the stuff*? to
48 # match more than it would have if the regex had been used in
49 # a standalone way ...
50 _comment_single = r'//(?:.|(?<=\\)\n)*\n'
51 _comment_multiline = r'/(?:\\\n)?[*](?:[^*]|[*](?!(?:\\\n)?/))*[*](?:\\\n)?/'
52
53 # Regex to match optional comments
54 _possible_comments = rf'\s*(?:(?:(?:{_comment_single})|(?:{_comment_multiline}))\s*)*'
55
56 tokens = {
57 'whitespace': [
58 # preprocessor directives: without whitespace
59 (r'^#if\s+0', Comment.Preproc, 'if0'),
60 ('^#', Comment.Preproc, 'macro'),
61 # or with whitespace
62 ('^(' + _ws1 + r')(#if\s+0)',
63 bygroups(using(this), Comment.Preproc), 'if0'),
64 ('^(' + _ws1 + ')(#)',
65 bygroups(using(this), Comment.Preproc), 'macro'),
66 # Labels:
67 # Line start and possible indentation.
68 (r'(^[ \t]*)'
69 # Not followed by keywords which can be mistaken as labels.
70 r'(?!(?:public|private|protected|default)\b)'
71 # Actual label, followed by a single colon.
72 r'(' + _ident + r')(\s*)(:)(?!:)',
73 bygroups(Whitespace, Name.Label, Whitespace, Punctuation)),
74 (r'\n', Whitespace),
75 (r'[^\S\n]+', Whitespace),
76 (r'\\\n', Text), # line continuation
77 (_comment_single, Comment.Single),
78 (_comment_multiline, Comment.Multiline),
79 # Open until EOF, so no ending delimiter
80 (r'/(\\\n)?[*][\w\W]*', Comment.Multiline),
81 ],
82 'statements': [
83 include('keywords'),
84 include('types'),
85 (r'([LuU]|u8)?(")', bygroups(String.Affix, String), 'string'),
86 (r"([LuU]|u8)?(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')",
87 bygroups(String.Affix, String.Char, String.Char, String.Char)),
88
89 # Hexadecimal floating-point literals (C11, C++17)
90 (r'0[xX](' + _hexpart + r'\.' + _hexpart + r'|\.' + _hexpart +
91 r'|' + _hexpart + r')[pP][+-]?' + _hexpart + r'[lL]?', Number.Float),
92
93 (r'(-)?(' + _decpart + r'\.' + _decpart + r'|\.' + _decpart + r'|' +
94 _decpart + r')[eE][+-]?' + _decpart + r'[fFlL]?', Number.Float),
95 (r'(-)?((' + _decpart + r'\.(' + _decpart + r')?|\.' +
96 _decpart + r')[fFlL]?)|(' + _decpart + r'[fFlL])', Number.Float),
97 (r'(-)?0[xX]' + _hexpart + _intsuffix, Number.Hex),
98 (r'(-)?0[bB][01](\'?[01])*' + _intsuffix, Number.Bin),
99 (r'(-)?0(\'?[0-7])+' + _intsuffix, Number.Oct),
100 (r'(-)?' + _decpart + _intsuffix, Number.Integer),
101 (r'[~!%^&*+=|?:<>/-]', Operator),
102 (r'[()\[\],.]', Punctuation),
103 (r'(true|false|NULL|nullptr)\b', Name.Builtin),
104 (_ident, Name)
105 ],
106 'types': [
107 (words(('int8', 'int16', 'int32', 'int64', 'wchar_t'), prefix=r'__',
108 suffix=r'\b'), Keyword.Reserved),
109 (words(('bool', 'int', 'long', 'float', 'short', 'double', 'char',
110 'unsigned', 'signed', 'void', '_BitInt',
111 '__int128'), suffix=r'\b'), Keyword.Type)
112 ],
113 'keywords': [
114 (r'(struct|union)(\s+)', bygroups(Keyword, Whitespace), 'classname'),
115 (r'case\b', Keyword, 'case-value'),
116 (words(('asm', 'auto', 'break', 'const', 'constexpr', 'continue', 'default',
117 'do', 'else', 'enum', 'extern', 'for', 'goto', 'if', 'register',
118 'restricted', 'return', 'sizeof', 'struct', 'static', 'switch',
119 'typedef', 'typeof', 'typeof_unqual', 'volatile', 'while', 'union',
120 'thread_local', 'alignas', 'alignof', 'static_assert', '_Pragma', 'fortran'),
121 suffix=r'\b'), Keyword),
122 (words(('inline', '_inline', '__inline', 'naked', 'restrict',
123 'thread'), suffix=r'\b'), Keyword.Reserved),
124 # Vector intrinsics
125 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved),
126 # Microsoft-isms
127 (words((
128 'asm', 'based', 'except', 'stdcall', 'cdecl',
129 'fastcall', 'declspec', 'finally', 'try',
130 'leave', 'w64', 'unaligned', 'raise', 'noop',
131 'identifier', 'forceinline', 'assume', 'null'),
132 prefix=r'__', suffix=r'\b'), Keyword.Reserved)
133 ],
134 'root': [
135 include('whitespace'),
136 include('keywords'),
137 # functions
138 (r'(' + _namespaced_ident + r'(?:[&*\s])+)' # return arguments
139 r'(' + _possible_comments + r')'
140 r'(' + _namespaced_ident + r')' # method name
141 r'(' + _possible_comments + r')'
142 r'(\([^;"\')]*?\))' # signature
143 r'(' + _possible_comments + r')'
144 r'([^;{/"\']*)(\{)',
145 bygroups(using(this), using(this, state='whitespace'),
146 Name.Function, using(this, state='whitespace'),
147 using(this), using(this, state='whitespace'),
148 using(this), Punctuation),
149 'function'),
150 # function declarations
151 (r'(' + _namespaced_ident + r'(?:[&*\s])+)' # return arguments
152 r'(' + _possible_comments + r')'
153 r'(' + _namespaced_ident + r')' # method name
154 r'(' + _possible_comments + r')'
155 r'(\([^;"\')]*?\))' # signature
156 r'(' + _possible_comments + r')'
157 r'([^;/"\']*)(;)',
158 bygroups(using(this), using(this, state='whitespace'),
159 Name.Function, using(this, state='whitespace'),
160 using(this), using(this, state='whitespace'),
161 using(this), Punctuation)),
162 include('types'),
163 default('statement'),
164 ],
165 'statement': [
166 include('whitespace'),
167 include('statements'),
168 (r'\}', Punctuation),
169 (r'[{;]', Punctuation, '#pop'),
170 ],
171 'function': [
172 include('whitespace'),
173 include('statements'),
174 (';', Punctuation),
175 (r'\{', Punctuation, '#push'),
176 (r'\}', Punctuation, '#pop'),
177 ],
178 'string': [
179 (r'"', String, '#pop'),
180 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|'
181 r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape),
182 (r'[^\\"\n]+', String), # all other characters
183 (r'\\\n', String), # line continuation
184 (r'\\', String), # stray backslash
185 ],
186 'macro': [
187 (r'('+_ws1+r')(include)('+_ws1+r')("[^"]+")([^\n]*)',
188 bygroups(using(this), Comment.Preproc, using(this),
189 Comment.PreprocFile, Comment.Single)),
190 (r'('+_ws1+r')(include)('+_ws1+r')(<[^>]+>)([^\n]*)',
191 bygroups(using(this), Comment.Preproc, using(this),
192 Comment.PreprocFile, Comment.Single)),
193 (r'[^/\n]+', Comment.Preproc),
194 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
195 (r'//.*?\n', Comment.Single, '#pop'),
196 (r'/', Comment.Preproc),
197 (r'(?<=\\)\n', Comment.Preproc),
198 (r'\n', Comment.Preproc, '#pop'),
199 ],
200 'if0': [
201 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
202 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'),
203 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
204 (r'.*?\n', Comment),
205 ],
206 'classname': [
207 include('whitespace'),
208 # C/C++ attributes before type name
209 (r'\[\[(?=[^\[\]]*\]\])', Punctuation, 'attribute'),
210 (_ident, Name.Class, '#pop'),
211 # template specification
212 (r'\s*(?=>)', Text, '#pop'),
213 default('#pop')
214 ],
215 # Mark identifiers preceded by `case` keyword as constants.
216 'case-value': [
217 (r'(?<!:)(:)(?!:)', Punctuation, '#pop'),
218 (_ident, Name.Constant),
219 include('whitespace'),
220 include('statements'),
221 ],
222 'attribute': [
223 (r'\]\]', Punctuation, '#pop'),
224 (_ident, Name.Attribute),
225 (r'\(', Punctuation, 'attribute-args'),
226 (r'::', Punctuation),
227 (r',', Punctuation),
228 include('whitespace'),
229 ],
230 'attribute-args': [
231 (r'\)', Punctuation, '#pop'),
232 (r'\(', Punctuation, '#push'),
233 include('statements'),
234 ],
235 }
236
237 stdlib_types = {
238 'size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 'sig_atomic_t', 'fpos_t',
239 'clock_t', 'time_t', 'va_list', 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t',
240 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'}
241 c99_types = {
242 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t',
243 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 'int_least16_t',
244 'int_least32_t', 'int_least64_t', 'uint_least8_t', 'uint_least16_t',
245 'uint_least32_t', 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t',
246 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 'uint_fast64_t',
247 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'}
248 linux_types = {
249 'clockid_t', 'cpu_set_t', 'cpumask_t', 'dev_t', 'gid_t', 'id_t', 'ino_t', 'key_t',
250 'mode_t', 'nfds_t', 'pid_t', 'rlim_t', 'sig_t', 'sighandler_t', 'siginfo_t',
251 'sigset_t', 'sigval_t', 'socklen_t', 'timer_t', 'uid_t'}
252 c11_atomic_types = {
253 'atomic_bool', 'atomic_char', 'atomic_schar', 'atomic_uchar', 'atomic_short',
254 'atomic_ushort', 'atomic_int', 'atomic_uint', 'atomic_long', 'atomic_ulong',
255 'atomic_llong', 'atomic_ullong', 'atomic_char16_t', 'atomic_char32_t', 'atomic_wchar_t',
256 'atomic_int_least8_t', 'atomic_uint_least8_t', 'atomic_int_least16_t',
257 'atomic_uint_least16_t', 'atomic_int_least32_t', 'atomic_uint_least32_t',
258 'atomic_int_least64_t', 'atomic_uint_least64_t', 'atomic_int_fast8_t',
259 'atomic_uint_fast8_t', 'atomic_int_fast16_t', 'atomic_uint_fast16_t',
260 'atomic_int_fast32_t', 'atomic_uint_fast32_t', 'atomic_int_fast64_t',
261 'atomic_uint_fast64_t', 'atomic_intptr_t', 'atomic_uintptr_t', 'atomic_size_t',
262 'atomic_ptrdiff_t', 'atomic_intmax_t', 'atomic_uintmax_t'}
263
264 def __init__(self, **options):
265 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True)
266 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True)
267 self.c11highlighting = get_bool_opt(options, 'c11highlighting', True)
268 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True)
269 RegexLexer.__init__(self, **options)
270
271 def get_tokens_unprocessed(self, text, stack=('root',)):
272 for index, token, value in \
273 RegexLexer.get_tokens_unprocessed(self, text, stack):
274 if token is Name:
275 if self.stdlibhighlighting and value in self.stdlib_types:
276 token = Keyword.Type
277 elif self.c99highlighting and value in self.c99_types:
278 token = Keyword.Type
279 elif self.c11highlighting and value in self.c11_atomic_types:
280 token = Keyword.Type
281 elif self.platformhighlighting and value in self.linux_types:
282 token = Keyword.Type
283 yield index, token, value
284
285
286class CLexer(CFamilyLexer):
287 """
288 For C source code with preprocessor directives.
289
290 Additional options accepted:
291
292 `stdlibhighlighting`
293 Highlight common types found in the C/C++ standard library (e.g. `size_t`).
294 (default: ``True``).
295
296 `c99highlighting`
297 Highlight common types found in the C99 standard library (e.g. `int8_t`).
298 Actually, this includes all fixed-width integer types.
299 (default: ``True``).
300
301 `c11highlighting`
302 Highlight atomic types found in the C11 standard library (e.g. `atomic_bool`).
303 (default: ``True``).
304
305 `platformhighlighting`
306 Highlight common types found in the platform SDK headers (e.g. `clockid_t` on Linux).
307 (default: ``True``).
308 """
309 name = 'C'
310 aliases = ['c']
311 filenames = ['*.c', '*.h', '*.idc', '*.x[bp]m']
312 mimetypes = ['text/x-chdr', 'text/x-csrc', 'image/x-xbitmap', 'image/x-xpixmap']
313 url = 'https://en.wikipedia.org/wiki/C_(programming_language)'
314 version_added = ''
315 priority = 0.1
316
317 tokens = {
318 'statements': [
319 # C23 attributes [[...]] — lookahead to avoid matching ObjC's [[obj msg] msg]
320 (r'\[\[(?=[^\[\]]*\]\])', Punctuation, 'attribute'),
321 inherit,
322 ],
323 'keywords': [
324 (words((
325 '_Alignas', '_Alignof', '_Noreturn', '_Countof', '_Generic', '_Thread_local',
326 '_Static_assert', '_Imaginary', 'countof', 'noreturn', 'imaginary', 'complex'),
327 suffix=r'\b'), Keyword),
328 inherit
329 ],
330 'types': [
331 (words(('_Bool', '_Complex', '_Atomic', '_Decimal32', '_Decimal64', '_Decimal128'), suffix=r'\b'), Keyword.Type),
332 inherit
333 ]
334 }
335
336 def analyse_text(text):
337 if re.search(r'^\s*#include [<"]', text, re.MULTILINE):
338 return 0.1
339 if re.search(r'^\s*#ifn?def ', text, re.MULTILINE):
340 return 0.1
341
342
343class CppLexer(CFamilyLexer):
344 """
345 For C++ source code with preprocessor directives.
346
347 Additional options accepted:
348
349 `stdlibhighlighting`
350 Highlight common types found in the C/C++ standard library (e.g. `size_t`).
351 (default: ``True``).
352
353 `c99highlighting`
354 Highlight common types found in the C99 standard library (e.g. `int8_t`).
355 Actually, this includes all fixed-width integer types.
356 (default: ``True``).
357
358 `c11highlighting`
359 Highlight atomic types found in the C11 standard library (e.g. `atomic_bool`).
360 (default: ``True``).
361
362 `platformhighlighting`
363 Highlight common types found in the platform SDK headers (e.g. `clockid_t` on Linux).
364 (default: ``True``).
365 """
366 name = 'C++'
367 url = 'https://isocpp.org/'
368 aliases = ['cpp', 'c++']
369 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++',
370 '*.cc', '*.hh', '*.cxx', '*.hxx',
371 '*.C', '*.H', '*.cp', '*.CPP', '*.tpp',
372 '*.cppm', '*.ixx', '*.mxx']
373 mimetypes = ['text/x-c++hdr', 'text/x-c++src']
374 version_added = ''
375 priority = 0.1
376
377 tokens = {
378 'statements': [
379 # C++11 raw strings
380 (r'((?:[LuU]|u8)?R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")',
381 bygroups(String.Affix, String, String.Delimiter, String.Delimiter,
382 String, String.Delimiter, String)),
383 # C++26 annotations [[=Annotation]]
384 (r'(\[\[)(=)(' + CFamilyLexer._ident + r')',
385 bygroups(Punctuation, Punctuation, Name.Decorator), 'annotation'),
386 # C++11 attributes [[...]]
387 (r'\[\[(?=[^\[\]]*\]\])', Punctuation, 'attribute'),
388 inherit,
389 ],
390 'root': [
391 inherit,
392 # C++ Microsoft-isms
393 (words(('virtual_inheritance', 'uuidof', 'super', 'extends', 'single_inheritance',
394 'multiple_inheritance', 'interface', 'implements', 'event', 'finally', 'null'),
395 prefix=r'__', suffix=r'\b'), Keyword.Reserved),
396 # Offload C++ extensions, http://offload.codeplay.com/
397 (r'__(offload|blockingoffload|outer)\b', Keyword.Pseudo),
398 ],
399 'classname': [
400 include('whitespace'),
401 # C++26 annotations before type name
402 (r'(\[\[)(=)(' + CFamilyLexer._ident + r')',
403 bygroups(Punctuation, Punctuation, Name.Decorator), 'annotation'),
404 # C++ attributes before type name
405 (r'\[\[(?=[^\[\]]*\]\])', Punctuation, 'attribute'),
406 (CFamilyLexer._ident, Name.Class, '#pop'),
407 # template specification
408 (r'\s*(?=>)', Text, '#pop'),
409 default('#pop')
410 ],
411 'enumname': [
412 include('whitespace'),
413 # 'enum class' and 'enum struct' C++11 support
414 (words(('class', 'struct'), suffix=r'\b'), Keyword),
415 # C++26 annotations before enum name
416 (r'(\[\[)(=)(' + CFamilyLexer._ident + r')',
417 bygroups(Punctuation, Punctuation, Name.Decorator), 'annotation'),
418 # C++ attributes before enum name
419 (r'\[\[(?=[^\[\]]*\]\])', Punctuation, 'attribute'),
420 (CFamilyLexer._ident, Name.Class, '#pop'),
421 # template specification
422 (r'\s*(?=>)', Text, '#pop'),
423 default('#pop')
424 ],
425 'attribute': [
426 (r'using\b', Keyword),
427 (r':', Punctuation),
428 inherit,
429 ],
430 'keywords': [
431 (r'(class|concept|typename)(\s+)', bygroups(Keyword, Whitespace), 'classname'),
432 (words((
433 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit',
434 'export', 'friend', 'mutable', 'new', 'operator',
435 'private', 'protected', 'public', 'reinterpret_cast', 'class',
436 '__restrict', 'static_cast', 'template', 'this', 'throw', 'throws',
437 'try', 'typeid', 'using', 'virtual', 'concept',
438 'decltype', 'noexcept', 'override', 'final', 'constinit', 'consteval',
439 'co_await', 'co_return', 'co_yield', 'requires', 'import', 'module',
440 'typename', 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
441 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq', 'contract_assert', 'pre', 'post'),
442 suffix=r'\b'), Keyword),
443 (r'namespace\b', Keyword, 'namespace'),
444 (r'(enum)(\s+)', bygroups(Keyword, Whitespace), 'enumname'),
445 inherit
446 ],
447 'types': [
448 (r'char(16_t|32_t|8_t)\b', Keyword.Type),
449 inherit
450 ],
451 'namespace': [
452 (r'[;{]', Punctuation, ('#pop', 'root')),
453 (r'inline\b', Keyword.Reserved),
454 (CFamilyLexer._ident, Name.Namespace),
455 include('statement')
456 ],
457 'annotation': [
458 (r'\]\]', Punctuation, '#pop'),
459 (r'\(', Punctuation, 'annotation-args'),
460 (r'::', Punctuation),
461 (r',', Punctuation),
462 (CFamilyLexer._ident, Name.Decorator),
463 include('whitespace'),
464 ],
465 'annotation-args': [
466 (r'\)', Punctuation, '#pop'),
467 (r'\(', Punctuation, '#push'),
468 include('statements'),
469 ],
470 }
471
472 def analyse_text(text):
473 if re.search('#include <[a-z_]+>', text):
474 return 0.2
475 if re.search('using namespace ', text):
476 return 0.4