]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/Recognizer.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / Recognizer.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 "ProxyErrorListener.h"
9
10 namespace antlr4 {
11
12   class ANTLR4CPP_PUBLIC Recognizer {
13   public:
14 #if __cplusplus >= 201703L
15     static constexpr size_t EOF = std::numeric_limits<size_t>::max();
16 #else
17     enum : size_t {
18       EOF = static_cast<size_t>(-1), // std::numeric_limits<size_t>::max(); doesn't work in VS 2013.
19     };
20 #endif
21
22     Recognizer();
23     Recognizer(Recognizer const&) = delete;
24     virtual ~Recognizer();
25
26     Recognizer& operator=(Recognizer const&) = delete;
27
28     /** Used to print out token names like ID during debugging and
29      *  error reporting.  The generated parsers implement a method
30      *  that overrides this to point to their String[] tokenNames.
31      *
32      * @deprecated Use {@link #getVocabulary()} instead.
33      */
34     virtual std::vector<std::string> const& getTokenNames() const = 0;
35     virtual std::vector<std::string> const& getRuleNames() const = 0;
36
37     /**
38      * Get the vocabulary used by the recognizer.
39      *
40      * @return A {@link Vocabulary} instance providing information about the
41      * vocabulary used by the grammar.
42      */
43     virtual dfa::Vocabulary const& getVocabulary() const;
44
45     /// <summary>
46     /// Get a map from token names to token types.
47     /// <p/>
48     /// Used for XPath and tree pattern compilation.
49     /// </summary>
50     virtual std::map<std::string, size_t> getTokenTypeMap();
51
52     /// <summary>
53     /// Get a map from rule names to rule indexes.
54     /// <p/>
55     /// Used for XPath and tree pattern compilation.
56     /// </summary>
57     virtual std::map<std::string, size_t> getRuleIndexMap();
58
59     virtual size_t getTokenType(const std::string &tokenName);
60
61     /// <summary>
62     /// If this recognizer was generated, it will have a serialized ATN
63     /// representation of the grammar.
64     /// <p/>
65     /// For interpreters, we don't know their serialized ATN despite having
66     /// created the interpreter from it.
67     /// </summary>
68     virtual const std::vector<uint16_t> getSerializedATN() const {
69       throw "there is no serialized ATN";
70     }
71
72     /// <summary>
73     /// For debugging and other purposes, might want the grammar name.
74     ///  Have ANTLR generate an implementation for this method.
75     /// </summary>
76     virtual std::string getGrammarFileName() const = 0;
77
78     /// Get the ATN interpreter (in fact one of it's descendants) used by the recognizer for prediction.
79     /// @returns The ATN interpreter used by the recognizer for prediction.
80     template <class T>
81     T* getInterpreter() const {
82       return dynamic_cast<T *>(_interpreter);
83     }
84
85     /**
86      * Set the ATN interpreter used by the recognizer for prediction.
87      *
88      * @param interpreter The ATN interpreter used by the recognizer for
89      * prediction.
90      */
91     void setInterpreter(atn::ATNSimulator *interpreter);
92
93     /// What is the error header, normally line/character position information?
94     virtual std::string getErrorHeader(RecognitionException *e);
95
96     /** How should a token be displayed in an error message? The default
97      *  is to display just the text, but during development you might
98      *  want to have a lot of information spit out.  Override in that case
99      *  to use t.toString() (which, for CommonToken, dumps everything about
100      *  the token). This is better than forcing you to override a method in
101      *  your token objects because you don't have to go modify your lexer
102      *  so that it creates a new Java type.
103      *
104      * @deprecated This method is not called by the ANTLR 4 Runtime. Specific
105      * implementations of {@link ANTLRErrorStrategy} may provide a similar
106      * feature when necessary. For example, see
107      * {@link DefaultErrorStrategy#getTokenErrorDisplay}.
108      */
109     virtual std::string getTokenErrorDisplay(Token *t);
110
111     /// <exception cref="NullPointerException"> if {@code listener} is {@code null}. </exception>
112     virtual void addErrorListener(ANTLRErrorListener *listener);
113
114     virtual void removeErrorListener(ANTLRErrorListener *listener);
115
116     virtual void removeErrorListeners();
117
118     virtual ProxyErrorListener& getErrorListenerDispatch();
119
120     // subclass needs to override these if there are sempreds or actions
121     // that the ATN interp needs to execute
122     virtual bool sempred(RuleContext *localctx, size_t ruleIndex, size_t actionIndex);
123
124     virtual bool precpred(RuleContext *localctx, int precedence);
125
126     virtual void action(RuleContext *localctx, size_t ruleIndex, size_t actionIndex);
127
128     virtual size_t getState() const ;
129
130     // Get the ATN used by the recognizer for prediction.
131     virtual const atn::ATN& getATN() const = 0;
132
133     /// <summary>
134     /// Indicate that the recognizer has changed internal state that is
135     ///  consistent with the ATN state passed in.  This way we always know
136     ///  where we are in the ATN as the parser goes along. The rule
137     ///  context objects form a stack that lets us see the stack of
138     ///  invoking rules. Combine this and we have complete ATN
139     ///  configuration information.
140     /// </summary>
141     void setState(size_t atnState);
142
143     virtual IntStream* getInputStream() = 0;
144
145     virtual void setInputStream(IntStream *input) = 0;
146
147     virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
148
149     template<typename T1>
150     void setTokenFactory(TokenFactory<T1> *input);
151
152   protected:
153     atn::ATNSimulator *_interpreter; // Set and deleted in descendants (or the profiler).
154
155     // Mutex to manage synchronized access for multithreading.
156     std::mutex _mutex;
157
158   private:
159     static std::map<const dfa::Vocabulary*, std::map<std::string, size_t>> _tokenTypeMapCache;
160     static std::map<std::vector<std::string>, std::map<std::string, size_t>> _ruleIndexMapCache;
161
162     ProxyErrorListener _proxListener; // Manages a collection of listeners.
163
164     size_t _stateNumber;
165
166     void InitializeInstanceFields();
167
168   };
169
170 } // namespace antlr4