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 "TokenSource.h"
9 #include "CommonTokenFactory.h"
13 /// Provides an implementation of <seealso cref="TokenSource"/> as a wrapper around a list
14 /// of <seealso cref="Token"/> objects.
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 {
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;
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).
32 const std::string sourceName;
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"/>.
41 /// This is the backing field for <seealso cref="#getTokenFactory"/> and
42 /// <seealso cref="setTokenFactory"/>.
43 TokenFactory<CommonToken> *_factory = CommonTokenFactory::DEFAULT.get();
46 /// Constructs a new <seealso cref="ListTokenSource"/> instance from the specified
47 /// collection of <seealso cref="Token"/> objects.
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;
55 ListTokenSource& operator = (const ListTokenSource& other) = delete;
58 /// Constructs a new <seealso cref="ListTokenSource"/> instance from the specified
59 /// collection of <seealso cref="Token"/> objects and source name.
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
68 /// <exception cref="NullPointerException"> if {@code tokens} is {@code null} </exception>
69 ListTokenSource(std::vector<std::unique_ptr<Token>> tokens_, const std::string &sourceName_);
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;
78 void setTokenFactory(TokenFactory<T1> *factory) {
79 this->_factory = factory;
82 virtual TokenFactory<CommonToken>* getTokenFactory() override;
85 void InitializeInstanceFields();