Lexy:一个C++17的解析器组合库
Lexy: A parser combinator library for C++17

原始链接: https://github.com/foonathan/lexy

```cpp namespace dsl = lexy::dsl; // 将IPv4地址解析为`std::uint32_t`。 struct ipv4_address { // 正在匹配的内容。 static constexpr auto rule = []{ // 匹配一个(十进制)数字序列并将其转换为`std::uint8_t`。 auto octet = dsl::integer; // 匹配四个以句点分隔的数字。 return dsl::times<4>(octet, dsl::sep(dsl::period)) + dsl::eof; }(); // 如何存储匹配的输出。 static constexpr auto value = lexy::callback([](std::uint8_t a, std::uint8_t b, std::uint8_t c, std::uint8_t d) { return (a << 24) | (b << 16) | (c << 8) | d; }); }; ```

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Lexy: 一个C++17解析器组合库 (github.com/foonathan) 8 分,来自 klaussilveira 1小时前 | 隐藏 | 过去的 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文
namespace dsl = lexy::dsl;

// Parse an IPv4 address into a `std::uint32_t`.
struct ipv4_address
{
    // What is being matched.
    static constexpr auto rule = []{
        // Match a sequence of (decimal) digits and convert it into a std::uint8_t.
        auto octet = dsl::integer<std::uint8_t>;

        // Match four of them separated by periods.
        return dsl::times<4>(octet, dsl::sep(dsl::period)) + dsl::eof;
    }();

    // How the matched output is being stored.
    static constexpr auto value
        = lexy::callback<std::uint32_t>([](std::uint8_t a, std::uint8_t b, std::uint8_t c, std::uint8_t d) {
            return (a << 24) | (b << 16) | (c << 8) | d;
        });
};
联系我们 contact @ memedata.com