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.2
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 */
115 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);
116
117 /**
118 * @brief Helper used by the compiler to generate a colorized context from a node
119 *
120 * @param message error message to be included in the context
121 * @param node AST node with the error
122 * @return std::string
123 */
124 ARK_API std::string makeContextWithNode(const std::string& message, const internal::Node& node);
125
126 /**
127 * @brief Generate a diagnostic from an error and print it to the standard output
128 *
129 * @param e code error
130 * @param code code of the file in which the error occurred
131 * @param os output stream
132 */
133 ARK_API void generate(const CodeError& e, std::string code = "", std::ostream& os = std::cout);
134 }
135}
136
137#endif
#define ARK_API
Definition: Module.hpp:28
ArkScript configuration macros.
An assertion error, only triggered from ArkScript code through (assert expr error-message)
Definition: Exceptions.hpp:73
AssertionFailed(const std::string &message)
Definition: Exceptions.hpp:75
Error(const std::string &message)
Definition: Exceptions.hpp:35
A type error triggered when types don't match.
Definition: Exceptions.hpp:45
TypeError(const std::string &message)
Definition: Exceptions.hpp:47
A special zero division error triggered when a number is divided by 0.
Definition: Exceptions.hpp:57
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:30
ARK_API void generate(const CodeError &e, std::string code="", std::ostream &os=std::cout)
Generate a diagnostic from an error and print it to the standard output.
Definition: Exceptions.cpp:157
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.
Definition: Exceptions.cpp:135
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)
Helper to create a colorized context to report errors to the user.
Definition: Exceptions.cpp:79
Definition: Builtins.hpp:21
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
Definition: Exceptions.hpp:85
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)
Definition: Exceptions.hpp:92
const std::size_t line
Definition: Exceptions.hpp:87
const std::string filename
Definition: Exceptions.hpp:86
const std::size_t col
Definition: Exceptions.hpp:88
const std::string expr
Definition: Exceptions.hpp:89
const std::optional< internal::utf8_char_t > symbol
Definition: Exceptions.hpp:90