• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
3 // Copyright (c)      2010 Bryce Lelbach
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 #include <boost/config/warning_disable.hpp>
9 #include <boost/detail/lightweight_test.hpp>
10 
11 #include <boost/mpl/print.hpp>
12 
13 #include <boost/spirit/include/karma.hpp>
14 #include <boost/spirit/include/support_utree.hpp>
15 
16 #include <sstream>
17 
18 #include "test.hpp"
19 
main()20 int main()
21 {
22     using spirit_test::test;
23     using spirit_test::test_delimited;
24     using boost::spirit::utree;
25     using boost::spirit::utree_type;
26     using boost::spirit::utf8_string_range_type;
27     using boost::spirit::utf8_string_type;
28     using boost::spirit::utf8_symbol_type;
29 
30     using boost::spirit::karma::char_;
31     using boost::spirit::karma::bool_;
32     using boost::spirit::karma::int_;
33     using boost::spirit::karma::double_;
34     using boost::spirit::karma::string;
35     using boost::spirit::karma::space;
36     using boost::spirit::karma::rule;
37 
38     typedef spirit_test::output_iterator<char>::type output_iterator;
39 
40     // kleene star
41     {
42         utree ut;
43         ut.push_back('a');
44         ut.push_back('b');
45         BOOST_TEST(test("ab", *char_, ut));
46 
47         ut.clear();
48         ut.push_back(123);
49         ut.push_back(456);
50         BOOST_TEST(test_delimited("123 456 ", *int_, ut, space));
51 
52         ut.clear();
53         ut.push_back(1.23);
54         ut.push_back(4.56);
55         BOOST_TEST(test_delimited("1.23 4.56 ", *double_, ut, space));
56     }
57 
58     // lists
59     {
60         rule<output_iterator, utree()> r1, r1ref;
61         rule<output_iterator, utf8_string_range_type()> r1str;
62         rule<output_iterator, utree::const_range()> r1list;
63 
64         r1 = double_ | int_ | r1str | r1list | r1ref;
65 
66         r1ref = r1.alias();
67 
68         r1str = string;
69 
70         r1list = '(' << -(r1 % ',') << ')';
71 
72         // ( "abc" "def" )
73         utree ut;
74         ut.push_back("abc");
75         ut.push_back("def");
76         BOOST_TEST(test("abc,def", string % ',', ut));
77         BOOST_TEST(test("(abc,def)", r1, ut));
78 
79         // ( ( "abc" "def" ) )
80         utree ut1;
81         ut1.push_back(ut);
82         BOOST_TEST(test("((abc,def))", r1, ut1));
83 
84 //         rule<output_iterator, std::vector<char>()> r2 = char_ % ',';
85 //         BOOST_TEST(test("abc,def", r2, ut));
86 //         BOOST_TEST(test("abc,def", r2, ut1));
87 
88         // ( ( "abc" "def" ) ( "abc" "def" ) )
89         ut1.push_back(ut);
90         BOOST_TEST(test("(abc,def) (abc,def)", r1 << ' ' << r1, ut1));
91 
92         // ( 123 456 )
93         ut.clear();
94         ut.push_back(123);
95         ut.push_back(456);
96         BOOST_TEST(test("123,456", int_ % ',', ut));
97         BOOST_TEST(test("(123,456)", r1, ut));
98 
99         // ( ( 123 456 ) )
100         ut1.clear();
101         ut1.push_back(ut);
102         BOOST_TEST(test("((123,456))", r1, ut1));
103 
104 //         rule<output_iterator, std::vector<int>()> r4 = int_ % ',';
105 //         BOOST_TEST(test("123,456", r4, ut));
106 //         BOOST_TEST(test("123,456", r4, ut1));
107 
108         // ( ( 123 456 ) ( 123 456 ) )
109         ut1.push_back(ut);
110         BOOST_TEST(test("(123,456) (123,456)", r1 << ' ' << r1, ut1));
111 
112         // ( 1.23 4.56 )
113         ut.clear();
114         ut.push_back(1.23);
115         ut.push_back(4.56);
116         BOOST_TEST(test("1.23,4.56", double_ % ',', ut));
117         BOOST_TEST(test("(1.23,4.56)", r1, ut));
118 
119         // ( ( 1.23 4.56 ) )
120         ut1.clear();
121         ut1.push_back(ut);
122         BOOST_TEST(test("((1.23,4.56))", r1, ut1));
123 
124 //         rule<output_iterator, std::vector<double>()> r6 = double_ % ',';
125 //         BOOST_TEST(test("1.23,4.56", r6, ut));
126 //         BOOST_TEST(test("1.23,4.56", r6, ut1));
127 
128         // ( ( 1.23 4.56 ) ( 1.23 4.56 ) )
129         ut1.push_back(ut);
130         BOOST_TEST(test("(1.23,4.56) (1.23,4.56)", r1 <<' ' << r1, ut1));
131     }
132 
133     // alternatives
134     {
135         rule<output_iterator, utree()> r1 = int_ | double_;
136         utree ut(10);
137         BOOST_TEST(test("10", int_ | double_, ut));
138         BOOST_TEST(test("10", r1, ut));
139 
140         ut = 10.2;
141         BOOST_TEST(test("10.2", int_ | double_, ut));
142         BOOST_TEST(test("10.2", r1, ut));
143     }
144 
145     // optionals
146     {
147         utree ut('x');
148         BOOST_TEST(test("x", -char_, ut));
149 
150         ut.clear();
151         BOOST_TEST(test("", -char_, ut));
152     }
153 
154     return boost::report_errors();
155 }
156