Merge branch 'colorize-err-msg'

This commit is contained in:
ToruNiina 2019-12-13 20:23:27 +09:00
commit a6d24b02d5
7 changed files with 286 additions and 132 deletions

View File

@ -12,7 +12,7 @@ jobs:
command: |
g++ --version
cd tests/
g++ -std=c++11 -O2 -Wall -Wextra -Werror -I../ check_toml_test.cpp -o check_toml_test
g++ -std=c++11 -O2 -Wall -Wextra -Werror -DTOML11_COLORIZE_ERROR_MESSAGE -I../ check_toml_test.cpp -o check_toml_test
go get github.com/BurntSushi/toml-test
$GOPATH/bin/toml-test ./check_toml_test
test_serialization:
@ -24,7 +24,7 @@ jobs:
command: |
g++ --version
cd tests/
g++ -std=c++11 -O2 -Wall -Wextra -Wpedantic -Werror -I../ check_serialization.cpp -o check_serialization
g++ -std=c++11 -O2 -Wall -Wextra -Wpedantic -Werror -DTOML11_COLORIZE_ERROR_MESSAGE -I../ check_serialization.cpp -o check_serialization
git clone https://github.com/BurntSushi/toml-test.git
cp check_serialization toml-test/tests/valid
cd toml-test/tests/valid
@ -47,7 +47,7 @@ jobs:
command: |
g++ --version
cd tests/
g++ -std=c++11 -O2 -Wall -Wextra -Wpedantic -Werror -I../ check.cpp -o check
g++ -std=c++11 -O2 -Wall -Wextra -Wpedantic -Werror -DTOML11_COLORIZE_ERROR_MESSAGE -I../ check.cpp -o check
git clone https://github.com/BurntSushi/toml-test.git
cp check toml-test/tests/invalid
cp check toml-test/tests/valid

View File

