c++ - Boost spirit x3 解析为结构,如果它为空则跳过成员

标签 c++ boost-spirit boost-spirit-x3

我正在尝试找出解析以下文本的方法

function() {body ...}
function(args_) {body...}

我应该对两个变体使用相同的结构还是只用一个结构就可以完成

struct function
{
    std::string name_;
    std::vector<std::string> args_;
    statement_list body_;
};

我现在解析它的方式(如果没有参数,如何跳过参数):

auto const identifier_def = raw[lexeme[(alpha | '_') >> *(alnum | '_')]];
auto const function_def  =
        lexeme["function" >> !(alnum | '_')] >> identifier
     >> '(' >> ((identifier % ',') )>> ')'
     >> '{' >> statement  >> '}'
        ;

我可以解析带参数的变体,但不能解析没有参数的变体!

我正在尝试使用 OR 运算符之类的东西,但没有成功。

谢谢!

最佳答案

作为一个快速提示,通常这是可行的:

 >> '(' >> -(identifier % ',') >> ')'

根据具体的类型(尤其是 identifier 的声明),你可能会有这样的调整:

 >> '(' >> (-identifier % ',') >> ')'

强制执行的想法:

 x3::rule<struct arglist_, std::vector<std::string> > { "arglist" }
         = '(' >> (
                       identifier % ','
                     | x3::attr(std::vector<std::string> ())
                   )
         >> ')';

关于c++ - Boost spirit x3 解析为结构,如果它为空则跳过成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52750720/

相关文章:

c++ - 将特定字符串数据解析为变量

c++ - 使用 Spirit x3,如何控制在每个不同的输入上调用哪个解析器?

c++ - 使用 Boost Spirit 解析语法

c++ - boost spirit 气整数,并在文字上使用默认值

c++ - 语义操作(使用 _val 和 _attr)如何影响 %= 和 x3::rule 的 force_attribute=true 的规则定义?

c++ - 基于 int 的短掩码在 C++ 库 Bullet 中如何工作?

c++ - 反转for循环导致系统错误

c++ - Qt - 有没有办法获取类中所有自定义插槽和信号的列表

c++ - 替代属性综合和 AST 设计

c++ - 使用 boost::spirit::x3 解析为 vector<boost::string_view>