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/config/warning_disable.hpp>
7 #include <boost/detail/lightweight_test.hpp>
8 
9 #include <boost/mpl/print.hpp>
10 
11 #include <boost/spirit/include/karma_operator.hpp>
12 #include <boost/spirit/include/karma_char.hpp>
13 #include <boost/spirit/include/karma_string.hpp>
14 #include <boost/spirit/include/karma_numeric.hpp>
15 #include <boost/spirit/include/karma_directive.hpp>
16 #include <boost/spirit/include/karma_action.hpp>
17 #include <boost/spirit/include/karma_nonterminal.hpp>
18 #include <boost/spirit/include/karma_auxiliary.hpp>
19 #include <boost/spirit/include/phoenix_core.hpp>
20 #include <boost/spirit/include/phoenix_operator.hpp>
21 #include <boost/spirit/include/phoenix_object.hpp>
22 #include <boost/spirit/include/phoenix_stl.hpp>
23 #include <boost/fusion/include/std_pair.hpp>
24 
25 #include <boost/assign/std/vector.hpp>
26 
27 #include <string>
28 #include <vector>
29 #include <iostream>
30 
31 #include "test.hpp"
32 
33 using namespace spirit_test;
34 using boost::spirit::unused_type;
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 struct action
38 {
actionaction39     action (std::vector<char>& vec)
40       : vec(vec), it(vec.begin())
41     {}
42 
operator ()action43     void operator()(unsigned& value, unused_type const&, bool& pass) const
44     {
45        pass = (it != vec.end());
46        if (pass)
47            value = *it++;
48     }
49 
50     std::vector<char>& vec;
51     mutable std::vector<char>::iterator it;
52 };
53 
54 ///////////////////////////////////////////////////////////////////////////////
main()55 int main()
56 {
57     using namespace boost::spirit;
58     using namespace boost::spirit::ascii;
59 
60     using namespace boost::assign;
61 
62     std::vector<char> v;
63     v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h';
64 
65     {
66         BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ',', v));
67         BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ",
68             char_ % ',', v, space));
69     }
70 
71     {
72         std::string s ("abcdefgh");
73         BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ',', s));
74         BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ",
75             char_ % ',', s, space));
76     }
77 
78     {
79         std::string s ("abcdefg");
80         BOOST_TEST(test("abc,de,fg", char_ << ((char_ << char_) % ','), s));
81         BOOST_TEST(test_delimited("a b c , d e , f g ",
82             char_ << ((char_ << char_) % ','), s, space));
83     }
84 
85     { // actions
86         namespace phx = boost::phoenix;
87 
88         BOOST_TEST(test("a,b,c,d,e,f,g,h", (char_ % ',')[_1 = phx::ref(v)]));
89         BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ",
90             (char_ % ',')[_1 = phx::ref(v)], space));
91     }
92 
93     // failing sub-generators
94     {
95         using boost::spirit::karma::strict;
96         using boost::spirit::karma::relaxed;
97 
98         typedef std::pair<char, char> data;
99         std::vector<data> v2;
100         v2 += std::make_pair('a', 'a'),
101               std::make_pair('b', 'b'),
102               std::make_pair('c', 'c'),
103               std::make_pair('d', 'd'),
104               std::make_pair('e', 'e'),
105               std::make_pair('f', 'f'),
106               std::make_pair('g', 'g');
107 
108         karma::rule<spirit_test::output_iterator<char>::type, data()> r;
109 
110         r = &char_('d') << char_;
111         BOOST_TEST(test("d", r % ',', v2));
112         BOOST_TEST(test("d", relaxed[r % ','], v2));
113         BOOST_TEST(!test("", strict[r % ','], v2));
114 
115         r = &char_('a') << char_;
116         BOOST_TEST(test("a", r % ',', v2));
117         BOOST_TEST(test("a", relaxed[r % ','], v2));
118         BOOST_TEST(test("a", strict[r % ','], v2));
119 
120         r = &char_('g') << char_;
121         BOOST_TEST(test("g", r % ',', v2));
122         BOOST_TEST(test("g", relaxed[r % ','], v2));
123         BOOST_TEST(!test("", strict[r % ','], v2));
124 
125         r = !char_('d') << char_;
126         BOOST_TEST(test("a,b,c,e,f,g", r % ',', v2));
127         BOOST_TEST(test("a,b,c,e,f,g", relaxed[r % ','], v2));
128         BOOST_TEST(test("a,b,c", strict[r % ','], v2));
129 
130         r = !char_('a') << char_;
131         BOOST_TEST(test("b,c,d,e,f,g", r % ',', v2));
132         BOOST_TEST(test("b,c,d,e,f,g", relaxed[r % ','], v2));
133         BOOST_TEST(!test("", strict[r % ','], v2));
134 
135         r = !char_('g') << char_;
136         BOOST_TEST(test("a,b,c,d,e,f", r % ',', v2));
137         BOOST_TEST(test("a,b,c,d,e,f", relaxed[r % ','], v2));
138         BOOST_TEST(test("a,b,c,d,e,f", strict[r % ','], v2));
139 
140         r = &char_('A') << char_;
141         BOOST_TEST(!test("", r % ',', v2));
142     }
143 
144     {
145         // make sure user defined end condition is applied if no attribute
146         // is passed in
147         BOOST_TEST(test("[61,62,63,64,65,66,67,68]",
148             '[' << hex[action(v)] % ',' << ']'));
149     }
150 
151     return boost::report_errors();
152 }
153 
154