aGrUM  0.13.2
errorsContainer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #include <agrum/agrum.h>
26 
28 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
32 # ifdef GUM_NO_INLINE
34 # endif /* GUM_NO_INLINE */
35 
36 namespace gum {
37 
38  ParseError::ParseError(bool is_error, const std::string& msg, Idx line) :
39  is_error(is_error), line(line), column(0), msg(msg), filename(""), code("") {
40  }
41 
42  ParseError::ParseError(bool is_error,
43  const std::string& msg,
44  const std::string& filename,
45  Idx line,
46  Idx col) :
47  is_error(is_error),
48  line(line), column(col), msg(msg), filename(filename), code("") {}
49 
50  ParseError::ParseError(bool is_error,
51  const std::string& msg,
52  const std::string& filename,
53  const std::string& code,
54  Idx line,
55  Idx col) :
56  is_error(is_error),
57  line(line), column(col), msg(msg), filename(filename), code(code) {}
58 
59  ParseError::ParseError(const ParseError& err) {
60  is_error = err.is_error;
61  line = err.line;
62  column = err.column; // default 0
63  msg = err.msg;
64  filename = err.filename; // default ""
65  code = err.code; // default ""
66  }
67 
68  ParseError ParseError::operator=(const ParseError& err) {
69  if (this != &err) {
70  is_error = err.is_error;
71  line = err.line;
72  column = err.column; // default 0
73  msg = err.msg;
74  filename = err.filename; // default ""
75  code = err.code; // default ""
76  }
77 
78  return *this;
79  }
80 
82  std::string ParseError::toString() const {
83  std::ostringstream s;
84 
85  if (!filename.empty()) s << filename << ":";
86 
87  if (line > 0) s << line << ": ";
88 
89  if (column > 0) s << column << " : ";
90 
91  s << (is_error ? "error" : "warning") << " : " << msg;
92 
93  return s.str();
94  }
95 
97  std::string ParseError::toElegantString() const {
98  if (code.empty()) {
99  std::ifstream ifs(filename.c_str());
100 
101  for (Idx i = 0; i < line; i++)
102  std::getline(ifs, code);
103  }
104 
105  std::ostringstream s;
106 
107  s << toString() << std::endl << code << std::endl;
108 
109  if (column > 0) s << std::string(column - 1, ' ') << "^";
110 
111  return s.str();
112  }
113 
114  ParseError ErrorsContainer::error(Idx i) const {
115  if (count() > i)
116  return errors[i]; // May throw an error if i >= count().
117  else {
118  GUM_ERROR(OutOfBounds, "Index out of bound.");
119  }
120  }
121 
122  ParseError ErrorsContainer::last() const {
123  if (count() > 0)
124  return errors[count() - 1];
125  else {
126  GUM_ERROR(OutOfBounds, "Index out of bound.");
127  }
128  }
129 
130  ErrorsContainer::ErrorsContainer() {
131  error_count = 0;
132  warning_count = 0;
133  }
134 
135  ErrorsContainer::ErrorsContainer(const ErrorsContainer& cont) {
136  error_count = cont.error_count;
137  warning_count = cont.warning_count;
138  errors.clear();
139  errors = cont.errors;
140  }
141 
142  ErrorsContainer ErrorsContainer::operator+(const ErrorsContainer& cont) const {
143  ErrorsContainer newCont;
144 
145  newCont.error_count = this->error_count + cont.error_count;
146  newCont.warning_count = this->warning_count + cont.warning_count;
147  std::copy(this->errors.begin(), this->errors.end(), newCont.errors.begin());
148  std::copy(cont.errors.begin(), cont.errors.end(), newCont.errors.end());
149 
150  return newCont;
151  }
152 
153  ErrorsContainer ErrorsContainer::operator=(const ErrorsContainer& cont) {
154  error_count = cont.error_count;
155  warning_count = cont.warning_count;
156  errors.clear();
157  errors = cont.errors;
158 
159  return *this;
160  }
161 
162  ErrorsContainer ErrorsContainer::operator+=(const ErrorsContainer& cont) {
163  error_count += cont.error_count;
164  warning_count += cont.warning_count;
165 
166  for (Idx i = 0; i < cont.count(); i++)
167  errors.push_back(cont.error(i));
168 
169  return *this;
170  }
171 
172  void ErrorsContainer::simpleErrors(std::ostream& o) const {
173  if (count() == 0) return;
174 
175  for (Idx i = 0; i < count(); i++) {
176  if (error(i).is_error) o << error(i).toString() << std::endl;
177  }
178  }
179 
180  void ErrorsContainer::simpleErrorsAndWarnings(std::ostream& o) const {
181  if (count() == 0) return;
182 
183  for (Idx i = 0; i < count(); i++)
184  o << error(i).toString() << std::endl;
185  }
186 
187  void ErrorsContainer::elegantErrors(std::ostream& o) const {
188  if (count() == 0) return;
189 
190  for (Idx i = 0; i < count(); i++) {
191  if (error(i).is_error) {
192  o << error(i).toElegantString();
193  o << std::endl;
194  }
195  }
196  }
197 
198  void ErrorsContainer::elegantErrorsAndWarnings(std::ostream& o) const {
199  if (count() == 0) return;
200 
201  for (Idx i = 0; i < count(); i++) {
202  o << error(i).toElegantString();
203  o << std::endl;
204  }
205  }
206 
207 } // namespace gum
208 
209 #endif // DOXYGEN_SHOULD_SKIP_THIS
Inlined implementation of the basic methods of ErrorsContainer.
Errors container (at least) for parser.
ParseError(bool is_error, const std::string &msg, Idx line)
Class constructor.
gum is the global namespace for all aGrUM entities
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...
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66