Regex: whenever Kakoune compiles a regex, pass it to the custom impl as well

That way we can see which features are missing.
This commit is contained in:
Maxime Coste 2017-09-26 16:44:30 +09:00
parent 002aba562f
commit eb1015cdfb
3 changed files with 34 additions and 8 deletions

View File

@ -1,6 +1,7 @@
#include "regex.hh"
#include "exception.hh"
#include "regex_impl.hh"
namespace Kakoune
{
@ -9,7 +10,9 @@ using Utf8It = RegexUtf8It<const char*>;
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)
{

View File

@ -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<const char*, Codepoint, int, InvalidPolicy>;
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<const char*, Codepoint, int, InvalidPolicy>;
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<const char*> 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)");

View File

@ -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