]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/c.js
1 /*! `c` grammar compiled for Highlight.js 11.11.1 */
3 var hljsGrammar
= (function () {
8 Category: common, system
9 Website: https://en.wikipedia.org/wiki/C_(programming_language)
12 /** @type LanguageFn */
14 const regex
= hljs
.regex
;
15 // added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does
16 // not include such support nor can we be sure all the grammars depending
17 // on it would desire this behavior
18 const C_LINE_COMMENT_MODE
= hljs
.COMMENT('//', '$', { contains: [ { begin: /\\\n/ } ] });
19 const DECLTYPE_AUTO_RE
= 'decltype\\(auto\\)';
20 const NAMESPACE_RE
= '[a-zA-Z_]\\w*::';
21 const TEMPLATE_ARGUMENT_RE
= '<[^<>]+>';
22 const FUNCTION_TYPE_RE
= '('
23 + DECLTYPE_AUTO_RE
+ '|'
24 + regex
.optional(NAMESPACE_RE
)
25 + '[a-zA-Z_]\\w*' + regex
.optional(TEMPLATE_ARGUMENT_RE
)
32 { begin: '\\b[a-z\\d_]*_t\\b' },
33 { match: /\batomic_[a-z]{3,6}\b/ }
38 // https://en.cppreference.com/w/cpp/language/escape
39 // \\ \x \xFF \u2837 \u00323747 \374
40 const CHARACTER_ESCAPES
= '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
48 contains: [ hljs
.BACKSLASH_ESCAPE
]
51 begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES
+ "|.)",
55 hljs
.END_SAME_AS_BEGIN({
56 begin: /(?:u8
?|U
|L
)?R
"([^()\\ ]{0,16})\(/,
57 end: /\)([^()\\ ]{0,16})"/
65 { match: /\b(0b[01']+)/ },
66 { match: /(-?)\b([\d']+(\.[\d']*)?|\.[\d']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)/ },
67 { match: /(-?)\b(0[xX][a-fA-F0-9]+(?:'[a-fA-F0-9]+)*(?:\.[a-fA-F0-9]*(?:'[a-fA-F0-9]*)*)?(?:[pP][-+]?[0-9]+)?(l|L)?(u|U)?)/ },
68 { match: /(-?)\b\d+(?:'\d+)*(?:\.\d*(?:'\d*)*)?(?:[eE][-+]?\d+)?/ }
73 const PREPROCESSOR
= {
75 begin: /#\s*[a-z]+\b/,
78 'if else elif endif define undef warning error line '
79 + 'pragma _Pragma ifdef ifndef elifdef elifndef include' },
85 hljs
.inherit(STRINGS
, { className: 'string' }),
91 hljs
.C_BLOCK_COMMENT_MODE
97 begin: regex
.optional(NAMESPACE_RE
) + hljs
.IDENT_RE
,
101 const FUNCTION_TITLE
= regex
.optional(NAMESPACE_RE
) + hljs
.IDENT_RE
+ '\\s*\\(';
144 // not a C keyword but is, for all intents and purposes, treated exactly like one.
188 literal: 'true false NULL',
189 // TODO: apply hinting work similar to what was done in cpp.js
190 built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream '
191 + 'auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set '
192 + 'unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos '
193 + 'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp '
194 + 'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper '
195 + 'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow '
196 + 'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp '
197 + 'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan '
198 + 'vfprintf vprintf vsprintf endl initializer_list unique_ptr',
201 const EXPRESSION_CONTAINS
= [
205 hljs
.C_BLOCK_COMMENT_MODE
,
210 const EXPRESSION_CONTEXT
= {
211 // This mode covers expression context where we can't expect a function
212 // definition and shouldn't highlight anything that looks like one:
213 // `return some()`, `else if()`, `(x*sum(1, 2))`
224 beginKeywords: 'new throw return else',
229 contains: EXPRESSION_CONTAINS
.concat([
234 contains: EXPRESSION_CONTAINS
.concat([ 'self' ]),
241 const FUNCTION_DECLARATION
= {
242 begin: '(' + FUNCTION_TYPE_RE
+ '[\\*&\\s]+)+' + FUNCTION_TITLE
,
247 illegal: /[^\w\s\*&:<>.]/,
249 { // to prevent it from being confused as the function title
250 begin: DECLTYPE_AUTO_RE
,
255 begin: FUNCTION_TITLE
,
257 contains: [ hljs
.inherit(TITLE_MODE
, { className: "title.function" }) ],
260 // allow for multiple declarations, e.g.:
261 // extern void f(int), g(char);
274 hljs
.C_BLOCK_COMMENT_MODE
,
278 // Count matching parentheses.
287 hljs
.C_BLOCK_COMMENT_MODE
,
297 hljs
.C_BLOCK_COMMENT_MODE
,
306 // Until differentiations are added between `c` and `cpp`, `c` will
307 // not be auto-detected to avoid auto-detect conflicts between C and C++
308 disableAutodetect: true,
312 FUNCTION_DECLARATION
,
317 begin: hljs
.IDENT_RE
+ '::',
322 beginKeywords: 'enum class struct union',
325 { beginKeywords: "final class struct" },
331 preprocessor: PREPROCESSOR
,
342 hljs
.registerLanguage('c', hljsGrammar
);