tan  0.0.1
error.h
1 #ifndef __TAN_SRC_BASE_ERROR_H__
2 #define __TAN_SRC_BASE_ERROR_H__
3 #include "base/container.h"
4 #include "source_file/source_file.h"
5 #include <stdexcept>
6 
7 [[noreturn]] void __tan_assert_fail(const char *expr, const char *file, size_t lineno);
8 
9 namespace tanlang {
10 
11 #ifdef DEBUG
12 #define TAN_ASSERT(expr) (static_cast<bool>((expr)) ? void(0) : __tan_assert_fail(#expr, __FILE__, __LINE__))
13 #else
14 #define TAN_ASSERT(expr)
15 #endif
16 
17 // REMEMBER to add to Error::ERROR_TYPE_ENUM_TO_STRING
18 enum class ErrorType {
19  GENERIC_ERROR,
20  NOT_IMPLEMENTED,
21  ASSERTION_FAILED,
22  FILE_NOT_FOUND,
23  SYNTAX_ERROR,
24  SEMANTIC_ERROR,
25  UNKNOWN_SYMBOL,
26  IMPORT_ERROR,
27  TYPE_ERROR,
28  COMPILE_ERROR,
29  LINK_ERROR,
30 };
31 
32 class Error;
33 
34 class CompileException : public std::runtime_error {
35 public:
36  CompileException(ErrorType error_type, const str &msg);
37  CompileException(ErrorType error_type, const char *msg);
38 
39  [[nodiscard]] ErrorType type() const;
40 
41 private:
42  ErrorType _type = ErrorType::GENERIC_ERROR;
43 };
44 
45 class Token;
46 
47 class Error {
48 public:
49  static umap<ErrorType, str> ERROR_TYPE_ENUM_TO_STRING;
50 
51 public:
52  explicit Error(const str &error_message);
53  Error(ErrorType type, const str &error_message);
54  Error(ErrorType type, Token *start, Token *end, const str &error_message);
55  Error(ErrorType type, SourceSpan span, const str &error_message);
56 
57  [[noreturn]] void raise() const;
58  [[nodiscard]] ErrorType type() const;
59 
60 private:
61  str _msg;
62  ErrorType _type = ErrorType::GENERIC_ERROR;
63 };
64 
65 } // namespace tanlang
66 
67 #endif // __TAN_SRC_BASE_ERROR_H__
A span of source code tokens, inclusive on both ends.
Definition: source_file.h:96