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 "Exceptions.h"
9 #include "misc/Interval.h"
10 #include "IntStream.h"
12 #include "support/StringUtils.h"
13 #include "support/CPPUtils.h"
15 #include "ANTLRInputStream.h"
17 using namespace antlr4;
18 using namespace antlrcpp;
22 ANTLRInputStream::ANTLRInputStream() {
23 InitializeInstanceFields();
26 #if __cplusplus >= 201703L
27 ANTLRInputStream::ANTLRInputStream(const std::string_view &input): ANTLRInputStream() {
28 load(input.data(), input.length());
32 ANTLRInputStream::ANTLRInputStream(const std::string &input): ANTLRInputStream() {
33 load(input.data(), input.size());
36 ANTLRInputStream::ANTLRInputStream(const char *data, size_t length) {
40 ANTLRInputStream::ANTLRInputStream(std::istream &stream): ANTLRInputStream() {
44 void ANTLRInputStream::load(const std::string &input) {
45 load(input.data(), input.size());
48 void ANTLRInputStream::load(const char *data, size_t length) {
49 // Remove the UTF-8 BOM if present.
50 const char *bom = "\xef\xbb\xbf";
51 if (length >= 3 && strncmp(data, bom, 3) == 0)
52 _data = antlrcpp::utf8_to_utf32(data + 3, data + length);
54 _data = antlrcpp::utf8_to_utf32(data, data + length);
58 void ANTLRInputStream::load(std::istream &stream) {
59 if (!stream.good() || stream.eof()) // No fail, bad or EOF.
64 std::string s((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
65 load(s.data(), s.length());
68 void ANTLRInputStream::reset() {
72 void ANTLRInputStream::consume() {
73 if (p >= _data.size()) {
74 assert(LA(1) == IntStream::EOF);
75 throw IllegalStateException("cannot consume EOF");
78 if (p < _data.size()) {
83 size_t ANTLRInputStream::LA(ssize_t i) {
85 return 0; // undefined
88 ssize_t position = static_cast<ssize_t>(p);
90 i++; // e.g., translate LA(-1) to use offset i=0; then _data[p+0-1]
91 if ((position + i - 1) < 0) {
92 return IntStream::EOF; // invalid; no char before first char
96 if ((position + i - 1) >= static_cast<ssize_t>(_data.size())) {
97 return IntStream::EOF;
100 return _data[static_cast<size_t>((position + i - 1))];
103 size_t ANTLRInputStream::LT(ssize_t i) {
107 size_t ANTLRInputStream::index() {
111 size_t ANTLRInputStream::size() {
115 // Mark/release do nothing. We have entire buffer.
116 ssize_t ANTLRInputStream::mark() {
120 void ANTLRInputStream::release(ssize_t /* marker */) {
123 void ANTLRInputStream::seek(size_t index) {
125 p = index; // just jump; don't update stream state (line, ...)
128 // seek forward, consume until p hits index or n (whichever comes first)
129 index = std::min(index, _data.size());
135 std::string ANTLRInputStream::getText(const Interval &interval) {
136 if (interval.a < 0 || interval.b < 0) {
140 size_t start = static_cast<size_t>(interval.a);
141 size_t stop = static_cast<size_t>(interval.b);
144 if (stop >= _data.size()) {
145 stop = _data.size() - 1;
148 size_t count = stop - start + 1;
149 if (start >= _data.size()) {
153 return antlrcpp::utf32_to_utf8(_data.substr(start, count));
156 std::string ANTLRInputStream::getSourceName() const {
158 return IntStream::UNKNOWN_SOURCE_NAME;
163 std::string ANTLRInputStream::toString() const {
164 return antlrcpp::utf32_to_utf8(_data);
167 void ANTLRInputStream::InitializeInstanceFields() {