Class Parser


  • public class Parser
    extends Object
    A generic PEG parser.
    • Method Detail

      • getPositionInfo

        public String getPositionInfo()
        Returns:
        A string showing parsing context, for debugging.
      • setState

        public Object setState​(Object state)
        Set the "state object" from the parser (can be used to parse state between parser functions).
        Parameters:
        state - The state object.
        Returns:
        The old value of the state object.
      • getState

        public Object getState()
        Get the "state object" from the parser (can be used to parse state between parser functions).
        Returns:
        The current value of the state object.
      • expect

        public void expect​(char expectedChar)
                    throws Parser.ParseException
        Expect the next character.
        Parameters:
        expectedChar - The expected character.
        Throws:
        Parser.ParseException - If the next character was not the expected character.
      • peek

        public char peek()
        Returns:
        The next character, or '\0' if at the end of the string.
      • peekExpect

        public void peekExpect​(char expectedChar)
                        throws Parser.ParseException
        Get the next character, throwing a Parser.ParseException if the next character is not the expected character.
        Parameters:
        expectedChar - The expected next character.
        Throws:
        Parser.ParseException - If the next character is not the expected next character.
      • peekMatches

        public boolean peekMatches​(String strMatch)
        Peek operator that can look ahead several characters.
        Parameters:
        strMatch - The string to compare, starting at the current position, as a "peek" operation.
        Returns:
        True if the strMatch matches a substring of the remaining string starting at the current position.
      • next

        public void next()
        Advance one character without returning the value of the character.
      • advance

        public void advance​(int numChars)
        Advance numChars character positions.
        Parameters:
        numChars - The number of character positions to advance.
        Throws:
        IllegalArgumentException - If there are insufficient characters remaining in the string.
      • hasMore

        public boolean hasMore()
        Returns:
        true if the input has not all been consumed.
      • getPosition

        public int getPosition()
        Returns:
        the current position.
      • setPosition

        public void setPosition​(int position)
        Set the position of the parser within the string.
        Parameters:
        position - The position to move to.
        Throws:
        IllegalArgumentException - If the position is out of range.
      • getSubsequence

        public CharSequence getSubsequence​(int startPosition,
                                           int endPosition)
        Return a subsequence of the input string.
        Parameters:
        startPosition - The start position.
        endPosition - The end position.
        Returns:
        The subsequence.
      • getSubstring

        public String getSubstring​(int startPosition,
                                   int endPosition)
        Return a substring of the input string.
        Parameters:
        startPosition - The start position.
        endPosition - The end position.
        Returns:
        The substring.
      • appendToToken

        public void appendToToken​(String str)
        Append the given string to the token buffer.
        Parameters:
        str - The string to append.
      • appendToToken

        public void appendToToken​(char c)
        Append the given character to the token buffer.
        Parameters:
        c - The character to append.
      • skipWhitespace

        public void skipWhitespace()
        Skip whitespace starting at the current position.
      • currToken

        public String currToken()
        Get the current token, and reset the token to empty.
        Returns:
        The current token. Resets the current token to empty.