boost-spirit-lex是为了给令牌一个字符串名称而构建的?

时间:2022-10-29 21:01:34

edit : I have ripped out the lexer as it does not cleanly integrate with Qi and just obfuscates grammars (see here).

编辑:我已经删除了词法分析器,因为它没有与Qi完全集成,只是混淆了语法(见这里)。


I need to give token types a name that I can use in on_error handler in qi. At the moment the _4 (qi::on_error<..>(...,std::cout << _4 << std::endl)) placeholder is giving me the regular expression string of the token. My lexer looks as follows :

我需要给令牌类型一个名字,我可以在qi中使用on_error处理程序。目前_4(qi :: on_error <..>(...,std :: cout << _4 << std :: endl))占位符正在为我提供令牌的正则表达式字符串。我的词霸看起来如下:

template <typename Lexer>
struct tokens : lex::lexer<Lexer>
{
    tokens()
        : left_curly("\"{\""),
        right_curly("\"}\""),
        left_paren("\"(\""),
        right_paren("\")\""),
        colon(":"),
        scolon(";"),
        namespace_("(?i:namespace)"),
        event("(?i:event)"),
        optional("(?i:optional)"),
        required("(?i:required)"),
        repeated("(?i:repeated)"),
        t_int_4("(?i:int4)"),
        t_int_8("(?i:int8)"),
        t_string("(?i:string)"),
        ordinal("\\d+"),
        identifier("\\w+")

    {
        using boost::spirit::lex::_val;

        this->self
            = 
            left_curly    
            | right_curly 
            | left_paren
            | right_paren
            | colon         
            | scolon
            | namespace_      
            | event             
            | optional           
            | required          
            | repeated
            | t_int_4
            | t_int_8
            | t_string
            | ordinal             
            | identifier         
            | lex::token_def<>("[ \\t\\n]+")   [lex::_pass = lex::pass_flags::pass_ignore];
    }


    lex::token_def<lex::omit> left_curly, right_curly, colon, scolon,repeated, left_paren, right_paren;
    lex::token_def<lex::omit> namespace_, event, optional, required,t_int_4, t_int_8, t_string;
    lex::token_def<boost::uint32_t> ordinal;
    lex::token_def<std::string> identifier;
};

1 个解决方案

#1


0  

edit : I have ripped out the lexer as it does not cleanly integrate with Qi and just obfuscates grammars (see here).

编辑:我已经删除了词法分析器,因为它没有与Qi完全集成,只是混淆了语法(见这里)。


Here is one brittle hack to detect what token was caught by the on_error mechanism. Wiring this up will not be pleasant :D

这是一个脆弱的黑客攻击,用于检测on_error机制捕获的令牌。布线不会令人愉快:D

template<typename State>
struct error_handler_impl
{
    typedef void result_type;

    State & state;
    error_handler_impl(State & state) : state(state) {}

    template <typename What, typename Iterator>
    void operator()(const std::string & msg,What what, Iterator it) const
    {
        if(boost::get<boost::spirit::utf8_string>(what.value) == state.tok.identifier.definition())
        {
            std::cout << "token type is identifier" << std::endl;
        }
    }
};

state.tok is the lexer object, via which the token definitions may be accessed.

state.tok是词法分析器对象,通过它可以访问令牌定义。

#1


0  

edit : I have ripped out the lexer as it does not cleanly integrate with Qi and just obfuscates grammars (see here).

编辑:我已经删除了词法分析器,因为它没有与Qi完全集成,只是混淆了语法(见这里)。


Here is one brittle hack to detect what token was caught by the on_error mechanism. Wiring this up will not be pleasant :D

这是一个脆弱的黑客攻击,用于检测on_error机制捕获的令牌。布线不会令人愉快:D

template<typename State>
struct error_handler_impl
{
    typedef void result_type;

    State & state;
    error_handler_impl(State & state) : state(state) {}

    template <typename What, typename Iterator>
    void operator()(const std::string & msg,What what, Iterator it) const
    {
        if(boost::get<boost::spirit::utf8_string>(what.value) == state.tok.identifier.definition())
        {
            std::cout << "token type is identifier" << std::endl;
        }
    }
};

state.tok is the lexer object, via which the token definitions may be accessed.

state.tok是词法分析器对象,通过它可以访问令牌定义。