Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/c_cpp.py: 81%

63 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2 pygments.lexers.c_cpp 

3 ~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexers for C/C++ languages. 

6 

7 :copyright: Copyright 2006-2023 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][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)\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'), suffix=r'\b'), Keyword.Type) 

111 ], 

112 'keywords': [ 

113 (r'(struct|union)(\s+)', bygroups(Keyword, Whitespace), 'classname'), 

114 (r'case\b', Keyword, 'case-value'), 

115 (words(('asm', 'auto', 'break', 'const', 'continue', 'default', 

116 'do', 'else', 'enum', 'extern', 'for', 'goto', 'if', 

117 'register', 'restricted', 'return', 'sizeof', 'struct', 

118 'static', 'switch', 'typedef', 'volatile', 'while', 'union', 

119 'thread_local', 'alignas', 'alignof', 'static_assert', '_Pragma'), 

120 suffix=r'\b'), Keyword), 

121 (words(('inline', '_inline', '__inline', 'naked', 'restrict', 

122 'thread'), suffix=r'\b'), Keyword.Reserved), 

123 # Vector intrinsics 

124 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved), 

125 # Microsoft-isms 

126 (words(( 

127 'asm', 'based', 'except', 'stdcall', 'cdecl', 

128 'fastcall', 'declspec', 'finally', 'try', 

129 'leave', 'w64', 'unaligned', 'raise', 'noop', 

130 'identifier', 'forceinline', 'assume'), 

131 prefix=r'__', suffix=r'\b'), Keyword.Reserved) 

132 ], 

133 'root': [ 

134 include('whitespace'), 

135 include('keywords'), 

136 # functions 

137 (r'(' + _namespaced_ident + r'(?:[&*\s])+)' # return arguments 

138 r'(' + _possible_comments + r')' 

139 r'(' + _namespaced_ident + r')' # method name 

140 r'(' + _possible_comments + r')' 

141 r'(\([^;"\')]*?\))' # signature 

142 r'(' + _possible_comments + r')' 

143 r'([^;{/"\']*)(\{)', 

144 bygroups(using(this), using(this, state='whitespace'), 

145 Name.Function, using(this, state='whitespace'), 

146 using(this), using(this, state='whitespace'), 

147 using(this), Punctuation), 

148 'function'), 

149 # function declarations 

150 (r'(' + _namespaced_ident + r'(?:[&*\s])+)' # return arguments 

151 r'(' + _possible_comments + r')' 

152 r'(' + _namespaced_ident + r')' # method name 

153 r'(' + _possible_comments + r')' 

154 r'(\([^;"\')]*?\))' # signature 

155 r'(' + _possible_comments + r')' 

156 r'([^;/"\']*)(;)', 

157 bygroups(using(this), using(this, state='whitespace'), 

158 Name.Function, using(this, state='whitespace'), 

159 using(this), using(this, state='whitespace'), 

160 using(this), Punctuation)), 

161 include('types'), 

162 default('statement'), 

163 ], 

164 'statement': [ 

165 include('whitespace'), 

166 include('statements'), 

167 (r'\}', Punctuation), 

168 (r'[{;]', Punctuation, '#pop'), 

169 ], 

170 'function': [ 

171 include('whitespace'), 

172 include('statements'), 

173 (';', Punctuation), 

174 (r'\{', Punctuation, '#push'), 

175 (r'\}', Punctuation, '#pop'), 

176 ], 

177 'string': [ 

178 (r'"', String, '#pop'), 

179 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|' 

180 r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape), 

181 (r'[^\\"\n]+', String), # all other characters 

182 (r'\\\n', String), # line continuation 

183 (r'\\', String), # stray backslash 

184 ], 

185 'macro': [ 

186 (r'('+_ws1+r')(include)('+_ws1+r')("[^"]+")([^\n]*)', 

187 bygroups(using(this), Comment.Preproc, using(this), 

188 Comment.PreprocFile, Comment.Single)), 

189 (r'('+_ws1+r')(include)('+_ws1+r')(<[^>]+>)([^\n]*)', 

190 bygroups(using(this), Comment.Preproc, using(this), 

191 Comment.PreprocFile, Comment.Single)), 

192 (r'[^/\n]+', Comment.Preproc), 

193 (r'/[*](.|\n)*?[*]/', Comment.Multiline), 

194 (r'//.*?\n', Comment.Single, '#pop'), 

195 (r'/', Comment.Preproc), 

196 (r'(?<=\\)\n', Comment.Preproc), 

197 (r'\n', Comment.Preproc, '#pop'), 

198 ], 

199 'if0': [ 

200 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), 

201 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'), 

202 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), 

203 (r'.*?\n', Comment), 

204 ], 

