From c11be92e23d899e28d45f67be24e47b2e5114d3a Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 21 Jan 2022 14:30:47 +0330 Subject: LibRegex: Implement an ECMA262 Regex quirk with negative lookarounds This implements the quirk defined by "Note 3" in section "Canonicalize" (https://tc39.es/ecma262/#sec-runtime-semantics-canonicalize-ch). Crosses off another quirk from #6042. --- Tests/LibRegex/Regex.cpp | 1 + Userland/Libraries/LibRegex/RegexMatcher.cpp | 15 +++++++++-- Userland/Libraries/LibRegex/RegexParser.cpp | 40 ++++++++++++++++++---------- Userland/Libraries/LibRegex/RegexParser.h | 14 ++++++++++ 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Tests/LibRegex/Regex.cpp b/Tests/LibRegex/Regex.cpp index 3123472bc8..d054f467e5 100644 --- a/Tests/LibRegex/Regex.cpp +++ b/Tests/LibRegex/Regex.cpp @@ -682,6 +682,7 @@ TEST_CASE(ECMA262_match) { "[\\0]"sv, "\0"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) }, { "[\\01]"sv, "\1"sv, true, ECMAScriptFlags::BrowserExtended }, { "(\0|a)"sv, "a"sv, true }, // #9686, Should allow null bytes in pattern + { "(.*?)a(?!(a+)b\\2c)\\2(.*)"sv, "baaabaac"sv, true }, // #6042, Groups inside lookarounds may be referenced outside, but their contents appear empty if the pattern in the lookaround fails. }; // clang-format on diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp index b065bc146b..26ae07d886 100644 --- a/Userland/Libraries/LibRegex/RegexMatcher.cpp +++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp @@ -486,7 +486,14 @@ Optional Matcher::execute(MatchInput const& input, MatchState& sta return true; case ExecutionResult::Failed: if (!states_to_try_next.is_empty()) { - state = states_to_try_next.take_last(); + auto next_state = states_to_try_next.take_last(); + // Note: ECMA262 quirk: Note 3, https://tc39.es/ecma262/#sec-runtime-semantics-canonicalize-ch + // capture groups defined in lookarounds "leak" outside the regex, + // but their contents are empty if the lookaround fails. + // This is done by manually clearing the groups where needed, and leaking their contents here. + if constexpr (IsSame) + swap(next_state.capture_group_matches, state.capture_group_matches); + state = move(next_state); continue; } return false; @@ -496,7 +503,11 @@ Optional Matcher::execute(MatchInput const& input, MatchState& sta return {}; return false; } - state = states_to_try_next.take_last(); + auto next_state = states_to_try_next.take_last(); + // See note above about an ECMA262 quirk. + if constexpr (IsSame) + swap(next_state.capture_group_matches, state.capture_group_matches); + state = move(next_state); ++recursion_level; continue; } diff --git a/Userland/Libraries/LibRegex/RegexParser.cpp b/Userland/Libraries/LibRegex/RegexParser.cpp index 5fc6aeb1c4..61b5ff1843 100644 --- a/Userland/Libraries/LibRegex/RegexParser.cpp +++ b/Userland/Libraries/LibRegex/RegexParser.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -1065,9 +1066,17 @@ bool ECMA262Parser::parse_assertion(ByteCode& stack, [[maybe_unused]] size_t& ma return true; } if (should_parse_forward_assertion && try_skip("!")) { + enter_capture_group_scope(); + ScopeGuard quit_scope { + [this] { + exit_capture_group_scope(); + } + }; if (!parse_inner_disjunction(assertion_stack, length_dummy, unicode, named)) return false; stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookAhead); + clear_all_capture_groups_in_scope(stack); + return true; } if (m_should_use_browser_extended_grammar) { @@ -1086,9 +1095,16 @@ bool ECMA262Parser::parse_assertion(ByteCode& stack, [[maybe_unused]] size_t& ma return true; } if (try_skip("