Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions runtime/Cpp/runtime/src/UnbufferedCharStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ using namespace antlrcpp;
using namespace antlr4;
using namespace antlr4::misc;

UnbufferedCharStream::UnbufferedCharStream(std::wistream &input) : _input(input) {
InitializeInstanceFields();

UnbufferedCharStream::UnbufferedCharStream(std::wistream &input)
: _p(0), _numMarkers(0), _lastChar(0), _lastCharBufferStart(0), _currentCharIndex(0), _input(input) {
// The vector's size is what used to be n in Java code.
fill(1); // prime
}
Expand Down Expand Up @@ -74,9 +73,7 @@ size_t UnbufferedCharStream::fill(size_t n) {
}

char32_t UnbufferedCharStream::nextChar() {
wchar_t result = 0;
_input >> result;
return result;
return _input.get();
}

void UnbufferedCharStream::add(char32_t c) {
Expand All @@ -101,7 +98,7 @@ size_t UnbufferedCharStream::LA(ssize_t i) {
return EOF;
}

if (_data[static_cast<size_t>(index)] == 0xFFFF) {
if (_data[static_cast<size_t>(index)] == std::char_traits<wchar_t>::eof()) {
return EOF;
}

Expand Down Expand Up @@ -178,7 +175,7 @@ std::string UnbufferedCharStream::getSourceName() const {
}

std::string UnbufferedCharStream::getText(const misc::Interval &interval) {
if (interval.a < 0 || interval.b >= interval.a - 1) {
if (interval.a < 0 || interval.b < interval.a - 1) {
throw IllegalArgumentException("invalid interval");
}

Expand All @@ -202,14 +199,10 @@ std::string UnbufferedCharStream::getText(const misc::Interval &interval) {
return std::move(maybeUtf8).value();
}

size_t UnbufferedCharStream::getBufferStartIndex() const {
return _currentCharIndex - _p;
std::string UnbufferedCharStream::toString() const {
throw UnsupportedOperationException("Unbuffered stream cannot be materialized to a string");
}

void UnbufferedCharStream::InitializeInstanceFields() {
_p = 0;
_numMarkers = 0;
_lastChar = 0;
_lastCharBufferStart = 0;
_currentCharIndex = 0;
size_t UnbufferedCharStream::getBufferStartIndex() const {
return _currentCharIndex - _p;
}
30 changes: 12 additions & 18 deletions runtime/Cpp/runtime/src/UnbufferedCharStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace antlr4 {
/// The name or source of this char stream.
std::string name;

UnbufferedCharStream(std::wistream &input);
explicit UnbufferedCharStream(std::wistream &input);

virtual void consume() override;
virtual size_t LA(ssize_t i) override;
void consume() override;
size_t LA(ssize_t i) override;

/// <summary>
/// Return a marker that we can release later.
Expand All @@ -30,35 +30,32 @@ namespace antlr4 {
/// protection against misuse where {@code seek()} is called on a mark or
/// {@code release()} is called in the wrong order.
/// </summary>
virtual ssize_t mark() override;
ssize_t mark() override;

/// <summary>
/// Decrement number of markers, resetting buffer if we hit 0. </summary>
/// <param name="marker"> </param>
virtual void release(ssize_t marker) override;
virtual size_t index() override;
void release(ssize_t marker) override;
size_t index() override;

/// <summary>
/// Seek to absolute character index, which might not be in the current
/// sliding window. Move {@code p} to {@code index-bufferStartIndex}.
/// </summary>
virtual void seek(size_t index) override;
virtual size_t size() override;
virtual std::string getSourceName() const override;
virtual std::string getText(const misc::Interval &interval) override;
void seek(size_t index) override;
size_t size() override;
std::string getSourceName() const override;
std::string getText(const misc::Interval &interval) override;

std::string toString() const override;

protected:
/// A moving window buffer of the data being scanned. While there's a marker,
/// we keep adding to buffer. Otherwise, <seealso cref="#consume consume()"/> resets so
/// we start filling at index 0 again.
// UTF-32 encoded.
#if defined(_MSC_VER) && _MSC_VER == 1900
i32string _data; // Custom type for VS 2015.
typedef __int32 storage_type;
#else
std::u32string _data;
typedef char32_t storage_type;
#endif

/// <summary>
/// 0..n-1 index into <seealso cref="#data data"/> of next character.
Expand Down Expand Up @@ -115,9 +112,6 @@ namespace antlr4 {
virtual char32_t nextChar();
virtual void add(char32_t c);
size_t getBufferStartIndex() const;

private:
void InitializeInstanceFields();
};

} // namespace antlr4