我会使用适当的规则进行简化 - 因此您无需在解析表达式中拼出 as_string。
在这种特殊情况下它的工作方式存在有问题(应将其作为错误报告给库维护者)。但是我可以通过在raw 指令中添加一个eps 解析器来解决它:
r %= '<' >> raw[int64_[phx::ref(value) = _1] >> eps] >> '>';
(注意也不需要用char_解析括号)。
Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iomanip>
using namespace std::string_literals;
int main()
{
namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;
qi::rule<std::string::const_iterator, std::string()> r;
int64_t value;
{
using namespace qi;
static const int_parser<int64_t> int64_{};
r %= '<' >> raw[int64_[phx::ref(value) = _1] >> eps] >> '>';
}
using Lim = std::numeric_limits<decltype(value)>;
for (std::string const test_str : {
"<+123>"s,
"<0123>"s,
"<0123>"s,
"<123>"s,
"<" + std::to_string(Lim::max()) + ">",
"<" + std::to_string(Lim::min()) + ">",
})
{
std::string value_str;
auto success
= qi::parse(test_str.begin(), test_str.end(), r, value_str);
std::cout << "success: " << std::boolalpha << success << "\n";
std::cout << "value: " << value << "\n";
std::cout << "matched substring: " << std::quoted(value_str) << "\n";
}
}
打印
success: true
value: 123
matched substring: "+123"
success: true
value: 123
matched substring: "0123"
success: true
value: 123
matched substring: "0123"
success: true
value: 123
matched substring: "123"
success: true
value: 9223372036854775807
matched substring: "9223372036854775807"
success: true
value: -9223372036854775808
matched substring: "-9223372036854775808"
奖金
也封装“值”参数,所以你不要使用全局变量:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iomanip>
using namespace std::string_literals;
namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;
using It = std::string::const_iterator;
using T = std::int64_t;
struct Parser : qi::grammar<It, std::string(T&)> {
Parser() : Parser::base_type(r) {
r %= '<' >> qi::raw[ int64_[qi::_r1 = qi::_1] >> qi::eps ] >> '>';
}
private:
qi::int_parser<T> int64_;
qi::rule<It, std::string(T&)> r;
};
int main()
{
Parser const p;
using Lim = std::numeric_limits<T>;
for (std::string const test_str : {
"<+123>"s,
"<0123>"s,
"<0123>"s,
"<123>"s,
"<" + std::to_string(Lim::max()) + ">",
"<" + std::to_string(Lim::min()) + ">",
})
{
std::string value_str;
int64_t value;
auto success
= qi::parse(test_str.begin(), test_str.end(), p(phx::ref(value)), value_str);
std::cout << "success: " << std::boolalpha << success << "\n";
std::cout << "value: " << value << "\n";
std::cout << "matched substring: " << std::quoted(value_str) << "\n";
}
}
印刷:
success: true
value: 123
matched substring: "+123"
success: true
value: 123
matched substring: "0123"
success: true
value: 123
matched substring: "0123"
success: true
value: 123
matched substring: "123"
success: true
value: 9223372036854775807
matched substring: "9223372036854775807"
success: true
value: -9223372036854775808
matched substring: "-9223372036854775808"