summaryrefslogtreecommitdiff
path: root/src/core/common/CharReader.hpp
blob: 0a220ee689de2ba3cd2a8a1e774ed9cce931aebc (plain)
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
    Ousía
    Copyright (C) 2014  Benjamin Paaßen, Andreas Stöckel

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file CharReader.hpp
 *
 * Used within all parsers to read single characters from an underlying stream.
 *
 * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_CHAR_READER_HPP_
#define _OUSIA_CHAR_READER_HPP_

#include <istream>
#include <list>
#include <memory>
#include <vector>

#include "Location.hpp"

namespace ousia {

/**
 * A chunked ring buffer used in CharReader to provide access to an input stream
 * with multiple read cursors. The Buffer automatically expands to the size of
 * the spanned by the read cursors while reusing already allocated memory.
 */
class Buffer {
public:
	/**
	 * Callback function which is called whenever new data is requested from the
	 * input stream.
	 *
	 * @param buf is points a the target memory region.
	 * @param size is the requested number of bytes.
	 * @param userData is a pointer at some user defined data given in the
	 * constructor.
	 * @return the actual number of bytes read. If the result is smaller than
	 * the requested size, this tells the Buffer that the end of the input
	 * stream is reached.
	 */
	using ReadCallback = size_t (*)(char *buf, size_t size, void *userData);

	/**
	 * Handle used to identify a cursor.
	 */
	using CursorId = size_t;

private:
	/**
	 * Number of bytes to request from the input stream. Set to 64 KiB because
	 * this seems to be a nice value for I/O operations according to multiple
	 * sources.
	 */
	static constexpr size_t REQUEST_SIZE = 64 * 1024;

	/**
	 * Number of bytes the buffer guarantees to be capable of looking back
	 * for extracting the current context.
	 */
	static constexpr size_t LOOKBACK_SIZE = 128;

	/**
	 * Type used internally to represent one chunk of memory.
	 */
	using Bucket = std::vector<char>;

	/**
	 * Type used internally to represent a bucket container.
	 */
	using BucketList = std::list<Bucket>;

	/**
	 * Type used internally for representing iterators in the bucket list.
	 */
	using BucketIterator = BucketList::iterator;

	/**
	 * Type used internally to represent a read cursor.
	 */
	struct Cursor {
		/**
		 * Iterator pointing at the current bucket.
		 */
		BucketIterator bucket;

		/**
		 * Index of the bucket relative to the start bucket.
		 */
		size_t bucketIdx;

		/**
		 * Current offset within that bucket.
		 */
		size_t bucketOffs;
	};

	/**
	 * List of buckets containing the buffered memory.
	 */
	BucketList buckets;

	/**
	 * List of cursors used to access the memory. Note that cursors can be
	 * marked as inactive and reused lateron (to avoid having to resize the
	 * vector).
	 */
	std::vector<Cursor> cursors;

	/**
	 * Bitfield specifying which of the cursors is actually valid.
	 */
	std::vector<bool> alive;

	/**
	 * Function to be called whenever new data is needed. Set to nullptr if the
	 * Buffer is not backed by an input stream.
	 */
	const ReadCallback callback;

	/**
	 * User data given in the constructor.
	 */
	void *userData;

	/**
	 * Set to true if the input stream is at its end.
	 */
	bool reachedEnd;

	/**
	 * Iterator pointing at the current start bucket.
	 */
	BucketIterator startBucket;

	/**
	 * Iterator pointing at the last bucket.
	 */
	BucketIterator endBucket;

	/**
	 * Byte offset of the start bucket relative to the beginning of the stream.
	 */
	size_t startOffset;

	/**
	 * Points at the smallest possible available cursor index, yet does not
	 * guarantee that this cursor index actuall is free.
	 */
	CursorId firstDead;

	/**
	 * Advances the bucket iterator, cares about wrapping around in the ring.
	 */
	void advance(BucketIterator &it);

	/**
	 * Advances the bucket iterator, cares about wrapping around in the ring.
	 */
	void advance(BucketList::const_iterator &it) const;

	/**
	 * Internally used to find the next free cursor in the cursors vector. The
	 * cursor is marked as active.
	 *
	 * @return the next free cursor index.
	 */
	CursorId nextCursor();

	/**
	 * Returns a reference at the next bucket into which data should be
	 * inserted.
	 *
	 * @return a bucket into which the data can be inserted.
	 */
	Bucket &nextBucket();

	/**
	 * Reads data from the input stream and places it in the next free buffer.
	 */
	void stream();

	/**
	 * Moves the given cursor forward.
	 */
	size_t moveForward(CursorId cursor, size_t relativeOffs);

