解析结构中的缔合的方式与升压精神与融合(Parsing a structure in an asso

2019-09-20 10:44发布

我试图解析键值字符串转换成一个结构。 一些键值可以不存在或者可以在不同的顺序,因此我想使用boost::fusion适应结构,然后解析到它与at_key<>指令。

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/fusion/sequence.hpp>

using namespace std;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;

using boost::fusion::at_key;

typedef string::const_iterator iter_type;

struct Couple {
    int a;
    int b;
    Couple() : a(0), b(0) {}
};

namespace keys {
    struct first;
    struct second;
}

BOOST_FUSION_ADAPT_ASSOC_STRUCT(
    Couple,
    (int, a, keys::first)
    (int, b, keys::second)
    )


struct G: qi::grammar< iter_type, Couple(), ascii::space_type >
{
    G() : G::base_type( start_rule ) {
        using qi::_val;
        using qi::_1;
        using qi::_2;

        start_rule =
                        ( "first" >> qi::int_
                                [ at_key<keys::first>(_val) = _1 ]
                        )
                    ^
                        ( "second" >> qi::int_
                                [ at_key<keys::second>(_val) = _1 ]
                        );
    }

    qi::rule< iter_type, Couple(), ascii::space_type > start_rule;
};

int main() {
    Couple couple;
    string example = "second 2 first 1";
    iter_type begin( example.begin() );
    iter_type end( example.end() );

    // test at_key -- compiles with no error
    at_key<keys::second>(couple) = 5;

    bool ok = qi::phrase_parse( begin, end, G(), ascii::space, couple );
    if ( ok )
        cout << couple.a << " " << couple.b << endl;
    else
        cout << "Parse failed" << endl;

    return 0;
}

问题是,该代码不编译(升压1.50.0,克++ 4.5.0,MinGW的),显然未能在at_key<>规则:

In file included from D:\projects\workspace\boost/boost/fusion/support/category_of.hpp:10:0,
                 from D:\projects\workspace\boost/boost/fusion/include/category_of.hpp:10,
                 from D:\projects\workspace\boost/boost/proto/fusion.hpp:20,
                 from D:\projects\workspace\boost/boost/proto/core.hpp:21,
                 from D:\projects\workspace\boost/boost/proto/proto.hpp:12,
                 from D:\projects\workspace\boost/boost/spirit/home/support/meta_compiler.hpp:19,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/include/qi.hpp:16,
                 from ..\src\spirit02_test.cpp:11:
D:\projects\workspace\boost/boost/fusion/support/detail/category_of.hpp: In instantiation of 'boost::fusion::detail::fusion_category_of<const boost::phoenix::actor<boost::spirit::attribute<0> > >':
D:\projects\workspace\boost/boost/fusion/support/category_of.hpp:44:58:   instantiated from 'boost::fusion::extension::category_of_impl<boost::fusion::non_fusion_tag>::apply<const boost::phoenix::actor<boost::spirit::attribute<0> > >'
D:\projects\workspace\boost/boost/fusion/support/category_of.hpp:66:9:   instantiated from 'boost::fusion::traits::category_of<const boost::phoenix::actor<boost::spirit::attribute<0> > >'
D:\projects\workspace\boost/boost/fusion/support/category_of.hpp:73:9:   instantiated from 'boost::fusion::traits::is_associative<const boost::phoenix::actor<boost::spirit::attribute<0> > >'
D:\projects\workspace\boost/boost/mpl/if.hpp:67:11:   instantiated from 'boost::mpl::if_<boost::fusion::traits::is_associative<const boost::phoenix::actor<boost::spirit::attribute<0> > >, boost::fusion::result_of::key_of<mpl_::arg<1> >, boost::fusion::result_of::value_of<mpl_::arg<1> > >'
D:\projects\workspace\boost/boost/fusion/algorithm/query/find.hpp:45:9:   instantiated from 'boost::fusion::result_of::find<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:38:17:   instantiated from 'boost::fusion::extension::at_key_impl<boost::fusion::non_fusion_tag>::apply<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:71:9:   instantiated from 'boost::fusion::result_of::at_key<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
..\src\spirit02_test.cpp:54:35:   instantiated from here
D:\projects\workspace\boost/boost/fusion/support/detail/category_of.hpp:15:38: error: no type named 'category' in 'const struct boost::phoenix::actor<boost::spirit::attribute<0> >'
In file included from D:\projects\workspace\boost/boost/proto/args.hpp:21:0,
                 from D:\projects\workspace\boost/boost/proto/core.hpp:14,
                 from D:\projects\workspace\boost/boost/proto/proto.hpp:12,
                 from D:\projects\workspace\boost/boost/spirit/home/support/meta_compiler.hpp:19,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/include/qi.hpp:16,
                 from ..\src\spirit02_test.cpp:11:
D:\projects\workspace\boost/boost/mpl/if.hpp: In instantiation of 'boost::mpl::if_<boost::fusion::traits::is_associative<const boost::phoenix::actor<boost::spirit::attribute<0> > >, boost::fusion::result_of::key_of<mpl_::arg<1> >, boost::fusion::result_of::value_of<mpl_::arg<1> > >':
D:\projects\workspace\boost/boost/fusion/algorithm/query/find.hpp:45:9:   instantiated from 'boost::fusion::result_of::find<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:38:17:   instantiated from 'boost::fusion::extension::at_key_impl<boost::fusion::non_fusion_tag>::apply<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:71:9:   instantiated from 'boost::fusion::result_of::at_key<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
..\src\spirit02_test.cpp:54:35:   instantiated from here
D:\projects\workspace\boost/boost/mpl/if.hpp:67:11: error: 'value' is not a member of 'boost::fusion::traits::is_associative<const boost::phoenix::actor<boost::spirit::attribute<0> > >'
D:\projects\workspace\boost/boost/mpl/if.hpp:70:41: error: 'value' is not a member of 'boost::fusion::traits::is_associative<const boost::phoenix::actor<boost::spirit::attribute<0> > >'
In file included from D:\projects\workspace\boost/boost/fusion/sequence/intrinsic.hpp:20:0,
                 from D:\projects\workspace\boost/boost/fusion/include/intrinsic.hpp:10,
                 from D:\projects\workspace\boost/boost/proto/fusion.hpp:22,
                 from D:\projects\workspace\boost/boost/proto/core.hpp:21,
                 from D:\projects\workspace\boost/boost/proto/proto.hpp:12,
                 from D:\projects\workspace\boost/boost/spirit/home/support/meta_compiler.hpp:19,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/include/qi.hpp:16,
                 from ..\src\spirit02_test.cpp:11:
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp: In instantiation of 'boost::fusion::extension::at_key_impl<boost::fusion::non_fusion_tag>::apply<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>':
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:71:9:   instantiated from 'boost::fusion::result_of::at_key<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
..\src\spirit02_test.cpp:54:35:   instantiated from here
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:38:17: error: no type named 'type' in 'struct boost::fusion::result_of::find<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::first>'
..\src\spirit02_test.cpp: In constructor 'G::G()':
..\src\spirit02_test.cpp:54:35: error: no matching function for call to 'at_key(const boost::spirit::_val_type&)'
In file included from D:\projects\workspace\boost/boost/fusion/sequence/intrinsic.hpp:20:0,
                 from D:\projects\workspace\boost/boost/fusion/include/intrinsic.hpp:10,
                 from D:\projects\workspace\boost/boost/proto/fusion.hpp:22,
                 from D:\projects\workspace\boost/boost/proto/core.hpp:21,
                 from D:\projects\workspace\boost/boost/proto/proto.hpp:12,
                 from D:\projects\workspace\boost/boost/spirit/home/support/meta_compiler.hpp:19,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi/action.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/home/qi.hpp:14,
                 from D:\projects\workspace\boost/boost/spirit/include/qi.hpp:16,
                 from ..\src\spirit02_test.cpp:11:
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp: At global scope:
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp: In instantiation of 'boost::fusion::extension::at_key_impl<boost::fusion::non_fusion_tag>::apply<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::second>':
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:71:9:   instantiated from 'boost::fusion::result_of::at_key<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::second>'
..\src\spirit02_test.cpp:58:36:   instantiated from here
D:\projects\workspace\boost/boost/fusion/sequence/intrinsic/at_key.hpp:38:17: error: no type named 'type' in 'struct boost::fusion::result_of::find<const boost::phoenix::actor<boost::spirit::attribute<0> >, keys::second>'
..\src\spirit02_test.cpp: In constructor 'G::G()':
..\src\spirit02_test.cpp:58:36: error: no matching function for call to 'at_key(const boost::spirit::_val_type&)'

如果我用一个简单的规则(无关联),一切都编译并运行正常,但这种方法是有些脆弱:

    // A non-associative solution
    //start_rule %= ( ("first" >> qi::int_) ^ ("second" >> qi::int_) );

为什么我不能用at_key在语义动作? 有没有更好的办法做到的“联想”解析成一个非关联的结构?

Answer 1:

你需要at_key的懒惰版本。 在此修补程序可悲的是从未进入灵(还):

/*=============================================================================
    Copyright (c) 2005-2008 Hartmut Kaiser
    Copyright (c) 2005-2007 Joel de Guzman
    Copyright (c) 2011      Michael Caisse

    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)
==============================================================================*/
#ifndef PHOENIX_SEQUENCE_AT_KEY_HPP
#define PHOENIX_SEQUENCE_AT_KEY_HPP

#include <boost/fusion/include/at_key.hpp>
#include <boost/spirit/home/phoenix/core/actor.hpp>
#include <boost/spirit/home/phoenix/core/compose.hpp>
#include <boost/type_traits/remove_reference.hpp>

namespace boost { namespace phoenix
{
    template <typename Key>
    struct at_key_eval
    {
        template <typename Env, typename Tuple>
        struct result
        {
            typedef typename Tuple::template result<Env>::type tuple;
            typedef typename
                fusion::result_of::at_key<
                    typename remove_reference<tuple>::type, Key
                >::type
            type;
        };

        template <typename RT, typename Env, typename Tuple>
        static RT
        eval(Env const& env, Tuple const& t)
        {
            return fusion::at_key<Key>(t.eval(env));
        }
    };

    template <typename Key, typename Tuple>
    inline actor<typename as_composite<at_key_eval<Key>, Tuple>::type>
    at_key(Tuple const& tup)
    {
        return compose<at_key_eval<Key> >(tup);
    }

}}

#endif

如果添加它,你可以使用boost::phoenix::at_key

using boost::phoenix::at_key;

事情会编译和正常工作。 链接的补丁还修改boost/spirit/home/phoenix/fusion.hpp包括这个新的报头,当然。



文章来源: Parsing a structure in an associative manner with Boost Spirit and Fusion