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 "TokenStream.h"
13 * This implementation of {@link TokenStream} loads tokens from a
14 * {@link TokenSource} on-demand, and places the tokens in a buffer to provide
15 * access to any previous token by index.
18 * This token stream ignores the value of {@link Token#getChannel}. If your
19 * parser requires the token stream filter tokens to only those on a particular
20 * channel, such as {@link Token#DEFAULT_CHANNEL} or
21 * {@link Token#HIDDEN_CHANNEL}, use a filtering token stream such a
22 * {@link CommonTokenStream}.</p>
24 class ANTLR4CPP_PUBLIC BufferedTokenStream : public TokenStream {
26 BufferedTokenStream(TokenSource *tokenSource);
27 BufferedTokenStream(const BufferedTokenStream& other) = delete;
29 BufferedTokenStream& operator = (const BufferedTokenStream& other) = delete;
31 virtual TokenSource* getTokenSource() const override;
32 virtual size_t index() override;
33 virtual ssize_t mark() override;
35 virtual void release(ssize_t marker) override;
37 virtual void seek(size_t index) override;
39 virtual size_t size() override;
40 virtual void consume() override;
42 virtual Token* get(size_t i) const override;
44 /// Get all tokens from start..stop inclusively.
45 virtual std::vector<Token *> get(size_t start, size_t stop);
47 virtual size_t LA(ssize_t i) override;
48 virtual Token* LT(ssize_t k) override;
50 /// Reset this token stream by setting its token source.
51 virtual void setTokenSource(TokenSource *tokenSource);
52 virtual std::vector<Token *> getTokens();
53 virtual std::vector<Token *> getTokens(size_t start, size_t stop);
56 /// Given a start and stop index, return a List of all tokens in
57 /// the token type BitSet. Return null if no tokens were found. This
58 /// method looks at both on and off channel tokens.
60 virtual std::vector<Token *> getTokens(size_t start, size_t stop, const std::vector<size_t> &types);
61 virtual std::vector<Token *> getTokens(size_t start, size_t stop, size_t ttype);
63 /// Collect all tokens on specified channel to the right of
64 /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or
65 /// EOF. If channel is -1, find any non default channel token.
66 virtual std::vector<Token *> getHiddenTokensToRight(size_t tokenIndex, ssize_t channel);
69 /// Collect all hidden tokens (any off-default channel) to the right of
70 /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL
73 virtual std::vector<Token *> getHiddenTokensToRight(size_t tokenIndex);
76 /// Collect all tokens on specified channel to the left of
77 /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
78 /// If channel is -1, find any non default channel token.
80 virtual std::vector<Token *> getHiddenTokensToLeft(size_t tokenIndex, ssize_t channel);
83 /// Collect all hidden tokens (any off-default channel) to the left of
84 /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
86 virtual std::vector<Token *> getHiddenTokensToLeft(size_t tokenIndex);
88 virtual std::string getSourceName() const override;
89 virtual std::string getText() override;
90 virtual std::string getText(const misc::Interval &interval) override;
91 virtual std::string getText(RuleContext *ctx) override;
92 virtual std::string getText(Token *start, Token *stop) override;
94 /// Get all tokens from lexer until EOF.
99 * The {@link TokenSource} from which tokens for this stream are fetched.
101 TokenSource *_tokenSource;
104 * A collection of all tokens fetched from the token source. The list is
105 * considered a complete view of the input once {@link #fetchedEOF} is set
108 std::vector<std::unique_ptr<Token>> _tokens;
111 * The index into {@link #tokens} of the current token (next token to
112 * {@link #consume}). {@link #tokens}{@code [}{@link #p}{@code ]} should be
115 * <p>This field is set to -1 when the stream is first constructed or when
116 * {@link #setTokenSource} is called, indicating that the first token has
117 * not yet been fetched from the token source. For additional information,
118 * see the documentation of {@link IntStream} for a description of
119 * Initializing Methods.</p>
121 // ml: since -1 requires to make this member signed for just this single aspect we use a member _needSetup instead.
122 // Use bool isInitialized() to find out if this stream has started reading.
126 * Indicates whether the {@link Token#EOF} token has been fetched from
127 * {@link #tokenSource} and added to {@link #tokens}. This field improves
128 * performance for the following cases:
131 * <li>{@link #consume}: The lookahead check in {@link #consume} to prevent
132 * consuming the EOF symbol is optimized by checking the values of
133 * {@link #fetchedEOF} and {@link #p} instead of calling {@link #LA}.</li>
134 * <li>{@link #fetch}: The check to prevent adding multiple EOF symbols into
135 * {@link #tokens} is trivial with this field.</li>
141 /// Make sure index {@code i} in tokens has a token.
143 /// <returns> {@code true} if a token is located at index {@code i}, otherwise
144 /// {@code false}. </returns>
145 /// <seealso cref= #get(int i) </seealso>
146 virtual bool sync(size_t i);
149 /// Add {@code n} elements to buffer.
151 /// <returns> The actual number of elements added to the buffer. </returns>
152 virtual size_t fetch(size_t n);
154 virtual Token* LB(size_t k);
156 /// Allowed derived classes to modify the behavior of operations which change
157 /// the current stream position by adjusting the target token index of a seek
158 /// operation. The default implementation simply returns {@code i}. If an
159 /// exception is thrown in this method, the current stream index should not be
162 /// For example, <seealso cref="CommonTokenStream"/> overrides this method to ensure that
163 /// the seek target is always an on-channel token.
165 /// <param name="i"> The target token index. </param>
166 /// <returns> The adjusted target token index. </returns>
167 virtual ssize_t adjustSeekIndex(size_t i);
169 virtual void setup();
172 * Given a starting index, return the index of the next token on channel.
173 * Return {@code i} if {@code tokens[i]} is on channel. Return the index of
174 * the EOF token if there are no tokens on channel between {@code i} and
177 virtual ssize_t nextTokenOnChannel(size_t i, size_t channel);
180 * Given a starting index, return the index of the previous token on
181 * channel. Return {@code i} if {@code tokens[i]} is on channel. Return -1
182 * if there are no tokens on channel between {@code i} and 0.
185 * If {@code i} specifies an index at or after the EOF token, the EOF token
186 * index is returned. This is due to the fact that the EOF token is treated
187 * as though it were on every channel.</p>
189 virtual ssize_t previousTokenOnChannel(size_t i, size_t channel);
191 virtual std::vector<Token *> filterForChannel(size_t from, size_t to, ssize_t channel);
193 bool isInitialized() const;
197 void InitializeInstanceFields();
200 } // namespace antlr4