Class TerminalSymbol


  • public class TerminalSymbol
    extends Symbol
    A terminal symbol.

    Terminal symbols match some input token(s) literally.

    • Constructor Detail

      • TerminalSymbol

        public TerminalSymbol​(Token token)
        Make a symbol for the specified token.
        Parameters:
        token - the token
        Throws:
        java.lang.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:
        java.lang.NullPointerException - if either is null
        GrammarException - if the attribute attempts to make the symbol optional
      • TerminalSymbol

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

      • s

        public static TerminalSymbol s​(java.lang.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​(java.lang.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​(java.lang.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 java.lang.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 java.lang.Object
        Returns:
        the hash code.
      • toString

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