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/spirit/include/qi.hpp>
12 #include <boost/spirit/include/support_utree.hpp>
13 #include <boost/mpl/print.hpp>
14
15 #include <sstream>
16
17 #include "test.hpp"
18
check(boost::spirit::utree const & val,std::string expected)19 inline bool check(boost::spirit::utree const& val, std::string expected)
20 {
21 std::stringstream s;
22 s << val;
23 if (s.str() == expected + " ")
24 return true;
25
26 std::cerr << "got result: " << s.str()
27 << ", expected: " << expected << std::endl;
28 return false;
29 }
30
main()31 int main()
32 {
33 using spirit_test::test_attr;
34 using boost::spirit::utree;
35 using boost::spirit::utree_type;
36 using boost::spirit::utf8_string_range_type;
37 using boost::spirit::utf8_symbol_type;
38 using boost::spirit::utf8_string_type;
39
40 using boost::spirit::qi::real_parser;
41 using boost::spirit::qi::strict_real_policies;
42 using boost::spirit::qi::digit;
43 using boost::spirit::qi::char_;
44 using boost::spirit::qi::string;
45 using boost::spirit::qi::int_;
46 using boost::spirit::qi::double_;
47 using boost::spirit::qi::space;
48 using boost::spirit::qi::space_type;
49 using boost::spirit::qi::rule;
50 using boost::spirit::qi::as;
51 using boost::spirit::qi::lexeme;
52
53 // as
54 {
55 typedef as<std::string> as_string_type;
56 as_string_type const as_string = as_string_type();
57
58 typedef as<utf8_symbol_type> as_symbol_type;
59 as_symbol_type const as_symbol = as_symbol_type();
60
61 utree ut;
62 BOOST_TEST(test_attr("xy", as_string[char_ >> char_], ut) &&
63 ut.which() == utree_type::string_type && check(ut, "\"xy\""));
64 ut.clear();
65
66 BOOST_TEST(test_attr("ab1.2", as_string[*~digit] >> double_, ut) &&
67 ut.which() == utree_type::list_type && check(ut, "( \"ab\" 1.2 )"));
68 ut.clear();
69
70 BOOST_TEST(test_attr("xy", as_string[*char_], ut) &&
71 ut.which() == utree_type::string_type && check(ut, "\"xy\""));
72 ut.clear();
73
74 BOOST_TEST(test_attr("x,y", as_string[char_ >> ',' >> char_], ut) &&
75 ut.which() == utree_type::string_type && check(ut, "\"xy\""));
76 ut.clear();
77
78 BOOST_TEST(test_attr("x,y", char_ >> ',' >> char_, ut) &&
79 ut.which() == utree_type::list_type && check(ut, "( \"x\" \"y\" )"));
80 ut.clear();
81
82 BOOST_TEST(test_attr("a,b1.2", as_string[~digit % ','] >> double_, ut) &&
83 ut.which() == utree_type::list_type && check(ut, "( \"ab\" 1.2 )"));
84 ut.clear();
85
86 BOOST_TEST(test_attr("a,b1.2", ~digit % ',' >> double_, ut) &&
87 ut.which() == utree_type::list_type && check(ut, "( \"a\" \"b\" 1.2 )"));
88 ut.clear();
89
90 BOOST_TEST(test_attr("xy", as_symbol[char_ >> char_], ut) &&
91 ut.which() == utree_type::symbol_type && check(ut, "xy"));
92 ut.clear();
93
94 BOOST_TEST(test_attr("ab1.2", as_symbol[*~digit] >> double_, ut) &&
95 ut.which() == utree_type::list_type && check(ut, "( ab 1.2 )"));
96 ut.clear();
97
98 BOOST_TEST(test_attr("xy", as_symbol[*char_], ut) &&
99 ut.which() == utree_type::symbol_type && check(ut, "xy"));
100 ut.clear();
101
102 BOOST_TEST(test_attr("x,y", as_symbol[char_ >> ',' >> char_], ut) &&
103 ut.which() == utree_type::symbol_type && check(ut, "xy"));
104 ut.clear();
105 BOOST_TEST(test_attr("a,b1.2", as_symbol[~digit % ','] >> double_, ut) &&
106 ut.which() == utree_type::list_type && check(ut, "( ab 1.2 )"));
107 ut.clear();
108 }
109
110 // subtrees
111 {
112 // -(+int_) is forcing a subtree
113 utree ut;
114 BOOST_TEST(test_attr("1 2", int_ >> ' ' >> -(+int_), ut) &&
115 ut.which() == utree_type::list_type && check(ut, "( 1 2 )"));
116 ut.clear();
117
118 BOOST_TEST(test_attr("1 2", int_ >> ' ' >> *int_, ut) &&
119 ut.which() == utree_type::list_type && check(ut, "( 1 2 )"));
120 ut.clear();
121
122 rule<char const*, std::vector<int>()> r1 = int_ % ',';
123 BOOST_TEST(test_attr("1 2,3", int_ >> ' ' >> r1, ut) &&
124 ut.which() == utree_type::list_type && check(ut, "( 1 2 3 )"));
125 ut.clear();
126
127 BOOST_TEST(test_attr("1,2 2,3", r1 >> ' ' >> r1, ut) &&
128 ut.which() == utree_type::list_type && check(ut, "( 1 2 2 3 )"));
129 ut.clear();
130
131 rule<char const*, utree()> r2 = int_ % ',';
132 BOOST_TEST(test_attr("1 2,3", int_ >> ' ' >> r2, ut) &&
133 ut.which() == utree_type::list_type && check(ut, "( 1 2 3 )"));
134 ut.clear();
135
136 BOOST_TEST(test_attr("1,2 2,3", r2 >> ' ' >> r2, ut) &&
137 ut.which() == utree_type::list_type && check(ut, "( 1 2 2 3 )"));
138 ut.clear();
139
140 rule<char const*, utree::list_type()> r3 = int_ % ',';
141 BOOST_TEST(test_attr("1 2,3", int_ >> ' ' >> r3, ut) &&
142 ut.which() == utree_type::list_type && check(ut, "( 1 ( 2 3 ) )"));
143 ut.clear();
144
145 BOOST_TEST(test_attr("1,2 2,3", r3 >> ' ' >> r3, ut) &&
146 ut.which() == utree_type::list_type && check(ut, "( ( 1 2 ) ( 2 3 ) )"));
147 ut.clear();
148
149 rule<char const*, utree()> r4 = int_;
150 BOOST_TEST(test_attr("1 1", int_ >> ' ' >> -r4, ut) &&
151 ut.which() == utree_type::list_type && check(ut, "( 1 1 )"));
152 ut.clear();
153
154 BOOST_TEST(test_attr("1 ", int_ >> ' ' >> -r4, ut) &&
155 ut.which() == utree_type::list_type && check(ut, "( 1 )"));
156 ut.clear();
157
158 rule<char const*, utree::list_type()> r5 = -r4;
159 BOOST_TEST(test_attr("1", r5, ut) &&
160 ut.which() == utree_type::list_type && check(ut, "( 1 )"));
161 ut.clear();
162
163 BOOST_TEST(test_attr("", r5, ut) &&
164 ut.which() == utree_type::list_type && check(ut, "( )"));
165 ut.clear();
166
167 BOOST_TEST(test_attr("1 1", r5 >> ' ' >> r5, ut) &&
168 ut.which() == utree_type::list_type && check(ut, "( ( 1 ) ( 1 ) )"));
169 ut.clear();
170
171 rule<char const*, utree::list_type()> r6 = int_;
172 rule<char const*, utree()> r7 = -r6;
173 BOOST_TEST(test_attr("1", r7, ut) &&
174 ut.which() == utree_type::list_type && check(ut, "( 1 )"));
175 ut.clear();
176
177 rule<char const*, utree::list_type()> r8 = r6 >> ' ' >> r6;
178 BOOST_TEST(test_attr("1 1", r8, ut) &&
179 ut.which() == utree_type::list_type && check(ut, "( ( 1 ) ( 1 ) )"));
180 ut.clear();
181 }
182
183 return boost::report_errors();
184 }
185