source: node_modules/highlight.js/lib/languages/c.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/**
2 * @param {string} value
3 * @returns {RegExp}
4 * */
5
6/**
7 * @param {RegExp | string } re
8 * @returns {string}
9 */
10function source(re) {
11 if (!re) return null;
12 if (typeof re === "string") return re;
13
14 return re.source;
15}
16
17/**
18 * @param {RegExp | string } re
19 * @returns {string}
20 */
21function optional(re) {
22 return concat('(', re, ')?');
23}
24
25/**
26 * @param {...(RegExp | string) } args
27 * @returns {string}
28 */
29function concat(...args) {
30 const joined = args.map((x) => source(x)).join("");
31 return joined;
32}
33
34/*
35Language: C
36Category: common, system
37Website: https://en.wikipedia.org/wiki/C_(programming_language)
38*/
39
40/** @type LanguageFn */
41function c(hljs) {
42 // added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does
43 // not include such support nor can we be sure all the grammars depending
44 // on it would desire this behavior
45 const C_LINE_COMMENT_MODE = hljs.COMMENT('//', '$', {
46 contains: [
47 {
48 begin: /\\\n/
49 }
50 ]
51 });
52 const DECLTYPE_AUTO_RE = 'decltype\\(auto\\)';
53 const NAMESPACE_RE = '[a-zA-Z_]\\w*::';
54 const TEMPLATE_ARGUMENT_RE = '<[^<>]+>';
55 const FUNCTION_TYPE_RE = '(' +
56 DECLTYPE_AUTO_RE + '|' +
57 optional(NAMESPACE_RE) +
58 '[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
59 ')';
60 const CPP_PRIMITIVE_TYPES = {
61 className: 'keyword',
62 begin: '\\b[a-z\\d_]*_t\\b'
63 };
64
65 // https://en.cppreference.com/w/cpp/language/escape
66 // \\ \x \xFF \u2837 \u00323747 \374
67 const CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
68 const STRINGS = {
69 className: 'string',
70 variants: [
71 {
72 begin: '(u8?|U|L)?"',
73 end: '"',
74 illegal: '\\n',
75 contains: [ hljs.BACKSLASH_ESCAPE ]
76 },
77 {
78 begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)",
79 end: '\'',
80 illegal: '.'
81 },
82 hljs.END_SAME_AS_BEGIN({
83 begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,
84 end: /\)([^()\\ ]{0,16})"/
85 })
86 ]
87 };
88
89 const NUMBERS = {
90 className: 'number',
91 variants: [
92 {
93 begin: '\\b(0b[01\']+)'
94 },
95 {
96 begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)'
97 },
98 {
99 begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)'
100 }
101 ],
102 relevance: 0
103 };
104
105 const PREPROCESSOR = {
106 className: 'meta',
107 begin: /#\s*[a-z]+\b/,
108 end: /$/,
109 keywords: {
110 'meta-keyword':
111 'if else elif endif define undef warning error line ' +
112 'pragma _Pragma ifdef ifndef include'
113 },
114 contains: [
115 {
116 begin: /\\\n/,
117 relevance: 0
118 },
119 hljs.inherit(STRINGS, {
120 className: 'meta-string'
121 }),
122 {
123 className: 'meta-string',
124 begin: /<.*?>/
125 },
126 C_LINE_COMMENT_MODE,
127 hljs.C_BLOCK_COMMENT_MODE
128 ]
129 };
130
131 const TITLE_MODE = {
132 className: 'title',
133 begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
134 relevance: 0
135 };
136
137 const FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
138
139 const CPP_KEYWORDS = {
140 keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
141 'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
142 'unsigned long volatile static protected bool template mutable if public friend ' +
143 'do goto auto void enum else break extern using asm case typeid wchar_t ' +
144 'short reinterpret_cast|10 default double register explicit signed typename try this ' +
145 'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
146 'concept co_await co_return co_yield requires ' +
147 'noexcept static_assert thread_local restrict final override ' +
148 'atomic_bool atomic_char atomic_schar ' +
149 'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
150 'atomic_ullong new throw return ' +
151 'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
152 built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
153 'auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set ' +
154 'unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos ' +
155 'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
156 'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
157 'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
158 'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
159 'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
160 'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
161 literal: 'true false nullptr NULL'
162 };
163
164 const EXPRESSION_CONTAINS = [
165 PREPROCESSOR,
166 CPP_PRIMITIVE_TYPES,
167 C_LINE_COMMENT_MODE,
168 hljs.C_BLOCK_COMMENT_MODE,
169 NUMBERS,
170 STRINGS
171 ];
172
173 const EXPRESSION_CONTEXT = {
174 // This mode covers expression context where we can't expect a function
175 // definition and shouldn't highlight anything that looks like one:
176 // `return some()`, `else if()`, `(x*sum(1, 2))`
177 variants: [
178 {
179 begin: /=/,
180 end: /;/
181 },
182 {
183 begin: /\(/,
184 end: /\)/
185 },
186 {
187 beginKeywords: 'new throw return else',
188 end: /;/
189 }
190 ],
191 keywords: CPP_KEYWORDS,
192 contains: EXPRESSION_CONTAINS.concat([
193 {
194 begin: /\(/,
195 end: /\)/,
196 keywords: CPP_KEYWORDS,
197 contains: EXPRESSION_CONTAINS.concat([ 'self' ]),
198 relevance: 0
199 }
200 ]),
201 relevance: 0
202 };
203
204 const FUNCTION_DECLARATION = {
205 className: 'function',
206 begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
207 returnBegin: true,
208 end: /[{;=]/,
209 excludeEnd: true,
210 keywords: CPP_KEYWORDS,
211 illegal: /[^\w\s\*&:<>.]/,
212 contains: [
213 { // to prevent it from being confused as the function title
214 begin: DECLTYPE_AUTO_RE,
215 keywords: CPP_KEYWORDS,
216 relevance: 0
217 },
218 {
219 begin: FUNCTION_TITLE,
220 returnBegin: true,
221 contains: [ TITLE_MODE ],
222 relevance: 0
223 },
224 {
225 className: 'params',
226 begin: /\(/,
227 end: /\)/,
228 keywords: CPP_KEYWORDS,
229 relevance: 0,
230 contains: [
231 C_LINE_COMMENT_MODE,
232 hljs.C_BLOCK_COMMENT_MODE,
233 STRINGS,
234 NUMBERS,
235 CPP_PRIMITIVE_TYPES,
236 // Count matching parentheses.
237 {
238 begin: /\(/,
239 end: /\)/,
240 keywords: CPP_KEYWORDS,
241 relevance: 0,
242 contains: [
243 'self',
244 C_LINE_COMMENT_MODE,
245 hljs.C_BLOCK_COMMENT_MODE,
246 STRINGS,
247 NUMBERS,
248 CPP_PRIMITIVE_TYPES
249 ]
250 }
251 ]
252 },
253 CPP_PRIMITIVE_TYPES,
254 C_LINE_COMMENT_MODE,
255 hljs.C_BLOCK_COMMENT_MODE,
256 PREPROCESSOR
257 ]
258 };
259
260 return {
261 name: "C",
262 aliases: [
263 'h'
264 ],
265 keywords: CPP_KEYWORDS,
266 // Until differentiations are added between `c` and `cpp`, `c` will
267 // not be auto-detected to avoid auto-detect conflicts between C and C++
268 disableAutodetect: true,
269 illegal: '</',
270 contains: [].concat(
271 EXPRESSION_CONTEXT,
272 FUNCTION_DECLARATION,
273 EXPRESSION_CONTAINS,
274 [
275 PREPROCESSOR,
276 { // containers: ie, `vector <int> rooms (9);`
277 begin: '\\b(deque|list|queue|priority_queue|pair|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<',
278 end: '>',
279 keywords: CPP_KEYWORDS,
280 contains: [
281 'self',
282 CPP_PRIMITIVE_TYPES
283 ]
284 },
285 {
286 begin: hljs.IDENT_RE + '::',
287 keywords: CPP_KEYWORDS
288 },
289 {
290 className: 'class',
291 beginKeywords: 'enum class struct union',
292 end: /[{;:<>=]/,
293 contains: [
294 {
295 beginKeywords: "final class struct"
296 },
297 hljs.TITLE_MODE
298 ]
299 }
300 ]),
301 exports: {
302 preprocessor: PREPROCESSOR,
303 strings: STRINGS,
304 keywords: CPP_KEYWORDS
305 }
306 };
307}
308
309module.exports = c;
Note: See TracBrowser for help on using the repository browser.