• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fusion/include/std_pair.hpp>
20 
21 #include <string>
22 #include <cstring>
23 #include <iostream>
24 #include "test.hpp"
25 
main()26 int main()
27 {
28     using spirit_test::test;
29     using spirit_test::test_delimited;
30 
31     using namespace boost::spirit::ascii;
32     using namespace boost::spirit::karma::labels;
33     using boost::spirit::karma::locals;
34     using boost::spirit::karma::rule;
35     using boost::spirit::karma::char_;
36     using boost::spirit::karma::debug;
37     using boost::spirit::karma::space;
38     using boost::spirit::karma::eps;
39 
40     typedef spirit_test::output_iterator<char>::type outiter_type;
41 
42     { // basic tests
43         rule<outiter_type, char()> a, b, c;
44         rule<outiter_type, std::vector<char>()> start;
45 
46         std::vector<char> v;
47         v.push_back('a');
48         v.push_back('b');
49         v.push_back('a');
50         v.push_back('c');
51         v.push_back('a');
52         v.push_back('b');
53         v.push_back('b');
54         v.push_back('a');
55 
56         a = char_('a');
57         b = char_('b');
58         c = char_('c');
59         BOOST_SPIRIT_DEBUG_NODE(a);
60         BOOST_SPIRIT_DEBUG_NODE(b);
61         BOOST_SPIRIT_DEBUG_NODE(c);
62 
63         start = *(a | b | c);
64         BOOST_SPIRIT_DEBUG_NODE(start);
65         BOOST_TEST(test("abacabba", start, v));
66 
67         // ignore the delimiter
68         BOOST_TEST(test_delimited("abacabba ", start, v, space));
69 
70         std::vector<char> v1;
71         v1.push_back('b');
72         v1.push_back('c');
73 
74         start = (a | b) << c;
75         BOOST_SPIRIT_DEBUG_NODE(start);
76         BOOST_TEST(test("bc", start, v1));
77     }
78 
79     { // tests with locals
80         rule<outiter_type, char()> a, b, c;
81         rule<outiter_type, std::vector<char>(), locals<int, double> > start;
82 
83         std::vector<char> v;
84         v.push_back('a');
85         v.push_back('b');
86         v.push_back('a');
87         v.push_back('c');
88         v.push_back('a');
89         v.push_back('b');
90         v.push_back('b');
91         v.push_back('a');
92 
93         a = char_('a');
94         b = char_('b');
95         c = char_('c');
96         BOOST_SPIRIT_DEBUG_NODE(a);
97         BOOST_SPIRIT_DEBUG_NODE(b);
98         BOOST_SPIRIT_DEBUG_NODE(c);
99 
100         start %= eps[(_a = 0, _b = 2.0)] << *(a[++_a] | b | c);
101         BOOST_SPIRIT_DEBUG_NODE(start);
102         BOOST_TEST(test("abacabba", start, v));
103     }
104 
105     return boost::report_errors();
106 }
107 
108