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