/
DefBlockTokeniser.h
461 lines (406 loc) · 12.1 KB
/
DefBlockTokeniser.h
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#pragma once
#include "ParseException.h"
#include <ios>
#include <iostream>
#include <string>
#include <ctype.h>
#include "string/tokeniser.h"
namespace parser
{
/**
* Abstract base class BlockTokeniser. This class inspects a given input block
* or stream and returns definition blocks (including name).
*
* C and C++-style comments are properly ignored.
*/
class BlockTokeniser
{
public:
struct Block
{
// The name of this block
std::string name;
// The block contents (excluding braces)
std::string contents;
void clear()
{
name.clear();
contents.clear();
}
};
/** Destructor */
virtual ~BlockTokeniser() {}
/**
* Test if this DefTokeniser has more blocks to return.
*
* @returns
* true if there are further blocks, false otherwise
*/
virtual bool hasMoreBlocks() = 0;
/**
* Return the next block in the sequence. This function consumes
* the returned block and advances the internal state to the following
* block.
*
* @returns
* A named Block structure.
*
* @pre
* hasMoreBlocks() must be true, otherwise an exception will be thrown.
*/
virtual Block nextBlock() = 0;
};
/* Model of a tokeniser function which splits tokens on whitespace with additional
* protection of quoted content.
*/
class DefBlockTokeniserFunc
{
// Enumeration of states
enum State
{
SEARCHING_NAME, // haven't found anything yet
TOKEN_STARTED, // first non-delimiter character found
SEARCHING_BLOCK, // searching for block opening char
BLOCK_CONTENT, // within a block
FORWARDSLASH, // forward slash found, possible comment coming
COMMENT_EOL, // double-forwardslash comment
COMMENT_DELIM, // inside delimited comment (/*)
STAR // asterisk, possibly indicates end of comment (*/)
} _state;
const char* _delims; // whitespace
const char _blockStartChar; // "{"
const char _blockEndChar; // "}"
// Test if a character is a delimiter
bool isDelim(char c)
{
const char* curDelim = _delims;
while (*curDelim != 0) {
if (*(curDelim++) == c) {
return true;
}
}
return false;
}
public:
// Constructor
DefBlockTokeniserFunc(const char* delims, char blockStartChar, char blockEndChar) :
_state(SEARCHING_NAME),
_delims(delims),
_blockStartChar(blockStartChar),
_blockEndChar(blockEndChar)
{}
/* REQUIRED. Operator() is called by the Tokeniser. This function
* must search for a token between the two iterators next and end, and if
* a token is found, set tok to the token, set next to position to start
* parsing on the next call, and return true.
*/
template<typename InputIterator>
bool operator() (InputIterator& next, const InputIterator& end, BlockTokeniser::Block& tok)
{
// Initialise state, no persistence between calls
_state = SEARCHING_NAME;
// Clear out the token, no guarantee that it is empty
tok.clear();
char ch = '\0';
std::size_t blockLevel = 0;
while (next != end)
{
ch = *next;
switch (_state)
{
case SEARCHING_NAME:
// Ignore delimiters
if (isDelim(ch))
{
++next;
continue;
}
// We have a non-delimiter, examine it
_state = TOKEN_STARTED;
// Fall through
case TOKEN_STARTED:
if (isDelim(ch))
{
// A delimiter indicates the name is complete
_state = SEARCHING_BLOCK;
continue;
}
else if (ch == _blockStartChar)
{
// Some defs don't have whitespaces between name and block
_state = SEARCHING_BLOCK;
continue;
}
else if (ch == '/')
{
// Found a slash, possibly start of comment
_state = FORWARDSLASH;
++next;
continue; // skip slash, will need to add it back if this is not a comment
}
// Token lasts until we find some of the above, append and continue
tok.name += ch;
++next;
continue;
case SEARCHING_BLOCK:
if (isDelim(ch)) {
++next; // keep on searching
continue;
}
else if (ch == _blockStartChar) {
// Found an opening brace
_state = BLOCK_CONTENT;
blockLevel++;
++next;
continue;
}
else if (ch == '/') {
// Forward slash, possible comment start
_state = FORWARDSLASH;
++next;
continue;
}
else {
// Not a delimiter, not an opening brace, must be
// an "extension" for the name
tok.name += ' ';
tok.name += ch;
// Switch back to name
_state = TOKEN_STARTED;
++next;
continue;
}
case BLOCK_CONTENT:
// Check for another opening brace
if (ch == _blockEndChar) {
blockLevel--;
if (blockLevel == 0) {
// End of block content, we're done here,
// don't add this last character either
++next;
return true;
}
else {
// Still within a block, add to contents
tok.contents += ch;
++next;
continue;
}
}
else if (ch == _blockStartChar) {
// another block within this block, ignore this
blockLevel++;
tok.contents += ch;
++next;
continue;
}
else {
tok.contents += ch;
++next;
continue;
}
case FORWARDSLASH:
// If we have a forward slash we may be entering a comment. The forward slash
// will NOT YET have been added to the token, so we must add it manually if
// this proves not to be a comment.
switch (ch) {
case '*':
_state = COMMENT_DELIM;
++next;
continue;
case '/':
_state = COMMENT_EOL;
++next;
continue;
default: // false alarm, add the slash and carry on
_state = TOKEN_STARTED;
tok.name += '/';
// Do not increment next here
continue;
}
case COMMENT_DELIM:
// Inside a delimited comment, we add nothing to the token but check for
// the "*/" sequence.
if (ch == '*') {
_state = STAR;
++next;
continue;
}
else {
++next;
continue; // ignore and carry on
}
case COMMENT_EOL:
// This comment lasts until the end of the line.
if (ch == '\r' || ch == '\n') {
// An EOL comment with non-empty name means searching for block
_state = (tok.name.empty()) ? SEARCHING_NAME : SEARCHING_BLOCK;
++next;
continue;
}
else {
++next;
continue; // do nothing
}
case STAR:
// The star may indicate the end of a delimited comment.
// This state will only be entered if we are inside a
// delimited comment.
if (ch == '/') {
// End of comment
_state = (tok.name.empty()) ? SEARCHING_NAME : SEARCHING_BLOCK;
++next;
continue;
}
else if (ch == '*') {
// Another star, remain in the STAR state in case we
// have a "**/" end of comment.
_state = STAR;
++next;
continue;
}
else {
// No end of comment
_state = COMMENT_DELIM;
++next;
continue;
}
}
}
// Return true if we have found a named block
return !tok.name.empty();
}
};
/**
* Tokenise a DEF file.
*
* This class provides a similar interface to Java's StringTokenizer class. It accepts
* an input stream and provides a simple interface to return the next block in the stream.
* It also protects quoted content and ignores both C and C++ style comments.
*/
template<typename ContainerT>
class BasicDefBlockTokeniser :
public BlockTokeniser
{
private:
// Internal tokeniser and its iterator
typedef string::Tokeniser<DefBlockTokeniserFunc,
std::string::const_iterator,
BlockTokeniser::Block> Tokeniser;
Tokeniser _tok;
Tokeniser::Iterator _tokIter;
public:
/**
* Construct a BasicDefBlockTokeniser with the given input type.
*
* @param str
* The container to tokenise.
*/
BasicDefBlockTokeniser(const ContainerT& str,
const char* delims = " \t\n\v\r",
const char blockStartChar = '{',
const char blockEndChar = '}') :
_tok(str, DefBlockTokeniserFunc(delims, blockStartChar, blockEndChar)),
_tokIter(_tok.getIterator())
{}
/** Test if this StringTokeniser has more blocks to return.
*
* @returns
* true if there are further blocks, false otherwise
*/
bool hasMoreBlocks() override
{
return !_tokIter.isExhausted();
}
/** Return the next token in the sequence. This function consumes
* the returned token and advances the internal state to the following
* token.
*
* @returns
* std::string containing the next token in the sequence.
*
* @pre
* hasMoreTokens() must be true, otherwise an exception will be thrown.
*/
Block nextBlock() override
{
if (hasMoreBlocks())
{
return *(_tokIter++);
}
throw ParseException("BlockTokeniser: no more blocks");
}
};
/**
* Specialisation of DefTokeniser to work with std::istream objects. This is
* needed because an std::istream does not provide begin() and end() methods
* to get an iterator, but needs a separate istream_iterator<> to be constructed
* for it.
*/
template<>
class BasicDefBlockTokeniser<std::istream> :
public BlockTokeniser
{
private:
// Istream iterator type
typedef std::istream_iterator<char> CharStreamIterator;
// Internal tokeniser and its iterator
typedef string::Tokeniser<DefBlockTokeniserFunc,
CharStreamIterator,
BlockTokeniser::Block> Tokeniser;
Tokeniser _tok;
Tokeniser::Iterator _tokIter;
// Helper function to set noskipws on the input stream.
static std::istream& setNoskipws(std::istream& is)
{
is >> std::noskipws;
return is;
}
public:
/**
* Construct a BasicDefBlockTokeniser with the given input stream.
*
* @param str
* The std::istream to tokenise. This is a non-const parameter, since tokens
* will be extracted from the stream.
*/
BasicDefBlockTokeniser(std::istream& str,
const char* delims = " \t\n\v\r",
const char blockStartChar = '{',
const char blockEndChar = '}') :
_tok(CharStreamIterator(setNoskipws(str)), // start iterator
CharStreamIterator(), // end (null) iterator
DefBlockTokeniserFunc(delims, blockStartChar, blockEndChar)),
_tokIter(_tok.getIterator())
{}
/**
* Test if this BlockTokeniser has more blocks to return.
*
* @returns
* true if there are further blocks, false otherwise
*/
bool hasMoreBlocks() override
{
return !_tokIter.isExhausted();
}
/**
* Return the next block in the sequence. This function consumes
* the returned block and advances the internal state to the following
* block.
*
* @returns
* The named block.
*
* @pre
* hasMoreBlocks() must be true, otherwise an exception will be thrown.
*/
Block nextBlock() override
{
if (hasMoreBlocks())
{
return *(_tokIter++);
}
throw ParseException("BlockTokeniser: no more tokens");
}
};
} // namespace parser