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