我正在尝试编写一个boost::uuids::uuid
解析器,boost::spirit::qi
以便以一种很好的方式与其他qi
解析器一起使用它并拥有一个很好的统一解析器 api。
我的第一个想法是编写一个qi::grammar
将使用的自定义,boost::conversion::try_lexical_convert<boost::uuids::uuid &, const std::string &>
但是这将存在将开始迭代器正确设置为使用位置的问题,因为boost::conversion::try_lexical_convert<boost::uuids::uuid &, const std::string &>
不仅会匹配 16 个字符长的输入,而且还会匹配大括号或不带破折号。
我的第二种方法是使用一个boost::spirit::qi::rule
(或者如果你愿意的话,可以使用一个语法 CRTP boost::spirit::qi::grammar::base_type
),但是我得到了编译错误,可能来自BOOST_FUSION_ADAPT_STRUCT
表达式:
#include <iostream>
#include <string>
#include <cstdint>
#include <boost/uuid/uuid.hpp>
#include <boost/spirit/include/qi.hpp>
BOOST_FUSION_ADAPT_STRUCT(
boost::uuids::uuid,
(uint8_t, data[0])
(uint8_t, data[1])
(uint8_t, data[2])
(uint8_t, data[3])
(uint8_t, data[4])
(uint8_t, data[5])
(uint8_t, data[6])
(uint8_t, data[7])
(uint8_t, data[8])
(uint8_t, data[9])
(uint8_t, data[10])
(uint8_t, data[11])
(uint8_t, data[12])
(uint8_t, data[13])
(uint8_t, data[14])
(uint8_t, data[15])
)
template<typename Iterator>
boost::spirit::qi::rule<Iterator, boost::uuids::uuid>
uuid_internal_{
boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
//time-low
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> -boost::spirit::qi::lit("-")
//time-mid
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> -boost::spirit::qi::lit("-")
//time-high-and-version
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> -boost::spirit::qi::lit("-")
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>() //clock-seq-and-reserved
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>() //clock-seq-low
>> -boost::spirit::qi::lit("-")
//node
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
};
template<typename Iterator>
struct uuid_
: ::boost::spirit::qi::grammar<Iterator, boost::uuids::uuid()>{
uuid_() : uuid_::base_type(start) {
start %= (boost::spirit::qi::lit("{") >> uuid_internal_ >> boost::spirit::qi::lit("}")) |
uuid_internal_ ;
}
boost::spirit::qi::rule<Iterator, boost::uuids::uuid()> start;
boost::spirit::qi::rule<Iterator, boost::uuids::uuid()>
uuid_internal_{
boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
//time-low
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> -boost::spirit::qi::lit("-")
//time-mid
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> -boost::spirit::qi::lit("-")
//time-high-and-version
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> -boost::spirit::qi::lit("-")
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>() //clock-seq-and-reserved
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>() //clock-seq-low
>> -boost::spirit::qi::lit("-")
//node
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
>> boost::spirit::qi::uint_parser<uint8_t, 16, 1, 1>()
};
};
int main() {
std::string input;
std::cin >> input;
uuid_<std::string::const_iterator> uuid_{};
boost::uuids::uuid uuid{};
auto begin = input.begin(), end = input.end();
const bool success = boost::spirit::qi::parse(begin, end, uuid_, uuid);
if (!success || begin != end)
throw std::runtime_error("Parsing failed");
return 0;
}
/opt/local/include/boost/spirit/home/support/container.hpp:292:15:错误:在'boost::uuids::uuid'中没有名为'insert'的成员c.insert(c.end() , 值);
似乎是由 生成的问题boost::spirit::qi::detail::pass_through_container
,但是我的方法与BOOST_FUSION_ADAPT_ADT
结合*(obj.begin()+n)
也失败并出现多个错误。