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