Spirit Qi用简单的C风格结构化输入解析问题

Spirit Qi parsing issue with simple C-style structured input

本文关键字:输入 结构化 问题 风格 Qi 简单 Spirit      更新时间:2023-10-16

我正在尝试为一些游戏数据设置一个基本的解析器,该解析器使用熟悉且非常简单的"C风格"格式。基本上,命名支撑的"structs",然后将参数和嵌套的"struct"放在里面。它会解析这样的东西:

Name0
{
Name1
{
Param0 *= 2
Param2 = "lol"
}
Param0 = 1
Param1 = "test"
Name2 { }
}
Name3 {
Param0 = "test"
}

然而,它甚至在"test{}"的简单输入测试中都失败了,更不用说像我上面的例子那样高级了。结构被设置为使用融合,这似乎足够直接,我怀疑这是问题所在。我目前没有使用几个规则,而且我的大多数规则都没有经过测试,因为它在root中尝试第一个category规则时失败了。这是我输入"测试{}"时出现的错误:

Error! Expecting <sequence>"{"<node> here: ""

这是Parser类:

template<typename Iterator>
struct Parser : qi::grammar<Iterator, std::vector<Category>(), ascii::space_type>
{
qi::rule<Iterator, std::vector<Category>(), ascii::space_type> root;
qi::rule<Iterator, Category(), ascii::space_type> category;
qi::rule<Iterator, Param(), ascii::space_type> param;
qi::rule<Iterator, Node(), ascii::space_type> node;
qi::rule<Iterator, Value(), ascii::space_type> value;
qi::rule<Iterator, char()> escape;
qi::rule<Iterator, std::string()> quotedstring;
qi::rule<Iterator, std::string()> normalstring;
qi::rule<Iterator> comment;
qi::rule<Iterator> commentblock;
Parser() : Parser::base_type(root, "root")
{
using namespace qi;
using ascii::char_;
using phoenix::construct;
using phoenix::val;
escape %= '' > char_("\"");
quotedstring %= '"' >> *((char_ - '"') | escape) > '"';
normalstring %= *(char_ - qi::eol);
comment = "//" >> *(char_ - qi::eol);
commentblock = "/*" >> *(char_ - "*/") > "*/";
node %= category | param; //comment? comment block? holding off for now
value %= normalstring | float_;
param %=
lexeme[+(char_ - operators)]
> operators
> value
> qi::eol;
category %=
lexeme[+(char_ - '{')] //won't this grab all whitespace around the tag too?
> '{'
>> *node
> '}';
root %= *category;
root.name("root");
category.name("category");
param.name("param");
node.name("node");
value.name("value");
escape.name("escape");
quotedstring.name("quotedstring");
normalstring.name("normalstring");
comment.name("comment");
commentblock.name("commentblock");
debug(root);
debug(category);
debug(param);
debug(node);
debug(value);
debug(escape);
debug(quotedstring);
debug(normalstring);
debug(comment);
debug(commentblock);
on_error<fail>
(
root,
std::cout
<< val("Error! Expecting ")
<< _4
<< val(" here: "")
<< construct<std::string>(_3, _2)
<< val(""")
<< std::endl
);
}
};

与此无关的是,在on_successon_error调用中是否可以使用C++11 lambda?我研究了on_error函数,它的参数似乎被模板化为规则类型,这意味着必须为每个规则类型(基本上是每个规则)定义lambda。这是正确的吗?这太糟糕了,那些phoenix Lambda太不透明了,我甚至不知道如何提取行号并将其放入结构中。

编辑:

这是operators表格:

struct Operators : qi::symbols<char, Operator>
{
Operators()
{
add
("=", Operator::equal)
("+=", Operator::plusequal)
("-=", Operator::minusequal)
("*=", Operator::timesequal)
("/=", Operator::divideequal)
;
}
} operators;

operators未给出。

我猜你的node规则吃掉了关闭的},所以catagory规则不能成功。