ThirdParty/Pygments/pygments/lexers/c_cpp.py

changeset 4697
c2e9bf425554
parent 4172
4f20dba37ab6
child 5072
aab59042fefb
equal deleted inserted replaced
4696:bf4d19a7cade 4697:c2e9bf425554
3 pygments.lexers.c_cpp 3 pygments.lexers.c_cpp
4 ~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~
5 5
6 Lexers for C/C++ languages. 6 Lexers for C/C++ languages.
7 7
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import re 12 import re
13 13
26 definitions. 26 definitions.
27 """ 27 """
28 28
29 #: optional Comment or Whitespace 29 #: optional Comment or Whitespace
30 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' 30 _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
31
32 # The trailing ?, rather than *, avoids a geometric performance drop here.
31 #: only one /* */ style comment 33 #: only one /* */ style comment
32 _ws1 = r'\s*(?:/[*].*?[*]/\s*)*' 34 _ws1 = r'\s*(?:/[*].*?[*]/\s*)?'
33 35
34 tokens = { 36 tokens = {
35 'whitespace': [ 37 'whitespace': [
36 # preprocessor directives: without whitespace 38 # preprocessor directives: without whitespace
37 ('^#if\s+0', Comment.Preproc, 'if0'), 39 ('^#if\s+0', Comment.Preproc, 'if0'),
61 (words(('auto', 'break', 'case', 'const', 'continue', 'default', 'do', 63 (words(('auto', 'break', 'case', 'const', 'continue', 'default', 'do',
62 'else', 'enum', 'extern', 'for', 'goto', 'if', 'register', 64 'else', 'enum', 'extern', 'for', 'goto', 'if', 'register',
63 'restricted', 'return', 'sizeof', 'static', 'struct', 65 'restricted', 'return', 'sizeof', 'static', 'struct',
64 'switch', 'typedef', 'union', 'volatile', 'while'), 66 'switch', 'typedef', 'union', 'volatile', 'while'),
65 suffix=r'\b'), Keyword), 67 suffix=r'\b'), Keyword),
66 (r'(bool|int|long|float|short|double|char|unsigned|signed|void|' 68 (r'(bool|int|long|float|short|double|char|unsigned|signed|void)\b',
67 r'[a-z_][a-z0-9_]*_t)\b',
68 Keyword.Type), 69 Keyword.Type),
69 (words(('inline', '_inline', '__inline', 'naked', 'restrict', 70 (words(('inline', '_inline', '__inline', 'naked', 'restrict',
70 'thread', 'typename'), suffix=r'\b'), Keyword.Reserved), 71 'thread', 'typename'), suffix=r'\b'), Keyword.Reserved),
71 # Vector intrinsics 72 # Vector intrinsics
72 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved), 73 (r'(__m(128i|128d|128|64))\b', Keyword.Reserved),
85 include('whitespace'), 86 include('whitespace'),
86 # functions 87 # functions
87 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments 88 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments
88 r'([a-zA-Z_]\w*)' # method name 89 r'([a-zA-Z_]\w*)' # method name
89 r'(\s*\([^;]*?\))' # signature 90 r'(\s*\([^;]*?\))' # signature
90 r'(' + _ws + r')?(\{)', 91 r'([^;{]*)(\{)',
91 bygroups(using(this), Name.Function, using(this), using(this), 92 bygroups(using(this), Name.Function, using(this), using(this),
92 Punctuation), 93 Punctuation),
93 'function'), 94 'function'),
94 # function declarations 95 # function declarations
95 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments 96 (r'((?:[\w*\s])+?(?:\s|[*]))' # return arguments
96 r'([a-zA-Z_]\w*)' # method name 97 r'([a-zA-Z_]\w*)' # method name
97 r'(\s*\([^;]*?\))' # signature 98 r'(\s*\([^;]*?\))' # signature
98 r'(' + _ws + r')?(;)', 99 r'([^;]*)(;)',
99 bygroups(using(this), Name.Function, using(this), using(this), 100 bygroups(using(this), Name.Function, using(this), using(this),
100 Punctuation)), 101 Punctuation)),
101 default('statement'), 102 default('statement'),
102 ], 103 ],
103 'statement': [ 104 'statement': [
120 (r'[^\\"\n]+', String), # all other characters 121 (r'[^\\"\n]+', String), # all other characters
121 (r'\\\n', String), # line continuation 122 (r'\\\n', String), # line continuation
122 (r'\\', String), # stray backslash 123 (r'\\', String), # stray backslash
123 ], 124 ],
124 'macro': [ 125 'macro': [
126 (r'(include)(' + _ws1 + ')([^\n]+)', bygroups(Comment.Preproc, Text, Comment.PreprocFile)),
125 (r'[^/\n]+', Comment.Preproc), 127 (r'[^/\n]+', Comment.Preproc),
126 (r'/[*](.|\n)*?[*]/', Comment.Multiline), 128 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
127 (r'//.*?\n', Comment.Single, '#pop'), 129 (r'//.*?\n', Comment.Single, '#pop'),
128 (r'/', Comment.Preproc), 130 (r'/', Comment.Preproc),
129 (r'(?<=\\)\n', Comment.Preproc), 131 (r'(?<=\\)\n', Comment.Preproc),
135 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'), 137 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
136 (r'.*?\n', Comment), 138 (r'.*?\n', Comment),
137 ] 139 ]
138 } 140 }
139 141
140 stdlib_types = ['size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 142 stdlib_types = set((
141 'sig_atomic_t', 'fpos_t', 'clock_t', 'time_t', 'va_list', 143 'size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t', 'sig_atomic_t', 'fpos_t',
142 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', 'mbstate_t', 144 'clock_t', 'time_t', 'va_list', 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t',
143 'wctrans_t', 'wint_t', 'wctype_t'] 145 'mbstate_t', 'wctrans_t', 'wint_t', 'wctype_t'))
144 c99_types = ['_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t', 146 c99_types = set((
145 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 147 '_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'uint8_t',
146 'int_least16_t', 'int_least32_t', 'int_least64_t', 148 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t', 'int_least16_t',
147 'uint_least8_t', 'uint_least16_t', 'uint_least32_t', 149 'int_least32_t', 'int_least64_t', 'uint_least8_t', 'uint_least16_t',
148 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t', 150 'uint_least32_t', 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t',
149 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 151 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t', 'uint_fast64_t',
150 'uint_fast64_t', 'intptr_t', 'uintptr_t', 'intmax_t', 152 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t'))
151 'uintmax_t'] 153 linux_types = set((
154 'clockid_t', 'cpu_set_t', 'cpumask_t', 'dev_t', 'gid_t', 'id_t', 'ino_t', 'key_t',
155 'mode_t', 'nfds_t', 'pid_t', 'rlim_t', 'sig_t', 'sighandler_t', 'siginfo_t',
156 'sigset_t', 'sigval_t', 'socklen_t', 'timer_t', 'uid_t'))
152 157
153 def __init__(self, **options): 158 def __init__(self, **options):
154 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True) 159 self.stdlibhighlighting = get_bool_opt(options, 'stdlibhighlighting', True)
155 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True) 160 self.c99highlighting = get_bool_opt(options, 'c99highlighting', True)
161 self.platformhighlighting = get_bool_opt(options, 'platformhighlighting', True)
156 RegexLexer.__init__(self, **options) 162 RegexLexer.__init__(self, **options)
157 163
158 def get_tokens_unprocessed(self, text): 164 def get_tokens_unprocessed(self, text):
159 for index, token, value in \ 165 for index, token, value in \
160 RegexLexer.get_tokens_unprocessed(self, text): 166 RegexLexer.get_tokens_unprocessed(self, text):
161 if token is Name: 167 if token is Name:
162 if self.stdlibhighlighting and value in self.stdlib_types: 168 if self.stdlibhighlighting and value in self.stdlib_types:
163 token = Keyword.Type 169 token = Keyword.Type
164 elif self.c99highlighting and value in self.c99_types: 170 elif self.c99highlighting and value in self.c99_types:
165 token = Keyword.Type 171 token = Keyword.Type
172 elif self.platformhighlighting and value in self.linux_types:
173 token = Keyword.Type
166 yield index, token, value 174 yield index, token, value
167 175
168 176
169 class CLexer(CFamilyLexer): 177 class CLexer(CFamilyLexer):
170 """ 178 """
177 priority = 0.1 185 priority = 0.1
178 186
179 def analyse_text(text): 187 def analyse_text(text):
180 if re.search('^\s*#include [<"]', text, re.MULTILINE): 188 if re.search('^\s*#include [<"]', text, re.MULTILINE):
181 return 0.1 189 return 0.1
182 if re.search('^\s*#ifdef ', text, re.MULTILINE): 190 if re.search('^\s*#ifn?def ', text, re.MULTILINE):
183 return 0.1 191 return 0.1
184 192
185 193
186 class CppLexer(CFamilyLexer): 194 class CppLexer(CFamilyLexer):
187 """ 195 """
200 (words(( 208 (words((
201 'asm', 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit', 209 'asm', 'catch', 'const_cast', 'delete', 'dynamic_cast', 'explicit',
202 'export', 'friend', 'mutable', 'namespace', 'new', 'operator', 210 'export', 'friend', 'mutable', 'namespace', 'new', 'operator',
203 'private', 'protected', 'public', 'reinterpret_cast', 211 'private', 'protected', 'public', 'reinterpret_cast',
204 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws', 212 'restrict', 'static_cast', 'template', 'this', 'throw', 'throws',
205 'typeid', 'typename', 'using', 'virtual', 213 'try', 'typeid', 'typename', 'using', 'virtual',
206 'constexpr', 'nullptr', 'decltype', 'thread_local', 214 'constexpr', 'nullptr', 'decltype', 'thread_local',
207 'alignas', 'alignof', 'static_assert', 'noexcept', 'override', 215 'alignas', 'alignof', 'static_assert', 'noexcept', 'override',
208 'final'), suffix=r'\b'), Keyword), 216 'final'), suffix=r'\b'), Keyword),
209 (r'char(16_t|32_t)\b', Keyword.Type), 217 (r'char(16_t|32_t)\b', Keyword.Type),
210 (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'), 218 (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),

eric ide

mercurial