lang_2023/include/error_handling.hpp

78 lines
2.5 KiB
C++

#pragma once
#include "interpreter_tree.hpp"
#include <iostream>
#include <pthread.h>
namespace error_handling {
inline void PrintPosition(std::ostream& out,
std::pair<size_t, size_t> start_position,
std::pair<size_t, size_t> end_position) {
out << '['
<< start_position.first + 1
<< ", "
<< start_position.second + 1
<< "] - ["
<< end_position.first + 1
<< ", "
<< end_position.second + 1
<< ']';
}
inline void HandleParsingError(const std::string& message,
std::pair<size_t, size_t> start_position,
std::pair<size_t, size_t> end_position) {
std::cerr << "\x1b[1;31mParsing Error:\x1b[0m " << message << " at ";
PrintPosition(std::cerr, start_position, end_position);
std::cerr << ".\n";
exit(1);
}
inline void HandleGeneralError(const std::string& message) {
std::cerr << "\x1b[1;31mGeneral Error:\x1b[0m " << message << ".\n";
exit(1);
}
inline void HandleInternalError(const std::string& message,
const std::string& place,
std::optional<const interpreter::tokens::BaseNode*> node) {
std::cerr << "\x1b[1;31mInternal Error:\x1b[0m " << message << " at " << place;
if (node.has_value()) {
std::cerr << ", at ";
PrintPosition(std::cerr, node.value()->start_position, node.value()->end_position);
}
std::cerr << ".\n";
exit(1);
}
inline void HandleTypecheckError(const std::string& message,
const interpreter::tokens::BaseNode& node) {
std::cerr << "\x1b[1;31mTypecheck Error:\x1b[0m " << message << " at ";
PrintPosition(std::cerr, node.start_position, node.end_position);
std::cerr << ".\n";
exit(1);
}
inline void HandleRuntimeError(const std::string& message,
const interpreter::tokens::BaseNode& node) {
std::cerr << "\x1b[1;31mRuntime Error:\x1b[0m " << message << " at ";
PrintPosition(std::cerr, node.start_position, node.end_position);
std::cerr << ".\n";
exit(1);
}
inline void HandleNamesError(const std::string& message,
const interpreter::tokens::BaseNode& node) {
std::cerr << "\x1b[1;31mNames Error:\x1b[0m " << message << " at ";
PrintPosition(std::cerr, node.start_position, node.end_position);
std::cerr << ".\n";
exit(1);
}
template<typename T>
inline void DebugPrint(const T& value) {
std::cerr << "\x1b[1;33mDebug:\x1b[0m " << value << '\n';
}
} // namespace error_handling