Parser.cpp
  1  /* ANTLR Translator Generator
  2   * Project led by Terence Parr at http://www.jGuru.com
  3   * Software rights: http://www.antlr.org/license.html
  4   *
  5   * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/src/Parser.cpp#2 $
  6   */
  7  
  8  #include "antlr/Parser.hpp"
  9  
 10  #include <stdio.h>
 11  
 12  #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
 13  namespace antlr {
 14  #endif
 15  
 16  /** A generic ANTLR parser (LL(k) for k>=1) containing a bunch of
 17   * utility routines useful at any lookahead depth.  We distinguish between
 18   * the LL(1) and LL(k) parsers because of efficiency.  This may not be
 19   * necessary in the near future.
 20   *
 21   * Each parser object contains the state of the parse including a lookahead
 22   * cache (the form of which is determined by the subclass), whether or
 23   * not the parser is in guess mode, where tokens come from, etc...
 24   *
 25   * <p>
 26   * During <b>guess</b> mode, the current lookahead token(s) and token type(s)
 27   * cache must be saved because the token stream may not have been informed
 28   * to save the token (via <tt>mark</tt>) before the <tt>try</tt> block.
 29   * Guessing is started by:
 30   * <ol>
 31   * <li>saving the lookahead cache.
 32   * <li>marking the current position in the TokenBuffer.
 33   * <li>increasing the guessing level.
 34   * </ol>
 35   *
 36   * After guessing, the parser state is restored by:
 37   * <ol>
 38   * <li>restoring the lookahead cache.
 39   * <li>rewinding the TokenBuffer.
 40   * <li>decreasing the guessing level.
 41   * </ol>
 42   *
 43   * @see antlr.Token
 44   * @see antlr.TokenBuffer
 45   * @see antlr.TokenStream
 46   * @see antlr.LL1Parser
 47   * @see antlr.LLkParser
 48   */
 49  
 50  bool DEBUG_PARSER = false;
 51  
 52  /** Parser error-reporting function can be overridden in subclass */
 53  void Parser::reportError(const RecognitionException& ex)
 54  {
 55  	fprintf(stderr, "%s", (ex.toString() + "\n").c_str());
 56  }
 57  
 58  /** Parser error-reporting function can be overridden in subclass */
 59  void Parser::reportError(const ANTLR_USE_NAMESPACE(std)string& s)
 60  {
 61  	if ( getFilename()=="" )
 62  		fprintf(stderr, "%s", ("error: " + s + "\n").c_str());
 63  	else
 64  		fprintf(stderr, "%s", (getFilename() + ": error: " + s + "\n").c_str());
 65  }
 66  
 67  /** Parser warning-reporting function can be overridden in subclass */
 68  void Parser::reportWarning(const ANTLR_USE_NAMESPACE(std)string& s)
 69  {
 70      if ( getFilename()=="" )
 71          fprintf(stderr, "%s", ("warning: " + s + "\n").c_str());
 72      else
 73          fprintf(stderr, "%s", (getFilename() + ": warning: " + s + "\n").c_str());
 74  }
 75  
 76  /** Set or change the input token buffer */
 77  //	void setTokenBuffer(TokenBuffer<Token>* t);
 78  
 79  void Parser::traceIndent()
 80  {
 81  	for( int i = 0; i < traceDepth; i++ )
 82  		printf(" ");
 83  }
 84  
 85  void Parser::traceIn(const char* rname)
 86  {
 87  	traceDepth++;
 88  
 89  	for( int i = 0; i < traceDepth; i++ )
 90  		printf(" ");;
 91  
 92  	printf("%s",((ANTLR_USE_NAMESPACE(std)string)"> " + rname
 93  		+ "; LA(1)==" + LT(1)->getText()
 94  		+	((inputState->guessing>0)?" [guessing]":"")
 95  		+ "\n").c_str());
 96  }
 97  
 98  void Parser::traceOut(const char* rname)
 99  {
100  	for( int i = 0; i < traceDepth; i++ )
101          printf(" ");;
102  
103  	printf("%s",((ANTLR_USE_NAMESPACE(std)string)"< " + rname
104  		+ "; LA(1)==" + LT(1)->getText()
105  		+	((inputState->guessing>0)?" [guessing]":"")
106  		+ "\n").c_str());
107  
108  	traceDepth--;
109  }
110  
111  #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
112  }
113  #endif