From 2613a9387e8a41c7b0f8d724575b58ea5bed621c Mon Sep 17 00:00:00 2001 From: Justin King Date: Mon, 20 Dec 2021 06:51:13 -0800 Subject: [PATCH] [C++] Fix bugs in UnbufferedCharStream --- .../Cpp/runtime/src/UnbufferedCharStream.cpp | 25 ++++++---------- .../Cpp/runtime/src/UnbufferedCharStream.h | 30 ++++++++----------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp b/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp index 3993938032..fd234de099 100755 --- a/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp +++ b/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp @@ -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 } @@ -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) { @@ -101,7 +98,7 @@ size_t UnbufferedCharStream::LA(ssize_t i) { return EOF; } - if (_data[static_cast(index)] == 0xFFFF) { + if (_data[static_cast(index)] == std::char_traits::eof()) { return EOF; } @@ -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"); } @@ -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; } diff --git a/runtime/Cpp/runtime/src/UnbufferedCharStream.h b/runtime/Cpp/runtime/src/UnbufferedCharStream.h index 98cdcc6142..a65c6ca054 100755 --- a/runtime/Cpp/runtime/src/UnbufferedCharStream.h +++ b/runtime/Cpp/runtime/src/UnbufferedCharStream.h @@ -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; /// /// Return a marker that we can release later. @@ -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. /// - virtual ssize_t mark() override; + ssize_t mark() override; /// /// Decrement number of markers, resetting buffer if we hit 0. /// - virtual void release(ssize_t marker) override; - virtual size_t index() override; + void release(ssize_t marker) override; + size_t index() override; /// /// Seek to absolute character index, which might not be in the current /// sliding window. Move {@code p} to {@code index-bufferStartIndex}. /// - 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, 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 /// /// 0..n-1 index into of next character. @@ -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