~maelkum/viuavm

2a6cb524400b389f2ee4df0869121e2a51ff9cb6 — Marek Marecki 4 months ago 4a526b6
Improve aside notes

They are still not as versatile as they should be, but now they can be
attached to non-main lexemes on an error line. Error messages spread out
over several lines are still a feature for the future (ha!).
M new/include/viua/libs/errors/compile_time.h => new/include/viua/libs/errors/compile_time.h +9 -2
@@ 159,13 159,20 @@ class Error {
     * Its location is independent of the main lexeme.
     */
    std::string aside_note;
    viua::libs::lexer::Lexeme aside_lexeme;
    std::optional<viua::libs::lexer::Lexeme> aside_lexeme;

  public:
    Error(viua::libs::lexer::Lexeme, Cause const, std::string = "");

    auto aside(std::string) -> Error&;
    auto aside(std::string, std::optional<Lexeme> = std::nullopt) -> Error&;
    auto aside() const -> std::string_view;
    auto aside_character() const -> size_t
    {
        if (aside_lexeme.has_value()) {
            return aside_lexeme->location.character;
        }
        return character();
    }

    auto note(std::string) -> Error&;
    auto notes() const -> std::vector<std::string> const&;

M new/src/tools/libs/errors/compile_time.cpp => new/src/tools/libs/errors/compile_time.cpp +5 -1
@@ 63,9 63,13 @@ Error::Error(viua::libs::lexer::Lexeme lx, Cause const ce, std::string m)
        : cause{ce}, message{std::move(m)}, main_lexeme{lx}
{}

auto Error::aside(std::string s) -> Error&
auto Error::aside(std::string s, std::optional<Lexeme> l) -> Error&
{
    aside_note = std::move(s);
    aside_lexeme = l;
    if (l.has_value()) {
        add(*l);
    }
    return *this;
}
auto Error::aside() const -> std::string_view

M new/src/tools/libs/stage.cpp => new/src/tools/libs/stage.cpp +2 -2
@@ 302,11 302,11 @@ auto display_error(std::filesystem::path source_path,
    if (not e.aside().empty()) {
        std::cerr << std::string(ERROR_MARKER.size(), ' ')
                  << std::string(LINE_NO_WIDTH, ' ') << esc(2, COLOR_FG_CYAN)
                  << SEPARATOR_ASIDE << std::string(e.character(), ' ') << '|'
                  << SEPARATOR_ASIDE << std::string(e.aside_character(), ' ') << '|'
                  << esc(2, ATTR_RESET) << "\n";
        std::cerr << std::string(ERROR_MARKER.size(), ' ')
                  << std::string(LINE_NO_WIDTH, ' ') << esc(2, COLOR_FG_CYAN)
                  << SEPARATOR_ASIDE << std::string(e.character(), ' ')
                  << SEPARATOR_ASIDE << std::string(e.aside_character(), ' ')
                  << e.aside() << esc(2, ATTR_RESET) << "\n";
        std::cerr << esc(2, COLOR_FG_CYAN)
                  << std::string(ERROR_MARKER.size(), ' ')