Skip to content

Commit e41e8b5

Browse files
committed
[C++] Fix bugs in UnbufferedCharStream
1 parent 5d6a782 commit e41e8b5

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

runtime/Cpp/runtime/src/UnbufferedCharStream.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ using namespace antlrcpp;
1313
using namespace antlr4;
1414
using namespace antlr4::misc;
1515

16-
UnbufferedCharStream::UnbufferedCharStream(std::wistream &input) : _input(input) {
17-
InitializeInstanceFields();
18-
16+
UnbufferedCharStream::UnbufferedCharStream(std::wistream &input)
17+
: _p(0), _numMarkers(0), _lastChar(0), _lastCharBufferStart(0), _currentCharIndex(0), _input(input) {
1918
// The vector's size is what used to be n in Java code.
2019
fill(1); // prime
2120
}
@@ -74,9 +73,7 @@ size_t UnbufferedCharStream::fill(size_t n) {
7473
}
7574

7675
char32_t UnbufferedCharStream::nextChar() {
77-
wchar_t result = 0;
78-
_input >> result;
79-
return result;
76+
return _input.get();
8077
}
8178

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

104-
if (_data[static_cast<size_t>(index)] == 0xFFFF) {
101+
if (_data[static_cast<size_t>(index)] == WEOF) {
105102
return EOF;
106103
}
107104

@@ -178,7 +175,7 @@ std::string UnbufferedCharStream::getSourceName() const {
178175
}
179176

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

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

205-
size_t UnbufferedCharStream::getBufferStartIndex() const {
206-
return _currentCharIndex - _p;
202+
std::string UnbufferedCharStream::toString() const {
203+
throw UnsupportedOperationException("Unbuffered stream cannot be materialized to a string");
207204
}
208205

209-
void UnbufferedCharStream::InitializeInstanceFields() {
210-
_p = 0;
211-
_numMarkers = 0;
212-
_lastChar = 0;
213-
_lastCharBufferStart = 0;
214-
_currentCharIndex = 0;
206+
size_t UnbufferedCharStream::getBufferStartIndex() const {
207+
return _currentCharIndex - _p;
215208
}

runtime/Cpp/runtime/src/UnbufferedCharStream.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ namespace antlr4 {
1818
/// The name or source of this char stream.
1919
std::string name;
2020

21-
UnbufferedCharStream(std::wistream &input);
21+
explicit UnbufferedCharStream(std::wistream &input);
2222

23-
virtual void consume() override;
24-
virtual size_t LA(ssize_t i) override;
23+
void consume() override;
24+
size_t LA(ssize_t i) override;
2525

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

3535
/// <summary>
3636
/// Decrement number of markers, resetting buffer if we hit 0. </summary>
3737
/// <param name="marker"> </param>
38-
virtual void release(ssize_t marker) override;
39-
virtual size_t index() override;
38+
void release(ssize_t marker) override;
39+
size_t index() override;
4040

4141
/// <summary>
4242
/// Seek to absolute character index, which might not be in the current
4343
/// sliding window. Move {@code p} to {@code index-bufferStartIndex}.
4444
/// </summary>
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;
45+
void seek(size_t index) override;
46+
size_t size() override;
47+
std::string getSourceName() const override;
48+
std::string getText(const misc::Interval &interval) override;
49+
50+
std::string toString() const override;
4951

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

6360
/// <summary>
6461
/// 0..n-1 index into <seealso cref="#data data"/> of next character.
@@ -115,9 +112,6 @@ namespace antlr4 {
115112
virtual char32_t nextChar();
116113
virtual void add(char32_t c);
117114
size_t getBufferStartIndex() const;
118-
119-
private:
120-
void InitializeInstanceFields();
121115
};
122116

123117
} // namespace antlr4

0 commit comments

Comments
 (0)