Skip to content

Commit 571704c

Browse files
Regex language (#1682)
This adds a Regex language that adds itself to a few languages which have regex literals.
1 parent 5712770 commit 571704c

13 files changed

+433
-1
lines changed

components.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,18 @@
734734
"require": "clike",
735735
"owner": "Golmote"
736736
},
737+
"regex": {
738+
"title": "Regex",
739+
"peerDependencies": [
740+
"actionscript",
741+
"coffeescript",
742+
"flow",
743+
"javascript",
744+
"typescript",
745+
"vala"
746+
],
747+
"owner": "RunDevelopment"
748+
},
737749
"rest": {
738750
"title": "reST (reStructuredText)",
739751
"owner": "Golmote"

components/prism-regex.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
(function (Prism) {
2+
3+
var specialEscape = {
4+
pattern: /\\[\\(){}[\]^$+*?|.]/,
5+
alias: 'escape'
6+
};
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
9+
10+
var rangeChar = '(?:[^\\\\-]|' + escape.source + ')';
11+
var range = RegExp(rangeChar + '-' + rangeChar);
12+
13+
// the name of a capturing group
14+
var groupName = {
15+
pattern: /(<|')[^<>']+(?=[>']$)/,
16+
lookbehind: true,
17+
alias: 'variable'
18+
};
19+
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+
30+
Prism.languages.regex = {
31+
'charset': {
32+
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:[^\\\]]|\\[\s\S])*\]/,
33+
lookbehind: true,
34+
inside: {
35+
'charset-negation': {
36+
pattern: /(^\[)\^/,
37+
lookbehind: true,
38+
},
39+
'charset-punctuation': /^\[|\]$/,
40+
'range': {
41+
pattern: range,
42+
inside: {
43+
'escape': escape,
44+
'range-punctuation': /-/
45+
}
46+
},
47+
'special-escape': specialEscape,
48+
'charclass': charClass,
49+
'backreference': backreference,
50+
'escape': escape
51+
}
52+
},
53+
'special-escape': specialEscape,
54+
'charclass': charClass,
55+
'backreference': backreference,
56+
'anchor': /[$^]|\\[ABbGZz]/,
57+
'escape': escape,
58+
'group': [
59+
{
60+
// https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html
61+
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2#grouping-constructs
62+
63+
// (), (?<name>), (?'name'), (?>), (?:), (?=), (?!), (?<=), (?<!), (?is-m), (?i-m:)
64+
pattern: /\((?:\?(?:<[^<>']+>|'[^<>']+'|[>:]|<?[=!]|[idmnsuxU]+(?:-[idmnsuxU]+)?:?))?/,
65+
inside: {
66+
'group-name': groupName
67+
}
68+
},
69+
/\)/
70+
],
71+
'quantifier': /[+*?]|\{(?:\d+,?\d*)\}/,
72+
'alternation': /\|/
73+
};
74+
75+
76+
[
77+
'actionscript',
78+
'coffescript',
79+
'flow',
80+
'javascript',
81+
'typescript',
82+
'vala'
83+
].forEach(function (lang) {
84+
var grammar = Prism.languages[lang];
85+
if (grammar) {
86+
grammar['regex'].inside = {
87+
'regex-flags': /[a-z]+$/,
88+
'regex-delimiter': /^\/|\/$/,
89+
'language-regex': {
90+
pattern: /[\s\S]+/,
91+
inside: Prism.languages.regex
92+
}
93+
};
94+
}
95+
});
96+
97+
}(Prism))

components/prism-regex.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/a+(?:[a-z]|\d)?/im;
2+
3+
----------------------------------------------------
4+
5+
[
6+
["regex", [
7+
["regex-delimiter", "/"],
8+
["language-regex", [
9+
"a",
10+
["quantifier", "+"],
11+
["group", ["(?:"]],
12+
["charset", [
13+
["charset-punctuation", "["],
14+
["range", [
15+
"a",
16+
["range-punctuation", "-"],
17+
"z"
18+
]],
19+
["charset-punctuation", "]"]
20+
]],
21+
["alternation", "|"],
22+
["charclass", "\\d"],
23+
["group", ")"],
24+
["quantifier", "?"]
25+
]],
26+
["regex-delimiter", "/"],
27+
["regex-flags", "im"]
28+
]],
29+
["punctuation", ";"]
30+
]
31+
32+
----------------------------------------------------
33+
34+
Checks for regex inclusion in JavaScript.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
^
2+
$
3+
\A
4+
\G
5+
\Z
6+
\z
7+
\b
8+
\B
9+
10+
----------------------------------------------------
11+
12+
[
13+
["anchor", "^"],
14+
["anchor", "$"],
15+
["anchor", "\\A"],
16+
["anchor", "\\G"],
17+
["anchor", "\\Z"],
18+
["anchor", "\\z"],
19+
["anchor", "\\b"],
20+
["anchor", "\\B"]
21+
]
22+
23+
----------------------------------------------------
24+
25+
Checks for anchors.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
\1 \2 \3 \4 \5 \6 \7 \8 \9
2+
\k<name>
3+
4+
----------------------------------------------------
5+
6+
[
7+
["backreference", "\\1"],
8+
["backreference", "\\2"],
9+
["backreference", "\\3"],
10+
["backreference", "\\4"],
11+
["backreference", "\\5"],
12+
["backreference", "\\6"],
13+
["backreference", "\\7"],
14+
["backreference", "\\8"],
15+
["backreference", "\\9"],
16+
17+
["backreference", [
18+
"\\k<",
19+
["group-name", "name"],
20+
">"
21+
]]
22+
]
23+
24+
----------------------------------------------------
25+
26+
Checks for backreferences.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.
2+
\w \W
3+
\s \S
4+
\d \D
5+
\p{ASCII}
6+
\P{ASCII}
7+
8+
----------------------------------------------------
9+
10+
[
11+
["charclass", "."],
12+
["charclass", "\\w"],
13+
["charclass", "\\W"],
14+
["charclass", "\\s"],
15+
["charclass", "\\S"],
16+
["charclass", "\\d"],
17+
["charclass", "\\D"],
18+
19+
["charclass", "\\p{ASCII}"],
20+
["charclass", "\\P{ASCII}"]
21+
]
22+
23+
----------------------------------------------------
24+
25+
Checks for character classes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[]
2+
[^]
3+
[foo]
4+
[\]\b]
5+
6+
----------------------------------------------------
7+
8+
[
9+
["charset", [
10+
["charset-punctuation", "["],
11+
["charset-punctuation", "]"]
12+
]],
13+
14+
["charset", [
15+
["charset-punctuation", "["],
16+
["charset-negation", "^"],
17+
["charset-punctuation", "]"]
18+
]],
19+
20+
["charset", [
21+
["charset-punctuation", "["],
22+
"foo",
23+
["charset-punctuation", "]"]
24+
]],
25+
26+
["charset", [
27+
["charset-punctuation", "["],
28+
["special-escape", "\\]"],
29+
["escape", "\\b"],
30+
["charset-punctuation", "]"]
31+
]]
32+
]
33+
34+
----------------------------------------------------
35+
36+
Checks for character sets.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
\0 \\ \. \+
2+
\xFF
3+
\uFFFF \u{10FFFF}
4+
\cA \cZ
5+
\01 \077 \377
6+
\n \r \t \f \a
7+
8+
\[ \]
9+
10+
----------------------------------------------------
11+
12+
[
13+
["escape", "\\0"],
14+
["special-escape", "\\\\"],
15+
["special-escape", "\\."],
16+
["special-escape", "\\+"],
17+
18+
["escape", "\\xFF"],
19+
20+
["escape", "\\uFFFF"],
21+
["escape", "\\u{10FFFF}"],
22+
23+
["escape", "\\cA"],
24+
["escape", "\\cZ"],
25+
26+
["escape", "\\01"],
27+
["escape", "\\077"],
28+
["escape", "\\377"],
29+
30+
["escape", "\\n"],
31+
["escape", "\\r"],
32+
["escape", "\\t"],
33+
["escape", "\\f"],
34+
["escape", "\\a"],
35+
36+
["special-escape", "\\["],
37+
["special-escape", "\\]"]
38+
]
39+
40+
----------------------------------------------------
41+
42+
Checks for escapes.

0 commit comments

Comments
 (0)