我想解析键值对,将字符串映射到字符串。因为我想支持{ ... }
右手边的块,所以我想出了一个简单的语法开始
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace grammar::map
{
using namespace boost::spirit::x3;
auto expression = rule<class expression, std::pair<std::string, std::string>>{"expression"};
auto lhs = *(~char_('='));
auto rest = *(~char_(';'));
auto block = '{' >> *expression >> '}';
auto expression_def = lhs >> '=' >> (block | rest) >> ';';
BOOST_SPIRIT_DEFINE(expression)
}
但它无法组合编译,除非我将表达式的属性更改为std::string
。
//! Transform a string into a key-value map of strings.
template <class M, class R>
requires InputRange<R>() && _ContainerLike<M>
&& Same<value_type_t<M>, std::pair<const std::string, std::string>>()
// && Assignable<std::pair<std::string, std::string>, value_type_t<M>>()
M parse(R&& range)
{
auto begin = rng::begin(range);
auto end = rng::end(range);
auto map = M{};
auto ret = x3::phrase_parse(begin, end, *grammar::map::expression, x3::space, map);
if (!ret)
throw std::runtime_error{"parse error"};
return map;
}
我得到错误
boost/spirit/home/x3/support/traits/move_to.hpp:62:18: error: cannot convert ‘std::remove_reference<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >&>::type {aka std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >}’ to ‘char’ in assignment
dest = std::move(src);
位于
boost/spirit/home/x3/support/traits/move_to.hpp: In instantiation of ‘void boost::spirit::x3::traits::detail::move_to_plain(Source&&, Dest&, mpl_::false_) [with Source = std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >; Dest = char; mpl_::false_ = mpl_::bool_<false>]’:
如果尝试以下表达式,也会发生同样的事情
auto pair = std::pair<std::string, std::string>{};
auto ret = x3::phrase_parse(begin, end, grammar::map::expression, x3::space, map);
几天后我就来了,但不知道如何正确地做到这一点。我感谢任何帮助... :^)
使用 boost-1.60{-62} 和 gcc 6.1.1、6.2 和较新的主干版本对其进行了测试。