@ -70,6 +70,7 @@ int main()
- [Formatting user-defined error messages](#formatting-user-defined-error-messages)
- [Obtaining location information](#obtaining-location-information)
- [Exceptions](#exceptions)
- [Colorize Error Messages](#colorize-error-messages)
- [Serializing TOML data](#serializing-toml-data)
- [Underlying types](#underlying-types)
- [Unreleased TOML features](#unreleased-toml-features)
@ -1375,6 +1376,63 @@ struct exception : public std::exception
It represents where the error occurs.
## Colorize Error Messages
By defining `TOML11_COLORIZE_ERROR_MESSAGE`, the error messages from
`toml::parse` and `toml::find|get` will be colorized. By default, this feature
is turned off.
With the following toml file taken from `toml-lang/toml/tests/hard_example.toml`,
```toml
[error]
array = [
"This might most likely happen in multiline arrays",
Like here,
"or here,
and here"
] End of array comment, forgot the #
```
the error message would be like this.
![error-message-1](https://github.com/ToruNiina/toml11/blob/misc/misc/toml11-err-msg-1.png)
With the following,
```toml
[error]
# array = [
# "This might most likely happen in multiline arrays",
# Like here,
# "or here,
# and here"
# ] End of array comment, forgot the #
number = 3.14 pi <--again forgot the #
```
the error message would be like this.
![error-message-2](https://github.com/ToruNiina/toml11/blob/misc/misc/toml11-err-msg-2.png)
The message would be messy when it is written to a file, not a terminal because
it uses [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code).
Without `TOML11_COLORIZE_ERROR_MESSAGE`, you can still colorize user-defined
error message by passing `true` to the `toml::format_error` function.
If you define `TOML11_COLORIZE_ERROR_MESSAGE`, the value is `true` by default.
If not, the defalut value would be `false`.
```cpp
std::cerr << toml::format_error("[error] value should be positive",
data.at("num"), "positive number required",
hints, /*colorize = */ true) << std::endl;
```
Note: It colorize `[error]` in red. That means that it detects `[error]` prefix
at the front of the error message. If there is no `[error]` prefix,
`format_error` adds it to the error message.
## Serializing TOML data
toml11 enables you to serialize data into toml format.

64
toml/color.hpp Normal file
View File

@ -0,0 +1,64 @@
#ifndef TOML11_COLOR_HPP
#define TOML11_COLOR_HPP
#include <ostream>
#include <cstdint>
#ifdef TOML11_COLORIZE_ERROR_MESSAGE
#define TOML11_ERROR_MESSAGE_COLORIZED true
#else
#define TOML11_ERROR_MESSAGE_COLORIZED false
#endif
namespace toml
{
// put ANSI escape sequence to ostream
namespace color_ansi
{
namespace detail
{
inline int colorize_index()
{
static const int index = std::ios_base::xalloc();
return index;
}
} // detail
inline std::ostream& colorize(std::ostream& os)
{
// by default, it is zero.
os.iword(detail::colorize_index()) = 1;
return os;
}
inline std::ostream& nocolorize(std::ostream& os)
{
os.iword(detail::colorize_index()) = 0;
return os;
}
inline std::ostream& reset (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[00m";} return os;}
inline std::ostream& bold (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[01m";} return os;}
inline std::ostream& grey (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[30m";} return os;}
inline std::ostream& red (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[31m";} return os;}
inline std::ostream& green (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[32m";} return os;}
inline std::ostream& yellow (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[33m";} return os;}
inline std::ostream& blue (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[34m";} return os;}
inline std::ostream& magenta(std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[35m";} return os;}
inline std::ostream& cyan (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[36m";} return os;}
inline std::ostream& white (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[37m";} return os;}
} // color_ansi
// ANSI escape sequence is the only and default colorization method currently
namespace color = color_ansi;
} // toml
#endif// TOML11_COLOR_HPP

View File

@ -186,7 +186,7 @@ get(const basic_value<C, M, V>& v)
}
default:
{
throw type_error(detail::format_underline("[error] toml::value "
throw type_error(detail::format_underline("toml::value: "
"bad_cast to std::chrono::system_clock::time_point", {
{std::addressof(detail::get_region(v)),
concat_to_string("the actual type is ", v.type())}
@ -301,7 +301,7 @@ get(const basic_value<C, M, V>& v)
if(ar.size() != container.size())
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[erorr] toml::get specified container size is ", container.size(),
"toml::get: specified container size is ", container.size(),
" but there are ", ar.size(), " elements in toml array."), {
{std::addressof(detail::get_region(v)), "here"}
}));
@ -326,7 +326,7 @@ get(const basic_value<C, M, V>& v)
if(ar.size() != 2)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[erorr] toml::get specified std::pair but there are ", ar.size(),
"toml::get: specified std::pair but there are ", ar.size(),
" elements in toml array."), {
{std::addressof(detail::get_region(v)), "here"}
}));
@ -357,7 +357,7 @@ get(const basic_value<C, M, V>& v)
if(ar.size() != std::tuple_size<T>::value)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] toml::get specified std::tuple with ",
"toml::get: specified std::tuple with ",
std::tuple_size<T>::value, " elements, but there are ", ar.size(),
" elements in toml array."), {
{std::addressof(detail::get_region(v)), "here"}
@ -430,7 +430,7 @@ basic_value<C, M, V> const& find(const basic_value<C, M, V>& v, const key& ky)
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), {
"key \"", ky, "\" not found"), {
{std::addressof(detail::get_region(v)), "in this table"}
}));
}
@ -444,7 +444,7 @@ basic_value<C, M, V>& find(basic_value<C, M, V>& v, const key& ky)
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), {
"key \"", ky, "\" not found"), {
{std::addressof(detail::get_region(v)), "in this table"}
}));
}
@ -458,7 +458,7 @@ basic_value<C, M, V> find(basic_value<C, M, V>&& v, const key& ky)
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), {
"key \"", ky, "\" not found"), {
{std::addressof(detail::get_region(v)), "in this table"}
}));
}
@ -477,7 +477,7 @@ find(const basic_value<C, M, V>& v, const key& ky)
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), {
"key \"", ky, "\" not found"), {
{std::addressof(detail::get_region(v)), "in this table"}
}));
}
@ -493,7 +493,7 @@ find(basic_value<C, M, V>& v, const key& ky)
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), {
"key \"", ky, "\" not found"), {
{std::addressof(detail::get_region(v)), "in this table"}
}));
}
@ -509,7 +509,7 @@ find(basic_value<C, M, V>&& v, const key& ky)
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), {
"key \"", ky, "\" not found"), {
{std::addressof(detail::get_region(v)), "in this table"}
}));
}

View File

@ -30,13 +30,13 @@ parse_boolean(location<Container>& loc)
else // internal error.
{
throw internal_error(format_underline(
"[error] toml::parse_boolean: internal error",
"toml::parse_boolean: internal error",
{{std::addressof(reg), "invalid token"}}),
source_location(std::addressof(reg)));
}
}
loc.reset(first); //rollback
return err(format_underline("[error] toml::parse_boolean: ",
return err(format_underline("toml::parse_boolean: ",
{{std::addressof(loc), "the next token is not a boolean"}}));
}
@ -58,7 +58,7 @@ parse_binary_integer(location<Container>& loc)
else // internal error.
{
throw internal_error(format_underline(
"[error] toml::parse_integer: internal error",
"toml::parse_integer: internal error",
{{std::addressof(token.unwrap()), "invalid token"}}),
source_location(std::addressof(loc)));
}
@ -66,7 +66,7 @@ parse_binary_integer(location<Container>& loc)
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_binary_integer:",
return err(format_underline("toml::parse_binary_integer:",
{{std::addressof(loc), "the next token is not an integer"}}));
}
@ -87,7 +87,7 @@ parse_octal_integer(location<Container>& loc)
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_octal_integer:",
return err(format_underline("toml::parse_octal_integer:",
{{std::addressof(loc), "the next token is not an integer"}}));
}
@ -108,7 +108,7 @@ parse_hexadecimal_integer(location<Container>& loc)
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_hexadecimal_integer",
return err(format_underline("toml::parse_hexadecimal_integer",
{{std::addressof(loc), "the next token is not an integer"}}));
}
@ -131,13 +131,13 @@ parse_integer(location<Container>& loc)
if(std::isdigit(*second))
{
return err(format_underline("[error] toml::parse_integer: "
return err(format_underline("toml::parse_integer: "
"leading zero in an Integer is not allowed.",
{{std::addressof(loc), "leading zero"}}));
}
else if(std::isalpha(*second))
{
return err(format_underline("[error] toml::parse_integer: "
return err(format_underline("toml::parse_integer: "
"unknown integer prefix appeared.",
{{std::addressof(loc), "none of 0x, 0o, 0b"}}));
}
@ -154,7 +154,7 @@ parse_integer(location<Container>& loc)
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_integer: ",
return err(format_underline("toml::parse_integer: ",
{{std::addressof(loc), "the next token is not an integer"}}));
}
@ -243,7 +243,7 @@ parse_floating(location<Container>& loc)
return ok(std::make_pair(v, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_floating: ",
return err(format_underline("toml::parse_floating: ",
{{std::addressof(loc), "the next token is not a float"}}));
}
@ -276,7 +276,7 @@ std::string read_utf8_codepoint(const region<Container>& reg,
{
if(0xD800 <= codepoint && codepoint <= 0xDFFF)
{
throw syntax_error(format_underline("[error] "
throw syntax_error(format_underline(
"toml::read_utf8_codepoint: codepoints in the range "
"[0xD800, 0xDFFF] are not valid UTF-8.", {{
std::addressof(loc), "not a valid UTF-8 codepoint"
@ -298,7 +298,7 @@ std::string read_utf8_codepoint(const region<Container>& reg,
}
else // out of UTF-8 region
{
throw syntax_error(format_underline("[error] toml::read_utf8_codepoint:"
throw syntax_error(format_underline("toml::read_utf8_codepoint:"
" input codepoint is too large.",
{{std::addressof(loc), "should be in [0x00..0x10FFFF]"}}),
source_location(std::addressof(loc)));
@ -312,7 +312,7 @@ result<std::string, std::string> parse_escape_sequence(location<Container>& loc)
const auto first = loc.iter();
if(first == loc.end() || *first != '\\')
{
return err(format_underline("[error]: toml::parse_escape_sequence: ", {{
return err(format_underline("toml::parse_escape_sequence: ", {{
std::addressof(loc), "the next token is not a backslash \"\\\""}}));
}
loc.advance();
@ -333,7 +333,7 @@ result<std::string, std::string> parse_escape_sequence(location<Container>& loc)
}
else
{
return err(format_underline("[error] parse_escape_sequence: "
return err(format_underline("parse_escape_sequence: "
"invalid token found in UTF-8 codepoint uXXXX.",
{{std::addressof(loc), "here"}}));
}
@ -346,14 +346,14 @@ result<std::string, std::string> parse_escape_sequence(location<Container>& loc)
}
else
{
return err(format_underline("[error] parse_escape_sequence: "
return err(format_underline("parse_escape_sequence: "
"invalid token found in UTF-8 codepoint Uxxxxxxxx",
{{std::addressof(loc), "here"}}));
}
}
}
const auto msg = format_underline("[error] parse_escape_sequence: "
const auto msg = format_underline("parse_escape_sequence: "
"unknown escape sequence appeared.", {{std::addressof(loc),
"escape sequence is one of \\, \", b, t, n, f, r, uxxxx, Uxxxxxxxx"}},
/* Hints = */{"if you want to write backslash as just one backslash, "
@ -378,7 +378,7 @@ parse_ml_basic_string(location<Container>& loc)
auto delim = lex_ml_basic_string_delim::invoke(inner_loc);
if(!delim)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_ml_basic_string: invalid token",
{{std::addressof(inner_loc), "should be \"\"\""}}),
source_location(std::addressof(inner_loc)));
@ -405,7 +405,7 @@ parse_ml_basic_string(location<Container>& loc)
}
if(inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_ml_basic_string: unexpected end of region",
{{std::addressof(inner_loc), "not sufficient token"}}),
source_location(std::addressof(inner_loc)));
@ -417,7 +417,7 @@ parse_ml_basic_string(location<Container>& loc)
else
{
loc.reset(first);
return err(format_underline("[error] toml::parse_ml_basic_string: "
return err(format_underline("toml::parse_ml_basic_string: "
"the next token is not a valid multiline string",
{{std::addressof(loc), "here"}}));
}
@ -436,7 +436,7 @@ parse_basic_string(location<Container>& loc)
auto quot = lex_quotation_mark::invoke(inner_loc);
if(!quot)
{
throw internal_error(format_underline("[error] parse_basic_string: "
throw internal_error(format_underline("parse_basic_string: "
"invalid token", {{std::addressof(inner_loc), "should be \""}}),
source_location(std::addressof(inner_loc)));
}
@ -458,7 +458,7 @@ parse_basic_string(location<Container>& loc)
}
if(inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_basic_string: unexpected end of region",
{{std::addressof(inner_loc), "not sufficient token"}}),
source_location(std::addressof(inner_loc)));
@ -470,7 +470,7 @@ parse_basic_string(location<Container>& loc)
else
{
loc.reset(first); // rollback
return err(format_underline("[error] toml::parse_basic_string: "
return err(format_underline("toml::parse_basic_string: "
"the next token is not a valid string",
{{std::addressof(loc), "here"}}));
}
@ -488,7 +488,7 @@ parse_ml_literal_string(location<Container>& loc)
const auto open = lex_ml_literal_string_delim::invoke(inner_loc);
if(!open)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_ml_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '''"}}),
source_location(std::addressof(inner_loc)));
@ -501,7 +501,7 @@ parse_ml_literal_string(location<Container>& loc)
const auto close = lex_ml_literal_string_delim::invoke(inner_loc);
if(!close)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_ml_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '''"}}),
source_location(std::addressof(inner_loc)));
@ -513,7 +513,7 @@ parse_ml_literal_string(location<Container>& loc)
else
{
loc.reset(first); // rollback
return err(format_underline("[error] toml::parse_ml_literal_string: "
return err(format_underline("toml::parse_ml_literal_string: "
"the next token is not a valid multiline literal string",
{{std::addressof(loc), "here"}}));
}
@ -531,7 +531,7 @@ parse_literal_string(location<Container>& loc)
const auto open = lex_apostrophe::invoke(inner_loc);
if(!open)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '"}}),
source_location(std::addressof(inner_loc)));
@ -542,7 +542,7 @@ parse_literal_string(location<Container>& loc)
const auto close = lex_apostrophe::invoke(inner_loc);
if(!close)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"parse_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '"}}),
source_location(std::addressof(inner_loc)));
@ -554,7 +554,7 @@ parse_literal_string(location<Container>& loc)
else
{
loc.reset(first); // rollback
return err(format_underline("[error] toml::parse_literal_string: "
return err(format_underline("toml::parse_literal_string: "
"the next token is not a valid literal string",
{{std::addressof(loc), "here"}}));
}
@ -588,7 +588,7 @@ parse_string(location<Container>& loc)
return parse_literal_string(loc);
}
}
return err(format_underline("[error] toml::parse_string: ",
return err(format_underline("toml::parse_string: ",
{{std::addressof(loc), "the next token is not a string"}}));
}
@ -604,7 +604,7 @@ parse_local_date(location<Container>& loc)
const auto y = lex_date_fullyear::invoke(inner_loc);
if(!y || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-')
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_inner_local_date: invalid year format",
{{std::addressof(inner_loc), "should be `-`"}}),
source_location(std::addressof(inner_loc)));
@ -613,7 +613,7 @@ parse_local_date(location<Container>& loc)
const auto m = lex_date_month::invoke(inner_loc);
if(!m || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-')
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_date: invalid month format",
{{std::addressof(inner_loc), "should be `-`"}}),
source_location(std::addressof(inner_loc)));
@ -622,7 +622,7 @@ parse_local_date(location<Container>& loc)
const auto d = lex_date_mday::invoke(inner_loc);
if(!d)
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_date: invalid day format",
{{std::addressof(inner_loc), "here"}}),
source_location(std::addressof(inner_loc)));
@ -637,7 +637,7 @@ parse_local_date(location<Container>& loc)
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_local_date: ",
return err(format_underline("toml::parse_local_date: ",
{{std::addressof(loc), "the next token is not a local_date"}}));
}
}
@ -654,7 +654,7 @@ parse_local_time(location<Container>& loc)
const auto h = lex_time_hour::invoke(inner_loc);
if(!h || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':')
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_time: invalid year format",
{{std::addressof(inner_loc), "should be `:`"}}),
source_location(std::addressof(inner_loc)));
@ -663,7 +663,7 @@ parse_local_time(location<Container>& loc)
const auto m = lex_time_minute::invoke(inner_loc);
if(!m || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':')
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_time: invalid month format",
{{std::addressof(inner_loc), "should be `:`"}}),
source_location(std::addressof(inner_loc)));
@ -672,7 +672,7 @@ parse_local_time(location<Container>& loc)
const auto s = lex_time_second::invoke(inner_loc);
if(!s)
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_time: invalid second format",
{{std::addressof(inner_loc), "here"}}),
source_location(std::addressof(inner_loc)));
@ -709,7 +709,7 @@ parse_local_time(location<Container>& loc)
{
if(before_secfrac != inner_loc.iter())
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_time: invalid subsecond format",
{{std::addressof(inner_loc), "here"}}),
source_location(std::addressof(inner_loc)));
@ -720,7 +720,7 @@ parse_local_time(location<Container>& loc)
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_local_time: ",
return err(format_underline("toml::parse_local_time: ",
{{std::addressof(loc), "the next token is not a local_time"}}));
}
}
@ -736,7 +736,7 @@ parse_local_datetime(location<Container>& loc)
const auto date = parse_local_date(inner_loc);
if(!date || inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_datetime: invalid datetime format",
{{std::addressof(inner_loc), "date, not datetime"}}),
source_location(std::addressof(inner_loc)));
@ -744,7 +744,7 @@ parse_local_datetime(location<Container>& loc)
const char delim = *(inner_loc.iter());
if(delim != 'T' && delim != 't' && delim != ' ')
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_datetime: invalid datetime format",
{{std::addressof(inner_loc), "should be `T` or ` ` (space)"}}),
source_location(std::addressof(inner_loc)));
@ -753,7 +753,7 @@ parse_local_datetime(location<Container>& loc)
const auto time = parse_local_time(inner_loc);
if(!time)
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_local_datetime: invalid datetime format",
{{std::addressof(inner_loc), "invalid time fomrat"}}),
source_location(std::addressof(inner_loc)));
@ -765,7 +765,7 @@ parse_local_datetime(location<Container>& loc)
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_local_datetime: ",
return err(format_underline("toml::parse_local_datetime: ",
{{std::addressof(loc), "the next token is not a local_datetime"}}));
}
}
@ -781,7 +781,7 @@ parse_offset_datetime(location<Container>& loc)
const auto datetime = parse_local_datetime(inner_loc);
if(!datetime || inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_offset_datetime: invalid datetime format",
{{std::addressof(inner_loc), "date, not datetime"}}),
source_location(std::addressof(inner_loc)));
@ -803,7 +803,7 @@ parse_offset_datetime(location<Container>& loc)
}
else if(*inner_loc.iter() != 'Z' && *inner_loc.iter() != 'z')
{
throw internal_error(format_underline("[error]: "
throw internal_error(format_underline(
"toml::parse_offset_datetime: invalid datetime format",
{{std::addressof(inner_loc), "should be `Z` or `+HH:MM`"}}),
source_location(std::addressof(inner_loc)));
@ -814,7 +814,7 @@ parse_offset_datetime(location<Container>& loc)
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_offset_datetime: ",
return err(format_underline("toml::parse_offset_datetime: ",
{{std::addressof(loc), "the next token is not a offset_datetime"}}));
}
}
@ -836,7 +836,7 @@ parse_simple_key(location<Container>& loc)
const auto reg = bare.unwrap();
return ok(std::make_pair(reg.str(), reg));
}
return err(format_underline("[error] toml::parse_simple_key: ",
return err(format_underline("toml::parse_simple_key: ",
{{std::addressof(loc), "the next token is not a simple key"}}));
}
@ -862,7 +862,7 @@ parse_key(location<Container>& loc)
}
else
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::detail::parse_key: dotted key contains invalid key",
{{std::addressof(inner_loc), k.unwrap_err()}}),
source_location(std::addressof(inner_loc)));
@ -879,7 +879,7 @@ parse_key(location<Container>& loc)
}
else
{
throw internal_error(format_underline("[error] toml::parse_key: "
throw internal_error(format_underline("toml::parse_key: "
"dotted key contains invalid key ",
{{std::addressof(inner_loc), "should be `.`"}}),
source_location(std::addressof(inner_loc)));
@ -895,7 +895,7 @@ parse_key(location<Container>& loc)
return ok(std::make_pair(std::vector<key>(1, smpl.unwrap().first),
smpl.unwrap().second));
}
return err(format_underline("[error] toml::parse_key: ",
return err(format_underline("toml::parse_key: ",
{{std::addressof(loc), "is not a valid key"}}));
}
@ -913,11 +913,11 @@ parse_array(location<Container>& loc)
const auto first = loc.iter();
if(loc.iter() == loc.end())
{
return err("[error] toml::parse_array: input is empty");
return err("toml::parse_array: input is empty");
}
if(*loc.iter() != '[')
{
return err("[error] toml::parse_array: token is not an array");
return err("toml::parse_array: token is not an array");
}
loc.advance();
@ -944,7 +944,7 @@ parse_array(location<Container>& loc)
auto array_start_loc = loc;
array_start_loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array: "
throw syntax_error(format_underline("toml::parse_array: "
"type of elements should be the same each other.", {
{std::addressof(array_start_loc), "array starts here"},
{
@ -965,7 +965,7 @@ parse_array(location<Container>& loc)
auto array_start_loc = loc;
array_start_loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array: "
throw syntax_error(format_underline("toml::parse_array: "
"value having invalid format appeared in an array", {
{std::addressof(array_start_loc), "array starts here"},
{std::addressof(loc), "it is not a valid value."}
@ -988,7 +988,7 @@ parse_array(location<Container>& loc)
auto array_start_loc = loc;
array_start_loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array:"
throw syntax_error(format_underline("toml::parse_array:"
" missing array separator `,` after a value", {
{std::addressof(array_start_loc), "array starts here"},
{std::addressof(loc), "should be `,`"}
@ -997,7 +997,7 @@ parse_array(location<Container>& loc)
}
}
loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array: "
throw syntax_error(format_underline("toml::parse_array: "
"array did not closed by `]`",
{{std::addressof(loc), "should be closed"}}),
source_location(std::addressof(loc)));
@ -1019,7 +1019,7 @@ parse_key_value_pair(location<Container>& loc)
if(const auto keyval_sep = lex_keyval_sep::invoke(loc))
{
loc.reset(first);
msg = format_underline("[error] toml::parse_key_value_pair: "
msg = format_underline("toml::parse_key_value_pair: "
"empty key is not allowed.",
{{std::addressof(loc), "key expected before '='"}});
}
@ -1035,7 +1035,7 @@ parse_key_value_pair(location<Container>& loc)
const auto line_end = std::find(loc.iter(), loc.end(), '\n');
if(std::find(loc.iter(), line_end, '=') != line_end)
{
msg = format_underline("[error] toml::parse_key_value_pair: "
msg = format_underline("toml::parse_key_value_pair: "
"invalid format for key",
{{std::addressof(loc), "invalid character in key"}},
{"Did you forget '.' to separate dotted-key?",
@ -1043,7 +1043,7 @@ parse_key_value_pair(location<Container>& loc)
}
else // if not, the error is lack of key-value separator.
{
msg = format_underline("[error] toml::parse_key_value_pair: "
msg = format_underline("toml::parse_key_value_pair: "
"missing key-value separator `=`",
{{std::addressof(loc), "should be `=`"}});
}
@ -1061,7 +1061,7 @@ parse_key_value_pair(location<Container>& loc)
if(sequence<maybe<lex_ws>, maybe<lex_comment>, lex_newline>::invoke(loc))
{
loc.reset(after_kvsp);
msg = format_underline("[error] toml::parse_key_value_pair: "
msg = format_underline("toml::parse_key_value_pair: "
"missing value after key-value separator '='",
{{std::addressof(loc), "expected value, but got nothing"}});
}
@ -1196,7 +1196,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
{
// show special err msg for conflicting table
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: array of table (\"",
"toml::insert_value: array of table (\"",
format_dotted_keys(first, last),
"\") cannot be defined"), {
{std::addressof(get_region(tab->at(k))),
@ -1208,7 +1208,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
else if(!(tab->at(k).is_array()))
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: array of table (\"",
"toml::insert_value: array of table (\"",
format_dotted_keys(first, last), "\") collides with"
" existing value"), {
{std::addressof(get_region(tab->at(k))),
@ -1223,7 +1223,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
if(!(a.front().is_table()))
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: array of table (\"",
"toml::insert_value: array of table (\"",
format_dotted_keys(first, last), "\") collides with"
" existing value"), {
{std::addressof(get_region(tab->at(k))),
@ -1251,7 +1251,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
if(detail::get_region(a.front()).str().substr(0,2) != "[[")
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: array of table (\"",
"toml::insert_value: array of table (\"",
format_dotted_keys(first, last), "\") collides with"
" existing array-of-tables"), {
{std::addressof(get_region(tab->at(k))),
@ -1280,7 +1280,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
tab->at(k), first, iter, last))
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: table (\"",
"toml::insert_value: table (\"",
format_dotted_keys(first, last),
"\") already exists."), {
{std::addressof(get_region(tab->at(k))),
@ -1308,7 +1308,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
tab->at(k).as_array().front().is_table())
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: array of tables (\"",
"toml::insert_value: array of tables (\"",
format_dotted_keys(first, last), "\") already exists."), {
{std::addressof(get_region(tab->at(k))),
"array of tables defined here"},
@ -1319,7 +1319,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
else
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: value (\"",
"toml::insert_value: value (\"",
format_dotted_keys(first, last), "\") already exists."), {
{std::addressof(get_region(tab->at(k))),
"value already exists here"},
@ -1355,7 +1355,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
if(!a.back().is_table())
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: target (",
"toml::insert_value: target (",
format_dotted_keys(first, std::next(iter)),
") is neither table nor an array of tables"), {
{std::addressof(get_region(a.back())),
@ -1368,7 +1368,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
else
{
throw syntax_error(format_underline(concat_to_string(
"[error] toml::insert_value: target (",
"toml::insert_value: target (",
format_dotted_keys(first, std::next(iter)),
") is neither table nor an array of tables"), {
{std::addressof(get_region(tab->at(k))),
@ -1392,7 +1392,7 @@ parse_inline_table(location<Container>& loc)
table_type retval;
if(!(loc.iter() != loc.end() && *loc.iter() == '{'))
{
return err(format_underline("[error] toml::parse_inline_table: ",
return err(format_underline("toml::parse_inline_table: ",
{{std::addressof(loc), "the next token is not an inline table"}}));
}
loc.advance();
@ -1421,7 +1421,7 @@ parse_inline_table(location<Container>& loc)
insert_nested_key(retval, val, keys.begin(), keys.end(), key_reg);
if(!inserted)
{
throw internal_error("[error] toml::parse_inline_table: "
throw internal_error("toml::parse_inline_table: "
"failed to insert value into table: " + inserted.unwrap_err(),
source_location(std::addressof(loc)));
}
@ -1439,14 +1439,14 @@ parse_inline_table(location<Container>& loc)
}
else if(*loc.iter() == '#' || *loc.iter() == '\r' || *loc.iter() == '\n')
{
throw syntax_error(format_underline("[error] "
throw syntax_error(format_underline(
"toml::parse_inline_table: missing curly brace `}`",
{{std::addressof(loc), "should be `}`"}}),
source_location(std::addressof(loc)));
}
else
{
throw syntax_error(format_underline("[error] "
throw syntax_error(format_underline(
"toml::parse_inline_table: missing table separator `,` ",
{{std::addressof(loc), "should be `,`"}}),
source_location(std::addressof(loc)));
@ -1454,7 +1454,7 @@ parse_inline_table(location<Container>& loc)
}
}
loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_inline_table: "
throw syntax_error(format_underline("toml::parse_inline_table: "
"inline table did not closed by `}`",
{{std::addressof(loc), "should be closed"}}),
source_location(std::addressof(loc)));
@ -1480,7 +1480,7 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
if(loc.iter() != loc.end() && (*loc.iter() == '+' || *loc.iter() == '-'
|| *loc.iter() == 'Z' || *loc.iter() == 'z'))
{
return err(format_underline("[error] bad offset: should be [+-]HH:MM or Z",
return err(format_underline("bad offset: should be [+-]HH:MM or Z",
{{std::addressof(loc), "[+-]HH:MM or Z"}},
{"pass: +09:00, -05:30", "fail: +9:00, -5:30"}));
}
@ -1500,14 +1500,14 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
const auto c = *loc.iter();
if(c == 'T' || c == 't')
{
return err(format_underline("[error] bad time: should be HH:MM:SS.subsec",
return err(format_underline("bad time: should be HH:MM:SS.subsec",
{{std::addressof(loc), "HH:MM:SS.subsec"}},
{"pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999",
"fail: 1979-05-27T7:32:00, 1979-05-27 17:32"}));
}
if('0' <= c && c <= '9')
{
return err(format_underline("[error] bad time: missing T",
return err(format_underline("bad time: missing T",
{{std::addressof(loc), "T or space required here"}},
{"pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999",
"fail: 1979-05-27T7:32:00, 1979-05-27 7:32"}));
@ -1516,7 +1516,7 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
('0' <= *std::next(loc.iter()) && *std::next(loc.iter())<= '9'))
{
loc.advance();
return err(format_underline("[error] bad time: should be HH:MM:SS.subsec",
return err(format_underline("bad time: should be HH:MM:SS.subsec",
{{std::addressof(loc), "HH:MM:SS.subsec"}},
{"pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999",
"fail: 1979-05-27T7:32:00, 1979-05-27 7:32"}));
@ -1533,7 +1533,7 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
{
if(loc.iter() != loc.end() && *loc.iter() == '_')
{
return err(format_underline("[error] bad float: `_` should be surrounded by digits",
return err(format_underline("bad float: `_` should be surrounded by digits",
{{std::addressof(loc), "here"}},
{"pass: +1.0, -2e-2, 3.141_592_653_589, inf, nan",
"fail: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0"}));
@ -1549,7 +1549,7 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
const auto c = *loc.iter();
if(c == '_')
{
return err(format_underline("[error] bad integer: `_` should be surrounded by digits",
return err(format_underline("bad integer: `_` should be surrounded by digits",
{{std::addressof(loc), "here"}},
{"pass: -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755",
"fail: 1__000, 0123"}));
@ -1558,21 +1558,21 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
{
// leading zero. point '0'
loc.retrace();
return err(format_underline("[error] bad integer: leading zero",
return err(format_underline("bad integer: leading zero",
{{std::addressof(loc), "here"}},
{"pass: -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755",
"fail: 1__000, 0123"}));
}
if(c == ':' || c == '-')
{
return err(format_underline("[error] bad datetime: invalid format",
return err(format_underline("bad datetime: invalid format",
{{std::addressof(loc), "here"}},
{"pass: 1979-05-27T07:32:00-07:00, 1979-05-27 07:32:00.999999Z",
"fail: 1979-05-27T7:32:00-7:00, 1979-05-27 7:32-00:30"}));
}
if(c == '.' || c == 'e' || c == 'E')
{
return err(format_underline("[error] bad float: invalid format",
return err(format_underline("bad float: invalid format",
{{std::addressof(loc), "here"}},
{"pass: +1.0, -2e-2, 3.141_592_653_589, inf, nan",
"fail: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0"}));
@ -1582,19 +1582,19 @@ result<value_t, std::string> guess_number_type(const location<Container>& l)
}
if(loc.iter() != loc.end() && *loc.iter() == '.')
{
return err(format_underline("[error] bad float: invalid format",
return err(format_underline("bad float: invalid format",
{{std::addressof(loc), "integer part required before this"}},
{"pass: +1.0, -2e-2, 3.141_592_653_589, inf, nan",
"fail: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0"}));
}
if(loc.iter() != loc.end() && *loc.iter() == '_')
{
return err(format_underline("[error] bad number: `_` should be surrounded by digits",
return err(format_underline("bad number: `_` should be surrounded by digits",
{{std::addressof(loc), "`_` is not surrounded by digits"}},
{"pass: -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755",
"fail: 1__000, 0123"}));
}
return err(format_underline("[error] bad format: unknown value appeared",
return err(format_underline("bad format: unknown value appeared",
{{std::addressof(loc), "here"}}));
}
@ -1623,7 +1623,7 @@ result<Value, std::string> parse_value(location<Container>& loc)
const auto first = loc.iter();
if(first == loc.end())
{
return err(format_underline("[error] toml::parse_value: input is empty",
return err(format_underline("toml::parse_value: input is empty",
{{std::addressof(loc), ""}}));
}
@ -1646,7 +1646,7 @@ result<Value, std::string> parse_value(location<Container>& loc)
case value_t::table : {return parse_inline_table<value_type>(loc);}
default:
{
const auto msg = format_underline("[error] toml::parse_value: "
const auto msg = format_underline("toml::parse_value: "
"unknown token appeared", {{std::addressof(loc), "unknown"}});
loc.reset(first);
return err(msg);
@ -1665,7 +1665,7 @@ parse_table_key(location<Container>& loc)
const auto open = lex_std_table_open::invoke(inner_loc);
if(!open || inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::parse_table_key: no `[`",
{{std::addressof(inner_loc), "should be `[`"}}),
source_location(std::addressof(inner_loc)));
@ -1676,7 +1676,7 @@ parse_table_key(location<Container>& loc)
const auto keys = parse_key(inner_loc);
if(!keys)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::parse_table_key: invalid key",
{{std::addressof(inner_loc), "not key"}}),
source_location(std::addressof(inner_loc)));
@ -1687,7 +1687,7 @@ parse_table_key(location<Container>& loc)
const auto close = lex_std_table_close::invoke(inner_loc);
if(!close)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::parse_table_key: no `]`",
{{std::addressof(inner_loc), "should be `]`"}}),
source_location(std::addressof(inner_loc)));
@ -1701,7 +1701,7 @@ parse_table_key(location<Container>& loc)
const auto nl = lex_newline_after_table_key::invoke(loc);
if(!nl)
{
throw syntax_error(format_underline("[error] "
throw syntax_error(format_underline(
"toml::parse_table_key: newline required after [table.key]",
{{std::addressof(loc), "expected newline"}}),
source_location(std::addressof(loc)));
@ -1711,7 +1711,7 @@ parse_table_key(location<Container>& loc)
}
else
{
return err(format_underline("[error] toml::parse_table_key: "
return err(format_underline("toml::parse_table_key: "
"not a valid table key", {{std::addressof(loc), "here"}}));
}
}
@ -1727,7 +1727,7 @@ parse_array_table_key(location<Container>& loc)
const auto open = lex_array_table_open::invoke(inner_loc);
if(!open || inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::parse_array_table_key: no `[[`",
{{std::addressof(inner_loc), "should be `[[`"}}),
source_location(std::addressof(inner_loc)));
@ -1736,7 +1736,7 @@ parse_array_table_key(location<Container>& loc)
const auto keys = parse_key(inner_loc);
if(!keys)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::parse_array_table_key: invalid key",
{{std::addressof(inner_loc), "not a key"}}),
source_location(std::addressof(inner_loc)));
@ -1745,7 +1745,7 @@ parse_array_table_key(location<Container>& loc)
const auto close = lex_array_table_close::invoke(inner_loc);
if(!close)
{
throw internal_error(format_underline("[error] "
throw internal_error(format_underline(
"toml::parse_table_key: no `]]`",
{{std::addressof(inner_loc), "should be `]]`"}}),
source_location(std::addressof(inner_loc)));
@ -1759,7 +1759,7 @@ parse_array_table_key(location<Container>& loc)
const auto nl = lex_newline_after_table_key::invoke(loc);
if(!nl)
{
throw syntax_error(format_underline("[error] toml::"
throw syntax_error(format_underline("toml::"
"parse_array_table_key: newline required after [[table.key]]",
{{std::addressof(loc), "expected newline"}}),
source_location(std::addressof(loc)));
@ -1769,7 +1769,7 @@ parse_array_table_key(location<Container>& loc)
}
else
{
return err(format_underline("[error] toml::parse_array_table_key: "
return err(format_underline("toml::parse_array_table_key: "
"not a valid table key", {{std::addressof(loc), "here"}}));
}
}
@ -1844,7 +1844,7 @@ parse_ml_table(location<Container>& loc)
{
const auto before2 = loc.iter();
lex_ws::invoke(loc); // skip whitespace
const auto msg = format_underline("[error] toml::parse_table: "
const auto msg = format_underline("toml::parse_table: "
"invalid line format", {{std::addressof(loc), concat_to_string(
"expected newline, but got '", show_char(*loc.iter()), "'.")}});
loc.reset(before2);
@ -1958,7 +1958,7 @@ result<Value, std::string> parse_toml_file(location<Container>& loc)
continue;
}
return err(format_underline("[error]: toml::parse_toml_file: "
return err(format_underline("toml::parse_toml_file: "
"unknown line appeared", {{std::addressof(loc), "unknown format"}}));
}

View File

@ -9,6 +9,7 @@
#include <iterator>
#include <iomanip>
#include <cassert>
#include "color.hpp"
namespace toml
{
@ -418,7 +419,8 @@ struct region final : public region_base
// to show a better error message.
inline std::string format_underline(const std::string& message,
const std::vector<std::pair<region_base const*, std::string>>& reg_com,
const std::vector<std::string>& helps = {})
const std::vector<std::string>& helps = {},
const bool colorize = TOML11_ERROR_MESSAGE_COLORIZED)
{
assert(!reg_com.empty());
@ -432,7 +434,27 @@ inline std::string format_underline(const std::string& message,
)->first->line_num().size());
std::ostringstream retval;
retval << message << '\n';
if(colorize)
{
retval << color::colorize; // turn on ANSI color
}
// XXX
// Here, before `colorize` support, it does not output `[error]` prefix
// automatically. So some user may output it manually and this change may
// duplicate the prefix. To avoid it, check the first 7 characters and
// if it is "[error]", it removes that part from the message shown.
if(message.size() > 7 && message.substr(0, 7) == "[error]")
{
retval << color::bold << color::red << "[error]" << color::reset
<< color::bold << message.substr(7) << color::reset << '\n';
}
else
{
retval << color::bold << color::red << "[error] " << color::reset
<< color::bold << message << color::reset << '\n';
}
for(auto iter = reg_com.begin(); iter != reg_com.end(); ++iter)
{
@ -440,34 +462,41 @@ inline std::string format_underline(const std::string& message,
if(iter != reg_com.begin() &&
std::prev(iter)->first->name() == iter->first->name())
{
retval << "\n ...\n";
retval << color::bold << color::blue << "\n ...\n" << color::reset;
}
else // if filename differs, print " --> filename.toml"
{
if(iter != reg_com.begin()) {retval << '\n';}
retval << " --> " << iter->first->name() << '\n';
retval << color::bold << color::blue << " --> " << color::reset
<< iter->first->name() << '\n';
// add one almost-empty line for readability
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ')
<< color::bold << color::blue << " | " << color::reset << '\n';
}
const region_base* const reg = iter->first;
const std::string& comment = iter->second;
retval << ' ' << std::setw(line_num_width) << reg->line_num();
retval << " | " << reg->line() << '\n';
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ');
retval << " | " << make_string(reg->before(), ' ');
retval << ' ' << std::setw(line_num_width) << color::bold << color::blue
<< reg->line_num() << " | " << color::reset << reg->line() << '\n';
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ')
<< color::bold << color::blue << " | " << color::reset
<< make_string(reg->before(), ' ');
if(reg->size() == 1)
{
// invalid
// ^------
retval << '^';
retval << make_string(reg->after(), '-');
retval << color::bold << color::red
<< '^' << make_string(reg->after(), '-') << color::reset;
}
else
{
// invalid
// ~~~~~~~
const auto underline_len = std::min(reg->size(), reg->line().size());
retval << make_string(underline_len, '~');
retval << color::bold << color::red
<< make_string(underline_len, '~') << color::reset;
}
retval << ' ';
retval << comment;
@ -477,10 +506,10 @@ inline std::string format_underline(const std::string& message,
{
retval << '\n';
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ');
retval << " | ";
retval << color::bold << color::blue << " | " << color::reset;
for(const auto help : helps)
{
retval << "\nHint: ";
retval << color::bold << "\nHint: " << color::reset;
retval << help;
}
}

View File

@ -32,7 +32,7 @@ template<value_t Expected,
throw_bad_cast(value_t actual, const ::toml::basic_value<C, T, A>& v)
{
throw type_error(detail::format_underline(concat_to_string(
"[error] toml::value bad_cast to ", Expected), {
"toml::value: bad_cast to ", Expected), {
{std::addressof(get_region(v)),
concat_to_string("the actual type is ", actual)}
}), v.location());
@ -1860,25 +1860,27 @@ operator>=(const basic_value<C, T, A>& lhs, const basic_value<C, T, A>& rhs)
template<typename C, template<typename ...> class T, template<typename ...> class A>
inline std::string format_error(const std::string& err_msg,
const basic_value<C, T, A>& v, const std::string& comment,
std::vector<std::string> hints = {})
std::vector<std::string> hints = {},
const bool colorize = TOML11_ERROR_MESSAGE_COLORIZED)
{
return detail::format_underline(err_msg,
std::vector<std::pair<detail::region_base const*, std::string>>{
{std::addressof(detail::get_region(v)), comment}
}, std::move(hints));
}, std::move(hints), colorize);
}
template<typename C, template<typename ...> class T, template<typename ...> class A>
inline std::string format_error(const std::string& err_msg,
const toml::basic_value<C, T, A>& v1, const std::string& comment1,
const toml::basic_value<C, T, A>& v2, const std::string& comment2,
std::vector<std::string> hints = {})
std::vector<std::string> hints = {},
const bool colorize = TOML11_ERROR_MESSAGE_COLORIZED)
{
return detail::format_underline(err_msg,
std::vector<std::pair<detail::region_base const*, std::string>>{
{std::addressof(detail::get_region(v1)), comment1},
{std::addressof(detail::get_region(v2)), comment2}
}, std::move(hints));
}, std::move(hints), colorize);
}
template<typename C, template<typename ...> class T, template<typename ...> class A>
@ -1886,14 +1888,15 @@ inline std::string format_error(const std::string& err_msg,
const toml::basic_value<C, T, A>& v1, const std::string& comment1,
const toml::basic_value<C, T, A>& v2, const std::string& comment2,
const toml::basic_value<C, T, A>& v3, const std::string& comment3,
std::vector<std::string> hints = {})
std::vector<std::string> hints = {},
const bool colorize = TOML11_ERROR_MESSAGE_COLORIZED)
{
return detail::format_underline(err_msg,
std::vector<std::pair<detail::region_base const*, std::string>>{
{std::addressof(detail::get_region(v1)), comment1},
{std::addressof(detail::get_region(v2)), comment2},
{std::addressof(detail::get_region(v3)), comment3}
}, std::move(hints));
}, std::move(hints), colorize);
}
template<typename Visitor, typename C,