Spirit Qi用简单的C风格结构化输入解析问题
Spirit Qi parsing issue with simple C-style structured input
我正在尝试为一些游戏数据设置一个基本的解析器,该解析器使用熟悉且非常简单的"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_success
和on_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
规则不能成功。
相关文章:
- 在基于范围的for循环中使用结构化绑定声明
- 使用结构化绑定'Reflection'
- 为什么结构化绑定不使用"auto&"返回对结构成员的引用,而是返回成员本身
- 为什么 boost::comb 对结构化绑定的支持缺少结构化绑定机制对 boost::tuples::cons 的适应?
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 在 C++14 中手动实现结构化绑定
- 为什么结构化绑定不支持可变数组?
- 为<vtkDataArray> VTK 非结构化网格声明 vtkSmartPointer 类型的变量时出现问题
- 在只读(即 const)访问器上执行结构化绑定的最佳实践是什么?
- 是否有像ADTF2这样的结构化属性?
- 当使用 std::CIN 输入结构时,它会出错
- 带有 std::minmax 和 rvalues 的结构化绑定
- 在无序映射的结构化绑定中推导类型
- 为什么基于范围的 for 循环中的结构化绑定只是一个副本而不是引用?
- 您自己的类型的结构化绑定,不是结构或元组(通过公共成员函数)
- 共享或私有 openmp 代码中的结构化类型变量
- 结构化绑定:遍历元组的双端面
- 结构化绑定是否适用于 std::vector?
- 结构化绑定,无需复制即可获取子向量的连续元素
- Spirit Qi用简单的C风格结构化输入解析问题