aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
errorsContainer.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
222  const std::string& filename,
223  Idx line,
224  Idx col);
225 
226  /**
227  * @brief Adds a warning.
228  * @param msg The warning's message.
229  * @param filename The warning's file.
230  * @param line The warning's line.
231  * @param col The warning's column.
232  */
233  void addWarning(const std::string& msg,
234  const std::string& filename,
235  Idx line,
236  Idx col);
237 
238  /**
239  * @brief Add an exception.
240  * @param msg The exception's message.
241  * @param filename The exception's file.
242  */
243  void addException(const std::string& msg, const std::string& filename);
244 
245  /**
246  * @brief Returns the number of errors and warnings.
247  * @return Returns the number of errors and warnings.
248  */
249  Size count() const;
250 
251  /**
252  * @brief Print errors on output stream.
253  * @param o The output strem to send results.
254  */
255  void syntheticResults(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 simpleErrors(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 simpleErrorsAndWarnings(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 elegantErrors(std::ostream& o) const;
274 
275  /**
276  * @brief Print errors on output stream.
277  * @param o The output strem to send results.
278  */
279  void elegantErrorsAndWarnings(std::ostream& o) const;
280 
281  /// @}
282  // ============================================================================
283  /// @name Coco/R helpers
284  // ============================================================================
285  /// @{
286 
287  /**
288  * @brief For adding errors.
289  * @param filename The error's file.
290  * @param line The error's line.
291  * @param col The error's column.
292  * @param msg The error's message.
293  */
294  void
295  Error(const std::wstring& filename, Idx line, Idx col, const wchar_t* msg);
296 
297  /**
298  * @brief For adding warnings.
299  * @param filename The warning's file.
300  * @param line The warning's line.
301  * @param col The warning's column.
302  * @param msg The warning's message.
303  */
304  void Warning(const std::wstring& filename,
305  Idx line,
306  Idx col,
307  const wchar_t* msg);
308 
309  /**
310  * @brief For adding exceptions.
311  * @param filename The exception's file.
312  * @param msg The exception's message.
313  */
314  void Exception(const std::wstring& filename, const wchar_t* msg);
315 
316  /// @}
317  // ============================================================================
318  /// @name Class operators
319  // ============================================================================
320  /// @{
321 
322  /**
323  * @brief Return the sum of two gum::ErrorsContainer.
324  * @param cont The gum::ErrorsContainer to add.
325  * @return Return the sum of two gum::ErrorsContainer.
326  */
327  ErrorsContainer operator+(const ErrorsContainer& cont) const;
328 
329  /**
330  * @brief Copy Operator.
331  * @param cont The gum::ErrorsContainer to copy.
332  * @return Returns this gum::ErrorsContainer.
333  */
334  ErrorsContainer operator=(const ErrorsContainer& cont);
335 
336  /**
337  * @brief Add the content of a gum::ErrorsContainer to this
338  * gum::ErrorsContainer.
339  * @param cont The gum::ErrorsContainer to add to this.
340  * @return Returns this gum::ErrorsContainer.
341  */
342  ErrorsContainer operator+=(const ErrorsContainer& cont);
343 
344  /// @}
345 
346  }; // ErrorsContainer
347 
348 } // namespace gum
349 
350 /// include the inlined functions if necessary
351 #ifndef GUM_NO_INLINE
352 # include <agrum/tools/core/errorsContainer_inl.h>
353 #endif /* GUM_NO_INLINE */
354 
355 #endif // GUM_ERRORS_CONTAINERS_H