提升精神:使成员函数的解析器无效

Boost spirit: Invalidate parser from member function

这篇文章 (boost spirit semantic action parameters) 解释了如何使带有签名的普通函数的匹配无效

void f(int attribute, const boost::fusion::unused_type& it, bool& mFlag)

我想使来自语法成员函数的匹配无效:

#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/home/phoenix.hpp>

#include <iostream>
#include <string>

namespace qi  = boost::spirit::qi;
namespace phoenix = boost::phoenix;


class moduleAccessManager
{
public:
    bool getModule(const std::string name)
    {
        if(name == "cat" || name == "dog")
            return true;
        else
            return false;
    }
};

void globalIsModule(std::string moduleName, const boost::spirit::unused_type&, bool& mFlag)
{
        moduleAccessManager acm; /* Dirty workaround for this example */
        if(acm.getModule(moduleName))
            std::cout << "[isModule] Info: Found module with name >"  << moduleName << "<" << std::endl;
        else
        {
            std::cout << "[isModule] Error: No module with name >" << moduleName << "<" << std::endl;
            mFlag = false; // No valid module name
        }
}


template <typename Iterator, typename Skipper>
class moduleCommandParser : public qi::grammar<Iterator, Skipper>
{
private:
    moduleAccessManager* m_acm;

    qi::rule<Iterator, Skipper> start, module;

public:
    std::string m_moduleName;

    moduleCommandParser(moduleAccessManager* acm)
        : moduleCommandParser::base_type(start)
        , m_acm(acm)
        , m_moduleName("<empty>")
    {
        module  =   qi::as_string[qi::lexeme[+(~qi::char_(' '))]]
            [&globalIsModule] // This works fine
//          [phoenix::bind(&moduleCommandParser::isModule, this)] // Compile error
            ;
        start    =  module >> qi::as_string[+(~qi::char_('\n'))];
    };

    void isModule(std::string moduleName, const boost::spirit::unused_type&, bool& mFlag)
    {
        // Check if a module with moduleName exists
        if(m_acm->getModule(moduleName))
            std::cout << "[isModule] Info: Found module with name >"  << moduleName << "<" << std::endl;
        else
        {
            std::cout << "[isModule] Error: No module with name >" << moduleName << "<" << std::endl;
            mFlag = false; // No valid module name
        }
    };

};


int main()
{
    moduleAccessManager acm;
    moduleCommandParser<std::string::const_iterator, qi::space_type> commandGrammar(&acm);

    std::string str;
    std::string::const_iterator first;
    std::string::const_iterator last;

    str = "cat run";
    first = str.begin();
    last = str.end();
    qi::phrase_parse(first, last, commandGrammar, qi::space);

    str = "bird fly";
    first = str.begin();
    last = str.end();
    qi::phrase_parse(first, last, commandGrammar, qi::space);
}

Coliru 上的代码:http://coliru.stacked-crooked.com/a/4319b38a6d36c362

重要的部分是这两行:

            [&globalIsModule] // This works fine
//          [phoenix::bind(&moduleCommandParser::isModule, this)] // Compile error

使用全局函数工作正常,但这对我来说不是一个选项,因为我需要访问特定于解析器的 m_acm 对象。

如何将成员函数绑定到语义操作,同时能够使来自该成员函数的匹配无效(使用上面提到的 3 参数函数签名)?

有两种方法:

  • 您可以使用 Phoenix actors
  • 分配给 qi::_val
  • 您可以在 "raw" 语义动作函数中分配给第三个参数 (bool&)

这里有一个例子:

  • (使用_val

语义动作函数的剖析(带有第三个参数):

  • boost spirit semantic action parameters

在您的情况下,您有一个成员函数,其签名大致为 "raw semantic action function"。当然,您必须绑定 this 参数(因为它是一个非静态成员函数)。

请注意,在这种特殊情况下,phoenix::bind 不是正确使用的绑定,因为 Phoenix Actor 将被视为 "cooked"(非原始)语义动作,它们将被执行在精神背景下。

你可以

  1. 使用boost::bind(甚至std::bind)绑定到一个保留成员arity(!)的函数功能:

    [boost::bind(&moduleCommandParser::isModule, this, ::_1, ::_2, ::_3)]
    

    这个有效:Live On Coliru

  2. 而是使用 "cooked" 语义操作,直接分配给 _pass 上下文占位符:

    [qi::_pass = phoenix::bind(&moduleAccessManager::getModule, m_acm, qi::_1)]
    

    这也有效:Live On Coliru

后面的例子,供以后参考:

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

#include <iostream>
#include <string>

namespace qi      = boost::spirit::qi;
namespace phoenix = boost::phoenix;

class moduleAccessManager {
public:
    bool getModule(const std::string name) {
        return name == "cat" || name == "dog";
    }
};

void globalIsModule(std::string moduleName, const boost::spirit::unused_type&, bool& mFlag)
{
        moduleAccessManager acm; /* Dirty workaround for this example */
        if(acm.getModule(moduleName))
            std::cout << "[isModule] Info: Found module with name >"  << moduleName << "<" << std::endl;
        else
        {
            std::cout << "[isModule] Error: No module with name >" << moduleName << "<" << std::endl;
            mFlag = false; // No valid module name
        }
}

template <typename Iterator, typename Skipper>
class moduleCommandParser : public qi::grammar<Iterator, Skipper>
{
private:
    moduleAccessManager* m_acm;

    qi::rule<Iterator, Skipper> start, module;

public:
    std::string m_moduleName;

    moduleCommandParser(moduleAccessManager* acm)
        : moduleCommandParser::base_type(start)
        , m_acm(acm)
        , m_moduleName("<empty>")
    {
        using namespace phoenix::arg_names;
        module  =   qi::as_string[qi::lexeme[+(~qi::char_(' '))]]
                        [qi::_pass = phoenix::bind(&moduleAccessManager::getModule, m_acm, qi::_1)]
                    ;
        start   =  module >> qi::as_string[+(~qi::char_('\n'))];
    };

};


int main()
{
    moduleAccessManager acm;
    moduleCommandParser<std::string::const_iterator, qi::space_type> commandGrammar(&acm);

    std::string str;
    std::string::const_iterator first;
    std::string::const_iterator last;

    str = "cat run";
    first = str.begin();
    last = str.end();
    std::cout << str << std::boolalpha 
              << qi::phrase_parse(first, last, commandGrammar, qi::space)
              << "\n";

    str = "bird fly";
    first = str.begin();
    last = str.end();
    std::cout << str << std::boolalpha 
              << qi::phrase_parse(first, last, commandGrammar, qi::space)
              << "\n";
}