Skip to content

Commit

Permalink
Regex language (#1682)
Browse files Browse the repository at this point in the history
This adds a Regex language that adds itself to a few languages which have regex literals.
  • Loading branch information
RunDevelopment authored Mar 4, 2019
1 parent 5712770 commit 571704c
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,18 @@
"require": "clike",
"owner": "Golmote"
},
"regex": {
"title": "Regex",
"peerDependencies": [
"actionscript",
"coffeescript",
"flow",
"javascript",
"typescript",
"vala"
],
"owner": "RunDevelopment"
},
"rest": {
"title": "reST (reStructuredText)",
"owner": "Golmote"
Expand Down
97 changes: 97 additions & 0 deletions components/prism-regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
(function (Prism) {

var specialEscape = {
pattern: /\\[\\(){}[\]^$+*?|.]/,
alias: 'escape'
};
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}|.)/
var charClass = /\\[wsd]|\.|\\p{[^{}]+}/i

var rangeChar = '(?:[^\\\\-]|' + escape.source + ')';
var range = RegExp(rangeChar + '-' + rangeChar);

// the name of a capturing group
var groupName = {
pattern: /(<|')[^<>']+(?=[>']$)/,
lookbehind: true,
alias: 'variable'
};

var backreference = [
/\\(?![123][0-7]{2})[1-9]/, // a backreference which is not an octal escape
{
pattern: /\\k<[^<>']+>/,
inside: {
'group-name': groupName
}
}
];

Prism.languages.regex = {
'charset': {
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:[^\\\]]|\\[\s\S])*\]/,
lookbehind: true,
inside: {
'charset-negation': {
pattern: /(^\[)\^/,
lookbehind: true,
},
'charset-punctuation': /^\[|\]$/,
'range': {
pattern: range,
inside: {
'escape': escape,
'range-punctuation': /-/
}
},
'special-escape': specialEscape,
'charclass': charClass,
'backreference': backreference,
'escape': escape
}
},
'special-escape': specialEscape,
'charclass': charClass,
'backreference': backreference,
'anchor': /[$^]|\\[ABbGZz]/,
'escape': escape,
'group': [
{
// https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference?view=netframework-4.7.2#grouping-constructs

// (), (?<name>), (?'name'), (?>), (?:), (?=), (?!), (?<=), (?<!), (?is-m), (?i-m:)
pattern: /\((?:\?(?:<[^<>']+>|'[^<>']+'|[>:]|<?[=!]|[idmnsuxU]+(?:-[idmnsuxU]+)?:?))?/,
inside: {
'group-name': groupName
}
},
/\)/
],
'quantifier': /[+*?]|\{(?:\d+,?\d*)\}/,
'alternation': /\|/
};


[
'actionscript',
'coffescript',
'flow',
'javascript',
'typescript',
'vala'
].forEach(function (lang) {
var grammar = Prism.languages[lang];
if (grammar) {
grammar['regex'].inside = {
'regex-flags': /[a-z]+$/,
'regex-delimiter': /^\/|\/$/,
'language-regex': {
pattern: /[\s\S]+/,
inside: Prism.languages.regex
}
};
}
});

}(Prism))
1 change: 1 addition & 0 deletions components/prism-regex.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions tests/languages/javascript!+regex/regex_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/a+(?:[a-z]|\d)?/im;

----------------------------------------------------

[
["regex", [
["regex-delimiter", "/"],
["language-regex", [
"a",
["quantifier", "+"],
["group", ["(?:"]],
["charset", [
["charset-punctuation", "["],
["range", [
"a",
["range-punctuation", "-"],
"z"
]],
["charset-punctuation", "]"]
]],
["alternation", "|"],
["charclass", "\\d"],
["group", ")"],
["quantifier", "?"]
]],
["regex-delimiter", "/"],
["regex-flags", "im"]
]],
["punctuation", ";"]
]

----------------------------------------------------

Checks for regex inclusion in JavaScript.
25 changes: 25 additions & 0 deletions tests/languages/regex/anchor_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
^
$
\A
\G
\Z
\z
\b
\B

----------------------------------------------------

[
["anchor", "^"],
["anchor", "$"],
["anchor", "\\A"],
["anchor", "\\G"],
["anchor", "\\Z"],
["anchor", "\\z"],
["anchor", "\\b"],
["anchor", "\\B"]
]

----------------------------------------------------

Checks for anchors.
26 changes: 26 additions & 0 deletions tests/languages/regex/backreference_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
\1 \2 \3 \4 \5 \6 \7 \8 \9
\k<name>

----------------------------------------------------

[
["backreference", "\\1"],
["backreference", "\\2"],
["backreference", "\\3"],
["backreference", "\\4"],
["backreference", "\\5"],
["backreference", "\\6"],
["backreference", "\\7"],
["backreference", "\\8"],
["backreference", "\\9"],

["backreference", [
"\\k<",
["group-name", "name"],
">"
]]
]

----------------------------------------------------

Checks for backreferences.
25 changes: 25 additions & 0 deletions tests/languages/regex/charclass_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.
\w \W
\s \S
\d \D
\p{ASCII}
\P{ASCII}

----------------------------------------------------

[
["charclass", "."],
["charclass", "\\w"],
["charclass", "\\W"],
["charclass", "\\s"],
["charclass", "\\S"],
["charclass", "\\d"],
["charclass", "\\D"],

["charclass", "\\p{ASCII}"],
["charclass", "\\P{ASCII}"]
]

----------------------------------------------------

Checks for character classes.
36 changes: 36 additions & 0 deletions tests/languages/regex/charset_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[]
[^]
[foo]
[\]\b]

----------------------------------------------------

[
["charset", [
["charset-punctuation", "["],
["charset-punctuation", "]"]
]],

["charset", [
["charset-punctuation", "["],
["charset-negation", "^"],
["charset-punctuation", "]"]
]],

["charset", [
["charset-punctuation", "["],
"foo",
["charset-punctuation", "]"]
]],

["charset", [
["charset-punctuation", "["],
["special-escape", "\\]"],
["escape", "\\b"],
["charset-punctuation", "]"]
]]
]

----------------------------------------------------

Checks for character sets.
42 changes: 42 additions & 0 deletions tests/languages/regex/escape_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
\0 \\ \. \+
\xFF
\uFFFF \u{10FFFF}
\cA \cZ
\01 \077 \377
\n \r \t \f \a

\[ \]

----------------------------------------------------

[
["escape", "\\0"],
["special-escape", "\\\\"],
["special-escape", "\\."],
["special-escape", "\\+"],

["escape", "\\xFF"],

["escape", "\\uFFFF"],
["escape", "\\u{10FFFF}"],

["escape", "\\cA"],
["escape", "\\cZ"],

["escape", "\\01"],
["escape", "\\077"],
["escape", "\\377"],

["escape", "\\n"],
["escape", "\\r"],
["escape", "\\t"],
["escape", "\\f"],
["escape", "\\a"],

["special-escape", "\\["],
["special-escape", "\\]"]
]

----------------------------------------------------

Checks for escapes.
64 changes: 64 additions & 0 deletions tests/languages/regex/group_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
()
(?:)
(?>)
(?=) (?!)
(?<=) (?<!)

(?<name>)
(?'name')

(?i)
(?i:)
(?idmsuxU-idmsuxU)
(?idmsux-idmsux:X)

----------------------------------------------------

[
["group", ["("]],
["group", ")"],

["group", ["(?:"]],
["group", ")"],

["group", ["(?>"]],
["group", ")"],

["group", ["(?="]],
["group", ")"],
["group", ["(?!"]],
["group", ")"],

["group", ["(?<="]],
["group", ")"],
["group", ["(?<!"]],
["group", ")"],

["group", [
"(?<",
["group-name", "name"],
">"]
],
["group", ")"],

["group", [
"(?'",
["group-name", "name"],
"'"]
],
["group", ")"],

["group", ["(?i"]],
["group", ")"],
["group", ["(?i:"]],
["group", ")"],
["group", ["(?idmsuxU-idmsuxU"]],
["group", ")"],
["group", ["(?idmsux-idmsux:"]],
"X",
["group", ")"]
]

----------------------------------------------------

Checks for groups.
17 changes: 17 additions & 0 deletions tests/languages/regex/quantifier_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
* + ?
{2} {2,} {0,1}

----------------------------------------------------

[
["quantifier", "*"],
["quantifier", "+"],
["quantifier", "?"],
["quantifier", "{2}"],
["quantifier", "{2,}"],
["quantifier", "{0,1}"]
]

----------------------------------------------------

Checks for quantifiers.
Loading

0 comments on commit 571704c

Please sign in to comment.