ArkScript
A small, lisp-inspired, functional scripting language
Exceptions.hpp
Go to the documentation of this file.
1/**
2 * @file Exceptions.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com), Max (madstk1@pm.me)
4 * @brief ArkScript homemade exceptions
5 * @date 2020-10-27
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef ARK_ERROR_EXCEPTIONS_HPP
12#define ARK_ERROR_EXCEPTIONS_HPP
13
14#include <string>
15#include <utility>
16#include <stdexcept>
17#include <optional>
18
22
23namespace Ark
24{
25 namespace internal
26 {
27 class Node;
28 }
29
30 class ARK_API Error : public std::runtime_error
31 {
32 public:
33 explicit Error(const std::string& message) :
34 std::runtime_error(message)
35 {}
36
37 [[nodiscard]] virtual std::string details(bool colorize [[maybe_unused]]) const
38 {
39 return what();
40 }
41 };
42
43 /**
44 * @brief A type error triggered when types don't match
45 *
46 */
47 class ARK_API TypeError final : public Error
48 {
49 public:
50 explicit TypeError(const std::string& message) :
51 Error(message)
52 {}
53 };
54
55 /**
56 * @brief An assertion error, only triggered from ArkScript code through (assert expr error-message)
57 *
58 */
59 class ARK_API AssertionFailed final : public Error
60 {
61 public:
62 explicit AssertionFailed(const std::string& message) :
63 Error("AssertionFailed: " + message)
64 {}
65 };
66
67 class ARK_API NestedError final : public Error
68 {
69 public:
70 NestedError(const Error& e, const std::string& details) :
71 Error("NestedError"),
72 m_details(e.details(/* colorize= */ false))
73 {
74 if (!m_details.empty() && m_details.back() != '\n')
75 m_details += '\n';
76 m_details += "\n" + details;
77 }
78
79 NestedError(const std::exception& e, const std::string& details) :
80 Error("NestedError"),
81 m_details(e.what())
82 {
83 if (!m_details.empty() && m_details.back() != '\n')
84 m_details += '\n';
85 m_details += "\n" + details;
86 }
87
88 [[nodiscard]] const char* what() const noexcept override
89 {
90 return m_details.c_str();
91 }
92
93 private:
94 std::string m_details;
95 };
96
97 /**
98 * @brief CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
99 *
100 */
101 struct ARK_API CodeError final : Error
102 {
104 const std::optional<CodeErrorContext> additional_context;
105
106 CodeError(const std::string& what, CodeErrorContext ctx, std::optional<CodeErrorContext> maybe_more_context = std::nullopt) :
107 Error(what),
108 context(std::move(ctx)),
109 additional_context(std::move(maybe_more_context))
110 {}
111 };
112}
113
114#endif // ARK_ERROR_EXCEPTIONS_HPP
Defines a code error context.
#define ARK_API
Definition Module.hpp:22
ArkScript configuration macros.
An assertion error, only triggered from ArkScript code through (assert expr error-message)
AssertionFailed(const std::string &message)
virtual std::string details(bool colorize) const
Error(const std::string &message)
NestedError(const Error &e, const std::string &details)
NestedError(const std::exception &e, const std::string &details)
const char * what() const noexcept override
std::string m_details
A type error triggered when types don't match.
TypeError(const std::string &message)
STL namespace.
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
const std::optional< CodeErrorContext > additional_context
const CodeErrorContext context
CodeError(const std::string &what, CodeErrorContext ctx, std::optional< CodeErrorContext > maybe_more_context=std::nullopt)