package tcra.math;

/**
 * MathTypeParser is the base MathType parser that must be extended
 * to parser a specific MathType.
 */
public abstract class MathTypeParser
{
   /**
    * Does nothing. Must be overridden by subclasses. All parse
    * functions in subclasses must be destructive to the
    * StringBuilder provided.
    *
    * @param input StringBuilder to be parsed for whatever
    *              MathType the parser is built for.
    * @return MathType the parser is built for.
    * @throws MathFormatException If the StringBuilder provided
    *                             does not match the pattern
    *                             required by the parser.
    */
   public abstract MathType parse(StringBuilder input)
          throws MathFormatException;

   /**
    * Removes all leading whitespaces.
    *
    * @param input StringBuilder to be trimmed.
    */
   protected void eatWhitespace(StringBuilder input)
   {
      while(input.length() > 0 &&
           (input.charAt(0) == ' ' || input.charAt(0) == '\t'))
         input.deleteCharAt(0);
   } // end eatWhitespace(StringBuilder)

   /**
    * Returns the index of the next non-whitespace character.
    *
    * @param input String to search
    */
   protected int nextToken(String input)
   {
      int position = 0;
      while(position < input.length() &&
           (input.charAt(position) == ' ' ||
            input.charAt(position) == '\t'))
         ++position;

      if(position == input.length()) return -1;
      return position;
   } // end nextToken(String)
} // end class
