Transform boost/std regex_error to Kakoune::regex_error at Regex construction

Fixes #318
This commit is contained in:
Maxime Coste 2015-07-14 21:06:41 +01:00
parent 7d9ec52bf2
commit 17e3be48a5
4 changed files with 62 additions and 94 deletions

View File

@ -253,8 +253,6 @@ public:
if (params.size() < 2)
throw runtime_error("wrong parameter count");
try
{
FacesSpec faces;
for (auto it = params.begin() + 1; it != params.end(); ++it)
{
@ -274,11 +272,6 @@ public:
return {id, make_unique<RegexHighlighter>(std::move(ex),
std::move(faces))};
}
catch (RegexError& err)
{
throw runtime_error(StringView{"regex error: "} + err.what());
}
}
private:
// stores the range for each highlighted capture of each match
@ -465,7 +458,7 @@ HighlighterAndId create_search_highlighter(HighlighterParameters params)
{
return s.empty() ? Regex{} : Regex{s.begin(), s.end()};
}
catch (RegexError& err)
catch (regex_error& err)
{
return Regex{};
}
@ -1169,8 +1162,6 @@ public:
}
static HighlighterAndId create(HighlighterParameters params)
{
try
{
static const ParameterDesc param_desc{
{ { "default", { true, "" } } },
@ -1199,11 +1190,6 @@ public:
auto default_group = parser.get_switch("default").value_or(StringView{}).str();
return {parser[0], make_unique<RegionsHighlighter>(std::move(regions), default_group)};
}
catch (RegexError& err)
{
throw runtime_error(StringView{"regex error: "} + err.what());
}
}
private:
const NamedRegionDescList m_regions;

View File

@ -596,27 +596,13 @@ void regex_prompt(Context& context, const String prompt, T func)
: Regex{str.begin(), str.end()};
func(std::move(regex), event, context);
}
catch (RegexError& err)
catch (regex_error& err)
{
if (event == PromptEvent::Validate)
throw runtime_error(format("regex error: {}", err.what()));
throw;
else
context.input_handler().set_prompt_face(get_face("Error"));
}
catch (std::runtime_error& err)
{
if (event == PromptEvent::Validate)
throw runtime_error(format("regex error: {}", err.what()));
else
{
context.input_handler().set_prompt_face(get_face("Error"));
if (context.has_ui())
{
Face face = get_face("Information");
context.ui().info_show("regex error", err.what(), CharCoord{}, face, InfoStyle::Prompt);
}
}
}
catch (runtime_error&)
{
context.selections_write_only() = selections;
@ -647,19 +633,12 @@ void search_next(Context& context, NormalParams params)
{
StringView str = context.main_sel_register_value("/");
if (not str.empty())
{
try
{
Regex ex{str.begin(), str.end()};
do {
select_next_match<direction, mode>(context.buffer(), context.selections(), ex);
} while (--params.count > 0);
}
catch (RegexError& err)
{
throw runtime_error(format("regex error: ", err.what()));
}
}
else
throw runtime_error("no search pattern");
}

View File

@ -13,14 +13,7 @@ String option_to_string(const Regex& re)
void option_from_string(StringView str, Regex& re)
{
try
{
re = Regex{str.begin(), str.end()};
}
catch (RegexError& err)
{
throw runtime_error(format("unable to create regex: {}", err.what()));
}
}
}

View File

@ -2,6 +2,7 @@
#define regex_hh_INCLUDED
#include "string.hh"
#include "exception.hh"
#ifdef KAK_USE_STDREGEX
#include <regex>
@ -12,18 +13,27 @@
namespace Kakoune
{
struct regex_error : runtime_error
{
regex_error(StringView desc)
: runtime_error{format("regex error: '{}'", desc)}
{}
};
#ifdef KAK_USE_STDREGEX
// Regex that keeps track of its string representation
struct Regex : std::regex
{
Regex() = default;
explicit Regex(StringView re, flag_type flags = ECMAScript)
explicit Regex(StringView re, flag_type flags = ECMAScript) try
: std::regex(re.begin(), re.end(), flags), m_str(re) {}
catch (std::runtime_error& err) { throw regex_error(err.what()); }
template<typename Iterator>
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript)
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript) try
: std::regex(begin, end, flags), m_str(begin, end) {}
catch (std::runtime_error& err) { throw regex_error(err.what()); }
bool empty() const { return m_str.empty(); }
bool operator==(const Regex& other) const { return m_str == other.m_str; }
@ -40,12 +50,14 @@ struct Regex : boost::regex
{
Regex() = default;
explicit Regex(StringView re, flag_type flags = ECMAScript)
explicit Regex(StringView re, flag_type flags = ECMAScript) try
: boost::regex(re.begin(), re.end(), flags) {}
catch (std::runtime_error& err) { throw regex_error(err.what()); }
template<typename Iterator>
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript)
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript) try
: boost::regex(begin, end, flags) {}
catch (std::runtime_error& err) { throw regex_error(err.what()); }
String str() const { auto s = boost::regex::str(); return {s.begin(), s.end()}; }
};
@ -58,8 +70,6 @@ using RegexIterator = regex_ns::regex_iterator<Iterator>;
template<typename Iterator>
using MatchResults = regex_ns::match_results<Iterator>;
using RegexError = regex_ns::regex_error;
String option_to_string(const Regex& re);
void option_from_string(StringView str, Regex& re);