]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/omit.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
9 #include <boost/fusion/include/vector.hpp>
10 #include <boost/fusion/include/at.hpp>
19 using namespace boost::spirit::x3::ascii
;
20 using boost::spirit::x3::omit
;
21 using boost::spirit::x3::unused_type
;
22 using boost::spirit::x3::unused
;
23 using boost::spirit::x3::int_
;
25 using boost::fusion::vector
;
26 using boost::fusion::at_c
;
28 using spirit_test::test
;
29 using spirit_test::test_attr
;
31 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(omit
['x']);
34 BOOST_TEST(test("a", omit
['a']));
38 // omit[] means we don't receive the attribute
40 BOOST_TEST((test_attr("abc", omit
[char_
] >> omit
['b'] >> char_
, attr
)));
41 BOOST_TEST((attr
== 'c'));
45 // If all elements except 1 is omitted, the attribute is
46 // a single-element sequence. For this case alone, we allow
47 // naked attributes (unwrapped in a fusion sequence).
49 BOOST_TEST((test_attr("abc", omit
[char_
] >> 'b' >> char_
, attr
)));
50 BOOST_TEST((attr
== 'c'));
54 // omit[] means we don't receive the attribute
56 BOOST_TEST((test_attr("abc", omit
[char_
] >> omit
['b'] >> omit
[char_
], attr
)));
60 // omit[] means we don't receive the attribute
61 // this test is merely a compile test, because using a unused as the
62 // explicit attribute doesn't make any sense
64 BOOST_TEST((test_attr("abc", omit
[char_
>> 'b' >> char_
], attr
)));
68 // omit[] means we don't receive the attribute, if all elements of a
69 // sequence have unused attributes, the whole sequence has an unused
71 vector
<char, char> attr
;
72 BOOST_TEST((test_attr("abcde",
73 char_
>> (omit
[char_
] >> omit
['c'] >> omit
[char_
]) >> char_
, attr
)));
74 BOOST_TEST((at_c
<0>(attr
) == 'a'));
75 BOOST_TEST((at_c
<1>(attr
) == 'e'));
79 // "hello" has an unused_type. unused attrubutes are not part of the sequence
80 vector
<char, char> attr
;
81 BOOST_TEST((test_attr("a hello c", char_
>> "hello" >> char_
, attr
, space
)));
82 BOOST_TEST((at_c
<0>(attr
) == 'a'));
83 BOOST_TEST((at_c
<1>(attr
) == 'c'));
87 // if only one node in a sequence is left (all the others are omitted),
88 // then we need "naked" attributes (not wrapped in a tuple)
90 BOOST_TEST((test_attr("a 123 c", omit
['a'] >> int_
>> omit
['c'], attr
, space
)));
91 BOOST_TEST((attr
== 123));
95 // unused means we don't care about the attribute
96 BOOST_TEST((test_attr("abc", char_
>> 'b' >> char_
, unused
)));
99 { // test action with omitted attribute
101 auto f
= [&](auto& ctx
){ c
= _attr(ctx
); };
103 BOOST_TEST(test("x123\"a string\"", (char_
>> omit
[int_
] >> "\"a string\"")[f
]));
104 BOOST_TEST(c
== 'x');
107 { // test action with omitted attribute
109 auto f
= [&](auto& ctx
){ n
= _attr(ctx
); };
111 BOOST_TEST(test("x 123 \"a string\"", (omit
[char_
] >> int_
>> "\"a string\"")[f
], space
));
112 BOOST_TEST(n
== 123);
115 return boost::report_errors();