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 class VM;
26
27 namespace internal
28 {
29 class Node;
30 }
31
32 class ARK_API Error : public std::runtime_error
33 {
34 public:
35 explicit Error(const std::string& message) :
36 std::runtime_error(message)
37 {}
38
39 [[nodiscard]] virtual std::string details(bool colorize [[maybe_unused]], VM& vm [[maybe_unused]]) const
40 {
41 return what();
42 }
43 };
44
45 /**
46 * @brief A type error triggered when types don't match
47 *
48 */
49 class ARK_API TypeError final : public Error
50 {
51 public:
52 explicit TypeError(const std::string& message) :
53 Error(message)
54 {}
55 };
56
57 /**
58 * @brief An assertion error, only triggered from ArkScript code through (assert expr error-message)
59 *
60 */
61 class ARK_API AssertionFailed final : public Error
62 {
63 public:
64 explicit AssertionFailed(const std::string& message) :
65 Error("AssertionFailed: " + message)
66 {}
67 };
68
69 class ARK_API NestedError final : public Error
70 {
71 public:
72 NestedError(const Error& e, const std::string& details, VM& vm) :
73 Error("NestedError"),
74 m_details(e.details(/* colorize= */ false, vm))
75 {
76 if (!m_details.empty() && m_details.back() != '\n')
77 m_details += '\n';
78 m_details += "\n" + details;
79 }
80
81 NestedError(const std::exception& e, const std::string& details) :
82 Error("NestedError"),
83 m_details(e.what())
84 {
85 if (!m_details.empty() && m_details.back() != '\n')
86 m_details += '\n';
87 m_details += "\n" + details;
88 }
89
90 [[nodiscard]] const char* what() const noexcept override
91 {
92 return m_details.c_str();
93 }
94
95 private:
96 std::string m_details;
97 };
98
99 /**
100 * @brief CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
101 *
102 */
103 struct ARK_API CodeError final : Error
104 {
106 const std::optional<CodeErrorContext> additional_context;
107
108 CodeError(const std::string& what, CodeErrorContext ctx, std::optional<CodeErrorContext> maybe_more_context = std::nullopt) :
109 Error(what),
110 context(std::move(ctx)),
111 additional_context(std::move(maybe_more_context))
112 {}
113 };
114}
115
116#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, VM &vm) const
Error(const std::string &message)
NestedError(const std::exception &e, const std::string &details)
const char * what() const noexcept override
NestedError(const Error &e, const std::string &details, VM &vm)
std::string m_details
A type error triggered when types don't match.
TypeError(const std::string &message)
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:47
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)