From 84676eab0ba67bd5608e5b3aac2ad3f324b12e52 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 9 Dec 2018 13:05:09 +0900 Subject: [PATCH] improve quality of error message --- toml/region.hpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/toml/region.hpp b/toml/region.hpp index a5d43a2..c97275d 100644 --- a/toml/region.hpp +++ b/toml/region.hpp @@ -3,6 +3,7 @@ #include "exception.hpp" #include #include +#include #include #include @@ -78,8 +79,8 @@ struct region_base virtual bool is_ok() const noexcept {return false;} virtual std::string str() const {return std::string("");} - virtual std::string name() const {return std::string("");} - virtual std::string line() const {return std::string("");} + virtual std::string name() const {return std::string("unknown location");} + virtual std::string line() const {return std::string("unknown line");} virtual std::string line_num() const {return std::string("?");} virtual std::size_t before() const noexcept {return 0;} @@ -187,7 +188,6 @@ struct region final : public region_base inline std::string format_underline(const std::string& message, const region_base& reg, const std::string& comment_for_underline) { - const auto line = reg.line(); const auto line_number = reg.line_num(); @@ -213,7 +213,8 @@ inline std::string format_underline(const std::string& message, template std::string format_underline(const std::string& message, const location& loc, - const std::string& comment_for_underline) + const std::string& comment_for_underline, + std::initializer_list helps = {}) { using const_iterator = typename location::const_iterator; using reverse_iterator = std::reverse_iterator; @@ -238,10 +239,20 @@ format_underline(const std::string& message, const location& loc, retval += " | "; retval += make_string(std::distance(line_begin, loc.iter()),' '); retval += '^'; - retval += make_string(std::distance(loc.iter(), line_end), '~'); + retval += make_string(std::distance(loc.iter(), line_end), '-'); retval += ' '; retval += comment_for_underline; - + if(helps.size() != 0) + { + retval += '\n'; + retval += make_string(line_number.size() + 1, ' '); + retval += " | "; + for(const auto help : helps) + { + retval += "\nHint: "; + retval += help; + } + } return retval; }