传递参数以增强精神自定义解析器

Passing arguments to boost spirit custom parser

我正在尝试按照此处的说明创建自定义解析器:http://boost-spirit.com/home/articles/qi-example/creating-your-own-parser-component-for-spirit-qi/。在这篇文章中,解析器没有参数,即我不能写像 qi::parse(first, input.end(), +qi::alpha >> custom_parser::iter_pos("my argument")); 这样的东西。如何修改示例以传递参数?我试过这个:

using namespace boost::spirit;

namespace parsernm { BOOST_SPIRIT_TERMINAL(aparser); }

namespace boost { namespace spirit {
    template <typename A0>
    struct use_terminal<
        qi::domain
      , terminal_ex<
            parsernm::tag::aparser
          , fusion::vector1<A0>
          >
      > : mpl::true_
    {};
}}

namespace parsernm
{
    struct aparser_impl : boost::spirit::qi::primitive_parser<aparser_impl>
    {
        aparser_impl(int _param)
          : param(_param)
        {}

        template <typename Iterator, typename Context, typename Skipper, typename Attribute>
        bool parse(Iterator& first, Iterator const& last, Context&,
                   Skipper const& skipper, Attribute& attr) const
        {
            return true;
        }

        template <typename Context>
        boost::spirit::info what(Context&) const {
            return boost::spirit::info("aparser");
        }

        int param;
    };
}

namespace boost { namespace spirit { namespace qi {
    template <typename Modifiers>
    struct make_primitive<parsernm::tag::aparser, Modifiers>
    {
        typedef parsernm::aparser_impl result_type;

        template <typename Terminal>
        result_type operator()(Terminal const& term, unused_type) const
        {
            return result_type(fusion::at_c<0>(term.args));
        }
    };
}}}

int main(int argc, char* argv[])
{
    std::string input("");
    std::string::iterator first = input.begin();
    bool result = qi::parse(first, input.end(), parsernm::aparser(10));

    return 0;
}

结果是 boost\spirit\home\qi\detail\parse.hpp(32): error C2338: error_invalid_expression.

我明白了。 BOOST_SPIRIT_TERMINAL 必须更改为 BOOST_SPIRIT_TERMINAL_EXmake_primitive 应该如下所示:

namespace boost { namespace spirit { namespace qi {
    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<
            parsernm::tag::aparser
          , fusion::vector1<A0>
          >
      , Modifiers
      >
    {
        typedef parsernm::aparser_impl result_type;

        template <typename Terminal>
        result_type operator()(Terminal const& term, unused_type) const
        {
            return result_type(fusion::at_c<0>(term.args));
        }
    };
}}}