Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug in parsing regex character classes that start with ^ and contain an unescaped ] #4241

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ class RegexParser(pattern: String) {
case '[' =>
// treat as a literal character and add to the character class
characterClass.append(ch)
case ']' if pos > start + 1 =>
case ']' if (!characterClass.negated && pos > start + 1) ||
(characterClass.negated && pos > start + 2) =>
// "[]" is not a valid character class
// "[]a]" is a valid character class containing the characters "]" and "a"
// "[^]a]" is a valid negated character class containing the characters "]" and "a"
characterClassComplete = true
case '^' if pos == start + 1 =>
// Negates the character class, causing it to match a single character not listed in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ class RegularExpressionParserSuite extends FunSuite {
RegexCharacterRange('A', 'Z'))))))
}

test("character class complex example") {
assert(parse("[^]+d]+") === RegexSequence(ListBuffer(
RegexRepetition(
RegexCharacterClass(negated = true,
ListBuffer(RegexChar(']'), RegexChar('+'), RegexChar('d'))),
SimpleQuantifier('+')))))
}

test("character classes containing ']'") {
// "[]a]" is a valid character class containing ']' and 'a'
assert(parse("[]a]") ===
RegexSequence(ListBuffer(
RegexCharacterClass(negated = false,
ListBuffer(RegexChar(']'), RegexChar('a'))))))
// "[^]a]" is a valid negated character class containing ']' and 'a'
assert(parse("[^]a]") ===
RegexSequence(ListBuffer(
RegexCharacterClass(negated = true,
ListBuffer(RegexChar(']'), RegexChar('a'))))))
// "[a]]" is a valid character class "[a]" followed by character ']'
assert(parse("[a]]") ===
RegexSequence(ListBuffer(
Expand Down