/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
#pragma once
#include "IntStream.h"
namespace antlr4 {
///
/// An whose symbols are instances.
///
class ANTLR4CPP_PUBLIC TokenStream : public IntStream {
///
/// Get the instance associated with the value returned by
/// . This method has the same pre- and post-conditions as
/// . In addition, when the preconditions of this method
/// are met, the return value is non-null and the value of
/// {@code LT(k).getType()==LA(k)}.
///
///
public:
virtual ~TokenStream();
virtual Token* LT(ssize_t k) = 0;
///
/// Gets the at the specified {@code index} in the stream. When
/// the preconditions of this method are met, the return value is non-null.
///
/// The preconditions for this method are the same as the preconditions of
/// . If the behavior of {@code seek(index)} is
/// unspecified for the current state and given {@code index}, then the
/// behavior of this method is also unspecified.
///
/// The symbol referred to by {@code index} differs from {@code seek()} only
/// in the case of filtering streams where {@code index} lies before the end
/// of the stream. Unlike {@code seek()}, this method does not adjust
/// {@code index} to point to a non-ignored symbol.
///
/// if {code index} is less than 0
/// if the stream does not support
/// retrieving the token at the specified index
virtual Token* get(size_t index) const = 0;
/// Gets the underlying TokenSource which provides tokens for this stream.
virtual TokenSource* getTokenSource() const = 0;
///
/// Return the text of all tokens within the specified {@code interval}. This
/// method behaves like the following code (including potential exceptions
/// for violating preconditions of , but may be optimized by the
/// specific implementation.
///
///
/// TokenStream stream = ...;
/// String text = "";
/// for (int i = interval.a; i <= interval.b; i++) {
/// text += stream.get(i).getText();
/// }
///
///
/// The interval of tokens within this stream to get text
/// for.
/// The text of all tokens within the specified interval in this
/// stream.
///
/// if {@code interval} is {@code null}
virtual std::string getText(const misc::Interval &interval) = 0;
///
/// Return the text of all tokens in the stream. This method behaves like the
/// following code, including potential exceptions from the calls to
/// and , but may be
/// optimized by the specific implementation.
///
///
/// TokenStream stream = ...;
/// String text = stream.getText(new Interval(0, stream.size()));
///
///
/// The text of all tokens in the stream.
virtual std::string getText() = 0;
///
/// Return the text of all tokens in the source interval of the specified
/// context. This method behaves like the following code, including potential
/// exceptions from the call to , but may be
/// optimized by the specific implementation.
///
/// If {@code ctx.getSourceInterval()} does not return a valid interval of
/// tokens provided by this stream, the behavior is unspecified.
///
///
/// TokenStream stream = ...;
/// String text = stream.getText(ctx.getSourceInterval());
///
///
/// The context providing the source interval of tokens to get
/// text for.
/// The text of all tokens within the source interval of {@code ctx}.
virtual std::string getText(RuleContext *ctx) = 0;
///
/// Return the text of all tokens in this stream between {@code start} and
/// {@code stop} (inclusive).
///
/// If the specified {@code start} or {@code stop} token was not provided by
/// this stream, or if the {@code stop} occurred before the {@code start}
/// token, the behavior is unspecified.
///
/// For streams which ensure that the method is
/// accurate for all of its provided tokens, this method behaves like the
/// following code. Other streams may implement this method in other ways
/// provided the behavior is consistent with this at a high level.
///
///
/// TokenStream stream = ...;
/// String text = "";
/// for (int i = start.getTokenIndex(); i <= stop.getTokenIndex(); i++) {
/// text += stream.get(i).getText();
/// }
///
///
/// The first token in the interval to get text for.
/// The last token in the interval to get text for (inclusive).
/// The text of all tokens lying between the specified {@code start}
/// and {@code stop} tokens.
///
/// if this stream does not support
/// this method for the specified tokens
virtual std::string getText(Token *start, Token *stop) = 0;
};
} // namespace antlr4