Skip to content

Commit

Permalink
More changes to the ANTLRInputStream and ANTLRFileStream classes
Browse files Browse the repository at this point in the history
- Added a default c-tor to the input stream to avoid an ambiquity.
- Changed the input stream API so that it can take a string pointer + length and use that for UTF conversion, avoiding so unnecessary copies. Convenience methods exist to use a std::string or a std::string_view.
- With that only a single load() method is necessary.
- In ANTLRFileStream the other c-tors are now also deleted, as they make no sense there.
  • Loading branch information
mike-lischke committed Mar 10, 2021
1 parent f881e3e commit 84d4ce7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
3 changes: 0 additions & 3 deletions runtime/Cpp/runtime/src/ANTLRFileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

using namespace antlr4;

ANTLRFileStream::ANTLRFileStream(): ANTLRInputStream(std::string()) {
}

void ANTLRFileStream::loadFromFile(const std::string &fileName) {
_fileName = fileName;
if (_fileName.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion runtime/Cpp/runtime/src/ANTLRFileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace antlr4 {
// TODO: this class needs testing.
class ANTLR4CPP_PUBLIC ANTLRFileStream : public ANTLRInputStream {
public:
ANTLRFileStream();
ANTLRFileStream(const std::string &) = delete;
ANTLRFileStream(const char *data, size_t length) = delete;
ANTLRFileStream(std::istream &stream) = delete;

// Assumes a file name encoded in UTF-8 and file content in the same encoding (with or w/o BOM).
virtual void loadFromFile(const std::string &fileName);
Expand Down
44 changes: 19 additions & 25 deletions runtime/Cpp/runtime/src/ANTLRInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,41 @@ using namespace antlrcpp;

using misc::Interval;

#if __cplusplus >= 201703L
ANTLRInputStream::ANTLRInputStream(std::string_view input) {
ANTLRInputStream::ANTLRInputStream() {
InitializeInstanceFields();
load(input);
}

#if __cplusplus >= 201703L
ANTLRInputStream::ANTLRInputStream(const std::string_view &input): ANTLRInputStream() {
load(input.data(), input.length());
}
#endif

ANTLRInputStream::ANTLRInputStream(const std::string &input) {
InitializeInstanceFields();
load(input);
ANTLRInputStream::ANTLRInputStream(const std::string &input): ANTLRInputStream() {
load(input.data(), input.size());
}

ANTLRInputStream::ANTLRInputStream(const char data_[], size_t numberOfActualCharsInArray)
: ANTLRInputStream(std::string(data_, numberOfActualCharsInArray)) {
ANTLRInputStream::ANTLRInputStream(const char *data, size_t length) {
load(data, length);
}

ANTLRInputStream::ANTLRInputStream(std::istream &stream) {
InitializeInstanceFields();
ANTLRInputStream::ANTLRInputStream(std::istream &stream): ANTLRInputStream() {
load(stream);
}

#if __cplusplus >= 201703L
void ANTLRInputStream::load(std::string_view input) {
// Remove the UTF-8 BOM if present.
constexpr std::string_view bom = "\xef\xbb\xbf";
if (input.compare(0, 3, bom) == 0)
input.remove_prefix(3);
_data = antlrcpp::utf8_to_utf32(input.data(), input.data() + input.size());
p = 0;
}
#else
void ANTLRInputStream::load(const std::string &input) {
load(input.data(), input.size());
}

void ANTLRInputStream::load(const char *data, size_t length) {
// Remove the UTF-8 BOM if present.
const char bom[4] = "\xef\xbb\xbf";
if (input.compare(0, 3, bom, 3) == 0)
_data = antlrcpp::utf8_to_utf32(input.data() + 3, input.data() + input.size());
if (strncmp(data, bom, 3) == 0)
_data = antlrcpp::utf8_to_utf32(data + 3, data + length);
else
_data = antlrcpp::utf8_to_utf32(input.data(), input.data() + input.size());
_data = antlrcpp::utf8_to_utf32(data, data + length);
p = 0;
}
#endif

void ANTLRInputStream::load(std::istream &stream) {
if (!stream.good() || stream.eof()) // No fail, bad or EOF.
Expand All @@ -66,7 +60,7 @@ void ANTLRInputStream::load(std::istream &stream) {
_data.clear();

std::string s((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
load(s);
load(s.data(), s.length());
}

void ANTLRInputStream::reset() {
Expand Down
14 changes: 7 additions & 7 deletions runtime/Cpp/runtime/src/ANTLRInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ namespace antlr4 {
/// What is name or source of this char stream?
std::string name;

ANTLRInputStream();

#if __cplusplus >= 201703L
ANTLRInputStream(std::string_view input = "");
ANTLRInputStream(const std::string_view &input);
#endif
ANTLRInputStream(const std::string &input = "");
ANTLRInputStream(const char data_[], size_t numberOfActualCharsInArray);

ANTLRInputStream(const std::string &input);
ANTLRInputStream(const char *data, size_t length);
ANTLRInputStream(std::istream &stream);

#if __cplusplus >= 201703L
virtual void load(std::string_view input);
#else
virtual void load(const std::string &input);
#endif
virtual void load(const char *data, size_t length);
virtual void load(std::istream &stream);

/// Reset the stream so that it's in the same state it was
Expand Down

0 comments on commit 84d4ce7

Please sign in to comment.