	/**
	 * Moves the given cursor backward.
	 */
	size_t moveBackward(CursorId cursor, size_t relativeOffs);

	/**
	 * Reads a character from the current cursor position and optionally
	 * advances.
	 */
	bool fetchCharacter(CursorId cursor, char &c, bool incr);

public:
	/**
	 * Intializes the Buffer with a reference to a ReadCallback that is used
	 * to fetch data from an underlying input stream.
	 *
	 * @param callback is the function that will be called whenever data is read
	 * from the ring buffer and the buffer does not hold enough data to fulfill
	 * this read request.
	 * @param userData is a pointer to user defined data which will be passed to
	 * the callback function.
	 */
	Buffer(ReadCallback callback, void *userData);

	/**
	 * Initializes the Buffer with a reference to an std::istream from which
	 * data will be read.
	 *
	 * @param istream is the input stream from which the data should be read.
	 */
	Buffer(std::istream &istream);

	/**
	 * Initializes the Buffer with the contents of the given string, after
	 * this operation the Buffer has a fixed size.
	 *
	 * @param str is the string containing the data that should be copied into
	 * the ring buffer.
	 */
	Buffer(const std::string &str);

#ifndef NDEBUG
	/**
	 * Destructor of the Buffer class. Makes sure that all cursors have been
	 * freed.
	 */
	~Buffer();
#endif

	// No copy
	Buffer(const Buffer &) = delete;

	// No assign
	Buffer &operator=(const Buffer &) = delete;

	/**
	 * Creates a new read cursor positioned at the smallest possible position
	 * in the ring buffer.
	 */
	CursorId createCursor();

	/**
	 * Creates a new read cursor positioned at the same position as the given
	 * read cursor.
	 *
	 * @param ref is the read cursor that should be used as reference for the
	 * new read cursor.
	 */
	CursorId createCursor(CursorId ref);

	/**
	 * Copies the position of one cursor to another cursor.
	 *
	 * @param from is the cursor id of which the position should be copied.
	 * @param to is the cursor id to which the position should be copied.
	 */
	void copyCursor(CursorId from, CursorId to);

	/**
	 * Deletes the cursor with the given id. The cursor may no longer be used
	 * after this function has been called.
	 *
	 * @param cursor is the id of the cursor that should be freed.
	 */
	void deleteCursor(CursorId cursor);

	/**
	 * Moves a cursor by offs bytes. Note that moving backwards is theoretically
	 * limited by the LOOKBACK_SIZE of the Buffer, practically it will most
	 * likely be limited by the REQUEST_SIZE, so you can got at most 64 KiB
	 * backwards.
	 *
	 * @param cursor is the cursor that should be moved.
	 * @param relativeOffs is a positive or negative integer number specifying
	 * the number of bytes the cursor should be moved forward (positive numbers)
	 * or backwards (negative numbers).
	 * @return the actual number of bytes the cursor was moved.
	 */
	ssize_t moveCursor(CursorId cursor, ssize_t relativeOffs);

	/**
	 * Moves the cursor to the given position.
	 *
	 * @param cursor is the cursor that should be moved.
	 * @param offs is the offset to which the cursor should be moved.
	 * @return the actual location that was reached.
	 */
	size_t seekCursor(CursorId cursor, size_t offs);

	/**
	 * Returns the current byte offset of the given cursor relative to the
	 * beginning of the stream.
	 *
	 * @param cursor is the cursor for which the byte offset relative to the
	 * beginning of the stream should be returned.
	 * @return the number of bytes since the beginning of the stream for the
	 * given cursor.
	 */
	size_t offset(CursorId cursor) const;

	/**
	 * Returns true if the given cursor currently is at the end of the stream.
	 *
	 * @param cursor is the cursor for which the atEnd flag should be returned.
	 * @return true if the there are no more bytes for this cursor. If false
	 * is returned, this means that there may be more bytes in the stream,
	 * nevertheless the end of the stream may be hit once the next read function
	 * is called.
	 */
	bool atEnd(CursorId cursor) const;

	/**
	 * Reads a single character from the ring buffer from the given cursor and
	 * moves to the next character.
	 *
	 * @param cursor specifies the cursor from which the data should be read.
	 * The cursor will be advanced by one byte.
	 * @param c is the character into which the data needs to be read.
	 * @return true if a character was read, false if the end of the stream has
	 * been reached.
	 */
	bool read(CursorId cursor, char &c);

