-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.h
159 lines (131 loc) · 4.78 KB
/
error.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/****************************************************************************
** Copyright (c) 2023, Adel Kara Slimane <adel.ks@zegrapher.com>
**
** This file is part of ZeCalculator.
**
** ZeCalculators is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU Affero General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#pragma once
#include <cstdint>
#include <zecalculator/parsing/data_structures/token.h>
namespace zc {
struct Error
{
// type of error
enum Type : uint8_t
{
CALLING_FUN_ARG_COUNT_MISMATCH,
OBJECT_INVALID_STATE, // expression that contains a function who cannot return values
EMPTY_EXPRESSION,
MISSING,
NAME_ALREADY_TAKEN,
NOT_IMPLEMENTED,
OBJECT_NOT_IN_WORLD,
RECURSION_DEPTH_OVERFLOW, // maximum recursion depth has been reached
UNDEFINED_FUNCTION,
UNDEFINED_VARIABLE,
UNEXPECTED,
UNEXPECTED_END_OF_EXPRESSION,
UNKNOWN,
WRONG_FORMAT,
WRONG_OBJECT_TYPE, // object has been used as a different type as it actually is, example "2+cos" (where cos is a function used here as variable)
NOT_MATH_OBJECT_DEFINITION, // the parsed expression is not of the form "[func_call] = [expression]" or " [variable_name] = [expression]"
CPP_INCORRECT_ARGNUM, // programmatically evaluating math object with incorrect number of arguments
};
static Error unexpected(parsing::tokens::Text token, std::string expression)
{
return Error {UNEXPECTED, token, std::move(expression)};
}
static Error unexpected_end_of_expression(std::string expression)
{
return Error {.type = UNEXPECTED_END_OF_EXPRESSION, .expression = std::move(expression)};
}
static Error wrong_format(parsing::tokens::Text token, std::string expression)
{
return Error {WRONG_FORMAT, token, std::move(expression)};
}
static Error missing(parsing::tokens::Text token, std::string expression)
{
return Error {MISSING, token, std::move(expression)};
}
static Error unkown()
{
return Error {UNKNOWN};
}
static Error undefined_variable(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {UNDEFINED_VARIABLE, tokenTxt, std::move(expression)};
}
static Error undefined_function(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {UNDEFINED_FUNCTION, tokenTxt, std::move(expression)};
}
static Error cpp_incorrect_argnum()
{
return Error {CPP_INCORRECT_ARGNUM};
}
static Error mismatched_fun_args(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {CALLING_FUN_ARG_COUNT_MISMATCH, tokenTxt, std::move(expression)};
}
static Error not_implemented(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {NOT_IMPLEMENTED, tokenTxt, std::move(expression)};
}
static Error empty_expression(std::string expression = {})
{
return Error {.type = EMPTY_EXPRESSION, .expression = std::move(expression)};
}
static Error object_in_invalid_state(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {OBJECT_INVALID_STATE, tokenTxt, std::move(expression)};
}
static Error recursion_depth_overflow()
{
return Error{RECURSION_DEPTH_OVERFLOW};
}
static Error wrong_object_type(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {WRONG_OBJECT_TYPE, tokenTxt, std::move(expression)};
}
static Error name_already_taken(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {NAME_ALREADY_TAKEN, tokenTxt, std::move(expression)};
}
static Error name_already_taken(std::string name)
{
return Error {NAME_ALREADY_TAKEN, {}, std::move(name)};
}
static Error object_not_in_world(parsing::tokens::Text tokenTxt, std::string expression)
{
return Error {OBJECT_NOT_IN_WORLD, tokenTxt, std::move(expression)};
}
static Error object_not_in_world()
{
return Error {OBJECT_NOT_IN_WORLD};
}
static Error not_math_object_definition()
{
return Error {NOT_MATH_OBJECT_DEFINITION};
};
// kind of error
Type type = UNKNOWN;
// on what token
parsing::tokens::Text token = {};
/// @brief full expression where the parsing error is
std::string expression = {};
bool operator == (const Error& other) const = default;
};
}