Class ParserContext


  • public final class ParserContext
    extends java.lang.Object
    An object for storing the data of a currently running parsing operation. Wraps the input string and also stores the current offset that is being parsed.
    • Constructor Summary

      Constructors 
      Constructor Description
      ParserContext​(java.lang.String input)
      Initialize a new parser context from the given input string.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void advance​(int amount)
      Consume the given number of characters.
      java.lang.String chopFront​(int length)
      Consume length characters from the input string, return them in a newly constructed string.
      void commit()
      Drop the last snapshot, use the currently valid parser state.
      int indexOf​(char c)
      Returns first index of a given character in the remaining part of the input string.
      char peekChar​(int offset)
      Look-ahead at the input string.
      char peekCharUnchecked​(int offset)
      Same as peekChar(int), but doesn't check for the end of the string.
      int remainingLength()  
      void revert()
      Reverts to the last valid snapshot.
      void snapshot()
      Take a snapshot of the current state of the parser.
      boolean startsWith​(java.lang.String prefix)
      Tests whether the current state starts with the given string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ParserContext

        public ParserContext​(java.lang.String input)
        Initialize a new parser context from the given input string. Used for starting a new parser pass.
        Parameters:
        input - The input string to parse.
    • Method Detail

      • snapshot

        public void snapshot()
        Take a snapshot of the current state of the parser. Useful for handling recursion, where children could fail parsing.

        Always MUST be paired with one of revert() or commit().

      • commit

        public void commit()
        Drop the last snapshot, use the currently valid parser state.
      • revert

        public void revert()
        Reverts to the last valid snapshot.
      • peekChar

        public char peekChar​(int offset)
                      throws java.io.EOFException
        Look-ahead at the input string.
        Parameters:
        offset - The number of characters that should be skipped. E.g. 0 means look at the currently parsed character.
        Returns:
        The character at the proper offset in the input.
        Throws:
        java.io.EOFException - When the read attempts to read anything past the end of the input string.
      • peekCharUnchecked

        public char peekCharUnchecked​(int offset)
        Same as peekChar(int), but doesn't check for the end of the string. Useful in loops bounded by remainingLength() to avoid the need for a try-catch block.
        Parameters:
        offset - The offset of the character to look at.
        Returns:
        The character at the given position.
      • advance

        public void advance​(int amount)
        Consume the given number of characters.
        Parameters:
        amount - The number of characters to consider parsed.
      • remainingLength

        public int remainingLength()
        Returns:
        The remaining length of the input string.
      • chopFront

        public java.lang.String chopFront​(int length)
        Consume length characters from the input string, return them in a newly constructed string.
        Parameters:
        length - The number of characters to consume from the beginning. (The method just calls advance(int) internally with this number.)
        Returns:
        A string of length length that appears at the current position in the input.
      • startsWith

        public boolean startsWith​(java.lang.String prefix)
        Tests whether the current state starts with the given string.
        Parameters:
        prefix - The prefix we are checking for.
        Returns:
        True if the current input at the current offset starts with the given string.
      • indexOf

        public int indexOf​(char c)
        Returns first index of a given character in the remaining part of the input string.
        Parameters:
        c - The character to search for.
        Returns:
        The offset of the character from the current position, or -1 if not found.