aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
errorsContainer.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
47#include <agrum/agrum.h>
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 ParseError::ParseError(bool is_error, std::string_view msg, Idx line) :
61 is_error(is_error), line(line), column(0), msg(msg), filename(""), code("") {}
62
63 ParseError::ParseError(bool is_error,
64 std::string_view msg,
65 std::string_view filename,
66 Idx line,
67 Idx col) :
68 is_error(is_error), line(line), column(col), msg(msg), filename(filename), code("") {}
69
70 ParseError::ParseError(bool is_error,
71 std::string_view msg,
72 std::string_view filename,
73 std::string_view code,
74 Idx line,
75 Idx col) :
76 is_error(is_error), line(line), column(col), msg(msg), filename(filename), code(code) {}
77
78 ParseError::ParseError(const ParseError& err) {
79 is_error = err.is_error;
80 line = err.line;
81 column = err.column; // default 0
82 msg = err.msg;
83 filename = err.filename; // default ""
84 code = err.code; // default ""
85 }
86
87 ParseError ParseError::operator=(const ParseError& err) {
88 if (this != &err) {
89 is_error = err.is_error;
90 line = err.line;
91 column = err.column; // default 0
92 msg = err.msg;
93 filename = err.filename; // default ""
94 code = err.code; // default ""
95 }
96
97 return *this;
98 }
99
101 std::string ParseError::toString() const {
102 std::string s;
103
104 if (!filename.empty()) s += filename + ":";
105
106 if (line > 0) s += std::format("{}: ", line);
107
108 if (column > 0) s += std::format("{} : ", column);
109
110 s += std::format("{} : {}", (is_error ? "error" : "warning"), msg);
111
112 return s;
113 }
114
116 std::string ParseError::toElegantString() const {
117 if (code.empty()) {
118 std::ifstream ifs(filename.c_str());
119
120 for (Idx i = 0; i < line; i++)
121 std::getline(ifs, code);
122 }
123
124 std::string s = toString() + '\n' + code + '\n';
125
126 if (column > 0) s += std::string(column - 1, ' ') + '^';
127
128 return s;
129 }
130
131 ParseError ErrorsContainer::error(Idx i) const {
132 if (count() > i) return errors[i]; // May throw an error if i >= count().
133 else { GUM_ERROR(OutOfBounds, "Index out of bound.") }
134 }
135
136 ParseError ErrorsContainer::last() const {
137 if (count() > 0) return errors[count() - 1];
138 else { GUM_ERROR(OutOfBounds, "Index out of bound.") }
139 }
140
141 ErrorsContainer::ErrorsContainer() {
142 error_count = 0;
143 warning_count = 0;
144 }
145
146 ErrorsContainer::ErrorsContainer(const ErrorsContainer& cont) {
147 error_count = cont.error_count;
148 warning_count = cont.warning_count;
149 errors.clear();
150 errors = cont.errors;
151 }
152
153 ErrorsContainer ErrorsContainer::operator+(const ErrorsContainer& cont) const {
154 ErrorsContainer newCont;
155
156 newCont.error_count = this->error_count + cont.error_count;
157 newCont.warning_count = this->warning_count + cont.warning_count;
158 std::copy(this->errors.begin(), this->errors.end(), newCont.errors.begin());
159 std::copy(cont.errors.begin(), cont.errors.end(), newCont.errors.end());
160
161 return newCont;
162 }
163
164 ErrorsContainer ErrorsContainer::operator=(const ErrorsContainer& cont) {
165 error_count = cont.error_count;
166 warning_count = cont.warning_count;
167 errors.clear();
168 errors = cont.errors;
169
170 return *this;
171 }
172
173 ErrorsContainer ErrorsContainer::operator+=(const ErrorsContainer& cont) {
174 error_count += cont.error_count;
175 warning_count += cont.warning_count;
176
177 for (Idx i = 0; i < cont.count(); i++)
178 errors.push_back(cont.error(i));
179
180 return *this;
181 }
182
183 void ErrorsContainer::simpleErrors(std::ostream& o) const {
184 if (count() == 0) return;
185
186 for (Idx i = 0; i < count(); i++) {
187 if (error(i).is_error) o << error(i).toString() << std::endl;
188 }
189 }
190
191 void ErrorsContainer::simpleErrorsAndWarnings(std::ostream& o) const {
192 if (count() == 0) return;
193
194 for (Idx i = 0; i < count(); i++)
195 o << error(i).toString() << std::endl;
196 }
197
198 void ErrorsContainer::elegantErrors(std::ostream& o) const {
199 if (count() == 0) return;
200
201 for (Idx i = 0; i < count(); i++) {
202 if (error(i).is_error) {
203 o << error(i).toElegantString();
204 o << std::endl;
205 }
206 }
207 }
208
209 void ErrorsContainer::elegantErrorsAndWarnings(std::ostream& o) const {
210 if (count() == 0) return;
211
212 for (Idx i = 0; i < count(); i++) {
213 o << error(i).toElegantString();
214 o << std::endl;
215 }
216 }
217
218} // namespace gum
219
220#endif // DOXYGEN_SHOULD_SKIP_THIS
Exception : out of bound.
ParseError(bool is_error, std::string_view msg, Idx line)
Class constructor.
Errors container (at least) for parser.
Inlined implementation of the basic methods of ErrorsContainer.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46