Include the filename in the error messages

This commit is contained in:
2024-03-30 00:21:58 +01:00
parent e04a816024
commit 3c19bf1504
8 changed files with 137 additions and 51 deletions

View File

@ -1,9 +1,46 @@
#pragma once
#include <algorithm>
#include <filesystem>
#include "elna/source/result.hpp"
namespace elna::cli
{
char *readSource(const char *source);
/**
* Reads an input file and returns its contents.
*
* \param source Input file.
*
* \return File contents.
*/
std::string read_source(const char *source);
/**
* Formats and prints the given error.
*
* \param compile_error The error to print.
*/
void print_error(const std::unique_ptr<source::error>& compile_error);
/**
* Prints the given errors to the standard output.
*
* \param begin Pointer to the first error.
* \param end Pointer pass the last error.
*/
template<typename I>
void print_errors(I begin, I end)
{
std::for_each(begin, end, &print_error);
}
/**
* Compiles \a in_file and writes the generated code into \a out_file.
*
* \param in_file Input file.
* \param out_file Output file.
*
* \return Exit status.
*/
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file);
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include <filesystem>
#include <optional>
#include <string>
#include <vector>
@ -123,7 +124,13 @@ namespace elna::source
std::string character;
public:
unexpected_character(const std::string& character, const source::position position);
/**
* \param character Unexpected character.
* \param path Source file name.
* \param position Unexpected token position.
*/
unexpected_character(const std::string& character, const std::filesystem::path& path,
const source::position position);
std::string what() const override;
};
@ -133,14 +140,18 @@ namespace elna::source
token m_token;
public:
explicit unexpected_token(const token& token);
/**
* \param token Unexpected token.
* \param path Source file name.
*/
unexpected_token(const token& token, const std::filesystem::path& path);
std::string what() const override;
};
struct lexer
{
lexer(std::vector<token>&& tokens, const position last_position);
lexer(std::vector<token>&& tokens, const position last_position, const std::filesystem::path& path);
lexer(const lexer&) = delete;
lexer(lexer&&) = default;
@ -217,6 +228,7 @@ namespace elna::source
std::vector<token> tokens;
std::vector<token>::const_iterator iterator;
std::list<std::unique_ptr<error>> m_errors;
std::filesystem::path source_file;
token eof;
};
@ -224,7 +236,8 @@ namespace elna::source
* Splits the source text into tokens.
*
* \param buffer Source text.
* \param path Source file location.
* \return Tokens or error.
*/
elna::source::result<lexer> lex(const std::string& buffer);
elna::source::result<lexer> lex(const std::string& buffer, const std::filesystem::path& path);
}

View File

@ -2,6 +2,7 @@
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <variant>
#include <list>
#include <memory>
@ -30,12 +31,16 @@ namespace elna::source
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 position position);
error(const std::filesystem::path& path, const position position);
public:
/// Error text.
@ -46,6 +51,9 @@ namespace elna::source
/// 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>
@ -106,7 +114,14 @@ namespace elna::source
std::string name;
public:
name_collision(const std::string& name, const position current, const position previous);
/**
* \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;
};