| 1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/spirit/include/karma_sequence.hpp> |
| 7 | |
| 8 | #include <boost/spirit/include/karma_char.hpp> |
| 9 | #include <boost/spirit/include/karma_string.hpp> |
| 10 | #include <boost/spirit/include/karma_numeric.hpp> |
| 11 | #include <boost/spirit/include/karma_generate.hpp> |
| 12 | #include <boost/spirit/include/karma_operator.hpp> |
| 13 | #include <boost/spirit/include/karma_directive.hpp> |
| 14 | #include <boost/spirit/include/karma_action.hpp> |
| 15 | #include <boost/spirit/include/karma_nonterminal.hpp> |
| 16 | #include <boost/spirit/include/karma_auxiliary.hpp> |
| 17 | #include <boost/spirit/include/karma_directive.hpp> |
| 18 | #include <boost/spirit/include/support_unused.hpp> |
| 19 | #include <boost/phoenix/core.hpp> |
| 20 | #include <boost/phoenix/operator.hpp> |
| 21 | #include <boost/phoenix/function.hpp> |
| 22 | #include <boost/fusion/include/vector.hpp> |
| 23 | |
| 24 | #include "test.hpp" |
| 25 | |
| 26 | using namespace spirit_test; |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // lazy version of fusion::size |
| 30 | struct seqsize_impl |
| 31 | { |
| 32 | template <typename Sequence> |
| 33 | struct result |
| 34 | : boost::fusion::result_of::size<Sequence> |
| 35 | {}; |
| 36 | |
| 37 | template <typename This, typename Sequence> |
| 38 | struct result<This(Sequence)> |
| 39 | : result<typename boost::proto::detail::uncvref<Sequence>::type> |
| 40 | {}; |
| 41 | |
| 42 | template <typename Sequence> |
| 43 | typename result<Sequence>::type |
| 44 | operator()(Sequence const& seq) const |
| 45 | { |
| 46 | return boost::fusion::size(seq); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | boost::phoenix::function<seqsize_impl> const seqsize = seqsize_impl(); |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | int main() |
| 54 | { |
| 55 | using namespace boost::spirit; |
| 56 | using namespace boost::spirit::ascii; |
| 57 | namespace fusion = boost::fusion; |
| 58 | |
| 59 | { |
| 60 | std::list<int> v; |
| 61 | v.push_back(x: 1); |
| 62 | v.push_back(x: 2); |
| 63 | v.push_back(x: 3); |
| 64 | BOOST_TEST(test("123" , int_ << int_ << int_, v)); |
| 65 | BOOST_TEST(test_delimited("1 2 3 " , int_ << int_ << int_, v, ' ')); |
| 66 | BOOST_TEST(test("1,2,3" , int_ << ',' << int_ << ',' << int_, v)); |
| 67 | BOOST_TEST(test_delimited("1 , 2 , 3 " , int_ << ',' << int_ << ',' << int_, v, ' ')); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | BOOST_TEST(test("aa" , lower[char_('A') << 'a'])); |
| 72 | BOOST_TEST(test_delimited("BEGIN END " , |
| 73 | upper[lit("begin" ) << "end" ], char(' '))); |
| 74 | BOOST_TEST(!test_delimited("BEGIN END " , |
| 75 | upper[lit("begin" ) << "nend" ], char(' '))); |
| 76 | |
| 77 | BOOST_TEST(test("Aa " , left_align[char_('A') << 'a'])); |
| 78 | BOOST_TEST(test(" Aa " , center[char_('A') << 'a'])); |
| 79 | BOOST_TEST(test(" Aa" , right_align[char_('A') << 'a'])); |
| 80 | } |
| 81 | |
| 82 | { |
| 83 | // make sure single element tuples get passed through if the rhs |
| 84 | // has a single element tuple as its attribute |
| 85 | typedef spirit_test::output_iterator<char>::type iterator_type; |
| 86 | fusion::vector<double, int> fv(2.0, 1); |
| 87 | karma::rule<iterator_type, fusion::vector<double, int>()> r; |
| 88 | r %= double_ << ',' << int_; |
| 89 | BOOST_TEST(test("test:2.0,1" , "test:" << r, fv)); |
| 90 | } |
| 91 | |
| 92 | // action tests |
| 93 | { |
| 94 | using namespace boost::phoenix; |
| 95 | |
| 96 | BOOST_TEST(test("abcdefg" , |
| 97 | (char_ << char_ << string)[(_1 = 'a', _2 = 'b', _3 = "cdefg" )])); |
| 98 | BOOST_TEST(test_delimited("a b cdefg " , |
| 99 | (char_ << char_ << string)[(_1 = 'a', _2 = 'b', _3 = "cdefg" )], |
| 100 | char(' '))); |
| 101 | |
| 102 | BOOST_TEST(test_delimited("a 12 c " , |
| 103 | (char_ << lit(12) << char_)[(_1 = 'a', _2 = 'c')], char(' '))); |
| 104 | |
| 105 | char c = 'c'; |
| 106 | BOOST_TEST(test("abc" , |
| 107 | (char_[_1 = 'a'] << 'b' << char_)[(_1 = 'x', _2 = ref(c))])); |
| 108 | BOOST_TEST(test_delimited("a b c " , |
| 109 | (char_[_1 = 'a'] << 'b' << char_)[_2 = ref(c)], char(' '))); |
| 110 | |
| 111 | BOOST_TEST(test("aa" , lower[char_ << 'A'][_1 = 'A'])); |
| 112 | BOOST_TEST(test("AA" , upper[char_ << 'a'][_1 = 'a'])); |
| 113 | |
| 114 | BOOST_TEST(test("Aa " , left_align[char_ << 'a'][_1 = 'A'])); |
| 115 | BOOST_TEST(test(" Aa " , center[char_ << 'a'][_1 = 'A'])); |
| 116 | BOOST_TEST(test(" Aa" , right_align[char_ << 'a'][_1 = 'A'])); |
| 117 | } |
| 118 | |
| 119 | // test special case where sequence has a one element vector attribute |
| 120 | // sequence and this element is a rule (attribute has to be passed through |
| 121 | // without change) |
| 122 | { |
| 123 | typedef spirit_test::output_iterator<char>::type outiter_type; |
| 124 | namespace karma = boost::spirit::karma; |
| 125 | |
| 126 | karma::rule<outiter_type, std::vector<int>()> r = -(int_ % ','); |
| 127 | std::vector<int> v; |
| 128 | BOOST_TEST(test(">" , '>' << r, v)); |
| 129 | |
| 130 | v.push_back(x: 1); |
| 131 | v.push_back(x: 2); |
| 132 | v.push_back(x: 3); |
| 133 | v.push_back(x: 4); |
| 134 | BOOST_TEST(test(">1,2,3,4" , '>' << r, v)); |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | namespace karma = boost::spirit::karma; |
| 139 | typedef spirit_test::output_iterator<char>::type outiter_type; |
| 140 | |
| 141 | karma::rule<outiter_type, std::string()> e = karma::string; |
| 142 | karma::rule<outiter_type, std::vector<std::string>()> l = e << *(',' << e); |
| 143 | |
| 144 | std::vector<std::string> v; |
| 145 | v.push_back(x: "abc1" ); |
| 146 | v.push_back(x: "abc2" ); |
| 147 | v.push_back(x: "abc3" ); |
| 148 | BOOST_TEST(test("abc1,abc2,abc3" , l, v)); |
| 149 | } |
| 150 | |
| 151 | { |
| 152 | namespace karma = boost::spirit::karma; |
| 153 | namespace phoenix = boost::phoenix; |
| 154 | |
| 155 | typedef spirit_test::output_iterator<char>::type outiter_type; |
| 156 | typedef fusion::vector<char, char, char> vector_type; |
| 157 | |
| 158 | vector_type p ('a', 'b', 'c'); |
| 159 | BOOST_TEST(test("ab" , char_ << char_, p)); |
| 160 | |
| 161 | karma::rule<outiter_type, vector_type()> r; |
| 162 | r %= char_ << char_ << &karma::eps[seqsize(_val) == 3]; |
| 163 | BOOST_TEST(!test("" , r, p)); |
| 164 | |
| 165 | r %= char_ << char_ << char_ << &karma::eps[seqsize(_val) == 3]; |
| 166 | BOOST_TEST(test("abc" , r, p)); |
| 167 | } |
| 168 | |
| 169 | { |
| 170 | std::list<int> v; |
| 171 | v.push_back(x: 1); |
| 172 | v.push_back(x: 2); |
| 173 | v.push_back(x: 3); |
| 174 | v.push_back(x: 4); |
| 175 | |
| 176 | BOOST_TEST(test("1234" , repeat(2)[int_] << *int_, v)); |
| 177 | BOOST_TEST(test_delimited("1 2 3 4 " , repeat(2)[int_] << *int_, v, char(' '))); |
| 178 | } |
| 179 | |
| 180 | return boost::report_errors(); |
| 181 | } |
| 182 | |
| 183 | |