我试图用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
(或者,如果可以的话,使用源自boost::spirit::qi::grammar::base_type
的语法CRTP),但是然后我得到了编译错误,可能是由于BOOST_FUSION_ADAPT_STRUCT
表达式:< / p>
#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(),val);
似乎是由boost::spirit::qi::detail::pass_through_container
生成的发行,但是我将BOOST_FUSION_ADAPT_ADT
与*(obj.begin()+n)
结合使用的方法也因多个错误而失败。
答案 0 :(得分:1)
您可以使用内置的qi::stream
指令获得90%的访问方式:
uuid_ = qi::stream;
start = '{' >> uuid_ >> '}' | uuid_;
#include <boost/spirit/include/qi.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
template <typename Iterator> struct uuid_type : ::qi::grammar<Iterator, boost::uuids::uuid()> {
uuid_type() : uuid_type::base_type(start) {
start = '{' >> uuid_ >> '}' | uuid_;
uuid_ = qi::stream;
}
private:
qi::rule<Iterator, boost::uuids::uuid()> start, uuid_;
};
int main() {
uuid_type<std::string::const_iterator> uuid_{};
for (std::string const input : {
"2bc69ead-4aba-4a39-92c0-9565f4d464b4",
"2BC69EAD-4ABA-4A39-92C0-9565F4D464B4",
"{2bc69ead-4aba-4a39-92c0-9565f4d464b4}",
"{2BC69EAD-4ABA-4A39-92C0-9565F4D464B4}",
//"{2bc69ead--4aba--4a39----92c0--9565f4d464b4}",
//"{2BC69EAD--4ABA--4A39----92C0--9565F4D464B4}",
})
{
boost::uuids::uuid uuid{};
std::cout << "==== Input " << std::quoted(input) << "\n";
if (qi::parse(input.begin(), input.end(), uuid_ >> qi::eoi, uuid))
std::cout << "Parsed " << uuid << "\n";
else
std::cout << "Parsing failed\n";
}
}
打印
==== Input "2bc69ead-4aba-4a39-92c0-9565f4d464b4"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "2BC69EAD-4ABA-4A39-92C0-9565F4D464B4"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "{2bc69ead-4aba-4a39-92c0-9565f4d464b4}"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "{2BC69EAD-4ABA-4A39-92C0-9565F4D464B4}"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
根据modified Pareto principle,其余10%是困难的部分。
我什至不确定您是否要这样做,但是+qi::lit("-")
表示注释的测试用例也应该被接受(?!):
//"{2bc69ead--4aba--4a39----92c0--9565f4d464b4}",
//"{2BC69EAD--4ABA--4A39----92C0--9565F4D464B4}",
如果这确实是您想要的,我确实建议您使用词法转换实现进行两阶段解析操作:
好吧,因为现在已经过了一个多小时,这意味着它更像是“功能的10%将花费900%的工作量”-我希望您真的想要它:)
#include <boost/spirit/include/qi.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iomanip>
using Uuid = boost::uuids::uuid;
namespace boost::spirit::traits {
template <> struct is_container<Uuid> : mpl::false_ {};
template <> struct assign_to_attribute_from_value<Uuid, std::string> {
static void call(std::string const& s, Uuid& v) { v = lexical_cast<Uuid>(s); }
};
}
namespace qi = boost::spirit::qi;
template <typename Iterator> struct uuid_type : qi::grammar<Iterator, Uuid()> {
uuid_type() : uuid_type::base_type(start) {
using namespace qi;
auto sep_ = copy(+lit('-') >> qi::attr('-'));
auto hex2_ = copy(xdigit >> xdigit >> xdigit >> xdigit);
auto hex4_ = copy(hex2_ >> hex2_);
auto hex6_ = copy(hex4_ >> hex2_);
auto fmt_ = copy(
hex4_ >> sep_ >> hex2_ >> sep_ >> hex2_ >> sep_ >> hex2_ >> sep_ >> hex6_
);
start = as_string['{' >> fmt_ >> '}' | fmt_];
}
private:
qi::rule<Iterator, Uuid()> start;
};
int main() {
uuid_type<std::string::const_iterator> uuid_{};
for (std::string const input : {
"2bc69ead-4aba-4a39-92c0-9565f4d464b4",
"2BC69EAD-4ABA-4A39-92C0-9565F4D464B4",
"{2bc69ead-4aba-4a39-92c0-9565f4d464b4}",
"{2BC69EAD-4ABA-4A39-92C0-9565F4D464B4}",
"{2bc69ead--4aba--4a39----92c0--9565f4d464b4}",
"{2BC69EAD--4ABA--4A39----92C0--9565F4D464B4}",
})
{
Uuid uuid{};
std::cout << "==== Input " << std::quoted(input) << "\n";
auto f = input.begin(), l = input.end();
if (qi::parse(f, l, uuid_ >> qi::eoi, uuid))
std::cout << "Parsed " << uuid << "\n";
else
std::cout << "Parsing failed\n";
}
}
打印
==== Input "2bc69ead-4aba-4a39-92c0-9565f4d464b4"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "2BC69EAD-4ABA-4A39-92C0-9565F4D464B4"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "{2bc69ead-4aba-4a39-92c0-9565f4d464b4}"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "{2BC69EAD-4ABA-4A39-92C0-9565F4D464B4}"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "{2bc69ead--4aba--4a39----92c0--9565f4d464b4}"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4
==== Input "{2BC69EAD--4ABA--4A39----92C0--9565F4D464B4}"
Parsed 2bc69ead-4aba-4a39-92c0-9565f4d464b4