ANTLR Support Libraries 2.7.1+
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Parser.hpp
Go to the documentation of this file.
1 #ifndef INC_Parser_hpp__
2 #define INC_Parser_hpp__
3 
4 /* ANTLR Translator Generator
5  * Project led by Terence Parr at http://www.jGuru.com
6  * Software rights: http://www.antlr.org/license.html
7  *
8  * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/Parser.hpp#2 $
9  */
10 
11 #include <antlr/config.hpp>
12 
13 #include <iostream>
14 #include <exception>
15 
16 #include <antlr/BitSet.hpp>
17 #include <antlr/TokenBuffer.hpp>
20 #include <antlr/ASTFactory.hpp>
22 
23 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
24 namespace antlr {
25 #endif
26 
27 extern bool DEBUG_PARSER;
28 
65 protected:
67  : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0)
68  {
69  }
71  : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0)
72  {
73  }
75  : inputState(state), astFactory(0), traceDepth(0)
76  {
77  }
78 public:
79  virtual ~Parser()
80  {
81  }
82 
87  virtual int LA(unsigned int i)=0;
88 
90  virtual RefToken LT(unsigned int i)=0;
91 
96  virtual void setASTNodeFactory( ASTFactory *factory )
97  {
98  astFactory = factory;
99  }
103  virtual void setASTFactory( ASTFactory *factory )
104  {
105  astFactory = factory;
106  }
111  virtual ASTFactory* getASTFactory()
112  {
113  return astFactory;
114  }
119  virtual RefAST getAST() = 0;
120 
122  virtual inline ANTLR_USE_NAMESPACE(std)string getFilename() const
123  {
124  return inputState->filename;
125  }
127  virtual void setFilename(const ANTLR_USE_NAMESPACE(std)string& f)
128  {
129  inputState->filename = f;
130  }
131 
132  virtual void setInputState(ParserSharedInputState state)
133  {
134  inputState = state;
135  }
136  virtual inline ParserSharedInputState getInputState() const
137  {
138  return inputState;
139  }
140 
142  virtual void consume()=0;
144  virtual void consumeUntil(int tokenType)
145  {
146  while (LA(1) != Token::EOF_TYPE && LA(1) != tokenType)
147  consume();
148  }
149 
151  virtual void consumeUntil(const BitSet& set)
152  {
153  while (LA(1) != Token::EOF_TYPE && !set.member(LA(1)))
154  consume();
155  }
156 
161  virtual void match(int t)
162  {
163  if ( DEBUG_PARSER )
164  {
165  traceIndent();
166  ANTLR_USE_NAMESPACE(std)cout << "enter match(" << t << ") with LA(1)=" << LA(1) << ANTLR_USE_NAMESPACE(std)endl;
167  }
168  if ( LA(1) != t )
169  {
170  if ( DEBUG_PARSER )
171  {
172  traceIndent();
173  ANTLR_USE_NAMESPACE(std)cout << "token mismatch: " << LA(1) << "!=" << t << ANTLR_USE_NAMESPACE(std)endl;
174  }
175  throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), t, false, getFilename());
176  }
177  else
178  {
179  // mark token as consumed -- fetch next token deferred until LA/LT
180  consume();
181  }
182  }
183 
184  virtual void matchNot(int t)
185  {
186  if ( LA(1)==t )
187  {
188  // Throws inverted-sense exception
189  throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), t, true, getFilename());
190  }
191  else
192  {
193  // mark token as consumed -- fetch next token deferred until LA/LT
194  consume();
195  }
196  }
197 
202  virtual void match(const BitSet& b)
203  {
204  if ( DEBUG_PARSER )
205  {
206  traceIndent();
207  ANTLR_USE_NAMESPACE(std)cout << "enter match(" << "bitset" /*b.toString()*/
208  << ") with LA(1)=" << LA(1) << ANTLR_USE_NAMESPACE(std)endl;
209  }
210  if ( !b.member(LA(1)) )
211  {
212  if ( DEBUG_PARSER )
213  {
214  traceIndent();
215  ANTLR_USE_NAMESPACE(std)cout << "token mismatch: " << LA(1) << " not member of "
216  << "bitset" /*b.toString()*/ << ANTLR_USE_NAMESPACE(std)endl;
217  }
218  throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), b, false, getFilename());
219  }
220  else
221  {
222  // mark token as consumed -- fetch next token deferred until LA/LT
223  consume();
224  }
225  }
226 
230  virtual inline unsigned int mark()
231  {
232  return inputState->getInput().mark();
233  }
235  virtual inline void rewind(unsigned int pos)
236  {
237  inputState->getInput().rewind(pos);
238  }
242  virtual void recover(const RecognitionException& ex, const BitSet& tokenSet)
243  {
244  consume();
245  consumeUntil(tokenSet);
246  }
247 
249  virtual void reportError(const RecognitionException& ex);
251  virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
253  virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
254 
256  virtual const char* getTokenName(int num) const = 0;
258  virtual const char* const* getTokenNames() const = 0;
262  virtual int getNumTokens(void) const = 0;
263 
265 // void setTokenBuffer(TokenBuffer<Token>* t);
266 
267  virtual void traceIndent();
268  virtual void traceIn(const char* rname);
269  virtual void traceOut(const char* rname);
270 protected:
271 // void setTokenNames(const char** tokenNames_);
272 
274 
275 // /// AST return value for a rule is squirreled away here
276 // RefAST returnAST;
277 
280 
281  // used to keep track of the indentation for the trace
283 
287  class Tracer { /*{{{*/
288  private:
290  const char* text;
291  public:
292  Tracer(Parser* p,const char * t)
293  : parser(p), text(t)
294  {
295  parser->traceIn(text);
296  }
298  {
299 #ifdef ANTLR_CXX_SUPPORTS_UNCAUGHT_EXCEPTION
300  // Only give trace if there's no uncaught exception..
301  if(!ANTLR_USE_NAMESPACE(std)uncaught_exception())
302 #endif
303  parser->traceOut(text);
304  }
305  private:
306  Tracer(const Tracer&); // undefined
307  const Tracer& operator=(const Tracer&); // undefined
308  /*}}}*/
309  };
310 private:
311  Parser(const Parser&); // undefined
312  const Parser& operator=(const Parser&); // undefined
313 };
314 
315 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
316 }
317 #endif
318 
319 #endif //INC_Parser_hpp__