2018-12-13 11:44:10 +00:00
|
|
|
// Copyright Toru Niina 2017.
|
|
|
|
// Distributed under the MIT License.
|
2018-12-02 11:52:04 +00:00
|
|
|
#ifndef TOML11_REGION_H
|
|
|
|
#define TOML11_REGION_H
|
2018-12-02 15:10:26 +00:00
|
|
|
#include "exception.hpp"
|
2018-12-02 11:52:04 +00:00
|
|
|
#include <memory>
|
2018-12-09 10:30:46 +00:00
|
|
|
#include <vector>
|
2018-12-02 11:52:04 +00:00
|
|
|
#include <algorithm>
|
2018-12-09 04:05:09 +00:00
|
|
|
#include <initializer_list>
|
2018-12-02 11:52:04 +00:00
|
|
|
#include <iterator>
|
2018-12-13 08:07:03 +00:00
|
|
|
#include <iomanip>
|
2018-12-02 11:52:04 +00:00
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
2018-12-05 07:55:31 +00:00
|
|
|
// helper function to avoid std::string(0, 'c')
|
|
|
|
template<typename Iterator>
|
|
|
|
std::string make_string(Iterator first, Iterator last)
|
|
|
|
{
|
|
|
|
if(first == last) {return "";}
|
|
|
|
return std::string(first, last);
|
|
|
|
}
|
|
|
|
inline std::string make_string(std::size_t len, char c)
|
|
|
|
{
|
|
|
|
if(len == 0) {return "";}
|
|
|
|
return std::string(len, c);
|
|
|
|
}
|
|
|
|
|
2018-12-02 11:52:04 +00:00
|
|
|
// location in a container, normally in a file content.
|
|
|
|
// shared_ptr points the resource that the iter points.
|
|
|
|
// it can be used not only for resource handling, but also error message.
|
|
|
|
template<typename Container>
|
|
|
|
struct location
|
|
|
|
{
|
2018-12-02 14:16:28 +00:00
|
|
|
static_assert(std::is_same<char, typename Container::value_type>::value,"");
|
2018-12-02 11:52:04 +00:00
|
|
|
using const_iterator = typename Container::const_iterator;
|
2018-12-02 15:10:26 +00:00
|
|
|
using source_ptr = std::shared_ptr<const Container>;
|
2018-12-02 11:52:04 +00:00
|
|
|
|
2018-12-05 07:55:31 +00:00
|
|
|
location(std::string name, Container cont)
|
2018-12-02 15:10:26 +00:00
|
|
|
: source_(std::make_shared<Container>(std::move(cont))),
|
2018-12-08 15:00:15 +00:00
|
|
|
source_name_(std::move(name)), iter_(source_->cbegin())
|
2018-12-02 11:52:04 +00:00
|
|
|
{}
|
|
|
|
location(const location&) = default;
|
|
|
|
location(location&&) = default;
|
|
|
|
location& operator=(const location&) = default;
|
|
|
|
location& operator=(location&&) = default;
|
2018-12-02 15:10:26 +00:00
|
|
|
~location() = default;
|
2018-12-02 11:52:04 +00:00
|
|
|
|
2018-12-02 15:10:26 +00:00
|
|
|
const_iterator& iter() noexcept {return iter_;}
|
|
|
|
const_iterator iter() const noexcept {return iter_;}
|
|
|
|
|
2018-12-08 15:00:15 +00:00
|
|
|
const_iterator begin() const noexcept {return source_->cbegin();}
|
|
|
|
const_iterator end() const noexcept {return source_->cend();}
|
2018-12-02 15:10:26 +00:00
|
|
|
|
|
|
|
source_ptr const& source() const& noexcept {return source_;}
|
|
|
|
source_ptr&& source() && noexcept {return std::move(source_);}
|
|
|
|
|
2018-12-05 07:55:31 +00:00
|
|
|
std::string const& name() const noexcept {return source_name_;}
|
|
|
|
|
2018-12-02 15:10:26 +00:00
|
|
|
private:
|
|
|
|
|
2018-12-05 07:55:31 +00:00
|
|
|
source_ptr source_;
|
|
|
|
std::string source_name_;
|
2018-12-08 15:00:15 +00:00
|
|
|
const_iterator iter_;
|
2018-12-02 11:52:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// region in a container, normally in a file content.
|
|
|
|
// shared_ptr points the resource that the iter points.
|
|
|
|
// combinators returns this.
|
2018-12-09 03:41:38 +00:00
|
|
|
// it will be used to generate better error messages.
|
|
|
|
struct region_base
|
|
|
|
{
|
|
|
|
region_base() = default;
|
|
|
|
virtual ~region_base() = default;
|
|
|
|
region_base(const region_base&) = default;
|
|
|
|
region_base(region_base&& ) = default;
|
|
|
|
region_base& operator=(const region_base&) = default;
|
|
|
|
region_base& operator=(region_base&& ) = default;
|
|
|
|
|
2018-12-09 09:08:04 +00:00
|
|
|
virtual bool is_ok() const noexcept {return false;}
|
2018-12-09 03:41:38 +00:00
|
|
|
|
2018-12-09 04:39:13 +00:00
|
|
|
virtual std::string str() const {return std::string("unknown region");}
|
|
|
|
virtual std::string name() const {return std::string("unknown file");}
|
2018-12-09 04:05:09 +00:00
|
|
|
virtual std::string line() const {return std::string("unknown line");}
|
2018-12-09 03:41:38 +00:00
|
|
|
virtual std::string line_num() const {return std::string("?");}
|
|
|
|
|
2018-12-09 09:08:04 +00:00
|
|
|
|
2018-12-09 03:41:38 +00:00
|
|
|
virtual std::size_t before() const noexcept {return 0;}
|
|
|
|
virtual std::size_t size() const noexcept {return 0;}
|
|
|
|
virtual std::size_t after() const noexcept {return 0;}
|
|
|
|
};
|
|
|
|
|
2018-12-02 11:52:04 +00:00
|
|
|
template<typename Container>
|
2018-12-09 03:41:38 +00:00
|
|
|
struct region final : public region_base
|
2018-12-02 11:52:04 +00:00
|
|
|
{
|
2018-12-02 14:16:28 +00:00
|
|
|
static_assert(std::is_same<char, typename Container::value_type>::value,"");
|
2018-12-02 11:52:04 +00:00
|
|
|
using const_iterator = typename Container::const_iterator;
|
2018-12-02 15:10:26 +00:00
|
|
|
using source_ptr = std::shared_ptr<const Container>;
|
2018-12-02 11:52:04 +00:00
|
|
|
|
2018-12-09 03:41:38 +00:00
|
|
|
// delete default constructor. source_ never be null.
|
|
|
|
region() = delete;
|
|
|
|
|
2018-12-02 11:52:04 +00:00
|
|
|
region(const location<Container>& loc)
|
2018-12-05 07:55:31 +00:00
|
|
|
: source_(loc.source()), source_name_(loc.name()),
|
2018-12-08 15:00:15 +00:00
|
|
|
first_(loc.iter()), last_(loc.iter())
|
2018-12-02 11:52:04 +00:00
|
|
|
{}
|
|
|
|
region(location<Container>&& loc)
|
2018-12-05 07:55:31 +00:00
|
|
|
: source_(loc.source()), source_name_(loc.name()),
|
2018-12-08 15:00:15 +00:00
|
|
|
first_(loc.iter()), last_(loc.iter())
|
2018-12-02 11:52:04 +00:00
|
|
|
{}
|
|
|
|
|
2018-12-02 14:04:49 +00:00
|
|
|
region(const location<Container>& loc, const_iterator f, const_iterator l)
|
2018-12-08 15:00:15 +00:00
|
|
|
: source_(loc.source()), source_name_(loc.name()), first_(f), last_(l)
|
2018-12-02 14:04:49 +00:00
|
|
|
{}
|
|
|
|
region(location<Container>&& loc, const_iterator f, const_iterator l)
|
2018-12-08 15:00:15 +00:00
|
|
|
: source_(loc.source()), source_name_(loc.name()), first_(f), last_(l)
|
2018-12-02 14:04:49 +00:00
|
|
|
{}
|
|
|
|
|
2018-12-02 11:52:04 +00:00
|
|
|
region(const region&) = default;
|
|
|
|
region(region&&) = default;
|
|
|
|
region& operator=(const region&) = default;
|
|
|
|
region& operator=(region&&) = default;
|
|
|
|
~region() = default;
|
|
|
|
|
2018-12-02 15:10:26 +00:00
|
|
|
region& operator+=(const region& other)
|
|
|
|
{
|
2018-12-08 15:00:15 +00:00
|
|
|
if(this->begin() != other.begin() || this->end() != other.end() ||
|
2018-12-02 15:10:26 +00:00
|
|
|
this->last_ != other.first_)
|
|
|
|
{
|
|
|
|
throw internal_error("invalid region concatenation");
|
|
|
|
}
|
|
|
|
this->last_ = other.last_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:41:38 +00:00
|
|
|
bool is_ok() const noexcept override {return static_cast<bool>(source_);}
|
2018-12-02 15:10:26 +00:00
|
|
|
|
2018-12-09 03:41:38 +00:00
|
|
|
std::string str() const override {return make_string(first_, last_);}
|
|
|
|
std::string line() const override
|
|
|
|
{
|
2018-12-09 09:08:04 +00:00
|
|
|
if(this->contain_newline())
|
|
|
|
{
|
|
|
|
return make_string(this->line_begin(),
|
|
|
|
std::find(this->line_begin(), this->last(), '\n'));
|
|
|
|
}
|
2018-12-09 03:41:38 +00:00
|
|
|
return make_string(this->line_begin(), this->line_end());
|
|
|
|
}
|
|
|
|
std::string line_num() const override
|
|
|
|
{
|
|
|
|
return std::to_string(1 + std::count(this->begin(), this->first(), '\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const noexcept override
|
|
|
|
{
|
|
|
|
return std::distance(first_, last_);
|
|
|
|
}
|
|
|
|
std::size_t before() const noexcept override
|
|
|
|
{
|
|
|
|
return std::distance(this->line_begin(), this->first());
|
|
|
|
}
|
|
|
|
std::size_t after() const noexcept override
|
|
|
|
{
|
|
|
|
return std::distance(this->last(), this->line_end());
|
|
|
|
}
|
|
|
|
|
2018-12-09 09:08:04 +00:00
|
|
|
bool contain_newline() const noexcept
|
|
|
|
{
|
|
|
|
return std::find(this->first(), this->last(), '\n') != this->last();
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:41:38 +00:00
|
|
|
const_iterator line_begin() const noexcept
|
|
|
|
{
|
|
|
|
using reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
return std::find(reverse_iterator(this->first()),
|
|
|
|
reverse_iterator(this->begin()), '\n').base();
|
|
|
|
}
|
|
|
|
const_iterator line_end() const noexcept
|
|
|
|
{
|
|
|
|
return std::find(this->last(), this->end(), '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const noexcept {return source_->cbegin();}
|
|
|
|
const_iterator end() const noexcept {return source_->cend();}
|
|
|
|
const_iterator first() const noexcept {return first_;}
|
|
|
|
const_iterator last() const noexcept {return last_;}
|
2018-12-02 15:10:26 +00:00
|
|
|
|
|
|
|
source_ptr const& source() const& noexcept {return source_;}
|
|
|
|
source_ptr&& source() && noexcept {return std::move(source_);}
|
|
|
|
|
2018-12-09 03:41:38 +00:00
|
|
|
std::string name() const override {return source_name_;}
|
2018-12-05 07:55:31 +00:00
|
|
|
|
2018-12-02 15:10:26 +00:00
|
|
|
private:
|
2018-12-02 14:16:28 +00:00
|
|
|
|
2018-12-05 07:55:31 +00:00
|
|
|
source_ptr source_;
|
|
|
|
std::string source_name_;
|
2018-12-08 15:00:15 +00:00
|
|
|
const_iterator first_, last_;
|
2018-12-02 11:52:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// to show a better error message.
|
2018-12-09 03:41:38 +00:00
|
|
|
inline std::string format_underline(const std::string& message,
|
2018-12-11 13:22:07 +00:00
|
|
|
const region_base& reg, const std::string& comment_for_underline,
|
|
|
|
std::vector<std::string> helps = {})
|
2018-12-02 11:52:04 +00:00
|
|
|
{
|
2018-12-12 14:22:31 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
const auto newline = "\r\n";
|
|
|
|
#else
|
|
|
|
const char newline = '\n';
|
|
|
|
#endif
|
2018-12-09 03:41:38 +00:00
|
|
|
const auto line = reg.line();
|
|
|
|
const auto line_number = reg.line_num();
|
2018-12-02 11:52:04 +00:00
|
|
|
|
|
|
|
std::string retval;
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += message;
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
|
|
|
retval += " --> ";
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += reg.name();
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
|
|
|
retval += ' ';
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += line_number;
|
|
|
|
retval += " | ";
|
2018-12-09 03:41:38 +00:00
|
|
|
retval += line;
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += make_string(line_number.size() + 1, ' ');
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += " | ";
|
2018-12-09 03:41:38 +00:00
|
|
|
retval += make_string(reg.before(), ' ');
|
|
|
|
retval += make_string(reg.size(), '~');
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += ' ';
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += comment_for_underline;
|
2018-12-11 13:22:07 +00:00
|
|
|
if(helps.size() != 0)
|
|
|
|
{
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
2018-12-11 13:22:07 +00:00
|
|
|
retval += make_string(line_number.size() + 1, ' ');
|
|
|
|
retval += " | ";
|
|
|
|
for(const auto help : helps)
|
|
|
|
{
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
|
|
|
retval += "Hint: ";
|
2018-12-11 13:22:07 +00:00
|
|
|
retval += help;
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 11:52:04 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-12-13 08:07:03 +00:00
|
|
|
// to show a better error message.
|
|
|
|
inline std::string format_underline(const std::string& message,
|
|
|
|
const region_base& reg1, const std::string& comment_for_underline1,
|
|
|
|
const region_base& reg2, const std::string& comment_for_underline2,
|
|
|
|
std::vector<std::string> helps = {})
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
const auto newline = "\r\n";
|
|
|
|
#else
|
|
|
|
const char newline = '\n';
|
|
|
|
#endif
|
|
|
|
const auto line1 = reg1.line();
|
|
|
|
const auto line_number1 = reg1.line_num();
|
|
|
|
const auto line2 = reg2.line();
|
|
|
|
const auto line_number2 = reg2.line_num();
|
|
|
|
const auto line_num_width =
|
|
|
|
std::max(line_number1.size(), line_number2.size());
|
|
|
|
|
|
|
|
std::ostringstream retval;
|
2018-12-17 09:07:57 +00:00
|
|
|
retval << message << newline;
|
|
|
|
retval << " --> " << reg1.name() << newline;
|
2018-12-13 08:07:03 +00:00
|
|
|
// ---------------------------------------
|
|
|
|
retval << ' ' << std::setw(line_num_width) << line_number1;
|
|
|
|
retval << " | " << line1 << newline;
|
|
|
|
retval << make_string(line_num_width + 1, ' ');
|
|
|
|
retval << " | ";
|
|
|
|
retval << make_string(reg1.before(), ' ');
|
|
|
|
retval << make_string(reg1.size(), '~');
|
|
|
|
retval << ' ';
|
|
|
|
retval << comment_for_underline1 << newline;
|
|
|
|
// ---------------------------------------
|
2018-12-17 09:07:57 +00:00
|
|
|
if(reg2.name() != reg1.name())
|
|
|
|
{
|
|
|
|
retval << " --> " << reg2.name() << newline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval << " ..." << newline;
|
|
|
|
}
|
2018-12-13 08:07:03 +00:00
|
|
|
retval << ' ' << std::setw(line_num_width) << line_number2;
|
|
|
|
retval << " | " << line2 << newline;
|
|
|
|
retval << make_string(line_num_width + 1, ' ');
|
|
|
|
retval << " | ";
|
|
|
|
retval << make_string(reg2.before(), ' ');
|
|
|
|
retval << make_string(reg2.size(), '~');
|
|
|
|
retval << ' ';
|
|
|
|
retval << comment_for_underline2;
|
|
|
|
if(helps.size() != 0)
|
|
|
|
{
|
|
|
|
retval << newline;
|
|
|
|
retval << make_string(line_num_width + 1, ' ');
|
|
|
|
retval << " | ";
|
|
|
|
for(const auto help : helps)
|
|
|
|
{
|
|
|
|
retval << newline;
|
|
|
|
retval << "Hint: ";
|
|
|
|
retval << help;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-02 11:52:04 +00:00
|
|
|
// to show a better error message.
|
|
|
|
template<typename Container>
|
2018-12-05 07:55:31 +00:00
|
|
|
std::string
|
|
|
|
format_underline(const std::string& message, const location<Container>& loc,
|
2018-12-09 04:05:09 +00:00
|
|
|
const std::string& comment_for_underline,
|
2018-12-09 10:30:46 +00:00
|
|
|
std::vector<std::string> helps = {})
|
2018-12-02 11:52:04 +00:00
|
|
|
{
|
2018-12-12 14:22:31 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
const auto newline = "\r\n";
|
|
|
|
#else
|
|
|
|
const char newline = '\n';
|
|
|
|
#endif
|
2018-12-05 07:55:31 +00:00
|
|
|
using const_iterator = typename location<Container>::const_iterator;
|
2018-12-02 11:52:04 +00:00
|
|
|
using reverse_iterator = std::reverse_iterator<const_iterator>;
|
2018-12-05 07:55:31 +00:00
|
|
|
const auto line_begin = std::find(reverse_iterator(loc.iter()),
|
|
|
|
reverse_iterator(loc.begin()),
|
|
|
|
'\n').base();
|
|
|
|
const auto line_end = std::find(loc.iter(), loc.end(), '\n');
|
2018-12-02 11:52:04 +00:00
|
|
|
|
|
|
|
const auto line_number = std::to_string(
|
2018-12-05 07:55:31 +00:00
|
|
|
1 + std::count(loc.begin(), loc.iter(), '\n'));
|
2018-12-02 11:52:04 +00:00
|
|
|
|
|
|
|
std::string retval;
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += message;
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
|
|
|
retval += " --> ";
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += loc.name();
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
|
|
|
retval += ' ';
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += line_number;
|
|
|
|
retval += " | ";
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += make_string(line_begin, line_end);
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += make_string(line_number.size() + 1, ' ');
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += " | ";
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += make_string(std::distance(line_begin, loc.iter()),' ');
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += '^';
|
2018-12-09 04:05:09 +00:00
|
|
|
retval += make_string(std::distance(loc.iter(), line_end), '-');
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += ' ';
|
2018-12-05 07:55:31 +00:00
|
|
|
retval += comment_for_underline;
|
2018-12-09 04:05:09 +00:00
|
|
|
if(helps.size() != 0)
|
|
|
|
{
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
2018-12-09 04:05:09 +00:00
|
|
|
retval += make_string(line_number.size() + 1, ' ');
|
|
|
|
retval += " | ";
|
|
|
|
for(const auto help : helps)
|
|
|
|
{
|
2018-12-12 14:22:31 +00:00
|
|
|
retval += newline;
|
|
|
|
retval += "Hint: ";
|
2018-12-09 04:05:09 +00:00
|
|
|
retval += help;
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 11:52:04 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
} // toml
|
|
|
|
#endif// TOML11_REGION_H
|