ANTLR Support Libraries 2.7.1+
Loading...
Searching...
No Matches
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
24namespace antlr {
25#endif
26
27extern bool DEBUG_PARSER;
28
65protected:
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 }
78public:
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 }
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
133 {
134 inputState = state;
135 }
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);
270protected:
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 };
310private:
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__
bool DEBUG_PARSER
Definition Parser.cpp:50
Definition ASTFactory.hpp:36
Definition BitSet.hpp:40
bool member(unsigned int el) const
Definition BitSet.cpp:40
Definition MismatchedTokenException.hpp:22
Definition ParserSharedInputState.hpp:25
Definition Parser.hpp:287
Tracer(Parser *p, const char *t)
Definition Parser.hpp:292
Parser * parser
Definition Parser.hpp:289
Tracer(const Tracer &)
const Tracer & operator=(const Tracer &)
~Tracer()
Definition Parser.hpp:297
const char * text
Definition Parser.hpp:290
Definition Parser.hpp:64
int traceDepth
Definition Parser.hpp:282
virtual void setInputState(ParserSharedInputState state)
Definition Parser.hpp:132
Parser(const ParserSharedInputState &state)
Definition Parser.hpp:74
virtual void setFilename(const std ::string &f)
Set the filename of the input file (used for error reporting).
Definition Parser.hpp:127
virtual unsigned int mark()
Definition Parser.hpp:230
virtual void match(const BitSet &b)
Definition Parser.hpp:202
virtual const char *const * getTokenNames() const =0
get a vector with all token names
virtual void consume()=0
Get another token object from the token stream.
const Parser & operator=(const Parser &)
ASTFactory * astFactory
AST support code; parser and treeparser delegate to this object.
Definition Parser.hpp:279
virtual void setASTNodeFactory(ASTFactory *factory)
Definition Parser.hpp:96
virtual int getNumTokens(void) const =0
virtual void traceOut(const char *rname)
Definition Parser.cpp:98
virtual RefAST getAST()=0
virtual void rewind(unsigned int pos)
rewind to a previously marked position
Definition Parser.hpp:235
virtual void consumeUntil(int tokenType)
Consume tokens until one matches the given token.
Definition Parser.hpp:144
virtual std::string getFilename() const
Return the filename of the input file.
Definition Parser.hpp:122
virtual ASTFactory * getASTFactory()
Definition Parser.hpp:111
virtual int LA(unsigned int i)=0
virtual ParserSharedInputState getInputState() const
Definition Parser.hpp:136
Parser(TokenBuffer &input)
Definition Parser.hpp:66
virtual const char * getTokenName(int num) const =0
get the token name for the token number 'num'
virtual RefToken LT(unsigned int i)=0
Return the i-th token of lookahead.
virtual void setASTFactory(ASTFactory *factory)
Definition Parser.hpp:103
Parser(TokenBuffer *input)
Definition Parser.hpp:70
Parser(const Parser &)
virtual ~Parser()
Definition Parser.hpp:79
virtual void consumeUntil(const BitSet &set)
Consume tokens until one matches the given token set.
Definition Parser.hpp:151
virtual void matchNot(int t)
Definition Parser.hpp:184
ParserSharedInputState inputState
Definition Parser.hpp:273
virtual void recover(const RecognitionException &ex, const BitSet &tokenSet)
Definition Parser.hpp:242
virtual void traceIn(const char *rname)
Definition Parser.cpp:85
virtual void match(int t)
Definition Parser.hpp:161
Definition RecognitionException.hpp:19
Definition TokenBuffer.hpp:36
static const int EOF_TYPE
Definition Token.hpp:32
#define ANTLR_USE_NAMESPACE(_x_)
Definition config.hpp:18
#define ANTLR_API
Definition config.hpp:22
Definition ANTLRException.hpp:15