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