2014-10-13 16:12:33 +04:00
|
|
|
#ifndef regex_hh_INCLUDED
|
|
|
|
#define regex_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "string.hh"
|
2015-07-14 23:06:41 +03:00
|
|
|
#include "exception.hh"
|
2016-05-10 11:12:30 +03:00
|
|
|
#include "utf8_iterator.hh"
|
2014-10-13 16:12:33 +04:00
|
|
|
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-07-14 23:06:41 +03:00
|
|
|
struct regex_error : runtime_error
|
|
|
|
{
|
|
|
|
regex_error(StringView desc)
|
|
|
|
: runtime_error{format("regex error: '{}'", desc)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2016-05-19 23:45:23 +03:00
|
|
|
using RegexBase = boost::basic_regex<wchar_t, boost::c_regex_traits<wchar_t>>;
|
|
|
|
|
2014-10-13 22:40:27 +04:00
|
|
|
// Regex that keeps track of its string representation
|
2016-12-15 02:50:29 +03:00
|
|
|
class Regex : public RegexBase
|
2014-10-13 22:40:27 +04:00
|
|
|
{
|
2016-12-14 23:59:39 +03:00
|
|
|
public:
|
2014-10-13 22:40:27 +04:00
|
|
|
Regex() = default;
|
|
|
|
|
2016-05-10 11:12:30 +03:00
|
|
|
explicit Regex(StringView re, flag_type flags = ECMAScript);
|
2014-10-13 22:40:27 +04:00
|
|
|
bool empty() const { return m_str.empty(); }
|
2015-04-09 01:16:27 +03:00
|
|
|
bool operator==(const Regex& other) const { return m_str == other.m_str; }
|
|
|
|
bool operator!=(const Regex& other) const { return m_str != other.m_str; }
|
2014-10-13 22:40:27 +04:00
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
const String& str() const { return m_str; }
|
2014-10-13 22:40:27 +04:00
|
|
|
|
2016-08-19 00:42:31 +03:00
|
|
|
static constexpr const char* option_type_name = "regex";
|
2016-08-06 11:05:50 +03:00
|
|
|
|
2014-10-13 22:40:27 +04:00
|
|
|
private:
|
|
|
|
String m_str;
|
|
|
|
};
|
2015-03-10 22:33:46 +03:00
|
|
|
|
2016-05-10 11:12:30 +03:00
|
|
|
template<typename It>
|
|
|
|
using RegexUtf8It = utf8::iterator<It, wchar_t, ssize_t>;
|
2015-03-10 22:33:46 +03:00
|
|
|
|
2016-05-19 23:45:23 +03:00
|
|
|
template<typename It>
|
|
|
|
using RegexIteratorBase = boost::regex_iterator<RegexUtf8It<It>, wchar_t,
|
|
|
|
boost::c_regex_traits<wchar_t>>;
|
|
|
|
|
2016-05-10 11:12:30 +03:00
|
|
|
namespace RegexConstant = boost::regex_constants;
|
2014-10-13 16:12:33 +04:00
|
|
|
|
|
|
|
template<typename Iterator>
|
2016-05-10 11:12:30 +03:00
|
|
|
struct MatchResults : boost::match_results<RegexUtf8It<Iterator>>
|
|
|
|
{
|
|
|
|
using ParentType = boost::match_results<RegexUtf8It<Iterator>>;
|
|
|
|
struct SubMatch : std::pair<Iterator, Iterator>
|
|
|
|
{
|
|
|
|
SubMatch() = default;
|
|
|
|
SubMatch(const boost::sub_match<RegexUtf8It<Iterator>>& m)
|
|
|
|
: std::pair<Iterator, Iterator>{m.first.base(), m.second.base()},
|
|
|
|
matched{m.matched}
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool matched = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct iterator : boost::match_results<RegexUtf8It<Iterator>>::iterator
|
|
|
|
{
|
|
|
|
using ParentType = typename boost::match_results<RegexUtf8It<Iterator>>::iterator;
|
|
|
|
iterator(const ParentType& it) : ParentType(it) {}
|
|
|
|
|
|
|
|
SubMatch operator*() const { return {ParentType::operator*()}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator begin() const { return {ParentType::begin()}; }
|
|
|
|
iterator cbegin() const { return {ParentType::cbegin()}; }
|
|
|
|
iterator end() const { return {ParentType::end()}; }
|
|
|
|
iterator cend() const { return {ParentType::cend()}; }
|
|
|
|
|
|
|
|
SubMatch operator[](size_t s) const { return {ParentType::operator[](s)}; }
|
|
|
|
};
|
2014-10-13 16:12:33 +04:00
|
|
|
|
|
|
|
template<typename Iterator>
|
2016-05-19 23:45:23 +03:00
|
|
|
struct RegexIterator : RegexIteratorBase<Iterator>
|
2016-05-10 11:12:30 +03:00
|
|
|
{
|
|
|
|
using Utf8It = RegexUtf8It<Iterator>;
|
|
|
|
using ValueType = MatchResults<Iterator>;
|
|
|
|
|
|
|
|
RegexIterator() = default;
|
|
|
|
RegexIterator(Iterator begin, Iterator end, const Regex& re,
|
|
|
|
RegexConstant::match_flag_type flags = RegexConstant::match_default)
|
2016-05-19 23:45:23 +03:00
|
|
|
: RegexIteratorBase<Iterator>{Utf8It{begin, begin, end}, Utf8It{end, begin, end}, re, flags} {}
|
2014-10-13 16:12:33 +04:00
|
|
|
|
2016-05-19 23:45:23 +03:00
|
|
|
const ValueType& operator*() const { return *reinterpret_cast<const ValueType*>(&RegexIteratorBase<Iterator>::operator*()); }
|
|
|
|
const ValueType* operator->() const { return reinterpret_cast<const ValueType*>(RegexIteratorBase<Iterator>::operator->()); }
|
2016-05-10 11:12:30 +03:00
|
|
|
};
|
2015-12-24 00:43:07 +03:00
|
|
|
|
2016-05-10 11:12:30 +03:00
|
|
|
inline RegexConstant::match_flag_type match_flags(bool bol, bool eol, bool bow, bool eow)
|
2015-12-24 00:43:07 +03:00
|
|
|
{
|
2016-05-10 11:12:30 +03:00
|
|
|
return (bol ? RegexConstant::match_default : RegexConstant::match_not_bol) |
|
2015-12-24 00:43:07 +03:00
|
|
|
(eol ? RegexConstant::match_default : RegexConstant::match_not_eol) |
|
2016-05-10 11:12:30 +03:00
|
|
|
(bow ? RegexConstant::match_default : RegexConstant::match_not_bow) |
|
2015-12-24 00:43:07 +03:00
|
|
|
(eow ? RegexConstant::match_default : RegexConstant::match_not_eow);
|
|
|
|
}
|
|
|
|
|
2016-05-10 11:12:30 +03:00
|
|
|
template<typename It>
|
|
|
|
bool regex_match(It begin, It end, const Regex& re)
|
|
|
|
{
|
|
|
|
using Utf8It = RegexUtf8It<It>;
|
2016-09-06 15:55:14 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return boost::regex_match(Utf8It{begin, begin, end}, Utf8It{end, begin, end}, re);
|
|
|
|
}
|
|
|
|
catch (std::runtime_error& err)
|
|
|
|
{
|
|
|
|
throw runtime_error{format("Regex matching error: {}", err.what())};
|
|
|
|
}
|
2016-05-10 11:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
|
|
|
bool regex_match(It begin, It end, MatchResults<It>& res, const Regex& re)
|
|
|
|
{
|
|
|
|
using Utf8It = RegexUtf8It<It>;
|
2016-09-06 15:55:14 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return boost::regex_match(Utf8It{begin, begin, end}, Utf8It{end, begin, end}, res, re);
|
|
|
|
}
|
|
|
|
catch (std::runtime_error& err)
|
|
|
|
{
|
|
|
|
throw runtime_error{format("Regex matching error: {}", err.what())};
|
|
|
|
}
|
2016-05-10 11:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
|
|
|
bool regex_search(It begin, It end, const Regex& re,
|
|
|
|
RegexConstant::match_flag_type flags = RegexConstant::match_default)
|
|
|
|
{
|
|
|
|
using Utf8It = RegexUtf8It<It>;
|
2016-09-06 15:55:14 +03:00
|
|
|
try
|
|
|
|
{
|
2016-11-28 22:57:57 +03:00
|
|
|
return boost::regex_search(Utf8It{begin, begin, end}, Utf8It{end, begin, end}, re, flags);
|
2016-09-06 15:55:14 +03:00
|
|
|
}
|
|
|
|
catch (std::runtime_error& err)
|
|
|
|
{
|
|
|
|
throw runtime_error{format("Regex searching error: {}", err.what())};
|
|
|
|
}
|
2016-05-10 11:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
|
|
|
bool regex_search(It begin, It end, MatchResults<It>& res, const Regex& re,
|
|
|
|
RegexConstant::match_flag_type flags = RegexConstant::match_default)
|
|
|
|
{
|
|
|
|
using Utf8It = RegexUtf8It<It>;
|
2016-09-06 15:55:14 +03:00
|
|
|
try
|
|
|
|
{
|
2016-11-28 22:57:57 +03:00
|
|
|
return boost::regex_search(Utf8It{begin, begin, end}, Utf8It{end, begin, end}, res, re, flags);
|
2016-09-06 15:55:14 +03:00
|
|
|
}
|
|
|
|
catch (std::runtime_error& err)
|
|
|
|
{
|
|
|
|
throw runtime_error{format("Regex searching error: {}", err.what())};
|
|
|
|
}
|
2016-05-10 11:12:30 +03:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:12:33 +04:00
|
|
|
String option_to_string(const Regex& re);
|
|
|
|
void option_from_string(StringView str, Regex& re);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // regex_hh_INCLUDED
|