From 491e4a8a3b3761e2050f311a13c66dbebac6bece Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sat, 28 Nov 2020 12:28:36 +0330 Subject: [PATCH] LibRegex: Allow '-' as the last element of a charclass Fixes #4189. --- Libraries/LibRegex/RegexParser.cpp | 8 ++++++++ Libraries/LibRegex/Tests/Regex.cpp | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/Libraries/LibRegex/RegexParser.cpp b/Libraries/LibRegex/RegexParser.cpp index d4f906e2065..cc52649eafc 100644 --- a/Libraries/LibRegex/RegexParser.cpp +++ b/Libraries/LibRegex/RegexParser.cpp @@ -1315,6 +1315,12 @@ bool ECMA262Parser::parse_nonempty_class_ranges(Vector& if (match(TokenType::HyphenMinus)) { consume(); + if (match(TokenType::RightBracket)) { + // Allow '-' as the last element in a charclass, even after an atom. + m_parser_state.lexer.back(2); // -] + m_parser_state.current_token = m_parser_state.lexer.next(); + goto read_as_single_atom; + } auto second_atom = read_class_atom(); if (!second_atom.has_value()) return false; @@ -1336,6 +1342,8 @@ bool ECMA262Parser::parse_nonempty_class_ranges(Vector& continue; } + read_as_single_atom:; + auto atom = first_atom.value(); if (atom.is_character_class) { diff --git a/Libraries/LibRegex/Tests/Regex.cpp b/Libraries/LibRegex/Tests/Regex.cpp index b4444c06e4f..0175f70fbc7 100644 --- a/Libraries/LibRegex/Tests/Regex.cpp +++ b/Libraries/LibRegex/Tests/Regex.cpp @@ -485,6 +485,10 @@ TEST_CASE(ECMA262_parse) "^hel(?l\\w).$", "^[-a-zA-Z\\w\\s]+$", "\\bhello\\B", + "^[\\w+/_-]+[=]{0,2}$", // #4189 + "^(?:[^<]*(<[\\w\\W]+>)[^>]*$|#([\\w\\-]*)$)", // #4189 + "\\/", // #4189 + "\\x", // Even invalid escapes are allowed if ~unicode. }; for (auto& pattern : patterns) {