205 'classname': [ 

206 (_ident, Name.Class, '#pop'), 

207 # template specification 

208 (r'\s*(?=>)', Text, '#pop'), 

209 default('#pop') 

210 ], 

211 # Mark identifiers preceded by `case` keyword as constants. 

212 'case-value': [ 

213 (r'(?<!:)(:)(?!:)', Punctuation, '#pop'), 

214 (_ident, Name.Constant), 

215 include('whitespace'), 

216 include('statements'), 

217 ] 

218 } 

219 

220 stdlib_types = { 

221 'size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 'sig_atomic_t', 'fpos_t', 

222 'clock_t', 'time_t', 'va_list', 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', 

223 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'} 

224 c99_types = { 

225 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t', 

226 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 'int_least16_t', 

227 'int_least32_t', 'int_least64_t', 'uint_least8_t', 'uint_least16_t', 

228 'uint_least32_t', 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t', 

229 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 'uint_fast64_t', 

230 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'} 

231 linux_types = { 

232 'clockid_t', 'cpu_set_t', 'cpumask_t', 'dev_t', 'gid_t', 'id_t', 'ino_t', 'key_t', 

233 'mode_t', 'nfds_t', 'pid_t', 'rlim_t', 'sig_t', 'sighandler_t', 'siginfo_t', 

234 'sigset_t', 'sigval_t', 'socklen_t', 'timer_t', 'uid_t'} 

235 c11_atomic_types = { 

236 'atomic_bool', 'atomic_char', 'atomic_schar', 'atomic_uchar', 'atomic_short', 

237 'atomic_ushort', 'atomic_int', 'atomic_uint', 'atomic_long', 'atomic_ulong', 

238 'atomic_llong', 'atomic_ullong', 'atomic_char16_t', 'atomic_char32_t', 'atomic_wchar_t', 

239 'atomic_int_least8_t', 'atomic_uint_least8_t', 'atomic_int_least16_t', 

240 'atomic_uint_least16_t', 'atomic_int_least32_t', 'atomic_uint_least32_t', 

241 'atomic_int_least64_t', 'atomic_uint_least64_t', 'atomic_int_fast8_t', 

242 'atomic_uint_fast8_t', 'atomic_int_fast16_t', 'atomic_uint_fast16_t', 

243 'atomic_int_fast32_t', 'atomic_uint_fast32_t', 'atomic_int_fast64_t', 

244 'atomic_uint_fast64_t', 'atomic_intptr_t', 'atomic_uintptr_t', 'atomic_size_t', 

245 'atomic_ptrdiff_t', 'atomic_intmax_t', 'atomic_uintmax_t'} 

246 

247 def __init__(self, **options): 

248 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True) 

249 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True) 

250 self.c11highlighting = get_bool_opt(options, 'c11highlighting', True) 

251 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True) 

252 RegexLexer.__init__(self, **options) 

253 

254 def get_tokens_unprocessed(self, text, stack=('root',)): 

255 for index, token, value in \ 

256 RegexLexer.get_tokens_unprocessed(self, text, stack): 

257 if token is Name: 

258 if self.stdlibhighlighting and value in self.stdlib_types: 

259 token = Keyword.Type 

260 elif self.c99highlighting and value in self.c99_types: 

261 token = Keyword.Type 

262 elif self.c11highlighting and value in self.c11_atomic_types: 

263 token = Keyword.Type 

264 elif self.platformhighlighting and value in self.linux_types: 

265 token = Keyword.Type 

266 yield index, token, value 

267 

268 

269class CLexer(CFamilyLexer): 

270 """ 

271 For C source code with preprocessor directives. 

272 

273 Additional options accepted: 

274 

275 `stdlibhighlighting` 

276 Highlight common types found in the C/C++ standard library (e.g. `size_t`). 

277 (default: ``True``). 

278 

279 `c99highlighting` 

280 Highlight common types found in the C99 standard library (e.g. `int8_t`). 

281 Actually, this includes all fixed-width integer types. 

282 (default: ``True``). 

283 

284 `c11highlighting` 

285 Highlight atomic types found in the C11 standard library (e.g. `atomic_bool`). 

286 (default: ``True``). 

287 

288 `platformhighlighting` 

289 Highlight common types found in the platform SDK headers (e.g. `clockid_t` on Linux). 

290 (default: ``True``). 

291 """ 

292 name = 'C' 

293 aliases = ['c'] 

294 filenames = ['*.c', '*.h', '*.idc', '*.x[bp]m'] 

295 mimetypes = ['text/x-chdr', 'text/x-csrc', 'image/x-xbitmap', 'image/x-xpixmap'] 

296 priority = 0.1 

297 

