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>
|
|
|
|
#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-05 07:55:31 +00:00
|
|
|
#include <iostream>
|
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;
|
|
|
|
|
|
|
|
virtual bool is_ok() const noexcept {return false;}
|
|
|
|
|
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("?");}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
const region_base& reg, const std::string& comment_for_underline)
|
2018-12-02 11:52:04 +00:00
|
|
|
{
|
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;
|
|
|
|
retval += "\n --> ";
|
|
|
|
retval += reg.name();
|
|
|
|
retval += "\n ";
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += line_number;
|
|
|
|
retval += " | ";
|
2018-12-09 03:41:38 +00:00
|
|
|
retval += line;
|
2018-12-02 11:52:04 +00:00
|
|
|
retval += '\n';
|
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-02 11:52:04 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
std::initializer_list<std::string> helps = {})
|
2018-12-02 11:52:04 +00:00
|
|
|
{
|
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;
|
|
|
|
retval += "\n --> ";
|
|
|
|
retval += loc.name();
|
|
|
|
retval += "\n ";
|
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-02 11:52:04 +00:00
|
|
|
retval += '\n';
|
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)
|
|
|
|
{
|
|
|
|
retval += '\n';
|
|
|
|
retval += make_string(line_number.size() + 1, ' ');
|
|
|
|
retval += " | ";
|
|
|
|
for(const auto help : helps)
|
|
|
|
{
|
|
|
|
retval += "\nHint: ";
|
|
|
|
retval += help;
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 11:52:04 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
} // toml
|
|
|
|
#endif// TOML11_REGION_H
|