作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想重用一个规则,只需在它前面加上一个关键字。所以我的规则是这样的:
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
namespace qi = boost::spirit::qi;
struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))
struct B {
A data;
};
BOOST_FUSION_ADAPT_STRUCT(::B, (A, data))
int main() {
typedef std::string::const_iterator iterator_type;
qi::rule<iterator_type, A()> a_rule = qi::int_ > qi::lit(",") > *(qi::char_);
qi::rule<iterator_type, B()> b_rule = qi::lit("(") > a_rule > qi::lit(")");
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") > b_rule;
return 0;
}
这不会编译,因为编译器想要从 B
创建 A
(引用 bad_rule 的定义):
C:/tc/gcc_x64_4.8.1_win32_seh_rev1/mingw64/my/src/boost_1_54_0/boost/spirit/home/qi/detail/assign_to.hpp:152:18: error: no matching function for call to 'A::A(const B&)'
attr = static_cast<Attribute>(val);
^
然而,将 struct B
更改为不那么简单,使其工作:
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
namespace qi = boost::spirit::qi;
struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))
struct B {
A data;
int dummy;
};
BOOST_FUSION_ADAPT_STRUCT(::B, (A, data)(int, dummy))
int main() {
typedef std::string::const_iterator iterator_type;
qi::rule<iterator_type, A()> a_rule = qi::int_ > qi::lit(",") > *(qi::char_);
qi::rule<iterator_type, B()> b_rule = qi::lit("(") > a_rule > qi::lit(")") > qi::attr(0);
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") > b_rule;
return 0;
}
任何想法,如何在没有这种解决方法的情况下相处以及这里发生了什么?
最佳答案
这又是一个适应单元素序列的问题。不幸的是,我不知道这种情况的解决方法。在这种情况下,您可以删除 B 的改编并专门化 transform_attribute
.有一个简单的例子here这几乎完全符合您的要求。
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
namespace qi = boost::spirit::qi;
struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))
struct B {
A data;
};
namespace boost{ namespace spirit{ namespace traits
{
template <>
struct transform_attribute<B,A,qi::domain>
{
typedef A& type;
static type pre(B& val){ return val.data;}
static void post(B&, A const&){}
static void fail(B&){}
};
}}}
int main() {
typedef std::string::const_iterator iterator_type;
qi::rule<iterator_type, A()> a_rule = qi::int_ >> qi::lit(",") >> *(qi::char_-qi::lit(")"));
qi::rule<iterator_type, B()> b_rule = qi::lit("(") >> a_rule >> qi::lit(")");
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") >> b_rule;
std::string test="keyword(1,one)";
iterator_type iter=test.begin(), end=test.end();
B b;
bool result = qi::parse(iter,end,bad_rule,b);
if(result && iter==end)
{
std::cout << "Success:" << std::endl;
std::cout << "b.data.index=" << b.data.index << ", b.data.name=" << b.data.name << std::endl;
}
else
{
std::cout << "Failed. Unparsed: " << std::string(iter,end) << std::endl;
}
return 0;
}
关于c++ - 具有相同简单改编结构属性的 boost::spirit::qi 规则会产生编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167412/
我正在尝试将这个mysql CRUD示例改编为mssql。 http://teknosains.com/i/simple-crud-nodejs-mysql 这里是 git: https://gith
我有一个满足 C++ 随机数引擎要求的 PRNG,如所述 here . (它来自 PCG family )。也就是说,C++ 标准库分发类可以使用该引擎的一个实例来生成随机数: pcg_extras:
我是一名优秀的程序员,十分优秀!