-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmcpp.h
More file actions
400 lines (355 loc) · 11.5 KB
/
rmcpp.h
File metadata and controls
400 lines (355 loc) · 11.5 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#pragma once
#include <functional>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <string>
namespace Util
{
// based on
// trim from start (in place)
template<typename CharT>
void ltrim(std::basic_string<CharT>& str)
{
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int ch)
{
return !std::isspace(ch);
}));
}
// trim from end (in place)
template<typename CharT>
void rtrim(std::basic_string<CharT>& str)
{
str.erase(std::find_if(str.rbegin(), str.rend(), [](int ch)
{
return !std::isspace(ch);
}).base(), str.end());
}
// trim from both ends (in place)
template<typename CharT>
void trim(std::basic_string<CharT>& str)
{
ltrim(str);
rtrim(str);
}
template<typename CharT>
void split(
const std::basic_string<CharT>& str,
const std::basic_string<CharT>& delim,
std::function<void(const std::basic_string<CharT>&)> cb)
{
size_t prevpos;
size_t herepos;
std::basic_string<CharT> token;
prevpos = 0;
herepos = 0;
while(true)
{
herepos = str.find(delim, prevpos);
if(herepos == std::basic_string<CharT>::npos)
{
herepos = str.size();
}
token = str.substr(prevpos, herepos-prevpos);
trim(token);
if (!token.empty())
{
cb(token);
}
prevpos = herepos + delim.size();
if((herepos > str.size()) || (prevpos > str.size()))
{
break;
}
}
}
template<typename CharT>
void escapechar(std::basic_ostream<CharT>& out, int ch, bool wquotes=false)
{
if((ch >= 32) && (ch <= 126))
{
if(ch == '\\')
{
out << CharT('\\') << CharT('\\');
}
else
{
out << CharT(ch);
}
return;
}
else
{
if((ch == CharT('"')) && wquotes)
{
out << CharT('\\') << CharT('"');
return;
}
switch(ch)
{
case 0: out << CharT('\\') << CharT('0'); return;
case 1: out << CharT('\\') << CharT('1'); return;
case '\n': out << CharT('\\') << CharT('n'); return;
case '\r': out << CharT('\\') << CharT('r'); return;
case '\t': out << CharT('\\') << CharT('t'); return;
case '\f': out << CharT('\\') << CharT('f'); return;
}
out
<< CharT('\\')
<< CharT('x')
<< std::setfill('0')
<< std::setw(2)
<< std::uppercase
<< std::hex
<< CharT(ch)
<< std::resetiosflags(std::ios_base::basefield)
;
}
}
template<typename CharT>
void escapestring(std::basic_ostream<CharT>& out, const std::basic_string<CharT>& str, bool wquotes=false)
{
if(wquotes)
{
out << CharT('"');
}
for(CharT ch: str)
{
escapechar(out, ch, wquotes);
}
if(wquotes)
{
out << CharT('"');
}
}
template<typename CharT>
std::basic_ostream<CharT>&
sfprintf(
std::basic_ostream<CharT>& out,
const std::string& format)
{
out << format;
return out;
}
template<typename CharT, typename Type, typename... Targs>
std::basic_ostream<CharT>& sfprintf(
std::basic_ostream<CharT>& out,
const std::string& fmt,
Type value,
Targs... Fargs
)
{
int cd;
size_t i;
for (i=0; i<fmt.size(); i++)
{
if(fmt[i] == '%')
{
cd = fmt[i + 1];
switch(cd)
{
case '%':
out << CharT('%');
break;
case 's':
case 'd':
case 'l':
case 'c':
out << value;
break;
case 'p':
{
std::basic_stringstream<CharT> tmp;
tmp << value;
escapestring(out, tmp.str(), false);
}
break;
case 'q':
{
std::basic_stringstream<CharT> tmp;
tmp << value;
escapestring(out, tmp.str(), true);
}
break;
default:
{
std::stringstream b;
b << "invalid format flag '" << char(cd) << "'";
throw std::runtime_error(b.str());
}
break;
}
auto subst = fmt.substr(i+2, fmt.length());
return sfprintf(out, subst, Fargs...);
}
out << fmt[i];
}
return out;
}
template<typename... Args>
void error(const std::string& fmtstr, Args&&... args)
{
sfprintf(std::cerr, "ERROR: ");
sfprintf(std::cerr, fmtstr, args...);
std::cerr << std::endl;
}
}
class CommentStripper
{
public:
enum State
{
// undefined/default state
CT_UNDEF,
CT_WHITESPACE,
// a single forward slash
CT_FWDSLASH,
// a single backward slash
CT_BCKSLASH,
// a single open parenthesis
CT_OPENPAREN,
// a single close parenthesis
CT_CLOSEPAREN,
/*
* C preprocessor line
* (#((els?)if(n?def)?|else|include|import|warning|error|pragma|line))
*
* this was already a major hack in the ruby version (see old/main.rb),
* and i'm not sure if it's worth implementing.
*/
CT_PREPROC,
// a C++ comment (like this one!)
CT_CPPCOMM,
/* a C comment (like this one!) */
CT_ANSICOMM,
/*
* a Pascal-style comment (* like this! *)
* supporting '{' and '}' is going to be a bit tougher, i think.
* according to freepascal (https://www.freepascal.org/docs-html/ref/refse2.html):
*
* Remark: In TP and Delphi mode, nested comments are not allowed,
* for maximum compatibility with existing code for those compilers.
*
* so comment-nesting is supported-ish, but seriously only -ish.
*/
CT_PASCALCOMM,
// a hash-comment (python, perl, ruby, etc ...) # like this!
// this will NOT WORK with many shell or perl scripts, as it
// does not, nor ever will, support heredocs, et cetera.
CT_HASHCOMM,
};
struct Options
{
//! is RemCom allowed to complain? default: yes
bool use_warningmessages = true;
//! is RemCom allowed to explain? default: no
bool use_debugmessages = false;
//! remove remaining whitespace? default: no
bool remove_emptylines = false;
//! remove C++-style comments? default: yes
bool remove_cppcomments = true;
//! remove ANSI C comments? default: yes
bool remove_ansicomments = true;
//! remove Pascal-style comments? default: no
bool remove_pascalcomments = false;
//! handle #-style comments? default: no (they clash with the preprocessor!)
bool remove_hashcomments = false;
bool do_convertcpp = false;
// infilename is only used for diagnostics
std::string infilename = "<stdin>";
};
using OnCommentCallback = std::function<bool(State, char)>;
private:
// parser options
Options m_opts;
// the input stream handle
std::istream* m_infp;
// current state the parser is in
State m_state;
// the previous character
int m_prevch;
// the current character
int m_currch;
// the next peeked character
int m_peekch;
// current line the parser is looking at
int m_posline;
// current column the parser is looking at
int m_poscol;
// tracks comment nesting levels
int m_pascalnest;
// when encountering a '{' in pascalmode.
bool m_pascalbrace;
// true if we're inside a comment
bool m_incomment;
OnCommentCallback m_oncommentcb;
private:
void initdefaults();
template<typename... Args>
void dbg(const std::string& fmtstr, Args&&... args)
{
if(m_opts.use_debugmessages)
{
Util::sfprintf(std::cerr, ">>>>[%s:%d:%d]: ", m_opts.infilename, m_posline, m_poscol);
Util::sfprintf(std::cerr, fmtstr, args...);
std::cerr << std::endl;
}
}
template<typename... Args>
void warn(const std::string& fmtstr, Args&&... args)
{
if(m_opts.use_warningmessages)
{
Util::sfprintf(std::cerr, "WARNING: [%p:%d:%d]: ", m_opts.infilename, m_posline, m_poscol);
Util::sfprintf(std::cerr, fmtstr, args...);
std::cerr << std::endl;
}
}
bool is_space();
bool is_pascalcomm_begin();
void do_skipemptylines();
void forward_comment(State st, char ch);
void forward_comment(State st, const std::string& str);
public:
/*
* infp has to be a pointer - a reference to the input stream.
* i.e.,
*
* CommentStripper cs(..., &std::cin);
*
* would create CommentStripper with standard input as input stream, whereas
*
* std::ifstream ifs("somefile.cpp", std::ios::in | std::ios::binary);
* if(ifs.good())
* {
* CommentStripper ifs(..., &ifs);
* // alternatively, if CommentStripper is needed as a ref somewhere else:
* //csptr = new CommentStripper(...);
* }
*
* in other words, the input-stream does NOT need to be new'd.
*/
CommentStripper(const Options& opts, std::istream* infp);
/**
* populates m_currchar with the current character in the stream cursor,
* m_prevchar with the prior value of m_currchar, and m_peekch with the
* value returned from peek().
* also advances m_posline and m_poscol, as needed.
* automatically discards carriage-returns.
*/
int more();
/**
* @returns the next character in the input-stream, without advancing
* the stream
*/
int peek();
void onComment(OnCommentCallback cb);
/**
* @param outfp the std::ostream-compatible output-stream to write to.
* @returns true if no errors occured, false otherwise.
*/
bool run(std::ostream& outfp);
};