mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +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:
parent
b162517065
commit
1400e3cf58
2 changed files with 21 additions and 0 deletions
|
@ -18,6 +18,15 @@ namespace regex {
|
||||||
static RegexDebug s_regex_dbg(stderr);
|
static RegexDebug s_regex_dbg(stderr);
|
||||||
#endif
|
#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>
|
template<class Parser>
|
||||||
Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options)
|
Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options)
|
||||||
: pattern_value(move(pattern))
|
: pattern_value(move(pattern))
|
||||||
|
@ -31,6 +40,15 @@ Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType
|
||||||
matcher = make<Matcher<Parser>>(this, regex_options);
|
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>
|
template<class Parser>
|
||||||
Regex<Parser>::Regex(Regex&& regex)
|
Regex<Parser>::Regex(Regex&& regex)
|
||||||
: pattern_value(move(regex.pattern_value))
|
: pattern_value(move(regex.pattern_value))
|
||||||
|
|
|
@ -80,7 +80,10 @@ public:
|
||||||
OwnPtr<Matcher<Parser>> matcher { nullptr };
|
OwnPtr<Matcher<Parser>> matcher { nullptr };
|
||||||
mutable size_t start_offset { 0 };
|
mutable size_t start_offset { 0 };
|
||||||
|
|
||||||
|
static regex::Parser::Result parse_pattern(StringView pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
|
||||||
|
|
||||||
explicit Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
|
explicit Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
|
||||||
|
Regex(regex::Parser::Result parse_result, String pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
|
||||||
~Regex() = default;
|
~Regex() = default;
|
||||||
Regex(Regex&&);
|
Regex(Regex&&);
|
||||||
Regex& operator=(Regex&&);
|
Regex& operator=(Regex&&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue