/**
 * This class provides support methods shared by all parser code.  E.g., this
 * provides code to check end-of-file conditions, convert strings to ints,
 * etc.  Each of the functions here exists largely to automate some of the
 * grunt work out of the way so that the actual parser code can be
 * straightforward.  Largely, these functions are responsible for error
 * checking and generating appropriate exceptions.
 * <p>
 * Note that this class is a <em>package local</em>.  It's accessible to
 * anything within the package, but not to anything outside it.  These
 * functions <em>could</em> potentially have been implemented in an abstract
 * class and then inherited by concrete parse classes, but that doesn't
 * express the right relationship, b/c the various parser parts are not
 * necessarily related by inheritance and don't necessarily have a common
 * parent.
 *
 * @author Terran Lane
 * @version 1.0
 */
class ParserSupport {

  /**
   * Helper function to handle the grunt work associated with translating a
   * token to an <code>Integer</code>.  This assumes that the handed in token
   * is really supposed to be a legal int and that any other outcome is an
   * exception.
   *
   * @param t Current token from the input stream.
   * @return Translated version of token, interpreted as an int.
   * @exception TypeException If <code>t</code> cannot be interpreted as an
   * int
   * @exception PEOFException If <code>t</code> represents EOF
   */
  static Integer parseInt(Token t) throws TypeException, PEOFException {
    if (t==null) {
      throw new PEOFException("Unexpected end of input stream " +
			       "while parsing IntList");
    }
    if (t.getTokType()!=WPIToken.T_INT) {
      throw new TypeException("Unexpected token type; expecting INT, " +
			       "got '" + t + "'");
    }
    // now t _should_ be an int, if the lexer is doing its job right
    try {
      Integer i=new Integer(t.getTokStr());
      return i;
    }
    catch (NumberFormatException e) {
      throw new TypeException("Incorrectly formatted integer '" + t + "'",
			       e);
    }
  }

  /**
   * Helper function to check EOF status of a token.  Its only job is to
   * throw an exception if <code>t==null</code>.  Should only be used where
   * an EOF is a real error condition.
   *
   * @param t Token to check.
   * @exception PEOFException iff <code>t==null</code>
   */
  static void checkEOF(Token t) throws PEOFException {
    if (t==null) {
      throw new PEOFException("Unexpected end of stream wile parsing " +
			       "IntList");
    }
  }
}
