编译错误源自这里:
test.cpp|21 col 58| required from here
/home/sehe/custom/spirit/include/boost/spirit/home/qi/nonterminal/grammar.hpp|77 col 13| error: static assertion failed: incompatible_start_rule
|| 77 | BOOST_SPIRIT_ASSERT_MSG(
|| | ^~~~~~~~~~~~~~~~~~~~~~~
如果您前往指定位置(另请参阅How do I grok boost spirit compiler errors):
// If you see the assertion below failing then the start rule
// passed to the constructor of the grammar is not compatible with
// the grammar (i.e. it uses different template parameters).
BOOST_SPIRIT_ASSERT_MSG(
(is_same<start_type, rule<Iterator_, T1_, T2_, T3_, T4_> >::value)
, incompatible_start_rule, (rule<Iterator_, T1_, T2_, T3_, T4_>));
那是你的问题。语法是<It, RawValue(), qi::space_type>,但起始规则是<iterator_type, RawValue(), skipper_type>,其中
static_assert(std::is_same_v<It, iterator_type>);
static_assert(std::is_same_v<qi::space_type, skipper_type>);
qi::space_type _y = skipper_type{};
显示第二个断言失败。第三行导致更详细的诊断:Live
test.cpp|33 col 29| error: could not convert ‘boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, project::RawValue(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0> >::skipper_type{}’ from ‘boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, project::RawValue(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0> >::skipper_type’ {aka ‘boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >’} to ‘boost::spirit::standard::space_type’ {aka ‘boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::te
|| 33 | qi::space_type _y = skipper_type{};
|| | ^~~~~~~~~~~~~~
|| | |
|| | boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, project::RawValue(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0> >::skipper_type {aka boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >}
您可以将整个事情分开,但只要说您认为skipper_type 等于第三个模板参数的假设是错误的就足够了。
只需拼出您期望的类型:Live
qi::rule<It, RawValue(), qi::space_type> expr;
附注
-
lexeme 禁用船长。考虑到简化整个事情:Live(另见Boost spirit skipper issues)
-
operator %= 没有语义动作就没有意义
-
expr 似乎用词不当
-
否定字符类比减法解析器更有效;你的意思是(char_ - blank) 比eol 更有意义吗?
-
现在不接受输入结尾而不是 eol。也许你应该允许它。我会这样写
start = *~qi::char_("\n\r") >> (eol | eoi);
简化列表
Live On Coliru
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
using It = std::string::const_iterator;
struct RawValue { std::string str; };
BOOST_FUSION_ADAPT_STRUCT(RawValue, str)
struct RawValueParser : qi::grammar<It, RawValue()> {
RawValueParser() : RawValueParser::base_type(start)
{
using namespace qi;
start = *~qi::char_("\n\r") >> (eol | eoi);
}
private:
qi::rule<It, RawValue()> start;
};
int main() {
RawValueParser expr;
for (std::string const s : {
"test1",
" test2\tbla \n",
}) {
RawValue result;
std::cout
<< std::boolalpha
//<< qi::phrase_parse(begin(s), end(s), expr, qi::space, result)
<< qi::parse(begin(s), end(s), expr, result) << " -> "
<< std::quoted(result.str) << "\n";
}
}
打印
true -> "test1"
true -> " test2 bla "