aGrUM 2.3.2
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-2025 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-2025 *
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, const std::string& msg, Idx line) :
61 is_error(is_error), line(line), column(0), msg(msg), filename(""), code("") {}
62
63 ParseError::ParseError(bool is_error,
64 const std::string& msg,
65 const std::string& 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 const std::string& msg,
72 const std::string& filename,
73 const std::string& 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::ostringstream s;
103
104 if (!filename.empty()) s << filename << ":";
105
106 if (line > 0) s << line << ": ";
107
108 if (column > 0) s << column << " : ";
109
110 s << (is_error ? "error" : "warning") << " : " << msg;
111
112 return s.str();
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::ostringstream s;
125
126 s << toString() << std::endl << code << std::endl;
127
128 if (column > 0) s << std::string(column - 1, ' ') << "^";
129
130 return s.str();
131 }
132
133 ParseError ErrorsContainer::error(Idx i) const {
134 if (count() > i) return errors[i]; // May throw an error if i >= count().
135 else { GUM_ERROR(OutOfBounds, "Index out of bound.") }
136 }
137
138 ParseError ErrorsContainer::last() const {
139 if (count() > 0) return errors[count() - 1];
140 else { GUM_ERROR(OutOfBounds, "Index out of bound.") }
141 }
142
143 ErrorsContainer::ErrorsContainer() {
144 error_count = 0;
145 warning_count = 0;
146 }
147
148 ErrorsContainer::ErrorsContainer(const ErrorsContainer& cont) {
149 error_count = cont.error_count;
150 warning_count = cont.warning_count;
151 errors.clear();
152 errors = cont.errors;
153 }
154
155 ErrorsContainer ErrorsContainer::operator+(const ErrorsContainer& cont) const {
156 ErrorsContainer newCont;
157
158 newCont.error_count = this->error_count + cont.error_count;
159 newCont.warning_count = this->warning_count + cont.warning_count;
160 std::copy(this->errors.begin(), this->errors.end(), newCont.errors.begin());
161 std::copy(cont.errors.begin(), cont.errors.end(), newCont.errors.end());
162
163 return newCont;
164 }
165
166 ErrorsContainer ErrorsContainer::operator=(const ErrorsContainer& cont) {
167 error_count = cont.error_count;
168 warning_count = cont.warning_count;
169 errors.clear();
170 errors = cont.errors;
171
172 return *this;
173 }
174
175 ErrorsContainer ErrorsContainer::operator+=(const ErrorsContainer& cont) {
176 error_count += cont.error_count;
177 warning_count += cont.warning_count;
178
179 for (Idx i = 0; i < cont.count(); i++)
180 errors.push_back(cont.error(i));
181
182 return *this;
183 }
184
185 void ErrorsContainer::simpleErrors(std::ostream& o) const {
186 if (count() == 0) return;
187
188 for (Idx i = 0; i < count(); i++) {
189 if (error(i).is_error) o << error(i).toString() << std::endl;
190 }
191 }
192
193 void ErrorsContainer::simpleErrorsAndWarnings(std::ostream& o) const {
194 if (count() == 0) return;
195
196 for (Idx i = 0; i < count(); i++)
197 o << error(i).toString() << std::endl;
198 }
199
200 void ErrorsContainer::elegantErrors(std::ostream& o) const {
201 if (count() == 0) return;
202
203 for (Idx i = 0; i < count(); i++) {
204 if (error(i).is_error) {
205 o << error(i).toElegantString();
206 o << std::endl;
207 }
208 }
209 }
210
211 void ErrorsContainer::elegantErrorsAndWarnings(std::ostream& o) const {
212 if (count() == 0) return;
213
214 for (Idx i = 0; i < count(); i++) {
215 o << error(i).toElegantString();
216 o << std::endl;
217 }
218 }
219
220} // namespace gum
221
222#endif // DOXYGEN_SHOULD_SKIP_THIS
Exception : out of bound.
ParseError(bool is_error, const std::string &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:72
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46