mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2026-01-25 21:17:11 +00:00
expression builders finished, type builders started
This commit is contained in:
parent
6682e0beb1
commit
535d8d26c3
12 changed files with 909 additions and 76 deletions
70
include/error_handling.hpp
Normal file
70
include/error_handling.hpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#pragma once
|
||||
#include "basic_nodes.hpp"
|
||||
#include "tree_sitter_wrapper.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace error_handling {
|
||||
|
||||
inline void print_position(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 handle_general_error(const std::string &message) {
|
||||
std::cerr << "\x1b[1;31mGeneral Error:\x1b[0m " << message << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void
|
||||
handle_internal_error(const std::string &message, const std::string &place,
|
||||
std::optional<nodes::Node> node = std::nullopt) {
|
||||
std::cerr << "\x1b[1;31mInternal Error:\x1b[0m " << message << " at "
|
||||
<< place;
|
||||
if (node.has_value()) {
|
||||
std::cerr << ", at ";
|
||||
print_position(std::cerr, node.value().get_start_position(),
|
||||
node.value().get_end_position());
|
||||
}
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void handle_parsing_error(const std::string &message,
|
||||
parser::ParseTree::Node parse_node) {
|
||||
std::cerr << "\x1b[1;31mParsing Error:\x1b[0m " << message << " at ";
|
||||
print_position(std::cerr, parse_node.get_start_point(),
|
||||
parse_node.get_end_point());
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void handle_typecheck_error(const std::string &message,
|
||||
nodes::Node node) {
|
||||
std::cerr << "\x1b[1;31mTypecheck Error:\x1b[0m " << message << " at ";
|
||||
print_position(std::cerr, node.get_start_position(), node.get_end_position());
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void handle_runtime_error(const std::string &message, nodes::Node node) {
|
||||
std::cerr << "\x1b[1;31mRuntime Error:\x1b[0m " << message << " at ";
|
||||
print_position(std::cerr, node.get_start_position(), node.get_end_position());
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void handle_names_error(const std::string &message, nodes::Node node) {
|
||||
std::cerr << "\x1b[1;31mNames Error:\x1b[0m " << message << " at ";
|
||||
print_position(std::cerr, node.get_start_position(), node.get_end_position());
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
template <typename T> inline void debug_print(const T &value) {
|
||||
std::cerr << "\x1b[1;33mDebug:\x1b[0m " << value << '\n';
|
||||
}
|
||||
|
||||
} // namespace error_handling
|
||||
Loading…
Add table
Add a link
Reference in a new issue