Include the filename in the error messages
This commit is contained in:
67
cli/cl.cpp
67
cli/cl.cpp
@ -3,55 +3,70 @@
|
||||
#include "elna/source/semantic.hpp"
|
||||
#include <cstddef>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace elna::cli
|
||||
{
|
||||
char *readSource(const char *source)
|
||||
std::string read_source(const char *source)
|
||||
{
|
||||
const std::size_t bufferSize = 255;
|
||||
constexpr std::size_t buffer_size = 4096;
|
||||
|
||||
std::ifstream input_stream{ source };
|
||||
std::stringstream buffer;
|
||||
buffer << input_stream.rdbuf();
|
||||
input_stream.close();
|
||||
std::string contents = buffer.str();
|
||||
char *result = reinterpret_cast<char *>(malloc(contents.size() + 1));
|
||||
std::copy(std::cbegin(contents), std::cend(contents), result);
|
||||
result[contents.size()] = '\0';
|
||||
std::ifstream input_stream{ source, std::ios::binary | std::ios::in };
|
||||
std::string output;
|
||||
if (input_stream.fail())
|
||||
{
|
||||
throw std::ios_base::failure("File does not exist");
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
const std::size_t old_size = output.size();
|
||||
output.resize(old_size + buffer_size);
|
||||
input_stream.read(&output[old_size], buffer_size);
|
||||
|
||||
return result;
|
||||
if (input_stream.eof())
|
||||
{
|
||||
output.resize(old_size + input_stream.gcount());
|
||||
break;
|
||||
}
|
||||
else if (input_stream.fail())
|
||||
{
|
||||
throw std::ios_base::failure("Unable to complete reading the source file");
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
void print_error(const std::unique_ptr<source::error>& compile_error)
|
||||
{
|
||||
std::cerr << compile_error->path().string() << ":"
|
||||
<< compile_error->line() << ':' << compile_error->column()
|
||||
<< ": " << compile_error->what() << std::endl;
|
||||
}
|
||||
|
||||
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file)
|
||||
{
|
||||
auto sourceText = readSource(in_file.c_str());
|
||||
if (sourceText == nullptr)
|
||||
std::string source_text;
|
||||
try
|
||||
{
|
||||
source_text = read_source(in_file.c_str());
|
||||
}
|
||||
catch (std::ios_base::failure&)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
size_t tokensCount{ 0 };
|
||||
auto lex_result = source::lex(sourceText);
|
||||
free(sourceText);
|
||||
auto lex_result = source::lex(source_text, in_file);
|
||||
|
||||
if (lex_result.has_errors())
|
||||
{
|
||||
for (const auto& compile_error : lex_result.errors())
|
||||
{
|
||||
std::cerr << compile_error->line() << ':' << compile_error->column()
|
||||
<< ": " << compile_error->what() << std::endl;
|
||||
}
|
||||
print_errors(lex_result.errors().cbegin(), lex_result.errors().cend());
|
||||
return 1;
|
||||
}
|
||||
source::parser parser{ std::move(lex_result.success()) };
|
||||
auto ast = parser.parse();
|
||||
if (ast == nullptr)
|
||||
{
|
||||
for (const auto& compile_error : parser.errors())
|
||||
{
|
||||
std::cerr << compile_error->line() << ':' << compile_error->column()
|
||||
<< ": " << compile_error->what() << std::endl;
|
||||
}
|
||||
print_errors(parser.errors().cbegin(), parser.errors().cend());
|
||||
return 2;
|
||||
}
|
||||
auto global_scope = std::make_shared<source::symbol_table>();
|
||||
|
Reference in New Issue
Block a user