gpt4 book ai didi

c++ - boost 灵气 : changing tag of specialized uint_parser

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:03 28 4
gpt4 key购买 nike

我刚刚在 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
)
)
;

除了一个小异常(exception),一切都很好,当我作为端口传递一些无效的 16 位无符号数字时,我看到一个错误,但它的描述性不够:

Expecting <unsigned-integer> here: '74888'

现在图书馆的用户无法理解 74888 是无效的 16 位 uint。 unsiged-integer 是附加到 qi::uint_parser 的标签。有什么办法可以改变这个标签吗?

最佳答案

我只是将名称附加到非终端规则:

        port = uint_parser<uint16_t, 10, 2, 5>();
port.name("valid port number 10-65535");

查看 Live On Coliru

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace pnx = boost::phoenix;

using port_range_type = std::pair<uint16_t, uint16_t>;

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 namespace qi;
port = uint_parser<uint16_t, 10, 2, 5>();
port.name("valid port number 10-65535");

start = port > lit("-") > port;
/*start = (tcp_endpoint | ipc_endpoint | inproc_endpoint)[_val=_1] > eoi;*/

on_error<fail>
( start
, pnx::bind
( [](auto const& what, auto begin, auto end)
{
std::cerr << "Expecting "
<< what
<< " here: '"
<< std::string(begin, end)
<< "'\n"
;
} , _4 , _3 , _2
)
)
;
}

private:
qi::rule<It, port_range_type()> start;
qi::rule<It, uint16_t()> port;
};

int main() {
using It = std::string::const_iterator;
std::string const input = "11-1q0";

It f = input.begin(), l = input.end();
port_range_type range;
bool ok = qi::parse(f, l, port_range_grammar<It>{}, range);

if (ok) {
std::cout << "Parsed port " << range.first << " to " << range.second << "\n";
} else {
std::cout << "Parse failed\n";
}

if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

打印

Expecting <valid port number 10-65535> here: '1q0'
Parse failed
Remaining unparsed: '11-1q0'

关于c++ - boost 灵气 : changing tag of specialized uint_parser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44156112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com