我正在尝试使用Boost.Spirit X3编写(部分)CSS解析器。
我有(非常)基本设置工作:
const auto declaration_block_def = '{' >> +declaration >> '}';
const auto declaration_def = property >> ':' >> value >> ';';
const auto property_def = +(char_ - ":");
const auto value_def = +(char_ - ";");
这里value
只是一个简单的字符串解析器,而property
是一个列出所有属性的枚举的所有CSS属性名称的符号表。但现在我想知道我是否能够以某种方式以强类型方式编码所有可能的键值对?具体来说,我会使用symbols<enum_type>
为每个具有固定数量可能性的属性匹配符号表条目,并为颜色等更复杂的属性使用一些自定义规则。
问题是declaration
规则必须具有某个属性,并且在CSS中,声明块可以包含任何数量的元素,它们都具有自己的“属性”类型。最后,我想得到一个结构,我将以下面的形式传递给BOOST_FUSION_ADAPT_STRUCT:
enum class align_content : std::uint8_t;
enum class align_items : std::uint8_t;
enum class align_self : std::uint8_t;
struct declaration_block
{
css::align_content align_content{};
css::align_items align_items{};
css::align_self align_self{};
};
然后,这将正确默认初始化任何未指定的成员。
我看到X3出现了一些我不知道如何解决的问题:
我发现了什么似乎是Boost.Spirit.Qi 2 implementation,但由于X3是如此不同,他们的最终结果似乎不清楚,我似乎无法找到任何帮助...
看起来您希望从struct定义生成解析器代码。但是,您可以使用代码生成器。
以下是我知道你可以与齐合理得到的结果:
#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灵感,我衷心建议:
x3::as_parser
x3::base_parser
/ x3::parser<CRTP>
(例如Spirit-Qi: How can I write a nonterminal parser?,对我来说,一个问题是我们应该能够使用结构化绑定,所以我们不再需要凤凰了。