Skip to content

Commit

Permalink
Regex: Changed how languages embed regexes (#2532)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Aug 31, 2020
1 parent bf169e5 commit f62ca78
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 58 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"title": "JavaScript",
"require": "clike",
"modify": "markup",
"optional": "regex",
"alias": "js",
"option": "default"
},
Expand Down Expand Up @@ -210,7 +211,7 @@
"oscript": "OneScript"
},
"owner": "Diversus23"
},
},
"c": {
"title": "C",
"require": "clike",
Expand Down Expand Up @@ -969,14 +970,6 @@
},
"regex": {
"title": "Regex",
"modify": [
"actionscript",
"coffeescript",
"flow",
"javascript",
"typescript",
"vala"
],
"owner": "RunDevelopment"
},
"renpy": {
Expand Down Expand Up @@ -1181,6 +1174,7 @@
"vala": {
"title": "Vala",
"require": "clike",
"optional": "regex",
"owner": "TemplarVolk"
},
"vbnet": {
Expand Down
12 changes: 11 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
lookbehind: true,
greedy: true
greedy: true,
inside: {
'regex-source': {
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
lookbehind: true,
alias: 'language-regex',
inside: Prism.languages.regex
},
'regex-flags': /[a-z]+$/,
'regex-delimiter': /^\/|\/$/
}
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

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

23 changes: 0 additions & 23 deletions components/prism-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,4 @@
}
};


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

}(Prism))
2 changes: 1 addition & 1 deletion components/prism-regex.min.js

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

12 changes: 11 additions & 1 deletion components/prism-vala.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ Prism.languages.insertBefore('vala','string', {
Prism.languages.insertBefore('vala', 'keyword', {
'regex': {
pattern: /\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[imsx]{0,4}(?=\s*(?:$|[\r\n,.;})\]]))/,
greedy: true
greedy: true,
inside: {
'regex-source': {
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
lookbehind: true,
alias: 'language-regex',
inside: Prism.languages.regex
},
'regex-flags': /[a-z]+$/,
'regex-delimiter': /^\/|\/$/
}
}
});
2 changes: 1 addition & 1 deletion components/prism-vala.min.js

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

12 changes: 11 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,17 @@ Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
lookbehind: true,
greedy: true
greedy: true,
inside: {
'regex-source': {
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
lookbehind: true,
alias: 'language-regex',
inside: Prism.languages.regex
},
'regex-flags': /[a-z]+$/,
'regex-delimiter': /^\/|\/$/
}
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/javascript!+regex/regex_inclusion.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[
["regex", [
["regex-delimiter", "/"],
["language-regex", [
["regex-source", [
"a",
["quantifier", "+"],
["group", ["(?:"]],
Expand Down
104 changes: 88 additions & 16 deletions tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,110 @@ yield /regex/;
----------------------------------------------------

[
["regex", "/foo bar/"], ["punctuation", ";"],
["regex", "/foo/gimyus"], ["punctuation", ","],
["regex", "/[\\[\\]]{2,4}(?:foo)*/"], ["punctuation", ";"],
["regex", "/foo\"test\"bar/"], ["punctuation", ";"],
["regex", "/foo\\//"], ["punctuation", ";"],
["regex", "/[]/"], ["punctuation", ";"],
["regex", "/[\\]/]/"], ["punctuation", ";"],
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"],
["regex", "/ '1' '2' '3' '4' '5' /"],
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "foo bar"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "foo"],
["regex-delimiter", "/"],
["regex-flags", "gimyus"]
]],
["punctuation", ","],
["regex", [
["regex-delimiter", "/"],
["regex-source", "[\\[\\]]{2,4}(?:foo)*"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "foo\"test\"bar"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "foo\\/"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "[]"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "[\\]/]"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["number", "1"],
["operator", "/"],
["number", "4"],
["operator", "+"],
["string", "\"/, not a regex\""],
["punctuation", ";"],
["regex", [
["regex-delimiter", "/"],
["regex-source", " '1' '2' '3' '4' '5' "],
["regex-delimiter", "/"]
]],
["punctuation", "["],
["regex", [
["regex-delimiter", "/"],
["regex-source", "foo"],
["regex-delimiter", "/"]
]],
["punctuation", "]"],

["keyword", "let"],
" a ",
["operator", "="],
["regex", "/regex/m"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "regex"],
["regex-delimiter", "/"],
["regex-flags", "m"]
]],
["comment", "// comment"],
["keyword", "let"],
" b ",
["operator", "="],
" condition ",
["operator", "?"],
["regex", "/regex/"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "regex"],
["regex-delimiter", "/"]
]],
["operator", ":"],
["regex", "/another one/"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "another one"],
["regex-delimiter", "/"]
]],
["keyword", "return"],
["regex", "/regex/"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "regex"],
["regex-delimiter", "/"]
]],
["punctuation", ";"],
["keyword", "yield"],
["regex", "/regex/"],
["regex", [
["regex-delimiter", "/"],
["regex-source", "regex"],
["regex-delimiter", "/"]
]],
["punctuation", ";"]
]

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

Checks for regex.
Checks for regex.
Loading

0 comments on commit f62ca78

Please sign in to comment.