mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-01-09 09:00:10 +00:00
add parse_data
This commit is contained in:
parent
6ab05a8594
commit
fd58cc507f
@ -294,6 +294,12 @@ BOOST_AUTO_TEST_CASE(test_float)
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
{
|
||||
const std::string source("42");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_boolean)
|
||||
@ -354,6 +360,12 @@ BOOST_AUTO_TEST_CASE(test_parse_local_time)
|
||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("10");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_local_date)
|
||||
@ -368,6 +380,12 @@ BOOST_AUTO_TEST_CASE(test_parse_local_date)
|
||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("10");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_local_date_time)
|
||||
@ -390,6 +408,12 @@ BOOST_AUTO_TEST_CASE(test_parse_local_date_time)
|
||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("1000-11-11");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_offset_date_time)
|
||||
@ -446,6 +470,12 @@ BOOST_AUTO_TEST_CASE(test_parse_offset_date_time)
|
||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("1000-11-11");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_datetime)
|
||||
@ -484,6 +514,12 @@ BOOST_AUTO_TEST_CASE(test_parse_datetime)
|
||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("12");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -583,6 +619,12 @@ BOOST_AUTO_TEST_CASE(test_parse_array)
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[dummy]");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -626,207 +668,268 @@ BOOST_AUTO_TEST_CASE(test_parse_inline_table)
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("{dummy}");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(!result.first.ok());
|
||||
BOOST_CHECK(result.second == source.begin());
|
||||
}
|
||||
}
|
||||
//
|
||||
// // BOOST_AUTO_TEST_CASE(test_parse_barekey)
|
||||
// // {
|
||||
// // {
|
||||
// // const std::string source("hoge");
|
||||
// // const std::string result = toml::parse_barekey<char>::invoke(
|
||||
// // source.cbegin(), toml::is_barekey<char>::invoke(source.cbegin()));
|
||||
// // BOOST_CHECK_EQUAL(source, result);
|
||||
// // }
|
||||
// // {
|
||||
// // const std::string source("bare-key");
|
||||
// // const std::string result = toml::parse_barekey<char>::invoke(
|
||||
// // source.cbegin(), toml::is_barekey<char>::invoke(source.cbegin()));
|
||||
// // BOOST_CHECK_EQUAL(source, result);
|
||||
// // }
|
||||
// // {
|
||||
// // const std::string source("bare_key");
|
||||
// // const std::string result = toml::parse_barekey<char>::invoke(
|
||||
// // source.cbegin(), toml::is_barekey<char>::invoke(source.cbegin()));
|
||||
// // BOOST_CHECK_EQUAL(source, result);
|
||||
// // }
|
||||
// // {
|
||||
// // const std::string source("42");
|
||||
// // const std::string result = toml::parse_barekey<char>::invoke(
|
||||
// // source.cbegin(), toml::is_barekey<char>::invoke(source.cbegin()));
|
||||
// // BOOST_CHECK_EQUAL(source, result);
|
||||
// // }
|
||||
// // }
|
||||
//
|
||||
//
|
||||
//
|
||||
// BOOST_AUTO_TEST_CASE(test_key_value_pair)
|
||||
// {
|
||||
// typedef toml::parse_key_value_pair<char> parser;
|
||||
// typedef toml::is_key_value_pair<char> acceptor;
|
||||
// {
|
||||
// const std::string source("key=1");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", 1};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key =\t1");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", 1};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = true");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", true};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = -42");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", -42};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = -42.0");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", -42.};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = \"string\"");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", "string"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = 1901-01-01T00:00:00");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", toml::Datetime(1901, 1,1,0,0,0,0,0)};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = [1,2,3]");
|
||||
// const std::pair<toml::key, toml::value> expected{"key", {1,2,3}};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("key = {foo=1,bar=2.0,baz='3'}");
|
||||
// const std::pair<toml::key, toml::value> expected{"key",
|
||||
// {{"foo", 1}, {"bar", 2.0}, {"baz", "3"}}};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// BOOST_AUTO_TEST_CASE(test_table_definition)
|
||||
// {
|
||||
// typedef toml::parse_standard_table_definition<char> parser;
|
||||
// typedef toml::is_table_definition<char> acceptor;
|
||||
// {
|
||||
// const std::string source("[foo]");
|
||||
// const std::vector<toml::key> expected{"foo"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[foo.bar.baz]");
|
||||
// const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[foo . bar. baz]");
|
||||
// const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[foo . \"bar\" . baz]");
|
||||
// const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[foo . \"b\\tar\" . baz]");
|
||||
// const std::vector<toml::key> expected{"foo", "b\tar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// BOOST_AUTO_TEST_CASE(test_array_of_table_definition)
|
||||
// {
|
||||
// typedef toml::parse_array_of_table_definition<char> parser;
|
||||
// typedef toml::is_array_of_table_definition<char> acceptor;
|
||||
// {
|
||||
// const std::string source("[[foo]]");
|
||||
// const std::vector<toml::key> expected{"foo"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[[foo.bar.baz]]");
|
||||
// const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[[foo . bar. baz]]");
|
||||
// const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[[foo . \"bar\" . baz]]");
|
||||
// const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// {
|
||||
// const std::string source("[[foo . \"b\\tar\" . baz]]");
|
||||
// const std::vector<toml::key> expected{"foo", "b\tar", "baz"};
|
||||
// const auto result = parser::invoke(
|
||||
// source.cbegin(), acceptor::invoke(source.cbegin()));
|
||||
// const bool check = result == expected;
|
||||
// BOOST_CHECK(check);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_barekey)
|
||||
{
|
||||
typedef toml::parse_barekey parser;
|
||||
typedef toml::is_barekey<toml::charactor> acceptor;
|
||||
{
|
||||
const std::string source("hoge");
|
||||
const toml::key expected("hoge");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("bare-key");
|
||||
const toml::key expected("bare-key");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("bare_key");
|
||||
const toml::key expected("bare_key");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("42");
|
||||
const toml::key expected("42");
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_key_value_pair)
|
||||
{
|
||||
typedef toml::parse_key_value_pair<char> parser;
|
||||
typedef toml::is_key_value_pair<char> acceptor;
|
||||
{
|
||||
const std::string source("key=1");
|
||||
const std::pair<toml::key, toml::value> expected{"key", 1};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key =\t1");
|
||||
const std::pair<toml::key, toml::value> expected{"key", 1};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = true");
|
||||
const std::pair<toml::key, toml::value> expected{"key", true};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = -42");
|
||||
const std::pair<toml::key, toml::value> expected{"key", -42};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = -42.0");
|
||||
const std::pair<toml::key, toml::value> expected{"key", -42.};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = \"string\"");
|
||||
const std::pair<toml::key, toml::value> expected{"key", "string"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = 1901-01-01T00:00:00");
|
||||
const std::pair<toml::key, toml::value> expected{"key", toml::Datetime(1901, 1,1,0,0,0,0,0)};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = [1,2,3]");
|
||||
const std::pair<toml::key, toml::value> expected{"key", {1,2,3}};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("key = {foo=1,bar=2.0,baz='3'}");
|
||||
const std::pair<toml::key, toml::value> expected{"key",
|
||||
{{"foo", 1}, {"bar", 2.0}, {"baz", "3"}}};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_table_definition)
|
||||
{
|
||||
typedef toml::parse_table_definition parser;
|
||||
typedef toml::is_table_definition<char> acceptor;
|
||||
{
|
||||
const std::string source("[foo]");
|
||||
const std::vector<toml::key> expected{"foo"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[foo.bar.baz]");
|
||||
const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[foo . bar. baz]");
|
||||
const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[foo . \"bar\" . baz]");
|
||||
const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[foo . \"b\\tar\" . baz]");
|
||||
const std::vector<toml::key> expected{"foo", "b\tar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_array_of_table_definition)
|
||||
{
|
||||
typedef toml::parse_array_of_table_definition parser;
|
||||
typedef toml::is_array_of_table_definition<char> acceptor;
|
||||
{
|
||||
const std::string source("[[foo]]");
|
||||
const std::vector<toml::key> expected{"foo"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[[foo.bar.baz]]");
|
||||
const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[[foo . bar. baz]]");
|
||||
const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[[foo . \"bar\" . baz]]");
|
||||
const std::vector<toml::key> expected{"foo", "bar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
{
|
||||
const std::string source("[[foo . \"b\\tar\" . baz]]");
|
||||
const std::vector<toml::key> expected{"foo", "b\tar", "baz"};
|
||||
const auto result = parser::invoke(source.cbegin());
|
||||
BOOST_CHECK(result.first.ok());
|
||||
BOOST_CHECK(result.first.get() == expected);
|
||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_parse_data)
|
||||
{
|
||||
typedef toml::parse_data parser;
|
||||
{
|
||||
const std::string source("#hogehoge");
|
||||
const toml::Table expected{};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
{
|
||||
const std::string source("key = 'value'");
|
||||
const toml::Table expected{{"key", "value"}};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
{
|
||||
const std::string source("key = 'value' #hoge");
|
||||
const toml::Table expected{{"key", "value"}};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
{
|
||||
const std::string source("[table]\nkey = 'value' #hoge");
|
||||
const toml::Table expected{{"table", {{"key", "value"}} }};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
{
|
||||
const std::string source("[table]\n\tkey = 'value'\n\t#hoge");
|
||||
const toml::Table expected{{"table", {{"key", "value"}} }};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
{
|
||||
const std::string source("[table]\n\tkey = 'value'\n\t#hoge");
|
||||
const toml::Table expected{{"table", {{"key", "value"}} }};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
{
|
||||
const std::string source("[nested.table]\n\tkey = 'value'\n\t#hoge");
|
||||
const toml::Table expected{{"nested", {{"table", {{"key", "value"}}}}}};
|
||||
const auto result = parser::invoke(source.cbegin(), source.cend());
|
||||
BOOST_CHECK(result == expected);
|
||||
}
|
||||
}
|
||||
|
405
toml/parser.hpp
405
toml/parser.hpp
@ -757,197 +757,220 @@ struct parse_inline_table
|
||||
}
|
||||
};
|
||||
|
||||
// template<typename charT>
|
||||
// struct parse_table_contents
|
||||
// {
|
||||
// typedef charT value_type;
|
||||
// typedef toml::Table result_type;
|
||||
//
|
||||
// template<typename Iterator, class = typename std::enable_if<
|
||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
// value_type>::value>::type>
|
||||
// static result_type invoke(Iterator iter, Iterator end)
|
||||
// {
|
||||
// result_type result;
|
||||
// while(iter != end)
|
||||
// {
|
||||
// iter = is_empty_lines<charT>::invoke(iter);
|
||||
//
|
||||
// Iterator tmp = is_key_value_pair<charT>::invoke(iter);
|
||||
// result.emplace(parse_key_value_pair<charT>::invoke(iter, tmp));
|
||||
// iter = is_one_of<is_comment<charT>, is_newline<charT>>::invoke(tmp);
|
||||
//
|
||||
// iter = is_empty_lines<charT>::invoke(iter);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// template<typename charT>
|
||||
// struct parse_standard_table_definition
|
||||
// {
|
||||
// typedef charT value_type;
|
||||
// typedef std::vector<toml::key> result_type;
|
||||
//
|
||||
// template<typename Iterator, class = typename std::enable_if<
|
||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
// value_type>::value>::type>
|
||||
// static result_type invoke(Iterator iter, Iterator end)
|
||||
// {
|
||||
// result_type result;
|
||||
// iter = is_any_num_of_ws<charT>::invoke(iter);
|
||||
// --end;
|
||||
// assert(*iter == '[' && *end == ']');
|
||||
// ++iter;
|
||||
// iter = is_any_num_of_ws<charT>::invoke(iter);
|
||||
//
|
||||
// Iterator tmp = is_key<charT>::invoke(iter);
|
||||
// result.emplace_back(parse_key<charT>::invoke(iter, tmp));
|
||||
// iter = is_any_num_of_ws<charT>::invoke(tmp);
|
||||
//
|
||||
// while(iter != end)
|
||||
// {
|
||||
// iter = is_charactor<charT, '.'>::invoke(iter);
|
||||
// iter = is_any_num_of_ws<charT>::invoke(iter);
|
||||
//
|
||||
// tmp = is_key<charT>::invoke(iter);
|
||||
// result.emplace_back(parse_key<charT>::invoke(iter, tmp));
|
||||
// iter = is_any_num_of_ws<charT>::invoke(tmp);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// template<typename charT>
|
||||
// struct parse_array_of_table_definition
|
||||
// {
|
||||
// typedef charT value_type;
|
||||
// typedef std::vector<toml::key> result_type;
|
||||
//
|
||||
// template<typename Iterator, class = typename std::enable_if<
|
||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
// value_type>::value>::type>
|
||||
// static result_type invoke(Iterator iter, Iterator end)
|
||||
// {
|
||||
// result_type result;
|
||||
// iter = is_any_num_of_ws<charT>::invoke(iter);
|
||||
// --end;
|
||||
// assert(*iter == '[' && *std::next(iter) == '[' &&
|
||||
// *end == ']' && *std::prev(end) == ']');
|
||||
// ++iter; ++iter; --end;
|
||||
//
|
||||
// iter = is_any_num_of_ws<charT>::invoke(iter);
|
||||
//
|
||||
// Iterator tmp = is_key<charT>::invoke(iter);
|
||||
// result.emplace_back(parse_key<charT>::invoke(iter, tmp));
|
||||
// iter = is_any_num_of_ws<charT>::invoke(tmp);
|
||||
//
|
||||
// while(iter != end)
|
||||
// {
|
||||
// iter = is_charactor<charT, '.'>::invoke(iter);
|
||||
// iter = is_any_num_of_ws<charT>::invoke(iter);
|
||||
//
|
||||
// tmp = is_key<charT>::invoke(iter);
|
||||
// result.emplace_back(parse_key<charT>::invoke(iter, tmp));
|
||||
// iter = is_any_num_of_ws<charT>::invoke(tmp);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// template<typename charT>
|
||||
// struct parse_data
|
||||
// {
|
||||
// typedef charT value_type;
|
||||
// typedef toml::Table result_type;
|
||||
//
|
||||
// template<typename Iterator, class = typename std::enable_if<
|
||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
// value_type>::value>::type>
|
||||
// static result_type invoke(Iterator iter, Iterator end)
|
||||
// {
|
||||
// result_type result;
|
||||
//
|
||||
// Iterator tmp = is_table_contents<charT>::invoke(iter);
|
||||
// if(tmp != iter)
|
||||
// {
|
||||
// result = parse_table_contents<charT>::invoke(iter, tmp);
|
||||
// }
|
||||
//
|
||||
// while(iter != end)
|
||||
// {
|
||||
// if(iter != (tmp = is_table_definition<charT>::invoke(iter)))
|
||||
// {
|
||||
// auto k = parse_standard_table_definition<charT>::invoke(iter, tmp);
|
||||
// iter = tmp;
|
||||
// tmp = is_table_contents<charT>::invoke(iter);
|
||||
// auto tab = parse_table_contents<charT>::invoke(iter, tmp);
|
||||
// push_table(result, std::move(tab), std::begin(k), std::end(k));
|
||||
// }
|
||||
// else if(iter != (tmp=is_array_of_table_definition<charT>::invoke(iter)))
|
||||
// {
|
||||
// auto k = parse_array_of_table_definition<charT>::invoke(iter, tmp);
|
||||
// iter = tmp;
|
||||
// tmp = is_table_contents<charT>::invoke(iter);
|
||||
// auto tab = parse_table_contents<charT>::invoke(iter, tmp);
|
||||
// push_array_of_table(result, std::move(tab),
|
||||
// std::begin(k), std::end(k));
|
||||
// }
|
||||
// else
|
||||
// throw internal_error("no array ");
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// template<typename Iterator, class = typename std::enable_if<
|
||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
// toml::key>::value>::type>
|
||||
// static void
|
||||
// push_table(toml::Table& data, toml::Table&& v, Iterator iter, Iterator end)
|
||||
// {
|
||||
// if(iter == std::prev(end))
|
||||
// {
|
||||
// if(data.count(*iter) == 1)
|
||||
// throw syntax_error("duplicate key");
|
||||
// data.emplace(*iter, std::move(v));
|
||||
// }
|
||||
//
|
||||
// if(data.count(*iter) == 0)
|
||||
// data.emplace(*iter, toml::Table());
|
||||
// else if(data[*iter].type() != value_t::Table)
|
||||
// throw syntax_error("duplicate key");
|
||||
//
|
||||
// return push_table(data[*iter].template cast<value_t::Table>(),
|
||||
// std::move(v), std::next(iter), end);
|
||||
// }
|
||||
//
|
||||
// template<typename Iterator, class = typename std::enable_if<
|
||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
// toml::key>::value>::type>
|
||||
// static void
|
||||
// push_array_of_table(toml::Table& data, toml::Table&& v,
|
||||
// Iterator iter, Iterator end)
|
||||
// {
|
||||
// if(iter == std::prev(end))
|
||||
// {
|
||||
// if(data.count(*iter) == 0)
|
||||
// data.emplace(*iter, toml::Array());
|
||||
// else if(data.at(*iter).type() != value_t::Array)
|
||||
// throw syntax_error("duplicate key");
|
||||
//
|
||||
// data[*iter].template cast<value_t::Array>().emplace_back(std::move(v));
|
||||
// }
|
||||
//
|
||||
// if(data.count(*iter) == 0)
|
||||
// data.emplace(*iter, toml::Table());
|
||||
// else if(data[*iter].type() != value_t::Table)
|
||||
// throw syntax_error("duplicate key");
|
||||
//
|
||||
// return push_array_of_table(data[*iter].template cast<value_t::Table>(),
|
||||
// std::move(v), std::next(iter), end);
|
||||
// }
|
||||
//
|
||||
// };
|
||||
struct parse_table_definition
|
||||
{
|
||||
typedef toml::charactor value_type;
|
||||
typedef detail::result<std::vector<toml::key>> result_type;
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
value_type>::value>::type>
|
||||
static std::pair<result_type, Iterator> invoke(Iterator iter)
|
||||
{
|
||||
const Iterator end = is_table_definition<value_type>::invoke(iter);
|
||||
if(iter == end) return std::make_pair(result_type{}, iter);
|
||||
|
||||
std::vector<toml::key> result;
|
||||
result.reserve(std::count(iter, end, '.')+1);
|
||||
|
||||
const Iterator last = std::prev(end);
|
||||
iter = is_any_num_of_ws<value_type>::invoke(iter);
|
||||
iter = is_any_num_of_ws<value_type>::invoke(std::next(iter));
|
||||
|
||||
auto tmp = parse_key::invoke(iter);
|
||||
if(!tmp.first.ok()) throw syntax_error("table definition");
|
||||
result.emplace_back(tmp.first.move());
|
||||
iter = is_any_num_of_ws<value_type>::invoke(tmp.second);
|
||||
|
||||
while(iter != last)
|
||||
{
|
||||
iter = is_charactor<value_type, '.'>::invoke(iter);
|
||||
iter = is_any_num_of_ws<value_type>::invoke(iter);
|
||||
|
||||
tmp = parse_key::invoke(iter);
|
||||
if(!tmp.first.ok()) throw syntax_error("table definition");
|
||||
result.emplace_back(tmp.first.move());
|
||||
iter = is_any_num_of_ws<value_type>::invoke(tmp.second);
|
||||
}
|
||||
return std::make_pair(result, end);
|
||||
}
|
||||
};
|
||||
|
||||
struct parse_array_of_table_definition
|
||||
{
|
||||
typedef toml::charactor value_type;
|
||||
typedef detail::result<std::vector<toml::key>> result_type;
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
value_type>::value>::type>
|
||||
static std::pair<result_type, Iterator> invoke(Iterator iter)
|
||||
{
|
||||
const Iterator end = is_array_of_table_definition<value_type>::invoke(iter);
|
||||
if(iter == end) return std::make_pair(result_type{}, iter);
|
||||
|
||||
std::vector<toml::key> result;
|
||||
result.reserve(std::count(iter, end, '.')+1);
|
||||
|
||||
const Iterator last = end - 2;
|
||||
iter = is_any_num_of_ws<value_type>::invoke(iter) + 2;
|
||||
iter = is_any_num_of_ws<value_type>::invoke(iter);
|
||||
|
||||
auto tmp = parse_key::invoke(iter);
|
||||
if(!tmp.first.ok()) throw syntax_error("array of table definition");
|
||||
result.emplace_back(tmp.first.move());
|
||||
iter = is_any_num_of_ws<value_type>::invoke(tmp.second);
|
||||
|
||||
while(iter != last)
|
||||
{
|
||||
iter = is_charactor<value_type, '.'>::invoke(iter);
|
||||
iter = is_any_num_of_ws<value_type>::invoke(iter);
|
||||
|
||||
tmp = parse_key::invoke(iter);
|
||||
if(!tmp.first.ok()) throw syntax_error("array of table definition");
|
||||
result.emplace_back(tmp.first.move());
|
||||
iter = is_any_num_of_ws<value_type>::invoke(tmp.second);
|
||||
}
|
||||
return std::make_pair(result, end);
|
||||
}
|
||||
};
|
||||
|
||||
struct parse_data
|
||||
{
|
||||
typedef toml::charactor value_type;
|
||||
typedef toml::Table result_type;
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
value_type>::value>::type>
|
||||
static result_type invoke(Iterator iter, const Iterator end)
|
||||
{
|
||||
toml::Table result;
|
||||
auto noname = parse_table_contents(iter, end);
|
||||
result = std::move(noname.first);
|
||||
iter = skip_empty(noname.second, end);
|
||||
|
||||
while(iter != end)
|
||||
{
|
||||
iter = skip_empty(iter, end);
|
||||
std::pair<detail::result<std::vector<toml::key>>, Iterator> tabname;
|
||||
if((tabname = parse_table_definition::invoke(iter)).first.ok())
|
||||
{
|
||||
auto contents = parse_table_contents(tabname.second, end);
|
||||
push_table(result, std::move(contents.first),
|
||||
tabname.first.get().begin(), tabname.first.get().end());
|
||||
iter = contents.second;
|
||||
}
|
||||
else if((tabname = parse_array_of_table_definition::invoke(iter)).first.ok())
|
||||
{
|
||||
auto contents = parse_table_contents(tabname.second, end);
|
||||
push_array_of_table(result, std::move(contents.first),
|
||||
tabname.first.get().begin(), tabname.first.get().end());
|
||||
iter = contents.second;
|
||||
}
|
||||
else
|
||||
throw syntax_error("parse_data: unknown line");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
value_type>::value>::type>
|
||||
static Iterator
|
||||
skip_empty(Iterator iter, Iterator end)
|
||||
{
|
||||
while(iter != end)
|
||||
{
|
||||
if(*iter == '#')
|
||||
{
|
||||
while(iter != end &&
|
||||
iter == is_newline<value_type>::invoke(iter)){++iter;}
|
||||
}
|
||||
else if(iter == is_newline<value_type>::invoke(iter) &&
|
||||
iter == is_whitespace<value_type>::invoke(iter))
|
||||
{
|
||||
return iter;
|
||||
}
|
||||
else
|
||||
{
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
return iter;
|
||||
}
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
value_type>::value>::type>
|
||||
static std::pair<toml::Table, Iterator>
|
||||
parse_table_contents(Iterator iter, Iterator end)
|
||||
{
|
||||
toml::Table table;
|
||||
iter = skip_empty(iter, end);
|
||||
while(iter != end)
|
||||
{
|
||||
auto kv = parse_key_value_pair<value_type>::invoke(iter);
|
||||
if(!kv.first.ok()) return std::make_pair(table, iter);
|
||||
|
||||
table.emplace(kv.first.move());
|
||||
iter = kv.second;
|
||||
iter = skip_empty(iter, end);
|
||||
}
|
||||
return std::make_pair(table, iter);
|
||||
}
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
toml::key>::value>::type>
|
||||
static void
|
||||
push_table(toml::Table& data, toml::Table&& v, Iterator iter, Iterator end)
|
||||
{
|
||||
if(iter == std::prev(end))
|
||||
{
|
||||
if(data.count(*iter) == 1)
|
||||
throw syntax_error("duplicate key");
|
||||
data.emplace(*iter, std::move(v));
|
||||
return;
|
||||
}
|
||||
|
||||
if(data.count(*iter) == 0)
|
||||
data.emplace(*iter, toml::Table());
|
||||
else if(data[*iter].type() != value_t::Table)
|
||||
throw syntax_error("duplicate key");
|
||||
|
||||
return push_table(data[*iter].template cast<value_t::Table>(),
|
||||
std::move(v), std::next(iter), end);
|
||||
}
|
||||
|
||||
template<typename Iterator, class = typename std::enable_if<
|
||||
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||
toml::key>::value>::type>
|
||||
static void
|
||||
push_array_of_table(toml::Table& data, toml::Table&& v,
|
||||
Iterator iter, Iterator end)
|
||||
{
|
||||
if(iter == std::prev(end))
|
||||
{
|
||||
if(data.count(*iter) == 0)
|
||||
data.emplace(*iter, toml::Array());
|
||||
else if(data.at(*iter).type() != value_t::Array)
|
||||
throw syntax_error("duplicate key");
|
||||
|
||||
data[*iter].template cast<value_t::Array>().emplace_back(std::move(v));
|
||||
return;
|
||||
}
|
||||
|
||||
if(data.count(*iter) == 0)
|
||||
data.emplace(*iter, toml::Table());
|
||||
else if(data[*iter].type() != value_t::Table)
|
||||
throw syntax_error("duplicate key");
|
||||
|
||||
return push_array_of_table(data[*iter].template cast<value_t::Table>(),
|
||||
std::move(v), std::next(iter), end);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}// toml
|
||||
#endif// TOML11_PARSER
|
||||
|
Loading…
Reference in New Issue
Block a user