Skip to content

Commit 8a72830

Browse files
Regex: Added aliases and minor improvements (#2325)
This adds a lot of aliases to the regex tokens, so themes can apply their styles. It also makes a few improvements. See the PR for more details.
1 parent 6d663b6 commit 8a72830

File tree

6 files changed

+132
-30
lines changed

6 files changed

+132
-30
lines changed

Diff for: components/prism-regex.js

+55-25
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
pattern: /\\[\\(){}[\]^$+*?|.]/,
55
alias: 'escape'
66
};
7-
var escape = /\\(?:x[\da-fA-F]{2}|u[\da-fA-F]{4}|u\{[\da-fA-F]+\}|c[a-zA-Z]|0[0-7]{0,2}|[123][0-7]{2}|.)/
8-
var charClass = /\\[wsd]|\.|\\p{[^{}]+}/i
7+
var escape = /\\(?:x[\da-fA-F]{2}|u[\da-fA-F]{4}|u\{[\da-fA-F]+\}|c[a-zA-Z]|0[0-7]{0,2}|[123][0-7]{2}|.)/;
8+
var charClass = {
9+
pattern: /\.|\\[wsd]|\\p{[^{}]+}/i,
10+
alias: 'class-name'
11+
};
12+
var charClassWithoutDot = {
13+
pattern: /\\[wsd]|\\p{[^{}]+}/i,
14+
alias: 'class-name'
15+
};
916

1017
var rangeChar = '(?:[^\\\\-]|' + escape.source + ')';
1118
var range = RegExp(rangeChar + '-' + rangeChar);
@@ -17,16 +24,6 @@
1724
alias: 'variable'
1825
};
1926

20-
var backreference = [
21-
/\\(?![123][0-7]{2})[1-9]/, // a backreference which is not an octal escape
22-
{
23-
pattern: /\\k<[^<>']+>/,
24-
inside: {
25-
'group-name': groupName
26-
}
27-
}
28-
];
29-
3027
Prism.languages.regex = {
3128
'charset': {
3229
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:[^\\\]]|\\[\s\S])*\]/,
@@ -35,25 +32,47 @@
3532
'charset-negation': {
3633
pattern: /(^\[)\^/,
3734
lookbehind: true,
35+
alias: 'operator'
36+
},
37+
'charset-punctuation': {
38+
pattern: /^\[|\]$/,
39+
alias: 'punctuation'
3840
},
39-
'charset-punctuation': /^\[|\]$/,
4041
'range': {
4142
pattern: range,
4243
inside: {
4344
'escape': escape,
44-
'range-punctuation': /-/
45+
'range-punctuation': {
46+
pattern: /-/,
47+
alias: 'operator'
48+
}
4549
}
4650
},
4751
'special-escape': specialEscape,
48-
'charclass': charClass,
49-
'backreference': backreference,
52+
'charclass': charClassWithoutDot,
5053
'escape': escape
5154
}
5255
},
5356
'special-escape': specialEscape,
5457
'charclass': charClass,
55-
'backreference': backreference,
56-
'anchor': /[$^]|\\[ABbGZz]/,
58+
'backreference': [
59+
{
60+
// a backreference which is not an octal escape
61+
pattern: /\\(?![123][0-7]{2})[1-9]/,
62+
alias: 'keyword'
63+
},
64+
{
65+
pattern: /\\k<[^<>']+>/,
66+
alias: 'keyword',
67+
inside: {
68+
'group-name': groupName
69+
}
70+
}
71+
],
72+
'anchor': {
73+
pattern: /[$^]|\\[ABbGZz]/,
74+
alias: 'function'
75+
},
5776
'escape': escape,
5877
'group': [
5978
{
@@ -62,14 +81,24 @@
6281

6382
// (), (?<name>), (?'name'), (?>), (?:), (?=), (?!), (?<=), (?<!), (?is-m), (?i-m:)
6483
pattern: /\((?:\?(?:<[^<>']+>|'[^<>']+'|[>:]|<?[=!]|[idmnsuxU]+(?:-[idmnsuxU]+)?:?))?/,
84+
alias: 'punctuation',
6585
inside: {
6686
'group-name': groupName
6787
}
6888
},
69-
/\)/
89+
{
90+
pattern: /\)/,
91+
alias: 'punctuation'
92+
}
7093
],
71-
'quantifier': /[+*?]|\{(?:\d+,?\d*)\}/,
72-
'alternation': /\|/
94+
'quantifier': {
95+
pattern: /(?:[+*?]|\{(?:\d+,?\d*)\})[?+]?/,
96+
alias: 'number'
97+
},
98+
'alternation': {
99+
pattern: /\|/,
100+
alias: 'keyword'
101+
}
73102
};
74103

75104

@@ -84,12 +113,13 @@
84113
var grammar = Prism.languages[lang];
85114
if (grammar) {
86115
grammar['regex'].inside = {
87-
'regex-flags': /[a-z]+$/,
88-
'regex-delimiter': /^\/|\/$/,
89116
'language-regex': {
90-
pattern: /[\s\S]+/,
117+
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/i,
118+
lookbehind: true,
91119
inside: Prism.languages.regex
92-
}
120+
},
121+
'regex-flags': /[a-z]+$/i,
122+
'regex-delimiter': /^\/|\/$/,
93123
};
94124
}
95125
});

