elna/include/elna/source/result.hpp

195 lines
4.7 KiB
C++

#pragma once
#include <cstddef>
#include <filesystem>
#include <variant>
#include <list>
#include <memory>
#include <string>
#include <type_traits>
#include "elna/source/types.hpp"
namespace elna::source
{
/**
* Position in the source text.
*/
struct position
{
/// Line.
std::size_t line = 1;
/// Column.
std::size_t column = 1;
};
/**
* A compilation error consists of an error message and position.
*/
class error
{
position m_position;
std::filesystem::path m_path;
protected:
/**
* Constructs an error.
*
* \param path Source file name.
* \param position Error position in the source text.
*/
error(const std::filesystem::path& path, const position position);
public:
virtual ~error() noexcept = default;
/// Error text.
virtual std::string what() const = 0;
/// Error line in the source text.
std::size_t line() const noexcept;
/// Error column in the source text.
std::size_t column() const noexcept;
/// Source file name.
const std::filesystem::path& path() const noexcept;
};
template<typename T>
struct result
{
using E = std::list<std::unique_ptr<source::error>>;
template<typename... Args, typename = std::enable_if<std::is_constructible_v<T, Args...>>>
explicit result(std::in_place_t, Args&&... arguments)
: payload(std::in_place_type<T>, std::forward<Args>(arguments)...)
{
}
explicit result(T&& result)
: payload(std::move(result))
{
}
template<typename U, typename = std::enable_if<std::is_base_of_v<error, U>>>
explicit result(U&& first)
: payload(E())
{
errors().emplace_back(std::make_unique<U>(first));
}
explicit result(E&& errors)
: payload(std::move(errors))
{
}
bool has_errors() const noexcept
{
return std::holds_alternative<E>(payload);
}
bool is_success() const noexcept
{
return std::holds_alternative<T>(payload);
}
T& success()
{
return std::get<T>(payload);
}
E& errors()
{
return std::get<E>(payload);
}
private:
std::variant<T, E> payload;
};
class name_collision final : public error
{
position previous;
std::string name;
public:
/**
* \param name Symbol name.
* \param path Source file name.
* \param current Current symbol position.
* \param previous Position of the previously defined symbol.
*/
name_collision(const std::string& name, const std::filesystem::path& path,
const position current, const position previous);
std::string what() const override;
};
struct type_mismatch final : public error
{
/**
* Kind of the operation on the type.
*/
enum class operation
{
dereference,
argument,
arithmetic,
comparison,
condition,
assignment
};
/**
* \param name Given type.
* \param kind Kind of the operation on the type.
* \param path Source file name.
* \param position Operation position.
*/
type_mismatch(std::shared_ptr<const type> got, operation kind, const std::filesystem::path& path,
const struct position position);
std::string what() const override;
private:
std::shared_ptr<const type> got;
operation kind;
};
template<typename T>
struct writer
{
/**
* Writes data to the table and saves it under the given label.
*
* \param label Symbol name.
* \param data Data to be written.
* \param size Data size.
*
* \return New size of the table.
*/
virtual std::size_t sink(const std::string& label, const T *data, std::size_t size) = 0;
/**
* Writes data and returns a label under that the data can be accessed.
*
* \param data Data to be written.
* \param size Data size.
*
* \return Label for the symbol.
*/
virtual std::string_view sink(const T *data, std::size_t size) = 0;
/**
* Creates an external symbol.
*/
virtual void sink(const std::string& label) = 0;
/**
* \return Actual size of the text section.
*/
virtual std::size_t size() const = 0;
};
}