ANTLR Support Libraries 2.7.1+
|
00001 #ifndef INC_Parser_hpp__ 00002 #define INC_Parser_hpp__ 00003 00004 /* ANTLR Translator Generator 00005 * Project led by Terence Parr at http://www.jGuru.com 00006 * Software rights: http://www.antlr.org/license.html 00007 * 00008 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/Parser.hpp#2 $ 00009 */ 00010 00011 #include <antlr/config.hpp> 00012 00013 #include <iostream> 00014 #include <exception> 00015 00016 #include <antlr/BitSet.hpp> 00017 #include <antlr/TokenBuffer.hpp> 00018 #include <antlr/RecognitionException.hpp> 00019 #include <antlr/MismatchedTokenException.hpp> 00020 #include <antlr/ASTFactory.hpp> 00021 #include <antlr/ParserSharedInputState.hpp> 00022 00023 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE 00024 namespace antlr { 00025 #endif 00026 00027 extern bool DEBUG_PARSER; 00028 00064 class ANTLR_API Parser { 00065 protected: 00066 Parser(TokenBuffer& input) 00067 : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0) 00068 { 00069 } 00070 Parser(TokenBuffer* input) 00071 : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0) 00072 { 00073 } 00074 Parser(const ParserSharedInputState& state) 00075 : inputState(state), astFactory(0), traceDepth(0) 00076 { 00077 } 00078 public: 00079 virtual ~Parser() 00080 { 00081 } 00082 00087 virtual int LA(unsigned int i)=0; 00088 00090 virtual RefToken LT(unsigned int i)=0; 00091 00096 virtual void setASTNodeFactory( ASTFactory *factory ) 00097 { 00098 astFactory = factory; 00099 } 00103 virtual void setASTFactory( ASTFactory *factory ) 00104 { 00105 astFactory = factory; 00106 } 00111 virtual ASTFactory* getASTFactory() 00112 { 00113 return astFactory; 00114 } 00119 virtual RefAST getAST() = 0; 00120 00122 virtual inline ANTLR_USE_NAMESPACE(std)string getFilename() const 00123 { 00124 return inputState->filename; 00125 } 00127 virtual void setFilename(const ANTLR_USE_NAMESPACE(std)string& f) 00128 { 00129 inputState->filename = f; 00130 } 00131 00132 virtual void setInputState(ParserSharedInputState state) 00133 { 00134 inputState = state; 00135 } 00136 virtual inline ParserSharedInputState getInputState() const 00137 { 00138 return inputState; 00139 } 00140 00142 virtual void consume()=0; 00144 virtual void consumeUntil(int tokenType) 00145 { 00146 while (LA(1) != Token::EOF_TYPE && LA(1) != tokenType) 00147 consume(); 00148 } 00149 00151 virtual void consumeUntil(const BitSet& set) 00152 { 00153 while (LA(1) != Token::EOF_TYPE && !set.member(LA(1))) 00154 consume(); 00155 } 00156 00161 virtual void match(int t) 00162 { 00163 if ( DEBUG_PARSER ) 00164 { 00165 traceIndent(); 00166 ANTLR_USE_NAMESPACE(std)cout << "enter match(" << t << ") with LA(1)=" << LA(1) << ANTLR_USE_NAMESPACE(std)endl; 00167 } 00168 if ( LA(1) != t ) 00169 { 00170 if ( DEBUG_PARSER ) 00171 { 00172 traceIndent(); 00173 ANTLR_USE_NAMESPACE(std)cout << "token mismatch: " << LA(1) << "!=" << t << ANTLR_USE_NAMESPACE(std)endl; 00174 } 00175 throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), t, false, getFilename()); 00176 } 00177 else 00178 { 00179 // mark token as consumed -- fetch next token deferred until LA/LT 00180 consume(); 00181 } 00182 } 00183 00184 virtual void matchNot(int t) 00185 { 00186 if ( LA(1)==t ) 00187 { 00188 // Throws inverted-sense exception 00189 throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), t, true, getFilename()); 00190 } 00191 else 00192 { 00193 // mark token as consumed -- fetch next token deferred until LA/LT 00194 consume(); 00195 } 00196 } 00197 00202 virtual void match(const BitSet& b) 00203 { 00204 if ( DEBUG_PARSER ) 00205 { 00206 traceIndent(); 00207 ANTLR_USE_NAMESPACE(std)cout << "enter match(" << "bitset" /*b.toString()*/ 00208 << ") with LA(1)=" << LA(1) << ANTLR_USE_NAMESPACE(std)endl; 00209 } 00210 if ( !b.member(LA(1)) ) 00211 { 00212 if ( DEBUG_PARSER ) 00213 { 00214 traceIndent(); 00215 ANTLR_USE_NAMESPACE(std)cout << "token mismatch: " << LA(1) << " not member of " 00216 << "bitset" /*b.toString()*/ << ANTLR_USE_NAMESPACE(std)endl; 00217 } 00218 throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), b, false, getFilename()); 00219 } 00220 else 00221 { 00222 // mark token as consumed -- fetch next token deferred until LA/LT 00223 consume(); 00224 } 00225 } 00226 00230 virtual inline unsigned int mark() 00231 { 00232 return inputState->getInput().mark(); 00233 } 00235 virtual inline void rewind(unsigned int pos) 00236 { 00237 inputState->getInput().rewind(pos); 00238 } 00242 virtual void recover(const RecognitionException& ex, const BitSet& tokenSet) 00243 { 00244 consume(); 00245 consumeUntil(tokenSet); 00246 } 00247 00249 virtual void reportError(const RecognitionException& ex); 00251 virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s); 00253 virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s); 00254 00256 virtual const char* getTokenName(int num) const = 0; 00258 virtual const char* const* getTokenNames() const = 0; 00262 virtual int getNumTokens(void) const = 0; 00263 00265 // void setTokenBuffer(TokenBuffer<Token>* t); 00266 00267 virtual void traceIndent(); 00268 virtual void traceIn(const char* rname); 00269 virtual void traceOut(const char* rname); 00270 protected: 00271 // void setTokenNames(const char** tokenNames_); 00272 00273 ParserSharedInputState inputState; 00274 00275 // /// AST return value for a rule is squirreled away here 00276 // RefAST returnAST; 00277 00279 ASTFactory *astFactory; 00280 00281 // used to keep track of the indentation for the trace 00282 int traceDepth; 00283 00287 class Tracer { /*{{{*/ 00288 private: 00289 Parser* parser; 00290 const char* text; 00291 public: 00292 Tracer(Parser* p,const char * t) 00293 : parser(p), text(t) 00294 { 00295 parser->traceIn(text); 00296 } 00297 ~Tracer() 00298 { 00299 #ifdef ANTLR_CXX_SUPPORTS_UNCAUGHT_EXCEPTION 00300 // Only give trace if there's no uncaught exception.. 00301 if(!ANTLR_USE_NAMESPACE(std)uncaught_exception()) 00302 #endif 00303 parser->traceOut(text); 00304 } 00305 private: 00306 Tracer(const Tracer&); // undefined 00307 const Tracer& operator=(const Tracer&); // undefined 00308 /*}}}*/ 00309 }; 00310 private: 00311 Parser(const Parser&); // undefined 00312 const Parser& operator=(const Parser&); // undefined 00313 }; 00314 00315 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE 00316 } 00317 #endif 00318 00319 #endif //INC_Parser_hpp__