3

我想重用一个规则,并在它前面加上一个关键字。所以我的规则是这样的:

#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;
}

这不会编译,因为编译器想要AB(参考 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;
}

任何想法,如何在没有这种解决方法的情况下相处以及这里发生了什么?

4

2 回答 2

1

这又是一个适应单元素序列的问题。不幸的是,我不知道这种情况的解决方法。在这种情况下,您可以删除 B 的适应并专门化transform_attribute这里有一个简单的例子,它几乎完全符合您的要求。

#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;
}
于 2013-08-11T07:16:47.377 回答
1

一个更简单的解决方法是放弃自动属性传播,而是将显式赋值写为一个微不足道的语义动作:

qi::rule<iterator_type, B()> bad_rule = "keyword" > b_rule[ qi::_val=qi::_1 ];

我同意这仍然是一个痛苦的解决方法。Spirit V3 承诺在这里简化机制并使属性兼容性在边缘更加平滑。

于 2013-08-17T20:41:00.243 回答