Class Token

java.lang.Object
mars.assembler.token.Token

public class Token extends Object
Represents one token in the input MIPS program. Each Token carries, along with its type and value, the position (line, column) in which its source appears in the MIPS program.
Author:
Pete Sanderson, August 2003; Sean Clarke, June 2024
  • Constructor Details

    • Token

      public Token(SourceLocation location, String literal, TokenType type, Object value)
      Create a new Token with the given information.
      Parameters:
      location - The location of this token in the source code.
      literal - The underlying fragment of source code.
      type - The type of this token.
      value - The "value" of this token, dependent on type.
      See Also:
  • Method Details

    • getType

      public TokenType getType()
      Get the type of this token.
      Returns:
      The type of this token.
    • setType

      public void setType(TokenType type)
      Modify the type of this token. This is typically used when the parsing stage of assembly reinterprets the token type, e.g. using an operator mnemonic as a label identifier.

      Note: setValue(Object) should be used alongside this method to ensure the token value remains valid.

      Parameters:
      type - The new type for this token.
    • getValue

      public Object getValue()
      Get the "value" this token represents, if any. The object returned by this method is dependent on this token's type; the documentation for each variant of TokenType indicates what to expect from this method.
      Returns:
      The token type-dependent value for this token.
    • setValue

      public void setValue(Object value)
      Modify the "value" this token represents. The new value must follow the guidelines of this token's type.
      Parameters:
      value - The new value for this token.
    • getLiteral

      public String getLiteral()
      Get the underlying fragment of source code this token was created from.
      Returns:
      The literal form of this token.
    • getLocation

      public SourceLocation getLocation()
      Get the location of this token in the source code.
      Returns:
      Name of file associated with this token.
    • toString

      public String toString()
      Get the string representation of this token. This method is equivalent to getLiteral().
      Overrides:
      toString in class Object
      Returns:
      The literal form of this token.
    • isSPIMStyleMacroParameter

      public boolean isSPIMStyleMacroParameter()
      Determine whether this token is a valid SPIM-style macro parameter. MARS-style macro parameters start with %, whereas SPIM-style macro parameters start with $. Note that register names such as $zero are not accepted.
      Returns:
      true if this token is an IDENTIFIER in the form of $ followed by at least one character, or false otherwise.
    • toOperand

      public Operand toOperand()
      Convert this token to an Operand, if possible. Only registers, integers, and characters can be converted using this method.
      Returns:
      An operand based on the value of this token, or null if the conversion fails.
    • copy

      public Token copy()
      Create a copy of this token.
      Returns:
      A new instance with identical data.