123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- // Copyright (c) 2001-2011 Hartmut Kaiser
- // Copyright (c) 2001-2011 Joel de Guzman
- // Copyright (c) 2010 Bryce Lelbach
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- #include <boost/config/warning_disable.hpp>
- #include <boost/detail/lightweight_test.hpp>
- #include <boost/spirit/include/qi.hpp>
- #include <boost/spirit/include/support_utree.hpp>
- #include <boost/mpl/print.hpp>
- #include <sstream>
- #include "test.hpp"
- inline bool check(boost::spirit::utree const& val, std::string expected)
- {
- std::stringstream s;
- s << val;
- if (s.str() == expected + " ")
- return true;
- std::cerr << "got result: " << s.str()
- << ", expected: " << expected << std::endl;
- return false;
- }
- int main()
- {
- using spirit_test::test_attr;
- using boost::spirit::utree;
- using boost::spirit::utree_type;
- using boost::spirit::utf8_string_range_type;
- using boost::spirit::utf8_symbol_type;
- using boost::spirit::utf8_string_type;
- using boost::spirit::qi::real_parser;
- using boost::spirit::qi::strict_real_policies;
- using boost::spirit::qi::digit;
- using boost::spirit::qi::char_;
- using boost::spirit::qi::string;
- using boost::spirit::qi::int_;
- using boost::spirit::qi::double_;
- using boost::spirit::qi::space;
- using boost::spirit::qi::space_type;
- using boost::spirit::qi::rule;
- using boost::spirit::qi::as;
- using boost::spirit::qi::lexeme;
- // as
- {
- typedef as<std::string> as_string_type;
- as_string_type const as_string = as_string_type();
- typedef as<utf8_symbol_type> as_symbol_type;
- as_symbol_type const as_symbol = as_symbol_type();
- utree ut;
- BOOST_TEST(test_attr("xy", as_string[char_ >> char_], ut) &&
- ut.which() == utree_type::string_type && check(ut, "\"xy\""));
- ut.clear();
- BOOST_TEST(test_attr("ab1.2", as_string[*~digit] >> double_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( \"ab\" 1.2 )"));
- ut.clear();
- BOOST_TEST(test_attr("xy", as_string[*char_], ut) &&
- ut.which() == utree_type::string_type && check(ut, "\"xy\""));
- ut.clear();
- BOOST_TEST(test_attr("x,y", as_string[char_ >> ',' >> char_], ut) &&
- ut.which() == utree_type::string_type && check(ut, "\"xy\""));
- ut.clear();
- BOOST_TEST(test_attr("x,y", char_ >> ',' >> char_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( \"x\" \"y\" )"));
- ut.clear();
- BOOST_TEST(test_attr("a,b1.2", as_string[~digit % ','] >> double_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( \"ab\" 1.2 )"));
- ut.clear();
- BOOST_TEST(test_attr("a,b1.2", ~digit % ',' >> double_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( \"a\" \"b\" 1.2 )"));
- ut.clear();
-
- BOOST_TEST(test_attr("xy", as_symbol[char_ >> char_], ut) &&
- ut.which() == utree_type::symbol_type && check(ut, "xy"));
- ut.clear();
- BOOST_TEST(test_attr("ab1.2", as_symbol[*~digit] >> double_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( ab 1.2 )"));
- ut.clear();
- BOOST_TEST(test_attr("xy", as_symbol[*char_], ut) &&
- ut.which() == utree_type::symbol_type && check(ut, "xy"));
- ut.clear();
- BOOST_TEST(test_attr("x,y", as_symbol[char_ >> ',' >> char_], ut) &&
- ut.which() == utree_type::symbol_type && check(ut, "xy"));
- ut.clear();
- BOOST_TEST(test_attr("a,b1.2", as_symbol[~digit % ','] >> double_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( ab 1.2 )"));
- ut.clear();
- }
- // subtrees
- {
- // -(+int_) is forcing a subtree
- utree ut;
- BOOST_TEST(test_attr("1 2", int_ >> ' ' >> -(+int_), ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 2 )"));
- ut.clear();
- BOOST_TEST(test_attr("1 2", int_ >> ' ' >> *int_, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 2 )"));
- ut.clear();
- rule<char const*, std::vector<int>()> r1 = int_ % ',';
- BOOST_TEST(test_attr("1 2,3", int_ >> ' ' >> r1, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 2 3 )"));
- ut.clear();
- BOOST_TEST(test_attr("1,2 2,3", r1 >> ' ' >> r1, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 2 2 3 )"));
- ut.clear();
- rule<char const*, utree()> r2 = int_ % ',';
- BOOST_TEST(test_attr("1 2,3", int_ >> ' ' >> r2, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 2 3 )"));
- ut.clear();
- BOOST_TEST(test_attr("1,2 2,3", r2 >> ' ' >> r2, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 2 2 3 )"));
- ut.clear();
- rule<char const*, utree::list_type()> r3 = int_ % ',';
- BOOST_TEST(test_attr("1 2,3", int_ >> ' ' >> r3, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 ( 2 3 ) )"));
- ut.clear();
- BOOST_TEST(test_attr("1,2 2,3", r3 >> ' ' >> r3, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( ( 1 2 ) ( 2 3 ) )"));
- ut.clear();
- rule<char const*, utree()> r4 = int_;
- BOOST_TEST(test_attr("1 1", int_ >> ' ' >> -r4, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 1 )"));
- ut.clear();
- BOOST_TEST(test_attr("1 ", int_ >> ' ' >> -r4, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 )"));
- ut.clear();
- rule<char const*, utree::list_type()> r5 = -r4;
- BOOST_TEST(test_attr("1", r5, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 )"));
- ut.clear();
- BOOST_TEST(test_attr("", r5, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( )"));
- ut.clear();
- BOOST_TEST(test_attr("1 1", r5 >> ' ' >> r5, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( ( 1 ) ( 1 ) )"));
- ut.clear();
- rule<char const*, utree::list_type()> r6 = int_;
- rule<char const*, utree()> r7 = -r6;
- BOOST_TEST(test_attr("1", r7, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( 1 )"));
- ut.clear();
- rule<char const*, utree::list_type()> r8 = r6 >> ' ' >> r6;
- BOOST_TEST(test_attr("1 1", r8, ut) &&
- ut.which() == utree_type::list_type && check(ut, "( ( 1 ) ( 1 ) )"));
- ut.clear();
- }
- return boost::report_errors();
- }
|