Skip to content

Commit d02b07f

Browse files
pure
1 parent 7d019ac commit d02b07f

File tree

1 file changed

+57
-61
lines changed

1 file changed

+57
-61
lines changed

src/languages/pure.js

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
1-
import { insertBefore } from '../util/language-util.js';
2-
31
/** @type {import('../types.d.ts').LanguageProto<'pure'>} */
42
export default {
53
id: 'pure',
64
grammar () {
75
// https://agraef.github.io/pure-docs/pure.html#lexical-matters
86

9-
const pure = /** @type {Grammar} */ ({
7+
const inlineLang = {
8+
pattern: /%<[\s\S]+?%>/,
9+
greedy: true,
10+
inside: {
11+
'lang': {
12+
pattern: /(^%< *)-\*-.+?-\*-/,
13+
lookbehind: true,
14+
alias: 'comment',
15+
},
16+
'delimiter': {
17+
pattern: /^%<.*|%>$/,
18+
alias: 'punctuation',
19+
},
20+
// C is the default inline language
21+
$rest: 'c',
22+
},
23+
};
24+
25+
const inlineLanguages = ['c', { lang: 'c++', alias: 'cpp' }, 'fortran'];
26+
const inlineLanguageRe = /%< *-\*- *<lang>\d* *-\*-[\s\S]+?%>/.source;
27+
28+
const insertBeforeInlineLang = [];
29+
inlineLanguages.forEach(item => {
30+
let alias;
31+
let lang;
32+
if (typeof item === 'string') {
33+
alias = lang = item;
34+
}
35+
else {
36+
alias = item.alias;
37+
lang = item.lang;
38+
}
39+
40+
insertBeforeInlineLang.push([
41+
['inline-lang-' + alias],
42+
{
43+
pattern: RegExp(
44+
inlineLanguageRe.replace(
45+
'<lang>',
46+
lang.replace(/([.+*?\/\\(){}\[\]])/g, '\\$1')
47+
),
48+
'i'
49+
),
50+
inside: {
51+
...inlineLang.inside,
52+
$rest: alias,
53+
},
54+
},
55+
]);
56+
});
57+
58+
return {
1059
'comment': [
1160
{
1261
pattern: /(^|[^\\])\/\*[\s\S]*?\*\//,
@@ -18,23 +67,7 @@ export default {
1867
},
1968
/#!.+/,
2069
],
21-
'inline-lang': {
22-
pattern: /%<[\s\S]+?%>/,
23-
greedy: true,
24-
inside: {
25-
'lang': {
26-
pattern: /(^%< *)-\*-.+?-\*-/,
27-
lookbehind: true,
28-
alias: 'comment',
29-
},
30-
'delimiter': {
31-
pattern: /^%<.*|%>$/,
32-
alias: 'punctuation',
33-
},
34-
// C is the default inline language
35-
$rest: /** @type {Grammar['$rest']} */ ('c'),
36-
},
37-
},
70+
'inline-lang': inlineLang,
3871
'string': {
3972
pattern: /"(?:\\.|[^"\\\r\n])*"/,
4073
greedy: true,
@@ -59,46 +92,9 @@ export default {
5992
/(?:[!"#$%&'*+,\-.\/:<=>?@\\^`|~\u00a1-\u00bf\u00d7-\u00f7\u20d0-\u2bff]|\b_+\b)+|\b(?:and|div|mod|not|or)\b/,
6093
// FIXME: How can we prevent | and , to be highlighted as operator when they are used alone?
6194
'punctuation': /[(){}\[\];,|]/,
62-
});
63-
64-
const inlineLanguages = ['c', { lang: 'c++', alias: 'cpp' }, 'fortran'];
65-
const inlineLanguageRe = /%< *-\*- *<lang>\d* *-\*-[\s\S]+?%>/.source;
66-
67-
inlineLanguages.forEach(item => {
68-
let alias;
69-
let lang;
70-
if (typeof item === 'string') {
71-
alias = lang = item;
72-
}
73-
else {
74-
alias = item.alias;
75-
lang = item.lang;
76-
}
77-
78-
insertBefore(pure, 'inline-lang', {
79-
['inline-lang-' + alias]: {
80-
pattern: RegExp(
81-
inlineLanguageRe.replace(
82-
'<lang>',
83-
lang.replace(/([.+*?\/\\(){}\[\]])/g, '\\$1')
84-
),
85-
'i'
86-
),
87-
inside: /** @type {Grammar} */ ({
88-
.../** @type {Grammar} */ (
89-
/** @type {GrammarToken} */ (pure['inline-lang']).inside
90-
),
91-
$rest: alias,
92-
}),
93-
},
94-
});
95-
});
96-
97-
return pure;
95+
$insertBefore: {
96+
'inline-lang': Object.fromEntries(insertBeforeInlineLang),
97+
},
98+
};
9899
},
99100
};
100-
101-
/**
102-
* @typedef {import('../types.d.ts').Grammar} Grammar
103-
* @typedef {import('../types.d.ts').GrammarToken} GrammarToken
104-
*/

0 commit comments

Comments
 (0)