From 985f61c9155b85fe7c8c5ffeb0a696ef2454980b Mon Sep 17 00:00:00 2001 From: pengbiao Date: Sat, 13 Aug 2022 20:45:47 +0800 Subject: [PATCH] Bugfix for RegExp named group parsing I5M6U2 Signed-off-by: pengbiao Change-Id: Id3857aff4b3917b6fe80c1d02a913aa472ccec7e --- es2panda/lexer/regexp/regexp.cpp | 13 ++++++++++--- es2panda/lexer/regexp/regexp.h | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/es2panda/lexer/regexp/regexp.cpp b/es2panda/lexer/regexp/regexp.cpp index fb65177d44..630555df54 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 513c340518..67d99c554f 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 -- Gitee