diff options
Diffstat (limited to 'nihil.ucl/parser.ccm')
| -rw-r--r-- | nihil.ucl/parser.ccm | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/nihil.ucl/parser.ccm b/nihil.ucl/parser.ccm new file mode 100644 index 0000000..5fa3495 --- /dev/null +++ b/nihil.ucl/parser.ccm @@ -0,0 +1,160 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <coroutine> +#include <expected> +#include <format> +#include <functional> +#include <memory> +#include <string> +#include <vector> + +#include <ucl.h> + +export module nihil.ucl:parser; + +import nihil.monad; +import :object; +import :map; + +namespace nihil::ucl { + +// UCL parser flags. +export inline constexpr int parser_key_lower = UCL_PARSER_KEY_LOWERCASE; +export inline constexpr int parser_zerocopy = UCL_PARSER_ZEROCOPY; +export inline constexpr int parser_no_time = UCL_PARSER_NO_TIME; + +// A macro handler. This proxies the C API callback to the C++ API. +using macro_callback_t = bool (std::string_view); + +struct macro_handler { + std::function<macro_callback_t> callback; + + // Handle a callback from the C API. + static auto handle( + unsigned char const *data, + std::size_t len, void + *ud) + -> bool; +}; + +/* + * A UCL parser. This wraps the C ucl_parser API. + * + * parser itself is not exported; use make_parser() to create one. + */ +struct parser { + // Create a parser from a UCL parser. + parser(::ucl_parser *); + + // Destroy our parser when we're destroyed. + ~parser(); + + // Not copyable. + parser(parser const &) = delete; + auto operator=(this parser &, parser const &) -> parser & = delete; + + // Movable. + parser(parser &&) noexcept; + auto operator=(this parser &, parser &&) noexcept -> parser &; + + // Add a parser macro. Unlike ucl_parser_register_macro, this doesn't + // take a userdata parameter; it's assumed the user will use lambda + // capture or similar if needed. + template<std::invocable<std::string_view> F> + auto register_macro(this parser &self, + std::string_view name, + F &&func) + -> void + requires (std::same_as<bool, std::invoke_result<F>>) + { + auto handler = std::make_unique<macro_handler>( + std::forward<F>(func)); + + auto cname = std::string(name); + ::ucl_parser_register_macro( + self.get_parser(), cname.c_str(), + ¯o_handler::handle, handler.get()); + + self.m_macros.emplace_back(std::move(handler)); + } + + // Add a parser variable. + auto register_value(this parser &self, + std::string_view variable, + std::string_view value) + -> void; + + // Add data to the parser. + [[nodiscard]] auto add(this parser &self, + std::ranges::contiguous_range auto &&data) + -> std::expected<void, error> + // Only bytes (chars) are permitted. + requires(sizeof(std::ranges::range_value_t<decltype(data)>) == 1) + { + auto *p = self.get_parser(); + auto dptr = reinterpret_cast<unsigned char const *>( + std::ranges::data(data)); + + auto ret = ::ucl_parser_add_chunk( + p, dptr, std::ranges::size(data)); + + if (ret == true) + return {}; + + return std::unexpected(error(::ucl_parser_get_error(p))); + } + + [[nodiscard]] auto add(this parser &self, + std::ranges::range auto &&data) + -> std::expected<void, error> + requires (!std::ranges::contiguous_range<decltype(data)>) + { + auto cdata = std::vector<char>( + std::from_range, + std::forward<decltype(data)>(data)); + co_await self.add(std::move(cdata)); + co_return {}; + } + + // Return the top object of this parser. + [[nodiscard]] auto top(this parser &self) -> map<object>; + + // Return the stored parser object. + [[nodiscard]] auto get_parser(this parser &self) -> ::ucl_parser *; + +private: + // The parser object. Should never be null, unless we've been + // moved-from. + ucl_parser *m_parser; + + // Functions added by register_macro. We have to store these as + // pointers because we pass the address to libucl. + std::vector<std::unique_ptr<macro_handler>> m_macros; +}; + +// Create a parser with the given flags. +export [[nodiscard]] auto +make_parser(int flags = 0) -> std::expected<parser, error>; + +// Utility function to parse something and return the top-level object. +export [[nodiscard]] auto +parse(int flags, std::ranges::range auto &&data) + -> std::expected<map<object>, error> +{ + auto p = co_await make_parser(flags); + co_await p.add(std::forward<decltype(data)>(data)); + co_return p.top(); +} + +export [[nodiscard]] auto +parse(std::ranges::range auto &&data) + -> std::expected<map<object>, error> +{ + co_return co_await parse(0, std::forward<decltype(data)>(data)); +} + +} // namespace nihil::ucl |
