1 | 'use strict'
|
---|
2 |
|
---|
3 | module.exports = c
|
---|
4 | c.displayName = 'c'
|
---|
5 | c.aliases = []
|
---|
6 | function c(Prism) {
|
---|
7 | Prism.languages.c = Prism.languages.extend('clike', {
|
---|
8 | comment: {
|
---|
9 | pattern:
|
---|
10 | /\/\/(?:[^\r\n\\]|\\(?:\r\n?|\n|(?![\r\n])))*|\/\*[\s\S]*?(?:\*\/|$)/,
|
---|
11 | greedy: true
|
---|
12 | },
|
---|
13 | string: {
|
---|
14 | // https://en.cppreference.com/w/c/language/string_literal
|
---|
15 | pattern: /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"/,
|
---|
16 | greedy: true
|
---|
17 | },
|
---|
18 | 'class-name': {
|
---|
19 | pattern:
|
---|
20 | /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+|\b[a-z]\w*_t\b/,
|
---|
21 | lookbehind: true
|
---|
22 | },
|
---|
23 | keyword:
|
---|
24 | /\b(?:_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|__attribute__|asm|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|inline|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|typeof|union|unsigned|void|volatile|while)\b/,
|
---|
25 | function: /\b[a-z_]\w*(?=\s*\()/i,
|
---|
26 | number:
|
---|
27 | /(?:\b0x(?:[\da-f]+(?:\.[\da-f]*)?|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?)[ful]{0,4}/i,
|
---|
28 | operator: />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/
|
---|
29 | })
|
---|
30 | Prism.languages.insertBefore('c', 'string', {
|
---|
31 | char: {
|
---|
32 | // https://en.cppreference.com/w/c/language/character_constant
|
---|
33 | pattern: /'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n]){0,32}'/,
|
---|
34 | greedy: true
|
---|
35 | }
|
---|
36 | })
|
---|
37 | Prism.languages.insertBefore('c', 'string', {
|
---|
38 | macro: {
|
---|
39 | // allow for multiline macro definitions
|
---|
40 | // spaces after the # character compile fine with gcc
|
---|
41 | pattern:
|
---|
42 | /(^[\t ]*)#\s*[a-z](?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
|
---|
43 | lookbehind: true,
|
---|
44 | greedy: true,
|
---|
45 | alias: 'property',
|
---|
46 | inside: {
|
---|
47 | string: [
|
---|
48 | {
|
---|
49 | // highlight the path of the include statement as a string
|
---|
50 | pattern: /^(#\s*include\s*)<[^>]+>/,
|
---|
51 | lookbehind: true
|
---|
52 | },
|
---|
53 | Prism.languages.c['string']
|
---|
54 | ],
|
---|
55 | char: Prism.languages.c['char'],
|
---|
56 | comment: Prism.languages.c['comment'],
|
---|
57 | 'macro-name': [
|
---|
58 | {
|
---|
59 | pattern: /(^#\s*define\s+)\w+\b(?!\()/i,
|
---|
60 | lookbehind: true
|
---|
61 | },
|
---|
62 | {
|
---|
63 | pattern: /(^#\s*define\s+)\w+\b(?=\()/i,
|
---|
64 | lookbehind: true,
|
---|
65 | alias: 'function'
|
---|
66 | }
|
---|
67 | ],
|
---|
68 | // highlight macro directives as keywords
|
---|
69 | directive: {
|
---|
70 | pattern: /^(#\s*)[a-z]+/,
|
---|
71 | lookbehind: true,
|
---|
72 | alias: 'keyword'
|
---|
73 | },
|
---|
74 | 'directive-hash': /^#/,
|
---|
75 | punctuation: /##|\\(?=[\r\n])/,
|
---|
76 | expression: {
|
---|
77 | pattern: /\S[\s\S]*/,
|
---|
78 | inside: Prism.languages.c
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | })
|
---|
83 | Prism.languages.insertBefore('c', 'function', {
|
---|
84 | // highlight predefined macros as constants
|
---|
85 | constant:
|
---|
86 | /\b(?:EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|__DATE__|__FILE__|__LINE__|__TIMESTAMP__|__TIME__|__func__|stderr|stdin|stdout)\b/
|
---|
87 | })
|
---|
88 | delete Prism.languages.c['boolean']
|
---|
89 | }
|
---|