From 2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Tue, 1 Jul 2025 17:07:04 +0100 Subject: add nihil.std --- nihil.ucl/parser.cc | 102 ---------------------------------------------------- 1 file changed, 102 deletions(-) delete mode 100644 nihil.ucl/parser.cc (limited to 'nihil.ucl/parser.cc') diff --git a/nihil.ucl/parser.cc b/nihil.ucl/parser.cc deleted file mode 100644 index 0a08670..0000000 --- a/nihil.ucl/parser.cc +++ /dev/null @@ -1,102 +0,0 @@ -/* - * This source code is released into the public domain. - */ - -module; - -#include -#include -#include - -#include - -module nihil.ucl; - -import nihil.error; - -namespace nihil::ucl { - -auto make_parser(int flags) -> std::expected -{ - auto *p = ::ucl_parser_new(flags); - if (p != nullptr) - return p; - - // TODO: Is there a way to get the actual error here? - return std::unexpected(error("failed to create parser")); -} - -auto macro_handler::handle(unsigned char const *data, - std::size_t len, void *ud) - -> bool -{ - auto handler = static_cast(ud); - auto string = std::string_view( - reinterpret_cast(data), - len); - return handler->callback(string); -} - -parser::parser(::ucl_parser *uclp) - : m_parser(uclp) -{ -} - -parser::~parser() -{ - if (m_parser) - ::ucl_parser_free(m_parser); -} - -parser::parser(parser &&other) noexcept - : m_parser(std::exchange(other.m_parser, nullptr)) - , m_macros(std::move(other.m_macros)) -{ -} - -auto parser::operator=(this parser &self, parser &&other) noexcept - -> parser & -{ - if (&self != &other) { - if (self.m_parser) - ::ucl_parser_free(self.m_parser); - - self.m_parser = std::exchange(other.m_parser, nullptr); - self.m_macros = std::move(other.m_macros); - } - - return self; -} - -auto parser::register_value( - this parser &self, - std::string_view variable, - std::string_view value) - -> void -{ - ::ucl_parser_register_variable( - self.get_parser(), - std::string(variable).c_str(), - std::string(value).c_str()); -} - -auto parser::top(this parser &self) -> map -{ - auto obj = ::ucl_parser_get_object(self.get_parser()); - if (obj != nullptr) - // ucl_parser_get_object() refs the object for us. - return {noref, obj}; - - throw std::logic_error( - "attempt to call top() on an invalid ucl::parser"); -} - -auto parser::get_parser(this parser &self) -> ::ucl_parser * -{ - if (self.m_parser == nullptr) - throw std::logic_error("attempt to fetch a null ucl::parser"); - - return self.m_parser; -} - -} // namespace nihil::ucl -- cgit v1.2.3