how to change the parser of a rule

帅比萌擦擦* 提交于 2019-12-13 03:19:36

问题


Is it possible to modify the parser of a rule at runtime? I am able to dynamically create parsers (classes that are derived from parser_base), but I dont know how I can assign the new parser to an existing rule.

Basically my problem is that I want to define a parser for a line that consists of elements like numbers (lets call them constants in the sense that they are invariant over all my parser input) and symbols (the variants that I want to capture with a dynamic parser approach). Since the symbols are based on the current environment at runtime I think I need a dynamic parser.

minimalistic issue, I want symbols and a line-end:

namespace parser
{
    x3::rule<class line, ast::line> line = "line";
    auto const line_def = variants_def >> lineend_def;
    BOOST_SPIRIT_DEFINE(line);
}

using line_type = boost::spirit::x3::rule<class client::parser::line, ast::line>;
line_type line(boost::spirit::x3::plus<boost::spirit::x3::symbols_parser<boost::spirit::char_encoding::standard, client::ast::command, boost::spirit::x3::tst<boost::spirit::char_encoding::standard::char_type, client::ast::command>>> symbols_parser)
{
    auto line_end = lineend();
    auto const line_def2 = symbols_parser >> line_end;
    return parser::line; // <-- how can I change the line_type to use line_def2?
}

This code might be far from minimal, but unfortunately Im not so familiar with C++ yet.


回答1:


You cannot.

Actually, I lied, you can have different parsers with the same id in dynamic libraries and change the rule's parser by switching dynamic libraries back and forth, but please, do not do this.



来源:https://stackoverflow.com/questions/56103411/how-to-change-the-parser-of-a-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!