mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
add parser impl for integer, float, boolean
This commit is contained in:
@@ -31,6 +31,12 @@ BOOST_AUTO_TEST_CASE(test_parse_basic_inline_string)
|
|||||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
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_basic_multiline_string)
|
BOOST_AUTO_TEST_CASE(test_parse_basic_multiline_string)
|
||||||
@@ -62,6 +68,12 @@ BOOST_AUTO_TEST_CASE(test_parse_basic_multiline_string)
|
|||||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
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_literal_inline_string)
|
BOOST_AUTO_TEST_CASE(test_parse_literal_inline_string)
|
||||||
@@ -100,6 +112,12 @@ BOOST_AUTO_TEST_CASE(test_parse_literal_inline_string)
|
|||||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
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_literal_multiline_string)
|
BOOST_AUTO_TEST_CASE(test_parse_literal_multiline_string)
|
||||||
@@ -122,6 +140,12 @@ BOOST_AUTO_TEST_CASE(test_parse_literal_multiline_string)
|
|||||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
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_string)
|
BOOST_AUTO_TEST_CASE(test_parse_string)
|
||||||
@@ -160,7 +184,147 @@ BOOST_AUTO_TEST_CASE(test_parse_string)
|
|||||||
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
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_integer)
|
||||||
|
{
|
||||||
|
typedef toml::parse_integer parser;
|
||||||
|
typedef toml::is_integer<toml::charactor> acceptor;
|
||||||
|
{
|
||||||
|
const std::string source("42");
|
||||||
|
const toml::Integer expected(42);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("+42");
|
||||||
|
const toml::Integer expected(42);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("-42");
|
||||||
|
const toml::Integer expected(-42);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("-4_2");
|
||||||
|
const toml::Integer expected(-42);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(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_float)
|
||||||
|
{
|
||||||
|
typedef toml::parse_float parser;
|
||||||
|
typedef toml::is_float<toml::charactor> acceptor;
|
||||||
|
{
|
||||||
|
const std::string source("42.0");
|
||||||
|
const toml::Float expected(42.0);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("+42.0");
|
||||||
|
const toml::Float expected(42.0);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("-42.0");
|
||||||
|
const toml::Float expected(-42.0);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("-4_2.0");
|
||||||
|
const toml::Float expected(-42.0);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("-42e0");
|
||||||
|
const toml::Float expected(-42.0);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("-42.0e0");
|
||||||
|
const toml::Float expected(-42.0);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(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_boolean)
|
||||||
|
{
|
||||||
|
typedef toml::parse_boolean parser;
|
||||||
|
typedef toml::is_boolean<toml::charactor> acceptor;
|
||||||
|
{
|
||||||
|
const std::string source("true");
|
||||||
|
const toml::Boolean expected(true);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("false");
|
||||||
|
const toml::Boolean expected(false);
|
||||||
|
const auto result = parser::invoke(source.cbegin());
|
||||||
|
BOOST_CHECK(result.first.ok());
|
||||||
|
BOOST_CHECK_EQUAL(result.first.get(), expected);
|
||||||
|
BOOST_CHECK(result.second == acceptor::invoke(source.begin()));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string source("T");
|
||||||
|
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_time)
|
// BOOST_AUTO_TEST_CASE(test_parse_local_time)
|
||||||
// {
|
// {
|
||||||
|
108
toml/parser.hpp
108
toml/parser.hpp
@@ -284,58 +284,62 @@ struct parse_string
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
struct parse_integer
|
||||||
// template<typename charT>
|
{
|
||||||
// struct parse_integer
|
typedef toml::charactor value_type;
|
||||||
// {
|
typedef std::basic_string<value_type> string_type;
|
||||||
// typedef charT value_type;
|
typedef detail::result<toml::Integer> result_type;
|
||||||
// typedef std::basic_string<value_type> string_type;
|
|
||||||
// typedef toml::Integer result_type;
|
template<typename Iterator, class = typename std::enable_if<
|
||||||
//
|
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||||
// template<typename Iterator, class = typename std::enable_if<
|
value_type>::value>::type>
|
||||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
static std::pair<result_type, Iterator> invoke(Iterator iter)
|
||||||
// value_type>::value>::type>
|
{
|
||||||
// static result_type invoke(Iterator iter, Iterator end)
|
const Iterator end = is_integer<value_type>::invoke(iter);
|
||||||
// {
|
if(iter == end) return std::make_pair(result_type{}, iter);
|
||||||
// string_type result; result.resize(std::distance(iter, end));
|
|
||||||
// std::copy_if(iter, end, result.begin(), [](charT c){return c != '_';});
|
string_type result; result.resize(std::distance(iter, end));
|
||||||
// return std::stoi(result);
|
std::copy_if(iter, end, result.begin(), [](value_type c){return c != '_';});
|
||||||
// }
|
return std::make_pair(std::stoll(result), end);
|
||||||
// };
|
}
|
||||||
//
|
};
|
||||||
// template<typename charT>
|
|
||||||
// struct parse_float
|
struct parse_float
|
||||||
// {
|
{
|
||||||
// typedef charT value_type;
|
typedef toml::charactor value_type;
|
||||||
// typedef std::basic_string<value_type> string_type;
|
typedef std::basic_string<value_type> string_type;
|
||||||
// typedef toml::Float result_type;
|
typedef detail::result<toml::Float> result_type;
|
||||||
//
|
|
||||||
// template<typename Iterator, class = typename std::enable_if<
|
template<typename Iterator, class = typename std::enable_if<
|
||||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||||
// value_type>::value>::type>
|
value_type>::value>::type>
|
||||||
// static result_type invoke(Iterator iter, Iterator end)
|
static std::pair<result_type, Iterator> invoke(Iterator iter)
|
||||||
// {
|
{
|
||||||
// string_type result; result.resize(std::distance(iter, end));
|
const Iterator end = is_float<value_type>::invoke(iter);
|
||||||
// std::copy_if(iter, end, result.begin(), [](charT c){return c != '_';});
|
if(iter == end) return std::make_pair(result_type{}, iter);
|
||||||
// return std::stod(result);
|
|
||||||
// }
|
string_type result; result.resize(std::distance(iter, end));
|
||||||
// };
|
std::copy_if(iter, end, result.begin(), [](value_type c){return c != '_';});
|
||||||
//
|
return std::make_pair(std::stod(result), end);
|
||||||
// template<typename charT>
|
}
|
||||||
// struct parse_boolean
|
};
|
||||||
// {
|
|
||||||
// typedef charT value_type;
|
struct parse_boolean
|
||||||
// typedef toml::Boolean result_type;
|
{
|
||||||
//
|
typedef toml::charactor value_type;
|
||||||
// template<typename Iterator, class = typename std::enable_if<
|
typedef detail::result<toml::Boolean> result_type;
|
||||||
// std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
|
||||||
// value_type>::value>::type>
|
template<typename Iterator, class = typename std::enable_if<
|
||||||
// static result_type invoke(Iterator iter, Iterator end)
|
std::is_same<typename std::iterator_traits<Iterator>::value_type,
|
||||||
// {
|
value_type>::value>::type>
|
||||||
// return (std::distance(iter, end) == 4);
|
static std::pair<result_type, Iterator> invoke(Iterator iter)
|
||||||
// }
|
{
|
||||||
// };
|
const Iterator end = is_boolean<value_type>::invoke(iter);
|
||||||
//
|
if(iter == end) return std::make_pair(result_type{}, iter);
|
||||||
|
return std::make_pair((std::distance(iter, end) == 4), end);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// template<typename charT>
|
// template<typename charT>
|
||||||
// struct parse_local_time
|
// struct parse_local_time
|
||||||
// {
|
// {
|
||||||
|
Reference in New Issue
Block a user