1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
8 #include "CharStream.h"
12 /// Do not buffer up the entire char stream. It does keep a small buffer
13 /// for efficiency and also buffers while a mark exists (set by the
14 /// lookahead prediction in parser). "Unbuffered" here refers to fact
15 /// that it doesn't buffer all data, not that's it's on demand loading of char.
16 class ANTLR4CPP_PUBLIC UnbufferedCharStream : public CharStream {
18 /// The name or source of this char stream.
21 UnbufferedCharStream(std::wistream &input);
23 virtual void consume() override;
24 virtual size_t LA(ssize_t i) override;
27 /// Return a marker that we can release later.
29 /// The specific marker value used for this class allows for some level of
30 /// protection against misuse where {@code seek()} is called on a mark or
31 /// {@code release()} is called in the wrong order.
33 virtual ssize_t mark() override;
36 /// Decrement number of markers, resetting buffer if we hit 0. </summary>
37 /// <param name="marker"> </param>
38 virtual void release(ssize_t marker) override;
39 virtual size_t index() override;
42 /// Seek to absolute character index, which might not be in the current
43 /// sliding window. Move {@code p} to {@code index-bufferStartIndex}.
45 virtual void seek(size_t index) override;
46 virtual size_t size() override;
47 virtual std::string getSourceName() const override;
48 virtual std::string getText(const misc::Interval &interval) override;
51 /// A moving window buffer of the data being scanned. While there's a marker,
52 /// we keep adding to buffer. Otherwise, <seealso cref="#consume consume()"/> resets so
53 /// we start filling at index 0 again.
55 #if defined(_MSC_VER) && _MSC_VER == 1900
56 i32string _data; // Custom type for VS 2015.
57 typedef __int32 storage_type;
60 typedef char32_t storage_type;
64 /// 0..n-1 index into <seealso cref="#data data"/> of next character.
66 /// The {@code LA(1)} character is {@code data[p]}. If {@code p == n}, we are
67 /// out of buffered characters.
72 /// Count up with <seealso cref="#mark mark()"/> and down with
73 /// <seealso cref="#release release()"/>. When we {@code release()} the last mark,
74 /// {@code numMarkers} reaches 0 and we reset the buffer. Copy
75 /// {@code data[p]..data[n-1]} to {@code data[0]..data[(n-1)-p]}.
79 /// This is the {@code LA(-1)} character for the current position.
80 size_t _lastChar; // UTF-32
83 /// When {@code numMarkers > 0}, this is the {@code LA(-1)} character for the
84 /// first character in <seealso cref="#data data"/>. Otherwise, this is unspecified.
86 size_t _lastCharBufferStart; // UTF-32
89 /// Absolute character index. It's the index of the character about to be
90 /// read via {@code LA(1)}. Goes from 0 to the number of characters in the
91 /// entire stream, although the stream size is unknown before the end is
94 size_t _currentCharIndex;
96 std::wistream &_input;
99 /// Make sure we have 'want' elements from current position <seealso cref="#p p"/>.
100 /// Last valid {@code p} index is {@code data.length-1}. {@code p+need-1} is
101 /// the char index 'need' elements ahead. If we need 1 element,
102 /// {@code (p+1-1)==p} must be less than {@code data.length}.
104 virtual void sync(size_t want);
107 /// Add {@code n} characters to the buffer. Returns the number of characters
108 /// actually added to the buffer. If the return value is less than {@code n},
109 /// then EOF was reached before {@code n} characters could be added.
111 virtual size_t fill(size_t n);
113 /// Override to provide different source of characters than
114 /// <seealso cref="#input input"/>.
115 virtual char32_t nextChar();
116 virtual void add(char32_t c);
117 size_t getBufferStartIndex() const;
120 void InitializeInstanceFields();
123 } // namespace antlr4