Class TerminalSymbol


public class TerminalSymbol extends Symbol
A terminal symbol.

Terminal symbols match some input token(s) literally.

  • Field Details

  • Constructor Details

    • TerminalSymbol

      public TerminalSymbol(Token token)
      Make a symbol for the specified token.
      Parameters:
      token - the token
      Throws:
      NullPointerException - if the token is null
    • TerminalSymbol

      public TerminalSymbol(Token token, ParserAttribute attribute)
      Make a symbol for the specified token with a given ParserAttribute.
      Parameters:
      token - the token
      attribute - the attribute
      Throws:
      NullPointerException - if either is null
      GrammarException - if the attribute attempts to make the symbol optional
    • TerminalSymbol

      public TerminalSymbol(Token token, Collection<ParserAttribute> attributes)
      Make a symbol for the specified token with the given attributes
      Parameters:
      token - the token
      attributes - a collection of attributes
      Throws:
      NullPointerException - if the token is null
      GrammarException - if the attributes attempt to make the symbol optional
  • Method Details

    • s

      public static TerminalSymbol s(String terminal)
      Return a token for a string.

      This is just a convenience method for a terminal symbol for a TokenString.

      Parameters:
      terminal - the string
      Returns:
      the terminal symbol
    • ch

      public static TerminalSymbol ch(char terminal)
      Return a token for a string.

      This is just a convenience method for a terminal symbol for a TokenCharacter.

      Parameters:
      terminal - the character
      Returns:
      the terminal symbol
    • regex

      public static TerminalSymbol regex(String regex)
      Return a token for a string.

      This is just a convenience method for a terminal symbol for a TokenRegex.

      Parameters:
      regex - the regex
      Returns:
      the terminal symbol
    • getToken

      public Token getToken()
      Get the token associated with this terminal symbol.
      Returns:
      the token
    • matches

      public boolean matches(Token input)
      Does this symbol match the given token?
      Specified by:
      matches in class Symbol
      Parameters:
      input - The token.
      Returns:
      true if it matches
    • matches

      public final boolean matches(Symbol input)
      Does this symbol match this other symbol?

      No, it does not. No terminal ever matches another symbol.

      Specified by:
      matches in class Symbol
      Parameters:
      input - The other symbol.
      Returns:
      false
    • equals

      public boolean equals(Object obj)
      Test tokens for equality.

      Two tokens are equal if they represent the same string and have the same attributes.

      Overrides:
      equals in class Object
      Parameters:
      obj - An object.
      Returns:
      true if obj is equal to this terminal character.
    • hashCode

      public int hashCode()
      Assure that equal tokens return the same hash code.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code.
    • toString

      public String toString()
      Pretty print a token.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.