298 tokens = { 

299 'keywords': [ 

300 (words(( 

301 '_Alignas', '_Alignof', '_Noreturn', '_Generic', '_Thread_local', 

302 '_Static_assert', '_Imaginary', 'noreturn', 'imaginary', 'complex'), 

303 suffix=r'\b'), Keyword), 

304 inherit 

305 ], 

306 'types': [ 

307 (words(('_Bool', '_Complex', '_Atomic'), suffix=r'\b'), Keyword.Type), 

308 inherit 

309 ] 

310 } 

311 

312 def analyse_text(text): 

313 if re.search(r'^\s*#include [<"]', text, re.MULTILINE): 

314 return 0.1 

315 if re.search(r'^\s*#ifn?def ', text, re.MULTILINE): 

316 return 0.1 

317 

318 

319class CppLexer(CFamilyLexer): 

320 """ 

321 For C++ source code with preprocessor directives. 

322 

323 Additional options accepted: 

324 

325 `stdlibhighlighting` 

326 Highlight common types found in the C/C++ standard library (e.g. `size_t`). 

327 (default: ``True``). 

328 

329 `c99highlighting` 

330 Highlight common types found in the C99 standard library (e.g. `int8_t`). 

331 Actually, this includes all fixed-width integer types. 

332 (default: ``True``). 

333 

334 `c11highlighting` 

335 Highlight atomic types found in the C11 standard library (e.g. `atomic_bool`). 

336 (default: ``True``). 

337 

338 `platformhighlighting` 

339 Highlight common types found in the platform SDK headers (e.g. `clockid_t` on Linux). 

340 (default: ``True``). 

341 """ 

342 name = 'C++' 

343 url = 'https://isocpp.org/' 

344 aliases = ['cpp', 'c++'] 

345 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', 

346 '*.cc', '*.hh', '*.cxx', '*.hxx', 

347 '*.C', '*.H', '*.cp', '*.CPP', '*.tpp'] 

348 mimetypes = ['text/x-c++hdr', 'text/x-c++src'] 

349 priority = 0.1 

350 

351 tokens = { 

352 'statements': [ 

353 # C++11 raw strings 

354 (r'((?:[LuU]|u8)?R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")', 

355 bygroups(String.Affix, String, String.Delimiter, String.Delimiter, 

356 String, String.Delimiter, String)), 

357 inherit, 

358 ], 

359 'root': [ 

360 inherit, 

361 # C++ Microsoft-isms 

362 (words(('virtual_inheritance', 'uuidof', 'super', 'single_inheritance', 

363 'multiple_inheritance', 'interface', 'event'), 

364 prefix=r'__', suffix=r'\b'), Keyword.Reserved), 

365 # Offload C++ extensions, http://offload.codeplay.com/ 

366 (r'__(offload|blockingoffload|outer)\b', Keyword.Pseudo), 

367 ], 

368 'enumname': [ 

369 include('whitespace'), 

370 # 'enum class' and 'enum struct' C++11 support 

371 (words(('class', 'struct'), suffix=r'\b'), Keyword), 

372 (CFamilyLexer._ident, Name.Class, '#pop'), 

373 # template specification 

374 (r'\s*(?=>)', Text, '#pop'), 

375 default('#pop') 

376 ], 

377 'keywords': [ 

378 (r'(class|concept|typename)(\s+)', bygroups(Keyword, Whitespace), 'classname'), 

379 (words(( 

380 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit', 

381 'export', 'friend', 'mutable', 'new', 'operator', 

382 'private', 'protected', 'public', 'reinterpret_cast', 'class', 

383 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws', 

384 'try', 'typeid', 'using', 'virtual', 'constexpr', 'nullptr', 'concept', 

385 'decltype', 'noexcept', 'override', 'final', 'constinit', 'consteval', 

386 'co_await', 'co_return', 'co_yield', 'requires', 'import', 'module', 

387 'typename'), 

388 suffix=r'\b'), Keyword), 

389 (r'namespace\b', Keyword, 'namespace'), 

390 (r'(enum)(\s+)', bygroups(Keyword, Whitespace), 'enumname'), 

391 inherit 

392 ], 

393 'types': [ 

394 (r'char(16_t|32_t|8_t)\b', Keyword.Type), 

395 inherit 

396 ], 

397 'namespace': [ 

398 (r'[;{]', Punctuation, ('#pop', 'root')), 

399 (r'inline\b', Keyword.Reserved), 

400 (CFamilyLexer._ident, Name.Namespace), 

401 include('statement') 

402 ] 

403 } 

404 

405 def analyse_text(text): 

406 if re.search('#include <[a-z_]+>', text): 

407 return 0.2 

408 if re.search('using namespace ', text): 

409 return 0.4