diff --git a/es2panda/lexer/regexp/regexp.cpp b/es2panda/lexer/regexp/regexp.cpp index fb65177d4461562d7ae66f152cd8e002e7eef49d..630555df5459a7a886401b3ee56acbd6fb425acd 100644 --- a/es2panda/lexer/regexp/regexp.cpp +++ b/es2panda/lexer/regexp/regexp.cpp @@ -96,6 +96,7 @@ void RegExpParser::ParsePattern() if (iter_.HasNext()) { ThrowError("Invalid closing parenthesis"); } + ValidateNamedGroupReferences(); } void RegExpParser::ParseDisjunction() @@ -699,10 +700,16 @@ void RegExpParser::ParseNamedBackreference() } util::StringView name = ParseIdent(); + namedGroupReferences_.insert(name); +} - auto result = groupNames_.find(name); - if (result == groupNames_.end()) { - ThrowError("Invalid named capture referenced"); +void RegExpParser::ValidateNamedGroupReferences() +{ + for (auto& ref : namedGroupReferences_) { + auto result = groupNames_.find(ref); + if (result == groupNames_.end()) { + ThrowError("Invalid named capture referenced"); + } } } diff --git a/es2panda/lexer/regexp/regexp.h b/es2panda/lexer/regexp/regexp.h index 513c3405188ab5dcaed74db76a1fedf6fa150b8e..67d99c554fbe31b2a8055eaf1e0549b15fe778db 100644 --- a/es2panda/lexer/regexp/regexp.h +++ b/es2panda/lexer/regexp/regexp.h @@ -92,12 +92,14 @@ private: char32_t Next(); void Advance(); bool Eos() const; + void ValidateNamedGroupReferences(); RegExp re_; ArenaAllocator *allocator_ {}; util::StringView::Iterator iter_; uint32_t capturingGroupCount_; std::unordered_set groupNames_; + std::unordered_set namedGroupReferences_; }; } // namespace panda::es2panda::lexer