diff --git a/src/regex.cc b/src/regex.cc index 1658af76..77e01d89 100644 --- a/src/regex.cc +++ b/src/regex.cc @@ -1,6 +1,7 @@ #include "regex.hh" #include "exception.hh" +#include "regex_impl.hh" namespace Kakoune { @@ -9,7 +10,9 @@ using Utf8It = RegexUtf8It; Regex::Regex(StringView re, flag_type flags) try : RegexBase{Utf8It{re.begin(), re}, Utf8It{re.end(), re}, flags}, m_str{re.str()} -{} catch (std::runtime_error& err) { throw regex_error(err.what()); } +{ + validate_regex(re); +} catch (std::runtime_error& err) { throw regex_error(err.what()); } String option_to_string(const Regex& re) { diff --git a/src/regex_impl.cc b/src/regex_impl.cc index ffc8edc8..8a61b760 100644 --- a/src/regex_impl.cc +++ b/src/regex_impl.cc @@ -8,6 +8,8 @@ #include "exception.hh" #include "array_view.hh" +#include "buffer_utils.hh" + namespace Kakoune { @@ -114,13 +116,6 @@ AstNodePtr make_ast_node(Op op, Codepoint value = -1, // standard, although the syntax is not fully compatible. struct Parser { - struct InvalidPolicy - { - Codepoint operator()(Codepoint cp) { throw runtime_error{"Invalid utf8 in regex"}; } - }; - - using Iterator = utf8::iterator; - static ParsedRegex parse(StringView re) { ParsedRegex res; @@ -131,6 +126,13 @@ struct Parser } private: + struct InvalidPolicy + { + Codepoint operator()(Codepoint cp) { throw runtime_error{"Invalid utf8 in regex"}; } + }; + + using Iterator = utf8::iterator; + static AstNodePtr disjunction(ParsedRegex& parsed_regex, Iterator& pos, Iterator end, unsigned capture = -1) { AstNodePtr node = alternative(parsed_regex, pos, end); @@ -790,6 +792,18 @@ struct ThreadedRegexVM Vector m_captures; }; +void validate_regex(StringView re) +{ + try + { + RegexCompiler::Parser::parse(re); + } + catch (runtime_error& err) + { + write_to_debug_buffer(format("regex-impl: <<{}>> failed to parse: {}", re, err.what())); + } +} + auto test_regex = UnitTest{[]{ { auto program = RegexCompiler::compile(R"(a*b)"); diff --git a/src/regex_impl.hh b/src/regex_impl.hh index 24de6907..8fafcacd 100644 --- a/src/regex_impl.hh +++ b/src/regex_impl.hh @@ -1,4 +1,13 @@ #ifndef regex_impl_hh_INCLUDED #define regex_impl_hh_INCLUDED +namespace Kakoune +{ + +class StringView; + +void validate_regex(StringView re); + +} + #endif // regex_impl_hh_INCLUDED