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 "ProxyErrorListener.h"
12 class ANTLR4CPP_PUBLIC Recognizer {
14 #if __cplusplus >= 201703L
15 static constexpr size_t EOF = std::numeric_limits<size_t>::max();
18 EOF = static_cast<size_t>(-1), // std::numeric_limits<size_t>::max(); doesn't work in VS 2013.
23 Recognizer(Recognizer const&) = delete;
24 virtual ~Recognizer();
26 Recognizer& operator=(Recognizer const&) = delete;
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.
32 * @deprecated Use {@link #getVocabulary()} instead.
34 virtual std::vector<std::string> const& getTokenNames() const = 0;
35 virtual std::vector<std::string> const& getRuleNames() const = 0;
38 * Get the vocabulary used by the recognizer.
40 * @return A {@link Vocabulary} instance providing information about the
41 * vocabulary used by the grammar.
43 virtual dfa::Vocabulary const& getVocabulary() const;
46 /// Get a map from token names to token types.
48 /// Used for XPath and tree pattern compilation.
50 virtual std::map<std::string, size_t> getTokenTypeMap();
53 /// Get a map from rule names to rule indexes.
55 /// Used for XPath and tree pattern compilation.
57 virtual std::map<std::string, size_t> getRuleIndexMap();
59 virtual size_t getTokenType(const std::string &tokenName);
62 /// If this recognizer was generated, it will have a serialized ATN
63 /// representation of the grammar.
65 /// For interpreters, we don't know their serialized ATN despite having
66 /// created the interpreter from it.
68 virtual const std::vector<uint16_t> getSerializedATN() const {
69 throw "there is no serialized ATN";
73 /// For debugging and other purposes, might want the grammar name.
74 /// Have ANTLR generate an implementation for this method.
76 virtual std::string getGrammarFileName() const = 0;
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.
81 T* getInterpreter() const {
82 return dynamic_cast<T *>(_interpreter);
86 * Set the ATN interpreter used by the recognizer for prediction.
88 * @param interpreter The ATN interpreter used by the recognizer for
91 void setInterpreter(atn::ATNSimulator *interpreter);
93 /// What is the error header, normally line/character position information?
94 virtual std::string getErrorHeader(RecognitionException *e);
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.
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}.
109 virtual std::string getTokenErrorDisplay(Token *t);
111 /// <exception cref="NullPointerException"> if {@code listener} is {@code null}. </exception>
112 virtual void addErrorListener(ANTLRErrorListener *listener);
114 virtual void removeErrorListener(ANTLRErrorListener *listener);
116 virtual void removeErrorListeners();
118 virtual ProxyErrorListener& getErrorListenerDispatch();
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);
124 virtual bool precpred(RuleContext *localctx, int precedence);
126 virtual void action(RuleContext *localctx, size_t ruleIndex, size_t actionIndex);
128 virtual size_t getState() const ;
130 // Get the ATN used by the recognizer for prediction.
131 virtual const atn::ATN& getATN() const = 0;
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.
141 void setState(size_t atnState);
143 virtual IntStream* getInputStream() = 0;
145 virtual void setInputStream(IntStream *input) = 0;
147 virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
149 template<typename T1>
150 void setTokenFactory(TokenFactory<T1> *input);
153 atn::ATNSimulator *_interpreter; // Set and deleted in descendants (or the profiler).
155 // Mutex to manage synchronized access for multithreading.
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;
162 ProxyErrorListener _proxListener; // Manages a collection of listeners.
166 void InitializeInstanceFields();
170 } // namespace antlr4