aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
errorsContainer.cpp
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 /** @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  }
43 
45  const std::string& msg,
46  const std::string& filename,
47  Idx line,
48  Idx col) :
50  line(line), column(col), msg(msg), filename(filename), code("") {}
51 
53  const std::string& msg,
54  const std::string& filename,
55  const std::string& code,
56  Idx line,
57  Idx col) :
60 
63  line = err.line;
64  column = err.column; // default 0
65  msg = err.msg;
66  filename = err.filename; // default ""
67  code = err.code; // default ""
68  }
69 
71  if (this != &err) {
73  line = err.line;
74  column = err.column; // default 0
75  msg = err.msg;
76  filename = err.filename; // default ""
77  code = err.code; // default ""
78  }
79 
80  return *this;
81  }
82 
83  ///
84  std::string ParseError::toString() const {
86 
87  if (!filename.empty()) s << filename << ":";
88 
89  if (line > 0) s << line << ": ";
90 
91  if (column > 0) s << column << " : ";
92 
93  s << (is_error ? "error" : "warning") << " : " << msg;
94 
95  return s.str();
96  }
97 
98  ///
100  if (code.empty()) {
102 
103  for (Idx i = 0; i < line; i++)
104  std::getline(ifs, code);
105  }
106 
108 
109  s << toString() << std::endl << code << std::endl;
110 
111  if (column > 0) s << std::string(column - 1, ' ') << "^";
112 
113  return s.str();
114  }
115 
117  if (count() > i)
118  return errors[i]; // May throw an error if i >= count().
119  else {
120  GUM_ERROR(OutOfBounds, "Index out of bound.");
121  }
122  }
123 
124  ParseError ErrorsContainer::last() const {
125  if (count() > 0)
126  return errors[count() - 1];
127  else {
128  GUM_ERROR(OutOfBounds, "Index out of bound.");
129  }
130  }
131 
133  error_count = 0;
134  warning_count = 0;
135  }
136 
140  errors.clear();
141  errors = cont.errors;
142  }
143 
146 
149  std::copy(this->errors.begin(), this->errors.end(), newCont.errors.begin());
151 
152  return newCont;
153  }
154 
158  errors.clear();
159  errors = cont.errors;
160 
161  return *this;
162  }
163 
167 
168  for (Idx i = 0; i < cont.count(); i++)
170 
171  return *this;
172  }
173 
174  void ErrorsContainer::simpleErrors(std::ostream& o) const {
175  if (count() == 0) return;
176 
177  for (Idx i = 0; i < count(); i++) {
178  if (error(i).is_error) o << error(i).toString() << std::endl;
179  }
180  }
181 
183  if (count() == 0) return;
184 
185  for (Idx i = 0; i < count(); i++)
186  o << error(i).toString() << std::endl;
187  }
188 
189  void ErrorsContainer::elegantErrors(std::ostream& o) const {
190  if (count() == 0) return;
191 
192  for (Idx i = 0; i < count(); i++) {
193  if (error(i).is_error) {
194  o << error(i).toElegantString();
195  o << std::endl;
196  }
197  }
198  }
199 
201  if (count() == 0) return;
202 
203  for (Idx i = 0; i < count(); i++) {
204  o << error(i).toElegantString();
205  o << std::endl;
206  }
207  }
208 
209 } // namespace gum
210 
211 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669