| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2015 Joel de Guzman |
| 3 | |
| 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/spirit/home/x3.hpp> |
| 8 | #include <boost/fusion/include/vector.hpp> |
| 9 | #include <boost/fusion/include/at.hpp> |
| 10 | |
| 11 | #include <string> |
| 12 | #include <iostream> |
| 13 | #include "test.hpp" |
| 14 | |
| 15 | using boost::spirit::x3::rule; |
| 16 | |
| 17 | rule<class direct_rule, int> direct_rule = "direct_rule" ; |
| 18 | rule<class indirect_rule, int> indirect_rule = "indirect_rule" ; |
| 19 | |
| 20 | auto const direct_rule_def = boost::spirit::x3::int_; |
| 21 | auto const indirect_rule_def = direct_rule; |
| 22 | |
| 23 | BOOST_SPIRIT_DEFINE(direct_rule, indirect_rule) |
| 24 | |
| 25 | int |
| 26 | main() |
| 27 | { |
| 28 | using namespace boost::spirit::x3::ascii; |
| 29 | using boost::spirit::x3::omit; |
| 30 | using boost::spirit::x3::unused_type; |
| 31 | using boost::spirit::x3::unused; |
| 32 | using boost::spirit::x3::int_; |
| 33 | |
| 34 | using boost::fusion::vector; |
| 35 | using boost::fusion::at_c; |
| 36 | |
| 37 | using spirit_test::test; |
| 38 | using spirit_test::test_attr; |
| 39 | |
| 40 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(omit['x']); |
| 41 | |
| 42 | { |
| 43 | BOOST_TEST(test("a" , omit['a'])); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | // omit[] means we don't receive the attribute |
| 48 | char attr; |
| 49 | BOOST_TEST((test_attr("abc" , omit[char_] >> omit['b'] >> char_, attr))); |
| 50 | BOOST_TEST((attr == 'c')); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | // If all elements except 1 is omitted, the attribute is |
| 55 | // a single-element sequence. For this case alone, we allow |
| 56 | // naked attributes (unwrapped in a fusion sequence). |
| 57 | char attr; |
| 58 | BOOST_TEST((test_attr("abc" , omit[char_] >> 'b' >> char_, attr))); |
| 59 | BOOST_TEST((attr == 'c')); |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | // omit[] means we don't receive the attribute |
| 64 | vector<> attr; |
| 65 | BOOST_TEST((test_attr("abc" , omit[char_] >> omit['b'] >> omit[char_], attr))); |
| 66 | } |
| 67 | |
| 68 | { |
| 69 | // omit[] means we don't receive the attribute |
| 70 | // this test is merely a compile test, because using a unused as the |
| 71 | // explicit attribute doesn't make any sense |
| 72 | unused_type attr; |
| 73 | BOOST_TEST((test_attr("abc" , omit[char_ >> 'b' >> char_], attr))); |
| 74 | } |
| 75 | |
| 76 | { |
| 77 | // omit[] means we don't receive the attribute, if all elements of a |
| 78 | // sequence have unused attributes, the whole sequence has an unused |
| 79 | // attribute as well |
| 80 | vector<char, char> attr; |
| 81 | BOOST_TEST((test_attr("abcde" , |
| 82 | char_ >> (omit[char_] >> omit['c'] >> omit[char_]) >> char_, attr))); |
| 83 | BOOST_TEST((at_c<0>(attr) == 'a')); |
| 84 | BOOST_TEST((at_c<1>(attr) == 'e')); |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | // "hello" has an unused_type. unused attrubutes are not part of the sequence |
| 89 | vector<char, char> attr; |
| 90 | BOOST_TEST((test_attr("a hello c" , char_ >> "hello" >> char_, attr, space))); |
| 91 | BOOST_TEST((at_c<0>(attr) == 'a')); |
| 92 | BOOST_TEST((at_c<1>(attr) == 'c')); |
| 93 | } |
| 94 | |
| 95 | { |
| 96 | // if only one node in a sequence is left (all the others are omitted), |
| 97 | // then we need "naked" attributes (not wrapped in a tuple) |
| 98 | int attr; |
| 99 | BOOST_TEST((test_attr("a 123 c" , omit['a'] >> int_ >> omit['c'], attr, space))); |
| 100 | BOOST_TEST((attr == 123)); |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | // unused means we don't care about the attribute |
| 105 | BOOST_TEST((test_attr("abc" , char_ >> 'b' >> char_, unused))); |
| 106 | } |
| 107 | |
| 108 | { // test action with omitted attribute |
| 109 | char c = 0; |
| 110 | auto f = [&](auto& ctx){ c = _attr(ctx); }; |
| 111 | |
| 112 | BOOST_TEST(test("x123\"a string\"" , (char_ >> omit[int_] >> "\"a string\"" )[f])); |
| 113 | BOOST_TEST(c == 'x'); |
| 114 | } |
| 115 | |
| 116 | { // test action with omitted attribute |
| 117 | int n = 0; |
| 118 | auto f = [&](auto& ctx){ n = _attr(ctx); }; |
| 119 | |
| 120 | BOOST_TEST(test("x 123 \"a string\"" , (omit[char_] >> int_ >> "\"a string\"" )[f], space)); |
| 121 | BOOST_TEST(n == 123); |
| 122 | } |
| 123 | |
| 124 | { |
| 125 | // test with simple rule |
| 126 | BOOST_TEST((test_attr("123" , omit[direct_rule], unused))); |
| 127 | } |
| 128 | |
| 129 | { |
| 130 | // test with complex rule |
| 131 | BOOST_TEST((test_attr("123" , omit[indirect_rule], unused))); |
| 132 | } |
| 133 | |
| 134 | return boost::report_errors(); |
| 135 | } |
| 136 | |