eric6/ThirdParty/Pygments/pygments/lexers/c_cpp.py

changeset 6942
2602857055c5
parent 6651
e8f3b5568b21
child 7547
21b0534faebc
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.c_cpp
4 ~~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for C/C++ languages.
7
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13
14 from pygments.lexer import RegexLexer, include, bygroups, using, \
15 this, inherit, default, words
16 from pygments.util import get_bool_opt
17 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
18 Number, Punctuation, Error
19
20 __all__ = ['CLexer', 'CppLexer']
21
22
23 class 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 tokens = {
37 'whitespace': [
38 # preprocessor directives: without whitespace
39 (r'^#if\s+0', Comment.Preproc, 'if0'),
40 ('^#', Comment.Preproc, 'macro'),
41 # or with whitespace
42 ('^(' + _ws1 + r')(#if\s+0)',
43 bygroups(using(this), Comment.Preproc), 'if0'),
44 ('^(' + _ws1 + ')(#)',
45 bygroups(using(this), Comment.Preproc), 'macro'),
46 (r'\n', Text),
47 (r'\s+', Text),
48 (r'\\\n', Text), # line continuation
49 (r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single),
50 (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline),
51 # Open until EOF, so no ending delimeter
52 (r'/(\\\n)?[*][\w\W]*', Comment.Multiline),
53 ],
54 'statements': [
55 (r'(L?)(")', bygroups(String.Affix, String), 'string'),
56 (r"(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')",
57 bygroups(String.Affix, String.Char, String.Char, String.Char)),
58 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float),
59 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
60 (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex),
61 (r'0[0-7]+[LlUu]*', Number.Oct),
62 (r'\d+[LlUu]*', Number.Integer),
63 (r'\*/', Error),
64 (r'[~!%^&*+=|?:<>/-]', Operator),
65 (r'[()\[\],.]', Punctuation),
66 (words(('asm', 'auto', 'break', 'case', 'const', 'continue',
67 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto',
68 'if', 'register', 'restricted', 'return', 'sizeof',
69 'static', 'struct', 'switch', 'typedef', 'union',
70 'volatile', 'while'),
71 suffix=r'\b'), Keyword),
72 (r'(bool|int|long|float|short|double|char|unsigned|signed|void)\b',
73 Keyword.Type),
74 (words(('inline', '_inline', '__inline', 'naked', 'restrict',
75 'thread', 'typename'), suffix=r'\b'), Keyword.Reserved),
76 # Vector intrinsics
77 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved),
78 # Microsoft-isms
79 (words((
80 'asm', 'int8', 'based', 'except', 'int16', 'stdcall', 'cdecl',
81 'fastcall', 'int32', 'declspec', 'finally', 'int64', 'try',
82 'leave', 'wchar_t', 'w64', 'unaligned', 'raise', 'noop',
83 'identifier', 'forceinline', 'assume'),
84 prefix=r'__', suffix=r'\b'), Keyword.Reserved),
85 (r'(true|false|NULL)\b', Name.Builtin),
86 (r'([a-zA-Z_]\w*)(\s*)(:)(?!:)', bygroups(Name.Label, Text, Punctuation)),
87 (r'[a-zA-Z_]\w*', Name),
88 ],
89 'root': [
90 include('whitespace'),
91 # functions
92 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments
93 r'([a-zA-Z_]\w*)' # method name
94 r'(\s*\([^;]*?\))' # signature
95 r'([^;{]*)(\{)',
96 bygroups(using(this), Name.Function, using(this), using(this),
97 Punctuation),
98 'function'),
99 # function declarations
100 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments
101 r'([a-zA-Z_]\w*)' # method name
102 r'(\s*\([^;]*?\))' # signature
103 r'([^;]*)(;)',
104 bygroups(using(this), Name.Function, using(this), using(this),
105 Punctuation)),
106 default('statement'),
107 ],
108 'statement': [
109 include('whitespace'),
110 include('statements'),
111 ('[{}]', Punctuation),
112 (';', Punctuation, '#pop'),
113 ],
114 'function': [
115 include('whitespace'),
116 include('statements'),
117 (';', Punctuation),
118 (r'\{', Punctuation, '#push'),
119 (r'\}', Punctuation, '#pop'),
120 ],
121 'string': [
122 (r'"', String, '#pop'),
123 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|'
124 r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape),
125 (r'[^\\"\n]+', String), # all other characters
126 (r'\\\n', String), # line continuation
127 (r'\\', String), # stray backslash
128 ],
129 'macro': [
130 (r'(include)(' + _ws1 + r')([^\n]+)',
131 bygroups(Comment.Preproc, Text, Comment.PreprocFile)),
132 (r'[^/\n]+', Comment.Preproc),
133 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
134 (r'//.*?\n', Comment.Single, '#pop'),
135 (r'/', Comment.Preproc),
136 (r'(?<=\\)\n', Comment.Preproc),
137 (r'\n', Comment.Preproc, '#pop'),
138 ],
139 'if0': [
140 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
141 (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'),
142 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
143 (r'.*?\n', Comment),
144 ]
145 }
146
147 stdlib_types = set((
148 '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',
150 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'))
151 c99_types = set((
152 '_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t',
153 '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',
155 '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',
157 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'))
158 linux_types = set((
159 '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',
161 'sigset_t', 'sigval_t', 'socklen_t', 'timer_t', 'uid_t'))
162
163 def __init__(self, **options):
164 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True)
165 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True)
166 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True)
167 RegexLexer.__init__(self, **options)
168
169 def get_tokens_unprocessed(self, text):
170 for index, token, value in \
171 RegexLexer.get_tokens_unprocessed(self, text):
172 if token is Name:
173 if self.stdlibhighlighting and value in self.stdlib_types:
174 token = Keyword.Type
175 elif self.c99highlighting and value in self.c99_types:
176 token = Keyword.Type
177 elif self.platformhighlighting and value in self.linux_types:
178 token = Keyword.Type
179 yield index, token, value
180
181
182 class CLexer(CFamilyLexer):
183 """
184 For C source code with preprocessor directives.
185 """
186 name = 'C'
187 aliases = ['c']
188 filenames = ['*.c', '*.h', '*.idc']
189 mimetypes = ['text/x-chdr', 'text/x-csrc']
190 priority = 0.1
191
192 def analyse_text(text):
193 if re.search(r'^\s*#include [<"]', text, re.MULTILINE):
194 return 0.1
195 if re.search(r'^\s*#ifn?def ', text, re.MULTILINE):
196 return 0.1
197
198
199 class CppLexer(CFamilyLexer):
200 """
201 For C++ source code with preprocessor directives.
202 """
203 name = 'C++'
204 aliases = ['cpp', 'c++']
205 filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++',
206 '*.cc', '*.hh', '*.cxx', '*.hxx',
207 '*.C', '*.H', '*.cp', '*.CPP']
208 mimetypes = ['text/x-c++hdr', 'text/x-c++src']
209 priority = 0.1
210
211 tokens = {
212 'statements': [
213 (words((
214 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit',
215 'export', 'friend', 'mutable', 'namespace', 'new', 'operator',
216 'private', 'protected', 'public', 'reinterpret_cast',
217 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws',
218 'try', 'typeid', 'typename', 'using', 'virtual',
219 'constexpr', 'nullptr', 'decltype', 'thread_local',
220 'alignas', 'alignof', 'static_assert', 'noexcept', 'override',
221 'final'), suffix=r'\b'), Keyword),
222 (r'char(16_t|32_t)\b', Keyword.Type),
223 (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
224 # C++11 raw strings
225 (r'(R)(")([^\\()\s]{,16})(\()((?:.|\n)*?)(\)\3)(")',
226 bygroups(String.Affix, String, String.Delimiter, String.Delimiter,
227 String, String.Delimiter, String)),
228 # C++11 UTF-8/16/32 strings
229 (r'(u8|u|U)(")', bygroups(String.Affix, String), 'string'),
230 inherit,
231 ],
232 'root': [
233 inherit,
234 # C++ Microsoft-isms
235 (words(('virtual_inheritance', 'uuidof', 'super', 'single_inheritance',
236 'multiple_inheritance', 'interface', 'event'),
237 prefix=r'__', suffix=r'\b'), Keyword.Reserved),
238 # Offload C++ extensions, http://offload.codeplay.com/
239 (r'__(offload|blockingoffload|outer)\b', Keyword.Pseudo),
240 ],
241 'classname': [
242 (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
243 # template specification
244 (r'\s*(?=>)', Text, '#pop'),
245 ],
246 }
247
248 def analyse_text(text):
249 if re.search('#include <[a-z_]+>', text):
250 return 0.2
251 if re.search('using namespace ', text):
252 return 0.4

eric ide

mercurial