]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/ListTokenSource.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / ListTokenSource.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 "TokenSource.h"
9 #include "CommonTokenFactory.h"
10
11 namespace antlr4 {
12
13   /// Provides an implementation of <seealso cref="TokenSource"/> as a wrapper around a list
14   /// of <seealso cref="Token"/> objects.
15   ///
16   /// If the final token in the list is an <seealso cref="Token#EOF"/> token, it will be used
17   /// as the EOF token for every call to <seealso cref="#nextToken"/> after the end of the
18   /// list is reached. Otherwise, an EOF token will be created.
19   class ANTLR4CPP_PUBLIC ListTokenSource : public TokenSource {
20   protected:
21     // This list will be emptied token by token as we call nextToken().
22     // Token streams can be used to buffer tokens for a while.
23     std::vector<std::unique_ptr<Token>> tokens;
24
25   private:
26     /// <summary>
27     /// The name of the input source. If this value is {@code null}, a call to
28     /// <seealso cref="#getSourceName"/> should return the source name used to create the
29     /// the next token in <seealso cref="#tokens"/> (or the previous token if the end of
30     /// the input has been reached).
31     /// </summary>
32     const std::string sourceName;
33
34   protected:
35     /// The index into <seealso cref="#tokens"/> of token to return by the next call to
36     /// <seealso cref="#nextToken"/>. The end of the input is indicated by this value
37     /// being greater than or equal to the number of items in <seealso cref="#tokens"/>.
38     size_t i;
39
40   private:
41     /// This is the backing field for <seealso cref="#getTokenFactory"/> and
42     /// <seealso cref="setTokenFactory"/>.
43     TokenFactory<CommonToken> *_factory = CommonTokenFactory::DEFAULT.get();
44
45   public:
46     /// Constructs a new <seealso cref="ListTokenSource"/> instance from the specified
47     /// collection of <seealso cref="Token"/> objects.
48     ///
49     /// <param name="tokens"> The collection of <seealso cref="Token"/> objects to provide as a
50     /// <seealso cref="TokenSource"/>. </param>
51     /// <exception cref="NullPointerException"> if {@code tokens} is {@code null} </exception>
52     ListTokenSource(std::vector<std::unique_ptr<Token>> tokens);
53     ListTokenSource(const ListTokenSource& other) = delete;
54
55     ListTokenSource& operator = (const ListTokenSource& other) = delete;
56
57     /// <summary>
58     /// Constructs a new <seealso cref="ListTokenSource"/> instance from the specified
59     /// collection of <seealso cref="Token"/> objects and source name.
60     /// </summary>
61     /// <param name="tokens"> The collection of <seealso cref="Token"/> objects to provide as a
62     /// <seealso cref="TokenSource"/>. </param>
63     /// <param name="sourceName"> The name of the <seealso cref="TokenSource"/>. If this value is
64     /// {@code null}, <seealso cref="#getSourceName"/> will attempt to infer the name from
65     /// the next <seealso cref="Token"/> (or the previous token if the end of the input has
66     /// been reached).
67     /// </param>
68     /// <exception cref="NullPointerException"> if {@code tokens} is {@code null} </exception>
69     ListTokenSource(std::vector<std::unique_ptr<Token>> tokens_, const std::string &sourceName_);
70
71     virtual size_t getCharPositionInLine() override;
72     virtual std::unique_ptr<Token> nextToken() override;
73     virtual size_t getLine() const override;
74     virtual CharStream* getInputStream() override;
75     virtual std::string getSourceName() override;
76
77     template<typename T1>
78     void setTokenFactory(TokenFactory<T1> *factory) {
79       this->_factory = factory;
80     }
81
82     virtual TokenFactory<CommonToken>* getTokenFactory() override;
83
84   private:
85     void InitializeInstanceFields();
86   };
87
88 } // namespace antlr4