Class TokenStream


  • @NotThreadSafe
    public class TokenStream
    extends Object
    A stream of tokens that can be consumed by a parser. This class is not thread-safe.
    • Method Detail

      • start

        public TokenStream start()
                          throws ParsingException
        Begin the token stream, including (if required) the tokenization of the input content.
        Returns:
        this object for easy method chaining; never null
        Throws:
        ParsingException - if an error occurs during tokenization of the content
      • initializeTokens

        protected List<TokenStream.Token> initializeTokens​(List<TokenStream.Token> tokens)
        Method to allow subclasses to pre-process the set of tokens and return the correct tokens to use. The default behavior is to simply return the supplied tokens.
        Parameters:
        tokens - the tokens
        Returns:
        list of tokens.
      • rewind

        public boolean rewind​(TokenStream.Marker marker)
        Reset the stream back to the position described by the supplied marker. This method does nothing if the mark is invalid. For example, it is not possible to advance the token stream beyond the current position.
        Parameters:
        marker - the marker
        Returns:
        true if the token stream was reset, or false if the marker was invalid
      • matches

        public boolean matches​(String expected)
                        throws IllegalStateException
        Determine if the current token matches the expected value.

        The ANY_VALUE constant can be used as a wildcard.

        Parameters:
        expected - the expected value of the current token
        Returns:
        true if the current token did match, or false if the current token did not match
        Throws:
        IllegalStateException - if this method was called before the stream was started
      • matches

        public boolean matches​(int type,
                               String expected)
                        throws IllegalStateException
        Determine if the current token matches the expected type and a value.

        The ANY_VALUE constant can be used as a wildcard.

        Parameters:
        type - the expected type of the curent token
        expected - the expected value of the current token
        Returns:
        true if the current token did match, or false if the current token did not match
        Throws:
        IllegalStateException - if this method was called before the stream was started
      • matchesAnyOf

        public boolean matchesAnyOf​(String[] options)
                             throws IllegalStateException
        Determine if the next token matches one of the supplied values.
        Parameters:
        options - the options for the value of the current token
        Returns:
        true if the current token's value did match one of the supplied options, or false otherwise
        Throws:
        IllegalStateException - if this method was called before the stream was started
      • hasNext

        public boolean hasNext()
        Determine if this stream has another token to be consumed.
        Returns:
        true if there is another token ready for consumption, or false otherwise
        Throws:
        IllegalStateException - if this method was called before the stream was started