1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:37:35 +00:00

LibRegex: Allow separately parsing patterns and creating Regex objects

Adds a static method to parse a regex pattern and return the result, and
a constructor to accept a parse result. This is to allow LibJS to parse
the pattern string of a RegExpLiteral once and hand off regex objects
any number of times thereafter.
This commit is contained in:
Timothy Flynn 2021-07-30 09:41:41 -04:00 committed by Linus Groh
parent b162517065
commit 1400e3cf58
2 changed files with 21 additions and 0 deletions

View file

@ -18,6 +18,15 @@ namespace regex {
static RegexDebug s_regex_dbg(stderr);
#endif
template<class Parser>
regex::Parser::Result Regex<Parser>::parse_pattern(StringView pattern, typename ParserTraits<Parser>::OptionsType regex_options)
{
regex::Lexer lexer(pattern);
Parser parser(lexer, regex_options);
return parser.parse();
}
template<class Parser>
Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options)
: pattern_value(move(pattern))
@ -31,6 +40,15 @@ Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType
matcher = make<Matcher<Parser>>(this, regex_options);
}
template<class Parser>
Regex<Parser>::Regex(regex::Parser::Result parse_result, String pattern, typename ParserTraits<Parser>::OptionsType regex_options)
: pattern_value(move(pattern))
, parser_result(move(parse_result))
{
if (parser_result.error == regex::Error::NoError)
matcher = make<Matcher<Parser>>(this, regex_options);
}
template<class Parser>
Regex<Parser>::Regex(Regex&& regex)
: pattern_value(move(regex.pattern_value))