Skip to content

Commit 8c330dc

Browse files
c
1 parent 6417e69 commit 8c330dc

File tree

1 file changed

+63
-68
lines changed

1 file changed

+63
-68
lines changed

src/languages/c.js

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { insertBefore } from '../util/language-util.js';
21
import clike from './clike.js';
32

43
/** @type {import('../types.d.ts').LanguageProto<'c'>} */
@@ -7,75 +6,8 @@ export default {
76
base: clike,
87
optional: 'opencl-extensions',
98
grammar ({ base, getOptionalLanguage }) {
10-
insertBefore(base, 'string', {
11-
'char': {
12-
// https://en.cppreference.com/w/c/language/character_constant
13-
pattern: /'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n]){0,32}'/,
14-
greedy: true,
15-
},
16-
});
17-
18-
insertBefore(base, 'string', {
19-
'macro': {
20-
// allow for multiline macro definitions
21-
// spaces after the # character compile fine with gcc
22-
pattern:
23-
/(^[\t ]*)#\s*[a-z](?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
24-
lookbehind: true,
25-
greedy: true,
26-
alias: 'property',
27-
inside: {
28-
'string': [
29-
{
30-
// highlight the path of the include statement as a string
31-
pattern: /^(#\s*include\s*)<[^>]+>/,
32-
lookbehind: true,
33-
},
34-
/** @type {GrammarToken} */ (base['string']),
35-
],
36-
'char': base['char'],
37-
'comment': base['comment'],
38-
'macro-name': [
39-
{
40-
pattern: /(^#\s*define\s+)\w+\b(?!\()/i,
41-
lookbehind: true,
42-
},
43-
{
44-
pattern: /(^#\s*define\s+)\w+\b(?=\()/i,
45-
lookbehind: true,
46-
alias: 'function',
47-
},
48-
],
49-
// highlight macro directives as keywords
50-
'directive': {
51-
pattern: /^(#\s*)[a-z]+/,
52-
lookbehind: true,
53-
alias: 'keyword',
54-
},
55-
'directive-hash': /^#/,
56-
'punctuation': /##|\\(?=[\r\n])/,
57-
'expression': {
58-
pattern: /\S[\s\S]*/,
59-
inside: 'c',
60-
},
61-
},
62-
},
63-
});
64-
65-
insertBefore(base, 'function', {
66-
// highlight predefined macros as constants
67-
'constant':
68-
/\b(?:EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|__DATE__|__FILE__|__LINE__|__TIMESTAMP__|__TIME__|__func__|stderr|stdin|stdout)\b/,
69-
});
70-
71-
delete base['boolean'];
72-
739
/* OpenCL host API */
7410
const extensions = getOptionalLanguage('opencl-extensions');
75-
if (extensions) {
76-
insertBefore(base, 'keyword', /** @type {GrammarTokens} */ (extensions));
77-
delete base['type-opencl-host-cpp'];
78-
}
7911

8012
return {
8113
'comment': {
@@ -98,6 +30,69 @@ export default {
9830
'number':
9931
/(?:\b0x(?:[\da-f]+(?:\.[\da-f]*)?|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?)[ful]{0,4}/i,
10032
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/,
33+
$insert: {
34+
'char': {
35+
$before: 'string',
36+
// https://en.cppreference.com/w/c/language/character_constant
37+
pattern: /'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n]){0,32}'/,
38+
greedy: true,
39+
},
40+
'macro': {
41+
$before: 'string',
42+
// allow for multiline macro definitions
43+
// spaces after the # character compile fine with gcc
44+
pattern:
45+
/(^[\t ]*)#\s*[a-z](?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
46+
lookbehind: true,
47+
greedy: true,
48+
alias: 'property',
49+
inside: {
50+
'string': [
51+
{
52+
// highlight the path of the include statement as a string
53+
pattern: /^(#\s*include\s*)<[^>]+>/,
54+
lookbehind: true,
55+
},
56+
/** @type {GrammarToken} */ (base['string']),
57+
],
58+
'char': base['char'],
59+
'comment': base['comment'],
60+
'macro-name': [
61+
{
62+
pattern: /(^#\s*define\s+)\w+\b(?!\()/i,
63+
lookbehind: true,
64+
},
65+
{
66+
pattern: /(^#\s*define\s+)\w+\b(?=\()/i,
67+
lookbehind: true,
68+
alias: 'function',
69+
},
70+
],
71+
// highlight macro directives as keywords
72+
'directive': {
73+
pattern: /^(#\s*)[a-z]+/,
74+
lookbehind: true,
75+
alias: 'keyword',
76+
},
77+
'directive-hash': /^#/,
78+
'punctuation': /##|\\(?=[\r\n])/,
79+
'expression': {
80+
pattern: /\S[\s\S]*/,
81+
inside: 'c',
82+
},
83+
},
84+
},
85+
// highlight predefined macros as constants
86+
'constant': {
87+
$before: 'function',
88+
pattern:
89+
/\b(?:EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|__DATE__|__FILE__|__LINE__|__TIMESTAMP__|__TIME__|__func__|stderr|stdin|stdout)\b/,
90+
},
91+
},
92+
$insertBefore: {
93+
'function': /** @type {import('../types.d.ts').GrammarTokens} */ (extensions),
94+
},
95+
$delete: ['boolean', ...(extensions ? ['type-opencl-host-cpp'] : [])],
10196
};
10297
},
10398
};

0 commit comments

Comments
 (0)