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/type_traits/is_same.hpp>
14
15 #include <sstream>
16
17 #include "test.hpp"
18
19 template <typename Expr, typename Iterator = boost::spirit::unused_type>
20 struct attribute_of_parser
21 {
22 typedef typename boost::spirit::result_of::compile<
23 boost::spirit::qi::domain, Expr
24 >::type parser_expression_type;
25
26 typedef typename boost::spirit::traits::attribute_of<
27 parser_expression_type, boost::spirit::unused_type, Iterator
28 >::type type;
29 };
30
31 template <typename Expected, typename Expr>
compare_attribute_type(Expr const &)32 inline bool compare_attribute_type(Expr const&)
33 {
34 typedef typename attribute_of_parser<Expr>::type type;
35 return boost::is_same<type, Expected>::value;
36 }
37
check(boost::spirit::utree const & val,std::string expected)38 inline bool check(boost::spirit::utree const& val, std::string expected)
39 {
40 std::stringstream s;
41 s << val;
42 if (s.str() == expected + " ")
43 return true;
44
45 std::cerr << "got result: " << s.str()
46 << ", expected: " << expected << std::endl;
47 return false;
48 }
49
main()50 int main()
51 {
52 using spirit_test::test_attr;
53 using boost::spirit::utree;
54 using boost::spirit::utree_type;
55 using boost::spirit::utf8_string_range_type;
56 using boost::spirit::utf8_symbol_type;
57 using boost::spirit::utf8_string_type;
58
59 using boost::spirit::qi::real_parser;
60 using boost::spirit::qi::strict_real_policies;
61 using boost::spirit::qi::digit;
62 using boost::spirit::qi::char_;
63 using boost::spirit::qi::string;
64 using boost::spirit::qi::int_;
65 using boost::spirit::qi::double_;
66 using boost::spirit::qi::space;
67 using boost::spirit::qi::space_type;
68 using boost::spirit::qi::rule;
69 using boost::spirit::qi::as;
70 using boost::spirit::qi::lexeme;
71
72 // kleene star
73 {
74 typedef real_parser<double, strict_real_policies<double> >
75 strict_double_type;
76 strict_double_type const strict_double = strict_double_type();
77
78 utree ut;
79 BOOST_TEST(test_attr("xy", *char_, ut) &&
80 ut.which() == utree_type::list_type && check(ut, "( \"x\" \"y\" )"));
81 ut.clear();
82 BOOST_TEST(test_attr("123 456", *int_, ut, space) &&
83 ut.which() == utree_type::list_type && check(ut, "( 123 456 )"));
84 ut.clear();
85 BOOST_TEST(test_attr("1.23 4.56", *double_, ut, space) &&
86 ut.which() == utree_type::list_type && check(ut, "( 1.23 4.56 )"));
87 ut.clear();
88
89 rule<char const*, utree(), space_type> r1;
90 rule<char const*, utree::list_type(), space_type> r2 = '(' >> *r1 >> ')';
91 r1 = strict_double | int_ | ~char_("()") | r2;
92
93 BOOST_TEST(test_attr("(x y)", r1, ut, space) &&
94 ut.which() == utree_type::list_type && check(ut, "( \"x\" \"y\" )"));
95 ut.clear();
96 BOOST_TEST(test_attr("(((123)) 456)", r1, ut, space) &&
97 ut.which() == utree_type::list_type && check(ut, "( ( ( 123 ) ) 456 )"));
98 ut.clear();
99 BOOST_TEST(test_attr("((1.23 4.56))", r1, ut, space) &&
100 ut.which() == utree_type::list_type && check(ut, "( ( 1.23 4.56 ) )"));
101 ut.clear();
102 BOOST_TEST(test_attr("x", r1, ut, space) &&
103 ut.which() == utree_type::string_type && check(ut, "\"x\""));
104 ut.clear();
105 BOOST_TEST(test_attr("123", r1, ut, space) &&
106 ut.which() == utree_type::int_type && check(ut, "123"));
107 ut.clear();
108 BOOST_TEST(test_attr("123.456", r1, ut, space) &&
109 ut.which() == utree_type::double_type && check(ut, "123.456"));
110 ut.clear();
111 BOOST_TEST(test_attr("()", r1, ut, space) &&
112 ut.which() == utree_type::list_type &&
113 check(ut, "( )"));
114 ut.clear();
115 BOOST_TEST(test_attr("((()))", r1, ut, space) &&
116 ut.which() == utree_type::list_type &&
117 check(ut, "( ( ( ) ) )"));
118 ut.clear();
119 }
120
121 // special attribute transformation for utree in alternatives
122 {
123 rule<char const*, utree()> r1;
124 rule<char const*, utree::list_type()> r2;
125
126 BOOST_TEST(compare_attribute_type<utree>(
127 r1 | -r1 | *r1 | r2 | -r2 | *r2));
128 }
129
130 // lists
131 {
132 utree ut;
133 BOOST_TEST(test_attr("x,y", char_ % ',', ut) &&
134 ut.which() == utree_type::list_type && check(ut, "( \"x\" \"y\" )"));
135 ut.clear();
136 BOOST_TEST(test_attr("123,456", int_ % ',', ut) &&
137 ut.which() == utree_type::list_type && check(ut, "( 123 456 )"));
138 ut.clear();
139 BOOST_TEST(test_attr("1.23,4.56", double_ % ',', ut) &&
140 ut.which() == utree_type::list_type && check(ut, "( 1.23 4.56 )"));
141
142 rule<char const*, std::vector<char>()> r1 = char_ % ',';
143 ut.clear();
144 BOOST_TEST(test_attr("x,y", r1, ut) &&
145 ut.which() == utree_type::list_type && check(ut, "( \"x\" \"y\" )"));
146
147 rule<char const*, std::vector<int>()> r2 = int_ % ',';
148 ut.clear();
149 BOOST_TEST(test_attr("123,456", r2, ut) &&
150 ut.which() == utree_type::list_type && check(ut, "( 123 456 )"));
151
152 rule<char const*, std::vector<double>()> r3 = double_ % ',';
153 ut.clear();
154 BOOST_TEST(test_attr("1.23,4.56", r3, ut) &&
155 ut.which() == utree_type::list_type && check(ut, "( 1.23 4.56 )"));
156
157 rule<char const*, utree()> r4 = double_ % ',';
158 ut.clear();
159 BOOST_TEST(test_attr("1.23,4.56", r4, ut) &&
160 ut.which() == utree_type::list_type && check(ut, "( 1.23 4.56 )"));
161 }
162
163 return boost::report_errors();
164 }
165