1 // Copyright (c) 2001-2011 Hartmut Kaiser
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 #define BOOST_SPIRIT_KARMA_DEBUG
8
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/spirit/include/karma_operator.hpp>
11 #include <boost/spirit/include/karma_char.hpp>
12 #include <boost/spirit/include/karma_string.hpp>
13 #include <boost/spirit/include/karma_numeric.hpp>
14 #include <boost/spirit/include/karma_auxiliary.hpp>
15 #include <boost/spirit/include/karma_nonterminal.hpp>
16 #include <boost/spirit/include/karma_action.hpp>
17 #include <boost/spirit/include/phoenix_core.hpp>
18 #include <boost/spirit/include/phoenix_operator.hpp>
19 #include <boost/spirit/include/phoenix_object.hpp>
20 #include <boost/spirit/include/phoenix_bind.hpp>
21 #include <boost/spirit/include/phoenix_statement.hpp>
22 #include <boost/fusion/include/std_pair.hpp>
23
24 #include <string>
25 #include <cstring>
26 #include <iostream>
27 #include "test.hpp"
28
main()29 int main()
30 {
31 using spirit_test::test;
32 using spirit_test::test_delimited;
33
34 using namespace boost::spirit::ascii;
35 using namespace boost::spirit::karma::labels;
36 using boost::spirit::karma::locals;
37 using boost::spirit::karma::rule;
38 using boost::spirit::karma::char_;
39 using boost::spirit::karma::debug;
40 using boost::spirit::karma::space;
41 using boost::spirit::karma::eps;
42
43 namespace phx = boost::phoenix;
44
45 typedef spirit_test::output_iterator<char>::type outiter_type;
46
47 { // basic tests
48 rule<outiter_type, char()> a, b, c;
49 rule<outiter_type, std::vector<char>()> start;
50
51 std::vector<char> v;
52 v.push_back('a');
53 v.push_back('b');
54 v.push_back('a');
55 v.push_back('c');
56 v.push_back('a');
57 v.push_back('b');
58 v.push_back('b');
59 v.push_back('a');
60
61 a = char_('a');
62 b = char_('b');
63 c = char_('c');
64 BOOST_SPIRIT_DEBUG_NODE(a);
65 BOOST_SPIRIT_DEBUG_NODE(b);
66 BOOST_SPIRIT_DEBUG_NODE(c);
67
68 start = *(a | b | c);
69 BOOST_SPIRIT_DEBUG_NODE(start);
70 BOOST_TEST(test("abacabba", start, v));
71
72 // ignore the delimiter
73 BOOST_TEST(test_delimited("abacabba ", start, v, space));
74
75 std::vector<char> v1;
76 v1.push_back('b');
77 v1.push_back('c');
78
79 start = (a | b) << c;
80 BOOST_SPIRIT_DEBUG_NODE(start);
81 BOOST_TEST(test("bc", start, v1));
82 }
83
84 { // tests with locals
85 rule<outiter_type, char()> a, b, c;
86 rule<outiter_type, std::vector<char>(), locals<int, double> > start;
87
88 std::vector<char> v;
89 v.push_back('a');
90 v.push_back('b');
91 v.push_back('a');
92 v.push_back('c');
93 v.push_back('a');
94 v.push_back('b');
95 v.push_back('b');
96 v.push_back('a');
97
98 a = char_('a');
99 b = char_('b');
100 c = char_('c');
101 BOOST_SPIRIT_DEBUG_NODE(a);
102 BOOST_SPIRIT_DEBUG_NODE(b);
103 BOOST_SPIRIT_DEBUG_NODE(c);
104
105 start %= eps[_a = 0, _b = 2.0] << *(a[++_a] | b | c);
106 BOOST_SPIRIT_DEBUG_NODE(start);
107 BOOST_TEST(test("abacabba", start, v));
108 }
109
110 return boost::report_errors();
111 }
112
113