	/**
	 * Returns a single character from the ring buffer from the current cursor
	 * position and stays at that position.
	 *
	 * @param cursor specifies the cursor from which the data should be read.
	 * The cursor will be advanced by one byte.
	 * @param c is the character into which the data needs to be read.
	 * @return true if a character could be fetched, false if the end of the
	 * stream has been reached.
	 */
	bool fetch(CursorId cursor, char &c);
};

// Forward declaration
class CharReaderFork;

/**
 * Used within parsers for convenient access to single characters in an input
 * stream or buffer. It allows reading and peeking single characters from a
 * buffer. Additionally it contains an internal state machine that handles the
 * detection of linebreaks and converts these to a single '\n'.
 */
class CharReader {
private:
	/**
	 * Substitutes "\r", "\n\r", "\r\n" with a single "\n".
	 *
	 * @param cursor is the cursor from which the character should be read.
	 * @param c a reference to the character that should be written.
	 * @return true if another character needs to be read.
	 */
	bool substituteLinebreaks(Buffer::CursorId &cursor, char &c);

	/**
	 * Reads a single character from the given cursor.
	 *
	 * @param cursor is the cursor from which the character should be read.
	 * @param c a reference to the character that should be written.
	 * @return true if a character was read, false if the end of the stream has
	 * been reached.
	 */
	bool readAtCursor(Buffer::CursorId &cursor, char &c);

protected:
	/**
	 * Reference pointing at the underlying buffer.
	 */
	std::shared_ptr<Buffer> buffer;

	/**
	 * Cursor used for reading.
	 */
	Buffer::CursorId readCursor;

	/**
	 * Cursor used for peeking.
	 */
	Buffer::CursorId peekCursor;

	/**
	 * Set to true as long the underlying Buffer cursor is at the same position
	 * for the read and the peek cursor. This is only used for optimization
	 * purposes and makes consecutive reads a bit faster.
	 */
	bool coherent;

	/**
	 * Id of the underlying source file.
	 */
	SourceId sourceId;

	/**
	 * Offset to be added to the underlying buffer byte positions.
	 */
	size_t offs;

	/**
	 * Protected constructor of the CharReader base class. Creates new read
	 * and peek cursors for the given buffer.
	 *
	 * @param buffer is a reference to the underlying Buffer class responsible
	 * for allowing to read from a single input stream from multiple locations.
	 * @param sourceId is the ID of the underlying source file.
	 * @param offs is the byte offset at which the char reader should start
	 * counting.
	 */
	CharReader(std::shared_ptr<Buffer> buffer, SourceId sourceId, size_t offs);

public:
	/**
	 * Creates a new CharReader instance from a string.
	 *
	 * @param str is a string containing the input data.
	 * @param sourceId is the ID of the underlying source file.
	 * @param offs is the byte offset at which the char reader should start
	 * counting.
	 */
	CharReader(const std::string &str, SourceId sourceId = InvalidSourceId,
	           size_t offs = 0);

	/**
	 * Creates a new CharReader instance for an input stream.
	 *
	 * @param istream is the input stream from which incomming data should be
	 * read.
	 * @param sourceId is the ID of the underlying source file.
	 * @param offs is the byte offset at which the char reader should start
	 * counting.
	 */
	CharReader(std::istream &istream, SourceId sourceId = InvalidSourceId,
	           size_t offs = 0);

	/**
	 * Deletes the used cursors from the underlying buffer instance.
	 */
	~CharReader();

	// No copy
	CharReader(const CharReader &) = delete;

	// No assign
	CharReader &operator=(const CharReader &) = delete;

	/**
	 * Move constructor.
	 */
	CharReader(CharReader &&) noexcept;

	/**
	 * Peeks a single character. If called multiple times, returns the
	 * character after the previously peeked character.
	 *
	 * @param c is a reference to the character to which the result should be
	 * written.
	 * @return true if the character was successfully read, false if there are
	 * no more characters to be read in the buffer.
	 */
	bool peek(char &c);

	/**
	 * Reads a character from the input data. If "peek" was called
	 * beforehand resets the peek pointer.
	 *
	 * @param c is a reference to the character to which the result should be
	 * written.
	 * @return true if the character was successfully read, false if there are
	 * no more characters to be read in the buffer.
	 */
	bool read(char &c);

	/**
	 * Returns the current character at the read cursor without advancing it.
	 *
	 * @param c is a reference to the character into which the result should be
	 * written.
	 * @return true if the operation was successful, false if the cursor is at
	 * the end of the file.
	 */
	bool fetch(char &c);

	/**
	 * Returns the current character at the peek cursor without advancing it.
	 *
	 * @param c is a reference to the character into which the result should be
	 * written.
	 * @return true if the operation was successful, false if the cursor is at
	 * the end of the file.
	 */
	bool fetchPeek(char &c);

	/**
	 * Peeks a character, checks whether this character equals the given
	 * character -- and if yes -- consumes the peek, otherwise resets it.
	 *
	 * @param c is the character that is expected.
	 * @return true if this character is actually next.
	 */
	bool expect(char c);

