enable to show err msg for 2 different location

This commit is contained in:
ToruNiina 2018-12-13 17:07:03 +09:00
parent acc7b7870f
commit e5c29c2870

View File

@ -6,7 +6,7 @@
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <iostream>
#include <iomanip>
namespace toml
{
@ -241,6 +241,64 @@ inline std::string format_underline(const std::string& message,
return retval;
}
// 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;
retval << message;
retval << newline;
retval << " --> ";
retval << reg1.name() << newline;;
// ---------------------------------------
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;
// ---------------------------------------
retval << " ..." << newline;
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();
}
// to show a better error message.
template<typename Container>
std::string