提升灵气:改变特uint_parser的标签
Boost Spirit Qi: changing tag of specialized uint_parser
我刚刚在 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
的标签。有没有办法更改此标签?
我只是将一个名称附加到非终端规则:
port = uint_parser<uint16_t, 10, 2, 5>();
port.name("valid port number 10-65535");
在科里鲁现场观看
#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 failedn";
}
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'
相关文章:
- GLSL 将 uint 转换为浮点颜色
- 需要帮助调试"attribute parser"!C++ 来自黑客排名的问题
- 定义 uint= "unsigned int" 没有像我在 Visual Studio 中使用 nvcc 时预期的那样应用
- Boost.Spirit Alternative Parser parallelization
- 着色器将uint8投射到float,并将其重新解释回uint
- 如何将boost::asio::ip::address_v6 ip转换为2个uint64_t数字,并从2个uint 64
- 将 CRC uint 转换为 QByteArray QT - C++
- OSX 上的 GCC 5、6 和 7 不支持 uint
- Multiplication: uint vs float
- 在 jsoncpp 中区分 int 和 uint 类型
- 将UINT转换为WS_STRING
- C 快速-CPP-CSV-Parser进入数组
- Spirit Qi First Parser
- 无法获得 boost::spirit parser&lexer 适用于 std::string 或 int 或 double 以外的令牌类型
- Parser JSON ON QT
- 重新定义 uint 类型 C++
- c 中的uint()函数在哪里
- 在spirit parser Action中,在上下文参数上编译boost :: bind(成员函数)上的错误
- Parser生成器的选择
- 使用boost.spirit.parser时无法破译编译错误