50 (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline), |
62 (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline), |
51 # Open until EOF, so no ending delimeter |
63 # Open until EOF, so no ending delimeter |
52 (r'/(\\\n)?[*][\w\W]*', Comment.Multiline), |
64 (r'/(\\\n)?[*][\w\W]*', Comment.Multiline), |
53 ], |
65 ], |
54 'statements': [ |
66 'statements': [ |
55 (r'(L?)(")', bygroups(String.Affix, String), 'string'), |
67 (r'([LuU]|u8)?(")', bygroups(String.Affix, String), 'string'), |
56 (r"(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')", |
68 (r"([LuU]|u8)?(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')", |
57 bygroups(String.Affix, String.Char, String.Char, String.Char)), |
69 bygroups(String.Affix, String.Char, String.Char, String.Char)), |
58 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float), |
70 |
59 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), |
71 # Hexadecimal floating-point literals (C11, C++17) |
60 (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex), |
72 (r'0[xX](' + _hexpart + r'\.' + _hexpart + r'|\.' + _hexpart + r'|' + _hexpart + r')[pP][+-]?' + _hexpart + r'[lL]?', Number.Float), |
61 (r'0[0-7]+[LlUu]*', Number.Oct), |
73 |
62 (r'\d+[LlUu]*', Number.Integer), |
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), |
63 (r'\*/', Error), |
80 (r'\*/', Error), |
64 (r'[~!%^&*+=|?:<>/-]', Operator), |
81 (r'[~!%^&*+=|?:<>/-]', Operator), |
65 (r'[()\[\],.]', Punctuation), |
82 (r'[()\[\],.]', Punctuation), |
|
83 (r'(struct|union)(\s+)', bygroups(Keyword, Text), 'classname'), |
66 (words(('asm', 'auto', 'break', 'case', 'const', 'continue', |
84 (words(('asm', 'auto', 'break', 'case', 'const', 'continue', |
67 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto', |
85 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto', |
68 'if', 'register', 'restricted', 'return', 'sizeof', |
86 'if', 'register', 'restricted', 'return', 'sizeof', 'struct', |
69 'static', 'struct', 'switch', 'typedef', 'union', |
87 'static', 'switch', 'typedef', 'volatile', 'while', 'union', |
70 'volatile', 'while'), |
88 'thread_local', 'alignas', 'alignof', 'static_assert', '_Pragma'), |
71 suffix=r'\b'), Keyword), |
89 suffix=r'\b'), Keyword), |
72 (r'(bool|int|long|float|short|double|char|unsigned|signed|void)\b', |
90 (r'(bool|int|long|float|short|double|char|unsigned|signed|void)\b', |
73 Keyword.Type), |
91 Keyword.Type), |
74 (words(('inline', '_inline', '__inline', 'naked', 'restrict', |
92 (words(('inline', '_inline', '__inline', 'naked', 'restrict', |
75 'thread', 'typename'), suffix=r'\b'), Keyword.Reserved), |
93 'thread'), suffix=r'\b'), Keyword.Reserved), |
76 # Vector intrinsics |
94 # Vector intrinsics |
77 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved), |
95 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved), |
78 # Microsoft-isms |
96 # Microsoft-isms |
79 (words(( |
97 (words(( |
80 'asm', 'int8', 'based', 'except', 'int16', 'stdcall', 'cdecl', |
98 'asm', 'int8', 'based', 'except', 'int16', 'stdcall', 'cdecl', |
81 'fastcall', 'int32', 'declspec', 'finally', 'int64', 'try', |
99 'fastcall', 'int32', 'declspec', 'finally', 'int64', 'try', |
82 'leave', 'wchar_t', 'w64', 'unaligned', 'raise', 'noop', |
100 'leave', 'wchar_t', 'w64', 'unaligned', 'raise', 'noop', |
83 'identifier', 'forceinline', 'assume'), |
101 'identifier', 'forceinline', 'assume'), |
84 prefix=r'__', suffix=r'\b'), Keyword.Reserved), |
102 prefix=r'__', suffix=r'\b'), Keyword.Reserved), |
85 (r'(true|false|NULL)\b', Name.Builtin), |
103 (r'(true|false|NULL)\b', Name.Builtin), |
86 (r'([a-zA-Z_]\w*)(\s*)(:)(?!:)', bygroups(Name.Label, Text, Punctuation)), |
104 (r'(' + _ident + r')(\s*)(:)(?!:)', bygroups(Name.Label, Text, Punctuation)), |
87 (r'[a-zA-Z_]\w*', Name), |
105 (_ident, Name) |
88 ], |
106 ], |
89 'root': [ |
107 'root': [ |
90 include('whitespace'), |
108 include('whitespace'), |
91 # functions |
109 # functions |
92 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments |
110 (r'((?:' + _ident + r'(?:[&*\s])+))' # return arguments |
93 r'([a-zA-Z_]\w*)' # method name |
111 r'(' + _ident + r')' # method name |
94 r'(\s*\([^;]*?\))' # signature |
112 r'(\s*\([^;]*?\))' # signature |
95 r'([^;{]*)(\{)', |
113 r'([^;{]*)(\{)', |
96 bygroups(using(this), Name.Function, using(this), using(this), |
114 bygroups(using(this), Name.Function, using(this), using(this), |
97 Punctuation), |
115 Punctuation), |
98 'function'), |
116 'function'), |
99 # function declarations |
117 # function declarations |
100 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments |
118 (r'((?:' + _ident + r'(?:[&*\s])+))' # return arguments |
101 r'([a-zA-Z_]\w*)' # method name |
119 r'(' + _ident + r')' # method name |
102 r'(\s*\([^;]*?\))' # signature |
120 r'(\s*\([^;]*?\))' # signature |
103 r'([^;]*)(;)', |
121 r'([^;]*)(;)', |
104 bygroups(using(this), Name.Function, using(this), using(this), |
122 bygroups(using(this), Name.Function, using(this), using(this), |
105 Punctuation)), |
123 Punctuation)), |
106 default('statement'), |
124 default('statement'), |
107 ], |
125 ], |
108 'statement': [ |
126 'statement': [ |
109 include('whitespace'), |
127 include('whitespace'), |
110 include('statements'), |
128 include('statements'), |
111 ('[{}]', Punctuation), |
129 (r'\}', Punctuation), |
112 (';', Punctuation, '#pop'), |
130 (r'[{;]', Punctuation, '#pop'), |
113 ], |
131 ], |
114 'function': [ |
132 'function': [ |
115 include('whitespace'), |
133 include('whitespace'), |
116 include('statements'), |
134 include('statements'), |
117 (';', Punctuation), |
135 (';', Punctuation), |
139 'if0': [ |
157 'if0': [ |
140 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), |
158 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'), |
141 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'), |
159 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'), |
142 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), |
160 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), |
143 (r'.*?\n', Comment), |
161 (r'.*?\n', Comment), |
|
162 ], |
|
163 'classname': [ |
|
164 (_ident, Name.Class, '#pop'), |
|
165 # template specification |
|
166 (r'\s*(?=>)', Text, '#pop'), |
|
167 default('#pop') |
144 ] |
168 ] |
145 } |
169 } |
146 |
170 |
147 stdlib_types = { |
171 stdlib_types = { |
148 'size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 'sig_atomic_t', 'fpos_t', |
172 'size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 'sig_atomic_t', 'fpos_t', |
149 'clock_t', 'time_t', 'va_list', 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', |
173 'clock_t', 'time_t', 'va_list', 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', |
150 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'} |
174 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'} |
151 c99_types = { |
175 c99_types = { |
152 '_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t', |
176 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t', |
153 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 'int_least16_t', |
177 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 'int_least16_t', |
154 'int_least32_t', 'int_least64_t', 'uint_least8_t', 'uint_least16_t', |
178 'int_least32_t', 'int_least64_t', 'uint_least8_t', 'uint_least16_t', |
155 'uint_least32_t', 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t', |
179 'uint_least32_t', 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t', |
156 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 'uint_fast64_t', |
180 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 'uint_fast64_t', |
157 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'} |
181 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'} |
158 linux_types = { |
182 linux_types = { |
159 'clockid_t', 'cpu_set_t', 'cpumask_t', 'dev_t', 'gid_t', 'id_t', 'ino_t', 'key_t', |
183 'clockid_t', 'cpu_set_t', 'cpumask_t', 'dev_t', 'gid_t', 'id_t', 'ino_t', 'key_t', |
160 'mode_t', 'nfds_t', 'pid_t', 'rlim_t', 'sig_t', 'sighandler_t', 'siginfo_t', |
184 'mode_t', 'nfds_t', 'pid_t', 'rlim_t', 'sig_t', 'sighandler_t', 'siginfo_t', |
161 'sigset_t', 'sigval_t', 'socklen_t', 'timer_t', 'uid_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'} |
162 |
197 |
163 def __init__(self, **options): |
198 def __init__(self, **options): |
164 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True) |
199 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True) |
165 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True) |
200 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True) |
|
201 self.c11highlighting = get_bool_opt(options, 'c11highlighting', True) |
166 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True) |
202 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True) |
167 RegexLexer.__init__(self, **options) |
203 RegexLexer.__init__(self, **options) |
168 |
204 |
169 def get_tokens_unprocessed(self, text): |
205 def get_tokens_unprocessed(self, text): |
170 for index, token, value in \ |
206 for index, token, value in \ |
172 if token is Name: |
208 if token is Name: |
173 if self.stdlibhighlighting and value in self.stdlib_types: |
209 if self.stdlibhighlighting and value in self.stdlib_types: |
174 token = Keyword.Type |
210 token = Keyword.Type |
175 elif self.c99highlighting and value in self.c99_types: |
211 elif self.c99highlighting and value in self.c99_types: |
176 token = Keyword.Type |
212 token = Keyword.Type |
|
213 elif self.c11highlighting and value in self.c11_atomic_types: |
|
214 token = Keyword.Type |
177 elif self.platformhighlighting and value in self.linux_types: |
215 elif self.platformhighlighting and value in self.linux_types: |
178 token = Keyword.Type |
216 token = Keyword.Type |
179 yield index, token, value |
217 yield index, token, value |
180 |
218 |
181 |
219 |
182 class CLexer(CFamilyLexer): |
220 class CLexer(CFamilyLexer): |
183 """ |
221 """ |
184 For C source code with preprocessor directives. |
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``). |
185 """ |
242 """ |
186 name = 'C' |
243 name = 'C' |
187 aliases = ['c'] |
244 aliases = ['c'] |
188 filenames = ['*.c', '*.h', '*.idc'] |
245 filenames = ['*.c', '*.h', '*.idc'] |
189 mimetypes = ['text/x-chdr', 'text/x-csrc'] |
246 mimetypes = ['text/x-chdr', 'text/x-csrc'] |
190 priority = 0.1 |
247 priority = 0.1 |
191 |
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 |
192 def analyse_text(text): |
260 def analyse_text(text): |
193 if re.search(r'^\s*#include [<"]', text, re.MULTILINE): |
261 if re.search(r'^\s*#include [<"]', text, re.MULTILINE): |
194 return 0.1 |
262 return 0.1 |
195 if re.search(r'^\s*#ifn?def ', text, re.MULTILINE): |
263 if re.search(r'^\s*#ifn?def ', text, re.MULTILINE): |
196 return 0.1 |
264 return 0.1 |
197 |
265 |
198 |
266 |
199 class CppLexer(CFamilyLexer): |
267 class CppLexer(CFamilyLexer): |
200 """ |
268 """ |
201 For C++ source code with preprocessor directives. |
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``). |
202 """ |
289 """ |
203 name = 'C++' |
290 name = 'C++' |
204 aliases = ['cpp', 'c++'] |
291 aliases = ['cpp', 'c++'] |
205 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', |
292 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', |
206 '*.cc', '*.hh', '*.cxx', '*.hxx', |
293 '*.cc', '*.hh', '*.cxx', '*.hxx', |
208 mimetypes = ['text/x-c++hdr', 'text/x-c++src'] |
295 mimetypes = ['text/x-c++hdr', 'text/x-c++src'] |
209 priority = 0.1 |
296 priority = 0.1 |
210 |
297 |
211 tokens = { |
298 tokens = { |
212 'statements': [ |
299 'statements': [ |
|
300 (r'(class|concept|typename)(\s+)', bygroups(Keyword, Text), 'classname'), |
213 (words(( |
301 (words(( |
214 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit', |
302 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit', |
215 'export', 'friend', 'mutable', 'namespace', 'new', 'operator', |
303 'export', 'friend', 'mutable', 'namespace', 'new', 'operator', |
216 'private', 'protected', 'public', 'reinterpret_cast', |
304 'private', 'protected', 'public', 'reinterpret_cast', 'class', |
217 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws', |
305 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws', |
218 'try', 'typeid', 'typename', 'using', 'virtual', |
306 'try', 'typeid', 'using', 'virtual', 'constexpr', 'nullptr', 'concept', |
219 'constexpr', 'nullptr', 'decltype', 'thread_local', |
307 'decltype', 'noexcept', 'override', 'final', 'constinit', 'consteval', |
220 'alignas', 'alignof', 'static_assert', 'noexcept', 'override', |
308 'co_await', 'co_return', 'co_yield', 'requires', 'import', 'module', |
221 'final', 'constinit', 'consteval', 'concept', 'co_await', |
309 'typename'), |
222 'co_return', 'co_yield', 'requires', 'import', 'module'), suffix=r'\b'), Keyword), |
310 suffix=r'\b'), Keyword), |
223 (r'char(16_t|32_t|8_t)\b', Keyword.Type), |
311 (r'char(16_t|32_t|8_t)\b', Keyword.Type), |
224 (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'), |
312 (r'(enum)(\s+)', bygroups(Keyword, Text), 'enumname'), |
|
313 |
225 # C++11 raw strings |
314 # C++11 raw strings |
226 (r'(R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")', |
315 (r'((?:[LuU]|u8)?R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")', |
227 bygroups(String.Affix, String, String.Delimiter, String.Delimiter, |
316 bygroups(String.Affix, String, String.Delimiter, String.Delimiter, |
228 String, String.Delimiter, String)), |
317 String, String.Delimiter, String)), |
229 # C++11 UTF-8/16/32 strings |
|
230 (r'(u8|u|U)(")', bygroups(String.Affix, String), 'string'), |
|
231 inherit, |
318 inherit, |
232 ], |
319 ], |
233 'root': [ |
320 'root': [ |
234 inherit, |
321 inherit, |
235 # C++ Microsoft-isms |
322 # C++ Microsoft-isms |