aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
errorsContainer.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Errors container (at least) for parser.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_ERRORS_CONTAINERS_H
29 #define GUM_ERRORS_CONTAINERS_H
30 
31 #include <fstream>
32 #include <iostream>
33 #include <vector>
34 
35 #include <agrum/agrum.h>
36 
37 #include <agrum/tools/core/cocoR/common.h>
38 
39 namespace gum {
40 
41  /**
42  * @class ParseError
43  * @headerfile errorsContainer.h <agrum/tools/core/errorsContainer.h>
44  * @brief This class is used to represent parsing errors for the different
45  * parser implemented in aGrUM.
46  * @ingroup basicstruct_group
47  *
48  */
49  class ParseError {
50  public:
51  // ============================================================================
52  /// @name Class constructors and destructors
53  // ============================================================================
54  /// @{
55 
56  /**
57  * @brief Class constructor.
58  * @param is_error True if this is an error.
59  * @param msg The parsing error message.
60  * @param line The line where the parsing error occured.
61  */
62  ParseError(bool is_error, const std::string& msg, Idx line);
63 
64  /**
65  * @brief Class constructor.
66  * @param is_error If false, then this ParseError is a warning.
67  * @param msg The parsing error message.
68  * @param filename The file where the parsing error occured.
69  * @param line The line where the parsing error occured.
70  * @param col The column where the parsing error occured.
71  */
72  ParseError(bool is_error,
73  const std::string& msg,
74  const std::string& filename,
75  Idx line,
76  Idx col = 0);
77 
78  /**
79  * @brief Class constructor.
80  * @param is_error If false, then this ParseError is a warning.
81  * @param msg The parsing error message.
82  * @param filename The file where the parsing error occured.
83  * @param code The code of the parsing error.
84  * @param line The line where the parsing error occured.
85  * @param col The column where the parsing error occured.
86  */
87  ParseError(bool is_error,
88  const std::string& msg,
89  const std::string& filename,
90  const std::string& code,
91  Idx line,
92  Idx col = 0);
93 
94  /**
95  * @brief Copy constructor.
96  * @param cont The gum::ParseError to copy.
97  */
98  ParseError(const ParseError& cont);
99 
100  /// @}
101  // ============================================================================
102  /// @name Class operator
103  // ============================================================================
104  /// @{
105 
106  /**
107  * @brief Copy operator.
108  * @param cont The gum::ParseError to copy.
109  * @return Returns this gum::ParseError.
110  */
111  ParseError operator=(const ParseError& cont);
112 
113  /// @}
114  // ============================================================================
115  /// @name Class operator
116  // ============================================================================
117  /// @{
118 
119  /// If false, this gum::ParseError is a warning.
120  bool is_error;
121 
122  /// The line of this gum::ParseError.
123  Idx line;
124 
125  /// The column of this gum::ParseError, default is 0.
126  Idx column;
127 
128  /// The gum::ParseError message.
129  std::string msg;
130 
131  /// The file of this gum::ParseError, default is "".
132  std::string filename;
133 
134  /// The code of this gum::ParseError, default is "".
135  mutable std::string code; // default ""
136 
137  /**
138  * @brief Return a std::string representation of this gum::ParseError.
139  * @return Return a std::string representation of this gum::ParseError.
140  */
141  std::string toString() const;
142 
143  /**
144  * @brief Return an elegant std::string representation of this
145  * gum::ParseError.
146  * @return Return an elegant std::string representation of this
147  * gum::ParseError.
148  */
149  std::string toElegantString() const;
150 
151  /// @}
152  };
153 
154  /**
155  * @class ErrorsContainer
156  * @headerfile errorsContainer.h <agrum/tools/core/errorsContainer.h>
157  * @brief This class is used contain and manipulate gum::ParseError.
158  * @ingroup basicstruct_group
159  */
160  class ErrorsContainer {
161  /// The list of gum::ParseError contained in this gum::ErrorsContainer.
162  mutable std::vector< ParseError > errors;
163 
164  public:
165  /// Number of errors detected.
166  Size error_count;
167 
168  // Number of warnings detected.
169  Size warning_count;
170 
171  // ============================================================================
172  /// @name Class Constructor
173  // ============================================================================
174  /// @{
175 
176  /**
177  * @brief Class Constructor.
178  */
179  ErrorsContainer();
180 
181  /**
182  * @brief Copy constructor.
183  * @param cont The ErrorsContainer to copy.
184  */
185  ErrorsContainer(const ErrorsContainer& cont);
186 
187  /// @}
188  // ============================================================================
189  /// @name Accessors / Modifiers
190  // ============================================================================
191  /// @{
192 
193  /**
194  * @brief Add an error object to the container.
195  * @param error The gum::ParseError to add.
196  */
197  void add(ParseError error);
198 
199  /**
200  * @brief Returns the i-th error.
201  * @param i The error to return.
202  * @return Returns the i-th error.
203  * @throw OutOfBounds Raised if there is less than i errors.
204  */
205  ParseError error(Idx i) const;
206 
207  /**
208  * @brief Returns the last added error.
209  * @return Returns the last added error.
210  * @throw OutOfBounds Raised if is no error to return.
211  */
212  ParseError last() const;
213 
214  /**
215  * @brief Adds an error.
216  * @param msg The error's message.
217  * @param filename The error's file.
218  * @param line The error's line.
219  * @param col The error's column.
220  */
221  void addError(const std::string& msg, const std::string& filename, Idx line, Idx col);
222 
223  /**
224  * @brief Adds a warning.
225  * @param msg The warning's message.
226  * @param filename The warning's file.
227  * @param line The warning's line.
228  * @param col The warning's column.
229  */
230  void addWarning(const std::string& msg, const std::string& filename, Idx line, Idx col);
231 
232  /**
233  * @brief Add an exception.
234  * @param msg The exception's message.
235  * @param filename The exception's file.
236  */
237  void addException(const std::string& msg, const std::string& filename);
238 
239  /**
240  * @brief Returns the number of errors and warnings.
241  * @return Returns the number of errors and warnings.
242  */
243  Size count() const;
244 
245  /**
246  * @brief Print errors on output stream.
247  * @param o The output strem to send results.
248  */
249  void syntheticResults(std::ostream& o) const;
250 
251  /**
252  * @brief Print errors on output stream.
253  * @param o The output strem to send results.
254  */
255  void simpleErrors(std::ostream& o) const;
256 
257  /**
258  * @brief Print errors on output stream.
259  * @param o The output strem to send results.
260  */
261  void simpleErrorsAndWarnings(std::ostream& o) const;
262 
263  /**
264  * @brief Print errors on output stream.
265  * @param o The output strem to send results.
266  */
267  void elegantErrors(std::ostream& o) const;
268 
269  /**
270  * @brief Print errors on output stream.
271  * @param o The output strem to send results.
272  */
273  void elegantErrorsAndWarnings(std::ostream& o) const;
274 
275  /// @}
276  // ============================================================================
277  /// @name Coco/R helpers
278  // ============================================================================
279  /// @{
280 
281  /**
282  * @brief For adding errors.
283  * @param filename The error's file.
284  * @param line The error's line.
285  * @param col The error's column.
286  * @param msg The error's message.
287  */
288  void Error(const std::wstring& filename, Idx line, Idx col, const wchar_t* msg);
289 
290  /**
291  * @brief For adding warnings.
292  * @param filename The warning's file.
293  * @param line The warning's line.
294  * @param col The warning's column.
295  * @param msg The warning's message.
296  */
297  void Warning(const std::wstring& filename, Idx line, Idx col, const wchar_t* msg);
298 
299  /**
300  * @brief For adding exceptions.
301  * @param filename The exception's file.
302  * @param msg The exception's message.
303  */
304  void Exception(const std::wstring& filename, const wchar_t* msg);
305 
306  /// @}
307  // ============================================================================
308  /// @name Class operators
309  // ============================================================================
310  /// @{
311 
312  /**
313  * @brief Return the sum of two gum::ErrorsContainer.
314  * @param cont The gum::ErrorsContainer to add.
315  * @return Return the sum of two gum::ErrorsContainer.
316  */
317  ErrorsContainer operator+(const ErrorsContainer& cont) const;
318 
319  /**
320  * @brief Copy Operator.
321  * @param cont The gum::ErrorsContainer to copy.
322  * @return Returns this gum::ErrorsContainer.
323  */
324  ErrorsContainer operator=(const ErrorsContainer& cont);
325 
326  /**
327  * @brief Add the content of a gum::ErrorsContainer to this
328  * gum::ErrorsContainer.
329  * @param cont The gum::ErrorsContainer to add to this.
330  * @return Returns this gum::ErrorsContainer.
331  */
332  ErrorsContainer operator+=(const ErrorsContainer& cont);
333 
334  /// @}
335 
336  }; // ErrorsContainer
337 
338 } // namespace gum
339 
340 /// include the inlined functions if necessary
341 #ifndef GUM_NO_INLINE
342 # include <agrum/tools/core/errorsContainer_inl.h>
343 #endif /* GUM_NO_INLINE */
344 
345 #endif // GUM_ERRORS_CONTAINERS_H