2020-04-14 09:45:32 +00:00
|
|
|
// This file is a part of toml++ and is subject to the the terms of the MIT license.
|
2021-01-02 15:48:47 +00:00
|
|
|
// Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
2020-04-14 09:45:32 +00:00
|
|
|
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-05-21 11:28:45 +00:00
|
|
|
// This example shows the error messages the library produces by forcing a set of specific parsing
|
|
|
|
// failures and printing their results.
|
2020-04-14 09:45:32 +00:00
|
|
|
|
2022-01-04 14:23:45 +00:00
|
|
|
#include "examples.h"
|
2020-04-14 09:45:32 +00:00
|
|
|
#include <toml++/toml.h>
|
2020-06-28 22:57:59 +00:00
|
|
|
|
2020-04-14 09:45:32 +00:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-10-25 21:49:17 +00:00
|
|
|
inline constexpr auto invalid_parses = std::array{
|
2022-01-04 14:23:45 +00:00
|
|
|
"########## comments and whitespace"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
"# bar\rkek"sv,
|
|
|
|
"# bar\bkek"sv,
|
2020-10-09 20:49:06 +00:00
|
|
|
"# \xf1\x63"sv,
|
2022-01-04 14:23:45 +00:00
|
|
|
"# val1 = 1\fval2 = 2"sv,
|
2022-01-09 11:01:56 +00:00
|
|
|
"foo = 1\n\u2000\nbar = 2"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
|
|
|
|
"########## inline tables"sv,
|
|
|
|
"val = {,}"sv,
|
2021-10-30 14:26:05 +00:00
|
|
|
"val = {a='b',}"sv, // allowed when TOML_ENABLE_UNRELEASED_FEATURES == 1
|
2020-04-14 09:45:32 +00:00
|
|
|
"val = {a='b',,}"sv,
|
|
|
|
"val = {a='b',"sv,
|
2021-10-30 14:26:05 +00:00
|
|
|
"val = {a='b',\n c='d'}"sv, // allowed when TOML_ENABLE_UNRELEASED_FEATURES == 1
|
2020-04-14 09:45:32 +00:00
|
|
|
"val = {?='b'}"sv,
|
2022-01-05 14:47:00 +00:00
|
|
|
"foo = {} \n [foo.bar]"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
|
|
|
|
"########## tables"sv,
|
2020-06-25 14:33:01 +00:00
|
|
|
"[]"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
"[foo"sv,
|
|
|
|
"[foo] ?"sv,
|
|
|
|
"[foo] [bar]"sv,
|
|
|
|
"[foo]\n[foo]"sv,
|
|
|
|
"? = 'foo' ?"sv,
|
2021-05-17 21:32:35 +00:00
|
|
|
"[ [foo] ]"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
|
|
|
|
"########## arrays"sv,
|
|
|
|
"val = [,]"sv,
|
|
|
|
"val = ['a',,]"sv,
|
|
|
|
"val = ['a',"sv,
|
|
|
|
|
|
|
|
"########## key-value pairs"sv,
|
|
|
|
"val = 'foo' ?"sv,
|
|
|
|
"val = "sv,
|
|
|
|
"val "sv,
|
|
|
|
"val ?"sv,
|
|
|
|
"val = ]"sv,
|
|
|
|
"[foo]\nbar = 'kek'\nbar = 'kek2'"sv,
|
|
|
|
"[foo]\nbar = 'kek'\nbar = 7"sv,
|
|
|
|
"[foo.bar]\n[foo]\nbar = 'kek'"sv,
|
|
|
|
"[foo]\nbar = 'kek'\nbar.kek = 7"sv,
|
|
|
|
"[foo]\nbar.? = 'kek'"sv,
|
2020-06-08 15:31:23 +00:00
|
|
|
R"('''val''' = 1)"sv,
|
|
|
|
R"(a."""val""" = 1)"sv,
|
2020-10-09 21:30:12 +00:00
|
|
|
"1= 0x6cA#+\xf1"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
|
|
|
|
"########## values"sv,
|
|
|
|
"val = _"sv,
|
|
|
|
"val = G"sv,
|
2021-05-17 21:32:35 +00:00
|
|
|
"PATHOLOGICALLY_NESTED"sv, // generated inline
|
2020-04-14 09:45:32 +00:00
|
|
|
|
|
|
|
"########## strings"sv,
|
|
|
|
"val = \" \r \""sv,
|
|
|
|
R"(val = ")"sv,
|
|
|
|
R"(val = "\g")"sv,
|
2021-10-30 14:26:05 +00:00
|
|
|
R"(val = "\x20")"sv, // allowed when TOML_ENABLE_UNRELEASED_FEATURES == 1
|
2020-04-14 09:45:32 +00:00
|
|
|
R"(val = "\uFFF")"sv,
|
|
|
|
R"(val = "\uFFFG")"sv,
|
|
|
|
R"(val = "\UFFFFFFF")"sv,
|
|
|
|
R"(val = "\UFFFFFGF")"sv,
|
|
|
|
R"(val = "\uD801")"sv,
|
|
|
|
R"(val = "\U00110000")"sv,
|
|
|
|
R"(val = """ """""")"sv,
|
|
|
|
R"(val = ''' '''''')"sv,
|
|
|
|
"val = '\n'"sv,
|
2020-06-29 14:49:48 +00:00
|
|
|
|
|
|
|
"########## integers"sv,
|
|
|
|
R"(val = -0b0)"sv,
|
|
|
|
R"(val = -0o0)"sv,
|
|
|
|
R"(val = -0x0)"sv,
|
|
|
|
R"(val = +0b0)"sv,
|
|
|
|
R"(val = +0o0)"sv,
|
|
|
|
R"(val = +0x0)"sv,
|
|
|
|
R"(val = 1-)"sv,
|
|
|
|
R"(val = -1+)"sv,
|
|
|
|
R"(val = -+1)"sv,
|
|
|
|
R"(val = 1_0_)"sv,
|
|
|
|
R"(val = 1_0_ )"sv,
|
2020-10-10 08:45:53 +00:00
|
|
|
R"(val = 01 )"sv,
|
2022-01-08 13:17:12 +00:00
|
|
|
R"(val = 0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000 )"sv,
|
|
|
|
R"(val = 0o1000000000000000000000 )"sv,
|
|
|
|
R"(val = 9223372036854775808 )"sv,
|
|
|
|
R"(val = 0x8000000000000000 )"sv,
|
2020-10-10 08:45:53 +00:00
|
|
|
|
|
|
|
"########## floats"sv,
|
2022-01-06 11:26:01 +00:00
|
|
|
R"(val = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0)"sv,
|
2020-04-14 09:45:32 +00:00
|
|
|
};
|
|
|
|
|
2021-05-21 11:28:45 +00:00
|
|
|
inline constexpr auto divider =
|
|
|
|
"################################################################################"sv;
|
|
|
|
}
|
2020-04-14 09:45:32 +00:00
|
|
|
|
2021-05-21 11:28:45 +00:00
|
|
|
int main()
|
2020-04-14 09:45:32 +00:00
|
|
|
{
|
2022-02-12 13:25:20 +00:00
|
|
|
const auto parse_and_print_if_error = [](std::string_view str)
|
|
|
|
{
|
|
|
|
#if TOML_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result = toml::parse(str);
|
|
|
|
static_cast<void>(result);
|
|
|
|
}
|
|
|
|
catch (const toml::parse_error& err)
|
|
|
|
{
|
|
|
|
std::cout << err << "\n\n"sv;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (auto result = toml::parse(str); !result)
|
|
|
|
std::cout << result.error() << "\n\n"sv;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-04-14 09:45:32 +00:00
|
|
|
for (auto str : invalid_parses)
|
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
continue;
|
|
|
|
|
2021-05-21 11:28:45 +00:00
|
|
|
// section headings
|
|
|
|
if (str.substr(0, 10) == "##########"sv)
|
2020-04-14 09:45:32 +00:00
|
|
|
{
|
2021-05-21 11:28:45 +00:00
|
|
|
std::cout << divider << '\n';
|
|
|
|
std::cout << "# "sv << str.substr(11) << '\n';
|
|
|
|
std::cout << divider << "\n\n"sv;
|
2020-04-14 09:45:32 +00:00
|
|
|
}
|
2021-05-21 11:28:45 +00:00
|
|
|
|
|
|
|
// error messages
|
2020-04-14 09:45:32 +00:00
|
|
|
else
|
|
|
|
{
|
2021-05-17 21:32:35 +00:00
|
|
|
toml::parse_result result;
|
|
|
|
|
|
|
|
if (str == "PATHOLOGICALLY_NESTED"sv)
|
|
|
|
{
|
2021-05-21 11:28:45 +00:00
|
|
|
std::string s(1000u, '[');
|
2021-05-17 21:32:35 +00:00
|
|
|
constexpr auto start = "array = "sv;
|
|
|
|
memcpy(s.data(), start.data(), start.length());
|
2022-02-12 13:25:20 +00:00
|
|
|
parse_and_print_if_error(s);
|
2021-05-17 21:32:35 +00:00
|
|
|
}
|
|
|
|
else
|
2022-02-12 13:25:20 +00:00
|
|
|
parse_and_print_if_error(str);
|
2020-04-14 09:45:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|