Skip to content

Commit

Permalink
Regex: Fixed char-class/char-set confusion (#3124)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Oct 5, 2021
1 parent 09a0e2b commit 4dde2e2
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 93 deletions.
14 changes: 7 additions & 7 deletions components/prism-regex.js
Expand Up @@ -5,11 +5,11 @@
alias: 'escape'
};
var escape = /\\(?:x[\da-fA-F]{2}|u[\da-fA-F]{4}|u\{[\da-fA-F]+\}|0[0-7]{0,2}|[123][0-7]{2}|c[a-zA-Z]|.)/;
var charClass = {
var charSet = {
pattern: /\.|\\[wsd]|\\p\{[^{}]+\}/i,
alias: 'class-name'
};
var charClassWithoutDot = {
var charSetWithoutDot = {
pattern: /\\[wsd]|\\p\{[^{}]+\}/i,
alias: 'class-name'
};
Expand All @@ -25,16 +25,16 @@
};

Prism.languages.regex = {
'charset': {
'char-class': {
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:[^\\\]]|\\[\s\S])*\]/,
lookbehind: true,
inside: {
'charset-negation': {
'char-class-negation': {
pattern: /(^\[)\^/,
lookbehind: true,
alias: 'operator'
},
'charset-punctuation': {
'char-class-punctuation': {
pattern: /^\[|\]$/,
alias: 'punctuation'
},
Expand All @@ -49,12 +49,12 @@
}
},
'special-escape': specialEscape,
'charclass': charClassWithoutDot,
'char-set': charSetWithoutDot,
'escape': escape
}
},
'special-escape': specialEscape,
'charclass': charClass,
'char-set': charSet,
'backreference': [
{
// a backreference which is not an octal escape
Expand Down
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.

8 changes: 4 additions & 4 deletions tests/languages/javascript!+regex/regex_inclusion.test
Expand Up @@ -9,17 +9,17 @@
"a",
["quantifier", "+"],
["group", ["(?:"]],
["charset", [
["charset-punctuation", "["],
["char-class", [
["char-class-punctuation", "["],
["range", [
"a",
["range-punctuation", "-"],
"z"
]],
["charset-punctuation", "]"]
["char-class-punctuation", "]"]
]],
["alternation", "|"],
["charclass", "\\d"],
["char-set", "\\d"],
["group", ")"],
["quantifier", "?"]
]],
Expand Down
48 changes: 48 additions & 0 deletions tests/languages/regex/char-class_feature.test
@@ -0,0 +1,48 @@
[]
[^]
[foo]
[\]\b]
[.^$\1]
[\d\D\p{L}]

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

[
["char-class", [
["char-class-punctuation", "["],
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
["char-class-negation", "^"],
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
"foo",
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
["special-escape", "\\]"],
["escape", "\\b"],
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
".^$",
["escape", "\\1"],
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
["char-set", "\\d"],
["char-set", "\\D"],
["char-set", "\\p{L}"],
["char-class-punctuation", "]"]
]]
]

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

Checks for character sets.
21 changes: 21 additions & 0 deletions tests/languages/regex/char-set_feature.test
@@ -0,0 +1,21 @@
.
\w \W
\s \S
\d \D
\p{ASCII}
\P{ASCII}

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

[
["char-set", "."],
["char-set", "\\w"], ["char-set", "\\W"],
["char-set", "\\s"], ["char-set", "\\S"],
["char-set", "\\d"], ["char-set", "\\D"],
["char-set", "\\p{ASCII}"],
["char-set", "\\P{ASCII}"]
]

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

Checks for character classes.
25 changes: 0 additions & 25 deletions tests/languages/regex/charclass_feature.test

This file was deleted.

44 changes: 0 additions & 44 deletions tests/languages/regex/charset_feature.test

This file was deleted.

22 changes: 10 additions & 12 deletions tests/languages/regex/range_feature.test
Expand Up @@ -5,8 +5,8 @@
----------------------------------------------------

[
["charset", [
["charset-punctuation", "["],
["char-class", [
["char-class-punctuation", "["],
["range", [
"a",
["range-punctuation", "-"],
Expand All @@ -22,11 +22,10 @@
["range-punctuation", "-"],
"9"
]],
["charset-punctuation", "]"]
["char-class-punctuation", "]"]
]],

["charset", [
["charset-punctuation", "["],
["char-class", [
["char-class-punctuation", "["],
["range", [
["escape", "\\xa1"],
["range-punctuation", "-"],
Expand All @@ -37,14 +36,13 @@
["range-punctuation", "-"],
["escape", "\\u{256}"]
]],
["charset-punctuation", "]"]
["char-class-punctuation", "]"]
]],

["charset", [
["charset-punctuation", "["],
["charset-negation", "^"],
["char-class", [
["char-class-punctuation", "["],
["char-class-negation", "^"],
"-aaa-",
["charset-punctuation", "]"]
["char-class-punctuation", "]"]
]]
]

Expand Down

0 comments on commit 4dde2e2

Please sign in to comment.