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