aGrUM  0.16.0
errorsContainer.cpp
Go to the documentation of this file.
1 
28 #include <agrum/agrum.h>
29 
31 
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 
35 # ifdef GUM_NO_INLINE
37 # endif /* GUM_NO_INLINE */
38 
39 namespace gum {
40 
41  ParseError::ParseError(bool is_error, const std::string& msg, Idx line) :
42  is_error(is_error), line(line), column(0), msg(msg), filename(""), code("") {
43  }
44 
45  ParseError::ParseError(bool is_error,
46  const std::string& msg,
47  const std::string& filename,
48  Idx line,
49  Idx col) :
50  is_error(is_error),
51  line(line), column(col), msg(msg), filename(filename), code("") {}
52 
53  ParseError::ParseError(bool is_error,
54  const std::string& msg,
55  const std::string& filename,
56  const std::string& code,
57  Idx line,
58  Idx col) :
59  is_error(is_error),
60  line(line), column(col), msg(msg), filename(filename), code(code) {}
61 
62  ParseError::ParseError(const ParseError& err) {
63  is_error = err.is_error;
64  line = err.line;
65  column = err.column; // default 0
66  msg = err.msg;
67  filename = err.filename; // default ""
68  code = err.code; // default ""
69  }
70 
71  ParseError ParseError::operator=(const ParseError& err) {
72  if (this != &err) {
73  is_error = err.is_error;
74  line = err.line;
75  column = err.column; // default 0
76  msg = err.msg;
77  filename = err.filename; // default ""
78  code = err.code; // default ""
79  }
80 
81  return *this;
82  }
83 
85  std::string ParseError::toString() const {
86  std::ostringstream s;
87 
88  if (!filename.empty()) s << filename << ":";
89 
90  if (line > 0) s << line << ": ";
91 
92  if (column > 0) s << column << " : ";
93 
94  s << (is_error ? "error" : "warning") << " : " << msg;
95 
96  return s.str();
97  }
98 
100  std::string ParseError::toElegantString() const {
101  if (code.empty()) {
102  std::ifstream ifs(filename.c_str());
103 
104  for (Idx i = 0; i < line; i++)
105  std::getline(ifs, code);
106  }
107 
108  std::ostringstream s;
109 
110  s << toString() << std::endl << code << std::endl;
111 
112  if (column > 0) s << std::string(column - 1, ' ') << "^";
113 
114  return s.str();
115  }
116 
117  ParseError ErrorsContainer::error(Idx i) const {
118  if (count() > i)
119  return errors[i]; // May throw an error if i >= count().
120  else {
121  GUM_ERROR(OutOfBounds, "Index out of bound.");
122  }
123  }
124 
125  ParseError ErrorsContainer::last() const {
126  if (count() > 0)
127  return errors[count() - 1];
128  else {
129  GUM_ERROR(OutOfBounds, "Index out of bound.");
130  }
131  }
132 
133  ErrorsContainer::ErrorsContainer() {
134  error_count = 0;
135  warning_count = 0;
136  }
137 
138  ErrorsContainer::ErrorsContainer(const ErrorsContainer& cont) {
139  error_count = cont.error_count;
140  warning_count = cont.warning_count;
141  errors.clear();
142  errors = cont.errors;
143  }
144 
145  ErrorsContainer ErrorsContainer::operator+(const ErrorsContainer& cont) const {
146  ErrorsContainer newCont;
147 
148  newCont.error_count = this->error_count + cont.error_count;
149  newCont.warning_count = this->warning_count + cont.warning_count;
150  std::copy(this->errors.begin(), this->errors.end(), newCont.errors.begin());
151  std::copy(cont.errors.begin(), cont.errors.end(), newCont.errors.end());
152 
153  return newCont;
154  }
155 
156  ErrorsContainer ErrorsContainer::operator=(const ErrorsContainer& cont) {
157  error_count = cont.error_count;
158  warning_count = cont.warning_count;
159  errors.clear();
160  errors = cont.errors;
161 
162  return *this;
163  }
164 
165  ErrorsContainer ErrorsContainer::operator+=(const ErrorsContainer& cont) {
166  error_count += cont.error_count;
167  warning_count += cont.warning_count;
168 
169  for (Idx i = 0; i < cont.count(); i++)
170  errors.push_back(cont.error(i));
171 
172  return *this;
173  }
174 
175  void ErrorsContainer::simpleErrors(std::ostream& o) const {
176  if (count() == 0) return;
177 
178  for (Idx i = 0; i < count(); i++) {
179  if (error(i).is_error) o << error(i).toString() << std::endl;
180  }
181  }
182 
183  void ErrorsContainer::simpleErrorsAndWarnings(std::ostream& o) const {
184  if (count() == 0) return;
185 
186  for (Idx i = 0; i < count(); i++)
187  o << error(i).toString() << std::endl;
188  }
189 
190  void ErrorsContainer::elegantErrors(std::ostream& o) const {
191  if (count() == 0) return;
192 
193  for (Idx i = 0; i < count(); i++) {
194  if (error(i).is_error) {
195  o << error(i).toElegantString();
196  o << std::endl;
197  }
198  }
199  }
200 
201  void ErrorsContainer::elegantErrorsAndWarnings(std::ostream& o) const {
202  if (count() == 0) return;
203 
204  for (Idx i = 0; i < count(); i++) {
205  o << error(i).toElegantString();
206  o << std::endl;
207  }
208  }
209 
210 } // namespace gum
211 
212 #endif // DOXYGEN_SHOULD_SKIP_THIS
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
ParseError(bool is_error, const std::string &msg, Idx line)
Class constructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void clear()
Clear all data of the calling expression as if it was constructed.
LpExpr operator+(LpExpr &&lhs, const T2 &rhs)
Overload of operator + between anything ( a scalar, a variable or an expression ) and anything except...
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55