// Copyright (c) 2001-2011 Hartmut Kaiser // Copyright (c) 2011 Dean Michael Berries // // 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 #include #include #include #include struct foo_parts { boost::optional first; std::string second; }; namespace boost { namespace spirit { namespace traits { template <> struct transform_attribute &> , spirit::qi::domain> { typedef fusion::tuple&> type; static type pre(foo_parts & parts) { return fusion::tie(parts.second, parts.first); } static void post(foo_parts &, type const &) {} static void fail(foo_parts &) {} }; }}} namespace qi = boost::spirit::qi; template struct foo_grammar : qi::grammar { foo_grammar() : foo_grammar::base_type(start, "foo") { foo_part = +qi::alpha >> -(+qi::digit) | qi::attr(std::string()) >> qi::attr(boost::optional()) ; start = foo_part.alias(); } typedef boost::fusion::tuple&> tuple_type; qi::rule foo_part; qi::rule start; }; int main() { foo_parts instance; foo_grammar grammar; std::string input = "abc123"; BOOST_TEST(qi::parse(input.begin(), input.end(), grammar, instance) && instance.first && instance.first.get() == "123" && instance.second == "abc"); return boost::report_errors(); }