看起来您希望从结构定义生成解析器代码。你可以,但是你应该使用代码生成器。
以下是我知道你可以与 Qi 相当接近的方法:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_auto.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <iostream>
#include <iomanip>
#include <type_traits>
namespace css {
enum class align_content : std::uint8_t;
enum class align_items : std::uint8_t;
enum class align_self : std::uint8_t;
}
namespace qi = boost::spirit::qi;
template <typename T> static constexpr char const* name_of = nullptr;
template <> constexpr char const* name_of<css::align_content> = "content";
template <> constexpr char const* name_of<css::align_items> = "items";
template <> constexpr char const* name_of<css::align_self> = "self";
namespace {
template <typename T> struct align_parser {
static auto call() {
return qi::copy(qi::lexeme[name_of<T>] >> ":" >> qi::int_ >> ';');
};
using type = decltype(call());
};
}
namespace css {
// grrr: https://stackoverflow.com/a/36568565/85371
template<class T, bool = std::is_enum<T>::value> struct safe_underlying_type : std::underlying_type<T> {};
template<class T> struct safe_underlying_type<T, false /* is_enum */> {};
template <typename T, typename Underlying = typename safe_underlying_type<T>::type > std::ostream& operator<<(std::ostream& os, T v) {
using Int = std::common_type_t<int, Underlying>;
return os << name_of<T> << " -> " << static_cast<Int>(v);
}
}
namespace boost::spirit::traits {
template <> struct create_parser<css::align_content> : align_parser<css::align_content> {};
template <> struct create_parser<css::align_items> : align_parser<css::align_items> {};
template <> struct create_parser<css::align_self> : align_parser<css::align_self> {};
}
struct declaration_block {
css::align_content align_content{};
css::align_items align_items{};
css::align_self align_self{};
};
BOOST_FUSION_ADAPT_STRUCT(declaration_block, align_content, align_items, align_self)
int main() {
for (std::string const input : {
"",
"self:42;",
"content:7;items:99;self:42;",
"content : 7 ; items : 99; self : 42; ",
" self : 42; items : 99; content : 7 ; ",
})
{
std::cout << " ==== Test: " << std::quoted(input) << "\n";
auto f = input.begin(), l = input.end();
declaration_block db;
bool ok = qi::phrase_parse(f, l, (qi::auto_ ^ qi::auto_ ^ qi::auto_) | qi::eoi, qi::space, db);
if (ok) {
using boost::fusion::operator<<;
std::cout << "Parsed: " << db << "\n";
}
else
std::cout << "Failed\n";
if (f != l)
std::cout << "Remaining: " << std::quoted(std::string(f,l)) << "\n";
}
}
打印
==== Test: ""
Parsed: (content -> 0 items -> 0 self -> 0)
==== Test: "self:42;"
Parsed: (content -> 0 items -> 0 self -> 42)
==== Test: "content:7;items:99;self:42;"
Parsed: (content -> 7 items -> 99 self -> 42)
==== Test: "content : 7 ; items : 99; self : 42; "
Parsed: (content -> 7 items -> 99 self -> 42)
==== Test: " self : 42; items : 99; content : 7 ; "
Parsed: (content -> 7 items -> 99 self -> 42)
更多信息/想法
更详细地了解这种方法:
我给出的那个问题也有 X3 风格的答案:
如需更多 X3 灵感,我衷心推荐:
对我来说,最讨厌的一点是我们应该能够使用结构化绑定,这样我们就不再需要 Phoenix。