/* Parsing driver.
   Copyright (C) 2025 Free Software Foundation, Inc.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "elna/boot/driver.h"

namespace elna
{
namespace boot
{
    position make_position(const yy::location& location)
    {
        position result;
        result.line = static_cast<std::size_t>(location.begin.line);
        result.column = static_cast<std::size_t>(location.begin.column);

        return result;
    }

    syntax_error::syntax_error(const std::string& message,
            const char *input_file, const yy::location& location)
        : error(input_file, make_position(location)), message(message)
    {
    }

    std::string syntax_error::what() const
    {
        return message;
    }

    driver::driver(const char *input_file)
        : input_file(input_file)
    {
    }

    void driver::error(const yy::location& loc, const std::string& message)
    {
        m_errors.emplace_back(new boot::syntax_error(message, input_file, loc));
    }

    const std::list<std::unique_ptr<struct error>>& driver::errors() const noexcept
    {
        return m_errors;
    }

    char escape_char(char escape)
    {
        switch (escape)
        {
        case 'n':
            return '\n';
        case 'a':
            return '\a';
        case 'b':
            return '\b';
        case 't':
            return '\t';
        case 'f':
            return '\f';
        case 'r':
            return '\r';
        case 'v':
            return '\v';
        case '\\':
            return '\\';
        case '\'':
            return '\'';
        case '"':
            return '"';
        case '?':
            return '\?';
        case '0':
            return '\0';
        default:
            return escape_invalid_char;
        }
    }
}
}