forked from m2osw/csspp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.cpp
More file actions
343 lines (287 loc) · 7.44 KB
/
error.cpp
File metadata and controls
343 lines (287 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// CSS Preprocessor
// Copyright (c) 2015-2019 Made to Order Software Corp. All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/** \file
* \brief Implementation of the CSS Preprocessor error handling.
*
* The library handles errors by printing messages to a standard output
* stream. The functions also count the number of errors and warnings
* that occur while parsing and compiling a source file.
*
* The output can be redirected to your own buffer.
*
* The number of errors can be \em protected by an RAII class so the
* exact same instance of the library can be reused any number of times
* (in case you were to create a GUI that helps debug code quickly,
* possibly inline...)
*
* \code
* // error counts are zero
* csspp::error_count_t count;
* {
* csspp::safe_error_t safe_errors;
*
* // super over simplified... (no proper error handling
* // and probably a bit wrong)
* csspp::position pos;
* csspp::lexer l(input, pos);
* csspp::parser p(l);
* csspp::node root(p.stylesheet());
* csspp::compiler c;
* c.set_root(root);
* c.compile()
*
* // get number of errors before the '}'
* count = csspp::error()->get_error_count();
* }
* // error counts are still zero, they were restored by safe_error_t
* \endcode
*
* \sa \ref lexer_rules
*/
#include "csspp/error.h"
#include <cmath>
#include <iostream>
namespace csspp
{
namespace
{
// the error instance
error *g_error = nullptr;
} // no name namespace
error::error()
: f_position("error.css")
{
}
error & error::instance()
{
// first time, allocate the instance
if(g_error == nullptr)
{
g_error = new error;
}
g_error->reset();
return *g_error;
}
std::ostream & error::get_error_stream() const
{
return *f_error;
}
void error::set_error_stream(std::ostream & err_stream)
{
f_error = &err_stream;
}
void error::set_count_warnings_as_errors(bool warnings_as_errors)
{
f_warnings_as_errors = warnings_as_errors;
}
error_count_t error::get_error_count() const
{
return f_error_count;
}
void error::set_error_count(error_count_t count)
{
f_error_count = count;
}
error_count_t error::get_warning_count() const
{
return f_warning_count;
}
void error::set_warning_count(error_count_t count)
{
f_warning_count = count;
}
void error::set_hide_all(bool hide_all)
{
f_hide_all = hide_all;
}
void error::set_show_debug(bool show_debug)
{
f_show_debug = show_debug;
}
void error::set_verbose(bool status)
{
f_verbose = status;
}
error & error::operator << (position const & pos)
{
f_position = pos;
return *this;
}
error & error::operator << (error_mode_t mode)
{
switch(mode)
{
case error_mode_t::ERROR_DEC:
f_message << std::dec;
break;
case error_mode_t::ERROR_FATAL:
case error_mode_t::ERROR_ERROR:
++f_error_count;
goto print_error;
case error_mode_t::ERROR_WARNING:
if(f_warnings_as_errors)
{
// count warnings just like if they were errors
++f_error_count;
}
else
{
// should we count warnings even if we do not show them?
if(f_hide_all)
{
break;
}
++f_warning_count;
}
goto print_error;
case error_mode_t::ERROR_DEBUG:
if(!f_show_debug)
{
break;
}
#if __cplusplus >= 201700
[[fallthrough]];
#endif
case error_mode_t::ERROR_INFO:
if(f_hide_all)
{
break;
}
print_error:
// print the error now
// (show the page number?)
{
std::ostream * out = f_error ? f_error : &std::cerr;
*out << f_position.get_filename()
<< "(" << f_position.get_line() << "): " << mode << ": "
<< f_message.str()
<< std::endl;
// for debug purposes, otherwise errors are always hidden!
if(f_verbose)
{
std::cerr << "ERROR CONSOLE OUTPUT -- " << f_position.get_filename()
<< "(" << f_position.get_line() << "): " << mode << ": "
<< f_message.str()
<< std::endl;
}
}
break;
case error_mode_t::ERROR_HEX:
f_message << std::hex;
break;
}
return *this;
}
error & error::operator << (std::string const & msg)
{
f_message << msg;
return *this;
}
error & error::operator << (char const * msg)
{
f_message << msg;
return *this;
}
error & error::operator << (int32_t value)
{
f_message << value;
return *this;
}
error & error::operator << (int64_t value)
{
f_message << value;
return *this;
}
error & error::operator << (double value)
{
f_message << value;
return *this;
}
void error::reset()
{
// reset the output message
f_message.str("");
f_message << std::dec;
}
safe_error_t::safe_error_t()
: f_error_count(error::instance().get_error_count())
, f_warning_count(error::instance().get_warning_count())
{
}
safe_error_t::~safe_error_t()
{
error::instance().set_error_count(f_error_count);
error::instance().set_warning_count(f_warning_count);
}
safe_error_stream_t::safe_error_stream_t(std::ostream & err_stream)
: f_error(&error::instance().get_error_stream())
{
error::instance().set_error_stream(err_stream);
}
safe_error_stream_t::~safe_error_stream_t()
{
error::instance().set_error_stream(*f_error);
}
error_happened_t::error_happened_t()
: f_error_count(error::instance().get_error_count())
, f_warning_count(error::instance().get_warning_count())
{
}
bool error_happened_t::error_happened() const
{
return f_error_count != error::instance().get_error_count();
}
bool error_happened_t::warning_happened() const
{
return f_warning_count != error::instance().get_warning_count();
}
} // namespace csspp
std::ostream & operator << (std::ostream & out, csspp::error_mode_t const type)
{
switch(type)
{
case csspp::error_mode_t::ERROR_DEBUG:
out << "debug";
break;
case csspp::error_mode_t::ERROR_DEC:
out << "dec";
break;
case csspp::error_mode_t::ERROR_ERROR:
out << "error";
break;
case csspp::error_mode_t::ERROR_FATAL:
out << "fatal";
break;
case csspp::error_mode_t::ERROR_INFO:
out << "info";
break;
case csspp::error_mode_t::ERROR_HEX:
out << "hex";
break;
case csspp::error_mode_t::ERROR_WARNING:
out << "warning";
break;
}
return out;
}
// Local Variables:
// mode: cpp
// indent-tabs-mode: nil
// c-basic-offset: 4
// tab-width: 4
// End:
// vim: ts=4 sw=4 et