From 789d78476990e42f5d9ab47f7047410f9ab4acf6 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 19 Apr 2019 13:18:35 +0900 Subject: [PATCH 1/5] chore: update README; about literals --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0097f9b..88ebc32 100644 --- a/README.md +++ b/README.md @@ -603,9 +603,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,]`). @@ -615,7 +617,7 @@ 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. +"[[1]]"_toml; // This literal is ambiguous. // Currently, it becomes a table taht 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. From 46be054ce959e9eea880459af1ad409e91eb04f7 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 19 Apr 2019 13:22:13 +0900 Subject: [PATCH 2/5] fix: improve err msg for multiline inline table show "missing curly brace" instead of "missing table key-value separator" --- toml/parser.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 `,`"}})); } } From 82e8c1e68b26a27e58c01d422c9b31b6ea50c828 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 21 Apr 2019 16:31:24 +0900 Subject: [PATCH 3/5] fix: skip first ws/newlines in toml literal when ""_toml literal is used with C++11 raw-string literal, it normally starts with newline like the following. ```cpp const auto v = u8R"( [table] key = "value" )"_toml; ``` With this, the error message shows the first empty line that starts just after `u8R"(` and thus the error message shows nothing. To avoid this, skip the first empty lines and whitespaces in literal. --- toml/literal.hpp | 3 +++ 1 file changed, 3 insertions(+) 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))); } From 2d9b4992eccedd19cf6d7e4f84338511c3a87e5e Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 21 Apr 2019 16:38:08 +0900 Subject: [PATCH 4/5] fix: restrict length of underline by size of line in some cases, `region` contains several lines and `region::size` returns the whole size that is a sum of lengthes of all the lines. To avoid too long underlines, restrict the length of underline by the length of the line that is shown in the message. --- toml/region.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 << ' '; From 6185dfee141f9b7be74b1948c6cfdddb6f10c032 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 23 Apr 2019 23:31:37 +0900 Subject: [PATCH 5/5] chore: fix typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 88ebc32..e8ea0a6 100644 --- a/README.md +++ b/README.md @@ -618,7 +618,7 @@ add a comma after the first element (like `[1,]`). "[[table]]"_toml; // This is a table that has an array of tables inside. "[[1]]"_toml; // This literal is ambiguous. - // Currently, it becomes a table taht has array of table "1". + // 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.