aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
errorsContainer.cpp
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 /** @file
23  * @brief Errors container (at least) for parser
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #include <agrum/agrum.h>
28 
29 #include <agrum/tools/core/errorsContainer.h>
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 
33 /// include the inlined functions if necessary
34 # ifdef GUM_NO_INLINE
35 # include <agrum/tools/core/errorsContainer_inl.h>
36 # endif /* GUM_NO_INLINE */
37 
38 namespace gum {
39 
40  ParseError::ParseError(bool is_error, const std::string& msg, Idx line) :
41  is_error(is_error), line(line), column(0), msg(msg), filename(""), code("") {}
42 
44  const std::string& msg,
45  const std::string& filename,
46  Idx line,
47  Idx col) :
49  line(line), column(col), msg(msg), filename(filename), code("") {}
50 
52  const std::string& msg,
53  const std::string& filename,
54  const std::string& code,
55  Idx line,
56  Idx col) :
59 
62  line = err.line;
63  column = err.column; // default 0
64  msg = err.msg;
65  filename = err.filename; // default ""
66  code = err.code; // default ""
67  }
68 
70  if (this != &err) {
72  line = err.line;
73  column = err.column; // default 0
74  msg = err.msg;
75  filename = err.filename; // default ""
76  code = err.code; // default ""
77  }
78 
79  return *this;
80  }
81 
82  ///
83  std::string ParseError::toString() const {
85 
86  if (!filename.empty()) s << filename << ":";
87 
88  if (line > 0) s << line << ": ";
89 
90  if (column > 0) s << column << " : ";
91 
92  s << (is_error ? "error" : "warning") << " : " << msg;
93 
94  return s.str();
95  }
96 
97  ///
99  if (code.empty()) {
101 
102  for (Idx i = 0; i < line; i++)
103  std::getline(ifs, code);
104  }
105 
107 
108  s << toString() << std::endl << code << std::endl;
109 
110  if (column > 0) s << std::string(column - 1, ' ') << "^";
111 
112  return s.str();
113  }
114 
116  if (count() > i)
117  return errors[i]; // May throw an error if i >= count().
118  else {
119  GUM_ERROR(OutOfBounds, "Index out of bound.")
120  }
121  }
122 
123  ParseError ErrorsContainer::last() const {
124  if (count() > 0)
125  return errors[count() - 1];
126  else {
127  GUM_ERROR(OutOfBounds, "Index out of bound.")
128  }
129  }
130 
132  error_count = 0;
133  warning_count = 0;
134  }
135 
139  errors.clear();
140  errors = cont.errors;
141  }
142 
145 
148  std::copy(this->errors.begin(), this->errors.end(), newCont.errors.begin());
150 
151  return newCont;
152  }
153 
157  errors.clear();
158  errors = cont.errors;
159 
160  return *this;
161  }
162 
166 
167  for (Idx i = 0; i < cont.count(); i++)
169 
170  return *this;
171  }
172 
173  void ErrorsContainer::simpleErrors(std::ostream& o) const {
174  if (count() == 0) return;
175 
176  for (Idx i = 0; i < count(); i++) {
177  if (error(i).is_error) o << error(i).toString() << std::endl;
178  }
179  }
180 
182  if (count() == 0) return;
183 
184  for (Idx i = 0; i < count(); i++)
185  o << error(i).toString() << std::endl;
186  }
187 
188  void ErrorsContainer::elegantErrors(std::ostream& o) const {
189  if (count() == 0) return;
190 
191  for (Idx i = 0; i < count(); i++) {
192  if (error(i).is_error) {
193  o << error(i).toElegantString();
194  o << std::endl;
195  }
196  }
197  }
198 
200  if (count() == 0) return;
201 
202  for (Idx i = 0; i < count(); i++) {
203  o << error(i).toElegantString();
204  o << std::endl;
205  }
206  }
207 
208 } // namespace gum
209 
210 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643