提升精神 x3 解析为结构,如果它为空,则跳过成员

Boost spirit x3 parse into struct with skipping member if it empty

本文关键字:如果 成员 结构 x3      更新时间:2023-10-16

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

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> ())
)
>> ')';
相关文章: