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