[1] | 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> |
---|
| 18 | #include <antlr/RecognitionException.hpp> |
---|
| 19 | #include <antlr/MismatchedTokenException.hpp> |
---|
| 20 | #include <antlr/ASTFactory.hpp> |
---|
| 21 | #include <antlr/ParserSharedInputState.hpp> |
---|
| 22 | |
---|
| 23 | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
---|
| 24 | namespace antlr { |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | extern bool DEBUG_PARSER; |
---|
| 28 | |
---|
| 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. |
---|
| 33 | * |
---|
| 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... |
---|
| 37 | * |
---|
| 38 | * <p> |
---|
| 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: |
---|
| 43 | * <ol> |
---|
| 44 | * <li>saving the lookahead cache. |
---|
| 45 | * <li>marking the current position in the TokenBuffer. |
---|
| 46 | * <li>increasing the guessing level. |
---|
| 47 | * </ol> |
---|
| 48 | * |
---|
| 49 | * After guessing, the parser state is restored by: |
---|
| 50 | * <ol> |
---|
| 51 | * <li>restoring the lookahead cache. |
---|
| 52 | * <li>rewinding the TokenBuffer. |
---|
| 53 | * <li>decreasing the guessing level. |
---|
| 54 | * </ol> |
---|
| 55 | * |
---|
| 56 | * @see antlr.Token |
---|
| 57 | * @see antlr.TokenBuffer |
---|
| 58 | * @see antlr.TokenStream |
---|
| 59 | * @see antlr.LL1Parser |
---|
| 60 | * @see antlr.LLkParser |
---|
| 61 | * |
---|
| 62 | * @todo add constructors with ASTFactory. |
---|
| 63 | */ |
---|
| 64 | class ANTLR_API Parser { |
---|
| 65 | protected: |
---|
| 66 | Parser(TokenBuffer& input) |
---|
| 67 | : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0) |
---|
| 68 | { |
---|
| 69 | } |
---|
| 70 | Parser(TokenBuffer* input) |
---|
| 71 | : inputState(new ParserInputState(input)), astFactory(0), traceDepth(0) |
---|
| 72 | { |
---|
| 73 | } |
---|
| 74 | Parser(const ParserSharedInputState& state) |
---|
| 75 | : inputState(state), astFactory(0), traceDepth(0) |
---|
| 76 | { |
---|
| 77 | } |
---|
| 78 | public: |
---|
| 79 | virtual ~Parser() |
---|
| 80 | { |
---|
| 81 | } |
---|
| 82 | |
---|
| 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). |
---|
| 86 | */ |
---|
| 87 | virtual int LA(unsigned int i)=0; |
---|
| 88 | |
---|
| 89 | /// Return the i-th token of lookahead |
---|
| 90 | virtual RefToken LT(unsigned int i)=0; |
---|
| 91 | |
---|
| 92 | /** DEPRECATED! Specify the factory to be used during tree building. (Compulsory) |
---|
| 93 | * Setting the factory is nowadays compulsory. |
---|
| 94 | * @see setASTFactory |
---|
| 95 | */ |
---|
| 96 | virtual void setASTNodeFactory( ASTFactory *factory ) |
---|
| 97 | { |
---|
| 98 | astFactory = factory; |
---|
| 99 | } |
---|
| 100 | /** Specify the factory to be used during tree building. (Compulsory) |
---|
| 101 | * Setting the factory is nowadays compulsory. |
---|
| 102 | */ |
---|
| 103 | virtual void setASTFactory( ASTFactory *factory ) |
---|
| 104 | { |
---|
| 105 | astFactory = factory; |
---|
| 106 | } |
---|
| 107 | /** Return a pointer to the ASTFactory used. |
---|
| 108 | * So you might use it in subsequent treewalkers or to reload AST's |
---|
| 109 | * from disk. |
---|
| 110 | */ |
---|
| 111 | virtual ASTFactory* getASTFactory() |
---|
| 112 | { |
---|
| 113 | return astFactory; |
---|
| 114 | } |
---|
| 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 |
---|
| 117 | * yourself. |
---|
| 118 | */ |
---|
| 119 | virtual RefAST getAST() = 0; |
---|
| 120 | |
---|
| 121 | /// Return the filename of the input file. |
---|
| 122 | virtual inline ANTLR_USE_NAMESPACE(std)string getFilename() const |
---|
| 123 | { |
---|
| 124 | return inputState->filename; |
---|
| 125 | } |
---|
| 126 | /// Set the filename of the input file (used for error reporting). |
---|
| 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 | |
---|
| 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) |
---|
| 145 | { |
---|
| 146 | while (LA(1) != Token::EOF_TYPE && LA(1) != tokenType) |
---|
| 147 | consume(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /// Consume tokens until one matches the given token set |
---|
| 151 | virtual void consumeUntil(const BitSet& set) |
---|
| 152 | { |
---|
| 153 | while (LA(1) != Token::EOF_TYPE && !set.member(LA(1))) |
---|
| 154 | consume(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 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. |
---|
| 160 | */ |
---|
| 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 | |
---|
| 198 | /** Make sure current lookahead symbol matches the given set |
---|
| 199 | * Throw an exception upon mismatch, which is catch by either the |
---|
| 200 | * error handler or by the syntactic predicate. |
---|
| 201 | */ |
---|
| 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 | |
---|
| 227 | /** Mark a spot in the input and return the position. |
---|
| 228 | * Forwarded to TokenBuffer. |
---|
| 229 | */ |
---|
| 230 | virtual inline unsigned int mark() |
---|
| 231 | { |
---|
| 232 | return inputState->getInput().mark(); |
---|
| 233 | } |
---|
| 234 | /// rewind to a previously marked position |
---|
| 235 | virtual inline void rewind(unsigned int pos) |
---|
| 236 | { |
---|
| 237 | inputState->getInput().rewind(pos); |
---|
| 238 | } |
---|
| 239 | /** called by the generated parser to do error recovery, override to |
---|
| 240 | * customize the behaviour. |
---|
| 241 | */ |
---|
| 242 | virtual void recover(const RecognitionException& ex, const BitSet& tokenSet) |
---|
| 243 | { |
---|
| 244 | consume(); |
---|
| 245 | consumeUntil(tokenSet); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /// Parser error-reporting function can be overridden in subclass |
---|
| 249 | virtual void reportError(const RecognitionException& ex); |
---|
| 250 | /// Parser error-reporting function can be overridden in subclass |
---|
| 251 | virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s); |
---|
| 252 | /// Parser warning-reporting function can be overridden in subclass |
---|
| 253 | virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s); |
---|
| 254 | |
---|
| 255 | /// get the token name for the token number 'num' |
---|
| 256 | virtual const char* getTokenName(int num) const = 0; |
---|
| 257 | /// get a vector with all token names |
---|
| 258 | virtual const char* const* getTokenNames() const = 0; |
---|
| 259 | /** Get the number of tokens defined. |
---|
| 260 | * This one should be overridden in subclasses. |
---|
| 261 | */ |
---|
| 262 | virtual int getNumTokens(void) const = 0; |
---|
| 263 | |
---|
| 264 | /** Set or change the input token buffer */ |
---|
| 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 | |
---|
| 273 | ParserSharedInputState inputState; |
---|
| 274 | |
---|
| 275 | // /// AST return value for a rule is squirreled away here |
---|
| 276 | // RefAST returnAST; |
---|
| 277 | |
---|
| 278 | /// AST support code; parser and treeparser delegate to this object |
---|
| 279 | ASTFactory *astFactory; |
---|
| 280 | |
---|
| 281 | // used to keep track of the indentation for the trace |
---|
| 282 | int traceDepth; |
---|
| 283 | |
---|
| 284 | /** Utility class which allows tracing to work even when exceptions are |
---|
| 285 | * thrown. |
---|
| 286 | */ |
---|
| 287 | class Tracer { /*{{{*/ |
---|
| 288 | private: |
---|
| 289 | Parser* parser; |
---|
| 290 | const char* text; |
---|
| 291 | public: |
---|
| 292 | Tracer(Parser* p,const char * t) |
---|
| 293 | : parser(p), text(t) |
---|
| 294 | { |
---|
| 295 | parser->traceIn(text); |
---|
| 296 | } |
---|
| 297 | ~Tracer() |
---|
| 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__ |
---|