【发布时间】:2017-05-24 10:45:47
【问题描述】:
我刚刚在 Qi 中实现了一个基本解析器来验证指定的 TCP 端口范围,例如80-444。
template<class It>
struct port_range_grammar : qi::grammar<It, port_range_type()>
{
port_range_grammar()
: port_range_grammar::base_type(start, "port_range")
{
using qi::lit;
start = port > lit("-") > port;
}
private:
qi::rule<It, port_range_type()> start;
qi::uint_parser<uint16_t, 10, 2, 5> port;
};
为了使错误更具描述性,我在开始规则中附加了一个错误处理程序(在一些高级语法中,它嵌入了这个),例如:
// this how the code is attached to the start rule in the top level grammar:
start = (tcp_endpoint | ipc_endpoint | inproc_endpoint)[_val=_1] > eoi;
on_error<fail>
( start
, pnx::bind
( [](auto const& what, auto begin, auto end)
{
ERROR_AC << "Expecting "
<< what
<< " here: '"
<< std::string(begin, end)
<< "'"
;
}
, _4
, _3
, _2
)
)
;
除了一个小例外,一切都很好,当我将一些无效的 16 位无符号数字作为端口传递时,我看到一个错误,但它的描述性不够:
Expecting <unsigned-integer> here: '74888'
现在库的用户无法理解 74888 是无效的 16 位 uint。 unsiged-integer 是附加到qi::uint_parser 的标签。有什么办法可以改变这个标签吗?
【问题讨论】:
标签: c++ boost boost-spirit boost-spirit-qi