Diff for: components/prism-regex.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: examples/prism-regex.html

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>The regex languages con be used for inline regex snippets like <code>(?&lt;number>\d+)[-_ ]\k&lt;number></code> but it mainly adds itself to other languages such as:</p>
2+
3+
<h2>JavaScript</h2>
4+
<pre class="language-javascript" data-dependencies="regex"><code>Prism.languages.markup = {
5+
'comment': /&lt;!--[\s\S]*?-->/,
6+
'prolog': /&lt;\?[\s\S]+?\?>/,
7+
'doctype': {
8+
pattern: /&lt;!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^&lt;"'\]]|"[^"]*"|'[^']*'|&lt;(?!!--)|&lt;!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
9+
greedy: true
10+
},
11+
'cdata': /&lt;!\[CDATA\[[\s\S]*?]]>/i,
12+
'tag': {
13+
pattern: /&lt;\/?(?!\d)[^\s>\/=$&lt;%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/i,
14+
greedy: true,
15+
inside: {
16+
'tag': {
17+
pattern: /^&lt;\/?[^\s>\/]+/i,
18+
inside: {
19+
'punctuation': /^&lt;\/?/,
20+
'namespace': /^[^\s>\/:]+:/
21+
}
22+
},
23+
'attr-value': {
24+
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/i,
25+
inside: {
26+
'punctuation': [
27+
/^=/,
28+
{
29+
pattern: /^(\s*)["']|["']$/,
30+
lookbehind: true
31+
}
32+
]
33+
}
34+
},
35+
'punctuation': /\/?>/,
36+
'attr-name': {
37+
pattern: /[^\s>\/]+/,
38+
inside: {
39+
'namespace': /^[^\s>\/:]+:/
40+
}
41+
}
42+
43+
}
44+
},
45+
'entity': /&amp;#?[\da-z]{1,8};/i
46+
};</code></pre>

Diff for: tests/examples-test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ describe('Examples', function () {
1313
'markup-templating',
1414
't4-templating',
1515
// this does alter some languages but it's mainly a library
16-
'javadoclike',
17-
// Regex doesn't have any classes supported by our themes and mainly extends other languages
18-
'regex'
16+
'javadoclike'
1917
]);
2018
const validFiles = new Set();
2119

Diff for: tests/languages/regex/charset_feature.test

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[^]
33
[foo]
44
[\]\b]
5+
[.^$\1]
56

67
----------------------------------------------------
78

@@ -28,6 +29,13 @@
2829
["special-escape", "\\]"],
2930
["escape", "\\b"],
3031
["charset-punctuation", "]"]
32+
]],
33+
34+
["charset", [
35+
["charset-punctuation", "["],
36+
".^$",
37+
["escape", "\\1"],
38+
["charset-punctuation", "]"]
3139
]]
3240
]
3341

Diff for: tests/languages/regex/quantifier_feature.test

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
* + ?
22
{2} {2,} {0,1}
33

4+
*? +? ??
5+
{2}? {2,}? {0,1}?
6+
7+
*+ ++ ?+
8+
{2}+ {2,}+ {0,1}+
9+
410
----------------------------------------------------
511

612
[
@@ -9,7 +15,21 @@
915
["quantifier", "?"],
1016
["quantifier", "{2}"],
1117
["quantifier", "{2,}"],
12-
["quantifier", "{0,1}"]
18+
["quantifier", "{0,1}"],
19+
20+
["quantifier", "*?"],
21+
["quantifier", "+?"],
22+
["quantifier", "??"],
23+
["quantifier", "{2}?"],
24+
["quantifier", "{2,}?"],
25+
["quantifier", "{0,1}?"],
26+
27+
["quantifier", "*+"],
28+
["quantifier", "++"],
29+
["quantifier", "?+"],
30+
["quantifier", "{2}+"],
31+
["quantifier", "{2,}+"],
32+
["quantifier", "{0,1}+"]
1333
]
1434

1535
----------------------------------------------------

0 commit comments

Comments
 (0)