]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/CommonTokenStream.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / CommonTokenStream.h
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.
4  */
5
6 #pragma once
7
8 #include "BufferedTokenStream.h"
9
10 namespace antlr4 {
11
12   /**
13    * This class extends {@link BufferedTokenStream} with functionality to filter
14    * token streams to tokens on a particular channel (tokens where
15    * {@link Token#getChannel} returns a particular value).
16    *
17    * <p>
18    * This token stream provides access to all tokens by index or when calling
19    * methods like {@link #getText}. The channel filtering is only used for code
20    * accessing tokens via the lookahead methods {@link #LA}, {@link #LT}, and
21    * {@link #LB}.</p>
22    *
23    * <p>
24    * By default, tokens are placed on the default channel
25    * ({@link Token#DEFAULT_CHANNEL}), but may be reassigned by using the
26    * {@code ->channel(HIDDEN)} lexer command, or by using an embedded action to
27    * call {@link Lexer#setChannel}.
28    * </p>
29    *
30    * <p>
31    * Note: lexer rules which use the {@code ->skip} lexer command or call
32    * {@link Lexer#skip} do not produce tokens at all, so input text matched by
33    * such a rule will not be available as part of the token stream, regardless of
34    * channel.</p>
35    */
36   class ANTLR4CPP_PUBLIC CommonTokenStream : public BufferedTokenStream {
37   public:
38     /**
39      * Constructs a new {@link CommonTokenStream} using the specified token
40      * source and the default token channel ({@link Token#DEFAULT_CHANNEL}).
41      *
42      * @param tokenSource The token source.
43      */
44     CommonTokenStream(TokenSource *tokenSource);
45
46     /**
47      * Constructs a new {@link CommonTokenStream} using the specified token
48      * source and filtering tokens to the specified channel. Only tokens whose
49      * {@link Token#getChannel} matches {@code channel} or have the
50      * {@link Token#getType} equal to {@link Token#EOF} will be returned by the
51      * token stream lookahead methods.
52      *
53      * @param tokenSource The token source.
54      * @param channel The channel to use for filtering tokens.
55      */
56     CommonTokenStream(TokenSource *tokenSource, size_t channel);
57
58     virtual Token* LT(ssize_t k) override;
59
60     /// Count EOF just once.
61     virtual int getNumberOfOnChannelTokens();
62     
63   protected:
64     /**
65      * Specifies the channel to use for filtering tokens.
66      *
67      * <p>
68      * The default value is {@link Token#DEFAULT_CHANNEL}, which matches the
69      * default channel assigned to tokens created by the lexer.</p>
70      */
71     size_t channel;
72
73     virtual ssize_t adjustSeekIndex(size_t i) override;
74
75     virtual Token* LB(size_t k) override;
76
77   };
78
79 } // namespace antlr4