1 #include "base/error.h"
2 #include "source_file/token.h"
3 #include "backtrace/tan_backtrace.h"
6 using namespace tanlang;
8 [[noreturn]]
void __tan_assert_fail(
const char *expr,
const char *file,
size_t lineno) {
10 Error(ErrorType::ASSERTION_FAILED,
11 fmt::format(
"ASSERTION FAILED: {}\nat {}:{}\n", expr, file, std::to_string(lineno)))
15 CompileException::CompileException(ErrorType error_type,
const str &msg) : std::runtime_error(msg), _type(error_type) {}
17 CompileException::CompileException(ErrorType error_type,
const char *msg)
18 : std::runtime_error(msg), _type(error_type) {}
20 ErrorType CompileException::type()
const {
return _type; }
22 Error::Error(
const str &error_message) { _msg =
"[ERROR] " + error_message; }
24 Error::Error(ErrorType type,
const str &error_message) : _type(type) {
25 _msg = fmt::format(
"[{}] {}", ERROR_TYPE_ENUM_TO_STRING[type], error_message);
28 Error::Error(ErrorType type,
SourceSpan span,
const str &error_message) : _type(type) {
31 "[{}] in {}:{} {}\n", ERROR_TYPE_ENUM_TO_STRING[type], src->get_filename(), span.start().l, error_message);
33 uint32_t col = span.start().c;
34 for (uint32_t line = span.start().l; line <= span.end().l; ++line) {
37 str indent = col > 0 ? str(col,
' ') :
"";
39 uint32_t end_col = (uint32_t)src_line.length();
40 if (line == span.end().l) {
41 end_col = span.end().c + 1;
43 str indicator = str(end_col - col,
'^');
45 _msg += fmt::format(
"{}\n{}{}\n", src_line, indent, indicator);
51 Error::Error(ErrorType type,
Token *start,
Token *end,
const str &error_message)
52 :
Error(type,
Token::GetSourceSpan(*start, *end), error_message) {}
56 ErrorType Error::type()
const {
return _type; }
58 #define ERROR_TYPE_TO_STRING_HELPER(x) \
61 umap<ErrorType, str> Error::ERROR_TYPE_ENUM_TO_STRING{
62 ERROR_TYPE_TO_STRING_HELPER(GENERIC_ERROR),
63 ERROR_TYPE_TO_STRING_HELPER(ASSERTION_FAILED),
64 ERROR_TYPE_TO_STRING_HELPER(FILE_NOT_FOUND),
65 ERROR_TYPE_TO_STRING_HELPER(SYNTAX_ERROR),
66 ERROR_TYPE_TO_STRING_HELPER(NOT_IMPLEMENTED),
67 ERROR_TYPE_TO_STRING_HELPER(SEMANTIC_ERROR),
68 ERROR_TYPE_TO_STRING_HELPER(UNKNOWN_SYMBOL),
69 ERROR_TYPE_TO_STRING_HELPER(IMPORT_ERROR),
70 ERROR_TYPE_TO_STRING_HELPER(TYPE_ERROR),
71 ERROR_TYPE_TO_STRING_HELPER(COMPILE_ERROR),
72 ERROR_TYPE_TO_STRING_HELPER(LINK_ERROR),
str get_line(size_t index) const
Return source at a specific line.
A span of source code tokens, inclusive on both ends.