1 /*=============================================================================
2 Copyright (c) 2001-2013 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;
32 BOOST_TEST(test("a", omit['a']));
36 // omit[] means we don't receive the attribute
38 BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> char_, attr)));
39 BOOST_TEST((attr == 'c'));
43 // If all elements except 1 is omitted, the attribute is
44 // a single-element sequence. For this case alone, we allow
45 // naked attributes (unwrapped in a fusion sequence).
47 BOOST_TEST((test_attr("abc", omit[char_] >> 'b' >> char_, attr)));
48 BOOST_TEST((attr == 'c'));
52 // omit[] means we don't receive the attribute
54 BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> omit[char_], attr)));
58 // omit[] means we don't receive the attribute
59 // this test is merely a compile test, because using a unused as the
60 // explicit attribute doesn't make any sense
62 BOOST_TEST((test_attr("abc", omit[char_ >> 'b' >> char_], attr)));
66 // omit[] means we don't receive the attribute, if all elements of a
67 // sequence have unused attributes, the whole sequence has an unused
69 vector<char, char> attr;
70 BOOST_TEST((test_attr("abcde",
71 char_ >> (omit[char_] >> omit['c'] >> omit[char_]) >> char_, attr)));
72 BOOST_TEST((at_c<0>(attr) == 'a'));
73 BOOST_TEST((at_c<1>(attr) == 'e'));
77 // "hello" has an unused_type. unused attrubutes are not part of the sequence
78 vector<char, char> attr;
79 BOOST_TEST((test_attr("a hello c", char_ >> "hello" >> char_, attr, space)));
80 BOOST_TEST((at_c<0>(attr) == 'a'));
81 BOOST_TEST((at_c<1>(attr) == 'c'));
85 // if only one node in a sequence is left (all the others are omitted),
86 // then we need "naked" attributes (not wraped in a tuple)
88 BOOST_TEST((test_attr("a 123 c", omit['a'] >> int_ >> omit['c'], attr, space)));
89 BOOST_TEST((attr == 123));
93 // unused means we don't care about the attribute
94 BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, unused)));
97 // $$$ Not yet implemented $$$
98 //~ { // test action with omitted attribute
101 //~ using boost::phoenix::ref;
103 //~ BOOST_TEST(test("x123\"a string\"", (char_ >> omit[int_] >> "\"a string\"")
105 //~ BOOST_TEST(c == 'x');
108 // $$$ Not yet implemented $$$
109 //~ { // test action with omitted attribute
112 //~ using boost::phoenix::ref;
114 //~ BOOST_TEST(test("x 123 \"a string\"",
115 //~ (omit[char_] >> int_ >> "\"a string\"")[ref(n) = _1], space));
116 //~ BOOST_TEST(n == 123);
119 return boost::report_errors();