diff --git a/README.md b/README.md index 2f7701b..2679aa0 100644 --- a/README.md +++ b/README.md @@ -609,9 +609,11 @@ toml::value operator""_toml(const char* str, std::size_t len); Access to the operator can be gained with `using namespace toml::literals;`, `using namespace toml::toml_literals`, and `using namespace toml::literals::toml_literals`. -Note that since it allows a bare value without a key, it is difficult to distinguish -arrays and table definitions. -Currently, it parses `[1]` as a table definition if there are no commas. +Note that a key that is composed only of digits is allowed in TOML. +And, unlike the file parser, toml-literal allows a bare value without a key. +Thus it is difficult to distinguish arrays having integers and definitions of +tables that are named as digits. +Currently, literal `[1]` becomes a table named "1". To ensure a literal to be considered as an array with one element, you need to add a comma after the first element (like `[1,]`). @@ -621,8 +623,8 @@ add a comma after the first element (like `[1,]`). "[[1,2,3]]"_toml; // This is an array of arrays "[[table]]"_toml; // This is a table that has an array of tables inside. -"[[1]]"_toml; // This is ambiguous. - // Currently, it becomes a table taht has array of table "1". +"[[1]]"_toml; // This literal is ambiguous. + // Currently, it becomes a table that has array of table "1". "1 = [{}]"_toml; // This is a table that has an array of table named 1. "[[1,]]"_toml; // This is an array of arrays. "[[1],]"_toml; // ditto. diff --git a/toml/literal.hpp b/toml/literal.hpp index d7194ed..0e61cc9 100644 --- a/toml/literal.hpp +++ b/toml/literal.hpp @@ -73,6 +73,9 @@ inline ::toml::value operator""_toml(const char* str, std::size_t len) if(auto data = ::toml::detail::parse_toml_file(loc)) { loc.reset(loc.begin()); // rollback to the top of the literal + // skip needless characters for error message + skip_line::invoke(loc); // skip the first several needless lines + skip_ws::invoke(loc); // skip the first several needless whitespaces return ::toml::value(std::move(data.unwrap()), ::toml::detail::region>(std::move(loc))); } diff --git a/toml/parser.hpp b/toml/parser.hpp index dc74913..f345d3b 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -1406,10 +1406,16 @@ parse_inline_table(location& loc) return ok(std::make_pair( retval, region(loc, first, loc.iter()))); } + else if(*loc.iter() == '#' || *loc.iter() == '\r' || *loc.iter() == '\n') + { + throw syntax_error(format_underline("[error] " + "toml::parse_inline_table: missing curly brace `}`", + {{std::addressof(loc), "should be `}`"}})); + } else { throw syntax_error(format_underline("[error] " - "toml:::parse_inline_table: missing table separator `,` ", + "toml::parse_inline_table: missing table separator `,` ", {{std::addressof(loc), "should be `,`"}})); } } diff --git a/toml/region.hpp b/toml/region.hpp index 1720a7f..6ef6f9b 100644 --- a/toml/region.hpp +++ b/toml/region.hpp @@ -343,7 +343,8 @@ inline std::string format_underline(const std::string& message, { // invalid // ~~~~~~~ - retval << make_string(reg->size(), '~'); + const auto underline_len = std::min(reg->size(), reg->line().size()); + retval << make_string(underline_len, '~'); } retval << ' ';