1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/*=============================================================================
Copyright (c) 2001-2015 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/fusion/include/vector.hpp>
#include <iostream>
#include "test.hpp"
#include "utils.hpp"
#ifdef _MSC_VER
// bogus https://developercommunity.visualstudio.com/t/buggy-warning-c4709/471956
# pragma warning(disable: 4709) // comma operator within array index expression
#endif
struct adata
{
int a;
boost::optional<int> b;
};
BOOST_FUSION_ADAPT_STRUCT(adata,
a, b
)
struct test_attribute_type
{
template <typename Context>
void operator()(Context& ctx) const
{
BOOST_TEST(typeid(decltype(_attr(ctx))).name() == typeid(boost::optional<int>).name());
}
};
int
main()
{
using boost::spirit::x3::traits::is_optional;
static_assert(is_optional<boost::optional<int>>(), "is_optional problem");
using spirit_test::test;
using spirit_test::test_attr;
using boost::spirit::x3::int_;
using boost::spirit::x3::omit;
using boost::spirit::x3::ascii::char_;
BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(-int_);
{
BOOST_TEST((test("1234", -int_)));
BOOST_TEST((test("abcd", -int_, false)));
boost::optional<int> n;
BOOST_TEST(test_attr("", -int_, n))
&& BOOST_TEST(!n);
BOOST_TEST(test_attr("123", -int_, n))
&& BOOST_TEST(n) && BOOST_TEST_EQ(*n, 123);
boost::optional<std::string> s;
BOOST_TEST(test_attr("", -+char_, s))
&& BOOST_TEST(!s);
BOOST_TEST(test_attr("abc", -+char_, s))
&& BOOST_TEST(s) && BOOST_TEST_EQ(*s, "abc");
}
{ // test propagation of unused
using boost::fusion::at_c;
using boost::fusion::vector;
vector<char, char> v;
BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
BOOST_TEST((at_c<0>(v) == 'a'));
BOOST_TEST((at_c<1>(v) == 'c'));
v = boost::fusion::vector<char, char>();
BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
BOOST_TEST((at_c<0>(v) == 'a'));
BOOST_TEST((at_c<1>(v) == 'c'));
char ch;
BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
BOOST_TEST((ch == 'c'));
}
{ // test action
boost::optional<int> n = 0;
BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
BOOST_TEST((n.get() == 1234));
}
{
std::string s;
BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
BOOST_TEST(s == "abc");
}
{
boost::optional<int> n = 0;
auto f = [&](auto& ctx){ n = _attr(ctx); };
BOOST_TEST((test("1234", (-int_)[f])));
BOOST_TEST(n.get() == 1234);
n = boost::optional<int>();
BOOST_TEST((test("abcd", (-int_)[f], false)));
BOOST_TEST(!n);
}
{
std::vector<adata> v;
BOOST_TEST((test_attr("a 1 2 a 2", *('a' >> int_ >> -int_), v
, char_(' '))));
BOOST_TEST(2 == v.size() &&
1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
2 == v[1].a && !v[1].b);
}
{ // test move only types
boost::optional<move_only> o;
BOOST_TEST(test_attr("s", -synth_move_only, o));
BOOST_TEST(o);
}
return boost::report_errors();
}
|