Token
Home ] Up ] [ Token ] Parser ] Exceptions ] Results ]

 

 

Here's the Token class, which represents the known tokens for the parser to consume.

package parser;

/**
 * Class to represent a token for the parser
 * 
 * @author Bryan
 */
public class Token
{
  /**
   * The allowed token types
   */
  public static enum TYPE
  {
    UNDEFINED,
    NUMBER,                         // numeric literal
    PLUS, MINUS, MULTIPLY, DIVIDE,  // arithmetic operators
    REMAINDER, EXPONENTIATION,      // more arirthmetic operators
    OPEN_PAREN, CLOSE_PAREN,        // open and close parentheses
    END_OF_INPUT                    // Special token to flag end of input
  };

  /**
   * Returns the type of the token.
   * 
   * @return token type
   */
  public TYPE getType()
  {
    return m_type;
  }
  
  /**
   * Sets the type of the token
   * 
   * @param type The type to set
   */
  void setType(TYPE type)
  {
    m_type = type;
  }
  
  /**
   * Get the token string value
   */
  public String getValue()
  {
    return m_value;
  }
  
  /**
   * Sets the token string value
   * 
   * @param value
   */
  public void setValue(String value)
  {
    m_value = value;
  }

  /**
   * Gets the token offset within the line
   */
  public int getOffset()
  {
    return m_offset;
  }
  
  /**
   * Sets the token offset within the line
   * 
   * @param offset
   */
  public void setOffset(int offset)
  {
    m_offset = offset;
  }

  /**
   * Return a usable string
   */
  @Override
  public String toString()
  {
    return "Token " + getType() + " [" + getValue() + "]" +
           " Offset: " + getOffset();
  }
  
  //// Private data ////
  private TYPE m_type = TYPE.UNDEFINED; // The token type
  private String m_value = "";          // The token string value
  private int m_offset = 0;             // The offset of the token in the line
  
  /**
   * Main entry point, for testing
   */
  public static void main(String[] args)
  {
    Token token = new Token();
    token.setType(Token.TYPE.EXPONENTIATION);
    token.setValue("^");
    System.out.println(token);
  }
}
The page was last updated February 19, 2008