	/**
	 * Resets the peek pointer to the "read" pointer.
	 */
	void resetPeek();

	/**
	 * Advances the read pointer to the peek pointer -- so if the "peek"
	 * function was called, "read" will now return the character after
	 * the last peeked character.
	 */
	void consumePeek();

	/**
	 * Moves the read cursor to the next non-whitespace character. Returns
	 * false, if the end of the stream was reached.
	 *
	 * @return false if the end of the stream was reached, false othrwise.
	 */
	bool consumeWhitespace();

	/**
	 * Creates a new CharReader located at the same position as this CharReader
	 * instance, yet the new CharReader can be used independently of this
	 * CharReader. Use the "commit" function of the returned CharReader to
	 * copy the state of the forked CharReaderFork to this CharReader.
	 *
	 * @return a CharReaderFork instance positioned at the same location as this
	 * CharReader instance.
	 */
	CharReaderFork fork();

	/**
	 * Reads raw data from the CharReader without any processing. Data is always
	 * read from the read cursor.
	 *
	 * @param buf is the target memory buffer.
	 * @param size is the number of bytes to be read.
	 * @return the number of bytes read.
	 */
	size_t readRaw(char *buf, size_t size);

	/**
	 * Moves read and peek cursor to the requested offset. Returns the offset
	 * that was actually reached.
	 *
	 * @param requestedOffset is the requested offset. This offset may no longer
	 * be reachable by the CharReader.
	 * @return the actually reached offset. The operation was successful, if
	 * the requested and reached offset are equal.
	 */
	size_t seek(size_t requestedOffset);

	/**
	 * Moves the peek cursor to the requested offset. Returns the offse that wa
	 * actually reached.
	 *
	 * @param requestedOffset is the requested offset. This offset may no longer
	 * be reachable by the CharReader.
	 * @return the actually reached offset. The operation was successful, if
	 * the requested and reached offset are equal.
	 */
	size_t seekPeekCursor(size_t requestedOffset);

	/**
	 * Returns true if there are no more characters as the stream was closed.
	 *
	 * @return true if there is no more data.
	 */
	bool atEnd() const;

	/**
	 * Returns the offset of the read cursor in bytes.
	 *
	 * @return the offset of the read cursor in bytes.
	 */
	size_t getOffset() const;

	/**
	 * Returns the offset of the peek cursor in bytes.
	 *
	 * @return the offset of the peek cursor in bytes.
	 */
	size_t getPeekOffset() const;

	/**
	 * Returns a SourceLocation object describing the exact position (including
	 * the source file) of the read cursor.
	 *
	 * @return a SourceLocation object at the position of the current read
	 * cursor.
	 */
	SourceLocation getLocation() const;

	/**
	 * Returns a SourceLocation object starting at the given start position and
	 * ending at the exact position (including the source file) of the read
	 * cursor.
	 *
	 * @return a SourceLocation object at the position of the current read
	 * cursor.
	 */
	SourceLocation getLocation(SourcePosition start) const;

	/**
	 * Returns the current SourceId which describes the Resource on which the
	 * CharReader is currently working.
	 *
	 * @return the current SourceId.
	 */
	SourceId getSourceId() const;
};

/**
 * A CharReaderFork is returned whenever the "fork" function of the CharReader
 * class is used. Its "commit" function can be used to move the underlying
 * CharReader instance to the location of the CharReaderFork instance. Otherwise
 * the read location of the underlying CharReader is left unchanged.
 */
class CharReaderFork : public CharReader {
private:
	friend CharReader;

	/**
	 * The reader cursor of the underlying CharReader instance.
	 */
	Buffer::CursorId parentReadCursor;

	/**
	 * The peek cursor of the underlying CharReader instance.
	 */
	Buffer::CursorId parentPeekCursor;

	/**
	 * Constructor of the CharReaderFork class.
	 *
	 * @param buffer is a reference at the parent Buffer instance.
	 * @param parentPeekCursor is a reference at the parent read cursor.
	 * @param parentPeekCursor is a reference at the parent peek cursor.
	 * @param location is the current location.
	 * @param coherent specifies whether the char reader cursors are initialized
	 * coherently.
	 */
	CharReaderFork(std::shared_ptr<Buffer> buffer,
	               Buffer::CursorId parentReadCursor,
	               Buffer::CursorId parentPeekCursor, SourceId sourceId,
	               size_t offs, bool coherent);

public:
	/**
	 * Moves the read and peek cursor of the parent CharReader to the location
	 * of the read and peek cursor in the fork.
	 */
	void commit();
};
}

#endif /* _OUSIA_CHAR_READER_HPP_ */