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
44
ParseError
::
ParseError
(
bool
is_error
,
45
const
std
::
string
&
msg
,
46
const
std
::
string
&
filename
,
47
Idx
line
,
48
Idx
col
) :
49
is_error
(
is_error
),
50
line
(
line
),
column
(
col
),
msg
(
msg
),
filename
(
filename
),
code
(
""
) {}
51
52
ParseError
::
ParseError
(
bool
is_error
,
53
const
std
::
string
&
msg
,
54
const
std
::
string
&
filename
,
55
const
std
::
string
&
code
,
56
Idx
line
,
57
Idx
col
) :
58
is_error
(
is_error
),
59
line
(
line
),
column
(
col
),
msg
(
msg
),
filename
(
filename
),
code
(
code
) {}
60
61
ParseError
::
ParseError
(
const
ParseError
&
err
) {
62
is_error
=
err
.
is_error
;
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
70
ParseError
ParseError
::
operator
=(
const
ParseError
&
err
) {
71
if
(
this
!= &
err
) {
72
is_error
=
err
.
is_error
;
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
{
85
std
::
ostringstream
s
;
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
///
99
std
::
string
ParseError
::
toElegantString
()
const
{
100
if
(
code
.
empty
()) {
101
std
::
ifstream
ifs
(
filename
.
c_str
());
102
103
for
(
Idx
i
= 0;
i
<
line
;
i
++)
104
std
::
getline
(
ifs
,
code
);
105
}
106
107
std
::
ostringstream
s
;
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
116
ParseError
ErrorsContainer
::
error
(
Idx
i
)
const
{
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
132
ErrorsContainer
::
ErrorsContainer
() {
133
error_count
= 0;
134
warning_count
= 0;
135
}
136
137
ErrorsContainer
::
ErrorsContainer
(
const
ErrorsContainer
&
cont
) {
138
error_count
=
cont
.
error_count
;
139
warning_count
=
cont
.
warning_count
;
140
errors
.
clear
();
141
errors
=
cont
.
errors
;
142
}
143
144
ErrorsContainer
ErrorsContainer
::
operator
+(
const
ErrorsContainer
&
cont
)
const
{
145
ErrorsContainer
newCont
;
146
147
newCont
.
error_count
=
this
->
error_count
+
cont
.
error_count
;
148
newCont
.
warning_count
=
this
->
warning_count
+
cont
.
warning_count
;
149
std
::
copy
(
this
->
errors
.
begin
(),
this
->
errors
.
end
(),
newCont
.
errors
.
begin
());
150
std
::
copy
(
cont
.
errors
.
begin
(),
cont
.
errors
.
end
(),
newCont
.
errors
.
end
());
151
152
return
newCont
;
153
}
154
155
ErrorsContainer
ErrorsContainer
::
operator
=(
const
ErrorsContainer
&
cont
) {
156
error_count
=
cont
.
error_count
;
157
warning_count
=
cont
.
warning_count
;
158
errors
.
clear
();
159
errors
=
cont
.
errors
;
160
161
return
*
this
;
162
}
163
164
ErrorsContainer
ErrorsContainer
::
operator
+=(
const
ErrorsContainer
&
cont
) {
165
error_count
+=
cont
.
error_count
;
166
warning_count
+=
cont
.
warning_count
;
167
168
for
(
Idx
i
= 0;
i
<
cont
.
count
();
i
++)
169
errors
.
push_back
(
cont
.
error
(
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
182
void
ErrorsContainer
::
simpleErrorsAndWarnings
(
std
::
ostream
&
o
)
const
{
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
200
void
ErrorsContainer
::
elegantErrorsAndWarnings
(
std
::
ostream
&
o
)
const
{
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
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669