]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/CommonTokenFactory.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / CommonTokenFactory.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 "TokenFactory.h"
9
10 namespace antlr4 {
11
12   /**
13    * This default implementation of {@link TokenFactory} creates
14    * {@link CommonToken} objects.
15    */
16   class ANTLR4CPP_PUBLIC CommonTokenFactory : public TokenFactory<CommonToken> {
17   public:
18     /**
19      * The default {@link CommonTokenFactory} instance.
20      *
21      * <p>
22      * This token factory does not explicitly copy token text when constructing
23      * tokens.</p>
24      */
25     static const std::unique_ptr<TokenFactory<CommonToken>> DEFAULT;
26
27   protected:
28     /**
29      * Indicates whether {@link CommonToken#setText} should be called after
30      * constructing tokens to explicitly set the text. This is useful for cases
31      * where the input stream might not be able to provide arbitrary substrings
32      * of text from the input after the lexer creates a token (e.g. the
33      * implementation of {@link CharStream#getText} in
34      * {@link UnbufferedCharStream} throws an
35      * {@link UnsupportedOperationException}). Explicitly setting the token text
36      * allows {@link Token#getText} to be called at any time regardless of the
37      * input stream implementation.
38      *
39      * <p>
40      * The default value is {@code false} to avoid the performance and memory
41      * overhead of copying text for every token unless explicitly requested.</p>
42      */
43     const bool copyText;
44
45   public:
46     /**
47      * Constructs a {@link CommonTokenFactory} with the specified value for
48      * {@link #copyText}.
49      *
50      * <p>
51      * When {@code copyText} is {@code false}, the {@link #DEFAULT} instance
52      * should be used instead of constructing a new instance.</p>
53      *
54      * @param copyText The value for {@link #copyText}.
55      */
56     CommonTokenFactory(bool copyText);
57
58     /**
59      * Constructs a {@link CommonTokenFactory} with {@link #copyText} set to
60      * {@code false}.
61      *
62      * <p>
63      * The {@link #DEFAULT} instance should be used instead of calling this
64      * directly.</p>
65      */
66     CommonTokenFactory();
67
68     virtual std::unique_ptr<CommonToken> create(std::pair<TokenSource*, CharStream*> source, size_t type,
69       const std::string &text, size_t channel, size_t start, size_t stop, size_t line, size_t charPositionInLine) override;
70
71     virtual std::unique_ptr<CommonToken> create(size_t type, const std::string &text) override;
72   };
73
74 } // namespace antlr4