parsi

所属分类:C/C++工具库
开发工具:C++
文件大小:0KB
下载次数:0
上传日期:2023-08-26 13:10:50
上 传 者sh-1993
说明:  声明性解析器组合器库。,
(A declarative parser combinator library.,)

文件列表:
.clang-format (524, 2023-12-07)
.editorconfig (218, 2023-12-07)
CMakeLists.txt (2847, 2023-12-07)
CMakePresets.json (4762, 2023-12-07)
LICENSE (1062, 2023-12-07)
benchmark/ (0, 2023-12-07)
benchmark/CMakeLists.txt (435, 2023-12-07)
benchmark/core.cpp (8194, 2023-12-07)
docs/ (0, 2023-12-07)
docs/CMakeLists.txt (2300, 2023-12-07)
docs/Doxyfile.in (128201, 2023-12-07)
docs/conf.py (2871, 2023-12-07)
docs/index.rst (80, 2023-12-07)
docs/requirements.txt (44, 2023-12-07)
examples/ (0, 2023-12-07)
examples/CMakeLists.txt (0, 2023-12-07)
include/ (0, 2023-12-07)
include/parsi/ (0, 2023-12-07)
include/parsi/base.hpp (3441, 2023-12-07)
include/parsi/charset.hpp (2632, 2023-12-07)
include/parsi/fn/ (0, 2023-12-07)
include/parsi/fn/anyof.hpp (1775, 2023-12-07)
include/parsi/fn/eos.hpp (649, 2023-12-07)
include/parsi/fn/expect.hpp (2664, 2023-12-07)
include/parsi/fn/extract.hpp (1951, 2023-12-07)
include/parsi/fn/optional.hpp (1146, 2023-12-07)
include/parsi/fn/repeated.hpp (4440, 2023-12-07)
include/parsi/fn/sequence.hpp (1827, 2023-12-07)
include/parsi/internal/ (0, 2023-12-07)
... ...

# parsi [![Build:Linux](https://github.com/cthulhu-irl/parsi/actions/workflows/linux.yml/badge.svg)](https://github.com/cthulhu-irl/parsi/actions?query=workflow%3ALinux) [![Build:Windows](https://github.com/cthulhu-irl/parsi/actions/workflows/windows.yml/badge.svg)](https://github.com/cthulhu-irl/parsi/actions?query=workflow%3AWindows) [![codecov](https://codecov.io/gh/cthulhu-irl/parsi/branch/build/coverage/graph/badge.svg?token=U2QVK5MRNW)](https://codecov.io/gh/cthulhu-irl/parsi) NOTE: this is WIP/prototype and not ready. parsi is a [parser combinator](https://en.wikipedia.org/wiki/Parser_combinator) library that provides basic parser blocks and a common way to define and combine parsers into more complex parsers. It can be used as an alternative to complex regex, especially when data needs to be extracted. The minimum required standard is currently `C++20`. A simple hex color parser would look like this: ```cpp struct Color { std::uint8_t red = 0; std::uint8_t green = 0; std::uint8_t blue = 0; }; constexpr auto color_from_string(std::string_view str) -> std::optional { constexpr auto convert_hex_digit = [](char digit) -> std::uint8_t { if ('0' <= digit && digit <= '9') { return digit - '0'; } if ('a' <= digit && digit <= 'f') { return 10 + (digit - 'a'); } if ('A' <= digit && digit <= 'F') { return 10 + (digit - 'A'); } // unreachable as parser has verified it return 0; }; Color color; constexpr auto hex_charset = parsi::Charset("0123456789abcdefABCDEF"); auto parser = parsi::sequence( parsi::expect('#'), // a color code with 6 hex digits like `#C3A3BB` is equivalent to // Color{ .red = 0xC3, .green = 0xA3, .blue = 0xBB } parsi::extract( parsi::repeat<6, 6>(parsi::expect(hex_charset)), // min=6 and max=6 [&](std::string_view str) { // str's length is guaranteed to be 6, // and characters are guaranteed to be in hex_charset. color.red = convert_hex_digit(str[0]) * 16 + convert_hex_digit(str[1]); color.green = convert_hex_digit(str[2]) * 16 + convert_hex_digit(str[3]); color.blue = convert_hex_digit(str[4]) * 16 + convert_hex_digit(str[5]); } ), parsi::eos() // end of stream ); if (!parser(str)) { return std::nullopt; } return color; } ``` ### Building Examples Example executables will be built with `example_` prefix into the `bin/` directory: ``` cmake -S . -B build -DPARSI_EXAMPLES=ON cmake --build build ``` ### Roadmap - [x] core: base types, parsers, and combinators (Stream, Result, expect, sequence, anyof, etc.) - [ ] core: runtime friendly api (polymorphism) - [ ] extra: json - [ ] extra: msgpack - [ ] core: parallelization - [ ] extra: toml

近期下载者

相关文件


收藏者