ArkScript
A small, fast, functional and scripting language for video games
Exceptions.hpp
Go to the documentation of this file.
1/**
2 * @file Exceptions.hpp
3 * @author Alexandre Plateau ([email protected]), Max ([email protected])
4 * @brief ArkScript homemade exceptions
5 * @version 1.3
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef INCLUDE_ARK_EXCEPTIONS_HPP
13#define INCLUDE_ARK_EXCEPTIONS_HPP
14
15#include <string>
16#include <vector>
17#include <stdexcept>
18#include <optional>
19#include <ostream>
20#include <iostream>
21
23#include <Ark/Platform.hpp>
24
25namespace Ark
26{
27 namespace internal
28 {
29 class Node;
30 }
31
32 class Error : public std::runtime_error
33 {
34 public:
35 explicit Error(const std::string& message) :
36 std::runtime_error(message)
37 {}
38 };
39
40 /**
41 * @brief A type error triggered when types don't match
42 *
43 */
44 class TypeError final : public Error
45 {
46 public:
47 explicit TypeError(const std::string& message) :
48 Error(message)
49 {}
50 };
51
52 /**
53 * @brief A special zero division error triggered when a number is divided by 0
54 *
55 */
56 class ZeroDivisionError final : public Error
57 {
58 public:
60 Error(
61 "ZeroDivisionError: In ordinary arithmetic, the expression has no meaning, "
62 "as there is no number which, when multiplied by 0, gives a (assuming a != 0), "
63 "and so division by zero is undefined. Since any number multiplied by 0 is 0, "
64 "the expression 0/0 is also undefined.")
65 {}
66 };
67
68 /**
69 * @brief An assertion error, only triggered from ArkScript code through (assert expr error-message)
70 *
71 */
72 class AssertionFailed final : public Error
73 {
74 public:
75 explicit AssertionFailed(const std::string& message) :
76 Error("AssertionFailed: " + message)
77 {}
78 };
79
80 /**
81 * @brief CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
82 *
83 */
84 struct CodeError final : Error
85 {
86 const std::string filename;
87 const std::size_t line;
88 const std::size_t col;
89 const std::string expr;
90 const std::optional<internal::utf8_char_t> symbol;
91
93 const std::string& what,
94 std::string filename_,
95 const std::size_t lineNum,
96 const std::size_t column,
97 std::string exp,
98 const std::optional<internal::utf8_char_t> opt_sym = std::nullopt) :
99 Error(what),
100 filename(std::move(filename_)), line(lineNum), col(column), expr(std::move(exp)), symbol(opt_sym)
101 {}
102 };
103
104 namespace Diagnostics
105 {
106 /**
107 * @brief Helper to create a colorized context to report errors to the user
108 *
109 * @param os stream in which the error will be written
110 * @param code content of the source file where the error is
111 * @param target_line line where the error is
112 * @param col_start where the error starts on the given line
113 * @param sym_size bad expression that triggered the error
114 * @param colorize generate colors or not
115 */
116 ARK_API void makeContext(std::ostream& os, const std::string& code, std::size_t target_line, std::size_t col_start, std::size_t sym_size, bool colorize);
117
118 /**
119 * @brief Helper used by the compiler to generate a colorized context from a node
120 *
121 * @param message error message to be included in the context
122 * @param node AST node with the error
123 * @return std::string
124 */
125 ARK_API std::string makeContextWithNode(const std::string& message, const internal::Node& node);
126
127 /**
128 * @brief Generate a diagnostic from an error and print it to the standard output
129 *
130 * @param e code error
131 * @param os output stream
132 * @param colorize generate colors or not
133 */
134 ARK_API void generate(const CodeError& e, std::ostream& os = std::cout, bool colorize = true);
135 }
136}
137
138#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
An assertion error, only triggered from ArkScript code through (assert expr error-message)
AssertionFailed(const std::string &message)
Error(const std::string &message)
A type error triggered when types don't match.
TypeError(const std::string &message)
A special zero division error triggered when a number is divided by 0.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:30
ARK_API void makeContext(std::ostream &os, const std::string &code, std::size_t target_line, std::size_t col_start, std::size_t sym_size, bool colorize)
Helper to create a colorized context to report errors to the user.
ARK_API void generate(const CodeError &e, std::ostream &os=std::cout, bool colorize=true)
Generate a diagnostic from an error and print it to the standard output.
ARK_API std::string makeContextWithNode(const std::string &message, const internal::Node &node)
Helper used by the compiler to generate a colorized context from a node.
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
const std::size_t line
const std::string filename
const std::size_t col
const std::string expr
const std::optional< internal::utf8_char_t > symbol
CodeError(const std::string &what, std::string filename_, const std::size_t lineNum, const std::size_t column, std::string exp, const std::optional< internal::utf8_char_t > opt_sym=std::nullopt)