1 #ifndef INC_Parser_hpp__
2 #define INC_Parser_hpp__
4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/Parser.hpp#2 $
11 #include <antlr/config.hpp>
16 #include <antlr/BitSet.hpp>
17 #include <antlr/TokenBuffer.hpp>
18 #include <antlr/RecognitionException.hpp>
19 #include <antlr/MismatchedTokenException.hpp>
20 #include <antlr/ASTFactory.hpp>
21 #include <antlr/ParserSharedInputState.hpp>
23 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
27 extern bool DEBUG_PARSER;
29 /** A generic ANTLR parser (LL(k) for k>=1) containing a bunch of
30 * utility routines useful at any lookahead depth. We distinguish between
31 * the LL(1) and LL(k) parsers because of efficiency. This may not be
32 * necessary in the near future.
34 * Each parser object contains the state of the parse including a lookahead
35 * cache (the form of which is determined by the subclass), whether or
36 * not the parser is in guess mode, where tokens come from, etc...
39 * During <b>guess</b> mode, the current lookahead token(s) and token type(s)
40 * cache must be saved because the token stream may not have been informed
41 * to save the token (via <tt>mark</tt>) before the <tt>try</tt> block.
42 * Guessing is started by:
44 * <li>saving the lookahead cache.
45 * <li>marking the current position in the TokenBuffer.
46 * <li>increasing the guessing level.
49 * After guessing, the parser state is restored by:
51 * <li>restoring the lookahead cache.
52 * <li>rewinding the TokenBuffer.
53 * <li>decreasing the guessing level.
57 * @see antlr.TokenBuffer
58 * @see antlr.TokenStream
59 * @see antlr.LL1Parser
60 * @see antlr.LLkParser
62 * @todo add constructors with ASTFactory.
64 class ANTLR_API Parser {
66 Parser(TokenBuffer& input)
67 : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0)
70 Parser(TokenBuffer* input)
71 : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0)
74 Parser(const ParserSharedInputState& state)
75 : inputState(state), astFactory(0), traceDepth(0)
83 /** Return the token type of the ith token of lookahead where i=1
84 * is the current token being examined by the parser (i.e., it
85 * has not been matched yet).
87 virtual int LA(unsigned int i)=0;
89 /// Return the i-th token of lookahead
90 virtual RefToken LT(unsigned int i)=0;
92 /** DEPRECATED! Specify the factory to be used during tree building. (Compulsory)
93 * Setting the factory is nowadays compulsory.
96 virtual void setASTNodeFactory( ASTFactory *factory )
100 /** Specify the factory to be used during tree building. (Compulsory)
101 * Setting the factory is nowadays compulsory.
103 virtual void setASTFactory( ASTFactory *factory )
105 astFactory = factory;
107 /** Return a pointer to the ASTFactory used.
108 * So you might use it in subsequent treewalkers or to reload AST's
111 virtual ASTFactory* getASTFactory()
115 /** Get the root AST node of the generated AST. When using a custom AST type
116 * or heterogenous AST's, you'll have to convert it to the right type
119 virtual RefAST getAST() = 0;
121 /// Return the filename of the input file.
122 virtual inline ANTLR_USE_NAMESPACE(std)string getFilename() const
124 return inputState->filename;
126 /// Set the filename of the input file (used for error reporting).
127 virtual void setFilename(const ANTLR_USE_NAMESPACE(std)string& f)
129 inputState->filename = f;
132 virtual void setInputState(ParserSharedInputState state)
136 virtual inline ParserSharedInputState getInputState() const
141 /// Get another token object from the token stream
142 virtual void consume()=0;
143 /// Consume tokens until one matches the given token
144 virtual void consumeUntil(int tokenType)
146 while (LA(1) != Token::EOF_TYPE && LA(1) != tokenType)
150 /// Consume tokens until one matches the given token set
151 virtual void consumeUntil(const BitSet& set)
153 while (LA(1) != Token::EOF_TYPE && !set.member(LA(1)))
157 /** Make sure current lookahead symbol matches token type <tt>t</tt>.
158 * Throw an exception upon mismatch, which is catch by either the
159 * error handler or by the syntactic predicate.
161 virtual void match(int t)
166 // printf("< lexer %s; c==%d\n", rname, LA(1));
167 printf("enter match(%d) with LA(1)=%d\n", t, LA(1));
174 printf("token mismatch: %d!=%d\n", LA(1), t);
176 throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), t, false, getFilename());
180 // mark token as consumed -- fetch next token deferred until LA/LT
185 virtual void matchNot(int t)
189 // Throws inverted-sense exception
190 throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), t, true, getFilename());
194 // mark token as consumed -- fetch next token deferred until LA/LT
199 /** Make sure current lookahead symbol matches the given set
200 * Throw an exception upon mismatch, which is catch by either the
201 * error handler or by the syntactic predicate.
203 virtual void match(const BitSet& b)
208 printf("enter match(bitset) with LA(1)=%d\n", LA(1));
210 if ( !b.member(LA(1)) )
215 printf("token mismatch: %d not member of bitset\n", LA(1));
217 throw MismatchedTokenException(getTokenNames(), getNumTokens(), LT(1), b, false, getFilename());
221 // mark token as consumed -- fetch next token deferred until LA/LT
226 /** Mark a spot in the input and return the position.
227 * Forwarded to TokenBuffer.
229 virtual inline unsigned int mark()
231 return inputState->getInput().mark();
233 /// rewind to a previously marked position
234 virtual inline void rewind(unsigned int pos)
236 inputState->getInput().rewind(pos);
238 /** called by the generated parser to do error recovery, override to
239 * customize the behaviour.
241 virtual void recover(const RecognitionException& ex, const BitSet& tokenSet)
244 consumeUntil(tokenSet);
247 /// Parser error-reporting function can be overridden in subclass
248 virtual void reportError(const RecognitionException& ex);
249 /// Parser error-reporting function can be overridden in subclass
250 virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
251 /// Parser warning-reporting function can be overridden in subclass
252 virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
254 /// get the token name for the token number 'num'
255 virtual const char* getTokenName(int num) const = 0;
256 /// get a vector with all token names
257 virtual const char* const* getTokenNames() const = 0;
258 /** Get the number of tokens defined.
259 * This one should be overridden in subclasses.
261 virtual int getNumTokens(void) const = 0;
263 /** Set or change the input token buffer */
264 // void setTokenBuffer(TokenBuffer<Token>* t);
266 virtual void traceIndent();
267 virtual void traceIn(const char* rname);
268 virtual void traceOut(const char* rname);
270 // void setTokenNames(const char** tokenNames_);
272 ParserSharedInputState inputState;
274 // /// AST return value for a rule is squirreled away here
277 /// AST support code; parser and treeparser delegate to this object
278 ASTFactory *astFactory;
280 // used to keep track of the indentation for the trace
283 /** Utility class which allows tracing to work even when exceptions are
286 class Tracer { /*{{{*/
291 Tracer(Parser* p,const char * t)
294 parser->traceIn(text);
298 #ifdef ANTLR_CXX_SUPPORTS_UNCAUGHT_EXCEPTION
299 // Only give trace if there's no uncaught exception..
300 if(!ANTLR_USE_NAMESPACE(std)uncaught_exception())
302 parser->traceOut(text);
305 Tracer(const Tracer&); // undefined
306 const Tracer& operator=(const Tracer&); // undefined
310 Parser(const Parser&); // undefined
311 const Parser& operator=(const Parser&); // undefined
314 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
318 #endif //INC_Parser_hpp__